Downloads · 30 days
18
12% of all-time downloads
lainlives/gemma-4-E4B-mtp-drafter
gemma-4-E4B-mtp-drafter is a text generation model from lainlives. Use it when you need the model to write or continue text. It is set up for transformers. The card lists the license as other.
First public extraction of Google Gemma 4's Multi-Token Prediction (MTP) drafter weights from LiteRT format into standard PyTorch safetensors.
Downloads · 30 days
18
12% of all-time downloads
All-time downloads
148
Public
Repo size
312 MB
Likes
0
Public
Click a slice to open those files.
.safetensors312 MB · 100%
From the Hugging Face model README
First public extraction of Google Gemma 4's Multi-Token Prediction (MTP) drafter weights from LiteRT format into standard PyTorch safetensors.
Google trained all Gemma 4 variants with MTP heads for built-in speculative decoding, then deliberately stripped them from the public HuggingFace release, retaining them exclusively in LiteRT on-device format. This repository provides the extracted drafter as a standard PyTorch module with a working speculative decoding implementation.
| Metric | Value |
|---|---|
| Step-0 top-1 acceptance (greedy) | 35% |
| Step-0 top-5 overlap with base model | ~80% |
| Architecture fully reconstructed | 42/42 tensors, all shapes verified |
| Outputs correct | Yes — coherent, accurate text on all test prompts |
| INT4 nibble order | low_first confirmed |
| Attention scaling | None (QK-norm with scale=1.0, confirmed correct) |
The 35% top-1 acceptance from dequantized INT4/INT8 mobile weights is the expected ceiling — quantization noise is irreversible. The 80% top-5 overlap makes these weights immediately useful for:
The MTP drafter is a lightweight 4-layer transformer (78M parameters) designed as a one-step recurrent cell:
Input: concat(token_embedding, projected_activation) [5120]
→ Linear pre-projection [5120 → 256]
→ 4 transformer blocks (256 hidden, 2048 intermediate, GeGLU MLP)
Blocks 0-2: sliding attention (window=512), head_dim=256, 4Q/2KV heads
Block 3: full attention, head_dim=512, 4Q/2KV heads
→ RMSNorm → parallel heads:
LM head: [256 → 262144] with tanh soft-cap at 30.0
Post-proj: [256 → 2560] (recurrent: fed back as next step's activation)
Critical architectural details:
1/sqrt(d) attention scaling — Gemma 4 uses QK-norm with attention_scale=1.0. Adding traditional scaling degrades accuracy (top-5 drops from 80% to 20%). The fixed-scale query norms (local=0.9916, global=1.0228) serve as the scaling mechanism.| File | Description |
|---|---|
mtp_drafter.safetensors | Extracted weights (297 MB, fp32, 42 tensors, 78M params) |
mtp_drafter_config.json | Architecture configuration |
gemma4_litert_mtp/ | Python package: PyTorch module, HF patches, speculative generation |
import torch
from transformers import AutoModelForConditionalGeneration, AutoTokenizer
from safetensors.torch import load_file
from gemma4_litert_mtp import Gemma4LiteRtMtpDrafter, LiteRtMtpDrafterConfig
from gemma4_litert_mtp.speculative_generate import Gemma4MtpSpeculativeGenerator
# Load base model (BF16 required — FP16 will degenerate)
model = AutoModelForConditionalGeneration.from_pretrained(
"google/gemma-4-E4B-it", torch_dtype=torch.bfloat16, device_map="auto"
)
tokenizer = AutoTokenizer.from_pretrained("google/gemma-4-E4B-it")
# Load extracted MTP drafter
config = LiteRtMtpDrafterConfig.from_json_file("mtp_drafter_config.json")
drafter = Gemma4LiteRtMtpDrafter(config)
drafter.load_state_dict(load_file("mtp_drafter.safetensors"), strict=True)
drafter = drafter.to(dtype=torch.bfloat16, device=model.device).eval()
# Generate with speculative decoding
generator = Gemma4MtpSpeculativeGenerator(model, drafter, tokenizer)
result = generator.generate("What is the capital of France?")
print(result.text) # "The capital of France is **Paris**."
print(f"Acceptance: {result.acceptance_rate:.0%}")
Important: Gemma 4 E4B is instruction-tuned. Always use the chat template (the generator handles this automatically). Raw prompts without the template produce degenerate repetitive output.
| Draft Steps | Step-0 Acceptance | Overall Acceptance | Notes |
|---|---|---|---|
| 1 | 35% | 35% | Best standalone config |
| 2 | 35% | 22% | Steps 1+ degrade (stale KV) |
| 3 | 35% | 14% | Further degradation |
In a Python speculative decoding loop, each cycle requires ≥2 base model forward passes (verify + state update). At 35% acceptance, expected tokens per cycle = 1.35, yielding ~0.7x — slower than autoregressive. This is not a flaw in the extraction; it's an inherent limitation of Python-level speculative decoding loops.
Production serving engines (vLLM, SGLang) eliminate this overhead via fused kernels that score draft tokens within the base model's forward pass. The 35% acceptance rate would deliver meaningful speedup there. The 80% top-5 overlap is particularly valuable for tree-based speculative methods that verify multiple candidates per position.
gemma-4-E4B-it.litertlm (3.5 GB) from litert-community/gemma-4-E4B-it-litert-lmtflite Python packagelow_first) to float32Architecture reverse-engineered from three sources: the .tflite binary (weights/shapes), graph JSON (operator topology), and the LiteRT-LM C++ runner (external interface and loop semantics).
# Extract MTP shard from .litertlm
python extract_mtp_from_litertlm.py --input gemma-4-E4B-it.litertlm --output-dir ./extracted
# Convert to safetensors using pre-built mapping
python convert_with_prebuilt_mapping.py --tflite ./extracted/section9_mtp_drafter.tflite --output-dir ./drafter
# Validate
python validate_drafter.py ./drafter
Quantization ceiling. The LiteRT drafter was INT4/INT8 quantized for mobile deployment. Dequantization cannot recover the original BF16 training precision. Step-0 acceptance is 35% (top-1) vs the theoretical ~80%+ achievable with full-precision weights.
E4B only. Google has not released LiteRT models for Gemma 4 26B MoE or 31B dense. The extraction pipeline generalizes with config changes if they do.
Standalone Python loop is slower than autoregressive. The per-cycle overhead of 2+ forward passes makes Python-level speculative decoding net-negative at 35% acceptance. Integration into vLLM/SGLang (which amortize verification into the base forward pass) is needed for real speedup.
Multi-step degradation. Steps 1+ use the drafter's own projected activations with stale KV caches, degrading rapidly. The drafter is most effective as a 1-step predictor.
mirifiuto independently extracted the same drafter and confirmed a working forward pass. Their architecture findings converge with ours, with one correction: layer 3 has 4 heads at head_dim=512 (not 8 heads at head_dim=256 as initially reported).
Extracted weights are derived from Google's Gemma 4 E4B model — usage subject to the Gemma Terms of Use. Extraction code and PyTorch module are Apache 2.0.