Skip to content

Fix torch.export compatibility for Mixtral MoE experts#46929

Open
Sarimsaljook wants to merge 1 commit into
huggingface:mainfrom
Sarimsaljook:fix-mixtral-export
Open

Fix torch.export compatibility for Mixtral MoE experts#46929
Sarimsaljook wants to merge 1 commit into
huggingface:mainfrom
Sarimsaljook:fix-mixtral-export

Conversation

@Sarimsaljook

Copy link
Copy Markdown

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:

  • Mixtral torch.export repro passes and prints "export ok".
  • Mixtral test suite: 155 passed, 111 skipped, 1216 subtests passed.

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 static range(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 the torch.export compatibility issue.

Environment:

  • torch=2.11.0+cu128
  • 24 threads
  • hidden_size=4096
  • intermediate_size=14336
  • num_local_experts=8
  • num_experts_per_tok=2

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.

  • I confirm that this is not a pure code agent PR.

Before submitting

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

@github-actions

Copy link
Copy Markdown
Contributor

[For maintainers] Suggested jobs to run (before merge)

run-slow: mixtral

@github-actions

Copy link
Copy Markdown
Contributor

CI Dashboard: View test results in Grafana

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.

Failed to export PyTorch traced graph of Mixtral-8x7B-Instruct-v0.1 due to the PR #32429

1 participant