| <!--Copyright 2023 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 contains specific syntax for our doc-builder (similar to MDX) that may not be | |
| rendered properly in your Markdown viewer. | |
| --> | |
| # Llama2 [[llama2]] | |
| ## ๊ฐ์ [[overview]] | |
| Llama2 ๋ชจ๋ธ์ Hugo Touvron, Louis Martin, Kevin Stone, Peter Albert, Amjad Almahairi, Ya1smine Babaei, Nikolay Bashlykov, Soumya Batra, Prajjwal Bhargava, Shruti Bhosale, Dan Bikel, Lukas Blecher, Cristian Canton Ferrer, Moya Chen, Guillem Cucurull, David Esiobu, Jude Fernandes, Jeremy Fu, Wenyin Fu, Brian Fuller, Cynthia Gao, Vedanuj Goswami, Naman Goyal, Anthony Hartshorn, Saghar Hosseini, Rui Hou, Hakan Inan, Marcin Kardas, Viktor Kerkez Madian Khabsa, Isabel Kloumann, Artem Korenev, Punit Singh Koura, Marie-Anne Lachaux, Thibaut Lavril, Jenya Lee, Diana Liskovich, Yinghai Lu, Yuning Mao, Xavier Martinet, Todor Mihaylov, Pushkar Mishra, Igor Molybog, Yixin Nie, Andrew Poulton, Jeremy Reizenstein, Rashi Rungta, Kalyan Saladi, Alan Schelten, Ruan Silva, Eric Michael Smith, Ranjan Subramanian, Xiaoqing EllenTan, Binh Tang, Ross Taylor, Adina Williams, Jian Xiang Kuan, Puxin Xu, Zheng Yan, Iliyan Zarov, Yuchen Zhang, Angela Fan, Melanie Kambadur, Sharan Narang, Aurelien Rodriguez, Robert Stojnic, Sergey Edunov, Thomas Scialom์ ๋ ผ๋ฌธ [LLaMA: Open Foundation and Fine-Tuned Chat Models](https://ai.meta.com/research/publications/llama-2-open-foundation-and-fine-tuned-chat-models/)์์ ์ ์๋์์ต๋๋ค. ์ฑํ ์ดํ๋ฆฌ์ผ์ด์ ์ ๋ง๊ฒ ๋ฏธ์ธ ์กฐ์ ๋ ์ฒดํฌํฌ์ธํธ๋ฅผ ํฌํจ๋ 7B์์ 70B ๋ฒ์์ ๋งค๊ฐ๋ณ์๋ฅผ ๊ฐ์ง ๊ธฐ์ด ์ธ์ด ๋ชจ๋ธ ๋ชจ์์ ๋๋ค! | |
| ๋ ผ๋ฌธ์ ์ด๋ก์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค: | |
| *์ด ์ฐ๊ตฌ์์ ์ฐ๋ฆฌ๋ 70์ต์์ 700์ต ํ๋ผ๋ฏธํฐ์ ๋ฒ์์์ ์ฌ์ ํ๋ จ ๋ฐ ๋ฏธ์ธ ์กฐ์ ๋ ๋๊ท๋ชจ ์ธ์ด ๋ชจ๋ธ(LLMs)์ ๋ชจ์์ธ Llama 2๋ฅผ ๊ฐ๋ฐ ๋ฐ ๊ณต๊ฐํฉ๋๋ค. Llama 2-Chat๋ผ๊ณ ๋ถ๋ฆฌ๋ ๋ฏธ์ธ ์กฐ์ ๋ LLMs์ ๋ํ ์ฌ์ฉ ์ฌ๋ก์ ์ต์ ํ๋์์ต๋๋ค. ์ฐ๋ฆฌ์ ๋ชจ๋ธ์ ํ ์คํธํ ๋๋ถ๋ถ์ ๋ฒค์น๋งํฌ์์ ์คํ ์์ค ์ฑํ ๋ชจ๋ธ๋ณด๋ค ์ฑ๋ฅ์ด ๋ฐ์ด๋๋ฉฐ, ์ ์ฉ์ฑ๊ณผ ์์ ์ฑ์ ๋ํ ์ธ์ ํ๊ฐ๋ฅผ ๋ฐํ์ผ๋ก ๋น๊ณต๊ฐ ์์ค ๋ชจ๋ธ์ ๋์ฒดํ ์ ์๋ ์ ์ ํ ๋์์ด ๋ ์ ์์ต๋๋ค. ์ฐ๋ฆฌ๋ Llama 2-Chat์ ๋ฏธ์ธ ์กฐ์ ๋ฐ ์์ ์ฑ ํฅ์์ ์ ๊ทผ ๋ฐฉ์์ ๋ํ ์์ธํ ์ค๋ช ์ ์ ๊ณตํ์ฌ ์ปค๋ฎค๋ํฐ๊ฐ ์ฐ๋ฆฌ์ ์์ ์ ๊ธฐ๋ฐ์ผ๋ก LLMs์ ์ฑ ์์๋ ๊ฐ๋ฐ์ ๊ธฐ์ฌํ ์ ์๋๋ก ํฉ๋๋ค.* | |
| [์ฌ๊ธฐ](https://huggingface.co/models?search=llama2)์์ ๋ชจ๋ Llama2 ๋ชจ๋ธ์ ํ์ธํ ์ ์์ต๋๋ค. | |
| <Tip warning={true}> | |
| `Llama2` ๋ชจ๋ธ์ `bfloat16`์ ์ฌ์ฉํ์ฌ ํ๋ จ๋์์ง๋ง, ์๋ ์ถ๋ก ์ `float16`์ ์ฌ์ฉํฉ๋๋ค. ํ๋ธ์ ์ ๋ก๋๋ ์ฒดํฌํฌ์ธํธ๋ `torch_dtype = 'float16'`์ ์ฌ์ฉํ๋ฉฐ, ์ด๋ `AutoModel` API์ ์ํด ์ฒดํฌํฌ์ธํธ๋ฅผ `torch.float32`์์ `torch.float16`์ผ๋ก ์บ์คํ ํ๋ ๋ฐ ์ฌ์ฉ๋ฉ๋๋ค. | |
| ์จ๋ผ์ธ ๊ฐ์ค์น์ `dtype`์ `model = AutoModelForCausalLM.from_pretrained("path", torch_dtype = "auto")`๋ฅผ ์ฌ์ฉํ์ฌ ๋ชจ๋ธ์ ์ด๊ธฐํํ ๋ `torch_dtype="auto"`๋ฅผ ์ฌ์ฉํ์ง ์๋ ํ ๋๋ถ๋ถ ๊ด๋ จ์ด ์์ต๋๋ค. ๊ทธ ์ด์ ๋ ๋ชจ๋ธ์ด ๋จผ์ ๋ค์ด๋ก๋๋ ๊ฒ์ด๊ณ (์จ๋ผ์ธ ์ฒดํฌํฌ์ธํธ์ `dtype`์ ์ฌ์ฉํ์ฌ) ๊ทธ๋ค์์ ๊ธฐ๋ณธ `dtype`์ธ `torch`๋ก ์บ์คํ ํ๊ณ (`torch.float32`๊ฐ ๋จ), ๋ง์ง๋ง์ผ๋ก ๊ตฌ์ฑ(configuration)์์ ์ ๊ณต๋ `torch_dtype`์ด ์๋ ๊ฒฝ์ฐ ์ด๋ฅผ ์ฌ์ฉํ๊ธฐ ๋๋ฌธ์ ๋๋ค. | |
| ๋ชจ๋ธ์ `float16`์์ ํ๋ จํ๋ ๊ฒ์ ๊ถ์ฅ๋์ง ์์ผ๋ฉฐ `nan`์ ์์ฑํ๋ ๊ฒ์ผ๋ก ์๋ ค์ ธ ์์ต๋๋ค. ๋ฐ๋ผ์ ๋ชจ๋ธ์ `bfloat16`์์ ํ๋ จ๋์ด์ผ ํฉ๋๋ค. | |
| </Tip> | |
| ๐ฏ ํ: | |
| - Llama2 ๋ชจ๋ธ์ ๊ฐ์ค์น๋ [์ด ์์](https://ai.meta.com/resources/models-and-libraries/llama-downloads/)์ ์์ฑํ์ฌ ์ป์ ์ ์์ต๋๋ค. | |
| - ์ํคํ ์ฒ๋ ์ฒ์ ๋ฒ์ ์ Llama์ ๋งค์ฐ ์ ์ฌํ๋ฉฐ, [์ด ๋ ผ๋ฌธ](https://arxiv.org/pdf/2305.13245.pdf)์ ๋ด์ฉ์ ๋ฐ๋ผ Grouped Query Attention (GQA)์ด ์ถ๊ฐ๋์์ต๋๋ค. | |
| - `config.pretraining_tp`๋ฅผ 1๊ณผ ๋ค๋ฅธ ๊ฐ์ผ๋ก ์ค์ ํ๋ฉด ๋ ์ ํํ์ง๋ง ๋๋ฆฐ ์ ํ ๋ ์ด์ด ๊ณ์ฐ์ด ํ์ฑํ๋์ด ์๋ณธ ๋ก์ง๊ณผ ๋ ์ ์ผ์นํ๊ฒ ๋ฉ๋๋ค. | |
| - ์๋ ๋ชจ๋ธ์ `pad_id = -1`์ ์ฌ์ฉํ๋๋ฐ, ์ด๋ ํจ๋ฉ ํ ํฐ์ด ์์์ ์๋ฏธํฉ๋๋ค. ๋์ผํ ๋ก์ง์ ์ฌ์ฉํ ์ ์์ผ๋ฏ๋ก `tokenizer.add_special_tokens({"pad_token":"<pad>"})`๋ฅผ ์ฌ์ฉํ์ฌ ํจ๋ฉ ํ ํฐ์ ์ถ๊ฐํ๊ณ ์ด์ ๋ฐ๋ผ ํ ํฐ ์๋ฒ ๋ฉ ํฌ๊ธฐ๋ฅผ ์กฐ์ ํด์ผ ํฉ๋๋ค. ๋ํ `model.config.pad_token_id`๋ฅผ ์ค์ ํด์ผ ํฉ๋๋ค. ๋ชจ๋ธ์ `embed_tokens` ๋ ์ด์ด๋ `self.embed_tokens = nn.Embedding(config.vocab_size, config.hidden_size, self.config.padding_idx)`๋ก ์ด๊ธฐํ๋์ด, ํจ๋ฉ ํ ํฐ ์ธ์ฝ๋ฉ์ด 0์ ์ถ๋ ฅํ๋๋ก ํฉ๋๋ค. ๋ฐ๋ผ์ ์ด๊ธฐํ ์์ ์ ๋ฌํ๋ ๊ฒ์ ๊ถ์ฅํฉ๋๋ค. | |
| - ์์์ ์์ฑํ๊ณ ๋ชจ๋ธ ์ฒดํฌํฌ์ธํธ ์ ๊ทผ ๊ถํ์ ์ป์ ํ์๋ ์ด๋ฏธ ๋ณํ๋ ์ฒดํฌํฌ์ธํธ๋ฅผ ์ฌ์ฉํ ์ ์์ต๋๋ค. ๊ทธ๋ ์ง ์๊ณ ์์ ์ ๋ชจ๋ธ์ ์ง์ ๋ณํํ๋ ค๋ ๊ฒฝ์ฐ, [๋ณํ ์คํฌ๋ฆฝํธ](https://github.com/huggingface/transformers/blob/main/src/transformers/models/llama/convert_llama_weights_to_hf.py)๋ฅผ ์์ ๋กญ๊ฒ ์ฌ์ฉํ์ธ์. ์คํฌ๋ฆฝํธ๋ ๋ค์๊ณผ ๊ฐ์ ์์์ ๋ช ๋ น์ด๋ก ํธ์ถํ ์ ์์ต๋๋ค: | |
| ```bash | |
| python src/transformers/models/llama/convert_llama_weights_to_hf.py \ | |
| --input_dir /path/to/downloaded/llama/weights --model_size 7B --output_dir /output/path | |
| ``` | |
| - ๋ณํ ํ ๋ชจ๋ธ๊ณผ ํ ํฌ๋์ด์ ๋ ๋ค์๊ณผ ๊ฐ์ด ๋ก๋ํ ์ ์์ต๋๋ค: | |
| ```python | |
| from transformers import LlamaForCausalLM, LlamaTokenizer | |
| tokenizer = LlamaTokenizer.from_pretrained("/output/path") | |
| model = LlamaForCausalLM.from_pretrained("/output/path") | |
| ``` | |
| ์คํฌ๋ฆฝํธ๋ฅผ ์คํํ๋ ค๋ฉด ๋ชจ๋ธ์ float16 ์ ๋ฐ๋๋ก ์ ๋ถ ํธ์คํธํ ์ ์์ ๋งํผ ์ถฉ๋ถํ CPU RAM์ด ํ์ํฉ๋๋ค (๊ฐ์ฅ ํฐ ๋ฒ์ ์ด ์ฌ๋ฌ ์ฒดํฌํฌ์ธํธ๋ก ์ ๊ณต๋๋๋ผ๋ ๊ฐ ์ฒดํฌํฌ์ธํธ๋ ๋ชจ๋ธ ๊ฐ์ค์น์ ์ผ๋ถ๋ง์ ํฌํจํ๋ฏ๋ก ๋ชจ๋ RAM์ ๋ก๋ํด์ผ ํฉ๋๋ค). 75B ๋ชจ๋ธ์ ๊ฒฝ์ฐ, ์ด 145GB์ RAM์ด ํ์ํฉ๋๋ค. | |
| - LLaMA ํ ํฌ๋์ด์ ๋ [sentencepiece](https://github.com/google/sentencepiece)๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ํ BPE ๋ชจ๋ธ์ ๋๋ค. sentencepiece์ ํน์ง ์ค ํ๋๋ ์ํ์ค๋ฅผ ๋์ฝ๋ฉํ ๋ ์ฒซ ๋ฒ์งธ ํ ํฐ์ด ๋จ์ด์ ์์์ด๋ฉด (์: "Banana") ํ ํฌ๋์ด์ ๋ ๋ฌธ์์ด ์์ ์ ๋์ฌ ๊ณต๊ฐ์ ์ถ๊ฐํ์ง ์๋ ๊ฒ์ ๋๋ค. | |
| ์ด ๋ชจ๋ธ์ [Arthur Zucker](https://huggingface.co/ArthurZ)๊ฐ [Lysandre Debut](https://huggingface.co/lysandre)์ ๋์์ ๋ฐ์ ์ ๊ณตํ์์ต๋๋ค. Hugging Face์์์ ๊ตฌํ ์ฝ๋๋ [์ฌ๊ธฐ](https://github.com/EleutherAI/gpt-neox)์ GPT-NeoX ๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ํฉ๋๋ค. ์ ์์ ์๋ ์ฝ๋๋ [์ฌ๊ธฐ](https://github.com/facebookresearch/llama)์์ ์ฐพ์ ์ ์์ต๋๋ค. | |
| ## ๋ฆฌ์์ค [[resources]] | |
| LLaMA2๋ฅผ ์์ํ๋ ๋ฐ ๋์์ด ๋ Hugging Face์ ๊ณต์ ๋ฐ ์ปค๋ฎค๋ํฐ(๐๋ก ํ์) ๋ฆฌ์์ค ๋ชฉ๋ก์ ๋๋ค. ์ฌ๊ธฐ์ ์๋ก์ด ๋ฆฌ์์ค๋ฅผ ์ถ๊ฐํ๊ธฐ ์ํด์ Pull Request๋ฅผ ์ด์ด ์ฃผ์๋ฉด ๊ฒํ ํ๊ฒ ์ต๋๋ค! ๋ฆฌ์์ค๋ ๊ธฐ์กด ๋ฆฌ์์ค์ ์ค๋ณต๋์ง ์๋ ์๋ก์ด ๊ฒ์ ๋ณด์ฌ์ฃผ๋ ๊ฒ์ด ์ด์์ ์ ๋๋ค. | |
| - [Llama 2 is here - get it on Hugging Face](https://huggingface.co/blog/llama2), Llama 2์ ๊ดํ ๋ธ๋ก๊ทธ ํฌ์คํธ์ ๐ค Transformers ๋ฐ ๐ค PEFT์ ํจ๊ป ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ์ ๋ํ ๋ด์ฉ์ ๋๋ค. | |
| - [LLaMA 2 - Every Resource you need](https://www.philschmid.de/llama-2), LLaMA 2์ ๋ํด ์์๋ณด๊ณ ๋น ๋ฅด๊ฒ ์์ํ๋ ๋ฐ ํ์ํ ๊ด๋ จ ๋ฆฌ์์ค์ ๋ชจ์์ ๋๋ค. | |
| <PipelineTag pipeline="text-generation"/> | |
| - Google Colab์์ QLoRA์ 4-bit ์ ๋ฐ๋๋ฅผ ์ฌ์ฉํ์ฌ Llama 2๋ฅผ ๋ฏธ์ธ ์กฐ์ ํ๋ ๋ฐฉ๋ฒ์ ๋ํ [๋ ธํธ๋ถ](https://colab.research.google.com/drive/1PEQyJO1-f6j0S_XJ8DV50NkpzasXkrzd?usp=sharing)์ ๋๋ค. ๐ | |
| - "Llama-v2-7b-guanaco" ๋ชจ๋ธ์ 4-bit QLoRA๋ก ๋ฏธ์ธ ์กฐ์ ํ๊ณ PDF์์ Q&A ๋ฐ์ดํฐ์ ์ ์์ฑํ๋ ๋ฐฉ๋ฒ์ ๋ํ [๋ ธํธ๋ถ](https://colab.research.google.com/drive/134o_cXcMe_lsvl15ZE_4Y75Kstepsntu?usp=sharing)์ ๋๋ค. ๐ | |
| โ๏ธ ์ต์ ํ | |
| - [Llama 2๋ฅผ DPO๋ก ๋ฏธ์ธ ์กฐ์ ํ๊ธฐ](https://huggingface.co/blog/dpo-trl), TRL ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ DPO ๋ฐฉ๋ฒ์ ์ฌ์ฉํ์ฌ ํน์ ๋ฐ์ดํฐ์ ์์ Llama 2๋ฅผ ๋ฏธ์ธ ์กฐ์ ํ๋ ๋ฐฉ๋ฒ์ ์๋ดํ๋ ๊ฐ์ด๋์ ๋๋ค. | |
| - [ํ์ฅ ๊ฐ์ด๋: Llama 2 ๋ช ๋ น์ด ์กฐ์ ](https://www.philschmid.de/instruction-tune-llama-2), ์ ๋ ฅ์์ ๋ช ๋ น์ด๋ฅผ ์์ฑํ๋๋ก Llama 2๋ฅผ ํ๋ จ์ํค๋ ๋ฐฉ๋ฒ์ ์๋ดํ๋ ๊ฐ์ด๋๋ก, ๋ช ๋ น์ด๋ฅผ ๋ฐ๋ฅด๋ ๋ชจ๋ธ์์ ๋ช ๋ น์ด๋ฅผ ์ฃผ๋ ๋ชจ๋ธ๋ก ๋ณํํฉ๋๋ค. | |
| - ๊ฐ์ธ ์ปดํจํฐ์์ QLoRA์ TRL์ ์ฌ์ฉํ์ฌ Llama 2 ๋ชจ๋ธ์ ๋ฏธ์ธ ์กฐ์ ํ๋ ๋ฐฉ๋ฒ์ ๋ํ [๋ ธํธ๋ถ](https://colab.research.google.com/drive/1SYpgFpcmtIUzdE7pxqknrM4ArCASfkFQ?usp=sharing)์ ๋๋ค. ๐ | |
| โก๏ธ ์ถ๋ก | |
| - AutoGPTQ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ GPTQ๋ฅผ ์ฌ์ฉํ์ฌ Llama 2 ๋ชจ๋ธ์ ์์ํํ๋ ๋ฐฉ๋ฒ์ ๋ํ [๋ ธํธ๋ถ](https://colab.research.google.com/drive/1TC56ArKerXUpbgRy5vM3woRsbTEVNq7h?usp=sharing)์ ๋๋ค. ๐ | |
| - ๋ก์ปฌ ์ปดํจํฐ๋ Google Colab์์ 4-bit ์์ํ๋ก Llama 2 ์ฑํ ๋ชจ๋ธ์ ์คํํ๋ ๋ฐฉ๋ฒ์ ๋ํ [๋ ธํธ๋ถ](https://colab.research.google.com/drive/1X1z9Q6domMKl2CnEM0QGHNwidLfR4dW2?usp=sharing)์ ๋๋ค. ๐ | |
| ๐ ๋ฐฐํฌ | |
| - [Amazon SageMaker์์ LLaMA 2 (7-70B) ๋ฏธ์ธ ์กฐ์ ํ๊ธฐ](https://www.philschmid.de/sagemaker-llama2-qlora), Amazon SageMaker์์ QLoRA ๋ฏธ์ธ ์กฐ์ ๋ฐ ๋ฐฐํฌ์ ์ด๋ฅด๊ธฐ๊น์ง์ ์์ ํ ๊ฐ์ด๋์ ๋๋ค. | |
| - [Amazon SageMaker์์ Llama 2 7B/13B/70B ๋ฐฐํฌํ๊ธฐ](https://www.philschmid.de/sagemaker-llama-llm), ์์ ํ๊ณ ํ์ฅ ๊ฐ๋ฅํ ๋ฐฐํฌ๋ฅผ ์ํด Hugging Face์ LLM DLC ์ปจํ ์ด๋๋ฅผ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ์ ๋ํ ๊ฐ์ด๋์ ๋๋ค. | |
| ## LlamaConfig [[llamaconfig]] | |
| [[autodoc]] LlamaConfig | |
| ## LlamaTokenizer [[llamatokenizer]] | |
| [[autodoc]] LlamaTokenizer | |
| - build_inputs_with_special_tokens | |
| - get_special_tokens_mask | |
| - create_token_type_ids_from_sequences | |
| - save_vocabulary | |
| ## LlamaTokenizerFast [[llamatokenizerfast]] | |
| [[autodoc]] LlamaTokenizerFast | |
| - build_inputs_with_special_tokens | |
| - get_special_tokens_mask | |
| - create_token_type_ids_from_sequences | |
| - update_post_processor | |
| - save_vocabulary | |
| ## LlamaModel [[llamamodel]] | |
| [[autodoc]] LlamaModel | |
| - forward | |
| ## LlamaForCausalLM [[llamaforcausallm]] | |
| [[autodoc]] LlamaForCausalLM | |
| - forward | |
| ## LlamaForSequenceClassification [[llamaforsequenceclassification]] | |
| [[autodoc]] LlamaForSequenceClassification | |
| - forward | |