Skip to content

TP: fix split state and granularity for fused QKV gemma4, qwen35 - #28965

Merged
ggerganov merged 3 commits into
ggml-org:masterfrom
dfriehs:tp-fused-qkv
Sep 16, 2026
Merged

ggerganov merged 3 commits into
ggml-org:masterfrom
dfriehs:tp-fused-qkv

Conversation

@dfriehs

@dfriehs dfriehs commented Sep 15, 2026

Copy link
Copy Markdown
Contributor

Overview

#22780 adds the option to fuse QKV for models which previously had either no QKV tensors (gemma4) or for which QKV tensors only existed for specific layers (qwen35, qwen35moe, ...).

Gemma 4 31B: n_embd is 5376 but Q is 8192 (raising GGML_ASSERT(tensor->ne[axis] == n_embd + 2*n_embd_gqa);), so calculate from n_head * n_embd_head_k instead.

Qwen 27B/35B-A3B: added a copy of the default qkv handler for full attention layers, with n_embd doubled for Q gate tensors, and add the doubling in get_split_granularity as well.

Additional information

I tested Gemma 4 31B, Qwen 3.8 27B and Qwen 3.6 35B-A3B on 4 P100 cards.

Requirements

  • I have read and agree with the contributing guidelines
  • AI usage disclosure: YES. I used Qwen 3.8 27B to analyze the errors I was getting with the fused models and then to reason through the changes required.

@dfriehs
dfriehs requested a review from CISC as a code owner September 15, 2026 19:19
@CISC CISC added the merge ready A maintainer can use this label to indicate that they consider the changes final and ready to merge. label Sep 16, 2026

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

What is the effect before and after this PR?

Comment thread src/llama-model.cpp
Comment on lines +776 to +778
// fused full attention layers need Q gate tensors handled like above:
if (ud->model->arch == LLM_ARCH_QWEN3NEXT || ud->model->arch == LLM_ARCH_QWEN35 || ud->model->arch == LLM_ARCH_QWEN35MOE ||
ud->model->arch == LLM_ARCH_QWEN4EXP) {

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.

We should deduplicate this condition if it has to match the one earlier:

Suggested change
// fused full attention layers need Q gate tensors handled like above:
if (ud->model->arch == LLM_ARCH_QWEN3NEXT || ud->model->arch == LLM_ARCH_QWEN35 || ud->model->arch == LLM_ARCH_QWEN35MOE ||
ud->model->arch == LLM_ARCH_QWEN4EXP) {
// fused full attention layers need Q gate tensors handled like above:
// TODO: deduplicate with the condition in `get_split_segments` [TAG_SPLIT_FUSED_QKV_QWEN]
if (ud->model->arch == LLM_ARCH_QWEN3NEXT || ud->model->arch == LLM_ARCH_QWEN35 || ud->model->arch == LLM_ARCH_QWEN35MOE ||
ud->model->arch == LLM_ARCH_QWEN4EXP) {

Comment thread src/llama-model.cpp
Comment on lines 600 to 601
if (ud->model->arch == LLM_ARCH_QWEN3NEXT || ud->model->arch == LLM_ARCH_QWEN35 || ud->model->arch == LLM_ARCH_QWEN35MOE ||
ud->model->arch == LLM_ARCH_QWEN4EXP) {

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.

Suggested change
if (ud->model->arch == LLM_ARCH_QWEN3NEXT || ud->model->arch == LLM_ARCH_QWEN35 || ud->model->arch == LLM_ARCH_QWEN35MOE ||
ud->model->arch == LLM_ARCH_QWEN4EXP) {
// TODO: clarify why this is necessary specifically for these models
// TODO: deduplicate [TAG_SPLIT_FUSED_QKV_QWEN]
if (ud->model->arch == LLM_ARCH_QWEN3NEXT || ud->model->arch == LLM_ARCH_QWEN35 || ud->model->arch == LLM_ARCH_QWEN35MOE ||
ud->model->arch == LLM_ARCH_QWEN4EXP) {

@dfriehs

dfriehs commented Sep 16, 2026 •

Copy link
Copy Markdown
Contributor Author

What is the effect before and after this PR?

On master both gemma4 and qwen35/qwen35moe crash for models exported with --fuse-qkv when -sm tensor is used.

I don't really have benchmarks, but I seem to get +2% tg on Qwen 3.6 35B-A3B with fused QKV on 4 cards (Q8_0: 70.86 -> 72.4), although both numbers use additional patches (force enabling cudagraphs on P100 which is only a net positive for MoE's on 4 cards or more).

TODO: clarify why this is necessary specifically for these models

My understanding is that attn_q contains both q and gate tensors, which is why it has double the elements and needs double the granularity.

@dfriehs

dfriehs commented Sep 16, 2026

Copy link
Copy Markdown
Contributor Author

@ggerganov I added a tag like your suggestions although I named it [TAG_SPLIT_QGATE_QWEN], hope this is alright. The doubled granularity is not specific for fused QKV and was already present for attn_q previously, I only extended it to fused QKV. I also added the tag to the attn_q handler that already existed previously.

The change to segmentation for qwen was necessary as the current code assumes the layout for recurrent layers for QKV tensors and no segmentation was necessary for other layers until now.

@ggerganov
ggerganov merged commit fb27a52 into ggml-org:master Sep 16, 2026
1 check passed
@dfriehs
dfriehs deleted the tp-fused-qkv branch September 17, 2026 06:53
CISC pushed a commit that referenced this pull request Sep 17, 2026
)

* model: calculate split states for attn_qkv from n_head * n_embd_head_k

required for gemma4 with --fuse-qkv, where n_embd is 5376 but Q is 8192.

* model: handle fused full attention layers for qwen35/qwen35moe

* model: add TODO: [TAG_SPLIT_QGATE_QWEN]
adromir pushed a commit to adromir/llama-cpp-turboquant that referenced this pull request Sep 17, 2026
…l-org#28965)

* model: calculate split states for attn_qkv from n_head * n_embd_head_k

required for gemma4 with --fuse-qkv, where n_embd is 5376 but Q is 8192.

* model: handle fused full attention layers for qwen35/qwen35moe

* model: add TODO: [TAG_SPLIT_QGATE_QWEN]
Te-eMster pushed a commit to Te-eMster/mx-llama.cpp that referenced this pull request Sep 18, 2026
…l-org#28965)

* model: calculate split states for attn_qkv from n_head * n_embd_head_k

required for gemma4 with --fuse-qkv, where n_embd is 5376 but Q is 8192.

* model: handle fused full attention layers for qwen35/qwen35moe

* model: add TODO: [TAG_SPLIT_QGATE_QWEN]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

merge ready A maintainer can use this label to indicate that they consider the changes final and ready to merge.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants