[Frontend] Fix index/i64 type mismatch in expert-mask codegen (issue #228)#231
Merged
Merged
Conversation
…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.
53dc897 to
81cdc75
Compare
4 tasks
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.
Summary
PyTorchSimFrontend/mlir/mlir_ops.py:268that made the operand2-sideindex_castbranch inbinary_elementwise_commonunreachable. Any binary op withi64on the lhs andindexon the rhs silently fell through to the same-bit-width branch (MLIR_TO_BIT["i64"] == MLIR_TO_BIT["index"] == 64) and emittedarith.cmpibetweenvector<Nxi64>andvector<Nxindex>, whichmlir-optrejects.tests/test_expert_mask.pycovering the(expert_idx_i64.unsqueeze(-1) == arange(N)) -> torch.wherepattern that hit this ondeepseek_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
vector<8xi64>vsvector<8xindex>inarith.cmpi eq, same shape as the bug report).\$TORCHSIM_DUMP_PATH,tests/test_expert_mask.pyruns the full Gem5/Spike/TOGSim pipeline and printsExpertMask (i64 == arange) Test Passed.python scripts/op_coverage.py --models deepseek_v3on the reporter's branch.🤖 Generated with Claude Code