Skip to content

qwen4exp : give drafters the mean of the hyper-connection streams as layer input - #307

Open
danielhanchen wants to merge 1 commit into
base/upstream-99db719bdfrom
fix/qwen4exp-dflash-hc-mean
Open

danielhanchen wants to merge 1 commit into
base/upstream-99db719bdfrom
fix/qwen4exp-dflash-hc-mean

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Fixes unslothai/unsloth#14381 (upstream report: ggml-org#30887).

Problem

Qwen3.8-Flash-Next (qwen4exp) with a DFlash drafter (for example incoai/Qwen3.8-Flash-Next-DFlash2-GGUF) loads fine, then the server aborts on the first prompt:

src/llama-context.cpp:2456: GGML_ASSERT(row_floats == model.hparams.n_embd) failed

DFlash and EAGLE3 turn on layer-input extraction for their target layers and read one n_embd row per token. qwen4exp keeps its residual as 4 hyper-connection streams and set res->t_layer_inp[il] = res_hc, the whole [n_embd, hc, T] tensor. That gives extract_layer_inputs hc * n_embd floats per token, and the assert fires. The drafter's model card says it was trained on the mean of the four streams, which llama.cpp did not compute.

Change

For layers a drafter requested, t_layer_inp[il] is now the mean over the hc streams. This is the same view + add + scale that deepseek4 already uses (build_hc_mean). Nothing is built when no layer input is requested, so the graph without a drafter is unchanged. One file, +10 / -1.

Based on upstream b11724 (the base of b11724-mix-c208e5f, the build in the report). The bug is still present on ggml-org master.

Evidence (CPU, tiny random models from test-llama-archs -o)

The harness loads the model, enables llama_set_embeddings_layer_inp for a layer, decodes 16 tokens, and compares every extracted row against an independent reference: the previous layer's l_last output, captured with cb_eval and averaged over the streams on the host.

arch upstream b11724 this PR
qwen4exp (hc=4, n_embd 256), layer 1 abort: llama-context.cpp:2409: GGML_ASSERT(row_floats == model.hparams.n_embd) failed rows [256, 16], max abs error vs stream mean 2.98e-08
deepseek4 (control, already averages), layers 1-3 max err <= 5.96e-08 identical
qwen35moe (control, no hc), layer 1 max err 0 identical
  • Same harness with extraction off: last-token logits are bit-identical between b11724 and this PR (FNV 5aeeaf45b3859deb), and turning extraction on leaves the logits unchanged.
  • test-llama-archs -a qwen4exp: CPU NMSE 0, roundtrip OK, mixed batch OK on both arms.
  • The patch applies cleanly to the merged b11724 pin tree, which is where the reporter's line 2456 assert sits.

Not run here: the full 125B target with the real drafter (82 GB+), so this PR does not measure acceptance rate. HIP / CUDA / Vulkan builds were not run either. The ops are the ones deepseek4 already runs on every backend.

…layer input

DFlash and EAGLE3 read one n_embd row per token from the target's layer
inputs. qwen4exp handed them the whole [n_embd, hc, T] residual, so
extract_layer_inputs saw hc*n_embd floats per row and aborted on
GGML_ASSERT(row_floats == model.hparams.n_embd) on the first prompt.

Build the mean over the hc streams only for the layers a drafter asked
for, as deepseek4 already does. The graph is unchanged when no layer
input is requested.
@danielhanchen
danielhanchen changed the base branch from master to base/upstream-99db719bd October 21, 2026 09:14
@danielhanchen
danielhanchen requested a review from CISC as a code owner October 21, 2026 09:14
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 21, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-21T09:31:41.355129Z 520c6b2 Manual request
🔒 Security Review ✅ Completed 2026-10-21T09:21:22.338813Z 520c6b2 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 520c6b28b6

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/models/qwen4exp.cpp
Comment on lines +451 to +456
if ((size_t) il < cparams.embeddings_layer_inp.size() && cparams.embeddings_layer_inp[il]) {
ggml_tensor * mean = ggml_view_2d(ctx0, res_hc, n_embd, res_hc->ne[2], res_hc->nb[2], 0);
for (int64_t s = 1; s < hc; ++s) {
mean = ggml_add(ctx0, mean, ggml_view_2d(ctx0, res_hc, n_embd, res_hc->ne[2], res_hc->nb[2], s*res_hc->nb[1]));
}
res->t_layer_inp[il] = ggml_scale(ctx0, mean, 1.0f/hc);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Add regression coverage for Qwen4exp layer extraction

This fix changes tensor shape and contents only when embeddings_layer_inp[il] is enabled, but no automated test enables that path for Qwen4exp; the existing test-llama-archs -a qwen4exp coverage therefore exercises only the unchanged graph and would still pass if this block again returned hc*n_embd values per token or computed the wrong reduction. Add a committed test that enables extraction and verifies an n_embd-wide stream mean; CONTRIBUTING.md:35 explicitly requires a regression test for bug fixes.

AGENTS.md reference: AGENTS.md:L18-L20

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Keeping the pin to the one-line graph change; test-llama-archs has no layer-input mode and adding one is a harness change beyond this fix. The extraction is checked against the host-side stream mean in the harness quoted in the description, and the pin is recorded under unchecked in feature-checks.json (#308).

Comment thread src/models/qwen4exp.cpp

for (int il = 0; il < n_layer; ++il) {
res->t_layer_inp[il] = res_hc;
// drafters (DFlash, EAGLE3) read one n_embd row per token: the mean of the hc streams, as in deepseek4

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Replace the AI-authored commit metadata

The reviewed commit identifies its author as Codex <[email protected]> and includes a generated long-form PR-style commit message, but this repository explicitly prohibits AI-written commit messages and automated commits and treats them as grounds for immediate closure. Have the human contributor author the commit metadata and message rather than submitting this commit as-is.

AGENTS.md reference: AGENTS.md:L48-L52

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Not the case: the commit is authored and committed by Daniel Han [email protected] (see the commit API for 520c6b2), and the message is a normal subject plus short body.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Can't wait for the next one!

Reviewed commit: 520c6b28b6

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

DFlash2 crashes on first prompt with Qwen3.8-Flash-Next in Unsloth build

1 participant