Skip to content

feat(speculative): use a qwen4_exp checkpoint's own mtp.* head as the drafter - #2918

Merged
inureyes merged 3 commits into
mainfrom
feature/issue-2917-qwen4-exp-embedded-mtp-drafter
Nov 10, 2026
Merged

inureyes merged 3 commits into
mainfrom
feature/issue-2917-qwen4-exp-embedded-mtp-drafter

Conversation

@inureyes

@inureyes inureyes commented Nov 10, 2026 •

Copy link
Copy Markdown
Member

A qwen4_exp target conversion that keeps its mtp.* tensors (mlx-community/Qwen3.8-Flash-Next-4bit-mtp) can now serve as its own MTP drafter: pass the target directory again as --draft-model (mlxcel generate) or --model-draft (mlxcel-server). No new flag, the Inkling convention.

  • Detection: resolve_drafter_kind resolves Mtp for a qwen4_exp directory whose index or headers hold mtp.* / model.mtp. / language_model.mtp. tensors. The predicate lives in mlxcel-core and the target sanitize's is_mtp_key delegates to it. --draft-kind mtp on a checkpoint without the head fails with the new DrafterError::NoEmbeddedMtpHead; without --draft-kind such a directory resolves as before. Core load_drafter refuses qwen4_exp by name, the binary drafter_loader builds it.
  • Config: Qwen4ExpMtpConfig::from_target_json builds the drafter config (block_size: 4, the target text_config, tie_word_embeddings) and keeps only the default quantization pair plus the mtp.* overrides with the prefix stripped, then runs the existing from_json.
  • Weights: Qwen4ExpMtpDraftModel::from_target_checkpoint loads through load_weights_from_dir_index_filtered, so only the head shard is opened; it logs the head's resident bytes.
  • Memory estimate: no estimate path adds drafter bytes, and the index total_size already counts the head once, so nothing changed there.
  • The split qwen4_exp_mtp directory path and the hc_count check are unchanged.

Tests: the tiny fixture written as a target checkpoint (with an unreadable non-mtp shard in the index) and as a split directory gives identical draft logits; quantization stripping and block size; NoEmbeddedMtpHead; the hc_count refusal for the embedded head; unchanged resolution without mtp.*; missing indexed shard named in the error. Real model (M3 Ultra, Qwen3.8-Flash-Next-4bit-mtp): offline MTP is byte-identical to classic on English, code and Korean prompts (acceptance 0.611 / 0.731 / 0.507), the head is resident at 1.61 GiB, mlxcel inspect counts it once, and the server with --model-draft returns the offline output; details, plus a pre-existing classic-server divergence on this target that the split drafter shows too, are in the PR comment.

Closes #2917

… drafter

A qwen4_exp target conversion that keeps its mtp.* tensors (mlx-community/Qwen3.8-Flash-Next-4bit-mtp) can now be passed again as --draft-model / --model-draft. resolve_drafter_kind detects the head from its tensors, like Inkling, and an explicit --draft-kind mtp on a checkpoint without them fails with the new DrafterError::NoEmbeddedMtpHead. Without --draft-kind such a directory resolves as before.

Qwen4ExpMtpDraftModel::from_target_checkpoint derives the head config with Qwen4ExpMtpConfig::from_target_json (block_size 4, the target text_config, only the mtp.* quantization overrides with the prefix stripped) and loads only the shards the index maps a head tensor to through load_weights_from_dir_index_filtered, logging the head's resident size. The mtp prefix predicate now lives in mlxcel-core and the target sanitize delegates to it. The split qwen4_exp_mtp directory path is unchanged.

Closes #2917
@inureyes inureyes added type:enhancement New features, capabilities, or significant additions priority:medium Medium priority area:inference Generation, sampling, decoding (incl. speculative, DRY) platform:macos macOS (Apple Silicon) specific arch:moe Sparse mixture-of-experts decoder status:review Under review labels Nov 10, 2026
Replace `.err().expect()` with `.expect_err()` in the embedded-head refusal test (clippy::err_expect under -D warnings), correct the drafter_loader comment about when the qwen4_exp arm is reached, and record the M3 Ultra validation of mlx-community/Qwen3.8-Flash-Next-4bit-mtp in the supported-models row: byte identity with classic on three prompts, acceptance rates, the 1.61 GiB head and the single count in mlxcel inspect.

Refs #2917
@inureyes

Copy link
Copy Markdown
Member Author

Real-model results on an M3 Ultra (512 GB), release build of 628e86e5 (cargo build --release --features metal,accelerate --bins), target and drafter mlx-community/Qwen3.8-Flash-Next-4bit-mtp at 825df623302aeebc434f44ca9122827b04f9a0f1. All runs were serialized under one lock.

Prompt Classic (MLXCEL_QMV_WIDE=0) vs --draft-model <same dir> Acceptance Mean accepted length Block tok/s classic / MTP
English (sky and sunsets) byte-identical, 256 tokens 0.611 2.83 4..4 46.1 / 60.7
Code (Python memoized Fibonacci) byte-identical, 214 tokens (EOS) 0.731 3.19 4..4 45.2 / 67.8
Korean (Seoul history and sights) byte-identical, 256 tokens 0.507 2.51 2..4 46.0 / 53.6

Command: mlxcel generate -m <dir> [--draft-model <dir>] -p <prompt> -n 256 --temp 0 --show-reasoning. The whole stream was compared, reasoning channel included. These are single runs, not a benchmark.

  • Detection: the log shows "Auto-detected the qwen4_exp checkpoint's embedded MTP head from mtp.* tensors" with no --draft-kind. The configured block size is 4.
  • Drafter resident size: the drafter log reports tensors=54 resident_bytes=1726854656 (1.61 GiB, the one 1.73 GB head shard). The target loads at 103.92 GB, unchanged.
  • mlxcel inspect -m <dir>: weights 113,245,755,466 bytes, which is the index total_size. The head is counted once.
  • Server: mlxcel-server --model <dir> --model-draft <dir> sends one /v1/chat/completions request (English prompt, max_tokens 256, temperature 0), run twice:
    • The two runs are stable.
    • The content and reasoning are byte-identical to the offline classic and offline MTP output above.
    • The split Qwen3.8-Flash-Next-MTP drafter on the same server and target returns the same bytes.
    • A classic mlxcel-server without a drafter (also under MLXCEL_QMV_WIDE=0) diverges from all of these in the first answer sentence. This happens with the split drafter too, so it is not caused by this PR. It looks like the 4-bit target's classic server decode path is not the offline classic chain, and it should be tracked separately. feat(speculative): wire the native MTP head and rollback for qwen4_exp #2908 validated the server against the OptiQ 2-bit target, not this one.
  • Split drafter regression check: models/Qwen3.8-Flash-Next-MTP with models/Qwen3.8-Flash-Next-OptiQ-2bit, 128 tokens with --show-reasoning, is byte-identical to classic (acceptance 0.500).

@inureyes inureyes added status:done Completed and removed status:review Under review labels Nov 10, 2026
@inureyes
inureyes marked this pull request as ready for review November 10, 2026 02:13
@inureyes
inureyes merged commit 60a3270 into main Nov 10, 2026
1 check passed
@inureyes
inureyes deleted the feature/issue-2917-qwen4-exp-embedded-mtp-drafter branch November 10, 2026 02:13
inureyes added a commit that referenced this pull request Nov 10, 2026
## Summary

Records the timing and memory measurements that #2908, #2906, #2912, #2909, #2911 and #2918 deferred to a quiet host, in `docs/benchmark_results/qwen4-exp-deferred-measurements-m3ultra-2026-11-10.md` (Mac Studio M3 Ultra, 512 GB, `main` @ `60a3270c`).

- Native MTP (#2880, #2917): 1.30x / 1.10x / 1.76x (code / prose / enumeration) for the OptiQ 2-bit target with the bf16 split head, 1.38x / 1.00x / 1.74x for the 4-bit target with its embedded head.
- SSD expert streaming (#2882): decode 42.8 tok/s resident, 23.0 at a 24 GB arena (89.8% hit), 18.3 at 8 GB (74.9% hit). Router sync costs 8.0 s per run at both sizes, more than the I/O wait. Output identical across arms.
- External PLE (#2881): 31 GB less resident (78.5 to 47.3 GB), same TTFT at 4K and 32K, identical output in every mode.
- Prompt-cache second turn (#2885): 0.51 to 0.60 s cached vs 15.1 s (16K) and 28.6 to 28.9 s (32K) after a reset, byte-identical in 10/10 repetitions.
- macOS memory soak (#2883): 0 compressions and 0 swap-outs in every setting. A 512 GB host cannot show the eviction the issue targets.

## Script changes

- `scripts/bench_speculative.sh`: new `flashnext` and `flashnext4` pairings.
- `scripts/macos_memory_soak.sh`: send the `model` field (every request got HTTP 400 without it), and default `--out` gets a `-wire-weights` suffix under `--wire-weights` so it no longer overwrites the plain run.

## Not covered

A Mac with less memory than the checkpoint, the `prepare-expert-stream` contiguous file, and streaming combined with the external PLE table.

Refs #2880, #2881, #2882, #2883, #2885, #2917
@inureyes inureyes added this to the 0.9 milestone Nov 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

arch:moe Sparse mixture-of-experts decoder area:inference Generation, sampling, decoding (incl. speculative, DRY) platform:macos macOS (Apple Silicon) specific priority:medium Medium priority status:done Completed type:enhancement New features, capabilities, or significant additions

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat(speculative): use a qwen4_exp checkpoint's own mtp.* head as the drafter

1 participant