TorchLean API

NN.API.Rand

Random Seeds #

Deterministic RNG helpers.

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

PyTorch mapping:

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 mixed with the base seed for each draw.

Instances For
    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 Spec.Random.nextSeed as a small deterministic mixing function.

        Instances For
          @[reducible, inline]
          abbrev TorchLean.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.

          This pure state-function representation preserves the result universe, including the higher universe used by model definitions.

          Instances For
            @[instance_reducible]

            Global seed stream used by rand.runGlobal and nn.withModel.

            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.build) and pass the base seed explicitly.

            Reset the global seed stream.

            PyTorch analogue: torch.manual_seed.

            Instances For
              def TorchLean.rand.runGlobal {α : Type} (computation : 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