TorchLean API

NN.API.Runtime.Autograd

Runtime Autograd Helpers #

Model-shaped and function-shaped differentiation helpers over TorchLean runtime programs.

@[reducible, inline]
abbrev NN.API.TorchLean.Autodiff.Model.Params {σ τ : Spec.Shape} (model : NN.Seq σ τ) (α : Type) :

Parameter pack type for a given model (a TensorPack over Seq.paramShapes).

Instances For
    @[reducible, inline]

    Loss function over a model output and a target.

    This is expressed in terms of RefTy so it works uniformly for eager execution and compiled execution.

    Instances For
      def NN.API.TorchLean.Autodiff.Model.initParamsWith {σ τ : Spec.Shape} (model : NN.Seq σ τ) {α : Type} (cast : Floatα) :
      Params model α

      Initialize model parameters by casting the model's Float initializers elementwise using cast.

      Instances For
        def NN.API.TorchLean.Autodiff.Model.initParams {σ τ : Spec.Shape} (model : NN.Seq σ τ) {α : Type} [Runtime.Scalar α] :
        Params model α

        Initialize model parameters using the runtime literal injection API.Runtime.ofFloat.

        Instances For
          def NN.API.TorchLean.Autodiff.Model.linearParams {α : Type} {inDim outDim seedW seedB : } (w : Spec.Tensor α (Tensor.Shape.Mat outDim inDim)) (b : Spec.Tensor α (Tensor.Shape.Vec outDim)) :
          Params (Layers.linear inDim outDim seedW seedB) α

          Pack explicit weight and bias tensors for a single Layers.linear model.

          Instances For

            Mean-squared error loss (mse) between yhat and y.

            Instances For

              Cross-entropy loss between logits and one-hot targets. PyTorch analogue: nn.CrossEntropyLoss.

              Instances For

                Detach the model output before feeding it into a loss.

                This is useful when you want to compute a metric loss without backpropagating through it.

                Instances For

                  Build a TorchLean Program that computes a scalar loss from (params, x, target).

                  This is the bridge between Seq.program (which produces model outputs) and the autograd entry points (which expect a scalar-valued program).

                  Instances For
                    def NN.API.TorchLean.Autodiff.Model.vjpParams {σ τ : Spec.Shape} (model : NN.Seq σ τ) {α : Type} [Context α] [DecidableEq Spec.Shape] (params : Params model α) (x : Spec.Tensor α σ) (seedOut : Spec.Tensor α τ) :
                    IO (Params model α)

                    VJP of the model output w.r.t. parameters.

                    Instances For
                      def NN.API.TorchLean.Autodiff.Model.vjpInputs {σ τ : Spec.Shape} (model : NN.Seq σ τ) {α : Type} [Context α] [DecidableEq Spec.Shape] (params : Params model α) (x : Spec.Tensor α σ) (seedOut : Spec.Tensor α τ) :

                      VJP of the model output w.r.t. inputs.

                      Instances For
                        def NN.API.TorchLean.Autodiff.Model.jacrevParams {σ τ : Spec.Shape} (model : NN.Seq σ τ) {α : Type} [Context α] [DecidableEq Spec.Shape] (params : Params model α) (x : Spec.Tensor α σ) :
                        IO (Array (Params model α))

                        Jacobian (reverse-mode) of the model output w.r.t. parameters, returned as rows.

                        Instances For
                          def NN.API.TorchLean.Autodiff.Model.gradParams {σ τ υ : Spec.Shape} (model : NN.Seq σ τ) (loss : OutputLoss τ υ) {α : Type} [Context α] [DecidableEq Spec.Shape] (params : Params model α) (x : Spec.Tensor α σ) (target : Spec.Tensor α υ) :
                          IO (Params model α)

                          Gradient of loss(model(params, x), target) w.r.t. parameters.

                          Instances For
                            def NN.API.TorchLean.Autodiff.Model.gradInputs {σ τ υ : Spec.Shape} (model : NN.Seq σ τ) (loss : OutputLoss τ υ) {α : Type} [Context α] [DecidableEq Spec.Shape] (params : Params model α) (x : Spec.Tensor α σ) (target : Spec.Tensor α υ) :
                            IO (TensorPack α [σ, υ])

                            Gradient of loss(model(params, x), target) w.r.t. inputs (x and target).

                            Instances For
                              def NN.API.TorchLean.Autodiff.Model.jvpParams {σ τ υ : Spec.Shape} (model : NN.Seq σ τ) (loss : OutputLoss τ υ) {α : Type} [Context α] [DecidableEq Spec.Shape] (params : Params model α) (x : Spec.Tensor α σ) (target : Spec.Tensor α υ) (vparams : Params model α) :
                              IO α

                              JVP of a scalar loss w.r.t. parameters in direction vparams.

                              Instances For
                                def NN.API.TorchLean.Autodiff.Model.hvpParams {σ τ υ : Spec.Shape} (model : NN.Seq σ τ) (loss : OutputLoss τ υ) {α : Type} [Context α] [DecidableEq Spec.Shape] (params : Params model α) (x : Spec.Tensor α σ) (target : Spec.Tensor α υ) (vparams : Params model α) :
                                IO (Params model α)

                                HVP (Hessian-vector product) of a scalar loss w.r.t. parameters in direction vparams.

                                Instances For
                                  @[reducible, inline]

                                  Type of a pure tensor function expressed in RefTy form.

                                  This matches the calling convention expected by TorchLean.Program/autodiff compilation.

                                  Instances For

                                    Turn an Fn into a single-input TorchLean Program.

                                    Instances For

                                      Forward-mode Jacobian (rows) of a pure function.

                                      Instances For

                                        Hessian for a scalar-valued pure function.

                                        Instances For