Guides
Fine-tune with LoRA
Train a small adapter on your own data with Hugging Face TRL.
LoRA trains a few million extra weights on top of a frozen model. It needs a fraction of the memory of full fine-tuning and the result is a small adapter you can copy off in seconds.
| Base model | LoRA, 16-bit base | QLoRA, 4-bit base |
|---|---|---|
| 8B | 24 GB | 12 GB |
| 14B | 40 GB | 16 GB |
| 32B | 80 GB | 32 GB |
| 70B | 2× 96 GB | 48 GB |
Rent a machine with the pytorch image, then install
pip install -U trl peft datasets accelerateTrain
from datasets import load_dataset
from peft import LoraConfig
from trl import SFTConfig, SFTTrainer
dataset = load_dataset("trl-lib/Capybara", split="train")
trainer = SFTTrainer(
model="Qwen/Qwen2.5-7B-Instruct",
train_dataset=dataset,
args=SFTConfig(output_dir="/root/out", per_device_train_batch_size=2, gradient_accumulation_steps=8, bf16=True),
peft_config=LoraConfig(r=16, lora_alpha=32, target_modules="all-linear"),
)
trainer.train()
trainer.save_model("/root/out/adapter")tmux new -s train
python train.pyYour own data: a JSONL file with a messages list per line, loaded with load_dataset("json", data_files="data.jsonl").
Copy the adapter off
rsync -avP -e "ssh -p 40122" [email protected]:/root/out/adapter ./adapterThen stop the machine.
Several GPUs
Run the same script with accelerate launch train.py to use every GPU of the machine. See Several GPUs.