LeanAX is a design exploration for a Lean-native array programming system inspired by JAX, with compilation to XLA through a stable tensor IR such as StableHLO.
The working idea is not "JAX reimplemented in Lean" in a narrow sense. It is:
- Use Lean as the host language for a small, typed tensor DSL.
- Represent tensor programs in an explicit intermediate representation.
- Support JAX-like transforms over that IR:
jit,grad,vmap, and shape checking. - Lower the checked IR to StableHLO/MLIR, then rely on XLA for device code.
- Use Lean's type system and theorem proving where they are actually useful: shape safety, transformation correctness, and semantic contracts.
JAX gets much of its power from a clean split:
- Python is the user-facing language.
- Tracing captures array code into an IR.
- Transformations rewrite that IR.
- XLA compiles the transformed IR for CPU/GPU/TPU execution.
Lean could occupy the front-end and proof layer in that stack. Instead of tracing ordinary Python at runtime, LeanAX could construct a typed expression graph directly, with shape and dtype facts available before lowering.
- Do not build a full NumPy clone.
- Do not write a new device compiler.
- Do not target raw XLA internals first if StableHLO/MLIR is sufficient.
- Do not make theorem proving mandatory for every user expression.
- docs/jax-xla-lean.md: what JAX and XLA are, and where Lean fits.
- docs/compiler-sketch.md: a possible LeanAX compiler pipeline.
- docs/mnist-mlp-goal.md: the high-level north star: train a small MLP on MNIST through LeanAX.
- docs/mnist-mlp-roadmap.md: a staged path from the current prototype to a JAX-shaped MNIST training loop.
- docs/mnist-runtime-boundary.md: the current direct-runtime boundary for the MNIST train-step path.
- examples/first-sketch.lean: pseudocode for the kind of API this project is imagining.
- notes/dialogue-001.md: first set of discussion questions and decisions.
- amitious_plan: ambitious roadmap documents and phase-one planning.
- .tickets: concrete ticket backlog for implementation slices.
The repository now contains a minimal Lean package and a reproducible e2e path:
nix develop --command bash -lc 'lake build && cargo test --locked --manifest-path e2e/runner/Cargo.toml && cargo run --locked --manifest-path e2e/runner/Cargo.toml'That command builds the Lean executable, tests the Rust manifest parser, emits
MLIR-parseable StableHLO-shaped text for the manifested examples, compares the
output against e2e/golden, runs a no-dependency Python verifier through uv,
parses generated modules with mlir-opt --allow-unregistered-dialect, probes
for stablehlo-opt and runs it when available, executes numeric cases through a
small Python evaluator for the supported generated-op subset, executes the first
LLVM-dialect runtime fixture with mlir-runner, and checks deterministic
host-side training loops. The unified e2e manifest records numeric cases,
runtime cases, validation failures, and training-loop checks, so unexpected
successes and unexpected failures are checked by the same runner.
See docs/stablehlo-verification.md for the current boundary between generic MLIR parsing and StableHLO semantic verification.
emit-stablehlo also writes lowering manifest sidecars when passed
--manifest-out. The e2e runner validates those manifests for every passing
generated module, and the affine sidecar has a checked-in golden fixture.
The first external runtime fixture is:
nix develop --command bash -lc 'lake exe leanax emit-runtime-llvm --case affine-runtime --out generated/affine-runtime.mlir && mlir-runner --entry-point-result=f32 generated/affine-runtime.mlir'It returns the affine fixture checksum 94.25 through the LLVM MLIR JIT.
The MNIST path currently starts with a deterministic fixture mode documented in
docs/mnist-data.md. It checks flattened 28x28 batches,
normalization, one-hot labels, and deterministic host-side batching without a
network dependency.
The current ten-class MNIST classifier training wrapper is:
nix develop --command uv run --no-managed-python --python python3 --project e2e/python python e2e/python/mnist_train_command.py --mode fixtureIt prints stable metrics: mode, epochs, samples, batches,
first_loss, final_loss, first_accuracy, final_accuracy, and the checked
artifact paths. Run the full e2e gate first so the generated compiler artifacts
exist under generated/; missing artifacts fail with the path that needs to be
generated.
Cached IDX mode is opt-in:
nix develop --command uv run --no-managed-python --python python3 --project e2e/python python e2e/python/mnist_train_command.py --mode cached --split train --max-samples 32It resolves the documented MNIST cache layout or explicit --images-idx and
--labels-idx paths. If the cache is absent, the non-default cached mode prints
mnist-train-skip ... reason=missing-cache instead of turning the default gate
into a network or download requirement.
The smallest meaningful prototype would be:
- Define a Lean tensor expression IR with static shapes and dtypes.
- Add primitive ops: constants, add, multiply, matmul, reshape, reduce-sum.
- Pretty-print that IR as a StableHLO-like textual module.
- Separately validate the output against real StableHLO tooling.
- Add one source-to-source transform, probably
vmapbeforegrad.
That would prove the core idea without pretending that the hard parts are solved.
The larger target is to train a small multilayer perceptron on MNIST from a
Lean-native tensor program. The program should stay close to how a pure JAX MLP
training loop is structured: pure forward, loss, and train_step functions,
explicit parameters and batches, and transforms such as jit, vmap, and
grad. LeanAX will differ by making shapes, dtypes, IR construction, and
selected correctness arguments explicit. The project should grow toward that
target in checked slices rather than jumping straight to a large runtime.