Retrieval-Augmented Decision Transformer: External Memory for In-context RL

Thomas Schmied, Fabian Paischer, Vihang Patil, Markus Hofmarcher, Razvan Pascanu, Sepp Hochreiter

TL;DR

RA-DT brings the power of Retrieval-Augmented Generation (RAG) to Reinforcement Learning, allowing agents to solve complex tasks by retrieving relevant sub-trajectories from an external memory. This approach enables in-context learning in environments with long episodes and sparse rewards while using significantly shorter context windows and training up to 7x faster than standard baselines.

What Problem Does This Paper Address?

In-context learning (ICL) allows models to adapt to new tasks simply by observing examples within their input context—no fine-tuning required. While this has revolutionized NLP, applying ICL to Reinforcement Learning (RL) is difficult.

Current state-of-the-art methods like Algorithm Distillation (AD) or Decision Pre-trained Transformers (DPT) require entire past episodes to be stored in the model's context window. This creates two major bottlenecks:

  1. Computational Cost: Transformers have a quadratic complexity relative to context length. RL episodes can span thousands of steps, making them prohibitively expensive to process.
  2. Information Density: In environments with sparse rewards, most of an episode is "noise." Stuffing a long, mostly useless trajectory into a context window makes it harder for the model to find the "signal" needed to solve the task.

Methods

The authors introduce the Retrieval-Augmented Decision Transformer (RA-DT). Instead of relying on a massive context window, RA-DT uses an external vector index to store past experiences and retrieves only the most relevant sub-trajectories.

Figure 1: Illustration of Retrieval-augmented Decision Transformer (RA-DT). Left: Prior to training, we encode pre-collected trajectories via an embedding model. During training, we retrieve sub-trajectories using the current context as a query and fuse them into layers via cross-attention. Right: During inference, the collected experiences are stored in a retrieval buffer and selectively retrieved during environment interaction depending on the current situation.

Key Components:

  • External Memory: A vector index populated with sub-trajectories. During inference, the agent's own experiences are stored here in real-time.
  • Retrieval Mechanism: The model uses the current trajectory as a query to perform a Maximum Inner Product Search (MIPS) against the memory.
  • Experience Reweighting: Retrieved sub-trajectories are scored based on relevance (cosine similarity to the query) and utility. During training, utility is based on task IDs; during inference, it is based on the total return of the episode the sub-trajectory came from.
  • Cross-Attention Fusion: The retrieved "relevant" experiences are fused into the Decision Transformer architecture via interleaved cross-attention layers.
  • Domain-Agnostic Embedding: Crucially, the authors show that the embedding model used for retrieval doesn't have to be trained on RL data. They use a frozen BERT model combined with a "FrozenHopfield" mechanism to map RL state-action tokens into a language embedding space.
Figure 2: Illustration of experience reweighting. Given a query trajectory, we retrieve the top l > k most relevant experiences by maximum inner product search. Each experience has an associated task ID and return, based on which we compute its utility. We reweight by srel and su to obtain the final retrieval score sret and return the top-k experiences.

Datasets

The researchers evaluated RA-DT across a diverse set of benchmarks:

  • Grid-worlds: Dark-Room, Dark Key-Door, and Maze-Runner. These involve navigating to invisible goals or sequences of goals. The authors released new datasets for these to foster further research.
  • Robotics: Meta-World (Sawyer arm tasks) and DeepMind Control Suite (DMControl).
  • Video Games: Procgen, a suite of 16 procedurally generated games designed to test generalization.

Key Insights and Ideas

  • Efficiency through Sparsity: By retrieving only relevant snippets, RA-DT avoids the quadratic cost of long contexts. On the largest grid-world (40x20), RA-DT trained nearly 7x faster than baselines.
  • Language Models as RL Encoders: The "Domain-Agnostic" variant of RA-DT performs almost as well as domain-specific versions. This suggests that pre-trained LLMs like BERT already possess a latent structure capable of organizing and retrieving abstract trajectory data.
  • Bursty Sequences: Retrieval naturally creates "bursty" data distributions (sequences of highly related information), which is a known catalyst for emergent in-context learning.
  • Memory vs. Meta-Learning: The paper highlights a distinction between an agent simply "copying" a successful path from memory (memory exploitation) and actually "learning to learn" (meta-learning).

Key Results

  • Superior Performance: In grid-world environments, RA-DT consistently outperformed AD and DPT, especially as the grid size (and thus episode length) increased.
  • Maze-Runner Success: RA-DT was the only method capable of showing significant performance improvements over the course of multiple ICL trials in the complex Maze-Runner environment.
  • Robotics & Games: While RA-DT achieved higher raw scores on unseen tasks in Meta-World and Procgen, the authors noted that neither RA-DT nor the baselines showed strong "learning-to-learn" curves in these high-dimensional settings, indicating a remaining challenge for the field.

Limitations and Future Work

  • Delusions: Like LLMs, RA-DT can suffer from "delusions"—in this context, repeating suboptimal or unsuccessful action sequences because they were retrieved from memory.
  • Exploration vs. Exploitation: The current model is better at imitating successful past experiences than systematically exploring to find new ones. The authors suggest training a value function to help the model infer the utility of actions more effectively.
  • Expert Demonstrations: Future work could involve "pre-filling" the external memory with expert human demonstrations, potentially allowing the agent to solve highly complex robotics tasks immediately upon deployment.

Summary updated

BibTeX
@misc{schmied2024retrieval,
  author = {Thomas Schmied and Fabian Paischer and Vihang Patil and Markus Hofmarcher and Razvan Pascanu and Sepp Hochreiter},
  title = {{Retrieval-Augmented Decision Transformer: External Memory for In-context RL}},
  year = {2024},
  eprint = {2410.07071},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2410.07071},
}
Figure