Repository navigation
Cast a downcast attention output back for an fp32 o_proj outside autocast - #17807
Merged
Merged
Conversation
…cast run_attention downcasts fp32 Q / K / V to bf16 / fp16 for kernels without an fp32 path (FlashAttention, the xformers flash op on sm_100+) and returns that dtype. A float32 model outside autocast then fed it to its fp32 o_proj: RuntimeError mat1 / mat2 dtype mismatch in eval, inference and custom loops, and block compile fell back to the stock forward. DoRA on a half-precision model and autocast runs are unchanged.
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
Member
Author
|
@codex review |
|
Codex Review: Didn't find any major issues. You're on a roll. Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
danielhanchen
added a commit
to danielhanchen/unsloth-staging-2
that referenced
this pull request
Nov 16, 2026
Final comment pass for recently merged PRs. Trims comments added by unslothai#17660, unslothai#17806, unslothai#17807, unslothai#17808, unslothai#17812, unslothai#17830, unslothai#17854, unslothai#17857 and unslothai#17870 that are still on main. Multi-line explanations are collapsed to one line, and comments that restate the code are removed. Comments that explain a version quirk or a correctness trap are kept, along with license headers and pragmas. Changed comment lines: unslothai#17660 5 -> 2, unslothai#17806 4 -> 2, unslothai#17808 1 -> 0, unslothai#17830 3 -> 1, unslothai#17857 1 -> 0. unslothai#17807, unslothai#17812, unslothai#17854 and unslothai#17870 are unchanged. Overall: 6 files, 14 lines removed, 5 added. Only comments change: the AST check (comment_tools.py check) and the pre-push gate both pass.
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.
Fixes a dtype crash for float32 models running outside autocast.
Cause
run_attention(attention_dispatch.py) downcasts fp32 Q / K / V to bf16 / fp16 for kernels with no fp32 path, such as FlashAttention and the xformers flash op on sm_100+. It returns the attention output in that dtype.original_apply_othen fed the half-precision tensor into an fp32o_projand raisedRuntimeError: mat1 and mat2 must have the same dtype.This shows up in eval, inference and custom training loops that run fp32 without autocast. Under block compile the failing trial also sent the layer back to the stock forward.
Fix
In
original_apply_o, cast the attention output back to fp32 only when all three hold:o_projweight (or its LoRAbase_layerweight) is fp32.Half-precision models, quantized
o_proj, DoRA on half-precision models and autocast runs take the same path as before.Tests
New
tests/test_fp32_attention_output_dtype.py, CPU, under 1 s:On B200 (correctness only), the end-to-end GPU check passes with this PR on transformers 4.57.6 + TRL 0.22.2 and on 5.23.0 + TRL 1.18.0.