You Only Sample (Almost) Once: Linear Cost Self-Attention Via Bernoulli Sampling

Zhanpeng Zeng, Yunyang Xiong, Sathya N. Ravi, Shailesh Acharya, Glenn Fung, Vikas Singh

Abstract

Transformers have revolutionized artificial intelligence but are constrained by the quadratic $O(n^2)$ complexity of the self-attention mechanism. This paper introduces YOSO-Attention ("You Only Sample Once"), a novel approach that approximates self-attention using a Bernoulli sampling scheme facilitated by Locality Sensitive Hashing (LSH). YOSO reduces computational and memory costs to $O(n)$, enabling the processing of significantly longer sequences. By utilizing GPU-friendly bucket summations and a differentiable lower-bound gradient, YOSO maintains high performance across standard benchmarks like GLUE and long-sequence tasks in the Long Range Arena (LRA).

Introduction

The self-attention mechanism is the core of the Transformer architecture, yet its $O(n^2)$ complexity presents a significant bottleneck. As sequence length $n$ increases, the memory and time required to compute the $n \times n$ similarity matrix grow quadratically, often limiting models like BERT to a 512-token context window. While various "efficient" Transformers have been proposed, they often face challenges in GPU implementation or performance degradation compared to standard softmax attention. YOSO-Attention addresses these issues by reframing self-attention as a sampling problem.

Methods

YOSO-Attention views the contribution of each token to the attention sum as a Bernoulli random variable. The probability of a token being included is determined by its similarity to the query, efficiently estimated using Locality Sensitive Hashing (LSH).

  1. Bernoulli Sampling via LSH: Instead of a full softmax, YOSO uses LSH to determine "collision" probabilities. Similar query-key pairs have a higher probability of falling into the same hash bucket.
  2. GPU-Friendly Summation: To avoid the overhead of dynamic hash tables, YOSO pre-sums the values of all keys within each hash bucket. Queries then retrieve these sums based on their own hash indices, a process highly efficient on modern hardware.
  3. Differentiable Approximation: To enable end-to-end training, the authors derived a lower-bound gradient for collision-based attention, ensuring numerical stability during backpropagation.
Comparison of attention weights and derivatives between exponential (softmax) and collision probability.

Key Insights

A critical observation is that LSH collision probability naturally mimics the mathematical properties of the softmax exponential function. As shown in the figure above, the collision probability and its derivatives align closely with the exponential function, making it a robust proxy for standard attention. Furthermore, the approximation error remains stable as sequence length increases because YOSO effectively prioritizes high-weight tokens.

Visualizing the approximation of Softmax attention by YOSO variants with varying sample sizes.

Results

YOSO demonstrates true linear scaling in time and memory. On the Long Range Arena (LRA) benchmark, YOSO-C (incorporating depthwise convolution) achieved an average accuracy of 59.87%, surpassing standard Softmax (58.77%) and other efficient variants like Performer (53.63%). On the GLUE benchmark, YOSO-64 remained within 1% of the standard BERT-base performance, proving its effectiveness even on shorter sequences.

Conclusion

YOSO-Attention provides a scalable and efficient alternative to standard self-attention by leveraging Bernoulli sampling and LSH. Its linear complexity and GPU-optimized design make it a promising candidate for long-document processing and potential applications in computer vision and point cloud modeling.

Summary updated

BibTeX
@inproceedings{zeng2021you,
  author = {Zhanpeng Zeng and Yunyang Xiong and Sathya N. Ravi and Shailesh Acharya and Glenn Fung and Vikas Singh},
  title = {{You Only Sample (Almost) Once: Linear Cost Self-Attention Via Bernoulli Sampling}},
  booktitle = {International Conference on Machine Learning},
  year = {2021},
  url = {https://openreview.net/forum?id=Cj_Emk6iRN},
}
Figure