Skip to content

Always ensure correct dtype for _keep_in_fp32_modules_strict - #50975

Merged
Cyrilvallez merged 9 commits into
mainfrom
ensure-fp32
Nov 20, 2026
Merged

Cyrilvallez merged 9 commits into
mainfrom
ensure-fp32

Conversation

@Cyrilvallez

@Cyrilvallez Cyrilvallez commented Nov 19, 2026 •

Copy link
Copy Markdown
Member

🤗 Transformers CI

What does this PR do?

This PR makes sure that everywhere we a parameter/buffer/module inside _keep_in_fp32_modules_strict, then that param/buffer/module must be hardcoded with torch.float32 inside the __init__ during model construction. See also my comment here for context.
If we do not do it, then initializing a model with __init__ or from_config will NOT respect the dtype, which is wrong (only from_pretrained would respect it).
It also adds gate.weight inside _keep_in_fp32_modules_strict for MANY moe models, where that weight is always upcasted to fp32 (so we want to keep it always in fp32 instead of always upcasting inside forward)

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Model linter — first pass

transformers-mlinter found 1 item(s) in the model files this PR touches. These are structural conventions a maintainer would otherwise flag by hand.

This is automated and advisory — it does not block merging.

rule count what it checks
TRF055 1 PreTrainedModel classes declare their config with a config: SomeConfig annotation, matched by __init__(config: SomeConfig); never config_class.
1 item(s) outside this diff
  • src/transformers/models/voxtral/modular_voxtral.py:67 TRF055 — TRF055: generated modeling_voxtral.py:257: VoxtralEncoder.__init__ takes config: VoxtralConfig but the class overrides config: VoxtralEncoderConfig. Make them match; add or update config: VoxtralConfig in the class body if that is intended.

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

@Cyrilvallez Cyrilvallez changed the title Ensure correct dtype for _keep_in_fp32_modules_strict Always ensure correct dtype for _keep_in_fp32_modules_strict Nov 19, 2026
@github-actions

Copy link
Copy Markdown
Contributor

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

run-slow: afmoe, apertus1p5, axk1, axk2, bailing_hybrid, beam, deepseek_ocr2, deepseek_v2, deepseek_v3, deepseek_v32, deepseek_v4, dots1, dots3_note, ernie4_5_moe, ernie4_5_vl_moe, esmfold2

@Cyrilvallez
Cyrilvallez merged commit 42efcfd into main Nov 20, 2026
123 of 124 checks passed
@Cyrilvallez
Cyrilvallez deleted the ensure-fp32 branch November 20, 2026 13:21
Rocketknight1 added a commit that referenced this pull request Nov 23, 2026
The sinks, the hyper-connections and the router biases are listed in
`_keep_in_fp32_modules_strict`, but only `from_pretrained` enforces that
list. A model built straight from a config got them in the default dtype,
which the common test from #50975 now catches. They are created in
float32 in their `__init__` instead.

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

Not a fan of this tbh because now we need to keep track of this for all the keep fp32 flags no? It creates easy misses during model additions imo

ManaEstras pushed a commit to ManaEstras/transformers that referenced this pull request Nov 24, 2026
- Rename `routed_expert_hidden_size` to `moe_hidden_size` (keeping a BC alias for published
  checkpoints) and update `FP8Experts` to resolve `hidden_dim` via
  `_first_attr(config, "moe_hidden_size", "hidden_size")`.
- Inherit `HYV4FlashAttention` from `DeepseekV3Attention` using `config.per_layer_config[layer_idx]`
  and move `post_proj_norm` out of attention onto `HYV4FlashDecoderLayer.post_self_attn_layernorm`
  with a reversible `WeightRenaming` mapping.
- Remove redundant `position_ids.expand(3, -1, -1)` inside `HYV4FlashRotaryEmbedding.forward`,
  split the latent-space MoE steps onto separate lines with a comment, and mark the
  `vision_config.model_type` redirect with `# BC:`.
- Align with latest `main` (`huggingface#50379` `MultimodalMoeBaseModelOutputWithPast`, `huggingface#50975`
  fp32-strict parameter dtypes, `huggingface#50897` deprecated MoE TP plan entries).
- Fix CI checks: prune `HYV4VL*` submodel names from `_no_split_modules` (TRF022/TRF068), add a
  minimal `test_processing_hy_v4_flash.py` subclassing `HYV4VLProcessorTest` (TRF038 without
  touching `utils/rules.toml`), and add the model contribution date to `hy_v4_flash.md`.
Rocketknight1 added a commit that referenced this pull request Nov 24, 2026
The sinks, the hyper-connections and the router biases are listed in
`_keep_in_fp32_modules_strict`, but only `from_pretrained` enforces that
list. A model built straight from a config got them in the default dtype,
which the common test from #50975 now catches. They are created in
float32 in their `__init__` instead.
Rocketknight1 added a commit that referenced this pull request Nov 25, 2026
The sinks, the hyper-connections and the router biases are listed in
`_keep_in_fp32_modules_strict`, but only `from_pretrained` enforces that
list. A model built straight from a config got them in the default dtype,
which the common test from #50975 now catches. They are created in
float32 in their `__init__` instead.
XuehaiPan pushed a commit to XuehaiPan/transformers that referenced this pull request Nov 25, 2026
…huggingface#51067)

huggingface#50975 keeps `_keep_in_fp32_modules_strict` params (incl. router `gate.weight`) in fp32 at load:
- dots3_note: the bf16-only dtype check now expects fp32 for params matching `_keep_in_fp32_modules_strict`
- axk1: tiny-axk1 stores router weights in fp32 (no longer rounded to bf16), update CUDA (8, 6) expected logits

Co-authored-by: ydshieh <ydshieh@users.noreply.github.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Rocketknight1 added a commit that referenced this pull request Nov 26, 2026
The sinks, the hyper-connections and the router biases are listed in
`_keep_in_fp32_modules_strict`, but only `from_pretrained` enforces that
list. A model built straight from a config got them in the default dtype,
which the common test from #50975 now catches. They are created in
float32 in their `__init__` instead.
ManaEstras pushed a commit to ManaEstras/transformers that referenced this pull request Nov 30, 2026
- Rename `routed_expert_hidden_size` to `moe_hidden_size` (keeping a BC alias for published
  checkpoints) and update `FP8Experts` to resolve `hidden_dim` via
  `_first_attr(config, "moe_hidden_size", "hidden_size")`.
- Inherit `HYV4FlashAttention` from `DeepseekV3Attention` using `config.per_layer_config[layer_idx]`
  and move `post_proj_norm` out of attention onto `HYV4FlashDecoderLayer.post_self_attn_layernorm`
  with a reversible `WeightRenaming` mapping.
- Remove redundant `position_ids.expand(3, -1, -1)` inside `HYV4FlashRotaryEmbedding.forward`,
  split the latent-space MoE steps onto separate lines with a comment, and mark the
  `vision_config.model_type` redirect with `# BC:`.
- Align with latest `main` (`huggingface#50379` `MultimodalMoeBaseModelOutputWithPast`, `huggingface#50975`
  fp32-strict parameter dtypes, `huggingface#50897` deprecated MoE TP plan entries).
- Fix CI checks: prune `HYV4VL*` submodel names from `_no_split_modules` (TRF022/TRF068), add a
  minimal `test_processing_hy_v4_flash.py` subclassing `HYV4VLProcessorTest` (TRF038 without
  touching `utils/rules.toml`), and add the model contribution date to `hy_v4_flash.md`.
Rocketknight1 added a commit that referenced this pull request Dec 3, 2026
The sinks, the hyper-connections and the router biases are listed in
`_keep_in_fp32_modules_strict`, but only `from_pretrained` enforces that
list. A model built straight from a config got them in the default dtype,
which the common test from #50975 now catches. They are created in
float32 in their `__init__` instead.
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.

3 participants