๐Ÿค– AI/ML

PEFT

Parameter-Efficient Fine-Tuning

A collective term for techniques that adapt a model efficiently by updating only some of its parameters.

๐Ÿ“– Detailed Explanation

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.

๐Ÿ’ป Code Example

# 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")

๐Ÿ—ฃ๏ธ Say It Like This in Practice

๐Ÿ’ฌ In a Meeting
"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%."
๐Ÿ’ฌ In an Interview
"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."
๐Ÿ’ฌ In a Technical Discussion
"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

โš ๏ธ Common Mistakes & Cautions

โŒ
Using too low a LoRA rank

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 LoRA to all layers

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.

โœ…
The right way

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.

๐Ÿ”— Related Terms

๐Ÿ“š Learn More