4.5 KiB
4.5 KiB
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_assistantbackbone_hidden_size: 2816 (must match paired target Gemma 4 backbone)use_ordered_embeddings: typicallyfalsefor 26B-A4B / 31B (dense tied LM head);truefor E2B/E4B (centroidMaskedEmbedder)num_centroids: 2048 (when ordered embeddings)centroid_intermediate_top_k: 32- Nested
text_config: 4 layers,layer_typese.g. threesliding_attention+ onefull_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,sanitizemlx_vlm/speculative/drafters/gemma4_assistant/masked_embedder.py— sparse centroid LM headmlx_vlm/speculative/drafters/gemma4_assistant/masks.py— SWA / full masks
Notes for llama.cpp port
- 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 mtpand remains dependent on target/draft pairing and cache layout. attention_k_eq_v: truemaps to missingblk.*.attn_v.weight(V taken from K), matching Gemma 4 handling ingemma4-iswa.cpp.- Greedy parity with the target requires byte-identical drafting; validate with
tests/test-speculative-mtpwhen 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:
centroid_logits = mul_mat(mtp.centroids, h)→[n_centroids, n_tokens].top_k(centroid_logits, centroid_intermediate_top_k)→ centroid indices (I32).token_orderingis viewed as[vsc, n_centroids]withvsc = n_vocab / n_centroidsso each centroid column listsvsctoken ids (matches HF flat layout);get_rowsgathers candidate token ids for the top centroids.get_rows(token_embd, ids)thenmul_matyields sparse logits over those ids.- 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-stylemin(selected)-1is 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], matchingcreate_tensor(..., {n_embd, n_c})andmul_mat(mtp_centroids, h)like the dense tied head.mtp.token_ordering.weight: stored as I32, lengthn_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_EDGEintests/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.