Skip to content

ggml-webgpu: improve MTP inference by using mat-vec path for small batches - #24811

Merged
yomaytk merged 2 commits into
ggml-org:masterfrom
yomaytk:mat-vec-n8
Jun 23, 2026
Merged

yomaytk merged 2 commits into
ggml-org:masterfrom
yomaytk:mat-vec-n8

Conversation

@yomaytk

@yomaytk yomaytk commented Jun 19, 2026

Copy link
Copy Markdown
Member

Overview

This PR improves small-batch decoding performance by applying the mat-vec path to these cases. This is particularly expected to improve the performance of speculative decoding, such as MTP.

The following table shows the performance of token decoding with llama-server on native WebGPU on M2 Pro (32 GiB RAM).

Qwen 3.5 4B (Q4_K, MTP GGUF)

Based on Qwen/Qwen3.5-4B

Mode Prompt Tokens Speed (t/s)
Normal Hello! who are you? 60 25.82
Normal Please list some popular sports 249 25.16
MTP (master) Hello! who are you? 59 10.07
MTP (master) Please list some popular sports 233 9.37
MTP (PR) Hello! who are you? 59 28.48
MTP (PR) Please list some popular sports 570 24.32

Qwen 3.6 27B (Q4_K, MTP GGUF)

Based on ggml-org/Qwen3.6-27B-MTP-BF16-GGUF

Mode Prompt Tokens Speed (t/s)
Normal Hello! who are you? 61 5.93
Normal Please list some popular sports 411 5.98
MTP (master) Hello! who are you? 61 1.94
MTP (master) Please list some popular sports 410 1.99
MTP (PR) Hello! who are you? 50 6.91
MTP (PR) Please list some popular sports 425 6.84

I can confirm that MTP decoding performance is significantly improved, and it is slightly better than normal decoding in this benchmark except for one case.

Additional Information

Reproduction (Qwen 3.6 27B):

  1. Quantize with:
    llama-quantize Qwen3.6-27B-MTP-BF16.gguf Qwen3.6-27B-MTP-Q4_K_M.gguf Q4_K_M

  2. Run llama-server:

llama-server \
    -m Qwen3.6-27B-MTP-Q4_K_M.gguf \
    --spec-type draft-mtp \
    -ngl all \
    --spec-draft-ngl all \
    --spec-draft-device WebGPU \
    -c 35000 \
    -np 1 \
    -b 2048 \
    -ub 1024 \
    --temp 0 \
    --spec-draft-n-max 2 \
    --spec-draft-p-min 0.6 \
    --host 127.0.0.1 \
    --port 8080 \
    -dev WebGPU

Requirements

  • I have read and agree with the contributing guidelines
  • AI usage disclosure: Yes — I used AI to generate the repeated code in mul_mat_vec_acc.tmpl and to analyze the performance log files.

@yomaytk
yomaytk requested review from a team and ggerganov as code owners June 19, 2026 15:21
@github-actions github-actions Bot added testing Everything test related ggml changes relating to the ggml tensor library for machine learning WebGPU labels Jun 19, 2026
ggml_tensor * dst) {
// Determine if this is a mat-vec operation
bool is_vec = (dst->ne[1] == 1);
bool use_mat_vec = (dst->ne[1] <= 4);

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Vulkan sets this threshold to 8, but I confirmed that some cases degrade performance on WebGPU when n >= 5. For example, Q2_K and Q5_K degrade at n=5, and all k-quants types degrade at n=8.

if (key.vectorized) {
variant += "_vectorized";
}
defines.push_back(std::string("NUM_COLS=1"));

@yomaytk yomaytk Jun 19, 2026 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I think that it is difficult to use mul_mat_vec_acc.tmpl as is for mul_mat_id, since the tokens for a single selected expert are not contiguous. So this PR doesn't address mul_mat_id, and I'll leave it as future work.

@yomaytk

yomaytk commented Jun 19, 2026

Copy link
Copy Markdown
Member Author

@reeselevine, can you check whether it works and measure the performance of this PR on an RTX machine when you have a chance? In my NVIDIA environment (Tesla V100), running the above MTP model produces random incorrect outputs, so I cannot measure the performance myself yet (for MMVQ path).

@CISC

CISC commented Jun 19, 2026

Copy link
Copy Markdown
Member

@yomaytk

yomaytk commented Jun 22, 2026

Copy link
Copy Markdown
Member Author

Failing tests:
https://github.com/ggml-org/llama.cpp/actions/runs/27834228205/job/82378241588?pr=24811#step:4:37525

Interesting, gpu-webgpu-apple failed, but all tests pass in my M2. I'll take a look.

@yomaytk

yomaytk commented Jun 22, 2026

Copy link
Copy Markdown
Member Author

workgroupBarrier is actually needed in the NUM_COLS loop in mul_mat_vec, and gpu-webgpu-apple now passes after adding it.

@reeselevine

reeselevine commented Jun 23, 2026 •

Copy link
Copy Markdown
Contributor

Thanks for the addition! I'm having some trouble getting mtp to work on the NVIDIA GPU I have access to, it's on a server with multiple GPUs and it seems like some combination of trying to get WebGPU to use it and MTP support is causing issues... I'll try and look into that at some point here (I'm on a bit of vacation this week), but in the meantime I think we can trust that this should help :).

I also think eventually we should look into failures on GPUs like the V100 and Intel GPUs (like this PR: #24887). But no big rush on that.

@yomaytk

yomaytk commented Jun 23, 2026

Copy link
Copy Markdown
Member Author

@reeselevine Thank you for checking this. Yeah, MTP seems to cause some issues on NVIDIA as expected.

I also think eventually we should look into failures on GPUs like the V100 and Intel GPUs (like this PR: #24887). But no big rush on that.

I see, I'll investigate when I have a chance. And thank you for the review during your vacation. Enjoy it! :)

@yomaytk
yomaytk merged commit 7c90850 into ggml-org:master Jun 23, 2026
28 of 29 checks passed
@yomaytk
yomaytk deleted the mat-vec-n8 branch June 23, 2026 08:14
Geminihaha pushed a commit to Geminihaha/llama.cpp that referenced this pull request Jun 25, 2026
…tches (ggml-org#24811)

* ggml-webgpu: improve small batches decoding

* Add barrier to the NUM_COLS loop in mul-mat-vec
adrianhoehne pushed a commit to adrianhoehne/llama.cpp that referenced this pull request Jul 5, 2026
…tches (ggml-org#24811)

* ggml-webgpu: improve small batches decoding

* Add barrier to the NUM_COLS loop in mul-mat-vec
zommiommy pushed a commit to zommiommy/llama.cpp that referenced this pull request Aug 18, 2026
…tches (ggml-org#24811)

* ggml-webgpu: improve small batches decoding

* Add barrier to the NUM_COLS loop in mul-mat-vec
zbrad pushed a commit to zbrad/llama.cpp that referenced this pull request Sep 10, 2026
…tches (ggml-org#24811)

* ggml-webgpu: improve small batches decoding

* Add barrier to the NUM_COLS loop in mul-mat-vec
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ggml changes relating to the ggml tensor library for machine learning testing Everything test related WebGPU

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants