TorchLean API

NN.Tensor.Internal.Check.Einsum

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:

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

    A readable label used in concrete shape diagnostics.

    Instances For
      @[implicit_reducible]

      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
        @[implicit_reducible]

        Whether every input and the output use the supported einsum surface.

        Instances For
          @[implicit_reducible]

          The number of non-ellipsis tensor dimensions written in an expression.

          Instances For
            @[implicit_reducible]

            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
              @[implicit_reducible]

              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
                @[implicit_reducible]

                Common ellipsis slots occupied by an operand with the given local rank.

                Instances For
                  theorem TorchLean.Tensor.Internal.Check.expandedEllipsisAxes_nodup (commonRank operandRank : ) :
                  (expandedEllipsisAxes commonRank operandRank).Nodup

                  Expanding an ellipsis never introduces duplicate logical axes.

                  @[implicit_reducible]

                  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
                    @[implicit_reducible]

                    Expanded logical labels for every input operand.

                    Instances For
                      @[implicit_reducible]

                      Expanded logical labels of the output tensor.

                      Instances For
                        @[implicit_reducible]

                        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
                          @[implicit_reducible]

                          Every physical input dimension paired with its expanded logical label.

                          Instances For
                            @[implicit_reducible]

                            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
                              @[implicit_reducible]

                              The concrete output, in the axis order written by the user.

                              Instances For
                                @[implicit_reducible]

                                Whether repeated occurrences of a label inside one operand have equal physical dimensions.

                                Instances For
                                  theorem TorchLean.Tensor.Internal.Check.repeatedEinsumDimensionsAgree_eq_true_iff {axes : List EinsumAxis} {inputShape : Shape} :
                                  repeatedEinsumDimensionsAgree axes inputShape = true leftaxes.zip inputShape, rightaxes.zip inputShape, left.1 = right.1left.2 = right.2

                                  The Boolean repeated-label check is exactly pairwise equality of dimensions for equal labels.

                                  @[implicit_reducible]

                                  Whether every axis in left occurs in right.

                                  Instances For
                                    theorem TorchLean.Tensor.Internal.Check.einsumAxesSubset_eq_true_iff {left right : List EinsumAxis} :
                                    einsumAxesSubset left right = true ∀ ⦃axis : EinsumAxis⦄, axis leftaxis right

                                    The Boolean einsum-axis subset test exactly expresses list containment.

                                    @[reducible, inline]

                                    Concrete input dimensions are singleton or equal to their resolved logical dimension.

                                    Instances For
                                      @[implicit_reducible]

                                      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
                                        @[reducible, inline]

                                        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.

                                          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.

                                                      Instances For