Repository navigation
Fix small legacy MoE bugs: block_size_M=0 on direct calls, test env r… - #8966
Conversation
🏷️ CI GuideRuns automatically on every PR:
Extended tests (opt-in via labels):
One backend per PR: PR title tags & labels: Reviews: |
WAITING ON AUTHORPR Self-ReviewRevision Waiting for the PR author. The readiness report will appear after the self-review answers are complete for this revision. Questions to answer in the PR description
Edit the Missing or invalid answers
Shadow mode reports what would block but keeps the status successful. Set |
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues are recorded for the legacy MoE block-size normalization and test-cleanup changes.
No open issues 🎉
What changed in this PR
This PR fixes two legacy MoE edge cases: unset block sizes in direct implementation calls and delayed environment cleanup after a test failure. The public API remains unchanged.
Changes:
- Normalize
block_size_M=0toNonein_fused_moe_impl, matching the public wrapper’s behavior. - Close case generators in
finallyso environment overrides are restored promptly when a test case raises.
| File | Description |
|---|---|
op_tests/test_moe_2stage.py |
Adds guaranteed generator cleanup around the existing case loop. |
aiter/fused_moe.py |
Treats zero and None as unset block sizes alongside -1. |
Review effort: Balanced
…estore - _fused_moe_impl: treat block_size_M=0 (and None) as unset, like -1. Callers that bypass fused_moe (CommFusedMoeRuntime.run, fhmoe, AOT, tune_fhmoe) passed block_m 0 to the sort, which raised ZeroDivisionError. - test_moe_2stage: close the case generators in a try/finally so a failing case restores the overridden environment right away, not at interpreter shutdown. Co-authored-by: Cursor <[email protected]>
Summary
Fixes two small legacy MoE bugs:
_fused_moe_implcrashed when called directly withblock_size_M=0, andop_tests/test_moe_2stage.pyrestored its environment overrides only at interpreter shutdown after a failing case.Motivation
fused_moetreats a falsyblock_size_Mas "unset" (0 -> -1), and_fused_moe_implturns -1 intoNone. Callers that skipfused_moeand call_fused_moe_impldirectly (CommFusedMoeRuntime.run, fhmoe, the FlyDSL AOT path,tune_fhmoe) therefore passedblock_size_M=0through to the sort, which raisedZeroDivisionErrorinmoe_sorting.test_moe_2stage.py,_iter_with_envis a generator that holdsoverride_envcontexts in anExitStack. When a case raised, the generator stayed suspended and was finalized only at interpreter shutdown, afteroswas torn down, so the restore failed with "Exception ignored in ... AttributeError: 'NoneType' object has no attribute 'environ'". The run had already failed, but this hid where the environment was restored.Changes
aiter/fused_moe.py:_fused_moe_impltreatsblock_size_Mof 0 orNoneas unset, the same as -1. This covers every direct caller, matchingfused_moe. Positive block sizes are unchanged.op_tests/test_moe_2stage.py: the module-level case loop runs insidetry/finally, and thefinallycloses the case generators, so the environment is restored right away when a case raises. A normal run behaves the same (closing a finished generator does nothing). Most of the diff is re-indentation;git diff -wshows the real change.Performance (if applicable)
Not applicable: no kernel or dispatch-selection change.
Testing
Tested on MI350X (gfx950); see "Validation and unverified scope".
Documentation
These are internal bug fixes; the public
fused_moeAPI is unchanged.Dependencies
Breaking Changes
None.
PR Self-Review
Reviewed revision
32dd3d2
Behavior and intent
A direct
_fused_moe_impl(..., block_size_M=0)call used to raiseZeroDivisionErrorinmoe_sorting. This affectsCommFusedMoeRuntime.run, fhmoe, the FlyDSL AOT path andtune_fhmoe, which all call it directly.fused_moealready maps a falsyblock_size_Mto -1, but those callers skip that step. Now_fused_moe_impltreats 0 andNoneas unset, the same as -1, so these callers pick the block size the same wayfused_moedoes.op_tests/test_moe_2stage.pynow restores its environment overrides as soon as a case fails. Before, the suspended_iter_with_envgenerator restored them only at interpreter shutdown, afteroswas torn down. That restore failed with "Exception ignored in ... AttributeError: 'NoneType' object has no attribute 'environ'" and hid where the environment was restored.Validation and unverified scope
HIP_VISIBLE_DEVICES=1 PYTHONPATH=<worktree> python check.py). The script callsfused_moe(block_size_M=None)as the reference, then_fused_moe_implwithblock_size_Mset to 0,Noneand -1.block_size_M=0call raisesZeroDivisionErrorinaiter/moev2/segments/sort.py(_moe_sorting_impl).black --checkandruff checkpass on both changed files.CommFusedMoeRuntimepath end to end (it goes through the same_fused_moe_implline that was fixed);test_moe_2stage.pyrun with a failing case (thefinallyonly matters when a case raises). On a normal run the loop and its results are unchanged, because closing a finished generator does nothing.PR-specific question
No changed op test fails before this PR, because this PR adds or changes no test assertions.
The only
op_tests/change is to the harness oftest_moe_2stage.py. Its case loop now closes the case generators in afinally. Before this PR, a run where some case raised still failed, as it should. The only difference was a misleading "Exception ignored in ... 'NoneType' object has no attribute 'environ'" message at shutdown, and the environment restored late. A passing run behaves the same before and after.No existing canonical test covers this, because it is a property of this file's own case loop (
_iter_with_envand_case_iters), not of any op. So there is nothing to defer to, and no new test is needed for it.The
block_size_M=0fix in_fused_moe_implhas no op test in this PR. Its validation is the direct before/after GPU comparison above.Changed op tests
op_tests/test_moe_2stage.py: it covers the 2-stage MoE cases (tuned CSV and legacy sweeps), and this PR leaves those cases and their checks unchanged. The change is a harness fix: a failing case now restores the overridden environment right away, instead of at interpreter shutdown. It adds no coverage, so it duplicates nothing.