Data-Adaptive Mahalanobis Metric Learning for Cross-Head Attention in Transformers
Organizations: School of Statistics and Mathematics, Zhongnan University of Economics and Law, Wuhan, China · CodeSoul.Co, Hangzhou, China · Innovation and Talent Base for Digital Technology and Finance, ZUEL · Department of Statistics, The Chinese University of Hong Kong, Hong Kong SAR, China
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. 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.
Figures & tables
| Image generation | MSE | FID | FID@Top-50 | Text generation | NLL | BARTScore | BARTScore@Top-50 | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Dataset | Method | mean | q25 | q75 | mean | q25 | q75 | Dataset | Method | mean | q25 | q75 | mean | q25 | q75 | ||
| CIFAR-10 | MHA | 0.1260 | 0.0750 | 0.1610 | 358.8570 | 293.0850 | 417.5810 | 91.4320 | WMT | MHA | 5.7730 | 5.1760 | 6.3750 | -8.4740 | -9.4590 | -7.3420 | -5.3090 |
| THA | 0.1357 | 0.0827 | 0.1726 | 366.7536 | 301.3989 | 421.4758 | 107.2156 | THA | 5.2368 | 4.5104 | 5.7082 | -6.7477 | -7.5275 | -5.8776 | -4.5788 | ||
| DCMHA | 0.1264 | 0.0758 | 0.1622 | 366.0802 | 297.8571 | 418.4777 | 105.1269 | DCMHA | 5.2461 | 4.5396 | 5.7308 | -6.8371 | -7.5254 | -6.0639 | -4.8286 | ||
| ColMHA | 0.1276 | 0.0752 | 0.1630 | 365.9343 | 298.6780 | 416.1373 | 120.1717 | ColMHA | 5.2432 | 4.5387 | 5.7324 | -6.1477 | -6.7462 | -5.4902 | -4.3363 | ||
| MMA | 0.1292 | 0.0771 | 0.1604 | 359.6663 | 267.3268 | 422.7725 | 84.9717 | MMA | 5.0301 | 4.5096 | 5.6847 | -8.3875 | -9.3292 | -7.3488 | -5.7903 | ||
| CIFAR-10 | CIFAR-100 | CommonGen | WMT | |||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Method | Mem. | GFLOPs | Lat.(ms) | Mem. | GFLOPs | Lat.(ms) | Mem. | GFLOPs | Lat.(ms) | Mem. | GFLOPs | Lat.(ms) |
| THA | 255.40 | 0.0797 | 3.494 | 255.40 | 0.0797 | 3.535 | 406.80 | 0.1845 | 3.611 | 1416.68 | 0.4698 | 7.299 |
| DCMHA | 292.71 | 0.0785 | 4.443 | 292.71 | 0.0785 | 4.499 | 463.36 | 0.1780 | 4.428 | 1650.30 | 0.4399 | 8.148 |
| ColMHA | 207.39 | 0.0765 | 3.576 | 207.39 | 0.0765 | 3.552 | 282.79 | 0.1699 | 3.529 | 903.74 | 0.4068 | 3.868 |
| MMA | 191.39 | 0.0755 | 16.677 | 191.39 | 0.0755 | 17.606 | 281.05 | 0.1678 | 25.863 | 923.74 | 0.4027 | 44.356 |
| MoA | 321.07 | 0.1101 | 4.078 | 321.07 | 0.1101 | 4.098 | 484.35 | 0.2538 | 4.095 | 1649.67 | 0.6417 | 6.770 |
| Setting | Dataset | FID@Top-50 / BART@Top-50 | FID / BART | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
| 1-layer | 3-layer | 5-layer | 7-layer | 9-layer | 1-layer | 3-layer | 5-layer | 7-layer | 9-layer | ||
| DAMCHA | CIFAR-10 | 77.97 | 75.79 | 71.07 | 74.07 | 58.64 | 250.35 | 231.52 | 234.37 | 229.09 | 229.84 |
| CIFAR-100 | 65.20 | 60.82 | 57.64 | 52.14 | 50.29 | 275.80 | 240.98 | 221.85 | 237.57 | 217.89 | |
| WMT | -5.26 | -5.14 | -5.08 | -4.98 | -4.51 | -7.71 | -7.27 | -6.00 | -6.32 | -5.61 | |
| CommonGen | -4.97 | -4.90 | -4.62 | -4.84 | -4.60 | -7.83 | -6.53 | -7.65 | -6.90 | -6.20 | |
| Diagonal-only | CIFAR-10 | 102.51 | 105.05 | 108.505 | 98.703 | 112.856 | 248.38 | 233.05 | 235.17 | 233.485 | 239.609 |
| Dataset | Variant | Metric | Metric@Top-50 |
|---|---|---|---|
| CIFAR-10 (FID ) | w/o +share | 233.038 -2.37% | 76.554 -17.59% |
| w/o share | 228.961 -0.58% | 77.851 -19.59% | |
| DAMCHA | 227.641 | 65.132 | |
| CIFAR-100 (FID ) | w/o +share | 229.467 -8.66% | 61.950 -15.25% |
| w/o share | 234.296 -10.95% | 57.934 -7.77% | |
| DAMCHA | 211.290 | 53.756 |
| Dataset | Cross-entropy loss | PPL | ||||
|---|---|---|---|---|---|---|
| MHA | DCMHA | DAMCHA | MHA | DCMHA | DAMCHA | |
| Enwik8 | 1.07 | 1.05 | 1.03 1.90% | 2.90 | 2.86 | 2.80 2.10% |
| Text8 | 1.06 | 1.05 | 1.03 1.90% | 2.88 | 2.86 | 2.81 1.75% |
| Mix | 1.09 | 1.08 | 1.04 3.70% | 2.98 | 2.94 | 2.82 4.08% |
| Model | Top-1 | Top-5 | ||
|---|---|---|---|---|
| epoch 100 | epoch 500 | epoch 100 | epoch 500 | |
| MHA | 69.02% | 79.51% | 88.68% | 94.68% |
| DCMHA | 67.90% | 81.70% | 88.71% | 95.00% |
| DAMCHA | 70.93% | 82.10% | 89.71% | 96.23% |
Appendix figures & tables11 assets
Supplementary material from the paper’s appendix.
Appendix
| Stage | MHA | DAMCHA |
|---|---|---|
| Q, K projections | — (replaced by ) | |
| Metric generator | — | |
| RowSum aggregation | — | |
| Attention scores | ||
| Softmax | ||
| V projection + output |
| Experiment | Dataset | Steps / Epochs | Approx. Training Time |
|---|---|---|---|
| Decoder-only Transformer | CIFAR-10 | 50 epochs | 3 h |
| CIFAR-100 | 50 epochs | 3 h | |
| WMT14 | 50 epochs | 5 h | |
| CommonGen | 50 epochs | 4 h | |
| GPT-2 Pretraining | Enwik8 | 100K steps | 8 h |
| Text8 | 100K steps | 8 h |
Multi-Head Attention
Multi-head attention uses parallel attention heads to route and combine information in Transformers, shaping their capacity, interpretability, and efficiency.
See all 74 papers