Abstract
Random feature attention (RFA) adopts random fourier feature (RFF) methods to approximate the softmax function, resulting in a linear time and space attention mechanism that enables the construction of an efficient Transformer. Inspired by RFA, we propose Macformer, a Transformer architecture that employs random Maclaurin features (RMF) to approximate various dot-product kernels, thereby accelerating attention computations for long sequences. Macformer consists of Random Maclaurin Feature Attention (RMFA) and pre-post Scaling Batch Normalization (ppSBN); the former is an unbiased approximation for dot-product kernelized attention and the latter is a two-stage regularization mechanism guaranteeing the error of RMFA. We conducted toy experiments to demonstrate the efficiency of RMFA and ppSBN, and experiments on the long range arena (LRA) benchmark to validate the acceleration and accuracy of Macformer with different dot-product kernels. Experiment results of Macformer are consistent with our theoretical analysis.
Introduction
The standard Transformer's self-attention mechanism is its primary computational bottleneck. Its cost grows quadratically ($O(n^2)$) with sequence length ($n$), making it prohibitively expensive for long-form data like high-resolution images or long documents. While previous "Linear Transformer" research utilized Random Fourier Features (RFF) to approximate attention, these methods are typically restricted to shift-invariant kernels. Standard Softmax attention, however, is a dot-product kernel. Macformer addresses this by using Maclaurin expansions to directly and unbiasedly approximate dot-product kernels, offering a flexible way to linearize attention.
The Macformer Architecture
Macformer introduces two primary components to achieve linear complexity while maintaining expressive power: Random Maclaurin Feature Attention (RMFA) and Pre-post Scaling Batch Normalization (ppSBN).
Random Maclaurin Feature Attention (RMFA)
Instead of computing the full $n \times n$ attention matrix, RMFA projects Queries ($Q$) and Keys ($K$) into a lower-dimensional random feature space using Maclaurin coefficients. By leveraging the associative property of matrix multiplication, the model can multiply Keys and Values first. This shift reduces computational complexity from $O(n^2d)$ to $O(ndD)$, where $D$ is the dimension of the random projection.
Pre-post Scaling Batch Normalization (ppSBN)
Random Maclaurin Features require inputs to be constrained within a unit ball ($\ell_2$ norm $\le 1$) for mathematical stability. ppSBN manages this through a two-stage process:
- Pre-scaling: Normalizes and scales $Q$ and $K$ to fit required constraints.
- Post-scaling: Uses learnable parameters ($\gamma, \beta$) to restore the dynamic range of the attention output, preventing loss of expressive power.
Experimental Evaluation
The authors evaluated Macformer across several benchmarks to test both efficiency and accuracy.
Performance on Long Range Arena (LRA)
On the LRA benchmark, designed for long-range dependencies, Macformer demonstrated significant speedups. For instance, on the Retrieval task, the Macformer_log variant achieved a training time of 0.348 relative to the base Transformer's 1.000—nearly a 3x speedup.
In terms of accuracy, Macformer variants often outperformed the standard Transformer. On the Listops task, several Macformer kernels achieved approximately 38.3% accuracy, surpassing the base Transformer's 37.3%. The results confirmed that as the projection dimension ($D$) increases, the approximation error drops rapidly, following theoretical bounds of $O(1/\sqrt{D})$.
Normalization as Regularization
Experiments on the Multi30k translation dataset revealed that ppSBN acts as a potent regularizer. Models utilizing ppSBN often outperformed the base Transformer in generalization (e.g., higher BLEU scores), even when not employing the random feature approximation. This suggests that the constraints required for RMFA stability also benefit overall model training.
Conclusion
Macformer provides a robust framework for linearizing attention through Random Maclaurin Features. By moving beyond the limitations of shift-invariant kernels, it allows for the exploration of various dot-product kernels (like log or sqrt) within a Transformer architecture. While future work is needed to automate kernel selection and further optimize memory for random mappings, Macformer represents a significant step toward efficient, long-sequence modeling with theoretical guarantees.
Summary updated