Repository navigation
feat(gguf): auto-detect + export Qwen3.5/3.8 MTP self-speculative head from GGUF - #529
Conversation
…terleave)
Two independent GGUF→config bugs surfaced while exporting the hybrid
Qwen3.8-27B (Gated DeltaNet + GQA) model:
1. MTP/nextn block-count. GGUF's `block_count` includes the trailing
Multi-Token-Prediction ("nextn") block(s), so `num_hidden_layers`
was one too high. The base decode model does not build the MTP head
and its weights are skipped during tensor mapping, leaving an extra
decoder layer whose linear-attention/GQA initializers have no backing
GGUF weights and tripping the `_check_weights` save invariant. Subtract
`<arch>.nextn_predict_layers` from the decoder layer count.
2. rope_interleave from M-RoPE sections. `rope.dimension_sections`
encodes Qwen-VL M-RoPE *section* sizes (e.g. [11,11,10,0]); it does
NOT select GPT-J adjacent-pair rotation. Deriving `rope_interleave`
from section presence set `rotary_interleaved=1` on the exported
GQA/RotaryEmbedding, corrupting RoPE (Qwen uses split-half/NEOX).
Only `deepseek4` genuinely needs the flat interleave flag.
Adds regression tests for both.
Co-authored-by: Copilot <[email protected]>
… (partial RoPE) The RotaryAttentionToGQA rewrite fuses an external RotaryEmbedding + Attention into a GroupQueryAttention with do_rotary=1, but never copied the source RotaryEmbedding's `rotary_embedding_dim`. Partial-RoPE models (Qwen3.5/3.8 rotate only the first 64 of 256 head elements) therefore defaulted the fused GQA to the full head_dim, reading past the partial cos/sin cache and emitting garbage from every full-attention layer. Read `rotary_embedding_dim` off the q RotaryEmbedding node and forward it to the GQA attributes when nonzero (full-RoPE models omit it and keep the correct head_dim default). Adds regression tests for the partial and full cases. Co-authored-by: Copilot <[email protected]>
…oherent decode The GGUF import path for Qwen3.5/3.8 hybrid models (arch `qwen35`/`qwen35moe`) produced garbage / degenerate (repeating) decode because three llama.cpp converter transforms of the Gated-DeltaNet weights were not undone when mapping into mobius's `GatedDeltaNet`, whose forward expects raw HF-style parameters. All three are applied in `_normalize_gguf_weights`, arch-scoped and derived from config (no hardcoded head counts), so future DeltaNet variants keep working: 1. A_log double-exp: the converter stores the SSM decay pre-transformed as `ssm_a = -exp(A_log)`, but `GatedDeltaNet` recomputes `-exp(A_log)` at runtime, squashing every head's decay to ~-1. Recover the raw parameter via `A_log = log(-ssm_a)` (scoped to `linear_attn.A_log`). 2. Zero-centered RMSNorm +1: the converter bakes `+1` into every `*norm.weight` except `linear_attn.norm.weight`; mobius adds it back via `OffsetRMSNorm`, so subtract 1 to avoid double-counting (scoped to `_OFFSET_NORM_GGUF_ARCHS`). 3. Gated-DeltaNet V-head tiling: for grouped linear attention (`num_value_heads != num_key_heads`) the converter reorders every V-indexed `linear_attn` tensor from HF grouped order into ggml tiled order. mobius consumes grouped order, so `_reorder_deltanet_v_heads` undoes the tiling for `in_proj_qkv` (V rows), `in_proj_z`, `in_proj_a/b`, `A_log`, `dt_bias`, `conv1d` (V channels) and `out_proj` (quantized K-block columns). Handles MatMulNBits triplets (weight/scales/zero_points) losslessly. With all three, greedy decode of unsloth/Qwen3.8-27B-Q4_0 → int4 CUDA ONNX is coherent: "The capital of France is" → "Paris. The capital of Germany is Berlin. ...". Adds regression tests asserting `_reorder_deltanet_v_heads` is the exact inverse of the converter's `_reorder_v_heads`, plus A_log / norm-offset unit tests. Co-authored-by: Copilot <[email protected]>
The dense Qwen3.5/3.8 GGUF (e.g. unsloth/Qwen3.8-27B) ships a trained
multi-token-prediction ("nextn") head as the trailing blk.<N> block,
tagged by <arch>.nextn_predict_layers. gguf_to_config read that key only
to subtract the block from num_hidden_layers, so the text-only backbone
build silently dropped the head and the exported ONNX had no
self-speculative drafter.
Recover the head as a mtp/ sidecar:
- _config_mapping: surface _gguf_nextn_predict_layers / _gguf_mtp_block_indices.
- _mtp.py (new): map blk.<N>.nextn.* + attn/ffn to Qwen35MtpModel stems,
derive Qwen35MtpConfig (all dims inherited from the backbone; no hardcoding),
and build the head via the existing quantized/dequantized loaders through an
injectable name_mapper. The two pre_fc_norm_* OffsetRMSNorm +1 offsets missed
by the generic pass are stripped explicitly.
- _builder: expose hidden_states.<N-1> as the head seed and attach pkg.mtp_head.
- __main__: save pkg.mtp_head to output_dir/mtp/ and write speculator metadata.
- inference_metadata: write_mtp_speculator_metadata (proposal_type: mtp).
- _mtp_test.py (new): mapping, config derivation, and tiny-GGUF head emission.
Verified the real Qwen3.8-27B GGUF blk.64 head tensor shapes match the
metadata-derived head (doubled-Q gating: attn_q = 2*NH*HD rows; eh_proj = H x 2H).
Unblocks MTP self-speculative decode for Qwen3.8-27B (engine side: onnx-genai #1598).
Co-authored-by: Copilot <[email protected]>
Performance Comparison
|
The Qwen3.5/3.8 MTP self-speculative head is now gated behind an explicit opt-in flag instead of being emitted whenever the GGUF ships a nextn block. Default behavior is unchanged: a text-only export with no head sidecar, so existing exports are byte-for-byte identical. - build_from_gguf gains include_mtp: bool = False. Both the backbone hidden-state seed (output_layer_indices=[N-1], step 4b) and the head sidecar build (step 10) are gated on include_mtp AND has_mtp_head. When include_mtp=True but the GGUF has no nextn head, a warning is logged and the build proceeds text-only; when a head is present but the flag is off, an info log points at --include-mtp. Rejects include_mtp with static_cache or mmproj (ValueError). - CLI: add `--include-mtp` (store_true, off by default), threaded through _cmd_build_gguf; the mtp/ sidecar save + SpeculatorConfig metadata write already key off pkg.mtp_head, so they only fire when the flag is set. CLI-level SystemExit guards mirror the build_from_gguf conflicts. - Tests: gating coverage (default omits head; --include-mtp attaches it with mtp_hidden output; static_cache conflict raises; no-head GGUF warns). MTP dims are still derived entirely from GGUF/source metadata; the flag only gates whether the subgraph is emitted. One driver now serves both the text-only and MTP paths. Co-authored-by: Copilot <[email protected]>
🏗️ Architecture Diff
No architecture changes detected. ✅ Legend: ⚪ No change · 🔵 Minor (attrs/inits) · 🟡 Moderate (nodes added/removed) · 🔴 Major (interface changed) |
Supersedes the opt-in flag: the MTP self-speculative head is a purely additive sidecar (mtp/model.onnx + shared embedding/lm_head + SpeculatorConfig metadata) that existing consumers ignore, so there is no reason to gate it behind a user flag. Emit it iff the source GGUF actually ships the head. - Presence check (has_mtp_head): the config carries _gguf_mtp_block_indices, derived in gguf_to_config from <arch>.nextn_predict_layers > 0 (the trailing blk.<N>.nextn.* head block). When present, always emit the sidecar + the backbone hidden_states.<N-1> seed + SpeculatorConfig. When absent, skip — text-only sources produce byte-identical output to before. - Remove build_from_gguf's include_mtp parameter and the CLI --include-mtp flag plus its conflict guards; steps 4b/10 gate on has_mtp_head alone. static_cache still skips the head (its dynamic-cache requirement) via a logged info, keeping static exports unchanged. - Tests: replace flag-gating with auto-detect coverage (nextn GGUF auto-emits the head with mtp_hidden output; plain GGUF omits it). Dims remain fully derived from GGUF/source metadata (no hardcoding). Co-authored-by: Copilot <[email protected]>
…etadata across dtype replace
Two defects prevented the real int4/bf16 Qwen3.8-27B export from emitting a
usable MTP self-speculative sidecar:
1. Qwen35TextModel.forward ignored output_layer_indices, so the backbone never
produced the hidden_states.{N-1} seed the head consumes (it always returned a
2-tuple). Add the same capture path as base.py TextModel: return the selected
per-layer post-residual hidden states so HybridCausalLMTask emits
hidden_states.{idx}. Without this the speculator.target_hidden_output
referenced a non-existent output.
2. build_from_gguf lost the private _gguf_* MTP metadata whenever an explicit
--dtype (or quantization) triggered dataclasses.replace, which returns a
fresh config without the plain attributes gguf_to_config set. Auto-detection
(has_mtp_head) then saw nothing and silently skipped the head. Capture the
nextn metadata into locals right after gguf_to_config (like _gguf_model_type)
and re-attach it onto the final config before step 4b.
Also extend write_mtp_speculator_metadata with embedding_weights,
lm_head_weights, and vocab_size so the SpeculatorConfig block names the shared
tables and vocab the runtime needs.
Tests: regression for the dtype-replace path and an assertion that the backbone
exposes hidden_states.{N-1} when the source ships the nextn head.
Co-authored-by: Copilot <[email protected]>
…latorConfig schema
The emitted `speculator:` block used field names the onnx-genai runtime cannot
parse, so InferenceMetadata.speculative deserialized to None and the MTP sidecar
was silently ignored (text-only decode). Conform exactly to the authoritative
schema in onnx-genai crates/onnx-genai-metadata (schema/generation.rs
SpeculatorConfig, parser.rs resolve_mtp, config.rs validate_resolved_mtp_config):
- Publish under the top-level `speculative` key (was `speculator`, an unknown
key that is dropped).
- `model_path` -> `model`; `hidden_size` -> `target_hidden_size`.
- `kv_mode: hidden_threaded` (engine-internal enum) -> `proposal_local`
(the only valid MtpKvMode for this k=1 head).
- `embedding_weights`/`lm_head_weights` flat strings -> `embedding`/`lm_head`
MtpTargetInitializer objects {source: target_initializer, name: ...}.
- Add `target_hidden_layout: BSH` (rank-3 [batch,seq,hidden] seed) and
`hc_mult: 1` (resolve_mtp requires hc_mult>0; validate pins it to 1 for BSH).
- Omit `mtp_state_output` (hc_mult==1 head has no recurrent HC state; the
sidecar emits only mtp_hidden + present.0.{key,value}).
Test: assert the emitted YAML has the exact schema keys/values and validates
against onnx-genai's published inference_metadata.schema.json.
Co-authored-by: Copilot <[email protected]>
Resolves 15 conflict blocks across 8 files. Twelve were formatting-only: main had run ruff format over code this branch also touched (quoted vs bare type annotations, list concat vs unpacking, line wrapping, a docstring rewording). Took main's side so the formatter would not immediately undo the merge. One of those blocks also dropped `_index_dim1`, which was defined and never called. Three were genuine two-sided changes and both sides are kept: - `__main__.py`: this branch added the MTP sidecar save; main replaced the runtime dispatch with `write_gguf_runtime_package` (dual onnx-genai/ort-genai from #543). The code after the conflict already referenced both, so dropping either would not have compiled. - `_group_query_attention.py` / its test: main added sliding-window support (`local_window_size` from the attention bias) plus a `TestSlidingWindowSurvivesFusion` suite; this branch had only a reformat there. `inference_metadata_test.py` needed more than conflict resolution. The branch's copy predates main's removal of `add_explicit_package_io` and referenced it seven times, so taking the branch side broke collection for six modules. Took main's file and re-applied this branch's `TestMtpSpeculatorMetadata` block (with its `_MtpBackboneConfig` fixture and `_seed_backbone_metadata` helper), plus the `write_mtp_speculator_metadata` import. `_seed_backbone_metadata` seeded `{"model_type": "qwen3_5_text"}`, which the runtime schema rejects as an unknown top-level property — a schema failure for a reason unrelated to the speculator block under test. Seeded an empty document instead; the writer only appends `speculative`. Tests: gguf + rewrite_rules 475 passed / 1 skipped. In inference_metadata_test.py, 4 pre-existing failures remain (reproduced on origin/main using main's own test file) and `TestMtpSpeculatorMetadata`'s schema case still fails — see the PR discussion, it is a stale committed schema rather than a defect in this branch. Co-authored-by: Copilot <[email protected]> Copilot-Session: d60eb808-7cc6-4abc-b48d-2a6dd3841624
ReviewConflicts resolved and pushed as Conflict resolution15 blocks across 8 files. Twelve were formatting-only — main had run Three were genuine two-sided changes, both sides kept:
What I liked
Test results
Finding: the committed JSON schema is stale, not this branch
Also worth flagging for whoever owns the runtime side: the only place that parses this exact shape is a test helper named Merging on the strength of the above; the schema divergence needs a separate fix on the onnx-genai side. |
There was a problem hiding this comment.
Pull request overview
This PR restores and auto-detects export of the Qwen3.5/3.8 GGUF “nextn” Multi-Token Prediction (MTP) self-speculative head by emitting it as an additive mtp/ sidecar ONNX model and wiring corresponding onnx-genai speculative metadata into inference_metadata.yaml.
Changes:
- Add GGUF-side MTP head detection/mapping/build logic and attach the built MTP sidecar package during
build_from_gguf. - Extend Qwen3.5 text backbone to optionally capture intermediate hidden states so the backbone can expose
hidden_states.{N-1}as the seed for the MTP head. - Emit onnx-genai schema-conformant
speculativemetadata for MTP and add tests covering schema validation and GGUF auto-detection.
Reviewed changes
Copilot reviewed 10 out of 10 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| src/mobius/models/qwen35.py | Adds optional per-layer hidden-state capture support to seed the MTP head. |
| src/mobius/integrations/onnx_genai/inference_metadata.py | Adds write_mtp_speculator_metadata to append a schema-conformant speculative MTP block. |
| src/mobius/integrations/onnx_genai/inference_metadata_test.py | Adds tests asserting exact MTP speculative schema keys/values + JSON schema validation. |
| src/mobius/integrations/gguf/_mtp.py | New module that maps GGUF nextn tensors to the Qwen3.5/3.8 MTP head and builds the sidecar. |
| src/mobius/integrations/gguf/_mtp_test.py | New tests for mapping, config derivation, head emission, and presence-driven auto-detect. |
| src/mobius/integrations/gguf/_config_mapping.py | Surfaces _gguf_nextn_predict_layers / _gguf_mtp_block_indices on the final config instance. |
| src/mobius/integrations/gguf/_builder.py | Re-attaches _gguf_* attrs after dataclasses.replace, exposes seed hidden state, and builds/attaches pkg.mtp_head; adds loader name_mapper support. |
| src/mobius/main.py | Saves mtp/ sidecar when present and appends MTP speculative metadata for onnx-genai packages. |
| src/mobius/integrations/gguf/_tokenizer.py | Formatting-only change in tokenizer reconstruction. |
| src/mobius/integrations/gguf/_repacker_test.py | Formatting-only change in dequantization test. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| present_key_values: list = [] | ||
| past_kvs = past_key_values or [None] * len(self.layers) | ||
| capture_set = set(self.output_layer_indices or ()) | ||
| captured_by_index: dict[int, ir.Value] = {} | ||
| for layer_idx, (layer, past_kv) in enumerate(zip(self.layers, past_kvs)): |
| if self.output_layer_indices is not None: | ||
| ordered = [captured_by_index[idx] for idx in self.output_layer_indices] | ||
| return hidden_states, present_key_values, ordered | ||
| return hidden_states, present_key_values |
| output_dir, backbone_config=getattr(pkg, "config", None) | ||
| ) | ||
| if spec_path is not None: | ||
| print(f" speculator: {spec_path}") |
What
Recovers the Multi-Token-Prediction (MTP / "nextn") self-speculative head that the Qwen3.8-27B ONNX export was silently dropping, and emits it automatically whenever the source GGUF ships the head — no CLI flag, no config toggle. The MTP sidecar is a purely-additive artifact (
mtp/model.onnx+ shared embedding/lm_head +SpeculatorConfigmetadata) that existing consumers ignore, so it is driven entirely by source-tensor presence: emit MTP iff available.Auto-detect (presence-driven, no flag)
The decision is made by
has_mtp_head(config)— true when the config carries_gguf_mtp_block_indices, whichgguf_to_configpopulates from the source GGUF. Exact presence check:<arch>.nextn_predict_layers > 0(for Qwen3.8-27B:qwen35.nextn_predict_layers = 1,block_count = 65), which yields_gguf_mtp_block_indices = [decoder_layers .. decoder_layers + nextn - 1](here[64]), andblk.<N>.nextn.*tensors —nextn.eh_proj(thefc),nextn.enorm,nextn.hnorm,nextn.shared_head_norm— plus that block's own attention (attn_q/k/v/output,attn_q_norm/k_norm,attn_norm,post_attention_norm) and FFN (ffn_gate/up/down) sublayer.Behavior:
mtp/model.onnxsidecar + the backbonehidden_states.<N-1>seed output + aSpeculatorConfig(proposal_type: mtp) block ininference_metadata.yaml.--static-cacheskips the head (it needs the dynamic concat-grow cache), logged as info; those exports are unchanged.MTP dims are derived entirely from GGUF/source metadata (hidden size, heads, head_dim, KV heads, rope, quant) — no hardcoding.
Root cause
The dense Qwen3.5/3.8 GGUF (e.g.
unsloth/Qwen3.8-27B) ships a trained MTP head as the trailingblk.<N>block (for 27B:blk.64), tagged by<arch>.nextn_predict_layers.gguf_to_configread that key only to subtract the block fromnum_hidden_layers, and the text-only backbone build never made a second pass — so the head tensors were dropped.Changes
integrations/gguf/_config_mapping.py— surface_gguf_nextn_predict_layers/_gguf_mtp_block_indices(the trailing block previously only subtracted).integrations/gguf/_mtp.py(new) —has_mtp_head(presence check),map_gguf_mtp_to_hf_names(GGUFblk.<N>.nextn.*+ attn/ffn ->Qwen35MtpModelstems),derive_mtp_config(all dims inherited from the backbone config — no hardcoding),build_mtp_head_from_gguf(reusesQwen35MtpModel/Qwen35MtpTaskand the existing quantized/dequantized loaders via an injectablename_mapper;fcis quantized on the same main-layer path). The twopre_fc_norm_*OffsetRMSNorm+1offsets missed by the generic norm strip are handled explicitly.integrations/gguf/_builder.py— steps 4b (seed output) and 10 (attachpkg.mtp_head) gate onhas_mtp_head(config)alone (auto-detect);static_cacheskips the head via a logged info. The quantized/dequantized loaders gained optionalname_mapper/warn_unmappedparams (backward compatible).__main__.py— whenpkg.mtp_headis present, save it intooutput_dir/mtp/and write speculator metadata. (No new CLI flag.)integrations/onnx_genai/inference_metadata.py—write_mtp_speculator_metadata(proposal_type: mtp,target_hidden_output,mtp_hidden, single-layer KV).integrations/gguf/_mtp_test.py(new) — mapping, config derivation, tiny-GGUF head emission, and auto-detect (nextn GGUF auto-emits the head withmtp_hiddenoutput; plain GGUF omits it).Validation (partial — stated explicitly)
build_from_gguf(source with nextn ->pkg.mtp_headattached; source without -> absent). Emitted head initializers includefc.weight_t,pre_fc_norm_embedding.weight,pre_fc_norm_hidden.weight,norm.weight,layers.0.self_attn.q_proj.weight_t; graph outputmtp_hidden; every initializer has backing weights._config_mapping/_tensor_mappingsuites still pass (74 total).blk.64shape contract VERIFIED against the metadata-derived head — all 15 head tensors map, backbone excluded:attn_q= (12288, 5120) = (2NHHD, H) -> doubled-Q output gatingattn_k/attn_v= (1024, 5120) = (NKVHD, H);attn_output= (5120, 6144) = (H, NHHD)nextn.eh_proj= (5120, 10240) = (H, 2H) — thefcGEMM (Q8_0 while projections are Q4_0; mixed quant handled per-tensor)onnx_irlacksmax_shard_size_bytes, which also breaks the pre-existingtest_default_quantized_package_save_reload— unrelated to this change).Real artifact produced + two export-path fixes
Producing the real int4/bf16 27B artifact surfaced two defects that made the MTP sidecar silently degenerate. Both are now fixed (commit
987c8d04):Qwen35TextModel.forwardignoredoutput_layer_indices— it always returned a 2-tuple, so the backbone never emitted thehidden_states.{N-1}seed the head consumes;speculator.target_hidden_outputpointed at a non-existent output. Added the same capture path asbase.pyTextModel(returns the selected per-layer post-residual hidden states soHybridCausalLMTaskemitshidden_states.{idx}).build_from_ggufdropped the_gguf_*nextn metadata under--dtype— an explicit dtype (or quantization) triggersdataclasses.replace, which returns a fresh config without the plain_gguf_*attributes, sohas_mtp_headsaw nothing and skipped the head. Now captured into locals right aftergguf_to_config(like_gguf_model_type) and re-attached before step 4b. Regression test added (test_mtp_survives_dtype_replace).write_mtp_speculator_metadataalso now emitsembedding_weights,lm_head_weights, andvocab_size.Full export — VALIDATED end-to-end
Command:
model.onnx(2.2 MB) +model.onnx.data(17.4 GB),mtp/model.onnx(38 KB) +mtp/model.onnx.data(354 MB),inference_metadata.yaml,tokenizer.json. Total 17 GB.hidden_states.63=[batch, sequence_len, 5120]— exactlyspeculator.target_hidden_output.inputs_embeds,hidden_states,attention_mask,position_ids,past_key_values.0.{key,value}; outputsmtp_hidden+present.0.{key,value}(single full-attention layer KV).fc.weight_t= (10240, 5120); q/k/v/o + gate/up/down projections are int4MatMulNBits(UINT8 packed, block-32) following the main-layer path;eh_projstays bf16 (Q8_0 in source). Shared embedding/lm_head are reused from the main model (named in the speculator block), not duplicated.proposal_type: mtp,num_speculative_tokens: 1,model_path: mtp/model.onnx,mtp_hidden_output: mtp_hidden,kv_mode: hidden_threaded,embedding_weights: model.embed_tokens.weight,lm_head_weights: lm_head.weight,target_hidden_output: hidden_states.63,hidden_size: 5120,vocab_size: 248320.Runtime schema-conformance fix (metadata block)
The first emitted metadata used a
speculator:block whose field names the onnx-genai runtime cannot parse, soInferenceMetadata.speculativedeserialized toNoneand the sidecar was silently ignored (text-only decode). Now conformed exactly to the authoritative schema (onnx-genaicrates/onnx-genai-metadata:schema/generation.rsSpeculatorConfig,parser.rsresolve_mtp,config.rsvalidate_resolved_mtp_config) — commitb6747e63:speculator->speculative(the runtime key; aliasspeculator_config).speculatorwas unknown -> dropped.model_path->model;hidden_size->target_hidden_size.kv_mode: hidden_threaded(engine-internal enum) ->proposal_local(only validMtpKvModefor this k=1 head).embedding_weights/lm_head_weightsflat strings -> nestedembedding/lm_headMtpTargetInitializerobjects{source: target_initializer, name: ...}.target_hidden_layout: BSH(rank-3[batch,seq,hidden]seed) +hc_mult: 1(resolve_mtprequireshc_mult>0;validate_resolved_mtp_configpins it to 1 for BSH).mtp_state_output(anhc_mult==1head has no recurrent Hyper-Connection state; the sidecar emits onlymtp_hidden+present.0.{key,value}).Emitted block:
Test
TestMtpSpeculatorMetadataasserts the exact keys/values and validates the emitted YAML against onnx-genai's publishedinference_metadata.schema.json. (The existing 17 GB artifact's metadata was hand-patched for the benchmark; not re-exported.)Why
Unblocks MTP self-speculative decode for Qwen3.8-27B batch=1 decode on H200. The engine side already landed in onnx-genai #1598 (recurrent-state commit-by-accepted-prefix);
mtp.rs/MtpDecodeSession+SpeculatorConfigconsume exactly this sidecar. Any Qwen3.5/3.8 GGUF that ships the head now exports it end-to-end automatically.Co-authored-by: Copilot [email protected]