When can transformers compositionally generalize in-context?

Seijin Kobayashi, Simon Schug, Yassir Akram, Florian Redhardt, Johannes von Oswald, Razvan Pascanu, Guillaume Lajoie, João SacramentoarXiv 2024 · DOI

Abstract

Transformers demonstrate remarkable in-context learning (ICL) capabilities, yet their ability to compositionally generalize—combining known task components in novel ways—remains a significant challenge. This paper investigates the mechanisms behind this failure, revealing a "knowing vs. doing" gap where vanilla Transformers internally identify out-of-distribution (OOD) tasks but fail to execute them correctly. We propose an architectural bottleneck that separates task inference from execution using a hypernetwork. Our results show that this separation enables near-perfect compositional generalization on modular regression tasks, provided the training data exhibits sufficient connectivity between task components.

Introduction

Compositionality is a fundamental aspect of human intelligence, allowing us to solve novel problems by combining familiar sub-tasks. In machine learning, this is termed compositional generalization. While Transformers are theoretically capable of such reasoning, they often rely on statistical shortcuts or pattern matching during in-context learning. This study asks whether Transformers can learn to compose independent modules to generalize across a combinatorial explosion of potential tasks.

Task Generation and Datasets

To evaluate compositional generalization, the authors developed a modular multi-task regression benchmark. Tasks are formed by additively combining independent modules. The training set consists of specific module combinations, while the test set includes Out-of-Distribution (OOD) combinations—pairs of modules never seen together during training.

Task generation process and the compositional split used to test for generalization.

A critical factor identified is "connected support": generalization is only achievable if the training tasks overlap such that the contribution of each module can be disentangled. If the training data is too fragmented, even specialized architectures fail to generalize.

Architectural Approaches

The study compares two primary architectures:

  1. Vanilla Transformer: A standard decoder-only model that predicts the target $y$ directly from a sequence of $(x, y)$ context pairs and a query $x$.
  2. Hypernetwork Transformer: This architecture introduces a "separation of concerns." The Transformer performs task inference, outputting a latent code. This code is then processed by a hypernetwork to generate weights for a small task-specific MLP, which performs the task execution.
Comparison between the Vanilla Transformer and the Hypernetwork Transformer architectures for in-context learning.

Key Results and Insights

The "Knowing vs. Doing" Gap

Linear probing of the Vanilla Transformer's internal states revealed that the model often correctly identifies the latent task code even when its final prediction is incorrect for OOD tasks. This suggests that the failure is not in task identification but in the execution of the identified task.

The Power of the Bottleneck

The Hypernetwork Transformer achieved near-perfect $R^2$ scores (~0.95+) on OOD combinations, significantly outperforming the Vanilla Transformer (~0.80-0.85). The architectural bottleneck acts as a strong inductive bias, forcing the model to learn the underlying modular structure rather than relying on simpler, non-generalizable shortcuts.

Specialization vs. Flexibility

While the Hypernetwork model excels at modular tasks, it fails on unstructured control tasks where the modular prior does not apply. In contrast, the Vanilla Transformer maintains some flexibility, albeit at the cost of robust compositional reasoning. This highlights a trade-off between general-purpose flexibility and specialized compositional robustness.

Conclusion

The failure of standard Transformers to compositionally generalize in-context is often a result of optimization favoring shortcuts over modular solutions. By architecturally separating task inference from execution, we can bridge the "knowing vs. doing" gap. Future work should explore general-purpose motifs that encourage this separation without requiring hard-coded task priors, potentially scaling these principles to large language models.

Summary updated

BibTeX
@misc{kobayashi2024when,
  author = {Seijin Kobayashi and Simon Schug and Yassir Akram and Florian Redhardt and Johannes von Oswald and Razvan Pascanu and Guillaume Lajoie and João Sacramento},
  title = {{When can transformers compositionally generalize in-context?}},
  year = {2024},
  eprint = {2407.12275},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2407.12275},
}
Figure