Fix JAX transformation compatibility with functional updates#2
Merged
Conversation
…unctional updates Improves upon the dataloader transformations to ensure robust JAX compatibility: - Fixes both data_transformation and data_inv_transformation for jax.grad - Uses purely functional approach with jnp.concatenate instead of in-place mutations - Removes unnecessary copy.deepcopy calls - Works uniformly with NumPy and JAX arrays without type checking - Passes all jax.grad compatibility tests
There was a problem hiding this comment.
Pull Request Overview
This PR improves JAX transformation compatibility for the dataloader by replacing in-place array mutations with functional operations using jnp.concatenate, enabling these methods to work with jax.grad.
Key Changes
- Replaced in-place mutations with functional
jnp.concatenateoperations indata_transformationanddata_inv_transformation - Removed unnecessary
copy.deepcopycalls that were blocking JAX gradient flow - Added comprehensive JAX gradient compatibility tests
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
src/probabilistic_posrec/dataloader/base.py |
Refactored data_transformation and data_inv_transformation to use functional concatenation instead of in-place mutations, enabling JAX gradient compatibility |
tests/test_dataloader_jax_grad.py |
Added new test file with gradient compatibility tests for both transformation methods and round-trip precision validation |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
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.
Summary
This PR improves JAX compatibility for dataloader transformation methods by replacing incomplete PR #1 with a more robust functional approach.
Changes
data_transformationanddata_inv_transformationnow work withjax.gradjnp.concatenateinstead of in-place mutationscopy.deepcopycallsTest Results
✅ All 3 JAX grad compatibility tests pass:
test_transformation_grad_compatible[data_transformation]test_transformation_grad_compatible[data_inv_transformation]test_round_trip_grad_precisionWhy This Approach
The functional pattern is more idiomatic JAX code that naturally handles both NumPy and JAX arrays without conditional logic.