[{"data":1,"prerenderedAt":4},["ShallowReactive",2],{"MH3H65s7a1":3},"\u003Ch1 align=\"center\">\n  \u003Cimg src=\"home_page/assets/media/brand/torchlean-logo.png\" alt=\"TorchLean logo\" width=\"88\" align=\"center\">\n  Formalizing Neural Networks in Lean\n\u003C/h1>\n\nTorchLean is a Lean 4 framework for writing, running, inspecting, and verifying\nneural-network programs. It provides typed tensors and model APIs, a shared graph\nIR, runtime/autograd support, finite-precision semantics, certificate checkers,\nCUDA/runtime boundaries, and examples across modern ML and scientific ML.\n\n## Installation\n\n```bash\ngit clone https://github.com/lean-dojo/TorchLean.git\ncd TorchLean\nlake exe cache get\nlake build\n```\n\nFor Linux, macOS, Windows/WSL, CUDA, optional LibTorch support, and an explanation of\nTorchLean's backend architecture, see the [Installation guide](https://lean-dojo.github.io/TorchLean/installation/).\n\nTorchLean is pinned by `lean-toolchain` and currently builds with\n`leanprover/lean4:v4.32.0`.\n\n## Quickstart\n\n```bash\nlake exe torchlean quickstart_mlp --device cpu --steps 10 --dtype float32 --backend eager\nlake exe torchlean quickstart_mlp --device cpu --steps 10 --dtype float --backend eager\n\n# Optional CUDA run, if the CUDA toolkit and an NVIDIA GPU are available:\nlake -R -K cuda=true build\nlake -R -K cuda=true exe torchlean mlp --device cuda --steps 1000\n```\n\nThe first quickstart uses TorchLean's executable IEEE-style Float32 scalar. The\nsecond uses Lean's builtin `Float` runtime path. The CUDA command uses the\nnative GPU runtime path and checks that the CUDA backend is available. Theorem\nstatements that mention CUDA cite the native-runtime boundary in\n`TRUST_BOUNDARIES.md` instead of treating a kernel launch as Lean proof evidence.\n\nThe public code shape is:\n\n```lean\nimport NN.API\nopen TorchLean\n\ndef model :=\n  nn.Sequential![\n    nn.Linear 2 8,\n    nn.ReLU,\n    nn.Linear 8 1\n  ]\n\ndef xs : Tensor.T Float (shape![4, 2]) :=\n  tensorOfList! [4, 2] [0.0, 0.0, 0.0, 1.0, 1.0, 0.0, 1.0, 1.0]\n\ndef ys : Tensor.T Float (shape![4, 1]) :=\n  tensorOfList! [4, 1] [0.2, 1.0, 1.0, 1.8]\n\ndef data : Trainer.Dataset (.dim 2 .scalar) (.dim 1 .scalar) :=\n  Data.tensorDataset xs ys\n\ndef trainOnce : IO Unit := do\n  let trainer :=\n    Trainer.new model\n      { task := .regression\n        optimizer := optim.sgd { lr := 0.05 }\n        backend := .compiled\n        dtype := .float32 }\n  let initialPrediction ← trainer.predict (tensorOfList! [2] [0.5, -0.25])\n  IO.println s!\"initial={Tensor.pretty initialPrediction}\"\n  let trained ← trainer.train data { steps := 200, batchSize := 16, logEvery := 25 }\n  trained.printSummary\n```\n\n## First Things To Try\n\n```bash\nlake exe torchlean --help\nlake exe verify --help\nlake exe verify -- torchlean-ibp\n```\n\nFor the maintained examples:\n\n```bash\nlake build NN.Examples.Zoo\n```\n\n## Use TorchLean From Another Lean Project\n\nTorchLean is a normal Lake package. You can depend on the Git repository directly:\n\n```lean\nrequire TorchLean from git \"https://github.com/lean-dojo/TorchLean.git\" @ \"main\"\n```\n\nThen run:\n\n```bash\nlake update\nlake exe cache get\nlake build\n```\n\nUse `import NN.API` for model, data, and training code. It provides the application-facing\n`TorchLean.nn`, `TorchLean.classical`, `TorchLean.Data`, `TorchLean.Trainer`, and\n`TorchLean.optim` namespaces. Use `import NN` when the same file also needs verification, proofs,\nor backend infrastructure; focused imports such as `NN.GraphSpec`, `NN.Runtime`, or `NN.Proofs`\nare available for subsystem work.\n\nDownstream model and training files should start from:\n\n```lean\nimport NN.API\nopen TorchLean\n```\n\nFor local development against a checkout, use a path dependency instead:\n\n```lean\nrequire TorchLean from \"../TorchLean\"\n```\n\n## Repository Map\n\n- `NN.lean`: canonical umbrella import for model, tensor, data, training, verification,\n  and proof workflows.\n- `NN/API`: the application facade exported by `import NN.API` and included by `import NN`.\n- `NN/Spec`: mathematical tensor, layer, model, and dynamical-system definitions.\n- `NN/Runtime`: executable autograd, optimizers, training loops, CUDA boundary,\n  PyTorch import/export, and RL runtime support.\n- `NN/Backend`: contract-carrying backend capsules, profiles, device targets, reports, and gates.\n- `NN/IR` and `NN/GraphSpec`: graph IR, graph semantics, and typed architecture\n  descriptions.\n- `NN/Proofs`: tensor algebra, selected autograd correctness theorems, analytic derivatives,\n  runtime approximation, and bridge proofs.\n- `NN/Floats`: finite-precision models, IEEE-style executable semantics,\n  NeuralFloat formats, and error-bound infrastructure.\n- `NN/MLTheory`: learning theory, robustness, CROWN/LiRPA, generative objectives,\n  optimization theory, and related proof layers.\n- `NN/Verification`: certificate checkers and CLI workflows.\n- `NN/Examples`: quickstarts, model zoo commands, widgets, bundled verification assets,\n  and interoperability workflows.\n- `blueprint/TorchLeanBlueprint/Guide`: source for the public guide.\n- `home_page`: project website sources.\n\n## Current Capabilities\n\nThe same rule applies across the tree: name the object, name the artifact, and name the boundary.\n\n- **Training and runtime.** `Trainer.new` supports supervised tasks, scalar/backend choices,\n  trained handles, prediction, logs, typed step streams, generated or file-backed batches, and\n  optional CUDA-backed Float32 runtime paths. Public code should look like one trainer with selected\n  backend options, not one public forward method per backend.\n- **Graphs and compiler fragments.** TorchLean models can be lowered to a shared IR. A first-order\n  forward fragment has Lean-side compiler-correctness theorems, and coverage grows operation by\n  operation. GraphSpec describes typed architectures above the lower-level op-tagged DAG consumed by\n  runtime, widgets, exporters, and verification passes.\n- **Verification.** The repository includes IBP/CROWN-style graph checks, α/β-CROWN-style artifact\n  replay, robustness workflows, VNN-COMP-style MNIST checks, PINN residual checks, ODE corridors,\n  spline certificates, and 3D geometry projection certificates. External producers write compact\n  artifacts; Lean parses the artifact, checks the stated predicate, and records any remaining\n  producer assumptions.\n- **ML theory.** The theory layer covers CROWN/LiRPA objects, optimizer laws, Muon\n  orthogonalization certificates, learning-theory examples, generative objective identities,\n  self-supervised-learning algebra, approximation theorems, and floating-point bridges. Optimizers\n  use a generic `TensorOptimizer` interface so new update rules share finite-stream laws. A\n  `StepSpec` is available when an independent mathematical recurrence must be related to the\n  executable update.\n- **Scientific ML.** The FNO Burgers and PINN/ODE paths show how numerical artifacts can be carried\n  back into Lean checks. External simulators and optimizers remain named producers; Lean owns the\n  artifact schemas, residual predicates, dataset checks, and certificate replay statements that sit\n  at the verification boundary.\n\n## What To Cite For A Claim\n\n| Claim shape | Where to look |\n| --- | --- |\n| \"This model runs and trains\" | `NN/Examples`, `NN/Runtime`, command output, and regression tests. |\n| \"This graph has a checked bound/certificate\" | `NN/Verification`, `NN/MLTheory/CROWN`, and the artifact schema named by the command. |\n| \"This compilation/evaluation fragment preserves meaning\" | `NN/Verification/TorchLean/Proved` and the theorem imported through `NN.Verification`. |\n| \"This optimizer update follows the intended equation\" | `NN/Runtime/Optim` for executable equations and `NN/MLTheory/Optimization` for reusable laws. |\n| \"This finite-precision statement has a formal model\" | `NN/Floats`, `NN/Proofs/RuntimeApprox`, and the relevant bridge hypotheses. |\n| \"This CUDA/LibTorch/PyTorch/Julia/Gymnasium path was used\" | `TRUST_BOUNDARIES.md`, the runtime module docstring, and the command or artifact provenance. |\n\n## Correctness and Boundaries\n\nFor correctness claims, trust assumptions, and third-party tooling:\n\n- `TRUST_BOUNDARIES.md`\n- `AI_USAGE.md`\n- `THIRD_PARTY_NOTICES.md`\n- `CONTRIBUTING.md`\n\nLean proofs, executable checkers, Lake builds, tests, and explicit\ntrust-boundary documentation are the source of truth for what is proved,\nchecked, or assumed.\n\n## Citation\n\nIf TorchLean is useful in your work, please cite\n[*TorchLean: Formalizing Neural Networks in Lean*](https://arxiv.org/abs/2602.22631):\n\n```bibtex\n@misc{george2026torchlean,\n  title         = {TorchLean: Formalizing Neural Networks in Lean},\n  author        = {George, Robert Joseph and Cruden, Jennifer and Adkisson, Will and\n                   Zhong, Xiangru and Zhang, Huan and Anandkumar, Anima},\n  year          = {2026},\n  eprint        = {2602.22631},\n  archivePrefix = {arXiv},\n  primaryClass  = {cs.MS},\n  url           = {https://arxiv.org/abs/2602.22631}\n}\n```\n\n## License\n\nTorchLean is released under the MIT License. See `LICENSE`.\n",1784558025756]