SwiGLU in Transformer Feed-Forward Layers: Trade-offs, Not Free Speed

SwiGLU is a gated activation used in the feed-forward sublayer of many Transformer models. It can improve model quality under some training setups, but it is not a free performance switch: the gated form uses an extra projection, and its quality and cost depend on how the layer width and training budget are chosen.

The feed-forward layer in a Transformer

After attention mixes information across token positions, a Transformer block applies a feed-forward network independently at each position. A simple two-layer version projects a hidden state to a wider dimension, applies a nonlinearity such as GELU, then projects back to the model dimension.

In the shorthand below, x is a token’s hidden vector, W1 and W2 are learned matrices, and φ is an activation:

FFN(x) = φ(x W1) W2

This sublayer adds nonlinear transformations, but each intermediate feature is mainly controlled by one activation of a projection.

What the gate changes

A Gated Linear Unit uses two projections from the same input. One branch supplies values; the other produces a gate that scales them element by element. SwiGLU uses the Swish, also called SiLU, activation on one branch:

SwiGLU(x) = (SiLU(x W_gate) ⊙ (x W_value)) W_out
SiLU(z) = z · sigmoid(z)

The symbol ⊙ means element-wise multiplication. The gate lets each feature depend on both projected branches, rather than only on a single activated projection. That extra interaction is a plausible source of the gains observed in some controlled comparisons, but the operation itself does not guarantee a better model.

Why dimension matching matters

A conventional feed-forward layer has two main weight matrices. A gated layer has three: one for the gate, one for values, and one to project back. If all hidden dimensions stay the same, the gated layer has more parameters and roughly more matrix-multiplication work than the two-projection baseline.

For a parameter-matched comparison, model designers can reduce the gated intermediate width. Ignoring biases and implementation details, using about two-thirds of the original intermediate width makes the three projection matrices occupy a similar parameter budget to two baseline matrices. This is a design trade-off, not an exact equivalence of training compute or quality.

Therefore a paper or model card that reports a SwiGLU improvement needs to be read with its comparison setup: intermediate width, parameter count, training tokens, optimizer, data, and compute. Comparing activations while changing model capacity can confound the result.

What the evidence supports

Shazeer’s study of GLU variants tested them in Transformer feed-forward sublayers and reported quality gains for some variants over ReLU or GELU under the paper’s experimental setup. PaLM and Llama 2 are prominent examples of model reports that use SwiGLU in their architectures. That adoption shows the variant is useful enough to include in major model recipes; it does not establish that it wins for every scale, dataset, or budget.

Calling the improvement “free performance” hides the extra projections and the need to compare at a controlled capacity. A more careful engineering claim is that gated activations are one architecture choice whose quality may justify their compute and memory cost in a given training recipe.

What changes at inference?

The feed-forward block runs for each processed token. Its weights are reused, but its matrix operations still contribute to prefill and decode compute. A wider gated block can add latency or reduce throughput unless the implementation and serving hardware handle it efficiently. Quantization and kernel fusion may change the measured cost, but those are deployment properties to benchmark rather than assumptions that follow from the activation name.

For inference evaluation, compare models or checkpoints with similar quality targets and record model width, batch size, sequence length, precision, hardware, and serving engine. A shorter training step or a better loss curve does not automatically mean lower per-token serving latency.

When should you choose SwiGLU?

If you are training a Transformer, use a validated architecture recipe as a starting point and compare alternatives within a controlled budget. Track validation loss and downstream task quality, then measure memory, training throughput, and inference latency. Keep the baseline configuration so a quality change is not confused with a change in width or data.

If you are selecting a pretrained model, its activation is usually less important than the checkpoint’s task performance, license, memory use, and serving support. Replacing an activation in a trained model is not a harmless inference-time toggle; it changes the computation the weights were trained to support.

The engineering takeaway

SwiGLU adds a learned gate to the Transformer’s feed-forward block. Research and model reports show why the design became common, while the extra projection explains why its gains are not free. Treat it as a capacity and compute trade-off, and compare it under a clearly specified training and serving budget.

Sources