TorchLean API

NN.API.Neural.Transformer

Transformer Blocks #

This module exposes attention, feed-forward, and Transformer-stack constructors used by sequence models and higher-level examples.

Config record for transformerEncoderBlock.

Separating the config as a structure makes it easier to write readable examples and keep seed management deterministic.

  • numHeads :

    Number of attention heads.

  • headDim :

    Per-head embedding dimension.

  • ffnHidden :

    Hidden dimension of the feed-forward network.

  • activation : Activation

    Activation used in the feed-forward network.

  • dropout? : Option Float

    Optional dropout probability for examples; none means no dropout.

  • normFirst : Bool

    Normalize before attention and feed-forward sublayers instead of after each residual.

  • attentionOutputBias : Bool

    Add a trainable bias after the attention output projection.

  • Attention and feed-forward weight initialization. none keeps each layer's default.

  • Initializer for the attention and feed-forward projections that write to residual streams.

    When omitted, weightInit? is used. The separate field supports depth-scaled residual initialization without imposing that convention on every Transformer.

  • seedBase :

    Base seed used to derive deterministic per-layer seeds inside the block.

Instances For

    Transformer encoder block configuration.

    This follows the familiar pattern: (residual MHA) -> LayerNorm -> (residual FFN) -> LayerNorm.

    PyTorch analogue:

    • torch.nn.TransformerEncoderLayer (https://pytorch.org/docs/stable/generated/torch.nn.TransformerEncoderLayer.html)
    Instances For

      Transformer encoder block.

      This is transformerEncoderBlockWithMask; pass mask := ... to enable causal masking (or other attention masks).

      Instances For

        Config record for transformerEncoderStack.

        This builds layers copies of transformerEncoderBlock, allocating seeds in a fixed stride.

        • layers :

          Layer stack.

        • Template config for each block (its seedBase is ignored; we allocate per-layer seeds).

        • seedBase :

          Base seed for the whole stack.

        • seedStride :

          Seed stride between consecutive blocks (must exceed the per-block seed footprint).

        Instances For

          Internal recursion for transformerEncoderStack.

          Builds remaining blocks starting at layerIdx, allocating each block's seedBase as seedBase + layerIdx * seedStride.

          Instances For
            def TorchLean.nn.Internal.blocks.transformerStackGo {batch n dModel : } [NeZero n] [NeZero dModel] (template : TransformerEncoderBlock) (seedBase seedStride layerIdx remaining : ) :

            Internal recursion for transformerEncoderStack (unmasked).

            This is transformerStackGoWithMask with mask := none.

            Instances For

              Stack cfg.layers copies of blocks.transformerEncoderBlock.

              TorchLean analogue of composing torch.nn.TransformerEncoderLayer into a torch.nn.TransformerEncoder, using Seq composition for the typed model.

              Instances For

                Stack cfg.layers copies of blocks.transformerEncoderBlock.

                This is transformerEncoderStackWithMask; pass mask := ... to enable causal masking (or other attention masks).

                Instances For

                  Transformer encoder followed by a flatten+linear classification head.

                  PyTorch analogue (approximately): nn.TransformerEncoder(...) + pooling/flattening + nn.linear.

                  Instances For