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

Data-Adaptive Mahalanobis Metric Learning for Cross-Head Attention in Transformers

Zhenke Duan, Xin Li, Jiqun Pan, Dong Xiaofei, Hanwen Ning, Xinyuan SongNeurIPS 2026

TL;DR

Standard Multi-Head Attention (MHA) forces attention heads to work in isolated silos using a frozen ruler to measure token similarity. This paper introduces DAMCHA, a drop-in replacement that lets heads dynamically collaborate and reshape their geometric "ruler" on the fly based on the input context. The result? Better generation and classification across vision and language tasks without blowing up your memory or compute budget.

What Problem Does This Paper Address?

Imagine a detective agency where each detective (attention head) investigates a different aspect of a crime—say, timeline vs. motive—but they are strictly forbidden from talking to each other until they file their final reports. Furthermore, once they graduate from the academy (finish training), their investigative playbooks are frozen forever.

Through the lens of metric learning (the math of measuring similarity), this is how standard Multi-Head Attention works. When head $i$ computes query-key dot products, it implicitly uses a similarity matrix $M_{i,i} = W_Q^{(i)}(W_K^{(i)})^\top$. Across all heads, these form a block-diagonal matrix where all off-diagonal interactions ($M_{i,j}$ for $i \neq j$) are zeroed out.

This creates three fundamental headaches:

  1. Isolated heads: Heads cannot coordinate during similarity calculation, often causing them to converge to redundant patterns.
  2. Static geometry: Because $W_Q$ and $W_K$ are fixed post-training, the underlying metric cannot adapt to shifting contexts during inference.
  3. Low-rank bottleneck: Factoring $M_{i,i}$ through narrow head dimensions caps its mathematical rank, limiting expressive power.

Methods

The authors reframe attention as learning a single, unified Mahalanobis-like metric across all heads simultaneously (unlike a strict Mahalanobis distance, this matrix doesn't need to be positive definite, giving the network total freedom). Their mechanism, Data-Adaptive Mahalanobis Cross-Head Attention (DAMCHA), introduces three neat tricks:

  • Cross-Head Metric Matrix (CHM) & RowSum: Instead of just the diagonal blocks, DAMCHA generates the full $h \times h$ grid of metric blocks $M_{i,j}(X)$, capturing how head $i$'s subspace interacts with head $j$'s. To keep compute light and output shapes identical to standard MHA, it sums the blocks across each row ($m_i(X) = \sum_j M_{i,j}(X)$) to create one rich, full-rank-capable similarity matrix per head before applying the quadratic form $X m_i(X) X^\top$.
  • Dynamic Input-Adaptivity: Rather than learning static query/key weights, a lightweight neural network $f_\theta(X)$ (like a 9-layer MLP) reads the input tokens and generates the metric matrix $M(X)$ dynamically on the fly.
Figure 1: The workflows of DAMCHA and MHA.
  • Stack-Wise Sharing: Putting a separate hyper-network $f_\theta$ in every Transformer layer would invite massive parameter bloat and overfitting. Instead, all stacked layers share the exact same $f_\theta(X)$ network, acting as a built-in regularizer that keeps attention geometries coherent across depth.
Figure 2: A basic DAMCHA-based decoder-only Transformer architecture with stack-wise sharing.

Datasets

The authors test DAMCHA across diverse modalities and scales:

  • Autoregressive Image Generation: CIFAR-10 and CIFAR-100 (flattened into sequences of 1,024 pixel tokens).
  • Text Generation: WMT14 (English-German translation) and CommonGen (commonsense reasoning generation).
  • Language Model Pretraining (GPT-2): Enwik8 (100M Wikipedia characters), Text8 (100M cleaned Wikipedia characters), and Mix—a newly combined 200M-character 50/50 blend of Enwik8 and Text8 designed to test heterogeneous, cross-view contexts.
  • Image Classification (ViT): ImageNet-1K.
  • Synthetic Tasks: Two custom benchmarks engineered to test controlled cross-subspace entanglement.

Key Insights and Ideas

  • Per-token certainty $\neq$ holistic quality: On WMT14 translation, a competing baseline (MMA, which uses rigid attention masks) achieved a slightly better token-level Negative Log-Likelihood (NLL) than DAMCHA, yet produced much worse overall translations as measured by BARTScore (-8.39 vs. DAMCHA's -5.45). Rigid rules help predict the very next word, but flexible, context-aware head coordination is what holds a whole sentence together.
  • Shining brightest on the best samples: Across tasks, DAMCHA’s biggest wins appear in Top-50 metrics (evaluating the highest-likelihood generations) and tighter output distributions. Dynamic head coordination matters most when generating complex, high-confidence outputs.
Figure 3: (a) Distribution of BARTScore on WMT data. (b) Distribution of NLL on WMT data.
  • Mixed contexts love adaptive metrics: In GPT-2 pretraining, DAMCHA's lead grew widest on the heterogeneous Mix dataset, confirming that when data styles shift, dynamically reconfiguring attention geometry pays off.
  • Both halves of the matrix matter: Ablations zeroing out either the diagonal or off-diagonal blocks at inference caused steep performance drops, proving DAMCHA genuinely relies on cross-head synergies alongside within-head similarities.

Key Results

  • Image Generation: On CIFAR-10, DAMCHA slashed FID@Top-50 from 91.43 (MHA) to 63.03 (a 31.1% drop, and 25.8% better than the strongest attention baseline). On CIFAR-100, FID@Top-50 dropped from 59.69 to 53.76.
  • Text Generation: Improved average BARTScore on WMT14 from -8.47 (MHA) to -5.45, and achieved top marks across all CommonGen metrics.
  • GPT-2 Pretraining: Reduced validation cross-entropy loss on the Mix dataset by 5.19% over MHA and 3.70% over the strongest baseline (DCMHA).
  • Vision Transformers (ImageNet-1K): Boosted ViT-S/16 Top-1 accuracy from 79.51% (MHA) to 82.10% at epoch 500, with even larger leads early in training (epoch 100).
  • Efficiency: Thanks to stack-wise sharing, DAMCHA uses less memory and fewer GFLOPs (0.3586 vs. 0.4027–0.6417) than competing multi-head attention variants on WMT text generation while maintaining ~3.9 ms latency.

Limitations and Future Work

Not discussed in the paper. (Note: While the main text defers complexity details and mechanistic baseline comparisons to the appendices, explicit limitations and future research directions are not stated in the main body of the paper.)

Summary updated

BibTeX
@inproceedings{duan2026data,
  author = {Zhenke Duan and Xin Li and Jiqun Pan and Dong Xiaofei and Hanwen Ning and Xinyuan Song},
  title = {{Data-Adaptive Mahalanobis Metric Learning for Cross-Head Attention in Transformers}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2026},
  url = {https://openreview.net/forum?id=aK0LiJE4IQ},
}
Figure