Attention / Core
verifiedCross-Attention
Cross-attention is attention between two different sequences. The queries come from the sequence being written, the keys and values from the one being read. It is how a decoder consults an encoded input, and how a model conditions on an image, an audio clip, or a retrieved document.
Identical arithmetic to self-attention with K and V projected from the other sequence. No causal mask is needed on the source, since the whole of it is available. Because the source encoding does not change while decoding, its keys and values are computed once and cached for every step.
CrossAttn(Y, H) = softmax(YW sub Q (HW sub K )ᵀ / √d sub k )·HW sub V , with Y the target sequence and H the source encoding. The score matrix is n sub target × n sub source , so scoring costs Θ(n sub target ·n sub source ) against the Θ((n sub target + n sub source )²) of self-attention over the two sequences concatenated.
6 queries against 20 keys; a brighter cell means more of that query's attention went to that key. Nothing is masked: every target position can read every source position.
Target positions as rows, source positions as columns, with no mask between them. Drag the length to see the score matrix stay rectangular rather than square.
Reviewed by opendroid · 2026-08-04
- arXiv:1706.03762 — Attention Is All You Need