LLM Library
Inside the TransformerAnimated
Multi-Head Attention
Several attention patterns computed in parallel.
The idea
The vector is split into several heads, each with its own Q, K and V. Different heads can focus on different relationships, such as syntax, position or topic.
Watch it work
Press play, pause, or step through. Some animations have settings you can change.
- positive
- negative
- attention weight
Take the sentence. We will compute what the word "cat" (highlighted) should pay attention to.
1/4
The vectors here are random, so the pattern of attention is arbitrary. In a trained model these weights are learned. Pick another query word with the slider.
Step by step
- 1Split the vectors into heads
- 2Run attention in each head
- 3Join the head outputs
- 4Mix them with a final linear layer
Where it is used
Every transformer.
Watch out for
More heads mean a bigger KV cache unless keys and values are shared.