Efficient forward- and reverse-mode sparse Jacobians using Jax.
Project description
sparsejac: Efficient sparse Jacobians using Jax
v0.2.0
Sparse Jacobians are frequently encountered in the simulation of physical systems. Jax tranformations jacfwd and jacrev make it easy to compute dense Jacobians, but these are wasteful when the Jacobian is sparse. sparsejac provides a function to more efficiently compute the Jacobian if its sparsity is known. It makes use of the recently-introduced jax.experimental.sparse module.
Install
pip install sparsejac
Example
A trivial example with a diagonal Jacobian follows:
fn = lambda x: x**2
x = jax.random.uniform(jax.random.PRNGKey(0), shape=(10000,))
@jax.jit
def sparse_jacrev_fn(x):
with jax.ensure_compile_time_eval():
sparsity = jax.experimental.sparse.BCOO.fromdense(jnp.eye(10000))
jacrev_fn = sparsejac.jacrev(fn, sparsity=sparsity)
return jacrev_fn(x)
dense_jacrev_fn = jax.jit(jax.jacrev(fn))
assert jnp.all(sparse_jacrev_fn(x).todense() == dense_jacrev_fn(x))
%timeit sparse_jacrev_fn(x).block_until_ready()
%timeit dense_jacrev_fn(x).block_until_ready()
And, the performance improvement can easily be seen:
93.1 µs ± 17.2 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
182 ms ± 26.9 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
Performance
- In general, it is preferable to directly provide the sparsity, rather than obtaining it from a dense matrix.
- GPU may show minimal or no performance advantage over CPU.
- Users are encouraged to test
jacrevandjacfwdon their specific problem to select the most performant option.
Project details
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 sparsejac-0.2.0.tar.gz.
File metadata
- Download URL: sparsejac-0.2.0.tar.gz
- Upload date:
- Size: 9.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.12.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7be53ed529f4c3aee6ff2635e3798290931033a87eb71b0cb7b15af741b7f3cf
|
|
| MD5 |
4f2395c452eb036644d351ba2e4a497d
|
|
| BLAKE2b-256 |
e36ae21c5d0d5e6e0120a9567eb20afd112148691eb377afdc863df46af54f49
|
File details
Details for the file sparsejac-0.2.0-py3-none-any.whl.
File metadata
- Download URL: sparsejac-0.2.0-py3-none-any.whl
- Upload date:
- Size: 6.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.12.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7c2ed4d3de9b52bdce3ff8a73dd1ebb9a1337a23d0499608cc6830e7fb0c8751
|
|
| MD5 |
75520b38ba89c136d6c7790deafa2946
|
|
| BLAKE2b-256 |
563c2946761fc10a15641970897c7970cb073963b93fa98d84b97f5dec4d6d88
|