TorchLean API

NN.Runtime.PyTorch.Import.Transformer

Transformer PyTorch Import #

Transformer weight import from JSON.

TorchLean's executable Transformer keeps query/key/value/output projections, feed-forward weights, and LayerNorm affine parameters in one shape-indexed state pack. In PyTorch these values are usually spread across several nn.Linear and nn.LayerNorm submodules.

For round-trip examples we accept a stable, explicit key format in JSON: Wq, Wk, Wv, Wo, W1, W2, b1, b2, norm1_gamma, norm1_beta, norm2_gamma, norm2_beta.

We also accept the nested PyTorch module keys emitted by Export.PyTorch.Transformer.withParameters, such as layers.0.mha.q_proj.weight. Explicit attention keys use (input, output) matrices. Nested nn.Linear projection keys use (output, input) and are transposed during import; feed-forward weights retain PyTorch's orientation under either naming scheme.

structure Import.PyTorch.Transformer.Parameters (modelWidth feedForwardWidth : ) :

Parameters for a single-layer Transformer encoder imported from a PyTorch state_dict.

This is the normalized typed view returned by the JSON loader. The loader accepts both TorchLean's explicit keys and the nested PyTorch module keys emitted by the exporter.

Instances For
    def Import.PyTorch.Transformer.load (modelWidth feedForwardWidth : ) (json : Lean.Json) :
    Option (Parameters modelWidth feedForwardWidth)

    Load Transformer parameters from JSON matching either supported export key format.

    Instances For