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.
Metadata
Release files for sparsejac 0.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| sparsejac-0.2.0.tar.gz | 9.0 kB | Without provenance | Details for sparsejac-0.2.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| sparsejac-0.2.0-py3-none-any.whl | Python 3 | none | any | Details for sparsejac-0.2.0-py3-none-any.whl |
Total release size: 15.6 kB
Release files / sparsejac-0.2.0.tar.gz
| Download URL | sparsejac-0.2.0.tar.gz |
|---|---|
| Size | 9.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
7be53ed529f4c3aee6ff2635e3798290931033a87eb71b0cb7b15af741b7f3cf
|
|
BLAKE2b-256 checksum How to use checksums |
e36ae21c5d0d5e6e0120a9567eb20afd112148691eb377afdc863df46af54f49
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.0.1 CPython/3.12.7
|
Release files / sparsejac-0.2.0-py3-none-any.whl
| Download URL | sparsejac-0.2.0-py3-none-any.whl |
|---|---|
| Size | 6.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
7c2ed4d3de9b52bdce3ff8a73dd1ebb9a1337a23d0499608cc6830e7fb0c8751
|
|
BLAKE2b-256 checksum How to use checksums |
563c2946761fc10a15641970897c7970cb073963b93fa98d84b97f5dec4d6d88
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.0.1 CPython/3.12.7
|
Security for sparsejac 0.2.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.