Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

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

Daniel Han8b4bb4c22109
diff --git a/tests/test_decode_fused_rope.py b/tests/test_decode_fused_rope.py
index defb388b72c..3f56554554e 100644
--- a/tests/test_decode_fused_rope.py
+++ b/tests/test_decode_fused_rope.py
@@ -200,7 +200,7 @@ def test_decode_strided_int32_positions():
def test_decode_split_sync_and_mixed_devices(monkeypatch):
- # Split models: both helpers drain the source stream like the other eager RoPE entry points.
+ # Split models: the decode helpers never drain the stream on the host.
cos, sin = _table(64, 128, torch.bfloat16)
q = torch.randn(2, 4, 1, 128, device = "cuda", dtype = torch.bfloat16)
k = torch.randn(2, 2, 1, 128, device = "cuda", dtype = torch.bfloat16)
@@ -213,7 +213,7 @@ def test_decode_split_sync_and_mixed_devices(monkeypatch):
assert (
QK.decode_qk_norm_rope(n, n, q.transpose(1, 2), k.transpose(1, 2), cos, sin, p) is not None
)
- assert len(calls) == 2
+ assert len(calls) == 0
# any operand off Q's device keeps the unfused path, with nothing written
before_q, before_k = q.clone(), k.clone()
assert not RE.decode_rope_inplace(q, k, cos, sin.cpu(), p)
@@ -224,4 +224,21 @@ def test_decode_split_sync_and_mixed_devices(monkeypatch):
assert QK.decode_qk_norm_rope(n, nc, qs, ks, cos, sin, p) is None
assert QK.decode_qk_norm_rope(n, n, qs, ks.cpu(), cos, sin, p) is None
assert QK.decode_qk_norm_rope(n, n, qs, ks, cos, sin.cpu(), p) is None
- assert len(calls) == 2
+ assert len(calls) == 0
+
+
[email protected](torch.cuda.device_count() < 2, reason = "needs two GPUs")
+def test_decode_rope_on_another_device_orders_with_the_copy():
+ # Rotate on GPU 1 with GPU 0 current, then copy to GPU 0: the copy must see rotated values.
+ cos, sin = _table(4096, 128, torch.bfloat16)
+ cos, sin = cos.to("cuda:1"), sin.to("cuda:1")
+ p = torch.randint(0, 4000, (4, 1), device = "cuda:1")
+ for _ in range(3):
+ q = 3 * torch.randn(4, 32, 1, 128, device = "cuda:1", dtype = torch.bfloat16)
+ k = 3 * torch.randn(4, 8, 1, 128, device = "cuda:1", dtype = torch.bfloat16)
+ qu, ku = _unfused_rope(q.clone(), k.clone(), cos, sin, p)
+ with torch.cuda.device(0):
+ assert RE.decode_rope_inplace(q, k, cos, sin, p)
+ q0, k0 = q.to("cuda:0"), k.to("cuda:0")
+ assert torch.allclose(q0.cpu().float(), qu.cpu().float(), atol = 0.05)
+ assert torch.allclose(k0.cpu().float(), ku.cpu().float(), atol = 0.05)
diff --git a/unsloth/kernels/qk_norm_rope.py b/unsloth/kernels/qk_norm_rope.py
index a1d2d7d6164..9bfebc6e77b 100644
--- a/unsloth/kernels/qk_norm_rope.py
+++ b/unsloth/kernels/qk_norm_rope.py
@@ -502,5 +502,5 @@ def decode_qk_norm_rope(q_norm, k_norm, Q, K, cos, sin, position_ids):
Q, K, Wq.contiguous(), Wk.contiguous(), cos2, sin2, rope_indices,
float(_eps(q_norm)), float(_eps(k_norm)), True, _eager_kernel,
) # fmt: skip
- _sync_if_split(Q)
+ # No host drain (see decode_rope_inplace).
return Qo.transpose(1, 2), Ko.transpose(1, 2)
diff --git a/unsloth/kernels/rope_embedding.py b/unsloth/kernels/rope_embedding.py
index a057e1bd431..2dea85edbd5 100644
--- a/unsloth/kernels/rope_embedding.py
+++ b/unsloth/kernels/rope_embedding.py
@@ -678,9 +678,9 @@ def decode_rope_inplace(Q, K, cos, sin, position_ids):
return False
# The kernel indexes positions densely; an int32 last-column slice keeps its row stride.
rope_ptr = position_ids.reshape(-1).to(dtype = torch.int32, device = Q.device).contiguous()
+ # No host drain: cross-device copies already order after Q's stream (a drain cost split decode ~40%).
with torch_gpu_device(Q.device):
_rope_qk(Q, K, Q, K, cos2, sin2, rope_ptr, True, False, _eager_kernel)
- _sync_if_split(Q)
return True