Implementing LoRA with PEFT Library
You'll learn how to apply LoRA adapters to any transformer model using Hugging Face's PEFT library with just a few lines of code.
The Core Idea
Now that you understand LoRA's theory—injecting low-rank matrices into specific layers—it's time to actually implement it. The Hugging Face PEFT (Parameter-Efficient Fine-Tuning) library handles all the mathematical complexity for you. You simply configure which layers to adapt, set your rank and alpha, and PEFT automatically wraps your model with trainable LoRA adapters while freezing the original weights.
Think of PEFT as a smart modification kit: you hand it your base model and a configuration blueprint, and it surgically installs the adapters exactly where you specified.
Using PEFT to Add LoRA
The workflow involves three key objects:
- LoraConfig: Your blueprint specifying rank (
r), scaling (lora_alpha), target modules, and other hyperparameters - get_peft_model(): The function that wraps your base model with LoRA adapters
- PeftModel: The resulting enhanced model with frozen base weights and trainable adapters
from peft import LoraConfig, get_peft_model
from transformers import AutoModelForCausalLM
# Load your base model
model = AutoModelForCausalLM.from_pretrained("meta-llama/Llama-2-7b-hf")
# Configure LoRA
lora_config = LoraConfig(
r=8, # Low rank
lora_alpha=16, # Scaling factor
target_modules=["q_proj", "v_proj"], # Which layers
lora_dropout=0.05,
task_type="CAUSAL_LM"
)
# Apply LoRA adapters
model = get_peft_model(model, lora_config)
model.print_trainable_parameters() # Shows dramatic reduction!
The result? Only 0.1–1% of parameters are trainable—the rest stay frozen.
Key Takeaway: PEFT's get_peft_model() transforms any transformer into a LoRA-enhanced version in seconds, automatically managing adapter injection and parameter freezing based on your LoraConfig blueprint.