https://github.com/lean-dojo/torchlean
TorchLean is the first unified Lean 4 framework for neural-network specification, execution, and verification.
https://github.com/lean-dojo/torchlean
ai4math ai4science lean4 neural-network pytorch scientific-computing theorem-proving verification
Last synced: 13 days ago
JSON representation
TorchLean is the first unified Lean 4 framework for neural-network specification, execution, and verification.
- Host: GitHub
- URL: https://github.com/lean-dojo/torchlean
- Owner: lean-dojo
- License: mit
- Created: 2026-05-11T04:32:07.000Z (about 1 month ago)
- Default Branch: main
- Last Pushed: 2026-05-29T23:08:17.000Z (20 days ago)
- Last Synced: 2026-05-30T01:09:08.729Z (20 days ago)
- Topics: ai4math, ai4science, lean4, neural-network, pytorch, scientific-computing, theorem-proving, verification
- Language: Lean
- Homepage: https://lean-dojo.github.io/TorchLean/
- Size: 30.3 MB
- Stars: 63
- Watchers: 0
- Forks: 7
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
- Citation: CITATION.cff
Awesome Lists containing this project
README
Formalizing Neural Networks in Lean
TorchLean is a Lean 4 framework for writing, running, inspecting, and verifying
neural-network programs. It provides typed tensors and model APIs, a shared graph
IR, runtime/autograd support, finite-precision semantics, certificate checkers,
CUDA/runtime boundaries, and examples across modern ML and scientific ML.
## Quickstart
```bash
git clone https://github.com/lean-dojo/TorchLean.git
cd TorchLean
lake build
python3 scripts/datasets/download_example_data.py --auto-mpg
lake exe torchlean mlp --cpu --steps 10
lake exe torchlean mlp --steps 10 --dtype float --backend eager
# Optional CUDA run, if the CUDA toolkit and an NVIDIA GPU are available:
lake build -K cuda=true
lake exe -K cuda=true torchlean mlp --cuda --fast-kernels --steps 1000
```
The first MLP command uses the executable IEEE-style Float32 path. The second
uses Lean's builtin `Float` runtime path. The CUDA command uses the native GPU
runtime path and checks that the CUDA backend is available; it is not a trusted
proof boundary.
TorchLean is pinned by `lean-toolchain` and currently builds with
`leanprover/lean4:v4.30.0`.
## First Things To Try
```bash
lake exe torchlean --help
lake exe verify --help
lake exe verify -- torchlean-ibp
```
For the maintained example surface:
```bash
lake build NN.Examples.Zoo
```
## Documentation
- Project site:
- Guide:
- API reference:
- Updates and recent validation notes:
- Paper: [*TorchLean: Formalizing Neural Networks in Lean*](https://arxiv.org/abs/2602.22631)
(arXiv:2602.22631)
Detailed tutorials, verification chapters, model walkthroughs, CUDA notes, and
API-level reference material belong in the guide and generated docs rather than
in this README.
## Use TorchLean From Another Lean Project
TorchLean is a normal Lake package. You can depend on the Git repository directly:
```lean
require TorchLean from git "https://github.com/lean-dojo/TorchLean.git" @ "main"
```
Then run:
```bash
lake update
lake exe cache get
lake build
```
Most downstream model and training files should start from the public facade:
```lean
import NN.API.Public
```
Use the broader umbrella when you want the maintained specification, IR, proof,
verification, examples, and widget surface:
```lean
import NN.Library
```
For local development against a checkout, use a path dependency instead:
```lean
require TorchLean from "../TorchLean"
```
## Repository Map
- `NN/API`: public facade for model, tensor, data, and training workflows.
- `NN/Spec`: mathematical tensor, layer, model, and dynamical-system definitions.
- `NN/Runtime`: executable autograd, optimizers, training loops, CUDA boundary,
PyTorch import/export, and RL runtime support.
- `NN/IR` and `NN/GraphSpec`: graph IR, graph semantics, and typed architecture
descriptions.
- `NN/Proofs`: tensor algebra, autograd correctness, analytic derivatives,
runtime approximation, and bridge proofs.
- `NN/Floats`: finite-precision models, IEEE-style executable semantics,
NeuralFloat formats, and error-bound infrastructure.
- `NN/MLTheory`: learning theory, robustness, CROWN/LiRPA, generative objectives,
optimization theory, and related proof layers.
- `NN/Verification`: certificate checkers and CLI workflows.
- `NN/Examples`: quickstarts, model zoo commands, widgets, bundled verification assets,
and interoperability workflows.
- `blueprint/TorchLeanBlueprint/Guide`: source for the public guide.
- `home_page`: project website sources.
## Correctness and Boundaries
For correctness claims, trust assumptions, and third-party tooling:
- `TRUST_BOUNDARIES.md`
- `AI_USAGE.md`
- `THIRD_PARTY_NOTICES.md`
- `CONTRIBUTING.md`
Lean proofs, executable checkers, Lake builds, tests, and explicit
trust-boundary documentation are the source of truth for what is proved,
checked, or assumed.
## Citation
For the current paper BibTeX, use the citation metadata on arXiv:
```bibtex
@misc{george2026torchleanformalizingneuralnetworks,
title={TorchLean: Formalizing Neural Networks in Lean},
author={Robert Joseph George and Jennifer Cruden and Will Adkisson and Xiangru Zhong and Huan Zhang and Anima Anandkumar},
year={2026},
eprint={2602.22631},
archivePrefix={arXiv},
primaryClass={cs.MS},
url={https://arxiv.org/abs/2602.22631},
}
```
## License
TorchLean is released under the MIT License. See `LICENSE`.