TorchLean API

NN.API.Seeded

Seeded model builders #

Layer constructors draw deterministic initialization seeds from an explicit seed stream.

Model Builders and Seeding #

TorchLean keeps initialization randomness explicit so examples are reproducible.

Layer constructors return nn.M, a deterministic state computation over the initialization seed stream. Call nn.run seed to construct a model reproducibly.

Note: nn.Sequential lives in Type 2, so it cannot be returned directly from IO. We keep model building pure by drawing a base seed in IO and then calling nn.run.

Set the global seed used by nn.runGlobal and nn.nextSeed.

Prefer nn.run seed when the seed belongs in the model definition itself.

Instances For
    def TorchLean.nn.globalAvgPool (leading : Spec.Shape := Spec.Shape.scalar) {d channels : } (spatial : Vector d) (spatialNonzero : ∀ (i : Fin d), spatial.get i 0) :
    rand.SeedM (Sequential (leading.concat (Spec.Shape.ofList (channels :: spatial.toList))) (leading.concat (Spec.Shape.dim channels Spec.Shape.scalar)))

    Build global average pooling over the supplied nonempty spatial dimensions without consuming a seed.

    Instances For

      Default Builders #

      The nn.* constructors allocate initialization seeds through nn.M.

      @[reducible, inline]
      abbrev TorchLean.nn.M (α : Type u_1) :
      Type u_1

      Seeded builder monad: a state monad over TorchLean.rand.SeedStream.

      Instances For
        def TorchLean.nn.run {α : Type 2} (seed : ) (x : M α) :
        α

        Run a seeded builder starting from a base seed.

        Instances For
          def TorchLean.nn.lift {α : Type 2} (x : α) :
          M α

          Lift a pure value into the seeded builder (consumes no seeds).

          Instances For
            def TorchLean.nn.mapLeading (leading : Spec.Shape) {σ τ : Spec.Shape} (model : Sequential σ τ) :
            M (Sequential (leading.concat σ) (leading.concat τ))

            Apply a model independently over an arbitrary collection of leading dimensions.

            Instances For
              def TorchLean.nn.withSeed {α : Type 2} (k : α) :
              M α

              Consume one fresh seed and pass it to k.

              Instances For
                def TorchLean.nn.withSeedPair {α : Type 2} (k : α) :
                M α

                Consume two fresh seeds and pass them to k (in order).

                Instances For

                  Build an elementwise ReLU layer without consuming an initialization seed.

                  Instances For

                    Build an elementwise SiLU layer without consuming an initialization seed.

                    Instances For

                      Build an elementwise GELU layer without consuming an initialization seed.

                      Instances For

                        Build an elementwise sigmoid layer without consuming an initialization seed.

                        Instances For

                          Build an elementwise hyperbolic-tangent layer without consuming an initialization seed.

                          Instances For

                            Build a softmax layer over a tensor shape without consuming an initialization seed.

                            Instances For

                              Build a reduction that sums every tensor entry to a scalar.

                              Instances For

                                Build a layer that flattens the entire input shape into one vector.

                                Instances For

                                  Build a layer that preserves the batch axis while flattening each example.

                                  Instances For
                                    def TorchLean.nn.maxPool (leading : Spec.Shape := Spec.Shape.scalar) {d channels : } (spatial : Vector d) (cfg : Pool d) :
                                    M (Sequential (leading.concat (Spec.Shape.ofList (channels :: spatial.toList))) (leading.concat (Spec.Shape.ofList (channels :: (Spec.poolOutSpatialPad spatial cfg.kernel cfg.stride cfg.padding).toList))))

                                    Build max pooling over arbitrary spatial rank using the supplied pooling configuration.

                                    Instances For
                                      def TorchLean.nn.avgPool (leading : Spec.Shape := Spec.Shape.scalar) {d channels : } (spatial : Vector d) (cfg : Pool d) :
                                      M (Sequential (leading.concat (Spec.Shape.ofList (channels :: spatial.toList))) (leading.concat (Spec.Shape.ofList (channels :: (Spec.poolOutSpatialPad spatial cfg.kernel cfg.stride cfg.padding).toList))))

                                      Build average pooling over arbitrary spatial rank using the supplied pooling configuration.

                                      Instances For
                                        def TorchLean.nn.linear (inDim outDim : ) (pfx : Spec.Shape := Spec.Shape.scalar) :
                                        M (Sequential (pfx.appendDim inDim) (pfx.appendDim outDim))

                                        Build an affine layer, consuming independent seeds for its weight and bias initializers.

                                        Instances For
                                          def TorchLean.nn.linearWith (inDim outDim : ) (cfg : Linear) (pfx : Spec.Shape := Spec.Shape.scalar) :
                                          M (Sequential (pfx.appendDim inDim) (pfx.appendDim outDim))

                                          Seeded affine layer with an explicit initialization policy.

                                          Instances For

                                            Vector-only linear layer, specialized to the scalar prefix shape.

                                            Instances For
                                              def TorchLean.nn.deterministic.linear (inDim outDim seedWeight seedBias : ) (leading : Spec.Shape := Spec.Shape.scalar) :
                                              Sequential (leading.appendDim inDim) (leading.appendDim outDim)

                                              Construct a linear layer with explicit parameter-initialization seeds.

                                              Instances For
                                                def TorchLean.nn.rnn (seqLen inputSize hiddenSize : ) :

                                                Build a seeded recurrent neural network over a fixed sequence length.

                                                Instances For
                                                  def TorchLean.nn.gru (seqLen inputSize hiddenSize : ) :

                                                  Build a seeded gated recurrent unit over a fixed sequence length.

                                                  Instances For
                                                    def TorchLean.nn.mamba (seqLen inputSize hiddenSize : ) :

                                                    Build a seeded Mamba-style state-space sequence layer.

                                                    Instances For
                                                      def TorchLean.nn.lstm (seqLen inputSize hiddenSize : ) :

                                                      Build a seeded long short-term memory layer over a fixed sequence length.

                                                      Instances For
                                                        def TorchLean.nn.conv (leading : Spec.Shape := Spec.Shape.scalar) {d inChannels : } (spatial : Vector d) (cfg : Conv d) [NeZero inChannels] :
                                                        M (Sequential (leading.concat (Spec.Shape.ofList (inChannels :: spatial.toList))) (leading.concat (Spec.Shape.ofList (cfg.outChannels :: (Spec.convOutSpatial spatial cfg.kernel cfg.stride cfg.padding).toList))))

                                                        Build an arbitrary-rank convolution, allocating separate kernel and bias seeds.

                                                        Instances For
                                                          def TorchLean.nn.batchNorm (leading : Spec.Shape := Spec.Shape.scalar) {d channels : } (spatial : Vector d) (cfg : ChannelNorm := { }) [NeZero leading.size] [NeZero channels] [NeZero (Spec.Shape.ofList spatial.toList).size] :
                                                          M (Sequential (leading.concat (Spec.Shape.ofList (channels :: spatial.toList))) (leading.concat (Spec.Shape.ofList (channels :: spatial.toList))))

                                                          Build batch normalization with seeded scale and offset parameters.

                                                          Instances For
                                                            def TorchLean.nn.instanceNorm (leading : Spec.Shape := Spec.Shape.scalar) {d channels : } (spatial : Vector d) (cfg : ChannelNorm := { }) [NeZero leading.size] [NeZero channels] [NeZero (Spec.Shape.ofList spatial.toList).size] :
                                                            M (Sequential (leading.concat (Spec.Shape.ofList (channels :: spatial.toList))) (leading.concat (Spec.Shape.ofList (channels :: spatial.toList))))

                                                            Build instance normalization with seeded scale and offset parameters.

                                                            Instances For
                                                              def TorchLean.nn.groupNorm (leading : Spec.Shape := Spec.Shape.scalar) {d channels : } (spatial : Vector d) (groups : ) (hGroups : groups > 0) (hGroupsLe : channels groups) (hDiv : channels % groups = 0) (cfg : ChannelNorm := { }) [NeZero leading.size] [NeZero channels] [NeZero (Spec.Shape.ofList spatial.toList).size] :
                                                              M (Sequential (leading.concat (Spec.Shape.ofList (channels :: spatial.toList))) (leading.concat (Spec.Shape.ofList (channels :: spatial.toList))))

                                                              Build group normalization after checking the positive group count and channel divisibility.

                                                              Instances For
                                                                def TorchLean.nn.embedding (vocab embedDim : ) (cfg : Embedding := { }) {pfx : Spec.Shape} :
                                                                M (Sequential (pfx.appendDim vocab) (pfx.appendDim embedDim))

                                                                Build an embedding lookup layer from a freshly seeded embedding table.

                                                                Instances For

                                                                  Build deterministic sinusoidal positional encoding for a batched sequence.

                                                                  Instances For
                                                                    def TorchLean.nn.rope {batch numHeads seqLen headDim : } (cfg : RoPE := { }) :

                                                                    Build deterministic rotary positional encoding for multi-head sequence features.

                                                                    Instances For

                                                                      Build learned positional embeddings from a freshly allocated parameter seed.

                                                                      Instances For
                                                                        def TorchLean.nn.layerNorm {batch seqLen embedDim : } [NeZero seqLen] [NeZero embedDim] :

                                                                        Build layer normalization with independently seeded scale and offset parameters.

                                                                        Instances For

                                                                          Build seeded multi-head self-attention with an optional fixed attention mask.

                                                                          Instances For

                                                                            Build one seeded transformer encoder block, optionally applying a fixed attention mask.

                                                                            Instances For

                                                                              Build a seeded stack of transformer encoder blocks with an optional attention mask.

                                                                              Instances For

                                                                                Build dropout with a fresh deterministic mask seed from the builder stream.

                                                                                Instances For
                                                                                  def TorchLean.nn.runGlobal {α : Type} (x : M α) :
                                                                                  IO α

                                                                                  Run a seeded builder using the global seed stream set by nn.manualSeed (results in Type).

                                                                                  Note: model values like nn.Sequential live in Type 2, so they cannot be returned from IO. For models, use nn.run with an explicit base seed (obtained from nn.nextSeed).

                                                                                  Instances For

                                                                                    Draw a fresh base seed from the global seed stream set by nn.manualSeed.

                                                                                    Instances For

                                                                                      Draw n fresh base seeds from the global seed stream.

                                                                                      Instances For

                                                                                        Naming Convenience #

                                                                                        nn.run / nn.nextSeed are the core primitives, but in user code it is often clearer to read:

                                                                                        Draw a fresh base seed from the global seed stream.

                                                                                        Instances For
                                                                                          def TorchLean.nn.withModel {σ τ : Spec.Shape} {β : Type} (mk : M (Sequential σ τ)) (k : Sequential σ τIO β) :
                                                                                          IO β

                                                                                          Build a model using the next global seed, then run a continuation.

                                                                                          nn.Sequential lives in Type 2, so executable code passes the model to a continuation rather than returning it directly from IO.

                                                                                          Instances For