huggingface--transformers
e06fe8e8c6
Secret Leaks / trufflehog (push) Failing after 1s
Build documentation / build (push) Failing after 1s
Build documentation / build_other_lang (push) Failing after 0s
CodeQL Security Analysis / CodeQL Analysis (push) Failing after 0s
PR CI / pr-ci (push) Failing after 1s
Slow tests on important models (on Push - A10) / Get all modified files (push) Failing after 1s
Slow tests on important models (on Push - A10) / Model CI (push) Has been skipped
Self-hosted runner (benchmark) / Benchmark (aws-g5-4xlarge-cache) (push) Has been cancelled
New model PR merged notification / Notify new model (push) Has been cancelled
Update Transformers metadata / build_and_package (push) Has been cancelled
94 ่ก
5.7 KiB
Markdown
94 ่ก
5.7 KiB
Markdown
<!--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>
|
|
|
|
[GPT-QModel](https://github.com/ModelCloud/GPTQModel) ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ GPTQ ์๊ณ ๋ฆฌ์ฆ์ ๊ตฌํํฉ๋๋ค. ์ด๋ ํ๋ จ ํ ์์ํ ๊ธฐ๋ฒ์ผ๋ก, ๊ฐ์ค์น ํ๋ ฌ์ ๊ฐ ํ์ ๋
๋ฆฝ์ ์ผ๋ก ์์ํํ์ฌ ์ค์ฐจ๋ฅผ ์ต์ํํ๋ ๊ฐ์ค์น ๋ฒ์ ์ ์ฐพ์ต๋๋ค. ์ด ๊ฐ์ค์น๋ int4๋ก ์์ํ๋์ง๋ง, ์ถ๋ก ์ค์๋ ์ค์๊ฐ์ผ๋ก fp16์ผ๋ก ๋ณต์๋ฉ๋๋ค. ์ด๋ int4 ๊ฐ์ค์น๊ฐ GPU์ ์ ์ญ ๋ฉ๋ชจ๋ฆฌ ๋์ ๊ฒฐํฉ๋ ์ปค๋์์ ์ญ์์ํ๋๊ธฐ ๋๋ฌธ์ ๋ฉ๋ชจ๋ฆฌ ์ฌ์ฉ๋์ 4๋ฐฐ ์ ์ฝํ ์ ์์ผ๋ฉฐ, ๋ ๋ฎ์ ๋นํธ ๋๋น๋ฅผ ์ฌ์ฉํจ์ผ๋ก์จ ํต์ ์๊ฐ์ด ์ค์ด๋ค์ด ์ถ๋ก ์๋๊ฐ ๋นจ๋ผ์ง ๊ฒ์ผ๋ก ๊ธฐ๋ํ ์ ์์ต๋๋ค.
|
|
|
|
์์ํ๊ธฐ ์ ์ ๋ค์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ค์ด ์ค์น๋์ด ์๋์ง ํ์ธํ์ธ์:
|
|
|
|
```bash
|
|
pip install gptqmodel --no-build-isolation
|
|
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 = ["gptqmodel is an easy-to-use model quantization library with user-friendly apis, based on the 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")
|
|
```
|