liveBook · Manning

Cross-attention

As explained in Build a Text-to-Image Generator (from Scratch)

An attention mechanism that computes attention between decoder representations and representations from an encoded conditioning input.

Cross-attention lets a decoder use a separate encoded input while generating an output sequence. In an encoder–decoder transformer, it sits between masked self-attention and a feed-forward network in each decoder layer. Masked self-attention uses earlier output tokens; cross-attention connects the resulting decoder state to the encoder output. section 2.1.2 section 2.3.2

Trace one decoder layer:

  • Masked self-attention updates the decoder state without exposing future output positions. section 2.3.2
  • Cross-attention uses that state as its query and the encoder output, called memory, as both key and value. In image captioning, the queries come from caption representations and the keys and values come from image representations, each passed through linear layers. section 2.3.2 section 4.1.2
  • A feed-forward sublayer processes the cross-attention output. Layer normalization and residual connections accompany the decoder sublayers. section 2.1.2
Listing 2.6 Creating a decoder layer
class DecoderLayer(nn.Module):
    def __init__(self, size, self_attn, src_attn,
                 feed_forward, dropout):
        super().__init__()
        self.size = size
        self.self_attn = self_attn
        self.src_attn = src_attn
        self.feed_forward = feed_forward
        self.sublayer = nn.ModuleList([deepcopy(
        SublayerConnection(size, dropout)) for i in range(3)])
    def forward(self, x, memory, src_mask, tgt_mask):
        x = self.sublayer[0](x, lambda x: 
                 self.self_attn(x, x, x, tgt_mask))  #1
        x = self.sublayer[1](x, lambda x:
         self.src_attn(x, memory, memory, src_mask))  #2
        output = self.sublayer[2](x, self.feed_forward)  #3
        return output

The decoder-layer implementation makes the data flow explicit: self_attn(x, x, x, tgt_mask) runs first, src_attn(x, memory, memory, src_mask) performs cross-attention second, and the feed-forward sublayer runs last. section 2.3.2

Conditioning a caption on an image

An image encoder produces image embeddings. Starting from a <start> token, the text decoder generates a caption one token at a time. At each decoder layer, cross-attention connects the current caption state to those image embeddings so the caption can align with image content. The same encoder–decoder pattern also supports German-to-English translation, with encoded German tokens conditioning English output. section 4.1.2 section 4.3.2 section 2.1.2

How Build a Text-to-Image Generator (from Scratch) covers it

How deep it goes
Specifies decoder-layer placement, query/key/value inputs, and a concrete call in code; does not elaborate the attention calculation here.
Examples
Uses German-to-English translation, a skiing-image caption, and a decoder-layer implementation with separate source and target masks.
What it assumes
Builds on encoder outputs, token embeddings, and self-attention; identifies the decoder’s query, key, and value roles explicitly.
What you can do after
Build it from scratch as a decoder sublayer that attends to encoder output for translation or image captioning.
How it connects
Links masked self-attention, encoder–decoder transformers, feed-forward networks, and autoregressive generation.

Also explained in

Compare how 4 books explain cross-attention →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build a Text-to-Image Generator (from Scratch). The section links point to the parts of the book it draws on.