1. Home
  2. Explore
  3. Computer Science
  4. Transformer Attention Mechanism

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

Transformer Attention Mechanism

  • 35Unfamiliar
  • 0Learning
  • 0Familiar
  • 0Mastered
Topic: Tokens and Embeddings

What are tokens in a transformer language model?

35 questions

  1. 1

    What are tokens in a transformer language model?

    Model answer

    Tokens are the pieces into which input text is broken, often words or subwords.

    Explanation

    Tokenization splits text into manageable units. In practice, tokens can be whole words or fragments, but the content simplifies by treating them as words.

  2. 2

    How are tokens initially represented in a transformer?

    Model answer

    Each token is associated with a high-dimensional vector called its embedding, obtained from a lookup table with no context.

    Explanation

    The initial embedding is context-free; it only encodes the token's identity based on a fixed embedding table.

  3. 3

    What does a direction in the embedding space represent?

    Model answer

    Directions in the high-dimensional embedding space correspond to semantic meanings, such as gender.

    Explanation

    For example, adding a certain vector to the embedding of a masculine noun yields the embedding of the corresponding feminine noun, showing that semantic features align with directions.

  4. 4

    Why are initial embeddings context-free?

    Model answer

    The initial embedding is a lookup table that assigns the same vector to the same token regardless of its surrounding words.

    Explanation

    Without context, the embedding for 'mole' is identical whether it appears in 'American shrew mole' or 'one mole of carbon dioxide'.

  5. 5

    What is the overall goal of the transformer's attention mechanism?

    Model answer

    To progressively adjust embeddings so they encode contextual meaning beyond individual words.

    Explanation

    As tokens pass through attention blocks, their embeddings get updated with information from other tokens in the context, enriching their representation.

  6. 6

    Give an example of how attention can refine the meaning of a word based on context.

    Model answer

    In 'a fluffy blue creature', attention allows the adjectives 'fluffy' and 'blue' to update the noun 'creature' so its embedding reflects a fluffy blue creature.

    Explanation

    The attention mechanism computes relevance scores and uses value vectors from adjectives to adjust the noun's embedding.

  7. 7

    How does attention handle polysemy (multiple meanings) of a word like 'mole'?

    Model answer

    Attention uses context to move the generic embedding of 'mole' toward one of several distinct directions representing its different meanings.

    Explanation

    The weights for updating the embedding are computed from nearby tokens, so the same word can be adjusted differently depending on context.

  8. 8

    How can attention use distant context to influence a word's meaning?

    Model answer

    Attention can move information from any token to any other token, even if they are far apart, by computing relevance scores across the full context.

    Explanation

    For example, the word 'Harry' can be updated based on 'wizard' appearing earlier or 'Queen' and 'Sussex' later in the text.

  9. 9

    What are the three main matrices that parameterize a single attention head?

    Model answer

    The query matrix (W_Q), key matrix (W_K), and value matrix (W_V).

    Explanation

    These matrices are learned during training and are used to compute queries, keys, and values from the embeddings.

  10. 10

    How is a query vector computed from a token embedding?

    Model answer

    The query vector is obtained by multiplying the embedding by the query matrix: q=WQeq = W_Q eq=WQ​e.

    Explanation

    Each token in the context gets its own query vector by applying the same W_Q to its embedding.

  11. 11

    How does the dimension of the query/key space compare to the embedding dimension in GPT-3?

    Model answer

    In GPT-3, the embedding dimension is 12,288, while the query/key space dimension is 128.

    Explanation

    The smaller dimension (128) allows efficient computation of attention scores while still capturing relevant patterns.

  12. 12

    How is an attention score computed between a query and a key?

    Model answer

    The attention score is the dot product of the query vector and the key vector: extscore=qcdotk ext{score} = q cdot kextscore=qcdotk.

    Explanation

    A high dot product indicates that the key is relevant to the query, meaning the corresponding token should influence the token that produced the query.

  13. 13

    What is the role of the value matrix in an attention head?

    Model answer

    The value matrix produces value vectors that, when weighted by attention scores, form the update added to the target embedding.

    Explanation

    The value vector of a token encodes what should be added to another token's embedding if that token is relevant.

  14. 14

    How are value vectors used to update an embedding?

    Model answer

    For each target token, the attention weights sum the value vectors of all tokens, and this weighted sum (the delta) is added to the original embedding.

    Explanation

    Mathematically, the new embedding is eextnew=e+sumjalphaijvje_{ ext{new}} = e + sum_j alpha_{ij} v_jeextnew​=e+sumj​alphaij​vj​, where alphaijalpha_{ij}alphaij​ are attention weights and vjv_jvj​ are value vectors.

  15. 15

    How are raw attention scores normalized to become attention weights?

    Model answer

    A softmax function is applied column-wise to the matrix of dot products, producing weights between 0 and 1 that sum to 1 per column.

    Explanation

    The softmax ensures that the attention weights form a probability distribution over the context for each token.

  16. 16

    What is the attention pattern?

    Model answer

    The attention pattern is a grid of attention weights, where each column corresponds to a target token and shows how much each source token contributes to its update.

    Explanation

    It is often visualized as a heatmap where larger dots indicate higher attention weights.

  17. 17

    What is the size of the attention pattern matrix?

    Model answer

    The attention pattern matrix has size nimesnn imes nnimesn, where nnn is the number of tokens in the context (context length).

    Explanation

    This quadratic scaling (O(n2)O(n^2)O(n2)) is why context length is a major bottleneck for transformers.

  18. 18

    Why is softmax applied column-wise to the attention scores?

    Model answer

    Softmax normalizes each column so that the weights for a given target token sum to 1, representing a probability distribution over source tokens.

    Explanation

    Column-wise softmax ensures that the update to each token is a convex combination of value vectors.

  19. 19

    What does a single column in the attention pattern represent?

    Model answer

    A column shows how much each token in the context attends to (influences) the token corresponding to that column.

    Explanation

    For the token at column position iii, the entries alphajialpha_{ji}alphaji​ give the weight of token jjj in updating token iii.

  20. 20

    Why is masking applied in causal language models like GPT?

    Model answer

    Masking prevents later tokens from influencing earlier tokens, ensuring that predictions for a token only depend on previous tokens.

    Explanation

    During training, the model predicts each token given only the preceding tokens; masking enforces this causality.

  21. 21

    How is masking implemented before softmax?

    Model answer

    The attention scores for positions where the source token comes after the target token are set to −infty-infty−infty before applying softmax.

    Explanation

    After softmax, these scores become zero, preventing any information flow from future tokens.

  22. 22

    What happens to the attention weights for masked positions after softmax?

    Model answer

    They become zero, while the remaining weights in the column still sum to 1.

    Explanation

    Setting scores to −infty-infty−infty ensures that the softmax output is zero for those positions, preserving normalization.

  23. 23

    Does cross-attention typically use masking? Why or why not?

    Model answer

    Cross-attention usually does not use masking because there is no concept of 'later' tokens between the two different data sets.

    Explanation

    In a translation task, for example, the entire source sentence is available, so each target token can attend to any source token.

  24. 24

    What is multi-headed attention?

    Model answer

    Multi-headed attention runs multiple attention heads in parallel, each with its own query, key, and value matrices, and sums their outputs.

    Explanation

    This allows the model to learn different types of relationships (e.g., grammatical, semantic) simultaneously.

  25. 25

    How many attention heads does GPT-3 use per block?

    Model answer

    GPT-3 uses 96 attention heads in each attention block.

    Explanation

    The large number of heads enables the model to capture many distinct patterns of interaction between tokens.

  26. 26

    How are the outputs of multiple attention heads combined?

    Model answer

    The proposed changes (deltas) from all heads for a given token are summed together, and the result is added to the original embedding of that token.

    Explanation

    If Deltai(h)Delta_i^{(h)}Deltai(h)​ is the update from head hhh for token iii, the final updated embedding is ei+sumhDeltai(h)e_i + sum_{h} Delta_i^{(h)}ei​+sumh​Deltai(h)​.

  27. 27

    Estimate the number of parameters in a single attention head for GPT-3 (embedding dimension 12,288, key/query dimension 128).

    Model answer

    About 6.3 million parameters. Each of the four matrices (W_Q, W_K, value-down, value-up) has size 128imes12288=1,572,864128 imes 12288 = 1,572,864128imes12288=1,572,864, so total is 4imes1,572,864=6,291,4564 imes 1,572,864 = 6,291,4564imes1,572,864=6,291,456.

    Explanation

    The value matrix is factored into two smaller matrices (value-down and value-up) each of the same size as W_Q and W_K.

  28. 28

    How is the value matrix factored in practice, and what are the two submatrices called?

    Model answer

    The value map is factored into a value-down matrix (projecting from embedding dimension to key dimension) and a value-up matrix (projecting back). In papers, the value matrix usually refers only to the first step (value-down), while the value-up matrices are combined into the output matrix.

    Explanation

    This factorization reduces the number of parameters and is especially efficient in multi-headed attention.

  29. 29

    How many attention blocks does GPT-3 have?

    Model answer

    GPT-3 has 96 layers, each containing one multi-headed attention block.

    Explanation

    The same structure repeats many times, allowing increasingly refined contextual representations.

  30. 30

    What fraction of GPT-3's total parameters are dedicated to attention?

    Model answer

    Attention accounts for about one-third of the total 175 billion parameters, roughly 58 billion.

    Explanation

    The remaining two-thirds come mostly from the multi-layer perceptron (MLP) blocks between attention layers.

  31. 31

    Why is attention considered highly parallelizable?

    Model answer

    All attention scores and weighted sums can be computed simultaneously for all tokens using matrix operations, which are efficiently executed on GPUs.

    Explanation

    Unlike recurrent models that process tokens sequentially, transformers compute attention in parallel, enabling scaling to large models and datasets.

  32. 32

    How does cross-attention differ from self-attention?

    Model answer

    In cross-attention, the keys and queries come from different data sets (e.g., source language and target language), while in self-attention they come from the same sequence.

    Explanation

    Cross-attention is used in encoder-decoder models where the decoder attends to the encoder's output.

  33. 33

    Give an application where cross-attention might be used.

    Model answer

    Neural machine translation, where the encoder processes the source sentence and the decoder uses cross-attention to attend to it while generating the translation.

    Explanation

    Other applications include speech recognition (attending to audio while generating text) and image captioning.

  34. 34

    What is a major bottleneck of the attention mechanism regarding context size?

    Model answer

    The attention pattern size grows quadratically (O(n2)O(n^2)O(n2)) with context length nnn, consuming memory and computation.

    Explanation

    This quadratic scaling limits practical context sizes; recent research focuses on sparse or efficient attention variants to mitigate this.

  35. 35

    What is the significance of parallelism for scaling models?

    Model answer

    Parallelizable architectures like attention allow training on large datasets with many GPUs, and scaling up often leads to qualitative improvements in performance.

    Explanation

    The ability to compute all attention operations in parallel is a key reason why transformers have become dominant in NLP and beyond.

1 of 35
Question 1 of 35

Ask anything

We've read this whole study set, so questions about it get the best answers.

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent