TorchLean API

NN.Runtime.Autograd.Torch.Utils

Torch Utils #

Helpers for writing PyTorch-style training loops on top of Runtime.Autograd.Torch.

This file focuses on training-loop ergonomics:

Stateful optimizer loops live in Runtime.Autograd.TorchLean, because those depend on TorchLean.Optim. Keeping that dependency out of this low-level utility module prevents the session/ref layer from depending upward on the model/optimizer API surface.

Initialization helpers (Float constants) #

Deterministic initialization schemes stored as Float constants.

PyTorch comparison:

  • .zeros / .ones correspond to torch.nn.init.zeros_ / torch.nn.init.ones_
  • .uniform lo hi corresponds to torch.nn.init.uniform_ (with explicit a=lo, b=hi)
  • .xavierUniform fanIn fanOut corresponds to torch.nn.init.xavier_uniform_
  • .kaimingUniform fanIn corresponds to torch.nn.init.kaiming_uniform_ with nonlinearity="relu"

References:

Instances For

    SplitMix64 mixing used by indexed parameter initialization.

    Instances For

      Deterministic U[0,1) sampler derived independently from a seed and scalar index.

      Indexing is constant-time, so constructing an n-element tensor takes O(n) sampler work. The same key/index formula is used by TorchLean's storage-first runtime initializer.

      Instances For

        Sample the idx-th scalar of a tensor initialized using Scheme.

        This is the scalar-level primitive used by Init.tensor.

        Instances For

          Create a Tensor Float s by sampling a Scheme deterministically.

          This pure initializer is convenient for model definitions and reproducible examples. Runtime initialization paths can provide more specialized allocation strategies for very large tensors.

          PyTorch comparison: this mimics using torch.nn.init.* routines on freshly allocated parameters, but here we work with pure Tensor Float s values (no mutation) and use a deterministic seeded sampler for reproducibility.

          Instances For
            Instances For

              Xavier/Glorot-uniform initializer for 2D weight matrices.

              PyTorch comparison: torch.nn.init.xavier_uniform_ with gain=1.

              Instances For

                Kaiming/He-uniform initializer for 2D weight matrices.

                PyTorch comparison: torch.nn.init.kaiming_uniform_ with nonlinearity="relu" and default parameters (so the bound is sqrt(6/fan_in)).

                Instances For

                  Conveniences for scalar training loops #

                  @[reducible, inline]

                  Extract the scalar value from a scalar-shaped tensor.

                  PyTorch comparison: like t.item() for a 0-dim tensor.

                  Instances For
                    def Runtime.Autograd.Torch.tlistSingleton {α : Type} {s₁ : Spec.Shape} (x₁ : Spec.Tensor α s₁) :
                    TList α [s₁]

                    Build a one-element TList (useful for curried trainer APIs).

                    Instances For

                      TList syntax sugar #

                      Build a TList from a comma-separated list of terms.

                      This is meant for training code where tlistSingleton/tlistPair/… becomes tedious.

                      Example:

                      let xs : TList Float [.dim 2 .scalar, .dim 1 .scalar] :=
                        tlist![x, y]
                      
                      Instances For
                        def Runtime.Autograd.Torch.tlistPair {α : Type} {s₁ s₂ : Spec.Shape} (x₁ : Spec.Tensor α s₁) (x₂ : Spec.Tensor α s₂) :
                        TList α [s₁, s₂]

                        Build a two-element TList (useful for curried trainer APIs).

                        Instances For
                          def Runtime.Autograd.Torch.tlistTriple {α : Type} {s₁ s₂ s₃ : Spec.Shape} (x₁ : Spec.Tensor α s₁) (x₂ : Spec.Tensor α s₂) (x₃ : Spec.Tensor α s₃) :
                          TList α [s₁, s₂, s₃]

                          Build a three-element TList (useful for curried trainer APIs).

                          Instances For
                            def Runtime.Autograd.Torch.tlistQuad {α : Type} {s₁ s₂ s₃ s₄ : Spec.Shape} (x₁ : Spec.Tensor α s₁) (x₂ : Spec.Tensor α s₂) (x₃ : Spec.Tensor α s₃) (x₄ : Spec.Tensor α s₄) :
                            TList α [s₁, s₂, s₃, s₄]

                            Build a four-element TList (useful for curried trainer APIs).

                            Instances For
                              def Runtime.Autograd.Torch.ScalarTrainer.forwardT {α : Type} {paramShapes inputShapes natInputShapes : List Spec.Shape} (tr : ScalarTrainer α paramShapes inputShapes natInputShapes) (xs : TList α inputShapes) (natInputs : TList natInputShapes) :

                              Uncurried forward pass for ScalarTrainer.

                              ScalarTrainer.forward is stored as a curried function over the input shapes; this helper lets you pass a TList (like a tuple of tensors).

                              Instances For
                                def Runtime.Autograd.Torch.ScalarTrainer.lossAndBackwardT {α : Type} {paramShapes inputShapes natInputShapes : List Spec.Shape} (tr : ScalarTrainer α paramShapes inputShapes natInputShapes) (xs : TList α inputShapes) (natInputs : TList natInputShapes) :

                                Uncurried loss-and-gradient pass for ScalarTrainer.

                                The loss and gradients come from the same tape. Use this instead of calling forwardT followed by backwardT when both results are needed.

                                Instances For
                                  def Runtime.Autograd.Torch.ScalarTrainer.backwardT {α : Type} {paramShapes inputShapes natInputShapes : List Spec.Shape} (tr : ScalarTrainer α paramShapes inputShapes natInputShapes) (xs : TList α inputShapes) (natInputs : TList natInputShapes) :
                                  IO (TList α paramShapes)

                                  Uncurried backward pass for ScalarTrainer.

                                  Returns per-parameter gradients (aligned with paramShapes).

                                  Instances For
                                    def Runtime.Autograd.Torch.ScalarTrainer.stepT {α : Type} {paramShapes inputShapes natInputShapes : List Spec.Shape} (tr : ScalarTrainer α paramShapes inputShapes natInputShapes) (lr : α) (xs : TList α inputShapes) (natInputs : TList natInputShapes) :

                                    Uncurried SGD step for ScalarTrainer.

                                    PyTorch comparison: analogous to loss.backward(); optimizer.step() for a fixed SGD optimizer, except here the trainer bundles the update rule.

                                    Instances For
                                      def Runtime.Autograd.Torch.ScalarTrainer.stepWithLossT {α : Type} {paramShapes inputShapes natInputShapes : List Spec.Shape} (tr : ScalarTrainer α paramShapes inputShapes natInputShapes) (lr : α) (xs : TList α inputShapes) (natInputs : TList natInputShapes) :

                                      Uncurried SGD step that returns the loss used to compute the update.

                                      Instances For
                                        def Runtime.Autograd.Torch.trainCycleSGD {α : Type} [ToString α] {paramShapes inputShapes : List Spec.Shape} (tr : ScalarTrainer α paramShapes inputShapes) (lr : α) (steps : ) (samples : List (TList α inputShapes)) (logEvery : := 1) :

                                        Train steps SGD updates, cycling through samples.

                                        PyTorch comparison: this matches the common eager training skeleton:

                                        for step in range(steps):
                                          batch = dataset[step % len(dataset)]
                                          loss = forward(batch)
                                          step(lr, batch)   # typically: loss.backward(); optimizer.step()
                                        

                                        Note: ScalarTrainer.step is the "bundled SGD optimizer" for the trainer. Stateful optimizers (Adam, RMSProp, ...) are exposed from Runtime.Autograd.TorchLean.

                                        Instances For
                                          def Runtime.Autograd.Torch.meanLoss {α : Type} [ToString α] [Add α] [Div α] [Zero α] [Coe α] {paramShapes inputShapes : List Spec.Shape} (tr : ScalarTrainer α paramShapes inputShapes) (samples : List (TList α inputShapes)) :
                                          IO α

                                          Evaluate mean loss over a dataset.

                                          PyTorch comparison: like running a model in torch.no_grad() over a dataloader and averaging the scalar loss values, except here we call ScalarTrainer.forwardT directly.

                                          Instances For