TorchLean API

NN.API.Rand

API Rand #

Deterministic RNG helpers.

TorchLean treats randomness explicitly (via seeds/keys) so examples are reproducible.

PyTorch mapping (rough):

Convenience wrappers for "PyTorch-like" dimension-first workflows.

uniform / mask are shape-indexed (Tensor α s) and are best used when s is already inferred. When you start from a runtime dims list (e.g. CLI args), uniformND/maskND are the ergonomic bridge.

def NN.API.rand.uniformND {α : Type} [Context α] (key : UInt64) (dims : List ) :

Dimension-first wrapper: uniform random tensor at runtime dims.

Instances For
    def NN.API.rand.maskND {α : Type} [Context α] (key : UInt64) (keepProb : α) (dims : List ) :

    Dimension-first wrapper: Bernoulli keep-mask at runtime dims (useful for dropout-style masks).

    Instances For
      def NN.API.rand.randND {α : Type} [Context α] (seed counter : ) (dims : List ) :

      Deterministic uniform tensor at runtime dims, using (seed,counter) to derive a key.

      Instances For

        Deterministic seed stream (seed + monotone counter).

        This is intended for model construction (parameter init keys, dropout keys, etc.) where you want PyTorch-like ergonomics but reproducible results.

        • seed :

          Base seed (think torch.manual_seed).

        • counter :

          Monotone counter (ensures each draw is distinct).

        Instances For
          @[implicit_reducible]
          Instances For
            @[reducible, inline]

            Create a fresh stream from a base seed.

            Instances For

              Draw a fresh seed and advance the stream.

              Implementation: we reuse TorchLean.Random.nextSeed as a small deterministic mixing function.

              Instances For

                Draw n fresh seeds.

                Instances For
                  @[reducible, inline]
                  abbrev NN.API.rand.SeedM (α : Type u) :

                  State monad for deterministic seed allocation.

                  Lean's StateT/StateM ties the state/result universes together, while TorchLean model definitions (e.g. nn.Sequential) live above Type 0.

                  So we define this seed builder directly (as a pure state monad), and run it in Type 2, which is sufficient for the public API.

                  Instances For
                    @[implicit_reducible]

                    Global seed stream used by rand.runGlobal / nn.runGlobal.

                    This is a convenience for script-like code that wants PyTorch-style "set the seed once" ergonomics. In proofs and reproducibility-sensitive code, prefer the pure interfaces (nn.run) and pass the base seed explicitly.

                    Reset the global seed stream.

                    PyTorch analogue: torch.manual_seed.

                    Instances For
                      def NN.API.rand.runGlobal {α : Type} (x : SeedM α) :
                      IO α

                      Run a seeded builder using the global seed stream and advance it.

                      This lets you build multiple models/layers in IO without explicitly threading seeds, while still remaining deterministic.

                      Instances For

                        Draw one fresh seed from the global seed stream.

                        Instances For

                          Draw n fresh seeds from the global seed stream.

                          Instances For