Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bump version and add tests for kwargs #18

Merged
merged 2 commits into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .bumpversion.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[tool.bumpversion]
current_version = "v0.1.3"
current_version = "v0.2.0"
commit = true
commit_args = "--no-verify"
tag = true
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# sparsejac: Efficient sparse Jacobians using Jax
`v0.1.3`
`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.

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[project]

name = "sparsejac"
version = "v0.1.3"
version = "v0.2.0"
description = "Efficient forward- and reverse-mode sparse Jacobians using Jax."
keywords = ["jax", "jacobian", "sparse"]
readme = "README.md"
Expand Down
2 changes: 1 addition & 1 deletion src/sparsejac/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""sparsejac - Efficient forward- and reverse-mode sparse Jacobians using Jax."""

__version__ = "v0.1.3"
__version__ = "v0.2.0"
__author__ = "Martin Schubert <[email protected]>"

from sparsejac.sparsejac import jacfwd as jacfwd
Expand Down
26 changes: 26 additions & 0 deletions tests/test_sparsejac.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,19 @@ def fn(x):
onp.testing.assert_array_equal(expected_jac, result_jac.todense())
onp.testing.assert_array_equal(expected_aux, result_aux)

def test_kwargs(self):
def fn(x, y):
return y * jnp.convolve(x, jnp.asarray([1.0, -2.0, 1.0]), mode="same") ** 2

x = jax.random.uniform(jax.random.PRNGKey(0), shape=(_SIZE,))
i, j = jnp.meshgrid(jnp.arange(_SIZE), jnp.arange(_SIZE), indexing="ij")
sparsity = (i == j) | ((i - 1) == j) | ((i + 1) == j)
sparsity = jsparse.BCOO.fromdense(sparsity)

result_jac = sparsejac.jacrev(fn, sparsity)(x, y=1)
expected_jac = jax.jacrev(fn)(x, y=1)
onp.testing.assert_array_equal(expected_jac, result_jac.todense())


class JacfwdTest(unittest.TestCase):
def test_sparsity_shape_validation(self):
Expand Down Expand Up @@ -350,6 +363,19 @@ def fn(x):
onp.testing.assert_array_equal(expected_jac, result_jac.todense())
onp.testing.assert_array_equal(expected_aux, result_aux)

def test_kwargs(self):
def fn(x, y):
return y * jnp.convolve(x, jnp.asarray([1.0, -2.0, 1.0]), mode="same") ** 2

x = jax.random.uniform(jax.random.PRNGKey(0), shape=(_SIZE,))
i, j = jnp.meshgrid(jnp.arange(_SIZE), jnp.arange(_SIZE), indexing="ij")
sparsity = (i == j) | ((i - 1) == j) | ((i + 1) == j)
sparsity = jsparse.BCOO.fromdense(sparsity)

result_jac = sparsejac.jacfwd(fn, sparsity)(x, y=1)
expected_jac = jax.jacfwd(fn)(x, y=1)
onp.testing.assert_array_equal(expected_jac, result_jac.todense())


class ConnectivityFromSparsityTest(unittest.TestCase):
def test_output_connectivity_matches_expected(self):
Expand Down