Fix torch.export compatibility for Mixtral MoE experts#46929
Open
Sarimsaljook wants to merge 1 commit into
Open
Fix torch.export compatibility for Mixtral MoE experts#46929Sarimsaljook wants to merge 1 commit into
Sarimsaljook wants to merge 1 commit into
Conversation
Contributor
|
[For maintainers] Suggested jobs to run (before merge) run-slow: mixtral |
Contributor
|
CI Dashboard: View test results in Grafana |
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.
What does this PR do?
Fixes #38518.
This PR fixes torch.export compatibility for Mixtral MoE experts. The previous implementation used nonzero() on expert_mask to build a dynamic Python loop over only the selected experts. This can make the number of loop iterations depend on routing results, which breaks torch.export with GuardOnDataDependentSymNode.
This PR replaces that data-dependent loop with a static loop over range(self.num_experts). Empty experts naturally receive empty token indices and contribute nothing, so the output is unchanged while the graph becomes export-safe.
Local validation:
One concern raised in the earlier discussions on this issue was that iterating over all experts could introduce a significant inference slowdown. To better understand that tradeoff, I ran a local benchmark comparing the current data-dependent
.nonzero()implementation against the staticrange(self.num_experts)loop used in this PR. On my setup, the static-loop version produced identical outputs (max_diff=0) and remained within roughly 0.1%–6.1% of the current implementation across the tested decode and prefill scenarios. While this is only a local measurement, it suggests the practical overhead may be much smaller than initially expected while resolving thetorch.exportcompatibility issue.Environment:
Results:
decode b=1 s=1 old=16.39 ms new=16.72 ms ratio=1.020x max_diff=0.00e+00
decode b=4 s=1 old=48.31 ms new=48.60 ms ratio=1.006x max_diff=0.00e+00
prefill b=1 s=128 old=110.08 ms new=116.77 ms ratio=1.061x max_diff=0.00e+00
prefill b=1 s=512 old=409.01 ms new=409.49 ms ratio=1.001x max_diff=0.00e+00
prefill b=1 s=2048 old=1340.33 ms new=1404.58 ms ratio=1.048x max_diff=0.00e+00
Code Agent Policy
The Transformers repo is currently being overwhelmed by a large number of PRs and issue comments written by
code agents. We are currently bottlenecked by our ability to review and respond to them. As a result,
we ask that new users do not submit pure code agent PRs at this time.
You may use code agents in drafting or to help you diagnose issues. We'd also ask autonomous "OpenClaw"-like agents
not to open any PRs or issues for the moment.
PRs that appear to be fully agent-written will probably be closed without review, and we may block users who do this
repeatedly or maliciously.
This is a rapidly-evolving situation that's causing significant shockwaves in the open-source community. As a result,
this policy is likely to be updated regularly in the near future. For more information, please read
CONTRIBUTING.md.Before submitting
Pull Request checks?
to it if that's the case. (fixes Failed to export PyTorch traced graph of Mixtral-8x7B-Instruct-v0.1 due to the PR #32429 #38518)
Who can review?
Anyone in the community is free to review the PR once the tests have passed. Feel free to tag
members/contributors who may be interested in your PR.
@ArthurZucker @Cyrilvallez