fix(gpt2): Resolve NaN/Inf issue in lm_head on Python 3.13 with tied weights#44676
Open
JokeYoonic wants to merge 1 commit intohuggingface:mainfrom
Open
fix(gpt2): Resolve NaN/Inf issue in lm_head on Python 3.13 with tied weights#44676JokeYoonic wants to merge 1 commit intohuggingface:mainfrom
JokeYoonic wants to merge 1 commit intohuggingface:mainfrom
Conversation
…weights Problem: - On macOS ARM64 + Python 3.13 + transformers 5.x, GPT-2 model's lm_head forward pass produces NaN/Inf values during inference - Root cause: lm_head.weight is tied to transformer.wte.weight, and the shared memory reference causes numerical instability in Python 3.13 Solution: - Clone the lm_head weight before passing to F.linear in GPT2LMHeadModel and GPT2DoubleHeadsModel forward methods - This breaks the memory sharing and resolves the NaN issue Changes: - src/transformers/models/gpt2/modeling_gpt2.py: Modified GPT2LMHeadModel.forward() and GPT2DoubleHeadsModel.forward() to use self.lm_head.weight.clone() Testing: - Verified fix with gpt2-medium model on Python 3.13.5 + PyTorch 2.6.0 - All existing GPT-2 model tests pass
Contributor
|
[For maintainers] Suggested jobs to run (before merge) run-slow: gpt2 |
Member
|
Hi, "shared memory reference causes numerical instability" doesn't make any sense to me, because "numerical instability" usually refers to errors in the least significant bits caused by floating point precision. Is the tied weight just not being handled correctly and we're getting randomly-initialized values instead? |
This was referenced Apr 29, 2026
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Problem:
Solution:
Changes:
Testing:
Reproduction Code
What does this PR do?
Fixes # (issue)
Before submitting
Pull Request section?
to it if that's the case.
documentation guidelines, and
here are tips on formatting docstrings.
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.