Architecture Companion · One Input, Two Projections: SiLU and SwiGLU
SiLU is an elementwise function. SwiGLU is a structure containing projections, activation, and multiplication. Similar names do not make a single function and a two-branch network interchangeable.
The formulas use row vectors without biases; matrices are teaching values. This is a conceptual explanation, not a model-replacement training or performance comparison.
Locate it between the second normalization and residual addition
Open the full forward diagram and locate steps 13–15
Block input X → Norm₁ → attention → output projection O → R = X + O → Z = Norm₂(R) → F = MLP(Z) ← SwiGLU lives here → Y = R + F → next BlockThe MLP receives Z, while the bypass adds R—not Z and not X again. Final normalization occurs after all Blocks and is distinct from this Norm₂.
For Z:[B,T,D]=[2,5,16], the MLP processes ten sixteen-dimensional vectors separately, using the same parameters at every position. It does not aggregate across Tokens, but Z already carries context from attention.
SiLU: compute Sigmoid, then multiply the input
sigmoid(x) = 1 / (1+exp(−x))SiLU(x) = x × sigmoid(x)| x | sigmoid(x) | SiLU(x) |
|---|---|---|
| −3 | 0.047426 | −0.142278 |
| −1 | 0.268941 | −0.268941 |
| 0 | 0.5 | 0 |
| 1 | 0.731059 | 0.731059 |
| 2 | 0.880797 | 1.761594 |
| 3 | 0.952574 | 2.857722 |
The Sigmoid coefficient lies between zero and one; SiLU’s output may be negative or exceed one. Large positive inputs are approximately retained, very negative inputs approach zero, and the function changes smoothly around zero. Standard SiLU has no trainable parameters and applies independently to each element without changing shape. It is not softmax and imposes no sum-to-one constraint. See the PyTorch SiLU definition.
Why put a nonlinearity between linear layers?
Two linear maps collapse into one: (zW₁)W₂=z(W₁W₂). Including biases still produces one affine map. SiLU prevents that collapse; for example, SiLU(2) ≠ 2SiLU(1).
Unlike ReLU’s zeroing of all negative inputs, SiLU retains compressed negative values smoothly. This does not prove it eliminates vanishing gradients or always outperforms other activations. In SwiGLU, multiplying two input-dependent branches also contributes nonlinearity.
Where the two branches come from
The same complete input z multiplies two different matrices:
a = zW_g gate projectionv = zW_v value projectiong = SiLU(a) input-dependent gateu = g ⊙ v elementwise productF = uW_d projection back to the original dimension z / \ ×W_g ×W_v ↓ ↓ a v ↓ SiLU │ g │ └── ⊙ ─┘ ↓ u ↓ ×W_d FThe branches are not halves of z or different batches. They can produce different results because W_g and W_v differ—not because SiLU creates two branches.
For intermediate feature j, u_j=SiLU(zW_g)_j × (zW_v)_j. “Feature” and “modulation” describe computational roles, not predetermined meanings for individual dimensions. Both projections learn through the same final task loss.
The gate is not a binary switch: it can suppress, amplify, or reverse a feature’s sign. This MLP v is not attention’s V and does not reuse the KV cache.
Shapes and parameters at every step
Use teaching dimensions D=16, F_width=48. Width 48 is not a universal Llama setting.
| Step | Parameters | Output shape |
|---|---|---|
| a=ZW_g | W_g: [16,48] | [2,5,48] |
| v=ZW_v | W_v: [16,48] | [2,5,48] |
| g=SiLU(a) | None added | [2,5,48] |
| u=g⊙v | None added | [2,5,48] |
| F=uW_d | W_d: [48,16] | [2,5,16] |
| Y=R+F | None added | [2,5,16] |
Expansion generates features through projection; it is not padding or copying. The symbol ⊙ means corresponding-element multiplication, not matrix multiplication. The output projection mixes intermediate features and returns to D for addition to R. PyTorch Linear normally stores weights as [output features,input features], transposed relative to the row-vector formulas here.
A complete calculation from z to F
Reduce to D=F_width=2 for hand calculation. This demonstrates branching, not expansion.
z = [1,2]W_g = [[1,0], W_v = [[2,0], W_d = [[1, 1], [0,1]] [0,3]] [1,−1]]Project first:
a = [1×1+2×0, 1×0+2×1] = [1,2]v = [1×2+2×0, 1×0+2×3] = [2,6]Apply SiLU only to a:
g = [1/(1+e⁻¹), 2/(1+e⁻²)] ≈ [0.731059,1.761594]Multiply corresponding elements:
u = [g₀×2, g₁×6] ≈ [1.462117,10.569565]Project the result:
F = [u₀+u₁, u₀−u₁] ≈ [12.031682,−9.107448]This is the MLP output, before the residual addition. Use the preserved upstream R in Y=R+F; do not assume z equals R. Intermediate values remain unrounded until display.
A negative input also clarifies the gate: SiLU(−1)≈−0.268941, so the gate itself can be negative. The sign after multiplication also depends on the corresponding value in the other branch.
Compared with a GELU MLP: more than a function replacement
GPT-2:F = GELU(zW₁+b₁)W₂+b₂
SwiGLU (bias-free here):F = [SiLU(zW_g) ⊙ (zW_v)]W_d| Property | Two-layer GELU MLP | SwiGLU |
|---|---|---|
| Input projections | One | Two, both reading the complete input |
| Weight matrices | Two | Three |
| Intermediate operations | GELU | SiLU and elementwise multiplication |
| Output | Returns to D | Returns to D |
| Cross-Token aggregation | None | None |
Let intermediate widths be F_G and F_S. Ignoring biases, weight counts are 2DF_G and 3DF_S. At equal width, SwiGLU has 50% more weights. Matching counts requires F_S=2F_G/3. A conventional width of 4D corresponds to F_S=8D/3, before implementation-specific adjustments and alignment.
The teaching shapes 16→64→16 and two branches of 16→48→16 contain 2048 and 2304 weights respectively. They are not parameter-matched. These counts cover only MLP weights, not the entire model.
The GLU variants paper found quality improvements for some gated variants in its Transformer experiments. That supports the structural choice, not a proof of superiority on every task. In the original Llama 3 FeedForward implementation, w1 is the gate projection, w3 the other input projection, and w2 the output projection.
Where learning occurs: the product splits the gradient
W_g, W_v, and W_d are parameters. The tensors a, v, g, u, and F are current intermediate results. Although g controls modulation, it is not a separately maintained parameter table.
For one element u=g×v, with upstream gradient δ:
∂L/∂g = δv∂L/∂v = δg
SiLU′(a) = sigmoid(a) + a sigmoid(a)(1−sigmoid(a))∂L/∂a = δv × SiLU′(a)Both branches differentiate through their projections, and their contributions add at the shared input z. W_d receives output-side gradients; W_g/W_v receive branch gradients. All three learn under one task objective. The architecture defines computation paths; training determines parameter values.
Keep three distinctions when reviewing: SiLU versus SwiGLU, parameters versus gate values, and MLP output F versus residual output Y. Place them back in the full Block to reconnect gating with attention, normalization, and residual paths.