mjx: preserve varying-axis metadata across Warp FFI calls - #3433
Open
guptaishaan wants to merge 1 commit into
Open
mjx: preserve varying-axis metadata across Warp FFI calls#3433guptaishaan wants to merge 1 commit into
guptaishaan wants to merge 1 commit into
Conversation
Warp builds its FFI output types from plain shape/dtype descriptors, which carry no manual-axis metadata, so every value returned by ffi_call comes back with an empty varying set. Under shard_map(check_vma=True) that is unsound: genuinely per-device data is reported as replicated. The call still succeeds and returns correct numbers, but a downstream lax.scan rejects the resulting carry-type mismatch. Restore the union of the inputs' varying axes in jax_callable_variadic_tuple, which every MJX Warp call funnels through. Only the missing axes are cast, since pcast has no varying -> varying rule, and the axis tuple is sorted so the traced jaxpr stays stable across processes under multi-controller SPMD. Adds ffi_test.py, which fakes a two-device mesh on CPU so the regression runs under ordinary CI rather than skipping without a GPU. Fixes google-deepmind#3426 Co-authored-by: Daniel Morton <40682860+danielpmorton@users.noreply.github.com> Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
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 #3426.
Credit for finding and diagnosing this goes to @danielpmorton, who filed the issue with a
minimal repro and a working fork. This PR builds directly on that patch; he is credited as
co-author on the commit. Happy to close this in favour of a PR from him if he'd prefer to
land it himself.
Root cause
Warp derives its FFI output types in
get_jax_output_type()from plain shape/dtypedescriptors, which carry no manual-axis metadata. Those go to
jax.ffi.ffi_call, so everyvalue coming back has an empty varying set.
Under
shard_map(..., check_vma=True)that is unsound rather than merely cosmetic: data thatis genuinely per-device is reported as replicated. The call still succeeds and returns
correct numbers — which is what makes it easy to miss.
lax.scanis only where it becomesfatal, because its carry-type equality check is the first thing to compare the two:
Approach
The fix restores the union of the inputs' varying axes onto the outputs — JAX's standard
propagation rule for an opaque op. Over-marking as varying is sound; under-marking is not.
One visible consequence worth calling out: code that declared
out_specs=P()expectingreplication will now get an error where it previously got silently-wrong results.
This is applied in
jax_callable_variadic_tuplerather than in the vendored Warp tree.Every MJX Warp call funnels through that wrapper (
bvh.py,smooth.py,render.py,forward.py,collision_driver.py, and the generated shim), so it covers MJX's wholesurface, and a patch to
mjx/mujoco/mjx/third_party/warp/would be reverted on the nextsync. The proper permanent home is upstream Warp, which would also cover
jax_kernel; theTODOin the code marks the removal condition.Two details in the implementation that are load-bearing and easy to "simplify" into bugs:
_pcast_funcshas novarying -> varyingentry, soa blanket
pcastover all outputs raisesUnsupported pcast.frozenset, andfrozensetiterationorder depends on per-process string hashing. Under multi-controller SPMD each host traces
independently, so an unsorted tuple can produce divergent jaxprs across hosts.
Tests
ffi_test.pyfakes a two-device mesh on CPU via--xla_force_host_platform_device_count=2, so this runs under ordinary CI instead ofskipping without a GPU. This works because the vendored Warp registers a
HostFFI target;released
warp-lang==1.14.0registers CUDA only.test_single_call_preserves_varying_axestest_varying_axes_survive_scan_carry(reported symptom)test_mixed_inputs_take_union_of_varying_axestest_replicated_input_stays_replicated(control)The first asserts the mechanism directly (
manual_axis_type.varying) rather than relying onscan's carry check, so it keeps testing this bug even ifscanchanges. The controlpasses in both states, showing the fix doesn't over-apply. I also confirmed the change is a
no-op under eager,
jit, and non-shardedscan.Open questions for reviewers
jax.lax.pcastneeds JAX >= 0.10, andmjx/pyproject.tomldeclares a bare
jax. I used ahasattrguard rather than adding a floor, since MJXotherwise uses no JAX >= 0.10 APIs and a floor would constrain all users for a
multi-device edge case. I did not establish whether the bug manifests on JAX 0.9 under the
older
.vma/pvaryspelling — if it does, the guard skips silently there. Happy toswitch to a version floor if you'd prefer.
🤖 Generated with Claude Code