Skip to content

Fused decode RoPE: drop the per-layer host drain on split models - #14611

Merged
danielhanchen merged 2 commits into
mainfrom
decode-rope-no-split-drain
Oct 22, 2026
Merged

danielhanchen merged 2 commits into
mainfrom
decode-rope-no-split-drain

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

#14599 (fused decode RoPE) calls _sync_if_split(Q) after its Triton launch in decode_rope_inplace and decode_qk_norm_rope. On a model split across GPUs (device_map), that blocks the host once per layer per decoded token.

The drain is not needed there. Both helpers launch inside torch_gpu_device(Q.device), so the kernel runs on Q's device and its current stream. The next layer's .to(other_device) is already ordered after it: PyTorch's device-to-device copy synchronizes with the current streams of both devices (aten/src/ATen/native/cuda/Copy.cu). The older eager RoPE entry points keep their drains. Those were added in #3049 / #3061 for launches made while another GPU was current, and this PR does not touch them.

Measured on a random-weight 8-layer Llama (hidden 2048, fp16) split 4 + 4 over two B200s: batch 1, 32 new tokens, 4 interleaved repeats, median generate time.

decode RoPE path median s tokens/s
before #14599 0.442 72.4
main (fused + drain) 0.723 44.3
fused, drain removed (this PR) 0.444 72.0

All three arms produced the same tokens. With random weights that is only a weak correctness check (the output is mostly one repeated token), so this PR also adds a direct ordering test.

Change

  • Remove _sync_if_split(Q) from decode_rope_inplace and decode_qk_norm_rope.
  • Single-GPU behaviour is unchanged: the drain was a no-op there.

Validation

  • test_decode_split_sync_and_mixed_devices now asserts that the decode helpers make no drain call.
  • New test_decode_rope_on_another_device_orders_with_the_copy, which needs two GPUs and skips on one:
    • rotates Q / K on GPU 1 while GPU 0 is current;
    • copies straight to GPU 0 with no host sync;
    • compares the result with the unfused rotation.
  • test_decode_fused_rope, test_qk_norm_rope, test_rope_kernel_layouts, test_rope_sync_single_device, test_qwen3_decode_offset_rope, GPU-marked tests included: 414 passed, 1 skipped (the two-GPU test) on torch 2.14.1 and on torch 2.10.0.

Not covered: the two-GPU test was not run here (single-GPU leases). The timing table comes from a two-GPU run of the same change outside this PR's CI. ROCm split models were not tested.

decode_rope_inplace and decode_qk_norm_rope launch inside torch_gpu_device(Q.device),
so the kernel runs on Q's device and current stream, and the next layer's
cross-device copy is already ordered after it. The _sync_if_split drain added in
#14599 blocked the host once per layer and made split-device decode about 40%
slower. Adds a two-GPU ordering test (skips on one GPU).
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 22, 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-22T06:29:42.067994Z 10d147c 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. Bravo.

Reviewed commit: 10d147cb64

ℹ️ 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".

[skip ci]
@danielhanchen
danielhanchen merged commit 8b4bb4c into main Oct 22, 2026
@danielhanchen
danielhanchen deleted the decode-rope-no-split-drain branch October 22, 2026 07:07
danielhanchen added a commit that referenced this pull request Oct 22, 2026
…able probe (#14661)

`tests/python/test_accelerator_skip_guards.py::test_no_skip_guard_reads_a_spoofable_accelerator_probe`
fails on main since #14611:

```
E   AssertionError: these skip guards read a probe tests/_zoo_aggressive_cuda_spoof.py patches to True process-wide, ...
E       test_decode_fused_rope.py:230: torch.cuda.device_count()
```

It runs in the Backend CI `studio` shard (`tests/python`), so every PR
whose shard runs on current main goes red there. The product code is
fine; the test's skip condition is what the guard exists to catch.

### Change

`tests/test_decode_fused_rope.py`:

- The two-GPU test is gated `not has_real_cuda() or
torch.cuda.device_count() < 2`, so the recorded real-hardware answer
short-circuits ahead of the spoofable `device_count()`, which is the
form the guard accepts.
- The module-level skip read `torch.cuda.is_available()`, which the same
spoof patches to True. The guard only inspects `skipif` decorators so it
did not flag it, but in a session that loads the spoof it would un-skip
the whole file on a CPU box. It now uses `has_real_cuda()` too.

### Verification

- The guard passes on this branch (24 passed) and fails on main with the
line above (1 failed, 23 passed).
- On a CPU box the file is skipped as one module.
- On a real GPU, `has_real_cuda()` is True with `device_count()` 1, so
the module still runs and only the two-GPU test skips.
danielhanchen added a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Oct 22, 2026
…2 ulp (unslothai#14752)

Follow-up to unslothai#14611.
`test_decode_rope_on_another_device_orders_with_the_copy` compared the
fused decode RoPE output with the unfused ops at `atol = 0.05`. The
fused kernel rounds once and the unfused ops round several times, so
valid bf16 results differ by one ulp, which is 0.0625 for |x| >= 8. With
the test's `3 * randn` inputs that happens on most seeds, so the test
fails on a two-GPU runner without anything being wrong.

## Change

- The copy is compared bitwise with the same kernel run on the same
inputs with its own device current. That is the property the test exists
for: a copy racing the rotation would see unrotated values, which a
bitwise check catches and a 0.05 tolerance could hide.
- Fused vs unfused uses the 2 ulp bound already used by
`test_decode_rope_inplace_matches_unfused` in the same file.

## Validation

- Single-GPU stand-in of the same comparison over 200 seeds (400
tensors): max fused vs unfused difference 0.0625, the old `atol = 0.05`
check fails 367 of 400, the 2 ulp bound holds on all 400, and the fused
kernel is bitwise deterministic run to run.
- Not run as written: the test needs two GPUs in one process and still
skips on single-GPU runners.

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
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