Cross-attention is an attention mechanism where the Query vectors originate from one sequence (the target) and the Key and Value vectors originate from a different sequence (the source), enabling the target to extract contextually relevant information from the source. This is the defining operation in encoder-decoder architectures, where the decoder uses cross-attention to focus on the encoder's output representations while generating each token.
Glossary
Cross-Attention

What is Cross-Attention?
Cross-attention is a mechanism in Transformer architectures that allows one sequence to focus on relevant parts of another, distinct sequence by using queries derived from one source and keys/values from another.
Unlike self-attention, where Q, K, and V all come from the same sequence, cross-attention establishes an information bottleneck between two distinct modalities or sequences, such as a source sentence and its translation. The mechanism computes attention weights by measuring the compatibility between the target's Query and the source's Keys, then uses these weights to aggregate the source's Values into a context vector that informs the target's next representation.
Key Characteristics of Cross-Attention
Cross-attention is the architectural bridge that allows one sequence to extract contextually relevant information from a completely different sequence. It is the defining mechanism of Encoder-Decoder Transformers and multi-modal models.
Asymmetric Source of QKV Vectors
Unlike Self-Attention, where Query, Key, and Value vectors all originate from the same input sequence, cross-attention uses an asymmetric source. The Query vectors are derived from the target sequence (e.g., the decoder's current hidden state), while the Key and Value vectors are derived from an external source sequence (e.g., the encoder's output). This allows the target to 'look up' relevant information in the source.
The Bridge in Encoder-Decoder Architectures
In models like the original Transformer and T5, cross-attention is the sole mechanism connecting the encoder and decoder stacks. The encoder processes the input (e.g., a French sentence) into a dense representation. The decoder then uses cross-attention at every layer to query this representation, ensuring that each generated token (e.g., English translation) is grounded in the full context of the source input.
Foundation of Multi-Modal Fusion
Cross-attention is the primary operator for fusing distinct data modalities. In a text-to-image model like Stable Diffusion, the embedded text prompt acts as the Key and Value source, while the latent image pixels generate the Query. This conditions the image generation process on the semantic meaning of the text without forcing the two modalities into a single representation space.
Computational Complexity Profile
The complexity of cross-attention is O(L_target * L_source), where L_target is the length of the sequence generating the Query and L_source is the length of the sequence providing the Keys and Values. This is distinct from self-attention's O(n^2) complexity. If the source sequence is a fixed-length latent array, as in Perceiver architectures, the complexity becomes linear with respect to the target sequence length.
Cross-Attention vs. Self-Attention
A direct comparison clarifies their distinct roles in sequence modeling:
- Self-Attention: Q, K, V all from the same sequence. Purpose is to build contextualized representations within a single input.
- Cross-Attention: Q from one sequence, K and V from another. Purpose is to inject external information into the current sequence's processing.
- Causal Self-Attention: A masked variant of self-attention used in decoders to prevent looking at future tokens.
Role in Retrieval-Augmented Generation (RAG)
In advanced RAG systems, cross-attention can be used to fuse retrieved document chunks directly into the generation process. The language model's hidden state acts as the Query, and the retrieved chunks serve as the Key and Value source. This is a deeper, more integrated alternative to simple prompt concatenation, allowing the model to attend to specific parts of retrieved documents while generating each token.
Enabling Efficiency, Speed & Accuracy
Intelligent Analysis, Decision & Execution
We build AI systems for teams that need search across company data, workflow automation across tools, or AI features inside products and internal software.
Talk to Us
Search across company data
Give teams answers from docs, tickets, runbooks, and product data with sources and permissions.
Useful when people spend too long searching or get different answers from different systems.

Automate internal workflows
Use AI to route work, draft outputs, trigger actions, and keep approvals and logs in place.
Useful when repetitive work moves across multiple tools and teams.

Add AI to products and internal tools
Build assistants, guided actions, or decision support into the software your team or customers already use.
Useful when AI needs to be part of the product, not a separate tool.
Frequently Asked Questions
Clear, technically precise answers to the most common questions about how cross-attention enables information transfer between distinct sequences in Transformer architectures.
Cross-attention is an attention mechanism where the Query vectors originate from one sequence (the target) and the Key and Value vectors originate from a different sequence (the source), enabling the target sequence to selectively extract relevant information from the source. Unlike self-attention—where Q, K, and V all come from the same sequence—cross-attention fuses information across two distinct modalities or representations. The mechanism computes attention scores by taking the dot product of each target Query with all source Keys, scales by 1/√d_k, applies softmax normalization, and uses the resulting weights to compute a weighted sum of source Values. This operation is fundamental to the Encoder-Decoder Transformer architecture, where the decoder's cross-attention sub-layer attends to the encoder's output representations, allowing the model to align generated tokens with input tokens during tasks like machine translation.
Related Terms
Cross-attention is a foundational component of sequence-to-sequence architectures. Understanding its relationship to other attention mechanisms and architectural patterns is crucial for designing effective Transformer models.
Self-Attention
The contrasting mechanism where Query, Key, and Value vectors all originate from the same input sequence. While cross-attention mixes information between two distinct sequences (e.g., encoder and decoder), self-attention computes contextualized representations within a single sequence, allowing each token to gather information from every other token in that same sequence.
Encoder-Decoder Architecture
The canonical architecture that relies on cross-attention. The encoder processes the full input sequence into a dense, contextualized representation. The decoder then generates the output autoregressively, using cross-attention in each decoder layer to extract relevant information from the encoder's final output. This is the original design of the Transformer, used in models like T5 and BART.
Query, Key, Value (QKV) Vectors
The three learned linear projections that form the basis of all attention calculations. In cross-attention specifically:
- Query (Q): Derived from the decoder's current hidden state.
- Key (K): Derived from the encoder's output.
- Value (V): Also derived from the encoder's output. The Query interacts with the Keys to determine attention weights, which are then applied to the Values to produce the output.
Multi-Head Cross-Attention
An extension of cross-attention where multiple parallel attention operations are performed, each with its own learned Q, K, V projections. This allows the decoder to jointly attend to information from different representation subspaces of the encoder output. For example, one head might focus on syntactic alignment while another focuses on semantic meaning.
Causal Self-Attention
A masking mechanism used in the decoder's self-attention layers (not cross-attention) that prevents a token from attending to future tokens. This ensures the autoregressive property during generation. Cross-attention, by contrast, has no causal mask because the decoder can attend to the entire encoder output sequence simultaneously, as it is fully known in advance.
Cross-Encoder Re-Ranking
A downstream application that uses full cross-attention between a query and a document to compute a relevance score. Unlike a Bi-Encoder that encodes each independently, a Cross-Encoder processes the concatenated query-document pair through a Transformer, allowing the model to capture fine-grained interactions via cross-attention. This is slower but significantly more precise for re-ranking top retrieval candidates.

About the author
Prasad Kumkar
CEO & MD, Inference Systems
Prasad Kumkar is the CEO & MD of Inference Systems and writes about AI systems architecture, LLM infrastructure, model serving, evaluation, and production deployment. Over 5+ years, he has worked across computer vision models, L5 autonomous vehicle systems, and LLM research, with a focus on taking complex AI ideas into real-world engineering systems.
His work and writing cover AI systems, large language models, AI agents, multimodal systems, autonomous systems, inference optimization, RAG, evaluation, and production AI engineering.
Partnered with leading AI, data, and software stack.
How We Work
Custom AI workflows for your Business
One-fit-all AI don't work for modern businesses. At Inferensys, we aim to understand your business & custom requirements; which we use to define most efficient agentic workflows, the data, and the tools for your business.
01
Review the use case
We understand the task, the users, and where AI can actually help.
Read more02
Pick the right approach
We define what needs search, automation, or product integration.
Read more03
Build the first useful version
We implement the part that proves the value first.
Read more04
Improve from there
We add the checks and visibility needed to keep it useful.
Read moreThe first call is a practical review of your use case and the right next step.
Talk to Us