↑↓ move Enter open ⌘K find from any page 256 of 256 tabs
Full model translation Loading… Block 1 of 6 in each stack · all 8 heads · bidirectional encoder, causal decoder
Loading recorded inference…

Two stacks: an encoder and a decoder

The original transformer was built for translation and has two stacks. The encoder reads the whole input at once, every token attending to every other, and produces one vector per input token. The decoder writes the output one token at a time: it attends causally to what it has written so far and, through cross-attention, to the encoder's vectors. Input and output can differ in length and even in language.

T5 turns every task into this shape, text in and text out, with the task named in the input. This page records T5-small (6 encoder and 6 decoder blocks of width 512, 8 heads of 64, 60,506,624 parameters) on two prompts. translate English to German: The house is wonderful. becomes Das Haus ist wunderbar., and translate English to French: The cat is sleeping. becomes Le chat dorme., which shows that a 60M-parameter model's French is not perfect (the verb should be dort).

The encoder

Each Encoder Block applies bidirectional self-attention and then an MLP, 512 → 2048 → 512 with a ReLU, each step with an RMS-style norm before it and a residual connection around it. T5's norm only rescales: , with no mean subtraction and no bias. After 6 blocks and a final norm, the Encoder hidden state holds one 512-dimensional vector for each of the 11 input tokens.

The band labelled encoder hidden state → keys and values of decoder block 1 cross-attention carries that state to the decoder. It is computed once per input, however many tokens the decoder then writes.

Relative position bias

T5 adds no position vectors to its tokens. Instead each head adds a learned number to every self-attention score, depending only on the distance between the two tokens:

Distances are grouped into 32 buckets: one per distance for short ranges (up to 7 in each direction in the encoder, which splits its buckets by direction, and up to 15 in the decoder), then logarithmically wider buckets out to 128, beyond which all distances share the last one. Block 1 of each stack holds the table and the later blocks reuse it. The score is not divided by ; T5 folds that scale into the initialisation of its weights.

The decoder: causal and cross-attention

The decoder starts from the padding token, which T5 uses as its start token, followed by the tokens written so far. Its Masked Self Attention adds the same kind of relative bias and a causal mask, for , so that a token reads only itself and earlier tokens; in the head card those cells are grey. The page runs the decoder over the whole generated prefix at once, as training does, and the mask makes every row equal to what generation computed one token at a time.

Cross Attention follows. Its queries come from the decoder and its keys and values from the encoder hidden state, with no mask and no position bias:

where is the decoder state and the encoder's output for input token . Each of the 6 decoder blocks projects the same encoder state with its own weights. Even in block 1 the rows often look like an alignment: in head 1, Das puts 0.735 of its weight on The and Haus 0.444 on house.

From hidden state to next token

After the last decoder block and a final norm, the hidden state is multiplied by and scored against the 32,128 vocabulary entries with the token embedding matrix itself (tied weights). Probabilities shows the result. After Das Haus ist wunderbar. the model gives the end-of-sequence token 98.2 %, so generation stops. The recording decodes greedily, taking the most likely token at each step.

Where encoder–decoders came from

Sutskever, Vinyals and Le (2014) translated with two LSTMs, one reading the sentence into a vector and one writing the translation from it. Bahdanau, Cho and Bengio (2014) let the decoder look back at every encoder state with learned weights, the ancestor of cross-attention. The transformer (Vaswani et al., 2017) kept the encoder–decoder shape and replaced the recurrence with attention, and Shaw et al. (2018) replaced its absolute positions with learned relative ones in attention.

T5 (Raffel et al., 2019) pre-trained an encoder–decoder by span corruption on a large web corpus and cast every task, from translation to classification, as text to text, with the simplified per-head scalar bias shown here. The Speech tab draws Whisper, an encoder–decoder over audio, with the same components; the Encoder tab keeps only the encoder, and the Dense tab only the decoder.

Papers

  1. Exploring the Limits of Transfer Learning with a Unified Text-to-Text Transformer Colin Raffel et al. · JMLR 2020 T5, the model this page records: text-to-text tasks, span-corruption pre-training and per-head relative position biases.
  2. Attention Is All You Need Ashish Vaswani et al. · NeurIPS 2017 The transformer encoder–decoder: bidirectional encoder, causal decoder and cross-attention between them.
  3. Neural Machine Translation by Jointly Learning to Align and Translate Dzmitry Bahdanau, Kyunghyun Cho, Yoshua Bengio · ICLR 2015 Attention from decoder to encoder states, learned jointly with translation: the idea behind cross-attention.
  4. Sequence to Sequence Learning with Neural Networks Ilya Sutskever, Oriol Vinyals, Quoc V. Le · NeurIPS 2014 The encoder–decoder for translation, with two LSTMs and a single vector between them.
  5. Self-Attention with Relative Position Representations Peter Shaw, Jakob Uszkoreit, Ashish Vaswani · NAACL 2018 Learned relative positions inside attention, which T5 simplifies to one bucketed scalar per head.
  6. Root Mean Square Layer Normalization Biao Zhang, Rico Sennrich · NeurIPS 2019 Normalisation by the root mean square alone, without centring, the form of T5's layer norm.

See also