Repository navigation
ROCm packed varlen: use it inside compiled blocks only when its fake kernel matches its real outputs - #18393
Merged
Conversation
…o match its real outputs ROCm's aten._flash_attention_forward varlen returns a logsumexp of another rank than the op's fake kernel promises. Eager runs, so the in-graph probe passed, but every compiled decoder block then failed assert_size_stride at run time (gfx1151: 'wrong number of dimensions3 for op: aten._flash_attention_forward'). The probes now also compare the output and logsumexp shapes of a real call with a FakeTensorMode call; a mismatch keeps the eager varlen wrapper.
Member
Author
|
@codex review |
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. |
|
Codex Review: Didn't find any major issues. What shall we delve into next? 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". |
[skip ci]
danielhanchen
added a commit
to NilayYadav/unsloth
that referenced
this pull request
Nov 19, 2026
…d instead of attending over the dense mask (unslothai#18435) ## Summary Follow-up to unslothai#18393. On ROCm builds where torch's flash varlen cannot run inside a compiled graph (its fake kernel declares a logsumexp of another rank, seen on gfx1151 with torch 2.11), packed rows in a compiled decoder block now leave the block and run the eager varlen wrapper. Before this change they stayed in the block and attended with SDPA over the dense `[T, T]` mask, while `HF_PACKED_ATTENTION_STATS["compiled"]` counted them as varlen. ## Root cause `_block_varlen_prepare` (`unsloth/utils/hf_packed_attention.py`) returned `(cu_seqlens, carrier)` without asking whether the varlen op can run in the graph. Inside the block, `_varlen_in_graph` then returned False, `_leaves_block` is always False on ROCm (it only targets slow cuDNN plans on CUDA), so the call ended at the wrapped `orig(...)`: dense SDPA inside the compiled block. Before unslothai#18393 that block failed to compile and the stock eager forward ran varlen, so on ROCm unslothai#18393 traded a graph break for dense attention (unslothai#18313 measured dense SDPA at 98.5 ms vs 11 ms for varlen at 2048 tokens). ## Fix On ROCm only, for fp16 / bf16 rows with head dim up to 256, `_block_varlen_prepare` asks `_varlen_in_graph` (the same cached probe the traced call uses). When it refuses, it returns `_LEAVE_BLOCK`, as the int32 overflow branch already does, so `_sdpa_packed_varlen` calls the eager wrapper (`torch.compiler.disable`), which runs torch's varlen op. These rows are no longer counted as `compiled`. CUDA builds, ROCm builds whose varlen traces, and fp32 rows keep the current route. ## Tests New `tests/test_hf_packed_attention_rocm_leave_block.py` (CPU, ROCm faked through `torch.version.hip`, probes stubbed): - ROCm with the in-graph probe refusing: `block_varlen_kwargs` returns `_LEAVE_BLOCK`, the compiled counter does not move, and a compiled call reaches the eager wrapper and torch's varlen once with no dense SDPA call. - CUDA (probe True or False), ROCm with the probe passing, and ROCm fp32 rows keep `(cu_seqlens, carrier)` and count as compiled. Both arms on CPU (`-o addopts= -m "not server and not native_mxc"`), each importing its own tree: | Arm | New test file | `tests/test_hf_packed_attention.py` + new file | |---|---|---| | main (merge base) | 1 failed (`_LEAVE_BLOCK` expected, got the tuple), 4 passed | 1 failed, 76 passed, 102 skipped | | this branch | 5 passed | 77 passed, 102 skipped | ## Not proven here - AMD gfx1151 timing and parity with this change (packed SFT step time vs unslothai#18313's varlen numbers) need a real run through the AMD CI. - The GPU-marked tests in `tests/test_hf_packed_attention.py` were skipped on this CPU host.
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.
Follow-up to #18313. On ROCm, a compiled decoder block holding packed rows fails with
wrong number of dimensions3 for op: torch.ops.aten._flash_attention_forward.default.Cause. The in-graph varlen probes (
_varlen_in_graphinunsloth/utils/hf_packed_attention.py,_packed_varlen_ok_eagerinunsloth/utils/attention_dispatch.py) only ran torch's flash varlen op eagerly. Inside a compiled graph the op's outputs are checked against its fake kernel, and on ROCm the real logsumexp has a different rank than the fake kernel declares. So every compiled block with packed rows failed:CUDA's real and fake shapes match, so only a ROCm run shows it.
Fix. Both probes also compare the real call's output and logsumexp shapes with the fake kernel's (under
FakeTensorMode). On a mismatch the op is reported unusable inside the graph, and packed rows use the eager varlen wrapper, which works on ROCm. CUDA is unchanged: on B200 the probe still passes for head dims 64, 128 and 256 ([(16, 4, 128), (4, 16)]real and fake), and bf16 packed parity numbers are identical.AMD gfx1151 Linux (torch 2.11.0+rocm, transformers 5.23.0, tiny Qwen2.5-VL packed SFT), before run 44389058128, after run 44393091645:
aten._flash_attention_forwardtests/test_hf_packed_attention.pyTests. Three CPU tests: the shape check passes when the logsumexp rank matches, fails when it differs, and the probe refuses the in-graph path when it fails.