Multimodal Foundations › Mathematical Foundations of Cross-Modal Attention
Language AI HandbookAll chapters Multimodal Foundations › Mathematical Foundations of Cross-Modal AttentionOne section · Sections in this chapter7 / 13 ↓
Reading tools Definitions & inline links
Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.
Article links
Make inline references clickable
The most powerful form of cross-modal interaction is cross-modal attention, a direct extension of the self-attention mechanism you studied in the attention chapters of this handbook. Understanding it precisely will help you read papers on multimodal transformers.
From Self-Attention to Cross-Attention
Recall from our earlier discussion of self-attention that it computes a weighted combination of value vectors, where the weights are determined by the similarity between query vectors and key vectors:
In self-attention, all three matrices , , are derived from the same input sequence by linear projections. For a sequence :
Each token queries and answers questions about other tokens in the same sequence. This is appropriate for processing a single modality but does not allow tokens from one modality to attend to tokens from another.
Cross-modal attention modifies this by letting queries come from one modality and keys and values come from the other. Specifically, to attend from text to visual features, we use:
- Queries from the text representation: where is the text token sequence
- Keys and Values from the visual representation: and where is the visual token sequence
The attention output is then:
where:
- is the query matrix from text tokens with query dimension
- is the key matrix from visual tokens with key dimension
- is the value matrix from visual tokens with value dimension
- The output has shape , one attended feature per text token
Each text token attends to all visual tokens, creating a weighted combination of visual values that is most relevant to that text token's query. A text token representing "cat" will attend strongly to image patches containing cat-like features and weakly to patches containing irrelevant background content. This soft, differentiable attention mechanism is precisely how cross-modal alignment is computed through the transformer architecture.
The scaling factor prevents the dot products from growing large as increases, which would otherwise cause softmax to saturate in a region of very small gradients. Recall from the attention chapters that this scaling is necessary for stable training, especially with larger key dimensions.
Bidirectional Cross-Attention
Many architectures apply cross-attention in both directions: text-to-image and image-to-text. This allows each modality to gather information from the other, creating representations where every token is cross-modally informed.
For text-to-image attention, queries come from the text representation and keys and values come from the image representation. Each text token learns which image regions are most relevant to its meaning. The output is a set of image-conditioned text representations where each text token has absorbed relevant visual context.
For image-to-text attention, queries come from the image representation and keys and values come from the text representation. Each image patch learns which text tokens are most relevant to its visual content. The output is a set of text-conditioned image representations where each patch has absorbed relevant linguistic context.
When combined with self-attention within each modality, this bidirectional cross-attention produces richly cross-informed representations. Each token has access to relevant content from both its own modality, through self-attention, and from the other modality, through cross-attention. The interplay between self-attention and cross-attention is what gives cross-encoder architectures their expressive power.