Enable Gemma 4 MTP (assistant_model) for faster decode
Browse files
app.py
CHANGED
|
@@ -15,7 +15,12 @@ import gradio as gr
|
|
| 15 |
import torch
|
| 16 |
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
|
| 17 |
|
| 18 |
-
MODEL_ID = os.environ.get("MODEL_ID", "google/gemma-4-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
|
| 21 |
model = AutoModelForCausalLM.from_pretrained(
|
|
@@ -25,6 +30,15 @@ model = AutoModelForCausalLM.from_pretrained(
|
|
| 25 |
)
|
| 26 |
model.eval()
|
| 27 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
DEFAULT_SYSTEM = (
|
| 29 |
"You are a host on 'Small Talk', a live AI-to-AI robot podcast hosted by "
|
| 30 |
"Reachy Mini robots. Stay fully in character. Keep every reply short, witty "
|
|
@@ -66,6 +80,7 @@ def chat(message, history, system_prompt, temperature, max_new_tokens):
|
|
| 66 |
streamer = TextIteratorStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)
|
| 67 |
gen_kwargs = dict(
|
| 68 |
**inputs,
|
|
|
|
| 69 |
streamer=streamer,
|
| 70 |
max_new_tokens=int(max_new_tokens),
|
| 71 |
do_sample=temperature > 0,
|
|
@@ -91,8 +106,9 @@ demo = gr.ChatInterface(
|
|
| 91 |
title="Small Talk · Gemma 4 12B brain",
|
| 92 |
description=(
|
| 93 |
"The live-banter brain for the Small Talk robot podcast — Gemma 4 12B "
|
| 94 |
-
"
|
| 95 |
-
"voice a character. Callable as an API
|
|
|
|
| 96 |
),
|
| 97 |
)
|
| 98 |
|
|
|
|
| 15 |
import torch
|
| 16 |
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
|
| 17 |
|
| 18 |
+
MODEL_ID = os.environ.get("MODEL_ID", "google/gemma-4-12B-it")
|
| 19 |
+
# Gemma 4's official MTP drafter — a ~0.8GB multi-token-prediction head. Passing
|
| 20 |
+
# it as `assistant_model` to generate() is "all you need to enable MTP"
|
| 21 |
+
# (https://ai.google.dev/gemma/docs/mtp/mtp): the target verifies several draft
|
| 22 |
+
# tokens per forward pass, so we run far fewer 12B passes per reply.
|
| 23 |
+
ASSISTANT_ID = os.environ.get("ASSISTANT_ID", "google/gemma-4-12B-it-assistant")
|
| 24 |
|
| 25 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
|
| 26 |
model = AutoModelForCausalLM.from_pretrained(
|
|
|
|
| 30 |
)
|
| 31 |
model.eval()
|
| 32 |
|
| 33 |
+
assistant_model = AutoModelForCausalLM.from_pretrained(
|
| 34 |
+
ASSISTANT_ID,
|
| 35 |
+
dtype=torch.bfloat16,
|
| 36 |
+
device_map="auto",
|
| 37 |
+
)
|
| 38 |
+
assistant_model.eval()
|
| 39 |
+
assistant_model.generation_config.num_assistant_tokens = 4
|
| 40 |
+
assistant_model.generation_config.num_assistant_tokens_schedule = "heuristic"
|
| 41 |
+
|
| 42 |
DEFAULT_SYSTEM = (
|
| 43 |
"You are a host on 'Small Talk', a live AI-to-AI robot podcast hosted by "
|
| 44 |
"Reachy Mini robots. Stay fully in character. Keep every reply short, witty "
|
|
|
|
| 80 |
streamer = TextIteratorStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)
|
| 81 |
gen_kwargs = dict(
|
| 82 |
**inputs,
|
| 83 |
+
assistant_model=assistant_model, # ← MTP / multi-token prediction
|
| 84 |
streamer=streamer,
|
| 85 |
max_new_tokens=int(max_new_tokens),
|
| 86 |
do_sample=temperature > 0,
|
|
|
|
| 106 |
title="Small Talk · Gemma 4 12B brain",
|
| 107 |
description=(
|
| 108 |
"The live-banter brain for the Small Talk robot podcast — Gemma 4 12B "
|
| 109 |
+
"on ZeroGPU, sped up with official **MTP** (multi-token prediction). Pass "
|
| 110 |
+
"a persona as the system prompt to voice a character. Callable as an API "
|
| 111 |
+
"by the podcast backend."
|
| 112 |
),
|
| 113 |
)
|
| 114 |
|