Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

๐ŸŒ [i18n-KO] Translated model_doc/mamba2.md to Korean #33629

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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/ko/_toctree.yml
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,8 @@
title: (๋ฒˆ์—ญ์ค‘) LUKE
- local: in_translation
title: (๋ฒˆ์—ญ์ค‘) M2M100
- local: model_doc/mamba2
title: Mamba2
- local: in_translation
title: (๋ฒˆ์—ญ์ค‘) MarianMT
- local: in_translation
Expand Down
111 changes: 111 additions & 0 deletions docs/source/ko/model_doc/mamba2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<!--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.

โš ๏ธ 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.

-->

# ๋ง˜๋ฐ”2[[mamba-2]]

## ์˜ค๋ฒ„๋ทฐ[[overview]]

๋ง˜๋ฐ”2 ๋ชจ๋ธ์€ Tri Dao, Albert Gu๊ฐ€ ์ œ์•ˆํ•œ [ํŠธ๋žœ์Šคํฌ๋จธ๋Š” SSM๋“ค ์ด๋‹ค: ๊ตฌ์กฐํ™”๋œ ์ƒํƒœ ๊ณต๊ฐ„ ์ด์ค‘์„ฑ์„ ํ†ตํ•œ ์ผ๋ฐ˜ํ™”๋œ ๋ชจ๋ธ๊ณผ ํšจ์œจ์ ์ธ ์•Œ๊ณ ๋ฆฌ์ฆ˜](https://arxiv.org/abs/2405.21060)๋ผ๋Š” ๋…ผ๋ฌธ์—์„œ ์†Œ๊ฐœ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ๋ง˜๋ฐ”2๋Š” ๋ง˜๋ฐ”1๊ณผ ์œ ์‚ฌํ•œ ์ƒํƒœ ๊ณต๊ฐ„ ๋ชจ๋ธ๋กœ, ๋‹จ์ˆœํ™”๋œ ์•„ํ‚คํ…์ฒ˜์—์„œ ๋” ๋‚˜์€ ์„ฑ๋Šฅ์„ ๋ณด์ž…๋‹ˆ๋‹ค.

ํ•ด๋‹น ๋…ผ๋ฌธ์˜ ์ดˆ๋ก์ž…๋‹ˆ๋‹ค:

*ํŠธ๋žœ์Šคํฌ๋จธ๊ฐ€ ์–ธ์–ด ๋ชจ๋ธ๋ง์—์„œ ๋”ฅ๋Ÿฌ๋‹ ์„ฑ๊ณต์˜ ์ฃผ์š” ์•„ํ‚คํ…์ฒ˜์˜€์ง€๋งŒ, ๋ง˜๋ฐ”์™€ ๊ฐ™์€ ์ƒํƒœ ๊ณต๊ฐ„ ๋ชจ๋ธ(SSM)์ด ์ตœ๊ทผ ์†Œ๊ทœ๋ชจ ํ˜น์€ ์ค‘๊ฐ„ ๊ทœ๋ชจ์—์„œ ํŠธ๋žœ์Šคํฌ๋จธ์™€ ๋Œ€๋“ฑํ•˜๊ฑฐ๋‚˜ ๋” ๋‚˜์€ ์„ฑ๋Šฅ์„ ๋ณด์ด๋Š” ๊ฒƒ์œผ๋กœ ๋‚˜ํƒ€๋‚ฌ์Šต๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๋Š” ์ด๋Ÿฌํ•œ ๋ชจ๋ธ ๊ณ„์—ด๋“ค์ด ์‹ค์ œ๋กœ ๋งค์šฐ ๋ฐ€์ ‘ํ•˜๊ฒŒ ์—ฐ๊ด€๋˜์–ด ์žˆ์Œ์„ ํŒŒ์•…ํ•˜๊ณ , ์ž˜ ์—ฐ๊ตฌ๋œ ๊ตฌ์กฐํ™”๋œ ์ค€๋ถ„๋ฆฌ(semiseparable) ํ–‰๋ ฌ ํด๋ž˜์Šค์˜ ๋‹ค์–‘ํ•œ ๋ถ„ํ•ด๋ฅผ ํ†ตํ•ด ์—ฐ๊ฒฐ๋œ SSM๊ณผ ์–ดํ…์…˜ ๋ณ€ํ˜• ์‚ฌ์ด์˜ ํ’๋ถ€ํ•œ ์ด๋ก ์  ์—ฐ๊ฒฐ ํ”„๋ ˆ์ž„์›Œํฌ๋ฅผ ๊ฐœ๋ฐœํ–ˆ์Šต๋‹ˆ๋‹ค. ์ƒํƒœ ๊ณต๊ฐ„ ์ด์ค‘์„ฑ(SSD) ํ”„๋ ˆ์ž„์›Œํฌ๋ฅผ ํ†ตํ•ด ๋ง˜๋ฐ”1์˜ ์„ ํƒ์  SSM์„ ๊ฐœ์„ ํ•œ ์ƒˆ๋กœ์šด ์•„ํ‚คํ…์ฒ˜๋ฅผ ์„ค๊ณ„ํ•  ์ˆ˜ ์žˆ์—ˆ๊ณ , ์ด๋Š” 2~8๋ฐฐ ๋” ๋น ๋ฅด๋ฉด์„œ๋„ ์–ธ์–ด ๋ชจ๋ธ๋ง์—์„œ ํŠธ๋žœ์Šคํฌ๋จธ์™€ ๊ฒฝ์Ÿ๋ ฅ์„ ์œ ์ง€ํ•ฉ๋‹ˆ๋‹ค.*

ํŒ:

์ด ๋ฒ„์ „์€ ๋ง˜๋ฐ”2 ๊ตฌํ˜„์„ ์ง€์›ํ•ด์•ผ ํ•˜๋ฉฐ, ํŠนํžˆ Mistral AI์˜ [Mamba-2 codestral](https://huggingface.co/mistralai/Mamba-Codestral-7B-v0.1)์„ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค. ํŠนํžˆ, mamba 2 codestral์€ 8๊ฐœ์˜ `groups`๋กœ ์ถœ์‹œ๋˜์—ˆ๋Š”๋ฐ, ์ด๋Š” ์ง๊ด€์ ์œผ๋กœ ์–ดํ…์…˜ ๊ธฐ๋ฐ˜ ๋ชจ๋ธ์˜ KV ํ—ค๋“œ ์ˆ˜์™€ ์œ ์‚ฌํ•˜๊ฒŒ ์ƒ๊ฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ด ๋ชจ๋ธ์€ `torch_forward`์™€ `cuda_kernels_forward`๋ผ๋Š” ๋‘ ๊ฐ€์ง€ ๋‹ค๋ฅธ ์ „๋ฐฉ ํŒจ์Šค๋ฅผ ๊ฐ€์ง‘๋‹ˆ๋‹ค. `cuda_kernels_forward`๋Š” ํ™˜๊ฒฝ์—์„œ cuda ์ปค๋„์„ ์ฐพ์œผ๋ฉด ์ด๋ฅผ ์‚ฌ์šฉํ•˜๋ฉฐ, prefill์—์„œ๋Š” ๋” ๋Š๋ฆฝ๋‹ˆ๋‹ค. ์ฆ‰ ๋†’์€ CPU ์˜ค๋ฒ„ํ—ค๋“œ๋กœ ์ธํ•ด "์›์—… ์‹คํ–‰"์ด ํ•„์š”ํ•˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ๊ด€๋ จ ๋‚ด์šฉ์€ [์ด๊ณณ](https://github.com/state-spaces/mamba/issues/389#issuecomment-2171755306)๊ณผ [์ด๊ณณ](https://github.com/state-spaces/mamba/issues/355#issuecomment-2147597457)์„ ์ฐธ๊ณ ํ•˜์„ธ์š”.

์ปดํŒŒ์ผ ์—†์ด๋Š” `torch_forward` ๊ตฌํ˜„์ด 3~4๋ฐฐ ๋น ๋ฆ…๋‹ˆ๋‹ค. ๋˜ํ•œ, ์ด ๋ชจ๋ธ์—๋Š” ์œ„์น˜ ์ž„๋ฒ ๋”ฉ์ด ์—†์ง€๋งŒ `attention_mask`์™€ ๋ฐฐ์น˜ ์ƒ์„ฑ์˜ ๊ฒฝ์šฐ ๋‘ ๊ณณ์—์„œ ์ˆจ๊ฒจ์ง„ ์ƒํƒœ๋ฅผ ๋งˆ์Šคํ‚นํ•˜๋Š” ํŠน์ • ๋กœ์ง์ด ์žˆ์Šต๋‹ˆ๋‹ค. ๊ด€๋ จ ๋‚ด์šฉ์€ [์ด๊ณณ](https://github.com/state-spaces/mamba/issues/66#issuecomment-1863563829)์„ ์ฐธ๊ณ ํ•˜์„ธ์š”.

์ด๋กœ์ธํ•ด ๋ง˜๋ฐ”2 ์ปค๋„์˜ ์žฌ๊ตฌํ˜„๊ณผ ํ•จ๊ป˜ ๋ฐฐ์น˜ ์ƒ์„ฑ ๋ฐ ์บ์‹œ๋œ ์ƒ์„ฑ์—์„œ ์•ฝ๊ฐ„์˜ ์ฐจ์ด๊ฐ€ ์˜ˆ์ƒ๋ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ cuda ์ปค๋„ ๋˜๋Š” tourch forward๊ฐ€ ์ œ๊ณตํ•˜๋Š” ๊ฒฐ๊ณผ๊ฐ€ ์•ฝ๊ฐ„ ๋‹ค๋ฅผ ๊ฒƒ์œผ๋กœ ์˜ˆ์ƒ๋ฉ๋‹ˆ๋‹ค. SSM ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ํ…์„œ ์ˆ˜์ถ•์— ํฌ๊ฒŒ ์˜์กดํ•˜๋Š”๋ฐ, ์ด๋Š” matmul๊ณผ ๋™๋“ฑ ํ•˜์ง€๋งŒ ์—ฐ์‚ฐ ์ˆœ์‚ฌ๊ฐ€ ์•ฝ๊ฐ„ ๋‹ค๋ฅด๋ฉฐ, ์ด๋กœ ์ธํ•ด ๋” ์ž‘์€ ์ •๋ฐ€๋„์—์„œ ์ฐจ์ด๊ฐ€ ๋” ์ปค์ง‘๋‹ˆ๋‹ค.

๋˜ ๋‹ค๋ฅธ ์ฐธ๊ณ ์‚ฌํ•ญ์œผ๋กœ, ํŒจ๋”ฉ ํ† ํฐ์— ํ•ด๋‹นํ•˜๋Š” ์ˆจ๊ฒจ์ง„ ์ƒํƒœ์˜ ์ข…๋ฃŒ๋Š” ๋‘ ๊ณณ์—์„œ ์ด๋ฃจ์–ด์ง€๋ฉฐ ์ฃผ๋กœ ์™ผ์ชฝ ํŒจ๋”ฉ์œผ๋กœ ํ…Œ์ŠคํŠธ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์˜ค๋ฅธ์ชฝ ํŒจ๋”ฉ์€ ๋…ธ์ด์ฆˆ๋ฅผ ์ „ํŒŒํ•˜๋ฏ€๋กœ ๋งŒ์กฑ์Šค๋Ÿฌ์šด ๊ฒฐ๊ณผ๋ฅผ ๋ณด์žฅํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. `tokenizer.padding_side = "left"`๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์˜ฌ๋ฐ”๋ฅธ ํŒจ๋”ฉ ๋ฐฉํ–ฅ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ด ๋ชจ๋ธ์€ [Molbap](https://huggingface.co/Molbap)์ด ๊ธฐ์—ฌํ–ˆ์œผ๋ฉฐ, [Anton Vlasjuk](https://github.com/vasqu)์˜ ํฐ ๋„์›€์„ ๋ฐ›์•˜์Šต๋‹ˆ๋‹ค.
์›๋ณธ ์ฝ”๋“œ๋Š” [์ด๊ณณ](https://github.com/state-spaces/mamba)์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.


# ์‚ฌ์šฉ

### ๊ฐ„๋‹จํ•œ ์ƒ์„ฑ ์˜ˆ:
```python
from transformers import Mamba2Config, Mamba2ForCausalLM, AutoTokenizer
import torch
model_id = 'mistralai/Mamba-Codestral-7B-v0.1'
tokenizer = AutoTokenizer.from_pretrained(model_id, revision='refs/pr/9', from_slow=True, legacy=False)
model = Mamba2ForCausalLM.from_pretrained(model_id, revision='refs/pr/9')
input_ids = tokenizer("Hey how are you doing?", return_tensors= "pt")["input_ids"]

out = model.generate(input_ids, max_new_tokens=10)
print(tokenizer.batch_decode(out))
```

์ด๊ณณ์€ ๋ฏธ์„ธ์กฐ์ •์„ ์œ„ํ•œ ์ดˆ์•ˆ ์Šคํฌ๋ฆฝํŠธ์ž…๋‹ˆ๋‹ค:
```python
from trl import SFTTrainer
from peft import LoraConfig
from transformers import AutoTokenizer, Mamba2ForCausalLM, TrainingArguments
model_id = 'mistralai/Mamba-Codestral-7B-v0.1'
tokenizer = AutoTokenizer.from_pretrained(model_id, revision='refs/pr/9', from_slow=True, legacy=False)
tokenizer.pad_token = tokenizer.eos_token
tokenizer.padding_side = "left" #์™ผ์ชฝ ํŒจ๋”ฉ์œผ๋กœ ์„ค์ •

model = Mamba2ForCausalLM.from_pretrained(model_id, revision='refs/pr/9')
dataset = load_dataset("Abirate/english_quotes", split="train")
# CUDA ์ปค๋„์—†์ด๋Š”, ๋ฐฐ์น˜ํฌ๊ธฐ 2๊ฐ€ 80GB ์žฅ์น˜๋ฅผ ํ•˜๋‚˜ ์ฐจ์ง€ํ•ฉ๋‹ˆ๋‹ค.
# ํ•˜์ง€๋งŒ ์ •ํ™•๋„๋Š” ๊ฐ์†Œํ•ฉ๋‹ˆ๋‹ค.
# ์‹คํ—˜๊ณผ ์‹œ๋„๋ฅผ ํ™˜์˜ํ•ฉ๋‹ˆ๋‹ค!
training_args = TrainingArguments(
output_dir="./results",
num_train_epochs=3,
per_device_train_batch_size=2,
logging_dir='./logs',
logging_steps=10,
learning_rate=2e-3
)
lora_config = LoraConfig(
r=8,
target_modules=["embeddings", "in_proj", "out_proj"],
task_type="CAUSAL_LM",
bias="none"
)
trainer = SFTTrainer(
model=model,
tokenizer=tokenizer,
args=training_args,
peft_config=lora_config,
train_dataset=dataset,
dataset_text_field="quote",
)
trainer.train()
```


## Mamba2Config

[[autodoc]] Mamba2Config

## Mamba2Model

[[autodoc]] Mamba2Model
- forward

## Mamba2LMHeadModel

[[autodoc]] Mamba2ForCausalLM
- forward