Skip to content
StackDecode
AI & ML Atlas
Deep LearningtechniqueInteractive simulation

Cross-Attention

Queries come from one sequence while keys and values come from another.

The idea

In an encoder-decoder transformer, the decoder's tokens form the queries and the encoder's outputs supply the keys and values, so while writing each output word the decoder looks at the relevant source words. Text-to-image models use it to let the image features look at the text prompt.

Watch it run

This is a real computation, not a drawing. Pause, step, change the settings, or try new random data.

thecatsatonthemat
  • 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

  1. 1Take queries from the decoder (or image) states.
  2. 2Take keys and values from the encoder (or text) states.
  3. 3Compute attention weights between them.
  4. 4Mix the values and pass the result onward.

Where it is used

Translation, captioning, text-to-image diffusion.

Watch out for

Needs both sequences available at the same time.

Related