Skip to Content
gpuvoxDocumentation
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 modelLoRA, 16-bit baseQLoRA, 4-bit base
8B24 GB12 GB
14B40 GB16 GB
32B80 GB32 GB
70B2× 96 GB48 GB

Rent a machine with the pytorch image, then install

pip install -U trl peft datasets accelerate

Train

train.py
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.py

Your 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 ./adapter

Then stop the machine.

Several GPUs

Run the same script with accelerate launch train.py to use every GPU of the machine. See Several GPUs.