fix: Npu Group MatMul op patchs only in EP#205
Open
0hujun wants to merge 3 commits into
Open
Conversation
Contributor
There was a problem hiding this comment.
Code Review
This pull request updates the NPU monkey patching logic to make the HuggingFace MoE Grouped MatMul (GMM) patch Expert Parallelism (EP) aware. It introduces _is_ep_enabled to check if EP is active and modifies the patching logic to skip GMM patching by default or when EP is not enabled, avoiding significant overhead from contiguous copies on transposed weights. The review comments point out two critical issues: a potential AttributeError when accessing model.device_mesh directly, and a logical contradiction where the default value for TWINKLE_NPU_GMM_PATCH is set to True instead of False, which conflicts with the intended behavior of skipping the patch by default when unset.
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
tastelikefeet
approved these changes
May 28, 2026
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.
PR type
PR information
When Expert Parallelism (EP) is not enabled, each rank holds all expert weights.
weight.transpose(-2, -1)produces a large non-contiguous view thatnpu_grouped_matmulforces to
.contiguous()(~12.88 GB per MoE layer), creating a bandwidth bottleneckthat makes the NPU GMM patch ~8x slower than the native per-expert fallback.
When EP is enabled, each rank holds only a subset of expert weights — small and
contiguous — making
npu_grouped_matmulefficient.Experiment results
TWINKLE_NPU_GMM_PATCH=1