TorchLean API

NN.Runtime.Autograd.Torch.Utils

Torch Utils #

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

This file focuses on demo/training 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 facade.

Initialization helpers (Float constants) #

Deterministic initialization schemes for small demos (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

    Next state of a simple 32-bit linear congruential generator (LCG).

    This exists to make demos deterministic and reproducible; it is not statistically strong and it is not cryptographically secure.

    Instances For

      Interpret the 32-bit LCG state as a Float in [0, 1).

      Instances For

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

        Implementation note: this is the LCG iterated idx+1 times, then normalized to [0,1).

        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 is intended for small demo models. It is not optimized.

            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

                    Small Sample Generators (Float Constants) #

                    Turn a point (x1,x2) into a Tensor Float (.dim 2 .scalar).

                    Instances For

                      Turn a scalar y into a Tensor Float (.dim 1 .scalar).

                      Instances For

                        Affine map y = w1*x1 + w2*x2 + b for building small regression datasets.

                        Instances For

                          Small conveniences for scalar training demos #

                          @[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.tlist1 {α : 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 small demo/training code, where tlist1/tlist2/… becomes tedious.

                              Example:

                              def xs : TList Float [Shape.Vec 2, Shape.Vec 1] :=
                                tlist![x, y]
                              
                              Instances For
                                def Runtime.Autograd.Torch.tlist2 {α : 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.tlist3 {α : 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.tlist4 {α : 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 : List Spec.Shape} (tr : ScalarTrainer α paramShapes inputShapes) (xs : TList α inputShapes) :

                                      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.backwardT {α : Type} {paramShapes inputShapes : List Spec.Shape} (tr : ScalarTrainer α paramShapes inputShapes) (xs : TList α inputShapes) :
                                        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 : List Spec.Shape} (tr : ScalarTrainer α paramShapes inputShapes) (lr : α) (xs : TList α inputShapes) :

                                          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.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