Skip to content

Fix loading of standalone Qwen3.5 MTP companion GGUFs - #2327

Closed
Thireus wants to merge 349 commits into
ikawrakow:mainfrom
Thireus:th/fix-qwen35-mtp-companion
Closed

Thireus wants to merge 349 commits into
ikawrakow:mainfrom
Thireus:th/fix-qwen35-mtp-companion

Conversation

@Thireus

@Thireus Thireus commented Aug 16, 2026

Copy link
Copy Markdown
Contributor

Problem

Qwen3.5 (qwen35 / qwen35moe) supports MTP speculative decoding, but only when the
NextN block is embedded in the same GGUF as the model. If the MTP block is shipped as a
separate predictor-only companion file and passed with -md, loading fails immediately:

$ llama-server -m Qwen3.8-27B.gguf -md mtp-Qwen3.8-27B.gguf --spec-type mtp
...
llama_model_load: error loading model: check_tensor_dims: tensor 'blk.0.attn_norm.weight' not found
llama_model_load_from_file: failed to load model
common_speculative_load_draft_model: failed to load draft model 'mtp-Qwen3.8-27B.gguf'

A companion declares the full block count (n_main + nextn_predict_layers, e.g. 65) but
only contains the NextN block (blk.64.*) plus the embeddings and output head. The main
blocks live in the target GGUF loaded with -m. create_qwen35_tensors() nevertheless
creates every block blk.0 … blk.63 as hard-required, so the loader stops on the first
missing tensor.

Step-3.5 and DeepSeek-V4 already handle this case (mtp_only in create_step35_tensors(),
is_standalone_mtp in create_deepseek4_tensors()). Qwen3.5 had no equivalent, and the
surrounding MTP plumbing did not recognise a Qwen3.5 companion either.

Fix

  • src/llama-load-tensors.cpp — detect a companion (nextn_predict_layers > 0 and
    blk.0.attn_norm.weight absent from the GGUF) and mark the absent main blocks
    TENSOR_SKIP | TENSOR_NOT_REQUIRED, mirroring the existing Step-3.5 idiom. Applied to
    both create_qwen35_tensors() and create_qwen35moe_tensors(). Also guards the
    "9B shares q_proj" fixup (a companion has no trunk layer to borrow wq from), and skips
    empty blocks in the split-mode-graph loop, where split_recurrent_tensors() would
    otherwise assert on the missing ssm_in/wqkv.

  • src/llama-model.cpp — add llama_model_is_qwen35_family() and let Qwen3.5 use the
    common MTP package contract, so a companion is classified COMPANION and a plain target
    as TARGET_ONLY. The latter is what makes llama_context_has_mtp_outputs() true for the
    target, so it exports the hidden states the companion consumes. enorm/hnorm are also
    accepted as a predictor tail, since a Qwen3.5 NextN block can be built without eh_proj.

  • src/llama-hparams.cpp — map qwen35moe n_layer 49 and 61 to 122B-A10B and
    397B-A17B, so companions of those sizes report their model type instead of ?B
    (the dense switch already had the corresponding +1 cases).

  • common/speculative.cpp — recognise the Qwen3.5 companion as an external MTP
    provider, validate that its architecture matches the target and that it has exactly one
    predictor layer, keep the existing "embedded tail + ordinary draft model" combination
    working, and force pooling_type = NONE on the companion context (an MTP graph never
    appends a pooling layer).

  • common/common.cpp — reject a companion passed as -m with an actionable message
    instead of aborting inside the graph builder.

Behaviour for Step-3.5, DeepSeek-V4, Gemma 4 and embedded-MTP Qwen3.5 is unchanged.

Companion layouts covered

Checked against the published Qwen3.5/3.6/3.8 companion tensor maps. All eleven follow one
of three layouts, and all three are handled:

Layout Sizes Tensors
dense, tied embeddings 0.8B, 2B, 4B 17 — no output.weight, falls back to token_embd
dense 9B, 27B (3.5/3.6/3.8) 18
MoE 35B-A3B, 122B-A10B, 397B-A17B, 3.6-35B-A3B 23 — routed + shared experts

Every one of them carries its own blk.<mtp>.attn_q.weight, so no companion depends on the
target's q_proj. output_extra.weight is absent throughout, so output_mtp falls back to
output as intended.

Testing

Verified against real GGUFs — Qwen3.5-0.8B and Qwen3.8-27B at IQ1_S_R4, each with its
matching companion. Both reproduce the reported check_tensor_dims failure before the patch
and load and generate after it. The 0.8B pair also covers the tied-embedding layout, since
its companion ships no output.weight.

Additionally checked with synthetic target/companion pairs reproducing each of the three
layouts above (4 main blocks + 1 NextN block, mixed full-attention and gated-delta-net
layers), which allows testing the qwen35moe path and a companion built without eh_proj.

Regression matrix, before vs. after:

Configuration Before After
plain target, no speculation ok ok
dense target + companion --spec-type mtp check_tensor_dims failure ok
dense target + tied companion (no output.weight) check_tensor_dims failure ok
MoE target + MoE companion --spec-type mtp check_tensor_dims failure ok
dense target + companion without eh_proj check_tensor_dims failure ok
MoE target, no speculation ok ok
embedded MTP, no speculation ok ok
embedded MTP, --spec-type mtp, no -md ok ok
embedded MTP without eh_proj, --spec-type mtp ok ok
embedded MTP + -md draft, --spec-type draft ok ok
target + -md draft, --spec-type draft ok ok
plain target --spec-type mtp, no -md error error (clearer: "provide a matching predictor-only companion with -md")
companion passed as -m check_tensor_dims failure clean error pointing at -md

Thireus and others added 24 commits July 27, 2026 20:03
A predictor-only MTP companion for qwen35/qwen35moe declares the full
block count (n_main + nextn_predict_layers) but ships only the NextN
block. Loading one with -md aborted at

  check_tensor_dims: tensor 'blk.0.attn_norm.weight' not found

because create_qwen35{,moe}_tensors() creates every main block as
hard-required. Step-3.5 and DeepSeek-V4 already have an "mtp_only"
path for this; Qwen3.5 did not, and the surrounding MTP plumbing did
not recognise a Qwen3.5 companion either.

- llama-load-tensors: detect the companion (nextn > 0 and blk.0
  attn_norm absent) and mark the absent main blocks
  TENSOR_SKIP|TENSOR_NOT_REQUIRED, mirroring create_step35_tensors().
  Guard the "9B shares q_proj" fixup, which has no trunk to borrow
  from, and skip empty blocks in the split-mode-graph loop, where
  split_recurrent_tensors() would assert on the missing ssm_in/wqkv.
- llama-model: add llama_model_is_qwen35_family() and let Qwen3.5 use
  the common MTP package contract, so a companion classifies as
  COMPANION and a plain target as TARGET_ONLY (which is what makes the
  target export the hidden states the companion consumes). Also accept
  enorm/hnorm as a predictor tail, since 9B/4B NextN blocks can be
  built without eh_proj.
- llama-hparams: map qwen35moe n_layer 49 and 61 to 122B-A10B and
  397B-A17B, so a companion of those sizes no longer prints "?B".
- speculative: recognise the Qwen3.5 companion, validate its arch and
  head count, keep the embedded-tail + ordinary-draft combination
  working, and force pooling_type = NONE on the companion context.
- common: reject a companion passed as -m with an actionable message
  instead of aborting inside the graph builder.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@Thireus Thireus closed this Aug 16, 2026
@Thireus
Thireus deleted the th/fix-qwen35-mtp-companion branch August 16, 2026 00:30
Support standalone Qwen3.5 MTP companion GGUFs (-md ... --spec-type mtp)
@Thireus Thireus reopened this Aug 16, 2026
@Thireus Thireus closed this Aug 16, 2026
@Thireus

Thireus commented Aug 16, 2026

Copy link
Copy Markdown
Contributor Author

PR moved there: #2328
Renaming the branch while this PR was open led to unintended outcomes.

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.

1 participant