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.
Python amdapple-siliconcudadeepseekexl3exllamav3glminferencellmllm-inferencelocal-llmnvidiaopenai-apiquantizationqwenrocmstrix-halo
Prefill attention: run the head_dim 256 FlashAttention-2 chunk as 1024-row sub-calls
YanissAmzb3ba485aaab4
diff --git a/exllamav3/modules/attention_fn/triton_paged.py b/exllamav3/modules/attention_fn/triton_paged.pyindex bd6aacc..9c79fcf 100644--- a/exllamav3/modules/attention_fn/triton_paged.py+++ b/exllamav3/modules/attention_fn/triton_paged.py@@ -709,18 +709,28 @@ _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+# The chunk runs as sub-calls of this many query rows (each over the keys up to its own last row, same bottom-right causal+# mask, so every row sees the same keys in the same order; whole 1024-row sub-calls match one call bit for bit, a short+# remainder may take FA2's split-kv schedule and differ in the last bits). One FA2 call over a+# 4096-row chunk with a long past reaches 73-78 TFLOPS on an RTX 3090, 1024-row sub-calls 92 (2048: 87, 512: 81, 256: 84).+# 0 = one call+_pf_fa2_rows = int(os.environ.get("EXL3_PF_FA2_ROWS", "1024"))+def _prefill_fa2_contig(q, kd, vd, past, n_kvh, head_dim, scale, out):from torch.nn.attention import sdpa_kernel, SDPBackendfrom torch.nn.attention.bias import causal_lower_rightq_len = q.shape[1]k1 = kd.view(1, -1, n_kvh, head_dim)[:, :past + q_len]v1 = vd.view(1, -1, n_kvh, head_dim)[:, :past + q_len]+ step = _pf_fa2_rows if _pf_fa2_rows > 0 else q_lenwith sdpa_kernel([SDPBackend.FLASH_ATTENTION]):- o = torch.nn.functional.scaled_dot_product_attention(- q.transpose(1, 2), k1.transpose(1, 2), v1.transpose(1, 2),- attn_mask = causal_lower_right(q_len, past + q_len), scale = scale, enable_gqa = True,- )- out.copy_(o.transpose(1, 2))+ for s in range(0, q_len, step):+ e = min(q_len, s + step)+ o = torch.nn.functional.scaled_dot_product_attention(+ q[:, s:e].transpose(1, 2), k1[:, :past + e].transpose(1, 2), v1[:, :past + e].transpose(1, 2),+ attn_mask = causal_lower_right(e - s, past + e), scale = scale, enable_gqa = True,+ )+ out[:, s:e].copy_(o.transpose(1, 2))return outdef _get_h32(device):diff --git a/tests/test_prefill_fa2_rows.py b/tests/test_prefill_fa2_rows.pynew file mode 100644index 0000000..5fb5cf4--- /dev/null+++ b/tests/test_prefill_fa2_rows.py@@ -0,0 +1,42 @@+import pytest+import torch++pytestmark = pytest.mark.skipif(not torch.cuda.is_available() or torch.version.hip is not None, reason = "CUDA only")++from exllamav3.modules.attention_fn import triton_paged as tp+++def _reference(q, k, v, past, scale):+ # fp32 attention, queries aligned to the end of the keys, GQA by repeat+ q_len, n_heads, hd = q.shape+ n_kv = k.shape[1]+ kf = k[:past + q_len].float().repeat_interleave(n_heads // n_kv, dim = 1)+ vf = v[:past + q_len].float().repeat_interleave(n_heads // n_kv, dim = 1)+ s = torch.einsum("qhd,khd->hqk", q.float(), kf) * scale+ mask = torch.arange(past + q_len, device = q.device)[None, :] > (past + torch.arange(q_len, device = q.device))[:, None]+ s = s.masked_fill(mask[None], float("-inf"))+ return torch.einsum("hqk,khd->qhd", s.softmax(-1), vf)++[email protected]("q_len, past", [(300, 0), (1024, 0), (2500, 3000), (4096, 5000), (777, 2000)])+def test_split_rows_equal_whole_and_close_to_reference(monkeypatch, q_len, past):+ torch.manual_seed(0)+ hd, n_heads, n_kv = 256, 24, 4+ q = torch.randn(1, q_len, n_heads, hd, device = "cuda", dtype = torch.half)+ kd = torch.randn(1, past + q_len + 100, n_kv, hd, device = "cuda", dtype = torch.half)+ vd = torch.randn(1, past + q_len + 100, n_kv, hd, device = "cuda", dtype = torch.half)+ scale = hd ** -0.5++ outs = {}+ for rows in (0, 1024, 512):+ monkeypatch.setattr(tp, "_pf_fa2_rows", rows)+ outs[rows] = tp._prefill_fa2_contig(q, kd, vd, past, n_kv, hd, scale, torch.empty_like(q))++ # every row sees the same keys in the same order whatever the split. Whole 1024-row sub-calls are bit-identical; a short+ # remainder sub-call may take FA2's split-kv schedule (different summation order), so there the check is rounding-level+ if q_len % 1024 == 0:+ assert torch.equal(outs[1024], outs[0])+ assert (outs[1024].float() - outs[0].float()).abs().max().item() < 2e-3+ assert (outs[512].float() - outs[0].float()).abs().max().item() < 2e-3+ ref = _reference(q[0], kd[0], vd[0], past, scale)+ assert (outs[1024][0].float() - ref).abs().max().item() < 5e-3