A toolchain for converting JAX Python functions to WebAssembly (WASM) via HLO (High Level Optimizer) and WAT (WebAssembly Text) intermediate representations.
- Converts JAX functions to HLO IR
- Converts HLO IR to WAT (WebAssembly Text)
- Converts WAT to WASM (WebAssembly binary) internally using Rust
- Handles both forward and gradient computations
- Python 3.8+
- Rust (with Cargo)
- pip
Install Python dependencies with:
pip install -r requirements.txtAll Rust dependencies are managed via Cargo. The project uses the wat crate for internal WAT-to-WASM conversion.
- Clone the repository:
git clone <repo-url>
cd jax2wasmCompiler- Install Python dependencies:
pip install -r requirements.txt- Build Rust project:
cargo build --releaseRun the Python script to generate HLO files for both the forward and gradient passes:
python3 src/jax2hlo.pyThis will create src/forward_optimized.hlo and src/grad_optimized.hlo.
Run the Rust pipeline to convert HLO to WAT and then to WASM:
cargo run --releaseThis will generate:
target/forward_generated.watandtarget/forward_generated.wasmtarget/grad_generated.watandtarget/grad_generated.wasm
You can use any WASM runtime or JS environment to load and test the generated .wasm files.
src/jax2hlo.py— Converts JAX functions to HLO IRsrc/— Rust source code for HLO parsing, WAT/WASM generationtarget/— Output directory for generated WAT/WASM filesrequirements.txt— Python dependenciesCargo.toml— Rust dependencies
- Add more HLO op support in the Rust code as needed
- Improve memory management and type support
- Add more tests and examples
MIT
To run a full demo of the pipeline:
./scripts/run_demo.shThis will:
- Generate HLO files from a built-in JAX demo function.
- Compile them to WAT and WASM.
- Output the results in the
target/directory.
You can inspect the generated .wat and .wasm files or run them in a WASM runtime.