Repository navigation
HIP RDNA2: batched MoE rounds on GemmStridedBatchedEx - #233
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. This pull request adds a grouped GEMM path for mixture-of-experts prefill on HIP, using hipblasGemmStridedBatchedEx_v2 with batched rounds, dequantization and gather/scatter kernels, and a fallback to the existing kernel on error or unsupported shapes. It also adds corresponding CUDA kernels and kernel-name registrations and updates the backend trait and prefill call sites to use the new grouped interface. The changes affect the HIP and CUDA GPU backends and the GPU model prefill path for expert matrix multiplication.
3 finding(s) posted as inline review comment(s). 4 earlier finding(s) from previous pushes still open. 1 earlier finding(s) no longer detected in this push.
Review detailsReviewed 6 file(s); skipped 0. Dismissed 0; outside PR diff 0. Grounding: 8 of 8 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. |
04561e4 to
c43a03e
Compare
|
Both findings checked on the card:
Both branches are pushed (heads |
c43a03e to
919cacb
Compare
|
Three more, checked:
While checking this I also found the branch was missing the |
919cacb to
abc4b13
Compare
abc4b13 to
a13a595
Compare
|
Real inconsistency - the guard bounded a single slot by
Covered by the existing fallback: a slot larger than |
|
All three checked, all three real:
Smoke on the updated head: the 700-token probe answers 4 of 4 planted facts and the server log has no |
a13a595 to
3f51016
Compare
|
Two notes:
|
|
Hi, ty. Same CUDA build break as 232 bc it sits on it. Also gemm_grouped is in impl Gpu, not in impl Backend for Gpu, so prefill never calls it and the MoE BLAS path does not run (rustc warns it is unused). Pls move it into the trait impl, and pls add a fallback to the normal kernel when hipBLAS fails, like attention and dense have. |
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)
3f51016 to
f987183
Compare
|
Review fixes, all in the head:
Smoke (gfx1030, mainline + #232 + this PR): a 6k-token recall probe answers its four planted facts, prefill 473 tok/s. |
…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.
|
Two fixes in the head, one dismissal.
Smoke (gfx1030, mainline + #232 + this PR, chunk 2048, 6000/10240 experts on the GPU): a 6k-token recall probe answers its four planted facts; an 8296-token prompt prefills at 740 tok/s (per chunk 483 -> 649 -> 734 -> 785); no kernel fallbacks in the log, so the MoE BLAS path is still the one running. |
713f639 to
ff0425d
Compare
|
Branch rebuilt on the new #232 head. One change and one cleanup:
Checked on gfx1030 (HIP backend only) on the top of the stack, @Aletheore please re-run the review on the current head. |
ff0425d to
d3e91c1
Compare
|
Branch rebuilt on the same #232 head with one line changed. Checked on gfx1030 on the top of the stack ( @Aletheore please re-run the review on the current head. |
|
Hi, ty. The batched expert rounds look good to me. I checked the three new bot comments:
|
Stacked on #232 (attention) and #231 (gfx10), which this builds on.
What
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-batchedGemmEx(hipblasGemmStridedBatchedEx_v2, fp16 in/out, f32 accumulate). The per-expert shapes of this model are small (512 x 2048), where one GEMM an expert costs more in rocBLAS host time than the products themselves - the batched entry runs a whole round with one call.blas_scatter; a call's lone expert keeps a single GEMM, act-fused throughgemm_splitk_sum).STRATA_MOE_BATCH=0puts every expert back on its own GEMM;STRATA_MOE_STATSprints the slot counts and the estimated time of the first calls.Blashandle): RDNA2 only by default,STRATA_HIPBLAS=1/0overrides, and never for a weight type the backend'sint8_gemmtakes (an int8 WMMA card keeps those on its int8 GEMM). CUDA and Vulkan get the trait's default, which forwardsgemm_groupedtogemmwith the same tile count - their behavior is unchanged.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
STRATA_HIPBLAS=0the backend logs no hipblas line and runs the dot2 kernels; ongfx1030the line appears by default.cargo test --release --lib: green (kernel_namescounts 475/403/451 withblas_scatteradded).Tooling note
The diagnosis, the patch and the verification runs above were prepared with the help of the deepseek-v4.1-flash-1017 model.