All models

gmlp_b16_224

An all-MLP image classifier whose Spatial Gating Unit replaces self-attention with a learned token-mixing matrix.

Image classification · 488 operations Checked

100%

Showing the top corner; full diagram not loaded yetShowing a small copy; full diagram not loaded yetLarge diagram, not loaded yet

Open SVG

Loading diagram (10 KB)

Open in a new tab

Click or tap a spot to read it up close. Scroll or drag inside the diagram to move through it. Zoom with + and − or Ctrl and scroll. Press 0 for the whole graph. Swipe inside the diagram to move through it. Use + and − to zoom.

At a glance

Family
gmlp
Parameters
73.1M
Operations
488
Framework
PyTorch
Recurrent
No
Published
2021
Cited by
355 (as of 17 November 2026)
Authors
Hanxiao Liu, Zihang Dai, David R. So, Quoc Viet Le
License
Apache-2.0
Checked
Passed: the trace was replayed operation by operation and matched the model's own output for this input.

Files

The diagram is a drawing of a TorchLens trace of the source code. It contains no code or weights.

Details
Stable ID
m5214
Graph hash
f91640718ba860968f649b637b8519a84824f447bc08e1c8933c86f80c8c161a
Skeleton hash
4e533242d92aa75f5add1ac7e26d5509e4b2d1f0e3f018cd113c0a5584b82cc5
Architecture hash
1efb68070538e25f046831245b4191ed35d8aad08bd1d62fecba0901907e4275
Diagram SHA-256
b0f0943321c38f03419eed9a27ccb65bcab1adf0e356ad1cffa86759fb9d950e
TorchLens version
2.35.3
Renderer
graphviz 2.43.0+pyvips 3.2.0+libvips 8.18.7+render-opts@06c1f0d3
Check
Forward pass replayed in PyTorch

How this diagram was made

timm.create_model('gmlp_b16_224', pretrained=False)

What it is

About this model's family

gMLP is a vision model that drops self-attention entirely: each block is nothing more than channel projections and spatial projections with static parameterization. The paper's aim was to ask whether the inductive bias of self-attention is essential to the success of Transformers, and the answer it reaches is that it is not critical for Vision Transformers. The captured members are timm's gMLP-Tiny, gMLP-Small and gMLP-Base. They follow the ViT/B16 input protocol, so the image is cut into 16 by 16 pixel patches at the stem and each patch becomes a token.

The key idea is the Spatial Gating Unit. After a channel expansion and a GeLU, the activations are split into two halves along the channel dimension; one half passes through a single linear projection across the token (spatial) dimension and is multiplied element-wise into the other half. What makes this different from attention is that the mixing matrix is a fixed learned weight, independent of the input, rather than something computed from the image. Because that layer carries positional information, gMLP needs no position embeddings. A quirk worth knowing when reading a freshly initialized graph: the spatial weights start near zero and the bias at one, so every block behaves like a regular feed-forward layer at the start of training and tokens only gradually begin to interact.

On ImageNet without extra data the authors find gMLPs comparable with DeiT, the regularized Vision Transformer, and with 66% fewer parameters a gMLP model is 3% more accurate than MLP-Mixer. They are candid that gMLPs still trail the best convolutional and hybrid models. Note that timm's author labels this gMLP implementation preliminary and not verified. One last finding from the paper: after training, the spatial projection matrices show locality and spatial invariance, so each one has effectively learned a convolution with a data-driven, irregular kernel shape.

Key idea. The Spatial Gating Unit: a static linear projection over the spatial (cross-token) dimension, gating one half of the channels with the other, in place of self-attention. With it, a stack of plain MLP blocks reaches the same ImageNet accuracy as a Vision Transformer and scales as well as Transformers with more data and compute.

  • gMLP
  • gated MLP
  • Pay Attention to MLPs
  • Spatial Gating Unit
  • SGU
  • MLP-like
  • attention-free
  • image classification
  • ImageNet
  • gMLP-Ti
  • gMLP-S
  • gMLP-B

AI-written summary, based on the paper and checked automatically against it; not reviewed by a person. Corrections welcome.

Cite the paper

@misc{liu2021payattentionmlps,
      title={Pay Attention to MLPs}, 
      author={Hanxiao Liu and Zihang Dai and David R. So and Quoc V. Le},
      year={2021},
      eprint={2105.08050},
      archivePrefix={arXiv},
      primaryClass={cs.LG},
      url={https://arxiv.org/abs/2105.08050}, 
}