Bring the mental model from Scaled Dot-Product Attention & Transformer Layers; this page will reuse it instead of restarting from zero.
Attention & Transformers
SwiGLU: Gated MLP Blocks in Transformers
Why modern transformer MLPs often use a learned multiplicative gate: one projection proposes a token-local write while another projection controls how much of each channel reaches the residual stream.
Concept Structure
SwiGLU: Gated MLP Blocks in Transformers
Start with the picture, metaphor, or geometric mechanism.
Make the objects explicit and connect them with notation.
Mirror the equations with runnable implementation details.
Manipulate the mechanism and watch the idea respond.
Learning map
SwiGLU: Gated MLP Blocks in TransformersConceptual Bridge
What should feel connected as you move through this page.
Why modern transformer MLPs often use a learned multiplicative gate: one projection proposes a token-local write while another projection controls how much of each channel reaches the residual stream.
The next edge should feel earned: use the demo prediction here before following Sparse Mixture of Experts: Routing, Load Balancing & Expert Parallelism.
01
Intuition
Build the mental picture first so the rest of the page has something to attach to.
Canonical sources: Shazeer, "GLU Variants Improve Transformer", Chowdhery et al., "PaLM: Scaling Language Modeling with Pathways", Touvron et al., "LLaMA: Open and Efficient Foundation Language Models", and Vaswani et al., "Attention Is All You Need".
Attention is the token-mixing part of a transformer block: each token reads from other tokens. The MLP or feedforward sublayer is the token-local writing part: each token takes its own residual vector, expands it into hidden channels, applies a nonlinearity, and writes a correction back.
A plain ReLU or GELU feedforward block asks each hidden channel one question: "how active is this feature?" SwiGLU asks two questions. One projection proposes a value. A second projection produces a gate. The final hidden channel is their product after passing the gate logit through SiLU.
That small change matters conceptually. The MLP is no longer just "linear, activation, linear." It becomes a bank of conditional writes: feature channels can be suppressed, passed, or amplified depending on another learned view of the same token state.
This page is only about the channel-level mechanism. It is not a benchmark claim that SwiGLU always beats GELU, and the common two-thirds hidden-width rule is a parameter-budget comparison, not a law of nature.
02
Math
Translate the story into symbols, assumptions, and a derivation you can inspect.
Use row-vector notation for one token. Let
be the residual stream vector for that token. A standard two-matrix feedforward block with hidden width has roughly
matrix parameters, ignoring biases.
SwiGLU uses two input projections and one output projection. Choose a gated hidden width and define
The value and gate logits are
SiLU, also called Swish, is
The gated hidden vector is the elementwise product
The token-local MLP write is
The parameter count is approximately
To compare it to a two-matrix block with expansion width , set
Then
So the usual comparison is not "SwiGLU has a free extra matrix." It is "SwiGLU spends a similar budget differently: two narrower input projections create a learned multiplicative gate."
03
Code
Keep the implementation aligned with the notation so the algorithm is legible.
This witness mirrors the math exactly. It uses one token vector, two input projections, a SiLU gate, an elementwise product, and an output projection. The final assertion checks the common two-thirds budget comparison.
import numpy as np
def silu(z):
return z / (1.0 + np.exp(-z))
d_model = 6
d_ff = 12
d_ff_prime = round((2.0 / 3.0) * d_ff)
x = np.array([[0.7, -0.4, 0.2, 1.1, -0.3, 0.5]]) # shape: (1, d_model)
rng = np.random.default_rng(7)
W_v = rng.normal(0.0, 0.25, size=(d_model, d_ff_prime))
W_g = rng.normal(0.0, 0.25, size=(d_model, d_ff_prime))
W_o = rng.normal(0.0, 0.25, size=(d_ff_prime, d_model))
v = x @ W_v # shape: (1, d_ff_prime)
g = x @ W_g # shape: (1, d_ff_prime)
gate = silu(g) # shape: (1, d_ff_prime)
h = v * gate # elementwise product
y = h @ W_o # shape: (1, d_model)
relu_ffn_params = 2 * d_model * d_ff
swiglu_params = 3 * d_model * d_ff_prime
ratio = swiglu_params / relu_ffn_params
assert v.shape == (1, d_ff_prime)
assert g.shape == (1, d_ff_prime)
assert gate.shape == (1, d_ff_prime)
assert h.shape == (1, d_ff_prime)
assert y.shape == (1, d_model)
assert 0.95 <= ratio <= 1.05
channel = 3
print({
"v_i": float(v[0, channel]),
"g_i": float(g[0, channel]),
"SiLU(g_i)": float(gate[0, channel]),
"product": float(h[0, channel]),
"budget_ratio": ratio,
})
The printed channel is the scalar version of the interactive demo. One number proposes a hidden-channel write, another number controls the gate, and their product decides what reaches the output projection.
04
Interactive Demo
Use direct manipulation to connect the explanation to a moving system.
Use the Gated MLP Write demo to inspect one synthetic hidden channel. Before reveal, you can see the value projection and the gate logit , but not or the product.
Predict whether the gate suppresses, passes, or amplifies the value. Then reveal the gate coefficient, the product , and a small toy selected-channel contribution through the output projection. The full MLP write would sum many such channel contributions; this demo isolates one. The point is not to memorize a curve; it is to feel why modern transformer MLPs can act like conditional feature writers.
Live Concept Demo
Explore SwiGLU: Gated MLP Blocks in Transformers
The stage is code-native and interactive. Use it to test the explanation against the mechanism.
Manipulate one control and predict the visible change.
Commit to what SwiGLU: Gated MLP Blocks in Transformers should make visible before reading the result.
After The First Pass
Turn the concept into an inspected object.
Once the invariant is visible in the intuition, math, code, and demo, use these panels to inspect the mechanism visually, check source support, practice the idea, and attach a grounded research question.
Mechanism Storyboard
See the idea move before the page explains it
Why modern transformer MLPs often use a learned multiplicative gate: one projection proposes a token-local write while another projection controls how much of each channel reaches the residual stream.
Start with the picture, metaphor, or geometric mechanism.
Before reading further, choose the kind of change SwiGLU: Gated MLP Blocks in Transformers should make visible.
Visual Inquiry
Make the image answer a mathematical question
Why modern transformer MLPs often use a learned multiplicative gate: one projection proposes a token-local write while another projection controls how much of each channel reaches the residual stream.
Which visible object should carry the first intuition?
Pick the cue that should make SwiGLU: Gated MLP Blocks in Transformers easier to reason about before the page gives the answer.
Claim Review
Why modern transformer MLPs often use a learned multiplicative gate: one projection proposes a token-local write while another projection controls how much of each channel reaches the residual stream.
Source IDs and witness objects are attached for review; they are not proof by themselves.
Add source metadata before claiming support.
Use equation, code, and demo objects to check whether the source support is operational.
Source support candidates
No structured source note is attached yet.
Practice Loop
Try the idea before it explains itself
Why modern transformer MLPs often use a learned multiplicative gate: one projection proposes a token-local write while another projection controls how much of each channel reaches the residual stream.
Before touching the demo, predict one visible change that should happen in SwiGLU: Gated MLP Blocks in Transformers.
Reveal when your model needs a nudge.
Reveal when your model needs a nudge.
Reveal when your model needs a nudge.
A concrete answer is on the canvas.
The answer names why the claim should hold.
It touches the page context or a neighboring idea.
Research Room
Attach the question to an exact object
Pick the concept, equation, source, code witness, claim, misconception, or demo state before asking for help. The handoff stays grounded to that object.Open the draft below to save one note and next action in this browser.
SwiGLU: Gated MLP Blocks in Transformers
What is the smallest example that makes SwiGLU: Gated MLP Blocks in Transformers click without losing the math?
Local action draftNo local draft saved yetExpand only when ready to capture one local next action
This draft stays locally in this browser for concept:attention-transformers/swiglu.
- Definition, prerequisite, and contrast concept links
- The equation or code witness that makes the concept operational
- One demo state that shows the invariant instead of a slogan
- The learner can state the mechanism in their own words
- The learner can name the prerequisite that would repair confusion
- The learner can predict how the mechanism changes under one perturbation
I am working in Continuous Function's research reading room. Object: concept - SwiGLU: Gated MLP Blocks in Transformers Object key: concept:attention-transformers/swiglu Context: Attention & Transformers Anchor id: concept/concept-notebook/attention-transformers/swiglu Open question: What is the smallest example that makes SwiGLU: Gated MLP Blocks in Transformers click without losing the math? Evidence to inspect: - Definition, prerequisite, and contrast concept links - The equation or code witness that makes the concept operational - One demo state that shows the invariant instead of a slogan What would resolve this: - The learner can state the mechanism in their own words - The learner can name the prerequisite that would repair confusion - The learner can predict how the mechanism changes under one perturbation Answer as a careful research tutor: stay source-grounded, separate verified evidence from assumptions, name the relevant math objects, and end with one next action.
concept/concept-notebook/attention-transformers/swiglu
concept:attention-transformers/swiglu