File size: 3,924 Bytes
17c6d62
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<!--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)์„ ์ตœ๋Œ€ํ•œ ๋งค๋„๋Ÿฝ๊ฒŒ ํ†ตํ•ฉํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.