LoRA
Low-Rank Adaptation
An efficient LLM fine-tuning technique. Trains only a small number of parameters. Saves memory.
Low-Rank Adaptation
An efficient LLM fine-tuning technique. Trains only a small number of parameters. Saves memory.
LoRA (Low-Rank Adaptation) is a technique for efficiently fine-tuning large language models. Instead of updating all model weights, it trains only small low-rank matrices, dramatically reducing memory and computational costs.
LoRA, published by a Microsoft research team in 2021, reduced the number of trainable parameters required to fine-tune the GPT-3 175B model by 10,000 times and cut GPU memory to one third. This made it possible to fine-tune LLMs even on consumer GPUs (RTX 3090, RTX 4090, etc.).
The core idea of LoRA is the hypothesis that weight updates have a low intrinsic rank. For the original weight W, the update is decomposed in the form W' = W + BA, where A and B are low-rank matrices. Even with the rank set to around r=4~8, it achieves performance equivalent to full fine-tuning.
In practice, LoRA is used for developing domain-specific LLMs, style customization, multi-task adapters, and more. It can be easily applied with the HuggingFace PEFT library, and with QLoRA (quantization + LoRA), a 7B model can be fine-tuned even on a 14GB GPU.
from transformers import AutoModelForCausalLM, AutoTokenizer, TrainingArguments
from peft import LoraConfig, get_peft_model, TaskType
from datasets import load_dataset
from trl import SFTTrainer
# 베이스 모델 로드
model_name = "meta-llama/Llama-3.2-3B"
model = AutoModelForCausalLM.from_pretrained(
model_name,
torch_dtype="auto",
device_map="auto"
)
tokenizer = AutoTokenizer.from_pretrained(model_name)
tokenizer.pad_token = tokenizer.eos_token
# LoRA 설정 (핵심 하이퍼파라미터)
lora_config = LoraConfig(
task_type=TaskType.CAUSAL_LM,
r=8, # 랭크 (4~64, 높을수록 표현력 증가)
lora_alpha=16, # 스케일링 팩터 (보통 r*2)
lora_dropout=0.05, # 드롭아웃
target_modules=["q_proj", "k_proj", "v_proj", "o_proj"], # 적용 레이어
bias="none"
)
# LoRA 어댑터 적용
model = get_peft_model(model, lora_config)
model.print_trainable_parameters()
# 출력: trainable params: 3,407,872 || all params: 3,213,539,072 || 0.11%
# 학습 데이터 준비
dataset = load_dataset("json", data_files="train_data.json")
# 학습 설정
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,
warmup_ratio=0.03,
logging_steps=10,
save_strategy="epoch"
)
# SFT Trainer로 학습
trainer = SFTTrainer(
model=model,
args=training_args,
train_dataset=dataset["train"],
tokenizer=tokenizer,
max_seq_length=2048
)
trainer.train()
# 어댑터만 저장 (수 MB)
model.save_pretrained("./my_lora_adapter")
This is a comparison of LoRA vs Full Fine-tuning as of January 2025.
| Item | Full Fine-tuning | LoRA | QLoRA (4-bit) |
|---|---|---|---|
| Price | Open source (free) | Open source (free) | Open source (free) |
| Trainable parameters | 100% | 0.1% ~ 1% | 0.1% ~ 1% |
| 7B model VRAM | 60GB+ | 16GB | 6GB |
| 70B model VRAM | 1,120GB+ | 140GB | 48GB |
| Memory savings rate | - | 3x (66%) | 17x (94%) |
| Performance retention rate | 100% | 90~95% | 90~95% |
Cost savings: With QLoRA, even a 70B model can be fine-tuned on an RTX 4090 ($1,500). Training costs are reduced by 50~70%.
"We fine-tuned a Llama 3 model with LoRA on our customer service data. We trained only 0.1% of all parameters, finished in 4 hours on a single A100, and the quality of customer inquiry responses improved by 35%."
"LoRA decomposes the weight update into low-rank matrices. In the form W' = W + BA, A has size (d, r) and B has size (r, k). If you set the rank r to 4, you train only 0.01% of the parameters compared to the original while still getting similar performance."
"With QLoRA, you can apply LoRA to a 4-bit quantized model and save even more GPU memory. You can fine-tune even a 70B model on an RTX 4090 24GB. However, there's a trade-off: inference speed becomes slightly slower."
Take a mock interview on this topic → WIA SOOM AI Interview · Start for free
Applying LoRA to only some layers degrades performance. You must apply it to all Attention layers to get optimal performance.
Apply LoRA to all of q_proj, k_proj, v_proj, and o_proj. lora_alpha is usually set to r*2. The learning rate should be set 10 times higher than in regular fine-tuning (1e-4~3e-4).