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 31 of 120
Trainer and TrainingArguments The training loop, done for you.
⏱ 2 min read ▶ Video at 49:49 🎓 Free
▶ starts at 49:49 Remember gradient descent? Loss, backward, step, repeated thousands of times, in batches. Writing that loop yourself is easy to get wrong. The Trainer class does it all for you.
Loss, backward, step Thousands of times The Trainer does it for you Training arguments holds the settings. Output dir is where results go. Num train epochs is how many passes. Per device train batch size is the batch size. Learning rate is the step size. And eval strategy epoch means, test the model after every epoch.
Argument Meaning output_dir where results are saved num_train_epochs passes over the data per_device_train_batch_size examples per batch learning_rate size of each step eval_strategy="epoch" test after each epoch
Here's train dot p y. It loads the model, like before. Then the training arguments. Two epochs, batches of sixteen, and a learning rate of five times ten to the minus five. The accuracy function compares the model's top guess with the right label. Finally, the Trainer brings everything together. The model, the settings, the train and test data, a data collator, and our metric.
train.py python Copy
import numpy as np
from transformers import (AutoModelForSequenceClassification, DataCollatorWithPadding,
Trainer, TrainingArguments)
from prep import tok, data, MODEL , label2id, id2label
model = AutoModelForSequenceClassification.from_pretrained(
MODEL , num_labels = 5 , id2label = id2label, label2id = label2id)
args = TrainingArguments( output_dir = "intent-model" , num_train_epochs = 2 ,
per_device_train_batch_size = 16 , learning_rate = 5e-5 ,
eval_strategy = "epoch" , logging_strategy = "epoch" ,
save_strategy = "no" , report_to = "none" )
def accuracy (pred):
return { "accuracy" : (np.argmax(pred.predictions, - 1 ) == pred.label_ids).mean()}
trainer = Trainer( model = model, args = args, train_dataset = data[ "train" ],
eval_dataset = data[ "test" ], data_collator = DataCollatorWithPadding(tok),
compute_metrics = accuracy) Prefer to watch? The full Fine-Tuning LLMs course is free All lessons in one video with animations, real code and chapters.
Watch free One new word. A data collator builds each batch. Our messages have different lengths. Data collator with padding pads each batch only to its longest message. That's faster than padding everything to one fixed length.
key value Data collator builds each batch With padding pads to the longest in the batch Benefit less wasted work Used by the Trainer
Let's recap. Training arguments holds the settings. The Trainer runs the whole training loop. And a data collator builds each batch. Next, let's press go.
Key takeaways TrainingArguments Trainer DataCollatorWithPadding Next up:
Train It — our first fine-tuning run