GraphSpec tutorial #
GraphSpec is TorchLean's architecture-facing graph language. It is useful when you want a model to exist first as a typed graph that can later be interpreted in several ways:
- as a pure specification (
Interp.spec) for theorem statements, - as an executable TorchLean program (
Compile.torchProgram) for direct execution, - as a sequential
nn.Sequentialview when the graph is just a layer stack, and - as a DAG model when the architecture has sharing, skip connections, or multi-input nodes.
That is why this folder is not a duplicate of NN.Spec.Models or NN.Examples.Models.
NN.Spec.Modelsdescribes mathematical/reference model semantics.NN.GraphSpec.Modelsdescribes architecture graphs and their parameter ABI.NN.Examples.Modelscontains runnable training scripts.
This tutorial does two things:
- It runs the smallest complete lowering path:
GraphSpec.Models.mlp → ToTorchLean.toSeq → train.run. - It typechecks and explains the broader GraphSpec model ladder: MLP, CNN, residual linear block, and ResNet18.
Only the MLP is trained here because it is the compact check path for Seq lowering. The larger
models are still GraphSpec models; they are deliberately kept as architecture terms so graph passes,
exporters, and proofs can consume them without turning this tutorial into a slow vision benchmark.
Run:
lake exe torchlean graphspec --backend eager
lake exe torchlean graphspec --backend compiled
You can also pass the standard scalar/runtime flags accepted by train.run, such as
--dtype ieee32.
Small architecture terms that should typecheck #
A small CNN graph, included here so the tutorial is visibly not “just MLP”.
This is still a sequential graph: convolution, ReLU, pooling, convolution, ReLU, pooling, flatten, linear head. The ugly-looking type is the point: the parameter shapes and intermediate spatial arithmetic are checked before the model can be used.
Instances For
The minimal DAG-native skip-connection example:
x ↦ relu((W x + b) + x).
This cannot be honestly represented as a plain chain without either duplicating the input path or
hiding sharing in a special layer. That is the pedagogical reason GraphSpec.DAG exists.
Instances For
The larger residual-family architecture available from the same model catalog.
We bind it here as a compile-time smoke check: users can inspect its type in the editor, while the runtime tutorial below stays small enough to run instantly.
Instances For
Print the architecture ladder this tutorial is checking.