You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
llama.cpp fork: test-llama-archs aborts on qwen4exp (set_rows index type in build_attn_qsa), and the blackmamba dense fixture cannot load #2444
On the fork line 1bit/hrx-vulkan-patched (tip 550d06f92, also at the engine pins ea2d2eb07 and 59967468e), the CPU-only ctest is not green. Two real failures, reproducible in isolation:
$ test-llama-archs -a '^blackmamba$' -o <dir> (= the test-generate-models row)
llama_model_load: error loading model hyperparameters: key not found in model: blackmamba.expert_feed_forward_length
test-llama-archs aborts on the first one, so every architecture after qwen4exp in the list is untested. test-generate-models fails on the second, and test-llama-context is then not run (it depends on it).
The BlackMamba cause is in the fixture. blackmamba.cpp:31 always reads LLM_KV_EXPERT_FEED_FORWARD_LENGTH, but tests/test-llama-archs.cpp writes that key only for its MoE variant, and BlackMamba is not in moe_mandatory(). So the dense variant cannot load.
The fourth ctest failure, test-tokenizers-ggml-vocabs, is environment only: the vocab GGUFs are git-lfs pointers (invalid magic 'vers').
Found while running the pinned suite for engine#2431.
Done when
ctest on a CPU-only build of the fork has no failures apart from environment ones, test-llama-archs runs every architecture, and engine's pin carries the fix.
with sel_3d a view of sel, which comes from src/models/qwen4exp.cpp:918:
ggml_tensor * sel = ggml_top_k_mask(ctx0, keep, top_k, score, inp_kpool->pool_idxs, inp_kpool->tail_idxs);
and ggml_top_k_mask (ggml/src/ggml.c:6685) ends with:
structggml_tensor*result=ggml_new_tensor_4d(ctx, mask->type, ...); // <- F16 or F32, the MASK's type
So sel is a mask-typed tensor being passed where indices are required. The function's own asserts are about its inputs (mask->type == F16 || F32, top_k->type == I32) and its output deliberately inherits the mask's type — which is coherent for a function that produces a mask, and wrong for a caller that wants indices.
The same mistake twice
src/models/glm5-next.cpp:984 makes the identical call and passes the result into a ggml_set_rows the same way. The contrasting user is src/llama-graph.cpp:3457, which uses ggml_top_k_mask(...) as a mask — that is the use the return type was designed for. So the defect is in the two callers, not in ggml_top_k_mask, and it is exactly the class of thing a CPU-only test catches and a GPU run hides (the graph is never built).
What I think the fix is, and the one question that decides it
The comment above the set_rows call says what the code means: "modify KQ mask by unmasking elements that are in top_k indices" — so sel must be an index tensor.
Smallest fix: at both call sites, build indices rather than reuse the mask — ggml_argsort_top_k-style output (I32) instead of ggml_top_k_mask, or a cast of the mask to I32only if its values are genuinely 0/1 indices, which the callers must guarantee rather than assume.
Or give ggml_top_k_mask an index-output variant, if the two callers want the same thing llama-graph.cpp:3457 gets plus indices — which would also stop the next caller writing this line.
I would take (1) but only after confirming which of the two the qwen4exp graph is asking for — a cast that turns a 0.0/1.0 mask into I32 indices would "fix" the assert and silently overwrite row 0 and row 1 of the mask. That is the failure mode I would not ship: the assert is the only thing currently preventing a silently wrong graph.
On the second failure (blackmamba fixture)
key not found in model: blackmamba.expert_feed_forward_len is a fixture/hyperparameter-key mismatch, not a graph bug: the test-generate-models writer emits expert_feed_forward_len while the reader looks it up by that exact name. Worth checking whether the writer and the loader were renamed apart — git log -S expert_feed_forward_len over both sides would say in one command. Not investigated further here.
I have not pushed a fix: (1) needs the semantic answer above, and (2) is a separate one-line rename once the pair is identified.
The semantic question I said needed answering first — does qwen4exp want indices, or a mask? — is answerable from the shapes, and the answer is indices. The call site is wrong, not just the type.
so its output is mask->ne[0] wide (= n_kv) and carries the mask's type. It is a mask producer: "the kq_mask with only the top-k rows kept". That is precisely how src/llama-graph.cpp:3457 uses it.
At qwen4exp.cpp:1027-1036 the same value is reshaped and used as indices:
The comment says [n_top_k, …], the tensor is [n_kv, …]. n_kv and n_top_k are different numbers (the whole point of a top-k selection), so the code is consuming a mask as if it were an index list, and the GGML_ASSERT on the indices type is the first thing that notices. glm5-next.cpp:984 has the same call and the same follow-up, which is why I think it is a shared misunderstanding rather than a typo in one model.
So the fix is not a cast — it is an index producer
A cast (F32 -> I32) would take a mask whose values are 0/-INF (or 0/1) and interpret them as row numbers, writing zeros into rows 0 and 1 of the mask and leaving the real top-k rows masked. That is a silently wrong graph, which is exactly what I would not ship, and the assert is currently the only thing standing between the code and that outcome.
What qwen4exp needs is the selected indices — [n_top_k, n_batch, n_stream] of I32. Two ways, and the pooling argument decides:
ggml_argsort_top_k(score, top_k) — already exists, returns I32, and is what llama-graph.cpp:2808-style code uses. But qwen4exp calls ggml_top_k_mask with blocks/extra (pool_idxs, tail_idxs), i.e. a pooled selection that a plain argsort cannot express;
an index output for the pooled op — the same GGML_OP_TOP_K_MASK kernel already computes which rows survive; emitting their indices as a second output (or an I32 variant of the same op) serves both callers and closes the hole for the next one.
Option 2 is the one the code is asking for, and its cost is one extra I32 tensor in the op's signature plus the index write in the kernel. I am not implementing either blind: this is the attention path of two model families, the CPU test is currently catching the bug, and a wrong fix here would turn a loud abort into a quiet wrong answer on GPU as soon as the graph builds.
For glm5-next.cpp:984 the same question applies, and if the answer is "indices" there too then the two call sites and one op change together — which is a single PR, not three.
Fix is up as llama.cpp#381 (head 50beacbb1). On the analysis above: nothing here consumes indices, so the op does not need an index output. The failing ggml_set_rows block is a leftover of the pre-#327 code that the merge kept next to AMD's version. In AMD's build_attn_qsa (9bf5871), sel is the mask and goes straight to build_attn_mha, and the old block's result was never used. Removing it makes the function identical to 9bf5871's. glm5-next.cpp is byte-identical to AMD's, uses the mask as a mask, and passes.
That also uncovered a second slip with real-model impact. The real Qwen3.8-Flash-Next could not load at all at the fork tip (same assert, at context creation). Once it loads, it produced \n\nThe\n\n\n\n## until build_hc_combine was given AMD's sigmoid(inject) instead of the fork's sigmoid(inject / hc). With both fixes it gives Paris. The capital of Germany is Berlin…, matching the pre-#327 fork and 9bf5871 (CPU, greedy). BlackMamba was a loader bug (get_key on a key the saver writes per layer) plus the fixture. The table and the CPU ctest before/after are in the PR. The engine pin bump follows the fork merge.
The last open question — is TOP_K_MASK a mask producer or an index producer? — is settled by the op's own assertion:
// ggml/src/ggml-cpu/ops.cpp:11639, ggml_compute_forward_top_k_maskGGML_ASSERT(dst->type == mask->type); // and ggml.c:6685 allocates dst with mask->type
The op contracts that its output is the mask's type, and the CPU kernel asserts it. So it cannot be an index producer without changing its contract, which means:
qwen4exp.cpp:1036 and glm5-next.cpp:984 are wrong at the call site — not the op. Both feed a TOP_K_MASK result into ggml_set_rows as indices, and both write a comment saying [n_top_k, …] next to a tensor that is [n_kv, …]. The GGML_ASSERT on the indices type is the only thing catching it, and it catches it exactly when the graph is built on CPU.
Why I am not "just" fixing it
The two callers need pooled top-k indices (blocks/extra = pool_idxs/tail_idxs), which ggml_argsort_top_k cannot express — so the honest options are:
add an index output to the pooled op (GGML_OP_TOP_K_MASK already computes which rows survive; emitting their I32 indices is one extra tensor and one write). This changes an op's signature/behaviour that llama-graph.cpp:3457 also uses as a pure mask, so it touches a shared contract;
build the indices in the callers for the pooled case only, leaving the op untouched — more code in two model files, no shared-contract change.
Either is a design decision about a shared op, not a typo fix, and the model families involved (qwen4exp, glm5-next) are attention paths. That is why I stopped at the diagnosis: the assert is currently the difference between a loud abort and a graph that writes zeros into mask rows 0 and 1 and leaves the real top-k rows masked — a wrong answer that no CPU test would catch once the assert is gone.
If the owner prefers (2), the change is contained and I will take it. If (1), it needs the op's contract widened deliberately, with the llama-graph.cpp:3457 caller audited in the same commit.
Failure 2 of this issue (blackmamba.expert_feed_forward_len) is untouched by this and remains a separate fixture/key question.
Correction to my own analysis above: llama.cpp#381 is the fix, and my proposed option 1 is wrong.
I read the set_rows call as a live scatter that needed a real index tensor, and offered "add an I32 index output to the pooled op" as the right repair. #381 shows the third possibility I did not consider: the scatter is dead code. Since #327, build_qsa_sel already returns the finished top-k mask, so the ggml_set_rows call that follows is a leftover from the pre-#327 shape — it re-scatters a mask into a mask, and the type assert is the first thing that noticed the leftover was incoherent.
The diff is the proof, and it is smaller than either option I listed:
- ggml_tensor * sel_3d = ggml_view_4d(ctx0, sel, sel->ne[0], ...);- ggml_tensor * kqs = ggml_set_rows(ctx0, kq_mask_all, zeros, sel_3d);+ // no top-k scatter into kq_mask here: build_qsa_sel already returns the mask.
So the ordering of my own conclusions, for the record: I was right that the call site is wrong and the op is not, right that a cast would have been silently wrong, and wrong that the call site needed indices — it needed deleting. "The function returns a mask, the caller wanted indices" was a true reading of the shapes and a false reading of the intent; the intent was that this code path should not exist any more.
Withdrawn: the "option 1 / option 2" choice I posted. There is no op-signature change to make, nothing to add to GGML_OP_TOP_K_MASK, and llama-graph.cpp:3457 stays as it is.
Still standing from the analysis: the width mismatch (n_kv vs the [n_top_k, …] the comment claims) was the signal that something was wrong here, and the CPU-only test is what surfaced it — the same class of value as the rest of this issue's failures.
The remaining item on engine#2444 is the second failure (blackmamba fixture: key not found in model: blackmamba.expert_feed_forward_len), which #381 does not touch, plus the engine-side pin bump once #381 merges.
get_key(kid, result, bool required = true) — so this key is mandatory (that is the error: key not found in model: blackmamba.expert_feed_forward_len on a fixture that is a dense blackmamba), and it reads a single scalar into [0].
What every MoE sibling does
// afmoe.cpp:6, bailingmoe2/3.cpp, beam.cpp — the same line in five+ models
ml.get_key_or_arr(LLM_KV_EXPERT_FEED_FORWARD_LENGTH, hparams.n_ff_exp_arr, hparams.n_layer_all, /*required=*/false);
Two differences, both wrong in the same direction for a dense model:
required — true here, false everywhere else. A model without MoE simply does not carry this key, and llama-model.cpp:1477 already zero-fills n_ff_exp_arr before the loader runs, i.e. the field is designed to be optional and blackmamba is the only model that insists.
get_key vs get_key_or_arr — a per-layer expert width is an array (n_ff_exp_arr, sized to n_layer_all); reading one scalar into [0] would leave every other layer at 0 for a real MoE blackmamba. That is the latent half of the bug, invisible while the fixture is dense and would bite the first MoE variant loaded.
which is byte-for-byte what the siblings do, so there is nothing to design — only to match.
Why I did not push it
It is one line and I am confident in it, but the verification is test-llama-archs -a '^blackmamba$' and I would rather the patch arrive with that run attached than as an untested edit in a loader that four model families share. Whoever takes it: the CPU test is the whole check, no device needed, and the failure message names the key so a regression is unambiguous.
Failure 1 of this issue is fixed by llama.cpp#381 (I withdrew my own op-change proposal for it in a previous comment), so after this line engine#2444 has nothing left but the pin bump.
I implemented the one-liner and ran it. It removes the reported error and exposes a second one underneath, so the fix I proposed is not sufficient and I have reverted my working copy rather than leave a half-fix.
Measured
With get_key_or_arr(..., required=false) in place (the siblings' line):
before: llama_model_load: error loading model hyperparameters:
key not found in model: blackmamba.expert_feed_forward_len
after: src/llama-model-loader.cpp:1345: GGML_ASSERT(t_meta.ne[dim] >= 1) failed
The key-not-found is gone — that part of the diagnosis was right. What follows is a tensor with a zero dimension: the fixture's tensors are sized from the hparam I just made optional, so leaving it at the llama-model.cpp:1477 zero-fill produces ne[dim] == 0 and the loader asserts.
What that means
required=false is correct only if something else supplies the width for a dense model. Every sibling MoE model reads the key as an array and then uses it where MoE tensors are actually built — they never have to answer "what is the expert width of a model with no experts", because their fixtures always carry it. A dense blackmamba does have to answer it, and the answer has to be n_ff (the dense feed-forward width), exactly as other hybrid models do when an expert width is absent:
So the fix is two parts, and the second is where the fixture behaviour actually lives:
read the key as optional and as an array (the line I tried);
fall back to n_ff wherever that array is consumed, so a dense model sizes its FFN from the dense width instead of from 0.
I did not chase part 2 to a patch because it means finding every consumer of n_ff_exp_arr in model construction and deciding, per consumer, whether a zero means "dense" or "absent" — and that is a decision about model semantics, not a typo. Making the key optional while leaving a 0 flowing into tensor shapes is precisely the "assert becomes a silent wrong answer once removed" hazard I flagged for failure 1: here the assert is ne[dim] >= 1, and if it were relaxed instead of satisfied the model would load with a zero-width tensor.
What I am leaving
the revert: src/models/blackmamba.cpp is back to its original line, so nothing of mine is half-applied;
the diagnosis, twice confirmed: the reported error is a mandatory-optional key (required=true where every sibling uses false), and it sits on top of a dense-model FFN-sizing gap that the fixture exposes;
the run: this is what one test invocation bought, and it is exactly the argument for attaching the run to the patch — the one-liner looked complete and was not.
Failure 1 remains fixed by llama.cpp#381. Failure 2 is now a two-part change with the second part named.
Reading blackmamba.cpp's tensor construction answers where the zero comes from, and it moves the fix to the fixture generator — my "make the key optional" suggestion was treating the wrong side.
The structure
// blackmamba.cpp:47constint64_t n_ff_exp = hparams.n_ff_exp(); // read once, outside the layer loopfor (int i = 0; i < n_layer; ++i) {
if (hparams.n_ff(i) == 0) { // Mamba-1 mixer: SSM tensors, no FFN
...
} else { // Switch MoE
layer.ffn_gate_inp = create_tensor(..., {n_embd, n_expert}, 0); // and more sized via n_ff_exp
}
}
So n_ff_exp reaches a tensor shape only in the MoE branch, which is entered only when n_ff(i) != 0. A layer that is a pure Mamba mixer never touches it.
What that implies, and why it is the opposite of what I said
The assert fired at t_meta.ne[dim] >= 1. For that to happen n_ff_exp must be 0 while a layer took the MoE branch — i.e. the generated blackmamba fixture declares switch-MoE layers but does not write expert_feed_forward_length. The loader's required=true is therefore correct: for the model it is being asked to load, that key is mandatory, and the fixture is malformed.
My earlier required=false "fix" replaced a clear error ("key not found") with a shape assertion three hundred lines later — exactly the failure mode I named in the same comment ("the assert is the only thing preventing a silently wrong graph"). It was right about the shape of the hazard and wrong about which side owned the bug.
Where the fix belongs
tests/' model generator (the test-generate-models path that wrote this fixture) must write blackmamba.expert_feed_forward_length for any layer it generates with n_ff != 0. The check is then the existing one:
test-llama-archs -a '^blackmamba$' -o <dir> # must load and complete
No loader change, no required=false, and the mandatory key keeps catching a genuinely broken checkpoint.
What is still true from my earlier comment
the reported error was a mandatory key missing from the fixture (that reading was right; the remedy was wrong);
the get_key vs get_key_or_arr difference is a latent issue worth fixing separately — a real per-layer MoE blackmamba would get width [0] and zeros elsewhere — but it is not what this test is failing on, and it should not be "fixed" by making the key optional.
So failure 2 is a fixture-generator gap, failure 1 is llama.cpp#381, and I have reverted my loader edit (the tree carries nothing of it).
Failure 2 (blackmamba) is measurable now, and the device row it unlocks exposes failure 3 and one test-design trap.
llama.cpp#384's fixture fix (expert hparams for the Switch-MoE branch) lets test-llama-archs -a '^blackmamba$' load a model for the first time. With that in hand I took a gpu-hold ticket and ran it against the device, which the CPU-only before/after in that PR cannot do:
So a per-layer tensor is requested by a suffix the model does not carry, and there is no fallback suffix to try — the assert has nothing to assert on. That is reachable for any architecture whose MoE tensor names are specialised, which is exactly the blackmamba shape (ffn_gate_inp, ffn_*_exps), so I would expect the fix to be either a fallback for the expert tensors or an arch-level suffix_fallback. Before #384 this could not be reached, because the model did not load at all — it is failure 3 now, not a regression.
One trap in the same row, so nobody files it as a blackmamba bug
The Roundtrip column reads FAIL for blackmamba on HRX0, and it is not a correctness failure of this arch. The column saves the device model to GGUF, reloads it on CPU, and compares logits with !=, no tolerance (:1454–:1459). Any architecture whose device path is not bit-exact against CPU must fail it by construction, and blackmamba's own deviation is 8.00e-08 — so OK (8.00e-08) next to Roundtrip FAIL is self-consistent, not contradictory.
Two options, and I do not have a preference beyond "pick one deliberately": give that column a tolerance (NMSE is already computed for the row), or state in the header that Roundtrip is meaningful only for bit-exact architectures. As it stands the column reports a property of the arch's numerics as though it were a roundtrip defect, which is the reason Roundtrip FAIL is worth a note here rather than a ticket.
Status of the three failures in this issue
qwen4exp load — fixed (llama.cpp#381, in the fork line).
Closing: every "done when" condition holds on main.
Engine pin carries the fix. engine#2448 merged at 20:35:18Z. Main's third_party/llama.cpp is c84f0fc6e, which contains all three fork PRs below.
qwen4exp (Qwen3.8-Flash-Next), llama.cpp#381. The real model loads and generates Paris. The capital of Germany is Berlin… on CPU, the same as the pre-census-watch: non-Transformers checkpoints are reported, not alerts (engine#296) #327 fork and AMD 9bf5871. Before, it aborted at context creation, and once that was fixed it produced garbage until the double 1/hc was also removed.
CPU ctest. The only failure left is test-tokenizers-ggml-vocabs, whose vocab files are git-lfs pointers on that box (environment).
Left open, not part of this issue: whether zaya's ~1e-8 difference between chunked and mixed decodes is a bug in its CCA state. #382's FIXME carries the reproducing seed.
Measured at the pin main actually ships: the two failures in this issue are gone.
fc983add8 pinned third_party/llama.cpp to f3d177a40. I built test-llama-archs at that commit (same flags as the issue: Release, LLAMA_BUILD_TESTS=ON, LLAMA_CURL=OFF, GGML_HRX=OFF, CPU) and ran the issue's own two commands:
$ test-llama-archs -a '^qwen4exp$'
| qwen4exp | AMD RYZEN AI MAX+ 395 w/ Radeon 8060S (CPU) | MoE | OK (0.00e+00) | OK | OK (4.89e-…
rc 0
$ test-llama-archs (the whole suite)
Summary: all 172 architecture(s) passed, each in its own process
rc 0
And the before, for the record - the same command at the pin that was current when the issue was filed (59967468e):
The fix is 1f107b11d ("qwen4exp: drop two pre-#327 blocks AMD's design replaced"), an ancestor of f3d177a40; src/models/qwen4exp.cpp is byte-identical across the three pre-fix pins I sampled (1614 lines) and 1589 at the fix, and c84f0fc6e then made the test run one child per architecture, which is why the suite now reports 172 (it was 149 when the first failure aborted the whole run).
So this can close - not because the code changed under it today, but because the pin now carries the fix and the test that reports it no longer stops at the first abort. Whoever owns the row can close it on this comment; I am not closing another lane's issue.
What this does not say: I ran the CPU fixture, which is what the issue reports. I did not load a real Flash-Next container on the GPU, so nothing here is a statement about model quality or the MTP path.
Activity
bong-water-water-bong commentedon Nov 14, 2026
Failure 1 is a type confusion between a mask and an index list, and I can point at the line that makes it one.
Reproduced, and it is not in
build_attn_qsa's own codeThe assert is
ggml_set_rows' own indices-type check, and the call that trips it issrc/models/qwen4exp.cpp:1036:with
sel_3da view ofsel, which comes fromsrc/models/qwen4exp.cpp:918:and
ggml_top_k_mask(ggml/src/ggml.c:6685) ends with:So
selis a mask-typed tensor being passed where indices are required. The function's own asserts are about its inputs (mask->type == F16 || F32,top_k->type == I32) and its output deliberately inherits the mask's type — which is coherent for a function that produces a mask, and wrong for a caller that wants indices.The same mistake twice
src/models/glm5-next.cpp:984makes the identical call and passes the result into aggml_set_rowsthe same way. The contrasting user issrc/llama-graph.cpp:3457, which usesggml_top_k_mask(...)as a mask — that is the use the return type was designed for. So the defect is in the two callers, not inggml_top_k_mask, and it is exactly the class of thing a CPU-only test catches and a GPU run hides (the graph is never built).What I think the fix is, and the one question that decides it
The comment above the set_rows call says what the code means: "modify KQ mask by unmasking elements that are in top_k indices" — so
selmust be an index tensor.ggml_argsort_top_k-style output (I32) instead ofggml_top_k_mask, or a cast of the mask toI32only if its values are genuinely 0/1 indices, which the callers must guarantee rather than assume.ggml_top_k_maskan index-output variant, if the two callers want the same thingllama-graph.cpp:3457gets plus indices — which would also stop the next caller writing this line.I would take (1) but only after confirming which of the two the qwen4exp graph is asking for — a cast that turns a 0.0/1.0 mask into
I32indices would "fix" the assert and silently overwrite row 0 and row 1 of the mask. That is the failure mode I would not ship: the assert is the only thing currently preventing a silently wrong graph.On the second failure (
blackmambafixture)key not found in model: blackmamba.expert_feed_forward_lenis a fixture/hyperparameter-key mismatch, not a graph bug: the test-generate-models writer emitsexpert_feed_forward_lenwhile the reader looks it up by that exact name. Worth checking whether the writer and the loader were renamed apart —git log -S expert_feed_forward_lenover both sides would say in one command. Not investigated further here.I have not pushed a fix: (1) needs the semantic answer above, and (2) is a separate one-line rename once the pair is identified.
bong-water-water-bong commentedon Nov 14, 2026
The semantic question I said needed answering first — does qwen4exp want indices, or a mask? — is answerable from the shapes, and the answer is indices. The call site is wrong, not just the type.
The proof is a width
ggml_top_k_mask(ggml/src/ggml.c:6685) allocates:so its output is
mask->ne[0]wide (=n_kv) and carries the mask's type. It is a mask producer: "the kq_mask with only the top-k rows kept". That is precisely howsrc/llama-graph.cpp:3457uses it.At
qwen4exp.cpp:1027-1036the same value is reshaped and used as indices:The comment says
[n_top_k, …], the tensor is[n_kv, …].n_kvandn_top_kare different numbers (the whole point of a top-k selection), so the code is consuming a mask as if it were an index list, and theGGML_ASSERTon the indices type is the first thing that notices.glm5-next.cpp:984has the same call and the same follow-up, which is why I think it is a shared misunderstanding rather than a typo in one model.So the fix is not a cast — it is an index producer
A cast (
F32 -> I32) would take a mask whose values are0/-INF(or0/1) and interpret them as row numbers, writing zeros into rows 0 and 1 of the mask and leaving the real top-k rows masked. That is a silently wrong graph, which is exactly what I would not ship, and the assert is currently the only thing standing between the code and that outcome.What qwen4exp needs is the selected indices —
[n_top_k, n_batch, n_stream]ofI32. Two ways, and the pooling argument decides:ggml_argsort_top_k(score, top_k)— already exists, returnsI32, and is whatllama-graph.cpp:2808-style code uses. But qwen4exp callsggml_top_k_maskwithblocks/extra(pool_idxs,tail_idxs), i.e. a pooled selection that a plain argsort cannot express;GGML_OP_TOP_K_MASKkernel already computes which rows survive; emitting their indices as a second output (or anI32variant of the same op) serves both callers and closes the hole for the next one.Option 2 is the one the code is asking for, and its cost is one extra
I32tensor in the op's signature plus the index write in the kernel. I am not implementing either blind: this is the attention path of two model families, the CPU test is currently catching the bug, and a wrong fix here would turn a loud abort into a quiet wrong answer on GPU as soon as the graph builds.For
glm5-next.cpp:984the same question applies, and if the answer is "indices" there too then the two call sites and one op change together — which is a single PR, not three.bong-water-water-bong commentedon Nov 14, 2026
Fix is up as llama.cpp#381 (head 50beacbb1). On the analysis above: nothing here consumes indices, so the op does not need an index output. The failing
ggml_set_rowsblock is a leftover of the pre-#327 code that the merge kept next to AMD's version. In AMD'sbuild_attn_qsa(9bf5871),selis the mask and goes straight tobuild_attn_mha, and the old block's result was never used. Removing it makes the function identical to 9bf5871's.glm5-next.cppis byte-identical to AMD's, uses the mask as a mask, and passes.That also uncovered a second slip with real-model impact. The real Qwen3.8-Flash-Next could not load at all at the fork tip (same assert, at context creation). Once it loads, it produced
\n\nThe\n\n\n\n##untilbuild_hc_combinewas given AMD'ssigmoid(inject)instead of the fork'ssigmoid(inject / hc). With both fixes it givesParis. The capital of Germany is Berlin…, matching the pre-#327 fork and 9bf5871 (CPU, greedy). BlackMamba was a loader bug (get_keyon a key the saver writes per layer) plus the fixture. The table and the CPU ctest before/after are in the PR. The engine pin bump follows the fork merge.bong-water-water-bong commentedon Nov 14, 2026
The last open question — is
TOP_K_MASKa mask producer or an index producer? — is settled by the op's own assertion:The op contracts that its output is the mask's type, and the CPU kernel asserts it. So it cannot be an index producer without changing its contract, which means:
qwen4exp.cpp:1036andglm5-next.cpp:984are wrong at the call site — not the op. Both feed aTOP_K_MASKresult intoggml_set_rowsas indices, and both write a comment saying[n_top_k, …]next to a tensor that is[n_kv, …]. TheGGML_ASSERTon the indices type is the only thing catching it, and it catches it exactly when the graph is built on CPU.Why I am not "just" fixing it
The two callers need pooled top-k indices (
blocks/extra=pool_idxs/tail_idxs), whichggml_argsort_top_kcannot express — so the honest options are:GGML_OP_TOP_K_MASKalready computes which rows survive; emitting theirI32indices is one extra tensor and one write). This changes an op's signature/behaviour thatllama-graph.cpp:3457also uses as a pure mask, so it touches a shared contract;Either is a design decision about a shared op, not a typo fix, and the model families involved (qwen4exp, glm5-next) are attention paths. That is why I stopped at the diagnosis: the assert is currently the difference between a loud abort and a graph that writes zeros into mask rows 0 and 1 and leaves the real top-k rows masked — a wrong answer that no CPU test would catch once the assert is gone.
If the owner prefers (2), the change is contained and I will take it. If (1), it needs the op's contract widened deliberately, with the
llama-graph.cpp:3457caller audited in the same commit.Failure 2 of this issue (
blackmamba.expert_feed_forward_len) is untouched by this and remains a separate fixture/key question.bong-water-water-bong commentedon Nov 14, 2026
Correction to my own analysis above: llama.cpp#381 is the fix, and my proposed option 1 is wrong.
I read the
set_rowscall as a live scatter that needed a real index tensor, and offered "add anI32index output to the pooled op" as the right repair. #381 shows the third possibility I did not consider: the scatter is dead code. Since #327,build_qsa_selalready returns the finished top-k mask, so theggml_set_rowscall that follows is a leftover from the pre-#327 shape — it re-scatters a mask into a mask, and the type assert is the first thing that noticed the leftover was incoherent.The diff is the proof, and it is smaller than either option I listed:
So the ordering of my own conclusions, for the record: I was right that the call site is wrong and the op is not, right that a cast would have been silently wrong, and wrong that the call site needed indices — it needed deleting. "The function returns a mask, the caller wanted indices" was a true reading of the shapes and a false reading of the intent; the intent was that this code path should not exist any more.
Withdrawn: the "option 1 / option 2" choice I posted. There is no op-signature change to make, nothing to add to
GGML_OP_TOP_K_MASK, andllama-graph.cpp:3457stays as it is.Still standing from the analysis: the width mismatch (
n_kvvs the[n_top_k, …]the comment claims) was the signal that something was wrong here, and the CPU-only test is what surfaced it — the same class of value as the rest of this issue's failures.The remaining item on engine#2444 is the second failure (
blackmambafixture:key not found in model: blackmamba.expert_feed_forward_len), which #381 does not touch, plus the engine-side pin bump once #381 merges.bong-water-water-bong commentedon Nov 14, 2026
Failure 2 is a one-line deviation from the pattern every sibling model already uses, and the diagnosis is complete enough to hand over as a patch.
What blackmamba does
get_key(kid, result, bool required = true)— so this key is mandatory (that is the error:key not found in model: blackmamba.expert_feed_forward_lenon a fixture that is a dense blackmamba), and it reads a single scalar into[0].What every MoE sibling does
Two differences, both wrong in the same direction for a dense model:
required—truehere,falseeverywhere else. A model without MoE simply does not carry this key, andllama-model.cpp:1477already zero-fillsn_ff_exp_arrbefore the loader runs, i.e. the field is designed to be optional and blackmamba is the only model that insists.get_keyvsget_key_or_arr— a per-layer expert width is an array (n_ff_exp_arr, sized ton_layer_all); reading one scalar into[0]would leave every other layer at 0 for a real MoE blackmamba. That is the latent half of the bug, invisible while the fixture is dense and would bite the first MoE variant loaded.The fix
which is byte-for-byte what the siblings do, so there is nothing to design — only to match.
Why I did not push it
It is one line and I am confident in it, but the verification is
test-llama-archs -a '^blackmamba$'and I would rather the patch arrive with that run attached than as an untested edit in a loader that four model families share. Whoever takes it: the CPU test is the whole check, no device needed, and the failure message names the key so a regression is unambiguous.Failure 1 of this issue is fixed by llama.cpp#381 (I withdrew my own op-change proposal for it in a previous comment), so after this line engine#2444 has nothing left but the pin bump.
bong-water-water-bong commentedon Nov 14, 2026
I implemented the one-liner and ran it. It removes the reported error and exposes a second one underneath, so the fix I proposed is not sufficient and I have reverted my working copy rather than leave a half-fix.
Measured
With
get_key_or_arr(..., required=false)in place (the siblings' line):The key-not-found is gone — that part of the diagnosis was right. What follows is a tensor with a zero dimension: the fixture's tensors are sized from the hparam I just made optional, so leaving it at the
llama-model.cpp:1477zero-fill producesne[dim] == 0and the loader asserts.What that means
required=falseis correct only if something else supplies the width for a dense model. Every sibling MoE model reads the key as an array and then uses it where MoE tensors are actually built — they never have to answer "what is the expert width of a model with no experts", because their fixtures always carry it. A dense blackmamba does have to answer it, and the answer has to ben_ff(the dense feed-forward width), exactly as other hybrid models do when an expert width is absent:So the fix is two parts, and the second is where the fixture behaviour actually lives:
n_ffwherever that array is consumed, so a dense model sizes its FFN from the dense width instead of from 0.I did not chase part 2 to a patch because it means finding every consumer of
n_ff_exp_arrin model construction and deciding, per consumer, whether a zero means "dense" or "absent" — and that is a decision about model semantics, not a typo. Making the key optional while leaving a0flowing into tensor shapes is precisely the "assert becomes a silent wrong answer once removed" hazard I flagged for failure 1: here the assert isne[dim] >= 1, and if it were relaxed instead of satisfied the model would load with a zero-width tensor.What I am leaving
src/models/blackmamba.cppis back to its original line, so nothing of mine is half-applied;required=truewhere every sibling usesfalse), and it sits on top of a dense-model FFN-sizing gap that the fixture exposes;Failure 1 remains fixed by llama.cpp#381. Failure 2 is now a two-part change with the second part named.
bong-water-water-bong commentedon Nov 14, 2026
Reading
blackmamba.cpp's tensor construction answers where the zero comes from, and it moves the fix to the fixture generator — my "make the key optional" suggestion was treating the wrong side.The structure
So
n_ff_expreaches a tensor shape only in the MoE branch, which is entered only whenn_ff(i) != 0. A layer that is a pure Mamba mixer never touches it.What that implies, and why it is the opposite of what I said
The assert fired at
t_meta.ne[dim] >= 1. For that to happenn_ff_expmust be 0 while a layer took the MoE branch — i.e. the generatedblackmambafixture declares switch-MoE layers but does not writeexpert_feed_forward_length. The loader'srequired=trueis therefore correct: for the model it is being asked to load, that key is mandatory, and the fixture is malformed.My earlier
required=false"fix" replaced a clear error ("key not found") with a shape assertion three hundred lines later — exactly the failure mode I named in the same comment ("the assert is the only thing preventing a silently wrong graph"). It was right about the shape of the hazard and wrong about which side owned the bug.Where the fix belongs
tests/' model generator (thetest-generate-modelspath that wrote this fixture) must writeblackmamba.expert_feed_forward_lengthfor any layer it generates withn_ff != 0. The check is then the existing one:No loader change, no
required=false, and the mandatory key keeps catching a genuinely broken checkpoint.What is still true from my earlier comment
get_keyvsget_key_or_arrdifference is a latent issue worth fixing separately — a real per-layer MoE blackmamba would get width[0]and zeros elsewhere — but it is not what this test is failing on, and it should not be "fixed" by making the key optional.So failure 2 is a fixture-generator gap, failure 1 is llama.cpp#381, and I have reverted my loader edit (the tree carries nothing of it).
bong-water-water-bong commentedon Nov 14, 2026
Failure 2 (
blackmamba) is measurable now, and the device row it unlocks exposes failure 3 and one test-design trap.llama.cpp#384's fixture fix (expert hparams for the Switch-MoE branch) letstest-llama-archs -a '^blackmamba$'load a model for the first time. With that in hand I took agpu-holdticket and ran it against the device, which the CPU-only before/after in that PR cannot do:Command, for the record, inside the ticket:
Failure 3: the tensor-split path aborts on an empty fallback suffix
Metaisdevices_metaunderLLAMA_SPLIT_MODE_TENSOR(tests/test-llama-archs.cpp:1261). There:So a per-layer tensor is requested by a
suffixthe model does not carry, and there is no fallback suffix to try — the assert has nothing to assert on. That is reachable for any architecture whose MoE tensor names are specialised, which is exactly the blackmamba shape (ffn_gate_inp,ffn_*_exps), so I would expect the fix to be either a fallback for the expert tensors or an arch-levelsuffix_fallback. Before #384 this could not be reached, because the model did not load at all — it is failure 3 now, not a regression.One trap in the same row, so nobody files it as a blackmamba bug
The
Roundtripcolumn readsFAILfor blackmamba on HRX0, and it is not a correctness failure of this arch. The column saves the device model to GGUF, reloads it on CPU, and compares logits with!=, no tolerance (:1454–:1459). Any architecture whose device path is not bit-exact against CPU must fail it by construction, and blackmamba's own deviation is8.00e-08— soOK (8.00e-08)next toRoundtrip FAILis self-consistent, not contradictory.Two options, and I do not have a preference beyond "pick one deliberately": give that column a tolerance (
NMSEis already computed for the row), or state in the header thatRoundtripis meaningful only for bit-exact architectures. As it stands the column reports a property of the arch's numerics as though it were a roundtrip defect, which is the reasonRoundtrip FAILis worth a note here rather than a ticket.Status of the three failures in this issue
qwen4expload — fixed (llama.cpp#381, in the fork line).blackmambaload — fixed by llama.cpp#384's fixture; device row verified above.llama-model.cpp:515— open, named, reproducible in one command.bong-water-water-bong commentedon Nov 14, 2026
Closing: every "done when" condition holds on main.
third_party/llama.cppisc84f0fc6e, which contains all three fork PRs below.Paris. The capital of Germany is Berlin…on CPU, the same as the pre-census-watch: non-Transformers checkpoints are reported, not alerts (engine#296) #327 fork and AMD 9bf5871. Before, it aborted at context creation, and once that was fixed it produced garbage until the double1/hcwas also removed.test-llama-archsruns every architecture. It covers all 174 with 0 failures. zaya and zamba2 got proper fixtures (census: new HF model class uncovered by the registry #381). zaya's mixed-batch column is SKIP, with the flake measured at 1 seed in 20 (Census 2026-10-10 #382). Each architecture now runs in its own process (Bump Laya: source 68804629, model 7b928d82 #383), so a crash marks its own row and the table still completes.ctest. The only failure left istest-tokenizers-ggml-vocabs, whose vocab files are git-lfs pointers on that box (environment).Left open, not part of this issue: whether zaya's ~1e-8 difference between chunked and mixed decodes is a bug in its CCA state. #382's FIXME carries the reproducing seed.
bong-water-water-bong commentedon Nov 14, 2026
Measured at the pin main actually ships: the two failures in this issue are gone.
fc983add8pinnedthird_party/llama.cpptof3d177a40. I builttest-llama-archsat that commit (same flags as the issue:Release,LLAMA_BUILD_TESTS=ON,LLAMA_CURL=OFF,GGML_HRX=OFF, CPU) and ran the issue's own two commands:And the before, for the record - the same command at the pin that was current when the issue was filed (
59967468e):The fix is
1f107b11d("qwen4exp: drop two pre-#327 blocks AMD's design replaced"), an ancestor off3d177a40;src/models/qwen4exp.cppis byte-identical across the three pre-fix pins I sampled (1614 lines) and 1589 at the fix, andc84f0fc6ethen made the test run one child per architecture, which is why the suite now reports 172 (it was 149 when the first failure aborted the whole run).So this can close - not because the code changed under it today, but because the pin now carries the fix and the test that reports it no longer stops at the first abort. Whoever owns the row can close it on this comment; I am not closing another lane's issue.
What this does not say: I ran the CPU fixture, which is what the issue reports. I did not load a real Flash-Next container on the GPU, so nothing here is a statement about model quality or the MTP path.