Hugging Face, End to EndLesson 62 of 75

Module 7 · Training and adapting, with the libraries the Hub is built on

TRL, and training on conversations

In this lesson

  • 9 min read
  • 6 reading sections
  • Knowledge check
  • Lesson 6 of 9 in this chapter

This chapter’s goalDecide from evidence whether a task needs fine-tuning at all, run a LoRA or QLoRA adaptation with Trainer or SFTTrainer on a single free GPU, read a loss curve well enough to name what is wrong, and publish an adapter a stranger can apply to the base model.

This chapter’s media

The film explains chapter ideas. The audiobook narrates the complete teaching text.

FormatEnglishहिंदी
Chapter filmWatch2:08Not available
Full audiobookNot availableNot available

A little at a time

Explore an idea, try it, then check what stays.

Step 1 of 8Free · no account needed
Lesson steps

Why a separate library

Trainer trains on tensors. Instruction data is conversations, which have to be templated, masked so loss falls only on the assistant’s turns, packed sensibly and truncated without cutting a turn in half. Module five did that by hand once, which is the right way to understand it and the wrong way to do it every time.

TRL’s SFTTrainer does all of it from a dataset of messages.

Bash
pip install trl
Python
from trl import SFTTrainer, SFTConfig

# each row: {"messages": [{"role": "user", ...}, {"role":
# "assistant", ...}]}
trainer = SFTTrainer(
    model=model,
    train_dataset=ds["train"],
    eval_dataset=ds["validation"],
    peft_config=lora_cfg,
    args=SFTConfig(
        output_dir="out",
        max_length=1024,
        packing=True,
        learning_rate=2e-4,
        num_train_epochs=3,
        per_device_train_batch_size=4,
        gradient_accumulation_steps=4,
        bf16=True,
    ),
)
trainer.train()

SFTConfig extends TrainingArguments, so everything from the previous lesson still applies. peft_config wires LoRA in without a separate get_peft_model call.

Your place and first choices stay on this browser. Classroom credit is recorded separately.

Every lesson here is open. Your progress is recorded once you pass the Image Generation, in Practice exam.

Stuck on something?Ask Arjun, your AI tutor

How to cite: Addaly (2026) ‘TRL, and training on conversations’, in Hugging Face, End to End. Edited by M. Mun. Addaly. Available at: https://addaly.com/learn/hugging-face/trl-and-instruction-tuning (Accessed: 29 November 2026).