NeurIPS 2026Coming to NeurIPS?Explore papers, orals & directions

How Transformers Learn Causal Structures In-Context: Explainable Mechanism Meets Theoretical Guarantee

Jianzhe Wei, Siyu Chen, Jianliang He, Zhuoran YangICLR 2026

Transformers have demonstrated a remarkable "magic" trick known as in-context learning (ICL)—the ability to adapt to new tasks from just a few examples without any parameter updates. However, most theoretical explanations of ICL assume that the data follows a fixed, rigid structure. In reality, the world is dynamic; the way events or words depend on each other changes based on context.

This paper investigates a fundamental question: Can Transformers infer these shifting "causal structures" (which elements depend on which predecessors) purely from the examples provided in a prompt? The authors reveal that Transformers do not just memorize patterns; they perform sophisticated statistical inference to uncover hidden causal structures on the fly.

The Challenge of Dynamic Contexts

In standard sequence modeling, dependencies are often treated as static. But consider a real-world scenario: in one document, a stock price might depend on interest rates, while in another, it might depend on a CEO's tweet. To understand if Transformers can adapt to these shifting rules, the researchers needed a framework where the underlying "physics" of the sequence changes from prompt to prompt.

Methodology: Markov Chains and Bayesian Inference

The authors introduce a novel framework using Markov chains with random causal dependencies. In this setup, each token in a sequence depends on exactly one previous token (its "parent"). Crucially, the identity of this parent is hidden and varies between different prompts.

In-context learning setup with Markov chains

To study how models solve this, the researchers focused on:

  • A 2-Layer Transformer: Specifically designed with Relative Positional Embeddings (RPE) to make the mathematical analysis tractable.
  • Bayesian Model Averaging (BMA): The gold-standard statistical approach for dealing with uncertainty. Instead of picking one "best" causal graph, BMA considers all possible graphs, weights them by how well they explain the data, and averages their predictions.
  • Mechanistic Interpretability: Rather than just evaluating the final output, the team analyzed internal attention patterns and weight matrices to see if the model was actually "calculating" the BMA solution.

The study relies on synthetic datasets—discrete Markov chains and continuous Linear Dynamical Systems (LDS)—where the ground truth is perfectly known, allowing for a direct comparison against the mathematically optimal statistical baseline.

Mechanistic Insights: The Copier and the Inferrer

Through rigorous theoretical and empirical analysis, the authors prove a specific functional division of labor within the Transformer:

  1. The "Copier" (Layer 1): The first layer retrieves historical pairs of tokens from the context.
  2. The "Inferrer" (Layer 2): The second layer uses these pairs to calculate the likelihood of different causal links, effectively implementing BMA.
Attention patterns in the first layer

In a trained Transformer, the attention weights in the second layer aren't just arbitrary scores. They directly represent the model's "confidence" (posterior probability) regarding which previous token is the true causal parent. Furthermore, the researchers found that the trained weights actually learn to encode the log-probabilities of the data-generating process. The model's parameters literally store the "physics" of the task.

Key Results and the Continuous Gap

  • Theoretical Guarantee: The paper's core theorem demonstrates that a 2-layer Transformer can exactly implement the BMA algorithm for causal structure inference.
  • Empirical Convergence: When trained via standard gradient descent, Transformers naturally "find" this BMA-like solution. Their parent-selection loss closely tracks the theoretical BMA baseline.
  • Information-Theoretic Discovery: The model identifies causal parents early in training by maximizing a specific information-theoretic metric ($\chi^2$-mutual information) that isolates the strongest dependency signals in the data.
  • The Continuous Gap: While Transformers are nearly perfect at BMA for discrete data, they hit a representational ceiling with continuous data. Because standard attention is "bilinear," it struggles to perfectly capture the quadratic terms required for optimal inference in continuous Gaussian systems.

Limitations and Future Work

The current model assumes a relatively simple causal structure: a latent tree where each token has exactly one parent. Real-world data, such as human language or computer code, involves much more complex, non-linear, and hierarchical dependencies. Future work will need to explore how models handle Directed Acyclic Graphs (DAGs) with multiple parents, and investigate architectural tweaks to overcome the representational limits discovered in continuous spaces.

Summary updated

BibTeX
@inproceedings{wei2026how,
  author = {Jianzhe Wei and Siyu Chen and Jianliang He and Zhuoran Yang},
  title = {{How Transformers Learn Causal Structures In-Context: Explainable Mechanism Meets Theoretical Guarantee}},
  booktitle = {International Conference on Learning Representations},
  year = {2026},
  url = {https://openreview.net/forum?id=bpF8zgSt41},
}
Figure