Fix Transformed/Chain for pytree-valued bijectors#92
Open
gvcallen wants to merge 2 commits into
Open
Conversation
`AbstractTransformed._infer_shapes_and_dtype` read `.shape`/`.dtype` directly off the traced output of `bijector.forward`, which only works if that output is a single array. For a bijector whose forward pass returns a pytree, `jax.eval_shape` instead returns a matching pytree of `ShapeDtypeStruct`s, so `.shape`/`.dtype` need to be mapped over it leaf-wise. `Chain.forward`/`inverse`/`forward_and_log_det`/`inverse_and_log_det` were also typed as `Array -> Array`, narrower than `AbstractBijector`'s own `PyTree -> PyTree`, even though `Chain` just composes whatever bijectors it's given. Widened these, along with the `Transformed`/`AbstractTransformed` methods that carry values through a bijector (`sample`, `sample_and_log_prob`, `log_prob`, `entropy`, `mean`, `mode`).
66a4b7d to
e03ceb8
Compare
lockwo
reviewed
Jul 6, 2026
| dummy = jnp.zeros(dummy_shape, dtype=self.distribution.dtype) | ||
| shape_dtype = jax.eval_shape(self.bijector.forward, dummy) | ||
| return shape_dtype.shape, shape_dtype.dtype | ||
| shapes = jax.tree_util.tree_map(lambda x: x.shape, shape_dtype) |
Owner
There was a problem hiding this comment.
can we do jax.tree.map over tree_util
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
AbstractTransformed._infer_shapes_and_dtyperead.shape/.dtypedirectly off the traced output ofbijector.forward, which only works if that output is a single array. For a bijector whose forward pass returns a pytree,jax.eval_shapeinstead returns a matching pytree ofShapeDtypeStructs, so.shape/.dtypeneed to be mapped over it leaf-wise.Chain.forward/inverse/forward_and_log_det/inverse_and_log_detwere also typed asArray -> Array, narrower thanAbstractBijector's ownPyTree -> PyTree, even thoughChainjust composes whatever bijectors it's given. This PR widens these, along with theTransformed/AbstractTransformedmethods that carry values through a bijector (sample,sample_and_log_prob,log_prob,entropy,mean,mode).