Skip to content

Another speed gain for Q4_0 and Q4_1 on Metal - #2375

Merged
ikawrakow merged 2 commits into
masterfrom
ik/metal_q4_0_1_new
Jul 25, 2023
Merged

ikawrakow merged 2 commits into
masterfrom
ik/metal_q4_0_1_new

Conversation

@ikawrakow

Copy link
Copy Markdown
Contributor

In #2279 there was some debate on whether it is better to have each thread in a Metal SIMD group process whole Q4_0 or Q4_1 blocks, 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

./main -m $model -p "I believe the meaning of life is" --ignore-eos -n 128 -s 1234 --no-mmap -t 8 -ngl 1
Quantization t/s Master t/s PR speedup
Q4_0 - 7B 18.6 17.6 5.7%
Q4_1 - 7B 20.0 18.9 5.8%
Q4_0 - 13B 32.5 30.1 8.0%
Q4_1 - 13B 33.7 32.2 4.7%
Q4_0 - 33B 76.5 67.2 11.4%
Q4_1 - 33B 82.3 75.3 9.3%
Q4_0 - 65B 146.8 133.4 10.0%
Q4_1 - 65B 152.8 145.4 5.1%

@ikawrakow
ikawrakow requested review from ggerganov and lshzh-ww July 24, 2023 15:50
@lshzh-ww

Copy link
Copy Markdown
Contributor

Test on M1 Max 32c

Quantization t/s Master t/s PR speedup
Q4_0 - 7B 19.18 18.07 6.1%
Q4_0 - 33B 73.9 70.9 4.2%

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.

@ikawrakow

Copy link
Copy Markdown
Contributor Author

I am ashamed that I didn't test intensively last time; we would have lost quite a bit of performance

@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!

Comment thread ggml-metal.metal
//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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. Thank you for answering my question.

@ggerganov ggerganov left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@ggerganov

Copy link
Copy Markdown
Member

@ikawrakow and @lshzh-ww

I assume your tables show ms/t instead of t/s, correct?

@ikawrakow

Copy link
Copy Markdown
Contributor Author

@ikawrakow and @lshzh-ww

I assume your tables show ms/t instead of t/s, correct?

Yes.

@ikawrakow
ikawrakow merged commit 9a08eaf into master Jul 25, 2023
@ikawrakow
ikawrakow deleted the ik/metal_q4_0_1_new branch July 25, 2023 10:48
Seunghhon pushed a commit to Seunghhon/llama.cpp that referenced this pull request Apr 26, 2026
* 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]>
phuongncn pushed a commit to phuongncn/llama.cpp-gx10-dgx-sparks-deepseekv4 that referenced this pull request Apr 28, 2026
* 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]>
ljubomirj pushed a commit to ljubomirj/llama.cpp that referenced this pull request May 6, 2026
* 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]>
AlexiAlp pushed a commit to minghaop/llama.cpp that referenced this pull request Jun 2, 2026
* 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]>
AlexiAlp pushed a commit to minghaop/llama.cpp that referenced this pull request Jun 2, 2026
* 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]>
TrevorS added a commit to TrevorS/llama.cpp that referenced this pull request Aug 31, 2026
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).
TrevorS added a commit to TrevorS/llama.cpp that referenced this pull request Sep 16, 2026
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).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants