C.W.K.
Stream
Lesson 07 of 13 · published

Self-Attention vs Cross-Attention

~10 min · self-attention, cross-attention

Level 0Token
0 XP0/94 lessons0/10 achievements
0/120 XP to next level120 XP to go0% complete

Two flavors of attention, used in different parts of an architecture:

Self-attention

Q, K, V all come from the same sequence. Each token attends to other tokens in the same input. This is what encoder-only models like BERT use throughout, and what decoder-only models like Llama use as the only attention type.

Cross-attention

Q comes from one sequence (the decoder), K and V come from another (the encoder's output). The decoder's tokens "look at" the encoder's representation of the input. Used in encoder-decoder models like T5 and Whisper to connect the encoder's understanding of the source to the decoder's generation of the target.

ArchitectureSelf-attnCross-attn
BERT (encoder-only)Yes (bidirectional)No
GPT / Llama (decoder-only)Yes (causal)No
T5 / Whisper (encoder-decoder)Yes in encoder, yes in decoder (causal)Yes (decoder Q attending encoder K, V)

For a translation task with a source sentence S and a target sentence T being generated, cross-attention is what lets the decoder, while producing the n-th target token, look back at the entire encoder representation of S and figure out which source tokens are relevant right now.

Code

Cross-attention in PyTorch·python
class CrossAttention(nn.Module):
    def __init__(self, d_model, n_heads):
        super().__init__()
        self.attn = nn.MultiheadAttention(d_model, n_heads, batch_first=True)
    def forward(self, decoder_x, encoder_out):
        # Q from decoder, K/V from encoder
        # PyTorch's MultiheadAttention takes (query, key, value)
        out, _ = self.attn(decoder_x, encoder_out, encoder_out)
        return out
# Inside an encoder-decoder block:
# x = self_attn(x)              # decoder self-attention (causal)
# x = cross_attn(x, enc_out)    # decoder looks at encoder output
# x = ffn(x)

External links

Exercise

Load a small T5 model. Print the layers in its decoder. Identify which layers do self-attention vs cross-attention. Then run a translation example and inspect the cross-attention weights for one decoder layer — do they 'look' at sensible source tokens for each target position?

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.