Learning Compositional Functions with Transformers from Easy-to-Hard Data

Zixuan Wang, Eshaan Nichani, Alberto Bietti, Alex Damian, Daniel Hsu, Jason D. Lee, Denny Wu · DOI

Introduction

Large Language Models (LLMs) have demonstrated a remarkable capacity for multi-step reasoning, yet the theoretical foundations of how they acquire these skills remain poorly understood. While previous research has established that Transformers are expressive enough to represent complex compositional functions, expressivity does not guarantee learnability. This paper investigates the "statistical-computational gap" in multi-step reasoning, demonstrating that while Transformers can solve $k$-fold function compositions, they cannot learn them from "hard" data alone. Instead, the authors show that a curriculum of "easy-to-hard" examples allows gradient descent to find solutions efficiently in polynomial time.

The $k$-fold Composition Task

To model discrete reasoning steps, the researchers focus on a $k$-fold composition task involving permutations. In this setup, the model must interleave two types of rules:

  1. Contextual Rules: Provided directly in the prompt (in-context).
  2. Parametric Rules: Embedded within the model's weights (in-weights).

The task requires the model to take a starting index $x$ and apply a sequence of $k$ permutations. This structure mirrors how LLMs must combine user-provided instructions with their internal knowledge to reach a correct conclusion.

The Statistical-Computational Gap

The core challenge in learning these tasks is that, without intermediate steps, the relationship between the initial input and the final output becomes increasingly opaque. For large $k$, the output appears as random noise to standard learning algorithms.

Using the Statistical Query (SQ) framework—a standard tool for proving the hardness of learning problems—the authors prove a "negative result": any algorithm resembling gradient descent requires an exponential number of samples ($N^{\Omega(k)}$) to learn the $k$-fold task if it is only exposed to the final, "hard" $k$-step examples. This explains why models often fail to generalize on complex logic tasks when trained on end-to-end data without a supporting curriculum.

Efficiency through Curriculum Learning

The authors demonstrate that the hardness of the task can be overcome by providing the model with a path from simplicity to complexity. They analyze two primary training strategies:

  • Staged Curriculum: The model is trained sequentially on 1-hop tasks, then 2-hop, then 4-hop, and so on.
  • Data Mixture: The model is trained on a simultaneous mixture of all hop lengths (from 1 to $k$).

Their analysis reveals that a data mixture is often sufficient. By including "easy" (low-hop) examples in the training set, the Transformer's layers can learn the composition steps sequentially. Under these conditions, the sample complexity and runtime drop from exponential to polynomial in $N$ and $k$, making the task efficiently learnable.

Architectural Insights: Logarithmic Depth

A significant theoretical contribution of the paper is the proof that a Transformer only requires logarithmic depth ($O(\log k)$ layers) to compose $k$ functions. This efficiency stems from the Transformer's ability to process information in parallel; each successive layer can essentially double the number of compositions performed by the previous layer. This suggests that the depth of a Transformer may be more related to the "parallelizable" depth of a reasoning task rather than the absolute number of steps.

Empirical Validation and Limitations

Experiments using standard encoder Transformers confirmed the theoretical findings. Models trained exclusively on 4-hop data failed to generalize, whereas those trained on a mixture of 1, 2, and 4-hop data achieved near-zero loss.

Despite these promising results, the authors note several limitations:

  • Initialization: The analysis assumes that the "Value" matrices in the attention mechanism are initialized with a specific sparse structure, as learning them from zero initialization remains difficult due to vanishing gradients.
  • Embedding Dimension: The current construction requires a large embedding dimension ($d \approx Nk$), which may not reflect the more compact representations used in practical LLMs.
  • Real-world Application: While permutations serve as an excellent proxy for logical reasoning, extending this framework to the nuances of natural language remains a goal for future research.

Conclusion

This work provides a rigorous theoretical basis for the necessity of curriculum learning in complex reasoning. By bridging the gap between expressivity and learnability, the authors show that the success of Transformers in multi-step tasks is not just a matter of model size, but a result of the inherent structure of the data and the presence of simpler, foundational examples that guide the optimization process.

Summary updated

BibTeX
@inproceedings{wang2025learning,
  author = {Zixuan Wang and Eshaan Nichani and Alberto Bietti and Alex Damian and Daniel Hsu and Jason D. Lee and Denny Wu},
  title = {{Learning Compositional Functions with Transformers from Easy-to-Hard Data}},
  booktitle = {Conference on Learning Theory},
  year = {2025},
  url = {https://openreview.net/forum?id=d84iwaRoRV},
}
Figure