Spaces:
Runtime error
Runtime error
File size: 10,740 Bytes
96e9536 |
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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
<!--Copyright 2022 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
โ ๏ธ 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.
-->
# ๋จ์ผ GPU์์ ํจ์จ์ ์ธ ์ถ๋ก [[efficient-inference-on-a-single-gpu]]
์ด ๊ฐ์ด๋ ์ธ์๋, [๋จ์ผ GPU์์์ ํ๋ จ ๊ฐ์ด๋](perf_train_gpu_one)์ [CPU์์์ ์ถ๋ก ๊ฐ์ด๋](perf_infer_cpu)์์๋ ๊ด๋ จ ์ ๋ณด๋ฅผ ์ฐพ์ ์ ์์ต๋๋ค.
## Better Transformer: PyTorch ๋ค์ดํฐ๋ธ Transformer ํจ์คํธํจ์ค [[better-transformer-pytorchnative-transformer-fastpath]]
PyTorch ๋ค์ดํฐ๋ธ [`nn.MultiHeadAttention`](https://pytorch.org/blog/a-better-transformer-for-fast-transformer-encoder-inference/) ์ดํ
์
ํจ์คํธํจ์ค์ธ BetterTransformer๋ [๐ค Optimum ๋ผ์ด๋ธ๋ฌ๋ฆฌ](https://huggingface.co/docs/optimum/bettertransformer/overview)์ ํตํฉ์ ํตํด Transformers์ ํจ๊ป ์ฌ์ฉํ ์ ์์ต๋๋ค.
PyTorch์ ์ดํ
์
ํจ์คํธํจ์ค๋ ์ปค๋ ํจ์ ๊ณผ [์ค์ฒฉ๋ ํ
์](https://pytorch.org/docs/stable/nested.html)์ ์ฌ์ฉ์ ํตํด ์ถ๋ก ์๋๋ฅผ ๋์ผ ์ ์์ต๋๋ค. ์์ธํ ๋ฒค์น๋งํฌ๋ [์ด ๋ธ๋ก๊ทธ ๊ธ](https://medium.com/pytorch/bettertransformer-out-of-the-box-performance-for-huggingface-transformers-3fbe27d50ab2)์์ ํ์ธํ ์ ์์ต๋๋ค.
[`optimum`](https://github.com/huggingface/optimum) ํจํค์ง๋ฅผ ์ค์นํ ํ์๋ ์ถ๋ก ์ค Better Transformer๋ฅผ ์ฌ์ฉํ ์ ์๋๋ก [`~PreTrainedModel.to_bettertransformer`]๋ฅผ ํธ์ถํ์ฌ ๊ด๋ จ ๋ด๋ถ ๋ชจ๋์ ๋์ฒดํฉ๋๋ค:
```python
model = model.to_bettertransformer()
```
[`~PreTrainedModel.reverse_bettertransformer`] ๋ฉ์๋๋ ์ ๊ทํ๋ transformers ๋ชจ๋ธ๋ง์ ์ฌ์ฉํ๊ธฐ ์ํด ๋ชจ๋ธ์ ์ ์ฅํ๊ธฐ ์ ์๋์ ๋ชจ๋ธ๋ง์ผ๋ก ๋์๊ฐ ์ ์๋๋ก ํด์ค๋๋ค:
```python
model = model.reverse_bettertransformer()
model.save_pretrained("saved_model")
```
PyTorch 2.0๋ถํฐ๋ ์ดํ
์
ํจ์คํธํจ์ค๊ฐ ์ธ์ฝ๋์ ๋์ฝ๋ ๋ชจ๋์์ ์ง์๋ฉ๋๋ค. ์ง์๋๋ ์ํคํ
์ฒ ๋ชฉ๋ก์ [์ฌ๊ธฐ](https://huggingface.co/docs/optimum/bettertransformer/overview#supported-models)์์ ํ์ธํ ์ ์์ต๋๋ค.
## FP4 ํผํฉ ์ ๋ฐ๋ ์ถ๋ก ์ ์ํ `bitsandbytes` ํตํฉ [[bitsandbytes-integration-for-fp4-mixedprecision-inference]]
`bitsandbytes`๋ฅผ ์ค์นํ๋ฉด GPU์์ ์์ฝ๊ฒ ๋ชจ๋ธ์ ์์ถํ ์ ์์ต๋๋ค. FP4 ์์ํ๋ฅผ ์ฌ์ฉํ๋ฉด ์๋์ ์ ์ฒด ์ ๋ฐ๋ ๋ฒ์ ๊ณผ ๋น๊ตํ์ฌ ๋ชจ๋ธ ํฌ๊ธฐ๋ฅผ ์ต๋ 8๋ฐฐ ์ค์ผ ์ ์์ต๋๋ค. ์๋์์ ์์ํ๋ ๋ฐฉ๋ฒ์ ํ์ธํ์ธ์.
<Tip>
์ด ๊ธฐ๋ฅ์ ๋ค์ค GPU ์ค์ ์์๋ ์ฌ์ฉํ ์ ์์ต๋๋ค.
</Tip>
### ์๊ตฌ ์ฌํญ [[requirements-for-fp4-mixedprecision-inference]]
- ์ต์ `bitsandbytes` ๋ผ์ด๋ธ๋ฌ๋ฆฌ
`pip install bitsandbytes>=0.39.0`
- ์ต์ `accelerate`๋ฅผ ์์ค์์ ์ค์น
`pip install git+https://github.com/huggingface/accelerate.git`
- ์ต์ `transformers`๋ฅผ ์์ค์์ ์ค์น
`pip install git+https://github.com/huggingface/transformers.git`
### FP4 ๋ชจ๋ธ ์คํ - ๋จ์ผ GPU ์ค์ - ๋น ๋ฅธ ์์ [[running-fp4-models-single-gpu-setup-quickstart]]
๋ค์ ์ฝ๋๋ฅผ ์คํํ์ฌ ๋จ์ผ GPU์์ ๋น ๋ฅด๊ฒ FP4 ๋ชจ๋ธ์ ์คํํ ์ ์์ต๋๋ค.
```py
from transformers import AutoModelForCausalLM
model_name = "bigscience/bloom-2b5"
model_4bit = AutoModelForCausalLM.from_pretrained(model_name, device_map="auto", load_in_4bit=True)
```
`device_map`์ ์ ํ ์ฌํญ์
๋๋ค. ๊ทธ๋ฌ๋ `device_map = 'auto'`๋ก ์ค์ ํ๋ ๊ฒ์ด ์ฌ์ฉ ๊ฐ๋ฅํ ๋ฆฌ์์ค๋ฅผ ํจ์จ์ ์ผ๋ก ๋์คํจ์นํ๊ธฐ ๋๋ฌธ์ ์ถ๋ก ์ ์์ด ๊ถ์ฅ๋ฉ๋๋ค.
### FP4 ๋ชจ๋ธ ์คํ - ๋ค์ค GPU ์ค์ [[running-fp4-models-multi-gpu-setup]]
๋ค์ค GPU์์ ํผํฉ 4๋นํธ ๋ชจ๋ธ์ ๊ฐ์ ธ์ค๋ ๋ฐฉ๋ฒ์ ๋จ์ผ GPU ์ค์ ๊ณผ ๋์ผํฉ๋๋ค(๋์ผํ ๋ช
๋ น์ด ์ฌ์ฉ):
```py
model_name = "bigscience/bloom-2b5"
model_4bit = AutoModelForCausalLM.from_pretrained(model_name, device_map="auto", load_in_4bit=True)
```
ํ์ง๋ง `accelerate`๋ฅผ ์ฌ์ฉํ์ฌ ๊ฐ GPU์ ํ ๋นํ GPU RAM์ ์ ์ดํ ์ ์์ต๋๋ค. ๋ค์๊ณผ ๊ฐ์ด `max_memory` ์ธ์๋ฅผ ์ฌ์ฉํ์ธ์:
```py
max_memory_mapping = {0: "600MB", 1: "1GB"}
model_name = "bigscience/bloom-3b"
model_4bit = AutoModelForCausalLM.from_pretrained(
model_name, device_map="auto", load_in_4bit=True, max_memory=max_memory_mapping
)
```
์ด ์์์๋ ์ฒซ ๋ฒ์งธ GPU๊ฐ 600MB์ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ์ฌ์ฉํ๊ณ ๋ ๋ฒ์งธ GPU๊ฐ 1GB๋ฅผ ์ฌ์ฉํฉ๋๋ค.
### ๊ณ ๊ธ ์ฌ์ฉ๋ฒ [[advanced-usage]]
์ด ๋ฐฉ๋ฒ์ ๋ ๊ณ ๊ธ ์ฌ์ฉ๋ฒ์ ๋ํด์๋ [์์ํ](main_classes/quantization) ๋ฌธ์ ํ์ด์ง๋ฅผ ์ฐธ์กฐํ์ธ์.
## Int8 ํผํฉ ์ ๋ฐ๋ ํ๋ ฌ ๋ถํด๋ฅผ ์ํ `bitsandbytes` ํตํฉ [[bitsandbytes-integration-for-int8-mixedprecision-matrix-decomposition]]
<Tip>
์ด ๊ธฐ๋ฅ์ ๋ค์ค GPU ์ค์ ์์๋ ์ฌ์ฉํ ์ ์์ต๋๋ค.
</Tip>
[`LLM.int8() : 8-bit Matrix Multiplication for Transformers at Scale`](https://arxiv.org/abs/2208.07339) ๋
ผ๋ฌธ์์ ์ฐ๋ฆฌ๋ ๋ช ์ค์ ์ฝ๋๋ก Hub์ ๋ชจ๋ ๋ชจ๋ธ์ ๋ํ Hugging Face ํตํฉ์ ์ง์ํฉ๋๋ค.
์ด ๋ฐฉ๋ฒ์ `float16` ๋ฐ `bfloat16` ๊ฐ์ค์น์ ๋ํด `nn.Linear` ํฌ๊ธฐ๋ฅผ 2๋ฐฐ๋ก ์ค์ด๊ณ , `float32` ๊ฐ์ค์น์ ๋ํด 4๋ฐฐ๋ก ์ค์
๋๋ค. ์ด๋ ์ ๋ฐ ์ ๋ฐ๋์์ ์ด์์น๋ฅผ ์ฒ๋ฆฌํจ์ผ๋ก์จ ํ์ง์ ๊ฑฐ์ ์ํฅ์ ๋ฏธ์น์ง ์์ต๋๋ค.

Int8 ํผํฉ ์ ๋ฐ๋ ํ๋ ฌ ๋ถํด๋ ํ๋ ฌ ๊ณฑ์
์ ๋ ๊ฐ์ ์คํธ๋ฆผ์ผ๋ก ๋ถ๋ฆฌํฉ๋๋ค: (1) fp16๋ก ๊ณฑํด์ง๋ ์ฒด๊ณ์ ์ธ ํน์ด๊ฐ ์ด์์น ์คํธ๋ฆผ ํ๋ ฌ(0.01%) ๋ฐ (2) int8 ํ๋ ฌ ๊ณฑ์
์ ์ผ๋ฐ์ ์ธ ์คํธ๋ฆผ(99.9%). ์ด ๋ฐฉ๋ฒ์ ์ฌ์ฉํ๋ฉด ๋งค์ฐ ํฐ ๋ชจ๋ธ์ ๋ํด ์์ธก ์ ํ ์์ด int8 ์ถ๋ก ์ด ๊ฐ๋ฅํฉ๋๋ค.
์ด ๋ฐฉ๋ฒ์ ๋ํ ์์ธํ ๋ด์ฉ์ [๋
ผ๋ฌธ](https://arxiv.org/abs/2208.07339)์ด๋ [ํตํฉ์ ๊ดํ ๋ธ๋ก๊ทธ ๊ธ](https://huggingface.co/blog/hf-bitsandbytes-integration)์์ ํ์ธํ ์ ์์ต๋๋ค.

์ปค๋์ GPU ์ ์ฉ์ผ๋ก ์ปดํ์ผ๋์ด ์๊ธฐ ๋๋ฌธ์ ํผํฉ 8๋นํธ ๋ชจ๋ธ์ ์คํํ๋ ค๋ฉด GPU๊ฐ ํ์ํฉ๋๋ค. ์ด ๊ธฐ๋ฅ์ ์ฌ์ฉํ๊ธฐ ์ ์ ๋ชจ๋ธ์ 1/4(๋๋ ๋ชจ๋ธ ๊ฐ์ค์น๊ฐ ์ ๋ฐ ์ ๋ฐ๋์ธ ๊ฒฝ์ฐ ์ ๋ฐ)์ ์ ์ฅํ ์ถฉ๋ถํ GPU ๋ฉ๋ชจ๋ฆฌ๊ฐ ์๋์ง ํ์ธํ์ธ์.
์ด ๋ชจ๋์ ์ฌ์ฉํ๋ ๋ฐ ๋์์ด ๋๋ ๋ช ๊ฐ์ง ์ฐธ๊ณ ์ฌํญ์ด ์๋์ ๋์ ์์ต๋๋ค. ๋๋ [Google colab](#colab-demos)์์ ๋ฐ๋ชจ๋ฅผ ๋ฐ๋ผํ ์๋ ์์ต๋๋ค.
### ์๊ตฌ ์ฌํญ [[requirements-for-int8-mixedprecision-matrix-decomposition]]
- `bitsandbytes<0.37.0`์ ์ฌ์ฉํ๋ ๊ฒฝ์ฐ, 8๋นํธ ํ
์ ์ฝ์ด(Turing, Ampere ๋๋ ์ดํ ์ํคํ
์ฒ - ์: T4, RTX20s RTX30s, A40-A100)๋ฅผ ์ง์ํ๋ NVIDIA GPU์์ ์คํํ๋์ง ํ์ธํ์ธ์. `bitsandbytes>=0.37.0`์ ์ฌ์ฉํ๋ ๊ฒฝ์ฐ, ๋ชจ๋ GPU๊ฐ ์ง์๋ฉ๋๋ค.
- ์ฌ๋ฐ๋ฅธ ๋ฒ์ ์ `bitsandbytes`๋ฅผ ๋ค์ ๋ช
๋ น์ผ๋ก ์ค์นํ์ธ์:
`pip install bitsandbytes>=0.31.5`
- `accelerate`๋ฅผ ์ค์นํ์ธ์
`pip install accelerate>=0.12.0`
### ํผํฉ Int8 ๋ชจ๋ธ ์คํ - ๋จ์ผ GPU ์ค์ [[running-mixedint8-models-single-gpu-setup]]
ํ์ํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ค์นํ ํ ํผํฉ 8๋นํธ ๋ชจ๋ธ์ ๊ฐ์ ธ์ค๋ ๋ฐฉ๋ฒ์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค:
```py
from transformers import AutoModelForCausalLM
model_name = "bigscience/bloom-2b5"
model_8bit = AutoModelForCausalLM.from_pretrained(model_name, device_map="auto", load_in_8bit=True)
```
ํ
์คํธ ์์ฑ์ ๊ฒฝ์ฐ:
* `pipeline()` ํจ์ ๋์ ๋ชจ๋ธ์ `generate()` ๋ฉ์๋๋ฅผ ์ฌ์ฉํ๋ ๊ฒ์ ๊ถ์ฅํฉ๋๋ค. `pipeline()` ํจ์๋ก๋ ์ถ๋ก ์ด ๊ฐ๋ฅํ์ง๋ง, ํผํฉ 8๋นํธ ๋ชจ๋ธ์ ์ต์ ํ๋์ง ์์๊ธฐ ๋๋ฌธ์ `generate()` ๋ฉ์๋๋ฅผ ์ฌ์ฉํ๋ ๊ฒ๋ณด๋ค ๋๋ฆด ์ ์์ต๋๋ค. ๋ํ, nucleus ์ํ๋ง๊ณผ ๊ฐ์ ์ผ๋ถ ์ํ๋ง ์ ๋ต์ ํผํฉ 8๋นํธ ๋ชจ๋ธ์ ๋ํด `pipeline()` ํจ์์์ ์ง์๋์ง ์์ต๋๋ค.
* ์
๋ ฅ์ ๋ชจ๋ธ๊ณผ ๋์ผํ GPU์ ๋ฐฐ์นํ๋ ๊ฒ์ด ์ข์ต๋๋ค.
๋ค์์ ๊ฐ๋จํ ์์
๋๋ค:
```py
from transformers import AutoModelForCausalLM, AutoTokenizer
model_name = "bigscience/bloom-2b5"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model_8bit = AutoModelForCausalLM.from_pretrained(model_name, device_map="auto", load_in_8bit=True)
prompt = "Hello, my llama is cute"
inputs = tokenizer(prompt, return_tensors="pt").to("cuda")
generated_ids = model.generate(**inputs)
outputs = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)
```
### ํผํฉ Int8 ๋ชจ๋ธ ์คํ - ๋ค์ค GPU ์ค์ [[running-mixedint8-models-multi-gpu-setup]]
๋ค์ค GPU์์ ํผํฉ 8๋นํธ ๋ชจ๋ธ์ ๋ก๋ํ๋ ๋ฐฉ๋ฒ์ ๋จ์ผ GPU ์ค์ ๊ณผ ๋์ผํฉ๋๋ค(๋์ผํ ๋ช
๋ น์ด ์ฌ์ฉ):
```py
model_name = "bigscience/bloom-2b5"
model_8bit = AutoModelForCausalLM.from_pretrained(model_name, device_map="auto", load_in_8bit=True)
```
ํ์ง๋ง `accelerate`๋ฅผ ์ฌ์ฉํ์ฌ ๊ฐ GPU์ ํ ๋นํ GPU RAM์ ์ ์ดํ ์ ์์ต๋๋ค. ๋ค์๊ณผ ๊ฐ์ด `max_memory` ์ธ์๋ฅผ ์ฌ์ฉํ์ธ์:
```py
max_memory_mapping = {0: "1GB", 1: "2GB"}
model_name = "bigscience/bloom-3b"
model_8bit = AutoModelForCausalLM.from_pretrained(
model_name, device_map="auto", load_in_8bit=True, max_memory=max_memory_mapping
)
```
์ด ์์์์๋ ์ฒซ ๋ฒ์งธ GPU๊ฐ 1GB์ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ์ฌ์ฉํ๊ณ ๋ ๋ฒ์งธ GPU๊ฐ 2GB๋ฅผ ์ฌ์ฉํฉ๋๋ค.
### Colab ๋ฐ๋ชจ [[colab-demos]]
์ด ๋ฐฉ๋ฒ์ ์ฌ์ฉํ๋ฉด ์ด์ ์ Google Colab์์ ์ถ๋ก ํ ์ ์์๋ ๋ชจ๋ธ์ ๋ํด ์ถ๋ก ํ ์ ์์ต๋๋ค.
Google Colab์์ 8๋นํธ ์์ํ๋ฅผ ์ฌ์ฉํ์ฌ T5-11b(42GB in fp32)๋ฅผ ์คํํ๋ ๋ฐ๋ชจ๋ฅผ ํ์ธํ์ธ์:
[](https://colab.research.google.com/drive/1YORPWx4okIHXnjW7MSAidXN29mPVNT7F?usp=sharing)
๋๋ BLOOM-3B์ ๋ํ ๋ฐ๋ชจ๋ฅผ ํ์ธํ์ธ์:
[](https://colab.research.google.com/drive/1qOjXfQIAULfKvZqwCen8-MoWKGdSatZ4?usp=sharing) |