Skip to content

[Frontend] Fix index/i64 type mismatch in expert-mask codegen (issue #228)#231

Merged
YWHyuk merged 1 commit into
developfrom
bugfix/issue-228-moe-mask-index-cast
May 22, 2026
Merged

[Frontend] Fix index/i64 type mismatch in expert-mask codegen (issue #228)#231
YWHyuk merged 1 commit into
developfrom
bugfix/issue-228-moe-mask-index-cast

Conversation

@YWHyuk
Copy link
Copy Markdown
Collaborator

@YWHyuk YWHyuk commented May 22, 2026

Summary

  • Fix one-token typo at PyTorchSimFrontend/mlir/mlir_ops.py:268 that made the operand2-side index_cast branch in binary_elementwise_common unreachable. Any binary op with i64 on the lhs and index on the rhs silently fell through to the same-bit-width branch (MLIR_TO_BIT["i64"] == MLIR_TO_BIT["index"] == 64) and emitted arith.cmpi between vector<Nxi64> and vector<Nxindex>, which mlir-opt rejects.
  • Add tests/test_expert_mask.py covering the (expert_idx_i64.unsqueeze(-1) == arange(N)) -> torch.where pattern that hit this on deepseek_v3.

Fixes #228.

The CLAUDE.md cache-replay gotcha I bumped into while verifying this is moved to PR #230 with the CI-allowlist note, since both are docs.

Test plan

  • Reproduce the MLIR type-mismatch crash with the new test on the unpatched tree (vector<8xi64> vs vector<8xindex> in arith.cmpi eq, same shape as the bug report).
  • After applying the fix and clearing \$TORCHSIM_DUMP_PATH, tests/test_expert_mask.py runs the full Gem5/Spike/TOGSim pipeline and prints ExpertMask (i64 == arange) Test Passed.
  • Re-verify python scripts/op_coverage.py --models deepseek_v3 on the reporter's branch.

🤖 Generated with Claude Code

…228)

The second clause of the index-cast guard at mlir_ops.py:268 compared
the whole [tile_size, dtype] list against the string "index", which
is never true. With i64 on the lhs and index on the rhs the code fell
through to the same-bit-width branch (MLIR_TO_BIT["i64"] ==
MLIR_TO_BIT["index"] == 64) and emitted arith.cmpi between
vector<Nxi64> and vector<Nxindex>, which mlir-opt rejects. This
blocked any MoE model whose (i64_buf == arange) expert-mask pattern
landed with that operand orientation -- first observed on
deepseek_v3.

Fix replaces the dead clause with op_type2[1] == "index" so the
operand2-side index_cast at lines 285-288 is reachable, normalizing
the rhs to i64 before the cmpi.

Add tests/test_expert_mask.py as a focused regression covering the
(expert_idx_i64.unsqueeze(-1) == arange(N)) -> torch.where pattern.
@YWHyuk YWHyuk force-pushed the bugfix/issue-228-moe-mask-index-cast branch from 53dc897 to 81cdc75 Compare May 22, 2026 11:50
@YWHyuk YWHyuk merged commit 0f5c60a into develop May 22, 2026
2 checks passed
@YWHyuk YWHyuk deleted the bugfix/issue-228-moe-mask-index-cast branch May 22, 2026 12:13
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.

1 participant