Skip to content

Compute Step-3.7 router in float32 - #51521

Open
qgallouedec wants to merge 3 commits into
huggingface:mainfrom
qgallouedec:step3p7-fp32-router
Open

qgallouedec wants to merge 3 commits into
huggingface:mainfrom
qgallouedec:step3p7-fp32-router

Conversation

@qgallouedec

@qgallouedec qgallouedec commented Nov 30, 2026 •

Copy link
Copy Markdown
Member

🤗 Transformers CI

Resolves #50440.

Compute Step-3.7 router logits in FP32 with autocast disabled, matching the reference implementation and its need_fp32_gate=true checkpoint. The cast is unconditional: FP32 weights are unaffected, and BF16 autocast no longer downcasts the matmul.

  • On H100, the issue's repro changes from 51/1024 mismatched expert sets to 0/1024.
  • The regression test checks exact FP32 routing with and without BF16 autocast.
import torch
from transformers import Step3p7TextConfig
from transformers.models.step3p7.modeling_step3p7 import Step3p7TopKRouter

torch.manual_seed(0)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
config = Step3p7TextConfig(hidden_size=128, n_routed_experts=288, num_experts_per_tok=8)
router = Step3p7TopKRouter(config)
torch.nn.init.normal_(router.weight, std=0.02)
router = router.to(device=device, dtype=torch.bfloat16).eval()
hidden_states = torch.randn(1024, config.hidden_size, device=device, dtype=torch.bfloat16)
with torch.no_grad():
    logits, _, _ = router(hidden_states)
print(logits.dtype)  # torch.float32

Who can review?

@zucchini-nlp

@HuggingFaceDocBuilderDev

Copy link
Copy Markdown

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

@zucchini-nlp zucchini-nlp left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

can we do it with _keep_in_fp32_modules_strict, i think that is the recommended way as per https://github.com/huggingface/transformers/pull/50722/changes

@qgallouedec

Copy link
Copy Markdown
Member Author

Thanks @zucchini-nlp! Tried it on a 288-expert, top-8 router loaded from a bf16 checkpoint. Share of tokens routed to the same experts as an fp32 reference:

no autocast autocast (bf16)
main 95.3% 95.3%
this PR 100% 100%
main + gate in _keep_in_fp32_modules_strict 100% 95.3%

So keeping the gate in fp32 fixes inference, but under autocast F.linear still runs in bf16. And since the checkpoint stores the gate in bf16 (unlike MiMo in #50722), keeping it in fp32 doesn't add precision either.

@github-actions

github-actions Bot commented Dec 2, 2026

Copy link
Copy Markdown
Contributor

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

run-slow: step3p7

@zucchini-nlp zucchini-nlp left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

ahh i see, makes sense! Looing at the linked ai slop issues, seems like DS models also would need autocast guard tho? in main we force fp32 and have th weights in keep_fp32_strict but no autocast

I just want to make sure we're fixing it once for all models. Approving so i'm not blocking you, feel free to merge if DS routers don't need that

This branch has not been deployed

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

Step-3.7: need_fp32_gate is ignored for BF16 router weights

3 participants