{'fraction_non_alphanumeric': 0.03422869408829843, 'fraction_numerical': 0.012531182920461797, 'mean_word_length': 4.989576094510077, 'pattern_counts': {'":': 0, '<': 0, '<?xml version=': 0, '>': 0, 'https://': 0, '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}}
Locality Matters: A Locality-Biased Linear Attention for Automatic Speech Recognition

Conformer has shown a great success in automatic speech recognition (ASR) on many public benchmarks. One of its crucial drawbacks is the quadratic time-space complexity with respect to the input sequence length, which prohibits the model to scale-up as well as process longer input audio sequences. To solve this issue, numerous linear attention methods have been proposed. However, these methods often have limited performance on ASR as they treat tokens equally in modeling, neglecting the fact that the neighbouring tokens are often more connected than the distanced tokens. In this paper, we take this fact into account and propose a new locality-biased linear attention for Conformer. It not only achieves higher accuracy than the vanilla Conformer, but also enjoys linear space-time computational complexity. To be specific, we replace the softmax attention with a locality-biased linear attention (LBLA) mechanism in Conformer blocks. The LBLA contains a kernel function to ensure the linear complexities and a cosine reweighing matrix to impose more weights on neighbouring tokens. Extensive experiments on the LibriSpeech corpus show that by introducing this locality bias to the Conformer, our method achieves a lower word error rate with more than 22% inference speed.


Introduction
ASR is a long-lasting problem in audio processing and has been extensively studied for decades. Recently, end-to-end deep neural network based methods are becoming one of the most promising approaches and have dominated numerous ASR leader boards. An end-to-end ASR model often integrates acoustic model, pronunciation model and language model coherently. There are several widely used structures for endto-end ASR modelling such as connectionist temporal classification (CTC) [1][2][3], attention-based encoder-decoder model (AED) [4][5][6][7], recurrent neutral network transducer (RNN-T) [8][9][10] and Transformer [11]. In particular, Conformer [12] is a variant of Transformer and the most advanced structure for ASR due to its high performance.
Attention is the key component in Conformer which aims to capture global dependencies. Compared with conventional encoders such as the long short-term memory (LSTM) [9] which implicitly encodes temporal information in hidden layers, the attention-based encoders explicitly store the global temporal dependencies through a relevance weighted matrix and use it to weight the input sequence. Although previous methods demonstrate the effectiveness of the attention mechanism, it suffers two crucial drawbacks: 1). It is less effective to capture local dependencies; 2). It requires quadratic space-time computational complexity with respect to the sequence length. Numerous approaches have been proposed to address these limitations.
To emphasize the local dependencies, ContextNet [13] proposes using convolution module to capture local dependencies and leveraging a squeeze-and-excitation module to summarize global context from input sequence. Conformer integrates the convolution module and self-attention network (SAN) module in a transformer block in a sequential manner to augment local dependencies and obtains the state-of-the-art performance. In this paper, we propose a new locality-biased attention to blend local dependencies in a SAN module as well and achieves a higher performance.
For the quadratic computational complexity, a common solution in ASR is to restrict the input speech length or truncate the long form speech into several smaller speeches. However, the model's performance may degrade due to the lack of longrange information dependencies. This solution does not touch the essence of the limitation as the network still suffers the quadratic complexity. Efficient transformers are then proposed to reduce the quadratic space-time complexities directly. Sparse transformers often use local windows [14], learnable sparse attention matrix [15], locality-sensitive hashing attention mechanism [16], and Kullback-Leibler divergence based sparsity measurement [17] to skip some attention weight's computation and thus reduce the computational complexities. Linear transformers, on the other hand, reduce the complexities to linear with some kernel tricks. For example, the Gaussian-kernalized selfattention [18] uses a variant of the Gaussian kernel to represent attention weight and replace dot-product self-attention. Random Feature Attention (RFA) [19] proposes a linear time and space attention that uses random feature methods to approximate the softmax function and applies in Transformer. Performers [20] estimates vanilla attention with comparable accuracy using a novel fast attention via positive orthogonal random features approach, which has linear space-time complexities. Since they involved softmax approximation in the attention process, and these errors may accumulated through layers, these methods often suffer from degenerated performances. For the ASR task, as it is trying to map the continuous speech features into discrete transcripts, it is more sensitive to these approximation errors and will cause severer performance drop.
In this paper, we solve these two drawbacks with one stone: a new locality-biased linear attention that not only ensures neighbouring tokens to have a higher weights than the distanced tokens but also enjoys linear space-time computational complexity. Our LBLA consists of two components: a kernel function that allows us to use kernel tricks to reduce the computational complexity to linear, and a locality-biased reweighting mechanism to impose the locality bias. It is worth noting that not all locality biased re-weighting matrix can be ap-plied here as it also needs to satisfy the linear attention requirements, so that we can use the kernel tricks on the re-weighting matrix as well. The experiments are conducted on the Lib-riSpeech corpus [21]. They show that compared with the vanilla Conformer model, the proposed method increases the inference speed by 6%∼22% and obtains the comparable word error rate (WER) performance. After finetuning with model initialization, the proposed method obtains WER reduced by relatively 2.4%∼5.7%.
The paper is organized as follows: Section 2 gives a brief introduction about conformer encoder architecture. The proposed conformer-based LBLA model is described in Section 3, followed by experiments and discussions in Section 4.

Softmax Attention
End-to-end ASR model maps the arbitrary input feature sequence X={x1, x2, ..xt} of length T into output sequence Y ={y1, y2, ...yu} of length U directly. Usually, the length of label U is much smaller than length of speech frames T . Transformer is proposed in [11], which is a stack of transformer layer consists of self-attention network (SAN), feed forward network (FFN), layernorm and residual connection. Self-attention mechanism in SAN can be represented below: is scaling factor, Q, K and V refer to the query, key and value that are projected to high-level vectors using parameter matrices Wq,W k and Wv: Multi-head attention (MHA) mechanism also proposed in [11] projects query, key and value to several separately linear vector subspace. Then all of value embedding concatenated and project again with Wo: Where h is the number of heads and W i q ,W i k and W i v are parameter matrices for i-th attention head.

Linear Attention
As shown in Eq. 1, query Q ∈ R T ×d and key K ∈ R T ×d are utilized for the attention weight matrix to represent the proximity between them, where T is the length of input sequence and d is the embedding length. Self-attention mechanism calculates attention weight matrix QK T ∈ R T ×T followed by softmax function that is applied to obtain normalization.
Since the complexity of calculating attention weight matrix is O(T 2 ), which has quadratic time and memory bottleneck with respect to the input sequence length. In order to address this issue, we approximate the attention weight matrix and rewrite self-attention function as: Denote operator exp(QiK T j ) as the proximity between i-th query and j-th key, the proximity measure function P(·) can be defined as: Then self-attention can be rewrite as: The proximity measure function P(·) need to be calculated T 2 times. To maintain a linear computation complexity, we adopt a decomposable proximity function below: where ψ(·) is a kernel function that maps the queries and keys to hidden representations. Consequently, we could rewrite selfattention in the form of kernel functions as: Instead of explicitly calculating the attention weight matrix QK T ∈R T ×T , key and value are calculated first as ψ(Kj) T Vj∈R d×d . Note that the dimension of head d is always much smaller than the input speech sequence length of T . By using this trick, the computational complexity of calculating matrix can be reduce to

Locality-Biased Linear Attention
In this section, we first provide a detailed description of the two components in our LBLA module, i.e., the kernel functions and the locality-biased re-weighting matrix, then illustrate how to integrate the LBLA module to a Conformer block.

The Kernel Function
To linearize the attention, the kernel functions have to satisfy the Eq. 9, then we can apply the kernel trick in Eq. 10 to reduce the computational complexities. How to find the right kernel function for the ASR task is a non-trivial task. Previous methods in natural language processing [22,23] suggest that the kernel function needs to project the tokens to a nonnegative space such that the negatively-correlated information can be excluded in contextual information aggregation. Weakattention [24] suggests that ASR will likely benefit from sparse and localized attention weight. Following these suggestions, we explore three commonly used non-negative activation functions as our kernel functions: the ReLU function, the Exponential function, and the Sigmoid function. We defined these functions as below: We use dot product as our proximity measure function P(·) so that the requirement of Eq. 9 fulfilled.
In the experiment section, we extensively ablate these selections and find that the Sigmoid one achieves the most accurate results. Therefore, in our LBLA, we use the Sigmoid function as our kernel function. Figure 1: Visualization of the locality-biased re-weighting patterns. The black cross denotes the query position and the mask denotes the corresponding locality weights.

Locality-biased Re-weighting Mechanism
The Eq. 9 computes the proximity measurements without any re-weighting mechanism. In Eq. 14, we show a locality-biased proximity measurement with a re-weighting matrix ω(·).
where the ω(·) needs to satisfy the Eq. 15 for linearization.
where f (·), g(·) are decomposed functions of ω and n is the number of decomposed terms. Not many re-weighting matrices meet this requirement. Here, we use a cosine re-weighting matrix which can be decomposed with the Ptolemy's theorem as below: Figure 1 illustrates our locality-biased re-weighting matrix, where the neighbouring tokens are assigned with higher weights and the distanced ones are with lower weights.

Locality-biased Conformer Block
Conformer has obtained state-of-the-art performance in ASR [12,[25][26][27][28][29]. As a variant of Transformer, Conformer uses convolution module to augment local dependencies. A single vanilla conformer block consists of convolution module, multihead self-attention module and two independent feed forward networks. In our locality-biased Conformer block, we replace the multi-head self-attention module with our LBLA module and keep the rest modules unchanged. As illustrated in Figure 2, our block starts with a FFN, then the LBLA module to capture locality-biased global context followed by a convolution module that captures local context, another FFN is deployed as the last module. Especially, FFN is employed half-step residual weights in Conformer. The convolution module consists of pointwise convolution and gated linear unit, followed by one dimension convolution module and batchnorm, the last in swish activation function and another pointwise convolution.

Experimental Setup
The experiments are conducted using LibriSpeech corpus, which has 960-hour audios for training. WER is evaluated on LibriSpeech test-clean and test-other test sets respectively. The test-clean contains simple and clean audios, test-other contains complex and noisy audios. All the neural networks are trained using Wenet toolkit [30]. Models are trained with 3727 subword units, which generated from training transcripts of Lib-riSpeech using SentencePiece [31]. Besides, the features embedding fed into the encoder is a 80-dimensional log filter-bank feature with 10ms frame shift and 25ms window size followed by Spec-Augment [32]. Two convolution layers are added with ReLU [33] activation function to downsample the frame rate to 4 with absolute position embedding layer.
The encoder has 12 layers, each conformer layer has convolution module with kernel size 31, model dimension 256 and two feed forward layers with dimension 2048. Dropout [34] rate is set to 0.1 for all layers. The decoder is a standard transformer decoder with 6 layers, each transformer layer has a FFN layer with dimension 2048, 4 attention heads and dropout is also set to 0.1 for all layers. CTC criterion is utilized to optimize the CTC loss as an auxiliary loss. Models are trained with the Adam [35] optimizer for about 150000 steps, peak learning rate is 4 × 10 −3 and warm up is 25000 steps. In inference, CTC greedy search and attention rescoring are applied to evaluate the performance of the model while beam size is set to 10. The amount of model parameter is about 49M. Besides, We trained our models on one machine with 8 NVIDIA Tesla A100 GPUs.

Results
All models are trained on LibriSpeech from scratch. The proposed model use kernel functions ReLU, Sigmoid [36] and Exponential for comparison. We have observed that it's beneficial to change the number of attention head. Table 1 shows the result of CTC greedy search and attention rescoring respectively.
Firstly, we evaluate the performance of models that use CTC greedy search. As illustrated in Table 1, Conformer baseline achieves a WER 3.71/9.67 on test-clean/test-other. Compared with baseline, R0-R2 perform slightly worse and achieve Confomer, and exponential-based kernel function such as Sigmoid function and Exponential function could enlarge the distance among the proximity P(·) to force the attention matrix to be more sparse.
We also evaluate the performance of models using attention rescoring. Conformer baseline achieves a WER 3.2/8.74 on test-clean/test-other. R0-R2 are slightly worse than baseline and achieve a relative degradation of WER by 2.1%∼6% on test-clean and 1.5%∼4.8% on test-other. E0-E2 achieve a relative degradation of WER by 2.5%∼4.7% on test-clean and 0%∼1.6% on test-other. However, S0-S2 obtain the best performance in our experiments and achieve a relative reduction of WER by 1.2% on test-clean and 1.5% on test-other. The proposed LBLA model also achieves better performance.
In our experiments, we obverse that validation loss is increasing while training last several epochs. It seems that the larger learning rate cannot make the LBLA model converge well in the late training stage. Consequently, We change the learning rate for late training stage while the rest strategies keep same by resume the model from middle of the training stage, all models' number of training steps keep same. As illustrated in Table 2. F1 outperforms the baseline trained from scratch by relatively 2.4%∼5.7%.   To evaluate the time consumption of inference, we conduct the experiments on three test sets that contain different duration of speech. As illustrated in Table 3, our proposed LBLA model obtains the better performance on time consumption. Compared with baseline, R1 increases the speed of inference by relatively 6% on test-clean and S2 increases relatively 22% on long speech sub set. Our proposed model will be more efficient with respect to the input sequence length.
LBLA model differs from a Conformer in some ways, specially, the inclusion of a kernel function and locality-biased reweight mechanism. Normalization is applied similar to vanilla attention mechanism. We study these effects of these differences by removing features. Table 4 shows the impact to the LBLA model. Kernel function and normalization are important to stabilize training until convergence. Locality-biased reweight is an efficient way to obtain shift-invariant positional information and better performance.

Conclusions
In this paper, we proposed a LBLA model which introduces a locality-biased linear mechanism in Conformer block to enjoy linear space-time complexities. The LBLA contains a kernel function to ensure linear computational complexity and a cosine reweighing matrix to impose more weight on neighbouring tokens. We evaluated it extensively on the LibriSpeech corpus. Compared with the vanilla Conformer, our proposed model enjoys linear computational complexity, obtains comparable WER performance and increases the inference speed by 6%∼22%.