fix(test): resolve flakiness on moe_tests.py#4518
Open
mesakhcienet wants to merge 1 commit into
Open
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
mesakhcienet
force-pushed
the
fix/routed-moe-test
branch
from
July 20, 2026 03:58
4aaa113 to
b390dc6
Compare
mesakhcienet
marked this pull request as ready for review
July 20, 2026 03:58
mesakhcienet
requested review from
A9isha,
NuojCheng,
RissyRan,
SurbhiJainUSC,
abhinavclemson,
aireenmei,
bvandermoon,
darisoy,
dipannita08,
gagika,
gobbleturk,
hengtaoguo,
huytransformer,
igorts-git,
jiangjy1982,
khatwanimohit,
richjames0,
shralex,
suexu1025,
vipannalla and
xibinliu
as code owners
July 20, 2026 03:58
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
This PR resolves persistent flakiness in the
tests/unit/moe_test.pysuite observed specifically in the Nightly CI pipelines. It addresses the root cause of the numerical drift by implementing a stablefloat32reference comparison.Problem Context
Recent changes in the bleeding-edge XLA compiler (pulled in the nightly pipeline) introduced slight variations in the accumulation order for
bfloat16matrix multiplications.Because our original tests were running both the reference implementation (
get_expected_output) and the production kernel (get_moe_output) inbfloat16, we were doing a "noise-vs-noise" comparison. The combined compiler drift on both sides of the equation pushed the accumulation error to~0.015, exceeding our strict0.01test tolerance.What We Tried & Why We Chose This Solution
Initially (in a reverted commit), we tried simply raising the
rtolandatolfrom1e-2to5e-2.5e-2tolerance is massive and completely masks real logic regressions, such as MoE routing collapse bugs (e.g., returning allNaNs, which passed silently becauseequal_nan=Falsewas dropped).float32Reference (The Correct Fix)We changed the reference
MoeLoopBlockto compute entirely injnp.float32, while keeping the actual production code inbfloat16.~0.0058, safely passing the strict0.01threshold without needing to weaken our test bounds.Implementation Details
get_expected_outputis now initialized withdtype=jnp.float32andhidden_statesare upcasted before the forward pass.float32_gate_logits=Trueto thepyconfig.initializeblocks. This ensures that thefloat32reference and thebfloat16production code make identical Top-K routing decisions, preventing massive divergence caused by floating-point rounding at the routing boundary.assert_moe_close: We removed the opaqueself.assertTrue(jax.numpy.allclose(...))wrappers and introduced a custom helper. This runsnp.testing.assert_allcloseto provide rich diffs, and explicitly prints themax_diffandmax_diff/RMSbefore evaluating. This drastically improves observability for future compiler changes.BUGS: https://buganizer.corp.google.com/issues/536702731
Tests
To prove the fix works, we reproduced the failing CI environment locally:
bash src/dependencies/scripts/setup.sh MODE=nightlyto pull the bleeding-edge XLA/libtpu dependencies that trigger the math drift.python3 -m pytest tests/unit/moe_test.pyon themainbranch to observe the legitimate failures.float32reference fix and reran the suite.max_diffhovered at0.0058and all tests passed perfectly within the original strictrtol=2e-2,atol=1e-2bounds.We also run the chages on the github workflow runner in here

Checklist
Before submitting this PR, please make sure (put X in square brackets):
gemini-reviewlabel.