Repository navigation
Attention: packed fp32 rerouted to xformers keeps an fp32 output without autocast - #15410
Merged
Merged
Conversation
…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.
Member
Author
|
@codex review |
Codex Review SummaryThis 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. |
|
Codex Review: Didn't find any major issues. Another round soon, please! 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". |
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
#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_attentiondowncasts 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.float32and trained without autocast (bf16 = False,fp16 = False) then hands a bf16 tensor to an fp32o_proj: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
FastLanguageModelwithdtype = torch.float32, LoRA on q/k/v/o, packed forward (packed_seq_lengths = [40, 32, 24]) plus backward, no autocast, block compile off:_packed_fp32_prefers_xformersforced off (pre-#14805 routing)expected mat1 and mat2 to have the same dtypeTests in
tests/utils/test_attention_packed_fp32_routing.py:test_packed_fp32_reroute_keeps_fp32_output_on_sm100(CPU, mocked xformers)test_packed_fp32_cuda_feeds_an_fp32_o_proj(real CUDA + xformers)tests/utils/test_attention_dispatch_dora_dtype.py: 5 passed on B200.Not covered