RTX 3050 - Order Now
Home / Blog / Tutorials / TRL SFTTrainer on a Dedicated GPU
Tutorials

TRL SFTTrainer on a Dedicated GPU

Hugging Face TRL's SFTTrainer is the vanilla fine-tuning API that every framework wraps. Using it directly gives you full control.

Axolotl, Unsloth, and every other fine-tuning framework ultimately wrap Hugging Face’s TRL SFTTrainer. Using it directly on dedicated GPU hosting gives you full visibility and control. Here is a minimal complete example.

Contents

Install

pip install torch transformers trl peft datasets accelerate bitsandbytes

Dataset

SFTTrainer accepts a Hugging Face Dataset with a messages column (chat format) or a plain text column. Chat format is easier for modern instruct tuning:

[
  {"messages": [
    {"role": "user", "content": "..."},
    {"role": "assistant", "content": "..."}
  ]},
  ...
]

Script

from datasets import load_dataset
from transformers import AutoModelForCausalLM, AutoTokenizer
from peft import LoraConfig
from trl import SFTTrainer, SFTConfig

ds = load_dataset("json", data_files="train.jsonl", split="train")
tok = AutoTokenizer.from_pretrained("meta-llama/Llama-3.1-8B-Instruct")
model = AutoModelForCausalLM.from_pretrained(
    "meta-llama/Llama-3.1-8B-Instruct",
    torch_dtype="bfloat16",
    device_map="cuda",
)
lora = LoraConfig(r=16, lora_alpha=32, target_modules=["q_proj","k_proj","v_proj","o_proj"])

trainer = SFTTrainer(
    model=model,
    args=SFTConfig(
        output_dir="./out",
        per_device_train_batch_size=4,
        gradient_accumulation_steps=4,
        learning_rate=1e-4,
        bf16=True,
        num_train_epochs=3,
        max_seq_length=2048,
    ),
    train_dataset=ds,
    peft_config=lora,
    tokenizer=tok,
)
trainer.train()
trainer.save_model("./out")

Tuning

  • max_seq_length: match your dataset’s p95 length; longer wastes compute on padding
  • packing=True in SFTConfig: batches multiple short samples into one sequence – 20-40% throughput gain
  • gradient_checkpointing=True: trade compute for VRAM when batch size is capped

Fine-Tuning Dedicated Hosting

Our UK dedicated GPUs ship with TRL, PEFT, and training data mounted.

Browse GPU Servers

Compare Axolotl (config-driven) and Unsloth (faster kernels).

Need a Dedicated GPU Server?

Deploy from RTX 3050 to RTX 5090. Full root access, NVMe storage, 1Gbps — UK datacenter.

Browse GPU Servers

gigagpu

We benchmark, deploy, and optimise GPU infrastructure for AI workloads. All data in our guides comes from real-world testing on our UK-based dedicated GPU servers.

Ready to deploy your AI workload?

Dedicated GPU servers from our UK datacenter. NVMe storage, 1Gbps networking, full root access.

Browse GPU Servers Contact Sales

Have a question? Need help?