Repository navigation
llama: Add option to merge gate and exp weights - #19139
Conversation
Ah, should probably also go through those to ensure they are all generators and yielding from base then. |
c846c0b to
93c09b0
Compare
|
Still, I think there are some problems with the conversion script:
|
|
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. |
You must update all arches in |
I don't understand what you mean. Please assume I have no experience with conversion. |
Sorry, I meant |
|
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 |
|
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
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 |
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
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:
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 |
Otherwise, another way to do is to abstract the loading of FFN into a new function, |
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.
Re the |
|
And btw this pattern already exists in |
A Edit: Either way, leave it for a follow-up. |
What I mean was that when writing the tensor loading code, the 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: Instead, 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 |
|
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 |
|
I think the first step could be to abstract out a 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. |
|
For my understanding, the python changes are good, correct? |
|
@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 |
Okay I will refactor to create a function
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 |
|
Ok got it. The gpt-oss needs some extra work since it utilizes the The |
| 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); |
There was a problem hiding this comment.
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
| il); | ||
| il, | ||
| nullptr, | ||
| model.layers[il].ffn_gate_up_exps); |
There was a problem hiding this comment.
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
|
Ok I will merge this, the one issue I see is that |
|
People are asking me already if I used this new My concern is if someone generates a bf16 GGUF using 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 |
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)
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)
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)
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)
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)
fortunately, ik was accommodating and resolved the discrepancies introduced with this PR so we again have parity in the worlds thanks and happy weekend! |
* 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]>
* 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]>
* 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]>
* 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]>
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
* 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]>
Continuing on #18740 and #18866, add option
--fuse_gate_up_expstoconvert_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_tensorsfor 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
this PR (with the fused GGUF)