Skip to content

Cast a downcast attention output back for an fp32 o_proj outside autocast - #17807

Merged
danielhanchen merged 1 commit into
mainfrom
fp32-o-proj-dtype
Nov 15, 2026
Merged

danielhanchen merged 1 commit into
mainfrom
fp32-o-proj-dtype

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

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_o then fed the half-precision tensor into an fp32 o_proj and raised RuntimeError: 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:

  • it is fp16 / bf16;
  • autocast is off;
  • the o_proj weight (or its LoRA base_layer weight) 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:

  • main: 2 failed, 1 passed;
  • this PR: 3 passed.

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.

…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.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 15, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-15T09:56:52.627613Z 51ac977 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. You're on a roll.

Reviewed commit: 51ac977207

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

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
danielhanchen merged commit 2b08644 into main Nov 15, 2026
@danielhanchen
danielhanchen deleted the fp32-o-proj-dtype branch November 15, 2026 12:03
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant