Skip to content

ROCm packed varlen: use it inside compiled blocks only when its fake kernel matches its real outputs - #18393

Merged
danielhanchen merged 2 commits into
mainfrom
rocm-varlen-meta-probe
Nov 18, 2026
Merged

danielhanchen merged 2 commits into
mainfrom
rocm-varlen-meta-probe

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

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_graph in unsloth/utils/hf_packed_attention.py, _packed_varlen_ok_eager in unsloth/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:

  • block compile dropped the block and kept the stock forward ("block compile failed ... keeping the stock forward"), losing the compiled speed;
  • a path with no such recovery crashed at run time.

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:

Cell Before After
bf16 packed vs per-document parity crash in aten._flash_attention_forward passes (logit diff 4.9e-3, leak 0.0, negative control live)
fp32 parity passes passes (logit 8.4e-7, grad rel 1.8e-6)
packed SFT trains, but "block compile failed" twice trains, no block compile failures; packed rows still run in compiled blocks
tests/test_hf_packed_attention.py 120 passed, 0 failed

Tests. 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.

…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.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 18, 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-18T20:49:55.311408Z f37cea5 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. What shall we delve into next?

Reviewed commit: f37cea50ce

ℹ️ 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 df68a27 into main Nov 18, 2026
@danielhanchen
danielhanchen deleted the rocm-varlen-meta-probe branch November 18, 2026 21:15
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.
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