Skip to content

Kyojin: open-source engine that runs 100 GB-class open models (GLM-5.3, Qwen3.8, DeepSeek V4.1) on one desktop machine. AMD Strix Halo, NVIDIA RTX and Apple Mac. OpenAI API, built on ExLlamaV3.

ROCm prefill attention: launch the q blocks slice by slice (EXL3_PF_ROW_SLICE, default 256 rows)

YanissAmzb46ea0d04111
diff --git a/exllamav3/modules/attention_fn/triton_paged.py b/exllamav3/modules/attention_fn/triton_paged.py
index bd6aacc..414b83d 100644
--- a/exllamav3/modules/attention_fn/triton_paged.py
+++ b/exllamav3/modules/attention_fn/triton_paged.py
@@ -709,6 +709,12 @@ _qc_prefill_two_pass_min_q = int(os.environ.get("EXL3_QC_PF_TWO_PASS_MIN_Q", "25
# EXL3_PF_FA2=0 restores the Triton kernel
_pf_fa2 = os.environ.get("EXL3_PF_FA2", "1") == "1" and torch.version.hip is None
+# Query rows per slice of the prefill grid (single-split launches): the programs run slice by slice, each slice's rows over all heads
+# before the next slice's. 0 = one wide q-block axis (the order before this knob). Same math per program, so bit-identical output.
+# gfx1151, Qwen3.8-27B geometry (24 q / 4 kv heads, hd 256, 4096-row chunk): 1.26x at 4K past, 1.8x at 32K, 2.2x at 120K (the wide axis
+# runs 10 TFLOPS at 120K, 256-row slices 21). Default on for ROCm only (CUDA not measured)
+_pf_row_slice = int(os.environ.get("EXL3_PF_ROW_SLICE", "256" if torch.version.hip else "0"))
+
def _prefill_fa2_contig(q, kd, vd, past, n_kvh, head_dim, scale, out):
from torch.nn.attention import sdpa_kernel, SDPBackend
from torch.nn.attention.bias import causal_lower_right
@@ -1585,6 +1591,7 @@ def _paged_attn_prefill_kernel(
WIDE_INDEX: tl.constexpr, # int64 q/out/partial index math (element offsets past 2^31)
BLOCK_M: tl.constexpr,
BLOCK_N: tl.constexpr,
+ SLICE_BLOCKS, # runtime: row-sliced grid (num_splits == 1), q blocks per slice, 0 = one q-block axis
):
"""FA2-style prefill over the paged cache: BLOCK_M query rows of one head per program;
unmasked interior kv tiles take a maskless fast path, only the causal boundary and the
@@ -1592,6 +1599,13 @@ def _paged_attn_prefill_kernel(
pid_m = tl.program_id(0)
bh = tl.program_id(1)
split = tl.program_id(2)
+ if SLICE_BLOCKS > 0:
+ # grid (SLICE_BLOCKS, heads, slices): the programs launch in the order of sequential sub-calls over SLICE_BLOCKS * BLOCK_M
+ # rows each (every program computes what it computes in one wide launch, bit for bit)
+ pid_m = pid_m + split * SLICE_BLOCKS
+ split = 0
+ if pid_m * BLOCK_M >= q_len:
+ return
batch = bh // n_q_heads
q_head = bh - batch * n_q_heads
group_size = n_q_heads // n_kv_heads
@@ -2092,6 +2106,13 @@ def paged_attn_triton_prefill(
# the ROCm Triton backend has no maxnreg launch option (it raises on the unknown keyword)
_launch_opts = {"maxnreg": _maxnreg} if _maxnreg and not torch.version.hip else {}
grid = (q_blocks, bsz * n_q_heads, num_splits)
+ slice_blocks = 0
+ if num_splits == 1 and _pf_row_slice > 0:
+ slice_blocks = max(1, _pf_row_slice // block_m)
+ if q_blocks > slice_blocks:
+ grid = (slice_blocks, bsz * n_q_heads, triton.cdiv(q_blocks, slice_blocks))
+ else:
+ slice_blocks = 0
# int64 index math only when an element offset can actually cross 2^31 (q/out, or the
# fp32 partial buffer); common geometries keep pure int32 codegen
wide_index = max(
@@ -2108,7 +2129,7 @@ def paged_attn_triton_prefill(
num_pages_per_seq, page_size, head_dim, hd_pad, vd_pad, v_dim, float(softmax_scale),
bool(causal), int(window_left), int(window_right),
window_left >= 0, window_right >= 0, float(softcap or 0.0),
- has_sinks, wide_index, block_m, block_n,
+ has_sinks, wide_index, block_m, block_n, slice_blocks,
num_warps=num_warps, num_stages=ns, **_launch_opts,
)
diff --git a/tests/test_prefill_row_slice.py b/tests/test_prefill_row_slice.py
new file mode 100644
index 0000000..eb1ce60
--- /dev/null
+++ b/tests/test_prefill_row_slice.py
@@ -0,0 +1,51 @@
+"""
+The row-sliced prefill grid (EXL3_PF_ROW_SLICE: programs launched slice by slice over the query rows) computes every program
+exactly as the single wide q-block axis does: the output must be BIT-identical for any slice size, with causal masks, windows,
+GQA, a ragged last q block and a past that is not tile-aligned.
+"""
+import sys, os
+sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
+import pytest
+import torch
+
+pytestmark = pytest.mark.skipif(not torch.cuda.is_available(), reason = "needs a GPU (Triton)")
+
+from exllamav3.modules.attention_fn import triton_paged as tp
+
+PAGE = 256
+device = "cuda:0"
+
+
+def _run(q, k, v, kc, vc, bt, past, slice_rows, **kw):
+ old = tp._pf_row_slice
+ tp._pf_row_slice = slice_rows
+ try:
+ sl = torch.tensor([past], dtype = torch.int32, device = device)
+ return tp.paged_attn_triton_prefill(q, k, v, kc, vc, bt, sl, causal = True, num_splits = 1, **kw)
+ finally:
+ tp._pf_row_slice = old
+
+
[email protected]("hd, n_heads, n_kv, q_len, past, window", [
+ (256, 24, 4, 4096, 3000, None), # Qwen3.8-27B full-attention geometry
+ (256, 24, 4, 1000, 0, None), # ragged last q block, no past
+ (256, 24, 4, 2500, 777, None), # past not tile-aligned
+ (128, 32, 8, 1536, 5000, None),
+ (256, 16, 2, 2048, 4096, 1024), # sliding window
+])
[email protected]("slice_rows", [128, 256, 1024])
+def test_row_slice_bit_identical(hd, n_heads, n_kv, q_len, past, window, slice_rows):
+ torch.manual_seed(1)
+ total = past + q_len
+ pages = (total + PAGE - 1) // PAGE
+ kc = (torch.randn((pages, PAGE, n_kv, hd), device = device) * 0.5).half()
+ vc = torch.randn((pages, PAGE, n_kv, hd), device = device).half()
+ bt = torch.arange(pages, dtype = torch.int32, device = device)[None]
+ q = torch.randn((1, q_len, n_heads, hd), device = device).half()
+ k = torch.randn((1, q_len, n_kv, hd), device = device).half()
+ v = torch.randn((1, q_len, n_kv, hd), device = device).half()
+ kw = dict(softmax_scale = hd ** -0.5, window_size = window, max_kv_len = past)
+ kc2, vc2 = kc.clone(), vc.clone()
+ ref = _run(q, k, v, kc, vc, bt, past, 0, **kw)
+ out = _run(q, k, v, kc2, vc2, bt, past, slice_rows, **kw)
+ assert torch.equal(ref, out)