Repository navigation
Another speed gain for Q4_0 and Q4_1 on Metal - #2375
Conversation
|
Test on M1 Max 32c
The performance improvement is solid. I am ashamed that I didn't test intensively last time; we would have lost quite a bit of performance if it weren't for you. |
@lshzh-ww That doesn't make sense. Your contribution in #2188 and #2248 were absolutely essential for improving Metal performance. Before you came along with #2188 we were basically stuck at 21.5 ms/t on M2 Max for the 7B model. This kind of collaborative progress is the whole point of open source! |
| //Note: This is a template, but strictly speaking it only applies to | ||
| // quantizations where the block size is 32. It also does not | ||
| // giard against the number of rows not being divisible by | ||
| // N_DST, so this is another explicit assumption of the implementation. |
There was a problem hiding this comment.
I think this template also works on matrices with number of rows not being divisible by N_DST. In the very last of template we have if (tiisg == 0 && first_row + row < ne01) which guarantees that we don't write into other tensor's region when there are less than N_DST rows left.
I checked by using a WizardLM model, which has a row number of 32001. This template generated exactly the same results as the llama.cpp before #2188 was merged.
There was a problem hiding this comment.
Well, yes, there is a guard to not write outside of the result bounds. But there is no guard to not read outside the bounds of the quantized tensor. We don't get a segfault because we only step out slightly and the address being accessed is within the process address space, but there is no guarantee that this will always be the case.
There was a problem hiding this comment.
I see. Thank you for answering my question.
ggerganov
left a comment
There was a problem hiding this comment.
M1 Pro
| Quantization | ms/t Master | ms/t PR |
|---|---|---|
| Q4_0 - 7B | 30.2 | 28.7 |
| Q4_1 - 7B | 32.9 | 31.2 |
| Q4_0 - 13B | 53.5 | 50.7 |
| Q4_1 - 13B | 58.1 | 55.6 |
|
@ikawrakow and @lshzh-ww I assume your tables show |
Yes. |
* Another speed gain for Q4_0 and Q4_1 on Metal * Have N_DST, etc., be template parameters --------- Co-authored-by: Iwan Kawrakow <[email protected]>
* Another speed gain for Q4_0 and Q4_1 on Metal * Have N_DST, etc., be template parameters --------- Co-authored-by: Iwan Kawrakow <[email protected]>
* Another speed gain for Q4_0 and Q4_1 on Metal * Have N_DST, etc., be template parameters --------- Co-authored-by: Iwan Kawrakow <[email protected]>
* Another speed gain for Q4_0 and Q4_1 on Metal * Have N_DST, etc., be template parameters --------- Co-authored-by: Iwan Kawrakow <[email protected]>
* Another speed gain for Q4_0 and Q4_1 on Metal * Have N_DST, etc., be template parameters --------- Co-authored-by: Iwan Kawrakow <[email protected]>
An nsys profile of a pp4096 run put 27.3% of prefill in elementwise kernels and
only 34.9% in matmul. The elementwise share is almost entirely the two hyper-
connection helpers, which run twice per layer across 48 layers -- 96 call sites
each -- and both materialise a full-width intermediate purely to read it back.
Two ops, both F32-only, both with a CPU reference so test-backend-ops can check
them:
ggml_hc_gate_mix(x, gate, hc, scale)
dst[e,t] = scale * sum_c x[e + c*n_embd, t] * gate[e + c*n_embd, t]
replaces mul + cont + (hc-1) adds over strided views + scale
ggml_hc_scatter_add(residual, block, inject, inv_hc)
dst[e,c,t] = residual[e,c,t] + block[e,t] * 2*sigmoid(inject[c,t] * inv_hc)
replaces sigmoid + 2x scale + repeat_4d + mul + add
The win is traffic, not arithmetic. Each chain wrote an [hc*n_embd, n_tokens]
tensor -- 168 MB at 4k tokens -- that existed only to be consumed by the next
kernel. Fused, the gated product stays in registers and block_out is re-read per
stream from cache instead of being repeat_4d'd into a full copy.
Measured on GB10, UD-IQ4_XS, llama-bench pp4096 at ub4096, same binary with
GGML_QWEN4EXP_HC_FUSED flipped:
off 636.47 +/- 4.25 t/s
scatter-add only 665.51 +/- 3.99 t/s (+4.6%)
both 685.23 +/- 3.28 t/s (+7.7%)
The off-arm reproduced at 636.37 and 636.47 across separate runs, so the
instrument resolves well under the 49 t/s gap.
A second nsys profile confirms the mechanism rather than just the outcome:
elementwise 27.3% -> 12.7%, the new ops cost 6.2%, so 8.4 points of net work
removed, and total GPU kernel time 5.33 s -> 5.00 s (-6.2%) -- an independent
corroboration of the throughput number.
Both fusions sit behind GGML_QWEN4EXP_HC_FUSED (default on, =0 restores the old
chains) so the two paths stay A/B-able on one build. They are not bit-identical
to each other: fusing changes the reduction order, and the arch test NMSE moves
8.54e-08 -> 8.64e-08 against an exact CPU reference.
What this does not fix: mm_ids_helper is now the largest single non-matmul kernel
at 11.9% and is untouched -- that is the same target as ik_llama ggml-org#2375 and
upstream ggml-org#26159. And matmul is 37.5% of the remaining time, which caps what any
further overhead removal can buy.
Gates: HC_SCATTER_ADD 4/4, HC_GATE_MIX 4/4, DSV4 fused ops 2/2 backends,
MUL_MAT/MUL_MAT_ID/ADD/MUL/SCALE/REPEAT/RMS_NORM 2319/2319, test-llama-archs
qwen4exp OK (GB10 8.64e-08, CPU 0.00e+00).
An nsys profile of a pp4096 run put 27.3% of prefill in elementwise kernels and
only 34.9% in matmul. The elementwise share is almost entirely the two hyper-
connection helpers, which run twice per layer across 48 layers -- 96 call sites
each -- and both materialise a full-width intermediate purely to read it back.
Two ops, both F32-only, both with a CPU reference so test-backend-ops can check
them:
ggml_hc_gate_mix(x, gate, hc, scale)
dst[e,t] = scale * sum_c x[e + c*n_embd, t] * gate[e + c*n_embd, t]
replaces mul + cont + (hc-1) adds over strided views + scale
ggml_hc_scatter_add(residual, block, inject, inv_hc)
dst[e,c,t] = residual[e,c,t] + block[e,t] * 2*sigmoid(inject[c,t] * inv_hc)
replaces sigmoid + 2x scale + repeat_4d + mul + add
The win is traffic, not arithmetic. Each chain wrote an [hc*n_embd, n_tokens]
tensor -- 168 MB at 4k tokens -- that existed only to be consumed by the next
kernel. Fused, the gated product stays in registers and block_out is re-read per
stream from cache instead of being repeat_4d'd into a full copy.
Measured on GB10, UD-IQ4_XS, llama-bench pp4096 at ub4096, same binary with
GGML_QWEN4EXP_HC_FUSED flipped:
off 636.47 +/- 4.25 t/s
scatter-add only 665.51 +/- 3.99 t/s (+4.6%)
both 685.23 +/- 3.28 t/s (+7.7%)
The off-arm reproduced at 636.37 and 636.47 across separate runs, so the
instrument resolves well under the 49 t/s gap.
A second nsys profile confirms the mechanism rather than just the outcome:
elementwise 27.3% -> 12.7%, the new ops cost 6.2%, so 8.4 points of net work
removed, and total GPU kernel time 5.33 s -> 5.00 s (-6.2%) -- an independent
corroboration of the throughput number.
Both fusions sit behind GGML_QWEN4EXP_HC_FUSED (default on, =0 restores the old
chains) so the two paths stay A/B-able on one build. They are not bit-identical
to each other: fusing changes the reduction order, and the arch test NMSE moves
8.54e-08 -> 8.64e-08 against an exact CPU reference.
What this does not fix: mm_ids_helper is now the largest single non-matmul kernel
at 11.9% and is untouched -- that is the same target as ik_llama ggml-org#2375 and
upstream ggml-org#26159. And matmul is 37.5% of the remaining time, which caps what any
further overhead removal can buy.
Gates: HC_SCATTER_ADD 4/4, HC_GATE_MIX 4/4, DSV4 fused ops 2/2 backends,
MUL_MAT/MUL_MAT_ID/ADD/MUL/SCALE/REPEAT/RMS_NORM 2319/2319, test-llama-archs
qwen4exp OK (GB10 8.64e-08, CPU 0.00e+00).
In #2279 there was some debate on whether it is better to have each thread in a Metal SIMD group process whole
Q4_0orQ4_1blocks, or have them process half blocks.On my M2 Max with 30-core GPU having threads process half a block is definitely faster, see table below. I'm curious to see if this change also improves (or worsens) performance on M1 Pro/Max.
TG-128 in ms/token on 30-core M2 Max. The command used was