What is LoRA (Low-Rank Adaptation) and QLoRA for efficient LLM fine-tuning?
❓ Interview Prompt: Explain Parameter-Efficient Fine-Tuning (PEFT), rank-decomposition matrices (A and B), and 4-bit QLoRA.
Full fine-tuning of a 70B parameter LLM requires updating all 70 billion weight matrices, demanding multiple 80GB A100 GPUs. LoRA (Low-Rank Adaptation) solves this memory crisis.
Mathematical Principle:
LoRA freezes the base model weight matrix ($W_0$) and injects two small trainable rank-decomposition matrices ($A$ and $B$) alongside attention layers:
$$\Delta W = B \times A$$
QLoRA:
Quantizes the frozen base model to 4-bit NormalFloat (NF4) while training LoRA adapters in 16-bit, allowing 70B model fine-tuning on a single consumer GPU!
Code Implementation Example
# PEFT LoRA configuration in HuggingFace PEFT (Python)
from peft import LoraConfig, get_peft_model
peft_config = LoraConfig(
r=8, # Low rank matrix dimension
lora_alpha=16, # Scaling factor
target_modules=["q_proj", "v_proj"], # Inject into attention layers
lora_dropout=0.05,
bias="none"
)
model = get_peft_model(base_model, peft_config)
# Only 0.1% parameters are trainable!LoRA freezes base model weights and trains small low-rank decomposition matrices, reducing fine-tuning VRAM requirements by 90%.