Repository navigation
HIP RDNA2: dense prefill projections on GemmEx - #235
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 adds a
1 finding(s) posted as inline review comment(s). 1 earlier finding(s) no longer detected in this push.
Review detailsReviewed 6 file(s); skipped 0. Dismissed 0; outside PR diff 0. Grounding: 10 of 10 proposed finding(s). Fix all with your agentBlast radius: 79 other file(s) depend on what this PR changes (30 directly, 49 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. |
813b8e5 to
c6fe2d8
Compare
|
Checked all four:
|
|
Three more, checked:
Smoke on the updated head: the 700-token probe answers 4 of 4 planted facts, no |
c6fe2d8 to
381959d
Compare
|
Two more, both checked:
No code change for either. |
|
Hi, ty, this one looks fine on its own, it just needs 232 and 233 fixed first bc of the CUDA build break. |
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)
The MoE of a prefill chunk hits hipBLAS through gemm_grouped: the experts of a call dequantize to f16, their x rows gather as f16, and the products run as strided-batched GemmEx (fp16 in/out, f32 accumulate). The per-expert shapes here are small (512x2048), where one GEMM an expert costs more in rocBLAS host time than the products themselves - a whole round runs with one call. - The slots of a call split into items of the sizes Tensile runs fast on this silicon (a cost table per shape; the sizes at which the same GEMM costs 4-8x more are avoided), the cheapest cover picked by a small DP over slot counts. - A round dequants and gathers in one launch each, runs one batched GEMM, and scatters the f16 results into y with the activation (blas_scatter); a call's lone expert keeps a single act-fused GEMM through gemm_splitk_sum. - STRATA_MOE_BATCH=0 puts every expert back on its own GEMM; STRATA_MOE_STATS prints the slot counts of the first calls. - Same gate as the attention (one Blas handle): RDNA2 only by default, STRATA_HIPBLAS=1/0 overrides. CUDA and Vulkan take the trait's default, which forwards gemm_grouped to gemm with the same tile count. Verified on Qwen4-35B-A3B Q4_K_M, RX 6800 XT: the 700-token probe answers all four planted facts (34.7 tok/s decode); the 5.9k dialogue and 5.7k decoy recall match llama.cpp. Series numbers (all four RDNA2 PRs): warm prefill 4K 494 -> 1007, 16K 239 -> 1264, 32K 134 -> 1052 tok/s.
… bits, not the value)
…fore writing (a partial y lapses the path)
381959d to
b7f8296
Compare
|
Rebased on the fixed #233. The only change needed here is Smoke (gfx1030, mainline + #232 + #233 + this PR): a 6k-token recall probe answers its four planted facts at 524 tok/s, a 33k-token one at 849 tok/s, no kernel fallbacks in the log. |
…l latch per call host_mailbox counts u32 words, so WORDS * SLOTS u64 slots need twice the words; the slot writes ran past the pinned allocation. MOE_PARTIAL now starts false on every gemm_grouped call, so a later failure that wrote no y still falls back to the dot2 kernel instead of aborting.
b7f8296 to
ba96a95
Compare
|
Rebased on the fixed #233. Two changes here:
Smoke (gfx1030, mainline + #232 + #233 + this PR, chunk 2048): 6k recall probe 4/4, an 8296-token prompt prefills at 740 tok/s, no kernel fallbacks - the stride gate does not push the MoE path back to dot2. |
|
One more fix in the head (33f7e73, one line): It changes nothing that runs today: the only setter of that bit is the MoE gate flags in prefill.rs, and it feeds No local rebuild for a one-line gate change - the CI build compiles it, and the previous full smoke still stands for the path that runs (6k recall 4/4, 8k prefill 740 tok/s, no fallbacks). |
|
Per hun's note on the gate: one line in d7029f0. On RDNA2 @Aletheore please re-run the review on the current head. |
The dense prefill GEMMs (mixer qkv/o, the SSM projections, a dense FFN where the model has one) run on hipBLAS's GemmEx: the matrix dequantizes to f16 once per call, the x block converts to f16, the product runs with an f16 store (the f32 store of the same call is a third of the throughput on RDNA2), and blas_scatter_d writes the result to y with the activation. Matrices of 4096 rows or more process 4096 tokens a step (the 8192-wide Tensile kernel of a 4096x2048 matrix costs what two halves do). A dense GEMM below 512 tokens stays on the dot2 kernel (dequantizing the whole matrix does not pay there); STRATA_HIP_DENSE=0 disables the path; a runtime failure is logged once and hands the rest of the run to dot2 (DENSE_FALLBACK). Same gate as the rest of the series: RDNA2 only by default, STRATA_HIPBLAS=1/0 override. CUDA and Vulkan are untouched - this is the HIP backend's own gemm_dense override. Verified on Qwen4-35B-A3B Q4_K_M, RX 6800 XT: the 700-token probe answers all four planted facts, the server log shows no kernel fallback lines, the 5.7k decoy recall answers 3 of 3 needles on 3 runs. kernel_names 539/467/515.
…er need x_gather_hg reads x a float4 at a time and blas_scatter writes past m0 * y_stride, so a stride that is not a multiple of 4, or one shorter than the row, has to stay on the dot2 kernel - the same gate gemm_dense already has.
d7029f0 to
4bf89b5
Compare
|
Branch rebuilt on the new #233 head. Checked on gfx1030 (HIP backend only) on this head: @Aletheore please re-run the review on the current head. |
4bf89b5 to
edd130a
Compare
|
Branch rebuilt on the new #233 head; Checked on gfx1030 on this head: with @Aletheore please re-run the review on the current head. |
|
Hi, ty. The dense GemmEx path looks good and the stride and int8 gates are in. About the last bot comment on MOE_PARTIAL for the first expert: I do not think it is a real problem. Inside one item y is written only by the last launch (the scatter or splitk sum), and for act 0 the GEMM writes y with beta 0, so a rerun on dot2 just overwrites it. If an earlier launch fails, y is not touched yet. You can dismiss it. |
Stacked on #233 (MoE), #232 (attention) and #231 (gfx10).
What
The dense prefill GEMMs (mixer qkv/o, the SSM projections, a dense FFN where the model has one) run on hipBLAS's
GemmEx: the matrix dequantizes to f16 once per call, the x block converts to f16, the product runs with an f16 store (the f32 store of the same call is a third of the throughput on RDNA2), andblas_scatter_dwrites the result to y with the activation (silu-mul or add). Matrices of 4096 rows or more process 4096 tokens a step: the 8192-wide Tensile kernel of a 4096 x 2048 matrix costs what two halves do.STRATA_HIP_DENSE=0disables the path.DENSE_FALLBACK), so a bad shape costs a warning, not a request.STRATA_HIPBLAS=1/0overrides on any HIP card, except for a weight type the backend'sint8_gemmtakes (an int8 WMMA card keeps those ongemm_raw). CUDA and Vulkan are untouched: this is the HIP backend's owngemm_denseoverride.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
dequant_h/x_gather_*/blas_scatter_dall resolve).att_expfix; with the earlierexp(x)+1it lost a needle).cargo test --release --lib: green (kernel_names477/405/453 withx_to_handblas_scatter_d).Tooling note
The diagnosis, the patch and the verification runs above were prepared with the help of the deepseek-v4.1-flash-1017 model.