Einsum plans and executable checking #
This module defines the semantic data carried by a checked einsum and checks named-axis patterns against finite tensor shapes. The executable checker implements the shape behavior of einops v0.8.2:
- an input ellipsis expands to the dimensions not named explicitly;
- ellipsis dimensions are right-aligned between operands;
- dimensions attached to the same global label broadcast when either is one;
- repeated labels inside one operand must have equal dimensions and denote a diagonal; and
- output labels must occur in an input.
The mathematical semantics accepts any finite number of named labels. The pinned Python implementation caps distinct labels at 52 because it encodes them as ASCII letters, but that ceiling belongs to the encoding rather than to einsum, so nothing here enforces it.
The solitary name _ is an ordinary einsum label. It is not the skipped-axis
wildcard used by parse_shape.
CheckedEinsum is the only einsum plan structure. It stores the parsed
pattern, input shapes, and one total logical-axis length function. Its
invariants prove that each physical dimension is either that resolved length
or a singleton, and that every resolved length is supplied by an occurrence
of the axis unless all occurrences are singleton. This representation supports
symbolic Nat dimensions without adding a parallel symbolic plan.
Reference #
The surface restrictions follow einops v0.8.2, pinned at commit
8e911db71f2e693a0c434b041180388c685ed06f. Its 52-label limit comes from
einops.einops._compactify_pattern_for_einsum, which encodes labels as ASCII
letters; it is not an einsum law. Dimension broadcasting is made explicit here
because the Python front end delegates it to tensor backends.
The identity of one logical einsum dimension after ellipsis expansion.
Named labels preserve the user's full identifier. Ellipsis slots are numbered from left to right in the common right-aligned ellipsis shape.
- named
(name : String)
: EinsumAxis
A logical dimension identified by the label written in the pattern.
- ellipsis
(index : ℕ)
: EinsumAxis
One slot of the common right-aligned ellipsis dimensions.
Instances For
Instances For
Instances For
Instances For
A readable label used in concrete shape diagnostics.
Instances For
Whether an einsum expression uses only the v0.8.2-supported axis forms.
Empty expressions represent scalar tensors and are accepted. Each nonempty top-level position must contain exactly one named label or one bare ellipsis. Parentheses around a single named label are harmless, as in the reference parser, but genuine compositions, unit axes, and numeric axes are rejected.
Instances For
Whether every input and the output use the supported einsum surface.
Instances For
The number of non-ellipsis tensor dimensions written in an expression.
Instances For
The number of physical dimensions captured by one input ellipsis.
Rank checking proves that the subtraction is exact. For an expression without an ellipsis the value is zero.
Instances For
The common ellipsis rank of an einsum call.
Each operand contributes its own captured rank. Taking their maximum creates the target to which shorter ellipses are right-aligned.
Instances For
Common ellipsis slots occupied by an operand with the given local rank.
Instances For
Expanding an ellipsis never introduces duplicate logical axes.
Expand one supported expression to logical labels.
ellipsisRank is the local captured rank for an input and the common rank for
the output. Unsupported composites contribute no labels; checked patterns
prove that this fallback is unreachable.
Instances For
Expanded logical labels for every input operand.
Instances For
Expanded logical labels of the output tensor.
Instances For
All logical labels in first-occurrence order.
Repeated input labels are collapsed here, but remain present in each operand's expanded list where they impose diagonal indexing.
Instances For
Every physical input dimension paired with its expanded logical label.
Instances For
The broadcast dimension of one logical label.
The first non-singleton occurrence determines the result; if every occurrence is singleton, the result is one. Compatibility checking proves that every other occurrence is either singleton or equal to this dimension. In particular, a zero dimension combined with singleton dimensions resolves to zero rather than one.
Instances For
The concrete output, in the axis order written by the user.
Instances For
Whether repeated occurrences of a label inside one operand have equal physical dimensions.
Instances For
The Boolean repeated-label check is exactly pairwise equality of dimensions for equal labels.
Whether every axis in left occurs in right.
Instances For
The Boolean einsum-axis subset test exactly expresses list containment.
Concrete input dimensions are singleton or equal to their resolved logical dimension.
Instances For
Every resolved logical-axis length is justified by the input dimensions.
A length may be 1 when all occurrences are singleton. Otherwise one
physical occurrence supplies the resolved length. Combined with
EinsumInputDimensions, this rules out inventing a larger broadcast result
for an all-singleton axis.
Instances For
Every operand satisfies the exact repeated-label dimension rule.
Instances For
An einsum call together with the invariants needed by tensor semantics and lowering.
No normalized axes or shapes are stored independently: all are computed from
pattern and inputShapes, and these fields certify those computations.
- pattern : Syntax.EinsumPattern
Parsed surface pattern accepted by the einsum checker.
Physical operand shapes in source order.
- axisLength : EinsumAxis → ℕ
Resolved length of each logical named or ellipsis axis.
The pattern contains exactly one input expression per operand shape.
Every input and output expression belongs to the supported einsum grammar.
- repeated_dimensions : EinsumRepeatedDimensions self.pattern self.inputShapes
Repeated labels within one operand always index equal-sized dimensions.
- input_dimensions : EinsumInputDimensions self.pattern self.inputShapes self.axisLength
Every physical dimension is its logical broadcast length or a singleton.
Every non-singleton logical length is witnessed by an input occurrence.
- output_axes_nodup : (einsumOutputAxes self.pattern self.inputShapes).Nodup
Output labels are unique after ellipsis expansion.
- output_axes_known : einsumAxesSubset (einsumOutputAxes self.pattern self.inputShapes) (einsumGlobalAxes self.pattern self.inputShapes) = true
Every output label is supplied by at least one input operand.
Instances For
Expanded logical labels for every checked input tensor.
Instances For
Expanded logical labels of the checked output tensor.
Instances For
Distinct logical labels over which a complete assignment ranges.
Instances For
Global logical axes summed out because they are absent from the output.
Instances For
The global logical-axis list contains each label exactly once.
The inferred shape of the checked output tensor.
Instances For
Every checked output label belongs to the global input-label list.
Check an einsum pattern against the concrete shapes of all input tensors.
Errors identify operand-count, surface, rank, repeated-label, output-label, and broadcast failures separately. A successful value carries the facts used by the independent contraction semantics.