Uncovering hidden geometry in Transformers via disentangling position and context

Jiajun Song, Yiqiao ZhongSubmission to ICLR 2024 · Rejected

TL;DR

By applying a two-way ANOVA-style decomposition to transformer hidden states, the authors disentangle embeddings into position, context, and residual components. They reveal that positional vectors trace out smooth, low-dimensional spirals, context vectors cluster cleanly by topic, and the two representations are virtually orthogonal (mutually incoherent)—providing a clear geometric explanation for induction heads and length generalization failures.

What Problem Does This Paper Address?

Despite the dominance of transformers, intermediate-layer embeddings remain notorious black boxes. Researchers frequently observe puzzling geometric quirks—such as extreme anisotropy (embeddings clustering in narrow cones) and mysterious spiral manifolds—without a unifying explanation. Furthermore, because positional encodings are usually injected only at the input layer (or via rotary embeddings), it has been difficult to quantify how much positional versus semantic context persists throughout deeper layers and how attention heads combine the two.

Methods

The authors introduce an empirical, mean-based decomposition of hidden representations across layers, analogous to two-way Analysis of Variance (ANOVA):

$$h_{c,t}^{(\ell)} = \mu^{(\ell)} + pos_t^{(\ell)} + ctx_c^{(\ell)} + resid_{c,t}^{(\ell)}$$

Where:

  • $\mu^{(\ell)}$ is the global mean vector across all sequences and positions.
  • $pos_t^{(\ell)}$ is the positional basis (mean embedding across contexts at position $t$).
  • $ctx_c^{(\ell)}$ is the context basis (mean embedding across positions for sequence $c$).
  • $resid_{c,t}^{(\ell)}$ is the residual vector capturing token-specific and higher-order interactions.
  • $cvec_{c,t}^{(\ell)} = ctx_c^{(\ell)} + resid_{c,t}^{(\ell)}$ combines context and residual components.

Using this framework, the authors analyze the geometry via:

  • Fourier & Spectral Analysis: 2D Discrete Cosine Transforms (DCT) and singular value decay to measure the frequency concentration of the positional Gram matrix.
  • Smoothness Theorems: Theoretical bounds proving that higher-order smoothness of the position Gram matrix enforces low-rank, low-frequency representation.
  • QK Matrix Decomposition: Splitting self-attention query-key interactions into four bilinear parts ($pos\text{-}pos$, $pos\text{-}cvec$, $cvec\text{-}pos$, and $cvec\text{-}cvec$).

Datasets

The authors evaluate models on diverse natural language, code, and synthetic datasets:

  • OpenWebText (general web text)
  • WikiText (encyclopedic text)
  • GitHub (code corpora for out-of-distribution evaluation)
  • Shakespeare (used for smaller models like NanoGPT)
  • Synthetic Arithmetic: An addition dataset formatted as "a + b = c" with multi-digit numbers (tested under carry and no-carry conditions with reversed output order).

Key Insights and Ideas

  • Positional Smoothness Generates Continuous Spirals: Positional bases form low-dimensional, curving spirals across layers. Gram matrix smoothness naturally biases models toward local attention (near tokens score higher QK affinities).
  • Mutual Incoherence (Near-Orthogonality): Across architectures, $pos_t$ and $ctx_c$ are nearly orthogonal. In compressed sensing and kernel theory, mutual incoherence allows complex composite kernels to factorize cleanly into independent multiplicative components.
  • Deconstructing Induction Heads: The four-way QK decomposition reveals the circuit anatomy of in-context copy-paste mechanisms:
  • Self-attention heads rely on dominant $cvec\text{-}cvec$ components to match identical tokens.
  • Neighbor heads rely on smooth $pos\text{-}pos$ interactions to shift attention to adjacent tokens.
  • Induction heads merge these operations to look back and copy the token following an earlier match.
  • Why Arithmetic Struggles with Length Generalization: On standard language tasks, positional Gram matrices are smooth. In contrast, models trained on arithmetic show fractured, discontinuous $pos\text{-}pos$ patterns, directly explaining why transformers struggle to generalize to longer digit sequences.

Key Results

  • Low-Rank Concentration: Positional bases concentrate in very low-dimensional subspaces across all layers. Estimated intrinsic ranks range from 7.86 (NanoGPT) to 12.62 (BERT), even though hidden dimensions are 768 or higher.
  • Significant Relative Magnitude: Positional bases contribute heavily to embedding magnitude, with relative operator norms $\|P\|_{op} / \|M\|_{op}$ reaching 0.66 in NanoGPT, 0.31 in GPT-2, and 0.24 in BERT.
  • Low Mutual Incoherence: Cosine similarity between normalized positional and context bases remains close to zero (e.g., 0.039–0.051 for GPT-2 and 0.043–0.046 for BERT).
  • Semantic Clustering: The context basis cleanly separates documents by topic. For Llama 2 and BLOOM, intra-cluster similarity reaches up to 1.00, compared to inter-cluster baselines of 0.12–0.33.

Limitations and Future Work

  • Focus on Pretrained Snapshots: The study primarily analyzes frozen, pretrained checkpoints due to compute constraints, rather than tracking how geometry evolves continuously throughout training.
  • Underexplored Residuals: The authors focus primarily on the first-order mean vectors ($pos$ and $ctx$); non-linear interactions within the residual term ($resid_{c,t}$) remain largely uncharacterized.
  • Extension to Better Arithmetic Formats: Investigating whether prompt formatting or alternate positional encodings can prevent fractured positional structures in reasoning and arithmetic remains open for future research.

Summary updated

Abstract

Transformers are widely used to extract complex semantic meanings from input tokens, yet they usually operate as black-box models. In this paper, we present a simple yet informative decomposition of hidden states (or embeddings) of trained transformers into interpretable components. For any layer, embedding vectors of input sequence samples are a tensor $h \in R^{C \times T \times d}$. Given embedding vector $h_{c,t} \in R^d$ at sequence position $t \le T$ in a sequence (or context) $c \le C$, extracting the mean effects yields the decomposition $$ h_{c,t} = \mu + pos_t + ctx_c + resid_{c,t} $$ where $\mu$ is the global mean vector, $pos_t$ and $ctx_c$ are the mean vectors across contexts and across positions respectively, and $resid_{c,t}$ is the residual vector. For popular transformer architectures and diverse text datasets, empirically we find pervasive mathematical structure: (1) $(pos_t)_t$ forms a low-dimensional, continuous, and often spiral shape across layers, (2) $(ctx_c)_c$ shows clear cluster structure that falls into context topics, and (3) $(pos_t)_t$ and $(ctx_c)_c$ are mutually incoherent---namely $pos_t$ is almost orthogonal to $ctx_c$---which is canonical in compressed sensing and dictionary learning. This decomposition offers structural insights about input formats in in-context learning (especially for induction heads) and in length generalization (especially for arithmetic tasks).
BibTeX
@misc{song2023uncovering,
  author = {Jiajun Song and Yiqiao Zhong},
  title = {{Uncovering hidden geometry in Transformers via disentangling position and context}},
  year = {2023},
  eprint = {2310.04861},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2310.04861},
}
Figure