Fix torch.export compatibility for Mixtral MoE experts
Author: Sarimsaljook
Pull request: yes
Labels: name: Code agent slop

# What does this PR do?

<!--
Congratulations! You've made it this far! You're not quite done yet though.

Once merged, your PR is going to appear in the release notes with the title you set, so make sure it's a great title that fully reflects the extent of your awesome contribution.

Then, please replace this with a description of the change and which issue is fixed (if applicable). Please also include relevant motivation and context. List any dependencies (if any) that are required for this change.

Once you're done, someone will review your PR shortly (see the section "Who can review?" below to tag some potential reviewers). They may suggest changes to make the code even better. If no one reviewed your PR after a week has passed, don't hesitate to post a new comment @-mentioning the same persons---sometimes notifications get lost.
-->

<!-- Remove if not applicable -->

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`](https://github.com/huggingface/transformers/blob/main/CONTRIBUTING.md).

- [X] I confirm that this is not a pure code agent PR.

## Before submitting
- [ ] This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
- [X] Did you read the [contributor guideline](https://huggingface.co/docs/transformers/contributing) and the
      [Pull Request](https://huggingface.co/docs/transformers/pr_checks) checks?
- [X] Was this discussed/approved via a Github issue or the [forum](https://discuss.huggingface.co/)? Please add a link
      to it if that's the case. (fixes #38518)
- [ ] Did you make sure to update the documentation with your changes according to the [guidelines](https://github.com/huggingface/transformers/tree/main/docs)?
- [ ] Did you write any new necessary [tests](https://huggingface.co/docs/transformers/testing)?


## 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.

<!-- Your PR will be replied to more quickly if you can figure out the right person to tag with @

 If you know how to use git blame, that is the easiest way, otherwise, here is a rough guide of **who to tag**.
 Please tag fewer than 3 people.

Models:

- text models: @ArthurZucker @Cyrilvallez @vasqu
- vision models: @yonigozlan @molbap
- audio models: @eustlb @ebezzam @vasqu
- multimodal models: @zucchini-nlp
- graph models: @clefourrier

Library:

- generate: @zucchini-nlp (visual-language models) or @Cyrilvallez (all others)
- continuous batching: @remi-or @ArthurZucker @McPatate
- pipelines: @Rocketknight1
- tokenizers: @ArthurZucker and @itazap
- trainer: @SunMarc
- attention: @vasqu @ArthurZucker @CyrilVallez
- model loading (from pretrained, etc): @CyrilVallez
- distributed: @3outeille @ArthurZucker
- CIs: @ydshieh

Integrations:

- ray/raytune: @richardliaw, @amogkam
- Big Model Inference: @SunMarc
- quantization: @SunMarc
- kernels: @vasqu @drbh
- peft: @BenjaminBossan @githubnemo

Devices/Backends:

- AMD ROCm: @ivarflakstad
- Intel XPU: @IlyasMoutawwakil
- Ascend NPU: @ivarflakstad 

Documentation: @stevhliu

Research projects are not maintained and should be taken as is.

 -->

@ArthurZucker @Cyrilvallez 

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

run-slow: mixtral

**CI Dashboard:** [View test results in Grafana](https://transformers-ci.lor-e.huggingface.cool/d/pytest-observability-by-pr/pytest-observability-branch?var-pr=46929)