gMLP stacks L blocks of identical shape and size. The left of the figure at the top of the article shows the overall flow for image classification, and the right shows a single gMLP block and the SGU inside it.
Inputs and outputs are handled as in ViT-B/16 for images (split the image into 16Ć16 patches and embed them) and as in BERT for language. However, no position embeddings are used, because the linear layer inside the SGU carries the positional information of the tokens.
The gMLP block
With XāRnĆd denoting the d-dimensional representations of n tokens, a block computes the following (Eq. (1) of the paper; residual connections, normalization, and biases omitted).
Z=Ļ(XU),Z~=s(Z),Y=Z~V
- Apply layer normalization: Apply layer normalization to the input X.
- Expand the channels: U expands the channels from d to dffnā, followed by GELU (Ļ). Up to this point it is the same as the first half of a Transformer MLP.
- Mix across tokens: The SGU (s) exchanges information along the token dimension.
- Project the channels back: V projects back to d dimensions, and the block input is added (residual connection).
If s is the identity, this is exactly the Transformer's position-wise MLP. In other words, gMLP is simply an MLP with a token-mixing layer inserted in the middle. The paper says it was inspired by the inverted bottleneck of MobileNetV2, which places a depthwise convolution in the middle.
The Spatial Gating Unit (SGU)
The SGU splits Z into two parts along the channel dimension (Z1ā and Z2ā), builds a gate from Z2ā, and multiplies it into Z1ā (Eqs. (2) and (4) of the paper).
fW,bā(Z)=WZ+b,s(Z)=Z1āāfW,bā(Z2ā)
- WāRnĆn is a linear projection along the token (spatial) dimension, whose size is set by the sequence length. It is shared across all channels. b is a per-token bias.
- ā is element-wise multiplication. Layer normalization is applied to Z2ā, the input of f (this stabilized training for large language models).
- To stabilize training, W is initialized near 0 and b to 1. At the start of training, f(Z2ā)ā1 and s(Z)āZ1ā, so each block starts out as an ordinary MLP that does not mix tokens. Information between tokens is injected gradually as training proceeds.
Because the channels are split in half, the SGU outputs dffnā/2 channels, and V maps them back to d.
The SGU resembles the gates in LSTM and Highway Networks and the multiplication in SENet. The difference is that the gate is computed from a projection along the token dimension, not the channel dimension. Unlike self-attention, W does not depend on the input, but because Z1ā and f(Z2ā) are multiplied, the output contains second-order products of the inputs (of the form ziāzjā).
Image classification model sizes (Table 1 of the paper)
| Model | Layers L | dmodelā | dffnā | Parameters | FLOPs |
|---|
| gMLP-Ti | 30 | 128 | 768 | 5.9M | 2.7B |
| gMLP-S | 30 | 256 | 1536 | 19.5M | 8.9B |
| gMLP-B | 30 | 512 | 3072 | 73.4M | 31.6B |