TorchLean API

NN.GraphSpec.ToTorchLean

GraphSpec → TorchLean.NN.Seq (sequential lowering) #

This provides a structural lowering from GraphSpec graphs into TorchLean sequential models.

What is this lowering for? #

GraphSpec gives us two things already:

So why do we also lower to TorchLean.NN.Seq?

TorchLean.NN.Seq is a small “training ergonomics” wrapper around a sequence of LayerDefs:

GraphSpec remains the source of truth for the model structure. Seq is a secondary view for the subset of models that are layer stacks.

Partial compilation: Except String #

Important design decision:

So the lowering returns Except String: it succeeds for graphs whose primitives provide Primitive.toLayerDefM?, and fails otherwise.

Deterministic initialization (occurrence index) #

We thread a Nat counter to support deterministic, occurrence-indexed initialization. This gives the lowering a stable RNG boundary without making global randomness part of the graph syntax.

For example, the default Primitive.linear uses:

See also:

Example (informal) #

If g : Graph ps σ τ is built from primitives that provide toLayerDefM?, then:

Convenience constructor for an error result in the Except String lowering pipeline.

Instances For
    @[reducible, inline]

    Public alias for TorchLean's sequential model type used as the lowering target.

    This keeps GraphSpec examples from exposing the lower-level Runtime.Autograd.TorchLean.NN.Seq path directly. It is definitionally the same type as NN.API.nn.Sequential, but avoids importing the full API facade back into GraphSpec.

    Instances For
      def NN.GraphSpec.ToTorchLean.toSeqAux {ps : List Shape} {σ τ : Shape} (g : Graph ps σ τ) (i : ) :

      Lower a graph to a TorchLean Seq, threading a “layer occurrence index”.

      The index is incremented for primitives with countsAsLayer = true.

      Instances For
        def NN.GraphSpec.ToTorchLean.toSeq {ps : List Shape} {σ τ : Shape} (g : Graph ps σ τ) :

        Try to lower a sequential GraphSpec graph into a TorchLean.NN.Seq.

        Use this when you specifically want the Seq wrapper for training ergonomics. If all you need is an executable program, prefer Compile.torchProgram: it is the more general path and does not require every primitive to have a LayerDef view.

        Instances For