Skip to main content
QUICK REVIEW

[Paper Review] Fast Transformers with Clustered Attention

Apoorv Vyas, Angelos Katharopoulos|arXiv (Cornell University)|Jul 9, 2020
Speech Recognition and SynthesisComputer Science28 references7 citations
TL;DR

This paper proposes clustered attention, a linear-complexity approximation of self-attention in Transformers that groups queries into clusters and computes attention only for cluster centroids, significantly reducing computational cost. The method achieves state-of-the-art performance on automatic speech recognition and can approximate a full BERT model with only 25 clusters and no performance drop on GLUE and SQuAD benchmarks.

ABSTRACT

Transformers have been proven a successful model for a variety of tasks in sequence modeling. However, computing the attention matrix, which is their key component, has quadratic complexity with respect to the sequence length, thus making them prohibitively expensive for large sequences. To address this, we propose clustered attention, which instead of computing the attention for every query, groups queries into clusters and computes attention just for the centroids. To further improve this approximation, we use the computed clusters to identify the keys with the highest attention per query and compute the exact key/query dot products. This results in a model with linear complexity with respect to the sequence length for a fixed number of clusters. We evaluate our approach on two automatic speech recognition datasets and show that our model consistently outperforms vanilla transformers for a given computational budget. Finally, we demonstrate that our model can approximate arbitrarily complex attention distributions with a minimal number of clusters by approximating a pretrained BERT model on GLUE and SQuAD benchmarks with only 25 clusters and no loss in performance.

Motivation & Objective

  • To address the quadratic computational complexity of self-attention in Transformers, which limits their use on long sequences.
  • To develop a method that maintains high model performance while drastically reducing inference and training time.
  • To enable efficient approximation of pre-trained models like BERT with minimal accuracy degradation.
  • To demonstrate that clustered attention can handle complex, sparse attention patterns found in real-world NLP tasks.
  • To show that the method scales efficiently, achieving up to 50% reduction in training time and CO2 emissions.

Proposed method

  • Queries are grouped into clusters using K-Means and locality-sensitive hashing to reduce the number of attention computations.
  • Attention is computed only once per cluster centroid, drastically reducing the number of dot product operations.
  • For improved accuracy, the method identifies and computes exact dot products with the keys that have the highest attention scores per query-cluster.
  • The approach maintains linear complexity with respect to sequence length for a fixed number of clusters.
  • The method is applied both during training and inference, and can be used to distill full attention mechanisms from pre-trained models.
  • Theoretical bounds are derived to analyze the approximation error with respect to full self-attention.

Experimental results

Research questions

  • RQ1Can a clustered attention mechanism achieve linear complexity while preserving performance in sequence modeling tasks?
  • RQ2How well can clustered attention approximate complex attention distributions found in pre-trained BERT models?
  • RQ3Does the method maintain performance under equal computational budgets compared to standard Transformers?
  • RQ4Can the method reduce training time and energy consumption without significant accuracy loss?
  • RQ5What is the minimal number of clusters required to achieve near-perfect approximation of full attention in downstream tasks?

Key findings

  • On the Switchboard ASR dataset, i-clustered attention achieved over 2 percentage points lower WER than full attention at a computational budget of ~50 seconds per forward pass.
  • With 12 layers, i-clustered reduced training time per epoch by 48% (1.91h vs. 3.84h) and total convergence time by 44% (132.13h vs. 228.05h).
  • On GLUE and SQuAD benchmarks, i-clustered with only 25 clusters matched full BERT performance on all tasks except SQuAD, where it was marginally worse (F1: 0.876 vs. 0.904).
  • Clustered attention with 25 clusters achieved near-zero performance drop on GLUE tasks, with only a minor drop on SQuAD and RTE, which require complex attention patterns.
  • The method enabled a 50% reduction in GPU training time for long sequences, directly translating to reduced CO2 emissions and energy use.
  • Theoretical analysis confirms that the approximation error is bounded and can be minimized by selecting high-attention keys per cluster.

Read this paper

Open the full text in the Nubint reader and ask about any passage as you read.

Read this paper on Nubint

This review was generated by AI from the original paper. Please check the paper before citing it.