Fix orthogonal_ init for low-precision dtypes (bf16/fp16)#47252
Open
janbernloehr wants to merge 2 commits into
Open
Fix orthogonal_ init for low-precision dtypes (bf16/fp16)#47252janbernloehr wants to merge 2 commits into
janbernloehr wants to merge 2 commits into
Conversation
torch.nn.init.orthogonal_ relies on torch.linalg.qr, whose geqrf kernel is only implemented for float32/float64 (on both CPU and CUDA). Since v5 materializes weights in the requested dtype before _init_weights runs, any QR-based init on a bf16/fp16 tensor crashes with e.g. NotImplementedError: "geqrf_cuda" not implemented for 'BFloat16' (RWKV being one affected model). Run the init in float32 and copy the result back for low-precision floating dtypes, on any device. Fixes huggingface#47225 Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Contributor
CI recapDashboard: View test results in Grafana |
Rocketknight1
left a comment
Member
There was a problem hiding this comment.
LGTM! Can we drop the new test file, though, and instead just add a small test into an existing file? With the comment this is unlikely to regress too, so even dropping the test entirely is fine I think.
Address review: drop the standalone test file and keep a single condensed low-precision regression test in an existing file. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Contributor
|
Thank you for your contribution 🤗! CI Security Gate — automatic approval blockedThis PR was not automatically approved for CI because the security gate failed. Possible reasons:
See the workflow run for the exact violations. A maintainer can review and manually approve CI if a finding is a false positive. |
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.
What does this PR do?
torch.nn.init.orthogonal_relies ontorch.linalg.qr, whosegeqrfkernel is only implemented for float32/float64 — on both CUDA ("geqrf_cuda" not implemented for 'BFloat16') and CPU ("geqrf_cpu" not implemented for 'BFloat16'). Since v5 materializes weights in the requested dtype before_init_weightsruns, constructing any model that uses orthogonal init (e.g. RWKV) withdtype=torch.bfloat16/float16crashes:This PR makes the
orthogonal_wrapper insrc/transformers/initialization.pyrun the QR-based init in a temporary float32 tensor (same device) and copy the result back when the target dtype is a floating dtype below 32 bits. Full-precision behavior is unchanged, and the_is_hf_initializedguard is preserved. Sinceguard_torch_init_functionspatches the torch modules with these wrappers, remote-code models callingtorch.nn.init.orthogonal_directly are covered as well.Added
tests/utils/test_initialization.py: verifies bf16/fp16 init succeeds and produces finite, orthogonal rows scaled bygain(fails withNotImplementedErrorwithout the fix), that the fp32 path exactly matchestorch.nn.init.orthogonal_, and that_is_hf_initializedis respected. Also validated end-to-end: RWKVfrom_configin bf16 now constructs on CPU and CUDA (H100), and trains with decreasing loss (see issue).Fixes #47225
Disclosure: this fix was implemented with the assistance of a code agent (Claude), per my proposal in the linked issue, and reviewed and validated by me.
Before submitting
geqrf_cuda not implemented for BFloat16under v5 bf16 on-device init (RWKV and any QR-based init) #47225Who can review?
@Cyrilvallez (author of the v5 weight-initialization refactor in #42191)