generated from fastai/nbdev_template
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Support casting to fp32 when word embeddings are tied to lm_head #4446
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
pramodith
merged 3 commits into
huggingface:main
from
pramodith:pramodith/support_tied_word_embeddings
Nov 4, 2025
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -479,21 +479,31 @@ def __init__( | |
|
|
||
| # Cast LM Head To FP32 | ||
| if args.cast_lm_head_to_fp32: | ||
| if not model.config.tie_word_embeddings: | ||
|
|
||
| def cast_inputs_to_fp32(module, input): | ||
| return (input[0].float(),) | ||
| def _cast_lm_head_to_fp32(target_model: PreTrainedModel): | ||
| """Cast lm_head to fp32 while preserving embedding output dtype if tied.""" | ||
|
|
||
| model.lm_head = model.lm_head.float() | ||
| model.lm_head.register_forward_pre_hook(cast_inputs_to_fp32) | ||
| if self.ref_model is not None: | ||
| self.ref_model.lm_head = self.ref_model.lm_head.float() | ||
| self.ref_model.lm_head.register_forward_pre_hook(cast_inputs_to_fp32) | ||
| else: | ||
| raise NotImplementedError( | ||
| "`cast_lm_head_to_fp32=True` is only supported when the model has untied word embedding and language modeling head layers" | ||
| "i.e. `tie_word_embeddings` in the model config is False." | ||
| ) | ||
| def cast_inputs_to_fp32(module, inputs): | ||
| # Preserve other positional args and kwargs untouched | ||
| if not inputs: | ||
| return inputs | ||
| return (inputs[0].to(torch.float32),) + inputs[1:] | ||
|
|
||
| original_dtype_local = target_model.lm_head.weight.dtype | ||
| target_model.lm_head = target_model.lm_head.float() | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. for the record, target_model.lm_head.float()it happens that |
||
| target_model.lm_head.register_forward_pre_hook(cast_inputs_to_fp32) | ||
|
|
||
| if target_model.config.tie_word_embeddings: | ||
|
|
||
| def cast_outputs_to_original_dtype(module, args, output): | ||
| return output.to(original_dtype_local) | ||
|
|
||
| # Only cast activations; weights are now fp32 (intentional for numerical stability of logits) | ||
| target_model.model.embed_tokens.register_forward_hook(cast_outputs_to_original_dtype) | ||
|
|
||
| _cast_lm_head_to_fp32(model) | ||
| if self.ref_model is not None: | ||
| _cast_lm_head_to_fp32(self.ref_model) | ||
|
|
||
| # Liger loss | ||
| if self.use_liger_kernel: | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Qwen3 has tied word embedding and Gemma 2 no, correct? If so, I'd just add a small comment so that we remember why we test these two cases
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's the other way around Qwen3 has untied and Gemma 2 has tied.