Skip to content

Commit be55ea0

Browse files
committed
feat: expand verified runtime and numerical foundations
1 parent 40ce64a commit be55ea0

490 files changed

Lines changed: 17826 additions & 21076 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

CONTRIBUTING.md

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -237,30 +237,6 @@ Project conventions:
237237
- Keep executable examples and proof code separate when they have different trust assumptions.
238238
- Avoid introducing axioms. If one is unavoidable, quarantine and document it.
239239

240-
## Checking Untrusted Proofs
241-
242-
TorchLean includes a wrapper for `leanprover/comparator`, which can compare a trusted
243-
`Challenge.lean` against an untrusted `Solution.lean` inside a `landrun` sandbox.
244-
245-
Prerequisite:
246-
247-
- Install `landrun` and make sure it is on `PATH`: https://github.com/Zouuup/landrun
248-
249-
Typical workflow:
250-
251-
1. Create a separate small Lake project with `Challenge.lean`, `Solution.lean`, and a comparator
252-
JSON config.
253-
2. Make that project depend on TorchLean, for example:
254-
`require TorchLean from "/path/to/TorchLean"`.
255-
3. Run:
256-
257-
```bash
258-
python3 /path/to/TorchLean/scripts/sandbox/run_comparator.py ./config.json --project .
259-
```
260-
261-
See `https://github.com/leanprover/comparator` for the JSON schema and default axiom allowlist
262-
pattern.
263-
264240
## PR Checklist
265241

266242
- `lake build` succeeds from a clean checkout.

NN/API.lean

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,34 @@ Authors: TorchLean Team
66

77
module
88

9-
public import NN.API.Public.Facade
109
public import NN.API.Adapters
11-
public import NN.API.Models.Generative
10+
public import NN.API.CLI
11+
public import NN.API.Checkpoint
12+
public import NN.API.Scalar
13+
public import NN.API.Data
14+
public import NN.API.Json
15+
public import NN.API.Loss
16+
public import NN.API.Module
17+
public import NN.API.Neural
18+
public import NN.API.Optim
19+
public import NN.API.RL
20+
public import NN.API.Runtime
1221
public import NN.API.SelfSupervised
22+
public import NN.API.Tensor
23+
public import NN.API.TensorPack
24+
public import NN.API.Text
25+
public import NN.API.Trainer
26+
public import NN.API.Verification
27+
public import NN.Spec.Models
1328

1429
/-!
15-
# TorchLean API
30+
# TorchLean
1631
17-
The focused import for applications that want TorchLean's concise `TorchLean.*` interface without
18-
loading the proof, verification, floating-point, and widget subsystems.
32+
Neural-network construction, training, runtime execution, datasets, automatic differentiation,
33+
verification, and mathematical model specifications.
1934
20-
The main namespaces are `TorchLean.nn` for neural networks, `TorchLean.classical` for classical
21-
and statistical models, `TorchLean.Trainer` for training, `TorchLean.optim` for optimizers, and
22-
`TorchLean.Data` for datasets. Use `import NN` when the same file also needs specifications,
23-
proofs, verification, or backend internals.
35+
Import `NN.API` for model code. Import `NN` when a file also uses the specification, proof,
36+
floating-point, or backend libraries.
2437
-/
2538

2639
@[expose] public section

NN/API/Adapters.lean

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,61 @@ Authors: TorchLean Team
66

77
module
88

9-
public import NN.API.Adapters.LoRA
9+
public import NN.Spec.Core.Tensor
10+
public import NN.Spec.Core.Tensor.Linalg
11+
public import NN.Spec.Core.TensorOps
1012

1113
/-!
12-
# Adapter APIs
14+
# Low-Rank Adapters
1315
14-
Small reusable adapters that can be attached to existing models without changing the model
15-
definition itself.
16+
LoRA represents a linear-weight update as two smaller matrices. For a base weight
17+
$W : \mathbb{R}^{d_{in}\times d_{out}}$, an adapter of rank $r$ uses
18+
$A : \mathbb{R}^{d_{in}\times r}$ and $B : \mathbb{R}^{r\times d_{out}}$:
19+
20+
$$W_{eff}=W+sAB.$$
21+
22+
The matrix orientation agrees with TorchLean's row-batch linear layers. This module defines the
23+
typed update and its action on a batch; the training code decides which parameters to optimize.
24+
25+
Reference: Hu et al., “LoRA: Low-Rank Adaptation of Large Language Models” (2021),
26+
https://arxiv.org/abs/2106.09685.
1627
-/
28+
29+
@[expose] public section
30+
31+
namespace TorchLean.Adapters.LoRA
32+
33+
open _root_.Spec
34+
open _root_.Spec.Tensor
35+
36+
/-- LoRA factors for a linear weight of shape `inDim × outDim`. -/
37+
structure Params (α : Type) (inDim rank outDim : Nat) where
38+
/-- Projection from the input dimension to the adapter rank. -/
39+
A : Tensor α (.dim inDim (.dim rank .scalar))
40+
/-- Projection from the adapter rank to the output dimension. -/
41+
B : Tensor α (.dim rank (.dim outDim .scalar))
42+
43+
/-- The scaled low-rank update $sAB$. -/
44+
def delta {α : Type} [Add α] [Mul α] [Zero α]
45+
{inDim rank outDim : Nat} (p : Params α inDim rank outDim) (scale : α) :
46+
Tensor α (.dim inDim (.dim outDim .scalar)) :=
47+
scaleSpec (matMulSpec p.A p.B) scale
48+
49+
/-- Add a LoRA update to a base linear weight. -/
50+
def effectiveWeight {α : Type} [Add α] [Mul α] [Sub α] [Zero α]
51+
{inDim rank outDim : Nat}
52+
(base : Tensor α (.dim inDim (.dim outDim .scalar)))
53+
(p : Params α inDim rank outDim) (scale : α) :
54+
Tensor α (.dim inDim (.dim outDim .scalar)) :=
55+
addSpec base (delta p scale)
56+
57+
/-- Apply a linear map whose weight is augmented by a LoRA update. -/
58+
def linear {α : Type} [Add α] [Mul α] [Sub α] [Zero α]
59+
{batch inDim rank outDim : Nat}
60+
(x : Tensor α (.dim batch (.dim inDim .scalar)))
61+
(base : Tensor α (.dim inDim (.dim outDim .scalar)))
62+
(p : Params α inDim rank outDim) (scale : α) :
63+
Tensor α (.dim batch (.dim outDim .scalar)) :=
64+
matMulSpec x (effectiveWeight base p scale)
65+
66+
end TorchLean.Adapters.LoRA

NN/API/Adapters/LoRA.lean

Lines changed: 0 additions & 86 deletions
This file was deleted.

0 commit comments

Comments
 (0)