Approximate Top-k for Increased Parallelism

Oscar Key, Luka Ribar, Alberto Cattaneo, Luke Hudlass-Galley, Douglas Orr

TL;DR

Exact top-$k$ selection is a major bottleneck on modern parallel accelerators because finding the global maximums requires extensive cross-thread synchronization. This paper evaluates two-stage bucketed approximate top-$k$ algorithms, demonstrating that relaxing exactness yields massive parallel scaling with virtually no downstream task degradation. In LLM sparsity applications (like SparQ attention), their custom PyTorch/CUDA kernel achieves over 4× speedups in the top-$k$ step and notable end-to-end generation speedups.

What Problem Does This Paper Address?

Top-$k$ selection identifies the $k$ largest values in an $n$-dimensional vector. It is central to $k$-nearest neighbors (KNN), token vocabulary sampling, and sparsity algorithms in Large Language Models (LLMs)—where top-$k$ dynamically selects the most critical attention heads, weights, or key-value cache tokens.

On GPUs and TPUs, exact top-$k$ algorithms (such as PyTorch’s radix- or bucket-selection routines) scale poorly over long sequence lengths $n$. Because inference batch sizes $m$ are often small, accelerators must parallelize across $n$. However, aggregating the top values requires frequent thread synchronization and shared memory bottlenecks. In sparse attention methods, top-$k$ can consume over 10% of the per-token generation runtime. Because sparsity methods are already heuristic, requiring exact top-$k$ represents unnecessary computational overhead.

Methods

The authors investigate a two-stage bucketed framework:

  1. Stage 1 (Local Selection): Partition the $n$ inputs into $b$ buckets. Independently compute an exact, small top-$k_b$ (where $k_b \le 4$) in each bucket, outputting $b \cdot k_b$ candidates.
  2. Stage 2 (Global Selection): If $b \cdot k_b > k$, execute an exact top-$k$ over the concatenated candidates to select the final $k$ values. If $b \cdot k_b = k$, Stage 2 is bypassed entirely.

Key Implementation Details:

  • Interleaved Bucketing: Real data exhibits strong sequence autocorrelation. Contiguous chunking assigns clusters of high values into the same bucket, wrecking recall. By interleaving bucket assignment ($B(i) = i \pmod b$), top values are uniformly dispersed across buckets without paying for random memory permutations.
  • Register-Level Queues: By capping $k_b \le 4$, local priority queues fit directly inside GPU per-thread registers using simple insertion sorts, eliminating thread communication during Stage 1.
  • Open-Source PyTorch Extension: Implemented in CUDA with two execution modes (1 thread per bucket or 64 threads per bucket with CUB BlockMergeSort).

Datasets

  • SQuAD v1.1 (augmented): Standard question-answering context extended with seven distractor contexts to evaluate long-context sparse LLM retrieval.
  • Text Repetition Task: Synthetic long-context recall benchmark measuring exact matching length of repeated context segments.
  • PharMeBINet: A biomedical knowledge graph benchmark containing 2,653,751 entities and 55,000 held-out test triples for link prediction (tail completion using the DistMult scoring function).
  • Synthetic Multivariate Normal Sequences: Generated with variable correlation factors $\rho \in [0, 1)$ to isolate the recall effects of data clumping.

Key Insights and Ideas

  • Different regimes require opposite strategies:
  • Large-$k$ ($k \propto n$, e.g., LLM sparsity where $k = n/8$): Stage 2 exact top-$k$ is far too expensive. The optimal policy is to eliminate Stage 2 by fixing $b \cdot k_b = k$ and increasing $k_b$ (e.g., $k_b = 2$ or $4$).
  • Small-$k$ ($k \ll n$, e.g., KNN search or vocabulary sampling): Stage 1 dominates runtime. The optimal policy is to set $k_b = 1$ and over-partition by increasing $b$ ($b \cdot k_b > k$), letting a cheap Stage 2 filter the final results.
  • $k_b = 2$ is virtually free: Because GPU execution for Stage 1 is heavily memory-bandwidth bound, maintaining a 2-element priority queue in registers takes the exact same wall-clock time as tracking a single maximum ($k_b = 1$), while dramatically boosting recall.

Key Results

  • Kernel Speed: On an NVIDIA H100 PCIe GPU (float32 and bfloat16), the bucketed kernel achieved over 4× speedup over torch.topk for large-$k$ workloads ($k = n/4$).
  • LLM Sparsity (SparQ Attention on Llama 3 8B & Llama 2 7B):
  • Top-$k$ latency dropped by $>4\times$ with $k_b = 2$ and $b \cdot k_b = k$ without any visible drop in SQuAD accuracy or repetition match score.
  • For long-context inference (40,000 prompt tokens), end-to-end generation speedup increased from 1.9× to 2.1× faster than dense attention (~10% overall token latency reduction).
  • Small-$k$ Tasks: On Llama 3 vocabulary sampling ($n = 128,256, k = 256$) and PharMeBINet link prediction ($n \approx 2.65 \times 10^6, k = 100$), setting $k_b = 2$ delivered 2× to 4× speedups over exact baselines with negligible recall error.

Limitations and Future Work

  • End-to-End Scope: End-to-end LLM decoding benchmarks were limited to SparQ Attention; other sparse-attention or activation-sparsity architectures remain unmeasured.
  • Large-$n$, Small-$k$ Tradeoff: Highly optimized exact libraries like NVIDIA RAFT outperform this approximate method when $n$ is very large and $k$ is small (though RAFT lacks bfloat16 support).
  • Batch Size Exploration: The impact of large batch dimensions ($m \gg 128$) was not thoroughly explored.
  • Future Work: Distributed multi-accelerator top-$k$, where bucketed approximations could provide even larger wins by eliminating high-latency inter-GPU communication.

Summary updated

BibTeX
@inproceedings{key2024approximate,
  author = {Oscar Key and Luka Ribar and Alberto Cattaneo and Luke Hudlass-Galley and Douglas Orr},
  title = {{Approximate Top-k for Increased Parallelism}},
  booktitle = {AFM @ NeurIPS},
  year = {2024},
  url = {https://openreview.net/forum?id=UonuElM9kV},
}
Figure