Repository navigation
HIP RDNA2: prefill attention on hipBLAS - #232
Conversation
🔍 Aletheore evidence diffgraph LR
n0["src/backend.rs"]
n1["src/cuda/mod.rs"]
n2["prefill.rs"]
n3["src/hip/mod.rs"]
n4["cpu_worker.rs"]
n5["src/cuda/host_share.rs"]
n6["chat.rs"]
n7["mat.rs"]
n8["expert_cache.rs"]
n9["src/gpu_model.rs"]
n10["src/gpu_model/host_share.rs"]
n11["dspark.rs"]
n12["forward.rs"]
n13["device.rs"]
n14["pool.rs"]
n15["worker.rs"]
n16["lib.rs"]
n17["main.rs"]
n18["model.rs"]
n19["projector.rs"]
n20["src/vulkan/mod.rs"]
n21["preflight.rs"]
n22["tests/backend.rs"]
n23["tests/common/mod.rs"]
n24["tests/gpu_model.rs"]
n25["inkling2_attn_kv.rs"]
n26["inkling2_q8_attention.rs"]
n27["kernels.rs"]
n28["kv_q8.rs"]
n29["kv_turbo.rs"]
n30["llama_numerics.rs"]
n31["tq_probe.rs"]
n32["vision_gpu.rs"]
n33["vulkan_split_kv.rs"]
n0 --> n4
n0 --> n7
n0 --> n8
n0 --> n9
n0 --> n10
n0 --> n11
n0 --> n12
n0 --> n13
n0 --> n14
n0 --> n15
n0 --> n16
n0 --> n17
n0 --> n18
n0 --> n19
n0 --> n20
n0 --> n22
n0 --> n23
n0 --> n24
n0 --> n25
n0 --> n26
n0 --> n27
n0 --> n28
n0 --> n29
n0 --> n30
n0 --> n31
n0 --> n32
n0 --> n33
n1 --> n5
n1 --> n6
n1 --> n13
n1 --> n16
n1 --> n17
n1 --> n20
n1 --> n21
n1 --> n23
n1 --> n24
n1 --> n25
n1 --> n28
n1 --> n29
n1 --> n30
n1 --> n33
n2 --> n9
n3 --> n16
n3 --> n23
classDef changed fill:#d9772c,color:#fff,stroke:#b85f1c;
classDef changedFinding fill:#c0392b,color:#fff,stroke:#922b21;
classDef dependent fill:#f2f0ec,color:#18140f,stroke:#d6d0c5;
class n0 changed
class n1 changed
class n2 changed
class n3 changed
class n4 dependent
class n5 dependent
class n6 dependent
class n7 dependent
class n8 dependent
class n9 dependent
class n10 dependent
class n11 dependent
class n12 dependent
class n13 dependent
class n14 dependent
class n15 dependent
class n16 dependent
class n17 dependent
class n18 dependent
class n19 dependent
class n20 dependent
class n21 dependent
class n22 dependent
class n23 dependent
class n24 dependent
class n25 dependent
class n26 dependent
class n27 dependent
class n28 dependent
class n29 dependent
class n30 dependent
class n31 dependent
class n32 dependent
class n33 dependent
What changed
Aggregate deltas
|
Aletheore Flash Lite reviewWhat this PR does. The pull request changes ATT_DENSE_MAX in src/hip/mod.rs from 32768 to 1 << 40. This affects HIP RDNA2 prefill attention using hipBLAS by raising the dense attention size threshold.
2 finding(s) posted as inline review comment(s). 2 earlier finding(s) from previous pushes still open.
Review detailsReviewed 1 file(s); skipped 0. Dismissed 0; outside PR diff 0. Grounding: 5 of 5 proposed finding(s). Fix all with your agentBlast radius: 53 other file(s) depend on what this PR changes (3 directly, 50 indirectly)Computed from the import graph of this exact commit, not guessed by a model. It sees imports only, so dynamic imports, reflection and non-code references are not counted.
Indirectly affected: The graph is large; this list is capped and not exhaustive. |
760a00c to
2265277
Compare
|
All six findings checked. Five are real and fixed in Per finding:
Re-verified after the fixes on an RX 6800 XT (gfx1030, Qwen4-35B-A3B Q4_K_M): the 700-token multi-topic probe answers all four planted facts; a 5.9k-token dialogue and the 5.7k decoy recall match llama.cpp. |
|
Checked against the ROCm 6.4 headers on the machine this path was measured on:
So no change here - but thanks, this is the kind of thing that would be nasty if it were real. |
2265277 to
bdc37c2
Compare
|
Checked on the card this time, not just in the header - a direct
If hipBLAS read alpha as This is the same conclusion as the header reading earlier, now with hardware evidence. Keeping the code as is - if you can show a counter-test on the same stack it would be interesting, but both the headers and the card say f32 scalar. |
|
Hi, ty for the work, and ty for checking the alpha type on the card, that part is fine. A few other things pls: |
RDNA2 has no matrix cores: the flash prefill kernel and the dot2 GEMMs run at 0.5-12 TFLOPS, and as the context grows the attention of a prefill chunk dominates its time. This runs the attention of a chunk on the library's Tensile kernels instead (hipBLAS, fp16 in, f16 scores, f32 out): - ffi: GemmEx and GemmEx_v2 bindings (the legacy entry takes hipblasDatatype_t, the _v2 entry hipDataType; C = x W^T needs OP_N for B). - hip: attn_prefill_blas - K/V and Q as f16, Q K^T a head into an f16 score block, a wave-a-row softmax with the QSA mask, P V a head straight into out, then 1/sum and the query gate applied in att_scale. Past 32k keys, or on a chunk the kernels do not cover, the flash kernel keeps running (attn_prefill_m_at falls through to attn_prefill_m). - On by default for RDNA2 only (gfx103*); STRATA_HIPBLAS=1 turns the library path on on any HIP card, =0 off anywhere. - kernels.cu: kv_to_h, q_to_h, att_softmax, att_scale, and att_exp for RDNA2's v_exp_f32. The CUDA build compiles them and never launches them. Verified on Qwen4-35B-A3B Q4_K_M, RX 6800 XT (gfx1030): a 5.9k-token multi-topic dialogue answers all four planted facts; a 5.7k decoy recall matches llama.cpp. Series numbers (all four RDNA2 PRs): warm prefill 4K 494 -> 1007, 16K 239 -> 1264, 32K 134 -> 1052 tok/s.
…ange The raw NUL byte made git treat the file as binary, so it was committed with CRLF and every later diff of it showed the whole file. ASCII again, LF like the rest of the tree.
… flash kernel masks a key at a time)
bdc37c2 to
5e1f1ed
Compare
|
Review fixes, all in the head:
One more thing this PR now carries: the prefill attention no longer drops to the flash kernel past 32k keys. The QSA mask hides keys but skips no tiles, so the flash kernel runs every tile and costs about five times the same keys on gfx1030 (a chunk of 8192 queries at a 24.6k prefix: 24.1 s against 5.6 s). With the GEMMs the prefill stops sagging with depth - a 33k-token probe went from 259 to 849 tok/s. The score block shrinks with the prefix ( Smoke (gfx1030, mainline + this PR): a 6k-token recall probe answers its four planted facts, prefill 476 tok/s. |
|
Two dismissals, no code change.
Smoke on the stack (gfx1030, mainline + this PR + #233 + #235, chunk 2048): 6k recall probe 4/4, an 8296-token prompt prefills at 740 tok/s, no kernel fallbacks. |
5e1f1ed to
25b44af
Compare
|
Branch rebuilt, no code change: the three doc-comment lines that the last commit had put on Checked on gfx1030 (HIP backend only) on the top of the stack, @Aletheore please re-run the review on the current head. |
|
Hi, ty for all the fixes. I went through the review points again and they are all done now: the u16 tiles, the TurboQuant KV gate, STRATA_HIPBLAS_LIB for the library path, the fallback to the flash kernel. Your answer on GemmEx_v2 alpha and compute type is right, the bot was wrong there. The kv_to_h point is also not quadratic, it is one pass over the prefix per chunk.
|
Stacked on #231 (the gfx10 enablement this needs).
What
RDNA2 has no matrix cores: the flash prefill kernel and the dot2 GEMMs run at 0.5-12 TFLOPS on the card, and as a prompt grows the attention of a prefill chunk dominates its time. This runs the attention of a chunk on the library's Tensile kernels instead (hipBLAS: fp16 in, f16 scores, f32 out - the fast store on this silicon).
ffi:GemmExandGemmEx_v2bindings. The legacy entry takeshipblasDatatype_tvalues, the_v2entry thehipDataTypeones;C = x W^TneedsOP_Nfor B.hip:attn_prefill_blas- K/V and Q of the cache as f16,Q K^Ta head into an f16 score block, a wave-a-row softmax with the QSA mask,P Va head straight intoout, then1/sumand the query gate applied byatt_scale. The flash kernel keeps every chunk the library path does not cover: past 32k keys, with a sink or a window, or when the scratch did not fit (attn_prefill_m_atfalls through toattn_prefill_m).kernels.cu:kv_to_h,q_to_h,att_softmax,att_scale, andatt_exp(RDNA2'sv_exp_f32; CUDA gets__expf). The CUDA build compiles them and never launches them.gfx103*);STRATA_HIPBLAS=1turns it on on any HIP card,=0disables it anywhere. The library itself is looked up throughSTRATA_HIPBLAS_LIB(a path or a name), apart from the switch.Series numbers
All four RDNA2 PRs together, Qwen4-35B-A3B-Uncensored Q4_K_M on an RX 6800 XT (16 GB), ctx 65536, kv q8, a fresh prefix each run (only the 13-token template prefix is cached, so every prompt is fully prefilled):
Prefill and decode in tok/s. chunk 8192 ran with
--vram-margin 1536(5210 of 10240 expert slots on the card, 14.6 GB resident), chunk 2048 with--vram-margin 1600(6000 slots, 14.6 GB); decode at chunk 8192 was 47-52 tok/s. The baseline column is upstream main + #231 at its own defaults (chunk 8192, 15 GB budget, 3.7k / 14.8k / 29.7k-token prompts of the same generator, so the ratios are against slightly shorter prompts); its decode was not measured in that sweep. Per-PR splits were not measured - the four PRs were benchmarked as one.Chunk size trades experts for scratch: the prefill slab holds
chunkrows of every scratch region (276 KB a token), so 8192 costs 2214 MB and 2048 costs 624 MB, and the freed space becomes expert slots. 8192 prefills faster, 2048 decodes ~20% faster (the same prompts on both).Long prompts stay slower than short ones because the attention of a chunk is dense over the whole prefix: the QSA mask masks the scores, it does not skip the work, so prefill falls roughly as 1/depth. With chunk 8192 and a 15 GB budget the 8192-token chunks of a 64k prompt ran at ~1100 tok/s at an 8k prefix, ~1000 at 16k, ~600 at 24k, ~300 at 32k and ~150 at 64k. A 132k-context setting keeps 4750 slots instead of 5210 and prefills a 33k prompt at ~440 tok/s.
Verified
cargo test --release --lib: green (kernel_namescounts updated to 474/402/450 for the four new kernels).attn_prefill_m, so their behavior is unchanged.Tooling note
The diagnosis, the patch and the verification runs above were prepared with the help of the deepseek-v4.1-flash-1017 model.