arXiv:2610.13261cs.LGOct 2026

Manuscript Offprint

arXiv:2610.13261

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

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

Open access. PaperVantage converted this article to text, so layout and figures may differ from the original.

Abstract

In this paper, we propose Data-Adaptive Mahalanobis Cross-Head Attention (DAM-CHA), 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. 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.

1 Introduction

MHA is a core block of Transformer architecture [1, 2, 3]. MHA essentially learns a set of similarity matrices induced by a bilinear form (usually denoted as Mi,i=WQ(i)(WK(i))⊤M_{i,i} = W_Q^{(i)}(W_K^{(i)})^\top) through a query-key mechanism [4, 5, 6]. These learned head-wise metrics are used to compute attention scores, which capture token matching intensities across disparate representation subspaces [2, 7, 8]. This formulation naturally aligns MHA with metric learning, where discriminative similarity structures are learned to support downstream tasks.[9, 10, 11, 12, 13].

Despite its extensive successes in diverse domains [14, 15, 16], when examined through this lens, standard MHA exhibits several inherent structural deficiencies that limit its expressive power. First, it

∗^{*}Corresponding author.

40th Conference on Neural Information Processing Systems (NeurIPS 2026).

assumes conditional independence between heads for a given input, where similarities are defined solely within each head and merely concatenated at the output. This ignores the cross-head synergies and higher-order interactions corresponding to Mi,j=WQ(i)(WK(j))⊤M_{i,j} = W_Q^{(i)}(W_K^{(j)})^\top for i≠ji \neq j, making it difficult to represent coupled geometries between complex feature subspaces [17, 18, 19]. Second, the similarity metrics Mi,iM_{i,i} remain static post-training, which implies that, during inference, the attention cannot adapt to the input data, failing to match the non-linear and locally anisotropic manifold structures prevalent in real-world data. In addition, because Mi,iM_{i,i} relies on a factorized formulation, its rank is inherently capped by the projection dimension, introducing a well-known low-rank bottleneck that limits representation capacity [20, 21, 22]. These issues are especially problematic in tasks where the geometry of the data distribution is critical [23, 24].

In this paper, we propose a paradigm shift: rather than viewing the attention as a collection of independent, dot-product, head-wise matrices, it should be understood as a single, learnable Mahalanobis-like metric defined over the joint head space (Here, the term Mahalanobis-like is used to emphasize that our formulation can fully exploit off-diagonal blocks; we do not require the resulting Mahalanobis-like metric matrix to be positive definite). In contrast, standard MHA corresponds to a restrictive block-diagonal constraint that discards all off-diagonal couplings, thereby enforcing an effectively Euclidean similarity measure. From a learning perspective, MHA learns only a global, input-independent similarity matrix that approximates an average attention geometry across the dataset. Guided by this high-level interpretation, we propose a novel attention mechanism, termed DAMCHA. DAMCHA introduces two primary innovations. Structural Expansion: It eliminates structural independence by explicitly learning a full cross-head metric. While its diagonal blocks recover conventional within-head similarities, the off-diagonal blocks encode how different heads should cooperate, compensate, or modulate one another, expanding attention from an additive mixture of isolated similarities to a genuine Mahalanobis-like metric. Dynamic Adaptivity: Unlike MHA which keeps underlying parameters static, DAMCHA parameterizes the similarity matrix by a learnable function of the input. This allows the model to dynamically select appropriate geometries for different contexts, aligning the attention operator with the local structure of the data manifold. Several prior works have sought to improve upon MHA through head interaction or geometric enrichment, including THA [25], DCMHA [19], ColMHA [26], MoA [27], and MMA [28]. However, none of these approaches is grounded in metric learning, and none jointly addresses cross-head interaction and input-adaptive metric learning within a unified framework.

Practically, DAMCHA yields benefits beyond expressiveness: (i) improved modeling of complex dependencies spanning multiple semantic factors (e.g., content–position or syntax–semantics); (ii) enhanced adaptability under distributional shifts or heterogeneous inputs, as the attention geometry reconfigures itself to match the current context; (iii) by directly learning the similarity matrices Mi,jM_{i,j}s through a functional mapping rather than the conventional linear query-key projections, DAMCHA naturally bypasses the low-rank bottleneck. We further introduce principled regularization strategies, most notably stack-wise parameter sharing, to control model complexity. Since the attention matrices in DAMCHA are generated by a compact metric learner, the overall parameter count remains comparable to standard MHA despite the expanded metric structure. This design ensures that the gains in expressiveness come at no significant cost in computational overhead or memory footprint.

Experimental results show that DAMCHA outperforms Transformers across a variety of architectures (e.g. GPT-2) and scales. In language modeling, DAMCHA is particularly superior. Notably, DAMCHA yields improvements in token-level pretraining; these gains are even stronger on mixed datasets, which are inherently cross-view. We further validate DAMCHA on autoregressive, decoder-only Transformers as well as Vision Transformers in generative and discriminative tasks. We provide extensive simulation experiments and ablation studies to elucidate the mechanisms of DAMCHA.

References (32)