[PEFT]: Parameter Efficient Fine-Tuning
PLMs๋ฅผ specific task์ ์ ์ฉํ ๋, ๋๋ถ๋ถ์ Parameter๋ฅผ freezeโ๏ธ, ์์์ parameter๋ง FTํ๋ ๊ธฐ๋ฒ.
PEFT๋?
PEFT๋ ๋ชจ๋ธ ์ฑ๋ฅ์ ์ ์ง + #parameter↓๊ฐ ๊ฐ๋ฅํจ.
๋ํ, catastrophic forgetting๋ฌธ์ ์ํ๋ ๋ํ ๋ฎ์.
๐คHuggingface์์ ์๊ฐํ ํ์ ์ ๋ฐฉ๋ฒ์ผ๋ก downstream task์์ FT๋ฅผ ์ํด ์ฌ์ฉ๋จ.
Catastrophic Forgetting์ด๋?
์๋ก์ด ์ ๋ณด๋ฅผ ํ์ตํ๊ฒ ๋ ๋, ๊ธฐ์กด์ ํ์ตํ ์ผ๋ถ์ ์ง์์ ๋ํด์๋ ๋ง๊ฐ์ ํ๊ฒ ๋๋ ํ์
Main Concept
- Reduced Parameter Fine-tuning(์ถ์๋ ํ๋ผ๋ฏธํฐ ํ์ธํ๋)
์ฌ์ ํ์ต๋ LLM ๋ชจ๋ธ์์ ๋๋ค์์ ํ๋ผ๋ฏธํฐ๋ฅผ ๊ณ ์ ํด ์์์ ์ถ๊ฐ์ ์ธ ํ๋ผ๋ฏธํฐ๋ง ํ์ธํ๋ํ๋ ๊ฒ์ด ์ค์
์ ํ์ ํ์ธํ๋์ผ๋ก ๊ณ์ฐ์ ์๊ตฌ๊ฐ ๊ธ๊ฒฉํ๊ฒ ๊ฐ์ํ๋ ํจ๊ณผ- Overcoming Catastrophic Forgetting(์น๋ช ์ ๋ง๊ฐ ๋ฌธ์ ๊ทน๋ณต)
Catastrophic Forgetting ๋ฌธ์ ๋ LLM ๋ชจ๋ธ ์ ์ฒด๋ฅผ ํ์ธ ํ๋ํ๋ ๊ณผ์ ์์ ๋ฐ์ํ๋ ํ์์ธ๋ฐ, PEFT๋ฅผ ํ์ฉํ์ฌ ์น๋ช ์ ๋ง๊ฐ ๋ฌธ์ ๋ฅผ ์ํํ ์ ์์
PEFT๋ฅผ ํ์ฉํ๋ฉด ์ฌ์ ํ๋ จ๋ ์ํ์ ์ง์์ ๋ณด์กดํ๋ฉฐ ์๋ก์ด downstream task์ ๋ํด ํ์ตํ ์ ์์- Application Across Modalities(์ฌ๋ฌ ๋ชจ๋ฌ๋ฆฌํฐ ์ ์ฉ ๊ฐ๋ฅ)
PEFT๋ ๊ธฐ์กด ์์ฐ์ด์ฒ๋ฆฌ(Natural Language Process: NLP) ์์ญ์ ๋์ด์ ๋ค์ํ ์์ญ์ผ๋ก ํ์ฅ ๊ฐ๋ฅํจ
์คํ ์ด๋ธ ๋ํจ์ (stable diffusion) ํน์ Layout LM ๋ฑ์ ํฌํจ๋ ์ปดํจํฐ ๋น์ (Computer Vision: CV) ์์ญ,
Whisper๋ XLS-R์ด ํฌํจ๋ ์ค๋์ค ๋ฑ์ ๋ค์ํ ๋ง๋ฌ๋ฆฌํฐ์ ์ฑ๊ณต์ ์ผ๋ก ์ ์ฉ๋จ- Supported PEFT Methods(์ฌ์ฉ ๊ฐ๋ฅํ PEFT)
๋ผ์ด๋ธ๋ฌ๋ฆฌ์์ ๋ค์ํ PEFT ๋ฐฉ๋ฒ์ ์ง์ํจ
LoRA(Low-Rank Adaption), Prefix Tuning, ํ๋กฌํํธ ํ๋ ๋ฑ ๊ฐ๊ฐ์ ๋ฐฉ๋ฒ์ ํน์ ํ ๋ฏธ์ธ ์กฐ์ ์๊ตฌ ์ฌํญ๊ณผ ์๋๋ฆฌ์ค์ ๋ง๊ฒ ์ฌ์ฉํ ์ ์๋๋ก ์ค๊ณ๋จ
์ฌ์ ํ์ต๊ฐ์ค์น(โ๏ธ)์ output activation์ weight matrix์ธ A, B๋ก ๊ตฌ์ฑ๋ LoRA์ ์ํด ์ฆ๊ฐ๋๋ค.
[Q-LoRA]: Quantized-LoRA
Q-LoRA๋?
2023๋ 5์ NeurIPS์์ ์์ํ์ LoRA๋ฅผ ํฉ์ณ "A6000 ๋จ์ผ GPU๋ก 65B๋ชจ๋ธ ํ๋์ด ๊ฐ๋ฅ"ํ ๋ฐฉ๋ฒ๋ก ์ ๋ฐํํจ.
QLoRA๋ ๊ฒฐ๊ตญ ๊ธฐ์กด์ LoRA์ ์๋ก์ด quantization์ ๋ํ ํํ์ด๋ค.
๋ฒ ์ด์ค ๋ชจ๋ธ์ธ PLM์ ๊ฐ์ค์น๋ฅผ ์ผ๋ฆฌ๊ณ (frozen), LoRA ์ด๋ํฐ์ ๊ฐ์ค์น๋ง ํ์ต ๊ฐ๋ฅํ๊ฒ(trainable)ํ๋ ๊ฒ์ LoRA์ ๋์ผํ๋ฉฐ, frozen PLM์ ๊ฐ์ค์น๊ฐ '4๋นํธ๋ก ์์ํ'๋์๋ค๋ ์ ๋๊ฐ ๋ค๋ฅธ ์ ์ด๋ค.
๋๋ฌธ์, QLoRA์์ ์ฃผ์ํ ์๋ก ์๊ฐ๋๋ ๊ธฐ์ (Main Contribution)์ ์์ํ ๋ฐฉ๋ฒ๋ก ์ด ์ฃผ๊ฐ ๋๋ค๋ ์ฌ์ค์ด๋ค.
์์ํ๋?
weight์ activation output์ ๋ ์์ bit๋จ์๋ก ํํํ๋๋ก ๋ณํํ๋ ๊ฒ.
์ฆ, data์ ๋ณด๋ฅผ ์ฝ๊ฐ์ค์ด๊ณ , ์ ๋ฐ๋๋ ๋ฎ์ถ์ง๋ง
"์ ์ฅ ๋ฐ ์ฐ์ฐ์ ํ์ํ ์ฐ์ฐ์ ๊ฐ์์์ผ ํจ์จ์ฑ์ ํ๋ณด"ํ๋ ๊ฒฝ๋ํ ๋ฐฉ๋ฒ๋ก ์ด๋ค.
How to Use in MLLMs...?
๊ทธ๋ ๋ค๋ฉด ์ด๋ป๊ฒ MLLMs์ ์ ์ฉํ ์ ์์๊น? MLLMs๋ ๋งค์ฐ ์ข ๋ฅ๊ฐ ๋ง์ง๋ง, ๊ฐ์ฅ ์ฌ์ด ์์ ๋ก VLMs๋ฅผ ๋ค์ด๋ณด์๋ฉด,
Q-LoRA ๋ฐ LoRA๋ PEFT๋ฐฉ๋ฒ๋ก ์ด๊ธฐ์ ์ด๋ LLMs, MLLMs๋ชจ๋ ํต์ฉ๋๋ ๋ฐฉ๋ฒ์ด๋ค.
๊ทธ๋ ๊ธฐ์ VLMs(Vision Encoder + LLM Decoder)๋ฅผ ๊ธฐ์ค์ผ๋ก ์ค๋ช ํด๋ณด์๋ฉด:
- ์ธ์ด์ ๋ฅ๋ ฅ์ ๊ฐํ์ํค๊ณ ์ถ๋ค๋ฉด, LLM๋ง PEFT๋ฅผ ์งํ.
- ์๊ฐ์ ๋ฅ๋ ฅ์ ๊ฐํ์ํค๊ณ ์ถ๋ค๋ฉด, Vision Encoder๋ง PEFT๋ฅผ ์งํ.
- ๋ ๋ฅ๋ ฅ ๋ชจ๋ ๊ฐํ์ํค๊ณ ์ถ๋ค๋ฉด, Encoder, Decoder ๋ชจ๋ PEFT๋ฅผ ์งํํ๋ฉด ๋๋ค.
Reference Code:
A Definitive Guide to QLoRA: Fine-tuning Falcon-7b with PEFT
Unveiling the Power of QLoRA: Comprehensive Explanation and Practical Coding with ๐ค PEFT
medium.com
Finetuning Llama2 with QLoRA — TorchTune documentation
Finetuning Llama2 with QLoRA In this tutorial, we’ll learn about QLoRA, an enhancement on top of LoRA that maintains frozen model parameters in 4-bit quantized precision, thereby reducing memory usage. We’ll walk through how QLoRA can be utilized withi
pytorch.org
์ฐธ๊ณ : https://github.com/V2LLAIN/Transformers-Tutorials/blob/master/qlora_baseline.ipynb
Transformers-Tutorials/qlora_baseline.ipynb at master · V2LLAIN/Transformers-Tutorials
This repository contains demos I made with the Transformers library by HuggingFace. - V2LLAIN/Transformers-Tutorials
github.com
Deepspeed๋?
# finetune_qlora.sh deepspeed ovis/train/train.py \ --deepspeed scripts/zero2.json \ ...โ
๋ฌผ๋ก ๋๋ง์ ๋ฐฉ๋ฒ์ ๊ณ ์ํ๋๊ฒ๋ ์ข์ง๋ง, ๋๋ถ๋ถ์ user๋ค์ด ์ด ๋ฐฉ๋ฒ์ ์ฌ์ฉํ๋๊ฑธ ๋ด์๋ ์ผ๋จ ์์๋๋๊ฒ ์ข์ ๊ฒ ๊ฐ๊ธฐ์ ์์๋ณด๊ณ ์ํ๋ค.
deepspeed...?
๋ชจ๋ธ์ training, inference์๋๋ฅผ ๋น ๋ฅด๊ณ ํจ์จ์ ์ผ๋ก ์ฒ๋ฆฌํ๊ฒ ๋์์ฃผ๋ Microsoft์ฌ์ ๋ฅ๋ฌ๋ ์ต์ ํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ด๋ค.
ํ์ต device ์ข ๋ฅ:
- CPU
Single GPU
1 Node, Multi GPU
Multi Node, Multi GPU --> ๋งค์ฐ ํฐ GPT4 ๋ฑ์ ํ์ต์ ์ํด ์ฌ์ฉ๋จ.๋ถ์ฐํ์ต ๋ฐฉ์:
- Data Parallel: ํ๋์ device๊ฐ data๋ฅผ ๋๋๊ณ , ๊ฐ device์์ ์ฒ๋ฆฌ๊ฒฐ๊ณผ๋ฅผ ๋ชจ์ ๊ณ์ฐ
--> ํ๋์ device๊ฐ ๋ค๋ฅธ device์ ๋นํด ๋ฉ๋ชจ๋ฆฌ ์ฌ์ฉ๋์ด ๋ง์์ง๋, ๋ฉ๋ชจ๋ฆฌ ๋ถ๊ท ํ ๋ฌธ์ ๊ฐ ๋ฐ์ํ๋ค!
- Distributed Data Parallel: ๊ฐ๊ฐ์ device๋ฅผ ํ๋์ Process๋ก ๋ณด๊ณ , ๊ฐ process์์ ๋ชจ๋ธ์ ๋์์ ์ฌ์ฉ.
์ด๋, ์ญ์ ํ์์๋ง ๋ด๋ถ์ ์ผ๋ก gradient๋ฅผ ๋๊ธฐํ --> ๋ฉ๋ชจ๋ฆฌ ๋ถ๊ท ํ๋ฌธ์ โ
cf) Requirements:- PyTorch must be installed before installing DeepSpeed. - For full feature support we recommend a version of PyTorch that is >= 1.9 and ideally the latest PyTorch stable release. - A CUDA or ROCm compiler such as nvcc or hipcc used to compile C++/CUDA/HIP extensions. - Specific GPUs we develop and test against are listed below, this doesn't mean your GPU will not work if it doesn't fall into this category it's just DeepSpeed is most well tested on the following: NVIDIA: Pascal, Volta, Ampere, and Hopper architectures AMD: MI100 and MI200
pip install deepspeed
๋ก ์ค์น๊ฐ ๊ฐ๋ฅํ๋ฉฐ, ์ฌ์ฉ๋ฐฉ๋ฒ์ ์๋์ ๊ฐ๋ค.
์ฌ์ฉ๋ฐฉ๋ฒ:
Step1) deepspeed์ฌ์ฉ์ ์ํ Config.jsonํ์ผ ์์ฑ{ "train_micro_batch_size_per_gpu": 160, "gradient_accumulation_steps": 1, "optimizer": { "type": "Adam", "params": { "lr": 0.001 } }, "zero_optimization": { "stage": 1, "offload_optimizer": { "device": "cpu", "pin_memory":true }, "overlap_comm": false, "contiguous_gradients": false } }
config Args:https://www.deepspeed.ai/docs/config-json/
Step2) import & read json
import deepspeed from deepspeed.ops.adam import DeepSpeedCPUAdam with open('config.json', 'r') as f: deepspeed_config = json.load(f)
Step3) optimizer ์ค์ & model,optimizer ์ด๊ธฐํ
optimizer = DeepSpeedCPUAdam(model.parameters(), lr=lr) model, optimizer, _, _ = deepspeed.initialize(model=model, model_parameters=model.parameters(), optimizer=optimizer, config_params=deepspeed_config)
cf) ArgumentParser์ ์ถ๊ฐํ๋๊ฒ๋ ๊ฐ๋ฅํจ!parser = argparse.ArgumentParser() parser.add_argument('--local_rank', type=int, default=-1) parser = deepspeed.add_config_arguments(parser)
Step4) Train!
# >> train.py deepspeed --num_gpus={gpu ๊ฐ์} train_deepspeed.py
# train.sh deepspeed --num_gpus={gpu ๊ฐ์} train_deepspeed.py # >> bash train.sh
์ฃผ์ !)DeepSpeed๋ CUDA_VISIBLE_DEVICES๋ก ํน์ GPU๋ฅผ ์ ์ดํ ์ ์๋ค!
์๋์ ๊ฐ์ด --include๋ก๋ง ํน์ GPU๋ฅผ ์ฌ์ฉํ ์ ์๋ค.deepspeed —inlcude localhost:<GPU_NUM1>, <GPU_NUM2> <python_file.py>
- gpu node์ ๊ฐ์๊ฐ ๋ง์์ง์๋ก deepspeed์ ์ฅ์ ์ธ ํ์ต ์๋๊ฐ ๋นจ๋ผ์ง๋ค!
DeepSpeed Configuration JSON
DeepSpeed is a deep learning optimization library that makes distributed training easy, efficient, and effective.
www.deepspeed.ai
'HuggingFace๐ค' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
HuggingFace(๐ค)์ ๋ฌธ์๋ฅผ ์ํ transformers ์ ๋ฆฌ (3) | 2024.11.18 |
---|---|
HuggingFace(๐ค)-Tutorials (1) | 2024.07.31 |
[Data Preprocessing] - Data Collator (1) | 2024.07.14 |
QLoRA ์ค์ต & Trainer vs SFTTrainer (0) | 2024.07.12 |