1 of 50

LLM Pre-training & Fine-tuning

September 2023

2 of 50

Overview

Topics we’ll cover

  • Evolution of LLMs
  • LLM Pre-training
    • Data collection
    • Data cleaning
    • Tokenization
    • Causal Language Modeling
  • LLM Training Considerations
    • Memory requirements
    • Mixed Precision training
    • Distributed training
    • Scaling laws
  • Fine-tuning
    • Types of fine-tuning
    • Instruction datasets
    • PEFT, LoRA, QLoRA

3 of 50

The Evolution of LLMs

4 of 50

A rapidly evolving ecosystem

5 of 50

A rapidly evolving ecosystem

6 of 50

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

7 of 50

LLM Pre-training

8 of 50

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

9 of 50

LLM Pre-training Workflow

Data Collection

Data Cleaning

Tokenization

Pretraining:� Next Token Prediction

Fine-tuning

10 of 50

Data Collection

  • Before we can train a model, we need to collect data
  • Large text corpus
    • Web scrapes (high quantity)
    • Books (high quality)
    • Academic literature
    • Code
  • Also can be domain specific data (e.g. BloombergGPT)
    • Mix of public and private data

Llama Data Mixture

11 of 50

Data Cleaning

Goal is to increase quality, address bias, remove harmful content.

General steps

  • Language detection - filter out undesired languages
  • Quality Filtering
    • Metric based - use perplexity to remove unnatural sentences
    • Statistic based - punctuation distribution, symbol-to-word ratio, etc
    • Keyword based - explicit content, HTML, hyperlinks, boilerplate,
  • Deduplication - crucial to avoid imbalance distribution, improve performance, reduce training steps needed (especially large models)
    • Exact deduplication (at document level)
    • Near-deduplication with MinHash + LSH
  • PII-removal

Cleaned corpora

  • RefinedWeb (600B tokens)
  • CCNet (360B tokens)
  • The Pile (340B tokens)

Usually 1-10% of original data is actually used!

RefinedWeb

12 of 50

Tokenization

Convert all text into list of integers

Typical vocab size

  • ~30-100k tokens
  • 1 token ~= 0.75 words

Typical algorithm

  • Byte Pair Encoding
  • Tokenizer is learned on the corpus
    • Learns rules to merge tokens based on most common character/sub-word pairs

Raw Text

Tokens

Inputs Ids

13 of 50

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)

14 of 50

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)

  • Yellow = its context
  • Green = current position prediction
  • Red = its target

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)

15 of 50

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

16 of 50

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

17 of 50

LLM Training Considerations

18 of 50

Computational challenges

Approximate GPU RAM needed

1 parameter = 4 bytes (32-bit float)

1B parameters = 4x10^9 bytes = 4GB

  • Llama2-7B → 28GB
  • Llama2-13B → 52GB
  • Llama2-70B → 280GB

GPU

VRAM

NVIDIA Tesla T4

16 GB

NVIDIA A10

24 GB

NVIDIA A100

40 GB | 80 GB

For reference, common GPU Hardware:

19 of 50

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

20 of 50

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

21 of 50

Computational challenges

  • FP32 computations are 2x slower than FP16, but…

  • With fp16 we have a smaller range of possible values, which causes issues with backward pass:
  • Weight updates are imprecise (unstable loss)
  • Gradients can underflow (very small numbers replaced by 0)
  • Gradients can overflow (very large numbers replaced by nan/inf)

So why not just use half-precision?

Largest value represented by fp32340,282,000,000,000,000,000,000,000,000,000,000,000

Largest value represented by fp1665,504

22 of 50

Mixed-precision training

Switch between 32 bit and 16 bit operations during training.

Benefits

  • Requires less memory which enables training larger models
  • Requires less memory bandwidth with speeds up data transfer operations
  • Math operations run faster in reduced precision - speeds up training
  • No loss in task-specific accuracy

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

23 of 50

Distributed Training

When to use

  • Use this if your model fits on one GPU
  • But you want to speed up training

How it works

  • Full model is replicated over multiple GPU’s
  • Each is fed a slice of the data
  • Processing is done in parallel, then synchronized after each train step

Distributed Data Parallel (DDP)

24 of 50

Distributed Training

When to use

  • When model doesn’t fit in VRAM on single GPU

How it works

  • Optimize memory by sharding/distributing model states across GPU’s with zero data overlap
  • Different “stages” of sharding depending on what how much of the memory is distributed
  • Trade-off in configuration complexity and I/O communication time to sync sharded outputs

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

25 of 50

Distributed Training

When to use

  • When model doesn’t fit in VRAM on single GPU

How it works

  • Optimize memory by sharding/distributing model states across GPU’s with zero data overlap
  • Different “stages” of sharding depending on what how much of the memory is distributed
  • Stage3 aka Fully Sharded Data Parallel (FSDP)

Fully Sharded Data Parallel (FSDP)

26 of 50

Chinchilla Scaling Laws for Compute-optimal Models (2022)

Experiments

  • Previously thought (from Open AI paper) that scaling model size was they key to improve model loss
  • Trained >400 models from 70M to 16B parameters on 5-500B tokens
  • Then, trained Chinchilla-70B with same compute budget as Gopher-280B, but 4x more data

Findings

  • Chinchilla outperforms Gopher (and GPT3) on a large range of tasks
  • So…
  • Many existing large models are over-parameterized and under-trained
  • Smaller models trained on more data could perform as well as larger models
  • Compute optimal training datasize is ~20x the number of model parameters

Deepmind investigated the optimal model size and # tokens for training LLMs under a given compute budget

27 of 50

Fine-tuning

28 of 50

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

29 of 50

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

30 of 50

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

31 of 50

Types of fine-tuning

  1. Continued pre-training: With domain-specific data, apply the same pre-training regime (next token prediction, masked language modeling) on the base model → Allows for domain adaptation.

  1. Single-task fine-tuning: The pre-trained model is honed for a narrow and specific task such as toxicity detection or summarization, similar to BERT and T5 → Allows you to use smaller models with fewer labeled examples.
  2. Possibility for catastrophic forgetting (aka alignment tax)
  3. This may not be a bad thing!

  1. Multi-task / Instruction fine-tuning: The pre-trained (base) model is fine-tuned on examples of instruction-output pairs to follow instructions, answer questions, and be conversationally be helpful → Allows you to interact with model naturally.

  1. Reinforcement learning with human feedback (RLHF): This combines instruction fine-tuning with reinforcement learning. It requires collecting human preferences (e.g., pairwise comparisons) which are then used to train a reward model. The reward model is then used to further fine-tune the instructed LLM via RL techniques such as proximal policy optimization (PPO).

32 of 50

Multi-task fine-tuning

  • T5 from Google (2019) was the first model that enabled multi-task capabilities from a single model
  • Previously, needed a specific fine-tuned model per task
  • First example of “prompt engineering” (in retrospect)

33 of 50

Instruction fine-tuning

  • FLAN-T5 (2022)
  • Fine tuning on instruction-context-answer pairs from a variety of tasks
  • Improves usability via natural language
  • Helps to generalize to unseen tasks

34 of 50

Instruction datasets

What makes a good instruction dataset?

  • Clear specific instructions
  • Diverse set of topics / tasks
  • Consistent formatting
  • Human feedback

Human-written

  • Dolly - 15k crowdsourced examples

Imitation learning (simple instructions)

  • Alpaca - used self-instruct, 52k examples
  • Vicuna - 70k examples from ShareGPT
  • GPT4All - 800k examples from ChatGPT
  • LIMA - 1k examples, high quality

Better imitation learning (complex instructions)

  • WizardLM - EvolInstruct
  • Orca - Explanation traces

Human-written

LLM Generated (imitation learning)

35 of 50

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

36 of 50

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?

  • Recipe: Pre-training on generic data + fine-tuning on specific downstream task
  • Large LLMs: full fine-tuning becomes infeasible to train on consumer hardware
  • Catastrophic forgetting: tuning all model parameters is prone to overfitting
  • Storage: storing and deploying fine-tuned models independently for each downstream task becomes very expensive

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

  • Prefix tuning
  • Prompt tuning
  • LoRA
  • etc.

37 of 50

LoRA Overview

Pros:

  • Fine tuning of LLMs using a fraction of the memory requirements
  • Fine-tuning is accessible on common GPU’s
  • Tiny checkpoints (example here)
  • Performance comparable to full fine-tuning
  • No Inference latency addition

Cons:

  • (only during training) The forward and backward pass is approximately twice as slow, due to the additional matrix multiplications in the adapter layers.

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

38 of 50

LoRA Overview

The theory behind it:

  • The rank of the matrix is the number of linearly independent column vectors.
  • Full rank means all columns are linearly independent.
  • Low rank means some columns are linear combinations of the other columns.

  • We take inspiration from Li et al. (2018a); Aghajanyan et al. (2020) which show that the learned over-parametrized models in fact reside on a low intrinsic dimension. We hypothesize that the change in weights during model adaptation also has a low “intrinsic rank”

39 of 50

LoRA Overview

During training:

40 of 50

LoRA Overview

  1. Freeze original self-attention weights (for specified layers)

During training:

W: (768x768)

41 of 50

LoRA Overview

  1. Freeze original self-attention weights (for specified layers)
  2. Inject 2 rank decomposition matrices whose product is same size matrix as original

During training:

W: (768x768)

A: (16x768)

B: (768x16)

BA: (768x768)

r = 16

42 of 50

LoRA Overview

  1. Freeze original self-attention weights (for specified layers)
  2. Inject 2 rank decomposition matrices whose product is same size matrix as original
  3. Train the weights of just these two low-rank matrices
  4. Pass embedding matrix through each separately and add together

During training:

W: (768x768)

A: (16x768)

B: (768x16)

BA: (768x768)

r = 16

43 of 50

LoRA Overview

  1. Freeze original self-attention weights (for specified layers)
  2. Inject 2 rank decomposition matrices whose product is same size matrix as original
  3. Train the weights of just these two low-rank matrices
  4. Pass embedding matrix through each separately and add together

During training:

W: (768x768)

A: (16x768)

B: (768x16)��BA: (768x768)

r = 16

Number of Trainable Params:

  • W: 590k
  • A, B: 12k

95% less trainable params

44 of 50

LoRA Overview

At inference:

W: (768x768)

A: (16x768)

B: (768x16)��BA: (768x768)

r = 16

h = W(x) + BA(x)

h = (W + BA)(x)

45 of 50

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!

46 of 50

LoRA Overview

Performance

47 of 50

LoRA Overview

Choosing the rank parameter

48 of 50

LoRA Overview

Best practices

Highlights:

  • Comparable evaluation performance than full fine-tuning on a variety of tasks
    • Images (diffusion models for different “styles”)
    • Audio (fine-tuning whisper large on a new language)
    • Text (fine-tuning language models)
  • No additional inference latency
  • Hyper-parameters:
    • Higher learning rate than full fine-tuning (order of magnitude of 10-100x)
    • Rank between 4-16
    • Lora alpha: usually 2-4x the LoRA rank

More details:

49 of 50

QLoRA Overview

Frozen model is 4-bit quantized

  • 30B model on 24GB GPU
  • 65B model on 48GB GPU

Uses Paged Optimizers that allow offload to CPU RAM when spikes happen

  • Prevents error and loss of training in middle of run

Only for fine-tuning, not for pre-training

Empirically demonstrated to preserve full fine-tuning task performance

50 of 50

Thank you!