PEFT
Parameter-Efficient Fine-Tuning
A collective term for techniques that adapt a model efficiently by updating only some of its parameters.
Parameter-Efficient Fine-Tuning
A collective term for techniques that adapt a model efficiently by updating only some of its parameters.
PEFT (Parameter-Efficient Fine-Tuning) is a collective term for techniques that efficiently adapt a model to a specific task by updating only some of the parameters of a large language model (LLM) instead of all of them. It makes it possible to fine-tune models with billions to hundreds of billions of parameters even with a small number of GPUs.
PEFT is a key technology that emerged in the LLM era. Training all the parameters of large models such as GPT-3 (175B) and LLaMA (65B) requires hundreds of GB of GPU memory, but with PEFT, fine-tuning has become possible even on consumer-grade GPUs.
Representative PEFT techniques include LoRA (Low-Rank Adaptation), QLoRA, Prefix Tuning, Prompt Tuning, and Adapter. LoRA adds low-rank decomposition matrices to the weight matrices and achieves performance close to Full Fine-tuning with only 0.1โ1% of the parameters.
In practice, it dramatically reduces cost and time. For a 7B model, LoRA can train with 8GB of VRAM, and by storing task-specific adapters separately, a single base model can be used for multiple purposes.
# PEFT (LoRA) ํ์ธํ๋ ์์
from transformers import AutoModelForCausalLM, AutoTokenizer, TrainingArguments
from peft import LoraConfig, get_peft_model, TaskType
from datasets import load_dataset
from trl import SFTTrainer
# 1. ๊ธฐ๋ณธ ๋ชจ๋ธ ๋ก๋
model_name = "meta-llama/Llama-2-7b-hf"
model = AutoModelForCausalLM.from_pretrained(
model_name,
torch_dtype="auto",
device_map="auto" # ์๋์ผ๋ก GPU์ ๋ถ์ฐ
)
tokenizer = AutoTokenizer.from_pretrained(model_name)
# 2. LoRA ์ค์ (ํต์ฌ!)
lora_config = LoraConfig(
task_type=TaskType.CAUSAL_LM,
r=16, # LoRA ๋ญํฌ (๋ฎ์์๋ก ํ๋ผ๋ฏธํฐ ์ ์)
lora_alpha=32, # ์ค์ผ์ผ๋ง ํฉํฐ
lora_dropout=0.1, # Dropout
target_modules=["q_proj", "v_proj"], # ์ ์ฉํ ๋ ์ด์ด
)
# 3. PEFT ๋ชจ๋ธ๋ก ๋ณํ
peft_model = get_peft_model(model, lora_config)
# ํ์ต ๊ฐ๋ฅํ ํ๋ผ๋ฏธํฐ ํ์ธ
peft_model.print_trainable_parameters()
# ์ถ๋ ฅ: trainable params: 4M || all params: 7B || trainable%: 0.05%
# 4. ํ์ต ์ค์
training_args = TrainingArguments(
output_dir="./lora-output",
num_train_epochs=3,
per_device_train_batch_size=4,
gradient_accumulation_steps=4,
learning_rate=2e-4,
fp16=True, # ๋ฉ๋ชจ๋ฆฌ ์ ์ฝ
)
# 5. ํ์ต ๋ฐ ์ ์ฅ
trainer = SFTTrainer(
model=peft_model,
args=training_args,
train_dataset=load_dataset("your_dataset")["train"],
)
trainer.train()
# 6. ์ด๋ํฐ๋ง ์ ์ฅ (์ MB ํฌ๊ธฐ)
peft_model.save_pretrained("./my-lora-adapter")
"Fully fine-tuning a 7B model requires several A100 80GB GPUs, but with LoRA, a single RTX 4090 is enough. You can cut costs by more than 90%."
"LoRA decomposes the weight matrix W into W + BA. B and A are low-rank matrices, so they have few parameters, and if you merge them at inference time, you can use them without additional latency."
"QLoRA is a technique that applies LoRA to a 4-bit quantized model. There are research results showing that it saves even more memory while delivering performance equivalent to 16-bit fine-tuning."
Take a mock interview on this topic โ WIA SOOM AI Interview ยท Start for free
A rank that is too low, such as r=4, lacks expressive power. In general, r=16โ64 is recommended, and for complex tasks, use a higher value.
Applying it to all layers increases the number of parameters and reduces the advantages of PEFT. Applying it to only q_proj, v_proj, or the attention layers is sufficient.
Adjust the rank according to task complexity, and optimize target_modules through experimentation. Store adapters separately from the base model and reuse them across multiple tasks.