Improved Algorithms for Kernel Matrix-Vector Multiplication Under Sparsity Assumptions

Piotr Indyk, Michael Kapralov, Kshiteej Sheth, Tal Wagner

Abstract

Kernel Matrix-Vector Multiplication (KMVM) is a fundamental operation in modern machine learning, particularly within the attention mechanisms of Transformer-based models. However, the $O(n^2)$ complexity of Gaussian KMVM presents a significant scaling bottleneck as sequence lengths increase. This paper introduces a novel subquadratic-time algorithm, APPROXKMV, which achieves a runtime of approximately $O(n^{1.89})$. Unlike previous approaches, this algorithm supports general vectors with negative entries and leverages a formalized "sparsity assumption" derived from empirical observations of Large Language Models (LLMs).

Introduction

In the architecture of Transformers, the Gaussian kernel is frequently employed to compute similarity between "queries" and "keys." For $n$ queries and $n$ keys, this involves constructing an $n \times n$ kernel matrix $K$ and computing the product $Kx$. As context windows in LLMs expand to accommodate hundreds of thousands of tokens, the quadratic scaling of this operation becomes computationally prohibitive.

While prior research attempted to mitigate this scaling issue, existing solutions often suffered from three primary limitations: they were restricted to low-dimensional spaces, required non-negative input vectors, or failed to exploit the inherent structure of real-world data. This work addresses these gaps by introducing a high-dimensional, subquadratic algorithm that functions for any vector $x \in \mathbb{R}^n$ by utilizing the natural sparsity found in neural attention patterns.

The Sparsity Assumption (Assumption A)

The efficiency of the proposed approach rests on Assumption A: the observation that the sum of all entries in the kernel matrix $K$ scales linearly with $n$, rather than quadratically. To validate this, the researchers analyzed attention matrices from pre-trained models like BERT, RoBERTa, and GPT-1 using the SQuAD dataset.

They discovered a consistent "Head-to-Tail" ratio: the energy of the matrix is concentrated in a dominant "head" (the largest $n$ entries), while the remaining "tail" remains manageable. In practice, the ratio between the tail sum and the head sum was found to be a small constant (typically between 1 and 5), confirming that real-world attention matrices are naturally sparse.

Methodology: The APPROXKMV Algorithm

The authors present Algorithm 3: APPROXKMV, which decomposes the KMVM process into three distinct phases to achieve subquadratic performance:

  1. Vector Pre-processing: The algorithm identifies extreme values within the input vector $x$. By handling these outliers separately, the remaining values are kept within a bounded range, ensuring numerical stability and reducing variance in subsequent estimation steps.
  2. Heavy Key Discovery via LSH: For any given query, only a subset of keys (the "heavy" keys) contributes significantly to the final product. The algorithm employs Locality Sensitive Hashing (LSH) to identify these high-contribution entries in subquadratic time, allowing their values to be calculated exactly.
  3. Tail Estimation via KDE: The remaining "light" keys—the long tail of the distribution—are estimated using a randomized sampling procedure. To maintain high precision, the algorithm utilizes a Kernel Density Estimation (KDE) primitive to adaptively determine the necessary sample size for each row, effectively smoothing the approximation.

Empirical Results and Discussion

The algorithm was benchmarked using GloVe word embeddings and validated against the internal representations of BERT and GPT-1.

Theoretical and Practical Speedups

The algorithm achieves a theoretical runtime of $\tilde{O}(dn^{1.89}/\epsilon^2)$, a marked improvement over the standard $O(n^2 d)$. Practical tests demonstrated that as sequence lengths grew, the ratio of exact-to-approximate runtime increased, confirming that the subquadratic scaling translates to real-world performance gains.

Breaking the Non-Negativity Constraint

A major theoretical contribution of this work is the removal of the non-negativity requirement. Previous subquadratic methods often relied on the Perron-Frobenius theorem, which is only applicable to non-negative vectors. By combining LSH-based heavy hitter discovery with KDE-based importance sampling, this paper provides the first robust subquadratic algorithm for general vectors in high-dimensional spaces.

Conclusion and Future Work

By formalizing the sparsity inherent in modern AI architectures, the authors have bypassed a long-standing computational bottleneck. The APPROXKMV algorithm offers a path toward processing significantly longer sequences without the quadratic cost traditionally associated with Gaussian kernels.

However, the algorithm's performance is intrinsically linked to the validity of the sparsity assumption. If future models or specific tasks produce dense kernel matrices where energy is distributed uniformly, the speedup may diminish. Future research will focus on integrating this algorithm directly into the training loops of Transformers to evaluate how approximation errors affect model convergence and to explore the limits of sequence length in high-capacity models.

Summary updated

BibTeX
@inproceedings{indyk2025improved,
  author = {Piotr Indyk and Michael Kapralov and Kshiteej Sheth and Tal Wagner},
  title = {{Improved Algorithms for Kernel Matrix-Vector Multiplication Under Sparsity Assumptions}},
  booktitle = {International Conference on Learning Representations},
  year = {2025},
  url = {https://openreview.net/forum?id=wLnls9LS3x},
}
Figure