File size: 10,123 Bytes
a9bd396
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275

<!--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="SDPA" src="https://img.shields.io/badge/SDPA-DE3412?style=flat&logo=pytorch&logoColor=white">
    </div>
</div>

# Gemma 3 [[gemma3]]

[Gemma 3](https://goo.gle/Gemma3Report)๋Š” ์‚ฌ์ „ ํ›ˆ๋ จ๋œ ๋ฒ„์ „๊ณผ ์ง€์‹œ๋ฌธ ์กฐ์ • ๋ฒ„์ „์„ ๊ฐ–์ถ˜ ๋ฉ€ํ‹ฐ๋ชจ๋‹ฌ ๋ชจ๋ธ๋กœ, 1B, 13B, 27B ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ œ๊ณต๋ฉ๋‹ˆ๋‹ค. ์•„ํ‚คํ…์ฒ˜๋Š” ์ด์ „ Gemma ๋ฒ„์ „๊ณผ ๋Œ€๋ถ€๋ถ„ ๋™์ผํ•ฉ๋‹ˆ๋‹ค. ์ฃผ์š” ์ฐจ์ด์ ์€ ๋ชจ๋“  ๊ธ€๋กœ๋ฒŒ ์…€ํ”„ ์–ดํ…์…˜ ๋ ˆ์ด์–ด๋งˆ๋‹ค 5๊ฐœ์˜ ๋กœ์ปฌ ์Šฌ๋ผ์ด๋”ฉ ์œˆ๋„์šฐ ์…€ํ”„ ์–ดํ…์…˜ ๋ ˆ์ด์–ด๋ฅผ ๋ฒˆ๊ฐˆ์•„ ์‚ฌ์šฉํ•˜๋Š” ์ , 128K ํ† ํฐ์˜ ๋” ๊ธด ์ปจํ…์ŠคํŠธ ๊ธธ์ด๋ฅผ ์ง€์›ํ•˜๋Š” ์ , ๊ทธ๋ฆฌ๊ณ  ๊ณ ํ•ด์ƒ๋„ ์ด๋ฏธ์ง€๋‚˜ ์ •์‚ฌ๊ฐํ˜•์ด ์•„๋‹Œ ์ข…ํšก๋น„์˜ ์ด๋ฏธ์ง€์—์„œ ์ •๋ณด๊ฐ€ ์‚ฌ๋ผ์ง€๋Š” ๊ฒƒ์„ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•ด ๊ณ ํ•ด์ƒ๋„ ์ด๋ฏธ์ง€๋ฅผ "ํŒจ๋‹ ๋ฐ ์Šค์บ๋‹"ํ•  ์ˆ˜ ์žˆ๋Š” [SigLip](./siglip) ์ธ์ฝ”๋”๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค๋Š” ์ ์ž…๋‹ˆ๋‹ค.

์ง€์‹œ๋ฌธ ์กฐ์ • ๋ฒ„์ „์€ ์ง€์‹ ์ฆ๋ฅ˜ ๋ฐ ๊ฐ•ํ™” ํ•™์Šต์œผ๋กœ ํ›„์† ํ•™์Šต๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

Gemma 3์˜ ๋ชจ๋“  ์›๋ณธ ์ฒดํฌํฌ์ธํŠธ๋Š” [Gemma 3](https://huggingface.co/collections/google/gemma-3-release-67c6c6f89c4f76621268bb6d) ๋ฆด๋ฆฌ์Šค์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

> [!ํŒ]
> Gemma๋ฅผ ๋‹ค์–‘ํ•œ ๋น„์ „ ๋ฐ ์–ธ์–ด ์ž‘์—…์— ์ ์šฉํ•˜๋Š” ์ถ”๊ฐ€ ์˜ˆ์‹œ๋ฅผ ๋ณด๋ ค๋ฉด ์˜ค๋ฅธ์ชฝ ์‚ฌ์ด๋“œ๋ฐ”์˜ Gemma 3 ๋ชจ๋ธ์„ ํด๋ฆญํ•˜์„ธ์š”.

์•„๋ž˜ ์˜ˆ์‹œ๋Š” [`Pipeline`] ๋˜๋Š” [`AutoModel`] ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ด๋ฏธ์ง€๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ํ…์ŠคํŠธ๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค.

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

```py
import torch
from transformers import pipeline

pipeline = pipeline(
    task="image-text-to-text",
    model="google/gemma-3-4b-pt",
    device=0,
    dtype=torch.bfloat16
)
pipeline(
    "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/pipeline-cat-chonk.jpeg",
    text="<start_of_image> What is shown in this image?"
)
```

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

```py
import torch
from transformers import AutoProcessor, Gemma3ForConditionalGeneration

model = Gemma3ForConditionalGeneration.from_pretrained(
    "google/gemma-3-4b-it",
    dtype=torch.bfloat16,
    device_map="auto",
    attn_implementation="sdpa"
)
processor = AutoProcessor.from_pretrained(
    "google/gemma-3-4b-it",
    padding_side="left"
)

messages = [
    {
        "role": "system",
        "content": [
            {"type": "text", "text": "You are a helpful assistant."}
        ]
    },
    {
        "role": "user", "content": [
            {"type": "image", "url": "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/pipeline-cat-chonk.jpeg"},
            {"type": "text", "text": "What is shown in this image?"},
        ]
    },
]
inputs = processor.apply_chat_template(
    messages,
    tokenize=True,
    return_dict=True,
    return_tensors="pt",
    add_generation_prompt=True,
).to(model.device)

output = model.generate(**inputs, max_new_tokens=50, cache_implementation="static")
print(processor.decode(output[0], skip_special_tokens=True))
```

</hfoption>
<hfoption id="transformers CLI">

```bash
echo -e "Plants create energy through a process known as" | transformers run --task text-generation --model google/gemma-3-1b-pt --device 0
```

</hfoption>
</hfoptions>

์–‘์žํ™”๋Š” ๊ฐ€์ค‘์น˜๋ฅผ ๋” ๋‚ฎ์€ ์ •๋ฐ€๋„๋กœ ํ‘œํ˜„ํ•˜์—ฌ, ํฐ ๋ชจ๋ธ์˜ ๋ฉ”๋ชจ๋ฆฌ ๋ถ€๋‹ด์„ ์ค„์—ฌ์ค๋‹ˆ๋‹ค. ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ์–‘์žํ™” ๋ฐฑ์—”๋“œ์— ๋Œ€ํ•œ ๋” ์ž์„ธํ•œ ๋‚ด์šฉ์€ [์–‘์žํ™”](../quantization/overview) ๊ฐœ์š”๋ฅผ ์ฐธ๊ณ ํ•˜์„ธ์š”.

์•„๋ž˜ ์˜ˆ์ œ์—์„œ๋Š” [torchao](../quantization/torchao)๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ€์ค‘์น˜๋ฅผ int4๋กœ๋งŒ ์–‘์žํ™”ํ•ฉ๋‹ˆ๋‹ค.

```py
# pip install torchao
import torch
from transformers import TorchAoConfig, Gemma3ForConditionalGeneration, AutoProcessor

quantization_config = TorchAoConfig("int4_weight_only", group_size=128)
model = Gemma3ForConditionalGeneration.from_pretrained(
    "google/gemma-3-27b-it",
    dtype=torch.bfloat16,
    device_map="auto",
    quantization_config=quantization_config
)
processor = AutoProcessor.from_pretrained(
    "google/gemma-3-27b-it",
    padding_side="left"
)

messages = [
    {
        "role": "system",
        "content": [
            {"type": "text", "text": "You are a helpful assistant."}
        ]
    },
    {
        "role": "user", "content": [
            {"type": "image", "url": "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/pipeline-cat-chonk.jpeg"},
            {"type": "text", "text": "What is shown in this image?"},
        ]
    },
]
inputs = processor.apply_chat_template(
    messages,
    tokenize=True,
    return_dict=True,
    return_tensors="pt",
    add_generation_prompt=True,
).to(model.device)

output = model.generate(**inputs, max_new_tokens=50, cache_implementation="static")
print(processor.decode(output[0], skip_special_tokens=True))
```

[AttentionMaskVisualizer](https://github.com/huggingface/transformers/blob/beb9b5b02246b9b7ee81ddf938f93f44cfeaad19/src/transformers/utils/attention_visualizer.py#L139)๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ชจ๋ธ์ด ์ฃผ๋ชฉํ•  ์ˆ˜ ์žˆ๋Š” ํ† ํฐ๊ณผ ์ฃผ๋ชฉํ•  ์ˆ˜ ์—†๋Š” ํ† ํฐ์„ ๋” ์ž˜ ์ดํ•ดํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

```py
from transformers.utils.attention_visualizer import AttentionMaskVisualizer

visualizer = AttentionMaskVisualizer("google/gemma-3-4b-it")
visualizer("<img>What is shown in this image?")
```

<div class="flex justify-center">
    <img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/model_doc/gemma-3-attn-mask.png"/>
</div>

## ๋…ธํŠธ [[notes]]

- ์ด๋ฏธ์ง€-ํ…์ŠคํŠธ ๋ฐ ์ด๋ฏธ์ง€ ์ „์šฉ ์ž…๋ ฅ์—๋Š” [`Gemma3ForConditionalGeneration`]์„ ์‚ฌ์šฉํ•˜์„ธ์š”.
- Gemma 3๋Š” ๋‹ค์ค‘ ์ž…๋ ฅ ์ด๋ฏธ์ง€๋ฅผ ์ง€์›ํ•˜์ง€๋งŒ, ํ”„๋กœ์„ธ์„œ์— ์ „๋‹ฌํ•˜๊ธฐ ์ „์— ์ด๋ฏธ์ง€๊ฐ€ ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ๋ฐฐ์น˜๋˜์—ˆ๋Š”์ง€ ํ™•์ธํ•˜์„ธ์š”. ๊ฐ ๋ฐฐ์น˜๋Š” ํ•˜๋‚˜ ์ด์ƒ์˜ ์ด๋ฏธ์ง€๋ฅผ ํฌํ•จํ•œ ๋ฆฌ์ŠคํŠธ์—ฌ์•ผ ํ•ฉ๋‹ˆ๋‹ค.

    ```py
    url_cow = "https://media.istockphoto.com/id/1192867753/photo/cow-in-berchida-beach-siniscola.jpg?s=612x612&w=0&k=20&c=v0hjjniwsMNfJSuKWZuIn8pssmD5h5bSN1peBd1CmH4="
    url_cat = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/pipeline-cat-chonk.jpeg"

    messages =[
        {
            "role": "system",
            "content": [
                {"type": "text", "text": "You are a helpful assistant."}
            ]
        },
        {
            "role": "user",
            "content": [
                {"type": "image", "url": url_cow},
                {"type": "image", "url": url_cat},
                {"type": "text", "text": "Which image is cuter?"},
            ]
        },
    ]
    ```
- ํ”„๋กœ์„ธ์„œ์— ์ „๋‹ฌ๋˜๋Š” ํ…์ŠคํŠธ์—๋Š” ์ด๋ฏธ์ง€๊ฐ€ ์‚ฝ์ž…๋˜์–ด์•ผ ํ•˜๋Š” ์œ„์น˜๋งˆ๋‹ค `<start_of_image>` ํ† ํฐ์ด ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
- ํ”„๋กœ์„ธ์„œ์—๋Š” ์ฑ„ํŒ… ๋ฉ”์‹œ์ง€๋ฅผ ๋ชจ๋ธ ์ž…๋ ฅ์œผ๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ์ž์ฒด [`~ProcessorMixin.apply_chat_template`] ๋ฉ”์†Œ๋“œ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.
- ๊ธฐ๋ณธ์ ์œผ๋กœ ์ด๋ฏธ์ง€๋Š” ์ž˜๋ฆฌ์ง€ ์•Š์œผ๋ฉฐ ๊ธฐ๋ณธ ์ด๋ฏธ์ง€๋งŒ ๋ชจ๋ธ๋กœ ์ „๋‹ฌ๋ฉ๋‹ˆ๋‹ค. ๊ณ ํ•ด์ƒ๋„ ์ด๋ฏธ์ง€๋‚˜ ์ •์‚ฌ๊ฐํ˜•์ด ์•„๋‹Œ ์ข…ํšก๋น„์˜ ์ด๋ฏธ์ง€์—์„œ๋Š” ๋น„์ „ ์ธ์ฝ”๋”๊ฐ€ 896x896์˜ ๊ณ ์ • ํ•ด์ƒ๋„๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์•„ํ‹ฐํŒฉํŠธ๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ์•„ํ‹ฐํŒฉํŠธ๋ฅผ ๋ฐฉ์ง€ํ•˜๊ณ  ์ถ”๋ก  ์ค‘ ์„ฑ๋Šฅ์„ ํ–ฅ์ƒ์‹œํ‚ค๋ ค๋ฉด, `do_pan_and_scan=True`๋ฅผ ์„ค์ •ํ•˜์—ฌ ์ด๋ฏธ์ง€๋ฅผ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ์ž‘์€ ํŒจ์น˜๋กœ ์ž๋ฅด๊ณ  ๊ธฐ๋ณธ ์ด๋ฏธ์ง€ ์ž„๋ฒ ๋”ฉ๊ณผ ์ด์–ด ๋ถ™์ž…๋‹ˆ๋‹ค. ๋” ๋น ๋ฅธ ์ถ”๋ก ์„ ์œ„ํ•ด ํŒฌ๊ณผ ์Šค์บ”์„ ๋น„ํ™œ์„ฑํ™”ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

    ```diff
    inputs = processor.apply_chat_template(
        messages,
        tokenize=True,
        return_dict=True,
        return_tensors="pt",
        add_generation_prompt=True,
    +   do_pan_and_scan=True,
        ).to(model.device)
    ```
- ํ…์ŠคํŠธ ์ „์šฉ ๋ชจ๋“œ๋กœ ํ›ˆ๋ จ๋œ Gemma-3 1B ์ฒดํฌํฌ์ธํŠธ์˜ ๊ฒฝ์šฐ, [`AutoModelForCausalLM`]์„ ๋Œ€์‹  ์‚ฌ์šฉํ•˜์„ธ์š”.

    ```py
    import torch
    from transformers import AutoModelForCausalLM, AutoTokenizer

    tokenizer = AutoTokenizer.from_pretrained(
        "google/gemma-3-1b-pt",
    )
    model = AutoModelForCausalLM.from_pretrained(
        "google/gemma-3-1b-pt",
        dtype=torch.bfloat16,
        device_map="auto",
        attn_implementation="sdpa"
    )
    input_ids = tokenizer("Plants create energy through a process known as", return_tensors="pt").to(model.device)

    output = model.generate(**input_ids, cache_implementation="static")
    print(tokenizer.decode(output[0], skip_special_tokens=True))
    ```

## Gemma3ImageProcessor

[[autodoc]] Gemma3ImageProcessor

## Gemma3ImageProcessorFast

[[autodoc]] Gemma3ImageProcessorFast

## Gemma3Processor

[[autodoc]] Gemma3Processor

## Gemma3TextConfig

[[autodoc]] Gemma3TextConfig

## Gemma3Config

[[autodoc]] Gemma3Config

## Gemma3TextModel

[[autodoc]] Gemma3TextModel
    - forward

## Gemma3Model

[[autodoc]] Gemma3Model

## Gemma3ForCausalLM

[[autodoc]] Gemma3ForCausalLM
    - forward

## Gemma3ForConditionalGeneration

[[autodoc]] Gemma3ForConditionalGeneration
    - forward

## Gemma3ForSequenceClassification

[[autodoc]] Gemma3ForSequenceClassification
    - forward