Data-Aware Random Feature Kernel for Transformers

Amirhossein Farzam, Hossein Mobahi, Nolan Andrew Miller, Luke Sernau

TL;DR

This paper introduces DARKFormer, a novel attention mechanism for Transformers that addresses the quadratic complexity bottleneck by using data-aware random features. By learning a kernel geometry that aligns with the input data's anisotropy, DARKFormer effectively implements importance sampling, significantly reducing approximation errors and improving training stability, especially in resource-constrained finetuning scenarios.

What Problem Does This Paper Address?

Transformers have revolutionized many fields, but their core attention mechanism has a quadratic complexity with respect to sequence length. This makes them computationally expensive and memory-intensive for long sequences. While random feature approximations (like in Performer) offer a linear-time solution, they often suffer from high variance when dealing with anisotropic input data (common in pretrained models). This anisotropy means that isotropic random sampling wastes computational resources on less informative directions. Existing importance sampling methods to mitigate this are often too complex to implement efficiently. DARKFormer aims to bridge this gap by providing an efficient, data-aware random feature attention mechanism that achieves better accuracy with fewer samples and improved training stability.

Methods

The core innovation of DARKFormer lies in its "data-aligned kernel geometry." Instead of using isotropic random features, DARKFormer learns a covariance matrix, denoted by $\Sigma$, which parameterizes the random projection distribution. This learned $\Sigma$ effectively adapts the sampling geometry to the input data's anisotropy.

Here's a breakdown of the key methodological aspects:

  • Data-Aware Kernel: DARKFormer replaces the standard dot product in attention with a Mahalanobis inner product, $q^\top \Sigma k$. This allows the kernel to measure similarity in a geometry that accounts for input correlations and scales, effectively "whitening" the representations.
  • Importance Sampling Equivalence: The paper demonstrates that sampling random features from a distribution with covariance $\Sigma$ (i.e., $p_\Sigma(\omega) = \mathcal{N}(0, \Sigma)$) is mathematically equivalent, in expectation, to sampling from an isotropic distribution ($p_I(\omega) = \mathcal{N}(0, I_d)$) and applying importance weights. This is a crucial insight, as it means DARKFormer implicitly performs importance sampling without the need to explicitly compute or store per-sample weights, which are typically intractable.
  • Learned Covariance: The covariance matrix $\Sigma$ is learned during training. This allows the model to adapt its attention mechanism to the specific data distribution it encounters, particularly beneficial during finetuning when pretrained representations are anisotropic.
  • Positive Random Features (PRFs): DARKFormer builds upon the Positive Random Features (PRFs) framework, which provides an unbiased approximation of the softmax kernel. The learned covariance $\Sigma$ is incorporated into the PRF construction.

Datasets

The experiments were conducted using the Gemma-2B model on the C4 dataset for the next-token-prediction task. The paper does not introduce new datasets or benchmarks.

Key Insights and Ideas

  • Anisotropy is the Enemy of Isotropic Sampling: The paper strongly emphasizes that anisotropic query-key distributions in pretrained models are a major cause of high variance in standard random feature attention. Isotropic sampling is inherently inefficient in such scenarios.
  • Data Alignment = Importance Sampling: The key theoretical contribution is showing that learning a data-aligned kernel geometry (via $\Sigma$) is equivalent to performing importance sampling. This is a significant simplification, as it bypasses the computational burden of explicit importance weight calculation.
  • Finetuning is Where DARKFormer Shines: The benefits of DARKFormer are particularly pronounced during finetuning. This is because pretrained models often have anisotropic representations, and DARKFormer's data-aware approach can adapt to these without extensive retraining.
  • Improved Training Stability: Beyond accuracy, DARKFormer also leads to more stable training dynamics, especially when using larger learning rates. This is attributed to the implicit whitening effect of the Mahalanobis geometry, which can temper extreme dot-product magnitudes.

Key Results

  • Reduced Performance Gap: DARKFormer significantly narrows the performance gap between random feature attention and exact softmax attention, especially in finetuning scenarios.
  • Efficiency: It achieves this improvement without requiring large feature budgets or extensive retraining, making it suitable for resource-constrained settings.
  • Outperforms Baselines: In experiments on Gemma-2B, DARKFormer consistently outperformed a Performer-type model (isotropic PRFs) and other baselines (learned feature kernels, random, constant attention) in next-token prediction accuracy, particularly during finetuning.
  • Training Stability: DARKFormer demonstrated more stable training dynamics and fewer loss spikes compared to the Performer model across various learning rates.

Limitations and Future Work

  • Computational Cost of Learning $\Sigma$: While DARKFormer avoids per-sample importance weights, learning the covariance matrix $\Sigma$ itself adds some computational overhead. The paper suggests that this is a worthwhile trade-off for the gains in accuracy and stability.
  • Generalizability to Other Architectures/Tasks: The current experiments focus on next-token prediction with Gemma. Future work could explore DARKFormer's effectiveness in other domains like computer vision (high-resolution inputs) or for long-context modeling.
  • Combining with Other Efficiency Techniques: The authors suggest combining DARKFormer with other efficiency techniques (e.g., low-rank approximations, sketching) for even greater gains.
  • On-Device Training: Investigating DARKFormer's suitability for on-device training is also proposed as a future direction.

Summary updated

BibTeX
@inproceedings{farzam2026data,
  author = {Amirhossein Farzam and Hossein Mobahi and Nolan Andrew Miller and Luke Sernau},
  title = {{Data-Aware Random Feature Kernel for Transformers}},
  booktitle = {DeLTa @ ICLR},
  year = {2026},
  url = {https://openreview.net/forum?id=dN3My5i8sl},
}
Figure