Gemma 4 E2B MTP drafter, fine-tuned for Turkish medical text
This is a fine-tuned version of google/gemma-4-E2B-it-assistant, the speculative-decoding (MTP) drafter that ships with Gemma 4 E2B. Use it as the assistant model for google/gemma-4-E2B-it.
What it does: it makes Gemma 4 E2B generate Turkish medical text about 1.7x faster than plain decoding. With the original drafter the speedup was about 1.2x. Other domains do not get worse.
What it does not do: it does not change what the model says. In speculative decoding the target model checks every drafted token, so the output is the same as running gemma-4-E2B-it alone. This is a speed add-on and not a medical model. The target model was not modified.
Results
Speedup is measured with HF generate, greedy decoding, batch size 1, 128 new tokens, on an A100-80GB with bf16. It is compared with the same target without an assistant. τ is the number of tokens accepted per target forward pass. Brackets show 95% bootstrap confidence intervals over prompts.
| Test set (prompts) | Original drafter, K=4 | This drafter, K=4 | Original, K=2 | This drafter, K=2 |
|---|---|---|---|---|
| Turkish medical, fully held-out (20) | 1.15x (τ 1.73) | 1.66x [1.58, 1.76] (τ 2.54) | 1.24x | 1.64x |
| Turkish medical (48) | 1.17x (τ 1.78) | 1.73x [1.68, 1.79] (τ 2.65) | 1.26x | 1.66x |
| Turkish general (32) | 1.24x | 1.44x | 1.31x | 1.47x |
| English medical (24) | 1.46x | 1.65x | 1.46x | 1.58x |
| English general (24) | 1.49x | 1.48x | 1.48x | 1.49x |
K is the number of draft tokens per step. K=4 is recommended.
Teacher-forced acceptance on held-out responses: τ̂ at K=4, averaged over 3 seeds. The seed-to-seed std is about 0.001.
| Test set | Original | This drafter | Difference [95% CI] |
|---|---|---|---|
| Turkish medical, held-out set | 1.905 | 2.544 | +0.64 [+0.54, +0.74] |
| Turkish general, held-out set | 2.049 | 2.243 | +0.20 [+0.16, +0.22] |
| English medical, held-out set | 2.199 | 2.344 | +0.15 [+0.10, +0.20] |
| English general, held-out set | 2.423 | 2.424 | +0.00 [−0.02, +0.02] |
Lossless check: on 16 Turkish medical prompts at K=2 and K=4 in fp32 with TF32 off, the output with this drafter is identical token by token to the output without it in 100% of cases. In bf16 a fraction of outputs diverge after a few dozen tokens. That divergence happens at exactly the same rate and positions with the original drafter, so it is bf16 numerical noise and not caused by the drafter.
Why the original drafter is slow on Turkish
The Gemma 4 drafter does not score the full 262k vocabulary. An ordered embedding head first picks the top 32 of 2,048 token clusters, then scores only the tokens in those clusters, which is about 1.6% of the vocabulary. If the target's next token is outside the selected clusters, the drafter cannot predict it.
On Turkish, the correct token was inside the selected clusters only 83% of the time, compared with 92–94% on English. Fine-tuning the cluster selector together with the drafter raises this to 96% for Turkish medical text. That one change gives most of the gain:
| Variant (3 seeds each) | Turkish medical held-out τ̂ (K=4) | Cluster recall |
|---|---|---|
| Original | 1.905 | 82.9% |
| Fine-tune drafter body, KL to target | 2.232 | 85.3% |
| + train cluster selector | 2.470 | 95.5% |
| + train lm_head (this model) | 2.544 | 95.4% |
Training
- Data: about 16,500 prompt–response pairs (Turkish medical 10,000, Turkish general 4,500, English medical 1,000, English general 1,000). The responses are greedy generations from
gemma-4-E2B-ititself (self-distillation, at most 512 tokens), so the drafter learns the target's own distribution. - Prompts:
- Turkish patient questions from the
kayrab/patient-doctor-qa-tr-*datasets. Only the questions were used, with PII-like strings removed; the original doctor answers were not used. - Turkish and English medical and general questions synthesized by the E2B model.
- A mix of all four categories prevented regressions. Training on Turkish medical data alone made English medical slightly worse.
- Turkish patient questions from the
- Held-out sets:
- The "held-out" test sets are hand-written prompts from a separate earlier set, never seen in training.
- The other test sets are held-out questions from the same sources as training.
- Objective: forward KL from the drafter to the target's full next-token distribution, unrolled over 3 draft steps with teacher forcing, plus a KL term on the cluster selector (the target's probabilities summed per cluster, weight 0.5).
- Setup:
- The target is frozen.
- The drafter cross-attends to the target's shared KV cache, exactly as at inference.
- AdamW, lr 5e-5, 2 epochs (1090 steps), cosine schedule, bf16 autocast, one A100-80GB.
Usage
import torch
from transformers import AutoProcessor, AutoModelForCausalLM
target_id, drafter_id = "google/gemma-4-E2B-it", "senpaicq/gemma-4-E2B-it-assistant-tr-medical"
processor = AutoProcessor.from_pretrained(target_id)
target = AutoModelForCausalLM.from_pretrained(target_id, dtype=torch.bfloat16, device_map="auto")
assistant = AutoModelForCausalLM.from_pretrained(drafter_id, dtype=torch.bfloat16, device_map="auto")
assistant.generation_config.num_assistant_tokens = 4
assistant.generation_config.num_assistant_tokens_schedule = "constant"
messages = [{"role": "user", "content": [{"type": "text", "text": "Tip 2 diyabet hastaları sabah kahvaltısında nelere dikkat etmeli?"}]}]
inputs = processor.apply_chat_template(messages, add_generation_prompt=True, tokenize=True,
return_dict=True, return_tensors="pt").to(target.device)
out = target.generate(**inputs, assistant_model=assistant, max_new_tokens=512, do_sample=False)
print(processor.decode(out[0, inputs["input_ids"].shape[1]:], skip_special_tokens=True))
Tested with transformers==5.21.1.
Limitations
- The speedups above were measured only in HF
transformers. Serving engines such as vLLM and SGLang have different overheads, so wall-clock gains there will differ. τ (acceptance length) transfers better than wall-clock speedup. - Only tested with Gemma 4 E2B. It will not work with other Gemma 4 sizes, because the drafter reads the target's hidden states and KV cache.
- Tested with greedy decoding and batch size 1.
- Gains for other Turkish domains (law, finance, …) are expected to fall between the Turkish general and Turkish medical numbers, but were not measured.
- The outputs are Gemma 4 E2B's outputs. Do not use them as medical advice.
Files
model.safetensors,config.json,generation_config.json: the drafter. It is a drop-in replacement forgoogle/gemma-4-E2B-it-assistant.train_cfg.json: the training configuration.eval_results.json: all evaluation numbers, including per-arm, per-seed and paired-bootstrap results.notebooks/: data generation, training/ablation and robustness notebooks, with outputs stripped.
The training data is not released because it contains real patient questions.