Decomposing Query-Key Feature Interactions Using Contrastive Covariances

Andrew Lee, Yonatan Belinkov, Fernanda Viégas, Martin Wattenberg

TL;DR

This paper introduces a novel contrastive covariance method to unpack the query-key (QK) space of Transformer attention heads into low-rank, human-interpretable feature subspaces. By isolating where specific features align between queries and keys, the authors successfully explain why attention heads attend to specific tokens and demonstrate how to causally manipulate attention patterns or attribute attention logits directly to individual features.

What Problem Does This Paper Address?

Mechanistic interpretability aims to reverse-engineer neural networks into understandable algorithms. While we have made progress understanding individual neurons or residual stream layers, attention heads remain notoriously opaque.

An attention head determines which tokens interact by computing a scalar dot product between a query vector ($q$) and a key vector ($k$). However, reducing high-dimensional vectors to a single scalar conceals the underlying feature dynamics: Which specific attributes triggered this connection? This paper tackles this gap by analyzing the bilinear QK joint embedding space to isolate and explain the precise feature interactions driving attention decisions.

Methods

The core innovation is the Contrastive Covariance Matrix ($\Delta C$). To isolate a specific feature of interest (e.g., whether a token is a "fruit"), the method constructs pairs of datasets that vary only by that feature while holding all other latent factors constant:

  1. Positive Covariance ($C^+$): Measures query-key correlations when the feature matches between the query and key.
  2. Negative Covariance ($C^-$): Measures correlations when the feature does not match.

Subtracting the two yields the contrastive covariance matrix: $$\Delta C = C^+ - C^-$$

Figure 1. Contrastive covariance method schema. We define positive and negative covariance terms between queries and keys, each capturing the presence (or absence) of a feature. The resulting contrastive covariance term isolates the feature in QK space.

Applying Singular Value Decomposition (SVD) to $\Delta C$ extracts the exact rank and basis vectors of the feature's subspace within both the query and key spaces.

Furthermore, because pre-softmax attention logits are linear with respect to the query, the authors project the query vector onto these recovered subspaces to compute Attention Logit Attributions. This breaks down the final attention score into exact contributions from individual features, leaving a clearly defined "residual" of unexplained logit mass.

Datasets

The authors validate their approach using both synthetic benchmarks and large language models (LLMs):

  • Synthetic Toy Model: A custom "payload retrieval task" where tokens are generated from controlled discrete (binary sign vectors) or continuous (Gaussian) latent variables.
  • LLM Evaluation: Custom evaluation datasets built for Llama 3.1-8B Instruct and Qwen 3-4B Instruct.
  • Categorical Semantics: 2,000 prompts listing items across various categories (e.g., animals, vehicles, fruits) to study "Filter Heads."
    • Entity Binding: 3,000 prompts containing varying numbers of entity-box pairs (e.g., "The hat is in box O. The jam is in box Z...") to study how models bind identities to positions.

Key Insights and Ideas

  • Attention as Subspace Alignment: High attention scores are generated when query and key vectors align within specific, low-rank feature subspaces.
  • Feature Splits vs. Superposition: When an attention head has sufficient dimensions ($r_1 + r_2 \le d_{head}$), it cleanly decomposes orthogonal latent variables into independent components ("feature splits"). When dimensions are tight, it compresses multiple features into the same space ("superposition"), resulting in off-diagonal interactions.
  • Debugging Attention via Logits: Logit attribution can expose subtle model errors that are normally hidden after the softmax operation, such as a model incorrectly placing feature mass on an unrelated token.

Key Results

1. Synthetic Validation

In the toy task, the contrastive covariance method perfectly recovered the ground-truth ranks of latent variables as long as the head was not bottlenecked by superposition.

When the attention head has sufficient dimensions, the method perfectly recovers the ground-truth ranks (dark green cells).

Visualizing these subspaces via PCA beautifully reconstructed the underlying geometry of the data—recovering a perfect 3D cube for discrete binary features.

Figure 3. PCA of Latent Variable Subspace. We project key and query vectors onto the recovered subspaces of latent variable z1 (of rank r1 = 3), then perform PCA, which recovers the 3D-cube structure of z1. Also note that keys and queries align onto the same clusters. See Figure 12 for the continuous task variant, in which our method recovers the spherical structure of latent variable s1.

2. Causal Interventions

To prove these subspaces are causally functional, the authors projected key vectors into the recovered feature subspaces and swapped their coordinates between an original token ($i_{orig}$) and a target token ($i_{target}$). In both the toy model and real LLMs, this intervention successfully redirected the vast majority of the attention weight to the target token, while random subspace controls had negligible effects.

Figure 4. Causal Interventions on Latent Variable Subspaces. Intervening on the recovered subspaces for latent variables z1 and z2 shifts all the attention from the original token to the target token, while intervening on random subspaces of the same dimension (i.e., “Rand r1, r2, r1 + r2”) has less of an effect.

3. Real-World LLM Subspaces

When applied to Llama 3.1 and Qwen 3, the method successfully localized:

  • Categorical Features: Isolated distinct rank-1 directions for individual semantic categories (fruits, countries, etc.) within Filter Heads.
  • Binding Mechanisms: Found that the positional "order-ID" mechanism operates in a very low-rank subspace (rank 2-3), whereas the "lexical" identity-matching mechanism requires a higher-rank subspace (rank 9-10).

Limitations and Future Work

The primary limitation of this method is that it is supervised: it requires a human experimenter to define what a "feature" is in order to set up the positive and negative contrastive pairs.

The authors note that an obvious and important next step is developing an unsupervised QK decomposition method that can automatically discover multi-dimensional features of varying ranks without human intervention. Additionally, exploring how to interpret and disentangle features under heavy superposition remains an open challenge.

Summary updated

BibTeX
@inproceedings{lee2026decomposing,
  author = {Andrew Lee and Yonatan Belinkov and Fernanda Viégas and Martin Wattenberg},
  title = {{Decomposing Query-Key Feature Interactions Using Contrastive Covariances}},
  booktitle = {International Conference on Machine Learning},
  year = {2026},
  url = {https://openreview.net/forum?id=t5cfrJfuzD},
}
Figure