| <!--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. | |
| --> | |
| # GPTQ [[gptq]] | |
| <Tip> | |
| PEFT๋ฅผ ํ์ฉํ GPTQ ์์ํ๋ฅผ ์ฌ์ฉํด๋ณด์๋ ค๋ฉด ์ด [๋ ธํธ๋ถ](https://colab.research.google.com/drive/1_TIrmuKOFhuRRiTWN94iLKUFu6ZX4ceb)์ ์ฐธ๊ณ ํ์๊ณ , ์์ธํ ๋ด์ฉ์ ์ด [๋ธ๋ก๊ทธ ๊ฒ์๋ฌผ](https://huggingface.co/blog/gptq-integration)์์ ํ์ธํ์ธ์! | |
| </Tip> | |
| [AutoGPTQ](https://github.com/PanQiWei/AutoGPTQ) ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ GPTQ ์๊ณ ๋ฆฌ์ฆ์ ๊ตฌํํฉ๋๋ค. ์ด๋ ํ๋ จ ํ ์์ํ ๊ธฐ๋ฒ์ผ๋ก, ๊ฐ์ค์น ํ๋ ฌ์ ๊ฐ ํ์ ๋ ๋ฆฝ์ ์ผ๋ก ์์ํํ์ฌ ์ค์ฐจ๋ฅผ ์ต์ํํ๋ ๊ฐ์ค์น ๋ฒ์ ์ ์ฐพ์ต๋๋ค. ์ด ๊ฐ์ค์น๋ int4๋ก ์์ํ๋์ง๋ง, ์ถ๋ก ์ค์๋ ์ค์๊ฐ์ผ๋ก fp16์ผ๋ก ๋ณต์๋ฉ๋๋ค. ์ด๋ int4 ๊ฐ์ค์น๊ฐ GPU์ ์ ์ญ ๋ฉ๋ชจ๋ฆฌ ๋์ ๊ฒฐํฉ๋ ์ปค๋์์ ์ญ์์ํ๋๊ธฐ ๋๋ฌธ์ ๋ฉ๋ชจ๋ฆฌ ์ฌ์ฉ๋์ 4๋ฐฐ ์ ์ฝํ ์ ์์ผ๋ฉฐ, ๋ ๋ฎ์ ๋นํธ ๋๋น๋ฅผ ์ฌ์ฉํจ์ผ๋ก์จ ํต์ ์๊ฐ์ด ์ค์ด๋ค์ด ์ถ๋ก ์๋๊ฐ ๋นจ๋ผ์ง ๊ฒ์ผ๋ก ๊ธฐ๋ํ ์ ์์ต๋๋ค. | |
| ์์ํ๊ธฐ ์ ์ ๋ค์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ค์ด ์ค์น๋์ด ์๋์ง ํ์ธํ์ธ์: | |
| ```bash | |
| pip install auto-gptq | |
| pip install --upgrade accelerate optimum transformers | |
| ``` | |
| ๋ชจ๋ธ์ ์์ํํ๋ ค๋ฉด(ํ์ฌ ํ ์คํธ ๋ชจ๋ธ๋ง ์ง์๋จ) [`GPTQConfig`] ํด๋์ค๋ฅผ ์์ฑํ๊ณ ์์ํํ ๋นํธ ์, ์์ํ๋ฅผ ์ํ ๊ฐ์ค์น ๊ต์ ๋ฐ์ดํฐ์ , ๊ทธ๋ฆฌ๊ณ ๋ฐ์ดํฐ์ ์ ์ค๋นํ๊ธฐ ์ํ ํ ํฌ๋์ด์ ๋ฅผ ์ค์ ํด์ผ ํฉ๋๋ค. | |
| ```py | |
| from transformers import AutoModelForCausalLM, AutoTokenizer, GPTQConfig | |
| model_id = "facebook/opt-125m" | |
| tokenizer = AutoTokenizer.from_pretrained(model_id) | |
| gptq_config = GPTQConfig(bits=4, dataset="c4", tokenizer=tokenizer) | |
| ``` | |
| ์์ ์ ๋ฐ์ดํฐ์ ์ ๋ฌธ์์ด ๋ฆฌ์คํธ ํํ๋ก ์ ๋ฌํ ์๋ ์์ง๋ง, GPTQ ๋ ผ๋ฌธ์์ ์ฌ์ฉํ ๋์ผํ ๋ฐ์ดํฐ์ ์ ์ฌ์ฉํ๋ ๊ฒ์ ๊ฐ๋ ฅํ ๊ถ์ฅํฉ๋๋ค. | |
| ```py | |
| dataset = ["auto-gptq is an easy-to-use model quantization library with user-friendly apis, based on GPTQ algorithm."] | |
| gptq_config = GPTQConfig(bits=4, dataset=dataset, tokenizer=tokenizer) | |
| ``` | |
| ์์ํํ ๋ชจ๋ธ์ ๋ก๋ํ๊ณ `gptq_config`์ [`~AutoModelForCausalLM.from_pretrained`] ๋ฉ์๋์ ์ ๋ฌํ์ธ์. ๋ชจ๋ธ์ ๋ฉ๋ชจ๋ฆฌ์ ๋ง์ถ๊ธฐ ์ํด `device_map="auto"`๋ฅผ ์ค์ ํ์ฌ ๋ชจ๋ธ์ ์๋์ผ๋ก CPU๋ก ์คํ๋ก๋ํ๊ณ , ์์ํ๋ฅผ ์ํด ๋ชจ๋ธ ๋ชจ๋์ด CPU์ GPU ๊ฐ์ ์ด๋ํ ์ ์๋๋ก ํฉ๋๋ค. | |
| ```py | |
| quantized_model = AutoModelForCausalLM.from_pretrained(model_id, device_map="auto", quantization_config=gptq_config) | |
| ``` | |
| ๋ฐ์ดํฐ์ ์ด ๋๋ฌด ์ปค์ ๋ฉ๋ชจ๋ฆฌ๊ฐ ๋ถ์กฑํ ๊ฒฝ์ฐ๋ฅผ ๋๋นํ ๋์คํฌ ์คํ๋ก๋๋ ํ์ฌ ์ง์ํ์ง ์๊ณ ์์ต๋๋ค. ์ด๋ด ๋๋ `max_memory` ๋งค๊ฐ๋ณ์๋ฅผ ์ฌ์ฉํ์ฌ ๋๋ฐ์ด์ค(GPU ๋ฐ CPU)์์ ์ฌ์ฉํ ๋ฉ๋ชจ๋ฆฌ ์์ ํ ๋นํด ๋ณด์ธ์: | |
| ```py | |
| quantized_model = AutoModelForCausalLM.from_pretrained(model_id, device_map="auto", max_memory={0: "30GiB", 1: "46GiB", "cpu": "30GiB"}, quantization_config=gptq_config) | |
| ``` | |
| <Tip warning={true}> | |
| ํ๋์จ์ด์ ๋ชจ๋ธ ๋งค๊ฐ๋ณ์๋์ ๋ฐ๋ผ ๋ชจ๋ธ์ ์ฒ์๋ถํฐ ์์ํํ๋ ๋ฐ ๋๋ ์๊ฐ์ด ์๋ก ๋ค๋ฅผ ์ ์์ต๋๋ค. ์๋ฅผ ๋ค์ด, ๋ฌด๋ฃ ๋ฑ๊ธ์ Google Colab GPU๋ก ๋น๊ต์ ๊ฐ๋ฒผ์ด [facebook/opt-350m](https://huggingface.co/facebook/opt-350m) ๋ชจ๋ธ์ ์์ํํ๋ ๋ฐ ์ฝ 5๋ถ์ด ๊ฑธ๋ฆฌ์ง๋ง, NVIDIA A100์ผ๋ก 175B์ ๋ฌํ๋ ๋งค๊ฐ๋ณ์๋ฅผ ๊ฐ์ง ๋ชจ๋ธ์ ์์ํํ๋ ๋ฐ๋ ์ฝ 4์๊ฐ์ ๋ฌํ๋ ์๊ฐ์ด ๊ฑธ๋ฆด ์ ์์ต๋๋ค. ๋ชจ๋ธ์ ์์ํํ๊ธฐ ์ ์, Hub์์ ํด๋น ๋ชจ๋ธ์ GPTQ ์์ํ ๋ฒ์ ์ด ์ด๋ฏธ ์กด์ฌํ๋์ง ํ์ธํ๋ ๊ฒ์ด ์ข์ต๋๋ค. | |
| </Tip> | |
| ๋ชจ๋ธ์ด ์์ํ๋๋ฉด, ๋ชจ๋ธ๊ณผ ํ ํฌ๋์ด์ ๋ฅผ Hub์ ํธ์ํ์ฌ ์ฝ๊ฒ ๊ณต์ ํ๊ณ ์ ๊ทผํ ์ ์์ต๋๋ค. [`GPTQConfig`]๋ฅผ ์ ์ฅํ๊ธฐ ์ํด [`~PreTrainedModel.push_to_hub`] ๋ฉ์๋๋ฅผ ์ฌ์ฉํ์ธ์: | |
| ```py | |
| quantized_model.push_to_hub("opt-125m-gptq") | |
| tokenizer.push_to_hub("opt-125m-gptq") | |
| ``` | |
| ์์ํ๋ ๋ชจ๋ธ์ ๋ก์ปฌ์ ์ ์ฅํ๋ ค๋ฉด [`~PreTrainedModel.save_pretrained`] ๋ฉ์๋๋ฅผ ์ฌ์ฉํ ์ ์์ต๋๋ค. ๋ชจ๋ธ์ด `device_map` ๋งค๊ฐ๋ณ์๋ก ์์ํ๋์์ ๊ฒฝ์ฐ, ์ ์ฅํ๊ธฐ ์ ์ ์ ์ฒด ๋ชจ๋ธ์ GPU๋ CPU๋ก ์ด๋ํด์ผ ํฉ๋๋ค. ์๋ฅผ ๋ค์ด, ๋ชจ๋ธ์ CPU์ ์ ์ฅํ๋ ค๋ฉด ๋ค์๊ณผ ๊ฐ์ด ํฉ๋๋ค: | |
| ```py | |
| quantized_model.save_pretrained("opt-125m-gptq") | |
| tokenizer.save_pretrained("opt-125m-gptq") | |
| # device_map์ด ์ค์ ๋ ์ํ์์ ์์ํ๋ ๊ฒฝ์ฐ | |
| quantized_model.to("cpu") | |
| quantized_model.save_pretrained("opt-125m-gptq") | |
| ``` | |
| ์์ํ๋ ๋ชจ๋ธ์ ๋ค์ ๋ก๋ํ๋ ค๋ฉด [`~PreTrainedModel.from_pretrained`] ๋ฉ์๋๋ฅผ ์ฌ์ฉํ๊ณ , `device_map="auto"`๋ฅผ ์ค์ ํ์ฌ ๋ชจ๋ ์ฌ์ฉ ๊ฐ๋ฅํ GPU์ ๋ชจ๋ธ์ ์๋์ผ๋ก ๋ถ์ฐ์์ผ ๋ ๋ง์ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ์ฌ์ฉํ์ง ์์ผ๋ฉด์ ๋ชจ๋ธ์ ๋ ๋น ๋ฅด๊ฒ ๋ก๋ํ ์ ์์ต๋๋ค. | |
| ```py | |
| from transformers import AutoModelForCausalLM | |
| model = AutoModelForCausalLM.from_pretrained("{your_username}/opt-125m-gptq", device_map="auto") | |
| ``` | |
| ## ExLlama [[exllama]] | |
| [ExLlama](https://github.com/turboderp/exllama)์ [Llama](model_doc/llama) ๋ชจ๋ธ์ Python/C++/CUDA ๊ตฌํ์ฒด๋ก, 4๋นํธ GPTQ ๊ฐ์ค์น๋ฅผ ์ฌ์ฉํ์ฌ ๋ ๋น ๋ฅธ ์ถ๋ก ์ ์ํด ์ค๊ณ๋์์ต๋๋ค(์ด [๋ฒค์น๋งํฌ](https://github.com/huggingface/optimum/tree/main/tests/benchmark#gptq-benchmark)๋ฅผ ์ฐธ๊ณ ํ์ธ์). ['GPTQConfig'] ๊ฐ์ฒด๋ฅผ ์์ฑํ ๋ ExLlama ์ปค๋์ด ๊ธฐ๋ณธ์ ์ผ๋ก ํ์ฑํ๋ฉ๋๋ค. ์ถ๋ก ์๋๋ฅผ ๋์ฑ ๋์ด๊ธฐ ์ํด, `exllama_config` ๋งค๊ฐ๋ณ์๋ฅผ ๊ตฌ์ฑํ์ฌ [ExLlamaV2](https://github.com/turboderp/exllamav2) ์ปค๋์ ์ฌ์ฉํ ์ ์์ต๋๋ค: | |
| ```py | |
| import torch | |
| from transformers import AutoModelForCausalLM, GPTQConfig | |
| gptq_config = GPTQConfig(bits=4, exllama_config={"version":2}) | |
| model = AutoModelForCausalLM.from_pretrained("{your_username}/opt-125m-gptq", device_map="auto", quantization_config=gptq_config) | |
| ``` | |
| <Tip warning={true}> | |
| 4๋นํธ ๋ชจ๋ธ๋ง ์ง์๋๋ฉฐ, ์์ํ๋ ๋ชจ๋ธ์ PEFT๋ก ๋ฏธ์ธ ์กฐ์ ํ๋ ๊ฒฝ์ฐ ExLlama ์ปค๋์ ๋นํ์ฑํํ ๊ฒ์ ๊ถ์ฅํฉ๋๋ค. | |
| </Tip> | |
| ExLlama ์ปค๋์ ์ ์ฒด ๋ชจ๋ธ์ด GPU์ ์์ ๋๋ง ์ง์๋ฉ๋๋ค. AutoGPTQ(๋ฒ์ 0.4.2 ์ด์)๋ก CPU์์ ์ถ๋ก ์ ์ํํ๋ ๊ฒฝ์ฐ ExLlama ์ปค๋์ ๋นํ์ฑํํด์ผ ํฉ๋๋ค. ์ด๋ฅผ ์ํด config.json ํ์ผ์ ์์ํ ์ค์ ์์ ExLlama ์ปค๋๊ณผ ๊ด๋ จ๋ ์์ฑ์ ๋ฎ์ด์จ์ผ ํฉ๋๋ค. | |
| ```py | |
| import torch | |
| from transformers import AutoModelForCausalLM, GPTQConfig | |
| gptq_config = GPTQConfig(bits=4, use_exllama=False) | |
| model = AutoModelForCausalLM.from_pretrained("{your_username}/opt-125m-gptq", device_map="cpu", quantization_config=gptq_config) | |
| ``` |