Automatic Sparse Differentiation in JAX
Project description
asdex
Automatic Sparse Differentiation in JAX.
asdex exploits sparsity structure to efficiently materialize Jacobians and Hessians.
It implements a custom Jaxpr interpreter
that uses abstract interpretation
to detect sparsity patterns from the computation graph,
then uses graph coloring to minimize the number of AD passes needed.
Installation
pip install asdex
Or with uv:
uv add asdex
Example
import asdex
import jax
import jax.numpy as jnp
def f(x):
return (x[1:] - x[:-1]) ** 2
x_sample = jnp.zeros(50) # sample input for sparsity pattern detection
jac_fn = jax.jit(asdex.jacobian(f, x_sample))
# ColoredPattern(49×50, nnz=98, sparsity=96.0%, JVP, 2 colors)
# 2 JVPs (instead of 49 VJPs or 50 JVPs)
# ⎡⠙⢦⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⎤ ⎡⣿⎤
# ⎢⠀⠀⠙⢦⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⎥ ⎢⣿⎥
# ⎢⠀⠀⠀⠀⠙⢦⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⎥ ⎢⣿⎥
# ⎢⠀⠀⠀⠀⠀⠀⠙⢦⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⎥ ⎢⣿⎥
# ⎢⠀⠀⠀⠀⠀⠀⠀⠀⠙⢦⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⎥ ⎢⣿⎥
# ⎢⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠙⢦⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⎥ ⎢⣿⎥
# ⎢⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠙⢦⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⎥ → ⎢⣿⎥
# ⎢⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠙⢦⡀⠀⠀⠀⠀⠀⠀⠀⠀⎥ ⎢⣿⎥
# ⎢⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠙⢦⡀⠀⠀⠀⠀⠀⠀⎥ ⎢⣿⎥
# ⎢⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠙⢦⡀⠀⠀⠀⠀⎥ ⎢⣿⎥
# ⎢⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠙⢦⡀⠀⠀⎥ ⎢⣿⎥
# ⎢⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠙⢦⡀⎥ ⎢⣿⎥
# ⎣⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠉⎦ ⎣⠉⎦
for x in inputs:
J = jac_fn(x)
Instead of 49 VJPs or 50 JVPs,
asdex computes the full sparse Jacobian with just 2 JVPs.
Since sparsity detection and coloring can be expensive on large problems, we recommend saving and reusing colored patterns:
import jax.numpy as jnp
from asdex import jacobian_coloring
from asdex import ColoredPattern, jacobian_from_coloring
# Compute coloring once...
x = jnp.zeros(1000)
coloring = jacobian_coloring(f, x)
coloring.save("colored.npz")
# ...load and reuse later
coloring = ColoredPattern.load("colored.npz")
jac_fn = jax.jit(jacobian_from_coloring(f, coloring))
Features
The full ASD pipeline:
- Sparse Jacobians and Hessians: one VJP/JVP/HVP per color, with automatic (or user-defined) mode selection.
- Sparsity detection: finds global sparsity patterns valid for all inputs.
- Graph coloring: row, column, and symmetric coloring minimize AD passes.
- Correctness verification against vanilla JAX.
You already know your sparsity pattern?
- Manually provide sparsity patterns: supply a known pattern from dense, COO, or BCOO formats.
- Precompute, save & load: reuse a colored pattern across inputs, or persist it by saving and loading.
An interface mirroring JAX:
- Multiple inputs and outputs: supports multi-argument functions via
argnums, as well as multiple return values. - PyTree inputs and outputs: sparse differentiation through arbitrary nested PyTrees.
- Auxiliary outputs: supports
has_aux=Truefor functions returning(output, aux). - Value and derivative:
value_and_jacobian/value_and_hessianreturn the primal valuef(x)without a redundant forward pass.
And more:
- Multiple output formats: decompression to BCOO, dense JAX arrays, NumPy, and SciPy (COO/CSR/CSC) arrays.
- Bounded memory:
chunk_sizecaps parallel AD passes for large color counts. - Visualizations:
spyplots and braille pattern previews.
Documentation
- Getting Started: step-by-step tutorial
- How-To Guides: task-oriented recipes
- Explanation: how and why it works
- API Reference: full API documentation
- Contributing: guidelines for collaborating on asdex
- AI Policy: guidelines for LLM contributions
Related work
Prior work on ASD by asdex's authors Adrian Hill (@adrhill) and Guillaume Dalle (@gdalle), as well as Alexis Montoison (@amontoison):
- An Illustrated Guide to Automatic Sparse Differentiation, Hill, Dalle, Montoison (2025)
- Sparser, Better, Faster, Stronger: Efficient Automatic Differentiation for Sparse Jacobians and Hessians, Hill & Dalle (2025)
- Revisiting Sparse Matrix Coloring and Bicoloring, Montoison, Dalle, Gebremedhin (2025)
SparseConnectivityTracer.jl, Hill & DalleSparseMatrixColorings.jl, Dalle & MontoisonDifferentiationInterface.jl, Dalle & Hill
Prior and concurrent (partial) attempts at ASD in JAX:
sparsejac: coloring and decompressionsparsediffax: coloring and decompression (by asdex's@gdalle)jax-nansparse: sparsity detection using NaN propagationJAX-AMG: specialized ASD module for algebraic multigrid methodstatva: specialized ASD module for FEM- See discussion in JAX issue #1032
Acknowledgements
Adrian Hill gratefully acknowledges funding from the German Federal Ministry of Education and Research under the grant BIFOLD26B.
This package is built with Claude Code, based on previous, hand-written work by the same authors in the Julia programming language, as noted above. These works in turn stand on the shoulders of giants, notably Andreas Griewank, Andrea Walther, and Assefaw Gebremedhin.
The asdex logo was designed by @overripemango.
Citation
If you use asdex in your research, please cite:
@software{asdex2026,
author = {Hill, Adrian},
title = {asdex: Automatic Sparse Differentiation in JAX},
url = {https://github.com/adrhill/asdex},
doi = {10.5281/zenodo.18788242}
}
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file asdex-0.5.1.tar.gz.
File metadata
- Download URL: asdex-0.5.1.tar.gz
- Upload date:
- Size: 503.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.11.28 {"installer":{"name":"uv","version":"0.11.28","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9fa01cd067689146a79a1adaf5aedf2e003bb63eb9ffab82d24518efb5b60648
|
|
| MD5 |
23b5f14d714ef03c961f059534a373c4
|
|
| BLAKE2b-256 |
71b60c99f48e8dd214efb274b3843ed617da6e92d29a7f9a244e2c60202cfdc3
|
File details
Details for the file asdex-0.5.1-py3-none-any.whl.
File metadata
- Download URL: asdex-0.5.1-py3-none-any.whl
- Upload date:
- Size: 144.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.11.28 {"installer":{"name":"uv","version":"0.11.28","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1ad9f71631e4aef8ab4264516b149d5cea47e5c250be6a606031b946867f296a
|
|
| MD5 |
3734d7bb5a113a7eaecf2c717e65b625
|
|
| BLAKE2b-256 |
1759a480a093d44633a447bb0fecc8593db9d76cbb2060030bf3f3eeef173748
|