Manuscript Offprint
arXiv:2609.04910Fast Gauss Sums via Flash Attention
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 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 denote the rescaled Gauss kernel. Sums of the form
with points , , values and bandwidth , 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 operations with an 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.
Preprint.
| # | Step | Work | Call |
|---|---|---|---|
| 1 | Input , , , | - | |
| 2 | Output , | - | |
| 3 | , | mult | |
| 4 | Compute , | Flash | |
| 5 | MC | mult | |
| 6 | return |
Flash attention evaluates (2) with operations and memory. The logits are computed as a by-product in fp32, but no gradient is implemented. To connect (2) with Gaussian kernel sums, we first remove the normalization by multiplying with to get
Then, we introduce to obtain
Algorithm 1 summarizes this method. Unfortunately, this implementation cannot handle gradients. Interestingly, we can proceed without using the (non-differentiable) logits by adding two additional entries to the queries and keys, see Algorithm 2. Then, it holds indeed for that
Proposition 1 (Stability). In Algorithm 2 (reweight) every satisfies
With , this gives .
Usually the weights are bounded. Thus, the critical condition is to ensure that does not underflow. The dynamic range of fp16 is , which restricts us to or .
| # | Step | Work | Call |
|---|---|---|---|
| 1 | Input , , , , | - | |
| 2 | Output , | - | |
| 3 | , | MD | pad |
| 4 | , , | ND | pad |
| 5 | , , | NC | pad |
| 6 | with and | Flash | |
| 7 | , | MC | mult |
| 8 | return |
Remark 2 (Practical implementation). Algorithm 2 is implemented entirely through the public scaled_dot_product_attention (SDPA) interface: automatic differentiation applies, and the backward pass again runs flash kernels. Algorithm 1 is algebraically leaner (no auxiliary key, channel or division) but requires the logits, which the PyTorch backends flash, cudnn and memory_efficient expose. In practice, we shift and by their common mean to avoid numerical overflow. Moreover, flash requires half precision (fp16 or bf16) and a multiple of 8 for the dimension of queries, keys and values. Flash is optimized for and only works up to . Both implementations therefore run at dimension and , respectively, where rounds up to a multiple of 8.
3 Applications
Maximum mean discrepancy. For discrete measures and , write their signed difference as , with . Then, their squared maximum mean discrepancy is
Thus, MMD amounts to a kernel sum with and , followed by the inner product .
MMD flows and Stein variational gradient descent. Particle flows require the gradient of (1) with respect to the query points, namely
which is again a single kernel sum with values . This is the regime and it covers both terms of the SVGD update [Liu and Wang, 2016, Eq. (8)], whose kernel part has the same form. Alternatively, plain autograd through Algorithm 2 applies.
4 Numerical results
Here, we benchmark different methods for computing (1), namely a naive torch.compile [Ansel et al., 2024] version (PyTorch), PyKeOps [Charlier et al., 2021] (PyKeOps) and the two proposed fflash-based variants reweight and prescale from Section 2. The inputs are independent standard Gaussian point clouds scaled by with and , which are evaluated in parallel. For reproducibility, we fix the random seed and always perform 3 warm-up runs. Then, we call each method 8 times and compute the mean and standard deviation of the elapsed time, the memory footprint (excluding input tensors), and the relative error against a fp64 reference summation. All timings were measured on an NVIDIA GeForce RTX 5090 with 32 GB using PyKeOps 2.3, PyTorch 2.13 with CUDA 13.0 and cuDNN 9.20.0. Since fflash relies on Tensor cores running in half precision, we report results for fp16. Appendix 6.2 briefly discusses the fp32 regime.

(a) Elapsed time.
(b) Memory overhead.
(c) Accuracy.

(d) Elapsed time.
(e) Memory overhead.
(f) Accuracy.
Figure 1: Sweep over computing the forward of the Gauss sum (1) for fixed in fp16 with backend fflash. The upper row is , and the lower row is , .
In Figure 1, we sweep over with fixed . As PyTorch allocates an entire matrix, this method quickly runs out of memory. For , PyKe0ps is marginally faster than reweight and prescale for large . Further, it uses consistently 3 times less memory than the flash variants. However, it has a worse error, especially for large . For , the flash variants outperform PyTorch and PyKe0ps in both speed and memory overhead, while maintaining a healthy relative accuracy. In Figure 2, we additionally include the gradient computations for the Gauss sum (1). Recall that prescale is not applicable here. The performance comparison turns out similar to the forward call (see also Figure 1 bottom row).

(a) Elapsed time.
(b) Memory overhead.
(c) Accuracy.
Figure 2: Sweep over computing the forward and backward of the Gauss sum (1) for fixed , and in fp16 with backend flash.

(a) Elapsed time.
(b) Memory overhead.
(c) Accuracy.
Figure 3: Sweep over computing the forward of the Gauss sum (1) for fixed , and in fp16 with backend flash.
In Figure 3, the performance across different dimensions is compared. PyKe0ps consistently has the worst accuracy with an error around , while all other methods lie an order of magnitude below around . Its speed degenerates with increasing dimension, while the memory scales linearly and even beats both flash variants for . Both speed and memory wise PyKe0ps is best in the low dimensional regime (). The flash variants are the fastest for . If is a power of 2 and , reweight is worse compared to prescale, because reweight needs to pad the dimension to while prescale only pads to . This effect becomes visible for in Figure 3a. For , the memory overhead of the flash variants is comparable to the one of PyKe0ps with a roughly linear incline. The accuracy of the flash variants lies around and slightly above PyTorch, which sits at .
5 Conclusion
Two small input augmentations make flash attention a fast and memory efficient drop-in method for evaluating unnormalized Gaussian kernel sums with signed weights. It excels at fp16, which often suits sampling, flows and testing, but not ill-conditioned solvers. For , the flash-based Gaussian kernel sums run 2–21 times faster than PyKe0ps forward and 3–10 times faster with gradients, at up to 7 times lower error and similar linear memory scaling. Despite the code optimization, both variants have quadratic complexity. Thus, combining the constant-factor gains shown here with subquadratic approximations [Hertrich, 2024, Rux et al., 2025] is a promising future direction.
References
J. Ansel, E. Yang, H. He, N. Gimelshein, A. Jain, M. Voznesensky, B. Bao, P. Bell, D. Berard, E. Burovski, G. Chauhan, A. Chourdia, W. Constable, A. Desmaison, Z. DeVito, E. Ellison, W. Feng, J. Gong, M. Gschwind, B. Hirsh, S. Huang, K. Kalambarkar, L. Kirsch, M. Lazos, M. Lezcano, Y. Liang, J. Liang, Y. Lu, C. K. Luk, B. Maher, Y. Pan, C. Puhrsch, M. Reso, M. Saroufim, M. Y. Siraichi, H. Suk, S. Zhang, M. Suo, P. Tillet, X. Zhao, E. Wang, K. Zhou, R. Zou, X. Wang, A. Mathews, W. Wen, G. Chanan, P. Wu, and S. Chintala. PyTorch 2: Faster machine learning through dynamic Python bytecode transformation and graph compilation. In Proceedings of the 29th ACM International Conference on Architectural Support for Programming Languages and Operating Systems, volume 2, pages 929–947, 2024. doi: 10.1145/3620665.3640366.
M. Arbel, A. Korba, A. Salim, and A. Gretton. Maximum mean discrepancy gradient flow. In Advances in Neural Information Processing Systems, volume 32. Curran Associates, Inc., 2019. URL https://proceedings.neurips.cc/paper_files/paper/2019/file/944a5ae3483ed5c1e10bbccb7942a279-Paper.pdf.
R. K. Beatson and G. N. Newsam. Fast evaluation of radial basis functions: I. Comput. Math. Appl., 24(12):7–19, 1992. doi: 10.1016/0898-1221(92)90167-G.
B. Charlier, J. Feydy, J. A. Glaunès, F.-D. Collin, and G. Durif. Kernel operations on the GPU, with autodiff, without memory overflows. J. Mach. Learn. Res., 22(74):1–6, 2021. URL https://jmlr.org/papers/v22/20-275.html.
T. Dao. FlashAttention-2: Faster attention with better parallelism and work partitioning. In International Conference on Learning Representations, pages 35549–35562, 2024. URL https://proceedings.iclr.cc/paper_files/paper/2024/file/98ed250b203d1ac6b24bbcf263e3d4a7-Paper-Conference.pdf.
T. Dao, D. Y. Fu, S. Ermon, A. Rudra, and C. Ré. FlashAttention: Fast and memory-efficient exact attention with IO-awareness. In Advances in Neural Information Processing Systems, volume 35. Curran Associates, Inc., 2022. URL https://proceedings.neurips.cc/paper_files/paper/2022/file/67d57c32e20fd0a7a302cb81d36e40d5-Paper-Conference.pdf.
L. Greengard and J. Strain. The fast Gauss transform. SIAM J. Sci. Stat. Comput., 12(1):79–94, 1991. doi: 10.1137/0912004.
J. Hertrich. Fast kernel summation in high dimensions via slicing and Fourier transforms. SIAM J. Math. Data Sci., 6(4):1109–1137, 2024. doi: 10.1137/24M1632085.
J. Hertrich, C. Wald, F. Altekrüger, and P. Hagemann. Generative sliced MMD flows with Riesz kernels. In International Conference on Learning Representations, pages 20923–20949, 2024. URL https://proceedings.iclr.cc/paper_files/paper/2024/file/5b288823575bb29654b0953a251e933b-Paper-Conference.pdf.
Q. Liu and D. Wang. Stein variational gradient descent: A general purpose Bayesian inference algorithm. In Advances in Neural Information Processing Systems, volume 29. Curran Associates, Inc., 2016. URL https://proceedings.neurips.cc/paper_files/paper/2016/file/b3ba8f1bee1238a2f37603d90b58898d-Paper.pdf.
A. Rahimi and B. Recht. Random features for large-scale kernel machines. In Advances in Neural Information Processing Systems, volume 20. Curran Associates, Inc., 2007. URL https://proceedings.neurips.cc/paper_files/paper/2007/file/013a006f03dbc5392effeb8f18fda755-Paper.pdf.
N. Rux, J. Hertrich, and S. Neumayer. Numerical methods for kernel slicing. arXiv preprint, 2025. URL https://arxiv.org/abs/2510.11478.
A. Vaswani, N. Shazeer, N. Parmar, J. Uszkoreit, L. Jones, A. N. Gomez, É. Kaiser, and I. Polosukhin. Attention is all you need. In Advances in Neural Information Processing Systems, volume 30. Curran Associates, Inc., 2017. URL https://proceedings.neurips.cc/paper_files/paper/2017/file/3f5ee243547dee91fbd053c1c4a845aa-Paper.pdf.
C. Yang, R. Duraiswami, and L. S. Davis. Efficient kernel machines using the improved fast Gauss transform. In Advances in Neural Information Processing Systems, volume 17. MIT Press, 2004. URL https://proceedings.neurips.cc/paper_files/paper/2004/file/85353d3b2f39b9c9b5ee3576578c04b7-Paper.pdf.
6 Appendix
6.1 Proof of Proposition 1
Proof. Since , the estimate follows from
The bound on follows directly as each is a convex combination of and is convex.
6.2 Single precision
For fp32, flash is no longer available as it relies on highly optimized tensor cores. Instead, the default backend for softmax attention is memory_efficient. Figure 3 and Figure 4 use the same setup (B = 64, C = 1, N = 16384) but in fp16 with flash and fp32 with memory_efficient. For , memory_efficient in fp32 is around 10 times slower than its flash-fp16 counterpart. In contrast, PyKeOps is optimized for fp32. Remarkably, its memory overhead remains tiny across dimensions. While the runtime of PyKeOps does not change much between fp16 and fp32, the softmax based variants become competitive only around . While all methods have a healthy accuracy of around , PyKeOps consistently has the lowest relative error. Thus, for fp32, PyKeOps remains the state-of-the-art implementation.

(a) Elapsed time.
(b) Memory overhead.
(c) Accuracy.
Figure 4: Sweep over computing the forward of the Gauss sum (1) for fixed , and in fp32 with backend memory_efficient.

(a) Elapsed time.
(b) Memory overhead.
(c) Accuracy.
Figure 5: Sweep over computing the forward of the Gauss sum (1) for fixed , and in fp32 with backend memory_efficient.