An attention matrix records how strongly tokens in a sequence connect to one another. Instead of processing text one token at a time, a transformer multiplies the matrix by a sequence of token embeddings, allowing connections across the sequence to contribute to its output. section 9.1.1
A short question
The tokens [What] [is] [BERT] [?] can be represented by four embedding vectors. Together they form an encoding: a two-dimensional array for the token sequence. An encoder attention matrix can connect words across that sequence, rather than looking only at neighboring tokens. section 9.1.2
Transformer layers keep their input and output encodings in a consistent shape. That lets an attention matrix span the required sequence length in each layer and lets layers stack. Input text is truncated or padded to that length, so the chosen length also limits how much text can be processed at once. section 9.1.2 section 9.1.1
Figure 9.3 The encoder attention matrix as connections between words
View the encoder attention matrix as connections between words in a sequence. section 9.1.2
Text generation needs a further constraint. During training, a decoder can receive a whole sequence at once, but a prediction must not use later tokens. A mask blocks those future connections in the attention matrix, preventing the model from peeking at the answer it is learning to generate. A causal data collator applies this earlier-token-only rule when preparing GPT-2 fine-tuning data. section 9.2.2 section 10.2.2


