Theoretical Understanding of In-Context Learning in Shallow Transformers with Unstructured Data
Yue Xing, Xiaofeng Lin, Chenheng Xu, Namjoon Suh, Qifan Song, Guang Cheng
TL;DR
This paper analyzes the theoretical mechanisms by which transformers perform in-context learning when demonstrations are provided as unstructured data in the prompt, rather than the structured single-column format. It shows that a two-layer transformer with a look-ahead attention mask is necessary for ICL on unstructured prompts, while a single-layer model fails; positional encoding and larger embedding dimensions further improve the matching between input and target tokens, enabling more accurate predictions. The work provides rigorous theorems and supporting simulations demonstrating how x_i and y_i can be effectively matched in the first layer and how the second layer can extract y_q with error diminishing as prompt length grows, under certain architectural choices. These results clarify the minimal architectural ingredients needed for ICL on unstructured data and offer guidance for prompt design and transformer design in practical settings.
Abstract
Large language models (LLMs) are powerful models that can learn concepts at the inference stage via in-context learning (ICL). While theoretical studies, e.g., \cite{zhang2023trained}, attempt to explain the mechanism of ICL, they assume the input $x_i$ and the output $y_i$ of each demonstration example are in the same token (i.e., structured data). However, in real practice, the examples are usually text input, and all words, regardless of their logic relationship, are stored in different tokens (i.e., unstructured data \cite{wibisono2023role}). To understand how LLMs learn from the unstructured data in ICL, this paper studies the role of each component in the transformer architecture and provides a theoretical understanding to explain the success of the architecture. In particular, we consider a simple transformer with one/two attention layers and linear regression tasks for the ICL prediction. We observe that (1) a transformer with two layers of (self-)attentions with a look-ahead attention mask can learn from the prompt in the unstructured data, and (2) positional encoding can match the $x_i$ and $y_i$ tokens to achieve a better ICL performance.
