Neural-symbolic AI framework unifying logical reasoning and tensor computation
Project description
TensorLogic
Neural-symbolic AI framework unifying logical reasoning and tensor computation. Bridge neural networks and symbolic reasoning through tensor operations based on Pedro Domingos' Tensor Logic paper (arXiv:2510.12269).
Quick Start
Installation
Basic Installation (NumPy backend):
uv add tensorlogic
Recommended (MLX backend for Apple Silicon):
uv add tensorlogic mlx>=0.30.0
Basic Usage
from tensorlogic.backends import create_backend
# Default: try MLX, fallback to NumPy
backend = create_backend()
# Create tensors
x = backend.zeros((2, 3))
y = backend.ones((2, 3))
# Tensor operations
result = backend.einsum('ij,ij->ij', x, y)
backend.eval(result) # Force evaluation (critical for MLX)
Backend System
TensorLogic uses a minimal Protocol-based abstraction (~25-30 operations) supporting multiple tensor frameworks:
- MLX Backend (Primary): GPU/Apple Silicon optimized with lazy evaluation
- NumPy Backend (Fallback): Universal CPU compatibility
Backend Selection
from tensorlogic.backends import create_backend
# Automatic selection (MLX → NumPy fallback)
backend = create_backend()
# Explicit NumPy backend
numpy_backend = create_backend("numpy")
# Explicit MLX backend (raises if unavailable)
mlx_backend = create_backend("mlx")
Lazy Evaluation (MLX)
MLX uses lazy evaluation - operations are not computed until explicitly evaluated:
backend = create_backend("mlx")
# These operations are lazy - not computed yet
a = backend.ones((100, 100))
b = backend.zeros((100, 100))
result = backend.einsum('ij,jk->ik', a, b)
# Force evaluation
backend.eval(result) # Now computed
Backend Protocol
All backends implement the TensorBackend Protocol with these operations:
Creation:
zeros(shape)- Zero-filled tensorones(shape)- One-filled tensorarange(start, stop)- Sequential valuesfull(shape, fill_value)- Constant-filled tensor
Transformation:
reshape(tensor, shape)- Change tensor shapebroadcast_to(tensor, shape)- Broadcast to shapetranspose(tensor, axes)- Permute axessqueeze(tensor, axis)- Remove size-1 dimensionsexpand_dims(tensor, axis)- Add size-1 dimension
Operations:
einsum(pattern, *tensors)- Einstein summationmaximum(a, b)- Element-wise maximumadd(a, b)- Element-wise additionsubtract(a, b)- Element-wise subtractionmultiply(a, b)- Element-wise multiplicationdivide(a, b)- Element-wise divisionmatmul(a, b)- Matrix multiplicationsum(tensor, axis, keepdims)- Sum reductionmax(tensor, axis, keepdims)- Max reductionmin(tensor, axis, keepdims)- Min reductionmean(tensor, axis, keepdims)- Mean reductionprod(tensor, axis, keepdims)- Product reduction
Utilities:
eval(*tensors)- Force evaluation (MLX lazy execution)step(x)- Heaviside step functionclip(tensor, min, max)- Clamp valuesabs(tensor)- Absolute valueexp(tensor)- Exponentiallog(tensor)- Natural logarithmsqrt(tensor)- Square rootpower(tensor, exponent)- Power operationastype(tensor, dtype)- Type conversionasarray(data)- Convert to tensor
Development
Running Tests
# All tests
uv run pytest tests/test_backends/
# With coverage
uv run pytest tests/test_backends/ --cov=tensorlogic.backends --cov-report=html
# Single test file
uv run pytest tests/test_backends/test_mlx.py
# Specific test
uv run pytest tests/test_backends/test_mlx.py::test_einsum_matrix_multiply
Type Checking
# Strict type checking
uv run mypy --strict src/tensorlogic/backends/
# Current status: 0 errors
Code Quality
# Linting
uv run ruff check .
# Formatting
uv run ruff format .
Project Status
Current Phase: Backend Implementation Complete
Completed:
- TensorBackend Protocol definition
- NumPy backend implementation (100% coverage)
- MLX backend implementation (100% coverage)
- Factory pattern with validation
- Cross-backend validation tests
- Performance benchmarks
- Production readiness (99% coverage, 0 mypy errors)
Next Phase: Core Logic Implementation (CORE-001)
- Logical operations (AND, OR, NOT, IMPLIES)
- Quantifiers (EXISTS, FORALL)
- Temperature-controlled reasoning
Documentation
- Backend API:
docs/backends/API.md(comprehensive API reference) - Architecture:
.sage/agent/system/architecture.md - Tech Stack:
.sage/agent/system/tech-stack.md - Original Paper: arXiv:2510.12269 (Domingos, 2025)
License
MIT License
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file python_tensorlogic-0.1.0.tar.gz.
File metadata
- Download URL: python_tensorlogic-0.1.0.tar.gz
- Upload date:
- Size: 111.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
34a5ce769cba7bc66a081c89d8e9bafc546edcc0b3cfbc9b74930cecbf0c70a2
|
|
| MD5 |
31ecec5407e3cd1f3db9499fb5545482
|
|
| BLAKE2b-256 |
fb5077bd38f8fbfd2ac13a98c3f5d31016e60a9af88dc68ae0593ed07dce79b9
|
File details
Details for the file python_tensorlogic-0.1.0-py3-none-any.whl.
File metadata
- Download URL: python_tensorlogic-0.1.0-py3-none-any.whl
- Upload date:
- Size: 66.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d0bacb5b5d4a65f424de2d230998e224b1516b353179aa62aed688ab8f4690a4
|
|
| MD5 |
a72d5119b2646b7be2460d149787da11
|
|
| BLAKE2b-256 |
03a7ab8a8ad094d902f89690d65cd916eafbcdcccaf250943e90655bf2a218b5
|