Skip to content

llama: Add option to merge gate and exp weights - #19139

Merged
am17an merged 8 commits into
ggml-org:masterfrom
am17an:exp_weight_merge_glm
Feb 26, 2026
Merged

am17an merged 8 commits into
ggml-org:masterfrom
am17an:exp_weight_merge_glm

Conversation

@am17an

@am17an am17an commented Jan 27, 2026

Copy link
Copy Markdown
Contributor

Continuing on #18740 and #18866, add option --fuse_gate_up_exps to convert_hf_to_gguf.py.
I've just added the gate_up tracking for deepseek2 (GLM 4.7 flash) and gpt-oss - although for gpt-oss we need even more changes (it goes through the generate_extra_tensors for generating expert weights). This PR is not complete as we would need to add this check in all MoE models and their tensors, but putting it out there in any case.

on 5090:

Master

model size params backend ngl fa test t/s
deepseek2 30B.A3B Q4_K - Medium 16.88 GiB 29.94 B CUDA 99 1 pp2048 6269.31 ± 11.73
deepseek2 30B.A3B Q4_K - Medium 16.88 GiB 29.94 B CUDA 99 1 pp4096 5833.01 ± 11.12
deepseek2 30B.A3B Q4_K - Medium 16.88 GiB 29.94 B CUDA 99 1 pp8192 5137.33 ± 16.16
deepseek2 30B.A3B Q4_K - Medium 16.88 GiB 29.94 B CUDA 99 1 tg128 168.98 ± 0.68

this PR (with the fused GGUF)

model size params backend ngl fa test t/s
deepseek2 30B.A3B Q4_K - Medium 16.88 GiB 29.94 B CUDA 99 1 pp2048 6846.90 ± 10.22
deepseek2 30B.A3B Q4_K - Medium 16.88 GiB 29.94 B CUDA 99 1 pp4096 6303.35 ± 15.73
deepseek2 30B.A3B Q4_K - Medium 16.88 GiB 29.94 B CUDA 99 1 pp8192 5502.23 ± 9.50
deepseek2 30B.A3B Q4_K - Medium 16.88 GiB 29.94 B CUDA 99 1 tg128 167.92 ± 0.75

@CISC

CISC commented Jan 27, 2026

Copy link
Copy Markdown
Member

although for gpt-oss we need even more changes (it goes through the generate_extra_tensors for generating expert weights).

Ah, should probably also go through those to ensure they are all generators and yielding from base then.

Comment thread convert_hf_to_gguf.py Resolved
Comment thread convert_hf_to_gguf.py Resolved
Comment thread gguf-py/gguf/constants.py Resolved
Comment thread convert_hf_to_gguf.py Resolved
Comment thread convert_hf_to_gguf.py Resolved
@am17an
am17an force-pushed the exp_weight_merge_glm branch from c846c0b to 93c09b0 Compare January 27, 2026 17:11
Comment thread convert_hf_to_gguf.py Resolved
@ngxson

ngxson commented Jan 27, 2026 •

Copy link
Copy Markdown
Collaborator

Still, I think there are some problems with the conversion script:

  • For gpt-oss, the model already come with the gate_up fused. It's split and repacked inside generate_extra_tensors, so in theory, up/gate won't be passed to modify_tensors. Instead, GptOssModel.generate_extra_tensors() method must be aware of either fuse_gate_up_exps is enabled or not, and repack it accordingly without going through modify_tensors (which is known to do dequant --> requant roud trip)
  • The cpp code currently only support gpt-oss and deepseek2, so I think the conversion script must throw an error on any other models. This is to avoid user to produce a non-working GGUF

@am17an

am17an commented Jan 28, 2026

Copy link
Copy Markdown
Contributor Author

I tried to address 2) above. Full disclosure I used AI for this to make a list of MoE models and make all the edits.

For gpt-oss, I don't know what is the correct way to do it, one way is what I added in #18740, I can add the same here.

@CISC

CISC commented Jan 28, 2026 •

Copy link
Copy Markdown
Member

I tried to address 2) above.

You must update all arches in tensor_mapping.pyconstants.py too, otherwise conversion will fail (so, really, this point was already addressed :)).

@am17an

am17an commented Jan 28, 2026

Copy link
Copy Markdown
Contributor Author

You must update all arches in tensor_mapping.py too

I don't understand what you mean. Please assume I have no experience with conversion.

@CISC

CISC commented Jan 28, 2026

Copy link
Copy Markdown
Member

You must update all arches in tensor_mapping.py too

I don't understand what you mean. Please assume I have no experience with conversion.

Sorry, I meant constants.py, since we changed it to use format_tensor_name:
https://github.com/ggml-org/llama.cpp/blob/566884c5dd90892d7c07a240093f6c5651cc7f18/convert_hf_to_gguf.py#L489-L491

@ngxson

ngxson commented Jan 28, 2026 •

Copy link
Copy Markdown
Collaborator

IMO it's probably better to find a way to generalize this, rather than apply the change to each models. Also, maybe only apply this to a limited number of models first, then see if it actually works.

What I'm worry is that contributors may not aware of this change and some models will eventually forget to support this. Also, it addes a bit more boilerplate to new models' code. We should find a way to establish a new rule that new models should use merged gate/up by default

@am17an

am17an commented Jan 28, 2026

Copy link
Copy Markdown
Contributor Author

My initial PR #18740 did this only for 1 model. The discussion was that it should be done for all models, for this we needed to call ModelBase::modify_tensors from each model's modify_tensors. See #18866, #19084, #19091. That was to lay the groundwork for doing this. This PR is also initially did it for 1 model (GLM 4.7 flash) but it would be weird to support a flag for one model and I can foresee people asking for support for other models.

Also, it addes a bit more boilerplate to new models' code.

The llama-arch.cpp file is full of boilerplate code. I don't see what this PR changes. A function can be made to abstract the if statements if that's you mean. I was planning to do that for the Q,K,V merge anyway.

With that said, I don't care if all models get support, especially since it involves a lot of code changes which aren't exactly fun or foolproof. My aim is extract maximum performance out of a select few models which are proven to have many users like gpt-oss, qwen3 and now glm 4.7 flash. So the solution I see is this PR with some arch's which have been verified to work, the rest can either fail through constants.py or we can hardcode the supported arch set.

@ngxson

ngxson commented Jan 28, 2026 •

Copy link
Copy Markdown
Collaborator

The llama-arch.cpp file is full of boilerplate code. I don't see what this PR changes. A function can be made to abstract the if statements if that's you mean. I was planning to do that for the Q,K,V merge anyway.

Most of them are not really boilerplate code, for example: you may see most models copy the same FFN/FFN_EXPS code, but this is because the tensor dimension must be specified and some models indeed can have different shapes for it, this is totally up to the original model (or the people who created the model) to decide. So, they are not actually boilerplate code, but they are configurations.

Definition of merged gate/up and QKV are not the same because they can be inferred from the existing code. In terms of functional programming: you can have a pure function to transform from the unmerged "definition" to merged one.

My suggestion is that we can avoid adding llama.cpp-specific code paths like what you're doing into the model "definition". Instead, it can be implemented as a transformation inside create_tensor, logic will be somewhat like:

  • if the tensor name tn is gate or up, see if we already had another up or gate added
  • if yes, look up in the list of tensors if we have gate_up merged tensor
  • if no, treat is as normal

The same logic can be reused for QKV merged. We can potentially generalize it further so more merged FFN and merged QKV can reuse exactly the same code path with minor modifications.

Otherwise, if we only support the 3 models as your said:

My aim is extract maximum performance out of a select few models which are proven to have many users like gpt-oss, qwen3 and now glm 4.7 flash.

I (more) agree with this because it makes things easier to test and to track down issues. As pointed out in my comments earlier, there were some issues regarding gpt-oss. So it makes more sense to scale down the scope of this PR, and put more efforts into testing it thoroughly

@ngxson

ngxson commented Jan 28, 2026

Copy link
Copy Markdown
Collaborator

My suggestion is that we can avoid adding llama.cpp-specific code paths like what you're doing into the model "definition". Instead, it can be implemented as a transformation inside create_tensor

Otherwise, another way to do is to abstract the loading of FFN into a new function, create_tensor_ffn. This can already significantly reduce the amount of code, while making it more aligned with build_ffn

@am17an

am17an commented Jan 28, 2026

Copy link
Copy Markdown
Contributor Author

Definition of merged gate/up and QKV are not the same because they can be inferred from the existing code. In terms of functional programming: you can have a pure function to transform from the unmerged "definition" to merged one.

Not sure what you mean. We can merge Q,K,V into a one big matrix and same for gate and up. I'm not able to see the difference and even your proposed implementation uses the same path for both.

Otherwise, if we only support the 3 models as your said:

Re the create_tensors part, I'm not willing to take an entirely different implementation approach to the current PR. So we can limit the scope to the 2-3 models and we can move on

@am17an

am17an commented Jan 28, 2026

Copy link
Copy Markdown
Contributor Author

And btw this pattern already exists in llama-model.cpp, checking for the fused tensor and fallback if not

https://github.com/ggml-org/llama.cpp/blob/362bff9773cd4fc1ea85bb78ebed29bf7d1e17c9/src/llama-model.cpp#L3297-L3309

@CISC

CISC commented Jan 28, 2026 •

Copy link
Copy Markdown
Member

And btw this pattern already exists in llama-model.cpp, checking for the fused tensor and fallback if not

A create_tensor_ffn/create_tensor_qkv sounds like a good idea, though since it would have to update layers[i] directly that would probably not be a good name for it.

Edit: Either way, leave it for a follow-up.

@ngxson

ngxson commented Jan 28, 2026

Copy link
Copy Markdown
Collaborator

Not sure what you mean. We can merge Q,K,V into a one big matrix and same for gate and up. I'm not able to see the difference and even your proposed implementation uses the same path for both.

What I mean was that when writing the tensor loading code, the create_tensor is merely to describe what are the shape of the QKV tensor and some addition flags.

The detail of whether they are merged or not should be abstracted out, because it's has nothing with the model definition itself.

In terms of functional programming, it doesn't make sense to write something like:

q = load_q(n_embd, n_dim_k)
k = load_k(n_embd, n_dim_k)
v = load_v(n_embd, n_dim_v)
if (!q && !k && !v)
  qkv = load_qkv(n_embd * 3, n_dim_qkv)

Instead, load_qkv can already be derived from load_q | k | v, duplicating the code is redundant. It can be abstracted out, something like create_tensor_qkv

It doesn't change the implementation under the hood, but the point is that: model definition should ONLY convey what make up the model, NOT what we added to make it faster

@am17an

am17an commented Feb 25, 2026

Copy link
Copy Markdown
Contributor Author

Sorry I kind of abandoned this PR because it seemed like we can't reach a consensus. What is something we can all agree on that should be mergeable? I don't think this sizeable speedup which is relatively free should just go unmerged. @ggerganov maybe you can weigh in as well

@ngxson

ngxson commented Feb 25, 2026 •

Copy link
Copy Markdown
Collaborator

I think the first step could be to abstract out a create_tensor_qkv as I suggested above, it should be a dedicated PR for visibility (and to allow git bisect if anything goes wrong)

And as explained earlier, GPT-OSS repacking won't work as-is, it need to be adapted somehow. I'll have a look if I have time, but for now I think we can leave out the GPT-OSS from this PR (I suspect it will currently be dequant --> quant round trip)

In short, I'm just trying to reduce the scope of this PR, as it still touching quite many models.

@ggerganov

Copy link
Copy Markdown
Member

For my understanding, the python changes are good, correct?

@ngxson

ngxson commented Feb 25, 2026

Copy link
Copy Markdown
Collaborator

@ggerganov the code suggests that python code won't work correctly with GPT-OSS because of the custom MXFP4 repacking logic, though @am17an please correct if I'm wrong

@am17an

am17an commented Feb 25, 2026

Copy link
Copy Markdown
Contributor Author

I think the first step could be to abstract out a create_tensor_qkv as I suggested above

Okay I will refactor to create a function create_tensor_up_gate_exps which does a similar pattern. I agree that GPT-OSS can be handled separately as a special case.

For my understanding, the python changes are good, correct?

Yes, as @ngxson said, any custom packing logic would need a special case. Although it seems like we don't have a generic way of converting mxfp4 models, adding that might fix this more generally

@ggerganov

Copy link
Copy Markdown
Member

Ok got it. The gpt-oss needs some extra work since it utilizes the generate_extra_tensors() mechanism in order to create mxfp4 tensors directly. We can address this later on, shouldn't be too difficult.

The create_tensor_up_gate_exps and create_tensor_qkv sound good to reduce repetitiveness. Thought it should be fine to do it in a follow-up PR too. Either way I think is fine.

Comment thread src/llama-model.cpp
layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert }, 0);
layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), { n_ff_exp, n_embd, n_expert }, 0);
layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert }, 0);
create_tensor_gate_up_exps(layer, i, n_embd, n_ff_exp, n_expert, 0);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

could be better to have a create_tensors_ffn, so that will be aligned with build_ffn

but that can be another PR, this is good for now

Comment thread src/models/deepseek2.cpp
il);
il,
nullptr,
model.layers[il].ffn_gate_up_exps);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

probably better to regroup all ffn tensors into a new model.layers[il].ffn struct, so that build_ffn / build_moe_ffn will only take one single param for weight.

I'll do that in a dedicated PR

@am17an

am17an commented Feb 26, 2026

Copy link
Copy Markdown
Contributor Author

Ok I will merge this, the one issue I see is that --fuse_gate_up_weights is available for every model, but the GGUF will only work for the models given above which have the cpp part implemented, there is currently no easy way to verifying that the support is only limited to so and so models (apart from hardcoding it), it will be best to do the whole thing at once. We can do it once we are sure this doesn't cause any unforeseen issues.

@am17an
am17an merged commit b68d751 into ggml-org:master Feb 26, 2026
81 of 82 checks passed
@am17an
am17an deleted the exp_weight_merge_glm branch February 26, 2026 13:01
@ubergarm

ubergarm commented Feb 26, 2026 •

Copy link
Copy Markdown
Contributor

People are asking me already if I used this new convert_hf_to_gguf.py --fuse-gate-up-exps (there is no --fuse_gate_up_weights) for recent qwen3 models...

My concern is if someone generates a bf16 GGUF using --fuse-gate-up-exps and calculates imatrix on it, then it will be missing data if someone tries to use the same imatrix file on a bf16 GGUF that was not fused (and vice versa maybe?)...

Not sure if it is possible to do the fusions at graph compile time instead of baking them into the GGUFs?

@ngxson

ngxson commented Feb 26, 2026

Copy link
Copy Markdown
Collaborator

Not sure if it is possible to do the fusions at graph compile time instead of baking them into the GGUFs?

I think it will quite tricky doing that way. It will require dequantizing FFN tensors in order to merge them.

Maybe we can add a pre-check in llama-quant in this case? And ideally find a way to merge imatrix data to match the new tensor layout? (Idk, just speculating)

JoursBleu added a commit to JoursBleu/llama.cpp that referenced this pull request Mar 4, 2026
Following the official gate_up fusion pattern from PR ggml-org#19139:
- Add create_tensor_qkv helper in llama-model.cpp that tries merged
  ATTN_QKV tensor first, falls back to separate Q/K/V
- Graph code (llama.cpp, qwen2.cpp) branches on wqkv: single matmul
  then view_2d split with bias addition and reshape_3d
- Python converter: --fuse-qkv flag concatenates Q/K/V weights into
  QKV tensor during HF-to-GGUF conversion
- Biases (bq/bk/bv) are added after splitting for models with
  attention biases (e.g., Qwen2)
- Non-fused path remains unchanged for backward compatibility

Benchmark results on Qwen2-0.5B (AMD R9700, gfx1201):
  Q4_0:   pp512 +9.1%, tg128 +2.7%
  Q4_K_M: pp512 +5.3%, tg128 +3.1%
  F16:    pp512 +5.8%, tg128 -2.2% (small model overhead)
JoursBleu added a commit to JoursBleu/llama.cpp that referenced this pull request Mar 5, 2026
Following the official gate_up fusion pattern from PR ggml-org#19139:
- Add create_tensor_qkv helper in llama-model.cpp that tries merged
  ATTN_QKV tensor first, falls back to separate Q/K/V
- Graph code (llama.cpp, qwen2.cpp) branches on wqkv: single matmul
  then view_2d split with bias addition and reshape_3d
- Python converter: --fuse-qkv flag concatenates Q/K/V weights into
  QKV tensor during HF-to-GGUF conversion
- Biases (bq/bk/bv) are added after splitting for models with
  attention biases (e.g., Qwen2)
- Non-fused path remains unchanged for backward compatibility

Benchmark results on Qwen2-0.5B (AMD R9700, gfx1201):
  Q4_0:   pp512 +9.1%, tg128 +2.7%
  Q4_K_M: pp512 +5.3%, tg128 +3.1%
  F16:    pp512 +5.8%, tg128 -2.2% (small model overhead)
JoursBleu added a commit to JoursBleu/llama.cpp that referenced this pull request Mar 5, 2026
Following the official gate_up fusion pattern from PR ggml-org#19139:
- Add create_tensor_qkv helper in llama-model.cpp that tries merged
  ATTN_QKV tensor first, falls back to separate Q/K/V
- Graph code (llama.cpp, qwen2.cpp) branches on wqkv: single matmul
  then view_2d split with bias addition and reshape_3d
- Python converter: --fuse-qkv flag concatenates Q/K/V weights into
  QKV tensor during HF-to-GGUF conversion
- Biases (bq/bk/bv) are added after splitting for models with
  attention biases (e.g., Qwen2)
- Non-fused path remains unchanged for backward compatibility

Benchmark results on Qwen2-0.5B (AMD R9700, gfx1201):
  Q4_0:   pp512 +9.1%, tg128 +2.7%
  Q4_K_M: pp512 +5.3%, tg128 +3.1%
  F16:    pp512 +5.8%, tg128 -2.2% (small model overhead)
JoursBleu added a commit to JoursBleu/llama.cpp that referenced this pull request Mar 5, 2026
Following the official gate_up fusion pattern from PR ggml-org#19139:
- Add create_tensor_qkv helper in llama-model.cpp that tries merged
  ATTN_QKV tensor first, falls back to separate Q/K/V
- Graph code (llama.cpp, qwen2.cpp) branches on wqkv: single matmul
  then view_2d split with bias addition and reshape_3d
- Python converter: --fuse-qkv flag concatenates Q/K/V weights into
  QKV tensor during HF-to-GGUF conversion
- Biases (bq/bk/bv) are added after splitting for models with
  attention biases (e.g., Qwen2)
- Non-fused path remains unchanged for backward compatibility

Benchmark results on Qwen2-0.5B (AMD R9700, gfx1201):
  Q4_0:   pp512 +9.1%, tg128 +2.7%
  Q4_K_M: pp512 +5.3%, tg128 +3.1%
  F16:    pp512 +5.8%, tg128 -2.2% (small model overhead)
JoursBleu added a commit to JoursBleu/llama.cpp that referenced this pull request Mar 5, 2026
Following the official gate_up fusion pattern from PR ggml-org#19139:
- Add create_tensor_qkv helper in llama-model.cpp that tries merged
  ATTN_QKV tensor first, falls back to separate Q/K/V
- Graph code (llama.cpp, qwen2.cpp) branches on wqkv: single matmul
  then view_2d split with bias addition and reshape_3d
- Python converter: --fuse-qkv flag concatenates Q/K/V weights into
  QKV tensor during HF-to-GGUF conversion
- Biases (bq/bk/bv) are added after splitting for models with
  attention biases (e.g., Qwen2)
- Non-fused path remains unchanged for backward compatibility

Benchmark results on Qwen2-0.5B (AMD R9700, gfx1201):
  Q4_0:   pp512 +9.1%, tg128 +2.7%
  Q4_K_M: pp512 +5.3%, tg128 +3.1%
  F16:    pp512 +5.8%, tg128 -2.2% (small model overhead)
@ubergarm

Copy link
Copy Markdown
Contributor

And ideally find a way to merge imatrix data to match the new tensor layout?

fortunately, ik was accommodating and resolved the discrepancies introduced with this PR so we again have parity in the worlds

thanks and happy weekend!

Seunghhon pushed a commit to Seunghhon/llama.cpp that referenced this pull request Apr 26, 2026
* llama: Add option to merge gate and exp weights

* Update convert_hf_to_gguf.py

Co-authored-by: Sigbjørn Skjæret <[email protected]>

* Update convert_hf_to_gguf.py

Co-authored-by: Sigbjørn Skjæret <[email protected]>

* update constants.py

* add gate_up for the all MoE models

* convert: simplify merge tensor condition

* update constants.py

* reduce number of models, add create_tensor_gate_up helper

---------

Co-authored-by: Sigbjørn Skjæret <[email protected]>
my-other-github-account pushed a commit to my-other-github-account/llama.cpp that referenced this pull request May 15, 2026
* llama: Add option to merge gate and exp weights

* Update convert_hf_to_gguf.py

Co-authored-by: Sigbjørn Skjæret <[email protected]>

* Update convert_hf_to_gguf.py

Co-authored-by: Sigbjørn Skjæret <[email protected]>

* update constants.py

* add gate_up for the all MoE models

* convert: simplify merge tensor condition

* update constants.py

* reduce number of models, add create_tensor_gate_up helper

---------

Co-authored-by: Sigbjørn Skjæret <[email protected]>
fukuro-kun pushed a commit to fukuro-kun/fukuro-llama-cpp-turboquant that referenced this pull request Jul 5, 2026
* llama: Add option to merge gate and exp weights

* Update convert_hf_to_gguf.py

Co-authored-by: Sigbjørn Skjæret <[email protected]>

* Update convert_hf_to_gguf.py

Co-authored-by: Sigbjørn Skjæret <[email protected]>

* update constants.py

* add gate_up for the all MoE models

* convert: simplify merge tensor condition

* update constants.py

* reduce number of models, add create_tensor_gate_up helper

---------

Co-authored-by: Sigbjørn Skjæret <[email protected]>
MrLordCat referenced this pull request in MrLordCat/llama.cpp-rdna-lab Jul 16, 2026
* llama: Add option to merge gate and exp weights

* Update convert_hf_to_gguf.py

Co-authored-by: Sigbjørn Skjæret <[email protected]>

* Update convert_hf_to_gguf.py

Co-authored-by: Sigbjørn Skjæret <[email protected]>

* update constants.py

* add gate_up for the all MoE models

* convert: simplify merge tensor condition

* update constants.py

* reduce number of models, add create_tensor_gate_up helper

---------

Co-authored-by: Sigbjørn Skjæret <[email protected]>
kainlan added a commit to kainlan/llama.cpp-intel-optimizations that referenced this pull request Aug 1, 2026
test-llama-archs aborted on `qwen3next | B70 | MoE`:

    [MOE-ROUTE] unresolved planner expert tensor=blk.0.ffn_gate_up_exps.weight
    expert=0 requested_layout=0 planned_layout=0 planned_device=-2
    planned_on_device=0 current_device=0 plan_missing=1 reason=not_found
    tier=unavailable

qwen3next is the first arch in llm_arch enum order that uses the FUSED
gate+up expert tensor `blk.N.ffn_gate_up_exps.weight` instead of the split
ffn_gate_exps / ffn_up_exps / ffn_down_exps trio. Three name-based
classifiers in the SYCL backend matched only the split trio:

  * infer_tensor_usage() (common.hpp) returned UNKNOWN, so the planner took
    the "dense weight, expert_id = -1" branch and never built per-expert
    placement entries at all.
  * expert_tensor_role_from_tensor_name() (unified-cache.hpp) returned
    UNKNOWN, and build_index() drops UNKNOWN roles from
    expert_placement_index_.
  * tensor_to_placement_priority() fell through to MOE_GATE_PROJ ("least
    impactful"), so the fused tensor would be demoted to host first under
    VRAM pressure.

The runtime still routes the tensor as MoE -- MUL_MAT_ID src0 is structural,
not name-based -- so planner and runtime disagreed and the route resolver
aborted with plan_missing=1.

Fix: add an explicit expert_tensor_role::GATE_UP rather than aliasing it
onto GATE. The gate<->up pairing logic (mxfp4_moe_gateup_role,
maybe_upgrade_moe_gate_up_layouts_to_i8, moe_triplet_complete) looks for a
matching partner entry that a fused tensor does not have, so those sites
exclude GATE_UP instead of letting it stand in as half a pair.

NOT A REGRESSION. This defect is ~76 days old and predates every commit
anyone would think to bisect (dates from `git log -S`):

  2026-01-07  5d01cfd  infer_tensor_usage lands with the 3-literal MoE
                         test; never updated since.
  2026-02-26  b68d751  upstream adds the fused ffn_gate_up_exps tensor
                         (ggml-org#19139). The SYCL classifier predates it by ~7
                         weeks and was never taught the name.
  2026-05-15  3c8f296  route.plan_missing AND the "unresolved planner
                         expert" abort are added in the same commit -- the
                         earliest date this can present as an abort.
  2026-07-30  39f07bd  merely made the row REACHABLE.

A naive bisect lands on 39f07bd, which only revealed it. There is no
regression window to find.

Same wall blocks qwen35moe, cohere2moe, deepseek2, deepseek2ocr and gemma4,
which take the fused tensor via create_tensor_gate_up_exps().

The abort message now names the likely cause and points at both classifiers,
so the next unrecognised expert-tensor name is a short diagnosis instead of
an hours-long one. Deliberately kept as an abort rather than softened to a
host fallback: a silent placement loss on an expert tensor is a
wrong-results bug with no signal, and this abort is the only reason the bug
was found.

Split-trio archs are unaffected by construction: every added name literal
can only match a fused name, the widened enum is only ever indexed with
bounds checks, and summarize_expert_placements() still derives 3 roles per
layer for them (it no longer hardcodes 3, which reported a spurious
`missing` count for fused archs).

Regression guard added to tests/test-sycl-layout-choice.cpp, covering both
classifier walls and a full build_index() -> lookup_expert_placement(name,
expert) round-trip -- the exact call the route resolver makes -- plus the
2-roles-per-layer expected/missing accounting. No GPU required.

That test's CMake registration is MOVED out of the GGML_SYCL_BUILD_XMX_TESTS
guard, which defaults OFF and so never built it (llama.cpp-5exz). It follows
the pattern of test-sycl-reset-model-weight-lease-preserve: a link-only
-fsycl options set, because XMX_TEST_SYCL_OPTIONS is defined inside the guard
and unavailable outside it. Its LABELS also drop the stale "cache" label --
CLAUDE.md's throttled sweep runs -LE 'residency|mem-handle|cache', so keeping
it would have left the test guarded out of the build and then filtered out of
the run. ENVIRONMENT pins level_zero:1 so it cannot perturb a B70 benchmark.

tests/test-layout-cache.cpp is deliberately NOT touched: it has no CMake
registration anywhere (not merely a guarded one), and unlike layout-choice it
requires a real device and defaults to level_zero:0, the benchmark card.
Registering it belongs to llama.cpp-5exz, not here.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01EHYpYxXNwtkx2t8dQJNumx
zommiommy pushed a commit to zommiommy/llama.cpp that referenced this pull request Aug 18, 2026
* llama: Add option to merge gate and exp weights

* Update convert_hf_to_gguf.py

Co-authored-by: Sigbjørn Skjæret <[email protected]>

* Update convert_hf_to_gguf.py

Co-authored-by: Sigbjørn Skjæret <[email protected]>

* update constants.py

* add gate_up for the all MoE models

* convert: simplify merge tensor condition

* update constants.py

* reduce number of models, add create_tensor_gate_up helper

---------

Co-authored-by: Sigbjørn Skjæret <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

model Model specific python python script changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants