Skip to content

Add MTP speculative decoding to the agent - #215

Merged
bararchy merged 2 commits into
masterfrom
feat/mtp-speculative
Nov 21, 2026
Merged

bararchy merged 2 commits into
masterfrom
feat/mtp-speculative

Conversation

@bararchy

Copy link
Copy Markdown
Member

Problem

Decode on Qwen3.8-27B is bound by weight reads per token. Speculative decoding needs a verify pass over a few tokens that costs about one decode step, and a way to roll the Gated DeltaNet state back after rejected drafts. Before this, a 2-8 token pass took the prefill GEMM path and cost 5.5x a single token (283 ms vs 50 ms for 4 tokens).

Changes

  • gemv_multirow.cuh: int8 multi-row GEMV for 2-8 rows, all 12 GGUF types in use. Each weight block is read once for all rows (llama.cpp mmvq scheme). GGUFMatrix#gemv_into uses it for 2-8 rows (SHAINET_GEMV_MR=0 disables it). 4-token pass: 283 -> 68 ms.
  • Gated DeltaNet verify mode: no state or conv-window writes during verify, and per-block row stash. commit_verify!(n) folds in the kept rows (gated_delta_rule_nowrite, short_conv_silu3_nostate, short_conv_state3).
  • MTPHead: Qwen3.5/3.8 NextN head following llama.cpp graph_mtp, loaded from the model or an mtp-*.gguf sidecar (HFLoader.load_gguf_mtp). Its KV cache stays in lockstep with the trunk, and it is included in clear, save/restore and snapshot.
  • Network#verify, #commit_verify!, #mtp_draft, and all-row logits.
  • Loader reserves VRAM for the head and drops it instead of moving trunk layers to the CPU.
  • Agent: speculative sampling loop (k=3, SHAINET_SPEC_K; 0 = off), sidecar discovery under ~/models.

Results (27B GSQ IQ3_S, RTX 4090 Laptop)

plain spec k=3
agent, temp 1.0, 32K ctx 16.3 tok/s 26.7 tok/s (54% drafts kept)
greedy 17.0 tok/s 34.2 tok/s (69% kept)

Tests

  • spec/gemv_multirow_spec.cr: multi-row vs single-row GEMV for every type.
  • spec/gdn_verify_commit_spec.cr: verify + commit(n) leaves the same state as n plain rows, with a control for a wrong n.
  • Full suite: 523 examples. The 2 failures (batched_kquant_matmul_spec:91, llama_attn_workspace_spec:152) also fail on master.

- gemv_multirow.cuh: int8 multi-row GEMV for 2-8 rows (all 12 GGUF types),
  used by GGUFMatrix#gemv_into. 4-token pass 283 -> 68 ms on the 27B.
- Gated DeltaNet verify mode: no state writes during verify, rows stashed per
  block, commit_verify!(n) folds in the kept rows.
- MTPHead: Qwen3.5/3.8 NextN head (llama.cpp graph_mtp), loaded from the model
  or an mtp-*.gguf sidecar; KV kept in lockstep with the trunk.
- Network: verify / commit_verify! / mtp_draft, all-row logits, MTP in
  clear/save/restore/snapshot.
- Loader reserves VRAM for the head and drops it rather than push trunk
  layers to the CPU.
- Agent: speculative sampling loop (k=3, SHAINET_SPEC_K), sidecar discovery
  under ~/models.
The tiled prefill kernel parallelizes over 64-query tiles and heads only, so a
4-token pass at 32K context ran 24 blocks and took 70 ms of attention against
7 ms for one decode token. Up to 8 tokens now take the grouped split-KV decode
kernel, extended to one query token per blockIdx.z: 22.5 ms, verify pass
120 -> 82 ms at 32K.
@bararchy
bararchy merged commit ccb4b02 into master Nov 21, 2026
5 checks passed
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