Fused native lowering for einsum #
A checked einsum allocates only its final output tensor. For each output entry, the executable kernel:
- enumerates flat indices for only the contracted logical axes;
- evaluates a precompiled row-major stride plan for each operand;
- reads every operand directly from its native array, with diagonal selection and singleton broadcasting already encoded by that plan;
- multiplies the operand values in source order; and
- sums those products into the output entry.
No broadcast, pointwise-product, permutation, or contraction tensor is materialized. The independent product tensor lives in the semantics module; the lowering module contains only the executable flat-index plan and its correctness bridge.
The executable loops need only addition, multiplication, and the scalar
literals zero and one. Their row-major order is part of the program, which
makes the same kernel available to IEEE floating-point types without
installing false algebraic instances. Stronger correctness theorems recover
the order-independent Rep.push denotation whenever the scalar operations
form additive and multiplicative monoids.
References #
The broadcast, ordered product, axis permutation, and reduction stages follow
the contraction behavior of einops v0.8.2 at commit
8e911db71f2e693a0c434b041180388c685ed06f.
Contracted axes inherit duplicate-freedom from the checked global axis list.
Verified flat indexing #
The fused kernel compiles each operand's named axes to a short list of ordinary triples:
- whether the coordinate comes from the output or contraction coordinate;
- the position of that logical axis in its source coordinate; and
- the operand's physical row-major stride.
Singleton physical dimensions contribute no term. Evaluation then performs
only coordinate projection, multiplication, addition, and one direct array
read. The helper theorems below prove that this arithmetic index is exactly
the linearization of inputCoordinateOfGlobal; all shape and broadcasting
proofs are erased from generated code.
Compile physical operand axes to source-position and input-stride triples.
The plan is built once per operand. Omitting singleton dimensions makes broadcasting free inside the scalar loop.
Instances For
Evaluate one compiled operand plan to its physical row-major array index.
Instances For
The certified physical row-major index read from one einsum operand.
This is the indexing contract shared by the generic kernel and pattern-specialized elaboration. Its value is ordinary natural-number arithmetic; the bound proof is erased from executable code.
Instances For
The executable kernel uses nested Fin.foldl loops so contraction and operand
traversal allocate no temporary finite sets or lists. The row-major traversal
itself is Semantics.coordinateSum: there is one such loop in the library, and
the lemmas below are what connect it to the big-operator semantics.
Pointwise-equal scalar kernels have equal multidimensional coordinate sums.
Every scalar kernel has the same coordinate sum over an empty coordinate space. This is the certificate used when an einsum contracts a zero-length axis, where the scalar kernels are intentionally never evaluated.
Split an ordered multiplicative fold into three contiguous operand ranges.
The factors retain their source order; only associativity and the identity laws of a monoid are used.
Move contraction-invariant factors from both ends of a coordinate sum.
Multiplication order is unchanged, so this law applies to noncommutative semirings as well as ordinary numeric scalar types.
The generic scalar product used by einsumTensor.
The public elaborator may replace this function by code generated from the checked literal. Its correctness obligation is pointwise equality with this reference implementation, so every generated loop reuses the same semantic bridge.
Instances For
The generic scalar product is the ordered finite fold of the certified input reads.
Generated kernels use this theorem as their proof boundary: optimized index
arithmetic is certified operand by operand, then the resulting scalar product
is compared with this fold without unfolding einsumInputProduct in a client
module.
Evaluate every contracted coordinate for one flat output position.
This reference executor is completely general. Literal syntax compiles it to the same nested loops after reducing the checked pattern, which removes coordinate-pair construction from the native scalar loop.
Instances For
Execute a checked einsum using the general verified output function.
For each output entry, the executor enumerates only contracted-axis coordinates, reads the corresponding values directly from every operand, preserves source order during multiplication, and accumulates the products.
Instances For
Accept a compiler-generated einsum result through an explicit proof boundary.
The elaborator supplies a specialized scalar function and an output tensor, which may have been assembled sequentially or from certified parallel chunks. Erased certificates identify both with the general executor. Keeping this boundary explicit avoids optional-argument wrappers inside generated proof terms.
Instances For
The compiler-generated einsum kernel equals the independent contraction denotation for every checked pattern.
No algebraic laws are required: the theorem compares the exact source-ordered operand fold and row-major contraction fold executed by both sides.
The general einsum executor equals the independent contraction denotation.