Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 21

SwiGLU: Gating for Enhanced FFN Capacity

Recommended resources

Hello! Welcome back.

In our last lesson, we began our deep dive into the components of a modern Transformer block by examining RMSNorm. We saw how it improves efficiency by simplifying the normalization process, focusing only on re-scaling activations.

Today, we'll continue this dissection by turning our attention to the Feed-Forward Network (FFN) sub-layer. This is the part of the Transformer block responsible for most of the computation and feature transformation. You'll learn how modern models have replaced the standard activation functions of the past with a more powerful and expressive alternative.

Our learning outcome for this lesson is to describe the SwiGLU activation function and how its gating mechanism increases FFN capacity. By the end, you will understand what "gating" means in a neural network, how SwiGLU implements it, and why this design has become a cornerstone of state-of-the-art models like Qwen 2.5 and Llama 3.


1. Beyond ReLU: The Need for Better Activations

As a brief recap, a standard Feed-Forward Network (FFN) in a Transformer block consists of two linear layers with a non-linear activation function in between. For a long time, the go-to activation function was the Rectified Linear Unit (ReLU), defined as .

While simple and effective, ReLU has limitations. Its gradient is not smooth—it's 0 for all negative inputs and 1 for all positive inputs, with a sharp "corner" at zero. This can sometimes hinder the training process.

Modern architectures have largely adopted a smoother, more nuanced activation called Swish (also known as SiLU, or Sigmoid Linear Unit).

Comparison of ReLU and Swish Activation Functions and their Gradients
This image compares the ReLU and Swish activation functions. On the left, you can see Swish's smooth curve, which dips slightly below zero before rising. On the right, notice the smooth gradient of Swish compared to the sharp, discontinuous gradient of ReLU.

The formula for Swish is elegantly simple:

where is the sigmoid function. This formula introduces a fascinating concept: the input x is multiplied by a "gated" version of itself (), which squashes values to the range (0, 1). This is a form of self-gating.

This gating idea is the key to understanding SwiGLU.



2. The Gating Mechanism

Before we get to SwiGLU specifically, it's crucial to understand the general concept of a Gated Linear Unit (GLU). A standard FFN transforms its input with a single pathway. A GLU, however, splits the input into two parallel pathways. One pathway computes the "content" or "value" to be transformed, while the second pathway acts as a gate, dynamically controlling how much of the content from the first pathway is allowed to pass through.

The output of the gate pathway (typically squashed between 0 and 1) is multiplied element-wise with the output of the value pathway.

To build a solid intuition for this, the following article provides an excellent conceptual overview.

Gated Linear Units: The FFN Architecture Behind Modern LLMs

Let's explore the core idea of a gating mechanism. This article introduces the concept and explains the original Gated Linear Unit (GLU) formulation.

Please read the sections titled 'Gated Linear Units', 'The Gating Mechanism', and 'The GLU Formulation'. Focus on the analogy of the mixing console and understanding the two-pathway structure: a 'value' pathway and a 'gate' pathway combined by element-wise multiplication.

As the reading explains, this multiplicative interaction is fundamentally more expressive than the purely additive interactions in a standard FFN. By allowing the network to learn not just what to transform but also how much of that transformation to use for any given input, we give it a more powerful tool for learning complex patterns.



3. SwiGLU: Combining Swish and Gating

Now we can combine the two concepts we've introduced: the Swish activation and the GLU architecture. The result is SwiGLU, which has become the de facto standard in modern LLMs.

The architecture is best understood visually.

Comparison of Original Feed-Forward Layer and Feed-Forward Layer with SwiGLU
This diagram shows the architectural difference. The original FFN (left) has a single path through two linear layers and a ReLU activation. The FFN with SwiGLU (right) splits the input into two parallel paths (through weights W1 and V), combines them with a multiplication, and then passes the result through a final linear layer (W2).

The formula for SwiGLU is:

Here, the input is projected through two separate weight matrices, and . The first projection, , is passed through the Swish activation. The second projection, , remains linear. The two results are then multiplied together element-wise ().

To get a complete understanding, including a step-by-step numerical example that satisfies your preference for practical applications, please continue with the same article.

Gated Linear Units: The FFN Architecture Behind Modern LLMs

Now, let's see exactly how SwiGLU is formulated and why it's so effective. The next sections of the article explain the SwiGLU formula and walk through a concrete numerical example.

Please read the sections 'SwiGLU: The Modern Standard' and 'A Complete Worked Example'. Focus on the formula, the reasons SwiGLU outperforms the original GLU, and the step-by-step computation in the example.

For a supplementary verbal explanation, the following video segment is also excellent.

LLaMA explained: KV-Cache, Rotary Positional Embedding, RMS Norm, Grouped Query Attention, SwiGLU

This video walks through the SwiGLU function in the context of the LLaMA model, reinforcing the concepts you've just read.

Please watch from 01:04:05 to 01:09:46. The presenter covers the Swish function, the GLU architecture, and discusses the empirical results that led to its adoption.


4. How Gating Increases FFN Capacity

You've now seen what SwiGLU is, but the learning outcome also asks how it increases the capacity of the FFN.

  1. Increased Expressivity: The multiplicative interaction between the two pathways allows the network to model more complex, second-order relationships between features that a standard additive FFN cannot.
  2. Parameter Efficiency: At first glance, SwiGLU seems more expensive because it uses three weight matrices (, , and the output projection ) instead of two. However, as noted in the "Transformer Design Guide" resource, models compensate for this by reducing the FFN's hidden dimension (). For example, instead of the standard , they use . This keeps the total parameter count roughly the same while delivering superior performance, demonstrating a more efficient use of parameters. The gating mechanism is so powerful that the FFN can be "thinner" and still outperform a "wider" FFN with a simpler activation function.


Conclusion

In this lesson, we've explored the SwiGLU activation function, a key component that distinguishes modern Transformers from their predecessors.

Key Takeaways:

  • SwiGLU replaces older activation functions like ReLU in the Feed-Forward Network (FFN) sub-layer.
  • It is a type of Gated Linear Unit (GLU), which uses a gating mechanism to dynamically control information flow.
  • The input is processed through two parallel linear projections. One is passed through the Swish activation (), and the other remains linear. The results are multiplied element-wise.
  • This multiplicative interaction increases the FFN's capacity and expressivity, allowing it to learn more complex feature relationships.
  • Despite using an extra weight matrix, SwiGLU allows for a smaller hidden dimension, resulting in similar or better performance for a comparable number of parameters.

Preview of the Next Lesson:
We have now covered two crucial pieces of the modern Transformer block: RMSNorm and SwiGLU. In the next lesson, we will examine the third and final piece from this foundational module: Rotary Positional Embeddings (RoPE). You will learn how this clever technique encodes positional information by rotating feature vectors in the complex plane, a method that is fundamental to the long-context capabilities of today's LLMs.