LLM Pre-training & Fine-tuning
September 2023
Overview
Topics we’ll cover
The Evolution of LLMs
A rapidly evolving ecosystem
A rapidly evolving ecosystem
Recipe for LLM Development
Stage
Algorithm
Dataset
Model
Compute
Cost
Pretraining
Raw (Web, Books)
Trillions of tokens�Low quality, large quantity
Language Modeling
Next Token prediction
100s - 1000s of GPUs�Weeks of training
Base Model
From scratch
$5-10M
Supervised Fine-tuning
Instructions/Chat
~10-100k Handwritten (prompt + response), High quality, low quantity
Language Modeling
Next Token prediction
1 - 100s of GPUs�Days of training
SFT Model
Init from Base
$100-50 000
Reward Modeling
Comparison
~100k- 1M (prompt1 > prompt2)�High quality, large quantity
Binary Classification
Score Comparisons
1-100s of GPUs�Days of training
RM Model
Init from SFT
$100-50 000
Reinforcement Learning
Prompts
~10k - 100k (inputs)�High quality, low quantity
Reinforcement Learning
Maximize Reward for Gen
1-100s of GPUs�Days of training
RL Model
Init from SFT + RM Model
$100-50 000
LLM Pre-training
Recipe for LLM Development
Stage
Algorithm
Dataset
Model
Compute
Cost
Pretraining
Raw (Web, Books)
Trillions of tokens�Low quality, large quantity
Language Modeling
Next Token prediction
100s - 1000s of GPUs�Weeks of training
Base Model
From scratch
$5-10M
Supervised Fine-tuning
Instructions/Chat
~10-100k Handwritten (prompt + response), High quality, low quantity
Language Modeling
Next Token prediction
1 - 100s of GPUs�Days of training
SFT Model
Init from Base
$100-50 000
Reward Modeling
Comparison
~100k- 1M (prompt1 > prompt2)�High quality, large quantity
Binary Classification
Score Comparisons
1-100s of GPUs�Days of training
RM Model
Init from SFT
$100-50 000
Reinforcement Learning
Prompts
~10k - 100k (inputs)�High quality, low quantity
Reinforcement Learning
Maximize Reward for Gen
1-100s of GPUs�Days of training
RL Model
Init from SFT + RM Model
$100-50 000
LLM Pre-training Workflow
Data Collection
Data Cleaning
Tokenization
Pretraining:� Next Token Prediction
Fine-tuning
Data Collection
Llama Data Mixture
Data Cleaning
Goal is to increase quality, address bias, remove harmful content.
General steps
Cleaned corpora
Usually 1-10% of original data is actually used!
RefinedWeb
Tokenization
Convert all text into list of integers
Typical vocab size
Typical algorithm
Raw Text
Tokens
Inputs Ids
Pre-training: next token prediction / causal language modeling
Raw Text Dataset (batch_size, context_length)
Row 1: Here is an example document 1 <|endoftext|>
Row 2: Example document 2 <|endoftext|> Example document 3 <|endoftext|> Example�Row 3: Document 4 <|endoftext|> Example Document 5
Processed Dataset
48098 | 2667 | 15399 | 318 | 257 | 1664 | 326 | 21126 | 50526 |
1262 | 4572 | 13 | 50526 | 632 | 290 | 50526 | 663 | 284 |
2748 | 4673 | 50526 | 3859 | 2985 | 15399 | 2899 | 2615 | 329 |
Context Length (C)
Batch Size (B)
Pre-training: next token prediction / causal language modeling
Each cell only "sees" cells in its row (on the left of it)
Trying to predict the next cell (on the right of it)
Processed Dataset
48098 | 2667 | 15399 | 318 | 257 | 1664 | 326 | 21126 | 50526 |
1262 | 4572 | 13 | 50526 | 632 | 290 | 50526 | 663 | 284 |
2748 | 4673 | 50526 | 3859 | 2985 | 15399 | 2899 | 2615 | 329 |
Context Length (C)
Batch Size (B)
Pre-training: next token prediction / causal language modeling
Random tokens, just garbage
After 30 000 steps
After 0 steps
After 500 steps
Random words, wrong grammar
Sentence with correct grammar
Pre-training: Examples
49,152 vocabulary size
8192 context length
15.5B parameter
Trained on 1T tokens
Trained for �86,016 GPU hours
StarCoder
32,000 vocabulary size
4096 context length
7-70B parameter
Trained on 2T tokens Trained for �1,720,320 GPU hours (70B)
Llama2
LLM Training Considerations
Computational challenges
Approximate GPU RAM needed
1 parameter = 4 bytes (32-bit float)
1B parameters = 4x10^9 bytes = 4GB
GPU | VRAM |
NVIDIA Tesla T4 | 16 GB |
NVIDIA A10 | 24 GB |
NVIDIA A100 | 40 GB | 80 GB |
For reference, common GPU Hardware:
Computational challenges
But that’s just to store model weights…
| | Bytes per parameter�(for fp32 training) |
Model parameters�(weights) | | 4 |
Optimizer states�(2 states for AdamW: moving averages of gradient + squared gradient) | Information that an optimization algorithm maintains during the training process | 8 |
Gradients | Derivatives of the loss function with respect to the model's parameters. They represent how much the loss would change in response to small changes in each parameter. | 4 |
Activations�(Saved for gradient computation) | Intermediate outputs obtained during the forward pass of data through a neural network | 4 |
Total | | 20 bytes per param |
16 extra bytes per param
Computational challenges
But that’s just to store model weights…
| Bytes per parameter�(for fp32 training) | Llama2-7B (GB) | Llama2-13B (GB) | Llama2-70B (GB) |
Model parameters (weights) | 4 | 28 GB | 52 GB | 280 GB |
Optimizer states�(2 states for AdamW) | 8 | 56 GB | 104 GB | 560 GB |
Gradients | 4 | 28 GB | 52 GB | 280 GB |
Activations | 4 | 28 GB | 52 GB | 280 GB |
Total | 20 bytes per param | 140 GB | 260 GB | 1400 GB |
Computational challenges
So why not just use half-precision?
Largest value represented by fp32 → 340,282,000,000,000,000,000,000,000,000,000,000,000
Largest value represented by fp16 → 65,504
Mixed-precision training
Switch between 32 bit and 16 bit operations during training.
Benefits
It works by identifying which steps require full precision for numerical stability, and using 16-bit everywhere else��This is standard method for pre-training models today
Forward pass and gradient computation are done in half precision == fast compute
Copy gradients back to full precision and update the model == numerical stability
Reducing floating point precision helps reduce model size and speed up computations
Distributed Training
When to use
How it works
Distributed Data Parallel (DDP)
Distributed Training
When to use
How it works
Zero Redundancy Optimizer (ZeRO) - Microsoft
ZeRO Stage 1�Shard Optimizer States (4x memory savings)
ZeRO Stage 2
Shard Optimizer States and gradients
(8x memory savings)
ZeRO Stage 3
Shard Optimizer States and gradients and model weights
(#GPUx memory savings)
Full model copy
Distributed Training
When to use
How it works
Fully Sharded Data Parallel (FSDP)
Chinchilla Scaling Laws for Compute-optimal Models (2022)
Experiments
Findings
Deepmind investigated the optimal model size and # tokens for training LLMs under a given compute budget
Fine-tuning
Recipe for LLM Development
Stage
Algorithm
Dataset
Model
Compute
Cost
Pretraining
Raw (Web, Books)
Trillions of tokens�Low quality, large quantity
Language Modeling
Next Token prediction
100s - 1000s of GPUs�Weeks of training
Base Model
From scratch
$5-10M
Supervised Fine-tuning
Instructions/Chat
~10-100k Handwritten (prompt + response), High quality, low quantity
Language Modeling
Next Token prediction
1 - 100s of GPUs�Days of training
SFT Model
Init from Base
$100-50 000
Reward Modeling
Comparison
~100k- 1M (prompt1 > prompt2)�High quality, large quantity
Binary Classification
Score Comparisons
1-100s of GPUs�Days of training
RM Model
Init from SFT
$100-50 000
Reinforcement Learning
Prompts
~10k - 100k (inputs)�High quality, low quantity
Reinforcement Learning
Maximize Reward for Gen
1-100s of GPUs�Days of training
RL Model
Init from SFT + RM Model
$100-50 000
Why supervised fine-tuning?
Base model is not trained to be an assistant (e.g. chatty)
Base models are optimized to predict the next word based on the corpus they were trained on… which alone isn’t all that useful
Why supervised fine-tuning?
Source: Open AI
Fine tuned models are.
Instruction fine-tuning makes it easier to access models knowledge in a familiar, conversational way
Types of fine-tuning
Multi-task fine-tuning
Instruction fine-tuning
Instruction datasets
What makes a good instruction dataset?
Human-written
Imitation learning (simple instructions)
Better imitation learning (complex instructions)
Human-written
LLM Generated (imitation learning)
Parameter Efficient Fine-tuning (PEFT)
| Bytes per parameter�(for fp32 training) | Llama2-7B (GB) | Llama2-13B (GB) | Llama2-70B (GB) |
Model parameters (weights) | 4 | 28 GB | 52 GB | 280 GB |
Optimizer states�(2 states for AdamW) | 8 | 56 GB | 104 GB | 560 GB |
Gradients | 4 | 28 GB | 52 GB | 280 GB |
Activations | 4 | 28 GB | 52 GB | 280 GB |
Total | 20 bytes per param | 140 GB | 260 GB | 1400 GB |
Full model training is inaccessible without significant compute
Parameter Efficient Fine-tuning (PEFT)
What?
Parameter-Efficient Fine-Tuning (PEFT) is a technique that allows us to fine-tune a large pretrained model on a specific downstream task while requiring significantly fewer parameters than full fine-tuning.
Why?
How?
PEFT approaches only fine-tune a small number of (extra) model parameters while freezing most parameters of the pretrained LLMs, thereby greatly decreasing the computational and storage costs, being portable, avoiding catastrophic forgetting and better in low data regimes.
Methods
LoRA Overview
Pros:
Cons:
Figure explaining how LoRA layers works: extra parameters (in blue) are added on top of frozen layers (in orange)
Adapted from the original paper, figure 1
LoRA Overview
The theory behind it:
LoRA Overview
During training:
LoRA Overview
During training:
W: (768x768)
LoRA Overview
During training:
W: (768x768)
A: (16x768)
B: (768x16)
BA: (768x768)
r = 16
LoRA Overview
During training:
W: (768x768)
A: (16x768)
B: (768x16)
BA: (768x768)
r = 16
LoRA Overview
During training:
W: (768x768)
A: (16x768)
B: (768x16)��BA: (768x768)
r = 16
Number of Trainable Params:
95% less trainable params
LoRA Overview
At inference:
W: (768x768)
A: (16x768)
B: (768x16)��BA: (768x768)
r = 16
h = W(x) + BA(x)
h = (W + BA)(x)
LoRA Overview
At inference:
W: (768x768)
A: (16x768)
B: (768x16)��BA: (768x768)
r = 16
h = W(x) + BA(x)
h = (W + BA)(x)
Easy to swap out LoRA adapters for different tasks!
LoRA Overview
Performance
LoRA Overview
Choosing the rank parameter
LoRA Overview
Best practices
Highlights:
More details:
QLoRA Overview
Frozen model is 4-bit quantized
Uses Paged Optimizers that allow offload to CPU RAM when spikes happen
Only for fine-tuning, not for pre-training
Empirically demonstrated to preserve full fine-tuning task performance
Thank you!