MLGuerrillaStart M1 →
Architectures·18 min read·Updated 24 September 2026

Transformer and attention

The transformer is the architecture from 2017 that sits inside almost every model on this index. Its core is attention, which updates each token using the tokens around it.

The transformer was introduced in Attention Is All You Need (Vaswani et al., 2017) to translate text, and it now runs everything from large language models to speech recognizers like Whisper. It takes a sequence of vectors as input, one per token, and returns a sequence of the same length. This page explains attention step by step, along with the parts of the architecture around it and what it costs to run.

What it is

A transformer updates every token using the tokens around it, all at once

Before 2017, the standard model for text was a recurrent neural network, which reads one token at a time and carries a running summary forward. For translation, an encoder network read the whole source sentence into one fixed-length vector, and a decoder network wrote the translation from that vector. Bahdanau et al. (2014) argued that "the use of a fixed-length vector is a bottleneck", and added attention, a way for the decoder to look back at every source word and weight the ones it needs for the word it's about to write.

The transformer kept attention and dropped the recurrent part. In the paper's words, it's "based solely on attention mechanisms, dispensing with recurrence and convolutions entirely". With no token-by-token reading, every position can be computed at the same time, which suits GPUs. The paper's base model trained in 12 hours on 8 GPUs.

The input doesn't have to be text. Anything you can cut into a sequence of pieces and turn into vectors works.

  • Text goes in as tokens, which are words or pieces of words.
  • An image goes in as square patches, which is the idea in An Image is Worth 16x16 Words (Dosovitskiy et al., 2020), the paper that introduced the Vision Transformer.
  • Audio goes in as short slices of a spectrogram, which is what Whisper (Radford et al., 2022) does.
  • The compressed image that a diffusion model denoises goes in as patches, which is what diffusion transformers (Peebles and Xie, 2022) do.

Keep reading

The rest of this article is free with an account

Create a free account to read the remaining 6 sections. It takes about twenty seconds and there's nothing to pay. The same account opens the slide decks and CV reviews.

  1. 02Attention lets each token pull in information from the tokens it needs
  2. 03A transformer layer is attention plus a feed-forward network, with a shortcut around each
  3. 04Attention's cost grows with the square of the input length
  4. 05Pick a transformer for almost anything with enough data, and know the neighbors
  5. 06See attention in code
  6. and 1 more