Treat JAX and NumPy arrays as interchangeable when deserialising - #1253
Open
JOhnsonKC201 wants to merge 1 commit into
Open
Treat JAX and NumPy arrays as interchangeable when deserialising#1253JOhnsonKC201 wants to merge 1 commit into
JOhnsonKC201 wants to merge 1 commit into
Conversation
Deserialising into a `filter_eval_shape` skeleton with a filter_spec that calls jax.device_get failed: the ShapeDtypeStructs in `like` are compared against the concrete array type, so a host-side np.ndarray was reported as a change of type even though shape and dtype matched. Compare array leaves by array-ness rather than exact type. Every other leaf type keeps the strict identity check, and the shape and dtype checks below are unchanged, so genuine mismatches are still caught.
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.
Fixes #861.
Problem
Deserialising into a
filter_eval_shapeskeleton fails if thefilter_specputs the result on the host:
Shape and dtype match. Only the array kind differs.
Cause
_assert_same_implsubstitutes the concrete array type for aShapeDtypeStructinlike, then compares withtypenew is not typeold. Thatidentity check leaves no room for a
filter_specto hand back a NumPy arrayinstead of a JAX one.
Fix
Compare array leaves by array-ness instead of exact type, which is what you
suggested in the issue: "Maybe we just consider all kinds of JAX and NumPy array
interchangeable?".
Every non-array leaf keeps the strict identity check, and the shape and dtype
checks immediately below are untouched, so real mismatches still raise. I
verified both: a shape mismatch still errors, and the behaviour for a
non-array
likeleaf is byte-for-byte the same as onmain.Test plan
test_eval_shape_deserialise_to_hostintests/test_serialisation.py.It fails on
mainwith theRuntimeErrorabove and passes with this change.passed, 4 skipped, so the only difference is the added test).
ruff checkandruff format --checkclean on both files.