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 38 of 120
Labels for Causal LMs What the model should learn.
⏱ 2 min read ▶ Video at 1:01:05 🎓 Free
▶ starts at 1:01:05 For classification, each label was one number. But a language model writes many tokens. So what are the labels now? And which tokens should the model actually learn from?
Classification: one label A writer: many tokens What are the labels? For a causal language model, the labels are simply the token IDs of the text. At each position, the model tries to predict the next token. The library shifts the labels by one, for us. But there's a problem. We don't want the model to learn to write the customer's question. Only our answer.
key value Labels the token IDs themselves At each position predict the next token Shifting done for you Problem don't learn the question
Here's the fix. We build the prompt with the chat template. The answer is our JSON, followed by the end token. We tokenize both parts. The input IDs are the prompt, then the answer. But in the labels, every prompt token is replaced by minus one hundred. Minus one hundred means, ignore this token in the loss. Run it. The template added a default system message, so the prompt has thirty six tokens. The answer has fifteen. And only the answer tokens have real labels. Including the end token, so the model learns to stop.
app.py python Copy
from transformers import AutoTokenizer
tok = AutoTokenizer.from_pretrained( "HuggingFaceTB/SmolLM2-135M-Instruct" )
prompt = tok.apply_chat_template([{ "role" : "user" , "content" : "Is Dune in stock?" }],
tokenize = False , add_generation_prompt = True )
answer = '{"intent": "stock", "book": "Dune"}<|im_end|>'
prompt_ids = tok(prompt)[ "input_ids" ]
answer_ids = tok(answer)[ "input_ids" ]
input_ids = prompt_ids + answer_ids
labels = [ - 100 ] * len (prompt_ids) + answer_ids
print ( "Prompt tokens:" , len (prompt_ids), " Answer tokens:" , len (answer_ids))
for token, label in list ( zip (input_ids, labels))[ - 16 :]:
print ( f " { tok.decode([token]) !r:14 } label = { label } " ) Output
Prompt tokens: 36 Answer tokens: 15
'\n' label = -100
'{"' label = 39428
'int' label = 591
'ent' label = 293
'":' label = 1799
' "' label = 476
'stock' label = 14621
'",' label = 1002
' "' label = 476
'book' label = 3767
'":' label = 1799
' "' label = 476
'D' label = 52
'une' label = 1935
'"}' label = 23597
'<|im_end|>' label = 2 Prefer to watch? The full Fine-Tuning LLMs course is free All lessons in one video with animations, real code and chapters.
Watch free You won't usually write this by hand. The trainer we'll use, from TRL, can do it for you. It's called training on completions only. But now you know what happens inside. Prompt tokens get minus one hundred. Answer tokens are learned.
key value By hand rarely needed TRL does it for you Name completion-only loss Inside -100 = ignore
Let's recap. For a language model, the labels are the tokens themselves. We set prompt tokens to minus one hundred, so only the answer is learned. And the end token teaches the model to stop. Next, why we need a cheaper way to fine-tune.
Key takeaways labels = token IDs -100 = ignore Learn the answer + stop