Installation
Quick start
All TRL trainers support PEFT through thepeft_config argument. The simplest way to enable PEFT is via the CLI with the --use_peft flag:
Three ways to configure PEFT
1. CLI flags (simplest)
1. CLI flags (simplest)
Use the Available CLI flags:
--use_peft flag with TRL scripts. Best for quick experiments and standard LoRA configurations.--use_peft— enable PEFT--lora_r— LoRA rank (default:16)--lora_alpha— LoRA alpha (default:32)--lora_dropout— LoRA dropout (default:0.05)--lora_target_modules— target modules (space-separated)--lora_modules_to_save— additional modules to train--use_rslora— enable Rank-Stabilized LoRA--use_dora— enable Weight-Decomposed LoRA (DoRA)--load_in_4bit— enable 4-bit quantization (QLoRA)--load_in_8bit— enable 8-bit quantization
2. Pass peft_config to trainer (recommended)
2. Pass peft_config to trainer (recommended)
Pass a PEFT configuration directly to the trainer for full control over PEFT methods including LoRA, Prompt Tuning, and others.
3. Apply PEFT to the model directly (advanced)
3. Apply PEFT to the model directly (advanced)
Apply PEFT to the model before passing it to the trainer. Useful for custom architectures or complex setups.
Using ModelConfig and get_peft_config
For script-based workflows, TRL provides ModelConfig and the helper functions get_peft_config and get_quantization_config to build PEFT and quantization configs directly from CLI arguments.
get_peft_config reads the following ModelConfig fields:
get_kbit_device_map
get_kbit_device_map() returns a device map appropriate for k-bit (4-bit or 8-bit) quantized models in multi-GPU environments. Returns {"": local_process_index} when a GPU is available, or None when running on CPU.
PEFT with different trainers
- SFT
- DPO
- GRPO
Learning rate considerations
When using LoRA or other PEFT methods, use a higher learning rate (approximately 10x) compared to full fine-tuning. PEFT methods train only a small fraction of parameters, requiring a larger learning rate to achieve comparable updates.QLoRA: quantized low-rank adaptation
QLoRA combines 4-bit quantization with LoRA to enable fine-tuning of very large models on consumer hardware. This can reduce memory requirements by up to 4x compared to standard LoRA.1
Install bitsandbytes
2
Load the model in 4-bit
3
Attach a LoRA adapter and train
BitsAndBytesConfig parameters
8-bit quantization
For slightly higher precision at reduced memory savings:LoRA configuration reference
Target module selection
Prompt tuning
Prompt tuning learns soft prompts (continuous embeddings) prepended to the input while keeping the entire model frozen. It is particularly parameter-efficient for large models.Saving and loading PEFT models
Pushing to Hub
Multi-GPU training
PEFT works with TRL’s multi-GPU support through Accelerate:Resources
SFT with LoRA/QLoRA notebook
Complete working example with both LoRA and QLoRA.
PEFT documentation
Official PEFT library documentation.
LoRA paper
Original LoRA methodology and results.
QLoRA paper
Efficient finetuning of quantized language models.