Skip to main content

extended-einsum

CI PyPI Python License: MIT

extended-einsum builds tensor-expression graphs, rewrites contraction programs, and executes them with numerically stable evaluation strategies. PyTorch is the primary backend; NumPy is included in the base installation and JAX is optional.

The package is an expression engine, not a drop-in replacement for torch.einsum or numpy.einsum. Wrap backend arrays with xe.array, compose operations lazily, then call materialize.

Installation

Python 3.12, 3.13, and 3.14 are supported.

pip install extended-einsum

Optional features are installed explicitly:

pip install "extended-einsum[jax]"
pip install "extended-einsum[jax-cuda]"
pip install "extended-einsum[visualization]"

The CUDA extra follows JAX's CUDA 13 installation. PyTorch device support is determined by the PyTorch build available from your configured package index.

Quickstart

import torch
import extended_einsum as xe

left_torch = torch.tensor([[1.0, 2.0], [3.0, 4.0]])
right_torch = torch.tensor([[2.0, 0.0], [1.0, 2.0]])

left = xe.array(left_torch)
right = xe.array(right_torch)
expression = xe.einsum("ik,kj->ij", xe.exp(left), right)
result = expression.materialize(stability_mode="unstable")

assert torch.allclose(
    result.backend_array,
    torch.einsum("ik,kj->ij", torch.exp(left_torch), right_torch),
)

The same interface accepts NumPy arrays. With the jax extra installed it also accepts JAX arrays:

import numpy as np
import extended_einsum as xe

source = xe.array(np.arange(6.0).reshape(2, 3))
result = xe.softmax(source, axis=1).materialize()
print(result.backend_array)

Expressions and stability

The public interface currently includes:

  • array, einsum, stack, take, slice, and select
  • exp, log, sin, cos, tan, sqrt, inverse, and softmax
  • TensorExpression.materialize and extract_program

Supported execution backends are PyTorch, NumPy, and optional JAX. Rich programs can be evaluated in unstable, scaled_min, scaled_sum, logspace_min, or logspace_max mode. Support is operator-dependent; unsupported combinations raise NotImplementedError instead of silently changing semantics.

Custom execution backends plug in through extended_einsum.register_backend: subclass extended_einsum.BackendFunctions (only a small set of primitives is abstract; derived operations such as softmax, select, and the arithmetic operators have defaults composed from them), then register it under a name, optionally with a compiler and an is_array predicate for automatic backend detection in xe.array. Without a compiler, programs are interpreted call by call; without a predicate, wrap arrays with xe.array(data, backend="yourname"). Validate an implementation with extended_einsum.testing.check_backend, which runs every operator through every stability mode against the NumPy reference backend.

The preprocessing API also provides expression folding and contraction-path optimization. DAG plotting is available from extended_einsum.visualization when the visualization extra is installed.

Examples

All introductory scripts in examples/ are standalone: copy one into another project with extended-einsum installed and run it from any working directory.

python examples/torch_quickstart.py
python examples/numpy_backend.py
python examples/stable_materialization.py
python examples/jax_backend.py                    # requires [jax]
python examples/visualize_expression.py graph.png # requires [visualization]

The advanced demo/cirkit.py integration is also standalone, but requires the repository demo dependencies and currently Python 3.12 or 3.13 because of Cirkit's SciPy constraint. Its defaults use a small synthetic circuit; training and benchmark sweeps are opt-in. Generated benchmark data and plots are repository artifacts and are never included in distributions.

Development

This project uses uv:

uv sync --group dev --group demo --extra jax --extra visualization
uv run ruff check .
uv run pytest
uv build

See CONTRIBUTING.md for development details and PUBLISHING.md for the release process.

License

Copyright © 2026 FSU Theoretical Computer Science II. Distributed under the MIT License.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

extended_einsum-1.1.1.tar.gz (60.6 kB view details)

Uploaded Source

Built Distribution

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

extended_einsum-1.1.1-py3-none-any.whl (71.5 kB view details)

Uploaded Python 3

File details

Details for the file extended_einsum-1.1.1.tar.gz.

File metadata

  • Download URL: extended_einsum-1.1.1.tar.gz
  • Upload date:
  • Size: 60.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.13

File hashes

Hashes for extended_einsum-1.1.1.tar.gz
Algorithm Hash digest
SHA256 93b6dacc2fabf2ecb5090683d01067016388087e8bebe1dcd53f6a78cb0a1e34
MD5 45b8252e47a33645391392046fad5de5
BLAKE2b-256 5a7cf433ec5a09b258ecabe45c4069986c09a0bc4826a22702527397362ad985

See more details on using hashes here.

Provenance

The following attestation bundles were made for extended_einsum-1.1.1.tar.gz:

Publisher: publish.yml on ti2-group/extended_einsum

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file extended_einsum-1.1.1-py3-none-any.whl.

File metadata

  • Download URL: extended_einsum-1.1.1-py3-none-any.whl
  • Upload date:
  • Size: 71.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.13

File hashes

Hashes for extended_einsum-1.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 ae41a72f6f2453a3b33fcba50e672c03eb62235777d0f33058e0c390fd049607
MD5 e537c49d6d54e8864dc15d8dd4c0c205
BLAKE2b-256 9ba4423e799ce28a7faa51953001ba1de25cda3e0064068ef7626567af6417ca

See more details on using hashes here.

Provenance

The following attestation bundles were made for extended_einsum-1.1.1-py3-none-any.whl:

Publisher: publish.yml on ti2-group/extended_einsum

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Release history Release notifications | RSS feed

This release

1.1.1 This release

2 files

1.1.0

2 files

1.0.0

2 files

0.1.0

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page