Skip to main content

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 tensor
  • ones(shape) - One-filled tensor
  • arange(start, stop) - Sequential values
  • full(shape, fill_value) - Constant-filled tensor

Transformation:

  • reshape(tensor, shape) - Change tensor shape
  • broadcast_to(tensor, shape) - Broadcast to shape
  • transpose(tensor, axes) - Permute axes
  • squeeze(tensor, axis) - Remove size-1 dimensions
  • expand_dims(tensor, axis) - Add size-1 dimension

Operations:

  • einsum(pattern, *tensors) - Einstein summation
  • maximum(a, b) - Element-wise maximum
  • add(a, b) - Element-wise addition
  • subtract(a, b) - Element-wise subtraction
  • multiply(a, b) - Element-wise multiplication
  • divide(a, b) - Element-wise division
  • matmul(a, b) - Matrix multiplication
  • sum(tensor, axis, keepdims) - Sum reduction
  • max(tensor, axis, keepdims) - Max reduction
  • min(tensor, axis, keepdims) - Min reduction
  • mean(tensor, axis, keepdims) - Mean reduction
  • prod(tensor, axis, keepdims) - Product reduction

Utilities:

  • eval(*tensors) - Force evaluation (MLX lazy execution)
  • step(x) - Heaviside step function
  • clip(tensor, min, max) - Clamp values
  • abs(tensor) - Absolute value
  • exp(tensor) - Exponential
  • log(tensor) - Natural logarithm
  • sqrt(tensor) - Square root
  • power(tensor, exponent) - Power operation
  • astype(tensor, dtype) - Type conversion
  • asarray(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

python_tensorlogic-0.1.0.tar.gz (111.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

python_tensorlogic-0.1.0-py3-none-any.whl (66.4 kB view details)

Uploaded Python 3

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

Hashes for python_tensorlogic-0.1.0.tar.gz
Algorithm Hash digest
SHA256 34a5ce769cba7bc66a081c89d8e9bafc546edcc0b3cfbc9b74930cecbf0c70a2
MD5 31ecec5407e3cd1f3db9499fb5545482
BLAKE2b-256 fb5077bd38f8fbfd2ac13a98c3f5d31016e60a9af88dc68ae0593ed07dce79b9

See more details on using hashes here.

File details

Details for the file python_tensorlogic-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for python_tensorlogic-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d0bacb5b5d4a65f424de2d230998e224b1516b353179aa62aed688ab8f4690a4
MD5 a72d5119b2646b7be2460d149787da11
BLAKE2b-256 03a7ab8a8ad094d902f89690d65cd916eafbcdcccaf250943e90655bf2a218b5

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page