Explain the concept of 'Attention Mechanisms' in LLMs and their impact on model performance.
Instruction: Describe what attention mechanisms are, how they are implemented in LLMs, and provide examples of how they improve the model's ability to process and generate language.
Updated
Example Answer
In standard transformer attention, learned projections produce queries, keys and values. For each query, dot products with the keys are divided by the square root of the key dimension. A mask excludes disallowed positions, and softmax converts the remaining scores to weights. The attention output is their weighted sum of value vectors. Multiple heads use different projections; their outputs are concatenated and projected again.
Self-attention connects token representations within a sequence. An autoregressive decoder can use only the current and earlier positions at that layer; it cannot inspect future tokens during training. Encoder–decoder attention instead lets a decoder query use the encoder's representations of the source. For translation or resolving a reference, this provides a direct way to combine relevant information from distant available positions instead of passing it through every intervening recurrent step.
Worked example
Suppose one query has two allowed, already-scaled scores [0, ln(3)] and scalar values [2, 6]. Softmax gives [1/4, 3/4], so its output is (1/4 × 2) + (3/4 × 6) = 5. If the second position is masked, the output is 2; changing the masked value must have no effect.
Dense attention's score computation grows quadratically with sequence length at fixed hidden size, and an available context position is not a guarantee the model uses it correctly. Attention alone does not establish factuality, reasoning accuracy or a faithful explanation of the final prediction.
Common Poor Answer
A weak answer says attention makes the model focus on every word, without explaining values, normalization or the positions that a causal mask excludes.