Phase 19: Capstone Projects

Cross-Attention Fusion

The projection layer aligns one image vector with one caption vector. A real vision-language decoder needs every text token to attend to every patch token, so the model can ground each word in a region. Cross-attention is how that grounding happens. The text queries; the vision keys and values answer. This lesson builds the cross-attention block, the causal text self-attention, and the mask shapes that keep both legal. Implement multi-head cross-attention where the query stream is text and the key/value stream is vision. Compose a decoder block: causal self-attention + cross-attention + feed-forward. Get the mask shapes right: causal mask for self-attention, no mask for cross-attention. Run a forward pass with batched text tokens and a fixed pool of image tokens. Concatenating image tokens and text tokens into one sequence is one fusion option (early fusion, the path Chameleon and Emu3 take). Cross-attention is the other (late fusion, the path Flamingo introduced and that every Flamingo-shaped decoder since has copied). In late fusion, the text decoder runs on text-only tokens and reaches over into the image stream through cross-attention at every layer. Late fusion has two advantages. First, the text stream stays clean and the model preserves text-only capabilities. Second, the image stream is computed once per image and reused for every decode step, so generation is cheap even for…

Cross-Attention Fusion: The projection layer aligns one image vector with one caption vector. A real vision-language decoder needs every text token to attend…

This free lesson is part of the AI Engineering from Scratch curriculum. Read the full explanation, run the lesson code, and verify the result in the interactive reader or from the repository source.

Browse the complete course catalog or open this lesson on GitHub.