Repository navigation
cuda : Qwen4-35B-A3B decode on gfx1151, fixed-grid short-row matvec and weight prefetch (tg 50.4 -> 54.4) - #347
Conversation
A single-column Q8_0 matvec with K <= 512 and at least 4096 rows (the qwen4exp hyper-connection up projection, 8192 x 256) launched one block of 16 waves per 16 rows, and every block quantized the activation again for one short row per wave. Four blocks per CU now loop over the rows, quantize the activation once, and load the next row's weights before the dot product of the current one. The loads, dot products and reduction of a row are those of mul_mat_vec_q_fq<Q8_0>, so results are bit-identical; the new MUL_MAT_EXACT_BATCH cases at 8192 x 256 compare it with n = 2..4. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…exp) Between two large weight reads (MoE experts, matrices >= 4 MB) a decode graph runs many small ops that leave DRAM idle, and its small matvecs wait on cold weights. A side stream now reads the small weights of the next segment into the 32 MB last-level cache while the main stream runs the small ops. The kernel only reads, so results do not change. It runs when llama enables it for the context (ggml_backend_cuda_set_weight_prefetch, set for LLM_ARCH_QWEN4EXP), on RDNA3.5, when no matmul has more than 4 columns, and when no overlap or concurrent-event region is active. The side stream joins the main stream before each CUDA graph chunk cut and at the end of the graph. Qwen4-35B-A3B tg128 on gfx1151: 50.4 -> 54.4 t/s. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Nice improvement and clean measurements — Must fix
Should fix
|
Assert that one MMVQ_FQ_PF chunk covers MMVQ_FQ_ROWS_MAX_K, since the kernel has no K loop. Take the kernel only for y_op 0..2, the ops it implements. Drop the Q8_1 block sum: the Q8_0 dot product reads only the scale, as in mul_mat_vec_q_fq<Q8_0>. Results do not change. Assisted-by: Claude Code Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- Prefetch a weight only if its data is set, it is contiguous, 16-byte aligned and in the CUDA buffer type of the context's device. Weights in host buffers (partial offload) are skipped. - Write the kernel's sentinel result to a real 4-byte device buffer of the context instead of a null pointer, and use all four lanes of each 16-byte load. Loads at a 64 B or 128 B stride lose most of the gain (tg128 50.9 and 50.2 against 53.0 at 16 B with 4-byte loads, master 50.4), so the kernel keeps 16-byte loads. - Start the prefetch of a segment after the CUDA graph chunk cut, so the cut joins only the prefetch of the previous chunk and the new one runs inside the new chunk. - Batch guard: ggml_nrows of src1 for MUL_MAT, token count for MUL_MAT_ID. - Create the events and the sink when the prefetch is enabled, with the device set; define GGML_CUDA_PREFETCH_STREAM next to GGML_CUDA_SIDE_STREAM. Assisted-by: Claude Code Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
@LaurentZuijdwijk Thanks for the careful read. I pushed two commits on top: 2058da6 (short-row matvec) and 28caec6 (prefetch). Point by point:
Verification on gfx1151 against master 4b4ec2d:
Written with AI assistance on behalf of the strix-llama.cpp maintainers. |
Overview
Single-token decode of Qwen4-35B-A3B on gfx1151 ran at 50.4 t/s, well below what its ~3 GB of weights per token allow.
Two changes take it to 54.3 t/s (+7.7%). Results are unchanged and the other models are not affected.
Qwen3.8-Flash-Next is not broken. It is the same
qwen4exparch, so both changes are active for it too. Itsoutput is identical to master (mean KLD 0, 100 % same top-1 at
-ub 1and-ub 4, max per-token KLD equal to masteragainst itself), prefill is unchanged and tg128 is 22.5 t/s against 21.5 on master (details below).
Fixed-grid Q8_0 matvec for many short rows (
mul_mat_vec_q8_0_fq_rows). The hyper-connection up projection(8192 rows, K = 256) launched 512 blocks of 16 waves, and each block re-quantized the activation before one short row
per wave. Now 4 blocks per CU loop over the rows: they quantize the activation once and load the next row's weights
before the dot product of the current one. The loads, dot products and reduction of a row are those of
mul_mat_vec_q_fq<Q8_0>, so the output is bit-identical. Taken only for RDNA3.5, one column, Q8_0 without fusion,K <= 512 and >= 4096 rows.
Decode weight prefetch into the last-level cache (qwen4exp only). Between two large weight reads (MoE experts,
matrices >= 4 MB), a decode graph runs many small ops that leave DRAM idle, and its small matvecs wait on cold
weights. A side stream reads the small weights of the next segment so they are in the 32 MB last-level cache when
their matvecs run. The prefetch kernel only reads, so results do not change. It runs only when:
ggml_backend_cuda_set_weight_prefetch, set forLLM_ARCH_QWEN4EXP);Only weights that are contiguous, 16-byte aligned and in the device buffer of the context are read. The side
stream joins the main stream before each CUDA graph chunk cut and at the end of the graph, and a segment that
starts at a cut is prefetched inside the new chunk.
Measurements
The non-qwen4exp rows are within run-to-run noise (pp512 spreads of 5-30 t/s between rounds).
Qwen3.8-Flash-Next UD-Q4_K_XL (same arch, so the prefetch is on for it too),
-ncmoe 20to stay inside 90 GiB ofGPU memory,
-r 3, two rounds:Correctness
test-backend-ops -b ROCm0: 57363/57363 (MUL_MAT19225/19225 on the final commit). New cases:MUL_MATQ8_0 with n = 1 at 4096/8192/8200 rows and K = 256/512 (the8200-row case has a partial last pass), and
MUL_MAT_EXACT_BATCHQ8_0 n = 2..4 at the qwen4exp shapes260x8192, 8192x256 and 2048x4096 (n = 1 is the new kernel at 8192x256).
tests/corpusfiles concatenated,-c 512 --chunks 2,at
-ub 1and-ub 4(the batch sizes these changes touch):-ncmoe 20)-ncmoe 20)The maximum per-token KLD of ~5e-5 is the run-to-run noise of master itself (master against master: mean 0.000000, max 0.000052 at ub 1 and 0.000053 at ub 4 on Qwen4-35B-A3B, 0.000044 / 0.000047 on Qwen3.8-Flash-Next, the same as this PR against master).
Review fixes (2058da6, 28caec6)
Re-measured after the review fixes, master 4b4ec2d / previous head e59e505 / new head 28caec6,
llama-bench -fa 1 -ngl 99 -p 512,4 -n 128 -r 3, four interleaved rounds (mean):
Logits after prefill and 16-32 greedy tokens at ubatch 1 are byte-identical between master, e59e505 and
28caec6 on Qwen4-35B-A3B and Qwen3.8-Flash-Next, also with GGML_CUDA_GRAPH_CHUNK_SIZE=1 and 37 (a cut at
almost every node); Qwen3-Coder-30B-A3B is byte-identical to master.
Not tested
--parallel > 1.AI usage: the changes, measurements and this description were produced by an AI agent (Claude Code) under my direction, on my box.
🤖 Generated with Claude Code