TorchLean API

NN.Examples.DeepDives.TorchIRPyTorch

TorchLean IR to PyTorch #

Tutorial: TorchLean → IR (NN.IR.Graph) → emitted PyTorch code.

Run: lake exe torchlean torch_ir_pytorch --arch linear > exported_model.py lake exe torchlean torch_ir_pytorch --arch mlp > exported_model.py lake exe torchlean torch_ir_pytorch --arch sum > exported_model.py lake exe torchlean torch_ir_pytorch --arch autoencoder > exported_model.py lake exe torchlean torch_ir_pytorch --arch mha > exported_model.py lake exe torchlean torch_ir_pytorch --arch mha-mask > exported_model.py lake exe torchlean torch_ir_pytorch --arch transformer > exported_model.py Then: python3 exported_model.py

The command emits the selected architecture and initialized parameters; it does not train them. Python execution requires PyTorch. Exporting or running the file does not by itself prove TorchLean/PyTorch numerical parity.

Command name used in diagnostics and by the top-level example runner.

Instances For

    Architectures #

    A 2 -> 3 -> 1 MLP with ReLU, the smallest architecture with a nonlinearity.

    Instances For

      A bare reduction, included because it exports to torch.sum rather than to a module.

      Instances For

        A 3 -> 2 -> 3 autoencoder with a tanh bottleneck.

        Instances For

          Two-head self-attention over a length-four sequence of width eight.

          Instances For

            A causal mask: position i may attend only to positions j ≤ i.

            Instances For

              The same attention block with the causal mask applied, so the export can be compared against torch.nn.MultiheadAttention with attn_mask.

              Instances For

                A full encoder block: attention, residual, LayerNorm, feed-forward, residual, LayerNorm.

                Deliberately tiny (one head of width two) so the emitted Python stays readable.

                Instances For

                  CLI parsing #

                  Export driver #

                  Lower a sequential model and its initial state, then write generated Python to stdout.

                  Instances For

                    Entry point. Writes Python to stdout for redirection to a file and execution under PyTorch.

                    Instances For