AbdulElahGwaith's picture
Upload folder using huggingface_hub
a9bd396 verified
<!--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.
-->
*이 λͺ¨λΈμ€ 2025λ…„ 5μ›” 20일에 μΆœμ‹œλ˜μ—ˆμœΌλ©°, 2025λ…„ 6μ›” 26일에 Hugging Face Transformers에 μΆ”κ°€λ˜μ—ˆμŠ΅λ‹ˆλ‹€.*
<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>
# Gemma3n[[gemma3n]]
## κ°œμš”[[overview]]
[Gemma3n](https://developers.googleblog.com/en/introducing-gemma-3n/)은 사전 ν›ˆλ ¨λœ 버전과 λͺ…λ Ήμ–΄ 기반 λ―Έμ„Έμ‘°μ • 버전이 μ œκ³΅λ˜λŠ” λ©€ν‹°λͺ¨λ‹¬ λͺ¨λΈμ΄λ©°, λͺ¨λΈ ν¬κΈ°λŠ” E4B와 E2B 두 κ°€μ§€λ‘œ μΆœμ‹œλ˜μ—ˆμŠ΅λ‹ˆλ‹€. μ–Έμ–΄ λͺ¨λΈ μ•„ν‚€ν…μ²˜λŠ” 이전 Gemma 버전과 λ§Žμ€ 뢀뢄을 κ³΅μœ ν•˜μ§€λ§Œ 이번 λ²„μ „μ—λŠ” μ—¬λŸ¬ κ°€μ§€ μƒˆλ‘œμš΄ 기법이 μΆ”κ°€λ˜μ—ˆμŠ΅λ‹ˆλ‹€. λŒ€ν‘œμ μœΌλ‘œ [ꡐ차 μ—…λ°μ΄νŠΈ(AltUp)](https://proceedings.neurips.cc/paper_files/paper/2023/hash/f2059277ac6ce66e7e5543001afa8bb5-Abstract-Conference.html), [ν•™μŠ΅λœ 증강 μž”μ—¬ λ ˆμ΄μ–΄(LAuReL)](https://huggingface.co/papers/2411.07501), [MatFormer](https://huggingface.co/papers/2310.07707), λ ˆμ΄μ–΄λ³„ μž„λ² λ”©, [톡계적 Top-kλ₯Ό μ΄μš©ν•œ ν™œμ„±ν™” ν¬μ†Œμ„±(SPARk-Transformer)](https://huggingface.co/papers/2506.06644), KV μΊμ‹œ 곡유 등이 μžˆμŠ΅λ‹ˆλ‹€. Gemma 3n은 [Gemma 3](./gemma3)와 μœ μ‚¬ν•œ μ–΄ν…μ…˜ νŒ¨ν„΄μ„ μ‚¬μš©ν•©λ‹ˆλ‹€. κΈ€λ‘œλ²Œ μ…€ν”„ μ–΄ν…μ…˜ λ ˆμ΄μ–΄ 1κ°œλ§ˆλ‹€ 둜컬 μŠ¬λΌμ΄λ”© μœˆλ„μš° μ…€ν”„ μ–΄ν…μ…˜ λ ˆμ΄μ–΄ 4개λ₯Ό ꡐ차둜 λ°°μΉ˜ν•˜λ©°, μ΅œλŒ€ μ»¨ν…μŠ€νŠΈ κΈΈμ΄λŠ” 32k ν† ν°κΉŒμ§€ μ§€μ›ν•©λ‹ˆλ‹€. λΉ„μ „ λͺ¨λ‹¬λ¦¬ν‹°μ—μ„œλŠ” MobileNet v5λ₯Ό λΉ„μ „ μΈμ½”λ”λ‘œ λ„μž…ν•˜μ—¬ κΈ°λ³Έ 해상도λ₯Ό 768x768 ν”½μ…€λ‘œ μ²˜λ¦¬ν•©λ‹ˆλ‹€. λ˜ν•œ μ˜€λ””μ˜€ λͺ¨λ‹¬λ¦¬ν‹°μ—μ„œλŠ” [Universal Speech Model(USM)](https://huggingface.co/papers/2303.01037) μ•„ν‚€ν…μ²˜λ₯Ό 기반으둜 μƒˆλ‘­κ²Œ ν•™μŠ΅λœ μ˜€λ””μ˜€ 인코더가 μΆ”κ°€λ˜μ—ˆμŠ΅λ‹ˆλ‹€.
λͺ…λ Ήμ–΄ 기반 λ―Έμ„Έμ‘°μ • 버전은 지식 증λ₯˜μ™€ κ°•ν™” ν•™μŠ΅μ„ 톡해 ν›„μ²˜λ¦¬ ν•™μŠ΅ λ˜μ—ˆμŠ΅λ‹ˆλ‹€.
Gemma 3n의 원본 μ²΄ν¬ν¬μΈνŠΈλŠ” [Gemma 3n][gemma3n-collection] μΆœμ‹œ νŽ˜μ΄μ§€μ—μ„œ 확인할 수 μžˆμŠ΅λ‹ˆλ‹€.
> [!TIP]
> 였λ₯Έμͺ½ μ‚¬μ΄λ“œλ°”μ— μžˆλŠ” Gemma 3n λͺ¨λΈμ„ ν΄λ¦­ν•˜λ©΄, Gemmaλ₯Ό λ‹€μ–‘ν•œ λΉ„μ „, μ˜€λ””μ˜€,
> μ–Έμ–΄ μž‘μ—…μ— μ μš©ν•˜λŠ” 더 λ§Žμ€ μ˜ˆμ‹œλ₯Ό 확인할 수 μžˆμŠ΅λ‹ˆλ‹€.
μ•„λž˜ μ˜ˆμ‹œλŠ” [`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-3n-e4b",
device=0,
dtype=torch.bfloat16
)
pipeline(
"https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/pipeline-cat-chonk.jpeg",
text="이 이미지에 무엇이 λ³΄μ΄λ‚˜μš”?"
)
```
</hfoption>
<hfoption id="AutoModel">
```py
import torch
from transformers import AutoProcessor, Gemma3nForConditionalGeneration
model = Gemma3nForConditionalGeneration.from_pretrained(
"google/gemma-3n-e4b-it",
dtype=torch.bfloat16,
device_map="auto",
attn_implementation="sdpa"
)
processor = AutoProcessor.from_pretrained(
"google/gemma-3n-e4b-it",
padding_side="left"
)
messages = [
{
"role": "system",
"content": [
{"type": "text", "text": "당신은 도움이 λ˜λŠ” μ–΄μ‹œμŠ€ν„΄νŠΈμž…λ‹ˆλ‹€."}
]
},
{
"role": "user", "content": [
{"type": "image", "url": "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/pipeline-cat-chonk.jpeg"},
{"type": "text", "text": "이 이미지에 무엇이 λ³΄μ΄λ‚˜μš”?"},
]
},
]
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 "식물은 νŠΉμ • 과정을 톡해 μ—λ„ˆμ§€λ₯Ό μƒμ„±ν•©λ‹ˆλ‹€." | transformers run --task text-generation --model google/gemma-3n-e2b --device 0
```
</hfoption>
</hfoptions>
## 참고사항[[notes]]
- [`Gemma3nForConditionalGeneration`] 클래슀λ₯Ό μ‚¬μš©ν•˜λ©΄ 이미지-μ˜€λ””μ˜€-ν…μŠ€νŠΈ, 이미지-ν…μŠ€νŠΈ, 이미지-μ˜€λ””μ˜€, μ˜€λ””μ˜€-ν…μŠ€νŠΈ, 이미지 단독, μ˜€λ””μ˜€ 단독 μž…λ ₯을 λͺ¨λ‘ μ²˜λ¦¬ν•  수 μžˆμŠ΅λ‹ˆλ‹€.
- Gemma 3n은 ν•œ 번의 μž…λ ₯에 μ—¬λŸ¬ 이미지λ₯Ό μ§€μ›ν•©λ‹ˆλ‹€. λ‹€λ§Œ ν”„λ‘œμ„Έμ„œμ— μ „λ‹¬ν•˜κΈ° 전에 이미지듀이 배치 λ‹¨μœ„λ‘œ μ˜¬λ°”λ₯΄κ²Œ λ¬Άμ—¬μžˆλŠ”μ§€ 확인해야 ν•©λ‹ˆλ‹€. 각 λ°°μΉ˜λŠ” ν•˜λ‚˜ μ΄μƒμ˜ 이미지λ₯Ό 담은 리슀트 ν˜•μ‹μž…λ‹ˆλ‹€.
```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": "당신은 도움이 λ˜λŠ” μ–΄μ‹œμŠ€ν„΄νŠΈμž…λ‹ˆλ‹€."}
]
},
{
"role": "user",
"content": [
{"type": "image", "url": url_cow},
{"type": "image", "url": url_cat},
{"type": "text", "text": "μ–΄λ–€ 이미지가 더 κ·€μ—½μŠ΅λ‹ˆκΉŒ?"},
]
},
]
```
- ν”„λ‘œμ„Έμ„œμ— μ „λ‹¬λ˜λŠ” ν…μŠ€νŠΈμ—λŠ” 이미지λ₯Ό μ‚½μž…ν•΄μ•Ό ν•˜λŠ” μœ„μΉ˜μ— `<image_soft_token>` 토큰을 포함해야 ν•©λ‹ˆλ‹€.
- Gemma 3n은 μž…λ ₯λ‹Ή μ΅œλŒ€ ν•˜λ‚˜μ˜ 타깃 μ˜€λ””μ˜€ 클립만 ν—ˆμš©ν•©λ‹ˆλ‹€. λ‹€λ§Œ 퓨샷 ν”„λ‘¬ν”„νŠΈμ—μ„œλŠ” μ—¬λŸ¬ 개의 μ˜€λ””μ˜€ 클립을 ν•¨κ»˜ μ œκ³΅ν•  수 μžˆμŠ΅λ‹ˆλ‹€.
- ν”„λ‘œμ„Έμ„œμ— μ „λ‹¬λ˜λŠ” ν…μŠ€νŠΈμ—λŠ” μ˜€λ””μ˜€ 클립을 μ‚½μž…ν•΄μ•Ό ν•˜λŠ” μœ„μΉ˜μ— `<audio_soft_token>` 토큰을 포함해야 ν•©λ‹ˆλ‹€.
- ν”„λ‘œμ„Έμ„œμ—λŠ” μ±„νŒ… λ©”μ‹œμ§€λ₯Ό λͺ¨λΈ μž…λ ₯ ν˜•μ‹μœΌλ‘œ λ³€ν™˜ν•˜κΈ° μœ„ν•œ 자체 λ©”μ„œλ“œμΈ [`~ProcessorMixin.apply_chat_template`]κ°€ ν¬ν•¨λ˜μ–΄ μžˆμŠ΅λ‹ˆλ‹€.
## Gemma3nAudioFeatureExtractor[[transformers.Gemma3nAudioFeatureExtractor]]
[[autodoc]] Gemma3nAudioFeatureExtractor
## Gemma3nProcessor[[transformers.Gemma3nProcessor]]
[[autodoc]] Gemma3nProcessor
## Gemma3nTextConfig[[transformers.Gemma3nTextConfig]]
[[autodoc]] Gemma3nTextConfig
## Gemma3nVisionConfig[[transformers.Gemma3nVisionConfig]]
[[autodoc]] Gemma3nVisionConfig
## Gemma3nAudioConfig[[transformers.Gemma3nAudioConfig]]
[[autodoc]] Gemma3nAudioConfig
## Gemma3nConfig[[transformers.Gemma3nConfig]]
[[autodoc]] Gemma3nConfig
## Gemma3nTextModel[[transformers.Gemma3nTextModel]]
[[autodoc]] Gemma3nTextModel
- forward
## Gemma3nModel[[transformers.Gemma3nModel]]
[[autodoc]] Gemma3nModel
- forward
## Gemma3nForCausalLM[[transformers.Gemma3nForCausalLM]]
[[autodoc]] Gemma3nForCausalLM
- forward
## Gemma3nForConditionalGeneration[[transformers.Gemma3nForConditionalGeneration]]
[[autodoc]] Gemma3nForConditionalGeneration
- forward