Skip to content

feat(gguf): auto-detect + export Qwen3.5/3.8 MTP self-speculative head from GGUF - #529

Merged
justinchuby merged 9 commits into
mainfrom
roy/qwen38-mtp-head-export
Aug 22, 2026
Merged

justinchuby merged 9 commits into
mainfrom
roy/qwen38-mtp-head-export

Conversation

@justinchuby

@justinchuby justinchuby commented Aug 21, 2026 •

Copy link
Copy Markdown
Member

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 + SpeculatorConfig metadata) 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, which gguf_to_config populates from the source GGUF. Exact presence check:

  • Metadata key <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]), and
  • the trailing block's blk.<N>.nextn.* tensors — nextn.eh_proj (the fc), 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:

  • Source has the nextn head -> always emit the mtp/model.onnx sidecar + the backbone hidden_states.<N-1> seed output + a SpeculatorConfig (proposal_type: mtp) block in inference_metadata.yaml.
  • Source has no nextn head -> emit nothing; text-only output is byte-for-byte identical to today.
  • --static-cache skips 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 trailing blk.<N> block (for 27B: blk.64), tagged by <arch>.nextn_predict_layers. gguf_to_config read that key only to subtract the block from num_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 (GGUF blk.<N>.nextn.* + attn/ffn -> Qwen35MtpModel stems), derive_mtp_config (all dims inherited from the backbone config — no hardcoding), build_mtp_head_from_gguf (reuses Qwen35MtpModel/Qwen35MtpTask and the existing quantized/dequantized loaders via an injectable name_mapper; fc is quantized on the same main-layer path). The two pre_fc_norm_* OffsetRMSNorm +1 offsets missed by the generic norm strip are handled explicitly.
  • integrations/gguf/_builder.py — steps 4b (seed output) and 10 (attach pkg.mtp_head) gate on has_mtp_head(config) alone (auto-detect); static_cache skips the head via a logged info. The quantized/dequantized loaders gained optional name_mapper / warn_unmapped params (backward compatible).
  • __main__.py — when pkg.mtp_head is present, save it into output_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 with mtp_hidden output; plain GGUF omits it).

Validation (partial — stated explicitly)

  • Unit + tiny-GGUF: 9 MTP tests PASS, incl. auto-detect through build_from_gguf (source with nextn -> pkg.mtp_head attached; source without -> absent). Emitted head initializers include fc.weight_t, pre_fc_norm_embedding.weight, pre_fc_norm_hidden.weight, norm.weight, layers.0.self_attn.q_proj.weight_t; graph output mtp_hidden; every initializer has backing weights. _config_mapping / _tensor_mapping suites still pass (74 total).
  • Real 16 GB GGUF blk.64 shape contract VERIFIED against the metadata-derived head — all 15 head tensors map, backbone excluded:
    • attn_q = (12288, 5120) = (2NHHD, H) -> doubled-Q output gating
    • attn_k/attn_v = (1024, 5120) = (NKVHD, H); attn_output = (5120, 6144) = (H, NHHD)
    • nextn.eh_proj = (5120, 10240) = (H, 2H) — the fc GEMM (Q8_0 while projections are Q4_0; mixed quant handled per-tensor)
  • Not run: a full 27B int4 head export through onnxruntime (heavy; the local venv's older onnx_ir lacks max_shard_size_bytes, which also breaks the pre-existing test_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):

  1. Qwen35TextModel.forward ignored output_layer_indices — it always returned a 2-tuple, so the backbone never emitted the hidden_states.{N-1} seed the head consumes; speculator.target_hidden_output pointed at a non-existent output. Added the same capture path as base.py TextModel (returns the selected per-layer post-residual hidden states so HybridCausalLMTask emits hidden_states.{idx}).
  2. build_from_gguf dropped the _gguf_* nextn metadata under --dtype — an explicit dtype (or quantization) triggers dataclasses.replace, which returns a fresh config without the plain _gguf_* attributes, so has_mtp_head saw nothing and skipped the head. Now captured into locals right after gguf_to_config (like _gguf_model_type) and re-attached before step 4b. Regression test added (test_mtp_survives_dtype_replace).

write_mtp_speculator_metadata also now emits embedding_weights, lm_head_weights, and vocab_size.

Full export — VALIDATED end-to-end

Command:

python -m mobius build-gguf /home/justinchu/qwen38-gguf/Qwen3.8-27B-Q4_0.gguf \
  --output /home/justinchu/qwen38-27b-int4-mtp-cuda --dtype bf16 --runtime onnx-genai
  • Layout: 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.
  • Main model exposes hidden_states.63 = [batch, sequence_len, 5120] — exactly speculator.target_hidden_output.
  • MTP head I/O: inputs inputs_embeds, hidden_states, attention_mask, position_ids, past_key_values.0.{key,value}; outputs mtp_hidden + present.0.{key,value} (single full-attention layer KV). fc.weight_t = (10240, 5120); q/k/v/o + gate/up/down projections are int4 MatMulNBits (UINT8 packed, block-32) following the main-layer path; eh_proj stays bf16 (Q8_0 in source). Shared embedding/lm_head are reused from the main model (named in the speculator block), not duplicated.
  • SpeculatorConfig block: 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.
  • Tests: 53 pass (MTP + qwen35 backbone + qwen35-mtp suites), including the new seed-output and dtype-replace regressions.

Runtime schema-conformance fix (metadata block)

The first emitted metadata used a speculator: block whose field names the onnx-genai runtime cannot parse, so InferenceMetadata.speculative deserialized to None and the sidecar was silently ignored (text-only decode). Now conformed exactly to the authoritative schema (onnx-genai crates/onnx-genai-metadata: schema/generation.rs SpeculatorConfig, parser.rs resolve_mtp, config.rs validate_resolved_mtp_config) — commit b6747e63:

  • Top-level key speculator -> speculative (the runtime key; alias speculator_config). speculator was unknown -> dropped.
  • model_path -> model; hidden_size -> target_hidden_size.
  • kv_mode: hidden_threaded (engine-internal enum) -> proposal_local (only valid MtpKvMode for this k=1 head).
  • embedding_weights/lm_head_weights flat strings -> nested embedding/lm_head MtpTargetInitializer objects {source: target_initializer, name: ...}.
  • Added target_hidden_layout: BSH (rank-3 [batch,seq,hidden] seed) + hc_mult: 1 (resolve_mtp requires hc_mult>0; validate_resolved_mtp_config pins it to 1 for BSH).
  • Omit mtp_state_output (an hc_mult==1 head has no recurrent Hyper-Connection state; the sidecar emits only mtp_hidden + present.0.{key,value}).

Emitted block:

speculative:
  proposal_type: mtp
  num_speculative_tokens: 1
  model: mtp/model.onnx
  target_hidden_layout: BSH
  hc_mult: 1
  mtp_hidden_output: mtp_hidden
  kv_mode: proposal_local
  embedding: {source: target_initializer, name: model.embed_tokens.weight}
  lm_head: {source: target_initializer, name: lm_head.weight}
  target_hidden_output: hidden_states.63
  target_hidden_size: 5120
  vocab_size: 248320

Test TestMtpSpeculatorMetadata asserts the exact keys/values and validates the emitted YAML against onnx-genai's published inference_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 + SpeculatorConfig consume 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]

justinchuby and others added 4 commits August 20, 2026 04:57
…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]>
@github-actions

github-actions Bot commented Aug 21, 2026 •

Copy link
Copy Markdown

Performance Comparison

Comparing eec8e3f → 3de1b7b

Model Metric Baseline Current Delta
bert (feature-extraction) model_size_bytes 359 KB 359 KB +0.0% ⚪
bert (feature-extraction) num_nodes 68 68 +0.0% ⚪
falcon model_size_bytes 364 KB 364 KB +0.0% ⚪
falcon num_nodes 66 66 +0.0% ⚪
gemma2 model_size_bytes 428 KB 428 KB +0.0% ⚪
gemma2 num_nodes 105 105 +0.0% ⚪
gpt2 model_size_bytes 388 KB 388 KB +0.0% ⚪
gpt2 num_nodes 54 54 +0.0% ⚪
llama model_size_bytes 425 KB 425 KB +0.0% ⚪
llama num_nodes 60 60 +0.0% ⚪
llama (static-cache) model_size_bytes 425 KB 425 KB +0.0% ⚪
llama (static-cache) num_nodes 56 56 +0.0% ⚪
mamba (ssm-text-generation) model_size_bytes 296 KB 296 KB +0.0% ⚪
mamba (ssm-text-generation) num_nodes 94 94 +0.0% ⚪
phi3 model_size_bytes 421 KB 421 KB +0.0% ⚪
phi3 num_nodes 58 58 +0.0% ⚪
phi3 (static-cache) model_size_bytes 421 KB 421 KB +0.0% ⚪
phi3 (static-cache) num_nodes 54 54 +0.0% ⚪
qwen2 model_size_bytes 425 KB 425 KB +0.0% ⚪
qwen2 num_nodes 60 60 +0.0% ⚪
qwen2 (static-cache) model_size_bytes 425 KB 425 KB +0.0% ⚪
qwen2 (static-cache) num_nodes 56 56 +0.0% ⚪
qwen3_5_moe (hybrid-text-generation) model_size_bytes 506 KB 506 KB +0.0% ⚪
qwen3_5_moe (hybrid-text-generation) num_nodes 264 264 +0.0% ⚪
qwen3_5_text (hybrid-text-generation) model_size_bytes 458 KB 458 KB +0.0% ⚪
qwen3_5_text (hybrid-text-generation) num_nodes 126 126 +0.0% ⚪
qwen3_5_vl (hybrid-qwen-vl) model_size_bytes 977 KB 977 KB +0.0% ⚪
qwen3_5_vl (hybrid-qwen-vl) num_nodes 428 428 +0.0% ⚪
t5 (seq2seq) model_size_bytes 836 KB 836 KB +0.0% ⚪
t5 (seq2seq) num_nodes 166 166 +0.0% ⚪
whisper (speech-to-text) model_size_bytes 1008 KB 1008 KB +0.0% ⚪
whisper (speech-to-text) num_nodes 128 128 +0.0% ⚪

No performance regressions.

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]>
@github-actions

github-actions Bot commented Aug 21, 2026 •

Copy link
Copy Markdown

🏗️ Architecture Diff

Comparing eec8e3f → 3de1b7b

Model Sub-model Changes Status
qwen model 0 ⚪
qwen (static-cache) model 0 ⚪
qwen3 model 0 ⚪
qwen3 (static-cache) model 0 ⚪

No architecture changes detected. ✅


Legend: ⚪ No change · 🔵 Minor (attrs/inits) · 🟡 Moderate (nodes added/removed) · 🔴 Major (interface changed)

@justinchuby justinchuby changed the title feat(gguf): emit Qwen3.5/3.8 MTP self-speculative head sidecar feat(gguf): opt-in Qwen3.5/3.8 MTP self-speculative head export (--include-mtp) Aug 21, 2026
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]>
@justinchuby justinchuby changed the title feat(gguf): opt-in Qwen3.5/3.8 MTP self-speculative head export (--include-mtp) feat(gguf): auto-detect + export Qwen3.5/3.8 MTP self-speculative head from GGUF Aug 21, 2026
justinchuby and others added 2 commits August 21, 2026 05:25
…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]>
Base automatically changed from sapper/qwen38-27b to main August 22, 2026 01:48
@justinchuby
justinchuby requested a review from a team August 22, 2026 01:48
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
Copilot AI lite review requested due to automatic review settings August 22, 2026 15:59
@justinchuby

Copy link
Copy Markdown
Member Author

Review

Conflicts resolved and pushed as 3de1b7b. Reviewed the change; it is sound and I recommend merging. One finding worth acting on separately, detailed below.

Conflict resolution

15 blocks across 8 files. Twelve were formatting-only — main had run ruff format over code this branch also touched (quoted vs bare annotations, list concat vs unpacking, wrapping, one docstring rewording). I took main's side so the formatter would not immediately undo the merge. One of those also dropped _index_dim1, which was defined and never called.

Three were genuine two-sided changes, both sides kept:

  • __main__.py — this branch adds the MTP sidecar save; main replaced the runtime dispatch with write_gguf_runtime_package (dual onnx-genai/ort-genai, feat(gguf): support Q6_K, fix byte-level tokenizers, emit loadable packages #543). The code after the conflict already referenced both, so dropping either would not have compiled.
  • _group_query_attention.py + test — main added sliding-window support (local_window_size from the attention bias) and 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. I took main's file and re-applied this branch's TestMtpSpeculatorMetadata block (with its _MtpBackboneConfig fixture and _seed_backbone_metadata helper) plus the missing import.

What I liked

  • Presence-driven, no flag. emit MTP iff the source ships the head is the right shape — no CLI toggle to get wrong, and a source without nextn is byte-for-byte unchanged.
  • Dims derived, never hardcoded. derive_mtp_config inherits everything from the backbone config, which is what Rule 2 asks for.
  • The auto-detect is actually tested both ways — nextn GGUF emits the head, plain GGUF omits it. A one-sided test would not have caught a detector stuck on true.
  • The PR body states what was not run (full 27B int4 export) rather than implying full coverage.

Test results

  • src/mobius/integrations/gguf/ + src/mobius/rewrite_rules/ — 475 passed, 1 skipped.
  • inference_metadata_test.py — 4 failures that pre-exist on main (reproduced by running main's own test file on main: same 4), plus one MTP schema case, below.

Finding: the committed JSON schema is stale, not this branch

TestMtpSpeculatorMetadata::test_matches_onnx_genai_json_schema fails, but the emitted block is not wrong:

  • The runtime's SpeculatorConfig (generation.rs:65) does use proposal_type / num_speculative_tokens — exactly what this branch writes.
  • parser.rs:26 documents "Every field is resolved from the speculative metadata section", so the key is right too.
  • But schema/inference_metadata.schema.json still binds speculative to SpeculativeContract (required: proposer, target, vocabulary, max_proposal_width) — a different, later structure. The committed schema and the Rust types have diverged.

Also worth flagging for whoever owns the runtime side: the only place that parses this exact shape is a test helper named parse_legacy_speculator. If speculative: SpeculatorConfig is genuinely legacy, this branch is writing a format the runtime intends to retire, and that decision should be made deliberately rather than discovered later. I did not change the emitted format — that is a runtime-contract call, not a merge-conflict call.

Merging on the strength of the above; the schema divergence needs a separate fix on the onnx-genai side.

@justinchuby
justinchuby merged commit c047f77 into main Aug 22, 2026
14 of 23 checks passed
@justinchuby
justinchuby deleted the roy/qwen38-mtp-head-export branch August 22, 2026 16:01

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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 speculative metadata 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.

Comment on lines 248 to 252
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)):
Comment on lines +268 to 271
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
Comment thread src/mobius/__main__.py
output_dir, backbone_config=getattr(pkg, "config", None)
)
if spec_path is not None:
print(f" speculator: {spec_path}")
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.

3 participants