Fine-Tuning LLMs Tutorial 120 lessons · 3:01:51+ of video
Fine-Tuning LLMs Home What Fine-Tuning Is
What Is Fine-Tuning? How LLMs Work Pretraining vs Fine-Tuning When to Fine-Tune
Prompting, RAG or Fine-Tuning? What Fine-Tuning Can and Can't Do The Tools: Transformers, PEFT, TRL Setup, the Hub and pipeline()
Setting Up The Hugging Face Hub Your First Model: pipeline() Tokenizers
Tokenizers Token IDs and Special Tokens Padding, Truncation, Attention Masks Models, Logits and Generation
Loading a Model Logits and Probabilities Generating Text Decoding, Instruct Models and Chat Templates
Decoding Settings Base vs Instruct Models Chat Templates Memory and Data Types
Parameters and Memory Data Types Training Memory How Training Works
Loss Gradients and Learning Rate Epochs, Batches, Steps Working with Datasets
The datasets Library map, filter, split Our Project Data Classification Fine-Tuning
Classification Fine-Tuning Tokenize the Dataset A Classification Model Training with the Trainer
Trainer and TrainingArguments Train It Evaluate It Overfitting, Saving and Using
Overfitting Save and Load Use Your Model Fine-Tuning a Language Model
Full Fine-Tuning an LLM Labels for Causal LMs Why We Need PEFT LoRA Explained
PEFT: the Big Idea LoRA Explained Rank and Alpha LoRA with PEFT
Which Layers? LoraConfig get_peft_model Instruction Data
Instruction Data Formats Build a Chat Dataset Train on Answers Only Your First LoRA Fine-Tune
TRL and SFTTrainer SFTConfig First LoRA Run Results, Loss and Hyperparameters
Before and After Reading the Loss Hyperparameters That Matter Save, Load and Merge Adapters
Saving Adapters Loading Adapters Merging Adapters Quantization and QLoRA
Quantization 4-bit NF4 QLoRA Fitting Training into Memory
GPU Memory Budget Gradient Accumulation Gradient Checkpointing Precision, Length and Hardware
Mixed Precision Sequence Length and Packing Where to Train Bigger Models and Many Adapters
A Bigger Model Many Adapters Practice Data: Quality and Collection
Quality Beats Quantity Collecting Data Synthetic Data Data: Cleaning, Splits and Size
Cleaning and Dedup Splits and Leakage How Much Data? Data: Prompts, Conversations and Sharing
System Prompts Multi-turn Data Sharing Datasets Data: Balance, Safety and Practice
Label Balance Bias and Safety Data Practice Evaluating a Fine-Tuned Model
Why Evaluation Is Hard Exact Match and Valid JSON Perplexity Evaluation Loops, Judges and Forgetting
An Evaluation Loop LLM as a Judge Catastrophic Forgetting Comparing Runs and Error Analysis
Comparing Runs Error Analysis Evaluation Practice Preference Tuning and DPO
Preference Tuning DPO DPO with TRL Beyond LoRA: PEFT, Distillation and Prompting
Other PEFT Methods Distillation When Not to Fine-Tune Sharing and Serving Your Model
Push to the Hub Serving with vLLM GGUF and Ollama Fast Inference, Many Adapters and Costs
Fast Inference Serving Many Adapters Costs Project: Plan, Data and Baseline
Project: Plan Project: Baseline Project: Dataset Project: Train, Evaluate and Improve
Project: Train Project: Evaluate Project: Improve Project: Save, Use and Wrap-up
Project: Merge and Save Project: Chat With It Project: Wrap-up Mistakes, Troubleshooting and Interview Basics
Common Mistakes Troubleshooting Interview: Core Interview Questions and Cheat Sheets
Interview: LoRA and Training Cheat Sheet: Code Cheat Sheet: Settings Final Quiz, Next Steps and Recap
Final Quiz What to Learn Next Course Recap Fine-Tuning LLMs · Lesson 49 of 120
TRL and SFTTrainer Supervised fine-tuning.
⏱ 2 min read ▶ Video at 1:17:59 🎓 Free
▶ starts at 1:17:59 Everything comes together now. A model, LoRA settings, and chat data. TRL gives us one trainer that connects them all. It's called the SFT trainer.
Model, LoRA, data TRL connects them The SFT trainer SFT stands for supervised fine-tuning. Supervised means every example comes with the right answer. It's the first, and most common, kind of fine-tuning for language models. The SFT trainer is built on the normal Trainer, but it also knows about chat templates, completion only loss, and PEFT.
key value SFT supervised fine-tuning Supervised every example has the answer SFTTrainer Trainer + chat + PEFT Most common the first step for LLMs
Here's s f t dot p y. We load the tokenizer and the model. The LoRA config uses rank sixteen, alpha thirty two, on all linear layers. SFT config holds the training settings. One epoch, a learning rate of two times ten to the minus four, and batches of eight. We log every eleven steps, and evaluate on the test set every twenty two. Finally, the SFT trainer gets the model, the settings, the data, the tokenizer, and the LoRA config. It adds the adapters for us.
sft.py python Copy
from transformers import AutoTokenizer, AutoModelForCausalLM
from peft import LoraConfig
from trl import SFTTrainer, SFTConfig
from chat import data
NAME = "HuggingFaceTB/SmolLM2-135M-Instruct"
tok = AutoTokenizer.from_pretrained( NAME )
model = AutoModelForCausalLM.from_pretrained( NAME )
lora = LoraConfig( r = 16 , lora_alpha = 32 , lora_dropout = 0.05 ,
target_modules = "all-linear" , task_type = "CAUSAL_LM" )
args = SFTConfig( output_dir = "ticket-lora" , num_train_epochs = 1 , learning_rate = 2e-4 ,
per_device_train_batch_size = 8 , logging_steps = 11 ,
eval_strategy = "steps" , eval_steps = 22 ,
save_strategy = "no" , report_to = "none" )
trainer = SFTTrainer( model = model, args = args, train_dataset = data[ "train" ],
eval_dataset = data[ "test" ], processing_class = tok, peft_config = lora) Prefer to watch? The full Fine-Tuning LLMs course is free All lessons in one video with animations, real code and chapters.
Watch free Let's recap. SFT means training on examples with the right answers. The SFT trainer handles chat data, and adds LoRA when we pass a peft config. And all our settings fit in one short file. Next, a closer look at SFT config.
Key takeaways Supervised fine-tuning SFTTrainer + peft_config sft.py