Skip to content

Fix small legacy MoE bugs: block_size_M=0 on direct calls, test env r… - #8966

Merged
zufayu merged 1 commit into
ROCm:mainfrom
qli88:bug_fix
Dec 2, 2026
Merged

zufayu merged 1 commit into
ROCm:mainfrom
qli88:bug_fix

Conversation

@qli88

@qli88 qli88 commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Fixes two small legacy MoE bugs: _fused_moe_impl crashed when called directly with block_size_M=0, and op_tests/test_moe_2stage.py restored its environment overrides only at interpreter shutdown after a failing case.

Motivation

  • fused_moe treats a falsy block_size_M as "unset" (0 -> -1), and _fused_moe_impl turns -1 into None. Callers that skip fused_moe and call _fused_moe_impl directly (CommFusedMoeRuntime.run, fhmoe, the FlyDSL AOT path, tune_fhmoe) therefore passed block_size_M=0 through to the sort, which raised ZeroDivisionError in moe_sorting.
  • In test_moe_2stage.py, _iter_with_env is a generator that holds override_env contexts in an ExitStack. When a case raised, the generator stayed suspended and was finalized only at interpreter shutdown, after os was 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_impl treats block_size_M of 0 or None as unset, the same as -1. This covers every direct caller, matching fused_moe. Positive block sizes are unchanged.
  • op_tests/test_moe_2stage.py: the module-level case loop runs inside try/finally, and the finally closes 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 -w shows the real change.

Performance (if applicable)

Not applicable: no kernel or dispatch-selection change.

Testing

  • Unit tests added or updated
  • Performance benchmarks run
  • Tested on MI250X
  • Tested on MI300X

Tested on MI350X (gfx950); see "Validation and unverified scope".

Documentation

  • Docstrings updated
  • User guide updated
  • Performance guide updated
  • No documentation change is needed, with the reason stated below

These are internal bug fixes; the public fused_moe API is unchanged.

Dependencies

  • No new third-party dependencies added
  • New dependencies are listed and justified below

Breaking Changes

None.

PR Self-Review

Reviewed revision

32dd3d2

Behavior and intent

A direct _fused_moe_impl(..., block_size_M=0) call used to raise ZeroDivisionError in moe_sorting. This affects CommFusedMoeRuntime.run, fhmoe, the FlyDSL AOT path and tune_fhmoe, which all call it directly. fused_moe already maps a falsy block_size_M to -1, but those callers skip that step. Now _fused_moe_impl treats 0 and None as unset, the same as -1, so these callers pick the block size the same way fused_moe does.

op_tests/test_moe_2stage.py now restores its environment overrides as soon as a case fails. Before, the suspended _iter_with_env generator restored them only at interpreter shutdown, after os was 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

  • MI350X (gfx950), bf16 no-quant, E=8, model_dim=1024, inter_dim=512, 32 tokens, top-2, run directly from the branch (HIP_VISIBLE_DEVICES=1 PYTHONPATH=<worktree> python check.py). The script calls fused_moe(block_size_M=None) as the reference, then _fused_moe_impl with block_size_M set to 0, None and -1.
    • With this PR: all three are bit-identical to the reference (max abs diff 0.0).
    • On unpatched main: the block_size_M=0 call raises ZeroDivisionError in aiter/moev2/segments/sort.py (_moe_sorting_impl).
  • black --check and ruff check pass on both changed files.
  • Not verified:
    • the multi-GPU CommFusedMoeRuntime path end to end (it goes through the same _fused_moe_impl line that was fixed);
    • a full test_moe_2stage.py run with a failing case (the finally only 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 of test_moe_2stage.py. Its case loop now closes the case generators in a finally. 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_env and _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=0 fix in _fused_moe_impl has 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.

@qli88
qli88 requested review from a team and a balanced review from Copilot November 30, 2026 23:05
@github-actions

Copy link
Copy Markdown
Contributor

🏷️ CI Guide

Runs automatically on every PR:

  • ✅ Pre-checks (submodule verification, code formatting)
  • ✅ Aiter standard tests on MI35X
  • ✅ Triton tests on MI35X (only when aiter/ops/triton/** or related paths are changed)

Extended tests (opt-in via labels):

Label Tests
ci:mi300x Run Aiter standard tests on MI300X in PRs; main branch always runs them
ci:gfx1250-ffm-triton Run the five-shard gfx1250 FFM Triton test suite
ci:triton-300x Run an additional Triton test job on MI300X in PRs (added automatically when gfx942 configs change); main branch always runs both MI35X and MI300X
ci:triton-355 Run the full Triton test suite on MI35X, not only the tests the change affects
ci:windows Windows JIT op smoke test on the Radeon R9700 ROCm runner
ci:multi-gpu Aiter multi-GPU tests on the 8-GPU runner
ci:sglang SGLang integration tests: DeepSeek-R1-MXFP4 accuracy, Qwen 3.5 accuracy
ci:atom ATOM benchmark: DeepSeek-R1-0528, GPT-OSS-120B
ci:atom_full ATOM accuracy suite for PR and main models from ATOM models_accuracy.json
ci:vllm vLLM benchmark: DeepSeek-V4-Pro, MiniMax-M3-MXFP4
ci:all All standard extended tests (excludes ci:atom_full)

Only add ci:atom_full for FlyDSL or Triton upgrades.
Add labels via the sidebar or gh pr edit 8966 --add-label <label>

One backend per PR:
A PR changes one kernel backend: [Triton/Gluon] (Triton and Gluon count as one), [HIP], [ASM], [CK], [OPUS] or [FlyDSL]. If the title ends up with two backend tags, split the PR -- as stacked pull requests when one part cannot merge without the other.

PR title tags & labels:
Component tags ([Triton/Gluon], [HIP], [CK], [ASM], ...) are added to the PR title and as PR labels automatically from the changed files and re-synced on every push — change-type tags like [fix]/[Perf], op tags like [MLA], and human labels (ci:*) are left untouched. Add the no-auto-title label to stop the title rewrites; labels stay in sync either way.

Reviews:
A reviewer is usually assigned within one working day after the PR is ready for review (see Code Review Process). If nobody has responded for about a week, mention the area contact listed in CONTACTS.md. Want to help review? See REVIEWERS.md.

@aiter-bot

aiter-bot commented Nov 30, 2026 •

Copy link
Copy Markdown

WAITING ON AUTHOR

PR Self-Review

Revision fe5af96ef882 · mode shadow

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

  1. What behavior does this PR change, and why is the existing behavior insufficient?
  2. What did you validate, and what remains unverified? Include commands, hardware/configuration and results.
  3. What behavior would each changed op test fail on before this PR, and why is that coverage not already provided by an existing canonical test?
  4. For every changed op_tests/ file, what behavior does it cover and why is it not redundant? Write None if no op test changed.

Edit the PR Self-Review section in the PR description. Include the current revision SHA; this comment refreshes automatically.

Missing or invalid answers

  • FAIL — Reviewed revision must contain at least the first 7 characters of the current head SHA

Shadow mode reports what would block but keeps the status successful. Set AITER_PR_SELF_REVIEW_MODE=enforce only after reviewing real false positives.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 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=0 to None in _fused_moe_impl, matching the public wrapper’s behavior.
  • Close case generators in finally so 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

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues were identified in the block-size normalization and test-generator cleanup changes.

No open issues 🎉

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]>
Copilot AI balanced review requested due to automatic review settings December 1, 2026 21:42

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The review reports no active issues with the MoE block-size normalization and test-generator cleanup changes.

No open issues 🎉

Review effort: Balanced

@zufayu
zufayu merged commit 5d07c44 into ROCm:main Dec 2, 2026
61 checks passed
@zufayu zufayu mentioned this pull request Dec 2, 2026
5 of 10 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants