Files changed (3) hide show
  1. README.md +64 -88
  2. chat_template.jinja +38 -73
  3. tokenizer_config.json +0 -46
README.md CHANGED
@@ -16,21 +16,20 @@ base_model:
16
  <a href="https://huggingface.co/collections/google/gemma-4" target="_blank">Hugging Face</a> |
17
  <a href="https://github.com/google-gemma" target="_blank">GitHub</a> |
18
  <a href="https://blog.google/innovation-and-ai/technology/developers-tools/gemma-4/" target="_blank">Launch Blog</a> |
19
- <a href="https://ai.google.dev/gemma/docs/core" target="_blank">Documentation</a> |
20
- <a href="https://arxiv.org/abs/2607.02770" target="_blank">Technical Report</a>
21
  <br>
22
  <b>License</b>: <a href="https://ai.google.dev/gemma/docs/gemma_4_license" target="_blank">Apache 2.0</a> | <b>Authors</b>: <a href="https://deepmind.google/models/gemma/" target="_blank">Google DeepMind</a>
23
  </p>
24
 
25
- Gemma is a family of open models built by Google DeepMind. Gemma 4 models are multimodal, handling text and image input (with audio supported on E2B, E4B, and 12B) and generating text output. This release includes open-weights models in both pre-trained and instruction-tuned variants. Gemma 4 features a context window of up to 256K tokens and maintains multilingual support in over 140 languages.
26
 
27
- Featuring both Dense and Mixture-of-Experts (MoE) architectures, Gemma 4 is well-suited for tasks like text generation, coding, and reasoning. The models are available in five distinct sizes: **E2B**, **E4B**, **12B**, **26B A4B**, and **31B**. Their diverse sizes make them deployable in environments ranging from high-end phones to laptops and servers, democratizing access to state-of-the-art AI.
28
 
29
  Gemma 4 introduces key **capability and architectural advancements**:
30
 
31
  * **Reasoning** – All models in the family are designed as highly capable reasoners, with configurable thinking modes.
32
 
33
- * **Extended Multimodalities** – Processes Text, Image with variable aspect ratio and resolution support (all models), Video, and Audio (featured natively on the E2B, E4B, and 12B models).
34
 
35
  * **Diverse & Efficient Architectures** – Offers Dense and Mixture-of-Experts (MoE) variants of different sizes for scalable deployment.
36
 
@@ -44,27 +43,25 @@ Gemma 4 introduces key **capability and architectural advancements**:
44
 
45
  ## **Models Overview**
46
 
47
- Gemma 4 models are designed to deliver frontier-level performance at each size, targeting deployment scenarios from mobile and edge devices (E2B, E4B) to consumer GPUs and workstations (12B, 26B A4B, 31B). They are well-suited for reasoning, agentic workflows, coding, and multimodal understanding.
48
 
49
  The models employ a hybrid attention mechanism that interleaves local sliding window attention with full global attention, ensuring the final layer is always global. This hybrid design delivers the processing speed and low memory footprint of a lightweight model without sacrificing the deep awareness required for complex, long-context tasks. To optimize memory for long contexts, global layers feature unified Keys and Values, and apply Proportional RoPE (p-RoPE).
50
 
51
  ### Dense Models
52
 
53
- | Property | E2B | E4B | 12B Unified | 31B Dense |
54
- | :---- | :---- | :---- | :---- | :---- |
55
- | **Total Parameters** | 2.3B effective <br> (5.1B with embeddings) | 4.5B effective <br> (8B with embeddings) | 11.95B | 30.7B |
56
- | **Layers** | 35 | 42 | 48 | 60 |
57
- | **Sliding Window** | 512 tokens | 512 tokens | 1024 tokens | 1024 tokens |
58
- | **Context Length** | 128K tokens | 128K tokens | 256K tokens | 256K tokens |
59
- | **Vocabulary Size** | 262K | 262K | 262K | 262K |
60
- | **Supported Modalities** | Text, Image, Audio | Text, Image, Audio | Text, Image, Audio | Text, Image |
61
- | **Vision Encoder Parameters** | *~150M* | *~150M* | - | *~550M* |
62
- | **Audio Encoder Parameters** | *~300M* | *~300M* | - | No Audio |
63
 
64
  The "E" in E2B and E4B stands for "effective" parameters. The smaller models incorporate Per-Layer Embeddings (PLE) to maximize parameter efficiency in on-device deployments. Rather than adding more layers or parameters to the model, PLE gives each decoder layer its own small embedding for every token. These embedding tables are large but are only used for quick lookups, which is why the effective parameter count is much smaller than the total.
65
 
66
- The "Unified" in Gemma 4 12B Unified refers to its encoder-free architecture. Other Gemma 4 models use dedicated encoders to process multimodal data before passing it to the LLM. Gemma 4 12B eliminates these encoders entirely, projecting raw image patches and audio waveforms directly into the LLM's embedding space through lightweight linear layers. This unified approach means all modalities flow straight into a single decoder-only transformer, reducing multimodal latency and allowing the entire model to be fine-tuned in one pass.
67
-
68
  ### Mixture-of-Experts (MoE) Model
69
 
70
  | Property | 26B A4B MoE |
@@ -85,44 +82,42 @@ The "A" in 26B A4B stands for "active parameters" in contrast to the total numbe
85
 
86
  These models were evaluated against a large collection of different datasets and metrics to cover different aspects of text generation. Evaluation results marked in the table are for instruction-tuned models.
87
 
88
- | | Gemma 4 31B | Gemma 4 26B A4B | Gemma 4 12B Unified | Gemma 4 E4B | Gemma 4 E2B | Gemma 3 27B (no think) |
89
- | :---- | :---- | :---- | :---- | :---- | :---- | :---- |
90
- | MMLU Pro | 85.2% | 82.6% | 77.2% | 69.4% | 60.0% | 67.6% |
91
- | AIME 2026 no tools | 89.2% | 88.3% | 77.5% | 42.5% | 37.5% | 20.8% |
92
- | LiveCodeBench v6 | 80.0% | 77.1% | 72.0% | 52.0% | 44.0% | 29.1% |
93
- | Codeforces ELO | 2150 | 1718 | 1659 | 940 | 633 | 110 |
94
- | GPQA Diamond | 84.3% | 82.3% | 78.8% | 58.6% | 43.4% | 42.4% |
95
- | Tau2 (average over 3) | 76.9% | 68.2% | 69.0% | 42.2% | 24.5% | 16.2% |
96
- | HLE no tools | 19.5% | 8.7% | 5.2% | - | - | - |
97
- | HLE with search | 26.5% | 17.2% | - | - | - | - |
98
- | BigBench Extra Hard | 74.4% | 64.8% | 53.0% | 33.1% | 21.9% | 19.3% |
99
- | MMMLU | 88.4% | 86.3% | 83.4% | 76.6% | 67.4% | 70.7% |
100
- | **Vision** | | | | | | |
101
- | MMMU Pro | 76.9% | 73.8% | 69.1% | 52.6% | 44.2% | 49.7% |
102
- | OmniDocBench 1.5 (average edit distance, lower is better) | 0.131 | 0.149 | 0.164 | 0.181 | 0.290 | 0.365 |
103
- | MATH-Vision | 85.6% | 82.4% | 79.7% | 59.5% | 52.4% | 46.0% |
104
- | MedXPertQA MM | 61.3% | 58.1% | 48.7% | 28.7% | 23.5% | - |
105
- | **Audio** | | | | | | |
106
- | CoVoST | - | - | 38.5<sup>*</sup> | 35.54 | 33.47 | - |
107
- | FLEURS (lower is better) | - | - | 0.069<sup>*</sup> | 0.08 | 0.09 | - |
108
- | **Long Context** | | | | | | |
109
- | MRCR v2 8 needle 128k (average) | 66.4% | 44.1% | 43.4% | 25.4% | 19.1% | 13.5% |
110
-
111
- <sup>*</sup>Excluding Chinese language.
112
 
113
  ## **Core Capabilities**
114
 
115
  Gemma 4 models handle a broad range of tasks across text, vision, and audio. Key capabilities include:
116
 
117
  * **Thinking** – Built-in reasoning mode that lets the model think step-by-step before answering.
118
- * **Long Context** – Context windows of up to 128K tokens (E2B/E4B) and 256K tokens (12B, 26B A4B/31B).
119
  * **Image Understanding** – Object detection, Document/PDF parsing, screen and UI understanding, chart comprehension, OCR (including multilingual), handwriting recognition, and pointing. Images can be processed at variable aspect ratios and resolutions.
120
  * **Video Understanding** – Analyze video by processing sequences of frames.
121
  * **Interleaved Multimodal Input** – Freely mix text and images in any order within a single prompt.
122
  * **Function Calling** – Native support for structured tool use, enabling agentic workflows.
123
  * **Coding** – Code generation, completion, and correction.
124
  * **Multilingual** – Out-of-the-box support for 35+ languages, pre-trained on 140+ languages.
125
- * **Audio** (E2B, E4B, and 12B only) – Automatic speech recognition (ASR) and speech-to-translated-text translation across multiple languages.
126
 
127
 
128
  ## Getting Started
@@ -134,13 +129,13 @@ You can use all Gemma 4 models with the latest version of Transformers. To get s
134
  Once you have everything installed, you can proceed to load the model with the code below:
135
 
136
  ```python
137
- from transformers import AutoProcessor, AutoModelForMultimodalLM
138
 
139
  MODEL_ID = "google/gemma-4-E4B-it"
140
 
141
  # Load model
142
  processor = AutoProcessor.from_pretrained(MODEL_ID)
143
- model = AutoModelForMultimodalLM.from_pretrained(
144
  MODEL_ID,
145
  dtype="auto",
146
  device_map="auto"
@@ -157,14 +152,13 @@ messages = [
157
  ]
158
 
159
  # Process input
160
- inputs = processor.apply_chat_template(
161
- messages,
162
- tokenize=True,
163
- return_dict=True,
164
- return_tensors="pt",
165
- add_generation_prompt=True,
166
  enable_thinking=False
167
- ).to(model.device)
 
168
  input_len = inputs["input_ids"].shape[-1]
169
 
170
  # Generate output
@@ -172,17 +166,18 @@ outputs = model.generate(**inputs, max_new_tokens=1024)
172
  response = processor.decode(outputs[0][input_len:], skip_special_tokens=False)
173
 
174
  # Parse output
175
- processor.parse_response(response, prefix=inputs["input_ids"])
176
  ```
177
 
178
  To enable reasoning, set `enable_thinking=True` and the `parse_response` function will take care of parsing the thinking output.
179
 
180
- Below, you will also find snippets for processing audio (E2B, E4B, 12B only), images, and video alongside text:
181
 
182
  <details>
183
  <summary>Code for processing Audio</summary>
184
 
185
- Make sure to install the following packages:
 
186
 
187
  `pip install -U transformers torch torchvision librosa accelerate`
188
 
@@ -206,13 +201,13 @@ Once the model is loaded, you can start generating output by directly referencin
206
 
207
 
208
  ```python
209
- # Prompt - add audio after text
210
  messages = [
211
  {
212
  "role": "user",
213
  "content": [
 
214
  {"type": "text", "text": "Transcribe the following speech segment in its original language. Follow these specific instructions for formatting the answer:\n* Only output the transcription, with no newlines.\n* When transcribing numbers, write the digits, i.e. write 1.7 and not one point seven, and write 3 instead of three."},
215
- {"type": "audio", "audio": "https://raw.githubusercontent.com/google-gemma/cookbook/refs/heads/main/apps/sample-data/journal1.wav"},
216
  ]
217
  }
218
  ]
@@ -232,7 +227,7 @@ outputs = model.generate(**inputs, max_new_tokens=512)
232
  response = processor.decode(outputs[0][input_len:], skip_special_tokens=False)
233
 
234
  # Parse output
235
- processor.parse_response(response, prefix=inputs["input_ids"])
236
  ```
237
 
238
  </details>
@@ -240,7 +235,7 @@ processor.parse_response(response, prefix=inputs["input_ids"])
240
  <details>
241
  <summary>Code for processing Images</summary>
242
 
243
- Make sure to install the following packages:
244
 
245
 
246
  `pip install -U transformers torch torchvision accelerate`
@@ -269,7 +264,7 @@ Once the model is loaded, you can start generating output by directly referencin
269
  messages = [
270
  {
271
  "role": "user", "content": [
272
- {"type": "image", "url": "https://raw.githubusercontent.com/google-gemma/cookbook/refs/heads/main/apps/sample-data/GoldenGate.png"},
273
  {"type": "text", "text": "What is shown in this image?"}
274
  ]
275
  }
@@ -290,7 +285,7 @@ outputs = model.generate(**inputs, max_new_tokens=512)
290
  response = processor.decode(outputs[0][input_len:], skip_special_tokens=False)
291
 
292
  # Parse output
293
- processor.parse_response(response, prefix=inputs["input_ids"])
294
  ```
295
 
296
  </details>
@@ -299,7 +294,7 @@ processor.parse_response(response, prefix=inputs["input_ids"])
299
  <details>
300
  <summary>Code for processing Videos</summary>
301
 
302
- Make sure to install the following packages:
303
 
304
  `pip install -U transformers torch torchvision librosa accelerate`
305
 
@@ -349,7 +344,7 @@ outputs = model.generate(**inputs, max_new_tokens=512)
349
  response = processor.decode(outputs[0][input_len:], skip_special_tokens=False)
350
 
351
  # Parse output
352
- processor.parse_response(response, prefix=inputs["input_ids"])
353
  ```
354
 
355
  </details>
@@ -383,14 +378,11 @@ Compared to Gemma 3, the models use standard `system`, `assistant`, and `user` r
383
 
384
  ### 3. Multi-Turn Conversations
385
 
386
- * **No Thinking Content in History**: In multi-turn conversations, the historical model output should only include the final response. Thoughts from previous model turns must *not be added* before the next user turn begins, with the exception of tool call turns where thinking content should be preserved.
387
 
388
  ### 4. Modality order
389
 
390
- For optimal performance with multimodal inputs, place:
391
-
392
- * Image content **before** the text in your prompt.
393
- * Audio content **after** the text in your prompt.
394
 
395
  ### 5. Variable Image Resolution
396
 
@@ -423,7 +415,7 @@ When formatting the answer, first output the transcription in {SOURCE_LANGUAGE},
423
 
424
  ### 7. Audio and Video Length
425
 
426
- All models support image inputs and can process videos as frames whereas the E2B, E4B, and 12B models also support audio inputs. Audio supports a maximum length of 30 seconds. Video supports a maximum of 60 seconds assuming the images are processed at one frame per second.
427
 
428
  ## **Model Data**
429
 
@@ -479,7 +471,7 @@ Multimodal models (capable of processing vision, language, and/or audio) have a
479
  * **Chatbots and Conversational AI**: Power conversational interfaces for customer service, virtual assistants, or interactive applications.
480
  * **Text Summarization**: Generate concise summaries of a text corpus, research papers, or reports.
481
  * **Image Data Extraction**: These models can be used to extract, interpret, and summarize visual data for text communications.
482
- * **Audio Processing and Interaction**: The E2B, E4B, and 12B models can analyze and interpret audio inputs, enabling voice-driven interactions and transcriptions.
483
  * **Research and Education**
484
  * **Natural Language Processing (NLP) and VLM Research**: These models can serve as a foundation for researchers to experiment with VLM and NLP techniques, develop algorithms, and contribute to the advancement of the field.
485
  * **Language Learning Tools**: Support interactive language learning experiences, aiding in grammar correction or providing writing practice.
@@ -522,20 +514,4 @@ The development of vision-language models (VLMs) raises several ethical concerns
522
 
523
  ### **Benefits**
524
 
525
- At the time of release, this family of models provides high-performance open vision-language model implementations designed from the ground up for responsible AI development compared to similarly sized models.
526
-
527
- ## **Citation**
528
-
529
- If you find our work helpful, please consider citing it:
530
-
531
- ```bibtex
532
- @misc{gemmateam2026gemma4,
533
- title={Gemma 4 Technical Report},
534
- author={Gemma Team},
535
- year={2026},
536
- eprint={2607.02770},
537
- archivePrefix={arXiv},
538
- primaryClass={cs.CL},
539
- url={https://arxiv.org/abs/2607.02770},
540
- }
541
- ```
 
16
  <a href="https://huggingface.co/collections/google/gemma-4" target="_blank">Hugging Face</a> |
17
  <a href="https://github.com/google-gemma" target="_blank">GitHub</a> |
18
  <a href="https://blog.google/innovation-and-ai/technology/developers-tools/gemma-4/" target="_blank">Launch Blog</a> |
19
+ <a href="https://ai.google.dev/gemma/docs/core" target="_blank">Documentation</a>
 
20
  <br>
21
  <b>License</b>: <a href="https://ai.google.dev/gemma/docs/gemma_4_license" target="_blank">Apache 2.0</a> | <b>Authors</b>: <a href="https://deepmind.google/models/gemma/" target="_blank">Google DeepMind</a>
22
  </p>
23
 
24
+ Gemma is a family of open models built by Google DeepMind. Gemma 4 models are multimodal, handling text and image input (with audio supported on small models) and generating text output. This release includes open-weights models in both pre-trained and instruction-tuned variants. Gemma 4 features a context window of up to 256K tokens and maintains multilingual support in over 140 languages.
25
 
26
+ Featuring both Dense and Mixture-of-Experts (MoE) architectures, Gemma 4 is well-suited for tasks like text generation, coding, and reasoning. The models are available in four distinct sizes: **E2B**, **E4B**, **26B A4B**, and **31B**. Their diverse sizes make them deployable in environments ranging from high-end phones to laptops and servers, democratizing access to state-of-the-art AI.
27
 
28
  Gemma 4 introduces key **capability and architectural advancements**:
29
 
30
  * **Reasoning** – All models in the family are designed as highly capable reasoners, with configurable thinking modes.
31
 
32
+ * **Extended Multimodalities** – Processes Text, Image with variable aspect ratio and resolution support (all models), Video, and Audio (featured natively on the E2B and E4B models).
33
 
34
  * **Diverse & Efficient Architectures** – Offers Dense and Mixture-of-Experts (MoE) variants of different sizes for scalable deployment.
35
 
 
43
 
44
  ## **Models Overview**
45
 
46
+ Gemma 4 models are designed to deliver frontier-level performance at each size, targeting deployment scenarios from mobile and edge devices (E2B, E4B) to consumer GPUs and workstations (26B A4B, 31B). They are well-suited for reasoning, agentic workflows, coding, and multimodal understanding.
47
 
48
  The models employ a hybrid attention mechanism that interleaves local sliding window attention with full global attention, ensuring the final layer is always global. This hybrid design delivers the processing speed and low memory footprint of a lightweight model without sacrificing the deep awareness required for complex, long-context tasks. To optimize memory for long contexts, global layers feature unified Keys and Values, and apply Proportional RoPE (p-RoPE).
49
 
50
  ### Dense Models
51
 
52
+ | Property | E2B | E4B | 31B Dense |
53
+ | :---- | :---- | :---- | :---- |
54
+ | **Total Parameters** | 2.3B effective (5.1B with embeddings) | 4.5B effective (8B with embeddings) | 30.7B |
55
+ | **Layers** | 35 | 42 | 60 |
56
+ | **Sliding Window** | 512 tokens | 512 tokens | 1024 tokens |
57
+ | **Context Length** | 128K tokens | 128K tokens | 256K tokens |
58
+ | **Vocabulary Size** | 262K | 262K | 262K |
59
+ | **Supported Modalities** | Text, Image, Audio | Text, Image, Audio | Text, Image |
60
+ | **Vision Encoder Parameters** | *~150M* | *~150M* | *~550M* |
61
+ | **Audio Encoder Parameters** | *~300M* | *~300M* | No Audio |
62
 
63
  The "E" in E2B and E4B stands for "effective" parameters. The smaller models incorporate Per-Layer Embeddings (PLE) to maximize parameter efficiency in on-device deployments. Rather than adding more layers or parameters to the model, PLE gives each decoder layer its own small embedding for every token. These embedding tables are large but are only used for quick lookups, which is why the effective parameter count is much smaller than the total.
64
 
 
 
65
  ### Mixture-of-Experts (MoE) Model
66
 
67
  | Property | 26B A4B MoE |
 
82
 
83
  These models were evaluated against a large collection of different datasets and metrics to cover different aspects of text generation. Evaluation results marked in the table are for instruction-tuned models.
84
 
85
+ | | Gemma 4 31B | Gemma 4 26B A4B | Gemma 4 E4B | Gemma 4 E2B | Gemma 3 27B (no think) |
86
+ | :---- | :---- | :---- | :---- | :---- | :---- |
87
+ | MMLU Pro | 85.2% | 82.6% | 69.4% | 60.0% | 67.6% |
88
+ | AIME 2026 no tools | 89.2% | 88.3% | 42.5% | 37.5% | 20.8% |
89
+ | LiveCodeBench v6 | 80.0% | 77.1% | 52.0% | 44.0% | 29.1% |
90
+ | Codeforces ELO | 2150 | 1718 | 940 | 633 | 110 |
91
+ | GPQA Diamond | 84.3% | 82.3% | 58.6% | 43.4% | 42.4% |
92
+ | Tau2 (average over 3) | 76.9% | 68.2% | 42.2% | 24.5% | 16.2% |
93
+ | HLE no tools | 19.5% | 8.7% | - | - | - |
94
+ | HLE with search | 26.5% | 17.2% | - | - | - |
95
+ | BigBench Extra Hard | 74.4% | 64.8% | 33.1% | 21.9% | 19.3% |
96
+ | MMMLU | 88.4% | 86.3% | 76.6% | 67.4% | 70.7% |
97
+ | **Vision** | | | | | |
98
+ | MMMU Pro | 76.9% | 73.8% | 52.6% | 44.2% | 49.7% |
99
+ | OmniDocBench 1.5 (average edit distance, lower is better) | 0.131 | 0.149 | 0.181 | 0.290 | 0.365 |
100
+ | MATH-Vision | 85.6% | 82.4% | 59.5% | 52.4% | 46.0% |
101
+ | MedXPertQA MM | 61.3% | 58.1% | 28.7% | 23.5% | - |
102
+ | **Audio** | | | | | |
103
+ | CoVoST | - | - | 35.54 | 33.47 | - |
104
+ | FLEURS (lower is better) | - | - | 0.08 | 0.09 | - |
105
+ | **Long Context** | | | | | |
106
+ | MRCR v2 8 needle 128k (average) | 66.4% | 44.1% | 25.4% | 19.1% | 13.5% |
 
 
107
 
108
  ## **Core Capabilities**
109
 
110
  Gemma 4 models handle a broad range of tasks across text, vision, and audio. Key capabilities include:
111
 
112
  * **Thinking** – Built-in reasoning mode that lets the model think step-by-step before answering.
113
+ * **Long Context** – Context windows of up to 128K tokens (E2B/E4B) and 256K tokens (26B A4B/31B).
114
  * **Image Understanding** – Object detection, Document/PDF parsing, screen and UI understanding, chart comprehension, OCR (including multilingual), handwriting recognition, and pointing. Images can be processed at variable aspect ratios and resolutions.
115
  * **Video Understanding** – Analyze video by processing sequences of frames.
116
  * **Interleaved Multimodal Input** – Freely mix text and images in any order within a single prompt.
117
  * **Function Calling** – Native support for structured tool use, enabling agentic workflows.
118
  * **Coding** – Code generation, completion, and correction.
119
  * **Multilingual** – Out-of-the-box support for 35+ languages, pre-trained on 140+ languages.
120
+ * **Audio** (E2B and E4B only) – Automatic speech recognition (ASR) and speech-to-translated-text translation across multiple languages.
121
 
122
 
123
  ## Getting Started
 
129
  Once you have everything installed, you can proceed to load the model with the code below:
130
 
131
  ```python
132
+ from transformers import AutoProcessor, AutoModelForCausalLM
133
 
134
  MODEL_ID = "google/gemma-4-E4B-it"
135
 
136
  # Load model
137
  processor = AutoProcessor.from_pretrained(MODEL_ID)
138
+ model = AutoModelForCausalLM.from_pretrained(
139
  MODEL_ID,
140
  dtype="auto",
141
  device_map="auto"
 
152
  ]
153
 
154
  # Process input
155
+ text = processor.apply_chat_template(
156
+ messages,
157
+ tokenize=False,
158
+ add_generation_prompt=True,
 
 
159
  enable_thinking=False
160
+ )
161
+ inputs = processor(text=text, return_tensors="pt").to(model.device)
162
  input_len = inputs["input_ids"].shape[-1]
163
 
164
  # Generate output
 
166
  response = processor.decode(outputs[0][input_len:], skip_special_tokens=False)
167
 
168
  # Parse output
169
+ processor.parse_response(response)
170
  ```
171
 
172
  To enable reasoning, set `enable_thinking=True` and the `parse_response` function will take care of parsing the thinking output.
173
 
174
+ Below, you will also find snippets for processing audio (E2B and E4B only), images, and video alongside text:
175
 
176
  <details>
177
  <summary>Code for processing Audio</summary>
178
 
179
+ Instead of using `AutoModelForCausalLM`, you can use `AutoModelForMultimodalLM` to process audio. To use it, make sure to install the following packages:
180
+
181
 
182
  `pip install -U transformers torch torchvision librosa accelerate`
183
 
 
201
 
202
 
203
  ```python
204
+ # Prompt - add audio before text
205
  messages = [
206
  {
207
  "role": "user",
208
  "content": [
209
+ {"type": "audio", "audio": "https://raw.githubusercontent.com/google-gemma/cookbook/refs/heads/main/Demos/sample-data/journal1.wav"},
210
  {"type": "text", "text": "Transcribe the following speech segment in its original language. Follow these specific instructions for formatting the answer:\n* Only output the transcription, with no newlines.\n* When transcribing numbers, write the digits, i.e. write 1.7 and not one point seven, and write 3 instead of three."},
 
211
  ]
212
  }
213
  ]
 
227
  response = processor.decode(outputs[0][input_len:], skip_special_tokens=False)
228
 
229
  # Parse output
230
+ processor.parse_response(response)
231
  ```
232
 
233
  </details>
 
235
  <details>
236
  <summary>Code for processing Images</summary>
237
 
238
+ Instead of using `AutoModelForCausalLM`, you can use `AutoModelForMultimodalLM` to process images. To use it, make sure to install the following packages:
239
 
240
 
241
  `pip install -U transformers torch torchvision accelerate`
 
264
  messages = [
265
  {
266
  "role": "user", "content": [
267
+ {"type": "image", "url": "https://raw.githubusercontent.com/google-gemma/cookbook/refs/heads/main/Demos/sample-data/GoldenGate.png"},
268
  {"type": "text", "text": "What is shown in this image?"}
269
  ]
270
  }
 
285
  response = processor.decode(outputs[0][input_len:], skip_special_tokens=False)
286
 
287
  # Parse output
288
+ processor.parse_response(response)
289
  ```
290
 
291
  </details>
 
294
  <details>
295
  <summary>Code for processing Videos</summary>
296
 
297
+ Instead of using `AutoModelForCausalLM`, you can use `AutoModelForMultimodalLM` to process videos. To use it, make sure to install the following packages:
298
 
299
  `pip install -U transformers torch torchvision librosa accelerate`
300
 
 
344
  response = processor.decode(outputs[0][input_len:], skip_special_tokens=False)
345
 
346
  # Parse output
347
+ processor.parse_response(response)
348
  ```
349
 
350
  </details>
 
378
 
379
  ### 3. Multi-Turn Conversations
380
 
381
+ * **No Thinking Content in History**: In multi-turn conversations, the historical model output should only include the final response. Thoughts from previous model turns must *not be added* before the next user turn begins.
382
 
383
  ### 4. Modality order
384
 
385
+ * For optimal performance with multimodal inputs, place image and/or audio content **before** the text in your prompt.
 
 
 
386
 
387
  ### 5. Variable Image Resolution
388
 
 
415
 
416
  ### 7. Audio and Video Length
417
 
418
+ All models support image inputs and can process videos as frames whereas the E2B and E4B models also support audio inputs. Audio supports a maximum length of 30 seconds. Video supports a maximum of 60 seconds assuming the images are processed at one frame per second.
419
 
420
  ## **Model Data**
421
 
 
471
  * **Chatbots and Conversational AI**: Power conversational interfaces for customer service, virtual assistants, or interactive applications.
472
  * **Text Summarization**: Generate concise summaries of a text corpus, research papers, or reports.
473
  * **Image Data Extraction**: These models can be used to extract, interpret, and summarize visual data for text communications.
474
+ * **Audio Processing and Interaction**: The smaller models (E2B and E4B) can analyze and interpret audio inputs, enabling voice-driven interactions and transcriptions.
475
  * **Research and Education**
476
  * **Natural Language Processing (NLP) and VLM Research**: These models can serve as a foundation for researchers to experiment with VLM and NLP techniques, develop algorithms, and contribute to the advancement of the field.
477
  * **Language Learning Tools**: Support interactive language learning experiences, aiding in grammar correction or providing writing practice.
 
514
 
515
  ### **Benefits**
516
 
517
+ At the time of release, this family of models provides high-performance open vision-language model implementations designed from the ground up for responsible AI development compared to similarly sized models.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
chat_template.jinja CHANGED
@@ -1,9 +1,3 @@
1
- {#
2
- Template: Google Gemma 4 Canonical Chat Template
3
- Author: Google Gemma Engineering Team
4
- Published: 2026-07-09
5
- Context: Fixed tool-calling loops, turn closures, and thinking content-ordering.
6
- #}
7
  {%- macro format_parameters(properties, required, filter_keys=false) -%}
8
  {%- set standard_keys = ['description', 'type', 'properties', 'required', 'nullable'] -%}
9
  {%- set ns = namespace(found_first=false) -%}
@@ -122,9 +116,7 @@
122
  }
123
  {%- endmacro -%}
124
  {%- macro format_argument(argument, escape_keys=True) -%}
125
- {%- if argument is none -%}
126
- {{- 'null' -}}
127
- {%- elif argument is string -%}
128
  {{- '<|"|>' + argument + '<|"|>' -}}
129
  {%- elif argument is boolean -%}
130
  {{- 'true' if argument else 'false' -}}
@@ -180,21 +172,18 @@
180
  {{- '<tool_response|>' -}}
181
  {%- endmacro -%}
182
 
183
- {#- ===== SETUP ===== -#}
184
- {%- set ns = namespace(prev_message_type=None, prev_non_tool_role=None) -%}
185
  {%- set loop_messages = messages -%}
186
- {%- set enable_thinking = enable_thinking | default(false) -%}
187
- {%- set preserve_thinking = preserve_thinking | default(false) -%}
188
  {{- bos_token -}}
189
  {#- Handle System/Tool Definitions Block -#}
190
- {%- if enable_thinking or tools or (messages and messages[0]['role'] in ['system', 'developer']) -%}
191
  {{- '<|turn>system\n' -}}
192
  {#- Inject Thinking token at the very top of the FIRST system turn -#}
193
- {%- if enable_thinking -%}
194
  {{- '<|think|>\n' -}}
195
  {%- set ns.prev_message_type = 'think' -%}
196
  {%- endif -%}
197
- {%- if messages and messages[0]['role'] in ['system', 'developer'] -%}
198
  {%- if messages[0]['content'] is string -%}
199
  {{- messages[0]['content'] | trim -}}
200
  {%- elif messages[0]['content'] is sequence -%}
@@ -228,21 +217,31 @@
228
  {%- if message['role'] != 'tool' -%}
229
  {%- set ns.prev_message_type = None -%}
230
  {%- set role = 'model' if message['role'] == 'assistant' else message['role'] -%}
231
- {#- Detect continuation using tracked state O(1) instead of O(n) backward scan -#}
232
- {%- set continue_same_model_turn = (role == 'model' and ns.prev_non_tool_role == 'assistant') -%}
 
 
 
 
 
 
 
 
 
 
 
233
  {%- if not continue_same_model_turn -%}
234
  {{- '<|turn>' + role + '\n' }}
235
  {%- endif -%}
236
 
237
  {#- Render reasoning/reasoning_content as thinking channel -#}
238
  {%- set thinking_text = message.get('reasoning') or message.get('reasoning_content') -%}
239
- {%- set thinking_gate = (loop.index0 > ns_turn.last_user_idx) or (preserve_thinking and message.get('tool_calls')) -%}
240
- {%- if thinking_text and thinking_gate -%}
241
  {{- '<|channel>thought\n' + thinking_text + '\n<channel|>' -}}
242
  {%- endif -%}
243
 
244
- {%- if message.get('tool_calls') -%}
245
- {%- for tool_call in message.get('tool_calls') -%}
246
  {%- set function = tool_call['function'] -%}
247
  {{- '<|tool_call>call:' + function['name'] + '{' -}}
248
  {%- if function['arguments'] is mapping -%}
@@ -252,13 +251,8 @@
252
  {%- set ns_args.found_first = true -%}
253
  {{- key -}}:{{- format_argument(value, escape_keys=False) -}}
254
  {%- endfor -%}
255
- {%- elif function['arguments'] is none -%}
256
- {%- else -%}
257
- {{- raise_exception(
258
- "chat_template: tool_calls[].function.arguments must be a "
259
- "JSON object (mapping), not a string. Deserialize arguments "
260
- "before passing to the template."
261
- ) -}}
262
  {%- endif -%}
263
  {{- '}<tool_call|>' -}}
264
  {%- endfor -%}
@@ -268,8 +262,8 @@
268
  {%- set ns_tr_out = namespace(flag=false) -%}
269
  {%- if message.get('tool_responses') -%}
270
  {#- Legacy: tool_responses embedded on the assistant message (Google/Gemma native) -#}
271
- {%- for tool_response in message.get('tool_responses') -%}
272
- {{- format_tool_response_block(tool_response['name'] | default('unknown', true), tool_response['response']) -}}
273
  {%- set ns_tr_out.flag = true -%}
274
  {%- set ns.prev_message_type = 'tool_response' -%}
275
  {%- endfor -%}
@@ -283,8 +277,8 @@
283
  {%- else -%}
284
  {%- set follow = loop_messages[k] -%}
285
  {#- Resolve tool_call_id to function name -#}
286
- {%- set ns_tname = namespace(name=follow.get('name') or 'unknown') -%}
287
- {%- for tc in message.get('tool_calls') -%}
288
  {%- if tc.get('id') == follow.get('tool_call_id') -%}
289
  {%- set ns_tname.name = tc['function']['name'] -%}
290
  {%- endif -%}
@@ -301,15 +295,6 @@
301
  {%- endif -%}
302
  {%- endfor -%}
303
  {{- format_tool_response_block(ns_tname.name, ns_txt.s) -}}
304
- {%- for part in tool_body -%}
305
- {%- if part.get('type') in ['image', 'image_url'] -%}
306
- {{- '<|image|>' -}}
307
- {%- elif part.get('type') in ['audio', 'input_audio'] -%}
308
- {{- '<|audio|>' -}}
309
- {%- elif part.get('type') == 'video' -%}
310
- {{- '<|video|>' -}}
311
- {%- endif -%}
312
- {%- endfor -%}
313
  {%- else -%}
314
  {{- format_tool_response_block(ns_tname.name, tool_body) -}}
315
  {%- endif -%}
@@ -320,26 +305,29 @@
320
  {%- endif -%}
321
 
322
  {%- set captured_content -%}
323
- {%- if message.get('content') is string -%}
324
  {%- if role == 'model' -%}
325
  {{- strip_thinking(message['content']) -}}
326
  {%- else -%}
327
  {{- message['content'] | trim -}}
328
  {%- endif -%}
329
- {%- elif message.get('content') is sequence -%}
330
  {%- for item in message['content'] -%}
331
- {%- if item.get('type') == 'text' -%}
332
  {%- if role == 'model' -%}
333
  {{- strip_thinking(item['text']) -}}
334
  {%- else -%}
335
  {{- item['text'] | trim -}}
336
  {%- endif -%}
337
- {%- elif item.get('type') in ['image', 'image_url'] -%}
338
  {{- '<|image|>' -}}
339
- {%- elif item.get('type') in ['audio', 'input_audio'] -%}
 
340
  {{- '<|audio|>' -}}
341
- {%- elif item.get('type') == 'video' -%}
 
342
  {{- '<|video|>' -}}
 
343
  {%- endif -%}
344
  {%- endfor -%}
345
  {%- endif -%}
@@ -348,39 +336,16 @@
348
  {{- captured_content -}}
349
  {%- set has_content = captured_content | trim | length > 0 -%}
350
 
351
- {#- Forward-scan: find next non-tool message role for continuation detection -#}
352
- {%- set next_nt = namespace(role=None, found=false) -%}
353
- {%- for j in range(loop.index0 + 1, loop_messages | length) -%}
354
- {%- if not next_nt.found -%}
355
- {%- if loop_messages[j]['role'] != 'tool' -%}
356
- {%- set next_nt.role = loop_messages[j]['role'] -%}
357
- {%- set next_nt.found = true -%}
358
- {%- endif -%}
359
- {%- endif -%}
360
- {%- endfor -%}
361
-
362
- {%- set continues_into_next = (
363
- role == 'model'
364
- and next_nt.role == 'assistant'
365
- and (not message.get('tool_calls') or ns_tr_out.flag)
366
- ) -%}
367
-
368
  {%- if ns.prev_message_type == 'tool_call' and not ns_tr_out.flag -%}
369
  {{- '<|tool_response>' -}}
370
- {%- elif continues_into_next -%}
371
- {%- elif not (ns_tr_out.flag and not has_content and not next_nt.found) -%}
372
  {{- '<turn|>\n' -}}
373
  {%- endif -%}
374
-
375
- {#- Track previous non-tool role for next iteration (avoids O(n) backward scan) -#}
376
- {%- set ns.prev_non_tool_role = message['role'] -%}
377
  {%- endif -%}
378
  {%- endfor -%}
379
 
380
  {%- if add_generation_prompt -%}
381
  {%- if ns.prev_message_type != 'tool_response' and ns.prev_message_type != 'tool_call' -%}
382
  {{- '<|turn>model\n' -}}
383
- {%- elif ns.prev_message_type == 'tool_response' and enable_thinking -%}
384
- {{- '<|channel>thought\n' -}}
385
  {%- endif -%}
386
- {%- endif -%}
 
 
 
 
 
 
 
1
  {%- macro format_parameters(properties, required, filter_keys=false) -%}
2
  {%- set standard_keys = ['description', 'type', 'properties', 'required', 'nullable'] -%}
3
  {%- set ns = namespace(found_first=false) -%}
 
116
  }
117
  {%- endmacro -%}
118
  {%- macro format_argument(argument, escape_keys=True) -%}
119
+ {%- if argument is string -%}
 
 
120
  {{- '<|"|>' + argument + '<|"|>' -}}
121
  {%- elif argument is boolean -%}
122
  {{- 'true' if argument else 'false' -}}
 
172
  {{- '<tool_response|>' -}}
173
  {%- endmacro -%}
174
 
175
+ {%- set ns = namespace(prev_message_type=None) -%}
 
176
  {%- set loop_messages = messages -%}
 
 
177
  {{- bos_token -}}
178
  {#- Handle System/Tool Definitions Block -#}
179
+ {%- if (enable_thinking is defined and enable_thinking) or tools or messages[0]['role'] in ['system', 'developer'] -%}
180
  {{- '<|turn>system\n' -}}
181
  {#- Inject Thinking token at the very top of the FIRST system turn -#}
182
+ {%- if enable_thinking is defined and enable_thinking -%}
183
  {{- '<|think|>\n' -}}
184
  {%- set ns.prev_message_type = 'think' -%}
185
  {%- endif -%}
186
+ {%- if messages[0]['role'] in ['system', 'developer'] -%}
187
  {%- if messages[0]['content'] is string -%}
188
  {{- messages[0]['content'] | trim -}}
189
  {%- elif messages[0]['content'] is sequence -%}
 
217
  {%- if message['role'] != 'tool' -%}
218
  {%- set ns.prev_message_type = None -%}
219
  {%- set role = 'model' if message['role'] == 'assistant' else message['role'] -%}
220
+ {#- Detect continuation: suppress duplicate <|turn>model when previous non-tool message was also assistant -#}
221
+ {%- set prev_nt = namespace(role=None, found=false) -%}
222
+ {%- if loop.index0 > 0 -%}
223
+ {%- for j in range(loop.index0 - 1, -1, -1) -%}
224
+ {%- if not prev_nt.found -%}
225
+ {%- if loop_messages[j]['role'] != 'tool' -%}
226
+ {%- set prev_nt.role = loop_messages[j]['role'] -%}
227
+ {%- set prev_nt.found = true -%}
228
+ {%- endif -%}
229
+ {%- endif -%}
230
+ {%- endfor -%}
231
+ {%- endif -%}
232
+ {%- set continue_same_model_turn = (role == 'model' and prev_nt.role == 'assistant') -%}
233
  {%- if not continue_same_model_turn -%}
234
  {{- '<|turn>' + role + '\n' }}
235
  {%- endif -%}
236
 
237
  {#- Render reasoning/reasoning_content as thinking channel -#}
238
  {%- set thinking_text = message.get('reasoning') or message.get('reasoning_content') -%}
239
+ {%- if thinking_text and loop.index0 > ns_turn.last_user_idx and message.get('tool_calls') -%}
 
240
  {{- '<|channel>thought\n' + thinking_text + '\n<channel|>' -}}
241
  {%- endif -%}
242
 
243
+ {%- if message['tool_calls'] -%}
244
+ {%- for tool_call in message['tool_calls'] -%}
245
  {%- set function = tool_call['function'] -%}
246
  {{- '<|tool_call>call:' + function['name'] + '{' -}}
247
  {%- if function['arguments'] is mapping -%}
 
251
  {%- set ns_args.found_first = true -%}
252
  {{- key -}}:{{- format_argument(value, escape_keys=False) -}}
253
  {%- endfor -%}
254
+ {%- elif function['arguments'] is string -%}
255
+ {{- function['arguments'] -}}
 
 
 
 
 
256
  {%- endif -%}
257
  {{- '}<tool_call|>' -}}
258
  {%- endfor -%}
 
262
  {%- set ns_tr_out = namespace(flag=false) -%}
263
  {%- if message.get('tool_responses') -%}
264
  {#- Legacy: tool_responses embedded on the assistant message (Google/Gemma native) -#}
265
+ {%- for tool_response in message['tool_responses'] -%}
266
+ {{- format_tool_response_block(tool_response['name'] | default('unknown'), tool_response['response']) -}}
267
  {%- set ns_tr_out.flag = true -%}
268
  {%- set ns.prev_message_type = 'tool_response' -%}
269
  {%- endfor -%}
 
277
  {%- else -%}
278
  {%- set follow = loop_messages[k] -%}
279
  {#- Resolve tool_call_id to function name -#}
280
+ {%- set ns_tname = namespace(name=follow.get('name') | default('unknown')) -%}
281
+ {%- for tc in message['tool_calls'] -%}
282
  {%- if tc.get('id') == follow.get('tool_call_id') -%}
283
  {%- set ns_tname.name = tc['function']['name'] -%}
284
  {%- endif -%}
 
295
  {%- endif -%}
296
  {%- endfor -%}
297
  {{- format_tool_response_block(ns_tname.name, ns_txt.s) -}}
 
 
 
 
 
 
 
 
 
298
  {%- else -%}
299
  {{- format_tool_response_block(ns_tname.name, tool_body) -}}
300
  {%- endif -%}
 
305
  {%- endif -%}
306
 
307
  {%- set captured_content -%}
308
+ {%- if message['content'] is string -%}
309
  {%- if role == 'model' -%}
310
  {{- strip_thinking(message['content']) -}}
311
  {%- else -%}
312
  {{- message['content'] | trim -}}
313
  {%- endif -%}
314
+ {%- elif message['content'] is sequence -%}
315
  {%- for item in message['content'] -%}
316
+ {%- if item['type'] == 'text' -%}
317
  {%- if role == 'model' -%}
318
  {{- strip_thinking(item['text']) -}}
319
  {%- else -%}
320
  {{- item['text'] | trim -}}
321
  {%- endif -%}
322
+ {%- elif item['type'] == 'image' -%}
323
  {{- '<|image|>' -}}
324
+ {%- set ns.prev_message_type = 'image' -%}
325
+ {%- elif item['type'] == 'audio' -%}
326
  {{- '<|audio|>' -}}
327
+ {%- set ns.prev_message_type = 'audio' -%}
328
+ {%- elif item['type'] == 'video' -%}
329
  {{- '<|video|>' -}}
330
+ {%- set ns.prev_message_type = 'video' -%}
331
  {%- endif -%}
332
  {%- endfor -%}
333
  {%- endif -%}
 
336
  {{- captured_content -}}
337
  {%- set has_content = captured_content | trim | length > 0 -%}
338
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
339
  {%- if ns.prev_message_type == 'tool_call' and not ns_tr_out.flag -%}
340
  {{- '<|tool_response>' -}}
341
+ {%- elif not (ns_tr_out.flag and not has_content) -%}
 
342
  {{- '<turn|>\n' -}}
343
  {%- endif -%}
 
 
 
344
  {%- endif -%}
345
  {%- endfor -%}
346
 
347
  {%- if add_generation_prompt -%}
348
  {%- if ns.prev_message_type != 'tool_response' and ns.prev_message_type != 'tool_call' -%}
349
  {{- '<|turn>model\n' -}}
 
 
350
  {%- endif -%}
351
+ {%- endif -%}
tokenizer_config.json CHANGED
@@ -63,52 +63,6 @@
63
  },
64
  "x-regex": "(\\<\\|channel\\>thought\\n(?P<thinking>.*?)\\<channel\\|\\>)?(?P<tool_calls>\\<\\|tool_call\\>.*\\<tool_call\\|\\>)?(?P<content>(?:(?!\\<turn\\|\\>)(?!\\<\\|tool_response\\>).)+)?(?:\\<turn\\|\\>|\\<\\|tool_response\\>)?"
65
  },
66
- "response_template": {
67
- "defaults": {
68
- "role": "assistant"
69
- },
70
- "fields": {
71
- "content": {
72
- "close": [
73
- "<turn|>",
74
- "<|tool_response>",
75
- "<eos>"
76
- ],
77
- "content": "text"
78
- },
79
- "thinking": {
80
- "close": "<channel|>",
81
- "content": "text",
82
- "open": "<|channel>thought\n"
83
- },
84
- "tool_calls": {
85
- "close": "<tool_call|>",
86
- "content": "json",
87
- "content_args": {
88
- "string_delims": [
89
- [
90
- "<|\"|>",
91
- "<|\"|>"
92
- ]
93
- ],
94
- "unquoted_keys": true
95
- },
96
- "open_pattern": "<\\|tool_call>call:(?P<name>\\w+)",
97
- "repeats": true,
98
- "transform": {
99
- "function": {
100
- "arguments": "{content}",
101
- "name": "{name}"
102
- },
103
- "type": "function"
104
- }
105
- }
106
- },
107
- "start_anchor": [
108
- "<|turn>model\n",
109
- "<tool_response|>"
110
- ]
111
- },
112
  "soc_token": "<|channel>",
113
  "sot_token": "<|turn>",
114
  "stc_token": "<|tool_call>",
 
63
  },
64
  "x-regex": "(\\<\\|channel\\>thought\\n(?P<thinking>.*?)\\<channel\\|\\>)?(?P<tool_calls>\\<\\|tool_call\\>.*\\<tool_call\\|\\>)?(?P<content>(?:(?!\\<turn\\|\\>)(?!\\<\\|tool_response\\>).)+)?(?:\\<turn\\|\\>|\\<\\|tool_response\\>)?"
65
  },
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66
  "soc_token": "<|channel>",
67
  "sot_token": "<|turn>",
68
  "stc_token": "<|tool_call>",