Sparse Attention as Compact Kernel Regression
Organizations: Instituto Superior Técnico & Instituto de Telecomunicações, Universidade de Lisboa, Portugal · Carnegie Mellon University, Pittsburgh, USA · Champalimaud Research, Lisbon, Portugal · TransPerfect, Lisbon, Portugal
Abstract
Recent work has revealed a link between self-attention mechanisms in transformers and test-time kernel regression via the Nadaraya-Watson estimator, with standard softmax attention corresponding to a Gaussian kernel. However, a kernel-theoretic understanding of sparse attention mechanisms is currently missing. In this paper, we establish a formal correspondence between sparse attention and compact (bounded support) kernels. We show that normalized ReLU and sparsemax attention arise from Epanechnikov kernel regression under fixed and adaptive normalizations, respectively. More generally, we demonstrate that widely used kernels in nonparametric density estimation -- including Epanechnikov, biweight, and triweight -- correspond to -entmax attention with for , while the softmax/Gaussian relationship emerges in the limit . This unified perspective explains how sparsity naturally emerges from kernel design and provides principled alternatives to heuristic top- attention and other associative memory mechanisms. Experiments with a kernel-regression-based variant of transformers -- Memory Mosaics -- show that kernel-based sparse attention achieves competitive performance on language modeling, in-context learning, and length generalization tasks, offering a principled framework for designing attention mechanisms.
Figures & tables
| Method | 1k | 2.5k | 5k | 10k | 20k | 40k | ||||||
| Acc | TVD | Acc | TVD | Acc | TVD | Acc | TVD | Acc | TVD | Acc | TVD | |
| RetNet | 51.3 | 67.4 | 50.5 | 63.9 | 81.5 | 33.3 | 88.4 | 25.7 | 90.8 | 22.5 | 92.6 | 18.9 |
| H3 | 51.5 | 65.1 | 58.0 | 57.0 | 70.9 | 47.4 | 76.9 | 41.4 | 80.7 | 36.3 | 87.7 | 25.4 |
| Gla | 51.8 | 65.3 | 51.1 | 62.2 | 82.1 | 31.0 | 90.2 | 24.5 | 91.2 | 21.2 | 92.9 | 19.0 |
| S4 | 52.1 | 62.8 | 51.7 | 62.2 | 54.5 | 59.0 | 63.3 | 52.9 | 72.1 | 45.8 | 82.5 | 33.8 |
| Mamba | 69.4 | 51.0 | 79.4 | 41.0 | 88.7 | 30.6 | 90.5 | 28.4 | 91.3 | 27.0 | 91.5 | 27.0 |
| MQMTAR | Reverse | Sort | ||||||||||
| Kernel | ||||||||||||
| Transformer | 1.00 | 1.00 | 0.99 | 0.09 | 0.01 (0.01) | 0.00 (0.00) | 0.00 (0.00) | 1.00 | 0.52 (0.62) | 1.00 | 0.00 (0.00) | 0.00 (0.00) |
| Gaussian | 0.99 | 0.96 | 0.93 (0.99) | 0.85 (0.97) | 0.64 (0.81) | 0.14 (0.20) | 0.00 (0.00) | 1.00 | 0.14 (0.38) | 1.00 | 0.06 (0.17) | 0.00 (0.00) |
| Gaussian kNN, | 0.99 | 0.97 | 0.93 (0.99) | 0.84 (0.96) | 0.68 (0.85) | 0.27 (0.36) | 0.05 (0.05) | 0.99 | 0.00 (0.00) | 1.00 | 0.80 (0.91) | 0.01 (0.02) |
| Uniform kNN, | 0.04 (0.08) | 0.00 (0.00) | 0.00 (0.00) | 0.00 (0.00) | 0.00 (0.00) | 0.00 (0.00) | 0.00 (0.00) | 0.42 (0.66) | 0.00 (0.00) | 1.00 | 0.00 (0.00) | 0.00 (0.00) |
| Epanechnikov (normReLU) | 1.00 | 1.00 | 0.89 | 0.66 | 0.44 | 0.33 | 0.31 | 1.00 | 0.19 (0.56) | 1.00 | 0.05 (0.14) | 0.00 (0.00) |
Appendix figures & tables9 assets
Supplementary material from the paper’s appendix.
Appendix
| Hyperparameter | 1 layer | 8 layers | 12 layers |
| Optimization | |||
| Batch size (total) | 512 | 512 | 512 |
| Learning rate | ∗ | ||
| Min learning rate | ∗ | ||
| Max iterations | 80k | 80k | 80k |
| Weight decay | 0.1 | 0.1 | 0.1 |
| Dataset partition | # Stories | # Tokens (GPT-2 tokenizer) | Avg. # Characters per story |
| Train | 2.2M | 474,704,907 | 888 |
| Valid | 2.2k | 4,749,107 | 889 |
| Parameter | Range |
| weight decay | { , } |
| depth | {2, 4, 8} |
| embedding size | {32, 128, 256} |
| number of heads | {1, 2, 4, 8} |
| learning rate | { } |
| maximum number of epochs | {200} |
| Parameter | MQMTAR | Reverse | Sorting |
| Training examples | |||
| # epochs | 1 | 1 | 1 |
| Vocab size | 263 | 36 | 36 |
| Weight decay | |||
| Depth | 4 | 4 | 2 |
| Embedding size | 512 | 512 | 512 |
| MQMTAR | Reverse | Sort | ||||||||||
| Kernel | ||||||||||||
| Gaussian kNN, | 0.99 | 0.97 | 0.93 | 0.85 | 0.65 (0.78) | 0.25 | 0.05 | 0.86 | 0.00 | 1.00 | 0.74 | 0.00 (0.00) |
| Gaussian kNN, | 0.99 | 0.97 | 0.93 | 0.84 | 0.68 | 0.27 | 0.05 (0.05) | 0.99 | 0.00 | 1.00 | 0.80 | 0.01 |
| Uniform kNN, | 0.58 | 0.01 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 1.00 | 0.41 | 0.00 |
| Uniform kNN, | 0.04 (0.08) | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.42 | 0.00 | 1.00 | 0.00 | 0.00 |
| MQMTAR | |||||||
| Model | |||||||
| Transformer (ALiBi) | 1.00 | 1.00 | 0.99 (0.99) | 0.09 (0.15) | 0.01 (0.01) | 0.00 (0.00) | 0.00 (0.00) |
| MM, Epanechnikov (ALiBi) | 0.99 | 0.96 | 0.92 | 0.85 | 0.72 (0.94) | 0.43 (0.71) | 0.14 (0.29) |
| MM, Epanechnikov (no ALiBi) | 1.00 | 1.00 | 1.00 | 0.98 | 0.86 | 0.63 | 0.31 |
Transformer Attention
Transformer attention lets models relate tokens by weighting information from other positions, enabling context-dependent sequence and multimodal processing.
See all 320 papersProjection-Free Transformers via Gaussian Kernel Attention
nanochat framework, we implement causal masking and sliding-window constraints by masking and renormalizing the Gaussian kernel. At depth 20, a GKA model with the parameters and the total training FLOPs of a standard attention baseline trains stably, exhibits a near-zero train-validation gap, and demonstrates competitive behavior on standard benchmarks, albeit with higher bits-per-byte (BPB) at this compute scale. Overall, GKA provides a minimal, interpretable attention mechanism with an explicit locality scale, offering a dimension in the accuracy-efficiency trade-off for Transformer design.