Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Add Xiaomi MiMo-V2#45144

Merged
vasqu merged 110 commits into
huggingface:mainfrom
casinca:xiaomi-mimo-v2
Jul 1, 2026
Merged

Add Xiaomi MiMo-V2#45144
vasqu merged 110 commits into
huggingface:mainfrom
casinca:xiaomi-mimo-v2

Conversation

@casinca

@casinca casinca commented Mar 31, 2026

Copy link
Copy Markdown
Contributor

CI

What does this PR do?

Hello, this PR aims to add the MiMo-V2-Flash model to the Transformers library
Fixes #42954

MiMo-V2 is "The last of the OSS SOTAs" that isn't natively supported by the Transformers library (besides Kimi), so I hope we can make this work.

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

  • This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
  • Did you read the contributor guideline,
    Pull Request section?
  • Was this discussed/approved via a Github issue or the forum? Please add a link
    to it if that's the case.
  • Did you make sure to update the documentation with your changes? Here are the
    documentation guidelines, and
    here are tips on formatting docstrings.
  • Did you write any new necessary tests?

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.

Only pinging HF engineers who were involved in the PRs mentioned below:

Modular Sensei @vasqu , I took your comments from the other PR, to find the best candidates to inherit from. Although see #45144 (comment) for some important points.

@ArthurZucker for gpt-oss similarity and text model

For reference, there were already 2 previous PRs (1 closed and 1 non modular/abandoned):

 

Afaik, MiMo in the codebase is a bit novel as it's the only hybrid SWA that combines both dual theta RoPE and dual head
dims + attn sinks (like gpt-oss). Most similar models are either one or the other, but not both.
So modularity wasn't easy mode pass, pass, pass.

I added some verbose NOTES for context/choices, and make the review easier. I'll obviously remove these once things are settled.

Additional useful info

(copy pasta from my own readme impl)

  • First layer is fully dense (GA+FFN, not MoE)
  • No shared experts in MoE
  • SWA and GA layers have different RoPE theta bases
  • SWA and GA layers have a different number of KV groups (GQA)
  • Values head dim is decoupled from QK head dim
  • Values are rescaled by sqrt(hidden_size / (num_attention_heads * v_head_dim))
  • Attention sink is only applied for SWA layers
  • Partial RoPE (rotating first 33% of the head dim)
  • Triple dtype with 1 quant

@casinca

casinca commented Mar 31, 2026

Copy link
Copy Markdown
Contributor Author

Important points:


fixed in: #45441

MiMoV2FlashTopKRouter:
Atm it is not a modular implementation inheriting from the existing DeepseekV3TopKRouter in transformers, for two reasons:

The native DSV3 router uses masked_fill=0.0 instead of masked_fill=-inf when masking non-selected expert groups. The remote DSV3 repo was fixed (see https://huggingface.co/deepseek-ai/DeepSeek-V3-0324/commit/e9b33add76883f293d6bf61f6bd89b497e80e335#d2h-632685) and remote MiMo also follows the fixed version. So I had to follow the correct behavior.

If it makes sense to change this masking in the repo for DSV3, then I can try to inherit something.

The class atm combines the 2 split DeepseekV3TopkRouter and DeepseekV3MoE.route_tokens_to_experts() to serve as a drop-in replacement in MixtralSparseMoeBlock, and simply override self.gate. This follows the newer pattern used by MiniMax-M2 for fused expert etc...

@vasqu

vasqu commented Apr 2, 2026

Copy link
Copy Markdown
Collaborator

Will try to take a look, likely next week after torch conference. Bad timing with holidays + conference sorry 😢 appreciate the work tho, just as a heads up that things are a bit delayed

@vasqu vasqu added the New model label Apr 2, 2026
@casinca

casinca commented Apr 2, 2026

Copy link
Copy Markdown
Contributor Author

Will try to take a look, likely next week after torch conference. Bad timing with holidays + conference sorry 😢 appreciate the work tho, just as a heads up that things are a bit delayed

Ah true, np, I understand. Thanks for letting me know.

@vasqu vasqu left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some initial comments: I think the biggest point is re attention I'd rather disable the attention types than to support all these paths - it gets too messy

Comment thread docs/source/en/model_doc/mimo_v2_flash.md Outdated
Comment thread docs/source/en/model_doc/mimo_v2_flash.md Outdated
Comment thread docs/source/en/model_doc/mimo_v2_flash.md Outdated
Comment thread docs/source/en/model_doc/mimo_v2_flash.md Outdated
Comment thread docs/source/en/model_doc/mimo_v2_flash.md
Comment thread src/transformers/models/mimo_v2_flash/modular_mimo_v2_flash.py Outdated
Comment thread src/transformers/models/mimo_v2_flash/modular_mimo_v2_flash.py Outdated
Comment thread src/transformers/models/mimo_v2_flash/modular_mimo_v2_flash.py Outdated
Comment thread src/transformers/models/mimo_v2_flash/modular_mimo_v2_flash.py Outdated
Comment thread src/transformers/models/mimo_v2_flash/modular_mimo_v2_flash.py Outdated
@vasqu

vasqu commented Apr 15, 2026

Copy link
Copy Markdown
Collaborator

Feel free to ping me when it's ready for another review 🤗

@casinca

casinca commented Jun 30, 2026

Copy link
Copy Markdown
Contributor Author

Updated with the recent changes, CI should be good now? Not sure that Mistral tokenizer failure is related.

Ye that's disappointing, even the V2.5 follows the same old config patterns. In any case you can ping me anytime, if you have any news I'll do a follow-up if need be @vasqu thanks again.

@vasqu

vasqu commented Jul 1, 2026

Copy link
Copy Markdown
Collaborator

run-slow: mimo_v2_flash

@vasqu

vasqu commented Jul 1, 2026

Copy link
Copy Markdown
Collaborator

@casinca just moved/copied the dummy model to internal testing, sanity checking with run slow but then merging 🤗

@github-actions

github-actions Bot commented Jul 1, 2026

Copy link
Copy Markdown
Contributor

Workflow Run ⚙️

This comment contains run-slow, running the specified jobs:

models: ["models/mimo_v2_flash"]
quantizations: []

@github-actions

github-actions Bot commented Jul 1, 2026

Copy link
Copy Markdown
Contributor

CI Results

Workflow Run ⚙️

Commit Info

Context Commit Description
RUN 1df57d3f workflow commit (merge commit)
PR 97432842 branch commit (from PR)
main 36778030 base commit (on main)

✅ No failing test specific to this PR 🎉 👏 !

@vasqu vasqu enabled auto-merge July 1, 2026 12:44
@github-actions

github-actions Bot commented Jul 1, 2026

Copy link
Copy Markdown
Contributor

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

run-slow: auto, mimo_v2_flash

@github-actions

github-actions Bot commented Jul 1, 2026

Copy link
Copy Markdown
Contributor

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

run-slow: auto, mimo_v2_flash

@github-actions

github-actions Bot commented Jul 1, 2026

Copy link
Copy Markdown
Contributor

CI recap

Dashboard: View test results in Grafana
Latest run: 28524616411:3
Result: failure | Jobs: 1 | Tests: 1,134 | Failures: 0 | Duration: 36m 28s

@vasqu vasqu added this pull request to the merge queue Jul 1, 2026
@github-merge-queue github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Jul 1, 2026
@vasqu vasqu added this pull request to the merge queue Jul 1, 2026
@github-merge-queue github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Jul 1, 2026
@vasqu vasqu added this pull request to the merge queue Jul 1, 2026
@github-merge-queue github-merge-queue Bot removed this pull request from the merge queue due to no response for status checks Jul 1, 2026
@vasqu vasqu added this pull request to the merge queue Jul 1, 2026
@github-merge-queue github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Jul 1, 2026
@vasqu vasqu added this pull request to the merge queue Jul 1, 2026
@github-merge-queue github-merge-queue Bot removed this pull request from the merge queue due to no response for status checks Jul 1, 2026
@vasqu vasqu added this pull request to the merge queue Jul 1, 2026
@github-merge-queue github-merge-queue Bot removed this pull request from the merge queue due to no response for status checks Jul 1, 2026
@vasqu vasqu merged commit 9f909e0 into huggingface:main Jul 1, 2026
192 of 194 checks passed
@vasqu

vasqu commented Jul 1, 2026

Copy link
Copy Markdown
Collaborator

Congrats on the merge !! 🤗 This was super annoying, CI being flaky and all...

@casinca casinca deleted the xiaomi-mimo-v2 branch July 1, 2026 19:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

XiaoMi MiMo

5 participants