Skip to content
Open

T5gemma2 #41834

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/source/en/_toctree.yml
Original file line number Diff line number Diff line change
Expand Up @@ -716,6 +716,8 @@
title: T5
- local: model_doc/t5gemma
title: T5Gemma
- local: model_doc/t5gemma2
title: T5Gemma2
- local: model_doc/t5v1.1
title: T5v1.1
- local: model_doc/tapex
Expand Down
109 changes: 109 additions & 0 deletions docs/source/en/model_doc/t5gemma2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@

<!--Copyright 2025 The HuggingFace Team. All rights reserved.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.

⚠️ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be
rendered properly in your Markdown viewer.

-->
<div style="float: right;">
<div class="flex flex-wrap space-x-1">
<img alt="PyTorch" src="https://img.shields.io/badge/PyTorch-DE3412?style=flat&logo=pytorch&logoColor=white">
<img alt="FlashAttention" src="https://img.shields.io/badge/%E2%9A%A1%EF%B8%8E%20FlashAttention-eae0c8?style=flat">
<img alt="SDPA" src="https://img.shields.io/badge/SDPA-DE3412?style=flat&logo=pytorch&logoColor=white">
</div>
</div>

# T5Gemma 2

T5Gemma 2 is a family of pretrained encoder-decoder large language models with strong multilingual, multimodal and long-context capability, available in 270M-270M, 1B-1B and 4B-4B parameters. Following T5Gemma, it is built via model adaptation (based on Gemma 3) using UL2. The architecture is similar to T5Gemma and Gemma 3, enhanced with tied word embeddings and merged self- and cross-attention to save model parameters.

> [!TIP]
> Click on the T5Gemma 2 models in the right sidebar for more examples of how to apply T5Gemma 2 to different language tasks.

The example below demonstrates how to chat with the model with [`Pipeline`] or the [`AutoModel`] class, and from the command line.

<hfoptions id="usage">
<hfoption id="Pipeline">

```python
import torch
from transformers import pipeline

generator = pipeline(
"image-text-to-text",
model="google/t5gemma-2-270m-270m",
dtype=torch.bfloat16,
device_map="auto",
)

generator(
"https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/bee.jpg",
text="<start_of_image> in this image, there is",
generate_kwargs={"do_sample": False, "max_new_tokens": 50},
)
```

</hfoption>
<hfoption id="AutoModel">

```python
import torch
import requests
from PIL import Image
from transformers import AutoProcessor, AutoModelForSeq2SeqLM

processor = AutoProcessor.from_pretrained("google/t5gemma-2-270m-270m")
model = AutoModelForSeq2SeqLM.from_pretrained(
"google/t5gemma-2-270m-270m",
device_map="auto",
dtype=torch.bfloat16,
)

url = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/bee.jpg"
image = Image.open(requests.get(url, stream=True).raw)
prompt = "<start_of_image> in this image, there is"

model_inputs = processor(text=prompt, images=image, return_tensors="pt")
generation = model.generate(**model_inputs, max_new_tokens=20, do_sample=False)
print(processor.decode(generation[0]))
```

</hfoption>
</hfoptions>

## T5Gemma2Config

[[autodoc]] T5Gemma2Config

## T5Gemma2ModuleConfig

[[autodoc]] T5Gemma2ModuleConfig

## T5Gemma2Model

[[autodoc]] T5Gemma2Model
- forward

## T5Gemma2ForConditionalGeneration

[[autodoc]] T5Gemma2ForConditionalGeneration
- forward

## T5Gemma2ForSequenceClassification

[[autodoc]] T5Gemma2ForSequenceClassification
- forward

## T5Gemma2ForTokenClassification

[[autodoc]] T5Gemma2ForTokenClassification
- forward
1 change: 1 addition & 0 deletions src/transformers/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,7 @@
from .switch_transformers import *
from .t5 import *
from .t5gemma import *
from .t5gemma2 import *
from .table_transformer import *
from .tapas import *
from .textnet import *
Expand Down
2 changes: 2 additions & 0 deletions src/transformers/models/auto/configuration_auto.py
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,7 @@
("switch_transformers", "SwitchTransformersConfig"),
("t5", "T5Config"),
("t5gemma", "T5GemmaConfig"),
("t5gemma2", "T5Gemma2Config"),
("table-transformer", "TableTransformerConfig"),
("tapas", "TapasConfig"),
("textnet", "TextNetConfig"),
Expand Down Expand Up @@ -855,6 +856,7 @@
("switch_transformers", "SwitchTransformers"),
("t5", "T5"),
("t5gemma", "T5Gemma"),
("t5gemma2", "T5Gemma2"),
("t5v1.1", "T5v1.1"),
("table-transformer", "Table Transformer"),
("tapas", "TAPAS"),
Expand Down
1 change: 1 addition & 0 deletions src/transformers/models/auto/image_processing_auto.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@
("swin", ("ViTImageProcessor", "ViTImageProcessorFast")),
("swin2sr", ("Swin2SRImageProcessor", "Swin2SRImageProcessorFast")),
("swinv2", ("ViTImageProcessor", "ViTImageProcessorFast")),
("t5gemma2", ("Gemma3ImageProcessor", "Gemma3ImageProcessorFast")),
("table-transformer", ("DetrImageProcessor", "DetrImageProcessorFast")),
("textnet", ("TextNetImageProcessor", "TextNetImageProcessorFast")),
("timesformer", ("VideoMAEImageProcessor", None)),
Expand Down
7 changes: 7 additions & 0 deletions src/transformers/models/auto/modeling_auto.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,7 @@ class _BaseModelWithGenerate(PreTrainedModel, GenerationMixin):
("switch_transformers", "SwitchTransformersModel"),
("t5", "T5Model"),
("t5gemma", "T5GemmaModel"),
("t5gemma2", "T5Gemma2Model"),
("table-transformer", "TableTransformerModel"),
("tapas", "TapasModel"),
("textnet", "TextNetModel"),
Expand Down Expand Up @@ -513,6 +514,7 @@ class _BaseModelWithGenerate(PreTrainedModel, GenerationMixin):
("switch_transformers", "SwitchTransformersForConditionalGeneration"),
("t5", "T5ForConditionalGeneration"),
("t5gemma", "T5GemmaForConditionalGeneration"),
("t5gemma2", "T5Gemma2ForConditionalGeneration"),
("tapas", "TapasForMaskedLM"),
("transfo-xl", "TransfoXLLMHeadModel"),
("tvlt", "TvltForPreTraining"),
Expand Down Expand Up @@ -612,6 +614,7 @@ class _BaseModelWithGenerate(PreTrainedModel, GenerationMixin):
("switch_transformers", "SwitchTransformersForConditionalGeneration"),
("t5", "T5ForConditionalGeneration"),
("t5gemma", "T5GemmaForConditionalGeneration"),
("t5gemma2", "T5Gemma2ForConditionalGeneration"),
("tapas", "TapasForMaskedLM"),
("transfo-xl", "TransfoXLLMHeadModel"),
("wav2vec2", "Wav2Vec2ForMaskedLM"),
Expand Down Expand Up @@ -1059,6 +1062,7 @@ class _BaseModelWithGenerate(PreTrainedModel, GenerationMixin):
("qwen3_vl_moe", "Qwen3VLMoeForConditionalGeneration"),
("shieldgemma2", "Gemma3ForConditionalGeneration"),
("smolvlm", "SmolVLMForConditionalGeneration"),
("t5gemma2", "T5Gemma2ForConditionalGeneration"),
("udop", "UdopForConditionalGeneration"),
("video_llama_3", "VideoLlama3ForConditionalGeneration"),
("vipllava", "VipLlavaForConditionalGeneration"),
Expand Down Expand Up @@ -1185,6 +1189,7 @@ class _BaseModelWithGenerate(PreTrainedModel, GenerationMixin):
("switch_transformers", "SwitchTransformersForConditionalGeneration"),
("t5", "T5ForConditionalGeneration"),
("t5gemma", "T5GemmaForConditionalGeneration"),
("t5gemma2", "T5Gemma2ForConditionalGeneration"),
("umt5", "UMT5ForConditionalGeneration"),
("voxtral", "VoxtralForConditionalGeneration"),
("xlm-prophetnet", "XLMProphetNetForConditionalGeneration"),
Expand Down Expand Up @@ -1314,6 +1319,7 @@ class _BaseModelWithGenerate(PreTrainedModel, GenerationMixin):
("starcoder2", "Starcoder2ForSequenceClassification"),
("t5", "T5ForSequenceClassification"),
("t5gemma", "T5GemmaForSequenceClassification"),
("t5gemma2", "T5Gemma2ForSequenceClassification"),
("tapas", "TapasForSequenceClassification"),
("transfo-xl", "TransfoXLForSequenceClassification"),
("umt5", "UMT5ForSequenceClassification"),
Expand Down Expand Up @@ -1521,6 +1527,7 @@ class _BaseModelWithGenerate(PreTrainedModel, GenerationMixin):
("starcoder2", "Starcoder2ForTokenClassification"),
("t5", "T5ForTokenClassification"),
("t5gemma", "T5GemmaForTokenClassification"),
("t5gemma2", "T5Gemma2ForTokenClassification"),
("umt5", "UMT5ForTokenClassification"),
("xlm", "XLMForTokenClassification"),
("xlm-roberta", "XLMRobertaForTokenClassification"),
Expand Down
1 change: 1 addition & 0 deletions src/transformers/models/auto/processing_auto.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@
("speech_to_text", "Speech2TextProcessor"),
("speech_to_text_2", "Speech2Text2Processor"),
("speecht5", "SpeechT5Processor"),
("t5gemma2", "Gemma3Processor"),
("trocr", "TrOCRProcessor"),
("tvlt", "TvltProcessor"),
("tvp", "TvpProcessor"),
Expand Down
7 changes: 7 additions & 0 deletions src/transformers/models/auto/tokenization_auto.py
Original file line number Diff line number Diff line change
Expand Up @@ -689,6 +689,13 @@
"GemmaTokenizerFast" if is_tokenizers_available() else None,
),
),
(
"t5gemma2",
(
"GemmaTokenizer" if is_sentencepiece_available() else None,
"GemmaTokenizerFast" if is_tokenizers_available() else None,
),
),
("tapas", ("TapasTokenizer", None)),
("tapex", ("TapexTokenizer", None)),
("transfo-xl", ("TransfoXLTokenizer", None)),
Expand Down
4 changes: 2 additions & 2 deletions src/transformers/models/t5gemma/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@


if TYPE_CHECKING:
from .configuration_encdecgemma2 import *
from .modeling_encdecgemma2 import *
from .configuration_t5gemma import *
from .modeling_t5gemma import *
else:
import sys

Expand Down
27 changes: 27 additions & 0 deletions src/transformers/models/t5gemma2/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Copyright 2024 The HuggingFace Team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from typing import TYPE_CHECKING

from ...utils import _LazyModule
from ...utils.import_utils import define_import_structure


if TYPE_CHECKING:
from .configuration_t5gemma2 import *
from .modeling_t5gemma2 import *
else:
import sys

_file = globals()["__file__"]
sys.modules[__name__] = _LazyModule(__name__, _file, define_import_structure(_file), module_spec=__spec__)
Loading