Spaces:
Runtime error
๋จ์ผ GPU์์ ํจ์จ์ ์ธ ์ถ๋ก [[efficient-inference-on-a-single-gpu]]
์ด ๊ฐ์ด๋ ์ธ์๋, ๋จ์ผ GPU์์์ ํ๋ จ ๊ฐ์ด๋์ CPU์์์ ์ถ๋ก ๊ฐ์ด๋์์๋ ๊ด๋ จ ์ ๋ณด๋ฅผ ์ฐพ์ ์ ์์ต๋๋ค.
Better Transformer: PyTorch ๋ค์ดํฐ๋ธ Transformer ํจ์คํธํจ์ค [[better-transformer-pytorchnative-transformer-fastpath]]
PyTorch ๋ค์ดํฐ๋ธ nn.MultiHeadAttention
์ดํ
์
ํจ์คํธํจ์ค์ธ BetterTransformer๋ ๐ค Optimum ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ ํตํฉ์ ํตํด Transformers์ ํจ๊ป ์ฌ์ฉํ ์ ์์ต๋๋ค.
PyTorch์ ์ดํ ์ ํจ์คํธํจ์ค๋ ์ปค๋ ํจ์ ๊ณผ ์ค์ฒฉ๋ ํ ์์ ์ฌ์ฉ์ ํตํด ์ถ๋ก ์๋๋ฅผ ๋์ผ ์ ์์ต๋๋ค. ์์ธํ ๋ฒค์น๋งํฌ๋ ์ด ๋ธ๋ก๊ทธ ๊ธ์์ ํ์ธํ ์ ์์ต๋๋ค.
optimum
ํจํค์ง๋ฅผ ์ค์นํ ํ์๋ ์ถ๋ก ์ค Better Transformer๋ฅผ ์ฌ์ฉํ ์ ์๋๋ก [~PreTrainedModel.to_bettertransformer
]๋ฅผ ํธ์ถํ์ฌ ๊ด๋ จ ๋ด๋ถ ๋ชจ๋์ ๋์ฒดํฉ๋๋ค:
model = model.to_bettertransformer()
[~PreTrainedModel.reverse_bettertransformer
] ๋ฉ์๋๋ ์ ๊ทํ๋ transformers ๋ชจ๋ธ๋ง์ ์ฌ์ฉํ๊ธฐ ์ํด ๋ชจ๋ธ์ ์ ์ฅํ๊ธฐ ์ ์๋์ ๋ชจ๋ธ๋ง์ผ๋ก ๋์๊ฐ ์ ์๋๋ก ํด์ค๋๋ค:
model = model.reverse_bettertransformer()
model.save_pretrained("saved_model")
PyTorch 2.0๋ถํฐ๋ ์ดํ ์ ํจ์คํธํจ์ค๊ฐ ์ธ์ฝ๋์ ๋์ฝ๋ ๋ชจ๋์์ ์ง์๋ฉ๋๋ค. ์ง์๋๋ ์ํคํ ์ฒ ๋ชฉ๋ก์ ์ฌ๊ธฐ์์ ํ์ธํ ์ ์์ต๋๋ค.
FP4 ํผํฉ ์ ๋ฐ๋ ์ถ๋ก ์ ์ํ bitsandbytes
ํตํฉ [[bitsandbytes-integration-for-fp4-mixedprecision-inference]]
bitsandbytes
๋ฅผ ์ค์นํ๋ฉด GPU์์ ์์ฝ๊ฒ ๋ชจ๋ธ์ ์์ถํ ์ ์์ต๋๋ค. FP4 ์์ํ๋ฅผ ์ฌ์ฉํ๋ฉด ์๋์ ์ ์ฒด ์ ๋ฐ๋ ๋ฒ์ ๊ณผ ๋น๊ตํ์ฌ ๋ชจ๋ธ ํฌ๊ธฐ๋ฅผ ์ต๋ 8๋ฐฐ ์ค์ผ ์ ์์ต๋๋ค. ์๋์์ ์์ํ๋ ๋ฐฉ๋ฒ์ ํ์ธํ์ธ์.
์ด ๊ธฐ๋ฅ์ ๋ค์ค GPU ์ค์ ์์๋ ์ฌ์ฉํ ์ ์์ต๋๋ค.
์๊ตฌ ์ฌํญ [[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 ๋ชจ๋ธ์ ์คํํ ์ ์์ต๋๋ค.
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 ์ค์ ๊ณผ ๋์ผํฉ๋๋ค(๋์ผํ ๋ช ๋ น์ด ์ฌ์ฉ):
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
์ธ์๋ฅผ ์ฌ์ฉํ์ธ์:
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]]
์ด ๋ฐฉ๋ฒ์ ๋ ๊ณ ๊ธ ์ฌ์ฉ๋ฒ์ ๋ํด์๋ ์์ํ ๋ฌธ์ ํ์ด์ง๋ฅผ ์ฐธ์กฐํ์ธ์.
Int8 ํผํฉ ์ ๋ฐ๋ ํ๋ ฌ ๋ถํด๋ฅผ ์ํ bitsandbytes
ํตํฉ [[bitsandbytes-integration-for-int8-mixedprecision-matrix-decomposition]]
์ด ๊ธฐ๋ฅ์ ๋ค์ค GPU ์ค์ ์์๋ ์ฌ์ฉํ ์ ์์ต๋๋ค.
LLM.int8() : 8-bit Matrix Multiplication for Transformers at Scale
๋
ผ๋ฌธ์์ ์ฐ๋ฆฌ๋ ๋ช ์ค์ ์ฝ๋๋ก Hub์ ๋ชจ๋ ๋ชจ๋ธ์ ๋ํ Hugging Face ํตํฉ์ ์ง์ํฉ๋๋ค.
์ด ๋ฐฉ๋ฒ์ float16
๋ฐ bfloat16
๊ฐ์ค์น์ ๋ํด nn.Linear
ํฌ๊ธฐ๋ฅผ 2๋ฐฐ๋ก ์ค์ด๊ณ , float32
๊ฐ์ค์น์ ๋ํด 4๋ฐฐ๋ก ์ค์
๋๋ค. ์ด๋ ์ ๋ฐ ์ ๋ฐ๋์์ ์ด์์น๋ฅผ ์ฒ๋ฆฌํจ์ผ๋ก์จ ํ์ง์ ๊ฑฐ์ ์ํฅ์ ๋ฏธ์น์ง ์์ต๋๋ค.
Int8 ํผํฉ ์ ๋ฐ๋ ํ๋ ฌ ๋ถํด๋ ํ๋ ฌ ๊ณฑ์ ์ ๋ ๊ฐ์ ์คํธ๋ฆผ์ผ๋ก ๋ถ๋ฆฌํฉ๋๋ค: (1) fp16๋ก ๊ณฑํด์ง๋ ์ฒด๊ณ์ ์ธ ํน์ด๊ฐ ์ด์์น ์คํธ๋ฆผ ํ๋ ฌ(0.01%) ๋ฐ (2) int8 ํ๋ ฌ ๊ณฑ์ ์ ์ผ๋ฐ์ ์ธ ์คํธ๋ฆผ(99.9%). ์ด ๋ฐฉ๋ฒ์ ์ฌ์ฉํ๋ฉด ๋งค์ฐ ํฐ ๋ชจ๋ธ์ ๋ํด ์์ธก ์ ํ ์์ด int8 ์ถ๋ก ์ด ๊ฐ๋ฅํฉ๋๋ค. ์ด ๋ฐฉ๋ฒ์ ๋ํ ์์ธํ ๋ด์ฉ์ ๋ ผ๋ฌธ์ด๋ ํตํฉ์ ๊ดํ ๋ธ๋ก๊ทธ ๊ธ์์ ํ์ธํ ์ ์์ต๋๋ค.
์ปค๋์ GPU ์ ์ฉ์ผ๋ก ์ปดํ์ผ๋์ด ์๊ธฐ ๋๋ฌธ์ ํผํฉ 8๋นํธ ๋ชจ๋ธ์ ์คํํ๋ ค๋ฉด GPU๊ฐ ํ์ํฉ๋๋ค. ์ด ๊ธฐ๋ฅ์ ์ฌ์ฉํ๊ธฐ ์ ์ ๋ชจ๋ธ์ 1/4(๋๋ ๋ชจ๋ธ ๊ฐ์ค์น๊ฐ ์ ๋ฐ ์ ๋ฐ๋์ธ ๊ฒฝ์ฐ ์ ๋ฐ)์ ์ ์ฅํ ์ถฉ๋ถํ GPU ๋ฉ๋ชจ๋ฆฌ๊ฐ ์๋์ง ํ์ธํ์ธ์. ์ด ๋ชจ๋์ ์ฌ์ฉํ๋ ๋ฐ ๋์์ด ๋๋ ๋ช ๊ฐ์ง ์ฐธ๊ณ ์ฌํญ์ด ์๋์ ๋์ ์์ต๋๋ค. ๋๋ Google colab์์ ๋ฐ๋ชจ๋ฅผ ๋ฐ๋ผํ ์๋ ์์ต๋๋ค.
์๊ตฌ ์ฌํญ [[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๋นํธ ๋ชจ๋ธ์ ๊ฐ์ ธ์ค๋ ๋ฐฉ๋ฒ์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค:
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์ ๋ฐฐ์นํ๋ ๊ฒ์ด ์ข์ต๋๋ค.
๋ค์์ ๊ฐ๋จํ ์์ ๋๋ค:
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 ์ค์ ๊ณผ ๋์ผํฉ๋๋ค(๋์ผํ ๋ช ๋ น์ด ์ฌ์ฉ):
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
์ธ์๋ฅผ ์ฌ์ฉํ์ธ์:
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)๋ฅผ ์คํํ๋ ๋ฐ๋ชจ๋ฅผ ํ์ธํ์ธ์:
๋๋ BLOOM-3B์ ๋ํ ๋ฐ๋ชจ๋ฅผ ํ์ธํ์ธ์: