{'fraction_non_alphanumeric': 0.028992594662568115, 'fraction_numerical': 0.015392855479483955, 'mean_word_length': 4.615666274355957, 'pattern_counts': {'":': 0, '<': 0, '<?xml version=': 0, '>': 0, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
Fast Transformers with Clustered Attention

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.


Introduction
Sequence modelling is a fundamental task of machine learning, integral in a variety of applications such as neural machine translation [2], image captioning [29], summarization [17], automatic speech recognition [8] and synthesis [18] etc. Transformers [27] have been proven a powerful tool significantly advancing the state-of-the-art for the majority of the aforementioned tasks. In particular, transformers employ self-attention that allows them to handle long sequences without the vanishing-gradient problem inherent in RNNs [12,1].
Nonetheless, despite their impressive performance, the use of self-attention comes with computational and memory requirements that scale quadratic to the sequence length, limiting their applicability to long sequences. The quadratic complexity becomes apparent if we consider the core mechanism of self-attention, namely splitting the input sequence into queries and keys and then each query attending to all keys. To this end, recently, there has been an increasing interest for developing methods that address this limitation [7,26,5,13].
These methods can be broadly categorized into two distinct lines of work, those that focus on improving the asymptotic complexity of the self-attention computation [5,13] and those that aim at developing techniques that make transformers applicable to longer sequences without addressing the quadratic complexity of self-attention [7,26]. The former limits the amount of keys that each query attends to, thus reducing the asymptotic complexity. The latter increases the length of the sequence that a transformer can attend to without altering the underlying complexity of the self-attention mechanism.
In this work, we propose clustered attention which is a fast approximation of self-attention. Clustered attention makes use of similarities between queries and groups them in order to reduce the computational cost. In particular, we perform fast clustering using locality-sensitive hashing and K-Means and only compute the attention once per cluster. This results in linear complexity for a fixed number of clusters ( § 3.2). In addition, we showcase that we can further improve the quality of our approximation by separately considering the keys with the highest attention per cluster ( § A.2). Finally, we provide theoretical bounds of our approximation quality with respect to the full attention ( § 3.2.1, § B) and show that our model can be applied for inference of pre-trained transformers with minimal loss in performance. We evaluate our model on two automatic speech recognition datasets and showcase that clustered attention consistently achieves better performance than vanilla attention when the computational budget is equalized. Moreover, we demonstrate that our proposed attention can approximate a pretrained BERT model on the popular GLUE and SQuAD benchmarks with only 25 clusters and without loss in performance.

Related Work
In this section, we discuss the most relevant works on scaling transformers to larger sequences. We start by presenting approaches that aim to speed up the attention computation in general. Subsequently, we discuss approaches that speed up transformers without changing the complexity of the attention layer and finally, we summarize the most related works on improving the asymptotic complexity of the attention layer in transformer models.

Attention Improvements Before Transformers
Attention has been an integral component of neural networks for sequence modelling for several years [2,29,4]. However, its quadratic complexity with respect to the sequence length hinders its applicability on large sequences.
Among the first attempts to address this was the work of Britz et al. [3] that propose to aggregate the information of the input sequence into fewer vectors and perform attention with these fewer vectors, thus speeding up the attention computation and reducing the memory requirements. However, the input aggregation is performed using a learned but fixed matrix that remains constant for all sequences, hence significantly limiting the expressivity of the model. Similarly, Chiu & Raffel [6] limit the amount of accessible elements to the attention, by attending monotonically from the past to the future. Namely, if timestep i attends to position j then timestep i + 1 cannot attend to any of the earlier positions. Note that in order to speed up the attention computation, the above methods are limiting the number of elements that each layer attends to. Recently, some of these approaches have also been applied in the context of transformers [16].

Non-asymptotic Improvements
In this section, we summarize techniques that seek to apply transformers to long sequences without focusing on improving the quadratic complexity of self-attention. The most important are Adaptive Attention Span Transformers [26] and Transformer-XL [7].
Sukhbaatar et al. [26] propose to limit the self-attention context to the closest samples (attention span), in terms of relative distance with respect to the time step, thus reducing both the time and memory requirements of self-attention computation. This is achieved using a masking function with learnable parameters that allows the network to increase the attention span if necessary. Transformer-XL [7], on the other hand, seeks to increase the effective sequence length by introducing segment-level recurrent training, namely splitting the input into segments and attending jointly to the previous and the current segment. The above, combined with a new relative positional encoding results in models that attend to more distant positions than the length of the segment used during training.
Although both approaches have been proven effective, the underlying limitations of self-attention still remains. Attending to an element that is N timesteps away requires O N 2 memory and computation. In contrast, our model trades-off a small error in the computation of the full attention for an improved linear asymptotic complexity. This makes processing long sequences possible.

Improvements in Asymptotic Complexity
Child et al. [5] factorize the self-attention mechanism in local and strided attention. The local attention is computed between the C nearest positions and the strided attention is computed between positions that are C steps away from each other. When C is set to √ N the total asymptotic complexity becomes O N √ N both in terms of memory and computation time. With the aforementioned factorization, in order for any position to attend to any other position, two self-attention layers are required. As a result Child et al. [5] propose to double the number of layers in a given model. In addition, the factorization is fixed and data independent. This makes it intuitive for certain signals (e.g. images), however in most cases it is arbitrary. In contrast, our method automatically groups the input queries that are similar without the need for a manually designed factorization. Moreover, in our model, information flows always from every position to every other position.
Recently, Kitaev et al. [13] introduced Reformer. A method that groups positions based on their similarity using locality-sensitive hashing (LSH) and only computes the attention within groups. For groups of fixed size, the asymptotic complexity of Reformer becomes linear with respect to the sequence length. Note that Reformer constrains the queries and keys of self-attention to be equal. As a result, it cannot be applied to neural machine translation, image captioning or memory networks, or generally any application with heterogenous queries and keys. In addition, as it uses hash collisions to form groups it can only handle a small number of bits, thus significantly reducing the quality of the grouping. Instead, our method uses clustering to group the queries, resulting in significantly better groups compared to hash collisions. We show that our method has the same asymptotic complexity, while it can also be used to speed up inference of pretrained models without additional training.

Scaling Attention with Fast Clustering
In this section, we formalize the proposed method for approximate softmax attention. In § 3.1, we first discuss the attention mechanism in vanilla transformers and present its computational complexity. We then introduce clustered attention in § 3.2 and show that for queries close in the Euclidean space, the attention difference can be bounded by the distance between the queries. This property allows us to reduce the computational complexity by clustering the queries. Subsequently, in § A.2 we show that we can further improve the approximation by first extracting the top-k keys with the highest attention per cluster and then computing the attention on these keys separately for each query that belongs to the cluster. A graphical illustration of our method is provided in the supplementary material.

Vanilla Attention
For any sequnce of length N , the standard attention mechanism that is used in transformers is the dot product attention introduced by Vaswani et al. [27]. Following standard notation, we define the attention matrix A ∈ R N ×N as, where Q ∈ R N ×D k denotes the queries and K ∈ R N ×D k denotes the keys. Note that softmax (·) is applied row-wise. Using the attention weights A and the values V ∈ R N ×Dv , we compute the new valuesV as follows,V = AV.
(2) An intuitive understanding of the attention, as described above, is that given Q, K, V we create new valuesV as the weighted average of the old ones, where the weights are defined by the attention matrix

Clustered Attention
Instead of computing the attention matrix for all queries, we group them into C clusters and compute the attention only for these clusters. Then, we use the same attention weights for queries that belong to the same cluster. As a result, the attention computation now becomes O (N CD k ), where C N .
More formally, let us define S ∈ {0, 1} N ×C , a partitioning of the queries Q into C non-overlapping clusters, such that, S ij = 1, if the i-th query Q i belongs to the j-th cluster and 0 otherwise. Using this partitioning, we can now compute the clustered attention. First, we compute the cluster centroids as follows, where Q c j is the centroid of the j-th cluster. Let us denote Q c ∈ R C×D k as the centroid matrix. Now, we can compute the clustered attention as if Q c were the queries. Namely, we compute the clustered and the new valuesV c ∈ R C×DvV c = A c V.
(5) Finally, the value of the i-th query becomes the value of its closest centroid, namely, From the above analysis, it is evident that we only need to compute the attention weights and the weighted average of the values once per cluster. Then, we can broadcast the same value to all queries belonging to the same cluster. This allows us to reduce the number of dot products from N for each query to C for each cluster, which results in an asymptotic complexity of O (N CD k ) + O (CN D v ).

Quality of the approximation
From the above, we show that grouping queries into clusters can speed-up the self-attention computation. However, in the previous analysis, we do not consider the effects of clustering on the attention weights A. To address this, we derive a bound for the approximation error. In particular, we show that the difference in attention can be bounded as a function of the Euclidean distance between the queries. Proposition 1. Given two queries Q i and Q j such that where K 2 denotes the spectral norm of K.
Proof. Given that softmax (·) has Lipschitz constant less than 1 [9], Proposition 1 shows that queries that are close in Euclidean space have similar attention distributions. As a result, the error in the attention approximation for the i-th query assigned to the j-th cluster can be bounded by its distance from the cluster centroid Q c j .

Grouping the Queries
From the discussion, we have shown that given a representative set of queries, we can approximate the attention with fewer computations. Thus, now the problem becomes finding this representative set of queries. K-Means clustering minimizes the sum of squared distances between the cluster members, which would be optimal given our analysis from § 3.2.1. However, for a sequence of length N one iteration of Lloyd's algorithm for the K-Means optimization problem has an asymptotic complexity O (N CD k ). To speed up the distance computations, we propose to use Locality-Sensitive Hashing (LSH) on the queries and then K-Means in Hamming space. In particular, we use the sign of random projections [25] to hash the queries followed by K-Means clustering with hamming distance as the metric. This results in an asymptotic complexity of O (N CL + CBL + N D k B), where L is the number of Lloyd iterations and B is the number of bits used for hashing.

Improving clustered attention
In the previous section, we show that clustered attention provides a fast approximation for softmax attention. In this section, we discuss how this approximation can be further improved by considering separately the keys with the highest attention. To intuitively understand the importance of the above, it suffices to consider a scenario where a key with low attention for some query gets a high attention as approximated with the cluster centroid. This can happen when the number of clusters are too low or due to the convergence failure of K-Means. For the clustered attention, described in § 3.2, this introduces significant error in the computed value. The variation discussed below addresses such limitations.
After having computed the clustered attention A c from equation 4, we find the k keys with the highest attention for each cluster. The main idea then is to improve the attention approximation on these top-k keys for each query that belongs to the cluster. To do so, we first compute the dot product attention as defined in equation 1 on these top-k keys for all queries belonging to this cluster. For any query, the computed attention on these top-k keys will sum up to one. This means that it cannot be directly used to substitute the clustered-attention on these keys. To address this, before substition, we scale the computed attention by the total probability mass assigned by the clustered attention to these top-k keys.
More formally, we start by introducing T ∈ {0, 1} C×N , where T ji = 1 if the i-th key is among the top-k keys for the j-th cluster and 0 otherwise. We can then compute the probability mass, let it bê m j , of the top-k keys for the j-th cluster, as followŝ Now we formulate an improved attention matrix approximation A t ∈ R N ×N as follows Note that in the above, i denotes the i-th query belonging to the j-th cluster and √ D k is ommited for clarity. In particular, equation 19 selects the clustered attention of equation 4 for keys that are not among the top-k keys for a given cluster. For the rest, it redistributes the massm j according to the dot product attention of the queries with the top-k keys. The corresponding new values,V ∈ R N ×Dv , are a simple matrix product of A t with the values, (11) Equation 11 can be decomposed into clustered attention computation and two sparse dot products, one for every query with the top-k keys and one for the top-k attention weights with the corresponding values. This adds O (N k max (D k , D v )) to the asymptotic complexity of the attention approximation of equation 4.

Quality of the approximation
In the following, we provide proof that improved clustered attention (eq. 19) is a direct improvement over the clustered attention (eq. 4), in terms of the L 1 distance from the attention matrix A. Proposition 2. For the i-th query belonging to the j-th cluster, the improved clustered attention A t i and clustered attention A c j relate to the full attention A i as follows, Due to lack of space, the proof of the above proposition is presented in the supplementary material. From equation 12 it becomes evident that improved clustered attention will always approximate the full attention better compared to clustered attention.

Experiments
In this section, we analyze experimentally the performance of our proposed method. Initially, we show that our model outperforms our baselines for a given computational budget on a real-world sequence to sequence task, namely automatic speech recognition on two datasets, the Wall Street Journal dataset ( § 4.1) and the Switchboard dataset ( § 4.2). Subsequently, in § 4.3, we demonstrate that our model can approximate a pretrained BERT model [15] on the GLUE [28] and SQuAD [24] benchmarks with minimal loss in performance even when the number of clusters is less than one tenth of the sequence length. Due to lack of space, we also provide, in the supplementary material, a thorough benchmark that showcases the linear complexity of clustered attention and an ablation study regarding how the number of clusters scales with respect to the sequence length.
We compare our model with the vanilla transformers [27], which we refer to as full and the Reformer [13], which we refer to as lsh-X, where X denotes the rounds of hashing. We refer to clustered attention, introduced in § 3.2, as clustered-X and to improved clustered attention, introduced in § A.2, as i-clustered-X, where X denotes the number of clusters. Unless mentioned otherwise we use k = 32 for the top-k keys with improved clustered.
All experiments are conducted using NVidia GTX 1080 Ti with 11GB of memory and all models are implemented in PyTorch [19]. For Reformer we use a PyTorch port of the published code. Note that we do not use reversible layers since it is a technique that could be applied to all methods. Our PyTorch code can be found at https://clustered-transformers.github.io.

Evaluation on Wall Street Journal (WSJ)
In our first experiment, we employ the Wall-Street Journal dataset [20]. The input to all transformers is 40-dimensional filter-bank features with fixed positional embeddings. We train using Connectionist Temporal Classification (CTC) [11] loss with phonemes as ground-truth labels. The approximate average and maximum sequence lengths for the training inputs are 780 and 2500 respectively.
Speed Accuracy Trade-off: We start by comparing the performance of our proposed model with various transformer variants under an equalized computational budget. To this end, we train full with 4, 6 and 9 layers to get a range of the required computation time and achieved phone error rate (PER). Similarly, we train i-clustered with 6 and 9 layers. Both models are trained with 100 and 200 clusters. We also train clustered with 9 layers, and 100, 200 and 300 clusters. Finally, we train Reformer with 9 layers, and 1 and 4 hashing rounds. We refer the reader to our supplementary for the specifics of all transformer architectures as well as their training details. In figure 1a, we plot the achieved PER on the validation set with respect to the required time to perform a full forward pass. Our i-clustered achieves lower PER than all other baselines for a given computational budget.
Approximation Quality: To assess the approximation capabilities of our method, we train different transformer variants on the aforementioned task and evaluate them using other self-attention implementations during inference. As the Reformer requires the queries to be identical to the keys to evaluate its approximation ability we also train a full attention model with shared queries and keys, which we refer to as shared-full. Note that both clustered attention and improved clustered attention can be used for approximating shared-full, simply by setting keys to be equal to queries. Table 1 summarizes the results. We observe that improved clustered attention (7-8 rows) achieves the lowest phone error rate in every comparison. This implies that it is the best choice for approximating pre-trained models. In addition, we also note that as the number of clusters increases, the approximation improves as well. Furthermore, to show that the top keys alone are not sufficient for approximating full, we also compare with an attention variant, that for each query only keeps the 32 keys with the highest attention. We refer to the latter as oracle-top. We observe that oracle-top achieves significantly larger phone error rate than improved clustered in all cases. This implies that improved clustered attention also captures the significant long tail of the attention distribution.
Convergence Behaviour: In Table 2, we report the required time per epoch as well as the total training time for all transformer variants with 9 layers. For completeness, we also provide the corresponding phone error rates on the test set. We observe that clustered attention is more than two times faster than full (per epoch) and achieves significantly lower PER than both Reformer variants (lsh-1 and lsh-4). Improved clustered is the only method that is not only faster per epoch but also in total wall-clock time required to converge.

Evaluation on Switchboard
We also evaluate our model on the Switchboard dataset [10], which is a collection of 2, 400 telephone conversations on common topics among 543 strangers. All transformers are trained with latticefree MMI loss [23] and as inputs we use 80-dimensional filter-bank features with fixed positional embeddings. The average input sequence length is roughly 534 and the maximum sequence length is approximately 3850. Details regarding the transformer architectures as well as their training details are provided in the supplementary.
Speed Accuracy Trade-off: Similar to § 4.1, we compare the performance of various transformer models given a specific computational budget. To this end, we train full with 6, 8 and 12 layers.
Similarly, we train i-clustered with 8 and 12 layers; both with 100 and 200 clusters. Finally, we also train clustered with 12 layers, and 100, 200 and 300 clusters. In figure 1b, we plot the achieved word error rate (WER) in the validation set of Switchboard with respect to the required time to perform a full forward pass. Our i-clustered is consistently better than full for a given computational budget.
In particular, for a budget of approximately 50 seconds, improved clustered achieves more than 2 percentage points lower WER. Furthermore, we note that it is consistently better than clustered attention for all computational budgets.

RoBERTa Approximation
To highlight the ability of our model to approximate arbitrarily complicated attention distributions, we evaluate our proposed method on the approximation of a fine-tuned RoBERTa model [15] on the GLUE [28] and SQuAD [24] benchmarks. In particular, we evaluate on 10 different tasks, among which there are tasks such as question answering (SQuAD) and textual entailment (RTE), which exhibit arbitrary and sparse attention patterns. We refer the reader to Wang et al. [28], Rajpurkar et al. [24] for a detailed analysis of all tasks.
For the GLUE tasks, the maximum sequence length is 128 while for SQuAD, it is 384. For each task, we use 25 clusters for approximation which is less than 20% and 10% of the input sequence length for GLUE and SQuAD tasks respectively. In Table 4, we summarize the performance per task. We observe that improved clustered performs as well as the full transformer in all tasks but SQuAD, in which it is only marginally worse. Moreover, we note that clustered performs significantly worse in tasks that require more complicated attention patterns such as SQuAD and RTE.  Table 4: We report the performance on GLUE and SQuAD benchmarks. Following common practice, we report accuracy for all tasks except STS-B and SQuAD, where we report Pearson correlation and F1-score respectively. For all metrics higher is better.

Conclusions
We have presented clustered attention a method that approximates vanilla transformers with significantly lower computational requirements. In particular, we have shown that our model can be up to 2× faster during training and inference with minimal loss in performance. In contrast to recent fast variations of transformers, we have also shown that our method can efficiently approximate pre-trained models with full attention while retaining the linear asymptotic complexity.
The proposed method opens several research directions towards applying transformers on long sequence tasks such as music generation, scene flow estimation etc. We consider masked language modeling for long texts to be of particular importance, as it will allow finetuning for downstream tasks that need a context longer than the commonly used 512 tokens.

Broader Impact
This work contributes towards the wider adoption of transformers by reducing their computational requirements; thus enabling their use on embedded or otherwise resource constrained devices. In addition, we have shown that for long sequences clustered attention can result to almost 50% reduction in GPU training time which translates to equal reduction in CO2 emmisions and energy consumption.

Supplementary Material for Fast Transformers with Clustered Attention A Scaling Attention with Fast Clustering
In this section we present graphical illustrations for the proposed clustered and i-clustered attention models in § A.1 and § A.2 respectively.

A.1 Clustered attention
In figure 2, we present the steps involved in clustered attention computation for an example sequence with 8 queries and the number of clusters set to 3. We first cluster the queries Q using the K-means clustering to output S which indicates the membership of queries to different clusters. We use different colors to represent different clusters. After clustering, the centroids Q c are used to compute the attention weights A c and the new values V c for the centroids. Finally, the values are broadcasted to get the new valuesV corresponding to each query.

A.2 Improved clustered attention
In this section, we first describe how we can efficiently compute the i-clustered attention using sparse dot products with the top-k keys and values. We then present the flow chart demonstrating the same.
As discussed in the § 3.3 of the main paper, the improved attention matrix approximation A t i for the query, Q i belonging to the cluster j is computed as follows: where, T ∈ {0, 1} C×N , stores the top-k keys for each cluster. T ji = 1 if the i-th key is among the top-k keys for the j-th cluster and 0 otherwise.
As described in the main paper,m j is the total probability mass on the top-k keys for the j-th cluster given by:m Note that we can compute the attention weights A t i on the top-k keys by first taking sparse dot-product of Q i with the top-k keys followed by the softmax activation and rescaling with total probablity mass m j . For the rest of the keys, the attention weight is the clustered-attention weight A c i . Similarly, the new valuesV i can be decomposed into the following two terms,  B Quality of the approximation Proposition 3. For the i-th query belonging to the j-th cluster, the improved clustered attention A t i and clustered attention A c j relate to the full attention A i as follows, Proof. As discussed before, the improved attention matrix approximation A t i for the query, Q i is computed as follows: where, T ∈ {0, 1} C×N , stores the top-k keys for each cluster, T ji = 1 if the i-th key is among the top-k keys for the j-th cluster and 0 otherwise.m j is the total probability mass on the top-k keys for the j-th cluster, computed as follows:m Given the full attention A i , equation 19 can be simplified to where, m i is the total probability mass on the same top-k keys for the i-th query, computed using the true attention A i , as follows: Without loss of generality, let us assume, T jl = 1 ∀ l ∈ {1, . . . , k} and T jl = 0 ∀ l ∈ {k + 1, . . . , N }.
In this case, equation 21 can be written as: The total probability masses on the top-k keys, m i andm j can now be expressed as: From equation 24 it is clear that the clustered attention, A c i , and the improved clustered attention, A t i , only differ on the keys {1, . . . , k}. Thus, it suffices to show that A t i has lower approximation error on these keys. The approximation error on the top-k keys {1, . . . , k}, let it be e t , between the i-clustered attention and the full attention is as follows: Therefore, C Experiments

C.1 Time and Memory Benchmark
To measure the computational cost, we compare the memory consumption and computation time on artificially generated sequences of various lengths. For clustered attention we use 100 clusters, 63 bits for the LSH, and 10 Lloyd iterations for the K-Means. For the improved clustered attention, we use the same configuration with k = 32. For Reformer, we evaluate on two variants using 1 and 4 rounds of hashing. All models consist of 1 layer with 6 attention heads, embedding dimension of 64 for each head, and a feed-forward dimension of 1536.
In this experiment, we measure the required memory and GPU time per single sequence element to perform a forward/backward pass for the various self-attention models. Figure 4 illustrates how these metrics evolve as the sequence length increases from N = 2 9 to N = 2 15 . For a fair comparison, we use the maximum possible batch size for each method and we divide the computational cost and memory with the number of samples in each batch and the sequence length.
We note that, in contrast to all other methods, vanilla transformer scales quadratically with respect to the sequence length and does not fit in GPU memory for sequences longer than 2 13 elements. All other methods scale linearly. Clustered attention becomes faster than the vanilla transformer for sequences with 1000 elements or more, while improved clustered attention surpasses it for sequences with 2000 elements. Note that with respect to per sample memory, both clustered and improved clustered attention perform better than all other methods. This can be explained by the fact that our method does not require storing intermediate results to compute the gradients from multiple hashing rounds as Reformer does. It can be seen, that lsh-1 is faster than the improved clustered clustered attention, however, as also mentioned by [13] Reformer requires multiple hashing rounds to generalize.

C.2 Ablation on clusters and sequence length
Following [13], we introduce a synthetic task to analyze the relationship between the number of clusters and sequence length. In our task, the transformer models need to copy some symbols that are masked out from either the first or second half of the sequence. In particular, we generate a random sequence of tokens and we prepend a unique separator token, let it be 0. The sequence is then copied to get a target of the form 0w0w, where w ∈ {1, . . . , C} L , C is the number of possible symbols and L is the sequence length. To generate the input, we replace some symbols from the first half of Accuracy with respect to clusters and hashing rounds the sequence and some different symbols from the second half, such that the target sequence can be reconstructed from the input. An example of an input output pair with L = 4 can be seen in figure 6. Note that to solve this task, transformers simply need to learn to attend to the corresponding tokens in the two identical halves of the sequence.
We set the sequence length L to one of {31, 63, 127, 255} which means the input length varies between N = 2 6 and N = 2 9 . For each sequence, we sample tokens uniformly from {1, . . . , 10} and randomly mask out 20% of the tokens. To analyze the impact of number of clusters on performance, we train full transformer as well as clustered variants with different number of clusters and Reformer with different number of hashing rounds.
All transformer variants consist of 4 layers, 6 attention heads, embedding dimension of 32 for each head, and feed-forward dimension of 768. For both clustered and improved clustered attention, we set the number of bits for LSH to 63 and the number of Lloyd iterations for the K-Means to 10. Both clustered and improved clustered attention are trained with 15, 30, 60 and 100 clusters. We also train Reformer with 1, 4, 8 and 16 hashing rounds. Finally, all models are trained using R-Adam optimizer [14] with a learning rate of 0.0002, batch size of 32 for 5000 iterations.
In figure 5, we illustrate the results of this experiment as heatmaps depicting the achieved accuracy for a given combination of number of clusters and sequence length for clustered transformers and number of hashing rounds and sequence length for Reformer. Note that the vanilla transformer solves the task perfectly for all sequence lengths. We observe that both clustered (Fig. 5b) and Reformer (Fig. 5c) require more clusters or more rounds as the sequence length increases. However, improved clustered achieves the same performance as vanilla transformers, namely perfect accuracy, for every number of clusters and sequence length combination. This result increases our confidence that the required number of clusters for our method is not a function of the sequence length but of the task at hand.

C.3 Automatic Speech Recognition
In this section, we present the details for the ASR experiments such as transformer architecture, optimizer and learning rate schedule. As mentioned in the main paper, for i-clustered, unless specified, k is set to 32. Furthermore, all transformers have 6 heads with an embedding dimension of 32 on each head and feed-forward dimension of 768. Other architectural details specific to experiments are described later.

C.3.1 Wall Street Journal
Convergence Behaviour: For this experiment, we train transformer with full, clustered and Reformer attention variants. All models consist of 9 layers. For Reformer, we train two variants with 1 and 4 rounds of hashing with chunk size fixed to 32 as suggested. For clustered and improved clustered attention we set the number of clusters to 100. We also set the number of Lloyd iterations for K-Means to 10 and the bits for LSH to 63. All models are trained to convergence using the R-Adam optimizer [14] with a learning rate of 0.0001, max gradient norm set to 10.0 and and weight decay of 0.01. The learning rate is dropped when the validation loss plateaus. For each model we select the largest batch size that fits the GPU. The full attention model was trained with a batch size of 2 while the clustered variants: clustered and i-clustered could fit batch sizes of 14 and 10 respectively. For Reformer variants: lsh-1 and lsh-4, batch sizes of 8 and 6 were used.
In figure 7a, we show the training loss convergence for different transformer variants. It can be seen that i-clustered has a much faster convergence than the clustered attention. This shows that the improved clustered attention indeed approximates the full attention better. More importantly, only Only i-clustered has a faster or comparable wall-clock convergence to full attention. Both the clustered variants are have a significantly better convergence than both lsh-1 and lsh-4. Note that due to a smaller batch size full makes many more updates than all other transformer variants. More details can be found in § C.3.1 and § C.3.2.
the i-clustered attention has a comparable wall-clock convergence. Given that full has a much smaller batch size, it make many more updates per-epoch. We think that a slightly smaller batchsize with more updates would have been a better choice for the clustered transformers w.r.t. the wall-clock convergence. This is reflected in the Switchboard experiments where the batchsizes for clustered variants were smaller due to more layers. Finally, as can be seen from the wall-clock convergence, the clustered transformers significantly outperform the Reformer variants.

Speed-Accuracy Tradeoff:
As described in the main paper, for this task we additionally train full with 4 and 6 layers. Similary, we train clustered with 9 layers, and 200 and 300 clusters. We also train an i-clustered model with 9 layer and 200 clusters, and smaller models with 6 layers, and 100 and 200 clusters.
For clustered and i-clustered variants with 9 layers, we finetuned the previously described models trained with 100 clusters. We finetuned for 15 epochs with a learning rate of 0.00001. We train full with 4 and 6 layers to convergence in a similar fashion to the full with 9 layers described previously. Finally, for i-clustered, we first trained model with 6 layers and 100 clusters using the training strategy used for 9 layers and 100 clusters. We then finetuned this model for 15 epochs using 200 clusters and a learning rate of 0.00001.

C.3.2 Switchboard
Convergence Behaviour: For this experiment, we train transformer with full and clustered attention variants. All models consist of 12 layers. For clustered and improved clustered attention we set the number of clusters to 100. We also set the number of Lloyd iterations for K-Means to 10 and the bits for LSH to 63.
Following common practice for flat-start lattice-free MMI training, we train over multiple gpus with weight averaging for synchronization as described in [22]. Specfically, we modify the e2e training recipe for the Wall Street Journal in Kaldi [21] with the following two key differences: first, the acoustic model training is done in PyTorch and second, we use R-Adam optimizer instead on natural stochastic gradient descent.
All models are trained using the R-Adam optimizer with a learning rate of 0.0002, max gradient norm set to 10.0 and and weight decay of 0.01. The learning rate is dropped when the validation loss plateaus. We use the word error rate (WER) on the validation set for early stopping and model selection. The full attention model is trained with a batch size of 2 while the clustered variants: clustered and i-clustered are trained with a batch size of 6.
In figure 7b, we show the training loss convergence for different transformer variants. It can be seen that i-clustered has the fastest convergence for this setup. Note that the overall training time for clustered attention is still less than that of full as it starts to overfit early on the validation set WER.

Speed-Accuracy Tradeoff:
For this task we additionally train full with 6 and 8 layers. Similary, we train clustered with 12 layers, and 200 and 300 clusters. We also train i-clustered with 12 layer and 200 clusters, and smaller models with 8 layers, and 100 and 200 clusters.
For clustered and i-clustered variants with 12 layers, we finetuned the previously described models trained with 100 clusters. We finetuned for 5 epochs with a learning rate of 0.00001. Once again, full with 6 and 8 layers were trained to convergence similar to full with 12 layers described previously. Finally, for i-clustered with 8 layers, we first train a model with 100 clusters using the training strategy used for 12 layers and 100 clusters. We then finetuned this model for 5 epochs using 200 clusters and a learning rate of 0.00001.

C.4 RoBERTa Approximation
In this section we provide a qualitative comparison between the full attention, and the clustered attention variants clustered and i-clustered used for approximation. As described in main paper, we use 25 clusters for both attention variants. In Figure 8 we show the attention distribution for the question tokens for a randomly selected question-context tuple from the SQuAD dataset. For each token in the question we show the attention distribution over the input sequence formed by concatenating question and context tokens with CLS and SEP tokens appended. It can be seen that with only few clusters, improved clustered approximates the full attention very closely even when the attention distribution has complicated and sparse patterns. In contrast, clustered attention fails to capture such attention distribution during approximation. Moreover, it can further be seen that for almost all question tokens, both full and improved clustered have the same tokens with the highest attention weights. This further strengthens our believe that improved clustered attention can approximate a wide range of complicated attention patterns.
Manning finished the year with a career-low 67.9 passer rating, throwing for 2,249 yards and nine touchdowns, with 17 interceptions. In contrast, Osweiler threw for 1,967 yards, 10 touchdowns and six interceptions for a rating of 86.4. Veteran receiver Demaryius Thomas led the team with 105 receptions for 1,304 yards and six touchdowns, while Emmanuel Sanders caught 76 passes for 1,135 yards and six scores, while adding another 106 yards returning punts. Tight end Owen Daniels was also a big element of the passing game with 46 receptions for 517 yards. Running back C. J. Anderson was the team's leading rusher 863 yards and seven touchdowns, while also catching 25 passes for 183 yards. Running back Ronnie Hillman also made a big impact with 720 yards, five touchdowns, 24 receptions, and a 4.7 yards per carry average. Overall, the offense ranked 19th in scoring with 355 points and did not have any Pro Bowl selections.  : Attention matrices for question-context tuples for full attention, and clustered and i-clustered attention used for approximation. 8a shows the the context for the question with answer higlighted in red. 8b shows the attention distribtution for full, 8c and 8d show the approximation using i-clustered and clustered respectively. Note that i-clustered has attention patterns very similar to full while clustered shows qualitatively different attention patterns. For each question token, we also present the tokens with highest attention above a threshold on the right axis. For more information refer to § C.4.