Retrieval-Augmented Training › Integrating Retrieved Context: The Mechanics
Language AI HandbookAll chapters Retrieval-Augmented Training › Integrating Retrieved Context: The MechanicsOne section · Sections in this chapter6 / 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
Understanding how retrieved text is used in the forward pass requires working through the architecture in detail.
Encoding Neighbors
The neighbor encoder is a bidirectional transformer that processes each retrieved sequence independently. Given a retrieved neighbor of length tokens (typically 128 tokens: 64 for the neighbor plus 64 for the continuation), the encoder produces a sequence of hidden states:
where is the encoder hidden dimension. These hidden states are the keys and values that the main model attends to through CCA.
Because the encoder is bidirectional, each token in the neighbor sequence attends to all other tokens, including those that come after it. This is appropriate because the neighbor is fully observed at inference time. The model is not generating the neighbor; it is reading it.
The encoder processes all neighbors in parallel. Their outputs are concatenated along the sequence dimension to form the full key-value matrix that the main model attends to for a given chunk.
Cross-Attention Integration
When the main model reaches a CCA layer, it performs cross-attention from the token representations in chunk to the encoded neighbor representations. Each token in can attend to all tokens across all encoded neighbors.
The cross-attention mechanism computes:
where:
- is the main model's hidden state for tokens in chunk
- is the concatenated encoded representation of all neighbors for chunk
- are learned projection matrices
- is the key dimension
The output of CCA is combined with the main model's self-attention output through a residual connection and layer normalization, before being passed to the feed-forward sublayer.
This design has a natural interpretation. The self-attention over allows tokens to contextualize themselves relative to the current local and global sequence context. The cross-attention to neighbors then allows those contextualized representations to be enriched with retrieved information. The feed-forward network integrates these two sources of information into the final token representations.
Causal Constraints and the Retrieval Lag
The causal constraint in RETRO creates a one-chunk lag in retrieval. When predicting tokens in chunk , the model uses retrieved neighbors queried from the prefix ending at chunk . This means the model never retrieves information that is directly relevant to the tokens it is currently predicting based on those same tokens, which preserves autoregressive causality.
In practice, this lag is small (64 tokens) and the retrieved neighbors are typically relevant because they were retrieved using the preceding context, which is closely related to the current context in most natural text. A text that discusses the French Revolution in one chunk will almost certainly continue to discuss it in the next chunk, so neighbors retrieved for the preceding context remain relevant.
The lag does create a theoretical gap at sequence boundaries, where the very beginning of a new topic might not have optimal retrievals because the preceding context established a different topic. This is a known limitation of the chunked approach.