Fast Transformers with Clustered Attention

Apoorv Vyas, Angelos Katharopoulos, François Fleuret
7/9/2020
cs.LGstat.ML
transformer-attention

Official code: idiap/fast-transformers (1,784 stars, Python, last commit 2023-03-23).

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.

Show full abstract

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.

View on arXivView PDF

Code Implementations(1)

Pytorch library for fast transformer implementations

1,784189Python, CudaJun 15, 20203 years ago

Similar Papers

Cite this paper

@article{vyas2020fast,
  title  = {Fast Transformers with Clustered Attention},
  author = {Apoorv Vyas and Angelos Katharopoulos and François Fleuret},
  year   = {2020},
  eprint = {2007.04825},
  archivePrefix = {arXiv},
  url    = {https://arxiv.org/abs/2007.04825}
}

Add a badge to your README

Shows whether official code is linked for this paper.

Code of Paper badge
[![Code of Paper](https://codeofpaper.com/badge/2007.04825.svg)](https://codeofpaper.com/paper/2007.04825)

Discussion