TorchLean API

NN.Runtime.Autograd.IRExec.Lowering.Primitives

IR Lowering Primitives #

Typed state, indices, and shape operations used by the checked IR lowering pass. These declarations remain under IRExec.Internal; the public lowering entry point lives in IRExec.Lowering.

@[reducible, inline]

Internal lowering state used by buildFrom.

It is a dependent pair of:

  • ss: shapes of already-lowered IR nodes,
  • ForwardData α [inShape] ss: forward closures for exactly that shape list.
Instances For

    Build a typed runtime index (Idx) for a numeric IR parent id.

    The forward executor's context is typed by [inShape] ++ ss, matching ForwardData's input-plus-node representation. mkIdx checks that:

    • id is in bounds, and
    • the context shape at that position matches the expected shape s.

    On failure, this returns a descriptive error string used directly by buildFrom.

    Instances For

      Package a typed forward closure as one node of the executable IR graph.

      Instances For
        @[simp]

        Evaluation projection for mkForwardNode.

        Internal list recursion used to track the dependent output shape of adjacent swaps.

        Instances For

          Apply adjacent swaps, represented by their axis depths, to a shape.

          Instances For

            Internal dependent recursion underlying applySwapsTensor.

            Instances For

              Apply the same adjacent-swap sequence as swapShapeBySwaps to a tensor value.

              Instances For
                @[reducible, inline]

                One typed concat input: a leading extent together with a closure that reads the tensor with that extent from the runtime context. Inputs for a nonzero concat axis permute the parent before returning it, so the closure is the common shape for every concat branch.

                Instances For

                  Concatenate typed tensors along their leading axis, folding from the first tensor.

                  The empty list yields the empty tensor with leading extent 0. This is the same fold shape as the IR evaluator's NN.IR.Graph.evalConcatLeadingAxisFold.

                  Instances For
                    theorem Runtime.Autograd.IRExec.Internal.concatLeadingAxisList_fst {α : Type} [TorchLean.Storage α] [Context α] {rest : Spec.Shape} (tensors : List ((n : ) × TorchLean.Tensor α (Spec.Shape.dim n rest))) :
                    (concatLeadingAxisList tensors).fst = List.foldl (fun (acc : ) (t : (n : ) × TorchLean.Tensor α (Spec.Shape.dim n rest)) => acc + t.fst) 0 tensors

                    The leading extent of the fold in concatLeadingAxisList is a plain sum of extents.

                    Concatenate the tensors produced by concat inputs along their leading axis.

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

                      The concatenated size reported by concatLeadingAxisFromInputs is the sum of the input extents.

                      This theorem justifies the output-shape cast in the concat lowering branches.