arXiv:2609.04910cs.LGSep 2026

Manuscript Offprint

arXiv:2609.04910

Fast Gauss Sums via Flash Attention

Nicolaj Rux, Sebastian Neumayer

Abstract

Gaussian kernel sums are the computational core of maximum mean discrepancies (MMDs), kernel gradient flows, Stein variational gradient descent (SVGD), and many other kernel methods. At the same time, softmax attention has received an extraordinary amount of hardware-aware code engineering, culminating in fflash attention. We show that Gauss kernel sums with arbitrary, signed weights can be evaluated via fflash attention: two small input augmentations turn the normalized softmax reduction into the unnormalized Gauss sum, without writing a single line of custom GPU code. For feature dimension D>8D > 8 in fp16, this approach beats compiled PyTorch code as well as PyKeOps kernels (often significantly) in speed, memory-overhead and accuracy. Indeed, its memory scaling remains linear.

1 Introduction

Let Φτ(q,k)=e−τ2∥q−k∥22\Phi_{\tau}(q, k) = \mathrm{e}^{-\frac{\tau}{2}\|q-k\|_2^2} denote the rescaled Gauss kernel. Sums of the form

sm:=∑n=1NΦτ(qm,kn)vn∈RC,m=1,…,M,(1)s_{m} := \sum_{n=1}^{N} \Phi_{\tau}(q_{m}, k_{n}) v_{n} \in \mathbb{R}^{C}, \qquad m = 1, \ldots, M, \tag{1}

with points k1,…,kNk_1, \ldots, k_N, q1,…,qM∈RDq_1, \ldots, q_M \in \mathbb{R}^D, values v1,…,vN∈RCv_1, \ldots, v_N \in \mathbb{R}^C and bandwidth τ>0\tau > 0, appear in essentially every Gaussian kernel method. They are the bottleneck of MMD gradient flows [Arbel et al., 2019, Hertrich et al., 2024] and of Stein variational gradient descent [Liu and Wang, 2016]. Indeed, evaluating (1) naively requires O(MN(C+D))\mathcal{O}(MN(C+D)) operations with an O(MN)\mathcal{O}(MN) memory footprint. Classical fast summation methods trade exactness for better asymptotics in restricted regimes [Greengard and Strain, 1991, Beatson and Newsam, 1992, Yang et al., 2004, Rahimi and Recht, 2007, Hertrich, 2024], while PyKeOps [Charlier et al., 2021] performs the exact reduction based on a fused, memory-efficient GPU kernel.

Meanwhile, a different community has devoted substantial code engineering effort to a single, specific reduction: softmax attention [Vaswani et al., 2017] was implemented as fflash attention [Dao et al., 2022, Dao, 2024]. In this note, we show that the Gaussian kernel sum (1) reduces to a fflash evaluation after two small augmentations, without any custom CUDA or Triton code.

Our contributions are: (i) two reductions of (1) to attention calls, one fully differentiable through the public PyTorch API and one that additionally reads the logits returned by the attention backends; (ii) an analysis of the fp16 pitfalls together with simple safeguards; and (iii) benchmarks against compiled PyTorch and PyKeOps implementations across different input shapes, including gradients.