Skip to content

Attention: packed fp32 rerouted to xformers keeps an fp32 output without autocast - #15410

Merged
danielhanchen merged 3 commits into
mainfrom
dh/s7-rc-fp32o
Oct 27, 2026
Merged

danielhanchen merged 3 commits into
mainfrom
dh/s7-rc-fp32o

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Problem

#14805 sends packed fp32 rows back to one xformers call when torch varlen is preferred and flash-attn is absent. On sm_100+ (B200, sm_120) xformers only runs its fp16 / bf16 flash op, so _run_attention downcasts Q/K/V to bf16 and returns the bf16 result. The per-segment SDPA that the reroute replaced returned fp32 when autocast is off.

A model loaded with dtype = torch.float32 and trained without autocast (bf16 = False, fp16 = False) then hands a bf16 tensor to an fp32 o_proj:

RuntimeError: expected mat1 and mat2 to have the same dtype, but got: c10::BFloat16 != float

Fix

When the packed fp32 reroute fires and autocast is off, the xformers output is cast back to the input dtype. Under autocast the output stays in the autocast dtype, which is what the per-segment SDPA returned too. The routing (one xformers launch per packed call) is unchanged.

Before / after

B200, torch 2.13, transformers 5.17, real xformers, no flash-attn. Tiny Llama (head_dim 64, 2 layers) loaded through FastLanguageModel with dtype = torch.float32, LoRA on q/k/v/o, packed forward (packed_seq_lengths = [40, 32, 24]) plus backward, no autocast, block compile off:

arm packed forward + backward xformers calls
main with _packed_fp32_prefers_xformers forced off (pre-#14805 routing) pass, loss 10.4728 0
main expected mat1 and mat2 to have the same dtype 1
this PR pass, loss 10.4729 4

Tests in tests/utils/test_attention_packed_fp32_routing.py:

test main this PR
test_packed_fp32_reroute_keeps_fp32_output_on_sm100 (CPU, mocked xformers) fail (fp16 output) pass
test_packed_fp32_cuda_feeds_an_fp32_o_proj (real CUDA + xformers) fail (bf16 output) pass
the 8 existing tests in the file pass pass

tests/utils/test_attention_dispatch_dora_dtype.py: 5 passed on B200.

Not covered

  • The dense (unpacked) xformers path on sm_100+ also downcasts fp32 and returns bf16, so an fp32 model without autocast fails there on main and before Attention: keep packed fp32 on one xformers call, read the fp32 downcast from Q's device #14805 alike. That is pre-existing and left alone here, since a DoRA model on a bf16 base without autocast may depend on the bf16 return (not tested).
  • Only B200 was run. Below sm_100 xformers runs fp32 natively, so no downcast happens there.

…out autocast

On sm_100+ the packed fp32 reroute to xformers (#14805) downcasts Q/K/V to bf16
for xformers' flash op and returned the bf16 result. The per-segment SDPA it
replaces returned fp32 without autocast, so an fp32 model's o_proj failed with
'expected mat1 and mat2 to have the same dtype'. Cast the output back to the
input dtype when autocast is off; autocast behaviour is unchanged.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 26, 2026 •

Copy link
Copy Markdown

Codex Review Summary

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

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-26T17:57:40.695265Z 14461c9 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.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Another round soon, please!

Reviewed commit: 14461c9c86

ℹ️ 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 f001b5b into main Oct 27, 2026
@danielhanchen
danielhanchen deleted the dh/s7-rc-fp32o branch October 27, 2026 00:34
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