Repolode
lean-dojo

lean-dojo/TorchLean

A Lean 4 framework for specifying, executing, and formally verifying neural networks, with shape-checked tensors and PyTorch-style training.

Stars
206
Forks
25
Open issues
3
Last commit
10 days ago

What is it?

TorchLean combines general tensor computation, machine learning, and mathematical proof in the Lean 4 language. Its shape-checked tensors support linear algebra and numerical algorithms, and the same model definitions can be used for training, graph transformations, certificate checks, and proofs.

The verification tools address questions such as whether a classifier keeps its prediction across an input region. Through the FloatLib dependency, numerical precision is selectable, and rounding and numerical error bounds can be proven under explicit assumptions. Automatic differentiation is implemented in TorchLean itself; on GPU it calls LibTorch's ATen operations for tensor values and local gradients.

The project provides a quickstart MLP example, a CUDA-enabled GPU path, and a Lake package that other Lean projects can depend on directly. The documentation separates the Lean proofs from the external CPU and GPU runtimes, which are listed as trust boundaries.

Key features

  • 01Shape-checked tensors and neural network layers written in Lean
  • 02Training on CPU or CUDA via LibTorch, with TorchLean-owned automatic differentiation
  • 03Selectable numerical precision through FloatLib, including binary32 and binary128
  • 04Verification workflows such as interval bound propagation and robustness checks
  • 05Import and export paths for PyTorch models and Jaxpr graphs

Use cases

  • Proving properties of a neural network, such as robustness of its prediction in an input region
  • Studying numerical error and rounding behavior of tensor computations under explicit assumptions
  • Training small models in Lean while keeping definitions reusable for proofs
  • Researching formal verification of machine learning systems

Quick start

  1. 1

    Clone the repository

    git clone https://github.com/lean-dojo/TorchLean.git
  2. 2

    Enter the project directory

    cd TorchLean
  3. 3

    Fetch the prebuilt cache

    scripts/lake.sh exe cache get
  4. 4

    Build and run the CPU quickstart

    scripts/lake.sh build && scripts/lake.sh exe torchlean quickstart_mlp --device cpu --steps 10

Things to know

  • The GPU path requires a CUDA-enabled LibTorch SDK and an NVIDIA GPU.
  • On GPU, the LibTorch/ATen implementation is an external trust boundary outside the Lean proofs.

Topics

FAQ

What is TorchLean?
A Lean 4 framework for specifying, executing, and formally verifying neural networks, with shape-checked tensors and PyTorch-style training. TorchLean combines general tensor computation, machine learning, and mathematical proof in the Lean 4 language. Its shape-checked tensors support linear algebra and numerical algorithms, and the same model definitions can be used for training, graph transformations, certificate checks, and proofs.
Is TorchLean free to use? What license does it have?
TorchLean is open source under the MIT license. Check the license terms before using it in commercial products.
Is TorchLean still maintained?
Actively maintained. The last commit to TorchLean was on Oct 29, 2026.
What can I use TorchLean for?
Proving properties of a neural network, such as robustness of its prediction in an input region Studying numerical error and rounding behavior of tensor computations under explicit assumptions Training small models in Lean while keeping definitions reusable for proofs Researching formal verification of machine learning systems
How do I install TorchLean?
Clone the repository: git clone https://github.com/lean-dojo/TorchLean.git Enter the project directory: cd TorchLean Fetch the prebuilt cache: scripts/lake.sh exe cache get Build and run the CPU quickstart: scripts/lake.sh build && scripts/lake.sh exe torchlean quickstart_mlp --device cpu --steps 10
Where can I get TorchLean?
The source code and installation instructions are on GitHub: https://github.com/lean-dojo/TorchLean

Summary generated by AI from the project's README. GitHub stats refresh daily.

Related projects