Concepts / SwiGLU Activation Function

SwiGLU Activation Function

The module-level topic here is SwiGLU, but the source pack describes SwiGLU only as the other notable alternative to ReLU that large language models use, so the detailed mechanics taught below are GELU's.

  • Programming

Predicting What an Activation Does at Zero

An activation function takes a number and produces a new number, and a deep learning model applies one after many of its internal computations. The source material for this article centers on GELU, a smooth activation function. The module-level topic here is SwiGLU, but the source pack describes SwiGLU only as the other notable alternative to ReLU that large language models use, so the detailed mechanics taught below are GELU's. Where a claim about SwiGLU would need details the source does not supply, this article says so rather than guessing.

What do you think happens?

Suppose you pass the single value 0 through the GELU module. Commit to a prediction before reading on. Could the tanh term rescue a nonzero answer?

  • The output is 0

  • The output is 0.5

  • The output is some small negative number

Reveal answer
Answer:

The output is 0

The leading factor in the approximation is 0.5 times x. When x is 0, that product is 0, and the rest of the expression is multiplied by it, so the tanh term cannot produce a nonzero result. The exact definition gives the same answer because it is x times a number between 0 and 1.

Reading the Exact GELU Definition

The exact GELU activation is defined as x times $\Phi(x)$, where $\Phi$ is the cumulative distribution function of the standard Gaussian distribution. The Gaussian is the familiar bell-shaped distribution. Its cumulative distribution function gives, for any input, the probability that a standard Gaussian random variable falls at or below that input.

Formula

Read the definition as a rule with two parts. The input x is kept as the value being transformed, and $\Phi(x)$ is a number that always lies between 0 and 1. The output is therefore the input scaled down by a factor between 0 and 1. Large positive inputs have $\Phi(x)$ close to 1 and pass through almost unchanged, while large negative inputs have $\Phi(x)$ close to 0 and are pushed toward zero. Because the scaling depends smoothly on the input, GELU does not switch abruptly at a single cutoff point.

xthe input valuePhi(x)a number between 0 and 1GELU(x)x times Phi(x)Standard GaussianCDFbell-curve probabilityevaluated to givepassed in askept as the valuescales the input
How does the input x get scaled by Phi(x), so that large positive values pass through nearly unchanged while negative values are suppressed?
Worked example

Evaluating GELU at zero

Compute GELU(0) using the exact definition.

  1. 1
    Identify the two factors

    The definition is x times Phi(x), so the output needs x and Phi(x) at x = 0.

  2. 2
    Evaluate the input factor

    The input factor is x = 0.

  3. 3
    Evaluate the Gaussian CDF

    The standard Gaussian distribution is symmetric around zero, so the probability of falling at or below 0 is one half. That gives Phi(0) = 0.5.

  4. 4
    Multiply the factors

    GELU(0) = 0 times 0.5 = 0.

Result

GELU(0) = 0. This matches the prediction from the opening step.

  • Common mistake

    Treating Phi(x) as the output instead of the scaling factor

    What it looks like

    Concluding that GELU(2) is about 0.977 because Phi(2) is about 0.977.

    Why it fails

    The definition multiplies the input by Phi(x). Phi(x) alone is only the gate, not the activation.

    Do this instead
    Multiply the input x by Phi(x) to get GELU(x).
  • Common mistake

    Thinking Phi(x) can be negative or larger than 1

    Why it fails

    Phi is a cumulative distribution function, and the source states that Phi(x) always lies between 0 and 1.

    Do this instead

    Expect the output magnitude to be at most the magnitude of the input for x values, with positive inputs mostly passing through.

Tracing the Tanh Approximation Line by Line

The exact definition needs a Gaussian cumulative distribution function, which is not a simple operation to compute directly. The source explains that practitioners commonly implement a computationally cheaper approximation instead. The approximation keeps the same character as the exact function: it is smooth, it passes positive inputs mostly through, and it suppresses negative inputs toward zero. What changes is how the scaling factor is computed. Instead of a Gaussian CDF, it uses a tanh expression built from simple powers, multiplications, and a square root.

Rule

Read the expression from left to right. The leading 0.5 times x is the input term, playing the role that x plays in the exact definition. The term $1+\tanh(\cdots)$ takes the place of $2\,\Phi(x)$, since $\tanh$ ranges from negative one to one, so $1+\tanh$ ranges from zero to two and half of it lands between zero and one, matching the range of $\Phi(x)$. The constants $\sqrt{2/\pi}$ and $0.044715$ are the values the source says were found via curve fitting, chosen so the approximation tracks the exact function's shape while using only cheap operations.

0.5 * xinput term, same role as x1 + tanh(...)replaces the 2 Phi(x) gatesqrt(2/pi)constant from curve fittingx + 0.044715 * x^3 polynomial inner term,fitted constant
Which part of the tanh expression stands in for Phi(x), and which constants come from approximating the Gaussian CDF?

Why Practitioners Accept an Approximation

The approximation is used because it is computationally cheaper than the exact form, and it is the version the source says was used in training the original GPT-2 model. Its constants were found by curve fitting, so the approximation was designed to closely follow the exact function's shape rather than to be a crude substitute. Engineers usually implement the approximation, while the exact definition remains the reference that explains what the approximation is trying to match.

In the real world

A practitioner writing a model's forward pass chooses between the exact Gaussian-CDF form and the tanh form. The tanh form uses only powers, multiplications, a square root, and tanh, so the activation is cheap to compute at every layer. The exact form remains the definition to check against when verifying an implementation.

Comparing GELU Curves with ReLU

ReLU has long been the default activation in deep learning because of its simplicity and effectiveness across many architectures. GELU is the more complex of the two. It is smooth, where the scaling depends continuously on the input, and the source reports that this offers improved performance for deep learning models. The key visual difference lies in the behavior near zero and in the negative region: GELU eases into its output instead of switching at a sharp boundary, and it lets negative inputs approach zero gradually rather than cutting them off at one point.

Simplicitysimple, the long-standingdefaultSimplicitymore complex than ReLUSmoothnesshard cutoff rather thansmooth scalingSmoothnesssmooth, input-dependentscalingHistorical usedefault for manyarchitecturesHistorical useused to train originalGPT-2
How does GELU's output differ from ReLU's for inputs near zero and for negative inputs, and why does that smoothness matter?
PropertyReLUGELU
SimplicitySimpler, the long-standing defaultMore complex
SmoothnessHard cutoff, not smoothSmooth, input-dependent scaling
Historical useDefault across many architecturesUsed in training original GPT-2 via the approximation
Reported performanceEffective across many architecturesReported to offer improved performance for deep learning models

Properties the source describes for ReLU and GELU

Mistakes Beginners Make with GELU

  • Common mistake

    Assuming the tanh code is the exact definition

    Why it fails

    The source says the code computes an approximation of the exact GELU, not the exact Gaussian CDF form.

    Do this instead

    Keep the exact definition x times Phi(x) as the reference, and treat the tanh form as a cheaper approximation to check against it.

  • Common mistake

    Expecting a nonzero output at x = 0

    What it looks like
    Predicting GELU(0) = 0.5 because Phi(0) = 0.5.
    Why it fails

    The output is x times Phi(x), and x = 0 makes the whole product zero.

    Do this instead

    Remember that the input factor multiplies the gate, so zero input gives zero output.

  • Common mistake

    Assuming the module has learnable parameters, when its init only calls the parent constructor and all the work happens in forward

  • Common mistake

    Mixing GELU and SwiGLU details

    Why it fails

    The source pack describes GELU's definition and approximation. It names SwiGLU only as another alternative and does not give its formula here.

    Do this instead

    Attribute the mechanics taught in this article to GELU, and consult a dedicated SwiGLU source before stating its details.

Checking Your Understanding of the Trace

MEDIUM

Using the exact definition, explain in your own words why GELU(x) is close to x for large positive x but close to 0 for large negative x. Then state which factor in the tanh approximation plays the role of Phi(x).

Hints
  • Phi(x) always lies between 0 and 1.
  • For large positive x, Phi(x) is close to 1, so the product stays near x.
  • In the tanh form, the term 1 + tanh(...) multiplied by 0.5 plays the gate role.
MEDIUM

A colleague says GELU and ReLU both output zero for every negative input, so they behave the same there. Explain why this is misleading using the smoothness property described in the source.

Hints
  • Think about whether GELU has a single sharp cutoff point.
  • Compare how each function scales inputs as they approach the boundary.

Summary of the GELU Trace

  1. 1
    GELU is defined exactly as x times Phi(x), where Phi is the cumulative distribution function of the standard Gaussian distribution, so each output is the input scaled by a factor between 0 and 1.
  2. 2
    The PyTorch module computes a tanh-based approximation that is cheaper than the exact form. Its constants were found by curve fitting, and each operation in its forward method traces to a part of the formula.
  3. 3
    GELU is smooth and more complex than ReLU, and the source reports it offers improved performance for deep learning models. ReLU remains the simpler long-standing default.
  4. 4
    The source names SwiGLU as the other notable alternative to ReLU in large language models, but its detailed mechanics are outside the material this article can ground.

At a glance

  • GELU is defined exactly as x times Phi(x), where Phi is the standard Gaussian CDF, so it scales each input by a factor between 0 and 1.
  • The tanh-based approximation is cheaper to compute than the exact form, and its constants were found by curve fitting.
  • GELU is smooth and more complex than ReLU, while ReLU remains the simpler long-standing default.
  • This article's detailed mechanics are GELU's; SwiGLU is named in the source only as another notable alternative.

Related concepts

More concepts starting with S