Skip to content

Add Xiaomi MiMo-V2#45144

Open
casinca wants to merge 7 commits intohuggingface:mainfrom
casinca:xiaomi-mimo-v2
Open

Add Xiaomi MiMo-V2#45144
casinca wants to merge 7 commits intohuggingface:mainfrom
casinca:xiaomi-mimo-v2

Conversation

@casinca
Copy link
Copy Markdown
Contributor

@casinca casinca commented Mar 31, 2026

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 SOTAs" that isn't natively supported by the Transformers library, 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.

@casinca
Copy link
Copy Markdown
Contributor Author

casinca commented Mar 31, 2026

The main modular anchor was to follow minimax M2 for the newer changes/V5/fused MoE
LoC: 584 (modular) vs 692 (generated) - saved 108 LoC (15.6%)

Important points:


edit: CI TRF009 is temporarily ignored to allow this
I'm currently importing the modules to make dual eager attention functions work temporarily, while waiting for
#45141 to be fixed.
This would allow disentanglement between sink and non-sink layers. Allowing users to benefits from SDPA/FA2/flex backends for the non-sink GA layers.


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...

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 1, 2026

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

run-slow: auto, mimo_v2_flash

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.

XiaoMi MiMo

1 participant