A Lean 4 library for neural network modeling, training, and formal verification.
β οΈ Warning: This module is a work in progress and may contain incomplete features, placeholders, and breaking changes. Use with caution and refer to the source code for the latest status.
ποΈ Acknowledgments: This project is inspired by the design of PyTorch via TorchLean (George et al., 2026) and CSLib. A warm thanks to the PyTorch and Lean communities for their support, and to everyone crazy enough to live at the intersection of formal verification and machine learning!
TorchLib brings deep learning primitives to Lean 4, combining expressive neural network construction with formal verification techniques.
It provides a composable layer system, automatic differentiation, a training loop, and certified robustness verification via Interval Bound Propagation (IBP) and CROWN.
- Core primitives - tensors, shapes, and basic operations
- Intermediate representation - computation graph IR for model inspection and transformation
- Layer library - composable building blocks (linear, activation, etc.)
- Model construction - high-level API for defining neural networks
- Automatic differentiation - forward and backward pass via
Autograd - Training - loss functions, optimizers, and training loops
- Formal verification - certified robustness bounds via IBP and CROWN
- Certificates - machine-checkable proofs of network properties
TorchLib/
βββ Core.lean # Core types and tensor primitives
βββ IR.lean # Intermediate representation
βββ Layers.lean # Layer definitions
βββ Models.lean # High-level model API
βββ Runtime/
β βββ Autograd.lean # Automatic differentiation
β βββ TorchLean.lean # LibTorch/runtime bindings
β βββ Training.lean # Training loop and optimizers
βββ Verification/
βββ Certificate.lean # Verification certificates
βββ CROWN.lean # CROWN bound propagation
βββ IBP.lean # Interval Bound Propagation
Add the following to your lakefile.toml:
[[require]]
name = "TorchLib"
git = "https://github.com/jgalego/torchlib"
rev = "main"Then run:
lake update
lake buildgit clone https://github.com/jgalego/torchlib
cd torchlib
lake buildlake build TorchLibTests
lake env lean scripts/RunTests.leanOr run individual test modules:
lake env lean TorchLibTests/Core.lean
lake env lean TorchLibTests/Autograd.lean
lake env lean TorchLibTests/IBP.leanRunnable examples live in the examples/ directory.
Each file is self-contained and can be checked individually:
lake env lean examples/Tensors.lean
lake env lean examples/LinearLayer.lean
lake env lean examples/MLP.lean
lake env lean examples/Autograd.lean
lake env lean examples/Training.lean
lake env lean examples/Verification.lean
lake env lean examples/VerificationMNIST.leanHere's a feature-by-feature comparison between TorchLean and this implementation:
β = implemented Β π§ = partial / stub Β β = not implemented
| Feature | TorchLean (paper) | TorchLib (impl) |
|---|---|---|
| Eager API (layers, models, training) | β | β |
| Compiled mode (lower to SSA/DAG IR) | β | π§ |
| Op-tagged SSA/DAG IR + interpreter | β | β |
| IEEE-754 binary32 kernel (IEEE32Exec) | β | β |
| Proof-relevant rounding models | β | β |
| Reverse-mode autograd | β | β |
| Optimisers (SGD, Adam, AdamW, RMSProp) | β | β |
| IBP bound propagation | β | β |
| CROWN / LiRPA back-substitution | β | β |
| Ξ±-CROWN (learnable slopes) | β | π§ |
| Certificate checking | β | β |
| Certified robustness (end-to-end) | β | β |
| PINN residual bounds | β | β |
| Lyapunov controller verification | β | β |
| Universal approximation theorem | β | β |
| Checkpoint import / export | β | β |
This project is licensed under the MIT License. See LICENSE for details.
