Skip to content
Open
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/source/ko/_toctree.yml
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@
title: (๋ฒˆ์—ญ์ค‘) Getting started
- local: quantization/bitsandbytes
title: bitsandbytes
- local: quantization/compressed_tensors
title: compressed-tensors
- local: quantization/gptq
title: GPTQ
- local: quantization/awq
Expand Down
190 changes: 190 additions & 0 deletions docs/source/ko/quantization/compressed_tensors.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
<!--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.

-->

# compressed-tensors[[compressed-tensors]]

[compressed-tensors](https://github.com/neuralmagic/compressed-tensors)๋Š” [safetensors](https://github.com/huggingface/safetensors) ํŒŒ์ผ์„ ์••์ถ•๋œ ํ…์„œ ๋ฐ์ดํ„ฐ ํƒ€์ž…์œผ๋กœ ํ™•์žฅํ•ด์„œ, dense, int ์–‘์žํ™”(int8), float ์–‘์žํ™”(fp8), pack ์–‘์žํ™”(int32๋กœ ํŒจํ‚น๋œ int4 ๋˜๋Š” int8 ๊ฐ€์ค‘์น˜ ์–‘์žํ™”) ๋“ฑ ๋‹ค์–‘ํ•œ ์–‘์žํ™” ๋ฐ ํฌ์†Œ์„ฑ ํ˜•์‹์„ ํ•˜๋‚˜์˜ ์ฒดํฌํฌ์ธํŠธ ํ˜•์‹์œผ๋กœ ์ €์žฅํ•˜๊ณ  ๋ถˆ๋Ÿฌ์˜ฌ ์ˆ˜ ์žˆ๊ฒŒ ํ•ฉ๋‹ˆ๋‹ค.

compressed-tensors๋Š” [PEFT](https://huggingface.co/docs/peft)๋ฅผ ์‚ฌ์šฉํ•œ ๋ฏธ์„ธ ์กฐ์ •์„ ์ง€์›ํ•˜๋ฉฐ, ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ธฐ๋Šฅ๋“ค์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.

- fp8, int4, int8 ๊ฐ€์ค‘์น˜ ๋ฐ ํ™œ์„ฑํ™” ํ•จ์ˆ˜ ์ถœ๋ ฅ ์ •๋ฐ€๋„.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- fp8, int4, int8 ๊ฐ€์ค‘์น˜ ๋ฐ ํ™œ์„ฑํ™” ํ•จ์ˆ˜ ์ถœ๋ ฅ ์ •๋ฐ€๋„.
- fp8, int4, int8 ๊ฐ€์ค‘์น˜ ๋ฐ ํ™œ์„ฑํ™” ํ•จ์ˆ˜ ์ถœ๋ ฅ ์ •๋ฐ€๋„

- [tensor, channel, group, block, token](https://github.com/neuralmagic/compressed-tensors/blob/83b2e7a969d70606421a76b9a3d112646077c8de/src/compressed_tensors/quantization/quant_args.py#L43-L52) ์ˆ˜์ค€์˜ ์–‘์žํ™” ์Šค์ผ€์ผ๊ณผ ์˜์  ์ „๋žต์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.
- ํ† ํฐ๋ณ„ ๋™์  ํ™œ์„ฑํ™” ํ•จ์ˆ˜ ๊ธฐ๋ฐ˜ ์–‘์žํ™”(๋˜๋Š” ์ •์  ์ „๋žต)๋ฅผ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค.
- ๋น„์ •ํ˜• ๋˜๋Š” 2:4์™€ ๊ฐ™์€ ๋ฐ˜์ •ํ˜• ๊ฐ€์ค‘์น˜ ํฌ์†Œ์„ฑ์„ ์–‘์žํ™”์™€ ๊ฒฐํ•ฉํ•˜์—ฌ ์••์ถ•๋ฅ  ๊ทน๋Œ€ํ™”
- [nn.Linear](https://pytorch.org/docs/stable/generated/torch.nn.Linear.html) ๋ชจ๋“ˆ๋ฟ๋งŒ ์•„๋‹Œ ์ž„์˜์˜ ๋ชจ๋“ˆ ์–‘์žํ™”
- ๋ชจ๋“ˆ ์ด๋ฆ„ ๋˜๋Š” ํด๋ž˜์Šค๋ณ„ ์–‘์žํ™” ๋Œ€์ƒ์„ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ตœ์‹  ์•ˆ์ • ๋ฒ„์ „์€ [PyPI](https://pypi.org/project/compressed-tensors)์—์„œ ์„ค์น˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์•ˆ์ •ํ™”๋˜์ง€ ์•Š์€ ์ตœ์‹  ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ์†Œ์Šค ์ฝ”๋“œ๋ฅผ ์ด์šฉํ•ด ์„ค์น˜ํ•˜์‹ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

<hfoptions id="install">
<hfoption id="PyPI">

```bash
pip install compressed-tensors
```

</hfoption>
<hfoption id="source code">

```bash
git clone https://github.com/neuralmagic/compressed-tensors
cd compressed-tensors
pip install -e .
```

</hfoption>
</hfoptions>

compressed-tensors [ํƒœ๊ทธ](https://huggingface.co/models?other=compressed-tensors)๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ Hugging Face Hub์—์„œ ์–‘์žํ™”๋œ ๋ชจ๋ธ์„ ์ฐพ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

ํ˜„์žฌ๋Š” ์ด๋ฏธ ์–‘์žํ™”๋œ ๋ชจ๋ธ๋งŒ ๋ถˆ๋Ÿฌ์˜ฌ ์ˆ˜ ์žˆ๊ณ , ๋ถˆ๋Ÿฌ์˜จ ๋ชจ๋ธ์€ ๋‹ค์‹œ ์ €์žฅํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. compressed-tensors ํ˜•์‹์œผ๋กœ ๋ชจ๋ธ์„ ์–‘์žํ™”ํ•˜๋ ค๋ฉด [llm-compressor](https://github.com/vllm-project/llm-compressor)๋ฅผ ์ฐธ๊ณ ํ•ด ์ฃผ์„ธ์š”. ๋˜๋Š” ๋ชจ๋ธ์„ ์ง์ ‘ ์ƒ์„ฑํ•˜๊ณ  compressed-tensors ์„ค์ •์œผ๋กœ ์ง๋ ฌํ™”ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.

```python
from transformers import AutoModelForCausalLM

ct_model = AutoModelForCausalLM.from_pretrained("nm-testing/Meta-Llama-3.1-8B-Instruct-FP8-hf", device_map="auto")

# ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ๋Ÿ‰ ์ธก์ •ํ•˜๊ธฐ
mem_params = sum([param.nelement()*param.element_size() for param in ct_model.parameters()])
print(f"{mem_params/2**30:.4f} GB")
# 8.4575 GB
```

## ๋ชจ๋ธ ์ฒดํฌํฌ์ธํŠธ[[model-checkpoint]]

compressed-tensor ๋ชจ๋ธ์€ ์„ค์ • ํ•ญ๋ชฉ์„ ํ†ตํ•ด ์ •์˜๋ฉ๋‹ˆ๋‹ค. ๋‹ค์Œ์€ [nm-testing/Meta-Llama-3.1-8B-Instruct-FP8-hf](https://huggingface.co/nm-testing/Meta-Llama-3.1-8B-Instruct-FP8-hf/blob/main/config.json) `config.json` ํŒŒ์ผ์—์„œ ๊ฐ€์ ธ์˜จ ์˜ˆ์‹œ์ž…๋‹ˆ๋‹ค.

์••์ถ• ์ „ํ›„์˜ ์œ ์—ฐํ•œ ํ‘œํ˜„์„ ์œ„ํ•ด ๋งŽ์€ ํ•ญ๋ชฉ์ด ์กด์žฌํ•˜์ง€๋งŒ, ๋ชจ๋ธ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ์™€ ์ถ”๋ก ์—๋Š” ํ•ต์‹ฌ ํ•ญ๋ชฉ ๋ช‡ ๊ฐ€์ง€๋งŒ ์•Œ์•„๋„ ๋ฉ๋‹ˆ๋‹ค.

```yaml
"quantization_config": {
"config_groups": {
"group_0": {
"input_activations": {
"num_bits": 8,
"strategy": "tensor",
"type": "float"
},
"targets": ["Linear"],
"weights": {
"num_bits": 8,
"strategy": "tensor",
"type": "float"
}
}
},
"format": "naive-quantized",
"ignore": ["lm_head"],
"quant_method": "compressed-tensors",
"quantization_status": "frozen"
},
```

๊ตฌ์„ฑ ํŒŒ์ผ์€ ๊ตฌ์„ฑ ๊ทธ๋ฃน(`group_0`)์— ๋Œ€ํ•ด ํ…์„œ๋ณ„ ์ •์  ์ „๋žต์œผ๋กœ ๊ฐ€์ค‘์น˜์™€ ํ™œ์„ฑํ™” ํ•จ์ˆ˜ ๊ธฐ๋ฐ˜ ๊ฐ’์„ fp8๋กœ ์–‘์žํ™”ํ•˜๋„๋ก ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค. `ignore` ํ‚ค์— ๋ช…์‹œ๋œ ๊ฒƒ์ฒ˜๋Ÿผ `lm_head` ๋ชจ๋“ˆ์€ ์–‘์žํ™”๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

๋ชจ๋ธ ๊ฐ€์ค‘์น˜๋ฅผ ๋” ์ž์„ธํžˆ ๋ณด๋ ค๋ฉด, ๋ชจ๋ธ ์นด๋“œ์˜ [safetensors ๋ทฐ์–ด](https://huggingface.co/nm-testing/Meta-Llama-3.1-8B-Instruct-FP8-hf?show_file_info=model.safetensors.index.json)๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ชจ๋“  [nn.Linear](https://pytorch.org/docs/stable/generated/torch.nn.Linear.html) ๋ชจ๋“ˆ์˜ ์–‘์žํ™”๋œ ๊ฐ€์ค‘์น˜, ์ž…๋ ฅ ์Šค์ผ€์ผ, ๊ฐ€์ค‘์น˜ ์Šค์ผ€์ผ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

| ํ…์„œ | ํ˜•ํƒœ | ์ •๋ฐ€๋„ |
| ------- | ----- | --------- |
model.layers.0.input_layernorm.weight | [4 096] | BF16
model.layers.0.mlp.down_proj.input_scale | [1] | BF16
model.layers.0.mlp.down_proj.weight | [4 096, 14 336] | F8_E4M3
model.layers.0.mlp.down_proj.weight_scale | [1] | BF16
model.layers.0.mlp.gate_proj.input_scale | [1] | BF16
model.layers.0.mlp.gate_proj.weight | [14 336, 4 096] | F8_E4M3
model.layers.0.mlp.gate_proj.weight_scale | [1] | BF16
model.layers.0.mlp.up_proj.input_scale| [1] |BF16
model.layers.0.mlp.up_proj.weight | [14 336, 4 096] | F8_E4M3
model.layers.0.mlp.up_proj.weight_scale | [1] | BF16
model.layers.0.post_attention_layernorm.weight | [4 096] |BF16
model.layers.0.self_attn.k_proj.input_scale | [1] | BF16
model.layers.0.self_attn.k_proj.weight | [1 024, 4 096]| F8_E4M3
model.layers.0.self_attn.k_proj.weight_scale |[1] | BF16
model.layers.0.self_attn.o_proj.input_scale | [1] | BF16
model.layers.0.self_attn.o_proj.weight | [4 096, 4 096] | F8_E4M3
model.layers.0.self_attn.o_proj.weight_scale | [1] | BF16
model.layers.0.self_attn.q_proj.input_scale | [1] | BF16
model.layers.0.self_attn.q_proj.weight | [4 096, 4 096] | F8_E4M3
model.layers.0.self_attn.q_proj.weight_scale | [1] | BF16
model.layers.0.self_attn.v_proj.input_scale | [1] | BF16
model.layers.0.self_attn.v_proj.weight | [1 024, 4 096] | F8_E4M3
model.layers.0.self_attn.v_proj.weight_scale | [1] | BF16

compressed-tensors ๋ชจ๋ธ์„ [`~quantizers.HFQuantizer`] ํ†ตํ•ฉ์œผ๋กœ ๋ถˆ๋Ÿฌ์˜ค๋ฉด, ์–‘์žํ™” ์„ค์ •์— ์ง€์ •๋œ ๋ชจ๋“  [nn.Linear](https://pytorch.org/docs/stable/generated/torch.nn.Linear.html) ๋ชจ๋“ˆ์ด [CompressedLinear](https://github.com/neuralmagic/compressed-tensors/blob/975cb223b19fcac2b98a4271d17668462d4d6e1d/src/compressed_tensors/linear/compressed_linear.py#L30) ๋ชจ๋“ˆ๋กœ ๋Œ€์ฒด๋˜์–ด ์••์ถ• ๊ฐ€์ค‘์น˜์™€ ์ˆœ์ „ํŒŒ๋ฅผ ๊ด€๋ฆฌํ•ฉ๋‹ˆ๋‹ค. `lm_head` ๋ชจ๋“ˆ์€ ์—ฌ์ „ํžˆ ์–‘์žํ™”๋˜์ง€ ์•Š์€ nn.Linear ๋ชจ๋“ˆ๋กœ ์œ ์ง€๋ฉ๋‹ˆ๋‹ค.

```python
from transformers import AutoModelForCausalLM

ct_model = AutoModelForCausalLM.from_pretrained("nm-testing/Meta-Llama-3.1-8B-Instruct-FP8-hf")
print(ct_model)
"""
LlamaForCausalLM(
(model): LlamaModel(
(embed_tokens): Embedding(128256, 4096)
(layers): ModuleList(
(0-31): 32 x LlamaDecoderLayer(
(self_attn): LlamaSdpaAttention(
(q_proj): CompressedLinear(
in_features=4096, out_features=4096, bias=False
(input_observer): MovingAverageMinMaxObserver()
(weight_observer): MovingAverageMinMaxObserver()
)
(k_proj): CompressedLinear(
in_features=4096, out_features=1024, bias=False
(input_observer): MovingAverageMinMaxObserver()
(weight_observer): MovingAverageMinMaxObserver()
)
(v_proj): CompressedLinear(
in_features=4096, out_features=1024, bias=False
(input_observer): MovingAverageMinMaxObserver()
(weight_observer): MovingAverageMinMaxObserver()
)
(o_proj): CompressedLinear(
in_features=4096, out_features=4096, bias=False
(input_observer): MovingAverageMinMaxObserver()
(weight_observer): MovingAverageMinMaxObserver()
)
(rotary_emb): LlamaRotaryEmbedding()
)
(mlp): LlamaMLP(
(gate_proj): CompressedLinear(
in_features=4096, out_features=14336, bias=False
(input_observer): MovingAverageMinMaxObserver()
(weight_observer): MovingAverageMinMaxObserver()
)
(up_proj): CompressedLinear(
in_features=4096, out_features=14336, bias=False
(input_observer): MovingAverageMinMaxObserver()
(weight_observer): MovingAverageMinMaxObserver()
)
(down_proj): CompressedLinear(
in_features=14336, out_features=4096, bias=False
(input_observer): MovingAverageMinMaxObserver()
(weight_observer): MovingAverageMinMaxObserver()
)
(act_fn): SiLU()
)
(input_layernorm): LlamaRMSNorm((4096,), eps=1e-05)
(post_attention_layernorm): LlamaRMSNorm((4096,), eps=1e-05)
)
)
(norm): LlamaRMSNorm((4096,), eps=1e-05)
(rotary_emb): LlamaRotaryEmbedding()
)
(lm_head): Linear(in_features=4096, out_features=128256, bias=False)
)
"""
```
Loading