Skip to content

[loading] Fix forced upcasting to fp32 - #43683

Merged
Cyrilvallez merged 9 commits into
mainfrom
keep-fp32
Feb 2, 2026
Merged

[loading] Fix forced upcasting to fp32#43683
Cyrilvallez merged 9 commits into
mainfrom
keep-fp32

Conversation

@Cyrilvallez

@Cyrilvallez Cyrilvallez commented Feb 2, 2026

Copy link
Copy Markdown
Member

What does this PR do?

As per the title. #41580 broke the keep_in_fp32_modules flag as it's supposed to be used only with fp16, not bf16.
I added very strict tests on this to avoid name clashing/typos etc!

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

@github-actions

github-actions Bot commented Feb 2, 2026

Copy link
Copy Markdown
Contributor

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

run-slow: afmoe

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

Perfect

@Cyrilvallez Cyrilvallez mentioned this pull request Feb 2, 2026

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

Looks overall good to me just one thing to remove (dtype plan is still in the signature of the core model loadin fn) + 2 small questions

Comment thread src/transformers/core_model_loading.py
self.shared_experts = AfmoeMLP(config, config.moe_intermediate_size * config.num_shared_experts)
self.experts = AfmoeExperts(config)
self.expert_bias = nn.Parameter(torch.zeros(config.num_experts, dtype=torch.float32), requires_grad=False)
self.expert_bias = nn.Parameter(torch.zeros(config.num_experts), requires_grad=False)

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.

I suspect this happens a few times 😅 not to worry but maybe we need another pass to check

@Cyrilvallez Cyrilvallez Feb 2, 2026

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Well the tests only complained on this one, so all models with a _keep_in_fp32_modules(_strict) should be safe at least - for others, I guess/hope we have tests about the dtype in general

Comment on lines +887 to +891
# Make sure the modules correctly exist if the flag is active
if model._keep_in_fp32_modules is None and model._keep_in_fp32_modules_strict is None:
self.skipTest(
reason=f"{model_class.__name__} has no _keep_in_fp32_modules nor _keep_in_fp32_modules_strict attribute defined"
)

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.

Just a thought, unsure: What if we had top level models where only a subpart of that model has these flags, e.g. a VLM where the text model has flags but otherwise not. Would these be also caught here

@Cyrilvallez Cyrilvallez Feb 2, 2026

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

That's a good point that should probably come in a following PR (see also my comment on other question). Currently, most vlms do

if language_model._keep_in_fp32_modules is not None:
    self._keep_in_fp32_modules.extend(language_model._keep_in_fp32_modules)

which is very not ideal but works...

config, _ = self.model_tester.prepare_config_and_inputs_for_common()
for model_class in self.all_model_classes:
with self.subTest(model_class.__name__):
model = model_class(copy.deepcopy(config))

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.

Is there a reason we have to construct the model (previously it was inferred from the class)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Because of this comment and behavior!

# Overwrite the class attribute to make it an instance attribute, so models like
# `InstructBlipForConditionalGeneration` can dynamically update it without modifying the class attribute
# when a different component (e.g. language_model) is used. 
self._keep_in_fp32_modules = copy.copy(self.__class__._keep_in_fp32_modules)
self._keep_in_fp32_modules_strict = copy.copy(self.__class__._keep_in_fp32_modules_strict)

@Cyrilvallez
Cyrilvallez merged commit fb22e73 into main Feb 2, 2026
26 checks passed
@Cyrilvallez
Cyrilvallez deleted the keep-fp32 branch February 2, 2026 15:39
@Cyrilvallez Cyrilvallez added the for patch Tag issues / labels that should be included in the next patch label Feb 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

for patch Tag issues / labels that should be included in the next patch

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants