Manuscript Offprint

arXiv:2610.08578

Random Feature Gaussian Process Attention: Linear-Time Probabilistic Attention with Calibrated Uncertainty

Amir Mohammad Mahfoozi, Zi Yang, Ying Li, Michael Minyi Zhang

Abstract

Transformers provide a state-of-the-art modeling framework, yet poor calibration limits their reliability in safety-critical applications. A promising direction addresses this issue by interpreting attention as a Gaussian process (GP) posterior, which enables principled uncertainty calibration but incurs cubic complexity in sequence length due to the inversion of the kernel; although decoupled GP variants reduced the cost to quadratic, the computation remains prohibitive in practice. In this paper, we propose the plug-and-play random Fourier feature Gaussian process attention (RFF-GPA) module, which represents the attention as a GP with a stationary kernel approximated by random Fourier features. This low-rank approximation results in linear-time complexity for approximating the posterior mean and variance, making it far more scalable compared to previous work. Empirical results on multiple real-world datasets show that our attention module improves calibration while maintaining predictive accuracy, and simultaneously reduces computational complexity to linear in the sequence length.

1 Introduction

Large language models (LLMs) such as GPT-3 and its successors (Brown et al. 2020; Zhao et al. 2023) are built upon the Transformer architecture (Vaswani et al. 2017). The core of the Transformer is the self-attention mechanism, which enables scalable modeling of long-range dependencies. Transformers have achieved breakthroughs by leveraging this design, not only in natural language processing but also in vision and multimodal learning (Vaswani et al. 2017; Dosovitskiy et al. 2021; Brown et al. 2020), thereby becoming the leading architecture in modern artificial intelligence.

Despite their apparent successes, Transformers are often overconfident in their predictions and lack principled mechanisms for uncertainty quantification, which makes them unreliable in applications where the downstream tasks are consequential (Guo et al. 2017; Minderer et al. 2021; Papamarkou et al. 2024). Recent work has bridged the standard attention output to the mean of a Gaussian process (GP) posterior (Williams and Rasmussen 2006). Specifically, Chen and Li (2023); Bui et al. (2024) describe an exact and a sparse GP formulation where the number of inducing points equals the sequence length L. From this perspective, we can extend the framework to incorporate the posterior covariance, which naturally provides a principled and well-calibrated quantification of uncertainty that is absent in vanilla Transformers.

Nevertheless, existing approaches remain computationally expensive. Typically, inference requires inversion of a L×LL \times L-dimensional kernel matrix, resulting in cubic O(L3)\mathcal{O}(L^3) time and quadratic memory complexity, which makes these methods impractical for long sequences in real-world applications. While Chen and Li (2023) introduced a decoupled GP variant to reduce the complexity from cubic to quadratic by decoupling the inducing points for mean and covariance representations, the resulting O(L2)\mathcal{O}(L^2) cost remains impractical for long sequences.

To obtain a model that combines calibrated uncertainty, strong predictive accuracy, and low computational cost, we propose random Fourier feature Gaussian process attention (RFF-GPA), a plug-and-play attention module for Transformer architectures.

Figure 1 summarizes the progression from standard attention to GP attention and illustrates how our random-feature formulation preserves posterior uncertainty while moving the expensive computation from token space to feature space. Our main contributions are summarized as follows:

• We introduce RFF-GPA, which approximates the attention kernel with random Fourier features (RFF) (Rahimi and Recht 2007) and performs GP posterior inference in the resulting feature space. Using the Woodbury matrix identity (Hager 1989), RFF-GPA avoids direct inversion of the full L×LL \times L kernel matrix and computes predictive means and covariance estimates in linear time with respect to the sequence length.

• To relax the symmetry constraint between key and query induced by stationary kernels, we further extend the model to a correlated random-feature GP attention variant, RFF-CGP. This variant models query and key representations through a shared latent GP structure, enabling a more flexible attention mechanism while retaining the computational advantages of the random-feature formulation.

• We evaluate the proposed models on image and text classification benchmarks, as well as under distribution shift on CIFAR-10-C. The results show that our methods improve calibration while maintaining competitive accuracy. They also achieve better runtime and memory scaling than inducing-point GP attention, highlighting the favorable

∗^{*}Equal contribution.

accuracy–calibration–efficiency trade-off of our approach.

References (33)

  • Lectures on Fourier Integrals, volume 42
  • Language models are few-shot learners
  • Revisiting Kernel Attention with Correlated Gaussian Process Representation
  • Calibrating Transformers via sparse Gaussian processes
  • Rethinking attention with performers
  • Deep Gaussian processes
  • An image is worth $16 \times 16$ words: Transformers for image recognition at scale
  • Bayesian attention modules
  • Dropout as a Bayesian approximation: Representing model uncertainty in deep learning
  • On calibration of modern neural networks
  • Updating the inverse of a matrix
  • Benchmarking Neural Network Robustness to Common Corruptions and Perturbations
  • SemEval-2019 Task 4: Hyperpartisan News Detection
  • Adam: A method for stochastic optimization
  • Learning multiple layers of features from tiny images
  • NewsWeeder: Learning to filter netnews
  • Preventing Model Collapse in Gaussian Process Latent Variable Models
  • Simple and principled uncertainty estimation with deterministic deep learning via distance awareness
  • Revisiting the calibration of modern neural networks
  • Bayesian Learning for Neural Networks
  • Reading digits in natural images with unsupervised feature learning
  • Position: Bayesian deep learning is needed in the age of large-scale AI. In International Conference on Machine Learning, 39556–39586
  • Random Feature Attention
  • Random features for large-scale kernel machines
  • Recursive deep models for semantic compositionality over a sentiment treebank
  • Bayesian layers: A module for neural network uncertainty
  • H.; Bai, S.; Yamada, M.; Morency, L.-P.; and Salakhutdinov, R. 2019
  • Attention is all you need
  • Gaussian Processes for Machine Learning, volume 2
  • Fashion-MNIST: a Novel Image Dataset for Benchmarking Machine Learning Algorithms
  • Bayesian transformer language models for speech recognition
  • Multi-View Oriented GPLVM: Expressiveness and Efficiency
  • A Survey of Large Language Models