arXiv preprint. Section 2 defines SwiGLU as (Swish(xW) ⊗ xV)W₂ with three weight matrices and reduces the hidden width to two thirds of the original to keep parameters and computation constant.
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.
Intuition
The idea in plain words, before the symbols.
Main sources: Shazeer (2020), "GLU Variants Improve Transformer", Chowdhery et al. (2023), "PaLM: Scaling Language Modeling with Pathways", Touvron et al. (2023), "LLaMA: Open and Efficient Foundation Language Models", and Vaswani et al. (2017), "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, amplified or reversed in sign, 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.
Study prompts for this section: Intuition
Each button copies a prompt about this section to your clipboard, for an AI assistant you already use. Nothing is sent by this site.
Mathematics
Definitions, assumptions and the derivation.
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."
One channel, worked by hand
Hold one hidden channel's value at and change only its gate logit. At , , so , and . At the sigmoid is still positive, , but SiLU also multiplies by the negative logit: and . The gate has reversed the sign of the value. It cannot push it far the other way: for , lies between about (its lowest point, at ) and . The sign of one channel before does not fix the sign of , which mixes from every channel.
The budget rule is exact in a small case. A bias-free plain block with and has matrix entries. A bias-free SwiGLU block with has . This is how Shazeer (2020, §2) keeps the parameter count and computation fixed when comparing variants; it counts matrix entries, not speed, activation memory or quality.
Study prompts for this section: Mathematics
Each button copies a prompt about this section to your clipboard, for an AI assistant you already use. Nothing is sent by this site.
Code
The same calculation as runnable code, in the notation of the derivation.
This code follows the math step by step. 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.
Study prompts for this section: Code
Each button copies a prompt about this section to your clipboard, for an AI assistant you already use. Nothing is sent by this site.
Demo
Change an input, predict the result, and see which quantities respond.
Explore SwiGLU: Gated MLP Blocks in Transformers
Observation guide
Optional and ungraded. Opening a guide saves nothing.
Choose what to inspect in SwiGLU: Gated MLP Blocks in Transformers, then open its guide.
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 reverses, 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 but to see why a gated MLP can decide, channel by channel, what to write.
Study prompts for this section: Demo
Each button copies a prompt about this section to your clipboard, for an AI assistant you already use. Nothing is sent by this site.
When the gate turns a value around
Hold one channel's value fixed, flip the sign of its gate logit, then count every matrix in two equal budgets.
Concept: SwiGLU: Gated MLP Blocks in Transformers
What is the smallest example of SwiGLU you can work through by hand, and what does it show?
DetailsAttention & Transformers
Reference id
concept:attention-transformers/swigluAfter the first pass
Go deeper
Optional material for a second reading. Sources are always shown; the other panels open from their headings or from these links, and closing one keeps what you wrote or revealed.What to look for in each sectionChoose a section and what you expect to see in it, then read short advice.
Section by section
Choose what to look for in each section
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.
Choose what you expect to see in this section of SwiGLU: Gated MLP Blocks in Transformers.
Questions to ask of the pictureChoose a part of the picture and what you expect, then read short advice.
Looking at the picture
Questions to ask of the picture
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 part of the picture should you look at first?
Pick the part of the picture you expect to explain SwiGLU: Gated MLP Blocks in Transformers.
Sources
References for this page
A reference shows where an idea comes from; it does not vouch for every step on this page.NeurIPS 2017. Section 3.3 defines the position-wise feed-forward block: two linear maps with a ReLU between them.
JMLR 2023. Section 2 lists SwiGLU as the activation in PaLM's feed-forward blocks.
arXiv preprint. Section 2.2 replaces ReLU with SwiGLU and uses a hidden width of two thirds of 4d.
What each claim rests on1 claim, each with its sources, where it appears on this page, and its caveats.
Claims and sources
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 checks were made by this site, not by an independent reviewer.
Shazeer (2020, §2) defines SwiGLU with three weight matrices and reduces the hidden width to two thirds to keep parameters and computation constant. The sign reversal follows from SiLU(g) = g·σ(g).
v = 4, g = ±ln 3 and the 36-entry blocks are chosen for exact arithmetic. One channel before W_o, not the block's output; the count says nothing about speed or quality.
Section 2 of Shazeer (2020) was read for Equation 6, the three-matrix form and the two-thirds width. The ±ln 3 values, SiLU's lowest point and the 36-versus-36 counts were recomputed independently.
Explain it without the pageWrite your own explanation before asking for feedback. Your draft and any hints stay while this page is open.
Practice · SwiGLU: Gated MLP Blocks in Transformers
Try the idea in your own words
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 · Selected for practice
SwiGLU: Gated MLP Blocks in Transformers
What it rests on: Sources: GLU Variants Improve Transformer; Attention Is All You Need; PaLM: Scaling Language Modeling with Pathways
Context and links
Attention & Transformers
Explain the mechanism
For SwiGLU: Gated MLP Blocks in Transformers: What is the smallest example of SwiGLU you can work through by hand, and what does it show? Explain your answer, including what changes, why, and which assumption matters.
A rough first thought is enough. Your draft stays when you change tasks.
Your draft stays on this page and is cleared when you leave or reload.
A little help · Explain
Open one hint at a time. These are suggestions, not your answer or a grade.
0 of 3 hints shown for this question.
Clearing your answer keeps this help record. Outside help is your own report; this page cannot check it.
Where am I stuck? (optional)
Take your draft to a feedback conversation
No AI feedback runs here. You can copy a prompt to use elsewhere; nothing is sent automatically. Review the text before sharing, and leave out private information.
Write an attempt before copying a feedback prompt.
A draft, or an AI reply to it, is not a test of what you have learned. To check that, try a different case later without help.
Ask about this pagePick one item on this page and copy a prompt about it into an AI assistant you already use. Notes you write here stay in this browser.
This code follows the math step by step. It uses one token vector, two input projections, a SiLU gate, an elementwise product, and an output projec...Your question
Choose what your question is about
Pick the idea, equation, source, code, claim, misconception or demo state you want to ask about. The prompt you copy includes it, so the answer can stay on that item.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 of SwiGLU you can work through by hand, and what does it show?
Fixed prompts generated from this item. They are not comments from people or independent reviews.
The 4 cited sources must support this exact item, not just the surrounding topic.
Connect the definition to one equation, piece of code or demo before widening the discussion.
Change one input, then check that the same relationship holds in the mathematics, the code and the demo.
You can state the mechanism in your own words
Local action draftNo local draft saved yetOpen when you are ready to write one next action
This draft stays in this browser and is attached to this item only.
- What the 4 cited sources say about this exact item
- The definition, its prerequisites, and a contrasting concept
- The equation or code that makes the concept concrete
- One demo state that shows what stays the same as the inputs change
- You can state the mechanism in your own words
- You can name the prerequisite that would clear up the confusion
- You can predict how the result changes when one input changes
Reference id
concept/concept-notebook/attention-transformers/swiglu
concept:attention-transformers/swiglu