Skip to content

JGalego/torchlib

Repository files navigation

TorchLib

mission license PR Welcome last commit

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 Logo

Overview

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.

Features

  • 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

Project Structure

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

Requirements

  • Lean 4 (see lean-toolchain for the exact version)
  • Lake (included with Lean 4)

Installation

Add the following to your lakefile.toml:

[[require]]
name = "TorchLib"
git = "https://github.com/jgalego/torchlib"
rev = "main"

Then run:

lake update
lake build

Building from Source

git clone https://github.com/jgalego/torchlib
cd torchlib
lake build

Running Tests

lake build TorchLibTests
lake env lean scripts/RunTests.lean

Or run individual test modules:

lake env lean TorchLibTests/Core.lean
lake env lean TorchLibTests/Autograd.lean
lake env lean TorchLibTests/IBP.lean

Examples

Runnable 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.lean

TorchLean vs TorchLib

Here'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 βœ… βœ…

License

This project is licensed under the MIT License. See LICENSE for details.

About

Deep learning meets Lean4 πŸ”₯βœ…

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors