Skip to main content
QUICK REVIEW

[Paper Review] Stable, Fast and Accurate: Kernelized Attention with Relative Positional Encoding

Shengjie Luo, Shanda Li|arXiv (Cornell University)|Jun 23, 2021
Advanced Neural Network ApplicationsComputer Science10 citations
TL;DR

This paper proposes a novel kernelized attention mechanism with relative positional encoding (RPE) that achieves O(n log n) complexity using Fast Fourier Transform (FFT), enabling stable, fast, and accurate long-sequence modeling in Transformers. The method leverages the Toeplitz structure of RPE matrices to accelerate computation while improving training stability by reducing variance in attention scores.

ABSTRACT

The attention module, which is a crucial component in Transformer, cannot scale efficiently to long sequences due to its quadratic complexity. Many works focus on approximating the dot-then-exponentiate softmax function in the original attention, leading to sub-quadratic or even linear-complexity Transformer architectures. However, we show that these methods cannot be applied to more powerful attention modules that go beyond the dot-then-exponentiate style, e.g., Transformers with relative positional encoding (RPE). Since in many state-of-the-art models, relative positional encoding is used as default, designing efficient Transformers that can incorporate RPE is appealing. In this paper, we propose a novel way to accelerate attention calculation for Transformers with RPE on top of the kernelized attention. Based upon the observation that relative positional encoding forms a Toeplitz matrix, we mathematically show that kernelized attention with RPE can be calculated efficiently using Fast Fourier Transform (FFT). With FFT, our method achieves $\\mathcal{O}(n\\log n)$ time complexity. Interestingly, we further demonstrate that properly using relative positional encoding can mitigate the training instability problem of vanilla kernelized attention. On a wide range of tasks, we empirically show that our models can be trained from scratch without any optimization issues. The learned model performs better than many efficient Transformer variants and is faster than standard Transformer in the long-sequence regime.

Motivation & Objective

  • To address the inefficiency of standard self-attention in long sequences due to quadratic complexity.
  • To enable efficient attention computation in Transformers that use relative positional encoding (RPE), which is standard in state-of-the-art models.
  • To overcome the limitations of prior approximation methods that cannot handle RPE due to its expanded expressiveness beyond dot-then-exponentiate attention.
  • To stabilize training of kernelized attention models, which often suffer from optimization instability in practice.
  • To achieve both computational efficiency and high performance in long-sequence regimes without relying on fine-tuning.

Proposed method

  • The method reformulates kernelized attention with RPE by exploiting the Toeplitz structure of the relative positional encoding matrix.
  • It uses Fast Fourier Transform (FFT) to accelerate matrix-vector multiplication between the RPE matrix and kernelized feature matrices, reducing time complexity to O(n log n).
  • The approach replaces the standard softmax attention with a kernelized attention mechanism that uses feature maps to approximate the attention function.
  • It introduces normalized attention (NPRF) to reduce variance and improve training stability, especially when query and key norms are large.
  • The method applies the kernelized attention mechanism to both encoder and decoder blocks, enabling full integration into Transformer architectures.
  • It supports various feature map functions (e.g., PRF, TRF, Sphere-PRF, ORF), demonstrating generalization across different choices.

Experimental results

Research questions

  • RQ1Can kernelized attention with relative positional encoding be computed efficiently beyond the standard dot-then-exponentiate formulation?
  • RQ2Does the use of RPE in kernelized attention improve training stability compared to vanilla kernelized attention?
  • RQ3Can the Toeplitz structure of RPE matrices be exploited to achieve sub-quadratic time complexity in attention computation?
  • RQ4How does the performance of the proposed method compare to standard and efficient Transformer variants on long-sequence tasks?
  • RQ5Is the proposed method robust to different feature map dimensions and types in the kernelized attention mechanism?

Key findings

  • The proposed method achieves O(n log n) time complexity by leveraging FFT on the Toeplitz-structured RPE matrix, enabling efficient long-sequence modeling.
  • The model with normalized kernelized attention and RPE trains stably from scratch without optimization issues, unlike vanilla kernelized attention.
  • On the ImageNet dataset, the proposed NPRF-Transformer with RPE achieves 80.9% top-1 accuracy, outperforming the standard DeiT-base (81.2%) and the PRF-converted model (79.5%).
  • On the IWSLT14 German-to-English translation task, the model with RPE and normalized attention achieves a BLEU score of 32.1, comparable to the well-trained standard Transformer (32.3), while maintaining linear complexity.
  • The method is robust to feature map dimensionality, with minimal performance drop even at 16 dimensions, and generalizes across different feature map functions (PRF, TRF, Sphere-PRF, ORF).
  • The use of RPE reduces the approximate error when converting from standard to kernelized attention, especially when combined with normalization, as shown by smaller performance degradation in ablation studies.

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.