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

Zhenke Duan, Xin Li, Jiqun Pan, Dong Xiaofei, Hanwen Ning +1 more
10/9/2026
cs.LG
transformer-attention

Official code: erwinmsmith/DAMCHA (1 star, Python, MIT, PyTorch, last commit 2026-10-12).

Abstract

In this paper, we propose Data-Adaptive Mahalanobis Cross-Head Attention (DAMCHA), a simple and effective attention mechanism that generalizes Multi-Head Attention (MHA) through the lens of metric learning. Standard MHA implicitly learns a static, head-wise Euclidean similarity corresponding to the diagonal blocks of a unified cross-head metric matrix, discarding all off-diagonal interactions across heads.

Show full abstract

DAMCHA addresses this limitation via two key innovations. First, it learns the full cross-head metric matrix, inducing a Mahalanobis-like similarity that explicitly captures inter-head interactions beyond the restricted block-diagonal structure of MHA. Second, it parameterizes the metric matrix as an input-dependent function, enabling the attention geometry to dynamically adapt to the intrinsic manifold structure of the data rather than remaining fixed after training. We further introduce principled regularization strategies, most notably stack-wise parameter sharing, to ensure computational efficiency and stable optimization. DAMCHA serves as a drop-in replacement for MHA and its variants in various Transformer architectures, delivering superior expressive power and learning performance with comparable or reduced computational overhead. Extensive experiments demonstrate that DAMCHA-based Transformers consistently outperform competing baselines across a range of benchmark tasks. Code is available at https://github.com/erwinmsmith/DAMCHA.

View on arXivView PDF

Code Implementations(1)

erwinmsmith/DAMCHAOfficial100%
PyTorchMIT

[NeurIPS 2026] DAMCHA: Data-Adaptive Mahalanobis Metric Learning for Cross-Head Attention in Transformers.

10Python, ShellJan 27, 20261 months agoMIT
attention-mechanismcomputer-visioncross-head-attentiondeep-learningmetric-learning+3 more

Similar Papers

Cite this paper

@article{duan2026dataadaptive,
  title  = {Data-Adaptive Mahalanobis Metric Learning for Cross-Head Attention in Transformers},
  author = {Zhenke Duan and Xin Li and Jiqun Pan and Dong Xiaofei and Hanwen Ning and Xinyuan Song},
  year   = {2026},
  eprint = {2610.13261},
  archivePrefix = {arXiv},
  url    = {http://arxiv.org/abs/2610.13261v1}
}

Add a badge to your README

Shows whether official code is linked for this paper.

Code of Paper badge
[![Code of Paper](https://codeofpaper.com/badge/2610.13261.svg)](https://codeofpaper.com/paper/2610.13261)

Discussion