| <!--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. | |
| --> | |
| # Quanto[[quanto]] | |
| <Tip> | |
| ์ด [๋ ธํธ๋ถ](https://colab.research.google.com/drive/16CXfVmtdQvciSh9BopZUDYcmXCDpvgrT?usp=sharing)์ผ๋ก Quanto์ transformers๋ฅผ ์ฌ์ฉํด ๋ณด์ธ์! | |
| </Tip> | |
| [๐ค Quanto](https://github.com/huggingface/optimum-quanto) ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ ๋ค๋ชฉ์ ํ์ดํ ์น ์์ํ ํดํท์ ๋๋ค. ์ด ๋ผ์ด๋ธ๋ฌ๋ฆฌ์์ ์ฌ์ฉ๋๋ ์์ํ ๋ฐฉ๋ฒ์ ์ ํ ์์ํ์ ๋๋ค. Quanto๋ ๋ค์๊ณผ ๊ฐ์ ์ฌ๋ฌ ๊ฐ์ง ๊ธฐ๋ฅ์ ์ ๊ณตํฉ๋๋ค: | |
| - ๊ฐ์ค์น ์์ํ (`float8`,`int8`,`int4`,`int2`) | |
| - ํ์ฑํ ์์ํ (`float8`,`int8`) | |
| - ๋ชจ๋ฌ๋ฆฌํฐ์ ๊ตฌ์ ๋ฐ์ง ์์ (e.g CV,LLM) | |
| - ์ฅ์น์ ๊ตฌ์ ๋ฐ์ง ์์ (e.g CUDA,MPS,CPU) | |
| - `torch.compile` ํธํ์ฑ | |
| - ํน์ ์ฅ์น์ ๋ํ ์ฌ์ฉ์ ์ ์ ์ปค๋์ ์ฌ์ด ์ถ๊ฐ | |
| - QAT(์์ํ๋ฅผ ๊ณ ๋ คํ ํ์ต) ์ง์ | |
| <!-- Add link to the blogpost --> | |
| ์์ํ๊ธฐ ์ ์ ๋ค์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๊ฐ ์ค์น๋์ด ์๋์ง ํ์ธํ์ธ์: | |
| ```bash | |
| pip install quanto accelerate transformers | |
| ``` | |
| ์ด์ [`~PreTrainedModel.from_pretrained`] ๋ฉ์๋์ [`QuantoConfig`] ๊ฐ์ฒด๋ฅผ ์ ๋ฌํ์ฌ ๋ชจ๋ธ์ ์์ํํ ์ ์์ต๋๋ค. ์ด ๋ฐฉ์์ `torch.nn.Linear` ๋ ์ด์ด๋ฅผ ํฌํจํ๋ ๋ชจ๋ ๋ชจ๋ฌ๋ฆฌํฐ์ ๋ชจ๋ ๋ชจ๋ธ์์ ์ ์๋ํฉ๋๋ค. | |
| ํ๊น ํ์ด์ค์ transformers ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ ๊ฐ๋ฐ์ ํธ์๋ฅผ ์ํด quanto์ ์ธํฐํ์ด์ค๋ฅผ ์ผ๋ถ ํตํฉํ์ฌ ์ง์ํ๊ณ ์์ผ๋ฉฐ, ์ด ๋ฐฉ์์ผ๋ก๋ ๊ฐ์ค์น ์์ํ๋ง ์ง์ํฉ๋๋ค. ํ์ฑํ ์์ํ, ์บ๋ฆฌ๋ธ๋ ์ด์ , QAT ๊ฐ์ ๋ ๋ณต์กํ ๊ธฐ๋ฅ์ ์ํํ๊ธฐ ์ํด์๋ [quanto](https://github.com/huggingface/optimum-quanto) ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ ํด๋น ํจ์๋ฅผ ์ง์ ํธ์ถํด์ผ ํฉ๋๋ค. | |
| ```py | |
| from transformers import AutoModelForCausalLM, AutoTokenizer, QuantoConfig | |
| model_id = "facebook/opt-125m" | |
| tokenizer = AutoTokenizer.from_pretrained(model_id) | |
| quantization_config = QuantoConfig(weights="int8") | |
| quantized_model = AutoModelForCausalLM.from_pretrained(model_id, device_map="cuda:0", quantization_config=quantization_config) | |
| ``` | |
| ์ฐธ๊ณ ๋ก, transformers์์๋ ์์ง ์ง๋ ฌํ๊ฐ ์ง์๋์ง ์์ง๋ง ๊ณง ์ง์๋ ์์ ์ ๋๋ค! | |
| ๋ชจ๋ธ์ ์ ์ฅํ๊ณ ์ถ์ผ๋ฉด quanto ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ๋์ ์ฌ์ฉํ ์ ์์ต๋๋ค. | |
| Quanto ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ ์์ํ๋ฅผ ์ํด ์ ํ ์์ํ ์๊ณ ๋ฆฌ์ฆ์ ์ฌ์ฉํฉ๋๋ค. ๋น๋ก ๊ธฐ๋ณธ์ ์ธ ์์ํ ๊ธฐ์ ์ด์ง๋ง, ์ข์ ๊ฒฐ๊ณผ๋ฅผ ์ป๋๋ฐ ์์ฃผ ํฐ ๋์์ด ๋ฉ๋๋ค! ๋ฐ๋ก ์๋์ ์๋ ๋ฒค์น๋งํฌ(llama-2-7b์ ํํ๋ ์ํฐ ์งํ)๋ฅผ ํ์ธํด ๋ณด์ธ์. ๋ ๋ง์ ๋ฒค์น๋งํฌ๋ [์ฌ๊ธฐ](https://github.com/huggingface/quanto/tree/main/bench/generation) ์์ ์ฐพ์ ์ ์์ต๋๋ค. | |
| <div class="flex gap-4"> | |
| <div> | |
| <img class="rounded-xl" src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/quantization/NousResearch-Llama-2-7b-hf_Perplexity.png" alt="llama-2-7b-quanto-perplexity" /> | |
| </div> | |
| </div> | |
| ์ด ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ ๋๋ถ๋ถ์ PTQ ์ต์ ํ ์๊ณ ๋ฆฌ์ฆ๊ณผ ํธํ๋ ๋งํผ ์ถฉ๋ถํ ์ ์ฐํฉ๋๋ค. ์์ผ๋ก์ ๊ณํ์ ๊ฐ์ฅ ์ธ๊ธฐ ์๋ ์๊ณ ๋ฆฌ์ฆ(AWQ, Smoothquant)์ ์ต๋ํ ๋งค๋๋ฝ๊ฒ ํตํฉํ๋ ๊ฒ์ ๋๋ค. |