Skip to content
Get started Guides Reference

Compiler API

from extended_einsum.language.rich_program import RichProgram

An immutable program record with:

FieldMeaning
instructionsTopologically ordered RichInstruction values
n_inputsNumber of SSA IDs reserved for external inputs
stability_modeSelected direct, scaled, or log-space lowering mode
shapesShape for every input and instruction result
tensor_formats"dense" or "sparse" for every SSA value
parameter_indicesInput SSA IDs marked as parameters
arguments_of_ssa_idDerived argument lookup
consumers_of_ssa_idDerived consumer lookup

output_ssa is the final instruction’s result ID. to_raw_program() removes rich shape, format, and stability metadata.

Each instruction contains a RichOperator and argument_ssa_ids. Input IDs come first; instruction result i has SSA ID program.n_inputs + i.

from extended_einsum.preprocess import (
FoldSameShapedOperations,
OptimizeContractionPaths,
)

Returns a rewritten program that batches compatible parallel operations.

FoldSameShapedOperations.apply_with_metadata(program)

Section titled “FoldSameShapedOperations.apply_with_metadata(program)”

Returns FoldSameShapedOperationsResult with:

  • program
  • batched_result_orders
  • non_parameter_stack_orders
  • parameter_stack_orders
  • input_axis0_orders
  • concatenated_batch_orders

Finds connected einsum components and creates depth-preserving binary plans. The pass uses sesum path search and the program’s shape metadata.

group_identical_ops_by_output_depth(program, min_group_size=2) and group_identical_ops_by_input_depth(...) expose the grouping analysis used by folding. These are advanced APIs useful when building alternative schedules.

from extended_einsum.backend_translation import translate_to_backend_program
backend_program = translate_to_backend_program(program, backend_functions)

Translation binds IR operators to backend primitives and inserts the operations required by the selected stability mode.

FieldMeaning
backend_callsPrimitive callables accepting a sequence of native arrays
call_argumentsPositions supplied to each callable
n_inputsNumber of leading runtime values treated as inputs

Input arrays occupy the first positions. Every backend call appends one value. The runtime returns the final value.

from extended_einsum.backend_translation import run_program
native_result = run_program(backend_program, native_inputs)

run_program is the interpreter used directly by NumPy and wrapped by the PyTorch and JAX compilers.