-
-
Notifications
You must be signed in to change notification settings - Fork 9
Add Split bijector #82
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
Open
gvcallen
wants to merge
1
commit into
lockwo:main
Choose a base branch
from
gvcallen:split
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| from typing import Union | ||
|
|
||
| import equinox as eqx | ||
| import jax.numpy as jnp | ||
| from jaxtyping import Array | ||
|
|
||
| from ._bijector import ( | ||
| AbstractBijector, | ||
| AbstractForwardInverseBijector, | ||
| AbstractFwdLogDetJacBijector, | ||
| AbstractInvLogDetJacBijector, | ||
| ) | ||
|
|
||
|
|
||
| class Split( | ||
| AbstractForwardInverseBijector, | ||
| AbstractInvLogDetJacBijector, | ||
| AbstractFwdLogDetJacBijector, | ||
| ): | ||
| """A bijector that splits a single array into a tuple of arrays along an axis. | ||
|
|
||
| This operates as a wrapper around `jax.numpy.split`. | ||
| """ | ||
|
|
||
| indices_or_sections: Union[int, tuple[int, ...]] = eqx.field(static=True) | ||
| axis: int = eqx.field(static=True) | ||
|
|
||
| _is_constant_jacobian: bool = True | ||
| _is_constant_log_det: bool = True | ||
|
|
||
| def __init__( | ||
| self, | ||
| indices_or_sections: Union[int, tuple[int, ...], list[int]], | ||
| axis: int = -1, | ||
| ): | ||
| """Initializes a Split bijector. | ||
|
|
||
| **Arguments:** | ||
|
|
||
| - `indices_or_sections`: If an integer `N`, the array will be divided into | ||
| `N` equal arrays along axis. If a tuple/list of sorted integers, the entries | ||
| indicate where along axis the array is split. | ||
| - `axis`: The axis along which to split. Defaults to -1 (last axis). | ||
| """ | ||
| # Ensure lists are converted to tuples so they remain hashable for JAX JIT | ||
| if isinstance(indices_or_sections, list): | ||
| indices_or_sections = tuple(indices_or_sections) | ||
|
|
||
| self.indices_or_sections = indices_or_sections | ||
| self.axis = axis | ||
|
|
||
| def forward_and_log_det(self, x: Array) -> tuple[tuple[Array, ...], Array]: | ||
| """Computes y = tuple(split(x)) and log|det J(f)(x)| = 0.""" | ||
| y = tuple(jnp.split(x, self.indices_or_sections, axis=self.axis)) | ||
| return y, jnp.zeros((), dtype=x.dtype) | ||
|
|
||
| def inverse_and_log_det(self, y: tuple[Array, ...]) -> tuple[Array, Array]: | ||
| """Computes x = concatenate(y) and log|det J(f^{-1})(y)| = 0.""" | ||
| x = jnp.concatenate(y, axis=self.axis) | ||
| dtype = y[0].dtype if y else jnp.float32 | ||
| return x, jnp.zeros((), dtype=dtype) | ||
|
|
||
| def same_as(self, other: AbstractBijector) -> bool: | ||
| """Returns True if this bijector is guaranteed to be the same as `other`.""" | ||
| return ( | ||
| type(other) is Split | ||
| and self.indices_or_sections == other.indices_or_sections | ||
| and self.axis == other.axis | ||
| ) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| # Split Bijector | ||
|
|
||
| ::: distreqx.bijectors.Split | ||
| options: | ||
| members: | ||
| - __init__ | ||
| --- |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| import jax | ||
|
|
||
| # Must be set before any JAX arrays are initialized. Living here (rather than in | ||
| # individual test files) guarantees it runs before any test module import. | ||
| jax.config.update("jax_enable_x64", True) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| from unittest import TestCase | ||
|
|
||
| import equinox as eqx | ||
| import jax | ||
| import jax.numpy as jnp | ||
| import numpy as np | ||
| from parameterized import parameterized # type: ignore | ||
|
|
||
| from distreqx.bijectors import Split | ||
|
|
||
|
|
||
| class SplitTest(TestCase): | ||
| def setUp(self): | ||
| # Bijector 1: Split into 3 equal sections | ||
| self.bij_equal = Split(indices_or_sections=3, axis=-1) | ||
|
|
||
| # Bijector 2: Split at specific indices | ||
| # (elements up to index 2, from 2 to 5, and from 5 onward) | ||
| self.bij_indices = Split(indices_or_sections=(2, 5), axis=-1) | ||
|
|
||
| def assertion_fn(self, rtol=1e-5): | ||
| return lambda x, y: np.testing.assert_allclose(x, y, rtol=rtol) | ||
|
|
||
| @parameterized.expand([("float32", jnp.float32), ("float64", jnp.float64)]) | ||
| def test_forward_and_log_det_equal_sections(self, name, dtype): | ||
| x = jnp.array([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], dtype=dtype) | ||
| y, log_det = self.bij_equal.forward_and_log_det(x) | ||
|
|
||
| self.assertIsInstance(y, tuple) | ||
| self.assertEqual(len(y), 3) | ||
| self.assertion_fn()(y[0], jnp.array([1.0, 2.0], dtype=dtype)) | ||
| self.assertion_fn()(y[1], jnp.array([3.0, 4.0], dtype=dtype)) | ||
| self.assertion_fn()(y[2], jnp.array([5.0, 6.0], dtype=dtype)) | ||
|
|
||
| self.assertEqual(log_det.shape, ()) | ||
| self.assertEqual(log_det, 0.0) | ||
| self.assertEqual(log_det.dtype, dtype) | ||
|
|
||
| @parameterized.expand([("float32", jnp.float32), ("float64", jnp.float64)]) | ||
| def test_forward_and_log_det_indices(self, name, dtype): | ||
| x = jnp.array([1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0], dtype=dtype) | ||
| y, log_det = self.bij_indices.forward_and_log_det(x) | ||
|
|
||
| self.assertIsInstance(y, tuple) | ||
| self.assertEqual(len(y), 3) | ||
| self.assertion_fn()(y[0], jnp.array([1.0, 2.0], dtype=dtype)) # x[:2] | ||
| self.assertion_fn()(y[1], jnp.array([3.0, 4.0, 5.0], dtype=dtype)) # x[2:5] | ||
| self.assertion_fn()(y[2], jnp.array([6.0, 7.0], dtype=dtype)) # x[5:] | ||
|
|
||
| @parameterized.expand([("float32", jnp.float32), ("float64", jnp.float64)]) | ||
| def test_inverse_and_log_det(self, name, dtype): | ||
| y = ( | ||
| jnp.array([1.0, 2.0], dtype=dtype), | ||
| jnp.array([3.0, 4.0], dtype=dtype), | ||
| jnp.array([5.0, 6.0], dtype=dtype), | ||
| ) | ||
| x, log_det = self.bij_equal.inverse_and_log_det(y) | ||
|
|
||
| self.assertion_fn()(x, jnp.array([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], dtype=dtype)) | ||
|
|
||
| self.assertEqual(log_det.shape, ()) | ||
| self.assertEqual(log_det, 0.0) | ||
| self.assertEqual(x.dtype, dtype) | ||
| self.assertEqual(log_det.dtype, dtype) | ||
|
|
||
| def test_jittable(self): | ||
| @eqx.filter_jit | ||
| def f_forward(bij, x): | ||
| return bij.forward_and_log_det(x) | ||
|
|
||
| @eqx.filter_jit | ||
| def f_inverse(bij, y): | ||
| return bij.inverse_and_log_det(y) | ||
|
|
||
| x = jnp.ones((6,)) | ||
| y, log_det_fwd = f_forward(self.bij_equal, x) | ||
|
|
||
| self.assertIsInstance(y, tuple) | ||
| self.assertIsInstance(log_det_fwd, jax.Array) | ||
|
|
||
| x_reconstructed, log_det_inv = f_inverse(self.bij_equal, y) | ||
| self.assertIsInstance(x_reconstructed, jax.Array) | ||
| self.assertIsInstance(log_det_inv, jax.Array) | ||
|
|
||
| def test_same_as(self): | ||
| same_bij = Split(indices_or_sections=3, axis=-1) | ||
| diff_bij_1 = Split(indices_or_sections=4, axis=-1) | ||
| diff_bij_2 = Split(indices_or_sections=3, axis=0) | ||
|
|
||
| self.assertTrue(self.bij_equal.same_as(same_bij)) | ||
| self.assertFalse(self.bij_equal.same_as(diff_bij_1)) | ||
| self.assertFalse(self.bij_equal.same_as(diff_bij_2)) | ||
|
|
||
| def test_list_to_tuple_conversion(self): | ||
| # A list should be converted to a tuple upon initialization for hashability | ||
| bij_list = Split(indices_or_sections=[2, 5]) | ||
| self.assertIsInstance(bij_list.indices_or_sections, tuple) | ||
| self.assertTrue(self.bij_indices.same_as(bij_list)) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think transformed has to be updated first since it does not support Split outputs. Split.forward() returns a tuple of arrays, but Transformed._infer_shapes_and_dtype() assumes jax.eval_shape(...) returns one array-like object with .shape and .dtype
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, is this not updated in my latest PR?