In-Context Compositional Q-Learning for Offline Reinforcement Learning

Qiushui Xu, Yu-Hao Huang, Yushu Jiang, Wenliang Zheng, Lei Song, Jinyu Wang, Jiang Bian

TL;DR

ICQL (In-Context Compositional Q-Learning) shifts the offline reinforcement learning paradigm from fitting a single global Q-function to adaptively inferring local value functions using linear Transformers. By treating value estimation as a contextual inference problem, ICQL achieves state-of-the-art results on complex, multi-stage tasks like the D4RL Kitchen benchmark, outperforming standard baselines by up to 16.4%.

The Challenge of Offline Reinforcement Learning

Offline Reinforcement Learning (RL) is the art of learning a policy from a fixed dataset without any new environment interaction. It is notoriously difficult due to distributional shift: when a learned policy tries an action not well-represented in the training data, the value function (Q-function) often produces wildly over-optimistic estimates, leading to catastrophic failure.

Most current methods (like CQL or IQL) attempt to solve this by regularizing a single, global neural network that represents the Q-function for the entire state space. However, the authors observe that many RL tasks are inherently compositional. For example, in a robotic kitchen task, the "knowledge" needed to open a microwave is fundamentally different from the knowledge needed to turn on a light. A single global model often struggles to capture these distinct sub-structures, leading to "blurry" value estimates that fail to generalize across diverse subtasks.

Methodology: In-Context Compositional Q-Learning

To address these limitations, the authors introduce In-Context Compositional Q-Learning (ICQL). Instead of asking a global network, "What is the value of this state?" ICQL asks, "Given these $k$ similar transitions I've seen before, what is the value of this state?"

The framework consists of three main components:

  1. Retrieval: For any query state-action pair, the system retrieves the $k$ most similar transitions from the offline dataset.
  2. In-Context Inference: These retrieved transitions are fed as "context" into a linear Transformer.
  3. Linear Attention as an Optimizer: A key technical contribution is showing that the forward pass of a linear Transformer can mathematically implement the iterative updates of Temporal Difference (TD) learning. Essentially, the Transformer "learns" the local Q-function on the fly for that specific neighborhood of the state space.
t-SNE visualization of state distribution and retrieved neighbors for two distinct queries.

By using linear attention, the model avoids the quadratic complexity of standard Transformers while maintaining the ability to perform complex contextual reasoning. The final policy is extracted using advantage-weighted regression, similar to the popular IQL framework, but powered by this much more accurate, localized critic.

Datasets and Evaluation

The researchers evaluated ICQL on the D4RL benchmark, the gold standard for offline RL. This includes:

  • MuJoCo Tasks: Standard locomotion (HalfCheetah, Hopper, Walker2d).
  • Adroit Tasks: High-dimensional robotic hand manipulation (Pen, Hammer, Door).
  • Kitchen Tasks: Long-horizon, multi-stage tasks that require completing several sub-goals in sequence.

While no new datasets were introduced, the focus on the Kitchen tasks is pivotal, as these environments are specifically designed to test compositional reasoning and multi-step planning.

Key Insights and Ideas

  • The "Aha" Moment for Linear Attention: The authors leverage a burgeoning theory in deep learning: Transformers aren't just sequence models; they are "meta-optimizers." They prove that ICQL’s architecture can implement a pre-conditioned version of the SARSA algorithm within its internal layers.
  • Locality is a Feature, Not a Bug: Global models often suffer from interference, where learning a new task "overwrites" knowledge of an old one. By forcing the model to look only at local neighbors, ICQL naturally isolates subtasks, making the value estimation much more robust.
  • Implicit Hierarchy: Unlike Hierarchical RL, which requires humans to manually label subtasks (e.g., "this is the 'opening door' phase"), ICQL discovers these structures implicitly through its retrieval mechanism. If the retrieved neighbors all involve "opening doors," the Transformer naturally specializes its value estimate for that specific behavior.

Key Results

  • Superior Performance: ICQL achieved an average gain of 16.4% over the best baseline on Kitchen tasks. On MuJoCo and Adroit, it saw improvements of 8.8% and 6.3%, respectively.
  • Accuracy Visualization: Using t-SNE plots, the authors showed that ICQL’s value estimates correlate much more closely ($r=0.69$) with an "oracle" online RL agent (SAC) than standard offline methods like IQL ($r=0.29$).
  • Efficiency: Despite the retrieval step, ICQL remains computationally competitive. By caching retrieval indices, the added overhead is only about 0.03 ms per training step.

Limitations and Future Work

  • The "Hammer-Human" Failure: ICQL struggled on the Hammer-Human dataset. The authors trace this to the dataset's small size and sparse rewards. If the "local neighborhood" is empty or contains only low-quality data, the in-context inference has nothing useful to learn from.
  • Retrieval Dependency: The performance is heavily tied to the distance metric used for retrieval (currently $L_2$ or cosine similarity). In very high-dimensional spaces, these metrics can fail to capture semantic similarity.
  • Future Directions: The authors suggest integrating pre-trained state encoders (like those from computer vision or foundation models) to improve retrieval quality and exploring learnable retrieval mechanisms that can adapt during training.

Summary updated

BibTeX
@inproceedings{xu2026context,
  author = {Qiushui Xu and Yu-Hao Huang and Yushu Jiang and Wenliang Zheng and Lei Song and Jinyu Wang and Jiang Bian},
  title = {{In-Context Compositional Q-Learning for Offline Reinforcement Learning}},
  booktitle = {International Conference on Learning Representations},
  year = {2026},
  url = {https://openreview.net/forum?id=ZBbKLvH0w4},
}
Figure