Skip to content
kmrasmussenPublic

About

Lean-native array programming experiments targeting StableHLO

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

LeanAX

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:

  1. Use Lean as the host language for a small, typed tensor DSL.
  2. Represent tensor programs in an explicit intermediate representation.
  3. Support JAX-like transforms over that IR: jit, grad, vmap, and shape checking.
  4. Lower the checked IR to StableHLO/MLIR, then rely on XLA for device code.
  5. Use Lean's type system and theorem proving where they are actually useful: shape safety, transformation correctness, and semantic contracts.

Why This Might Be Interesting

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.

Non-Goals For The First Pass

  • 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.

Initial Artifacts

Current E2E Slice

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 fixture

It 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 32

It 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.

A Useful First Milestone

The smallest meaningful prototype would be:

  1. Define a Lean tensor expression IR with static shapes and dtypes.
  2. Add primitive ops: constants, add, multiply, matmul, reshape, reduce-sum.
  3. Pretty-print that IR as a StableHLO-like textual module.
  4. Separately validate the output against real StableHLO tooling.
  5. Add one source-to-source transform, probably vmap before grad.

That would prove the core idea without pretending that the hard parts are solved.

North Star

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.

About

Lean-native array programming experiments targeting StableHLO

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages