Cross-Attention

Cross-attention mixes information from one representation into another. It is used when queries come from one sequence or object, while keys and values come from a different source.

The basic form is:

where provides queries and provides conditioning context. can be encoded text, image patches, retrieved documents, graph nodes, protein residues, ligand atoms, or tool observations.

For multi-head cross-attention:

where is an optional mask over invalid or unavailable context entries.

When It Appears

  • Encoder-decoder Transformers, where decoder tokens attend to encoder states.
  • Image or video generation conditioned on text.
  • Multimodal models that align image, audio, video, and text features.
  • Structure-based models where ligand representations attend to pocket or protein representations.
  • Agent systems where a policy attends to retrieved context, memory, or tool outputs.

Why It Matters

  • Cross-attention preserves token-level access to the conditioning source.
  • It can be more expressive than compressing the source into one pooled vector.
  • Its memory cost scales with query length times context length.
  • It can hide leakage if conditioning context contains labels or future information.

Interface Contract

FieldQuestion
Query objectWhich tokens request information?
Context objectWhich tokens provide keys and values?
DirectionIs conditioning one-way or bidirectional?
MaskWhich context entries are unavailable or invalid?
Positional relationAre query-context distances, order, or geometry encoded?
Update policyAre only queries updated, or both sides updated in alternating blocks?

Cross-attention is directional. Ligand-to-pocket attention and pocket-to-ligand attention are not the same interface.

Complexity

If the query length is and context length is , the attention matrix has size:

so the main memory and compute scale as:

This matters for long documents, protein residues, dense image patches, and atom-level protein-ligand complexes.

Leakage and Ground Truth Context

Context SourceRisk
retrieved documentsanswer appears verbatim in retrieval set
ground-truth structureevaluation uses information unavailable at inference
ligand-defined pocketpocket chosen using the answer ligand
future tokenscausal generation becomes invalid
tool outputtool may expose labels or hidden test information

Checks

  • What object provides queries?
  • What object provides keys and values?
  • Is the context fixed, retrieved, generated, or ground truth?
  • Does the model need bidirectional exchange, or one-way conditioning only?
  • Are masks preventing access to future or invalid context?
  • Does context length dominate the compute budget?
  • Is cross-attention being interpreted as causal evidence without validation?