Skip to content
Get started Guides Reference

Backend API

from extended_einsum.backend_translation import (
BackendArray,
BackendCompiler,
BackendFunctions,
BackendProgram,
)

A protocol requiring a .shape property.

A structural protocol. Implementations provide these static methods:

CategoryMethods
Gradientstop_gradient
Transcendentalexp, log
Reductionssum, max, min
Elementwisemaximum, add, subtract, multiply, divide
Shapereshape, broadcast_to
Compositionstack, concat
Routingtake, select, slice
Tensor operationssoftmax, einsum

Reduction axes accept int | tuple[int, ...] | None and a keepdims flag. These exact semantics are necessary for the broadcast scales created by stable translation.

Validates and specializes a BackendProgram, returning a callable that takes a sequence of native input arrays.

BackendFunctionsCompiler behavior
PyTorchTorchBackendFunctionstorch.compile(partial(run_program, program))
JAXJaxBackendFunctionsjax.jit(...).trace(inputs).lower().compile()
NumPyNumpyBackendFunctionsInterprets with run_program
from extended_einsum.backends.registry import (
BACKEND_TO_COMPILER,
BACKEND_TO_FUNCTIONS,
)

TensorExpression.materialize() looks up both mappings using the expression’s .backend value.

get_backend_of_array(array) recognizes torch.Tensor, numpy.ndarray, and jax.Array, returning "torch", "numpy", or "jax". Unsupported array types raise ValueError.

See write a custom backend for an implementation checklist and registration example.