|
| 1 | + |
| 2 | +<!--Copyright 2025 The HuggingFace Team. All rights reserved. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
| 5 | +the License. You may obtain a copy of the License at |
| 6 | +
|
| 7 | +http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +
|
| 9 | +Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
| 10 | +an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 11 | +specific language governing permissions and limitations under the License. |
| 12 | +
|
| 13 | +⚠️ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be |
| 14 | +rendered properly in your Markdown viewer. |
| 15 | +
|
| 16 | +--> |
| 17 | +<div style="float: right;"> |
| 18 | + <div class="flex flex-wrap space-x-1"> |
| 19 | + <img alt="PyTorch" src="https://img.shields.io/badge/PyTorch-DE3412?style=flat&logo=pytorch&logoColor=white"> |
| 20 | + <img alt="FlashAttention" src="https://img.shields.io/badge/%E2%9A%A1%EF%B8%8E%20FlashAttention-eae0c8?style=flat"> |
| 21 | + <img alt="SDPA" src="https://img.shields.io/badge/SDPA-DE3412?style=flat&logo=pytorch&logoColor=white"> |
| 22 | + </div> |
| 23 | +</div> |
| 24 | + |
| 25 | +# T5Gemma 2 |
| 26 | + |
| 27 | +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. |
| 28 | + |
| 29 | +> [!TIP] |
| 30 | +> Click on the T5Gemma 2 models in the right sidebar for more examples of how to apply T5Gemma 2 to different language tasks. |
| 31 | +
|
| 32 | +The example below demonstrates how to chat with the model with [`Pipeline`] or the [`AutoModel`] class, and from the command line. |
| 33 | + |
| 34 | +<hfoptions id="usage"> |
| 35 | +<hfoption id="Pipeline"> |
| 36 | + |
| 37 | +```python |
| 38 | +import torch |
| 39 | +from transformers import pipeline |
| 40 | + |
| 41 | +generator = pipeline( |
| 42 | + "image-text-to-text", |
| 43 | + model="google/t5gemma-2-270m-270m", |
| 44 | + dtype=torch.bfloat16, |
| 45 | + device_map="auto", |
| 46 | +) |
| 47 | + |
| 48 | +generator( |
| 49 | + "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/bee.jpg", |
| 50 | + text="<start_of_image> in this image, there is", |
| 51 | + generate_kwargs={"do_sample": False, "max_new_tokens": 50}, |
| 52 | +) |
| 53 | +``` |
| 54 | + |
| 55 | +</hfoption> |
| 56 | +<hfoption id="AutoModel"> |
| 57 | + |
| 58 | +```python |
| 59 | +import torch |
| 60 | +import requests |
| 61 | +from PIL import Image |
| 62 | +from transformers import AutoProcessor, AutoModelForSeq2SeqLM |
| 63 | + |
| 64 | +processor = AutoProcessor.from_pretrained("google/t5gemma-2-270m-270m") |
| 65 | +model = AutoModelForSeq2SeqLM.from_pretrained( |
| 66 | + "google/t5gemma-2-270m-270m", |
| 67 | + device_map="auto", |
| 68 | + dtype=torch.bfloat16, |
| 69 | +) |
| 70 | + |
| 71 | +url = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/bee.jpg" |
| 72 | +image = Image.open(requests.get(url, stream=True).raw) |
| 73 | +prompt = "<start_of_image> in this image, there is" |
| 74 | + |
| 75 | +model_inputs = processor(text=prompt, images=image, return_tensors="pt") |
| 76 | +generation = model.generate(**model_inputs, max_new_tokens=20, do_sample=False) |
| 77 | +print(processor.decode(generation[0])) |
| 78 | +``` |
| 79 | + |
| 80 | +</hfoption> |
| 81 | +</hfoptions> |
| 82 | + |
| 83 | +## T5Gemma2Config |
| 84 | + |
| 85 | +[[autodoc]] T5Gemma2Config |
| 86 | + |
| 87 | +## T5Gemma2ModuleConfig |
| 88 | + |
| 89 | +[[autodoc]] T5Gemma2ModuleConfig |
| 90 | + |
| 91 | +## T5Gemma2Model |
| 92 | + |
| 93 | +[[autodoc]] T5Gemma2Model |
| 94 | + - forward |
| 95 | + |
| 96 | +## T5Gemma2ForConditionalGeneration |
| 97 | + |
| 98 | +[[autodoc]] T5Gemma2ForConditionalGeneration |
| 99 | + - forward |
| 100 | + |
| 101 | +## T5Gemma2ForSequenceClassification |
| 102 | + |
| 103 | +[[autodoc]] T5Gemma2ForSequenceClassification |
| 104 | + - forward |
| 105 | + |
| 106 | +## T5Gemma2ForTokenClassification |
| 107 | + |
| 108 | +[[autodoc]] T5Gemma2ForTokenClassification |
| 109 | + - forward |
0 commit comments