esc
  • WorkflowsPages
  • Case studiesPages
  • Machine Learning FoundationsLearn
  • AI EngineeringLearn
  • AI Guardrails & Red TeamingLearn
  • UI/UXLearn
  • Design SystemsLearn
  • Production System DesignLearn
  • AI papersPages
  • Interview KitPages
  • Book a callContact
Let’s talk

I’ll build the agent for it.

GELU and SwiGLU Activations

How GELU and SwiGLU bend a transformer's numbers, why SwiGLU narrows its hidden layer, and what it costs, explained simply for your AI interview.

In short

GELU is a smooth bend: small values pass a little, and large values pass almost in full. SwiGLU is a gated version that Meta's LLaMA model uses. It needs three weight matrices, so its hidden layer is made narrower to keep the cost the same.

11 min readAdvancedOccasional · estimateFacts checked 27 November 2026

Say it out loud

Read it once, then say it without looking. Start with the short one; use the long one when they ask you to go deeper.

GELU and SwiGLU are the bends a transformer puts on numbers inside its feed-forward block. GELU is a smooth version of ReLU. Small negative values pass a little, instead of being cut to zero. SwiGLU splits the input into two paths, and one path gates the other. It adds a third weight matrix, so the hidden layer gets narrower to keep the cost the same.

What are GELU and SwiGLU, in plain words?

A transformer layer does a matrix multiply, then an activation function. The activation bends each number. Without the bend, stacked layers would act like one straight line, however many you add. The bend sits in the feed-forward block, which comes after attention in each layer.

Picture a loan officer sorting applications. A hard rule passes a file only if it clears a fixed line, and rejects the rest at once. A softer officer weighs each file by how strong it is, so a borderline file still gets a fair look. GELU and SwiGLU are the softer kind. In the model, the file is a number, and the activation decides how much of it passes on. In a loan-file model, a raw score of -2.0 from one neuron comes out of GELU as -0.0455, almost zero, while ReLU would give exactly 0.

Key words

Activation function
The bend applied to each number after a layer's matrix multiply.
Feed-forward block
The part of each transformer layer that reworks each token's numbers, after attention.
Gate
A signal that decides how much of another signal gets through: near 0 is off, near 1 is on.
Hidden width
How many numbers the middle of the block holds for each token.
Weight matrix
A grid of learned numbers that a layer multiplies its input by.

How does GELU decide what to pass on?

GELU stands for Gaussian Error Linear Unit. For an input x, it multiplies x by the chance that a bell-curve (Gaussian) random number falls below x. Large positive inputs pass almost unchanged. Large negative inputs shrink to almost zero. Values near zero get a smooth share, with no sharp corner.

  • “We propose the Gaussian Error Linear Unit (GELU), a high-performing neural network activation function.”

    Hendrycks and Gimpel, the GELU paper

  • “The GELU nonlinearity weights inputs by their value, rather than gates inputs by their sign as in ReLUs”

    Hendrycks and Gimpel, the GELU paper

  • ReLU

    A hard cut at zero

    Any negative input becomes exactly 0. Simple and fast.

  • GELU

    A smooth curve

    Small negative inputs pass a little, and the curve has no sharp corner at zero.

BERT, an early and influential language model, used GELU rather than the standard ReLU, following OpenAI's GPT.

What is SwiGLU, and why do some models use it?

Noam Shazeer's 2020 paper tests variants of the gated linear unit (GLU). Dauphin and colleagues introduced GLU in 2016. A GLU multiplies two linear projections of the input, each one a matrix multiply, and passes one of them through a sigmoid first. A sigmoid is an S-shaped curve that squeezes any number between 0 and 1. SwiGLU swaps that sigmoid for Swish, a smooth bend that PyTorch also calls SiLU.

Shazeer tried many versions in the feed-forward layer of the Transformer. He reports that some of them improve on ReLU and GELU, and that the GEGLU and SwiGLU versions gave the best scores in his pre-training tests. Perplexity measures how surprised a model is by real text, so lower is better.

How a SwiGLU layer runs

One token's vector

3,072 numbers in the made-up model

Two paths from one input

Gate path: smooth bend

smooth on-off signal

Value path: no bend

the content that passes

Multiply

the gate sets how much passes

Back to full width

the third weight matrix

Output

goes to the next layer

  1. 1One copy of the vector feeds the gate path.
  2. 2A second copy feeds the value path.
  3. 3The gate path, after its smooth bend, reaches the multiply.
  4. 4The value path reaches the multiply with no bend.
  5. 5The product goes through a second weight matrix.
  6. 6The result is back at full width, ready for the next layer.
One token's vector enters two paths. The gate path and the value path meet in a multiply, then a second matrix sends the result back to full width. There are three weight matrices in all.

Meta's LLaMA model is one example of a model built with SwiGLU. Its paper says so directly:

  • “We replace the ReLU non-linearity by the SwiGLU activation function, introduced by Shazeer (2020) to improve the performance.”

    Meta AI, the LLaMA paper

  • “We offer no explanation as to why these architectures seem to work”

    Shazeer, the SwiGLU paper

Tip

In an interview, describe SwiGLU as a gated layer with a third matrix, which is a bigger change than a new curve. Say the paper reports quality gains in its tests, and that the paper itself gives no explanation for why it works.

What does the switch cost in weights?

SwiGLU adds a third matrix. If the hidden width stayed at 4 times the model width, the layer would grow by half. Shazeer keeps the weight count and the arithmetic per token the same by shrinking the hidden width.

Here is a worked example with made-up numbers. A bank's team is training a small model to read loan files, and its width is 3,072 numbers. A classic layer with a hidden width of 4 times 3,072, which is 12,288, holds two matrices and 75,497,472 weights. A SwiGLU layer with a hidden width of 8,192 holds three matrices and the same 75,497,472 weights. Biases are left out.

Here is the same arithmetic in plain Python. The first part compares three bends: ReLU, GELU and SiLU. The second part counts the weights.

Python
import math

def relu(x):
    return max(0.0, x)

def gelu(x):
    # Exact GELU: x times the chance that a Gaussian number is below x
    return x * 0.5 * (1 + math.erf(x / math.sqrt(2)))

def silu(x):
    # SiLU (Swish with beta 1): x times a sigmoid gate between 0 and 1
    return x / (1 + math.exp(-x))

print("   x     ReLU     GELU     SiLU")
for x in [-2.0, -0.5, 0.0, 0.5, 2.0]:  # raw scores from one neuron in a loan-file model
    print(f"{x:5.1f} {relu(x):8.4f} {gelu(x):8.4f} {silu(x):8.4f}")

d = 3072                      # made-up model width
classic = 2 * d * (4 * d)     # two matrices, hidden width 4d
hidden = 8 * d // 3           # SwiGLU hidden width
swiglu = 3 * d * hidden       # three matrices, same total
print(f"classic FFN weights: {classic:,}")
print(f"SwiGLU hidden width: {hidden:,}")
print(f"SwiGLU FFN weights:  {swiglu:,}")

Output

   x     ReLU     GELU     SiLU
 -2.0   0.0000  -0.0455  -0.2384
 -0.5   0.0000  -0.1543  -0.1888
  0.0   0.0000   0.0000   0.0000
  0.5   0.5000   0.3457   0.3112
  2.0   2.0000   1.9545   1.7616
classic FFN weights: 75,497,472
SwiGLU hidden width: 8,192
SwiGLU FFN weights:  75,497,472

Tip

Changing the activation of a trained model breaks the weights it learned, so keep the one it was trained with.

How would you choose an activation for a new in-house model?

First, check what the base model uses. Hugging Face models name the activation in their config file, under hidden_act. BERT's docs say hidden_act defaults to gelu. Match it, because a changed activation breaks the weights the model learned.

  1. 1

    Check the base model's config

    Look for hidden_act and match the name the checkpoint was trained with.

  2. 2

    Training from scratch? Match the budget

    Keep the weight count the same, and narrow the SwiGLU hidden width to do it.

  3. 3

    Test on your own data

    Compare error on a set of loan files the model never trained on. The paper's tests may not match your data, so measure yours.

The trade-off here is simpler GELU against gated SwiGLU, which costs a third matrix and reports quality gains in its tests.

What does the interviewer want to hear?

A strong answer covers

  • An activation bends each number, or stacked layers are one straight line.
  • GELU is smooth and weights each input by its value.
  • SwiGLU is gated: two paths, one gating the other, three matrices.
  • The hidden width shrinks so the cost stays the same.
  • A trained model's activation can't be swapped without breaking it.
  • The paper reports gains in its tests, so test on your own data.

Common traps

  • Trap"GELU and ReLU are the same idea."

    BetterBoth pass large positive values. GELU is smooth near zero, while ReLU has a hard corner there.

  • Trap"SwiGLU is a better curve, so swap it in."

    BetterIt adds a gate and a third matrix. Swapping it changes the layer's shape, so the width has to change too.

  • Trap"The gain is big, so always use it."

    BetterThe paper reports gains in its own tests. Test on your own data before you commit to the cost.

Can you spot the problem?

Spot the problem

A bank's team loads a saved copy of a model trained with GELU (a checkpoint), but their code sets the activation to SiLU. What goes wrong?

Quick check

A team keeps a feed-forward layer at 4 times the model width, then swaps in SwiGLU with three matrices and no narrowing. What changes?

Can you answer these from memory?

Flash cards · tap a card to flip it

1 / 6

What will they ask next?

  1. Q064Roughly how many parameters are in one transformer layer?9 min
  2. Q058Multi-query vs grouped-query attention?10 min
  3. Q061Dropout in transformers?11 min
  4. Q062How does BERT differ from GPT?11 min
  5. Q067What is weight tying?10 min
  6. Q054What does the feed-forward block do?10 min

Learn it in depth

Last updated on 27 November 2026

Building an AI feature on your own data?

I design and build AI products and agents, including the system behind them. Book a short call and tell me what you are working on.