Pretraining
Supervised finetuning Preference tuning
Transformers & Large Language Models
Slides from: Fall26 CME295, Stanford University,
Prof. Pawan Goyal, Prof. Saptarshi Ghosh’s lectures
Paradigm for LLM training
LLM training. Train LLM to understand language, then tune it for end task
1 Pretraining
2 "Tuning"
"Pretrained" model
Spam detection
model
Sentiment extraction
model
Translation
model
Pretraining overview
Goal. Learn pattern of language and code
My cute teddy bear is reading by the window, his tiny paws holding a book almost bigger than him. The afternoon light dances across his soft fur as he turns each page with great care…
English
class TeddyBear:
def init (self, name): self.name = name
def hug(self):
print(f"{self.name} gives a warm hug!")
TeddyBear("Fluffy").hug()...
teddy_bear.py
Multi-lingual | |
Mon ours en peluche est mon plus fidèle compagnon. Toujours assis sur mon lit, il m’attend patiemment, prêt à écouter mes secrets… | |
ﺖﺨﺗ ور ﻪﺸﯿﻤﻫ .ﺖﺴﻫ ﻦﻣ ﺖﺳود ﻦیﺮﺗﯽمﯿﻤﺻ ﻦﻣ یﺪﺗ سﺮﺧ ...هﺪﺑ شﻮﮔ ﻦﻣ یﺎﻫﻪﺼﻗ ﻪﺑ هﺮﺿﺎﺣ و ﻪتﺴﺸﻧ ﻦﻣ | |
… | |
package main
func main(){
println("Teddy bear hug!")
}
teddy_bear.go
Pretraining overview
Goal. Learn pattern of language and code
Objective function. Predict next token
A teddy bear is reading Persian poetry. Around him, the room is still: a warm lamp, a cup of tea gone cold, a notebook full of underlined metaphors, and a window looking out onto the evening [...]
Pretraining overview
Goal. Learn pattern of language and code
Objective function. Predict next token
[BOS] | | A | | teddy | | bear | | is | | reading |
| | | | | | | | | | |
Persian | | poetry | | . | | Around | | him | | , |
| | | | | | | | | | |
the | | room | | is | | still | | : | | a |
warm | | lamp | | , | | a | | cup | | … |
Pretraining overview
Goal. Learn pattern of language and code
Objective function. Predict next token
?
LLM
[BOS]
Pretraining overview
Goal. Learn pattern of language and code
Objective function. Predict next token
A
?
LLM
[BOS]
A
Pretraining overview
Goal. Learn pattern of language and code
Objective function. Predict next token
A
teddy
?
LLM
[BOS]
A
teddy
Pretraining overview
Goal. Learn pattern of language and code
Objective function. Predict next token
A
teddy
bear
?
LLM
[BOS]
A
teddy
bear
Pretraining overview
Goal. Learn pattern of language and code
Objective function. Predict next token
A
teddy
bear
is
?
LLM
[BOS]
A
teddy
bear
is
Pretraining is highly parallelizable via teacher forcing
Goal. Learn pattern of language and code
Objective function. Predict next token
Masked self-attention
LLM
[BOS]
A
teddy
bear
is
Input
Right figure from The Annotated Transformer, Harvard NLP, 2018.
Pretraining is highly parallelizable via teacher forcing
Goal. Learn pattern of language and code
Objective function. Predict next token
A
teddy
bear
is
reading
Ground truth
LLM
Prediction
[BOS]
A
teddy
bear
is
Input
Pretraining is highly parallelizable via teacher forcing
Goal. Learn pattern of language and code
Objective function. Predict next token
Target token
Past tokens
LLM
Predicted probability
Number of tokens
Making the loss less dependent on choice of tokenizer
Number of bytes
Conversion of
nats to bits
Goal. Learn pattern of language and code
Objective function. Predict next token
Number of tokens
Cross-Entropy
Bits Per Byte
Pretraining overview
Goal. Learn pattern of language and code
Objective function. Predict next token
Data mixtures.
Teddy bear Wikipedia article and a GitHub repo. Screenshots taken on October 4, 2026.
Pretraining overview
Goal. Learn pattern of language and code
Objective function. Predict next token
Data mixtures (Unlabeled corpus of text, code)
Architectures
Encoder-Decoder
Decoder-only
Encoder-only
Teddy bear Wikipedia article and a GitHub repo. Screenshots taken on October 4, 2026.
Recap - Pretraining Encoders – Masked LM
Transfer Learning through Fine-tuning
Fine Tuning for Sequence Classification
Classifier head can be a fully connected layer (the classification layer) followed by softmax over the set of classes (to get probability of each class)
Fine Tuning for Sequence Classification
These parameters are updated during fine-tuning, using task-specific training data
Pre-trained weights; Frozen or a small subset can be updated
Pairwise Sequence Classification
Text | Hypothesis | Judgement |
An older and younger man smiling. | The older man is not smiling | Contradiction |
An older and younger man smiling. | Two men are laughing at the cats playing on the floor. | Neutral |
An older and younger man smiling. | An older man is smiling. | Entailment |
Pair-wise Sequence Classification
Sequence Labeling (e.g., NER, POS tagging)
We want to label each token.
Do not use the [CLS] embedding; rather, attach a predictor head to the representation of individual tokens.
POS Tagging
These parameters need to be trained during fine-tuning
Pretraining Decoders
It’s natural to pretrain decoders as language models
Causal self-attention : a token attends to only the past / previous tokens.
Pre-training task: next word / token prediction
Teacher forcing used during pre-training. Loss computed w.r.t. ground truth next-token and back-propagated.
Generative Pretrained Transformer (GPT)
Vocabulary larger than BERT
How to use Pretrained Decoder-only models?
We will feed the input tokens (context) as the first few tokens, say w1, w2, w3.
The subsequent output tokens will attend to the input tokens as well as previous output tokens (using causal / masked self-attention).
Input and output tokens are part of the same sequence in decoder-only models.
Decoder-only model can generate sequences using much lesser parameters than Encoder-Decoder models
Fine-tune GPT-2 for Machine Translation
Convert each training data-point to a sequence using a special token <to-fr>
Use the sequences to fine-tune GPT-2 (next token prediction task)
GPT-2: Summarization
In these examples, we are using a decoder-only architecture for sequence-to-sequence tasks
Pretraining overview
Goal. Learn pattern of language and code
Objective function. Predict next token
Data mixtures.
Size. Approx. trillions of tokens (example: GLM-5 pretrained on ~30T tokens)
GLM-5: from Vibe Coding to Agentic Engineering, GLM-5 team, 2026.
Pretraining
Supervised finetuning Preference tuning
Transformers & Large Language Models
Supervised finetuning
SFT = Supervised FineTuning
Idea. Change the model behavior by tuning its weights
Strategy.
Supervised finetuning
SFT = Supervised FineTuning
Idea. Change the model behavior by tuning its weights
Strategy.
● Collect pairs of input/outputs with desired behavior (aka SFT data)
● Train using next word prediction objective given the input
Special case. SFT on instruction-following data: "Instruction tuning"
Instruction tuning overview
Goal. "Graduate" the model to being a helpful assistant.
Story writing | |
Write a short story about a teddy bear who likes to read poetry. | |
Once upon a time, a bear, Teddy, stumbled upon verses from Attar. The bear became so captivated with the rhythm of the poem's sentences that he got filled with happiness. | |
Lists generation | |
List three fun activities a teddy bear might do on a rainy day. | |
Sure!
| |
Poem creation | |
Create a poem about my cute teddy bear. | |
Soft and cuddly, full of charm, Always keeps me safe from harm, With button eyes and fur so neat, My teddy bear is oh so sweet. | |
Explanation | |
Explain why a teddy bear is a great friend. | |
A teddy bear is a great friend because it provides comfort and companionship. Its soft fur and cuddly nature is perfect for hugs. Teddy bears are always there in both happy and sad moments. | |
Finetuning to Instruction Finetuning
There are 100s of tasks.
Do we finetune 100 times?
No.
Perform instruction-finetuning on many
Generalize to arbitrary tasks from
Instruction
Positive Examples
Negative Examples
Input
Predict output (same training setup)
Goal: from Instance to Task-level Generalization
Instruction Finetuning (60 to 1600)
Train on a mixture of crowdsource instruction, input and label.
Data:
Natural Instructions, Mishra et al. 2022 (61 tasks)
Generalize to 1600+ tasks. Crowdsourced initiative to add tasks in a Github repo.
Add simple tasks as well with simpler instructions like longest palindrome substring etc.
Instruction tuning overview
Goal. "Graduate" the model to being a helpful assistant.
Objective function. Predict next token given the input.
Do
X
.
Sure
...
LLM | |||||
| | | |||
[BOS] Do X | . | | Sure | ||
Finetuned language models are zero-shot learners, Wei et al., 2022.
Instruction tuning overview
Goal. "Graduate" the model to being a helpful assistant.
LLM
Objective function. Predict next token given the input.
Input prompt
Target token
Past tokens
Predicted probability
Number of tokens
Instruction tuning overview
Goal. "Graduate" the model to being a helpful assistant
Objective function. Predict next token given the input.
Data mixtures. Can be both human-written and synthetic data.
Instruction tuning overview
Goal. "Graduate" the model to being a helpful assistant
Objective function. Predict next token given the input.
Data mixtures. Can be both human-written and synthetic data.
Size. Thousands to millions of examples.
Behavior
Can I put my teddy bear in the washer?
Pretrained + instruction tuned LLM
No, it might get damaged. Try hand washing instead.
Challenges
Challenges