| # | Topic | Section |
|---|---|---|
| 12 | SwiGLU Feed-Forward Network | §12 |
| 13 | Mixture of Experts (MoE) | §13 |
| 14 | Auxiliary-Loss-Free Load Balancing | §14 |
| 15 | Dynamic Skip Gate | §15 |
| 16 | Multi-Token Prediction | §16 |
The FFN transforms each token's representation through a non-linear projection. SwiGLU uses gating — one branch decides what to keep, another provides the values.
Where:
- $\mathbf{W}{\text{gate}} \in \mathbb{R}^{d \times d{ff}}$ — gate branch
- $\mathbf{W}{\text{up}} \in \mathbb{R}^{d \times d{ff}}$ — value branch
- $\mathbf{W}{\text{down}} \in \mathbb{R}^{d{ff} \times d}$ — projection back
-
$\text{SiLU}(x) = x \cdot \sigma(x)$ — Sigmoid Linear Unit -
$\odot$ — elementwise multiplication (gating) -
$d_{ff} = \frac{8}{3} d$ — intermediate dimension
| Behavior | |||
|---|---|---|---|
| -3.0 | 0.047 | -0.143 | Near zero (suppressed) |
| -1.0 | 0.269 | -0.269 | Slightly negative |
| 0.0 | 0.500 | 0.000 | Zero |
| 1.0 | 0.731 | 0.731 | Slightly below identity |
| 3.0 | 0.953 | 2.858 | Near identity |
| FFN Type | Formula | Params | Perplexity |
|---|---|---|---|
| ReLU FFN | Baseline | ||
| GELU FFN | -0.5% | ||
| SwiGLU | -1.5% |
SwiGLU uses 50% more parameters but wins ~1-2% perplexity. The FFN dimension is reduced to
d = 4, d_ff = 3
x = [1.0, -0.5, 0.3, 0.8]
W_gate (4×3): W_up (4×3):
[[0.5, -0.3, 0.1], [[0.2, 0.4, -0.1],
[0.2, 0.4, 0.3], [0.1, -0.2, 0.5],
[-0.1, 0.2, 0.5], [0.3, 0.1, 0.2],
[0.4, -0.1, 0.2]] [-0.2, 0.3, 0.1]]
Step 1 — Gate branch: x @ W_gate
[1×0.5+(-0.5)×0.2+0.3×(-0.1)+0.8×0.4, ...]
= [0.69, -0.36, 0.31]
Step 2 — Apply SiLU:
SiLU([0.69, -0.36, 0.31]) = [0.474, -0.163, 0.186]
Step 3 — Value branch: x @ W_up
= [0.01, 0.39, -0.23]
Step 4 — Gate × Value (elementwise):
[0.474×0.01, -0.163×0.39, 0.186×(-0.23)]
= [0.005, -0.064, -0.043]
Step 5 — Project back: result @ W_down → [d] output
Replaces a single large FFN with
Where:
- Router: $\mathbf{r} = \mathbf{x} \cdot \mathbf{W}{\text{router}} + \mathbf{b}{\text{balance}} \in \mathbb{R}^E$
- Top-K selection:
$(e_1, \ldots, e_K) = \text{top-}K(\mathbf{r})$ - Routing weights:
$g_k = \text{softmax}(r_{e_1}, \ldots, r_{e_K})_k$
| Parameter | Small | Medium | Large |
|---|---|---|---|
| Total experts |
4 | 64 | 256 |
| Active experts |
2 | 8 | 8 |
| Shared experts | 1 | 2 | 2 |
| MoE layer frequency | Every odd layer | Every odd layer | Every odd layer |
| Active/Total ratio | 50% | 12.5% | 3.1% |
APEX-1 Large: 256 experts, 8 active → 900B total params but only 45B active per token. 20× parameter efficiency.
x = [0.5, -0.3] (d=2, simplified)
W_router (2×4): (4 experts)
b_balance = [0.0, 0.0, 0.0, 0.0] (initially)
Step 1 — Router logits:
r = x @ W_router + b_balance
r = [1.2, -0.3, 0.8, 0.1]
Step 2 — Top-K (K=2):
Top-2 indices: [0, 2] (values 1.2 and 0.8)
Step 3 — Routing weights (softmax over selected):
softmax([1.2, 0.8]) = [exp(1.2)/(exp(1.2)+exp(0.8)),
exp(0.8)/(exp(1.2)+exp(0.8))]
= [0.599, 0.401]
Step 4 — Combine expert outputs:
output = 0.599 × Expert_0(x) + 0.401 × Expert_2(x)
(Experts 1 and 3 are NOT computed — zero FLOPs for them)
Without balancing, routers develop "winner-take-all" patterns — a few experts get most tokens while others are starved.
Add auxiliary loss:
Problem:
Update a per-expert bias outside the gradient computation:
After each optimizer step:
- Count tokens per expert:
$c_e = |{t : e \in \text{top-K}(t)}|$ - Observed load:
$f_e = c_e / (N \times K)$ - Target load:
$\tau = 1/E$ - Update:
$b_e \leftarrow b_e + \alpha \cdot \text{sign}(\tau - f_e)$ - Clamp:
$b_e \leftarrow \text{clamp}(b_e, -1, 1)$
| Expert Status |
|
Bias change | Effect | |
|---|---|---|---|---|
| Overloaded |
|
Router scores this expert lower | ||
| Underloaded |
|
Router scores this expert higher | ||
| Balanced | No change | Equilibrium |
E = 4 experts, K = 2, N = 8 tokens, α = 0.1
Target rate τ = 1/4 = 0.25
Initial bias = [0, 0, 0, 0]
After step 1:
Token assignments: Expert 0 got 5 tokens, Expert 1 got 4,
Expert 2 got 3, Expert 3 got 4
Total assignments = 8 × 2 = 16
Observed rates: [5/16, 4/16, 3/16, 4/16] = [0.313, 0.250, 0.188, 0.250]
Deltas (τ - f_e): [-0.063, 0.000, 0.062, 0.000]
Signs: [-1, 0, +1, 0]
Bias update (α=0.1): [-0.1, 0.0, +0.1, 0.0]
New bias: [-0.1, 0.0, +0.1, 0.0]
Effect: Expert 0's router scores drop by 0.1 → fewer tokens routed
Expert 2's router scores increase by 0.1 → more tokens routed
A learned gate that decides per-token whether to skip the FFN entirely. Simple tokens (punctuation, articles) don't need the FFN.
Where
Problem: The threshold comparison
Solution: STE — forward uses hard threshold, backward passes gradient through as identity:
| Token Type | Gate Value | Skipped? | Why |
|---|---|---|---|
| "the" | 0.05 | ✅ Yes | Article — no complex semantics |
| "," | 0.03 | ✅ Yes | Punctuation — trivial |
| "however" | 0.42 | ❌ No | Discourse marker — changes meaning |
| "mitochondria" | 0.89 | ❌ No | Domain knowledge required |
| "\n\n" | 0.08 | ✅ Yes | Formatting — trivial |
At convergence: 25-35% of tokens skip with < 0.3% quality loss.
FFN is ~67% of each block's compute. With 30% skip rate:
Instead of predicting only the next token, APEX-1 predicts the next
Where:
- $L_{\text{main}} = \text{CE}(\text{logits}{t}, \text{token}{t+1})$ — standard next-token
- $L_{\text{offset}_k} = \text{CE}(\text{head}_k(\mathbf{h}t), \text{token}{t+k})$ — predict
$k$ steps ahead -
$\lambda = 0.1$ — speculative heads contribute 10% gradient weight -
$N = 4$ — predict 4 tokens ahead
Each
At inference, the speculative heads draft tokens that the main model verifies:
Step 1: Main model generates token t₁
Step 2: Spec heads draft [t₂', t₃', t₄', t₅'] from hidden state
Step 3: Main model verifies all 4 drafts in ONE forward pass
Step 4: Accept matching prefix, resample at first mismatch
If 3 out of 4 drafts match → 4 tokens generated in ~1.3 forward passes
Normal: 4 tokens = 4 forward passes
Speedup: 4 / 1.3 ≈ 3× throughput
| Task | Acceptance Rate | Effective Speedup |
|---|---|---|
| Code completion | 70-85% | 2.5-3.5× |
| Factual Q&A | 50-65% | 2.0-2.5× |
| Creative writing | 30-45% | 1.5-2.0× |
| Translation | 55-70% | 2.0-3.0× |
| Component | Formula | Params |
|---|---|---|
| SwiGLU | ||
| MoE output | ||
| Router |
|
|
| Load balance | 0 (no gradient) | |
| Skip gate | ||
| Multi-token |
← Part 2: Attention | Continue to Part 4: Training, Alignment & Complete Pipeline →