Instructions to use google/gemma-4-E4B with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use google/gemma-4-E4B with Transformers:
# Load model directly from transformers import AutoProcessor, AutoModelForMultimodalLM processor = AutoProcessor.from_pretrained("google/gemma-4-E4B") model = AutoModelForMultimodalLM.from_pretrained("google/gemma-4-E4B") - Notebooks
- Google Colab
- Kaggle
DSFSDFDSFE
#2
by zaid-peerbits - opened
README.md
CHANGED
|
@@ -14,21 +14,20 @@ pipeline_tag: any-to-any
|
|
| 14 |
<a href="https://huggingface.co/collections/google/gemma-4" target="_blank">Hugging Face</a> |
|
| 15 |
<a href="https://github.com/google-gemma" target="_blank">GitHub</a> |
|
| 16 |
<a href="https://blog.google/innovation-and-ai/technology/developers-tools/gemma-4/" target="_blank">Launch Blog</a> |
|
| 17 |
-
<a href="https://ai.google.dev/gemma/docs/core" target="_blank">Documentation</a>
|
| 18 |
-
<a href="https://arxiv.org/abs/2607.02770" target="_blank">Technical Report</a>
|
| 19 |
<br>
|
| 20 |
<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>
|
| 21 |
</p>
|
| 22 |
|
| 23 |
-
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
|
| 24 |
|
| 25 |
-
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
|
| 26 |
|
| 27 |
Gemma 4 introduces key **capability and architectural advancements**:
|
| 28 |
|
| 29 |
* **Reasoning** β All models in the family are designed as highly capable reasoners, with configurable thinking modes.
|
| 30 |
|
| 31 |
-
* **Extended Multimodalities** β Processes Text, Image with variable aspect ratio and resolution support (all models), Video, and Audio (featured natively on the E2B
|
| 32 |
|
| 33 |
* **Diverse & Efficient Architectures** β Offers Dense and Mixture-of-Experts (MoE) variants of different sizes for scalable deployment.
|
| 34 |
|
|
@@ -42,27 +41,25 @@ Gemma 4 introduces key **capability and architectural advancements**:
|
|
| 42 |
|
| 43 |
## **Models Overview**
|
| 44 |
|
| 45 |
-
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 (
|
| 46 |
|
| 47 |
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).
|
| 48 |
|
| 49 |
### Dense Models
|
| 50 |
|
| 51 |
-
| Property | E2B | E4B |
|
| 52 |
-
| :---- | :---- | :---- | :---- |
|
| 53 |
-
| **Total Parameters** | 2.3B effective
|
| 54 |
-
| **Layers** | 35 | 42 |
|
| 55 |
-
| **Sliding Window** | 512 tokens | 512 tokens | 1024 tokens |
|
| 56 |
-
| **Context Length** | 128K tokens | 128K tokens | 256K tokens |
|
| 57 |
-
| **Vocabulary Size** | 262K | 262K | 262K |
|
| 58 |
-
| **Supported Modalities** | Text, Image, Audio | Text, Image, Audio | Text, Image
|
| 59 |
-
| **Vision Encoder Parameters** | *~150M* | *~150M* |
|
| 60 |
-
| **Audio Encoder Parameters** | *~300M* | *~300M* |
|
| 61 |
|
| 62 |
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.
|
| 63 |
|
| 64 |
-
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.
|
| 65 |
-
|
| 66 |
### Mixture-of-Experts (MoE) Model
|
| 67 |
|
| 68 |
| Property | 26B A4B MoE |
|
|
@@ -83,44 +80,42 @@ The "A" in 26B A4B stands for "active parameters" in contrast to the total numbe
|
|
| 83 |
|
| 84 |
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.
|
| 85 |
|
| 86 |
-
| | Gemma 4 31B | Gemma 4 26B A4B | Gemma 4
|
| 87 |
-
| :---- | :---- | :---- | :---- | :---- | :---- |
|
| 88 |
-
| MMLU Pro | 85.2% | 82.6% |
|
| 89 |
-
| AIME 2026 no tools | 89.2% | 88.3% |
|
| 90 |
-
| LiveCodeBench v6 | 80.0% | 77.1% |
|
| 91 |
-
| Codeforces ELO | 2150 | 1718 |
|
| 92 |
-
| GPQA Diamond | 84.3% | 82.3% |
|
| 93 |
-
| Tau2 (average over 3) | 76.9% | 68.2% |
|
| 94 |
-
| HLE no tools | 19.5% | 8.7% |
|
| 95 |
-
| HLE with search | 26.5% | 17.2% | - | - | - |
|
| 96 |
-
| BigBench Extra Hard | 74.4% | 64.8% |
|
| 97 |
-
| MMMLU | 88.4% | 86.3% |
|
| 98 |
-
| **Vision** | | | | | |
|
| 99 |
-
| MMMU Pro | 76.9% | 73.8% |
|
| 100 |
-
| OmniDocBench 1.5 (average edit distance, lower is better) | 0.131 | 0.149 | 0.
|
| 101 |
-
| MATH-Vision | 85.6% | 82.4% |
|
| 102 |
-
| MedXPertQA MM | 61.3% | 58.1% |
|
| 103 |
-
| **Audio** | | | | | |
|
| 104 |
-
| CoVoST | - | - |
|
| 105 |
-
| FLEURS (lower is better) | - | - | 0.
|
| 106 |
-
| **Long Context** | | | | | |
|
| 107 |
-
| MRCR v2 8 needle 128k (average) | 66.4% | 44.1% |
|
| 108 |
-
|
| 109 |
-
<sup>*</sup>Excluding Chinese language.
|
| 110 |
|
| 111 |
## **Core Capabilities**
|
| 112 |
|
| 113 |
Gemma 4 models handle a broad range of tasks across text, vision, and audio. Key capabilities include:
|
| 114 |
|
| 115 |
* **Thinking** β Built-in reasoning mode that lets the model think step-by-step before answering.
|
| 116 |
-
* **Long Context** β Context windows of up to 128K tokens (E2B/E4B) and 256K tokens (
|
| 117 |
* **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.
|
| 118 |
* **Video Understanding** β Analyze video by processing sequences of frames.
|
| 119 |
* **Interleaved Multimodal Input** β Freely mix text and images in any order within a single prompt.
|
| 120 |
* **Function Calling** β Native support for structured tool use, enabling agentic workflows.
|
| 121 |
* **Coding** β Code generation, completion, and correction.
|
| 122 |
* **Multilingual** β Out-of-the-box support for 35+ languages, pre-trained on 140+ languages.
|
| 123 |
-
* **Audio** (E2B
|
| 124 |
|
| 125 |
## Getting Started
|
| 126 |
|
|
@@ -131,13 +126,13 @@ You can use all Gemma 4 models with the latest version of Transformers. To get s
|
|
| 131 |
Once you have everything installed, you can proceed to load the model with the code below:
|
| 132 |
|
| 133 |
```python
|
| 134 |
-
from transformers import AutoProcessor,
|
| 135 |
|
| 136 |
MODEL_ID = "google/gemma-4-E4B-it"
|
| 137 |
|
| 138 |
# Load model
|
| 139 |
processor = AutoProcessor.from_pretrained(MODEL_ID)
|
| 140 |
-
model =
|
| 141 |
MODEL_ID,
|
| 142 |
dtype="auto",
|
| 143 |
device_map="auto"
|
|
@@ -154,14 +149,13 @@ messages = [
|
|
| 154 |
]
|
| 155 |
|
| 156 |
# Process input
|
| 157 |
-
|
| 158 |
-
messages,
|
| 159 |
-
tokenize=
|
| 160 |
-
|
| 161 |
-
return_tensors="pt",
|
| 162 |
-
add_generation_prompt=True,
|
| 163 |
enable_thinking=False
|
| 164 |
-
)
|
|
|
|
| 165 |
input_len = inputs["input_ids"].shape[-1]
|
| 166 |
|
| 167 |
# Generate output
|
|
@@ -174,12 +168,13 @@ processor.parse_response(response)
|
|
| 174 |
|
| 175 |
To enable reasoning, set `enable_thinking=True` and the `parse_response` function will take care of parsing the thinking output.
|
| 176 |
|
| 177 |
-
Below, you will also find snippets for processing audio (E2B
|
| 178 |
|
| 179 |
<details>
|
| 180 |
<summary>Code for processing Audio</summary>
|
| 181 |
|
| 182 |
-
|
|
|
|
| 183 |
|
| 184 |
`pip install -U transformers torch torchvision librosa accelerate`
|
| 185 |
|
|
@@ -203,13 +198,13 @@ Once the model is loaded, you can start generating output by directly referencin
|
|
| 203 |
|
| 204 |
|
| 205 |
```python
|
| 206 |
-
# Prompt - add audio
|
| 207 |
messages = [
|
| 208 |
{
|
| 209 |
"role": "user",
|
| 210 |
"content": [
|
|
|
|
| 211 |
{"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."},
|
| 212 |
-
{"type": "audio", "audio": "https://raw.githubusercontent.com/google-gemma/cookbook/refs/heads/main/apps/sample-data/journal1.wav"},
|
| 213 |
]
|
| 214 |
}
|
| 215 |
]
|
|
@@ -237,7 +232,7 @@ processor.parse_response(response)
|
|
| 237 |
<details>
|
| 238 |
<summary>Code for processing Images</summary>
|
| 239 |
|
| 240 |
-
|
| 241 |
|
| 242 |
|
| 243 |
`pip install -U transformers torch torchvision accelerate`
|
|
@@ -266,7 +261,7 @@ Once the model is loaded, you can start generating output by directly referencin
|
|
| 266 |
messages = [
|
| 267 |
{
|
| 268 |
"role": "user", "content": [
|
| 269 |
-
{"type": "image", "url": "https://raw.githubusercontent.com/google-gemma/cookbook/refs/heads/main/
|
| 270 |
{"type": "text", "text": "What is shown in this image?"}
|
| 271 |
]
|
| 272 |
}
|
|
@@ -296,7 +291,7 @@ processor.parse_response(response)
|
|
| 296 |
<details>
|
| 297 |
<summary>Code for processing Videos</summary>
|
| 298 |
|
| 299 |
-
|
| 300 |
|
| 301 |
`pip install -U transformers torch torchvision librosa accelerate`
|
| 302 |
|
|
@@ -352,7 +347,6 @@ processor.parse_response(response)
|
|
| 352 |
</details>
|
| 353 |
|
| 354 |
|
| 355 |
-
|
| 356 |
## **Best Practices**
|
| 357 |
|
| 358 |
For the best performance, use these configurations and best practices:
|
|
@@ -380,14 +374,11 @@ Compared to Gemma 3, the models use standard `system`, `assistant`, and `user` r
|
|
| 380 |
|
| 381 |
### 3. Multi-Turn Conversations
|
| 382 |
|
| 383 |
-
* **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
|
| 384 |
|
| 385 |
### 4. Modality order
|
| 386 |
|
| 387 |
-
For optimal performance with multimodal inputs, place
|
| 388 |
-
|
| 389 |
-
* Image content **before** the text in your prompt.
|
| 390 |
-
* Audio content **after** the text in your prompt.
|
| 391 |
|
| 392 |
### 5. Variable Image Resolution
|
| 393 |
|
|
@@ -420,7 +411,7 @@ When formatting the answer, first output the transcription in {SOURCE_LANGUAGE},
|
|
| 420 |
|
| 421 |
### 7. Audio and Video Length
|
| 422 |
|
| 423 |
-
All models support image inputs and can process videos as frames whereas the E2B
|
| 424 |
|
| 425 |
## **Model Data**
|
| 426 |
|
|
@@ -476,7 +467,7 @@ Multimodal models (capable of processing vision, language, and/or audio) have a
|
|
| 476 |
* **Chatbots and Conversational AI**: Power conversational interfaces for customer service, virtual assistants, or interactive applications.
|
| 477 |
* **Text Summarization**: Generate concise summaries of a text corpus, research papers, or reports.
|
| 478 |
* **Image Data Extraction**: These models can be used to extract, interpret, and summarize visual data for text communications.
|
| 479 |
-
* **Audio Processing and Interaction**: The
|
| 480 |
* **Research and Education**
|
| 481 |
* **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.
|
| 482 |
* **Language Learning Tools**: Support interactive language learning experiences, aiding in grammar correction or providing writing practice.
|
|
@@ -520,19 +511,3 @@ The development of vision-language models (VLMs) raises several ethical concerns
|
|
| 520 |
### **Benefits**
|
| 521 |
|
| 522 |
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.
|
| 523 |
-
|
| 524 |
-
## **Citation**
|
| 525 |
-
|
| 526 |
-
If you find our work helpful, please consider citing it:
|
| 527 |
-
|
| 528 |
-
```bibtex
|
| 529 |
-
@misc{gemmateam2026gemma4,
|
| 530 |
-
title={Gemma 4 Technical Report},
|
| 531 |
-
author={Gemma Team},
|
| 532 |
-
year={2026},
|
| 533 |
-
eprint={2607.02770},
|
| 534 |
-
archivePrefix={arXiv},
|
| 535 |
-
primaryClass={cs.CL},
|
| 536 |
-
url={https://arxiv.org/abs/2607.02770},
|
| 537 |
-
}
|
| 538 |
-
```
|
|
|
|
| 14 |
<a href="https://huggingface.co/collections/google/gemma-4" target="_blank">Hugging Face</a> |
|
| 15 |
<a href="https://github.com/google-gemma" target="_blank">GitHub</a> |
|
| 16 |
<a href="https://blog.google/innovation-and-ai/technology/developers-tools/gemma-4/" target="_blank">Launch Blog</a> |
|
| 17 |
+
<a href="https://ai.google.dev/gemma/docs/core" target="_blank">Documentation</a>
|
|
|
|
| 18 |
<br>
|
| 19 |
<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>
|
| 20 |
</p>
|
| 21 |
|
| 22 |
+
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.
|
| 23 |
|
| 24 |
+
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.
|
| 25 |
|
| 26 |
Gemma 4 introduces key **capability and architectural advancements**:
|
| 27 |
|
| 28 |
* **Reasoning** β All models in the family are designed as highly capable reasoners, with configurable thinking modes.
|
| 29 |
|
| 30 |
+
* **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).
|
| 31 |
|
| 32 |
* **Diverse & Efficient Architectures** β Offers Dense and Mixture-of-Experts (MoE) variants of different sizes for scalable deployment.
|
| 33 |
|
|
|
|
| 41 |
|
| 42 |
## **Models Overview**
|
| 43 |
|
| 44 |
+
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.
|
| 45 |
|
| 46 |
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).
|
| 47 |
|
| 48 |
### Dense Models
|
| 49 |
|
| 50 |
+
| Property | E2B | E4B | 31B Dense |
|
| 51 |
+
| :---- | :---- | :---- | :---- |
|
| 52 |
+
| **Total Parameters** | 2.3B effective (5.1B with embeddings) | 4.5B effective (8B with embeddings) | 30.7B |
|
| 53 |
+
| **Layers** | 35 | 42 | 60 |
|
| 54 |
+
| **Sliding Window** | 512 tokens | 512 tokens | 1024 tokens |
|
| 55 |
+
| **Context Length** | 128K tokens | 128K tokens | 256K tokens |
|
| 56 |
+
| **Vocabulary Size** | 262K | 262K | 262K |
|
| 57 |
+
| **Supported Modalities** | Text, Image, Audio | Text, Image, Audio | Text, Image |
|
| 58 |
+
| **Vision Encoder Parameters** | *~150M* | *~150M* | *~550M* |
|
| 59 |
+
| **Audio Encoder Parameters** | *~300M* | *~300M* | No Audio |
|
| 60 |
|
| 61 |
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.
|
| 62 |
|
|
|
|
|
|
|
| 63 |
### Mixture-of-Experts (MoE) Model
|
| 64 |
|
| 65 |
| Property | 26B A4B MoE |
|
|
|
|
| 80 |
|
| 81 |
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.
|
| 82 |
|
| 83 |
+
| | Gemma 4 31B | Gemma 4 26B A4B | Gemma 4 E4B | Gemma 4 E2B | Gemma 3 27B (no think) |
|
| 84 |
+
| :---- | :---- | :---- | :---- | :---- | :---- |
|
| 85 |
+
| MMLU Pro | 85.2% | 82.6% | 69.4% | 60.0% | 67.6% |
|
| 86 |
+
| AIME 2026 no tools | 89.2% | 88.3% | 42.5% | 37.5% | 20.8% |
|
| 87 |
+
| LiveCodeBench v6 | 80.0% | 77.1% | 52.0% | 44.0% | 29.1% |
|
| 88 |
+
| Codeforces ELO | 2150 | 1718 | 940 | 633 | 110 |
|
| 89 |
+
| GPQA Diamond | 84.3% | 82.3% | 58.6% | 43.4% | 42.4% |
|
| 90 |
+
| Tau2 (average over 3) | 76.9% | 68.2% | 42.2% | 24.5% | 16.2% |
|
| 91 |
+
| HLE no tools | 19.5% | 8.7% | - | - | - |
|
| 92 |
+
| HLE with search | 26.5% | 17.2% | - | - | - |
|
| 93 |
+
| BigBench Extra Hard | 74.4% | 64.8% | 33.1% | 21.9% | 19.3% |
|
| 94 |
+
| MMMLU | 88.4% | 86.3% | 76.6% | 67.4% | 70.7% |
|
| 95 |
+
| **Vision** | | | | | |
|
| 96 |
+
| MMMU Pro | 76.9% | 73.8% | 52.6% | 44.2% | 49.7% |
|
| 97 |
+
| OmniDocBench 1.5 (average edit distance, lower is better) | 0.131 | 0.149 | 0.181 | 0.290 | 0.365 |
|
| 98 |
+
| MATH-Vision | 85.6% | 82.4% | 59.5% | 52.4% | 46.0% |
|
| 99 |
+
| MedXPertQA MM | 61.3% | 58.1% | 28.7% | 23.5% | - |
|
| 100 |
+
| **Audio** | | | | | |
|
| 101 |
+
| CoVoST | - | - | 35.54 | 33.47 | - |
|
| 102 |
+
| FLEURS (lower is better) | - | - | 0.08 | 0.09 | - |
|
| 103 |
+
| **Long Context** | | | | | |
|
| 104 |
+
| MRCR v2 8 needle 128k (average) | 66.4% | 44.1% | 25.4% | 19.1% | 13.5% |
|
|
|
|
|
|
|
| 105 |
|
| 106 |
## **Core Capabilities**
|
| 107 |
|
| 108 |
Gemma 4 models handle a broad range of tasks across text, vision, and audio. Key capabilities include:
|
| 109 |
|
| 110 |
* **Thinking** β Built-in reasoning mode that lets the model think step-by-step before answering.
|
| 111 |
+
* **Long Context** β Context windows of up to 128K tokens (E2B/E4B) and 256K tokens (26B A4B/31B).
|
| 112 |
* **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.
|
| 113 |
* **Video Understanding** β Analyze video by processing sequences of frames.
|
| 114 |
* **Interleaved Multimodal Input** β Freely mix text and images in any order within a single prompt.
|
| 115 |
* **Function Calling** β Native support for structured tool use, enabling agentic workflows.
|
| 116 |
* **Coding** β Code generation, completion, and correction.
|
| 117 |
* **Multilingual** β Out-of-the-box support for 35+ languages, pre-trained on 140+ languages.
|
| 118 |
+
* **Audio** (E2B and E4B only) β Automatic speech recognition (ASR) and speech-to-translated-text translation across multiple languages.
|
| 119 |
|
| 120 |
## Getting Started
|
| 121 |
|
|
|
|
| 126 |
Once you have everything installed, you can proceed to load the model with the code below:
|
| 127 |
|
| 128 |
```python
|
| 129 |
+
from transformers import AutoProcessor, AutoModelForCausalLM
|
| 130 |
|
| 131 |
MODEL_ID = "google/gemma-4-E4B-it"
|
| 132 |
|
| 133 |
# Load model
|
| 134 |
processor = AutoProcessor.from_pretrained(MODEL_ID)
|
| 135 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 136 |
MODEL_ID,
|
| 137 |
dtype="auto",
|
| 138 |
device_map="auto"
|
|
|
|
| 149 |
]
|
| 150 |
|
| 151 |
# Process input
|
| 152 |
+
text = processor.apply_chat_template(
|
| 153 |
+
messages,
|
| 154 |
+
tokenize=False,
|
| 155 |
+
add_generation_prompt=True,
|
|
|
|
|
|
|
| 156 |
enable_thinking=False
|
| 157 |
+
)
|
| 158 |
+
inputs = processor(text=text, return_tensors="pt").to(model.device)
|
| 159 |
input_len = inputs["input_ids"].shape[-1]
|
| 160 |
|
| 161 |
# Generate output
|
|
|
|
| 168 |
|
| 169 |
To enable reasoning, set `enable_thinking=True` and the `parse_response` function will take care of parsing the thinking output.
|
| 170 |
|
| 171 |
+
Below, you will also find snippets for processing audio (E2B and E4B only), images, and video alongside text:
|
| 172 |
|
| 173 |
<details>
|
| 174 |
<summary>Code for processing Audio</summary>
|
| 175 |
|
| 176 |
+
Instead of using `AutoModelForCausalLM`, you can use `AutoModelForMultimodalLM` to process audio. To use it, make sure to install the following packages:
|
| 177 |
+
|
| 178 |
|
| 179 |
`pip install -U transformers torch torchvision librosa accelerate`
|
| 180 |
|
|
|
|
| 198 |
|
| 199 |
|
| 200 |
```python
|
| 201 |
+
# Prompt - add audio before text
|
| 202 |
messages = [
|
| 203 |
{
|
| 204 |
"role": "user",
|
| 205 |
"content": [
|
| 206 |
+
{"type": "audio", "audio": "https://raw.githubusercontent.com/google-gemma/cookbook/refs/heads/main/Demos/sample-data/journal1.wav"},
|
| 207 |
{"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."},
|
|
|
|
| 208 |
]
|
| 209 |
}
|
| 210 |
]
|
|
|
|
| 232 |
<details>
|
| 233 |
<summary>Code for processing Images</summary>
|
| 234 |
|
| 235 |
+
Instead of using `AutoModelForCausalLM`, you can use `AutoModelForMultimodalLM` to process images. To use it, make sure to install the following packages:
|
| 236 |
|
| 237 |
|
| 238 |
`pip install -U transformers torch torchvision accelerate`
|
|
|
|
| 261 |
messages = [
|
| 262 |
{
|
| 263 |
"role": "user", "content": [
|
| 264 |
+
{"type": "image", "url": "https://raw.githubusercontent.com/google-gemma/cookbook/refs/heads/main/Demos/sample-data/GoldenGate.png"},
|
| 265 |
{"type": "text", "text": "What is shown in this image?"}
|
| 266 |
]
|
| 267 |
}
|
|
|
|
| 291 |
<details>
|
| 292 |
<summary>Code for processing Videos</summary>
|
| 293 |
|
| 294 |
+
Instead of using `AutoModelForCausalLM`, you can use `AutoModelForMultimodalLM` to process videos. To use it, make sure to install the following packages:
|
| 295 |
|
| 296 |
`pip install -U transformers torch torchvision librosa accelerate`
|
| 297 |
|
|
|
|
| 347 |
</details>
|
| 348 |
|
| 349 |
|
|
|
|
| 350 |
## **Best Practices**
|
| 351 |
|
| 352 |
For the best performance, use these configurations and best practices:
|
|
|
|
| 374 |
|
| 375 |
### 3. Multi-Turn Conversations
|
| 376 |
|
| 377 |
+
* **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.
|
| 378 |
|
| 379 |
### 4. Modality order
|
| 380 |
|
| 381 |
+
* For optimal performance with multimodal inputs, place image and/or audio content **before** the text in your prompt.
|
|
|
|
|
|
|
|
|
|
| 382 |
|
| 383 |
### 5. Variable Image Resolution
|
| 384 |
|
|
|
|
| 411 |
|
| 412 |
### 7. Audio and Video Length
|
| 413 |
|
| 414 |
+
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.
|
| 415 |
|
| 416 |
## **Model Data**
|
| 417 |
|
|
|
|
| 467 |
* **Chatbots and Conversational AI**: Power conversational interfaces for customer service, virtual assistants, or interactive applications.
|
| 468 |
* **Text Summarization**: Generate concise summaries of a text corpus, research papers, or reports.
|
| 469 |
* **Image Data Extraction**: These models can be used to extract, interpret, and summarize visual data for text communications.
|
| 470 |
+
* **Audio Processing and Interaction**: The smaller models (E2B and E4B) can analyze and interpret audio inputs, enabling voice-driven interactions and transcriptions.
|
| 471 |
* **Research and Education**
|
| 472 |
* **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.
|
| 473 |
* **Language Learning Tools**: Support interactive language learning experiences, aiding in grammar correction or providing writing practice.
|
|
|
|
| 511 |
### **Benefits**
|
| 512 |
|
| 513 |
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.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|