diff --git a/.gitignore b/.gitignore index 15dc4014f432..ab436a016c7c 100644 --- a/.gitignore +++ b/.gitignore @@ -145,3 +145,6 @@ poetry.toml /.windsurf/ # emscripten a.out.* + +.history/ +.scratch/ \ No newline at end of file diff --git a/AGENTS.md b/AGENTS.md index 97c25074b4c6..e69de29bb2d1 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -1,110 +0,0 @@ -# Instructions for llama.cpp - -> [!IMPORTANT] -> This project does **not** accept pull requests that are fully or predominantly AI-generated. AI tools may be utilized solely in an assistive capacity. -> -> Read more: [CONTRIBUTING.md](CONTRIBUTING.md) - -AI assistance is permissible only when the majority of the code is authored by a human contributor, with AI employed exclusively for corrections or to expand on verbose modifications that the contributor has already conceptualized (see examples below). - ---- - -## Guidelines for Contributors Using AI - -llama.cpp is built by humans, for humans. Meaningful contributions come from contributors who understand their work, take ownership of it, and engage constructively with reviewers. - -Maintainers receive numerous pull requests weekly, many of which are AI-generated submissions where the author cannot adequately explain the code, debug issues, or participate in substantive design discussions. Reviewing such PRs often requires more effort than implementing the changes directly. - -**A pull request represents a long-term commitment.** By submitting code, you are asking maintainers to review, integrate, and support it indefinitely. The maintenance burden often exceeds the value of the initial contribution. - -Most maintainers already have access to AI tools. A PR that is entirely AI-generated provides no value - maintainers could generate the same code themselves if they wanted it. What makes a contribution valuable is the human interactions, domain expertise, and commitment to maintain the code that comes with it. - -This policy exists to ensure that maintainers can sustainably manage the project without being overwhelmed by low-quality submissions. - ---- - -## Guidelines for Contributors - -Contributors are expected to: - -1. **Demonstrate full understanding of their code.** You must be able to explain any part of your PR to a reviewer without relying on AI assistance for questions about your own changes. - -2. **Take responsibility for maintenance.** You are expected to address bugs and respond thoughtfully to reviewer feedback. - -3. **Communicate clearly and concisely.** Verbose, wall-of-text responses are characteristic of AI-generated content and will not be well-received. Direct, human communication is expected. - -4. **Respect maintainers' time.** Search for existing issues and discussions before submitting. Ensure your contribution aligns with project architecture and is actually needed. - -Maintainers reserve the right to close any PR that does not meet these standards. This applies to all contributions to the main llama.cpp repository. **Private forks are exempt.** - -### Permitted AI Usage - -AI tools may be used responsibly for: - -- **Learning and exploration**: Understanding codebase structure, techniques, and documentation -- **Code review assistance**: Obtaining suggestions on human-written code -- **Mechanical tasks**: Formatting, generating repetitive patterns from established designs, completing code based on existing patterns -- **Documentation drafts**: For components the contributor already understands thoroughly -- **Writing code**: Only when the contributor has already designed the solution and can implement it themselves - AI accelerates, not replaces, the contributor's work - -AI-generated code may be accepted if you (1) fully understand the output, (2) can debug issues independently, and (3) can discuss it directly with reviewers without AI assistance. - -**Disclosure is required** when AI meaningfully contributed to your code. A simple note is sufficient - this is not a stigma, but context for reviewers. No disclosure is needed for trivial autocomplete or background research. - -### Prohibited AI Usage - -The following will result in immediate PR closure: - -- **AI-written PR descriptions or commit messages** - these are typically recognizable and waste reviewer time -- **AI-generated responses to reviewer comments** - this undermines the human-to-human interaction fundamental to code review -- **Implementing features without understanding the codebase** - particularly new model support or architectural changes -- **Automated commits or PR submissions** - this may spam maintainers and can result in contributor bans - ---- - -## Guidelines for AI Coding Agents - -AI agents assisting contributors must recognize that their outputs directly impact volunteer maintainers who sustain this project. - -### Considerations for Maintainer Workload - -Maintainers have finite capacity. Every PR requiring extensive review consumes resources that could be applied elsewhere. Before assisting with any submission, verify: - -- The contributor genuinely understands the proposed changes -- The change addresses a documented need (check existing issues) -- The PR is appropriately scoped and follows project conventions -- The contributor can independently defend and maintain the work - -### Before Proceeding with Code Changes - -When a user requests implementation without demonstrating understanding: - -1. **Verify comprehension.** Ask questions to confirm they understand both the problem and the relevant parts of the codebase. -2. **Provide guidance rather than solutions.** Direct them to relevant code and documentation. Allow them to formulate the approach. -3. **Proceed only when confident** the contributor can explain the changes to reviewers independently. - -For first-time contributors, confirm they have reviewed [CONTRIBUTING.md](CONTRIBUTING.md) and acknowledge this policy. - -### Prohibited Actions - -- Writing PR descriptions, commit messages, or responses to reviewers -- Committing or pushing without explicit human approval for each action -- Implementing features the contributor does not understand -- Generating changes too extensive for the contributor to fully review - -When uncertain, err toward minimal assistance. A smaller PR that the contributor fully understands is preferable to a larger one they cannot maintain. - -### Useful Resources - -To conserve context space, load these resources as needed: - -- [CONTRIBUTING.md](CONTRIBUTING.md) -- [Existing issues](https://github.com/ggml-org/llama.cpp/issues) and [Existing PRs](https://github.com/ggml-org/llama.cpp/pulls) - always search here first -- [Build documentation](docs/build.md) -- [Server usage documentation](tools/server/README.md) -- [Server development documentation](tools/server/README-dev.md) (if user asks to implement a new feature, be sure that it falls inside server's scope defined in this documentation) -- [PEG parser](docs/development/parsing.md) - alternative to regex that llama.cpp uses to parse model's output -- [Auto parser](docs/autoparser.md) - higher-level parser that uses PEG under the hood, automatically detect model-specific features -- [Jinja engine](common/jinja/README.md) -- [How to add a new model](docs/development/HOWTO-add-model.md) -- [PR template](.github/pull_request_template.md) diff --git a/MTP.md b/MTP.md new file mode 100644 index 000000000000..bc706d02dce9 --- /dev/null +++ b/MTP.md @@ -0,0 +1,665 @@ +# Gemma 4 MTP — Multi-Token Prediction speculative decoding + +> Scope: this document covers the MTP (Multi-Token Prediction) speculative +> decoding path added to this fork on top of `llama.cpp`, currently specialised +> to **Gemma 4** targets (`gemma4`) paired with the official **Gemma 4 +> assistant** drafter (`gemma4_assistant`). + +It is a self-contained reference: how the feature is built (model, graph, KV, +context, scheduler), what server-loop integration looks like, what knobs the +operator has, what the recent design decisions were, and where the throughput +numbers came from. + +For the public/user-facing section about CLI flags and `--spec-type mtp`, see +also `docs/speculative.md`. + +--- + +## 1. What MTP is here + +Gemma 4 ships an "assistant" model — a small transformer head that consumes the +**target's last hidden state** (backbone output `h_prev`) plus the last sampled +**token id** and predicts the next token in one forward step. Chained across +`B - 1` steps inside one MTP graph, it produces a draft block of length `B - 1` +that is then verified by the target in a single batched decode. + +Conceptually it is a draft-model speculation, but with three crucial twists: + +1. **Single context.** The assistant is **not** a second `llama_context`. Its + weights live next to the target in `llama_model::mtp_assistant`. There is no + second tokenizer, no second KV cache, no second sampler. +2. **Cross-attention into the target's KV.** Each MTP layer reads `K/V` from + the **last layer of the matching attention type** (full / sliding) of the + target's KV cache (`llama_kv_cache_iswa::init_mtp` → `mtp_slot_info`). No + draft-side KV is allocated. +3. **Shared `h_prev` from the target.** The assistant ingests the target's + per-token backbone hidden state (`embeddings_ith`) for the **last accepted** + position. Embeddings must therefore stay enabled on the target context + (`llama_set_embeddings(ctx_tgt, true)`). + +This makes MTP much cheaper than a normal "small draft model" approach: there +is essentially no draft KV, no second model orchestration, and the per-step +graph is tiny (4 transformer blocks for 26B/31B; centroid LM head for E2B/E4B). + +--- + +## 2. Components and where they live + +| Concern | File(s) | +|---|---| +| MTP graph (per-step build) | `src/models/gemma4-assistant.cpp` | +| Model arch + tensor types | `src/llama-arch.cpp/.h`, `src/llama-model.cpp` | +| GGUF tensor mapping | `gguf-py/gguf/tensor_mapping.py`, `gguf-py/gguf/constants.py`, `convert_hf_to_gguf.py` | +| Loading assistant into target | `src/llama.cpp::llama_model_load_mtp_from_file` | +| MTP scheduler + worker + APIs | `src/llama-context.cpp/.h` (`sched_mtp`, `mtp_worker_loop`, `decode_mtp_*`) | +| KV cross-attention helpers | `src/llama-kv-cache.cpp`, `src/llama-kv-cache-iswa.cpp` (`init_mtp`) | +| Speculative driver / overlap | `common/speculative.cpp` (`common_speculative_state_mtp`) | +| Server integration | `tools/server/server-context.cpp` | +| Public C API | `include/llama.h` (`llama_decode_mtp_async/_wait`, `llama_model_load_mtp_from_file`, `llama_model_mtp_n_embd_backbone`, …) | +| Verification helper | `scripts/verify-gemma4-assistant-gguf.py` | +| Run scripts | `scripts/run-gemma4-{,e2b-,e4b-,31b-}mtp-server.sh`, `scripts/quantize-gemma4-edge-assistant-mtp.sh` | +| Tests | `tests/test-speculative-mtp.cpp` | +| Tracing | `LLAMA_MTP_ACC_TRACE` (NDJSON in `common/speculative.cpp`) | + +--- + +## 3. Model side: assistant, centroid LM head, GGUF layout + +The assistant is loaded from its own GGUF and **attached** to a target model: + +```c +int32_t llama_model_load_mtp_from_file( + struct llama_model * model, + const char * path_assistant, + struct llama_model_params mparams); +``` + +After load, the target carries: + +- `model.mtp_assistant` — a fully-loaded `llama_model` with arch + `gemma4_assistant` (4 transformer blocks, the pre/post backbone projections, + optional centroid head). +- `hparams.n_embd_backbone` — must equal target's backbone hidden size; this is + asserted at load and re-checked at draft time (`n_bb` in + `decode_mtp_run`). + +CLI surface (`common/arg.cpp`, `common/common.cpp`): + +- `--mtp-head ` (preferred) and `--model-draft / -md` (back-compat alias) + — feed the same `mparams_dft.path` field. +- `--draft-block-size ` — head proposes `B - 1` tokens per round. +- `--gpu-layers-draft / -ngld`, `-ctkd / -ctvd` — placement and KV typing for + the **assistant** weights when offloaded. + +### Centroid / ordered-embeddings LM head (E2B / E4B) + +For Edge models (`use_ordered_embeddings = true` in HF config), the LM head is +not the dense tied embedding but a **MaskedEmbedder**: + +1. `centroid_logits = mul_mat(mtp.centroids, h)` → `[n_centroids]`. +2. `top_k(centroid_logits, centroid_intermediate_top_k)` → `top_k` centroid ids + (I32, on-device). +3. `mtp.token_ordering` is viewed as `[vsc, n_centroids]` + (`vsc = n_vocab / n_centroids`); each centroid column lists `vsc` candidate + token ids. `get_rows` gathers the candidate ids for the chosen centroids. +4. `get_rows(token_embd, ids)` then `mul_mat(·, h)` produces sparse logits over + only those candidates. +5. We **scatter** them into a full `[n_vocab]` row pre-filled with `-1e30` + (`ggml_fill_inplace + ggml_set_rows`). The full-vocab row is what the + verifier expects — sparse-only argmax broke server accept (rare-token edge + cases) and so was reverted. + +GGUF layout for the centroid head (see `convert_hf_to_gguf.py` and +`docs/development/gemma4-assistant-tensor-inventory.md`): + +| Tensor | Stored type | On-disk shape | Notes | +|---|---|---|---| +| `mtp.centroids.weight` | F16/F32 (or quant) | `[n_embd, n_centroids]` after GGUF dim packing | numpy is `[n_centroids, n_embd]`; written as-is so loader sees `mul_mat`-compatible shape | +| `mtp.token_ordering.weight` | **I32** (kept integer end-to-end) | `[n_vocab]` | must not be quantized — the converter explicitly preserves I32 | +| `mtp.pre_projection.weight` / `mtp.post_projection.weight` | as model | `[2*n_embd, n_embd]` / `[n_embd, n_embd_backbone]` | concatenates `[token_embd, h_prev]` then projects back | + +The verifier `scripts/verify-gemma4-assistant-gguf.py` enforces these shape / +dtype invariants and is run automatically by every `run-gemma4-*-mtp-server.sh` +script (skip with `VERIFY_ASSISTANT_GGUF=0`). + +--- + +## 4. Per-step MTP graph (`gemma4-assistant.cpp`) + +`llm_build_gemma4_mtp` builds a single-token, single-sequence graph (`n_tokens += 1`, `n_seqs = 1`, `n_outputs = 1`): + +1. Inputs (registered as `llm_graph_input_mtp`): + - `inp_last_token : I32 [1]` + - `inp_h_prev : F32 [n_embd_backbone, 1]` + - `inp_pos` (standard `build_inp_pos`) + - `inp_attn` (`build_attn_inp_kv_iswa`) +2. Token embedding from the **target's** `tok_embd`, then scaled by + `sqrt(n_embd)` to mirror Gemma 4's input scaling. +3. `concat([tok_e, h_prev], axis=0) → mtp.pre_projection` (collapses the + `2 * n_embd` channel back to `n_embd`). +4. 4 transformer blocks (`mtp.layers[il]`): + - RMSNorm → Q proj → Q-norm → RoPE. + - **Cross-attention** via `build_attn_mtp`: queries from MTP, K/V fetched + from the target's KV cache at `il_kv = last layer in target with the same + attention type` (SWA / full). + - Per HF Gemma 4 quirk (`attention_k_eq_v: true`): even when V was derived + from K, the V slot is **written** with rms-norm-without-scale and + un-rotated, so cross-attn must always read V from cache (not reuse the + post-RoPE K). This is encoded as `use_k_as_v = false`. + - Standard residual + post-norm + GELU FFN + post-FFN norm + per-layer + `out_scale` (if present) + `build_cvec`. +5. Final RMSNorm → `mtp.post_projection` produces the **next-step `h_prev`** + (this is what the host stitches between steps). +6. LM head — dense (tied) **or** centroid-routed for ordered embeddings. +7. Optional `f_final_logit_softcapping`. +8. **In-graph greedy argmax**: `ggml_argmax(cur)` → `I32 [1]`. The final result + exposes three tensors via `llm_graph_result`: + - `t_embd` = `h_post` (next `h_prev`) + - `t_logits` = full-vocab row (kept for diagnostic / `out_logits` API) + - `t_argmax` = greedy token id + +The host reads `t_argmax` (4 bytes) per step instead of pulling the full +F32 `[n_vocab]` row across a backend boundary and running CPU argmax. On +Gemma 4 + Q4_K_XL this alone delivered ~+2-3% throughput +(109.5 → 112.5 tps at n=128; 95.8 → 97.8 tps at n=512), with bit-identical +greedy drafts. The full row is still computed in-graph and is fetched on +demand by passing `out_logits != NULL` to the synchronous +`llama_decode_mtp(...)` API (legacy / diagnostic path), which transparently +falls back to `decode_mtp_sync`. + +--- + +## 5. KV sharing — what is read, what is appended + +The MTP step does **not** allocate or write its own KV. It reads the target's +KV by: + +- `kv_iswa->init_mtp(seq_id, ub)` — produces a memory context whose attention + inputs (`build_attn_inp_kv_iswa`) wire the cross-attn to the target slot for + `seq_id`, with a mask that admits all positions `≤ attn_pos`. +- `attn_pos` is taken from `llama_memory_seq_pos_max(mem, seq_id)` immediately + before submission (post-`seq_rm`). All `n_steps` step positions chosen for + RoPE are strictly **`> attn_pos`**, so the causal/SWA mask uniformly admits + every target cell — that is why a single mask suffices for the whole chained + draft. + +KV-safety contract for asynchronous draft work (see Section 7): + +- `decode_mtp_async` snapshots `h_prev` and `attn_pos` at submit time. +- The target may **append** at positions `> attn_pos` between submit and + `_wait` (this is what the verify decode does), but it must not evict, rewrite + or `seq_rm` cells at positions `≤ attn_pos` until `_wait` returns. +- The current append-only KV cache satisfies this. `common_speculative_cancel` + is invoked at the few server-loop points that *do* mutate KV destructively + (request stop / release; new request `seq_rm`; spec-disabled iterations). + +--- + +## 6. `llama_context` plumbing — `sched_mtp`, worker, and APIs + +The async pipeline lives in `src/llama-context.cpp/.h`. New members on the +context (Phase C of the original plan): + +- `sched_mtp` — a **dedicated** `ggml_backend_sched` for MTP. Created lazily + by `ensure_sched_mtp()` and reserved with a single-token MTP graph (the MTP + graph is invariant in size: `n_tokens = 1`, `n_seqs = 1`, `n_outputs = 1`, + so one reserve covers all subsequent calls). +- `gf_res_prev_mtp` — a **separate** `llm_graph_result` cache. This is the key + reason MTP graph reuse survives target-decode resets, and is the single + largest win of the async refactor. +- A worker thread (`mtp_worker`), `std::mutex` + 2 condition variables + (`mtp_cv_request`, `mtp_cv_response`), and request/response slots + (`std::optional`, `bool mtp_in_flight`, `std::optional`). +- `backend_cfg_mu` — guards shared backend reconfiguration + (`set_threadpool_fn`, `set_n_threads_fns`) so the worker's + `graph_compute_mtp` cannot race the main thread's `graph_compute`. The lock + is held only across cheap setters; the actual `graph_compute_async` calls run + unlocked so target verify and MTP encode can interleave on each scheduler. + +Public C APIs (`include/llama.h`): + +```c +LLAMA_API int32_t llama_decode_mtp_async( + struct llama_context * ctx, + llama_seq_id seq_id, + llama_pos attn_pos, + llama_token last_token, + const float * h_prev, + int32_t n_steps); + +LLAMA_API int32_t llama_decode_mtp_wait( + struct llama_context * ctx, + llama_token * out_drafts, + float * out_h_prev_last); + +// Backward-compatible synchronous facade. If out_logits != NULL falls back to +// decode_mtp_sync (per-step logits captured in-thread). +LLAMA_API int32_t llama_decode_mtp( + struct llama_context * ctx, + llama_seq_id seq_id, llama_pos attn_pos, + llama_token last_token, float * h_prev, + int32_t n_steps, + llama_token * out_drafts, float * out_logits, float * out_h_prev_last); +``` + +Contract: + +- At most **one in-flight request per context**. `_async` while a previous + request has not been `_wait`ed returns `-7`. +- `h_prev` is *copied* into the request → caller may free / reuse immediately. +- Drafts are written into `out_drafts[0..n_steps-1]`; the last `h_prev` is + optionally copied into `out_h_prev_last`. + +Worker loop (`mtp_worker_loop`): waits on `mtp_pending`, runs +`decode_mtp_run` (the per-step chain on `sched_mtp`), publishes +`mtp_completed`, and notifies. `decode_mtp_run` per step: + +1. Build `llama_ubatch` `{token=last_token, embd=h, pos=attn_pos+1+k, output=0}`. +2. `kv_iswa->init_mtp(seq_id, ub)` → memory context. +3. `process_ubatch_mtp` → reuse cached graph if `can_reuse(gparams)` else + rebuild + alloc. +4. `graph_compute_mtp` → `sched_mtp.graph_compute_async` → synchronize. +5. Read `t_argmax` (4 bytes) → `last_token = drafts[k]`; read `t_embd` → + `h` (next `h_prev`). + +On context destruction the worker is signalled via `mtp_worker_stop`, woken, +and joined before tearing down `sched_mtp`. + +--- + +## 7. Speculative driver — pipeline depth-2 + +The host driver lives in `common/speculative.cpp :: +common_speculative_state_mtp`. Its job is to translate the server's +"draft / accept" loop into the right `_async / _wait` calls and to enforce the +KV-safety contract. + +### Depth-2 overlap + +The server normally goes: + +``` + loop: + drafts = common_speculative_draft(...) # produce drafts + target_decode(...) # verify drafts + n_acc, sampled = sample_and_accept_n(...) + common_speculative_accept(spec, n_acc) + seq_rm(...); update slot.sampled / batch.dft index +``` + +Depth-2 inserts a `prepare_next` at the **end** of the iteration, after +`accept` and `seq_rm`: + +``` + common_speculative_prepare_next(spec, slot.sampled) # async submit +``` + +…which calls `llama_decode_mtp_async(...)` for the *next* round using: + +- `attn_pos = seq_pos_max(seq_id)` (post `seq_rm`), +- the real sampled token `slot.sampled` (no optimistic guess), +- `h_prev = embeddings_ith(h_idx)` snapshotted right after sample/accept (see + Section 8 for `h_idx`). + +Then on the next iteration, `common_speculative_draft` checks +`has_pending`. If pending and `pending_n_steps == n_steps`, it goes "lazy": + +``` + llama_decode_mtp_wait(...) # blocks only on whatever is left of MTP +``` + +This **overlaps MTP draft compute with everything that happens between +`accept` and the next `draft`**: token I/O, OAI streaming, slot bookkeeping, +batching, the next prefill if any. The benefit is real because the MTP graph, +while small, is not free — it is `B - 1` sequential single-token decodes +through 4 layers + cross-attn + LM head. + +When `n_steps` changes between iterations (e.g. on the last iteration of a +request), or when the target is about to mutate KV destructively, the driver +**drains** the in-flight request (`mtp_drain_pending_discard`) to keep the +`_async`/`_wait` invariant intact. + +The depth-2 path can be A/B-tested at runtime by exporting +`LLAMA_PIPELINE_DEPTH2=0`, which turns `prepare_next` into a no-op and +restores depth-1 (sync `_async + _wait` inside `draft`). + +### Drain points (server-side) + +`tools/server/server-context.cpp` invokes `common_speculative_cancel` in three +places: + +1. When the iteration **skips** speculative decoding (`n_remaining == 1`, + `n_min` not satisfied, etc.) — otherwise the worker would compute against + KV that is about to change in the upcoming target_decode (we observed Metal + command-buffer status 3 on turbo3 KV before this guard). +2. After `send_final_response` / `slot.release` — the next request will + `seq_rm` and overwrite cells the worker is still reading. +3. In `common_speculative_begin` (new prompt) — the previous generation's + in-flight MTP must not bleed into the next prompt. + +--- + +## 8. The `h_idx` correction + +A subtle correctness issue: `embeddings_ith(-1)` returns the **last batch +output**, which after partial draft acceptance is the hidden state of a +**rejected** draft (computed for the wrong input). Feeding that as `h_prev` +collapses acceptance. + +Fix: after `sample_and_accept_n` the server sets + +```cpp +common_speculative_set_h_idx(slot.spec, slot.i_batch_dft[ids.size() - 1]); +``` + +i.e. it points the next MTP draft at the batch index of the **last accepted +token**. This is honored both in the sync `draft` path and in +`prepare_next` (Section 7). + +--- + +## 9. Adaptive skip-streak + +There are workloads (numbers, code, rare tokens deep in long generations) where +the MTP head is consistently wrong. Drafting still costs ~10 ms with no +accepted tokens. The driver detects this: + +- `prev_n_acc_drafts` snapshot at the end of each `draft`. +- Increment `zero_accept_streak` when `n_acc_drafts` did not move since the + previous call; reset on any non-empty accept. +- After `LLAMA_MTP_SKIP_STREAK_THRESHOLD` consecutive zero-accepts (1..32), + return an empty draft for one batch (server falls back to a single-token + verify), reset the streak, and let the next batch re-arm. +- `skip_streak_last_draft` prevents threshold=1 from oscillating into a + permanent skip. + +Off by default (env unset / `0`). The `MTP_PRESET=throughput` Edge presets do +**not** enable it either — turn it on per-deployment when the workload +warrants. + +--- + +## 10. Diagnostic NDJSON tracer (`LLAMA_MTP_ACC_TRACE`) + +Set `LLAMA_MTP_ACC_TRACE=1` (stderr) or `LLAMA_MTP_ACC_TRACE=/path/to.ndjson` +(append) to enable the `mtp_acc_tracer` in `common/speculative.cpp`. Off by +default at zero overhead (enabled-only L2 reduction over `n_bb` per draft). + +Two events per iteration, paired by `iter`: + +- `mtp_draft` — `iter`, `path` (`sync` / `lazy` / `skip-streak` / `skip-nsteps`), + `seq_id`, `id_last`, `h_idx`, `attn_pos`, `n_steps`, `h_l2` (L2 norm of + `h_prev`), and `drafts[]`. +- `mtp_accept` — `iter`, `n_accepted`, `n_drafted_prev`. + +This is the recommended tool for any acceptance-rate debugging: per-position +acceptance, h_prev stability, `h_idx` selection bias, and depth-2 lazy/sync +distribution all fall out by joining on `iter`. + +--- + +## 11. Operating it — scripts and presets + +### Pre-built assistant GGUFs + +Official Gemma 4 assistant heads, converted with this fork's +`convert_hf_to_gguf.py` (preserves I32 `mtp.token_ordering` for the +centroid-head Edge variants), are published as a Hugging Face collection: + +> [AtomicChat / Gemma 4 Assistant GGUF](https://huggingface.co/collections/AtomicChat/gemma-4-assistant-gguf) +> — F16 / Q8_0 / Q5_K_M / **Q4_K_M** / Q4_K_S quantizations. + +| Target | Assistant repo | Recommended quant | +|---|---|---| +| Gemma 4 E2B | [`AtomicChat/gemma-4-E2B-it-assistant-GGUF`](https://huggingface.co/AtomicChat/gemma-4-E2B-it-assistant-GGUF) | **Q4_K_M** | +| Gemma 4 E4B | [`AtomicChat/gemma-4-E4B-it-assistant-GGUF`](https://huggingface.co/AtomicChat/gemma-4-E4B-it-assistant-GGUF) | **Q4_K_M** | +| Gemma 4 26B-A4B | [`AtomicChat/gemma-4-26B-A4B-it-assistant-GGUF`](https://huggingface.co/AtomicChat/gemma-4-26B-A4B-it-assistant-GGUF) | **Q4_K_M** / Q4_K_S | +| Gemma 4 31B | [`AtomicChat/gemma-4-31B-it-assistant-GGUF`](https://huggingface.co/AtomicChat/gemma-4-31B-it-assistant-GGUF) | **Q4_K_M** / Q4_K_S | + +Q4_K_M is the recommended default: throughput is identical to F16 in the +matrix bench (the head is small enough that bandwidth, not weight precision, +dominates), while VRAM/RAM footprint is ~4× lower. Drop to F16 only if you +are debugging an acceptance regression that you suspect is quant-related; the +verifier `scripts/verify-gemma4-assistant-gguf.py` will refuse to load a +malformed assistant GGUF in either case. + +The repo helpers prefer a quantized assistant under `.scratch/` when one +exists (`gemma-{e2b,e4b,…}-assistant-mtp-Q4_K_M.gguf`) and fall back to F16 +otherwise. Override with `DRAFT_GGUF=…` or pass `--mtp-head` directly. + +### Run scripts + +Helper scripts live under `scripts/`: + +| Script | Target | Notes | +|---|---|---| +| `run-gemma4-mtp-server.sh` | gemma 4 26B | dense LM head; `MTP_PRESET` not used | +| `run-gemma4-31b-mtp-server.sh` | gemma 4 31B | dense LM head | +| `run-gemma4-e2b-mtp-server.sh` | gemma 4 E2B | centroid head; `MTP_PRESET` aware | +| `run-gemma4-e4b-mtp-server.sh` | gemma 4 E4B | centroid head; `MTP_PRESET` aware | +| `run-gemma4-server-turbo.sh` | dense baselines, no MTP | TurboQuant KV demo | +| `quantize-gemma4-edge-assistant-mtp.sh` | quantizer for E2B/E4B assistant | preserves I32 ordering | + +Edge presets (`MTP_PRESET`): + +| Preset | `--draft-block-size` (B) | `--draft-max` | +|---|---:|---:| +| `throughput` | 2 | 6 | +| `lift` | 3 | 8 | +| `balanced` | 3 | 8 | +| `quality` | 4 | 16 | + +Override directly with `DRAFT_BLOCK_SIZE`, `DRAFT_MAX`, +`LLAMA_MTP_SKIP_STREAK_THRESHOLD`. KV typing is taken from `CTK / CTV / CTKD / +CTVD` — both target and assistant inherit the same default (`turbo3`). + +The bench harness `.scratch/bench-matrix.sh` runs the matrix +`{model} × {f16-base, turbo3-base, f16-mtp, turbo3-mtp} × {short=128, long=512} +× 3 runs` against `/v1/chat/completions` with `temperature=0`, +`cache_prompt=false` and `stream=false`, and reports median tps + mean +draft-accept rate. + +--- + +## 12. Latest matrix benchmark (`.scratch/bench-logs/matrix-q4chat.log`) + +Run on 2026-05-07. Q4_K_S assistant heads, draft-block defaults from each +script (`B = 3` for the dense scripts at the time of this run). `accept` is +`draft_n_accepted / draft_n` averaged over 3 runs; `tps` is the median. + +### Bench host + +| Component | Value | +|---|---| +| Machine | MacBook Pro (`Mac16,5`, MX313LL/A) | +| SoC | Apple **M4 Max** — 16 CPU cores (12P + 4E), 40-core GPU | +| Unified memory | 48 GB LPDDR5 | +| OS | macOS 26.3.1 (build 25D2128), Darwin 25.3.0 | +| llama.cpp backend | Metal (full GPU offload: `-ngl 99 -ngld 99`, `-fa on`) | +| Server | local `llama-server` over `127.0.0.1:8080` | +| Client | `python3 urllib` → `/v1/chat/completions`, `temperature=0`, `cache_prompt=false`, `stream=false` | +| Driver | `.scratch/bench-matrix.sh` (3 runs/cell, median tps, mean accept) | + +Single-slot configuration (`--parallel 1 -np 1 --cont-batching`); no other +heavy GPU/CPU workloads were running on the host during the matrix sweep. + +| model | mode | short tps (n=128) | long tps (n=512) | short accept | long accept | +|---|---|---:|---:|---:|---:| +| gemma-26B | f16-base | 81.54 | 83.06 | — | — | +| gemma-26B | turbo3-base | 53.81 | 53.89 | — | — | +| gemma-26B | f16-mtp | **109.49** | **95.75** | 85.9% | 68.9% | +| gemma-26B | turbo3-mtp | 81.91 | 72.17 | 82.3% | 67.9% | +| gemma-31B | f16-base | 14.15 | 15.20 | — | — | +| gemma-31B | turbo3-base | 15.79 | 14.82 | — | — | +| gemma-31B | f16-mtp | **20.24** | **17.30** | 88.0% | 74.6% | +| gemma-31B | turbo3-mtp | 18.67 | 15.68 | 87.0% | 70.8% | + +Key observations: + +- **f16 MTP**: +34 % short / +15 % long over baseline on 26B; +43 % short / + +14 % long on 31B. Acceptance is dominated by short, "essay-y" prompts; long + drafts hit the natural ceiling once content drifts into less predictable + spans. +- **turbo3 MTP**: +52 % short / +34 % long over the turbo3 baseline on 26B + (turbo3 baseline is slower than f16 because the gemma-26B target is + compute-bound at `f16` and bandwidth-helped by turbo3 only when + memory-bound; that asymmetry is not specific to MTP). +- **31B base inversion** (`turbo3-base 15.79 > f16-base 14.15` short): 31B is + bandwidth-bound on this rig, so turbo3 KV beats f16 on the short cell. MTP + still adds value on top of either KV typing. +- **Accept short > accept long** is consistent across the matrix: as decode + drifts away from boilerplate phrasing, the assistant's drafts become less + reliable and `B - 1` chained steps amplify the rejection. + +### How we got here (history within this branch) + +The matrix logs in `.scratch/bench-logs/` show the optimisation journey for the +gemma-26B `f16-mtp` short-prompt cell: + +| Log (mtime, `ls -lt`) | Short tps | Long tps | Short accept | What changed | +|---|---:|---:|---:|---| +| `matrix-run2.log` (01:26) | 70.89 | 76.79 | 55.5% | early async pipeline, sync wrapper | +| `matrix-old.log` (01:41) | 61.88 | 63.98 | 50.0% | depth-1 sync MTP, `h_idx=-1` regression | +| `matrix-q4chat.log` (02:02) | **109.49** | 95.75 | **85.9%** | depth-2 + in-graph argmax + correct `h_idx` | +| `matrix-c-prime.log` (02:50, partial) | 112.30 | 96.69 | 85.9% | identical config, additional run sample | + +The big jump (~62 → ~109 tps short) came from three independent fixes +landing together: + +1. **`h_idx` correction** so MTP feeds the *accepted* hidden state instead of a + rejected draft's output (acceptance jumps from ~50% to ~86%). +2. **Pipeline depth-2 overlap** so MTP work overlaps post-accept bookkeeping + (steady ~+8% throughput at fixed accept). +3. **In-graph argmax** so the host transfers 4 bytes instead of `n_vocab × 4 B` + per step (~+2-3% on top). + +--- + +## 13. Trade-offs and gotchas + +These are the non-obvious failure / regression modes you should keep in mind +when changing or extending this code. + +**Embeddings on the target context.** MTP is meaningless without +`llama_set_embeddings(ctx_tgt, true)`. The server wires this conditionally per +batch (`need_embeddings = need_embd() || mtp_active`). If a future code path +flips embeddings off mid-generation, MTP will silently degrade to drafting +against zero `h_prev`. + +**`h_idx` after partial accept.** Forgetting to call +`common_speculative_set_h_idx` after `sample_and_accept_n` regresses accept +rate to ~50 % on the same workload (matrix-old vs matrix-q4chat). Any new code +path that produces drafts must restore the correct batch index of the *last +accepted* token, not `-1`. + +**KV append-only invariant.** Async MTP correctness depends on `attn_pos` cells +remaining stable until `_wait`. Any new operation that rewrites KV in place +(eviction, sliding-window compaction, retroactive `seq_rm` past `attn_pos`) +must call `common_speculative_cancel` first. The server has three explicit +drain points (Section 7); reuse them rather than inventing a fourth contract. + +**Single in-flight request per context.** This is intentional — multiplexing +MTP across slots requires a sched-per-slot or a request queue with its own +graph-cache. Today a second `_async` returns `-7` and `prepare_next` is a +no-op when one is in flight. With `--parallel > 1` slots run on the same +context: the MTP overlap currently benefits only the slot whose `prepare_next` +won the race; the others fall back to sync. Lifting this is non-trivial +(graph-cache, scheduler, KV snapshot all need per-slot identity). + +**`draft_block_size` vs. `draft_max`.** `draft_block_size` is the **MTP head's +block** (head emits `B - 1` tokens). `draft_max` is the standard llama.cpp +upper bound on draft length the server will accept. For Edge centroid heads +(heavier per-step), small `B` (2-3) usually wins; for the dense 26B/31B, +`B = 3` is the current sweet spot in the matrix bench. + +**Centroid-head `top_k` cost.** Edge MTP runs `top_k` over `n_centroids` and a +routed `get_rows` per draft step. Greedy still materialises the full-vocab row +(masked-fill + scatter) so verify-side argmax stays consistent. +`use_ordered_embeddings` has measurably higher per-step cost than the dense +head; budget `B = 2` (`MTP_PRESET=throughput`) by default on Edge. The Edge +matrix cell is not yet in `matrix-q4chat.log` (the script lists `gemma-E4B` +in `MODELS`, but the row is not present — the GGUF was missing on the bench +host that day). + +**Skip-streak hysteresis.** With `LLAMA_MTP_SKIP_STREAK_THRESHOLD=1` and +without `skip_streak_last_draft`, the driver would skip every other batch +forever as soon as one zero-accept happened. Keep that guard. + +**Backend reconfiguration races.** `set_n_threads` / `set_threadpool` are +process-global on a backend. The `backend_cfg_mu` window in `graph_compute` / +`graph_compute_mtp` is intentionally tiny (only the setters, never the +`graph_compute_async` itself). Lengthening that critical section will block +the worker on every target step and erase the depth-2 win. + +**Vocab compatibility for MTP is laxer than for `--spec-type draft`.** Target +chat templates own stop / EOS tokens; the MTP head only predicts next-token +ids. `common_speculative_are_compatible_mtp` therefore checks `vocab_type`, +size (within `SPEC_VOCAB_MAX_SIZE_DIFFERENCE`) and per-token text equality +from id ≥ 5, but **skips** bos/eos/add_bos/add_eos checks. Don't reuse this +loosened check for non-MTP draft pairings. + +**Optimistic last token (future work).** Submitting `prepare_next` with a +guess of the next sampled token before sample/accept could hide one extra +`llama_decode` on hits. On misses we'd waste the entire MTP block. Not landed +— would need a clear measurement that hit-rate is high enough to justify the +miss cost on this workload. + +--- + +## 14. Quick reference + +```sh +# 26B + Q4 assistant, MTP on TurboQuant3 KV (matches matrix-q4chat row). +scripts/run-gemma4-mtp-server.sh + +# E4B, throughput preset (B=2, max=6), centroid head, optional skip-streak. +LLAMA_MTP_SKIP_STREAK_THRESHOLD=4 \ +MTP_PRESET=throughput \ +scripts/run-gemma4-e4b-mtp-server.sh + +# A/B-test depth-2 overlap vs sync at the same model/config: +LLAMA_PIPELINE_DEPTH2=0 scripts/run-gemma4-mtp-server.sh + +# NDJSON acceptance trace to a file. +LLAMA_MTP_ACC_TRACE=/tmp/mtp.ndjson scripts/run-gemma4-mtp-server.sh + +# Re-run the matrix bench (median over 3 runs per cell). +bash .scratch/bench-matrix.sh | tee .scratch/bench-logs/matrix-$(date +%H%M).log +``` + +Environment knobs: + +| Var | Default | Effect | +|---|---|---| +| `LLAMA_PIPELINE_DEPTH2` | unset (on) | `=0` disables depth-2 overlap; falls back to sync `_async + _wait` inside `draft`. | +| `LLAMA_MTP_SKIP_STREAK_THRESHOLD` | unset / `0` (off) | `1..32` enables zero-accept skip streak. | +| `LLAMA_MTP_ACC_TRACE` | unset (off) | `1` → stderr; any other value → file path (append). | +| `LLAMA_GRAPH_REUSE_DISABLE` | unset (off) | Disables `llm_graph_result::can_reuse`. Useful when changing the MTP graph; disastrous for throughput. | + +Public API entry points: + +```c +llama_model_load_mtp_from_file(model, path, mparams); +llama_model_has_mtp_assistant(model); +llama_model_get_mtp_assistant(model); +llama_model_mtp_n_embd_backbone(model); + +llama_decode_mtp_async(ctx, seq_id, attn_pos, last_token, h_prev, n_steps); +llama_decode_mtp_wait (ctx, out_drafts, out_h_prev_last); +llama_decode_mtp (ctx, ..., out_logits, ...); // sync facade +``` + +Driver entry points (`common/speculative.h`): + +```c +common_speculative_init / _free +common_speculative_set_seq_id // server slot -> target seq id +common_speculative_set_h_idx // last accepted batch idx after accept_n +common_speculative_begin // per-prompt; drains stale MTP +common_speculative_draft // emits drafts (lazy-waits depth-2) +common_speculative_accept // updates stats; emits trace +common_speculative_prepare_next // depth-2: async submit for next round +common_speculative_cancel // drain in-flight MTP +common_speculative_print_stats +``` diff --git a/README.md b/README.md index be23abcea67f..b3160fdf9f9c 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,8 @@ LLM inference in C/C++ ## Hot topics +- **Gemma 4 MTP speculative decoding: pair a `gemma4` target with the official `gemma4_assistant` head (loaded via `--mtp-head`) for ~+30-50 % short-prompt throughput. See [MTP.md](MTP.md) and the pre-built Q4 assistant GGUFs at the [AtomicChat/Gemma 4 Assistant GGUF collection](https://huggingface.co/collections/AtomicChat/gemma-4-assistant-gguf).** +- **TurboQuant KV cache & weights: WHT-rotated low-bit quantization with backend-native kernels (Metal `TurboFlash`, CUDA, Vulkan, HIP). Use `-ctk turbo3 -ctv turbo3` for ~4.3× KV compression, or quantize weights to `TQ4_1S`/`TQ3_1S`. See [Compression below](#turboquant-kv-cache--weight-compression).** - **Hugging Face cache migration: models downloaded with `-hf` are now stored in the standard Hugging Face cache directory, enabling sharing with other HF tools.** - **[guide : using the new WebUI of llama.cpp](https://github.com/ggml-org/llama.cpp/discussions/16938)** - [guide : running gpt-oss with llama.cpp](https://github.com/ggml-org/llama.cpp/discussions/15396) @@ -70,6 +72,76 @@ range of hardware - locally and in the cloud. The `llama.cpp` project is the main playground for developing new features for the [ggml](https://github.com/ggml-org/ggml) library. +## TurboQuant — KV cache & weight compression + +> **Credits.** TurboQuant in this fork is built on top of the absolutely +> awesome work by **[@TheTom](https://github.com/TheTom)** in +> [TheTom/llama-cpp-turboquant](https://github.com/TheTom/llama-cpp-turboquant). +> Huge thanks for the original WHT-rotated quantization design, the reference +> kernels, and the relentless backend ports — none of this would exist +> without that project. ❤️ + +This fork (`atomic-llama-cpp-turboquant`) packages **TurboQuant** as a family +of WHT-rotated low-bit quantization formats with backend-native kernels. They +target two distinct memory-traffic problems: + +- **KV cache compression** — `TURBO2_0` / `TURBO3_0` / `TURBO4_0` (2/3/4-bit, + WHT + PolarQuant). Selected at runtime via `-ctk` / `-ctv`. +- **Model weight compression** — `TQ3_1S` / `TQ4_1S` (3/4-bit, WHT-rotated + Lloyd-Max with `block_size = 32`). Selected at quantize time as a + `--type` for `llama-quantize`. + +### KV cache types (`-ctk` / `-ctv`) + +| Type | Bits | Compression vs F16 | Notes | +|---|---:|---:|---| +| `turbo2` | 2 | ~6.4× | maximum compression, intended for large-context budgets | +| `turbo3` | 3 | ~4.3× | **recommended default**; Metal `TurboFlash` decode kernel | +| `turbo4` | 4 | ~3.8× | highest accuracy of the family, safest fallback | + +Typical invocation with full GPU offload + Flash-Attention: + +```bash +llama-server -m model.gguf -c 32768 -ngl 99 \ + -ctk turbo3 -ctv turbo3 -fa on +``` + +Pair with `--cache-reuse N` and a long `-c` to see the practical KV-budget +win — TurboQuant typically shifts the OOM ceiling on Apple Silicon / +discrete GPUs by 3-6× at the same context length. + +### Weight quantization types (`llama-quantize`) + +| Type | Bits | Block size | Notes | +|---|---:|---:|---| +| `TQ3_1S` | 3 | 32 | 8-level Lloyd-Max + WHT rotation | +| `TQ4_1S` | 4 | 32 | 16-level Lloyd-Max + WHT rotation; fused Metal/Vulkan MUL_MAT_VEC kernels | + +```bash +# Convert / re-quantize an F16/F32 GGUF to TQ4_1S. +llama-quantize model-f16.gguf model-tq4_1s.gguf TQ4_1S +``` + +`TQ4_1S` typically delivers ~25-35 % size reduction vs Q8_0 with single-digit-% +PPL deltas; on bandwidth-bound models / GPUs it can also be faster than Q8_0 +because of the lighter memory traffic. + +### Backend support + +| Backend | KV `turbo2` / `turbo3` / `turbo4` | Weights `TQ3_1S` / `TQ4_1S` | +|---|---|---| +| Metal (Apple Silicon) | yes; `TurboFlash` flash-attn decode kernel for `turbo3` (off-by-default on Apple10 — see PR #91) | yes (V2.1 fused kernels) | +| CUDA (NVIDIA) | `turbo3` / `turbo4` (full); `turbo2` via reference path | `TQ4_1S` MUL_MAT_VEC | +| Vulkan | `turbo3` KV (FA + coopmat), `SET_ROWS` for `turbo2/4` | `TQ4_1S` (specialised MUL_MAT_VEC, SET_ROWS, CPY) | +| HIP / ROCm | `turbo3` KV; F16-K + TURBO-V mixed dispatch | reference | +| CPU | reference (correctness, not throughput) | reference | + +For combining TurboQuant KV with **Gemma 4 MTP speculative decoding**, see +[MTP.md §11-12](MTP.md). The matrix bench shows that the combo +(`turbo3` KV + MTP) is the right pick when the target model is bandwidth-bound +(e.g. Gemma 4 31B), and that f16-KV + MTP wins when the target is +compute-bound (e.g. Gemma 4 26B-A4B on M4 Max). +
Models @@ -408,6 +480,87 @@ To learn more about model quantization, [read this documentation](tools/quantize
+-
+ Enable TurboQuant KV cache compression (this fork) + + Use a TurboQuant KV-cache type for both K and V — typically with + Flash-Attention enabled — to cut KV memory traffic and footprint at + long contexts. Recommended default is **`turbo3`** (3-bit, ~4.3× vs F16, + accelerated by `TurboFlash` on Metal and dedicated kernels on + CUDA / Vulkan / HIP). + + ```bash + # ~4.3x KV compression vs F16, full GPU offload, Flash-Attn on. + llama-server -m model.gguf -c 32768 \ + -ngl 99 -ctk turbo3 -ctv turbo3 -fa on + ``` + + Pick a stronger compression preset by stepping the bit-width: + + ```bash + -ctk turbo2 -ctv turbo2 # 2-bit KV, ~6.4x vs F16 (highest compression) + -ctk turbo3 -ctv turbo3 # 3-bit KV, ~4.3x (default sweet spot) + -ctk turbo4 -ctv turbo4 # 4-bit KV, ~3.8x (highest accuracy / fallback) + ``` + + See the longer write-up [below](#turboquant-kv-cache--weight-compression) + for weight quantization (`TQ4_1S` / `TQ3_1S`) and the per-backend support + matrix. + +
+ +-
+ Enable Gemma 4 MTP speculative decoding (this fork) + + Pair a `gemma4` target with the official `gemma4_assistant` MTP head. The + head is loaded **into** the target context (no second `llama_context`, + no second KV cache) and runs on a dedicated scheduler so MTP draft compute + overlaps target verification. + + Pre-built assistant GGUFs (recommended **`Q4_K_M`** / `Q4_K_S` for best + speed/quality) are published in the [AtomicChat / Gemma 4 Assistant GGUF + collection](https://huggingface.co/collections/AtomicChat/gemma-4-assistant-gguf): + + | Target model | Assistant (MTP head) GGUF | + |---|---| + | Gemma 4 E2B | [`AtomicChat/gemma-4-E2B-it-assistant-GGUF`](https://huggingface.co/AtomicChat/gemma-4-E2B-it-assistant-GGUF) | + | Gemma 4 E4B | [`AtomicChat/gemma-4-E4B-it-assistant-GGUF`](https://huggingface.co/AtomicChat/gemma-4-E4B-it-assistant-GGUF) | + | Gemma 4 26B-A4B | [`AtomicChat/gemma-4-26B-A4B-it-assistant-GGUF`](https://huggingface.co/AtomicChat/gemma-4-26B-A4B-it-assistant-GGUF) | + | Gemma 4 31B | [`AtomicChat/gemma-4-31B-it-assistant-GGUF`](https://huggingface.co/AtomicChat/gemma-4-31B-it-assistant-GGUF) | + + ```bash + # Manual invocation — works for any of the four targets above. + llama-server \ + -m /path/to/gemma-4-target.gguf \ + --mtp-head /path/to/gemma-4-assistant-Q4_K_M.gguf \ + --spec-type mtp \ + --draft-block-size 3 \ + -c 16384 \ + -ngl 99 -ngld 99 \ + -fa on \ + --host 127.0.0.1 --port 8080 + ``` + + Repo helper scripts pick the right defaults per target (and prefer + a quantized assistant when present under `.scratch/`): + + ```bash + # Dense targets (block size 3 by default). + scripts/run-gemma4-mtp-server.sh # 26B-A4B + scripts/run-gemma4-31b-mtp-server.sh # 31B + + # Edge / centroid-head targets (MTP_PRESET aware: throughput|lift|balanced|quality). + MTP_PRESET=throughput scripts/run-gemma4-e4b-mtp-server.sh + MTP_PRESET=throughput scripts/run-gemma4-e2b-mtp-server.sh + ``` + + Full architecture, async pipeline, KV-safety contract, tuning knobs and + the latest matrix benchmark live in [MTP.md](MTP.md). User-facing CLI + flags (`--spec-type`, `--draft-*`) are documented in + [docs/speculative.md](docs/speculative.md). + +
+ -
Serve an embedding model diff --git a/common/arg.cpp b/common/arg.cpp index faee613aa77e..cb36c5541289 100644 --- a/common/arg.cpp +++ b/common/arg.cpp @@ -3492,11 +3492,18 @@ common_params_context common_params_parser_init(common_params & params, llama_ex ).set_examples({LLAMA_EXAMPLE_SPECULATIVE, LLAMA_EXAMPLE_SERVER, LLAMA_EXAMPLE_CLI}).set_env("LLAMA_ARG_N_GPU_LAYERS_DRAFT")); add_opt(common_arg( {"-md", "--model-draft"}, "FNAME", - "draft model for speculative decoding (default: unused)", + "draft model for speculative decoding, or Gemma 4 MTP assistant GGUF when using --spec-type mtp (default: unused)", [](common_params & params, const std::string & value) { params.speculative.mparams_dft.path = value; } ).set_examples({LLAMA_EXAMPLE_SPECULATIVE, LLAMA_EXAMPLE_SERVER, LLAMA_EXAMPLE_CLI}).set_env("LLAMA_ARG_MODEL_DRAFT")); + add_opt(common_arg( + {"--mtp-head"}, "FNAME", + "alias for Gemma 4 MTP: path to gemma4_assistant GGUF (loaded into the target; use with --spec-type mtp)", + [](common_params & params, const std::string & value) { + params.speculative.mparams_dft.path = value; + } + ).set_examples({LLAMA_EXAMPLE_SPECULATIVE, LLAMA_EXAMPLE_SERVER, LLAMA_EXAMPLE_CLI}).set_env("LLAMA_ARG_MTP_HEAD")); add_opt(common_arg( {"--spec-replace"}, "TARGET", "DRAFT", "translate the string in TARGET into DRAFT if the draft model and main model are not compatible", @@ -3505,12 +3512,18 @@ common_params_context common_params_parser_init(common_params & params, llama_ex } ).set_examples({LLAMA_EXAMPLE_SPECULATIVE, LLAMA_EXAMPLE_SERVER, LLAMA_EXAMPLE_CLI})); add_opt(common_arg( - {"--spec-type"}, "[none|ngram-cache|ngram-simple|ngram-map-k|ngram-map-k4v|ngram-mod]", - string_format("type of speculative decoding to use when no draft model is provided (default: %s)\n", + {"--spec-type"}, "[none|draft|mtp|eagle3|ngram-cache|ngram-simple|ngram-map-k|ngram-map-k4v|ngram-mod]", + string_format("type of speculative decoding (default: %s). For Gemma 4 MTP use --spec-type mtp and --mtp-head (or --model-draft) pointing at gemma4_assistant GGUF.\n", common_speculative_type_to_str(params.speculative.type).c_str()), [](common_params & params, const std::string & value) { if (value == "none") { params.speculative.type = COMMON_SPECULATIVE_TYPE_NONE; + } else if (value == "draft") { + params.speculative.type = COMMON_SPECULATIVE_TYPE_DRAFT; + } else if (value == "mtp") { + params.speculative.type = COMMON_SPECULATIVE_TYPE_MTP; + } else if (value == "eagle3") { + params.speculative.type = COMMON_SPECULATIVE_TYPE_EAGLE3; } else if (value == "ngram-cache") { params.speculative.type = COMMON_SPECULATIVE_TYPE_NGRAM_CACHE; } else if (value == "ngram-simple") { @@ -3522,10 +3535,20 @@ common_params_context common_params_parser_init(common_params & params, llama_ex } else if (value == "ngram-mod") { params.speculative.type = COMMON_SPECULATIVE_TYPE_NGRAM_MOD; } else { - throw std::invalid_argument("unknown speculative decoding type without draft model"); + throw std::invalid_argument("unknown speculative decoding type"); } } ).set_examples({LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_SPEC_TYPE")); + add_opt(common_arg( + {"--draft-block-size"}, "N", + string_format("MTP draft block size B (drafts B-1 tokens per round; default: %d)", params.speculative.draft_block_size), + [](common_params & params, int value) { + if (value < 2 || value > 32) { + throw std::invalid_argument("draft block size must be between 2 and 32"); + } + params.speculative.draft_block_size = value; + } + ).set_examples({LLAMA_EXAMPLE_SERVER, LLAMA_EXAMPLE_SPECULATIVE}).set_env("LLAMA_ARG_DRAFT_BLOCK_SIZE")); add_opt(common_arg( {"--spec-ngram-size-n"}, "N", string_format("ngram size N for ngram-simple/ngram-map speculative decoding, length of lookup n-gram (default: %d)", params.speculative.ngram_size_n), diff --git a/common/common.cpp b/common/common.cpp index 825df65ab4ff..4821a5620483 100644 --- a/common/common.cpp +++ b/common/common.cpp @@ -1188,6 +1188,43 @@ common_init_result::common_init_result(common_params & params) : pimpl->lora.emplace_back(std::move(lora)); // copy to list of loaded adapters } + if (params.speculative.has_dft() && params.speculative.type == COMMON_SPECULATIVE_TYPE_MTP) { + if (params.speculative.mparams_dft.path.empty() && params.speculative.mparams_dft.hf_repo.empty()) { + LOG_ERR("%s: MTP speculative decoding requires --mtp-head or --model-draft with a local path\n", __func__); + pimpl->model.reset(); + return; + } + if (params.speculative.mparams_dft.path.empty()) { + LOG_ERR("%s: MTP assistant must be loaded from a local GGUF path (HF repo download not wired here)\n", __func__); + pimpl->model.reset(); + return; + } + + common_params p_mtp = params; + p_mtp.n_parallel = 1; + p_mtp.n_ctx = params.speculative.n_ctx > 0 ? params.speculative.n_ctx : params.n_ctx; + p_mtp.n_batch = std::max(p_mtp.n_ctx, params.n_batch); + p_mtp.devices = params.speculative.devices; + p_mtp.model = params.speculative.mparams_dft; + p_mtp.n_gpu_layers = params.speculative.n_gpu_layers; + p_mtp.cache_type_k = params.speculative.cache_type_k; + p_mtp.cache_type_v = params.speculative.cache_type_v; + if (params.speculative.cpuparams.n_threads > 0) { + p_mtp.cpuparams = params.speculative.cpuparams; + p_mtp.cpuparams_batch = params.speculative.cpuparams_batch; + } + p_mtp.tensor_buft_overrides = params.speculative.tensor_buft_overrides; + + llama_model_params mparams_mtp = common_model_params_to_llama(p_mtp); + const char * path_mtp = params.speculative.mparams_dft.path.c_str(); + if (llama_model_load_mtp_from_file(model, path_mtp, mparams_mtp) != 0) { + LOG_ERR("%s: failed to load MTP assistant from '%s'\n", __func__, path_mtp); + pimpl->model.reset(); + return; + } + params.speculative.model_dft = nullptr; + } + // updates params.sampling // TODO: fix naming common_init_sampler_from_model(model, params.sampling); diff --git a/common/common.h b/common/common.h index 020b6a721ff9..674b24885572 100644 --- a/common/common.h +++ b/common/common.h @@ -177,6 +177,7 @@ enum common_speculative_type { COMMON_SPECULATIVE_TYPE_NGRAM_MAP_K4V, // self-speculative decoding with n-gram keys and 4 m-gram values COMMON_SPECULATIVE_TYPE_NGRAM_MOD, COMMON_SPECULATIVE_TYPE_NGRAM_CACHE, // self-speculative decoding with 3-level n-gram cache + COMMON_SPECULATIVE_TYPE_MTP, // Gemma 4 MTP assistant drafter COMMON_SPECULATIVE_TYPE_COUNT // number of types, unknown type }; @@ -316,6 +317,12 @@ struct common_params_speculative { int32_t n_max = 16; // maximum number of tokens to draft during speculative decoding int32_t n_min = 0; // minimum number of draft tokens to use for speculative decoding + + // MTP (Gemma 4 assistant): draft block size B produces B-1 draft tokens per round. + // Default 3 (= 2 chained MTP draft steps): empirically the universal sweet spot for + // both f16 and turbo3 KV cache types after the chained MTP draft optimization + // (see docs/speculative.md and bench-block-size scripts). + int32_t draft_block_size = 3; float p_split = 0.1f; // speculative decoding split probability float p_min = 0.75f; // minimum speculative decoding probability (greedy) diff --git a/common/sampling.h b/common/sampling.h index 5b57ad658119..2426126b3463 100644 --- a/common/sampling.h +++ b/common/sampling.h @@ -66,8 +66,10 @@ llama_token common_sampler_sample(struct common_sampler * gsmpl, struct llama_co // generalized version of common_sampler_sample // -// will cross-reference the sampled tokens with a batch of draft tokens and accept those that match -// if the sampler disagrees at some point, we stop and return the accepted tokens up to now +// will cross-reference greedy-argmax target tokens with a batch of draft tokens and accept those that match +// (draft positions use raw-logit argmax so stochastic params like temperature do not break MTP / greedy drafts) +// then samples the next continuation token with the full chain when all drafts matched +// if the target disagrees at some point, we stop and return the accepted tokens up to now // // common_sampler_sample_n(gsmpl, ctx, { idx }, {}); // diff --git a/common/speculative.cpp b/common/speculative.cpp index 3e68c38e49cc..349d23dcd201 100644 --- a/common/speculative.cpp +++ b/common/speculative.cpp @@ -10,13 +10,20 @@ #include "sampling.h" #include +#include +#include +#include #include #include #include +#include +#include +#include #define SPEC_VOCAB_MAX_SIZE_DIFFERENCE 128 #define SPEC_VOCAB_CHECK_START_TOKEN_ID 5 + const std::vector common_speculative_types = { COMMON_SPECULATIVE_TYPE_NONE, COMMON_SPECULATIVE_TYPE_DRAFT, @@ -25,7 +32,8 @@ const std::vector common_speculative_types = { COMMON_SPECULATIVE_TYPE_NGRAM_MAP_K, COMMON_SPECULATIVE_TYPE_NGRAM_MAP_K4V, COMMON_SPECULATIVE_TYPE_NGRAM_MOD, - COMMON_SPECULATIVE_TYPE_NGRAM_CACHE + COMMON_SPECULATIVE_TYPE_NGRAM_CACHE, + COMMON_SPECULATIVE_TYPE_MTP }; const std::map common_speculative_type_from_name_map = { @@ -36,7 +44,8 @@ const std::map common_speculative_typ {"ngram_map_k", COMMON_SPECULATIVE_TYPE_NGRAM_MAP_K}, {"ngram_map_k4v", COMMON_SPECULATIVE_TYPE_NGRAM_MAP_K4V}, {"ngram_mod", COMMON_SPECULATIVE_TYPE_NGRAM_MOD}, - {"ngram_cache", COMMON_SPECULATIVE_TYPE_NGRAM_CACHE} + {"ngram_cache", COMMON_SPECULATIVE_TYPE_NGRAM_CACHE}, + {"mtp", COMMON_SPECULATIVE_TYPE_MTP} }; struct common_speculative_config { @@ -47,6 +56,48 @@ struct common_speculative_config { const common_params_speculative & p = common_params_speculative{}) : type(t), params(p) {} }; +static bool common_speculative_mtp_arch_ok(const llama_model * model_tgt, const llama_model * model_dft) { + return std::strcmp(llama_model_arch_str(model_tgt), "gemma4") == 0 + && std::strcmp(llama_model_arch_str(model_dft), "gemma4_assistant") == 0; +} + +// MTP-specific vocab compatibility: +// the assistant (draft) only predicts the next token id from the same SentencePiece +// vocabulary; stop-condition / chat-template special tokens are owned by the target. +// Therefore we only require identical vocab_type, vocab size (within tolerance) and +// per-token text equality, and intentionally skip bos/eos id and add_bos/add_eos +// checks (target may be chat-tuned with eos==106, draft with eos==1). +static bool common_speculative_are_compatible_mtp( + const llama_model * model_tgt, + const llama_model * model_dft) { + const llama_vocab * vocab_tgt = llama_model_get_vocab(model_tgt); + const llama_vocab * vocab_dft = llama_model_get_vocab(model_dft); + + if (llama_vocab_type(vocab_tgt) != llama_vocab_type(vocab_dft)) { + return false; + } + + const int n_vocab_tgt = llama_vocab_n_tokens(vocab_tgt); + const int n_vocab_dft = llama_vocab_n_tokens(vocab_dft); + const int vocab_diff = n_vocab_tgt > n_vocab_dft + ? n_vocab_tgt - n_vocab_dft + : n_vocab_dft - n_vocab_tgt; + + if (vocab_diff > SPEC_VOCAB_MAX_SIZE_DIFFERENCE) { + return false; + } + + for (int i = SPEC_VOCAB_CHECK_START_TOKEN_ID; i < std::min(n_vocab_tgt, n_vocab_dft); ++i) { + const char * t_tgt = llama_vocab_get_text(vocab_tgt, i); + const char * t_dft = llama_vocab_get_text(vocab_dft, i); + if (std::strcmp(t_tgt, t_dft) != 0) { + return false; + } + } + + return true; +} + static bool common_speculative_are_compatible( const llama_model * model_tgt, const llama_model * model_dft) { @@ -142,6 +193,14 @@ struct common_speculative_state { llama_tokens & result) = 0; virtual void accept(uint16_t n_accepted) = 0; + + // Optional hook: MTP submits async draft work after accept for overlap with the next draft() wait. + virtual void prepare_next(llama_token id_last) { + GGML_UNUSED(id_last); + } + + // Optional hook: drain any in-flight async work (prepare_next) and discard. + virtual void cancel() {} }; struct common_speculative_state_draft : public common_speculative_state { @@ -462,6 +521,395 @@ struct common_speculative_state_eagle3 : public common_speculative_state { } }; +// Optional NDJSON tracer for MTP draft/accept events, gated by env LLAMA_MTP_ACC_TRACE. +// Value semantics: +// unset / "0" / "" -> disabled +// "1" -> stderr +// anything else -> treated as a file path (append mode) +// Each event is one JSON object per line. Disabled at zero overhead; otherwise computes +// h_prev L2 norm (n_bb floats) per draft, which is negligible vs the MTP step itself. +namespace { +struct mtp_acc_tracer { + bool enabled = false; + FILE * fp = nullptr; + std::mutex mu; + + mtp_acc_tracer() { + const char * v = std::getenv("LLAMA_MTP_ACC_TRACE"); + if (!v || v[0] == '\0' || std::strcmp(v, "0") == 0) { + return; + } + if (std::strcmp(v, "1") == 0) { + fp = stderr; + } else { + fp = std::fopen(v, "a"); + } + enabled = (fp != nullptr); + } + + ~mtp_acc_tracer() { + if (fp && fp != stderr) { + std::fclose(fp); + } + } + + void writeln(const std::string & line) { + if (!enabled) { + return; + } + std::lock_guard lk(mu); + std::fputs(line.c_str(), fp); + std::fputc('\n', fp); + std::fflush(fp); + } +}; + +mtp_acc_tracer & mtp_tracer() { + static mtp_acc_tracer t; + return t; +} +} // namespace + +struct common_speculative_state_mtp : public common_speculative_state { + llama_context * ctx_tgt; + llama_seq_id seq_id = 0; // target-side sequence id (set by host, e.g. server slot.id) + int h_idx = -1; // output index in target's last decode for h_prev (-1 = last) + // Adaptive skip after consecutive zero-accept batches: when MTP head consistently + // mispredicts (e.g. on numbers/code/rare tokens during long generation), drafting + // costs ~10ms but yields no accepted tokens. Detect this and fall back to plain + // verify-only for one batch; reset skip on next non-empty accept. + size_t prev_n_acc_drafts = 0; + int zero_accept_streak = 0; + // 0 = disabled (always draft). Set LLAMA_MTP_SKIP_STREAK_THRESHOLD to 1–32 to enable. + int skip_streak_threshold = 0; + // After a skip-streak verify-only batch, do not count the next draft() as another + // zero-accept miss (otherwise threshold==1 skips every round forever — see debug logs). + + bool skip_streak_last_draft = false; + + // Pipeline depth-2: submit at end of iteration via prepare_next(); draft() waits first. + bool has_pending = false; + int32_t pending_n_steps = 0; + common_params_speculative last_spec_params; + + // ---- MTP acceptance tracing (LLAMA_MTP_ACC_TRACE), no behavior change ---- + int trace_iter = 0; + int trace_submit_id_last = -1; // id_last passed to the most recent submit + int trace_submit_h_idx = -1; // h_idx used for h_prev at submit time + llama_pos trace_submit_attn_pos = -1; // attn_pos used at submit time + float trace_submit_h_l2 = 0.0f; // L2 norm of h_prev at submit time + int32_t trace_submit_n_steps = 0; + int trace_last_n_drafted = 0; // drafts.size() returned to caller (for accept pairing) + + static float compute_h_l2(const float * h, int32_t n) { + if (!h || n <= 0) { + return 0.0f; + } + double s = 0.0; + for (int32_t i = 0; i < n; ++i) { + const float v = h[i]; + s += (double) v * (double) v; + } + return (float) std::sqrt(s); + } + + void trace_emit_draft(const llama_tokens & drafts, const char * path) { + trace_last_n_drafted = (int) drafts.size(); + if (!mtp_tracer().enabled) { + return; + } + std::ostringstream oss; + oss << "{\"evt\":\"mtp_draft\"" + << ",\"iter\":" << trace_iter + << ",\"path\":\"" << path << "\"" + << ",\"seq_id\":" << (int) seq_id + << ",\"id_last\":" << trace_submit_id_last + << ",\"h_idx\":" << trace_submit_h_idx + << ",\"attn_pos\":" << (int) trace_submit_attn_pos + << ",\"n_steps\":" << trace_submit_n_steps + << ",\"h_l2\":" << std::fixed << std::setprecision(4) << trace_submit_h_l2 + << ",\"drafts\":["; + for (size_t i = 0; i < drafts.size(); ++i) { + if (i) oss << ','; + oss << (int) drafts[i]; + } + oss << "]}"; + mtp_tracer().writeln(oss.str()); + } + + void trace_emit_accept(int n_accepted) { + if (!mtp_tracer().enabled) { + return; + } + std::ostringstream oss; + oss << "{\"evt\":\"mtp_accept\"" + << ",\"iter\":" << trace_iter + << ",\"n_accepted\":" << n_accepted + << ",\"n_drafted_prev\":" << trace_last_n_drafted + << "}"; + mtp_tracer().writeln(oss.str()); + ++trace_iter; + } + + explicit common_speculative_state_mtp(enum common_speculative_type type, llama_context * ctx_tgt) + : common_speculative_state(type), ctx_tgt(ctx_tgt) { + // MTP reads last backbone hidden from the target; keep embeddings on across decodes. + llama_set_embeddings(ctx_tgt, true); + // Optional: after N consecutive zero-accept MTP batches, skip drafting for one verify-only batch. + if (const char * e = std::getenv("LLAMA_MTP_SKIP_STREAK_THRESHOLD")) { + const int v = std::atoi(e); + if (v >= 1 && v <= 32) { + skip_streak_threshold = v; + } + } + } + + // If a prepare_next() is in flight, block and discard its output (keeps worker contract sane). + void mtp_drain_pending_discard() { + if (!has_pending) { + return; + } + std::vector discard((size_t) pending_n_steps); + const int32_t rc = llama_decode_mtp_wait(ctx_tgt, discard.data(), /*out_h_prev_last*/ nullptr); + if (rc != 0) { + LOG_ERR("%s: llama_decode_mtp_wait (drain) failed (%d)\n", __func__, (int) rc); + } + has_pending = false; + pending_n_steps = 0; + } + + // Projected skip on the next draft() call, without mutating zero_accept_streak. + bool mtp_would_skip_next_draft() const { + if (skip_streak_threshold <= 0) { + return false; + } + if (skip_streak_last_draft) { + return false; + } + const int proj = (n_call_draft > 0) + ? (n_acc_drafts == prev_n_acc_drafts ? zero_accept_streak + 1 : 0) + : zero_accept_streak; + return proj >= skip_streak_threshold; + } + + static int32_t mtp_effective_n_steps(const common_params_speculative & p) { + const int32_t n_steps_raw = p.draft_block_size > 1 ? p.draft_block_size - 1 : 0; + return std::min(n_steps_raw, p.n_max); + } + + void begin(const llama_tokens & prompt) override { + GGML_UNUSED(prompt); + llama_set_embeddings(ctx_tgt, true); + skip_streak_last_draft = false; + // New request / prompt: do not leak in-flight MTP from the previous generation. + mtp_drain_pending_discard(); + } + + void draft( + const common_params_speculative & params, + const llama_tokens & prompt_tgt, + llama_token id_last, + llama_tokens & draft_tokens) override { + + draft_tokens.clear(); + GGML_UNUSED(prompt_tgt); + + const llama_model * model_tgt = llama_get_model(ctx_tgt); + const uint32_t n_bb_u = llama_model_mtp_n_embd_backbone(model_tgt); + if (n_bb_u == 0) { + LOG_ERR("%s: no MTP assistant on target model\n", __func__); + return; + } + const int32_t n_bb = (int32_t) n_bb_u; + + last_spec_params = params; + + // Detect zero-accept of previous draft batch: n_acc_drafts only increments when + // common_speculative_accept is called with n_accepted>0. So if it didn't move + // since our previous draft() return, the previous batch produced 0 accepted drafts. + if (n_call_draft > 0) { + if (skip_streak_last_draft) { + skip_streak_last_draft = false; + } else if (n_acc_drafts == prev_n_acc_drafts) { + ++zero_accept_streak; + } else { + zero_accept_streak = 0; + } + } + // After threshold consecutive misses, skip MTP draft for one batch — drafting + // would cost ~10ms with no benefit; better to let server do a single-token + // verify (baseline path). + if (skip_streak_threshold > 0 && zero_accept_streak >= skip_streak_threshold) { + // Reset streak after one skip; if next batch still misses (streak resumes), we'll skip again. + zero_accept_streak = 0; + skip_streak_last_draft = true; + prev_n_acc_drafts = n_acc_drafts; + mtp_drain_pending_discard(); + trace_submit_id_last = (int) id_last; + trace_submit_n_steps = 0; + trace_emit_draft(draft_tokens, "skip-streak"); + return; // empty draft_tokens — server falls back to single-token verify + } + + int32_t n_steps_raw = params.draft_block_size > 1 ? params.draft_block_size - 1 : 0; + int32_t n_steps = std::min(n_steps_raw, params.n_max); + + if (n_steps <= 0) { + mtp_drain_pending_discard(); + trace_submit_id_last = (int) id_last; + trace_submit_n_steps = 0; + trace_emit_draft(draft_tokens, "skip-nsteps"); + return; + } + + llama_set_embeddings(ctx_tgt, true); + + // Lazy wait: overlap MTP worker with server post-accept work from the previous iteration. + if (has_pending) { + if (pending_n_steps != n_steps) { + mtp_drain_pending_discard(); + // Fall through to synchronous submit below. + } else { + draft_tokens.resize((size_t) n_steps); + const int32_t rc = llama_decode_mtp_wait( + ctx_tgt, draft_tokens.data(), /*out_h_prev_last*/ nullptr); + has_pending = false; + pending_n_steps = 0; + if (rc != 0) { + LOG_ERR("%s: llama_decode_mtp_wait failed (%d)\n", __func__, (int) rc); + draft_tokens.clear(); + } + prev_n_acc_drafts = n_acc_drafts; + // submit-time fields were captured by prepare_next() of previous iter + trace_emit_draft(draft_tokens, "lazy"); + return; + } + } + + std::vector h_prev((size_t) n_bb, 0.0f); + // Use the explicit h_idx pointing at the last accepted output (set by the host after + // sample_and_accept_n). If unset, fall back to -1 (last output) which is correct only + // when the previous decode was prefill or when ALL drafts of the previous batch were + // accepted (otherwise -1 points at a rejected draft's hidden state). + if (float * h_tgt = llama_get_embeddings_ith(ctx_tgt, h_idx)) { + const int32_t n_out_tgt = llama_model_n_embd_out(model_tgt); + const int32_t n_copy = std::min(n_bb, n_out_tgt); + std::memcpy(h_prev.data(), h_tgt, (size_t) n_copy * sizeof(float)); + } + + llama_memory_t mem = llama_get_memory(ctx_tgt); + llama_pos attn_pos = mem ? llama_memory_seq_pos_max(mem, seq_id) : (llama_pos) 0; + if (attn_pos < 0) { + attn_pos = 0; + } + + // Capture submit-time fields for tracing. + trace_submit_id_last = (int) id_last; + trace_submit_h_idx = h_idx; + trace_submit_attn_pos = attn_pos; + trace_submit_n_steps = n_steps; + trace_submit_h_l2 = mtp_tracer().enabled + ? compute_h_l2(h_prev.data(), n_bb) + : 0.0f; + + // Bootstrap path (first iter or after drain): submit and wait immediately on sched_mtp. + draft_tokens.resize((size_t) n_steps); + int32_t rc = llama_decode_mtp_async( + ctx_tgt, + seq_id, + attn_pos, + id_last, + h_prev.data(), + n_steps); + if (rc != 0) { + LOG_ERR("%s: llama_decode_mtp_async failed (%d)\n", __func__, (int) rc); + draft_tokens.clear(); + } else { + rc = llama_decode_mtp_wait(ctx_tgt, draft_tokens.data(), /*out_h_prev_last*/ nullptr); + if (rc != 0) { + LOG_ERR("%s: llama_decode_mtp_wait failed (%d)\n", __func__, (int) rc); + draft_tokens.clear(); + } + } + // Snapshot accepted-draft counter for next call's zero-accept detection. + prev_n_acc_drafts = n_acc_drafts; + trace_emit_draft(draft_tokens, "sync"); + } + + void accept(uint16_t n_accepted) override { + GGML_UNUSED(n_accepted); + } + + void cancel() override { + skip_streak_last_draft = false; + mtp_drain_pending_discard(); + } + + void prepare_next(llama_token id_last) override { + // Kill switch for A/B testing depth-2 vs sync. + static const bool depth2_disabled = []() { + const char * v = std::getenv("LLAMA_PIPELINE_DEPTH2"); + return v && std::strcmp(v, "0") == 0; + }(); + if (depth2_disabled) { + return; + } + const llama_model * model_tgt = llama_get_model(ctx_tgt); + const uint32_t n_bb_u = llama_model_mtp_n_embd_backbone(model_tgt); + if (n_bb_u == 0) { + return; + } + if (has_pending) { + LOG_WRN("%s: MTP prepare_next called while a draft is already pending; ignoring\n", __func__); + return; + } + if (mtp_would_skip_next_draft()) { + return; + } + const int32_t n_steps = mtp_effective_n_steps(last_spec_params); + if (n_steps <= 0) { + return; + } + const int32_t n_bb = (int32_t) n_bb_u; + + std::vector h_prev((size_t) n_bb, 0.0f); + if (float * h_tgt = llama_get_embeddings_ith(ctx_tgt, h_idx)) { + const int32_t n_out_tgt = llama_model_n_embd_out(model_tgt); + const int32_t n_copy = std::min(n_bb, n_out_tgt); + std::memcpy(h_prev.data(), h_tgt, (size_t) n_copy * sizeof(float)); + } + + llama_memory_t mem = llama_get_memory(ctx_tgt); + llama_pos attn_pos = mem ? llama_memory_seq_pos_max(mem, seq_id) : (llama_pos) 0; + if (attn_pos < 0) { + attn_pos = 0; + } + + // Capture submit-time fields for tracing of the upcoming lazy-wait. + trace_submit_id_last = (int) id_last; + trace_submit_h_idx = h_idx; + trace_submit_attn_pos = attn_pos; + trace_submit_n_steps = n_steps; + trace_submit_h_l2 = mtp_tracer().enabled + ? compute_h_l2(h_prev.data(), n_bb) + : 0.0f; + + const int32_t rc = llama_decode_mtp_async( + ctx_tgt, + seq_id, + attn_pos, + id_last, + h_prev.data(), + n_steps); + if (rc != 0) { + LOG_ERR("%s: llama_decode_mtp_async failed (%d)\n", __func__, (int) rc); + return; + } + has_pending = true; + pending_n_steps = n_steps; + } +}; + // state of self-speculation (simple implementation, not ngram-map) struct common_speculative_state_ngram_simple : public common_speculative_state { common_ngram_simple_config config; @@ -786,6 +1234,7 @@ std::string common_speculative_type_to_str(enum common_speculative_type type) { case COMMON_SPECULATIVE_TYPE_NGRAM_MAP_K4V: return "ngram_map_k4v"; case COMMON_SPECULATIVE_TYPE_NGRAM_MOD: return "ngram_mod"; case COMMON_SPECULATIVE_TYPE_NGRAM_CACHE: return "ngram_cache"; + case COMMON_SPECULATIVE_TYPE_MTP: return "mtp"; default: return "unknown"; } } @@ -840,7 +1289,8 @@ common_speculative * common_speculative_init( common_params_speculative & params, llama_context * ctx_tgt) { llama_context * ctx_dft = nullptr; - if (params.model_dft) { + // Gemma4 MTP loads the assistant into the target model (llama_model_load_mtp_from_file); no second context. + if (params.model_dft && params.type != COMMON_SPECULATIVE_TYPE_MTP) { ctx_dft = llama_init_from_model(params.model_dft, params.cparams_dft); if (ctx_dft == nullptr) { LOG_ERR("%s", "failed to create draft context\n"); @@ -848,6 +1298,24 @@ common_speculative * common_speculative_init( } } + if (params.type == COMMON_SPECULATIVE_TYPE_MTP) { + const llama_model * model_tgt = llama_get_model(ctx_tgt); + const llama_model * model_mtp = llama_model_get_mtp_assistant(model_tgt); + + if (!model_mtp) { + LOG_ERR("%s: MTP requires the assistant GGUF loaded into the target (CLI: --spec-type mtp with --mtp-head or --model-draft)\n", __func__); + return nullptr; + } + if (!common_speculative_mtp_arch_ok(model_tgt, model_mtp)) { + LOG_ERR("%s: MTP requires target arch gemma4 and assistant arch gemma4_assistant\n", __func__); + return nullptr; + } + if (!common_speculative_are_compatible_mtp(model_tgt, model_mtp)) { + LOG_ERR("%s: MTP assistant failed vocab compatibility check\n", __func__); + return nullptr; + } + } + // Compute the implementations to use based on the config and their order of preference std::vector configs = {}; // list of speculative configs to try { @@ -859,6 +1327,7 @@ common_speculative * common_speculative_init( bool has_ngram_map_k = (params.type == COMMON_SPECULATIVE_TYPE_NGRAM_MAP_K); bool has_ngram_map_k4v = (params.type == COMMON_SPECULATIVE_TYPE_NGRAM_MAP_K4V); bool has_ngram_mod = (params.type == COMMON_SPECULATIVE_TYPE_NGRAM_MOD); + bool has_mtp = (params.type == COMMON_SPECULATIVE_TYPE_MTP); // In a more complex implementation we could use the same implementation but with different parameters. // This was initially used in PR-18471 but removed to simplify the code. @@ -893,7 +1362,11 @@ common_speculative * common_speculative_init( configs.push_back(common_speculative_config(COMMON_SPECULATIVE_TYPE_NGRAM_CACHE, params)); } if (has_draft) { - configs.push_back(common_speculative_config(COMMON_SPECULATIVE_TYPE_DRAFT, params)); + if (has_mtp) { + configs.push_back(common_speculative_config(COMMON_SPECULATIVE_TYPE_MTP, params)); + } else { + configs.push_back(common_speculative_config(COMMON_SPECULATIVE_TYPE_DRAFT, params)); + } } if (has_draft_eagle3) { configs.push_back(common_speculative_config(COMMON_SPECULATIVE_TYPE_EAGLE3, params)); @@ -919,6 +1392,10 @@ common_speculative * common_speculative_init( impls.push_back(std::make_unique(config.type)); break; } + case COMMON_SPECULATIVE_TYPE_MTP: { + impls.push_back(std::make_unique(config.type, ctx_tgt)); + break; + } case COMMON_SPECULATIVE_TYPE_NGRAM_SIMPLE: { common_ngram_map ngram_map = get_common_ngram_map(config); @@ -980,6 +1457,28 @@ void common_speculative_free(common_speculative * spec) { delete spec; } +void common_speculative_set_seq_id(common_speculative * spec, llama_seq_id seq_id) { + if (spec == nullptr) { + return; + } + for (auto & impl : spec->impls) { + if (impl->type == COMMON_SPECULATIVE_TYPE_MTP) { + static_cast(impl.get())->seq_id = seq_id; + } + } +} + +void common_speculative_set_h_idx(common_speculative * spec, int batch_idx) { + if (spec == nullptr) { + return; + } + for (auto & impl : spec->impls) { + if (impl->type == COMMON_SPECULATIVE_TYPE_MTP) { + static_cast(impl.get())->h_idx = batch_idx; + } + } +} + void common_speculative_begin(common_speculative * spec, const llama_tokens & prompt) { if (spec == nullptr) { return; @@ -1025,6 +1524,13 @@ llama_tokens common_speculative_draft( } void common_speculative_accept(common_speculative * spec, uint16_t n_accepted) { + // Trace BEFORE the early-out: the n_accepted==0 case is a zero-accept event we want to record. + if (mtp_tracer().enabled && spec && spec->curr_impl + && spec->curr_impl->type == COMMON_SPECULATIVE_TYPE_MTP) { + static_cast(spec->curr_impl) + ->trace_emit_accept((int) n_accepted); + } + if (n_accepted == 0) { return; } @@ -1045,6 +1551,24 @@ void common_speculative_accept(common_speculative * spec, uint16_t n_accepted) { } } +void common_speculative_prepare_next(common_speculative * spec, llama_token id_last) { + if (spec == nullptr) { + return; + } + for (auto & impl : spec->impls) { + impl->prepare_next(id_last); + } +} + +void common_speculative_cancel(common_speculative * spec) { + if (spec == nullptr) { + return; + } + for (auto & impl : spec->impls) { + impl->cancel(); + } +} + void common_speculative_print_stats(const common_speculative * spec) { if (spec == nullptr) { return; diff --git a/common/speculative.h b/common/speculative.h index 876cde3d1801..839237f19d4d 100644 --- a/common/speculative.h +++ b/common/speculative.h @@ -27,6 +27,17 @@ void common_speculative_free(common_speculative * spec); // optionally call once at the beginning of a new generation void common_speculative_begin(common_speculative * spec, const llama_tokens & prompt); +// set target-side sequence id used by implementations that read from the target's KV memory +// (currently only used by the MTP implementation; safe no-op for others) +void common_speculative_set_seq_id(common_speculative * spec, llama_seq_id seq_id); + +// Set the output index in the target's most recent decode whose embeddings should be read +// as h_prev for the next MTP draft. -1 means "last output" (default). +// In speculative verification, after partial draft acceptance the last batch output corresponds +// to a rejected draft; the correct h_prev is at the last *accepted* batch index. +// Safe no-op for non-MTP implementations. +void common_speculative_set_h_idx(common_speculative * spec, int batch_idx); + // sample up to n_draft tokens and add them to the batch using the draft model llama_tokens common_speculative_draft( common_speculative * spec, @@ -37,5 +48,15 @@ llama_tokens common_speculative_draft( // informs the speculative decoder that n_accepted tokens were accepted by the target model void common_speculative_accept(common_speculative * spec, uint16_t n_accepted); +// After target sample/accept, submit MTP work for the next iteration so it can overlap +// server bookkeeping until the next common_speculative_draft() (pipeline depth-2, no optimistic token). +// Safe no-op for non-MTP implementations. +void common_speculative_prepare_next(common_speculative * spec, llama_token id_last); + +// Drain any pending async draft from a previous prepare_next() and discard the result. +// MUST be called before the host mutates target KV in a way that would invalidate the +// snapshot (e.g. slot stop / release / new request seq_rm). Safe no-op when nothing is pending. +void common_speculative_cancel(common_speculative * spec); + // print statistics about the speculative decoding void common_speculative_print_stats(const common_speculative * spec); diff --git a/convert_hf_to_gguf.py b/convert_hf_to_gguf.py index f6441b8d2662..264475cd6c2a 100755 --- a/convert_hf_to_gguf.py +++ b/convert_hf_to_gguf.py @@ -780,9 +780,10 @@ def prepare_tensors(self): old_dtype = data_torch.dtype - # convert any unsupported data types to float32 + # convert any unsupported data types to float32 (keep integer buffers integer) if data_torch.dtype not in (torch.float16, torch.float32): - data_torch = data_torch.to(torch.float32) + if "masked_embedding.token_ordering" not in name: + data_torch = data_torch.to(torch.float32) # use the first number-like part of the tensor name as the block id bid = None @@ -796,6 +797,25 @@ def prepare_tensors(self): # data = data_torch.squeeze().numpy() data = data_torch.numpy() + # HF masked_embedding.centroids.weight is numpy-shaped [n_centroids, n_embd]. + # GGUFWriter packs dimensions reversed vs numpy; that yields on-disk / loader dims [n_embd, n_centroids], + # matching llama.cpp create_tensor(..., {n_embd, n_c}). Do not transpose here (transpose breaks loader layout). + if new_name == gguf.TENSOR_NAMES[gguf.MODEL_TENSOR.MTP_CENTROIDS] + ".weight": + data = gguf.LazyNumpyTensor.to_eager(data) + + # Centroid routing: vocab indices must stay I32 (quantize() only supports floats). + if self.match_model_tensor_name(new_name, gguf.MODEL_TENSOR.MTP_TOKEN_ORDERING, bid): + # NOTE: data can be LazyNumpyTensor; astype() on lazy object keeps zero-filled meta. + # Force eager materialization first, then cast. + data_eager = gguf.LazyNumpyTensor.to_eager(data) + data_i32 = np.ascontiguousarray(data_eager.astype(np.int32, copy=False)) + shape_str = f"{{{', '.join(str(n) for n in reversed(data_i32.shape))}}}" + logger.info( + f"{f'%-{max_name_len}s' % f'{new_name},'} {old_dtype} --> I32, shape = {shape_str}" + ) + self.gguf_writer.add_tensor(new_name, data_i32, raw_dtype=gguf.GGMLQuantizationType.I32) + continue + n_dims = len(data.shape) data_qtype: gguf.GGMLQuantizationType | bool = self.tensor_force_quant(name, new_name, bid, n_dims) @@ -1019,11 +1039,20 @@ def __init__(self, *args, **kwargs): else: self.hf_arch = "" + root_model_type = None if "text_config" in self.hparams: - # move the text_config to the root level + # HF nested text_config often sets architectures=null and model_type=gemma4_text, which would + # wipe the wrapper's Gemma4AssistantForCausalLM / gemma4_assistant metadata if merged naively. + root_arch = self.hparams.get("architectures") + root_model_type = self.hparams.get("model_type") self.hparams = {**self.hparams, **self.hparams["text_config"]} + if root_arch is not None and self.hparams.get("architectures") is None: + self.hparams["architectures"] = root_arch - self.block_count = self.find_hparam(["n_layers", "num_hidden_layers", "n_layer", "num_layers"]) + if root_model_type == "gemma4_assistant" and "num_hidden_layers" in self.hparams: + self.block_count = int(self.hparams["num_hidden_layers"]) + else: + self.block_count = self.find_hparam(["n_layers", "num_hidden_layers", "n_layer", "num_layers"]) self.tensor_map = gguf.get_tensor_name_map(self.model_arch, self.block_count) self.rope_parameters = self.hparams.get("rope_parameters", self.hparams.get("rope_scaling")) or {} @@ -7788,6 +7817,67 @@ def modify_tensors(self, data_torch: Tensor, name: str, bid: int | None) -> Iter yield from super().modify_tensors(data_torch, name, bid) +@ModelBase.register("Gemma4AssistantForCausalLM") +class Gemma4AssistantModel(Gemma4Model): + model_arch = gguf.MODEL_ARCH.GEMMA4_ASSISTANT + + def set_gguf_parameters(self): + super().set_gguf_parameters() + hp = self.hparams + self.gguf_writer.add_n_centroids(hp.get("num_centroids", 0)) + self.gguf_writer.add_centroid_top_k(hp.get("centroid_intermediate_top_k", 0)) + self.gguf_writer.add_n_embd_backbone(hp.get("backbone_hidden_size", 0)) + self.gguf_writer.add_use_ordered_embeddings(hp.get("use_ordered_embeddings", False)) + self.gguf_writer.add_attention_k_eq_v(hp.get("attention_k_eq_v", False)) + # MTP head must be paired with this target architecture in llama.cpp + self.gguf_writer.add_requires_target_arch("gemma4") + + def modify_tensors(self, data_torch: Tensor, name: str, bid: int | None) -> Iterable[tuple[str, Tensor]]: + if name.endswith("per_dim_scale") or name.endswith("layer_scalar"): + name = name + ".weight" + + # Tied lm_head shares embed; skip standalone lm_head if present in checkpoint + if bid is None and name.endswith("lm_head.weight"): + return + + # HF layout is [vocab, hidden_size]. Some checkpoints widen hidden to backbone_hidden_size; llama.cpp expects hidden_size (= text_config.hidden_size). + if bid is None and name == "model.embed_tokens.weight" and data_torch.ndim == 2: + hs = int(self.hparams["hidden_size"]) + n_bb = int(self.hparams.get("backbone_hidden_size") or 0) + n_rows, n_cols = data_torch.shape + if n_rows > n_cols: + # [vocab, n_embd] + if n_cols > hs: + logger.warning(f"gemma4_assistant: trimming embed_tokens weight columns {n_cols} -> {hs}") + data_torch = data_torch[:, :hs].contiguous() + elif n_bb > 0 and n_cols > n_rows and n_rows >= n_bb and n_rows > hs: + # Mistaken GGML-ish [n_embd_padded, vocab] + logger.warning(f"gemma4_assistant: trimming embed_tokens weight rows {n_rows} -> {hs}") + data_torch = data_torch[:hs, :].contiguous() + + if name == "pre_projection.weight": + # HF Linear (out, in) = (n_embd, 2*n_bb); GGUF / ggml mul_mat expects w ne [2*n_bb, n_embd] + yield (self.format_tensor_name(gguf.MODEL_TENSOR.MTP_PRE_PROJECTION), data_torch) + return + if name == "post_projection.weight": + # HF (n_bb, n_embd); GGUF ne [n_embd, n_bb] for mul_mat(h, w) with h [n_embd, ...] + yield (self.format_tensor_name(gguf.MODEL_TENSOR.MTP_POST_PROJECTION), data_torch) + return + if name.endswith("masked_embedding.centroids.weight"): + yield (self.format_tensor_name(gguf.MODEL_TENSOR.MTP_CENTROIDS), data_torch) + return + if "masked_embedding.token_ordering" in name: + yield (self.format_tensor_name(gguf.MODEL_TENSOR.MTP_TOKEN_ORDERING), data_torch.to(torch.int32)) + return + + # Gemma4Model.modify_tensors expects a language_model.* prefix on HF names + lm_name = name + if name.startswith("model.") and not name.startswith("language_model."): + lm_name = "language_model." + name + + yield from super().modify_tensors(data_torch, lm_name, bid) + + @ModelBase.register("Gemma4ForConditionalGeneration") class Gemma4VisionAudioModel(MmprojModel): has_audio_encoder = True @@ -13057,6 +13147,8 @@ class LazyTorchTensor(gguf.LazyBase): torch.float16: np.float16, torch.float32: np.float32, torch.uint8: np.uint8, + torch.int32: np.int32, + torch.int64: np.int64, } # only used when byteswapping data. Only correct size is needed diff --git a/docs/development/gemma4-assistant-tensor-inventory.md b/docs/development/gemma4-assistant-tensor-inventory.md new file mode 100644 index 000000000000..cda975e3bdfb --- /dev/null +++ b/docs/development/gemma4-assistant-tensor-inventory.md @@ -0,0 +1,60 @@ +# Gemma 4 Assistant (MTP drafter) — tensor inventory (Stage 0) + +This document records the expected tensor layout for `Gemma4AssistantForCausalLM` / `model_type: gemma4_assistant`, cross-referenced with the MLX-VLM reference implementation (PR #1112). + +## HF config highlights (`google/gemma-4-26B-A4B-it-assistant`) + +- `architectures`: `["Gemma4AssistantForCausalLM"]` +- `model_type`: `gemma4_assistant` +- `backbone_hidden_size`: 2816 (must match paired target Gemma 4 backbone) +- `use_ordered_embeddings`: typically `false` for 26B-A4B / 31B (dense tied LM head); `true` for E2B/E4B (centroid `MaskedEmbedder`) +- `num_centroids`: 2048 (when ordered embeddings) +- `centroid_intermediate_top_k`: 32 +- Nested `text_config`: 4 layers, `layer_types` e.g. three `sliding_attention` + one `full_attention`, `attention_k_eq_v: true`, `vocab_size: 262144`, `hidden_size: 1024`, etc. + +## Expected safetensors names (HF / MLX) + +| Logical component | Typical HF / checkpoint name | GGUF name (this fork) | +|-------------------|------------------------------|------------------------| +| Pre-projection | `pre_projection.weight` | `mtp.pre_projection.weight` | +| Post-projection | `post_projection.weight` | `mtp.post_projection.weight` | +| Token embeddings (inner) | `model.embed_tokens.weight` | `token_embd.weight` | +| Final norm | `model.norm.weight` | `output_norm.weight` | +| Per-layer | `model.layers.{i}.*` | `blk.{i}.*` (via `tensor_mapping.py`) | +| Centroid head (E2B/E4B) | `masked_embedding.centroids.weight` | `mtp.centroids.weight` | +| Token ordering | `masked_embedding.token_ordering` | `mtp.token_ordering` (I32) | +| LM head (if untied) | `lm_head.weight` | `output.weight` | + +## MLX reference files + +- `mlx_vlm/speculative/drafters/gemma4_assistant/gemma4_assistant.py` — forward, `draft_block`, `sanitize` +- `mlx_vlm/speculative/drafters/gemma4_assistant/masked_embedder.py` — sparse centroid LM head +- `mlx_vlm/speculative/drafters/gemma4_assistant/masks.py` — SWA / full masks + +## Notes for llama.cpp port + +1. MTP consumes **target K/V** for the last sliding-attention and last full-attention layers; the assistant has **no independent KV** in the reference design. The initial C++ integration uses the assistant as a loadable arch with standard KV for bring-up; full KV sharing is wired through `--spec-type mtp` and remains dependent on target/draft pairing and cache layout. +2. `attention_k_eq_v: true` maps to missing `blk.*.attn_v.weight` (V taken from K), matching Gemma 4 handling in `gemma4-iswa.cpp`. +3. Greedy parity with the target requires byte-identical drafting; validate with `tests/test-speculative-mtp` when model paths are available. + +## Centroid / ordered embeddings LM head (E2B / E4B) — implemented in MTP + +When `use_ordered_embeddings=true`, the MTP step uses the masked LM head (same idea as HF `Gemma4AssistantMaskedEmbedder` / MLX `MaskedEmbedder`), implemented in `gemma4_mtp_build_one_step()` in `src/models/gemma4-assistant.cpp`: + +1. `centroid_logits = mul_mat(mtp.centroids, h)` → `[n_centroids, n_tokens]`. +2. `top_k(centroid_logits, centroid_intermediate_top_k)` → centroid indices (I32). +3. `token_ordering` is viewed as `[vsc, n_centroids]` with `vsc = n_vocab / n_centroids` so each centroid column lists `vsc` token ids (matches HF flat layout); `get_rows` gathers candidate token ids for the top centroids. +4. `get_rows(token_embd, ids)` then `mul_mat` yields sparse logits over those ids. +5. Full `[n_vocab, n_tokens]` logits are built for host sampling / tracing: base tensor filled with **`-1e30f`**, then **SET_ROWS** writes the sparse logits at the selected ids (MLX-style `min(selected)-1` is optional if parity tests require it). + +### GGUF / converter layout + +- **`mtp.centroids.weight`**: HF checkpoint stores `[n_centroids, n_embd]` rows; numpy is written as-is so that after GGUF’s dimension packing the loader sees **`[n_embd, n_centroids]`**, matching `create_tensor(..., {n_embd, n_c})` and `mul_mat(mtp_centroids, h)` like the dense tied head. +- **`mtp.token_ordering.weight`**: stored as **I32**, length `n_vocab`. + +Verification: `scripts/verify-gemma4-assistant-gguf.py` checks embedding-length parity, and when `use_ordered_embeddings` is true asserts centroid shape, I32 ordering, and `n_vocab % n_centroids == 0`. + +### Tests / scripts + +- Edge smoke (optional): `LLAMA_MTP_TEST_TARGET_EDGE` + `LLAMA_MTP_TEST_HEAD_EDGE` in `tests/test-speculative-mtp.cpp` (paths must exist locally; CI skips when unset). +- Server helpers: `scripts/run-gemma4-e4b-mtp-server.sh`, `scripts/run-gemma4-e2b-mtp-server.sh`. diff --git a/docs/development/pipeline-depth-2-pure-overlap.md b/docs/development/pipeline-depth-2-pure-overlap.md new file mode 100644 index 000000000000..a2052d4e1707 --- /dev/null +++ b/docs/development/pipeline-depth-2-pure-overlap.md @@ -0,0 +1,35 @@ +# Pipeline depth-2: pure MTP overlap (implementation notes) + +## Summary + +`llama-server` overlaps MTP draft compute with per-iteration post-accept work by +splitting `llama_decode_mtp_async` / `llama_decode_mtp_wait` across server loop +iterations: + +1. End of iteration (after `common_speculative_accept`, prompt rollback, `slot.sampled` update, `llama_memory_seq_rm`): `common_speculative_prepare_next(slot.spec, slot.sampled)`. +2. Start of next iteration: `common_speculative_draft` calls `llama_decode_mtp_wait` first when a request is pending. + +No optimistic token is used; drafts always match the last accepted target token. + +## KV safety (Phase 0) + +- `decode_mtp_async` snapshots `h_prev` and uses `attn_pos = seq_pos_max` after `seq_rm`. +- MTP memory context uses `llama_kv_cache_iswa::init_mtp` / `mtp_slot_info` (assistant read path). +- Target `llama_decode` only appends at positions strictly after the rolled-back window; cells at positions `≤ attn_pos` are not rewritten before `_wait`. + +## API + +- `void common_speculative_prepare_next(common_speculative * spec, llama_token id_last);` — no-op for non-MTP implementations. +- Virtual `prepare_next` on `common_speculative_state` (default empty). + +## Bench decision gate + +Plan target: **≥ +15%** median tps vs baseline on f16-mtp short prompt (3 runs), via `.scratch/bench-mtp.sh`. + +**Status**: not executed in this workspace (no GGUF fixtures under `.scratch/`). Run locally after `scripts/run-gemma4-mtp-server.sh` (or your paths), then: + +```bash +bash .scratch/bench-mtp.sh 127.0.0.1:8080 256 +``` + +Compare accept rate (`draft_n` / `draft_n_accepted`) to ensure parity with pre-change runs. diff --git a/docs/speculative.md b/docs/speculative.md index 29da332875f0..5317dc3cf7c1 100644 --- a/docs/speculative.md +++ b/docs/speculative.md @@ -13,6 +13,133 @@ The `llama-server` application supports several implementations of speculative d A much smaller model (called the _draft model_) generates drafts. A draft model is the most used approach in speculative decoding. +### Gemma 4 MTP assistant (`mtp`) + +For **Gemma 4** targets with the **gemma4_assistant** (MTP head) GGUF, use `--spec-type mtp`. The assistant is **not** a second `llama_context`: weights are loaded into the target model via `llama_model_load_mtp_from_file` (done automatically when using the server/CLI init path). Cross-attention in the MTP graph reads **K/V from the target KV cache** (shared full/sliding layers). + +- Prefer **`--mtp-head /path/to/assistant.gguf`** for clarity; **`--model-draft` (`-md`)** is accepted as a backward-compatible alias (same path field). +- The draft block size \(B\) is **`--draft-block-size`** (the head proposes `B - 1` tokens per round; default 4). +- **`--gpu-layers-draft` / `-ngld`** and **`-ctkd` / `-ctvd`** still apply to how the **assistant tensors** are placed and typed when the assistant GGUF is loaded; the target uses `-ngl` and `-ctk`/`-ctv`. + +Example (paths illustrative). **TurboQuant** KV on the target: `-ctk`/`-ctv`. Assistant-side cache types follow the draft flags if you use them for offload/quant selection. + +```sh +llama-server \ + -m /path/to/gemma-4-target.gguf \ + --mtp-head /path/to/gemma-4-assistant.gguf \ + --spec-type mtp \ + --draft-block-size 4 \ + -c 16384 \ + -ngl 99 -ngld 99 \ + -ctk turbo3 -ctv turbo3 \ + -ctkd turbo3 -ctvd turbo3 \ + -fa on \ + --host 127.0.0.1 --port 8080 +``` + +Repo helper (defaults under `.scratch/`): `scripts/run-gemma4-mtp-server.sh`. + +#### Async MTP draft pipeline + +The MTP draft head runs on a dedicated `ggml_backend_sched` (`sched_mtp`) and a +worker thread, isolated from the target's scheduler. This is exposed via two C +APIs: + +```c +LLAMA_API int32_t llama_decode_mtp_async( + struct llama_context * ctx, + llama_seq_id seq_id, + llama_pos attn_pos, + llama_token last_token, + const float * h_prev, + int32_t n_steps); + +LLAMA_API int32_t llama_decode_mtp_wait( + struct llama_context * ctx, + llama_token * out_drafts, + float * out_h_prev_last); +``` + +Contract: + +- At most **one in-flight request per context**. Calling `_async` while a + previous request has not been waited on returns `-7`. +- `h_prev` is copied into the request, so the caller may free or reuse it + immediately after `_async` returns. +- Target KV positions ≤ `attn_pos` must remain stable until `_wait` returns. + The current append-only KV cache satisfies this as long as no eviction or + overlapping `seq_rm` happens between submit and wait. +- `llama_decode_mtp(...)` is preserved as a backward-compatible synchronous + facade (= `_async` immediately followed by `_wait`). + +Why use the async pair? + +- **Graph-cache isolation**: MTP graph reuse is no longer invalidated by target + decode resets. On Gemma 4 + Q4_K_XL, this alone delivered **~+8% throughput** + in single-slot benchmarks (95.3 → 102.8 tps at `--draft-block-size 3`), with + identical accept rate. +- **Pipeline depth-2 (pure overlap, `llama-server`)**: after target + sample/accept and `llama_memory_seq_rm`, the server calls + `common_speculative_prepare_next(spec, last_accepted_token)`, which submits + `llama_decode_mtp_async` for the *next* round. The blocking + `llama_decode_mtp_wait` is deferred to the start of the next + `common_speculative_draft`, so MTP work can overlap with post-accept + bookkeeping and token I/O. This uses the real sampled token (no optimistic + guess). **KV contract**: submit uses `attn_pos` after `seq_rm`; the next + `llama_decode` appends only positions `> attn_pos`, so backbone cells read by + MTP remain stable until `_wait` (append-only cache). Stale in-flight requests + are drained in `common_speculative_begin` and on skip / param-mismatch paths. +- **In-graph argmax**: the MTP graph publishes a `ggml_argmax` of the final + logits (I32 [1]) via `llm_graph_result::get_argmax()`. Per draft step the host + reads back 4 bytes (one token id) instead of the full F32 [n_vocab] logits + row, and the serial CPU argmax over `n_vocab` is gone. The full logits are + still computed in-graph and can be fetched on demand by passing a non-null + `out_logits` to the synchronous `llama_decode_mtp(...)` API (diagnostic / + legacy path). On Gemma 4 + Q4_K_XL this delivered an additional **~+2-3% + throughput** (`109.5 → 112.5 tps` at `n=128`, `95.8 → 97.8 tps` at `n=512`), + with bit-identical greedy drafts. +- **Future work**: optimistic last-token prediction could hide an additional + `llama_decode` latency on hits but risks wrong drafts on misses. + +#### Diagnostic: per-draft acceptance trace + +Set `LLAMA_MTP_ACC_TRACE` to enable a per-iteration NDJSON tracer in +`common/speculative.cpp`. Each `mtp_draft` event records `iter`, `path` +(`sync` / `lazy` / `skip-streak` / `skip-nsteps`), `seq_id`, `id_last`, +`h_idx`, `attn_pos`, `n_steps`, `h_l2` (L2 norm of the input hidden state), +and the drafted token ids; each `mtp_accept` event records the iteration +counter, `n_accepted`, and `n_drafted_prev`. The host can pair them by `iter` +to reconstruct per-position acceptance, MTP h_prev stability, and +selection-bias breakdowns by `h_idx`. + +```sh +# write trace to stderr +LLAMA_MTP_ACC_TRACE=1 ./llama-server [...] +# write trace to a file +LLAMA_MTP_ACC_TRACE=/tmp/mtp.ndjson ./llama-server [...] +``` + +#### Throughput tuning (Edge assistants / heavy backends) + +Ordered-embeddings MTP (`use_ordered_embeddings`) runs a centroid LM head (`top_k`, routed `get_rows`) per draft step; the greedy graph still materializes a full-vocab logits row (masked fill + scatter) so argmax and optional full-row export stay consistent with verify. + +- **`LLAMA_MTP_SKIP_STREAK_THRESHOLD`**: **off by default** (`0` / unset). If set to `1`–`32`, after that many consecutive batches with **zero** accepted **MTP** drafts, MTP drafting is skipped for one verify-only batch, then retried. +- Helper scripts `scripts/run-gemma4-e4b-mtp-server.sh` and `scripts/run-gemma4-e2b-mtp-server.sh` support **`MTP_PRESET`**: `throughput` (block 2 / max 6), `lift` (block 3 / max 8), `balanced`, or `quality`. Override with `DRAFT_BLOCK_SIZE`, `DRAFT_MAX`, and optionally `LLAMA_MTP_SKIP_STREAK_THRESHOLD`. + +Disabled at zero overhead (no env var or value `0` / empty); when enabled the +extra cost is one `n_bb`-wide L2 reduction per draft and a small NDJSON write. + +#### Reconverting `gemma4_assistant` from Hugging Face + +Example (paths relative to repo root): + +```sh +python convert_hf_to_gguf.py .scratch/gemma-4-26B-A4B-it-assistant \ + --outfile .scratch/gemma-assistant-mtp.gguf --outtype f16 +``` + +Use the resulting GGUF as `--mtp-head` (or `-md`) with `--spec-type mtp`. Older assistant GGUFs with `token_embd.weight` first axis 2816 (backbone width) instead of 1024 will fail load; run `scripts/verify-gemma4-assistant-gguf.py` on the file to check. + ### n-gram Cache (`ngram-cache`) An n-gram is a sequence of n tokens. The n-gram cache implementation maintains statistics about short n-gram sequences. diff --git a/gguf-py/gguf/constants.py b/gguf-py/gguf/constants.py index 997744f34c34..678a0060ed83 100644 --- a/gguf-py/gguf/constants.py +++ b/gguf-py/gguf/constants.py @@ -153,6 +153,12 @@ class LLM: DENSE_FEAT_IN_SIZE = "{arch}.{dense}_feat_in" DENSE_FEAT_OUT_SIZE = "{arch}.{dense}_feat_out" + N_CENTROIDS = "{arch}.n_centroids" + CENTROID_TOP_K = "{arch}.centroid_top_k" + N_EMBD_BACKBONE = "{arch}.n_embd_backbone" + USE_ORDERED_EMBEDDINGS = "{arch}.use_ordered_embeddings" + REQUIRES_TARGET_ARCH = "{arch}.requires_target_arch" + class Attention: HEAD_COUNT = "{arch}.attention.head_count" HEAD_COUNT_KV = "{arch}.attention.head_count_kv" @@ -181,6 +187,7 @@ class Attention: KEY_LENGTH_SWA = "{arch}.attention.key_length_swa" VALUE_LENGTH_SWA = "{arch}.attention.value_length_swa" SHARED_KV_LAYERS = "{arch}.attention.shared_kv_layers" + K_EQ_V = "{arch}.attention.k_eq_v" SLIDING_WINDOW_PATTERN = "{arch}.attention.sliding_window_pattern" TEMPERATURE_SCALE = "{arch}.attention.temperature_scale" @@ -420,6 +427,7 @@ class MODEL_ARCH(IntEnum): GEMMA3 = auto() GEMMA3N = auto() GEMMA4 = auto() + GEMMA4_ASSISTANT = auto() GEMMA_EMBEDDING = auto() STARCODER2 = auto() RWKV6 = auto() @@ -842,6 +850,11 @@ class MODEL_TENSOR(IntEnum): NEXTN_HNORM = auto() NEXTN_SHARED_HEAD_HEAD = auto() NEXTN_SHARED_HEAD_NORM = auto() + # Gemma 4 MTP assistant (GGUF names: mtp.*) + MTP_PRE_PROJECTION = auto() + MTP_POST_PROJECTION = auto() + MTP_CENTROIDS = auto() + MTP_TOKEN_ORDERING = auto() # lfm2 audio A_ENC_NORM_CONV = auto() A_ENC_LINEAR_POS = auto() @@ -905,6 +918,7 @@ class MODEL_TENSOR(IntEnum): MODEL_ARCH.GEMMA3: "gemma3", MODEL_ARCH.GEMMA3N: "gemma3n", MODEL_ARCH.GEMMA4: "gemma4", + MODEL_ARCH.GEMMA4_ASSISTANT: "gemma4_assistant", MODEL_ARCH.GEMMA_EMBEDDING: "gemma-embedding", MODEL_ARCH.STARCODER2: "starcoder2", MODEL_ARCH.RWKV6: "rwkv6", @@ -1337,6 +1351,10 @@ class MODEL_TENSOR(IntEnum): MODEL_TENSOR.NEXTN_HNORM: "blk.{bid}.nextn.hnorm", MODEL_TENSOR.NEXTN_SHARED_HEAD_HEAD: "blk.{bid}.nextn.shared_head_head", MODEL_TENSOR.NEXTN_SHARED_HEAD_NORM: "blk.{bid}.nextn.shared_head_norm", + MODEL_TENSOR.MTP_PRE_PROJECTION: "mtp.pre_projection", + MODEL_TENSOR.MTP_POST_PROJECTION: "mtp.post_projection", + MODEL_TENSOR.MTP_CENTROIDS: "mtp.centroids", + MODEL_TENSOR.MTP_TOKEN_ORDERING: "mtp.token_ordering", } MODEL_TENSORS: dict[MODEL_ARCH, list[MODEL_TENSOR]] = { @@ -2366,6 +2384,26 @@ class MODEL_TENSOR(IntEnum): MODEL_TENSOR.PER_LAYER_PROJ_NORM, MODEL_TENSOR.PER_LAYER_POST_NORM, ], + MODEL_ARCH.GEMMA4_ASSISTANT: [ + MODEL_TENSOR.ROPE_FREQS, + MODEL_TENSOR.TOKEN_EMBD, + MODEL_TENSOR.MTP_PRE_PROJECTION, + MODEL_TENSOR.MTP_POST_PROJECTION, + MODEL_TENSOR.MTP_CENTROIDS, + MODEL_TENSOR.MTP_TOKEN_ORDERING, + MODEL_TENSOR.OUTPUT_NORM, + MODEL_TENSOR.ATTN_Q, + MODEL_TENSOR.ATTN_Q_NORM, + MODEL_TENSOR.ATTN_OUT, + MODEL_TENSOR.FFN_GATE, + MODEL_TENSOR.FFN_DOWN, + MODEL_TENSOR.FFN_UP, + MODEL_TENSOR.ATTN_NORM, + MODEL_TENSOR.ATTN_POST_NORM, + MODEL_TENSOR.FFN_PRE_NORM, + MODEL_TENSOR.FFN_POST_NORM, + MODEL_TENSOR.LAYER_OUT_SCALE, + ], MODEL_ARCH.GEMMA_EMBEDDING: [ MODEL_TENSOR.TOKEN_EMBD, MODEL_TENSOR.OUTPUT, diff --git a/gguf-py/gguf/gguf_writer.py b/gguf-py/gguf/gguf_writer.py index 90d500dc7719..d166db415352 100644 --- a/gguf-py/gguf/gguf_writer.py +++ b/gguf-py/gguf/gguf_writer.py @@ -799,6 +799,24 @@ def add_clamp_kqv(self, value: float) -> None: def add_shared_kv_layers(self, value: int) -> None: self.add_uint32(Keys.Attention.SHARED_KV_LAYERS.format(arch=self.arch), value) + def add_n_centroids(self, value: int) -> None: + self.add_uint32(Keys.LLM.N_CENTROIDS.format(arch=self.arch), value) + + def add_centroid_top_k(self, value: int) -> None: + self.add_uint32(Keys.LLM.CENTROID_TOP_K.format(arch=self.arch), value) + + def add_n_embd_backbone(self, value: int) -> None: + self.add_uint32(Keys.LLM.N_EMBD_BACKBONE.format(arch=self.arch), value) + + def add_use_ordered_embeddings(self, value: bool) -> None: + self.add_bool(Keys.LLM.USE_ORDERED_EMBEDDINGS.format(arch=self.arch), value) + + def add_attention_k_eq_v(self, value: bool) -> None: + self.add_bool(Keys.Attention.K_EQ_V.format(arch=self.arch), value) + + def add_requires_target_arch(self, value: str) -> None: + self.add_string(Keys.LLM.REQUIRES_TARGET_ARCH.format(arch=self.arch), value) + # if input is array, true means SWA and false means full_attention for each layer def add_sliding_window_pattern(self, value: int | Sequence[bool]) -> None: key = Keys.Attention.SLIDING_WINDOW_PATTERN.format(arch=self.arch) diff --git a/gguf-py/gguf/tensor_mapping.py b/gguf-py/gguf/tensor_mapping.py index 01a9b236000b..68e6c7d32551 100644 --- a/gguf-py/gguf/tensor_mapping.py +++ b/gguf-py/gguf/tensor_mapping.py @@ -2138,6 +2138,19 @@ class TensorNameMap: MODEL_TENSOR.NEXTN_SHARED_HEAD_NORM: ( "model.layers.{bid}.shared_head.norm", ), + + MODEL_TENSOR.MTP_PRE_PROJECTION: ( + "pre_projection", + ), + MODEL_TENSOR.MTP_POST_PROJECTION: ( + "post_projection", + ), + MODEL_TENSOR.MTP_CENTROIDS: ( + "masked_embedding.centroids", + ), + MODEL_TENSOR.MTP_TOKEN_ORDERING: ( + "masked_embedding.token_ordering", + ), } # architecture-specific block mappings diff --git a/include/llama.h b/include/llama.h index d4211d3c47b1..6807fdf42cfe 100644 --- a/include/llama.h +++ b/include/llama.h @@ -492,6 +492,20 @@ extern "C" { size_t n_paths, struct llama_model_params params); + // Gemma 4 MTP: load gemma4_assistant GGUF into a gemma4 target (call after llama_model_load_from_file, before llama_init_from_model). + // Returns 0 on success. + LLAMA_API int llama_model_load_mtp_from_file( + struct llama_model * model, + const char * path_mtp, + struct llama_model_params params); + + LLAMA_API const struct llama_model * llama_model_get_mtp_assistant(const struct llama_model * model); + + LLAMA_API bool llama_model_has_mtp_assistant(const struct llama_model * model); + + // Backbone hidden size for MTP input (0 if no MTP assistant is loaded). + LLAMA_API uint32_t llama_model_mtp_n_embd_backbone(const struct llama_model * model); + LLAMA_API void llama_model_save_to_file( const struct llama_model * model, const char * path_model); @@ -587,6 +601,16 @@ extern "C" { // Returns label of classifier output by index (