12 KiB
Mistralmistral
๊ฐ์overview
๋ฏธ์คํธ๋์ Albert Jiang, Alexandre Sablayrolles, Arthur Mensch, Chris Bamford, Devendra Singh Chaplot, Diego de las Casas, Florian Bressand, Gianna Lengyel, Guillaume Lample, Lรฉlio Renard Lavaud, Lucile Saulnier, Marie-Anne Lachaux, Pierre Stock, Teven Le Scao, Thibaut Lavril, Thomas Wang, Timothรฉe Lacroix, William El Sayed๊ฐ ์์ฑํ ์ด ๋ธ๋ก๊ทธ ํฌ์คํธ์์ ์๊ฐ๋์์ต๋๋ค.
๋ธ๋ก๊ทธ ํฌ์คํธ์ ์๋๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค:
๋ฏธ์คํธ๋ AIํ์ ํ์กดํ๋ ์ธ์ด ๋ชจ๋ธ ์ค ํฌ๊ธฐ ๋๋น ๊ฐ์ฅ ๊ฐ๋ ฅํ ๋ฏธ์คํธ๋7B๋ฅผ ์ถ์ํ๊ฒ ๋์ด ์๋์ค๋ฝ์ต๋๋ค.
๋ฏธ์คํธ๋-7B๋ mistral.ai์์ ์ถ์ํ ์ฒซ ๋ฒ์งธ ๋๊ท๋ชจ ์ธ์ด ๋ชจ๋ธ(LLM)์ ๋๋ค.
์ํคํ ์ฒ ์ธ๋ถ์ฌํญarchitectural-details
๋ฏธ์คํธ๋-7B๋ ๋ค์๊ณผ ๊ฐ์ ๊ตฌ์กฐ์ ํน์ง์ ๊ฐ์ง ๋์ฝ๋ ์ ์ฉ ํธ๋์คํฌ๋จธ์ ๋๋ค:
- ์ฌ๋ผ์ด๋ฉ ์๋์ฐ ์ดํ ์ : 8k ์ปจํ ์คํธ ๊ธธ์ด์ ๊ณ ์ ์บ์ ํฌ๊ธฐ๋ก ํ๋ จ๋์์ผ๋ฉฐ, ์ด๋ก ์ 128K ํ ํฐ์ ์ดํ ์ ๋ฒ์๋ฅผ ๊ฐ์ง๋๋ค.
- GQA(Grouped Query Attention): ๋ ๋น ๋ฅธ ์ถ๋ก ์ด ๊ฐ๋ฅํ๊ณ ๋ ์์ ํฌ๊ธฐ์ ์บ์๋ฅผ ์ฌ์ฉํฉ๋๋ค.
- ๋ฐ์ดํธ ํด๋ฐฑ(Byte-fallback) BPE ํ ํฌ๋์ด์ : ๋ฌธ์๋ค์ด ์ ๋ ์ดํ ๋ชฉ๋ก ์ธ์ ํ ํฐ์ผ๋ก ๋งคํ๋์ง ์๋๋ก ๋ณด์ฅํฉ๋๋ค.
๋ ์์ธํ ๋ด์ฉ์ ์ถ์ ๋ธ๋ก๊ทธ ํฌ์คํธ๋ฅผ ์ฐธ์กฐํ์ธ์.
๋ผ์ด์ ์คlicense
๋ฏธ์คํธ๋-7B๋ ์ํ์น 2.0 ๋ผ์ด์ ์ค๋ก ์ถ์๋์์ต๋๋ค.
์ฌ์ฉ ํusage-tips
๋ฏธ์คํธ๋ AIํ์ ๋ค์ 3๊ฐ์ง ์ฒดํฌํฌ์ธํธ๋ฅผ ๊ณต๊ฐํ์ต๋๋ค:
- ๊ธฐ๋ณธ ๋ชจ๋ธ์ธ ๋ฏธ์คํธ๋-7B-v0.1์ ์ธํฐ๋ท ๊ท๋ชจ์ ๋ฐ์ดํฐ์์ ๋ค์ ํ ํฐ์ ์์ธกํ๋๋ก ์ฌ์ ํ๋ จ๋์์ต๋๋ค.
- ์ง์ ์กฐ์ ๋ชจ๋ธ์ธ ๋ฏธ์คํธ๋-7B-Instruct-v0.1์ ์ง๋ ๋ฏธ์ธ ์กฐ์ (SFT)๊ณผ ์ง์ ์ ํธ๋ ์ต์ ํ(DPO)๋ฅผ ์ฌ์ฉํ ์ฑํ ์ ์ต์ ํ๋ ๊ธฐ๋ณธ ๋ชจ๋ธ์ ๋๋ค.
- ๊ฐ์ ๋ ์ง์ ์กฐ์ ๋ชจ๋ธ์ธ ๋ฏธ์คํธ๋-7B-Instruct-v0.2๋ v1์ ๊ฐ์ ํ ๋ฒ์ ์ ๋๋ค.
๊ธฐ๋ณธ ๋ชจ๋ธ์ ๋ค์๊ณผ ๊ฐ์ด ์ฌ์ฉํ ์ ์์ต๋๋ค:
>>> from transformers import AutoModelForCausalLM, AutoTokenizer
>>> model = AutoModelForCausalLM.from_pretrained("mistralai/Mistral-7B-v0.1", device_map="auto")
>>> tokenizer = AutoTokenizer.from_pretrained("mistralai/Mistral-7B-v0.1")
>>> prompt = "My favourite condiment is"
>>> model_inputs = tokenizer([prompt], return_tensors="pt").to(model.device)
>>> model.to(device)
>>> generated_ids = model.generate(**model_inputs, max_new_tokens=100, do_sample=True)
>>> tokenizer.batch_decode(generated_ids)[0]
"My favourite condiment is to ..."
์ง์ ์กฐ์ ๋ชจ๋ธ์ ๋ค์๊ณผ ๊ฐ์ด ์ฌ์ฉํ ์ ์์ต๋๋ค:
>>> from transformers import AutoModelForCausalLM, AutoTokenizer
>>> model = AutoModelForCausalLM.from_pretrained("mistralai/Mistral-7B-Instruct-v0.2", device_map="auto")
>>> tokenizer = AutoTokenizer.from_pretrained("mistralai/Mistral-7B-Instruct-v0.2")
>>> messages = [
... {"role": "user", "content": "What is your favourite condiment?"},
... {"role": "assistant", "content": "Well, I'm quite partial to a good squeeze of fresh lemon juice. It adds just the right amount of zesty flavour to whatever I'm cooking up in the kitchen!"},
... {"role": "user", "content": "Do you have mayonnaise recipes?"}
... ]
>>> model_inputs = tokenizer.apply_chat_template(messages, return_tensors="pt").to(model.device)
>>> generated_ids = model.generate(model_inputs, max_new_tokens=100, do_sample=True)
>>> tokenizer.batch_decode(generated_ids)[0]
"Mayonnaise can be made as follows: (...)"
์ง์ ์กฐ์ ๋ชจ๋ธ์ ์ ๋ ฅ์ด ์ฌ๋ฐ๋ฅธ ํ์์ผ๋ก ์ค๋น๋๋๋ก ์ฑํ ํ ํ๋ฆฟ์ ์ ์ฉํด์ผ ํฉ๋๋ค.
ํ๋์ ์ดํ ์ ์ ์ด์ฉํ ๋ฏธ์คํธ๋ ์๋ํฅ์speeding-up-mistral-by-using-flash-attention
์์ ์ฝ๋ ์ค๋ํซ๋ค์ ์ด๋ค ์ต์ ํ ๊ธฐ๋ฒ๋ ์ฌ์ฉํ์ง ์์ ์ถ๋ก ๊ณผ์ ์ ๋ณด์ฌ์ค๋๋ค. ํ์ง๋ง ๋ชจ๋ธ ๋ด๋ถ์์ ์ฌ์ฉ๋๋ ์ดํ ์ ๋ฉ์ปค๋์ฆ์ ๋ ๋น ๋ฅธ ๊ตฌํ์ธ ํ๋์ ์ดํ ์ 2์ ํ์ฉํ๋ฉด ๋ชจ๋ธ์ ์๋๋ฅผ ํฌ๊ฒ ๋์ผ ์ ์์ต๋๋ค.
๋จผ์ , ์ฌ๋ผ์ด๋ฉ ์๋์ฐ ์ดํ ์ ๊ธฐ๋ฅ์ ํฌํจํ๋ ํ๋์ ์ดํ ์ 2์ ์ต์ ๋ฒ์ ์ ์ค์นํด์ผ ํฉ๋๋ค.
pip install -U flash-attn --no-build-isolation
ํ๋์จ์ด์ ํ๋์ ์ดํ
์
2์ ํธํ์ฌ๋ถ๋ฅผ ํ์ธํ์ธ์. ์ด์ ๋ํ ์์ธํ ๋ด์ฉ์ ํ๋์ ์ดํ
์
์ ์ฅ์์ ๊ณต์ ๋ฌธ์์์ ํ์ธํ ์ ์์ต๋๋ค. ๋ํ ๋ชจ๋ธ์ ๋ฐ์ ๋ฐ๋(์: torch.float16)๋ก ๋ถ๋ฌ์์ผํฉ๋๋ค.
ํ๋์ ์ดํ ์ 2๋ฅผ ์ฌ์ฉํ์ฌ ๋ชจ๋ธ์ ๋ถ๋ฌ์ค๊ณ ์คํํ๋ ค๋ฉด ์๋ ์ฝ๋ ์ค๋ํซ์ ์ฐธ์กฐํ์ธ์:
>>> import torch
>>> from transformers import AutoModelForCausalLM, AutoTokenizer
>>> model = AutoModelForCausalLM.from_pretrained("mistralai/Mistral-7B-v0.1", dtype=torch.float16, attn_implementation="flash_attention_2", device_map="auto")
>>> tokenizer = AutoTokenizer.from_pretrained("mistralai/Mistral-7B-v0.1")
>>> prompt = "My favourite condiment is"
>>> model_inputs = tokenizer([prompt], return_tensors="pt").to(model.device)
>>> model.to(device)
>>> generated_ids = model.generate(**model_inputs, max_new_tokens=100, do_sample=True)
>>> tokenizer.batch_decode(generated_ids)[0]
"My favourite condiment is to (...)"
๊ธฐ๋ํ๋ ์๋ ํฅ์expected-speedups
๋ค์์ mistralai/Mistral-7B-v0.1 ์ฒดํฌํฌ์ธํธ๋ฅผ ์ฌ์ฉํ ํธ๋์คํฌ๋จธ์ ๊ธฐ๋ณธ ๊ตฌํ๊ณผ ํ๋์ ์ดํ
์
2 ๋ฒ์ ๋ชจ๋ธ ์ฌ์ด์ ์์ ์ถ๋ก ์๊ฐ์ ๋น๊ตํ ์์ ์๋ ํฅ์ ๋ค์ด์ด๊ทธ๋จ์
๋๋ค.
์ฌ๋ผ์ด๋ฉ ์๋์ฐ ์ดํ ์ sliding-window-attention
ํ์ฌ ๊ตฌํ์ ์ฌ๋ผ์ด๋ฉ ์๋์ฐ ์ดํ
์
๋ฉ์ปค๋์ฆ๊ณผ ๋ฉ๋ชจ๋ฆฌ ํจ์จ์ ์ธ ์บ์ ๊ด๋ฆฌ ๊ธฐ๋ฅ์ ์ง์ํฉ๋๋ค. ์ฌ๋ผ์ด๋ฉ ์๋์ฐ ์ดํ
์
์ ํ์ฑํํ๋ ค๋ฉด, ์ฌ๋ผ์ด๋ฉ ์๋์ฐ ์ดํ
์
๊ณผ ํธํ๋๋flash-attn(>=2.3.0)๋ฒ์ ์ ์ฌ์ฉํ๋ฉด ๋ฉ๋๋ค.
๋ํ ํ๋์ ์ดํ
์
2 ๋ชจ๋ธ์ ๋ ๋ฉ๋ชจ๋ฆฌ ํจ์จ์ ์ธ ์บ์ ์ฌ๋ผ์ด์ฑ ๋ฉ์ปค๋์ฆ์ ์ฌ์ฉํฉ๋๋ค. ๋ฏธ์คํธ๋ ๋ชจ๋ธ์ ๊ณต์ ๊ตฌํ์์ ๊ถ์ฅํ๋ ๋กค๋ง ์บ์ ๋ฉ์ปค๋์ฆ์ ๋ฐ๋ผ, ์บ์ ํฌ๊ธฐ๋ฅผ ๊ณ ์ (self.config.sliding_window)์ผ๋ก ์ ์งํ๊ณ , padding_side="left"์ธ ๊ฒฝ์ฐ์๋ง ๋ฐฐ์น ์์ฑ(batch generation)์ ์ง์ํ๋ฉฐ, ํ์ฌ ํ ํฐ์ ์ ๋ ์์น๋ฅผ ์ฌ์ฉํด ์์น ์๋ฒ ๋ฉ์ ๊ณ์ฐํฉ๋๋ค.
์์ํ๋ก ๋ฏธ์คํธ๋ ํฌ๊ธฐ ์ค์ด๊ธฐshrinking-down-mistral-using-quantization
๋ฏธ์คํธ๋ ๋ชจ๋ธ์ 70์ต ๊ฐ์ ํ๋ผ๋ฏธํฐ๋ฅผ ๊ฐ์ง๊ณ ์์ด, ์ ๋ฐ์ ์ ๋ฐ๋(float16)๋ก ์ฝ 14GB์ GPU RAM์ด ํ์ํฉ๋๋ค. ๊ฐ ํ๋ผ๋ฏธํฐ๊ฐ 2๋ฐ์ดํธ๋ก ์ ์ฅ๋๊ธฐ ๋๋ฌธ์ ๋๋ค. ํ์ง๋ง ์์ํ๋ฅผ ์ฌ์ฉํ๋ฉด ๋ชจ๋ธ ํฌ๊ธฐ๋ฅผ ์ค์ผ ์ ์์ต๋๋ค. ๋ชจ๋ธ์ 4๋นํธ(์ฆ, ํ๋ผ๋ฏธํฐ๋น ๋ฐ ๋ฐ์ดํธ)๋ก ์์ํํ๋ฉด ์ฝ 3.5GB์ RAM๋ง ํ์ํฉ๋๋ค.
๋ชจ๋ธ์ ์์ํํ๋ ๊ฒ์ quantization_config๋ฅผ ๋ชจ๋ธ์ ์ ๋ฌํ๋ ๊ฒ๋งํผ ๊ฐ๋จํฉ๋๋ค. ์๋์์๋ BitsAndBytes ์์ํ๋ฅผ ์ฌ์ฉํ์ง๋ง, ๋ค๋ฅธ ์์ํ ๋ฐฉ๋ฒ์ ์ด ํ์ด์ง๋ฅผ ์ฐธ๊ณ ํ์ธ์:
>>> import torch
>>> from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
>>> # specify how to quantize the model
>>> quantization_config = BitsAndBytesConfig(
... load_in_4bit=True,
... bnb_4bit_quant_type="nf4",
... bnb_4bit_compute_dtype="torch.float16",
... )
>>> model = AutoModelForCausalLM.from_pretrained("mistralai/Mistral-7B-Instruct-v0.2", quantization_config=True, device_map="auto")
>>> tokenizer = AutoTokenizer.from_pretrained("mistralai/Mistral-7B-Instruct-v0.2")
>>> prompt = "My favourite condiment is"
>>> messages = [
... {"role": "user", "content": "What is your favourite condiment?"},
... {"role": "assistant", "content": "Well, I'm quite partial to a good squeeze of fresh lemon juice. It adds just the right amount of zesty flavour to whatever I'm cooking up in the kitchen!"},
... {"role": "user", "content": "Do you have mayonnaise recipes?"}
... ]
>>> model_inputs = tokenizer.apply_chat_template(messages, return_tensors="pt").to(model.device)
>>> generated_ids = model.generate(model_inputs, max_new_tokens=100, do_sample=True)
>>> tokenizer.batch_decode(generated_ids)[0]
"The expected output"
์ด ๋ชจ๋ธ์ Younes Belkada์ Arthur Zucker๊ฐ ๊ธฐ์ฌํ์ต๋๋ค. ์๋ณธ ์ฝ๋๋ ์ด๊ณณ์์ ํ์ธํ ์ ์์ต๋๋ค.
๋ฆฌ์์คresources
๋ฏธ์คํธ๋์ ์์ํ๋ ๋ฐ ๋์์ด ๋๋ Hugging Face์ community ์๋ฃ ๋ชฉ๋ก(๐๋ก ํ์๋จ) ์ ๋๋ค. ์ฌ๊ธฐ์ ํฌํจ๋ ์๋ฃ๋ฅผ ์ ์ถํ๊ณ ์ถ์ผ์๋ค๋ฉด PR(Pull Request)๋ฅผ ์ด์ด์ฃผ์ธ์. ๋ฆฌ๋ทฐํด ๋๋ฆฌ๊ฒ ์ต๋๋ค! ์๋ฃ๋ ๊ธฐ์กด ์๋ฃ๋ฅผ ๋ณต์ ํ๋ ๋์ ์๋ก์ด ๋ด์ฉ์ ๋ด๊ณ ์์ด์ผ ํฉ๋๋ค.
- ๋ฏธ์คํธ๋-7B์ ์ง๋ํ ๋ฏธ์ธ์กฐ์ (SFT)์ ์ํํ๋ ๋ฐ๋ชจ ๋ ธํธ๋ถ์ ์ด๊ณณ์์ ํ์ธํ ์ ์์ต๋๋ค. ๐
- 2024๋ ์ Hugging Face ๋๊ตฌ๋ฅผ ์ฌ์ฉํด LLM์ ๋ฏธ์ธ ์กฐ์ ํ๋ ๋ฐฉ๋ฒ์ ๋ํ ๋ธ๋ก๊ทธ ํฌ์คํธ. ๐
- Hugging Face์ ์ ๋ ฌ(Alignment) ํธ๋๋ถ์๋ ๋ฏธ์คํธ๋-7B๋ฅผ ์ฌ์ฉํ ์ง๋ํ ๋ฏธ์ธ ์กฐ์ (SFT) ๋ฐ ์ง์ ์ ํธ ์ต์ ํ(DPO)๋ฅผ ์ํํ๊ธฐ ์ํ ์คํฌ๋ฆฝํธ์ ๋ ์ํผ๊ฐ ํฌํจ๋์ด ์์ต๋๋ค. ์ฌ๊ธฐ์๋ ๋จ์ผ GPU์์ QLoRa ๋ฐ ๋ค์ค GPU๋ฅผ ์ฌ์ฉํ ์ ์ฒด ๋ฏธ์ธ ์กฐ์ ์ ์ํ ์คํฌ๋ฆฝํธ๊ฐ ํฌํจ๋์ด ์์ต๋๋ค.
- ์ธ๊ณผ์ ์ธ์ด ๋ชจ๋ธ๋ง ์์ ๊ฐ์ด๋
MistralConfigtransformers.MistralConfig
autodoc MistralConfig
MistralModeltransformers.MistralModel
autodoc MistralModel - forward
MistralForCausalLMtransformers.MistralForCausalLM
autodoc MistralForCausalLM - forward
MistralForSequenceClassificationtransformers.MistralForSequenceClassification
autodoc MistralForSequenceClassification - forward
MistralForTokenClassificationtransformers.MistralForTokenClassification
autodoc MistralForTokenClassification - forward
