| # | Topic | Section |
|---|---|---|
| 1 | Notation & Symbol Table | §1 |
| 2 | Input Embedding with Scaling | §2 |
| 3 | RMSNorm (Root Mean Square Normalization) | §3 |
| 4 | RoPE (Rotary Position Embedding) | §4 |
| 5 | YaRN (Yet another RoPE extensioN) | §5 |
Every formula in this reference uses the following consistent notation:
| Symbol | Meaning | Typical Value |
|---|---|---|
| Batch size | 1–64 | |
| Sequence length (number of tokens) | 2048–131072 | |
Model hidden dimension (d_model) |
512–7168 | |
Per-head dimension (d_head) |
64–128 | |
RoPE head dimension (d_head_rope) |
32–64 | |
| FFN intermediate dimension | ||
| KV compressed dimension (MLA) | 256–512 | |
| Q compressed dimension (MLA) | 192–1536 | |
| Number of query heads | 8–128 | |
| Number of KV heads | 2–8 | |
| Number of transformer layers | 6–72 | |
| Vocabulary size | 151,643 | |
| Number of MoE experts | 4–256 | |
| Number of active experts per token | 2–8 | |
| Sliding window size | 512–4096 | |
| Numerical stability constant | ||
| Learnable scale parameter (RMSNorm) | init: 1.0 | |
| RoPE frequency for dimension pair |
varies | |
| Token position index | 0 to |
All tensors follow the shape [Batch, Sequence, Dimension] for 3D and [Batch, Heads, Sequence, HeadDim] for 4D attention tensors.
Converts discrete token IDs (integers) into continuous vectors that the neural network can process. APEX-1 scales the embedding by
Where:
-
$\text{Embed}: \mathbb{Z}^{B \times S} \rightarrow \mathbb{R}^{B \times S \times d}$ is a lookup table of shape$[V, d]$ - The
$\sqrt{d}$ scaling ensures the initial activations have variance ≈ 1.0
Without scaling, embedding vectors have variance ≈
- Prevents vanishing activations in early layers
- Matches the expected input scale of RMSNorm
- Used by: T5, PaLM, Gemma, APEX-1
d_model = 4 (tiny example)
Vocabulary = {0: "the", 1: "cat", 2: "sat"}
Embedding table (randomly initialized):
Token 0 → [0.01, -0.03, 0.02, 0.01]
Token 1 → [-0.02, 0.04, -0.01, 0.03]
Token 2 → [0.03, 0.01, -0.04, 0.02]
Input: [1, 0, 2] → "cat the sat"
Step 1 — Lookup:
[[−0.02, 0.04, −0.01, 0.03],
[ 0.01,−0.03, 0.02, 0.01],
[ 0.03, 0.01, −0.04, 0.02]]
Step 2 — Scale by √4 = 2.0:
[[−0.04, 0.08, −0.02, 0.06],
[ 0.02,−0.06, 0.04, 0.02],
[ 0.06, 0.02, −0.08, 0.04]]
The embedding table
This saves
Normalizes activations to stabilize training. Unlike LayerNorm, RMSNorm does not center (no mean subtraction), making it 20-40% faster with equal quality.
Where:
-
$\gamma \in \mathbb{R}^d$ is a learnable scale parameter (initialized to 1.0) -
$\epsilon = 10^{-6}$ prevents division by zero
| Property | LayerNorm | RMSNorm |
|---|---|---|
| Mean centering | ✅ Yes ( |
❌ No |
| Variance normalization | ✅ Yes | ✅ Yes (via RMS) |
| Learnable parameters |
|
|
| Speed | Baseline | 20-40% faster |
| Quality | Baseline | Equal or better |
| Used by | GPT-2, BERT | Llama, DeepSeek, Qwen, APEX-1 |
Input: x = [2.0, -1.0, 0.5, 1.5] (d = 4)
γ = [1.0, 1.0, 1.0, 1.0] (initial)
ε = 1e-6
Step 1 — Compute mean of squares:
mean(x²) = (4.0 + 1.0 + 0.25 + 2.25) / 4 = 7.5 / 4 = 1.875
Step 2 — Compute RMS:
RMS = √(1.875 + 1e-6) = √1.875001 ≈ 1.3693
Step 3 — Normalize:
x / RMS = [2.0/1.3693, -1.0/1.3693, 0.5/1.3693, 1.5/1.3693]
= [1.4606, -0.7303, 0.3651, 1.0954]
Step 4 — Scale by γ:
output = [1.4606, -0.7303, 0.3651, 1.0954] × [1, 1, 1, 1]
= [1.4606, -0.7303, 0.3651, 1.0954]
Verification: mean(output²) = (2.133 + 0.533 + 0.133 + 1.200)/4 ≈ 1.0 ✓
RMSNorm appears twice in every transformer block:
- Pre-attention norm:
RMSNorm(x)→ Attention - Pre-FFN norm:
RMSNorm(x')→ FFN
Plus once at the end: Final norm before the LM head.
Total:
Encodes position information into Query and Key vectors by rotating pairs of dimensions. The rotation angle depends on position, so the model learns relative distances between tokens.
For each pair of dimensions
Where:
-
$m$ = position index (0, 1, 2, ...) -
$\theta_i = \frac{1}{b^{2i/d_h}}$ = frequency for dimension pair$i$ -
$b = 10{,}000$ = base frequency (standard)
| Dim pair |
|
Wavelength | What it encodes |
|---|---|---|---|
| 0 | 1.0000 | 6.28 | Very local (adjacent tokens) |
| 1 | 0.0316 | 198.7 | Short-range (phrases) |
| 2 | 0.0010 | 6,283 | Medium-range (paragraphs) |
| 3 | 0.00003 | 198,692 | Long-range (document-level) |
Key insight: Low dimensions rotate fast (local patterns), high dimensions rotate slow (global patterns).
The full rotation can be written compactly as:
Where
The inner product of two rotated vectors depends only on the relative distance:
This means: attention scores naturally depend on how far apart two tokens are, not their absolute position. This is crucial for generalization to longer sequences.
d_head = 4, base = 10000
Position m = 3
Step 1 — Compute frequencies:
θ₀ = 1 / 10000^(0/4) = 1.0
θ₁ = 1 / 10000^(2/4) = 1 / 100 = 0.01
Step 2 — Compute angles at position 3:
angle₀ = 3 × 1.0 = 3.0 rad
angle₁ = 3 × 0.01 = 0.03 rad
Step 3 — Compute cos/sin:
cos = [cos(3.0), cos(3.0), cos(0.03), cos(0.03)]
= [−0.990, −0.990, 0.9996, 0.9996]
sin = [sin(3.0), sin(3.0), sin(0.03), sin(0.03)]
= [0.141, 0.141, 0.030, 0.030]
Step 4 — Apply to query q = [1.0, 0.5, 0.3, -0.2]:
rotate_half(q) = [-0.5, 1.0, 0.2, 0.3]
q' = q × cos + rotate_half(q) × sin
= [1.0×(−0.990) + (−0.5)×0.141,
0.5×(−0.990) + 1.0×0.141,
0.3×0.9996 + 0.2×0.030,
(−0.2)×0.9996 + 0.3×0.030]
= [−1.061, −0.354, 0.306, −0.191]
Note: magnitude is preserved: ||q|| = ||q'|| ≈ 1.187 ✓
RoPE cos/sin tables are computed once at model init and reused:
# Precompute for all positions up to max_seq_len
cos_cache[m, 2i] = cos_cache[m, 2i+1] = cos(m × θᵢ)
sin_cache[m, 2i] = sin_cache[m, 2i+1] = sin(m × θᵢ)
# Shape: [max_seq_len, d_head]Extends a model trained at context length
Naive extrapolation fails because:
- High-frequency dimensions see angles they never trained on → noise
- Simply dividing all frequencies by
$s$ breaks local syntax patterns
YaRN's insight: different dimensions need different treatment.
| Regime | Condition | Action | Rationale |
|---|---|---|---|
| High-frequency | wavelength |
No scaling | Local syntax works at any length |
| Low-frequency | wavelength |
Full scaling: |
Long-range position needs compression |
| Mid-frequency | Between the two | Smooth interpolation | Gradual transition |
Default:
For mid-frequency dimensions:
Where
At long context, attention entropy increases (scores become more uniform). YaRN counteracts this with a temperature correction:
This is multiplied into attention scores to maintain sharpness.
Scale factor s = 128K / 4K = 32
β_fast = 32, β_slow = 1
Dimension pair i=0: θ = 1.0, λ = 6.28
λ = 6.28 < 32 (β_fast) → HIGH-FREQ → no scaling
θ'₀ = 1.0 (unchanged)
Dimension pair i=15: θ = 0.0001, λ = 62,832
λ = 62,832 > 1 × 32 = 32 (β_slow × s) → LOW-FREQ → full scaling
θ'₁₅ = 0.0001 / 32 = 0.000003125
Dimension pair i=8: θ = 0.01, λ = 628
628 > 32 but 628 < 32 → MID-FREQ → interpolate
t = (628/1 - 1) / (32 - 1) = 627/31 ≈ 20.2 → clamped
(In practice, interpolation weights depend on exact values)
Temperature correction:
attn_factor = 0.1 × ln(32) + 1.0 = 0.1 × 3.466 + 1.0 = 1.347
| Model | Training Context | Extended Context | Method |
|---|---|---|---|
| Llama 2 | 4K | 32K | Position interpolation |
| KIMI | 128K | 1M+ | YaRN |
| DeepSeek-V3 | 128K | 128K | Native long (b=1M) |
| APEX-1 Small | 8K | 32K | YaRN (s=4) |
| APEX-1 Large | 128K | 1M+ | YaRN (s=8+) |
| Component | Formula | Params |
|---|---|---|
| Embedding | ||
| RMSNorm | ||
| RoPE freq | 0 (fixed) | |
| RoPE apply | 0 (fixed) | |
| YaRN scale | 0 (fixed) | |
| YaRN temp | 0 (fixed) |
Continue to Part 2: Attention (MLA, GQA+SW, Masks) →