Trace the engine's eval kernel under the mesh and the axis rules - #5144
Open
NuojCheng wants to merge 2 commits into
Open
Trace the engine's eval kernel under the mesh and the axis rules#5144NuojCheng wants to merge 2 commits into
NuojCheng wants to merge 2 commits into
Conversation
`fwd_bwd` and `update` wrap their kernel calls in `_sharding_ctx()`; `eval_step` was the one step path that did not, so the eval kernel was traced with no mesh in context and an empty logical axis rule set. Under `shard_mode=auto` that only costs partitioning quality, which is why it went unnoticed. Under explicit axis types it is fatal: the MaxText layers call `jax.sharding.reshard(x, P(...))`, and a bare `PartitionSpec` outside a mesh context is an error rather than a no-op, so `eval_step` raises ValueError: Using PartitionSpec when you are not under a mesh context is not allowed. Reproduced on qwen3.5-35b-a3b (v7-8, ep=8, shard_mode=explicit): eval_step died on the first call after training. With the context entered, the same run completes and eval costs 139.9 ms median per step -- against 1327.4 ms for the identical run under `auto`, since the eval kernel now gets the partitioning the training kernels already had. Wrapped around the call rather than around `_compile_eval_for_batch`, because `jax.jit` is lazy: the trace happens at the call, which is the pattern `fwd_bwd` and `update` follow.
NuojCheng
requested review from
A9isha,
RissyRan,
SurbhiJainUSC,
abhinavclemson,
aireenmei,
bvandermoon,
darisoy,
dipannita08,
gagika,
gobbleturk,
hengtaoguo,
huytransformer,
igorts-git,
jiangjy1982,
khatwanimohit,
richjames0,
shralex,
shuningjin,
vipannalla and
xibinliu
as code owners
September 4, 2026 17:49
There was a problem hiding this comment.
Code Review
This pull request wraps the evaluation kernel execution in eval_step within the sharding context (_sharding_ctx) to ensure that tracing occurs with the correct mesh and axis rules active. It also adds a unit test to verify this behavior for both compiled and uncompiled evaluation paths. There are no review comments, so no further feedback is provided.
4 tasks
W0102: binding the observation dict as a default argument to dodge the loop-closure warning trips dangerous-default-value instead. A helper method that runs one case and returns what it saw has neither problem.
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
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.
Description
fwd_bwdandupdatewrap their kernel calls in_sharding_ctx()— the engine'swith jax.set_mesh(self._mesh), nn_partitioning.axis_rules(config.logical_axis_rules).eval_stepwas the one step path that did not, so the eval kernel was traced with no meshin context and an empty logical axis rule set.
Under
shard_mode=autothat only costs partitioning quality — everymaybe_shard_with_logicalbecomes a no-op and XLA guesses — which is why it went unnoticed.Under explicit axis types it is fatal. The MaxText layers call
jax.sharding.reshard(x, P(...))(e.g.
models/qwen3.py:940), and a barePartitionSpecoutside a mesh context is an errorrather than a no-op:
So
eval_stepdoes not merely evaluate with bad partitioning undershard_mode=explicit—it raises on the first call.
The context is entered around the call, not around
_compile_eval_for_batch, becausejax.jitis lazy: tracing happens at the call. That is the patternfwd_bwdandupdatealready follow, and
_sharding_ctx's own docstring spells it out.Pre-existing; not introduced by any open PR. Independent of #5138 and #5143 — no file overlap
in the source hunk beyond the same method, and this branch is cut from
main.Tests
Unit —
tests/post_training/unit/maxtext_engine_test.py:test_eval_step_traces_under_the_mesh_and_axis_rulesreadsjax.sharding.get_abstract_mesh()and
nn_partitioning.get_axis_rules()from inside the loss fn, covering both eval branches(
compile()d and eager) via subTest. Verified red onmain— both subtests fail withAssertionError: True is not false : the eval kernel was traced with no mesh in context— andgreen with the fix. Full file: 51 passed.
(the
JAX_PLATFORMS=cpumatters — the suite silently skips without it)End-to-end — qwen3.5-35b-a3b on a v7-8 (8 devices),
ep=8, ring-of-experts + ragged-sort,GA=8, bf16, seq 1024, adamw, 23 steps, then 4
eval_stepcalls:shard_modeexpliciteval_stepraisesValueErroron the first callautoTraining step time and peak HBM are unchanged (4185.3 ms / 61.40 G explicit, 4043.8 ms /
61.39 G auto) — this only touches the eval path. The 9.5x explicit-vs-auto eval gap is the
point of the fix: with the rules live, the eval kernel gets the partitioning the training
kernels already had.
Checklist
Before submitting this PR, please make sure (put X in square brackets):
gemini-reviewlabel.