TorchLean API

NN.Runtime.Autograd.Model.Program

Operation-Polymorphic Tensor Programs #

Program is model code abstract over a tensor-operation interpreter. Supplying the eager interpreter executes operations immediately and records a dynamic tape; supplying the typed-graph interpreter records reusable shape-indexed SSA data.

The execution-polymorphic surface shared by eager and typed-graph execution.

Operation-reference notation #

@[reducible, inline]
abbrev Runtime.Autograd.Model.RefTy (m : TypeType) (α : Type) [TorchLean.Storage α] [Context α] [Ops m α] (s : Spec.Shape) :

A tensor reference under the currently selected operation interpreter.

Instances For
    def Runtime.Autograd.Model.LeadingAxis.Internal.mapOuterAxis {α : Type} [TorchLean.Storage α] [Context α] {m : TypeType} [Monad m] [Ops m α] {batch : } {s t : Spec.Shape} (x : RefTy m α (s.prependDim batch)) (f : RefTy m α sm (RefTy m α t)) :
    m (RefTy m α (t.prependDim batch))

    Apply a single-sample operation independently along a leading axis.

    Instances For

      Prefix-polymorphic derived operations #

      def Runtime.Autograd.Model.mapLeading {α : Type} [TorchLean.Storage α] [Context α] {m : TypeType} [Monad m] [Ops m α] (leadingShape : Spec.Shape) {s t : Spec.Shape} (x : RefTy m α (leadingShape.concat s)) (f : RefTy m α sm (RefTy m α t)) :
      m (RefTy m α (leadingShape.concat t))

      Apply a single-sample operation independently over an arbitrary prefix shape.

      Instances For
        def Runtime.Autograd.Model.linearEach {α : Type} [TorchLean.Storage α] [Context α] {m : TypeType} [Monad m] [Ops m α] {leadingShape : Spec.Shape} {inDim outDim : } (weight : RefTy m α [outDim, inDim]) (bias : RefTy m α [outDim]) (input : RefTy m α (leadingShape.concat [inDim])) :
        m (RefTy m α (leadingShape.concat [outDim]))

        Affine transformation of the final axis, independently over any prefix shape.

        Instances For
          def Runtime.Autograd.Model.conv {α : Type} [TorchLean.Storage α] [Context α] {m : TypeType} [Monad m] [Ops m α] {leadingShape : Spec.Shape} {d inC outC : } {kernel stride padding inSpatial : TorchLean.Tensor [d]} (weight : RefTy m α (Spec.Shape.ofList (outC :: inC :: kernel.to (List )))) (bias : RefTy m α [outC]) (input : RefTy m α (leadingShape.concat (Spec.Shape.ofList (inC :: inSpatial.to (List ))))) :
          m (RefTy m α (leadingShape.concat (Spec.Shape.ofList (outC :: (Spec.convOutSpatial inSpatial kernel stride padding).to (List )))))

          Rank-polymorphic convolution over channels-first inputs with any prefix shape.

          Instances For
            def Runtime.Autograd.Model.convTranspose {α : Type} [TorchLean.Storage α] [Context α] {m : TypeType} [Monad m] [Ops m α] {leadingShape : Spec.Shape} {d inC outC : } {kernel stride padding inSpatial : TorchLean.Tensor [d]} (weight : RefTy m α (Spec.Shape.ofList (inC :: outC :: kernel.to (List )))) (bias : RefTy m α [outC]) (input : RefTy m α (leadingShape.concat (Spec.Shape.ofList (inC :: inSpatial.to (List ))))) :
            m (RefTy m α (leadingShape.concat (Spec.Shape.ofList (outC :: (Spec.convTransposeOutSpatial inSpatial kernel stride padding).to (List )))))

            Rank-polymorphic transpose convolution over channels-first inputs with any prefix shape.

            Instances For
              def Runtime.Autograd.Model.maxPool {α : Type} [TorchLean.Storage α] [Context α] {m : TypeType} [Monad m] [Ops m α] {leadingShape : Spec.Shape} {d channels : } {inSpatial kernel stride padding : TorchLean.Tensor [d]} (input : RefTy m α (leadingShape.concat (Spec.Shape.ofList (channels :: inSpatial.to (List ))))) :
              m (RefTy m α (leadingShape.concat (Spec.Shape.ofList (channels :: (Spec.poolOutSpatialPad inSpatial kernel stride padding).to (List )))))

              Rank-polymorphic max pooling over channels-first inputs with any prefix shape.

              Instances For
                def Runtime.Autograd.Model.avgPool {α : Type} [TorchLean.Storage α] [Context α] {m : TypeType} [Monad m] [Ops m α] {leadingShape : Spec.Shape} {d channels : } {inSpatial kernel stride padding : TorchLean.Tensor [d]} (input : RefTy m α (leadingShape.concat (Spec.Shape.ofList (channels :: inSpatial.to (List ))))) :
                m (RefTy m α (leadingShape.concat (Spec.Shape.ofList (channels :: (Spec.poolOutSpatialPad inSpatial kernel stride padding).to (List )))))

                Rank-polymorphic average pooling over channels-first inputs with any prefix shape.

                Instances For
                  def Runtime.Autograd.Model.smoothMaxPool {α : Type} [TorchLean.Storage α] [Context α] [DecidableEq α] {m : TypeType} [Monad m] [Ops m α] {leadingShape : Spec.Shape} {d channels : } {inSpatial kernel stride padding : TorchLean.Tensor [d]} (input : RefTy m α (leadingShape.concat (Spec.Shape.ofList (channels :: inSpatial.to (List ))))) (temp : α) :
                  m (RefTy m α (leadingShape.concat (Spec.Shape.ofList (channels :: (Spec.poolOutSpatialPad inSpatial kernel stride padding).to (List )))))

                  Batched rank-polymorphic smooth maximum pooling over channels-first inputs.

                  Instances For
                    def Runtime.Autograd.Model.layerNorm {α : Type} [TorchLean.Storage α] [Context α] {m : TypeType} [Monad m] [Ops m α] {leading : Spec.Shape} {width : } (hWidth : width > 0) (x : RefTy m α (leading.appendDim width)) (gamma beta : RefTy m α [width]) (epsilon : α := TorchLean.normalizationEpsilon) :
                    m (RefTy m α (leading.appendDim width))

                    Layer normalization over the final axis of an arbitrary tensor.

                    Backends expose a matrix-shaped fused primitive. This wrapper gives that primitive its rank-polymorphic TorchLean interface by flattening the leading axes once and restoring them after the operation. Empty leading axes produce the unique empty tensor without invoking a backend kernel that requires a positive row count.

                    Instances For
                      def Runtime.Autograd.Model.multiHeadAttention {α : Type} [TorchLean.Storage α] [Context α] {m : TypeType} [Monad m] [Ops m α] {leadingShape : Spec.Shape} {n numHeads dModel headDim : } (hN : n 0) (wq wk wv : RefTy m α [dModel, numHeads * headDim]) (wo : RefTy m α [numHeads * headDim, dModel]) (x : RefTy m α (leadingShape.concat [n, dModel])) (mask : Option (TorchLean.Tensor Bool [n, n]) := none) :
                      m (RefTy m α (leadingShape.concat [n, dModel]))

                      Multi-head self-attention over any prefix shape.

                      Instances For
                        def Runtime.Autograd.Model.multiHeadAttentionOutputBias {α : Type} [TorchLean.Storage α] [Context α] {m : TypeType} [Monad m] [Ops m α] {leadingShape : Spec.Shape} {n numHeads dModel headDim : } (hN : n 0) (wq wk wv : RefTy m α [dModel, numHeads * headDim]) (wo : RefTy m α [numHeads * headDim, dModel]) (bo : RefTy m α [dModel]) (x : RefTy m α (leadingShape.concat [n, dModel])) (mask : Option (TorchLean.Tensor Bool [n, n]) := none) :
                        m (RefTy m α (leadingShape.concat [n, dModel]))

                        Multi-head attention followed by a trainable output-feature bias.

                        Instances For
                          @[reducible, inline]

                          An execution-polymorphic differentiable tensor program.

                          Instances For
                            @[reducible, inline]

                            An execution-polymorphic program with differentiable tensors followed by non-differentiable data tensors. The data element type is explicit; for example, token models may use Fin vocab so an out-of-range token is unrepresentable.

                            Instances For