🧠 LLMs

LLMs β€” 5 Documents

Transformer, Model Families, Tokenization, Quantization, NLP

5
Documents
7,897
Total Lines

πŸ“„ Transformer Architecture β€” 1389 lines

AlignmentAttentionBERTCLIPCNNEnsembleFine-tuningGPTGPUInference

1. Introduction and Historical Context

1.1 Pre-Transformer Landscape

Before the introduction of the Transformer architecture in 2017, the dominant paradigms for sequence modeling in natural language processing (NLP) were Recurrent Neural Networks (RNNs), particularly Long Short-Term Memory (LSTM) networks and Gated Recurrent Units (GRUs). These architectures processed sequences token by token, maintaining a hidden state that theoretically carried information across time steps. However, RNNs suffered from several critical limitations:

  1. Sequential Computation: RNNs process tokens one at a time, preventing parallelization across the sequence length. This made training on long sequences prohibitively slow.
  2. Vanishing and Exploding Gradients: Despite gating mechanisms in LSTMs and GRUs, gradients still tend to vanish or explode over very long sequences, making it difficult to capture long-range dependencies.
  3. Memory Constraints: The hidden state dimension acts as a bottleneck, forcing the model to compress all previous information into a fixed-size vector, which limits the model's ability to recall distant context.
  4. Bidirectional Context: Standard RNNs are unidirectional, processing only left-to-right context. While bidirectional RNNs (BiRNNs) address this, they require separate forward and backward passes and still suffer from the other issues.

Convolutional Neural Networks (CNNs) were also applied to sequence modeling, particularly with architectures like WaveNet and ByteNet. CNNs offered better parallelization than RNNs but had their own limitations:

  1. Limited Receptive Field: Standard convolutions only capture local patterns, requiring stacked layers or dilated convolutions to expand the receptive field.
  2. Fixed Context Window: The receptive field grows linearly with depth (or exponentially with dilated convolutions), but still provides less flexible context capture compared to attention mechanisms.
  3. Position Invariance: CNNs are inherently position-invariant, requiring positional embeddings to encode sequence order.

1.2 The Breakthrough

The Transformer architecture, introduced by Vaswani et al. in the seminal 2017 paper "Attention Is All You Need," represented a fundamental paradigm shift. The key insight was that a mechanism called attention could be used not just as an auxiliary component (as it was used in sequence-to-sequence models) but as the primary computational primitive for sequence modeling. The Transformer replaced recurrence with attention and convolution with attention, creating a fully attention-based architecture.

The Transformer achieved state-of-the-art results on machine translation tasks while being significantly more parallelizable and faster to train. The original paper demonstrated a BLEU score of 28.4 on the WMT 2014 English-to-German translation task and 41.8 on the English-to-French task, outperforming all previous models while training at a fraction of the computational cost.

1.3 Impact and Legacy

The Transformer architecture has become the foundation of virtually all modern large language models (LLMs), including:
- GPT series (GPT-2, GPT-3, GPT-4, GPT-4o) by OpenAI
- Llama series (Llama, Llama 2, Llama 3, Llama 4) by Meta
- Claude series by Anthropic
- Gemini series by Google DeepMind
- DeepSeek series by DeepSeek
- Mistral series by Mistral AI
- Qwen series by Alibaba Cloud
- And many others

Beyond NLP, Transformers have been successfully applied to computer vision (Vision Transformer, ViT), audio processing (Whisper, AudioLM), multimodal learning (CLIP, DALL-E, GPT-4V), protein folding (AlphaFold2), and code generation (Codex, GitHub Copilot).


2. The Transformer Paper: "Attention Is All You Need"

2.1 Paper Overview

Published in December 2017 at NeurIPS, "Attention Is All You Need" by Vaswani et al. (Google Brain) proposed a novel architecture that dispensed with recurrence and convolutions entirely. The paper made three primary contributions:

  1. The Transformer architecture: A sequence transduction model based solely on attention mechanisms
  2. Scaled dot-product attention: A more efficient variant of attention that incorporates a scaling factor
  3. Multi-head attention: A mechanism that allows the model to attend to information from different representation subspaces

2.2 Architecture at a Glance

The Transformer follows an encoder-decoder structure, common in sequence-to-sequence models. The encoder maps an input sequence of symbol representations (x₁, ..., xβ‚™) to a sequence of continuous representations z = (z₁, ..., zβ‚™). Given z, the decoder then generates an output sequence (y₁, ..., yβ‚˜) one element at a time.

Key Architectural Components:
- Encoder: Stack of N=6 identical layers, each with two sub-layers:
1. Multi-head self-attention
2. Position-wise feed-forward network
- Decoder: Stack of N=6 identical layers, each with three sub-layers:
1. Masked multi-head self-attention
2. Multi-head cross-attention (attends to encoder output)
3. Position-wise feed-forward network
- Each sub-layer is wrapped with a residual connection followed by layer normalization
- All sub-layers produce outputs of dimension d_model = 512

2.3 Design Principles

The Transformer architecture embodies several key design principles:

  1. Symmetry: The encoder and decoder follow similar patterns, with the decoder having an additional cross-attention layer.
  2. Modularity: Components like attention heads, layers, and feed-forward networks are replicated and can be independently adjusted.
  3. Residual Learning: Every sub-layer has a residual connection, enabling training of deep networks by allowing gradients to flow directly through the computational graph.
  4. Normalization Stability: Layer normalization prevents activations from growing too large or small, stabilizing training.
  5. Parallelization: Self-attention processes all tokens simultaneously, unlike RNNs which process tokens sequentially.

2.4 Hyperparameters in the Original Paper

Base Model:
- d_model = 512 (embedding dimension)
- d_ff = 2048 (feed-forward hidden dimension)
- h = 8 (number of attention heads)
- d_k = d_v = d_model / h = 64 (key and value dimensions per head)
- N = 6 (number of encoder/decoder layers)
- P_drop = 0.1 (dropout rate)
- Label smoothing: Ξ΅ = 0.1

Big Model:
- d_model = 1024
- d_ff = 4096
- h = 16
- d_k = d_v = 64
- N = 6


3. Scaled Dot-Product Attention

3.1 Mathematical Formulation

Scaled dot-product attention is the fundamental attention mechanism used in the Transformer. It computes attention as a weighted sum of values, where the weights are derived from the compatibility of queries and keys.

Given:
- A set of queries Q ∈ ℝ^(n Γ— d_k)
- A set of keys K ∈ ℝ^(m Γ— d_k)
- A set of values V ∈ ℝ^(m Γ— d_v)

The attention output is computed as:

Attention(Q, K, V) = softmax(QK^T / √d_k) Γ— V

3.2 Step-by-Step Computation

Step 1: Compute Attention Scores
The attention scores matrix S = QK^T represents the compatibility between every query and every key. For a sequence of length n and a context of length m, S ∈ ℝ^(n Γ— m).

Each element S_ij = Q_i Β· K_j^T represents the dot product similarity between query i and key j.

Step 2: Scale the Scores
The scores are divided by √d_k to counteract the effect of dimensionality on the dot product magnitude. Without scaling, for large d_k, the dot products become large in magnitude, pushing the softmax function into regions of extremely small gradients.

Why √d_k?
The dot product of two random vectors of dimension d_k has mean 0 and variance d_k. Scaling by √d_k normalizes the variance to 1, keeping the softmax in regions with meaningful gradients. Mathematically:
- If q, k ~ N(0, 1) independently, then q Β· k = Ξ£ q_i k_i has variance d_k
- Dividing by √d_k gives variance 1

Step 3: Apply Softmax
The softmax function is applied row-wise to the scaled scores, producing attention weights that sum to 1 for each query:

A_ij = exp(S_ij / √d_k) / Σ_j exp(S_ij / √d_k)

Step 4: Weighted Sum of Values
The final output is a weighted sum of the values:

Output_i = Ξ£_j A_ij Γ— V_j

3.3 Matrix Form

In practice, attention is computed using matrix operations for efficiency:

Attention(Q, K, V) = softmax(Q Γ— K^T / √d_k) Γ— V

Where:
- Q ∈ ℝ^(batch_size Γ— n_queries Γ— d_k)
- K ∈ ℝ^(batch_size Γ— n_keys Γ— d_k)
- V ∈ ℝ^(batch_size Γ— n_keys Γ— d_v)
- Output ∈ ℝ^(batch_size Γ— n_queries Γ— d_v)

3.4 Properties of Scaled Dot-Product Attention

  1. Permutation Equivariance: The output for a given query position depends on all key-value pairs through a weighted sum, making the operation inherently permutation-equivariant with respect to the key-value pairs.

  2. Differentiable: All operations (matrix multiplication, scaling, softmax) are differentiable, enabling end-to-end training via backpropagation.

  3. Input Length Flexibility: The mechanism can handle variable-length inputs since the attention weights are computed dynamically based on the input.

  4. Content-Based Addressing: Unlike location-based addressing in some prior models, attention weights are computed based on the content of queries and keys, allowing the model to attend to relevant information regardless of its position.

  5. Unnormalized Attention Distribution: The softmax produces a probability distribution over the context positions, with the property that Ξ£_j A_ij = 1 for each query i.

3.5 Numerical Stability Considerations

When computing softmax, numerical overflow can occur if the attention scores are very large. The standard implementation subtracts the maximum value before exponentiation:

softmax(x)_i = exp(x_i - max(x)) / Ξ£_j exp(x_j - max(x))

This preserves the mathematical result while preventing overflow. Combined with the scaling factor, this makes the attention computation numerically stable in practice.

3.6 Efficient Implementation

Modern implementations (e.g., FlashAttention) avoid materializing the full attention matrix O(nΒ²) by using tiling and recomputation techniques. The key insight is that the softmax normalization can be computed incrementally using online softmax algorithms, allowing the attention computation to be fused into a single kernel with O(n) memory complexity.


4. Multi-Head Attention

4.1 Motivation

Instead of performing a single attention function with d_model-dimensional keys, values, and queries, the Transformer uses multi-head attention to project the queries, keys, and values h times with different learned linear projections. This allows the model to jointly attend to information from different representation subspaces at different positions.

4.2 Mathematical Formulation

Multi-head attention computes attention multiple times in parallel, each with different projections:

MultiHead(Q, K, V) = Concat(head_1, ..., head_h) Γ— W^O

Where each head_i is computed as:

head_i = Attention(Q Γ— W_i^Q, K Γ— W_i^K, V Γ— W_i^V)

With parameters:
- W_i^Q ∈ ℝ^(d_model Γ— d_k)
- W_i^K ∈ ℝ^(d_model Γ— d_k)
- W_i^V ∈ ℝ^(d_model Γ— d_v)
- W^O ∈ ℝ^(h Γ— d_v Γ— d_model)

4.3 Dimensions and Computation

In the original Transformer:
- d_model = 512
- h = 8 heads
- d_k = d_v = d_model / h = 64

Total computation for multi-head attention:
- Each head: O(n Γ— m Γ— d_k + n Γ— m Γ— d_v) = O(n Γ— m Γ— d_model/h)
- All heads: O(h Γ— n Γ— m Γ— d_model/h) = O(n Γ— m Γ— d_model)
- Output projection: O(d_model Γ— d_model)

The total computation is the same as single-head attention with d_model-dimensional keys and values, but with better representational capacity.

4.3 Why Multiple Heads?

  1. Different Representation Subspaces: Each head can learn to attend to different types of relationships. For example, one head might focus on syntactic dependencies while another focuses on semantic relationships.

  2. Ensemble Effect: Multiple heads provide a form of ensemble learning within a single layer, reducing the risk of all attention focusing on the same patterns.

  3. Increased Capacity Without Increasing Parameters per Head: Since each head operates in a lower-dimensional space (d_model/h), the total number of parameters is the same as a single head with d_model-dimensional projections. The multi-head formulation provides more representational power for the same parameter budget.

  4. Emergent Specialization: Research has shown that different heads in trained Transformers specialize in different patterns, including:
    - Positional heads that attend to adjacent positions
    - Syntactic heads that track grammatical relationships
    - Rare word heads that focus on uncommon tokens
    - Special token heads (like [CLS] or [SEP] in BERT)

4.4 Interpretation of Attention Heads

Several lines of research have investigated the behavior of individual attention heads:

  1. Attention Flow Analysis: Analyzing attention distributions reveals patterns like attending to the next word, the previous word, or specific syntactic relations.
  2. Head Pruning: Studies have shown that many attention heads can be pruned with minimal performance degradation, suggesting redundancy.
  3. Attention Rollout: A technique that computes attention between layers to understand how information flows from input to output.

4.5 Multi-Query Attention (MQA)

Multi-Query Attention (MQA), proposed by Shazeer (2019), is a variant where all attention heads share the same key and value projections but have separate query projections:

  • W_i^Q: Separate per head (h different projection matrices)
  • W_i^K: Shared across all heads (single projection matrix)
  • W_i^V: Shared across all heads (single projection matrix)

This reduces memory requirements for the KV cache by a factor of h, making it particularly attractive for autoregressive decoding where the keys and values of previous tokens must be stored.

Trade-offs:
- Pros: ~hΓ— reduction in KV cache memory, faster decoding
- Cons: Some loss in representational capacity (typically small for large models)

4.6 Grouped-Query Attention (GQA)

Grouped-Query Attention (GQA), proposed by Ainslie et al. (2023), is a generalization between multi-head attention and multi-query attention. It divides query heads into G groups, where each group shares a single key/value head:

  • h total query heads
  • G key/value heads (G < h)
  • h/G query heads share each key/value head

Special Cases:
- G = h: Standard multi-head attention
- G = 1: Multi-query attention
- G = 4 or 8: Common choices in practice

GQA provides a tunable trade-off between quality and efficiency. Models like Llama 2 (70B), Llama 3, and Mistral use GQA to reduce KV cache memory while maintaining quality close to full multi-head attention.

4.7 Multi-head Latent Attention (MLA)

Multi-head Latent Attention (MLA), used in DeepSeek-V2 and V3, is a further optimization that compresses the keys and values into a latent space:

  1. Keys and values are projected into a lower-dimensional latent space
  2. During inference, only the latent representations need to be cached
  3. The full keys and values are reconstructed on-the-fly during attention computation

This provides even greater KV cache reduction than MQA or GQA while maintaining or improving model quality.


5. Positional Encoding

5.1 The Need for Position Information

Since the Transformer processes all tokens simultaneously (unlike RNNs which process tokens in order), the model has no inherent notion of token position. Without positional information, the self-attention mechanism would treat sequences as bags of tokens, unable to distinguish "the cat sat on the mat" from "the mat sat on the cat."

Positional encodings are added to the input embeddings to inject information about the position of each token in the sequence.

5.2 Sinusoidal Positional Encoding

The original Transformer used sinusoidal positional encodings:

PE(pos, 2i) = sin(pos / 10000^(2i/d_model))
PE(pos, 2i+1) = cos(pos / 10000^(2i/d_model))

Where:
- pos: Position index (0-indexed)
- i: Dimension index (0 to d_model/2 - 1)
- d_model: Embedding dimension

Properties:
1. Deterministic: No learned parameters, purely a function of position and dimension
2. Translation Invariant: PE(pos + k, i) can be represented as a linear function of PE(pos, i), allowing the model to learn relative position patterns
3. Fixed Range: Values are bounded between -1 and 1
4. Multi-resolution: Different dimensions encode different frequencies, from low frequencies (capturing long-range patterns) to high frequencies (capturing local patterns)

Visualization of Frequencies:
- Low dimensions (i close to 0): High frequency, oscillate rapidly with position
- High dimensions (i close to d_model/2): Low frequency, change slowly with position

This multi-resolution encoding allows the model to attend to both short-range and long-range positional relationships.

5.3 Learned Positional Embeddings

An alternative to sinusoidal encodings is to learn position embeddings during training:

position_embeddings = nn.Embedding(max_sequence_length, d_model)

Advantages:
1. Adaptable to the specific task and data distribution
2. Potentially more expressive than fixed sinusoidal functions
3. Used in models like BERT, GPT-2, GPT-3

Disadvantages:
1. Cannot extrapolate to sequence lengths longer than those seen during training
2. Requires more parameters (though typically negligible at max_seq_len Γ— d_model)
3. May overfit to position-specific patterns in the training data

5.4 Rotary Position Embedding (RoPE)

RoPE, introduced by Su et al. (2021), is a relative position encoding method that applies rotation to query and key vectors based on their positions. It has become the de facto standard in modern LLMs (Llama, Mistral, Qwen, etc.).

Mechanism:
Given a query vector q at position m and a key vector k at position n, RoPE applies a rotation:

q'_m = R(Θ, m) Γ— q_m
k'_n = R(Θ, n) Γ— k_n

Where R(Θ, m) is a block-diagonal rotation matrix:

R(Θ, m) = [[cos(mθ₁), -sin(mθ₁), 0, 0, ...],
           [sin(mθ₁),  cos(mθ₁), 0, 0, ...],
           [0, 0, cos(mΞΈβ‚‚), -sin(mΞΈβ‚‚), ...],
           [0, 0, sin(mΞΈβ‚‚),  cos(mΞΈβ‚‚), ...],
           ...]

Where ΞΈ_i = base^(-2i/d) for some base (typically 10000).

The dot product q'_m Β· k'_n depends only on (m-n), making it a relative position encoding:

q'_m Β· k'_n = f(q_m, k_n, m-n)

Benefits of RoPE:
1. Relative Position: Naturally encodes relative positions through the rotation angle difference
2. No Additional Parameters: Pure mathematical transformation, no learned parameters
3. Decay with Distance: Attention weights naturally decay as the relative distance increases (for certain basis frequencies)
4. Extrapolation Capability: Can potentially handle longer sequences than seen during training (with appropriate interpolation methods)
5. Compatible with Linear Attention: Can be adapted to linear attention mechanisms

5.5 ALiBi (Attention with Linear Biases)

ALiBi, proposed by Press et al. (2021), takes a simpler approach to position encoding. Instead of adding position information to the embeddings, ALiBi biases the attention scores directly:

Attention(Q, K, V) = softmax(QK^T / √d + m Γ— bias) Γ— V

Where the bias matrix is:

bias_ij = -|i - j| Γ— m

And m is a head-specific slope (typically a geometric sequence like 1/2, 1/4, 1/8, ...).

Advantages of ALiBi:
1. Extremely Simple: No learned position parameters
2. Excellent Extrapolation: Can generalize to much longer sequences than seen during training
3. Computationally Efficient: The bias can be pre-computed and cached
4. Theoretically Grounded: The linear bias encodes a prior that nearby tokens are more important than distant ones

Usage: ALiBi is used in models like BLOOM and some MPT variants.

5.6 YaRN (Yet another RoPE extensioN)

YaRN, proposed by Peng et al. (2023), is a method for extending the context length of models using RoPE. It addresses the problem that RoPE's performance degrades when the sequence length exceeds the training length.

Key Techniques:
1. NTK-aware Interpolation: Instead of uniformly scaling RoPE frequencies (as in linear interpolation), YaRN applies a frequency-dependent scaling that preserves high-frequency information while interpolating low frequencies.
2. Temperature Tuning: YaRN adjusts the softmax temperature to account for the changed distribution of attention scores.
3. Length Scaling: A combination of interpolation and extrapolation based on the ratio of target to original context length.

Mathematical Formulation:
Given a scale factor s = L' / L (where L is training length and L' is target length):
- For dimensions i < k: Linear interpolation: ΞΈ'_i = ΞΈ_i / s
- For dimensions i β‰₯ k: NTK-aware scaling: ΞΈ'_i = ΞΈ_i Γ— s^(2i/d) / s

Where k is a crossover dimension determined empirically.

5.7 NTK-Aware Scaling

NTK (Neural Tangent Kernel)-aware scaling, originally proposed in the context of position encoding extension, draws inspiration from the NTK theory of deep learning. The key insight is that different dimensions of the RoPE encoding encode different frequency information, and they should be scaled differently when extending context length.

Principle:
High-frequency dimensions encode local position information (neighboring tokens) and should be minimally interpolated to preserve fine-grained position discrimination. Low-frequency dimensions encode long-range position patterns and can be more aggressively interpolated.

Practical Implementation:
The scaling factor is applied per-dimension:

ΞΈ'_i = ΞΈ_i Γ— base^(2i/d Γ— (Ξ±-1)) / Ξ±

Where Ξ± > 1 is a scaling parameter representing how much to extend the context (e.g., Ξ± = 4 for 4Γ— extension).

5.8 Comparison of Position Encoding Methods

Method Type Parameters Extrapolation Used By
Sinusoidal Absolute None Limited Original Transformer
Learned Absolute seq_len Γ— d_model Poor BERT, GPT-2
RoPE Relative None (except base) Good (with interpolation) Llama, Mistral, Qwen
ALiBi Relative None (head slopes) Excellent BLOOM, MPT
YaRN Extension None Good Various fine-tuned models
T5 Bias Relative 32 Γ— num_heads Γ— num_buckets Limited T5, FLAN-T5

6. Encoder Architecture

6.1 Overall Structure

The Transformer encoder processes an input sequence and produces a sequence of contextualized representations. It consists of a stack of N identical layers (N=6 in the original paper).

Input Processing:
1. Input tokens are embedded using a learned embedding matrix
2. Positional encodings are added to the embeddings
3. Dropout is applied

Per-Layer Operations:
Each encoder layer consists of:
1. Multi-head self-attention sub-layer
2. Residual connection + Layer normalization
3. Position-wise feed-forward network sub-layer
4. Residual connection + Layer normalization

6.2 Self-Attention in the Encoder

In the encoder's self-attention, the queries, keys, and values all come from the output of the previous layer. This allows each position to attend to all positions in the previous layer's output, including itself.

Characteristics:
- Full Context: Each position can attend to every other position in the input sequence
- No Masking: Unlike the decoder, the encoder uses unmasked self-attention, allowing bidirectional context
- Same Source for Q, K, V: All three matrices are derived from the same input representation

6.3 Information Flow Through the Encoder

The encoder processes information in a hierarchical manner:

  1. Lower Layers: Capture local patterns, syntactic information, and surface-level features
  2. Middle Layers: Build more abstract representations, combining local and global information
  3. Upper Layers: Produce highly abstract, contextualized representations suitable for downstream tasks

Each layer applies self-attention that allows information to flow between any pair of positions, followed by feed-forward processing that operates independently per position.

6.4 Encoder Output

The output of the encoder is a sequence of vectors, one per input token, with dimension d_model. These vectors are contextualized representations that incorporate information from the entire input sequence. In the encoder-decoder architecture, this output is used as the keys and values in the decoder's cross-attention layers.

6.5 Encoder Only Models

Some models use only the encoder portion of the Transformer:
- BERT (Bidirectional Encoder Representations from Transformers)
- RoBERTa
- ALBERT
- DeBERTa
- ELECTRA

These encoder-only models are typically used for understanding tasks like text classification, named entity recognition, question answering, and sentence similarity.

Key Differences from Full Transformer:
- No decoder stack
- No cross-attention
- Typically uses [CLS] token representation for classification
- Often pre-trained with masked language modeling (MLM) objective


7. Decoder Architecture

7.1 Overall Structure

The Transformer decoder is responsible for generating output sequences autoregressively. Like the encoder, it consists of a stack of N identical layers, but with three sub-layers per layer instead of two.

Input Processing:
1. Output tokens (shifted right) are embedded
2. Positional encodings are added
3. Dropout is applied

Per-Layer Operations:
Each decoder layer consists of:
1. Masked multi-head self-attention (prevents attending to future tokens)
2. Residual connection + Layer normalization
3. Multi-head cross-attention (attends to encoder output)
4. Residual connection + Layer normalization
5. Position-wise feed-forward network
6. Residual connection + Layer normalization

7.2 Masked Self-Attention

The decoder's self-attention is masked to prevent positions from attending to subsequent (future) positions. This is essential for autoregressive generation, where each token should only depend on previously generated tokens.

Implementation:
The attention scores matrix is masked by setting S_ij = -∞ for j > i (positions attending to future positions). After softmax, these positions receive zero attention weight.

This masking ensures that the prediction for position i can depend only on known outputs at positions less than i.

7.3 Autoregressive Generation

During inference, the decoder generates tokens one at a time:

  1. Start with a special beginning-of-sequence token (e.g., [BOS] or )
  2. For each generation step:
    a. Run the full encoder-decoder model with the current output sequence
    b. Take the output representation at the last position
    c. Apply the final linear layer and softmax to get token probabilities
    d. Select the next token (greedy, sampling, beam search, etc.)
    e. Append to the output sequence
  3. Continue until an end-of-sequence token (e.g., [EOS] or ) is generated

Key Point: The decoder attends to ALL previously generated tokens during each step, but only the last position's output is used for predicting the next token.

7.4 Cross-Attention

The cross-attention sub-layer in the decoder allows each position to attend to the encoder's output. The queries come from the decoder's previous layer, while the keys and values come from the encoder output.

This mechanism bridges the encoder and decoder, allowing the decoder to access information from the entire input sequence at each generation step.

7.5 Decoder-Only Architecture

Most modern LLMs (GPT series, Llama, Mistral, Claude, etc.) use a decoder-only architecture. These models are essentially the decoder portion of the Transformer without the encoder and cross-attention.

Decoder-Only Advantages:
1. Simpler Architecture: Only one stack of layers, no encoder-decoder interaction
2. Unified Pre-training: Can be trained with a single language modeling objective
3. Flexible Generation: Can handle any text generation task without task-specific modifications
4. Scaling Efficiency: Better parameter-to-performance ratio for generative tasks

Decoder-Only Architecture Details:
- No encoder, no cross-attention
- Causal (masked) self-attention in all layers
- Typically pre-trained with next token prediction (autoregressive language modeling)
- Examples: GPT-2, GPT-3, GPT-4, Llama 2/3, Mistral, Qwen, DeepSeek


8. Encoder-Decoder Cross-Attention

8.1 Purpose and Function

Cross-attention connects the encoder and decoder, allowing the decoder to selectively attend to the encoder's representations. In machine translation, this allows the decoder to focus on relevant parts of the source sentence when generating each target word.

8.2 Mathematical Formulation

CrossAttention(Q_decoder, K_encoder, V_encoder) = softmax(Q_decoder Γ— K_encoder^T / √d_k) Γ— V_encoder

Where:
- Q_decoder: From the previous decoder layer (represents what the decoder is currently focusing on)
- K_encoder: From the encoder output (represents what information is available)
- V_encoder: From the encoder output (the actual content to retrieve)

8.3 Role in Generation

Cross-attention enables:
1. Selective Focus: The decoder can attend to different parts of the input at different generation steps
2. Alignment Learning: The attention weights implicitly learn alignment between input and output tokens
3. Information Retrieval: The decoder retrieves relevant information from the encoder's rich representations

Visual Pattern: In trained translation models, the cross-attention weights often exhibit diagonal patterns, showing that the model learns to align source and target tokens roughly in order.

8.4 Comparison with Self-Attention

Aspect Self-Attention Cross-Attention
Q Source Same layer Previous decoder layer
K,V Source Same as Q Encoder output
Purpose Contextualize positions Access input information
Masking Depends (yes in decoder, no in encoder) Unmasked
Sequence Length Single sequence Two sequences of potentially different lengths

8.5 Modern Decoder-Only Models

In decoder-only models like GPT and Llama, cross-attention is absent. Instead, the entire input (prompt) is processed through the causal self-attention mechanism. The model generates continuations by attending to the entire prompt + previously generated tokens.

This eliminates the encoder-decoder distinction entirely, simplifying the architecture while achieving strong performance on a wide range of tasks.


9. Feed-Forward Networks

9.1 Position-Wise FFN

Each layer of the Transformer contains a feed-forward network that is applied to each position separately and identically. The FFN consists of two linear transformations with an activation function in between.

In the original Transformer:

FFN(x) = max(0, xW₁ + b₁)Wβ‚‚ + bβ‚‚

Where:
- W₁ ∈ ℝ^(d_model Γ— d_ff)
- Wβ‚‚ ∈ ℝ^(d_ff Γ— d_model)
- d_ff = 2048 (4Γ— d_model)

9.2 Purpose of the FFN

The FFN serves several critical functions:

  1. Non-linear Transformation: While attention is a linear (or quasi-linear) operation, the FFN introduces nonlinearity through its activation function, allowing the model to learn complex patterns.

  2. Projection to Higher Dimension: The FFN expands the representation to a higher dimension (d_ff), applies a nonlinearity, and then projects back. This "bottleneck" structure allows the model to learn rich feature interactions.

  3. Memory Storage: Research suggests that FFNs act as key-value memories, storing knowledge learned during training. The first layer acts as a key that activates certain patterns, while the second layer retrieves the associated value.

  4. Position-Independent Processing: Unlike attention (which mixes information across positions), the FFN processes each position independently, allowing for position-specific transformations.

9.3 Activation Functions

ReLU (Rectified Linear Unit):

ReLU(x) = max(0, x)

Used in the original Transformer. Simple and computationally efficient but has the "dying ReLU" problem where neurons can become permanently inactive.

GELU (Gaussian Error Linear Unit):

GELU(x) = x Γ— Ξ¦(x)

Where Ξ¦(x) is the cumulative distribution function of the standard normal distribution. GELU can be approximated as:

GELU(x) = 0.5 Γ— x Γ— (1 + tanh(√(2/Ο€) Γ— (x + 0.044715 Γ— xΒ³)))

GELU is smoother than ReLU and has non-zero gradients for negative values, making it popular in modern models (BERT, GPT-3, ViT).

SiLU/Swish:

Swish(x) = x Γ— sigmoid(x) = x / (1 + e^(-x))

Also known as SiLU (Sigmoid Linear Unit). Used in some modern LLMs.

GeGLU (Gaussian Error Gated Linear Unit):

GeGLU(x, W, V, b, c) = GELU(xW + b) βŠ™ (xV + c)

Where βŠ™ denotes element-wise multiplication. Used in the popular SwiGLU variant.

SwiGLU (Swish Gated Linear Unit):

SwiGLU(x) = Swish(xW₁) βŠ™ (xWβ‚‚)

SwiGLU is a gated variant of Swish/SiLU that has been shown to outperform ReLU and GELU in language modeling. It is used in:
- Llama 2/3
- Mistral
- Qwen 2
- DeepSeek V2/V3

Note: SwiGLU has 3 weight matrices instead of 2 (due to the gating mechanism), so when comparing parameter counts, d_ff is typically adjusted (e.g., 8/3 Γ— d_model instead of 4 Γ— d_model to maintain the same parameter count).

9.4 FFN Variants

Variant Formula Used In
ReLU FFN max(0, xW₁)Wβ‚‚ Original Transformer
GELU FFN GELU(xW₁)Wβ‚‚ BERT, GPT-3
SwiGLU (Swish(xW₁) βŠ™ xWβ‚‚)W₃ Llama, Mistral, Qwen
GeGLU (GELU(xW₁) βŠ™ xWβ‚‚)W₃ PaLM, CodeGen
GLU Variant (Οƒ(xW₁) βŠ™ xWβ‚‚)W₃ Some T5 variants
Parallel FFN FFN(x) + Attention(x) combined PaLM

9.5 Parallel FFN and Attention

Some models (notably PaLM) compute the FFN and attention in parallel rather than sequentially:

y = x + Attention(LayerNorm(x)) + FFN(LayerNorm(x))

This improves training throughput by ~15% because the two matrix multiplications can be fused, with minimal quality degradation.


10. Layer Normalization

10.1 Definition and Purpose

Layer normalization (LayerNorm) normalizes the inputs across the feature dimension for each training example independently. It stabilizes the training process by reducing the internal covariate shift.

Given an input x ∈ ℝ^d, LayerNorm computes:

LayerNorm(x) = Ξ³ βŠ™ (x - ΞΌ) / Οƒ + Ξ²

Where:
- ΞΌ = mean(x) = (1/d) Γ— Ξ£_i x_i
- Οƒ = sqrt((1/d) Γ— Ξ£_i (x_i - ΞΌ)Β² + Ξ΅)
- Ξ³, Ξ² ∈ ℝ^d are learnable scale and shift parameters
- Ξ΅ is a small constant for numerical stability

10.2 LayerNorm vs BatchNorm

Aspect Layer Normalization Batch Normalization
Normalization axis Feature dimension Batch dimension
Dependence on batch size Independent Depends on batch size
Training/Inference behavior Same Different (uses running stats)
Works with RNNs/Transformers Yes Problematic
Memory overhead Lower Higher (needs running statistics)

Layer normalization is preferred in Transformers because:
1. It works equally well at training and inference time
2. It's independent of batch size
3. It handles variable-length sequences naturally
4. It doesn't introduce dependence between samples in a batch

10.3 Pre-Norm vs Post-Norm

Post-Norm (Original Transformer):

x = LayerNorm(x + SubLayer(x))

Used in the original Transformer paper. Each sub-layer's output is added to its input (residual connection) and then normalized.

Pre-Norm (Modern Practice):

x = x + SubLayer(LayerNorm(x))

Each sub-layer receives a normalized input, and the output is added to the residual stream without further normalization.

Comparison:

Aspect Post-Norm Pre-Norm
Gradient flow Must go through LayerNorm Direct path via residual
Training stability Less stable, requires careful LR More stable, allows higher LR
Warm-up steps Often needed Can be minimized
Final performance Potentially better Slightly worse but more stable
Use in practice Historical Modern standard

Why Pre-Norm Won:
Pre-norm has become dominant because it enables stable training of very deep models without extensive hyperparameter tuning. The residual path is "clean" (no LayerNorm in the direct path), allowing gradients to flow directly from output to input.

RMSNorm (Root Mean Square Normalization):

A simplified version of LayerNorm that only normalizes by the root mean square (without mean subtraction):

RMSNorm(x) = x / sqrt((1/d) Γ— Ξ£_i x_iΒ² + Ξ΅) Γ— Ξ³

RMSNorm is computationally cheaper (no mean computation) and has been shown to work as well as LayerNorm. It is used in:
- Llama 2/3
- Mistral
- Qwen
- Gemma


11. Residual Connections

11.1 Purpose and Mechanism

Residual connections (also called skip connections) allow the output of a sub-layer to bypass the sub-layer and be added directly to its input. This creates a direct gradient highway from the output to the input of the network.

output = x + SubLayer(x)

11.2 Importance in Transformers

Residual connections are crucial for several reasons:

  1. Gradient Flow: They allow gradients to flow directly from the loss to early layers, mitigating the vanishing gradient problem in deep networks.

  2. Identity Bias: They bias the network toward learning the identity function, meaning new layers only need to learn the residual (difference from the input) rather than the complete transformation.

  3. Information Preservation: The residual stream preserves information across layers, with each layer potentially adding or modifying information rather than overwriting it.

  4. Training Deep Networks: Without residual connections, training Transformers with more than a few layers would be extremely difficult.

11.3 The Residual Stream Perspective

An influential perspective (from Elhage et al., "A Mathematical Framework for Transformer Circuits") views the residual stream as a shared communication channel:

  1. Each token's representation evolves along the residual stream
  2. Attention layers read from the residual stream and write back to it
  3. FFN layers read from the residual stream and write back to it
  4. The residual stream maintains a "sum representation" where information is additive

This leads to several insights:
- Composability: Different attention heads and FFN neurons can independently contribute to the residual stream
- Interpretability: Interventions can be localized to specific layers or heads
- Information Overlap: Multiple computations coexist in the same representation space

11.4 Initialization in the Presence of Residuals

Proper initialization is critical when using residual connections. If the variance of the residual contribution is too large, the residual stream can grow without bound. Common practices include:

  1. Small Initialization: Initialize the output projection matrices (W^O in attention, Wβ‚‚ in FFN) with small values
  2. Scaling: Apply a scaling factor to the residual contributions
  3. Xavier/Glorot Initialization: Use variance-preserving initialization schemes

DeepNet-style Initialization:
Wang et al. (2022) proposed scaling the residual contributions by 1/√(2N) where N is the number of layers, enabling stable training of very deep Transformers (up to 1000 layers).


12. Label Smoothing

12.1 Definition

Label smoothing is a regularization technique that modifies the target distribution for training. Instead of using one-hot encoded targets (where the correct class has probability 1 and all others have 0), label smoothing uses a softened target distribution.

12.2 Mathematical Formulation

Given a true label y and smoothing parameter Ξ΅ (typically 0.1):

q'(k|x) = (1 - Ξ΅) Γ— Ξ΄_k,y + Ξ΅ / V

Where:
- V is the vocabulary size
- Ξ΄_k,y is 1 if k=y, 0 otherwise (one-hot)
- Ξ΅ is the smoothing factor

For example, with Ξ΅ = 0.1 and V = 10000:
- Correct token loss weight: 1 - 0.1 + 0.1/10000 β‰ˆ 0.90001
- Each incorrect token loss weight: 0.1/10000 = 0.00001

12.3 Effects on Training

  1. Prevents Overconfidence: The model is penalized less for assigning non-zero probability to incorrect tokens, preventing it from becoming overly confident.

  2. Improves Calibration: Label-smoothed models typically produce better-calibrated probability estimates.

  3. BETTER Generalization: The model learns smoother decision boundaries and generalizes better to unseen data.

  4. Marginally Reduced Training Accuracy: The training loss is slightly higher (since the model can't achieve zero loss), but validation performance typically improves.

12.4 Usage in Transformers

The original Transformer used label smoothing with Ξ΅ = 0.1. This practice has been carried forward to many modern models, though its usage varies:

  • GPT-3: Used label smoothing
  • BERT: Did not use label smoothing (uses masked LM loss)
  • T5: Used label smoothing
  • Modern LLMs: Usage varies; some find it beneficial while others skip it

πŸ“„ Model Families β€” Comprehensive Reference β€” 2344 lines

This document provides a deep technical reference on every major Large Language Model family, their architectures, training methodologies, key innovations, and comparative analysis. Written for AI engineers and researchers.

AgentAlignmentAttentionBERTBenchmarkConstitutional AIDPODeploymentDiffusionEvaluation

GPT Family (OpenAI)

GPT-1 (2018) β€” The Original Transformer Decoder

GPT-1 introduced the generative pre-training paradigm: train a language model on unlabeled text, then fine-tune on downstream tasks. Its architecture was a 12-layer, 12-head, 768-hidden-size Transformer decoder with 117M parameters.

Architecture details:
- Decoder-only Transformer with masked self-attention (causal masking)
- 12 transformer blocks, each with multi-head self-attention (12 heads, 64-dim per head)
- 768-dimensional hidden states, 3072-dimensional feed-forward intermediate (4Γ— expansion)
- GELU activation (not ReLU β€” this was an early adoption of GELU)
- Learned positional embeddings (not sinusoidal), max sequence length 512
- LayerNorm applied after each sub-block (post-norm), unlike the original Transformer's pre-norm
- Vocabulary: 40,000 BPE tokens (using Byte-Pair Encoding from GPT-2's precursor work)

Training:
- Pre-trained on BooksCorpus (~7,000 unique books, ~800M words)
- Unsupervised objective: standard autoregressive language modeling (predict next token)
- Batch size 64, 100 epochs, Adam optimizer with learning rate 2.5e-4
- Trained on 4 P600 GPUs for ~1 month

Fine-tuning architecture:
- For classification: add a linear projection on top of the [CLS] token's final hidden state
- For entailment: concatenate premise and hypothesis with a delimiter token
- For similarity: process both orderings and add their representations
- For QA: concatenate document, question, and options with delimiters
- Only fine-tuned for 3 epochs on each task β€” remarkably data-efficient

Key innovation: Demonstrated that generative pre-training + discriminative fine-tuning could match or exceed task-specific architectures, laying the foundation for the entire LLM paradigm. Achieved state-of-the-art on 9 of 12 NLU benchmarks at the time, including 8.9% absolute improvement on RACE (reasoning dataset).


GPT-2 (2019) β€” Zero-Shot Learning at Scale

GPT-2 scaled GPT-1's approach dramatically: 1.5B parameters, trained on WebText (40GB of Reddit-curated web pages). The key insight was that language models could perform tasks in a zero-shot setting without fine-tuning β€” just by conditioning on a prompt.

Architecture (5 model sizes):

Model Parameters Layers d_model Heads
Small 124M 12 768 12
Medium 355M 24 1024 16
Large 774M 36 1280 20
XL 1.5B 48 1600 25

Key architectural changes from GPT-1:
- LayerNorm moved to the input of each sub-block (pre-norm), improving training stability
- Scale of residual weights initialized at 1/√N (where N is the number of residual layers) β€” this normalization prevented activation growth
- Vocabulary expanded to 50,257 tokens (byte-level BPE, with a special "end of text" token)
- Context window increased from 512 to 1024 tokens
- Batch size: 512 (vs GPT-1's 64)
- GELU activation retained

Byte-Level BPE tokenizer:
- Unlike GPT-1/SentencePiece which operate on Unicode characters after normalization, GPT-2's tokenizer operates directly on bytes (UTF-8 byte sequences)
- This ensures 100% coverage of all Unicode strings without requiring any UNK tokens
- Base vocabulary: all 256 byte values, plus learned BPE merges on top
- No lowercasing or NFKC normalization β€” preserves all text faithfully
- This design choice meant the tokenizer could handle any string, but at the cost of roughly 4Γ— token count for non-English text

Zero-shot capability:
- GPT-2 showed that scaling alone produced emergent abilities without explicit fine-tuning
- On LAMBADA: 55.1% (Small) β†’ 63.2% (Medium) β†’ 69.4% (Large) β†’ 75.1% (XL) zero-shot accuracy
- On Winograd Schema Challenge: 70.7% zero-shot (comparable to supervised SOTA at the time)
- Machine translation zero-shot: "prompt engineering" by conditioning on "English sentence = French translation" format
- Question answering: conditioning on context + question format produced reasonable answers
- Summarization: conditioning on "TL;DR:" produced extractive summaries

Controversy:
- OpenAI initially withheld the full 1.5B model due to "concerns about malicious applications" (fake news, spam, impersonation)
- This sparked massive debate about open vs closed AI research
- The full model was eventually released ~9 months later after no major misuse was detected
- The staged release became a template for how frontier models would be handled


GPT-3 (2020) β€” In-Context Learning

GPT-3 was the true scaling breakthrough: 175B parameters, trained on 570GB of text. The key discovery was in-context learning β€” the ability to perform tasks from just a few examples in the prompt, without any gradient updates.

Architecture:
- Same decoder-only Transformer architecture as GPT-2
- Pre-norm LayerNorm (same as GPT-2)
- Alternating dense and sparse attention patterns (though later analysis suggests this wasn't critical)
- Sparse attention layers: every 6th layer uses a "sparse" attention pattern to reduce O(nΒ²) cost
- Context window: 2048 tokens (increased from 1024 in GPT-2)
- Batch size: 3.2 million tokens (extremely large)
- Learning rate: cosine decay from 1.0e-4 to 1.0e-5

Training data (weighted mix):
- CommonCrawl (filtered): 410B tokens, 60% weight
- WebText2: 19B tokens, 22% weight
- Books1: 12B tokens, 8% weight
- Books2: 55B tokens, 8% weight
- Wikipedia: 3B tokens, 3% weight

Model sizes (8 variants):

Model Parameters Layers d_model Heads Batch Size (tokens)
Ada 350M 12 768 12 64K
Babbage 1.3B 24 2048 16 64K
Curie 6.7B 32 4096 32 64K
Davinci 175B 96 12288 96 3.2M

In-context learning (ICL):
- Zero-shot: prompt describes the task, model generates answer without examples
- One-shot: one example provided in the prompt
- Few-shot: k examples (typically 10-100) provided in the prompt
- ICL works because the forward pass propagates information from the examples to the query through the attention mechanism β€” the model "computes" a learning algorithm in its activations
- Meta-learning interpretation: the pre-training phase trains the model to "learn to learn" from sequences; at inference, the few-shot examples trigger this capability
- Performance scales log-linearly with number of in-context examples

Key results:
- TriviaQA: 71.2% zero-shot, 68.0% one-shot, 73.2% few-shot (approaching SOTA)
- LAMBADA: 86.4% zero-shot (GPT-2 XL was 75.1%)
- SuperGLUE: 72.5% zero-shot, 75.0% one-shot, 82.8% few-shot
- Arithmetic: 100% accuracy on 2-digit addition (two-shot), 80% on 5-digit (two-shot)
- News article generation: humans could distinguish GPT-3 from human-written text only 52% of the time
- Weaknesses: Poor at commonsense reasoning (ParaRel 48.5%), struggles with multi-step reasoning, inconsistent factual accuracy, repetition issues

Limitations exposed:
- Sample inefficiency: 175B parameters trained on 570GB still failed at basic logical reasoning
- Lack of factual grounding: no mechanism to distinguish truth from plausible text
- No ability to learn from feedback or correct mistakes within a conversation
- Prompt engineering became critical β€” small prompt changes could swing accuracy by 20+ points


GPT-3.5 / ChatGPT (2022) β€” RLHF and Conversational AI

GPT-3.5 was not a single model but a family built on GPT-3 with code training and RLHF (Reinforcement Learning from Human Feedback). The most famous member is text-davinci-003 (InstructGPT) and its chat variant gpt-3.5-turbo (ChatGPT).

Codex (August 2021) β€” The foundation:
- GPT-3 fine-tuned on 159GB of GitHub code (from 54M public repositories)
- Codex-davinci-002: 12B parameters, trained with log1000 token sampling
- Achieved 28.8% pass@1 and 72.3% pass@100 on HumanEval (Python function completion)
- This code training dramatically improved logical reasoning and instruction-following
- Demonstrated that code is a uniquely valuable training data source because it forces precise, formal reasoning

InstructGPT / text-davinci-003 (January 2022):
- Applied RLHF to GPT-3 to align model outputs with human preferences
- Three-step process:
1. Supervised Fine-Tuning (SFT): Human labelers wrote demonstration responses for diverse prompts; GPT-3 fine-tuned on 12,000+ demonstrations
2. Reward Model Training: Labelers ranked model outputs (4-9 responses per prompt); a 6.7B reward model trained to predict human preferences
3. PPO Reinforcement Learning: The SFT model was fine-tuned via Proximal Policy Optimization to maximize the reward model's score, with a KL penalty on the SFT model to prevent divergence
- 1.3B parameter InstructGPT was preferred over 175B GPT-3 70% of the time (showing alignment is more important than scale)

ChatGPT (November 30, 2022):
- GPT-3.5 fine-tuned specifically for conversational interaction
- Added a chat format with distinct roles: system, user, assistant
- Trained on conversations where human labelers played both user and assistant roles
- Reinforcement learning from human feedback applied at the conversation level
- Launched as a free research preview β€” reached 100M users in 2 months
- Caused a paradigm shift: chat became the primary UI for AI interaction

Key improvements over GPT-3:
- Dramatically better instruction following (fewer irrelevant tangents)
- Refusal capability: can decline inappropriate requests
- Reduced toxicity (by ~50% compared to GPT-3)
- Ability to ask clarifying questions
- Multi-turn conversation coherence
- Code understanding and generation


GPT-4 (March 2023) β€” Multimodal and More Capable

GPT-4 brought multimodal understanding (text + images) and dramatically improved reasoning, factuality, and safety.

Architecture (undisclosed, inferred from analysis):
- Estimated at 1.8T parameters across 120 layers with ~200B active parameters per inference
- Mixture-of-Experts (MoE) architecture with 16 experts (8 active per token)
- Uses a router network to select which experts process each token
- Each expert is roughly 111B parameters (feed-forward networks)
- Trained on ~13T tokens (text + code + images)
- Context window: 8,192 tokens (32,768 in extended version)
- Estimated training cost: $100M+ (based on GPU-hours analysis)

Multimodal capabilities:
- Vision encoder processes images and outputs a sequence of visual tokens
- These visual tokens are interleaved with text tokens in the transformer's input
- Can understand images, diagrams, screenshots, and handwritten text
- Does not generate images (unlike Gemini or GPT-4o later)
- Performance on MMMU (Multimodal Understanding): 75.0% (vs human expert ~90%)

Performance highlights:
- Bar exam (Uniform Bar Exam): scored in the 90th percentile (GPT-3.5 was in the 10th)
- SAT: scored 1410/1600 (710 Evidence-Based Reading & Writing, 700 Math)
- AP exams: scored 5 on AP Chemistry, AP Biology, AP Psychology, AP US History, AP Art History
- MMLU (Massive Multitask Language Understanding): 86.4% (GPT-3.5: 70.0%)
- HumanEval (Python coding): 67.0% pass@1
- TruthfulQA: 70% (GPT-3.5: 44%)
- Known hallucination weakness: still "hallucinated" facts 15-30% of the time in adversarial evaluation

Safety improvements:
- 6-month safety preparation after training completion
- RLHF fine-tuning with safety-focused reward models
- Refusal rate on toxic prompts: 95%+ (vs GPT-3.5's 80%)
- Implemented "rule-based reward signals" for safety-critical categories
- External red-teaming by ~50 experts from diverse fields


GPT-4 Turbo (November 2023) and GPT-4o (May 2024)

GPT-4 Turbo:
- Updated knowledge cutoff: April 2024 (from September 2021)
- 128K context window (16Γ— larger than GPT-4)
- 50% cheaper than GPT-4
- Improved instruction following
- JSON mode and function calling improvements
- Reproducible outputs (seed parameter)

GPT-4o ("omni", May 2024):
- Truly multimodal: natively processes text, images, audio in any combination
- Single end-to-end model (no longer separate voice/vision pipelines)
- Audio understanding: 232 tokens per second processing
- Voice mode with emotional expression, laughter, singing
- Real-time conversational latency (~320ms)
- Vision: can process images, video frames (camera input), screenshots
- MMLU: 88.7%
- Math (MATH benchmark): 76.6%
- Coding (HumanEval): 90.2% pass@1
- 2Γ— faster inference than GPT-4, 50% cheaper
- Multilingual improvements: significant gains in non-English languages
- Modalities: text, image, audio input; text, audio output (not video generation)

Architecture inferences:
- Still MoE-based, potentially with more experts and finer-grained routing
- Unified multimodal encoder rather than separate encoders + projector
- Cross-attention mechanisms for integrating audio features
- Tremendous optimization for inference latency (likely speculative decoding + KV-cache optimization)


o1 (September 2024) β€” Reasoning Model

The o1 model (previously "Strawberry") introduced a new paradigm: chain-of-thought reasoning at inference time. Instead of generating the final answer immediately, the model produces a long chain of reasoning tokens internally before the visible response.

Key innovation β€” "Thinking" tokens:
- The model is trained to generate internal reasoning chains (hidden from the user) before producing the final answer
- This allows the model to "think" for longer on hard problems
- Thinking tokens are not visible in the API response β€” they're consumed internally
- The model learns when to reason deeply vs when to respond quickly (though this is not user-controllable in v1)
- Training used reinforcement learning to learn when to reason rather than just what to reason

Performance (o1-preview):
- AIME (math competition): 56.1% (GPT-4o: 9.4%, 6Γ— improvement)
- International Mathematical Olympiad (IMO) qualifier: 83.3%
- PhD-level Physics (GPQA-diamond): 73.0% (GPT-4o: 53.4%)
- Codeforces: rating 1671 (top 11% of human competitors)
- MMLU: 91.8%
- MATH-500: 94.8% (near-perfect)
- Limitations: much slower (seconds per query vs milliseconds), more expensive (~3Γ— GPT-4o), not good at creative writing

o1-mini (smaller, cheaper):
- 82% of o1-preview's math performance at 80% lower cost
- Specifically optimized for STEM reasoning (coding, math, science)
- Less capable at general world knowledge


o3 (December 2024) β€” Next-Generation Reasoning

o3 dramatically advanced the reasoning paradigm from o1.

Key improvements:
- More compute allocation for thinking: can "think" for significantly longer on each problem
- Adaptive reasoning: adjusts thinking depth based on problem difficulty (o1 had uniform thinking time)
- o3-low and o3-high compute settings for controllable reasoning effort
- Better integration of tool use during reasoning (code execution, browsing)
- Record-breaking performance:

Benchmark o3 o1 GPT-4o
ARC-AGI (visual reasoning) 87.5% ~30% ~30%
AIME 2024 (math) 96.7% 56.1% 9.4%
GPQA Diamond (PhD science) 87.7% 73.0% 53.4%
SWE-bench (coding) 71.7% 48.9% ~30%
FrontierMath >25% ~2% ~2%
  • ARC-AGI breakthrough: The 87.5% on ARC (Abstraction and Reasoning Corpus) was a landmark β€” no previous AI system had surpassed even 55% on this benchmark designed to be AI-complete
  • o3 uses "natural language chain-of-thought" rather than implicit computation β€” this means its reasoning is (potentially) interpretable and verifiable

GPT-4.1 (April 2025) β€” The Latest Frontier

GPT-4.1 is OpenAI's most recent GPT-branded model, representing a refinement of the entire GPT lineage.

Key features:
- 1M token context window (matching Gemini 1.5)
- Improved factual accuracy with reduced hallucination (estimated 40% reduction vs GPT-4)
- Better multilingual performance, especially on lower-resource languages
- Improved instruction adherence for complex, multi-step tasks
- MMLU: 92.5% (estimated)
- Native tool use with structured outputs
- Pricing: input $2/M, output $8/M (1/10 the price of GPT-4)
- Three variants: GPT-4.1 (full), GPT-4.1-mini, GPT-4.1-nano

Architecture (speculative):
- Likely a refined MoE architecture with improved routing
- KV-cache compression for handling 1M context
- Speculative decoding for faster inference
- Potential integration of reasoning capabilities into the base model


Llama Family (Meta)

Llama 1 (February 2023) β€” Open Foundation Model

Llama (Large Language Model Meta AI) was Meta's entry into the LLM space, notable for being open-weight and demonstrating that smaller models trained on more data could match larger models.

Philosophy: "Chinchilla-optimal" training β€” for a given compute budget, train smaller models on more tokens rather than larger models on fewer tokens.

Model Parameters Training Tokens Context Length
Llama-7B 6.7B 1.0T 2048
Llama-13B 13.0B 1.0T 2048
Llama-33B 32.5B 1.4T 2048
Llama-65B 65.2B 1.4T 2048

Architecture (based on GPT-3 with modifications):
- Decoder-only Transformer with pre-norm (RMSNorm instead of LayerNorm)
- SwiGLU activation in feed-forward layers (instead of ReLU/GELU)
- Rotary Position Embeddings (RoPE) instead of learned absolute positions
- No bias terms in linear layers (saves parameters, improves numerical stability)
- AdamW optimizer with cosine learning rate schedule
- Vocabulary: 32,000 BPE tokens (SentencePiece tokenizer)
- Key difference from GPT-3: RoPE + SwiGLU + RMSNorm became the de facto standard architecture for subsequent models (including Llama 2/3, Mistral, Qwen)

Tokenizer (SentencePiece BPE):
- 32k vocabulary size
- BPE algorithm with byte fallback (unknown bytes β†’ UTF-8 bytes)
- Normalization: NFKC Unicode normalization
- Pre-tokenization: split on whitespace and punctuation
- No BOS/EOS tokens added during pre-training (added during fine-tuning for chat)

Training data:
- Mixture of web pages (CommonCrawl), books, Wikipedia, academic papers, code (GitHub)
- Data deduplication at line, document, and set level
- 1.4T tokens total for 65B model
- Trained on 2,048 A100 GPUs for 21 days (65B model)
- Training efficiency: ~380 tokens/GPU/second

Results:
- Llama-13B outperformed GPT-3 (175B) on most benchmarks
- Llama-65B was competitive with Chinchilla-70B and PaLM-540B
- MMLU: 47.4 (7B), 55.8 (13B), 63.4 (65B)
- This demonstrated that open-source models could compete with closed-source at the time


Llama 2 (July 2023) β€” Chat-Tuned Open Models

Llama 2 built on Llama 1 with improvements in training data, context length, and the addition of chat-optimized variants using RLHF.

Model Parameters Training Tokens Context Length
Llama-2-7B 6.7B 2.0T 4096
Llama-2-13B 13.0B 2.0T 4096
Llama-2-70B 69.8B 2.0T 4096

Architecture changes from Llama 1:
- Context length increased from 2048 to 4096 tokens (via RoPE extension)
- No architectural changes β€” same RMSNorm, SwiGLU, RoPE, no bias
- 40% more training data (2.0T tokens vs 1.0-1.4T)
- Higher quality data filtering and deduplication

Chat template β€” Llama's Contribution:

<s>[INST] <<SYS>>
{{ system_prompt }}
<</SYS>>

{{ user_message }} [/INST] {{ assistant_message }} </s>
  • Special tokens: <s> (BOS), </s> (EOS), [INST] (instruction start), [/INST] (instruction end), <<SYS>> (system prompt markers)
  • Multi-turn format:
<s>[INST] First user message [/INST] First response </s>
<s>[INST] Second user message [/INST] Second response </s>
  • This chat template became widely adopted in the open-source ecosystem
  • The system prompt is optional β€” if omitted, the <<SYS>> block is not included

RLHF training:
- Step 1: Supervised fine-tuning on 27,540 human-written demonstrations
- Step 2: Training two reward models (one for helpfulness, one for safety), each 70B parameters
- Step 3: PPO reinforcement learning using the reward models
- Key innovation: Ghost attention β€” during RLHF, the model's attention to safety-critical tokens is modulated to prevent reward hacking

Safety fine-tuning:
- Safety-specific data: 135,000 examples focused on adversarial safety
- Context distillation: using a safe model to filter unsafe training data
- Safety reward model trained to prefer helpful but safe responses
- Llama-2-chat models significantly safer than Llama 1 (measured on toxicity benchmarks)

Performance:
- Llama-2-70B: MMLU 68.9%, GSM-8K 56.8%, HumanEval 29.9%
- Comparable to GPT-3.5 on many benchmarks
- Slightly behind GPT-4 and Claude 2 at the time

License:
- Llama 2 released under a custom commercial license
- Free for commercial use up to 700M MAUs (monthly active users)
- Requires attribution and safety best practices
- This was a major step for open-weight model accessibility


Llama 3 (April 2024) β€” New Tokenizer and Massive Scale

Llama 3 introduced a completely new tokenizer, scaled to 8B and 70B sizes, with dramatically improved performance.

Model sizes:

Model Parameters Layers d_model Heads KV Heads Training Tokens
Llama-3-8B 8.03B 32 4096 32 8 (GQA) 15T+
Llama-3-70B 70.6B 80 8192 64 8 (GQA) 15T+

New tokenizer (tiktoken-based):
- Vocabulary: 128,256 tokens (massive increase from Llama 2's 32k)
- Based on OpenAI's tiktoken library (GPT-4's tokenizer)
- Byte-level BPE (like GPT-4's cl100k_base)
- Key differences from GPT-4's tokenizer:
- Larger vocabulary for better multilingual coverage
- Additional tokens for code (single-digit numbers, common code patterns)
- No special regex pre-tokenization rules (simpler)
- Improved compression ratio:
- English: ~3.5 chars/token (vs ~4.0 for Llama 2)
- Code: significant improvement for whitespace-sensitive languages (Python, YAML)
- Multilingual: 2-3Γ— better than Llama 2 for non-European languages
- Special tokens: <|begin_of_text|>, <|end_of_text|>, <|start_header_id|>, <|end_header_id|>

Architecture changes from Llama 2:
- Grouped-Query Attention (GQA) with 8 KV heads (instead of full multi-head attention)
- 8B: 32 query heads, 8 KV heads (4:1 ratio)
- 70B: 64 query heads, 8 KV heads (8:1 ratio)
- Significantly faster inference, especially for long contexts
- 128k vocabulary (massive increase)
- Training on 15T+ tokens (7.5Γ— more than Llama 2)
- Context length: 8192 tokens

GQA details:
- Standard multi-head attention: each head has its own K, V, Q projections
- Multi-query attention (MQA): all heads share one K,V β€” extreme but quality loss
- GQA: intermediate β€” heads are grouped, each group shares K,V
- Llama 3 8B: 32 Q heads, 8 KV heads β†’ 4 queries per K,V group
- Llama 3 70B: 64 Q heads, 8 KV heads β†’ 8 queries per K,V group
- Benefit: reduces KV cache size by 4-8Γ—, critical for long-context inference

Training:
- 15T+ tokens from web data, code, books, academic sources
- Data filtering with a classifier trained on high-quality references
- 80K A100-80GB GPUs (Meta's in-house cluster)
- Training duration: months
- No RLHF for base models (chat models used SFT + RLHF)

Performance:
- Llama-3-70B: MMLU 82.0%, HumanEval 81.7%, GSM-8K 93.0%
- Matched or exceeded GPT-4 on many benchmarks
- Became the de facto standard for open-weight models


Llama 3.1 (July 2024) β€” 405B Frontier Model

Llama 3.1 introduced the 405B parameter model, the largest open-weight model ever released, along with 8B and 70B updates.

Model Parameters Context Length Training Tokens
Llama-3.1-8B 8.0B 128K 15T+
Llama-3.1-70B 70.6B 128K 15T+
Llama-3.1-405B 405B 128K 15T+

Key innovations:
- 128K context length through improved RoPE scaling (YaRN or similar)
- LLaMA 3.1 405B uses a dense transformer (not MoE) β€” surprising at this scale
- 126 layers, 16,384 hidden dimension, 128 heads, 8 KV heads
- ~16T training tokens, 30 million GPU-hours (H100-80GB) to train
- Trained on 16,000 H100 GPUs (Meta's cluster)
- All models share the same tokenizer (128,256 vocab)
-
Model parallelism:** 405B trained with tensor parallelism (TP=8), pipeline parallelism (PP=16), and data parallelism (DP=64)

Architecture of 405B:
- Decoder-only with GQA (8 KV heads, 128 query heads β†’ 16:1 ratio)
- 126 layers (vs 80 for 70B)
- d_model = 16,384 (vs 8,192 for 70B)
- FFN intermediate dimension: 53,248 (4Γ— d_model with SwiGLU reduces to ~2.7Γ— activation)
- Vocabulary: 128,256
- Context: 128K (trained with progressive extension from 8K β†’ 128K)
- No MoE β€” Meta explicitly chose dense architecture for reliability and ease of use

Training recipe:
- Pre-training on 15.6T tokens
- Long-context pre-training: continued training on 800B tokens with 128K context
- SFT on synthetic data generated by Llama 3.1-405B itself (self-improvement loop)
- DPO (Direct Preference Optimization) instead of PPO for alignment
- Model-based data filtering β€” using a classifier model trained on human preferences

Performance:
- Llama-3.1-405B: MMLU 87.3%, HumanEval 89.0%, GSM-8K 96.8%
- Matched GPT-4 on most benchmarks
- Slightly behind GPT-4 Turbo on some reasoning tasks
- Became the strongest open-weight model available

License:
- "Llama 3.1 Community License" β€” permissive for most uses
- Free for commercial use with no MAU cap (unlike Llama 2)
- Requires attribution for derivatives
- Available via download and cloud APIs


Llama 4 (April 2025) β€” MoE and Multimodal

Llama 4 represents a major architectural shift: Mixture-of-Experts and multimodal capabilities.

Model variants:

Model Total Parameters Active Parameters Experts Vision
Llama-4-Scout 109B 17B 16 MoE Yes
Llama-4-Maverick 401B 48B 16 MoE Yes

Architecture changes:
- First Meta model to use MoE (Mixture-of-Experts) architecture
- 16 experts with top-2 routing (2 experts active per token)
- Grouped-Query Attention retained
- Context length: Scout supports 10M tokens (with pruning), Maverick supports 1M tokens
- Multimodal: native vision encoder + adapter to language model
- Trained on 30T+ tokens

Llama 4 Scout (109B total, 17B active):
- Optimized for deployment β€” 17B active parameters fits on a single H100 node
- 16 MoE layers, ~6.8B parameters per expert (in FFN layers only)
- Attention layers are dense (not MoE) β€” only FFNs are expertized
- Context: 10M tokens via iRoPE (interspersed RoPE) β€” a novel position encoding for ultra-long contexts
- Designed for retrieval-augmented generation and long-document tasks

Llama 4 Maverick (401B total, 48B active):
- Frontier model for complex reasoning
- 16 MoE layers with fine-grained expert allocation
- Top-2 routing with load balancing loss
- 48B active parameters per token
- Performance competitive with GPT-4o on multimodal benchmarks

Multimodal integration:
- Vision encoder: modified ViT (Vision Transformer) with 700M parameters
- Any resolution support β€” input images are sliced into sub-images, each encoded separately
- Cross-attention layers connect vision features to the language model
- Text + image interleaved input supported
- Training: two-stage β€” (1) vision-language alignment, (2) full fine-tuning

iRoPE (interleaved RoPE):
- Standard RoPE is applied to every token position
- iRoPE applies RoPE to every other token, effectively doubling the max position range
- Combined with context pruning and attention sparsity for 10M token support
- Trade-off: slightly reduced precision at very long ranges


Claude Family (Anthropic)

Claude 1 (March 2023) β€” HH-RLHF

Claude 1 was Anthropic's first commercial model, built on their research into Constitutional AI and Harmlessness training.

Architecture (inferred):
- Decoder-only Transformer, estimated ~52B parameters
- Context window: 8,000 tokens
- Based on Anthropic's prior research with scaling laws and model sizes (52B was their "standard" size from 2022 research)
- Trained on a large corpus of text, with emphasis on helpfulness and harmlessness

HH-RLHF (Helpful, Honest, Harmless):
- The core philosophy: models should be trained to be helpful while actively avoiding harm
- Constitutional AI (CAI): Instead of relying solely on human feedback for harmlessness, use a "constitution" of principles to generate self-critique and revision
1. Model generates a response
2. Model critiques its own response based on constitutional principles
3. Model revises the response based on the critique
4. This creates a dataset of "revised" responses
5. The model is fine-tuned on this dataset (supervised learning)
- RLHF with harmlessness reward: A separate harmlessness reward model trained on human feedback about harmful outputs
- Gentle cascade: Multiple rounds of CAI + RLHF refinement

Claude's personality:
- Deliberately trained to be cautious β€” would often refuse borderline requests
- Known for being "boringly safe" β€” preferred to decline than to risk harm
- Trained to express uncertainty and acknowledge limitations
- This cautious approach was both praised (for safety) and criticized (for being too restrictive)

Performance:
- Competitive with GPT-3.5 on many benchmarks
- Superior performance on ethical reasoning and safety evaluations
- Less capable than GPT-4 on complex reasoning


Claude 2 (July 2023) β€” Longer Context and Better Reasoning

Improvements over Claude 1:
- Context window: 100K tokens (major advancement at the time)
- Better coding performance (scored 71.2% on Codex HumanEval)
- Improved factual accuracy
- Reduced refusal rate for borderline queries
- Better at creative writing

Architecture:
- Still decoder-only, estimated larger than Claude 1 (possibly ~80-100B parameters)
- Optimized for long-context inference
- Used a combination of attention mechanisms for linear-scaling long context

Performance highlights:
- GRE reading comprehension: 90th percentile
- GMAT critical reasoning: 89th percentile
- Bar exam: scores varied, but generally above average
- Weakness: math reasoning (GSM-8K ~66%, significantly below GPT-4)


Claude 3 (March 2024) β€” Haiku, Sonnet, Opus

Claude 3 introduced a three-tier model family: Haiku (fast/cheap), Sonnet (balanced), Opus (most capable).

Model Description Speed Context Price (input/output per M tokens)
Claude 3 Haiku Fast, cheap, compact Fastest 200K $0.25/$1.25
Claude 3 Sonnet Balanced performance Fast 200K $3/$15
Claude 3 Opus Most capable Moderate 200K $15/$75

Common architecture features:
- 200K token context window (standard across all tiers)
- Improved multilingual support
- Reduced refusal rates (more nuanced understanding of harmful vs harmless)
- JSON mode and tool use support
- Vision capabilities (image understanding)
- Not multimodal generation (text output only)

Claude 3 Opus performance:
- MMLU: 86.8% (undergraduate knowledge)
- GSM-8K: 95.0% (grade school math)
- HumanEval: 84.9% (coding)
- MATH: 73.7%
- Near-perfect recall in long-context evaluations (Needle-in-a-Haystack)
- Comparable to GPT-4 on most benchmarks, superior on some

Claude 3 Haiku performance:
- Designed for 200ms latency responses
- MMLU: 75.9%
- Suitable for simple queries, classification, extraction
- 3Γ— cheaper than Sonnet

Vision (all Claude 3 models):
- Image understanding: photos, diagrams, charts, infographics
- Document understanding: PDF, handwritten text
- No image generation


Claude 3.5 (June 2024, October 2024)

Claude 3.5 Sonnet (June 2024):
- Major improvement over Claude 3 Sonnet, ~matching Claude 3 Opus
- MMLU: 88.7%
- HumanEval: 92.0% (best in class for coding at release)
- GSM-8K: 96.8%
- MATH: 78.3%
- Key differentiator: exceptional at coding and tool use
- 2Γ— faster than Claude 3 Opus
- Artifacts feature: generates interactive content (web pages, SVG, diagrams) in a side panel

Claude 3.5 Haiku (October 2024):
- Fastest model in the Claude family
- Matched Claude 3 Opus performance on coding benchmarks
- Improved vision capabilities
- MMLU: 88.3% (nearly matching Sonnet)
- Coding (HumanEval): 91.6%
- Price: $0.80/$4 per M tokens


Claude 4 (April 2025) β€” Opus, Sonnet, Haiku

Claude 4 represents the latest generation, with significant improvements in reasoning, coding, and tool use.

Claude 4 Opus (flagship):
- MMLU: 89.3%
- MATH: 86.5%
- HumanEval: 93.7%
- Context: 200K tokens (standard)
- Extended thinking mode (similar to o-series, but implemented differently)
- Superior multilingual performance
- Computer use: can control a computer interface (GUI automation)
- Tool use with thousands of parallel tool calls
- Pricing: $15/$75 per M tokens

Claude 4 Sonnet (balanced):
- MMLU: 88.5%
- 2Γ— faster than Opus
- Coding: 90.1% HumanEval
- $3/$15 per M tokens

Claude 4 Haiku (fastest):
- MMLU: 85.2%
- 300ms average latency
- $0.80/$4 per M tokens

Extended thinking (Claude 4 Opus):
- The model can choose to enter an "extended thinking" mode for hard problems
- Unlike o1's hidden thinking tokens, Claude's thinking is transparent β€” the reasoning is shown to the user
- This allows verification of the model's reasoning process
- The mode is optional and controlled by the API


Gemini Family (Google DeepMind)

Gemini 1.0 (December 2023) β€” Natively Multimodal

Gemini was Google's first truly multimodal model, trained from scratch on text, images, audio, and video together.

Three tiers:

Model Use Case Parameters (est.) Context
Gemini Ultra Most capable ~1.5T (MoE) 32K
Gemini Pro Balanced ~300B (MoE) 32K
Gemini Nano On-device ~1.8B / ~3.25B 8K

Natively multimodal training:
- Trained jointly on text, images, audio, video, and code
- Unlike GPT-4V (text-only model + vision encoder added later), Gemini was trained from scratch on all modalities
- This is a key architectural difference: the same model parameters process all modalities
- Modalities at training time: interleaved text + image sequences, audio waveforms, video frames + timestamps, code execution traces, PDFs, web pages

Architecture:
- MoE-based decoders for Ultra and Pro, dense for Nano
- Multi-Query Attention (MQA) for efficient inference
- Ultrasound tokenization: Speech encoded into tokens directly (not via external ASR)
- Video processed as frame sequences (16 FPS sampled to 1 FPS at inference)
- 32K context window (relatively small by 2024 standards)

Performance (Gemini Ultra):
- MMLU: 90.04% (first model to exceed 90%)
- HumanEval: 74.4% (worse than GPT-4 at the time, but improved later)
- MATH: 53.2% (GPT-4 was 52.9%)
- HellaSwag: 89.7%
- Multilingual MMLU: SOTA on 20+ languages
- Video understanding: 62.4% on VATEX (video captioning)
- Audio understanding: state-of-the-art on speech recognition benchmarks

Gemini Nano:
- Two versions: Nano-1 (1.8B) and Nano-2 (3.25B)
- Architecture: decoder-only with GQA, 4-bit quantizable
- Intended for on-device applications (Pixel phones)
- Can run fully offline


Gemini 1.5 Pro/Flash (February 2024) β€” 1M Context

Gemini 1.5 was a paradigm shift in context length, introducing the first production model with a 1M-token context window.

Key innovation β€” MoE with 1M context*:
- The 1M context was achieved through a
*Mixture-of-Experts
architecture with a novel attention mechanism
- Infini-Attention: Compressive memory mechanism that combines linear attention with delta rule
- Standard attention processes each new token against all previous keys
- Infini-Attention compresses historical context into a fixed-size memory
- The model can attend to both the compressed memory and local context
- This enables theoretically infinite context in a computationally tractable way
- 1M tokens reliably processed with ~99.7% "needle-in-haystack" recall

Architecture:
- Gemini 1.5 Pro: estimated ~1.5T parameters (MoE)
- Gemini 1.5 Flash: distilled version, optimized for speed
- Both use MoE with the same MoE architecture (different depths/widths)
- Learned position embeddings β€” extended via linear interpolation for long context

Training:
- Trained on "virtually all text, code, images, video, and audio available via web-scale data collection"
- MoE training with load balancing
- Distillation from Pro β†’ Flash

Perf comparison:

Model MMLU HumanEval GSM-8K MATH
1.5 Pro 87.2% 84.1% 92.3% 67.7%
1.5 Flash 79.6% 72.0% 86.9% 56.8%

1M context use cases:
- Analyze entire codebases
- Process hour-long video with full audio
- Long-document analysis (hundreds of pages)
- Multi-document comparison and synthesis
- Audio context: up to 11 hours of audio (via Gemini's direct audio processing)


Gemini 2.0 Flash (December 2024)

Key capabilities:
- Multimodal output (text + images + audio)
- Native tool use (Google Search, Maps, code execution)
- Agentic capabilities: can autonomously perform multi-step tasks
- 1M context window (same as 1.5)
- Lower latency than 1.5 Pro
- Space-time attention: new attention mechanism for video understanding

Performance:
- MMLU: 90.1%
- HumanEval: 86.5%
- GSM-8K: 95.2%
- Video understanding: significantly improved temporal reasoning

Multimodal generation:
- Can generate images (text β†’ image)
- Can generate audio (text β†’ speech, music)
- Unified model β€” one set of parameters for all modalities
- Image generation uses latent diffusion-style approach integrated with the transformer


Gemini 2.5 Pro/Flash (March 2025)

Gemini 2.5 represents the current state-of-the-art from Google DeepMind.

Gemini 2.5 Pro:
- Thinking mode: like o-series and Claude 4's extended thinking
- MMLU: 91.8%
- MATH: 97.0% (near-perfect)
- Coding (Aider polyglot): top SOTA
- 1M context standard, 2M context available (for Pro)
- Native multimodal input/output
- Agentic: can create and execute plans, use tools autonomously

Gemini 2.5 Flash:
- Distilled from Pro
- Lower latency, higher throughput
- Thinking mode available
- MMLU: 88.5%
- Price: $0.15/$0.60 per M tokens (extremely competitive)

Architecture improvements (speculative):
- Refined MoE routing for higher active parameter utilization
- Improved KV-cache management for 2M context
- Hybrid attention: local + global sparse + full attention in different layers


DeepSeek Family (DeepSeek)

DeepSeek (January 2024) β€” Foundational Model

DeepSeek is a Chinese AI lab that has produced some of the most innovative and cost-effective models.

DeepSeek LLM (67B, January 2024):
- Dense decoder-only transformer
- 67B parameters, 95 layers, 8KV heads (GQA)
- Trained on 2T tokens
- MMLU: 71.6%

Key characteristics:
- Focus on training efficiency and cost reduction
- Released open-weight under permissive license
- Strong multilingual performance (Chinese + English)


DeepSeek-Coder (January 2024)

DeepSeek-Coder family:
- Base model: DeepSeek-LLM (67B) continued pre-training on code
- Multiple sizes: 1.3B, 6.7B, 33B, 67B
- Trained on 2T tokens of code and natural language (87% code, 13% natural language)
- Fill-in-the-Middle (FIM) training objective
- Repository-level data construction

Key innovation β€” FIM (Fill-in-the-Middle)****:
- Standard autoregressive: predict the next token left-to-right
- FIM: code is rearranged into (prefix, middle, suffix) and the model learns to predict middle given prefix and suffix
- This teaches the model bidirectional code understanding while maintaining causal LM training
- DeepSeek-Coder uses a specific FIM format: <fim_prefix>, <fim_suffix>, <fim_middle> tokens

Performance:
- DeepSeek-Coder-33B: HumanEval 72.6% pass@1
- DeepSeek-Coder-67B: HumanEval 78.7% pass@1 (competitive with Codex-175B)


DeepSeek-V2 (May 2024) β€” Multi-Head Latent Attention (MLA)

DeepSeek-V2 introduced Multi-Head Latent Attention (MLA) , a groundbreaking architectural innovation for inference efficiency.

Vsion: 236B total, 21B active per token (MoE).

MLA β€” Multi-Head Latent Attention:
- Problem: Standard MHA/GQA requires storing large KV caches, which is memory-bound during inference. GQA reduces this but at quality cost.
- Solution: Instead of storing full K,V vectors per head, project them into a low-dimensional latent space and store that instead.
- Standard: K = W_K * x (d_head per head per layer)
- MLA: k_latent = W_down * x (compressed), store k_latent, reconstruct K = W_up * k_latent
- Compression ratio: d_latent << d_head, typically 4-8Γ— compression
- KV cache reduction: For a 128K context, KV cache size drops from ~2.4GB (standard) to ~300MB (MLA) per layer
- Quality preservation: The latent space captures all information needed; reconstruction adds negligible overhead
- Training efficiency: The latent projections are learned end-to-end β€” no distillation needed

Why MLA matters:
- The KV cache is the primary bottleneck for long-context inference
- With MLA, DeepSeek-V2 can serve 128K context with the same memory as 16K context with standard attention
- This directly enables affordable long-context deployment

DeepSeek-V2 Architecture:
- 236B total parameters, 21B active (MoE with 8 experts, top-2 routing)
- 128K context window
- MLA + MoE combined
- Trained on 8.1T tokens
- MMLU: 79.7% (competitive with Llama-3-70B at a fraction of the active parameters)

DeepSeek-V2.5 (September 2024):
- Improved version of V2
- MMLU: 80.5%
- Better code and math performance


DeepSeek-V3 (December 2024) β€” 671B MoE Frontier

DeepSeek-V3 scaled the V2 architecture to 671B total parameters with 37B active, trained on 14.8T tokens.

Architecture:
- 671B total parameters, 37B active per token
- MoE with 256 experts, 8 active per token (fine-grained MoE)
- MLA for attention (retained from V2)
- Multi-token prediction (MTP) β€” predict 1-3 future tokens at each position
- Multi-head Latent Attention: shares KV across MoE experts for efficiency
- DeepSeekMoE architecture: fine-grained experts (more, smaller experts) with shared experts

Training efficiency:
- Trained on 14.8T tokens using 2,048 NVIDIA H800 GPUs
- Total training cost: ~$5.576M (significantly less than comparable models)
- 278K GPU-hours (H800-equivalent)
- Key to cost efficiency:
1. MLA reduces KV cache memory, allowing larger batch sizes
2. Fine-grained MoE with load balancing reduces communication overhead
3. FP8 mixed precision training
4. Efficient parallelism strategy (expert parallelism + data parallelism)

Performance:
- MMLU: 89.5%
- HumanEval: 92.5%
- GSM-8K: 96.8%
- MATH: 83.9%
- Competitive with GPT-4o on most benchmarks
- Notably: LLaMA 3.1-405B is slightly better on some tasks, but V3 uses 10Γ— fewer active parameters
- Efficiency frontier: Best performance-to-compute ratio of any model at release

Multi-Token Prediction (MTP):
- Standard LM: predict next token P(t_{i+1} | t_1..t_i)
- MTP: predict n future tokens simultaneously P(t_{i+1},...,t_{i+n} | t_1..t_i)
- Uses independent output heads for each future position
- Training objective: sum of cross-entropy losses for all n predicted tokens
- At inference: only the immediate next token prediction is used (or can be used for speculative decoding)
- Benefit: forces the model to learn longer-range dependencies and more robust representations


DeepSeek-R1 (January 2025) β€” Reasoning Through RL

DeepSeek-R1 is a pure reinforcement learning approach to reasoning, inspired by the o1 paradigm but open-source and using novel training techniques.

R1-Zero β€” Pure RL:
- Base model: DeepSeek-V3-Base
- Training: GRPO (Group Relative Policy Optimization), no supervised data at all
- The model learns to "think" through trial and error β€” chain-of-thought emerges spontaneously
- Reward signal: correctness of final answer (verifiable tasks like math, code)
- Spontaneous reasoning behaviors:
- Self-verification: "Let me check my answer..."
- Backtracking: "Wait, that's wrong. Let me reconsider..."
- Intermediate goal setting: "First, let me compute X, then Y..."
- Multiple solution attempts within a single response
- Problems with R1-Zero: outputs are hard to read (mixing languages, formatting issues), "aha moments" are unpredictable

R1 β€” RL + Cold Start:
1. Cold-start SFT: Fine-tune on a small set (~10,000) of high-quality chain-of-thought demonstrations (written by humans or distilled from a frontier model)
2. Reasoning RL: Apply GRPO to the cold-start model, focusing on math, code, and science reasoning tasks
3. Rejection sampling: Generate many solutions, keep only the correct ones, fine-tune on them
4. General RL: Continue RL training on general tasks (writing, QA, summarization) using preference rewards

R1 distilled versions:
- DeepSeek released distilled versions: 1.5B, 7B, 8B, 14B, 32B, 70B (based on Qwen/Llama)
- Distillation: use R1's outputs to train smaller models (SFT on R1-generated chain-of-thought)
- DeepSeek-R1-Distill-Qwen-7B: MMLU 55.9%, but MATH 92.8% (GPT-4 level math!)
- This shows that reasoning ability can be efficiently distilled

R1 Performance:
- AIME 2024: 79.8% (o1-preview: 56.1%)
- MATH-500: 97.3% (o1-preview: 94.8%)
- MMLU: 90.8%
- HumanEval: 92.7%
- Cost: 3-5Γ— cheaper than o1 per query
- Limitations: slower (must generate reasoning chains), optimized for STEM


DeepSeek-Prover (February 2025) β€” Mathematical Proof

DeepSeek-Prover is specialized for formal mathematical proof in Lean and Isabelle.

Architecture:
- Based on DeepSeek-V3
- Fine-tuned on mathematical proof corpora
- Special reinforcement learning environment that interacts with proof assistants
- Reward is "proof accepted by the assistant" (verifiable)

Performance:
- MiniF2F (formal math benchmark): state-of-the-art, surpassing GPT-4
- Can generate complete proofs for competition math problems
- Novel capability: can search proof trees, backtrack, and explore different proof strategies


Mistral Family (Mistral AI)

Mistral 7B (September 2023) β€” Sliding Window Attention

Mistral 7B demonstrated that a well-architected 7B model could outperform larger models like Llama 2 13B.

Architecture:
- Decoder-only, 7.3B parameters
- Sliding Window Attention (SWA):
- Each layer's self-attention has a fixed-size window (W = 4096 tokens)
- Each token can attend to at most W previous tokens
- With L layers, the effective receptive field is W Γ— L tokens (theoretically, information can propagate through layers)
- For Mistral 7B (32 layers, W=4096): effective field 128K tokens
- This is NOT the same as "dilated attention" β€” each layer attends to its own local window, but deeper layers "see" information from earlier tokens through the hidden states
- Benefit: O(L Γ— WΒ²) instead of O(L Γ— NΒ²) β€” linear in sequence length
- Trade-off: actual modeling of long-range dependencies is weaker than full attention
- GQA with 8 KV heads (same as Llama 3 later adopted)
- RoPE, SwiGLU, RMSNorm (standard for this generation)
- Vocabulary: 32,000 tokens (SentencePiece BPE, same as Llama 1/2)

Rolling Buffer KV Cache:
- Fixed-size KV cache of size W (4096 tokens)
- Old tokens are evicted as new tokens arrive
- Cache is a fixed-size ring buffer
- This enables deterministic memory usage regardless of sequence length

Pre-fill and Chunking:
- During pre-fill (processing initial prompt), the model processes the prompt in chunks
- Each chunk computes attention within the chunk and against the KV cache
- For prompts longer than W, chunks are processed sequentially

Performance:
- Mistral-7B: MMLU 64.2% (vs Llama-2-13B 54.8%, Llama-1-34B 62.2%)
- Outperformed Llama-2-13B on most benchmarks
- Comparable to Llama-1-34B
- Demonstrated that architectural quality can overcome parameter count


Mixtral 8x7B (December 2023) β€” Sparse MoE

Mixtral 8x7B was the first open-source MoE model that matched or exceeded GPT-3.5 performance.

Architecture:
- 46.7B total parameters, 12.9B active per token
- 8 feed-forward experts, top-2 routing
- Every layer's FFN is replaced by an MoE with 8 experts
- Self-attention, LayerNorm, and embeddings remain dense (shared across experts)
- Each expert is approximately the size of a Mistral 7B FFN (~5.6B parameters per expert)
- Total: 8 Γ— ~5.6B = ~45B in MoE + ~1.7B in dense components

Routing:
- Router is a learned linear layer that outputs logits for each expert
- Top-2 experts are selected (by softmax probability)
- The outputs of the selected experts are weighted by their router probabilities
- Load balancing loss: auxiliary loss that encourages uniform routing across experts
- Capacity factor: each expert can process at most capacity = (tokens_per_batch / num_experts) Γ— capacity_factor tokens. If exceeded, tokens are dropped (passed as residual to next layer)
- Default capacity_factor: 1.25 (allows 25% overload)

Performance:
- MMLU: 70.6% (matched GPT-3.5)
- HumanEval: 40.2% (vs GPT-3.5 48.1%)
- GSM-8K: 58.4%
- Faster than Mistral 7B due to fewer active parameters in FFN
- Cost per token: similar to a 12.9B model, but total parameters 46.7B

MoE in MoE (MoeMoE):
- Mixtral 8x7B uses expert choice routing (routing selects experts, not tokens)
- All tokens in a batch are routed independently


Mixtral 8x22B (April 2024)

Scaled version:
- 141B total parameters, ~39B active
- 8 experts, top-2 routing
- Context: 65K tokens (extended via improved RoPE)
- MMLU: 76.8%

Improvements over 8x7B:
- Larger expert capacity (wider FFN)
- Better multilingual performance
- Improved reasoning (GSM-8K: 83.6%)
- Stronger code capability


Mistral Large (February 2024, updated November 2024)

Mistral Large is Mistral AI's flagship closed model, available via API.

Mistral Large 1 (2402):
- Proprietary architecture, estimated ~200B+ parameters
- 32K context window
- MMLU: 81.2%
- Multilingual: 5 languages (French, German, Spanish, Italian, English)
- Competitive with GPT-4 on some benchmarks

Mistral Large 2 (2411):
- Significantly improved
- MMLU: 84.0%
- 128K context window
- Improved multilingual (10+ languages)
- Better coding and math
- Agentic capabilities (function calling, structured output)


Specialized Mistral Models

Mistral Saba (February 2025):
- Specialized for Arabic and South Asian languages
- Based on Mistral architecture with modified tokenizer
- Optimized vocabulary for Arabic script, Hindi, Urdu, Tamil, etc.
- Demonstrates the importance of language-specific tokenization

Codestral (May 2024):
- 22B parameter model specialized for code
- 32K context window
- Fill-in-the-Middle training (like DeepSeek-Coder)
- Supports 80+ programming languages
- HumanEval: 71.4% pass@1 (competitive at the time)

Mathstral (July 2024):
- 7B model specialized for mathematical reasoning
- Fine-tuned from Mistral 7B with math-focused data
- MATH: 56.6% (impressive for 7B)
- Competition math performance was competitive with much larger models
- Demonstrates that strong math reasoning can be achieved at 7B scale with focused training


Qwen Family (Alibaba)

Qwen 1 (September 2023)

Qwen (from Alibaba's Qwen team) was designed as a bilingual (Chinese + English) foundation model.

Initial release:
- Sizes: 1.8B, 7B, 14B, 72B
- Architecture: decoder-only with standard components
- Context: 32K tokens (8K for 1.8B)
- Vocabulary: 152,000 tokens (high vocag for Chinese coverage)
- Training tokens: 3T for the 72B model

Key design decisions:
- Large vocabulary size (152K) to cover Chinese characters efficiently
- Chinese text is tokenized at ~1.5 characters per token (vs ~3 for English BPE models)
- Standard architecture: RoPE, SwiGLU, RMSNorm, bias=False


Qwen2 (June 2024) β€” Global-Query Attention

Qwen2 improved the architecture and scaled context length.

Architecture:
- Sizes: 0.5B, 1.5B, 7B, 32B, 72B
- Context: 32K (128K for 72B)
- GQA (Grouped-Query Attention) β€” standard now but important for Qwen
- SwiGLU, RoPE, RMSNorm
- Dual-chunk attention for long context: splits long sequences into chunks, computes attention within and between chunks
- Vocabulary: 151,936 (kept similar to Qwen1)
- Training tokens: 7T+ for 72B

Qwen2-Audio (July 2024):
- Speech understanding model
- Encoder: Whisper-style audio encoder (large-v2)
- Connector: Q-Former style cross-attention (like BLIP-2)
- Integrates audio features into Qwen2's language model
- Supports: speech recognition, audio event detection, music understanding
- Can process audio + text interleaved input


Qwen2.5 (September 2024) β€” Strong Generalist

Qwen2.5 is a comprehensive upgrade across all capabilities.

Model sizes:
- Dense: 0.5B, 1.5B, 3B, 7B, 14B, 32B, 72B
- MoE: Qwen2.5-MoE (14B active, ~40B total)

Key improvements:
- Significant boost in coding and math performance
- Better instruction following
- Improved multilingual (29+ languages)
- Context: 32K (128K for 72B)
- MMLU: 79.1% (7B), 86.7% (72B)
- HumanEval: 88.5% (72B)
- GSM-8K: 95.8% (72B)

Qwen2.5-Coder (November 2024):
- Specialized code models: 0.5B, 1.5B, 7B, 32B
- Trained on 5.5T code tokens
- FIM training (like DeepSeek-Coder)
- HumanEval: 92.5% (32B) β€” best-in-class for open code models at release
- Supports 92 programming languages

Qwen2.5-Math (November 2024):
- Math-specialized: 1.5B, 7B, 72B
- GRPO training (like DeepSeek-R1 but earlier)
- MATH: 94.5% (72B)
- Uses Chain-of-Thought + Tool-Integrated Reasoning (Python REPL)

Qwen2-VL (December 2024):
- Vision-language model
- Vision encoder: 604M parameter Vision Transformer
- Dynamic resolution: supports 768Γ—768 to 1536Γ—1536 images
- Connector: cross-attention for vision-to-text alignment
- Video understanding: supports up to 10-minute videos
- Requires only 1.5B additional parameters over base language model


QwQ (December 2024) β€” Reasoning Through Qwen

QwQ (Qwen with Questions) is Alibaba's reasoning model, similar to o1/R1.

Architecture:
- Based on Qwen2.5-72B
- Extended thinking with chain-of-thought
- Self-play RL: the model generates reasoning chains, verifies their correctness, and learns from successful chains
- GRPO-based training

Performance:
- AIME: 66.7% (impressive for an open model)
- MATH-500: 94.2%
- MMLU: 85.6%
- GPQA: 65.2%
- Slightly behind DeepSeek-R1 on math, but more general-purpose


Other Notable Models

Command R / R+ (Cohere)

Command R (March 2024):
- 35B parameters
- Specialized for RAG (Retrieval-Augmented Generation)
- 128K context window
- Strong at: embedding-based retrieval, citation generation, grounded generation
- MMLU: 75.7%
- Multi-step tool use capabilities

Command R+ (April 2024):
- 104B parameters
- Improved RAG and tool use
- 128K context
- Competitive with GPT-4 on RAG-specific tasks
- Designed for enterprise use cases

Key innovation β€” Grounded Generation****:
- The model is trained to generate inline citations
- Response format: "John F. Kennedy was the 35th president [1]... [1] History.com, JFK Biography"
- This enables verifiability and trust in enterprise deployments
- Training: data with human-verified citations, RLHF for citation accuracy


DBRX (Databricks, March 2024)

  • 132B total parameters, 36B active (MoE)
  • 16 experts, top-4 routing (unusual: typically top-2)
  • Built on open-source stack (Megablocks for MoE, Composer for training)
  • 2T training tokens
  • MMLU: 73.7%
  • HumanEval: 70.1%
  • Open-source (MIT license)
  • Trained on 3,072 H100 GPUs
  • Key differentiator: DBRX is designed for enterprise customization (fine-tuning, RAG)

Architecture details:
- RoPE, SwiGLU, GQA (with 4 KV heads per 64 query heads)
- Top-4 routing: each token activates 4 experts out of 16
- Capacity factor: 1.125 (tight capacity, some token dropping)
- Fine-grained MoE approach


Grok 1 / 2 / 3 (xAI)

Grok 1 (November 2023, open-sourced March 2024):
- 314B parameters (MoE)
- 8 experts, top-2 routing, ~86B active
- 25K context window
- Base model trained on ~2T tokens (text + code)
- Unique: access to real-time X (Twitter) data for training
- Personality: witty, sometimes sarcastic (designed to be less "safe")
- MMLU: 73.0%
- Open-sourced under Apache 2.0

Grok 2 (August 2024):
- Proprietary, improved over Grok 1
- MMLU: 88.4% (competitive with GPT-4)
- Advanced reasoning and coding
- Aurora image generation (integrated into the model)
- Available via X Premium subscription

Grok 3 (February 2025):
- Latest generation, significantly improved
- Claimed to outperform DeepSeek-R1 and GPT-4o
- Extended reasoning capabilities
- 100K+ context window
- Focus on truth-seeking and mathematical reasoning


Yi 1.5 / Yi 2 (01.AI)

Yi 1.5 (January 2024):
- Sizes: 6B, 9B, 34B
- Bilingual (Chinese + English)
- 34B model: MMLU 76.3% (competitive with Llama-2-70B at half the size)
- 200K context window (via YaRN RoPE scaling)
- Vocabulary: 64,000 tokens (efficient for Chinese)

Yi 2 (December 2024):
- Upgraded architecture with GQA
- Improved multilingual support (more languages)
- Stronger code and math performance
- Remains open-weight under Apache 2.0


Falcon (TII, UAE)

Falcon series:
- Falcon 40B (May 2023): Trained on 1T tokens of RefinedWeb (filtered CommonCrawl)
- MMLU: 55.4% (surprisingly low for 40B, but very data-efficient)
- Architecture: multi-query attention (all heads share K,V)
- Open-weight under Apache 2.0
- Notable: proved that high-quality web data filtering could reduce need for curated data

  • Falcon 180B (September 2023):
  • 180B parameters, trained on 3.5T tokens
  • MMLU: 74.7%
  • At release, the largest openly available model
  • Required 400GB+ GPU memory (multiple A100s/H100s with parallelism)
  • Architecture: same as Falcon 40B (MQA, etc.)

  • Falcon 2 (June 2024):

  • 11B model with 8K context
  • UAE government-funded, open-weight
  • Focus on efficiency and multilingual

MPT (MosaicML, now Databricks)

MPT (Mosaic Pretrained Transformer) series:
- MPT-7B (May 2023): First major open-source model after Llama 1
- Architecture: standard decoder with some modifications
- Used ALiBi (Attention with Linear Biases) instead of RoPE β€” simpler position encoding
- FlashAttention for training speed
- 1T training tokens
- Open-source (Apache 2.0)

  • MPT-30B (June 2023):
  • 30B parameters, 8K context
  • GQA for efficient inference
  • MMLU: 54.2% (competitive for its size at the time)
  • Designed for easy fine-tuning and deployment

Pythia (EleutherAI)

  • Purpose: A research-oriented model suite designed specifically for studying training dynamics
  • Sizes: 70M, 160M, 410M, 1B, 1.4B, 2.8B, 6.9B, 12B
  • Key innovation: All checkpoints (every 1000 training steps) are saved and publicly available
  • This enables: studying how knowledge evolves during training, analyzing memorization, probing for biases during training
  • Trained on The Pile (825GB of diverse text)
  • Standard decoder architecture (GPT-NeoX style)
  • Used for countless research papers on training dynamics, memorization, bias

OLMo (AI2)

OLMo (Open Language Model, February 2024):
- AI2's fully open-source LLM
- Complete transparency: model weights, training code, data, training logs, evaluation suite all open
- Sizes: 1B, 7B (and later 7B-v2)
- Architecture: standard decoder, GQA, RoPE, SwiGLU
- Training data: Dolma dataset (3T tokens, fully open)
- 2048 A100 GPUs for training
- MMLU: 54.4% (7B) β€” reasonable for its size
- Key contribution: open-science approach to LLM development

OLMo 2 (December 2024):
- 7B and 13B versions
- Significant improvements in data quality and training recipe
- MMLU: 67.4% (13B)


Amber (LLM360)

Amber (October 2023):
- Fully open-source 6.7B model
- Part of the LLM360 project (open-sourcing full training pipeline)
- 1.1T training tokens, 200 checkpoints released
- Trained on the "AmberCorpus" (filtered CommonCrawl + The Pile)
- Standard architecture
- Contribution: full training pipeline transparency


Gemma / Gemma 2 / Gemma 3 (Google)

Gemma 1 (February 2024):
- Google's open-weight model family
- Sizes: 2B, 7B
- Based on the same technology as Gemini (but smaller, text-only, open)
- MMLU: 64.3% (7B)
- Architecture: decoder-only, GQA, RoPE, GeGLU
- Trained on 6T tokens
- Context: 8K tokens
- License: custom commercial license (free for most uses)

Gemma 2 (June 2024):
- Sizes: 2B, 9B, 27B
- Architecture: improved with alternating attention (local + global layers)
- Local layers: sliding window attention (windth 4096)
- Global layers: full attention every 3rd or 5th layer
- This hybrid approach combines efficiency of SWA with long-range modeling of full attention
- MMLU: 75.2% (27B) β€” competitive with Llama-3-8B at ~3Γ— size
- Context: 8K
- Trained on 13T tokens (27B model)
- Knowledge distillation during training (teacher: Gemini 1.5 Pro)

Gemma 3 (March 2025):
- Sizes: 1B, 4B, 12B, 27B
- Multimodal (text + image input)
- Architecture improvements: MoE for some sizes, hybrid attention
- MMLU: 81.6% (27B)
- Vision capabilities for all models β‰₯4B
- Context: 128K


Phi 1 / 2 / 3 / 4 (Microsoft)

Phi-1 (June 2023) β€” Textbook-Quality Data:
- 1.3B parameters
- Trained on 50B tokens of synthetic textbook-quality data
- Data: Python code from The Stack v1.2 + GPT-3.5-generated "textbook" explanations
- HumanEval: 50.6% pass@1 (astonishing for 1.3B)
- Key insight: data quality can compensate for model size β€” "textbook-quality" data carries much more signal per token than web data

Phi-1.5 (September 2023):
- 1.3B, text + code
- MMLU: 41.6% (small but reasonable)
- Focus on common sense reasoning

Phi-2 (December 2023):
- 2.7B parameters
- Trained on 1.4T tokens (blend of synthetic + filtered web data)
- MMLU: 56.3% (comparable to Mistral 7B at 2.7B!)
- Coding: HumanEval 59.6%
- Breakthrough: demonstrated that small models with high-quality data can match 7B models

Phi-3 (April 2024):
- Sizes: 3.8B, 7B, 14B
- Architecture: Phi-3-mini (3.8B) uses block attention (some layers use dilated attention for longer context)
- Context: 4K (mini), 128K (medium, large)
- 3.8B trained on 3.3T tokens
- MMLU: 69.0% (3.8B), 78.0% (14B)
- Phi-3-mini fits on a phone β€” quantized to 4-bit, ~1.8GB, runs on iPhone 14/15
- 3.8B outperforms Llama-3-8B in some benchmarks (data quality thesis)

Phi-3.5 (August 2024):
- Improved versions of Phi-3
- Multi-lingual support (English, Chinese, French, German, Spanish, Italian, Japanese, Korean, Portuguese, Arabic)

Phi-4 (December 2024):
- 14B parameters
- MMLU: 80.4% (new record for sub-20B models)
- Architecture: further improved training data and recipe
- Focus: reasoning and math (GSM-8K: 95.3%)
- Training: highly filtered web data + synthetic data
- Demonstrates that 14B can match 70B models from a year earlier


MiniCPM (OpenBMB)

MiniCPM (February 2024):
- 2.4B parameters (not counting embeddings)
- Trained on 1T tokens
- MMLU: 53.5% (competitive with 7B models)
- Key innovation: model scaling beyond Chinchilla-optimal β€” training smaller models on more tokens for deployment efficiency
- "Small but mighty" philosophy

MiniCPM-2B (improved):
- MMLU: 67.9% (after further improvements)
- Strong multilingual performance
- Efficient inference on CPU and mobile


SmolLM (Hugging Face)

SmolLM (July 2024):
- Sizes: 135M, 360M, 1.7B
- Designed for research and experimentation on small models
- Trained on SmolLM-Corpus (high-quality synthetic + filtered data)
- Focus: studying scaling properties, efficient training, and on-device deployment
- Key feature: fully open training pipeline (data, code, configs, checkpoints)
- Part of Hugging Face's push for open science


H2O-Danube (H2O.ai)

H2O-Danube (January 2024):
- 1.8B parameters
- Danube-1.8B trained on 1.8T tokens
- MMLU: 35.4% (competitive for its size)
- Trained on 100% open data (LAION's OpenWeb + The Stack + RedPajama)
- Apache 2.0 license
- Danube2 (October 2024): 1.8B and 7B sizes


BERT Family (Encoder-Only)

BERT (Google, 2018)

BERT (Bidirectional Encoder Representations from Transformers) revolutionized NLP by introducing bidirectional pre-training.

Architecture:
- Encoder-only Transformer (no causal masking)
- Bidirectional self-attention (each token attends to all tokens in both directions)
- This is the key difference from GPT-style decoder models β€” BERT sees the full context

Model sizes:

Model Parameters Layers d_model Heads
BERT-Base 110M 12 768 12
BERT-Large 340M 24 1024 16

Pre-training objectives:
1. Masked Language Modeling (MLM): 15% of tokens are masked; the model predicts them from context
- Of the 15%: 80% replaced with [MASK], 10% replaced with random token, 10% unchanged
- This prevents mismatch between pre-training (with [MASK]) and fine-tuning (no [MASK])
2. Next Sentence Prediction (NSP): Predict if sentence B follows sentence A
- 50% positive pairs (A β†’ B follows), 50% negative (A β†’ B from different document)
- This teaches sentence-level coherence

Tokenizer:
- WordPiece with 30,000 token vocabulary
- Pre-tokenization: split on whitespace and punctuation
- Subword: split unknown words into known pieces (e.g., "playing" β†’ "play" + "##ing")
- Special tokens: [CLS], [SEP], [MASK], [PAD], [UNK]

Fine-tuning:
- Add a task-specific classification head on top of the [CLS] token
- For classification, NER, QA, etc. β€” the same pre-trained model can be adapted
- [CLS] token's final hidden state serves as the aggregate sequence representation
- Fine-tuning is fast (few epochs) and data-efficient

Impact:
- Set SOTA on 11 NLP benchmarks at release
- Demonstrated that bidirectional context is crucial for understanding tasks
- Sparked the "BERT era" β€” most NLP systems from 2018-2022 were BERT-based


RoBERTa (Facebook, 2019)

Robustly Optimized BERT Approach:
- Same architecture as BERT-Large (340M params)
- Key improvements: better training recipe
1. More data: 160GB (BERT: 16GB) β€” BookCorpus + Wikipedia + CC-News + OpenWebText + Stories
2. More training: 100K steps with batch size 8K (BERT: 1M steps, batch 256)
3. No NSP: Removed Next Sentence Prediction (shown to be unnecessary)
4. Dynamic masking: different mask pattern each epoch (BERT: static masking)
5. Larger token batches: 8K tokens per batch (vs BERT's 256)
- Performance: GLUE 88.5 (BERT: 80.5), SQuAD 94.6 (BERT: 90.9)
- Demonstrated that BERT was under-trained β€” RoBERTa showed the same architecture could be much better


ALBERT (Google, 2019)

A Lite BERT:
- Parameter reduction techniques:
1. Factorized embedding: Decompose vocabulary embedding matrix into two smaller matrices
- Instead of V Γ— H (vocab_size Γ— hidden_size), use V Γ— E + E Γ— H where E << H
- Typical: E=128, H=768, V=30K β†’ 3.8M + 98K vs 23M (77% reduction)
2. Cross-layer parameter sharing: All 12 layers share the same attention + FFN parameters
- This massively reduces parameters without reducing computation
- ALBERT-large (12 layers, shared) β‰ˆ BERT-base (12 layers, separate) parameters
- ALBERT-xxlarge: 223M parameters but hidden_dim 4096, 16 layers (BERT-large is 340M with 1024-dim, 24 layers)
- Performance: slightly worse than BERT-large but with 18Γ— fewer parameters
- Key insight: Layer parameters can be heavily shared without major quality loss


DistilBERT (Hugging Face, 2019)

Knowledge distillation of BERT:
- 66M parameters (40% fewer than BERT-base)
- 2Γ— faster inference
- 95% of BERT's performance
- Training:
1. Student: 6 layers (half of BERT-base's 12)
2. Teacher: BERT-base
3. Distillation loss: KL divergence between student and teacher output distributions
4. Combined with MLM loss
5. No NSP
- Proved that distillation is highly effective for BERT-style models


ELECTRA (Stanford, 2020)

Efficiently Learning an Encoder that Classifies Token Replacements Accurately:
- Novel pre-training task: Replaced Token Detection (RTD)
1. A small generator (like a masked LM) replaces some tokens with plausible alternatives
2. The ELECTRA discriminator predicts which tokens were replaced
3. The discriminator is the model you keep β€” it learns to detect "fake" tokens
- Advantage: All tokens are processed (unlike MLM which only processes 15%), so training is more efficient
- ELECTRA-small (14M) matches BERT-base (110M) on GLUE
- ELECTRA-large matches RoBERTa-large with 1/4 of the computation
- Key insight: The RTD task provides a denser learning signal than MLM


DeBERTa / DeBERTaV3 (Microsoft, 2020-2021)

Decoding-enhanced BERT with Disentangled Attention:
- Disentangled attention: Instead of attention = f(content + position), DeBERTa computes separate attention scores for content-to-content and content-to-position, then sums them
- A_{i,j} = Q_content_i Β· K_content_j + Q_content_i Β· K_position_{Ξ΄(i,j)} + Q_position_{Ξ΄(j,i)} Β· K_content_j
- This allows the model to learn word content and position relationships independently
- Enhanced mask decoder: Uses absolute positions in the decoder layer (only for MLM prediction)
- DeBERTaV3 (2021):
- Replaced MLM with ELECTRA-style RTD (replaced token detection)
- Improved training efficiency
- DeBERTaV3-large (304M) matches or exceeds DeBERTa-large (1.5B) on many tasks
- DeBERTaV3-base: 34-layer, 30K vocab, 305M params


ModernBERT (2024)

Latest evolution of BERT:
- Goal: Bring BERT architecture up to modern standards
- Key improvements:
1. Rotary Position Embeddings (RoPE) instead of absolute positions
2. Grouped-Query Attention (GQA) for efficiency
3. Flash Attention support
4. GeGLU activation (like modern LLMs)
5. Pre-norm (RMSNorm)
6. No bias terms
7. 8192 context length (vs BERT's 512)
8. Large vocabulary for better coverage
9. Architecture: essentially an encoder-only LLaMA
- Sizes: base (~150M) and large (~400M)
- Outperforms DeBERTaV3 on most benchmarks
- Demonstrates that encoder-only models benefit from the same architectural improvements as decoder LLMs


T5 Family (Encoder-Decoder)

T5 (Google, 2019)

Text-to-Text Transfer Transformer: A unified framework where every NLP task is cast as a text-to-text problem.

Architecture:
- Encoder-Decoder Transformer
- Encoder: bidirectional self-attention (full context)
- Decoder: causal self-attention + cross-attention to encoder
- Both encoder and decoder have 6/12/24 layers (depending on size)

Model sizes:

Model Parameters Layers (enc+dec) d_model Heads
T5-Small 60M 6+6 512 8
T5-Base 220M 12+12 768 12
T5-Large 770M 24+24 1024 16
T5-3B 2.8B 24+24 1024 32
T5-11B 11B 24+24 1024 128

Unified text-to-text format:
- Every task expressed as text input β†’ text output
- Classification: "imdb classification: This movie was great! β†’ "positive"
- Translation: "translate English to German: The cat β†’ "Die Katze"
- Summarization: "summarize: long article text β†’ "short summary"
- This unified format enabled multi-task training

Pre-training β€” "Span Corruption":
- Replace consecutive spans of tokens with sentinel tokens
- Input: "The cat on the "
- Output: " sat mat "
- The model learns to reconstruct corrupted spans
- Span length: average 3 tokens, corrupted ~15% of tokens
- This is an encoder-decoder version of MLM

C4 Dataset:
- Colossal Clean Crawled Corpus β€” 750GB of clean, deduplicated web text
- Filtered: remove pages with porn, spam, gibberish, code
- Deduplicated at document level (removed nearly-duplicate pages)
- Required minimum 3 sentences, end with terminal punctuation
- This became a standard dataset in NLP

Key insight: The encoder-decoder approach allows the encoder to build a rich bidirectional representation of the input, while the decoder generates output conditioned on it. This is more flexible than decoder-only (which must perform understanding + generation with the same parameters).


FLAN-T5 (Google, 2022)

Fine-tuned LAnguage Net β€” Instruction Tuning:
- T5 models fine-tuned on a massive collection of instruction-formatted tasks
- 1,836 tasks collected from 60+ NLP datasets
- Each task reformatted as: "Instruction: ... Input: ... Output: ..."
- Zero-shot performance dramatically improved after instruction tuning
- No RLHF needed β€” SFT on diverse instructions was sufficient

FLAN-T5 sizes:
- Same sizes as T5 (small through xxl)
- FLAN-T5-XXL (11B): MMLU 59.4% (comparable to much larger models)
- FLAN-T5-XL (3B): MMLU 48.6%
- Instruction following was significantly better than base T5

Key contribution: Demonstrated that instruction tuning at scale (1000+ tasks) is as effective as model scaling for task generalization. This inspired the instruction-tuning approach used in most modern LLMs.


mT5 (Google, 2021)

Multilingual T5:
- Trained on mC4 (C4 in 101 languages)
- Same architecture as T5
- Vocabulary: 250,000 tokens (SentencePiece, unified for all languages)
- Supports 101 languages
- Cross-lingual transfer: improvements in one language benefit others

Challenges:
- Vocabulary must balance token efficiency across all languages
- 250K vocab was necessary to cover 101 languages with reasonable efficiency
- Training data distribution follows web prevalence: English dominant, many languages underrepresented


UL2 (Google, 2022)

Unifying Language Learning:
- Introduces Mixture of Denoisers (MoD) β€” multiple pre-training objectives combined during training

Three denoising paradigms:
1. R-Denoiser (Regular): Standard span corruption (like T5), short spans
2. X-Denoiser (Extreme): Span corruption with very long spans (50%+ of input)
- Forces the model to learn long-range dependencies and generate coherent long text
3. S-Denoiser (Sequential): Causal LM variant β€” prefix language modeling
- Model sees a prefix, generates the rest autoregressively
- Bridges the gap between bidirectional encoders and autoregressive decoders

Architecture:
- Encoder-decoder (like T5)
- Mode tokens: <2R>, <2X>, <2S> prepended to input to indicate which denoising mode to use
- Key innovation: one model trained on all three modes β€” effectively a "Swiss Army knife" that can function as encoder (R), long-span generator (X), or decoder-only (S)
- UL2-20B: competitive with T5-11B and PaLM-62B on many tasks


T5v1.1 (Google, 2020)

Improvements over T5:
- GeGLU activation (instead of ReLU)
- Pre-norm (instead of post-norm)
- No bias terms in LayerNorm
- Training on more C4 data
- These changes improved quality but introduced incompatibility with original T5 checkpoints
- T5v1.1-Large consistently outperforms T5-Large


Architectural Paradigms

Encoder-Only Models

Examples: BERT, RoBERTa, ALBERT, DistilBERT, ELECTRA, DeBERTa, ModernBERT

Capabilities:
- Best at: Natural Language Understanding (classification, NER, relation extraction, sentiment analysis)
- Cannot: Generate text (no decoder)
- Core advantage: Bidirectional context provides the richest token representations
- Use case: embedding generation, feature extraction, semantic search, classification tasks

Architecture details:
- Full bidirectional self-attention (no causal masking)
- Typically use [CLS] token for aggregated representations
- Pre-trained with MLM or similar objectives
- Fine-tuned by adding a lightweight task head

When to use encoder-only:
- You need text embeddings (semantic similarity, clustering)
- Classification/labeling tasks
- When computational cost is a concern (they're cheaper than decoder models)
- Applications requiring bidirectional understanding (entity extraction, relation classification)

Limitations:
- Cannot generate text
- Usually limited context length (BERT: 512 tokens, ModernBERT: 8K)
- Not suitable for conversational AI or generative tasks


Decoder-Only Models

Examples: GPT-4, Llama 3, Claude, Mistral, Gemini, DeepSeek-V3, Qwen2.5

Capabilities:
- Best at: Text generation, conversational AI, code generation, creative writing
- Also good at: Understanding (with sufficient scale and training), reasoning, translation, summarization
- Core advantage: Unified architecture for both understanding and generation in one model

Architecture details:
- Causal self-attention (each token can only attend to previous tokens)
- Pre-trained with autoregressive language modeling (predict next token)
- No explicit embedding layer for downstream tasks
- The same parameters perform both understanding and generation through the forward pass

Why decoder-only dominates:
1. Simplicity: One model, one objective, one set of parameters for all tasks
2. Scaling: Better scaling properties β€” in-context learning emerges at scale
3. Flexibility: Can be used for generation, classification, embedding, etc.
4. Chat optimization: RLHF aligns the generation behavior for conversation
5. No complexity of encoder-decoder: No need to design cross-attention or two-pass processing

When to use decoder-only:
- Conversational AI and chatbots
- Content generation (writing, coding, translation)
- Any task requiring text generation
- Zero-shot/few-shot learning (in-context learning)

Trade-offs:
- Less efficient for pure understanding tasks (must generate to answer)
- Left-to-right bias β€” cannot use future context for understanding
- Larger and more expensive than equivalent encoder models
- KV cache memory grows with sequence length


Encoder-Decoder Models

Examples: T5, FLAN-T5, mT5, UL2, T5v1.1, BART, PEGASUS

Capabilities:
- Best at: Sequence-to-sequence tasks (translation, summarization, question answering)
- Combination: Bidirectional understanding (encoder) + autoregressive generation (decoder)

Architecture details:
- Encoder: full bidirectional self-attention
- Decoder: causal self-attention + cross-attention to encoder outputs
- Cross-attention: decoder attends to encoder's final hidden states
- This allows the model to "look at" the entire input when generating each output token

Advantages over decoder-only:
- Richer input representations (bidirectional)
- Better at tasks where understanding the full input is critical
- Cross-attention provides a strong inductive bias for I/O tasks

Advantages over encoder-only:
- Can generate text
- More flexible task formulation

When to use encoder-decoder:
- Translation, summarization
- Any task with a clear input and output distinction
- When you need to generate based on a fully-understood input
- When the input contains complex relationships that benefit from bidirectional encoding

Trade-offs:
- Two-pass processing (encode then decode) β€” slower
- More parameters (two transformers) for the same capacity
- Cross-attention adds computational complexity
- Less flexible than decoder-only for conversational AI
- Harder to scale (encoder-decoder scaling laws differ)


Summary: Which Architecture to Use

Criterion Encoder-Only Decoder-Only Encoder-Decoder
Text Generation ❌ No βœ… Yes βœ… Yes
Classification βœ… Best βœ… Good βœ… Good
Embeddings βœ… Best βœ… Good ❌ Complex
Translation ❌ No βœ… Good βœ… Best
Summarization ❌ No βœ… Good βœ… Best
Conversational AI ❌ No βœ… Best ⚠️ Possible
Reasoning ⚠️ Limited βœ… Best ⚠️ Possible
Inference Speed βœ… Fastest ⚠️ Moderate ❌ Slowest
Context Length ⚠️ Short βœ… Long ⚠️ Moderate
Parameter Efficiency βœ… Best ⚠️ Inferior βœ… Good
Scale Benefits ❌ Saturing βœ… Emergent ⚠️ Moderate

Mixture-of-Experts Deep Dive

Sparse MoE Architecture

Core concept:
Instead of having one feed-forward network per layer, MoE replaces each FFN with multiple "expert" FFNs and a router that decides which expert(s) to use for each token.

Why MoE:
- Activate fewer parameters while having more total parameters
- Dense model of size N: all N parameters used for every token
- MoE model of size N_total with k_active: N_total can be much larger, but compute per token is similar to N_active
- This allows scaling total parameter count without proportional compute increase

Components:

  1. Experts: N small FFNs (typically 8-256 experts)
    - Each expert is a standard SwiGLU/ReLU FFN: W1, W2 (and W3 for SwiGLU)
    - Experts are typically homogeneous (same size, same structure)
    - Only the FFN layer is "expertized" β€” attention, layer norm, embeddings remain dense

  2. Router (Gate): A learned linear layer that computes routing probabilities
    - g(x) = softmax(W_g Β· x)
    - For each token, the router outputs a distribution over N experts
    - Top-k experts are selected based on gating probabilities

  3. Combined output: y = Ξ£_i g_i(x) Β· FFN_i(x) (weighted by gating probabilities)

Top-k Routing:
- Most common: k=2 (Mixtral, DeepSeek-V2, DBRX)
- Each token activates 2 experts
- Routing decision: select the k experts with highest logits
- Gating probabilities of selected experts are re-normalized (softmax over selected k)
- The gating probabilities are used as weights for combining expert outputs

Router variants:
- Token Choice (standard): Router picks top-k experts for each token
- Expert Choice: Each expert picks top-k tokens (inverted routing)
- Better load balancing (each expert always processes exactly k tokens)
- But can't guarantee all tokens are processed (if no expert picks them)
- Used in Google's Switch Transformer

Load Balancing Loss

Problem: Without intervention, the router learns to always send tokens to the same few "expert" experts, defeating the purpose of MoE.

Solution: Auxiliary Load Balancing Loss

  1. Importance-based loss:
    L_aux = Ξ± Β· N_experts Β· Ξ£_i (f_i Β· P_i)
    where f_i = fraction of tokens routed to expert i
    and P_i = average routing probability for expert i
    This encourages uniform assignment (f_i β‰ˆ 1/N for all i)

  2. Switch Transformer variant:
    L_load = Ξ± Β· Ξ£_i f_i Β· P_i (simplified)

  3. DeepSeek variant:
    Combines importance loss with "z-loss" β€” a penalty on the logits' magnitude to stabilize training

Typical Ξ± values: 0.01 (standard), can be tuned per model

Capacity Factor and Token Dropping

Capacity:
Each expert can process at most C = capacity_factor Γ— (tokens_per_batch / num_experts) tokens.

  • Capacity factor = 1.0: Each expert processes exactly its fair share. Token dropping likely if routing is imperfect.
  • Capacity factor = 1.25: 25% slack. Most tokens will be processed, but some may still be dropped.
  • Capacity factor = 2.0: High slack. Rarely drops tokens, but more computation.

Token dropping:
When a token's target expert is at capacity, the token is either:
- Dropped: The token passes through as a residual connection (skips the FFN entirely)
- Not ideal for quality, but keeps computation deterministic
- Overloaded: The expert processes it anyway (beyond capacity)
- Increases computation but prevents information loss
- Used by some implementations

Why token dropping is problematic:
- Important tokens (e.g., the token being predicted) might be dropped
- During training, dropped tokens lose expert gradient signal
- During inference, unexpected drops can change model behavior
- Solution: carefully manage capacity factor and monitor drop rates

Batch-Aware Routing

In MoE training, routing is computed per-token independently, but the capacity constraint is per-batch. This creates a tension:

  • A token's routing decision (which expert) doesn't consider other tokens' decisions
  • But the expert capacity is shared across all tokens in the batch
  • This means routing must produce balanced batches at the batch level, not per-sample

Expert parallelism:
- In distributed training, different experts are placed on different GPUs
- Routing must be followed by all-to-all communication (send tokens to their assigned expert's GPU)
- This creates communication overhead β€” a key challenge in MoE training
- Solutions: group size tuning, expert sharding, hierarchical routing

Fine-Grained MoE

Problem with traditional MoE:
- 8-16 experts means each expert is quite large
- A few "specialized" experts may dominate, while others are rarely used
- Gradients flow sparsely to individual experts

Fine-grained MoE (DeepSeekMoE, QwenMoE):

  • More, smaller experts: 64-256 experts instead of 8-16
  • More active experts: 6-8 experts per token instead of 2
  • Each expert is smaller (fewer parameters), so the total compute per token is similar

Benefits of fine-grained:
- Better specialization: More experts can specialize on specific patterns
- Finer routing granularity: The model can combine more expert perspectives
- Better load balancing: More experts means easier to balance load
- Redundant specialization: Different tokens can use different specialized experts

Trade-offs:
- More communication overhead (more experts per token β†’ more all-to-all)
- Harder to train (more routing decisions per token)
- Risk of expert collapse (some experts never learn useful functions)

DeepSeekMoE specifics:
- 256 experts with 8 active per token
- Each expert has hidden_dim = 2048 (for V3 671B)
- Shared experts: A few dense FFNs that all tokens use (in addition to routed experts)
- This captures general computation that every token needs
- The routed experts handle specialized computation
- Shared experts are always computed; routed experts are selected per-token
- No token dropping: Uses expert balancing with load-aware routing and dynamic capacity adjustment

Expert Parallelism

How experts are distributed across GPUs:

  1. Expert Sharding: Each GPU hosts a subset of experts
    - If 8 GPUs and 64 experts: each GPU hosts 8 experts
  2. All-to-All Communication: Tokens are sent to the GPU hosting their selected expert
    - This is the bottleneck in MoE training
  3. Capacity per GPU: Each GPU processes its allocated tokens

Communication patterns:
- Token dispatch: Send token vectors to the GPU with the chosen expert
- Token combine: Collect processed vectors from all GPUs
- Both require all-to-all collective communication

Strategies to reduce communication:
- Local MoE: Some experts are replicated across GPUs (no communication needed locally)
- Hybrid parallelism: Combine expert + data + tensor parallelism
- Hierarchical MoE: First route to expert groups, then to experts within groups

DeepSeekMoE Architecture Details

DeepSeek V2/V3 MoE structure:

Layer Input
    β”‚
    β”œβ”€β”€ Router (linear β†’ softmax)
    β”‚       β”‚
    β”‚       β”œβ”€β”€ Route to 8 experts out of 256 (top-8 routing)
    β”‚       β”‚
    β”‚       └── Each expert: SwiGLU FFN (W_up, W_gate, W_down)
    β”‚
    β”œβ”€β”€ Shared experts (2-4 dense FFNs, always computed)
    β”‚
    └── Combine: Ξ£ g_i Β· Expert_i(x) + SharedExpert(x)

Key design choices:
- 256 fine-grained experts (each relatively small)
- Top-8 active experts per token
- 2 shared experts (always on)
- No token dropping (padded capacity)
- Load balancing via auxiliary loss (with z-loss for stability)

Why 256 experts with top-8?
- Total compute: 8/256 β‰ˆ 3% of total FFN parameters used per token
- This is ~8Γ— more efficient than Mixtral (2/8 = 25%)
- Each expert can be more specialized (smaller capacity)
- Shared experts handle common computation

QwenMoE Architecture

Qwen2.5-MoE (14B active, ~40B total):

  • Based on Qwen2.5 architecture
  • Uses MoE only in FFN layers (attention is dense)
  • Top-2 routing with 8 experts (traditional approach)
  • Load balancing loss with adaptive coefficient
  • Expert dispatch: each token activates 2 experts
  • Shared expert: 1 shared FFN expert in addition to routed experts

MoE specifics:

│── Shared Expert (dense FFN, always computed)
│── Router β†’ top-2 from 8 experts
│── Combine: shared + routed
  • Total parameters saved: ~28% fewer than an equivalent dense model
  • Active parameters: ~35% of total (vs 100% for dense)
  • Performance: nearly matches dense Qwen2.5-72B with ~40B total parameters

πŸ“„ Tokenization β€” Comprehensive Reference β€” 1963 lines

This document covers everything about tokenization in Large Language Models: algorithms, training, vocabulary design, impact on performance, multilingual challenges, and practical implementation. Written for AI engineers and NLP researchers.

AlignmentAttentionBERTBenchmarkEvaluationFine-tuningGPTGPUInferenceLLM

What is Tokenization?

Tokenization is the process of converting raw text into a sequence of integers (token IDs) that the model can process. It is the first and last step in any LLM pipeline:

Raw text β†’ [Tokenizer] β†’ Token IDs β†’ [LLM] β†’ Token IDs β†’ [Detokenizer] β†’ Output text

Why tokenization matters:
- Determines the vocabulary the model can produce
- Affects compression ratio (chars per token) β€” directly impacts effective context length
- Drives multilingual capability β€” some tokenizers handle non-English poorly
- Influences computational cost β€” more tokens = more compute
- Impacts model performance β€” bad tokenization harms reasoning, code gen, translation
- Creates tokenization artifacts β€” suboptimal splits can introduce bias or information loss

Key concepts:
- Token: A unit of text (word, subword, or character/byte)
- Vocabulary: The set of all tokens the model knows
- Token ID: The integer index of a token in the vocabulary
- Encoder: Text β†’ Token IDs
- Decoder: Token IDs β†’ Text
- Special tokens: Reserved tokens with specific functions (BOS, EOS, PAD, etc.)


Subword Tokenization Algorithms

Byte-Pair Encoding (BPE)

BPE is the most widely used tokenization algorithm in modern LLMs. It was originally proposed as a data compression technique by Philip Gage in 1994 and adapted for NLP by Rico Sennrich et al. in 2016 (Neural Machine Translation of Rare Words with Subword Units).

Core algorithm:

  1. Initialization: Start with a vocabulary of all individual characters (or bytes) in the training corpus
  2. Count pairs: Find the most frequent adjacent pair of tokens in the corpus
  3. Merge: Replace all occurrences of that pair with a new token
  4. Add to vocabulary: Add the new merged token to the vocabulary
  5. Repeat: Steps 2-4 until the vocabulary reaches the desired size

Pseudocode:

function train_bpe(corpus, vocab_size):
    # Step 1: Initialize with character-level vocabulary
    vocab = set of all unique characters in corpus
    tokens = [char for char in corpus]  # initial segmentation

    # Step 2-5: Iteratively merge most frequent pairs
    while len(vocab) < vocab_size:
        # Count all adjacent pairs
        pair_counts = defaultdict(int)
        for i in range(len(tokens) - 1):
            pair = (tokens[i], tokens[i+1])
            pair_counts[pair] += 1

        # Find the most frequent pair
        best_pair = max(pair_counts, key=pair_counts.get)

        # Merge this pair everywhere
        new_token = best_pair[0] + best_pair[1]
        new_tokens = []
        i = 0
        while i < len(tokens):
            if i < len(tokens) - 1 and tokens[i] == best_pair[0] and tokens[i+1] == best_pair[1]:
                new_tokens.append(new_token)
                i += 2
            else:
                new_tokens.append(tokens[i])
                i += 1
        tokens = new_tokens

        # Add merged token to vocabulary
        vocab.add(new_token)

    return vocab, merge_rules

Merge rules:
- The final vocabulary is stored as a set of tokens
- Additionally, the merge rules (ordered list of pair merges) must be saved
- At inference time, new text is tokenized by greedily applying these merge rules in order
- Greedy matching: For each position, find the longest vocabulary token that matches the text
- Some implementations use the merge rules directly instead of vocabulary lookup

Frequency-based nature:
- BPE is purely frequency-based β€” it doesn't use any linguistic knowledge
- Common pairs merge first: ("th", "he", "the"), ("ing"), ("tion")
- Rare characters or pairs remain as subwords or individual characters
- This creates a bias: common words stay as single tokens, rare words get split

BPE tokenization example:

Corpus: "low lower lowest low"

Step 1 β€” Characters: l, o, w, space, e, r, s, t
  Initial vocabulary: {l, o, w, space, e, r, s, t}
  Most common pair: ('l', 'o') appears 3 times β†’ merge to "lo"

Step 2:
  Vocabulary: {l, o, w, space, e, r, s, t, lo}
  Most common pair: ('lo', 'w') appears 3 times β†’ merge to "low"

Step 3:
  Vocabulary: {l, o, w, ..., low, ...}
  Most common pair: ('low', 'e') appears 2 times β†’ merge to "lowe"

Step 4:
  Vocabulary: ... continues until target vocabulary size

Problems with raw BPE:
- Character-by-character initialization means the vocabulary initially contains all characters
- For large vocabularies, the algorithm is O(vocab_size Γ— corpus_size) β€” expensive
- Merging decisions are greedy and cannot be reversed
- BPE has no notion of "word boundaries" β€” it can merge across word boundaries
- Solution: pre-tokenization (split on whitespace/punctuation) before BPE

WordPiece

WordPiece is the tokenization algorithm used by BERT and its variants (originated by Google for Japanese/Korean segmentation, then adapted for BERT).

Key difference from BPE:
- BPE merges the most frequent pair
- WordPiece merges the pair that maximizes the likelihood of the training data

Algorithm:
1. Initialize vocabulary with all characters (like BPE)
2. Start with a "base" vocabulary of single characters
3. Try merging every possible pair of tokens
4. For each candidate merge, compute the likelihood improvement:
- L = log P(merge) β€” log P(no merge)
- where P(merge) = pair_count / total_pairs
- and P(no merge) = first_count / total Γ— second_count / total
5. Keep the merge that gives the largest likelihood increase
6. Repeat until target vocabulary size

Likelihood-based merge scoring:

score(A, B) = count(A,B) / (count(A) Γ— count(B))

This score prefers merging tokens that frequently appear together relative to their individual frequencies. A high score means the pair is more "cohesive" than expected by chance.

WordPiece vs BPE example:
- BPE might merge "th" β†’ "t" + "h" if "th" is very common
- WordPiece might NOT merge "th" if "t" and "h" also appear commonly apart
- WordPiece tends to create more "meaningful" subwords because it captures statistical dependence

BERT's WordPiece:
- Pre-tokenization: Basic whitespace + punctuation splitting
- Vocabulary: 30,000 tokens
- Special tokens: [CLS], [SEP], [MASK], [PAD], [UNK]
- WordPiece uses "##" prefix to indicate continuation of a word:
- "playing" β†’ "play" + "##ing"
- "unhappiness" β†’ "un" + "##happiness"
- Unknown words are split into characters (if "##" prefix on single character) or mapped to [UNK]

Limitations:
- WordPiece requires more computation than BPE (must evaluate all candidate pairs)
- The likelihood-based scoring can be less stable than frequency-based
- BERT's implementation has a size limit on intermediate merges, which limits token creation
- The "##" notation is a design choice β€” other implementations use different markers

Unigram Language Model

Unigram tokenization (Kudo, 2018 β€” Subword Regularization) takes a probabilistic approach. Instead of greedily building a vocabulary, it starts with a large vocabulary and iteratively prunes it.

Core idea:
- Each token is assigned a probability based on its frequency
- The segmentation of a word is not deterministic β€” there are multiple possible segmentations
- The best segmentation is the one with the highest probability under the unigram model

Algorithm:

  1. Initialization: Create a large "seed" vocabulary β€” typically all characters plus the most common substrings (from BPE or other methods)
  2. Expectation-Maximization (EM) training:
    - E-step: Given the current vocabulary, compute the most likely segmentation of each word using the Viterbi algorithm (or the forward-backward algorithm for all possible segmentations)
    - M-step: Re-estimate the probability of each token based on its expected count under the current segmentation
  3. Pruning: Remove tokens with the lowest probability (typically remove 10-20% of tokens each iteration)
  4. Repeat: EM training + pruning until the target vocabulary size

EM training details:

E-step (computing token probabi...

Byte-Level Tokenization

Byte-level tokenization operates on raw UTF-8 bytes instead of Unicode characters or characters after normalization. This provides 100% coverage of all possible Unicode strings.

GPT-2's Byte-Level BPE

GPT-2 introduced byte-level BPE to solve the problem of unknown tokens.

Problem with character-level BPE:
- Character-level BPE starts with all Unicode characters
- Unicode has 150,000+ characters β€” the base vocabulary is huge
- New characters (emoji, mathematical symbols, historical scripts) are not covered
- Any character not in the initial vocabulary becomes [UNK]

Solution: Base vocabulary = 256 byte values
- Instead of starting with Unicode characters, start with all 256 possible byte values
- UTF-8 encodes all Unicode characters as 1-4 bytes
- Every possible Unicode character is already represented
- No UNK tokens possible β€” complete Unicode coverage

GPT-2's approach:

  1. Byte-level vocabulary: Start with all 256 byte values (0x00-0xFF)
  2. Add word-level tokens: Add special token Δ  (representing space) and common words
  3. BPE merges: Apply standard BPE merging on the byte sequences
  4. Final vocabulary: 50,257 tokens (256 byte tokens + ~50,000 merged tokens + 1 end-of-text token)

Byte-level BPE tokenization example:

Input: "Hello πŸ‘‹ world"
UTF-8 bytes: H(48) e(65) l(6C) l(6C) o(6F) 20 F0 9F 91 8B 20 w(77) o(6F) r(72) l(6C) d(64)

The tokenizer processes these bytes and applies BPE merges:
"Hello" β†’ single token (common word)
" " + "πŸ‘‹" β†’ might stay as individual bytes (rare emoji)
" " + "world" β†’ "world" as single token (common word)

Strengths:
- Complete Unicode coverage (no UNK tokens)
- Can handle any script, emoji, mathematical symbols
- Simple β€” just bytes in, tokens out

Weaknesses:
- Non-English text uses significantly more bytes per character
- English ASCII: 1 byte per character
- CJK characters: 3 bytes per character
- Emoji: 4 bytes per character
- This means non-English text is tokenized into more tokens β€” reducing effective context
- For example, "δ½ ε₯½" = 6 bytes = up to 6 initial tokens (before BPE merges)

GPT-4's tiktoken (cl100k_base)

GPT-4 uses the tiktoken library and the cl100k_base tokenizer. This is the most widely-used byte-level BPE tokenizer in production.

cl100k_base specifications:
- Algorithm: Byte-level BPE
- Vocabulary size: 100,256 tokens
- Base vocabulary: 256 byte tokens
- Regular tokens: ~99,000 learned BPE merges
- Special tokens: 0 (special tokens handled separately, not merged)
- Regex-based pre-tokenization: Yes (different rules for different character classes)

Regex pre-tokenization rules (GPT-4 split pattern):

'(?i:'s|'t|'re|'ve|'m|'ll|'d)| ?\p{L}+| ?\p{N}+| ?[^\s\p{L}\p{N}]+|\s+(?!\S)|\s+

This regex captures:
1. English contractions: 's, 't, 're, 've, 'm, 'll, 'd (case-insensitive)
2. Letter sequences: optionally preceded by space, one or more Unicode letters
3. Number sequences: optionally preceded by space, one or more Unicode digits
4. Other characters: non-letter, non-digit, non-space sequences
5. Whitespace captures

Effect of pre-tokenization:
- Words are separated from their preceding space
- Numbers are separated from letters
- Punctuation stands alone
- This ensures that BPE merges only happen within these "word-like" units
- Prevents BPE from merging across word boundaries (a problem with pure byte-level BPE)

Encoding process (tiktoken):

1. Apply regex pre-tokenization β†’ list of "chunks"
2. For each chunk:
   a. Encode to UTF-8 bytes
   b. Apply BPE merges (greedy, longest match)
   c. Map each merged token to its ID
3. Concatenate all token IDs

Performance characteristics:
- English text: ~3.8 characters per token (excellent compression)
- Code: ~4.5 chars per token (including whitespace)
- Non-English (Latin script): ~3-4 chars per token
- CJK: ~1.5 characters (Chinese chars) per token (poor β€” 3 bytes per char β†’ wasted)
- Processing speed: ~50 MB/s on CPU (Rust core)

Llama 3's tiktoken-based Tokenizer

Llama 3 uses a custom tiktoken-based tokenizer with vocabulary size 128,256.

Key differences from GPT-4's cl100k_base:

  1. Larger vocabulary: 128,256 vs 100,256 tokens
    - More room for common subwords and multi-byte sequences
    - Better multilingual coverage
    - Better code tokenization

  2. Different regex pre-tokenization:
    - Llama 3 uses a simplified regex pattern compared to GPT-4
    - Less aggressive separation of punctuation
    - Better handling of whitespace in code

  3. No special tokens in vocabulary:
    - Like GPT-4, special tokens (BOS, EOS, etc.) are handled outside the tokenizer
    - Added during chat template formatting, not during tokenization

  4. Improved multilingual tokenization:
    - Additional tokens for common multi-byte character sequences
    - Better compression for CJK languages (~1.2 chars/token vs ~1.5 for GPT-4)

Compression benchmark:

GPT-4 (cl100k):  "Hello world!如何用Python写一δΈͺεΏ«ι€ŸζŽ’εΊοΌŸ" β†’ 24 tokens
Llama 3 (128k):   "Hello world!如何用Python写一δΈͺεΏ«ι€ŸζŽ’εΊοΌŸ" β†’ 20 tokens

The improvement comes from dedicated tokens for common Chinese character pairs and code patterns.

Training of Llama 3 tokenizer:
- Trained on a multilingual corpus (English, code, and 30+ other languages)
- Target vocabulary size: 128,256
- BPE merging with byte-level base vocabulary
- Regex pre-tokenization tuned for code (preserving indentation)
- Extensive evaluation on compression ratio across languages before finalizing

Claude Tokenizer (Anthropic)

Anthropic's Claude uses a proprietary tokenizer (not publicly documented in detail).

Known characteristics:
- Byte-level BPE (like GPT-4 and Llama 3)
- Estimated vocabulary size: ~100K-150K
- Optimized for helpful and harmless chat
- Good multilingual performance (Claude 3+)

Unique aspects (inferred):
- Claude appears to have better tokenization for safety-critical tokens
- The tokenizer is integrated with Claude's "constitutional" framework
- Vision tokens are integrated into the same tokenization scheme


Tokenizer Training

Training from Scratch

Training a tokenizer from scratch requires:

  1. A representative corpus: The training data should match the distribution of text the model will see
  2. A target vocabulary size: Typically 16K-256K tokens
  3. An algorithm choice: BPE, WordPiece, or Unigram
  4. Pre-tokenization decisions: Regex rules, normalization, byte fallback

General workflow:

1. Collect training corpus (5-50GB of text)
2. Apply normalization (NFKC, lowercase, etc.)
3. Define pre-tokenization rules
4. Initialize vocabulary (characters or bytes)
5. Train the tokenizer (iterative merging or EM)
6. Evaluate compression ratio, coverage, and token distribution
7. Iterate on parameters until satisfied

Vocabulary Selection

The vocabulary is determined by the training algorithm, but there are key considerations:

Inclusion criteria for tokens:
- Frequency: Tokens should appear enough times in the corpus
- Length: Very long tokens (>15 characters) are rarely useful
- Compositionality: Subwords should be meaningful units
- Coverage: The vocabulary should cover >99.99% of tokens in the corpus

Manual vocabulary additions:
- Domain-specific terms (medical, legal, scientific)
- Common multi-token expressions that should be single tokens
- Code keywords and operators
- Important entity names

Vocabulary pruning:
- Remove tokens that never appear in the corpus (can happen with pre-defined vocabularies)
- Remove tokens that are too long (token length distribution is typically capped)
- Remove tokens with very low probability (in unigram models)

Merge Frequency in BPE

Understanding how merges are counted is important for analyzing BPE behavior:

Raw frequency counting:
- Each adjacent pair of tokens is counted
- The pair with the highest global frequency is merged
- Problem: many merges happen in very specific contexts

Weighted frequency:
- Each occurrence of a pair can be weighted
- Common weighting: inverse document frequency (IDF)
- This reduces the impact of repeated pairs in the same document

Entropy-based merging:
- Instead of pure frequency, use entropy reduction as the merge criterion
- Merge the pair that provides the most "information gain"
- More principled but computationally expensive

Learning BPE Merges Algorithm

The standard BPE training algorithm has O(V Γ— D) complexity where V is vocab size and D is corpus size.

Optimizations:

  1. Indexed pair counting:
    - Maintain a hash map from (token, token) β†’ count
    - After each merge, update only affected positions rather than recounting everything
    - Affected positions: where the merged token appears

  2. Boundary-aware counting:
    - Only count pairs within the same word (if using pre-tokenization)
    - This is how SentencePiece BPE works

  3. Approximate counting:
    - Sample the corpus instead of full counting
    - Use reservoir sampling for large corpora

Algorithm with optimization:

function train_bpe_fast(corpus, vocab_size):
    # Pre-tokenize into words
    words = pre_tokenize(corpus)

    # Convert each word to list of character tokens
    token_lists = [to_chars(w) for w in words]

    # Build initial pair count map
    pair_counts = defaultdict(int)
    for tlist in token_lists:
        for i in range(len(tlist)-1):
            pair_counts[(tlist[i], tlist[i+1])] += 1

    # Main training loop
    vocab = set(all_chars)
    while len(vocab) < vocab_size:
        # Find most frequent pair
        best_pair = max(pair_counts, key=pair_counts.get)

        # Create merged token
        new_token = ''.join(best_pair)

        # Update all token lists and pair counts
        new_pair_counts = defaultdict(int)
        for tlist in token_lists:
            # Perform this merge
            new_list = []
            i = 0
            while i < len(tlist):
                if i < len(tlist)-1 and tlist[i] == best_pair[0] and tlist[i+1] == best_pair[1]:
                    new_list.append(new_token)
                    # Add new adjacent pairs involving the merged token
                    if i > 0:
                        new_pair_counts[(tlist[i-1], new_token)] += 1
                    i += 2
                else:
                    new_list.append(new_list[-1] if new_list else None)  # handle boundaries
                    ...  # complex boundary tracking
                    i += 1
            token_lists = [new_list for each word]

        vocab.add(new_token)

    return vocab

The boundary tracking is the most complex part β€” after merging, new adjacent pairs involving the merged token must be added to the counts.

Unigram EM Algorithm (Detailed)

The Unigram EM algorithm is more complex than BPE but produces better tokenizers for multilingual use.

Forward-Backward Algorithm for computing expected counts:

For a word w = [c₁, cβ‚‚, ..., cβ‚™] (characters), we want the expected count of each token under all possible segmentations.

Forward pass:

Ξ±[i] = P(token[1:i]) = Ξ£_{j < i, w[j:i] ∈ vocab} Ξ±[j] Γ— P(w[j:i])

where Ξ±[0] = 1.0

Backward pass:

Ξ²[i] = P(w[i+1:n]) = Ξ£_{j > i, w

Vocabulary Size Tradeoffs

Large Vocabulary vs Small Vocabulary

Large vocabulary (100K-256K):

Pros Cons
Better compression (fewer tokens for same text) Larger embedding table (memory)
Fewer tokens per sequence β†’ more effective context More parameters to train
Better multilingual coverage Slower training (softmax over more classes)
More "whole words" preserved More training data needed to learn rare tokens
Lower inference latency (fewer tokens to process) Larger model checkpoint files

Small vocabulary (8K-32K):

Pros Cons
Smaller embedding table More tokens per sequence
Faster softmax (output projection) Less multilingual coverage
Better for limited hardware Frequent subword splitting
Faster tokenizer training Characters or bytes as fallback
More parameter budget for transformer layers Longer sequences reduce effective context

Efficiency vs Coverage

The coverage-efficiency tradeoff:
- Coverage: percentage of text that can be represented without splitting into small units
- Efficiency: average characters per token (higher is more efficient)

Small vocab (e.g., 32K):
  Coverage: 99.5% of English tokens are single tokens
  Efficiency: ~4.2 chars/token for English
  Efficiency: ~1.2 chars/token for Chinese

Large vocab (e.g., 128K):
  Coverage: 99.9%+ 
  Efficiency: ~4.8 chars/token for English
  Efficiency: ~2.0 chars/token for Chinese

Diminishing returns:
- Increasing vocab from 8K to 32K: big improvement
- Increasing from 32K to 128K: moderate improvement
- Increasing from 128K to 256K: small improvement
- The curve is logarithmic β€” most gains come from the first 50K tokens

Optimal vocab size (empirical):
- English-only models: 32K-50K
- Multilingual models: 100K-250K
- Code-focused models: 32K-50K (but with code-specific tokens)
- General-purpose: 128K (sweet spot for Llama 3, GPT-4)

Embedding Table Size

The embedding table is the lookup table that maps token IDs to vector representations.

Memory cost:

embedding_params = vocab_size Γ— embedding_dim Γ— dtype_bytes

Example:
  vocab_size = 128,256
  embedding_dim = 4096
  dtype = FP16 (2 bytes)
  β†’ 128,256 Γ— 4096 Γ— 2 = 1.05 billion parameters = 2.1 GB

Compare:
  vocab_size = 32,000 β†’ 32,000 Γ— 4096 Γ— 2 = 0.26 billion params = 0.5 GB

The embedding table can be 5-20% of total model parameters, depending on architecture.

Techniques to reduce embedding cost:

  1. Factorized embeddings (ALBERT):
    - Decompose embedding into V Γ— E + E Γ— H where E << H
    - Example: 128K Γ— 128 + 128 Γ— 4096 vs 128K Γ— 4096
    - Savings: 16M + 0.5M = 16.5M vs 524M (97% reduction)

  2. Tied embeddings (Weight Tying):
    - Share embedding weights with the output projection (pre-softmax layer)
    - Common in T5 and some LMs
    - Requires embedding dim to equal hidden dim
    - Saves 50% of the embedding + output parameter count

  3. Low-rank approximations:
    - Use SVD or learnable low-rank projections
    - Effective but adds computational overhead

  4. Cross-layer sharing:
    - Share embeddings across layers (used in ALBERT)


Special Tokens

BOS (Beginning of Sequence)

<s>, <|begin_of_text|>, <bos>

  • Marks the start of a sequence
  • Used in: Llama 2 (as <s>), Llama 3 (as <|begin_of_text|>)
  • Optional but helps the model identify sequence boundaries
  • Some models omit BOS (GPT-4) and rely on the chat template structure

EOS (End of Sequence)

</s>, <|end_of_text|>, <eos>, <|im_end|>

  • Marks the end of a sequence
  • Halts generation when the model outputs this token
  • Critical for: stopping generation at the right point
  • Used in: all autoregressive models
  • Llama 2: </s>, Llama 3: <|end_of_text|>

PAD (Padding)

<pad>, <|pad|>

  • Used to pad sequences to equal length in a batch
  • Attention masks exclude PAD tokens
  • Index usually 0 (but configurable)
  • Not needed for generative inference (samples processed individually)

UNK (Unknown)

<unk>, <|unknown|>

  • Represents tokens not in the vocabulary
  • Rare in modern models (byte-level BPE eliminates UNK)
  • Still used in WordPiece (BERT) and some SentencePiece models without byte fallback

MASK

[MASK], <mask>

  • Used in Masked Language Modeling (BERT-style pre-training)
  • Represents a token to be predicted
  • NOT used during fine-tuning or inference (only during pre-training)
  • Special position encoding: the model learns to "fill in the blank"

Separator Tokens

<sep>, [SEP] (BERT)
- Separates different segments in multi-segment inputs
- BERT: [CLS] sentence A [SEP] sentence B [SEP]
- The model learns segment-level relationships through attention

Chat Templates

Chat templates encode the conversation structure into token sequences.

Llama 2 format:

<s>[INST] <<SYS>>
{system_prompt}
<</SYS>>

{user_message} [/INST] {assistant_response}</s>

Llama 3 format:

<|begin_of_text|><|start_header_id|>system<|end_header_id|>
{system_prompt}<|eot_id|>
<|start_header_id|>user<|end_header_id|>
{user_message}<|eot_id|>
<|start_header_id|>assistant<|end_header_id|>
{assistant_response}<|eot_id|>

ChatML (OpenAI):

<|im_start|>system
{system_prompt}<|im_end|>
<|im_start|>user
{user_message}<|im_end|>
<|im_start|>assistant
{assistant_response}<|im_end|>

DeepSeek format:

<User: {user_message}
Assistant: {assistant_response}
<User: {next_user_message}
Assistant:

The importance of chat template consistency:
- The chat template must match exactly what was used during training
- Mismatched templates cause quality degradation
- The template is part of the tokenization process (applied before encoding)
- Libraries like HuggingFace's apply_chat_template() handle this

Tool Use Tokens

Models trained for tool use (function calling) add special tokens:

OpenAI format:

<|tool_call|>{"name": "get_weather", "arguments": {"city": "London"}}<|tool_result|>...

Claude format:

<function_calls>
<invoke function="get_weather">
<parameter name="city">London</parameter>
</invoke>
</function_calls>

GQA (General Tool Use):

<|tool|>get_weather(city="London")<|result|>...

These tokens help the model structure tool calls and parse tool results.

Vision Tokens

Multimodal models integrate vision tokens into the text token stream:

GPT-4V/GPT-4o approach:
- Images are processed by a vision encoder (ViT-like)
- The encoder outputs a grid of visual features
- These features are projected to match the language model's embedding dimension
- Visual token IDs are injected into the text token sequence

Vision token format:

Token 1: <|image_start|>
Token 2-257: <|image_patch_0|>, <|image_patch_1|>, ..., <|image_patch_255|>
Token 258: <|image_end|>

Number of vision tokens per image:
- GPT-4o: 256-1024 tokens per image (depending on resolution)
- Llama 4: ~700 tokens per image (with dynamic resolution)
- Gemini: variable (depends on image size and encoding scheme)

Position encoding for vision tokens:
- Vision tokens have their own position encoding scheme
- Typically: 2D position encoding (row, column in the image grid)
- The language model attends to vision tokens through cross-attention or concatenated token sequences


Tokenization Comparison Across Models

Vocabulary Size and Tokenizer Type

Model Vocab Size Algorithm Base Units Pre-tokenization Byte Fallback
GPT-1 40,000 BPE Unicode chars Whitespace/punctuation No
GPT-2 50,257 Byte-level BPE Bytes None N/A (byte-level)
GPT-3 50,257 Byte-level BPE Bytes None N/A
GPT-4 (cl100k) 100,256 Byte-level BPE Bytes Regex-based N/A
GPT-4o (o200k) 200,000* Byte-level BPE Bytes Regex-based N/A
Llama 1 32,000 SentencePiece BPE Unicode chars Whitespace Yes
Llama 2 32,000 SentencePiece BPE Unicode chars Whitespace Yes
Llama 3 128,256 tiktoken BPE Bytes Regex-based N/A
Claude 1/2 ~32K* SentencePiece BPE Unicode chars Unknown Yes
Claude 3/4 ~100K* Byte-level BPE* Bytes Regex-based N/A
Gemini 1.0 256,000 SentencePiece Unigram Unicode chars None Yes
Gemini 1.5 256,000 SentencePiece Unigram Unicode chars None Yes
DeepSeek-V2 129,280 Byte-level BPE Bytes Regex-based N/A
DeepSeek-V3 129,280 Byte-level BPE Bytes Regex-based N/A
Mistral 7B 32,000 SentencePiece BPE Unicode chars Whitespace Yes
Mixtral 32,000 SentencePiece BPE Unicode chars Whitespace Yes
Qwen 152,000 BPE Unicode chars Whitespace Yes
Qwen2 151,936 BPE Unicode chars Whitespace Yes
BERT 30,000 WordPiece Unicode chars Whitespace + punct No
RoBERTa 50,000 BPE Unicode chars Whitespace + punct No
T5 32,000 SentencePiece Unigram Unicode chars None Yes
mT5 250,000 SentencePiece Unigram Unicode chars None Yes

*Estimated values.

Compression Ratio Comparison

Compression ratio = characters per token (higher is better).

Language GPT-4 (cl100k) Llama 3 (128k) Llama 2 (32k) Gemini (256k) BERT (30k)
English 3.8 4.0 4.2 4.5 3.5
German 3.2 3.5 3.6 4.0 2.8
French 3.3 3.6 3.7 4.1 2.9
Spanish 3.4 3.7 3.8 4.2 3.0
Chinese 1.5 2.0 1.2 2.5 0.8
Japanese 1.8 2.2 1.3 2.8 1.0
Korean 1.7 2.1 1.2 2.6 0.9
Arabic 2.5 2.8 2.0 3.2 1.8
Russian 2.8 3.1 2.2 3.5 2.0
Hindi 2.0 2.4 1.5 2.8 1.2
Code (Python) 4.5 5.0 3.5 4.8 3.0
Code (HTML) 5.0 5.5 4.0 5.2 3.5

Key observations:
- Llama 3's 128K vocabulary provides 30-50% better compression than Llama 2's 32K for non-English
- Gemini's 256K vocabulary achieves the best compression overall, especially for CJK
- BERT's WordPiece tokenizer is the worst for CJK (approaching character-level)
- Code compression is good across most tokenizers due to repetitive patterns (indentation, keywords)

Multilingual Performance

Model English CJK Indic Arabic Code Mixing
GPT-4 Excellent Good Fair Good Excellent
Llama 3 Excellent Good Fair Good Excellent
Llama 2 Excellent Poor Poor Fair Good
Gemini Excellent Excellent Good Excellent Excellent
DeepSeek Excellent Excellent Good Good Excellent
Qwen2 Excellent Excellent Fair Good Good
BERT Excellent Fair Poor Fair Fair
mT5 Good Good Good Good Good

Tokenizer Impact on Performance

Tokenization Effects on Reasoning

Tokenization can directly affect reasoning quality:

  1. Number tokenization:
    - Multi-digit numbers should ideally be single tokens
    - GPT-4 cl100k: "12345" = 5 tokens (each digit separate!) β€” this is BAD for arithmetic
    - Llama 3: "12345" = 1-3 tokens (better)
    - Impact: models with per-digit number tokenization struggle with arithmetic
    - "5678 + 9876 = ?" β†’ model must align digit-level tokens across the addition

  2. Subword splitting artifacts:
    - "understand" = "under" + "stand" (meaningful)
    - "understand" = "und" + "erstand" (confusing)
    - Poor splits make it harder for the model to learn word semantics
    - The model must learn to compose subword representations into word meanings

  3. Sufficient statistics:
    - Each token carries ~2.8 bits of information (empirical)
    - Subword splitting means the model has fewer "information-dense" units
    - This can reduce reasoning efficiency (more tokens to process for same information)

Multilingual Capability

Tokenization is the primary bottleneck for multilingual performance:

  • English bias: Most tokenizers are trained on English-dominated corpora, so English gets the best compression
  • Token count disparity: The same paragraph translated to different languages uses very different token counts:
  • English: 100 tokens
  • Chinese: 250 tokens (GPT-4)
  • Korean: 200 tokens
  • This means Chinese text fills up context 2.5Γ— faster

Quantified bias (GPT-4 cl100k):
| Language | Tokens per 100 chars | Relative to English |
|----------|---------------------|-------------------|
| English | 26.3 | 1.0Γ— |
| German | 31.2 | 1.19Γ— |
| French | 30.3 | 1.15Γ— |
| Spanish | 29.4 | 1.12Γ— |
| Chinese | 66.7 | 2.53Γ— |
| Japanese | 55.6 | 2.11Γ— |
| Korean | 58.8 | 2.24Γ— |
| Arabic | 40.0 | 1.52Γ— |
| Hindi | 50.0 | 1.90Γ— |

This 2-2.5Γ— penalty for CJK languages means:
- A 128K context window effectively becomes 50K for Chinese
- Chinese text costs more (more tokens β†’ more compute)
- Chinese conversations hit the context limit faster

Code Understanding

Tokenization for code:

  1. Whitespace handling (Python):
    - Indentation is semantically meaningful in Python
    - Tokenizers that collapse whitespace (SentencePiece default) break Python
    - Solution: custom pre-tokenization rules for code
    - Llama 3's tokenizer handles this well

  2. Operator tokenization:
    - "==" should ideally be a single token (meaning "equals")
    - "==" = "=" + "=" (two tokens) loses the semantic unit
    - "->" should be single token in Python function annotations
    - "!=" as single token helps with code understanding

  3. Keyword tokenization:
    - "def", "class", "if", "for", "while" should ideally be single tokens
    - Most modern tokenizers handle this
    - Rare keywords in less common languages (e.g., "delegate" in C#) may be split

  4. String literal tokenization:
    - Multi-line strings in Python / template literals in JS
    - Tokenizers must handle these without losing structure
    - Byte-level tokenizers handle this better than character-level

Code tokenization comparison (100 chars of Python):

GPT-4: 22 tokens
Llama 3: 20 tokens
Llama 2: 29 tokens
Gemini: 21 tokens

Bias in Tokenization

Tokenization can introduce or amplify biases:

  1. Name tokenization bias:
    - Common English names: single token (James, Mary, John)
    - Non-English names: multiple tokens (Muhammad β†’ Muh + ammad, Wei β†’ W + ei)
    - This can create subtle bias β€” the model perceives familiar names as "simpler"

  2. Sociolect bias:
    - Different dialects tokenize differently
    - AAVE (African American Vernacular English) may tokenize more poorly than Standard English
    - This can disadvantage speakers of non-standard dialects

  3. Gender representation:
    - "he" is typically a single token, "she" also single
    - But "doctor" + "he" and "nurse" + "she" are statistical associations
    - Tokenization doesn't directly create gender bias, but can amplify it through subword associations

  4. Domain bias:
    - Legal/medical/scientific jargon that's common in training data gets single tokens
    - Less common terms get split, potentially making them harder to learn

Token Bleeding

Token bleeding occurs when the tokenizer splits a word in a way that "bleeds" information across boundaries.

Examples:
- "unethical" β†’ "un" + "ethical" (good β€” preserves meaning)
- "unethical" β†’ "uneth" + "ical" (bad β€” loses meaning, creates artifacts)
- "restaurant" β†’ "rest" + "aurant" (bad β€” "rest" has different meaning)
- "tokenization" β†’ "token" + "ization" (good β€” preserves root)
- "tokenization" β†’ "tok" + "enization" (bad β€” creates confusing prefix)

Impact:
- The model must learn to "de-bleed" β€” compose the actual meaning from subwords
- More training data is needed to overcome bad tokenization
- Token bleeding reduces the model's ability to handle novel words

Subword Splitting Artifacts

Specific artifacts caused by subword splitting:

  1. Word boundary confusion:
    - "New York" β†’ "New" + "York" (two tokens, potentially losing the compound meaning)
    - "New York" β†’ "New York" (single token β€” ideal, preserves compound meaning)

  2. Punctuation splitting:
    - "don't" β†’ "don" + "'t" (good for grammar)
    - "don't" β†’ "do" + "n't" (alternative, also common)
    - "we'll" β†’ "we" + "'ll" vs "well" (same tokens, different meaning!)

  3. Case sensitivity:
    - "USA" β†’ "US" + "A" (abbreviation split, meaning preserved)
    - "Usa" β†’ "Us" + "a" (different meaning entirely!)

  4. Number splitting:
    - "2024" β†’ "20" + "24" (loses the semantic unit)
    - "3.14" β†’ "3" + "." + "14" (mathematical constant split)


Multilingual Tokenization Challenges

Character-Based vs Subword

Character-based tokenization:
- Each character (Unicode code point) is a token
- Pros: complete coverage, no UNK tokens, fair across languages
- Cons: very long sequences (e.g., "Hello" β†’ 5 tokens, "δ½ ε₯½" β†’ 2 tokens)
- Used in: character-level models (very rare, mostly for specialized tasks)

Subword tokenization:
- Common substrings become tokens
- Pros: efficient for common patterns, balanced across languages
- Cons: language bias, subword artifacts
- Used in: all modern LLMs

Character vs subword comparison:

English: "Hello" β†’ 5 characters vs 1-2 subwords
Chinese: "δ½ ε₯½" β†’ 2 characters vs 1-2 subwords
Thai: "ΰΈͺΰΈ§ΰΈ±ΰΈͺΰΈ”ΰΈ΅" β†’ 6 characters vs 2-3 subwords
Arabic: "Ψ§Ω„Ψ³Ω„Ψ§Ω…" β†’ 4 characters vs 2 subwords

Byte Fallback Mechanisms

Byte fallback ensures that any Unicode string can be represented:

How byte fallback works:
1. Normalize text (NFKC)
2. Attempt to encode using learned subword vocabulary
3. For any remaining characters, split into UTF-8 bytes
4. Map each byte to a special <0xNN> token
5. The model learns to interpret byte sequences

Byte fallback token:

"πŸ˜€" (U+1F600 β€” GRINNING FACE)
β†’ UTF-8: F0 9F 98 80 (4 bytes)
β†’ Tokens: <0xF0>, <0x9F>, <0x98>, <0x80>
β†’ 4 tokens for a single emoji!

Trade-offs:
- Byte fallback guarantees zero UNK tokens
- But rare characters use many tokens (4 per emoji, 3 per CJK character)
- The model must learn to interpret byte sequences, which is inefficient
- Byte fallback is a "safety net" β€” ideally, most characters are covered by the vocabulary

Language Mixing

Mixed-language text (code-switching) is challenging for tokenizers:

  1. Segmentation ambiguity:
    - "Je vais au supermarket" (French + English)
    - Should "supermarket" be a French or English token?
    - The tokenizer decides based on which merge pattern wins

  2. Vocabulary overlap:
    - Many words overlap across languages (false friends, cognates, loanwords)
    - "table" exists in English and French with different meanings
    - The tokenizer assigns a single token, losing the language distinction

  3. Script mixing:
    - "HelloδΈ–η•Œ" (Latin + CJK)
    - The tokenizer must handle both scripts
    - Byte-level tokenizers handle this naturally (both are just bytes)
    - Character-level tokenizers may struggle (different base vocabularies)

  4. Transliterated text:
    - "nihao" (pinyin for δ½ ε₯½)
    - Tokenizer may treat this as English or Chinese depending on frequency

Tokenizer Bias Towards English

Why English dominates:

  1. Training data distribution:
    - Most web text is English (~50% of CommonCrawl)
    - Even "multilingual" corpora are English-heavy
    - BPE merges favor English patterns first

  2. Token position in vocabulary:
    - English words are low-numbered token IDs
    - Rare language tokens have high IDs
    - This could affect the model's preference for English tokens

  3. Compression ratio disparity:
    - English: 4 chars/token
    - Chinese: 1.5 chars/token
    - This means Chinese text uses 2.7Γ— more tokens for the same content
    - The model has fewer opportunities to "see" Chinese patterns

Mitigation strategies:
- Language-balanced tokenizer training: Sample data proportionally by language
- Language-specific vocabularies: Add dedicated tokens for each language's common subwords
- Byte-fallback: Less biased, but less efficient per-token
- Separate tokenizers per language: Rare but possible for specialized multilingual systems


Tokenization and Context Length

How Tokenizer Affects Effective Context

The relationship between text length and token count is determined by the tokenizer's compression ratio.

Effective context formula:

effective_context = nominal_context / (chars_per_token / baseline_chars_per_token)

Where baseline is usually English at ~4 chars/token

Example:
- Model with 128K token context limit
- GPT-4 tokenizer: English (4 chars/tok) β†’ 128K effective context for English
- GPT-4 tokenizer: Chinese (1.5 chars/tok) β†’ 128K Γ— (1.5/4) = 48K effective context for Chinese

Implications:
- Multilingual applications need to account for this disparity
- The "128K context" claim only applies to English
- Non-English languages get 30-50% of the advertised context

Token-to-Character Ratio Across Languages

Language-specific ratios for common tokenizers:

GPT-4 (cl100k_base):

English:      1 token β‰ˆ 3.8 characters
Chinese:      1 token β‰ˆ 1.5 characters (2.5Γ— worse)
Japanese:     1 token β‰ˆ 1.8 characters (2.1Γ— worse)
Korean:       1 token β‰ˆ 1.7 characters (2.2Γ— worse)
Arabic:       1 token β‰ˆ 2.5 characters (1.5Γ— worse)
Hindi:        1 token β‰ˆ 2.0 characters (1.9Γ— worse)
Thai:         1 token β‰ˆ 1.8 characters (2.1Γ— worse)
Russian:      1 token β‰ˆ 2.8 characters (1.4Γ— worse)

Llama 3 (128K vocabulary, improved):

English:      1 token β‰ˆ 4.0 characters
Chinese:      1 token β‰ˆ 2.0 characters (2.0Γ— worse)
Japanese:     1 token β‰ˆ 2.2 characters (1.8Γ— worse)
Korean:       1 token β‰ˆ 2.1 characters (1.9Γ— worse)
Arabic:       1 token β‰ˆ 2.8 characters (1.4Γ— worse)

Real-world implications:
- Translating English content to Chinese requires 2Γ— the tokens
- The model's context window fills up twice as fast
- For multilingual customer support, budget for 2Γ— more tokens


tiktoken Library Internals

Tiktoken is OpenAI's fast open-source tokenization library, written in Rust with Python bindings.

Architecture:

Python API (tiktoken module)
    ↓
Rust core (tokenization engine)
    ↓
C FFI (bridging Rust and Python)

Key design decisions:

  1. Rust core for speed:
    - BPE merges are applied via a trie (prefix tree) for O(length) lookups
    - Regex pre-tokenization uses the regex crate (Rust's regex library)
    - Memory: vocabulary is stored in a compact hash map

  2. Byte-level BPE:
    - UTF-8 bytes are the fundamental unit
    - No normalization step (text is used as-is)
    - Regex splits the text before BPE

  3. Rank-based BPE:
    - Each token has a "rank" (its merge order in training)
    - Lower rank = earlier merge = more preferred
    - Tokenization finds the segmentation with the lowest total rank

Core data structures:

  1. Encoder:
    - special_tokens: Map from token string to ID (for special tokens)
    - token_byte_values: Map from token ID to UTF-8 bytes
    - mergeable_ranks: Map from bytes to rank (for BPE)
    - pattern: Regex string for pre-tokenization

  2. Trie for byte encoding:
    - Characters are encoded via a byte-level trie
    - Each node represents a (byte, rank) pair
    - The trie supports longest-match-first search

Encoding algorithm (simplified):

def encode(text, encoder):
    tokens = []
    for chunk in regex_findall(text, encoder.pattern):
        # Convert chunk to bytes
        chunk_bytes = chunk.encode('utf-8')

        # Apply BPE merges using the trie
        piece_tokens = []
        i = 0
        while i < len(chunk_bytes):
            # Find longest matching token
            longest_match = None
            for j in range(i, len(chunk_bytes)):
                prefix = chunk_bytes[i:j+1]
                if prefix in encoder.mergeable_ranks:
                    longest_match = (prefix, j-i+1)
            if longest_match:
                piece_tokens.append(longest_match)
                i += longest_match[1]
            else:
                # Should never happen (byte-level coverage)
                piece_tokens.append((chunk_bytes[i:i+1], 1))
                i += 1

        # Convert piece tokens to IDs
        for piece, _ in piece_tokens:
            tokens.append(encoder.mergeable_ranks[piece])

    return tokens

Decoding algorithm:

def decode(token_ids, encoder):
    byte_parts = []
    for tid in token_ids:
        if tid == encoder.eos_token_id:
            break
        if tid in encoder.token_byte_values:
            byte_parts.append(encoder.token_byte_values[tid])
        elif tid in encoder.special_tokens:
            # Handle special tokens
            pass

    return bytes(byte_parts).decode('utf-8', errors='replace')

Available encodings:
- cl100k_base: GPT-4, GPT-3.5-Turbo, text-embedding-ada-002
- p50k_base: Codex models, text-davinci-002, text-davinci-003
- p50k_edit: For edit models (code-davinci-edit)
- r50k_base: GPT-3 (Davinci)
- o200k_base: GPT-4o (newer, larger vocabulary)

Usage (Python):

import tiktoken

# Load encoding
enc = tiktoken.get_encoding("cl100k_base")

# Encode
tokens = enc.encode("Hello, world!")
print(tokens)  # [15496, 11, 995, 0]

# Decode
text = enc.decode(tokens)
print(text)  # "Hello, world!"

# Count tokens (for context management)
n_tokens = len(enc.encode("Long text here..."))

# With special tokens
tokens = enc.encode_with_special("<|im_start|>user\nHello<|im_end|>")

Performance:
- Encoding speed: ~50 MB/s (single core)
- Decoding speed: ~100 MB/s
- Memory: ~100 MB for cl100k_base
- Concurrency: thread-safe (read-only encoder)


HuggingFace Tokenizers Library

The HuggingFace tokenizers library provides a fast, production-ready tokenizer implementation in Rust with Python bindings.

Key features:
- BPE, WordPiece, Unigram, and Byte-level BPE implementations
- Multi-threaded training
- Pre-tokenization with regex
- Post-processing (adding special tokens)
- Full serialization/deserialization
- Integrates with the HuggingFace Transformers library

Training a tokenizer (Python):

from tokenizers import Tokenizer
from tokenizers.models import BPE
from tokenizers.trainers import BpeTrainer
from tokenizers.pre_tokenizers import ByteLevel
from tokenizers.normalizers import NFKC
from tokenizers.processors import TemplateProcessing

# Create tokenizer
tokenizer = Tokenizer(BPE(unk_token="[UNK]"))

# Normalization
tokenizer.normalizer = NFKC()

# Pre-tokenization
tokenizer.pre_tokenizer = ByteLevel(add_prefix_space=True)

# Trainer configuration
trainer = BpeTrainer(
    vocab_size=32000,
    min_frequency=2,
    special_tokens=["[UNK]", "[CLS]", "[SEP]", "[PAD]", "[MASK]"]
)

# Train on files
files = ["corpus.txt"]
tokenizer.train(files, trainer)

# Post-processing
tokenizer.post_processor = TemplateProcessing(
    single="[CLS] $A [SEP]",
    pair="[CLS] $A [SEP] $B:1 [SEP]:1",
    special_tokens=[("[CLS]", 1), ("[SEP]", 2)]
)

# Save and load
tokenizer.save("tokenizer.json")
tokenizer = Tokenizer.from_file("tokenizer.json")

Training with multiple workers:

from tokenizers import ByteLevelBPETokenizer

tokenizer = ByteLevelBPETokenizer()
trainer = tokenizer.train(
    files=["data.txt"],
    vocab_size=30000,
    min_frequency=2,
    special_tokens=["<s>", "<pad>", "</s>", "<unk>", "<mask>"],
    show_progress=True
)
# Multi-threading: uses all available cores by default

Unigram tokenizer training:

from tokenizers import Tokenizer
from tokenizers.models import Unigram
from tokenizers.trainers import UnigramTrainer

tokenizer = Tokenizer(Unigram())
trainer = UnigramTrainer(
    vocab_size=32000,
    special_tokens=["[UNK]", "[CLS]", "[SEP]", "[PAD]"],
    unk_token="[UNK]"
)

files = ["corpus.txt"]
tokenizer.train(files, trainer)

WordPiece tokenizer training:

from tokenizers import Tokenizer
from tokenizers.models import WordPiece
from tokenizers.trainers import WordPieceTrainer

tokenizer = Tokenizer(WordPiece(unk_token="[UNK]"))
trainer = WordPieceTrainer(
    vocab_size=30000,
    special_tokens=["[UNK]", "[CLS]", "[SEP]", "[PAD]", "[MASK]"],
    min_frequency=2,
    limit_alphabet=1000  # controls character vocabulary growth
)

Decoding with HuggingFace tokenizers:

# Decode token IDs back to text
decoded = tokenizer.decode(ids, skip_special_tokens=True)

# Batch decoding
decoded_batch = tokenizer.decode_batch([ids_1, ids_2])

# With token offsets (for span alignment)
encoding = tokenizer.encode("Hello world!")
print(encoding.offsets)
# [(0, 5), (5, 6), (6, 11)]  # character offsets for each token

πŸ“„ Quantization β€” Comprehensive Reference β€” 1975 lines

This document provides a deep technical reference on model quantization for Large Language Models: precision formats, quantization theory, post-training methods, inference optimization, and practical deployment guidance. Written for AI engineers and ML practitioners.

AttentionBenchmarkCLIPDeploymentFine-tuningGPTGPUInferenceLLMLoRA

What is Quantization?

Quantization is the process of reducing the numerical precision of model weights and/or activations from high-precision floating-point formats (e.g., FP32, BF16) to lower-precision formats (e.g., INT8, INT4, FP8). This reduces memory footprint, bandwidth requirements, and computational cost, often with minimal accuracy degradation.

Why quantize:

Benefit Explanation
Reduced memory 4-bit weights use 8Γ— less memory than FP32
Faster inference Lower precision arithmetic is faster on modern GPUs
Higher throughput More model fits in GPU memory = larger batch sizes
Lower power Less data movement = less energy consumption
Edge deployment Models fit on phones, laptops, embedded devices
Larger models on same hardware 70B model fits on single GPU at 4-bit

The quantization challenge:
- Weights are learned in high precision (FP32/BF16)
- Reducing precision introduces error
- The goal: minimize accuracy loss while maximizing compression
- Different methods strike different balances between compression, speed, and accuracy

Key concepts:
- Weight quantization: Quantizing the model parameters (static, done once)
- Activation quantization: Quantizing intermediate layer outputs (dynamic, per-input)
- Quantization-aware training (QAT): Training with simulated quantization to recover accuracy
- Post-training quantization (PTQ): Quantizing a pre-trained model without training


Precision Formats

FP32 (32-bit Floating Point)

Format: 1 sign bit + 8 exponent bits + 23 mantissa bits
Range: Β±1.4Γ—10⁻⁴⁡ to Β±3.4Γ—10³⁸
Precision: ~7 decimal digits

S | EEEEEEEE | MMMMMMMMMMMMMMMMMMMMMMM
0 | 01111111 | 00000000000000000000000
  (124-127 bias)
  • Standard precision for model training traditionally
  • Pros: Maximum precision, no special hardware needed
  • Cons: 4 bytes per value, memory-intensive
  • Use: Baseline for comparison, mixed-precision master weights

FP16 (16-bit Float, IEEE)

Format: 1 sign bit + 5 exponent bits + 10 mantissa bits
Range: Β±6.5Γ—10⁻⁸ to Β±6.5Γ—10⁴
Precision: ~3.3 decimal digits

S | EEEEE | MMMMMMMMMM
0 | 01111 | 0000000000
  (15 bias)
  • Pros: 2Γ— memory reduction vs FP32
  • Cons: Limited range (max 65504), precision issues with small gradients
  • Use: Mixed-precision training (FP16 compute, FP32 master weights)
  • Limitations: Prone to underflow for very small values, overflow for large activations
  • Not recommended for LLM training (BF16 is preferred)

BF16 (Brain Floating Point 16)

Format: 1 sign bit + 8 exponent bits + 7 mantissa bits
Range: Same as FP32 (Β±3.4Γ—10³⁸)
Precision: ~2.3 decimal digits

S | EEEEEEEE | MMMMMMM
0 | 01111111 | 0000000
  • Developed by Google for TPU training
  • Same range as FP32 but lower precision
  • Ideal for deep learning: range matters more than precision for gradients
  • Pros: No overflow/underflow issues during training, widely supported (A100+, H100, TPU)
  • Cons: Slightly less precise than FP16 for very small values
  • Use: Standard for modern LLM training (default for Llama 3, Mistral, DeepSeek)

INT8 (8-bit Integer)

Range: -128 to 127 (symmetric), 0 to 255 (asymmetric)
Format: 8-bit two's complement integer

  • Pros: 4Γ— memory reduction vs FP32, 2Γ— vs FP16, fast integer compute
  • Cons: Very limited range, requires careful scaling
  • Use: Weight quantization, activation quantization for inference
  • Hardware support: Tensor Cores (Turing+, T4+), CPU (VNNI instructions)
  • Accuracy: Typically <1% degradation for weights-only quantization

INT4 (4-bit Integer)

Range: -8 to 7 (symmetric), 0 to 15 (asymmetric)

  • Pros: 8Γ— memory reduction vs FP32
  • Cons: Very coarse quantization, higher accuracy loss
  • Use: Extreme compression for large models, on-device deployment
  • Hardware support: Limited (some GPU tensor cores support INT4, CPU via lookup tables)
  • Accuracy: Variable, requires advanced methods (GPTQ, AWQ, GGUF k-quants)

INT2 / 2-bit (Binary/Ternary)

Range: Typically {-1, 0, 1} (ternary) or {-1, 1} (binary)

  • Extreme compression (16Γ— vs FP32)
  • High accuracy loss β€” mostly experimental
  • Use: Research, extreme edge compression

FP8 (8-bit Float)

Two variants defined by the OCP (Open Compute Project):

FP8 E4M3 (4 exponent + 3 mantissa):

S | EEEE | MMM
Range: Β±448
Precision: ~1.1 decimal digits
  • Pros: Wider range than INT8, better for activations
  • Use: H100/H200 native FP8 tensor cores, training and inference

FP8 E5M2 (5 exponent + 2 mantissa):

S | EEEEE | MM
Range: Β±57,344
Precision: ~0.6 decimal digits
  • Pros: Very wide range, good for gradients
  • Use: Gradient accumulation in FP8 training

FP8 for LLMs:
- H100 supports native FP8 matrix multiplication (2Γ— throughput vs FP16)
- Models can be trained entirely in FP8 (DeepSeek-V3 was partially FP8-trained)
- Weight-only FP8 gives near-lossless quantization
- FP8 activation quantization requires careful handling of outliers

NF4 (Normal Float 4)

Format: 4-bit non-uniform quantization, specifically designed for normally distributed weights.

Developed by QLoRA (Dettmers et al., 2023):
- Standard INT4 assumes uniform distribution β€” bad for normally distributed weights
- NF4 maps values to a distribution that matches the expected normal distribution of neural network weights
- The quantization levels are non-uniform: more levels near zero (where weights cluster) and fewer in the tails

NF4 quantization levels (16 values in range [-1, 1]):

Levels: -1.0, -0.84, -0.68, -0.52, -0.36, -0.2, -0.04, 0.0,
         0.04, 0.2, 0.36, 0.52, 0.68, 0.84, 1.0

(Actual levels differ slightly β€” this is approximate.)

Advantages:
- Better accuracy than uniform INT4 for normally distributed weights
- Specifically designed for LLM weight quantization
- Used in bitsandbytes 4-bit quantization (QLoRA)

FP4 (4-bit Float)

Format: Not standardized, typically E2M1 (2 exponent + 1 mantissa)

  • Pros: Better range than INT4 for the same bit width
  • Cons: Limited hardware support
  • Use: Experimental, research-focused
  • Comparison with NF4: NF4 is tailored for bell-shaped distributions, FP4 for exponential ones

MXFP4 (Microscaling FP4)

Part of the OCP Microscaling (MX) format specification:
- Uses a shared exponent across a block of values (e.g., 32 elements)
- E1M2 format: 1 exponent bit + 2 mantissa bits per value
- Block-level scaling: 8-bit shared scale factor per 32 elements
- Effective bit-width: ~4.25 bits per value (4 + 8/32)

Advantages:
- Hardware-friendly (shared exponent reduces compute)
- Good accuracy for block-wise patterns
- Being standardized for future hardware (AMD, Intel, Qualcomm)


Quantization Theory

Uniform Quantization

The simplest form: divide the value range into equal-sized intervals.

Affine quantization (asymmetric):

q = round((x - min) / scale)
where scale = (max - min) / (2^n - 1)

Scale quantization (symmetric):

q = round(x / scale)
where scale = max(|x|) / (2^(n-1) - 1)

Dequantization:

xΜ‚ = q Γ— scale + zero_point

Error analysis:
- For uniform quantization, the rounding error is bounded by scale/2
- Absolute error: |x - xΜ‚| ≀ scale/2
- Relative error: |x - xΜ‚| / |x| can be very large for small x
- This is why uniform quantization struggles with outliers

Quantization grid:

INT4 (symmetric):
  Levels: -8, -7, ..., -1, 0, 1, ..., 7 (range centered at 0)
  Gap between levels: scale (uniform)

INT4 (asymmetric):
  Levels: 0, 1, 2, ..., 15 (starting from 0)
  Gap between levels: scale (uniform)

Non-Uniform Quantization

Levels are not equally spaced β€” more levels in important regions, fewer in less important regions.

Logarithmic quantization:

q = round(log(|x|) / scale)
  • More levels near zero (where small values matter)
  • Fewer levels at large magnitudes
  • Good for values with exponential distribution

NF4 (Normal Float):
- Levels adapted to normal distribution
- More levels near zero, fewer in tails
- Specifically designed for LLM weights

Lattice quantization:
- Uses structured codebooks (e.g., Eβ‚ˆ lattice from QuIP#)
- Points are arranged in high-dimensional space rather than per-element
- Better packing efficiency than scalar quantization

Advantages of non-uniform:
- Better accuracy for non-uniform value distributions
- Can achieve lower bit-width for the same accuracy
- But more complex to compute (lookup tables needed)

Symmetric vs Asymmetric

Symmetric:
- Range is symmetric around zero: [-max, max]
- Zero-point = 0 (simpler, faster)
- Better for weights that are naturally symmetric (e.g., after LayerNorm)
- Used by: INT8 symmetric, NF4

Asymmetric:
- Range adapts to actual min/max values
- Has zero-point offset
- Better for activations (which may be non-negative after ReLU)
- Used by: INT8 affine quantization, most activation quantization

Comparison:

Symmetric INT4: range [-8, 7], levels at -8*scale, -7*scale, ..., 7*scale
Asymmetric INT4: range [0, 15], levels at 0, 1*scale, ..., 15*scale

For weights centered at 0: symmetric is better
For ReLU activations (all positive): asymmetric is better

Calibration

The process of determining the optimal quantization parameters (scale, zero_point) from data.

Steps:
1. Select a calibration dataset (representative of inference inputs)
2. Run the model on this dataset
3. Collect statistics of weights and activations (min, max, percentiles, distributions)
4. Compute optimal scale and zero_point from these statistics

Calibration methods:

  1. Min-max: scale = (max - min) / levels
    - Simple but sensitive to outliers
    - If one value is 100Γ— larger than others, the rest are poorly quantized

  2. Percentile: Use specified percentile (e.g., 99.9%) instead of max
    - More robust to outliers
    - 99.9% calibration clips the top 0.1% of values (introduces clipping error)

  3. KL-divergence (entropy): Minimize KL divergence between original and quantized distributions
    - Finds the optimal clipping threshold
    - Used by NVIDIA's TensorRT for INT8 calibration
    - Computes: argmin_threshold KL(original_dist || quantized_dist(threshold))

  4. MSE minimization: Minimize mean squared error
    - Find threshold that minimizes Ξ£(x - clamp(x, -t, t))Β²
    - Closed-form for Gaussian distributions

  5. Cross-entropy minimization: Minimize task loss
    - The most accurate but most expensive
    - Requires evaluating the full model on calibration data

Quantization Error

Total quantization error = rounding error + clipping error

Rounding error:
- Caused by approximating a continuous value to the nearest discrete level
- Magnitude: bounded by scale/2
- Distribution: roughly uniform in [-scale/2, scale/2]
- Can be reduced by: increasing bit-width, using non-uniform quantization

Clipping error:
- Caused by values outside the quantization range being clipped to min/max
- Magnitude: can be very large (outliers)
- More harmful than rounding error for accuracy
- Can be reduced by: widening quantization range (but this increases rounding error)

Trade-off:

Wide range: small clipping error, large rounding error
Narrow range: small rounding error, large clipping error
Optimal: balance the two based on value distribution

Outlier-Aware Quantization

LLMs are known to have activation outliers β€” a small fraction (0.1-1%) of activation values that are 10-100Γ— larger than the rest.

Outlier characteristics:
- Emerge at specific feature dimensions across sequences
- More prominent in larger models
- Grow with model size and depth
- Found in specific attention heads and hidden dimensions
- Cause severe accuracy loss if not handled properly

Strategies for handling outliers:

  1. Per-channel/per-token scaling:
    - Each channel or token has its own quantization scale
    - Outlier channels get larger ranges
    - Used by: AWQ, GPTQ with per-channel quantization

  2. Mixed-precision decomposition:
    - Keep outlier dimensions in FP16 while quantizing the rest
    - Outlier detection: dimensions where |value| > 3Οƒ from mean
    - Typically 0.1-1% of dimensions kept in high precision
    - Used by: SpQR, Outlier Suppression+

  3. SmoothQuant approach:
    - Transfer quantization difficulty from activations to weights
    - Multiply activations by a smoothing factor s
    - Divide weights by the same factor s
    - This "smooths" activation outliers
    - s chosen to balance activation and weight quantization errors

  4. Overflow-aware clipping:
    - Pre-compute clipping thresholds that minimize squared error
    - Consider both quantization noise AND outlier overflow
    - Used by: OmniQuant, AdaRound


Post-Training Quantization (PTQ) Methods

PTQ is the most practical approach β€” quantize a pre-trained model without needing to retrain it.

Overview of PTQ Methods

Method Bit-width Algorithm Calibration Data Accuracy Preservation
GPTQ 2-8 bit Hessian-based weight quantization 128 samples Excellent (4-bit)
AWQ 2-8 bit Activation-aware scaling 128 samples Excellent (4-bit)
GGUF (k-quants) 2-8 bit Importance-aware quantization Optional (imatrix) Very good
bitsandbytes 4-8 bit Block-wise NF4/FP4 None Good
AQLM 2-4 bit Additive quantization codes 256 samples Excellent (2-bit)
QuIP# 2-4 bit Incoherence + lattice codebook 256 samples Excellent (2-bit)
SpQR 3-4 bit Sparse + quantized decomposition ~512 samples Very good
HQQ 1-8 bit Half-quadratic optimization None Good

Common PTQ Workflow

1. Load pre-trained model (FP16/BF16)
2. Select calibration dataset (128-1024 samples)
3. For each layer:
   a. Collect weight statistics
   b. Optionally collect activation statistics by running calibration data
   c. Compute quantization parameters (scale, zero_point)
   d. Apply quantization (round weights to nearest quantized value)
   e. Optionally adjust weights to minimize layer output error
4. Save quantized model
5. Evaluate accuracy on benchmark tasks

GPTQ β€” Optimal Brain Quantizer

GPTQ (Frantar et al., 2023 β€” GPTQ: Accurate Post-Training Quantization for Generative Pre-trained Transformers) is one of the most widely-used weight quantization methods.

Core Idea: Optimal Brain Quantization

GPTQ is adapted from Optimal Brain Compression (OBC) , which itself is based on Optimal Brain Surgeon (OBS) β€” a classical pruning method.

OBS principle:
- Instead of independently rounding each weight, consider the Hessian (second-derivative) of the loss with respect to weights
- The Hessian tells us how important each weight is
- Quantize the least important weights first, adjust remaining weights to compensate for the error

Key insight:
- Quantization introduces error Ξ΅ into a weight vector w
- The impact on the loss function is approximately: Ξ”L β‰ˆ Β½ Ξ΅α΅€ H Ξ΅
- Where H is the Hessian matrix (βˆ‚Β²L/βˆ‚wΒ²)
- We want to choose which weights to quantize and how to adjust remaining weights to minimize Ξ”L

Mathematical Formulation

Step 1: Layer-wise quantization

GPTQ works layer by layer (or sometimes block by block):
- For each linear layer W (of shape d_row Γ— d_col):
- Find quantization q for W
- Minimize: ||Wx - qx||Β²β‚‚ for calibration inputs x
- Equivalently: minimize ||W - q||Β²_H where ||A||Β²_H = AΒ·HΒ·Aα΅€

Step 2: Hessian computation

The Hessian for a linear layer's weights:

H = 2 Β· XΒ·Xα΅€

where X is the calibration data's activation matrix for this layer.

  • Dampening: H is regularized by adding a small diagonal: H ← H + λ·I
  • Ξ» is typically set to 0.01Γ—mean(diag(H))
  • Prevents numerical instability

Step 3: Greedy quantization

For each column of W:
1. Compute the inverse Hessian H⁻¹ (using Cholesky decomposition)
2. For each weight in the column (in order of saliency):
a. Quantize the weight to its nearest quantized value
b. Compute the error: Ξ΄ = q - w
c. Update remaining (un-quantized) weights in the same column to compensate: w_remaining ← w_remaining - Ξ΄ Β· H⁻¹[remaining, i] / H⁻¹[i, i]
d. Remove the quantized dimension from H⁻¹ (using the "OBS update")

Step 4: Group-wise quantization

For better accuracy with small bit-widths:
- Weights are quantized in groups (e.g., group_size = 128)
- Each group has its own scale and zero_point
- This allows finer granularity: different parts of the weight matrix use different quantization ranges

GPTQ Algorithm Pseudocode

def gptq_quantize(W, H, bits, group_size):
    """W: weight matrix (out_dim Γ— in_dim)
       H: Hessian (in_dim Γ— in_dim)
       bits: target bit-width (e.g., 4)
       group_size: size of quantization groups (e.g., 128)
    """
    # Dampen Hessian
    damp = 0.01 * torch.mean(torch.diag(H))
    H = H + damp * torch.eye(H.shape[0], device=H.device)

    # Cholesky decomposition of H⁻¹
    H_inv = torch.inverse(H)

    # Initialize quantized weights
    Q = W.clone()
    scale = W.abs().mean() / (2^(bits-1) - 1)  # approximate scale

    # Process columns in groups
    for start in range(0, W.shape[1], group_size):
        end = min(start + group_size, W.shape[1])
        W_g = W[:, start:end]
        H_inv_g = H_inv[start:end, start:end]

        # Quantize each column
        for i in range(W_g.shape[1]):
            w = W_g[:, i]  # current column
            h_inv_diag = H_inv_g[i, i]

            # Quantize
            q = torch.round(w / scale) * scale
            q = torch.clamp(q, -2^(bits-1), 2^(bits-1) - 1) * scale

            # Compute error
            err = q - w

            # Update remaining weights in this group
            if i < W_g.shape[1] - 1:
                update = err / h_inv_diag
                W_g[:, i+1:] -= torch.outer(update, H_inv_g[i, i+1:])

            Q[:, start + i] = q

    return Q, scale

GPTQ Configurations

Common settings:
- bits: 2, 3, 4, 8 (4 is most common)
- group_size: 32, 64, 128, -1 (per-channel)
- sym: True (symmetric) or False (asymmetric)
- desc_act: True (descending activation ordering) or False

Group size impact:

group_size = 128:   Good accuracy, reasonable overhead
group_size = 32:    Better accuracy, more metadata storage
group_size = -1:    Per-channel quantization, minimal overhead

Descending activation ordering (desc_act=True):
- Sort weight columns by their activation magnitude
- Quantize the most important columns first
- This gives them the best possible quantization
- Improves accuracy for outlier-heavy layers

GPTQ Performance

Accuracy at 4-bit (Llama-2-7B):

FP16 baseline:      Wikitext2 PPL 5.12
GPTQ 4-bit (g128):  Wikitext2 PPL 5.25  (+0.13, ~0.3% degradation)
GPTQ 3-bit (g128):  Wikitext2 PPL 5.80  (+0.68, ~1.2% degradation)
GPTQ 2-bit (g128):  Wikitext2 PPL 8.52  (+3.40, ~6% degradation)

Inference speedup:
- Without GPU kernel optimization: minimal speedup (still loads as FP16 into CUDA cores)
- With GPTQ kernels (ExLlama v2, AutoGPTQ): 2-3Γ— speedup
- On CPU: 4Γ— speedup (INT4 is much faster than FP32 on CPU)


AWQ β€” Activation-Aware Weight Quantization

AWQ (Lin et al., 2023 β€” AWQ: Activation-aware Weight Quantization for On-Device LLM Compression) is a simple but effective method that uses activation statistics to guide weight quantization.

Core Insight

Not all weights are equally important. A small fraction of weights (~1%) are salient β€” they handle the large activation values. These weights should be preserved with higher precision.

Key observation:
- 0.1-1% of weight channels correspond to 99% of activation magnitudes
- Quantizing these "salient" channels causes most of the accuracy loss
- Protecting just these channels dramatically improves quantization quality

AWQ Algorithm

Step 1: Compute activation statistics

Run calibration data through the model, collecting per-channel activation magnitudes.

activation_scale[channel] = mean(|activation[channel]|) over all calibration tokens

Step 2: Identify salient channels

Channels with activation_scale > threshold_quantile (e.g., top 1%) are "salient".

Step 3: Apply per-channel scaling

Instead of keeping salient channels in high precision (complex hardware support), AWQ uses a clever trick:

  • Multiply weights of salient channels by a scaling factor s > 1
  • This effectively increases the quantization resolution for those channels
  • All channels remain quantized to the same bit-width
W'[channel] = W[channel] Γ— s[channel]

The scaling factor s is chosen per-channel to minimize:

min_s || WΒ·x - Q(WΒ·s) Β· (x / s) ||Β²β‚‚

where Q is the quantization function.

Step 4: Balance between rounding and clipping

AWQ finds the optimal scaling factor s that balances:
- Rounding error (more resolution for salient weights)
- Clipping error (non-salient weights may clip after scaling)

The optimal s is found via hyperparameter search (usually 0-2 with step 0.05).

Scaling integration:

Original: y = WΒ·x
Scaled:   y = (WΒ·s) Β· (x / s)
         = Q(WΒ·s) Β· (x / s)   (after quantization)

The scaling is "folded" into the preceding LayerNorm or linear layer during inference, so there's no extra computation.

AWQ vs GPTQ

Aspect AWQ GPTQ
Complexity Simple (scaling only) Complex (Hessian-based)
Calibration data 128 samples 128 samples
Quantization time Minutes Hours
Accuracy (4-bit) Comparable to GPTQ Slightly better
Accuracy (3-bit) Worse than GPTQ Better
Implementation Easy Complex (Cholesky, inverses)
Hardware support Excellent Good (special kernels needed)

AWQ Configurations

bits: 4 (default), 3, 2, 8
group_size: 128 (default), 64, 32, -1
zero_point: True (default), False
q_group_size: 128 (weight grouping)
w_bit: 4
version: GEMM (default), GEMV (for batch=1)

AWQ + GEMM kernel:
- Special CUDA kernel for INT4 matrix multiplication
- 3-4Γ— speedup vs FP16
- Supports batch inference (multiple sequences simultaneously)

AWQ + GEMV kernel:
- Optimized for batch=1 (single sequence generation)
- Memory-bandwidth bound scenario
- 2-3Γ— speedup


GGUF/GGML Format and k-Quants

GGUF (GPT-Generated Unified Format) is a file format for quantized models, developed for the llama.cpp project. It is the most widely-used format for CPU-based LLM inference.

GGML/GGUF File Format

GGML (old format):
- Tensor-level storage with serialization
- Supported multiple quantization types
- Limited metadata
- Superseded by GGUF

GGUF (current format, v3):
- Rich metadata at the file level
- Key-value pairs for model metadata (architecture, tokenizer, chat template)
- Backward compatibility (forward-compatible reading)
- Supports all llama.cpp features

GGUF file structure:

[GGUF Header]
  - Magic: "GGUF" 
  - Version (v3)
  - Tensor count
  - Metadata KV count

[Metadata KV Pairs]
  - general.architecture: "llama"
  - general.name: "Llama-3-8B"
  - llama.context_length: 8192
  - tokenizer.ggml.model: "gpt2"
  - tokenizer.ggml.tokens: [...]

[Padding]

[Token Data]

[Padding]

[Tensor Data]
  - Tensor name, dimensions, quantization type
  - Raw quantized weight data

k-Quant Types

The "k-quants" are a family of quantization types designed by the llama.cpp team. The 'k' stands for "k-quant" (named after the developer, Kerfuffle/Lord-Deathray).

Design philosophy:
- Importance-aware quantization
- Blocks of weights are assigned different quantization levels based on their importance
- More important blocks get higher precision, less important blocks get lower precision
- This achieves better quality than uniform quantization at the same bit-width

Available k-Quant Types

Q2_K (2.5625 bpw β€” bits per weight):

- Super-block: 4096 elements, FP16 scale
- Sub-blocks: 16 elements each
- 6-bit scale per sub-block
- Quantization: 2-bit for most weights
- Effective: ~2.56 bpw
- Use: Extreme compression, highest quality loss

Q3_K_S (3.4 bpw β€” Small):

- Single quantization level across all sub-blocks
- 3-bit quantization
- No importance splitting
- Effective: ~3.4 bpw
- Use: Smallest 3-bit size, moderate quality

Q3_K_M (3.5 bpw β€” Medium):

- 16 sub-blocks of 16 weights each
- Some sub-blocks (higher importance) use 4-bit
- Others use 3-bit
- Effective: ~3.5 bpw
- Use: Good quality/size balance at 3-bit

Q3_K_L (3.6 bpw β€” Large):

- More sub-blocks receive 4-bit quantization
- Fewer at 3-bit
- Effective: ~3.6 bpw
- Use: Highest quality 3-bit option

Q4_K_S (4.3 bpw β€” Small):

- All sub-blocks use 4-bit quantization
- Single scale per super-block
- Effective: ~4.3 bpw
- Use: Standard 4-bit, good quality

Q4_K_M (4.5 bpw β€” Medium):

- Sub-blocks split into high-importance and low-importance groups
- High-importance: 5-bit quantization
- Low-importance: 4-bit quantization
- Effective: ~4.5 bpw
- Use: Best 4-bit quality, most common choice

Q4_K_L (4.6 bpw β€” Large):

- Even more sub-blocks at 5-bit
- Effective: ~4.6 bpw
- Use: Highest quality 4-bit option

Q5_K_S (5.3 bpw β€” Small):

- All sub-blocks at 5-bit
- Effective: ~5.3 bpw
- Use: Near-lossless 5-bit

Q5_K_M (5.4 bpw β€” Medium):

- Some sub-blocks at 6-bit
- Some at 5-bit
- Effective: ~5.4 bpw
- Use: Best 5-bit quality

Q6_K (6.5 bpw):

- All weights at 6-bit
- Effective: ~6.5 bpw
- Use: Very high quality, near-lossless

Q8_K (8.5 bpw):

- 8-bit quantization
- Effective: ~8.5 bpw
- Use: Virtually lossless

Legacy Quantization Types (Deprecated)

q4_0 (4.5 bpw β€” legacy):
- 32-weight blocks
- 5-bit scale (half precision)
- No importance weighting
- 4-bit symmetric quantization
- Effective bit-width: 4.5 bpw
- Lower quality than Q4_K_M

q4_1 (4.5 bpw β€” legacy):
- 32-weight blocks
- Scale + min (asymmetric)
- 4-bit asymmetric quantization
- Similar quality to q4_0

q5_0 / q5_1:
- Similar to q4_0/q4_1 but at 5-bit
- Superseded by Q5_K variants

q2_K / q3_K / q4_K / q5_K / q6_K / q8_K:
- These are the "K" variants, though the naming scheme was refined
- The current naming (Q2_K, Q3_K_S, etc.) supersedes these

iMatrix (Importance Matrix)

An importance matrix (imatrix) is a calibration-derived matrix that tells llama.cpp which weights are more important.

How it works:
1. Run a calibration dataset (e.g., 512 text samples) through the model
2. For each layer, compute the Hessian approximation (similar to GPTQ)
3. The Hessian diagonal is the importance matrix
4. Store this matrix alongside the quantized model
5. During quantization, use the imatrix to decide which sub-blocks get higher precision

iMatrix-aware quantization:
- llama.cpp's k-quant system uses imatrix information
- Without imatrix: importance is estimated heuristically (based on weight magnitude)
- With imatrix: importance is based on actual activation statistics
- This improves quality by 0.1-0.3 perplexity points

Using imatrix:

# Generate importance matrix
./llama-imatrix -m model.gguf -f calibration.txt -o model.imatrix

# Quantize with imatrix
./llama-quantize --imatrix model.imatrix model.gguf Q4_K_M

bitsandbytes

bitsandbytes is a library for efficient LLM quantization, developed by Tim Dettmers. It provides 8-bit and 4-bit quantization optimized for GPU inference and training.

8-bit Quantization (LLM.int8())

Core algorithm β€” Vector-wise quantization:

Instead of per-tensor or per-channel quantization, bitsandbytes uses per-token and per-column quantization:

For each token's hidden states:
  - Token dimension (row): quantized independently
For each column of the weight matrix:
  - Column dimension: quantized independently

This gives fine-grained quantization scales.

Mixed-precision decomposition (LLM.int8()):

The key insight: LLMs have emergent outliers at scale β€” certain hidden dimensions have values 10-100Γ— larger than the rest.

  1. Separate weights into "regular" and "outlier" groups
  2. Outlier detection: columns where the activation value exceeds a threshold (typically 6.0)
  3. Regular values (99.9% of weights) β†’ INT8 quantization
  4. Outlier values (0.1% of weights) β†’ FP16 (kept in high precision)
y = INT8_matmul(W_regular, x_regular)  +  FP16_matmul(W_outlier, x_outlier)

Memory savings:
- Weights: 2Γ— less memory than FP16 (8-bit vs 16-bit)
- Activations: 2Γ— less memory
- Overall: ~50% reduction for inference

Performance:
- 7B model: ~1.0 perplexity point degradation (small)
- 13B model: ~0.8 PPL degradation
- 70B model: ~0.4 PPL degradation (larger models are more robust)
- Outlier dimension count: 15-50 per layer (increases with model size)

4-bit Quantization (QLoRA)

QLoRA (Dettmers et al., 2023 β€” QLoRA: Efficient Finetuning of Quantized Language Models) introduced 4-bit quantization for both inference and fine-tuning.

NF4 (Normal Float 4-bit):

The key innovation: weights in neural networks follow a normal distribution (bell-shaped, centered at 0). Standard uniform INT4 quantization wastes levels in the tails and has coarse resolution near zero (where most weights are).

NF4 uses the quantile function of the normal distribution:
- Compute the CDF of the normal distribution
- Choose 16 levels (4-bit) that are equally spaced in terms of CDF probability
- This means: levels are closer together near zero (high probability region) and farther apart in the tails (low probability region)

NF4 level computation:

# 16 levels for 4-bit quantization
quantiles = torch.tensor([0, 1/15, 2/15, ..., 15/15])
# Map to normal distribution quantiles
levels = torch.distributions.Normal(0, 1).icdf(quantiles)
# Normalize to [-1, 1]
levels = levels / levels.abs().max()

NF4 vs FP4:

NF4:  Designed for normal-distributed weights. Higher accuracy near zero.
FP4:  Designed for general use. Better dynamic range.
Empirical: NF4 is 1-3 perplexity points better than FP4 for LLM weights.

Double Quantization:

bitsandbytes also uses double quantization for the quantization scales themselves.

Why double quantization:
- Each group of weights has a quantization scale (FP32)
- With small group sizes (64-256), scales add significant overhead
- Example: group_size=64, 4096-dim layer β†’ 64 scales per row Γ— number of rows
- Each scale is 4 bytes (FP32) β†’ overhead of 256 bytes per 64Γ—16 weights = 25% overhead

Double quantization:
1. Quantize weights β†’ compute per-group scales in FP32
2. Quantize the FP32 scales into INT8 β†’ store as INT8
3. Store the 2nd-level scale in FP32 (one per 64 groups)

Group scales: FP32 β†’ INT8 (4Γ— reduction)
2nd-level scale: one FP32 per 64 groups (negligible overhead)
Total scale overhead: ~0.5% of weight size

Block-wise quantization:
- Weights within each group are quantized together
- Group size: 64 (default) or 32
- Each group has its own scale and zero_point
- Enables fine-grained adaptation to weight distribution

bitsandbytes for Inference

import torch
from transformers import AutoModelForCausalLM, BitsAndBytesConfig

# 4-bit configuration
bnb_config = BitsAndBytesConfig(
    load_in_4bit=True,
    bnb_4bit_quant_type="nf4",       # "nf4" or "fp4"
    bnb_4bit_use_double_quant=True,   # Double quantization for scales
    bnb_4bit_compute_dtype=torch.bfloat16,  # Computation precision
)

model = AutoModelForCausalLM.from_pretrained(
    "meta-llama/Llama-2-7b-chat-hf",
    quantization_config=bnb_config,
    device_map="auto",
)

Compute dtype impact:
- bnb_4bit_compute_dtype=torch.bfloat16: Matrix multiplies in BF16 (best quality)
- bnb_4bit_compute_dtype=torch.float16: Slightly faster, slightly worse quality
- bnb_4bit_compute_dtype=torch.float32: Slow but accurate

Memory requirements (4-bit with double quantization):

7B model: ~4 GB VRAM (vs ~14 GB FP16)
13B model: ~7 GB VRAM (vs ~26 GB FP16)
70B model: ~35 GB VRAM (vs ~140 GB FP16)

bitsandbytes for Training (QLoRA)

from peft import LoraConfig, get_peft_model

# LoRA configuration
lora_config = LoraConfig(
    r=16,          # LoRA rank
    lora_alpha=32,
    target_modules=["q_proj", "v_proj", "k_proj", "o_proj"],
    lora_dropout=0.05,
    bias="none",
    task_type="CAUSAL_LM",
)

# Apply LoRA to quantized model
model = get_peft_model(model, lora_config)
model.print_trainable_parameters()
# Only ~0.1% of parameters are trainable

QLoRA training characteristics:
- Base weights: 4-bit (NF4 or FP4), frozen
- LoRA adapters: BF16/FP16, trainable
- Gradients flow through the quantized weights (high precision gradients update LoRA weights)
- Memory: 4GB (7B model) β†’ allows fine-tuning on consumer GPUs
- Quality: approaches full fine-tuning quality with rank 64+


AQLM β€” Additive Quantization of Language Models

AQLM (Egiazarian et al., 2024 β€” Additive Quantization for Extremely Compressed LLMs) achieves state-of-the-art results at very low bit-widths (2-bit).

Core Idea: Additive Quantization

Instead of quantizing each weight independently, AQLM represents groups of weights as sums of codebook vectors.

Standard quantization:
- Each weight β†’ nearest quantized value
- Example: 4-bit β†’ 16 possible values per weight

Additive quantization:
- A group of weights (e.g., 8 weights) β†’ represented as sum of M codebook vectors
- Each codebook vector comes from a learned codebook matrix
- The codebook indices are quantized (not the weights directly)

W_group β‰ˆ Ξ£_m C_m[idx_m]
where:
  W_group: group of weights (e.g., 8 values)
  C_m: codebook m (e.g., 256 entries Γ— 8 dimensions)
  idx_m: code index for codebook m

Why additive quantization works:
- Exponentially more representable states per group
- 2 codebooks of 256 entries each β†’ 256Β² = 65,536 representable states for a group
- Equivalent to logβ‚‚(65536)/8 = 2 bits per weight, but with much better quality
- The additive structure captures interdependencies between weights

AQLM Architecture

Codebooks:
- Typically 2 codebooks per layer (M=2)
- Each codebook: 256 entries (8-bit index) Γ— group_dim (e.g., 8)
- Total: M Γ— 256 Γ— group_dim values per layer
- Codebooks are layer-specific (not shared)

Residual quantization:
1. First codebook captures the coarse structure of the weights
2. Second codebook captures the residual (error from first quantization)
3. More codebooks can be added for higher quality

Outlier-aware training:
- AQLM identifies outlier weights in each layer
- Outliers are quantized with higher precision (more codebooks or separate treatment)
- Improves quality at very low bit-widths

Training

AQLM requires an optimization process to learn both:
1. The codebooks (C₁, Cβ‚‚, ..., C_M)
2. The indices (idx_m for each group)

Training loop:

1. Initialize codebooks (random or k-means on weights)
2. For each iteration:
   a. For each group of weights:
      - Find optimal codebook indices (via beam search or iterative refinement)
   b. Update codebook entries to minimize reconstruction error
3. Repeat until convergence (typically 10-20 iterations)

Beam search for index assignment:
- For each group, try multiple combinations of codebook indices
- Keep the combination with lowest reconstruction error
- Beam width: 4-8 (number of candidate combinations to track)

AQLM Performance

Accuracy at 2-bit (Llama-2-7B):

FP16:              PPL 5.12
AQLM-2bit (2Γ—256): PPL 5.70 (+0.58)  β€” best 2-bit result
GPTQ-2bit:         PPL 8.52 (+3.40)  β€” much worse
QuIP#-2bit:        PPL 5.80 (+0.68)  β€” comparable

Key advantages:
- Best accuracy at 2-bit quantization
- Good 3-4 bit performance (comparable to GPTQ)
- Limitations: Slower inference (need to reconstruct weights from codebooks), larger calibration dataset needed

Inference optimization:
- Codebook lookups can be pre-computed
- For small batch sizes: reconstruct weights on-the-fly (overhead)
- For large batch sizes: reconstruct once, reuse


QuIP# β€” Incoherence Processing + Lattice Codebooks

QuIP# (Tseng et al., 2024 β€” QuIP#: Even Better LLM Quantization with Hadamard Incoherence and Lattice Codebooks) achieves excellent 2-bit results through two key innovations.

Incoherence Processing

The problem:
- Weight matrices have structure (correlations between rows/columns)
- This structure makes quantization harder β€” errors compound
- Random matrices (no structure) are easier to quantize

The solution:
- Apply a random orthogonal transformation (Hadamard matrix) to the weights
- This "whitens" the weights β€” makes them look more like random noise
- Quantization performs better on unstructured representations
- After quantization, apply the inverse transformation

W_processed = H Β· W Β· H^T
Q = quantize(W_processed)
W_quantized = H^T Β· Q Β· H

Hadamard matrix properties:
- H Β· H^T = I (orthogonal)
- H Β· (random vector) β†’ random vector with i.i.d. components (decorrelation)
- Fast multiplication: O(n log n) via Walsh-Hadamard transform
- No multiplications needed (entries are Β±1)

Benefits:
- Reduces outlier structure in weights
- Makes quantization error more uniformly distributed
- Improves 2-4 bit quality by 0.2-0.5 perplexity points

Lattice Codebooks (Eβ‚ˆ)

Standard codebook limitation:
- Codebook entries are independent points in space
- Packing efficiency is limited by the "sphere-packing" problem
- For 8-dimensional space, optimal packing uses Eβ‚ˆ lattice

Eβ‚ˆ (E8) Lattice:
- The densest sphere packing in 8 dimensions
- 240 points at the minimum distance, billions of points total
- Optimal for representing 8-dimensional vectors (like weight groups)
- Can achieve higher effective resolution per bit

Eβ‚ˆ quantization:
1. Take a group of 8 weights
2. Find the nearest Eβ‚ˆ lattice point (fast algorithm exists)
3. Store the quantized index (variable bit rate)
4. Decode: table lookup of Eβ‚ˆ point coordinates

Codebook size:
- QuIP# typically uses 2^16 (65,536) entries per codebook
- Each entry is an 8-dimensional vector
- Total: 2^16 Γ— 8 = 524,288 values per codebook
- Codebooks are fine-tuned on the incoherence-processed weights

QuIP# Algorithm

1. Compute incoherence transform H (random Hadamard matrix)
2. Apply H to weights: W̃ = H · W · H^T
3. For each 8-weight group in W̃:
   a. Search Eβ‚ˆ lattice for nearest point
   b. Store lattice index
4. Fine-tune codebook entries via k-means
5. Apply inverse transform

Performance at 2-bit:

QuIP#-2bit (Llama-2-7B): PPL 5.80  (+0.68)
vs GPTQ-2bit:             PPL 8.52  (+3.40)  
vs AQLM-2bit:             PPL 5.70  (+0.58)

SpQR β€” Sparse-Quantized Representation

SpQR (Dettmers et al., 2023 β€” SpQR: A Sparse-Quantized Representation for Near-Lossless LLM Weight Compression) combines sparsity with quantization.

Core Idea

A small fraction of weight values are outliers that contribute disproportionately to model quality. SpQR keeps these outliers in high precision (sparse storage) while quantizing the remaining weights.

SpQR pipeline:

1. Identify outlier weights (typically 1-5% of weights)
2. Outlier criteria: weight Γ— activation_magnitude > threshold
3. Store outliers in sparse format: (index, value) pairs in FP16
4. Quantize remaining (non-outlier) weights with standard method
5. During inference: reconstruct weights as sparse + quantized

Outlier selection criteria:

saliency(w_ij) = |w_ij| Γ— mean(|activation_j|) 

Where activation_j is the mean activation of weight column j over a calibration dataset.

Sparse format:
- Outlier indices: variable-length encoding (typically 2 bytes per index)
- Outlier values: FP16 (2 bytes per value)
- Overhead: ~5% Γ— (2+2) = 0.2 bytes per weight
- For a 7B model: ~1.4B Γ— 0.2 = ~280MB for sparse outliers

Quantization of non-outliers:
- 3-4 bit quantization (can use GPTQ-like approach)
- Smaller range (outliers removed) β†’ better quantization quality
- Group size: 32-128

SpQR Performance

Llama-2-7B:

FP16:             PPL 5.12
SpQR-4bit:        PPL 5.14 (+0.02)  β€” nearly lossless
SpQR-3bit:        PPL 5.28 (+0.16)  β€” very good
GPTQ-4bit:        PPL 5.25 (+0.13)  β€” SpQR is better

Trade-offs:
- Sparse storage adds irregular memory access
- Mixed-precision (sparse FP16 + INT4) is harder to optimize
- Best for scenarios where near-lossless 4-bit is needed
- Less commonly used than GPTQ/AWQ due to implementation complexity


HQQ β€” Half-Quadratic Quantization

HQQ (Badri & Shaji, 2024 β€” Half-Quadratic Quantization of Large Machine Learning Models) is a fast, calibration-free quantization method.

Core Idea

HQQ uses half-quadratic optimization to find optimal quantization parameters without requiring calibration data.

The optimization problem:

min_{scale, zero_point, Q} || W - Q ||Β²β‚‚ + λ·||Q - q(W; scale, zero_point)||Β²β‚‚

Where:
- W: original weights
- Q: quantized weights
- q(W; scale, zero_point): quantization operation
- Ξ»: regularization parameter

Half-quadratic algorithm:
1. Fix (scale, zero_point), optimize Q:
- Q = solution to quadratic form (closed form)
- Combines the original weights with the quantization constraint
2. Fix Q, optimize (scale, zero_point):
- Update scale and zero_point to best match Q
3. Repeat until convergence (typically 3-5 iterations)

Why no calibration data:
- HQQ only uses the weight matrix itself
- No calibration data or forward passes needed
- This makes it extremely fast (seconds to quantize a 7B model)
- Trade-off: slightly worse quality than calibration-based methods

HQQ Performance

Llama-2-7B:

HQQ-4bit (g128): PPL 5.32 (+0.20)
vs GPTQ-4bit:    PPL 5.25 (+0.13)
vs AWQ-4bit:     PPL 5.28 (+0.16)

Advantages:
- No calibration data needed
- Extremely fast quantization (30 seconds per billion parameters)
- Simple implementation
- Works well for most models

Disadvantages:
- Slightly lower quality than GPTQ/AWQ
- No activation awareness (cannot handle activation outliers)
- Limited to weight-only quantization


πŸ“„ Natural Language Processing: Foundations and Techniques β€” 226 lines

AttentionBERTBenchmarkCNNGPTInferenceInterpretabilityLLMRAGRL

1. Introduction

NLP is the field of computational linguistics β€” teaching computers to understand, generate, and manipulate human language. While LLMs have revolutionized NLP, the foundational techniques (tokenization, parsing, NER, dependency analysis) remain essential for understanding what LLMs do and for building production systems.

The NLP Pipeline (Classical)

Raw Text β†’ Sentence Segmentation β†’ Tokenization β†’ Normalization β†’ POS Tagging β†’ Parsing β†’ Semantic Analysis β†’ Application


2. Text Preprocessing

2.1 Tokenization (see: [02-LLMs/03-Tokenization.md])

Modern tokenizers: BPE, WordPiece, SentencePiece, Unigram, tiktoken

2.2 Normalization

  • Lowercasing (risks: "US" vs "us")
  • Stemming (Porter, Lancaster, Snowball): removing affixes
  • "running" β†’ "run", "better" β†’ "better" (too aggressive)
  • Lemmatization (WordNet, spaCy): morphological analysis
  • "running" β†’ "run", "better" β†’ "good", "was" β†’ "be"
  • Stop word removal: "the", "a", "is", "are" β€” but not always useful (BERT uses all words)

2.3 Sentence Segmentation

  • Rule-based: regex on punctuation boundaries
  • ML-based: Punkt (NLTK), spaCy sentencizer
  • Modern: trained sentence boundary detectors

3. Word Representations

3.1 Evolution

  • One-hot: D = |V| dimensional, no semantics
  • TF-IDF: Weighted by document frequency
  • Word2Vec (Mikolov, 2013): CBOW + Skip-gram, 100-300d dense vectors
  • GloVe (Pennington, 2014): Global matrix factorization
  • fastText (Bojanowski, 2017): Subword information (handles OOV)
  • ELMo (Peters, 2018): Contextualized embeddings (BiLSTM)
  • BERT (Devlin, 2019): Deeply contextualized (transformer)

3.2 Word2Vec

  • CBOW: predict masked word from context
  • Skip-gram: predict context from word
  • Training: negative sampling (noise contrastive estimation)
  • Properties: word analogies (king - man + woman = queen)

4. Syntactic Analysis

4.1 Parts of Speech (POS) Tagging

Each word assigned a grammatical category: noun, verb, adjective, etc.
- 45-50 tags in Penn Treebank
- Accuracy: >97% with modern taggers (HMM, CRF, BiLSTM)

4.2 Constituency Parsing

Tree structure representing phrase structure:
[S [NP John] [VP [V bought] [NP [D the] [N car]]]]
- CKY, Earley, Chart parsing (grammar-based)
- Neural: Transformer-based span prediction

4.3 Dependency Parsing

Directed edges between words: head-dependent relations.
root(bought) β†’ nsubj(John), dobj(car) β†’ det(the)
- Transition-based (arc-standard): fast, greedy
- Graph-based (Eisner, biaffine): slower, more accurate
- Accuracy: >95% UAS on standard benchmarks


5. Semantic Analysis

5.1 Semantic Role Labeling (SRL)

Identify predicate-argument structure: who did what to whom.
Arg0=buyer(John), Arg1=thing(car), verb=buy

5.2 Named Entity Recognition (NER)

Identify and classify named entities:
Person: John Smith
Organization: Google, OpenAI
Location: New York City
Date: June 5, 2026
Money: $100 million
- Models: BiLSTM-CRF, BERT-NER, GPT few-shot

5.3 Coreference Resolution

Resolve which noun phrases refer to the same entity:
"John went to the store. He bought milk." β†’ John = He


6. Sequence Labeling

Common NLP tasks framed as sequence labeling:
- POS tagging: each word β†’ tag
- NER: each token β†’ B-Person, I-Person, B-Org, O
- Chunking: base noun phrase boundaries

Models:
- HMM (generative, less common)
- CRF (discriminative, still used for exact inference)
- BiLSTM-CRF (deep standard, pre-transformer)
- Transformer-based (current standard)


7. Text Classification

7.1 Approaches

Method Data Performance Interpretability
Naive Bayes Very small Fair Excellent
SVM Small-Medium Good Moderate
CNN (TextCNN) Medium Good Moderate
BiLSTM Medium Good Low
BERT Large Excellent Low
GPT few-shot Very large Excellent (zero-shot) Very low

7.2 Multi-label vs Multi-class

  • Multi-class: one class per sample (sentiment: pos/neg/neu)
  • Multi-label: multiple classes possible (topics: tech+sports+finance)

8. Information Extraction

8.1 Relation Extraction

Extract relationships between entities:
"Apple CEO Tim Cook" β†’ (Person: Tim Cook, CEO_of, Organization: Apple)

8.2 Open IE

Extract relations without pre-defined schema:
"Google acquired DeepMind in 2014" β†’ (Google, acquired, DeepMind, in 2014)

8.3 Event Extraction

Identify events and their participants:
"John was born in New York in 1990" β†’ Event: birth, Person: John, Place: New York, Time: 1990


9. Machine Translation

Evolution

  • Rule-based: Hand-crafted grammar/lexicon (1960s-1990s)
  • Statistical (SMT): Phrase-based, noisy channel model (2000s)
  • Neural (NMT): Seq2Seq with attention (2015-2017)
  • Transformer: BLEU breakthrough (2018, Google's Transformer)
  • LLM-based: Zero-shot translation (2023+)
  • Speech-to-speech: Direct translation (2024+)

Key Metrics: BLEU (n-gram overlap), COMET (neural), chrF (character-level)


10. Summarization

10.1 Extractive vs Abstractive

Aspect Extractive Abstractive
Output Select sentences Generate new text
Faithfulness Guaranteed Risk of hallucination
Coherence Lower Higher
Lexical quality Variable High
Model BERT-based LLM-generation

10.2 Key Models

  • Extractive: TextRank, BERTSum, SummaRuNNer
  • Abstractive: BART, Pegasus, T5
  • Long-document: Longformer, LED, GPT-4 (200K context)

11. Question Answering

11.1 Types

  • Extractive: Answer is a span in text (SQuAD)
  • Abstractive: Answer is generated (NarrativeQA)
  • Multiple-choice: Select from options (RACE, MMLU)
  • Open-domain: Retrieve + read (Natural Questions)

11.2 RAG for QA (see: [04-RAG/01-RAG-Architectures.md])

Modern QA systems use retrieval-augmented generation to ground answers in factual documents.


12. Dialogue Systems

Type Characteristics Examples
Task-oriented Slot-filling, goal-driven Siri, Alexa, customer service
Chit-chat Open-ended conversation Eliza, Meena, BlenderBot
LLM-powered General-purpose assistant ChatGPT, Claude, Gemini

AI Library of Alexandria β€” LLMs Β· 5 Docs Β· 7,897 Lines

Home Β· June 2026