Skip to main content

Efficient forward- and reverse-mode sparse Jacobians using Jax.

Project description

sparsejac: Efficient forward- and reverse-mode sparse Jacobians using Jax.

v0.1.3

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)

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

sparsejac-0.1.3.tar.gz (8.1 kB view details)

Uploaded Source

Built Distribution

sparsejac-0.1.3-py3-none-any.whl (6.5 kB view details)

Uploaded Python 3

File details

Details for the file sparsejac-0.1.3.tar.gz.

File metadata

  • Download URL: sparsejac-0.1.3.tar.gz
  • Upload date:
  • Size: 8.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.12.2

File hashes

Hashes for sparsejac-0.1.3.tar.gz
Algorithm Hash digest
SHA256 8aee7fcd1834ebbd1414000111eef9882622ace346719175a02f7f6d4141edeb
MD5 69ee91e1140e2d9a9a101347b101bca9
BLAKE2b-256 be2e2f666e974d3566cd2a6276d465d10f829e3583f927c301cb8e20d5848838

See more details on using hashes here.

File details

Details for the file sparsejac-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: sparsejac-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 6.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.12.2

File hashes

Hashes for sparsejac-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 0016f226ebec7e4173d41c2d8b7d97c6904f5d327b5db53ce3cd72a6d63eba73
MD5 4721dee8231f1876c5837ac4a1730297
BLAKE2b-256 9bdcb838506896332e920764d7898ec0c33879b9eb8ef39f782f3eb656d57dae

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page