TorchLean API

NN.Runtime.Autograd.IRExec.Lowering.Shape

Shape and Concatenation IR Lowering #

Checked lowering for permutations, reshaping, flattening, concatenation, and transpose.

Concatenation along axis 0 reads each parent through a typed index and folds Tensor.concatAxisSpec over the parents; concatenation along another axis first moves that axis to the front of every parent, folds, and moves it back. Both branches use concatLeadingAxisFromInputs, and the output-shape cast is justified by concatLeadingAxisFromInputs_size_eq_sum rather than by a proof embedded in the runtime code. The nonzero-axis branch validates each parent exactly as NN.IR.Graph.permuteSomeTensor does and records that evidence in ConcatFrontInput, so the correctness proof can replay the evaluator's permutation on every parent.

Each operation has its own small lower* definition. lowerShape only dispatches on the operation kind, and the lowerShape_* equation lemmas let correctness proofs reduce a dispatch to the branch they care about without unfolding the whole dispatcher.

Checked lowering for .permute perm through a sequence of adjacent axis swaps.

Instances For

    Checked lowering for .reshape inS outS.

    Instances For

      Checked lowering for .flatten s.

      Instances For

        Build the leading-axis concat inputs for .concat 0: one typed index per parent, all sharing the tail shape rest.

        Instances For

          One validated parent of a nonzero-axis concat: its typed index, the adjacent swaps that move the concatenated axis to the front, and the resulting leading extent. The proof fields record exactly the checks the IR evaluator performs in NN.IR.Graph.permuteSomeTensor, so the lowering and the evaluator permute each parent identically.

          Instances For
            def Runtime.Autograd.IRExec.Internal.ConcatFrontInput.toInput {α : Type} [TorchLean.Storage α] [Context α] {Γ : List Spec.Shape} {permFront : Array } {restFront : Spec.Shape} (input : ConcatFrontInput α Γ permFront restFront) :
            ConcatInput α Γ restFront

            Read and permute the parent so the concatenated axis comes first.

            Instances For
              def Runtime.Autograd.IRExec.Internal.concatAxisFrontInputs {α : Type} [TorchLean.Storage α] [Context α] {Γ : List Spec.Shape} (ctx : NodeLoweringContext α Γ) (permFront : Array ) (restFront : Spec.Shape) :
              Except String (Array (ConcatFrontInput α Γ permFront restFront))

              Build the concat inputs for a nonzero axis: every parent is read through a typed index and permuted by permFront so the concatenated axis comes first, sharing the tail restFront.

              Instances For
                def Runtime.Autograd.IRExec.Internal.concatInputsForward {α : Type} [TorchLean.Storage α] [Context α] {Γ : List Spec.Shape} {rest : Spec.Shape} (inputs : Array (ConcatInput α Γ rest)) (nOut : ) (hSum : Array.foldl (fun (acc : ) (input : ConcatInput α Γ rest) => acc + input.fst) 0 inputs = nOut) (context : TorchLean.TensorPack α Γ) :

                Fold concat inputs along the leading axis into a tensor of the declared leading extent nOut.

                hSum records that the input extents add up to nOut, so the cast is justified by concatLeadingAxisFromInputs_size_eq_sum.

                Instances For

                  Checked lowering for .concat axis along an arbitrary axis.

                  Instances For

                    Checked lowering for .transpose axis₁ axis₂ through a sequence of adjacent axis swaps.

                    Instances For

                      Checked lowering for permutations, reshaping, flattening, concatenation, and transpose.

                      Instances For
                        @[simp]

                        Dispatch equation for .permute perm.

                        @[simp]

                        Dispatch equation for .reshape inS outS.

                        @[simp]

                        Dispatch equation for .flatten s.

                        @[simp]

                        Dispatch equation for .concat axis.

                        @[simp]
                        theorem Runtime.Autograd.IRExec.Internal.lowerShape_transpose {α : Type} [TorchLean.Storage α] [Context α] {Γ : List Spec.Shape} (ctx : NodeLoweringContext α Γ) (axis₁ axis₂ : ) :
                        lowerShape ctx (NN.IR.OpKind.transpose axis₁ axis₂) = lowerTranspose ctx axis₁ axis₂

                        Dispatch equation for .transpose axis₁ axis₂.