Linformer and Performer: Linear Transformers
1. The Standard Transformer Architecture
The Standard Transformer Architecture
The Transformer architecture, introduced by Vaswani et al. (2017), revolutionized sequence modeling by replacing recurrent and convolutional operations with self-attention mechanisms. At its core, the Transformer processes input sequences through stacked encoder and decoder layers, each employing multi-head attention and position-wise feed-forward networks.
Self-Attention Mechanism
The self-attention mechanism computes weighted sums of input representations, where the weights are dynamically derived from pairwise interactions between elements. Given an input sequence X ∈ ℝn×d with n tokens and d-dimensional embeddings, the queries (Q), keys (K), and values (V) are computed as:
where WQ, WK, WV ∈ ℝd×dk are learnable projection matrices. The attention weights A are then calculated using scaled dot-product attention:
The scaling factor 1/√dk prevents gradient vanishing issues when dk is large. The final output is computed as the weighted sum of values:
Multi-Head Attention
Multi-head attention extends this mechanism by applying h parallel attention heads, each with separate projection matrices. This allows the model to jointly attend to information from different representation subspaces. The outputs of all heads are concatenated and linearly projected:
where each headi = Attention(QWQ(i), KWK(i), VWV(i)) and WO ∈ ℝhdv×d.
Position-Wise Feed-Forward Networks
Each attention sub-layer is followed by a position-wise feed-forward network (FFN) that applies two linear transformations with a ReLU activation in between:
where W1 ∈ ℝd×dff, W2 ∈ ℝdff×d, and dff is typically 4×d.
Layer Normalization and Residual Connections
The Transformer employs residual connections around each sub-layer, followed by layer normalization:
This architecture enables parallel processing of entire sequences while maintaining the ability to model long-range dependencies through self-attention. However, the quadratic complexity O(n2) of computing the attention matrix becomes a computational bottleneck for long sequences, motivating the development of efficient variants like Linformer and Performer.

Computational and Memory Bottlenecks in Attention Mechanisms
The standard self-attention mechanism in Transformers scales quadratically with sequence length, presenting significant computational and memory challenges. For an input sequence of length N, the attention mechanism computes pairwise interactions between all tokens, resulting in O(N²) time and space complexity. This becomes prohibitive for long sequences, such as in document processing or high-resolution image tasks.
Mathematical Formulation of the Bottleneck
The attention operation computes three matrices—Query (Q), Key (K), and Value (V)—each of dimension N×d, where d is the embedding dimension. The attention scores are computed as:
Here, the matrix multiplication QKT produces an N×N matrix, which requires O(N²d) operations and O(N²) memory. For sequences of length 10,000, this results in 100 million entries just for the attention scores, making training and inference computationally intensive.
Memory Bandwidth Limitations
Beyond raw FLOPs, the memory bandwidth required to load the Q, K, and V matrices becomes a critical bottleneck. Modern GPUs and TPUs are often memory-bound, meaning their computational throughput is limited by how quickly data can be fetched from memory. The quadratic memory requirement exacerbates this issue, leading to underutilization of compute resources.
Practical Implications
- Batch Processing Constraints: Larger sequence lengths reduce the maximum batch size that can fit in GPU memory, slowing down training.
- Long-Range Dependency Modeling: Truncating sequences to fit memory limits the model's ability to capture long-range dependencies.
- Hardware Utilization: The memory-bound nature of attention leads to inefficient use of specialized hardware like TPUs, which are designed for compute-intensive operations.
Case Study: Genome Sequence Analysis
In bioinformatics, genome sequences can exceed 100,000 tokens. A standard Transformer would require ~40GB of memory just for the attention scores (assuming 32-bit floats), making it infeasible on most hardware. This has driven research into linear-time alternatives like Linformer and Performer, which approximate full attention with O(N) complexity.
Here, E and F are low-rank projection matrices that reduce the N×N attention matrix to N×k, where k ≪ N. This reduces both compute and memory to O(Nk), enabling efficient processing of long sequences.
1.3 Need for Efficient Alternatives: Linformer and Performer
The quadratic computational and memory complexity of standard Transformer models, specifically the self-attention mechanism, poses a significant bottleneck for processing long sequences. Given an input sequence of length n, the self-attention mechanism computes an n × n attention matrix, leading to O(n²) time and space complexity. This becomes prohibitive for tasks involving long documents, high-resolution images, or genomic sequences, where n can easily exceed tens of thousands.
Limitations of Standard Self-Attention
The self-attention mechanism computes pairwise interactions between all tokens in the sequence. For queries Q, keys K, and values V, the attention output is:
Here, QKT requires O(n²d) operations, where d is the embedding dimension. While several approximations like sparse attention and locality-sensitive hashing (LSH) have been proposed, they often sacrifice theoretical guarantees or empirical performance.
Linformer: Low-Rank Projection of Attention
The Linformer (Wang et al., 2020) addresses this by projecting the n × d key and value matrices into a lower-dimensional space k × d, where k ≪ n. This reduces the effective sequence length from n to k, yielding linear O(n) complexity. The projected attention is computed as:
where E and F are learned projection matrices of size k × n. The key insight is that the attention matrix is often low-rank, allowing compression without significant performance degradation.
Performer: Kernel-Based Linear Attention
The Performer (Choromanski et al., 2021) reformulates attention using random feature maps to approximate the softmax kernel. By leveraging the mathematical equivalence:
where φ is a randomized feature map, the Performer computes attention in linear time. The generalized attention mechanism is:
This avoids explicitly computing the n × n attention matrix, reducing complexity to O(n) while maintaining strong theoretical guarantees.
Practical Trade-offs and Applications
Both Linformer and Performer have distinct advantages:
- Linformer excels in tasks where the sequence length is fixed or can be bounded, such as document classification or protein sequence modeling.
- Performer is more flexible for variable-length sequences and has been successfully applied to large-scale vision transformers and DNA sequence analysis.
Empirical studies show that these methods achieve comparable accuracy to standard Transformers while reducing memory usage by up to 90% for sequences of length 8192. However, the choice between them depends on the specific task constraints, such as whether low-rank assumptions (Linformer) or kernel approximations (Performer) are more suitable.

2. Key Innovations: Low-Rank Projections and Linear Attention
Key Innovations: Low-Rank Projections and Linear Attention
The computational bottleneck in standard transformer architectures stems from the quadratic complexity of self-attention with respect to sequence length. For an input sequence of length n, the attention mechanism computes an n×n matrix, leading to O(n²) time and memory complexity. Both Linformer and Performer address this limitation through distinct but mathematically related approaches centered on low-rank approximations of the attention matrix.
Low-Rank Projections in Linformer
Linformer introduces a parameterized low-rank projection to reduce the effective dimensionality of the key (K) and value (V) matrices. The key insight is that the attention matrix A = softmax(QKT/√d) often exhibits approximately low-rank structure in practice. By projecting K and V from n×d to k×d where k ≪ n, the effective attention computation becomes linear in n:
where Ei, Fi ∈ ℝk×n are learned projection matrices. The theoretical justification comes from the Johnson-Lindenstrauss lemma, which guarantees that random projections can preserve pairwise distances with high probability. In practice, Linformer uses learned projections rather than random ones, achieving better empirical performance while maintaining the O(n) complexity.
Linear Attention in Performer
The Performer takes a different approach by reformulating attention through kernel methods. The standard softmax attention can be viewed as a dot product in exponential space:
The Performer replaces this with a generalized attention mechanism using positive random features for unbiased approximation. The key innovation is the decomposition of the exponential kernel:
where ϕ(·) maps the queries and keys to a higher-dimensional space where their dot product approximates the exponential kernel. This allows rewriting the attention computation as:
where Q' = ϕ(Q) and K' = ϕ(K) are the transformed matrices. The resulting architecture, called FAVOR+ (Fast Attention Via Positive Orthogonal Random features), achieves O(n) complexity while maintaining competitive performance with standard attention.
Practical Considerations and Tradeoffs
Both approaches make different tradeoffs between approximation quality and computational efficiency:
- Linformer excels in scenarios where sequences are very long but the attention patterns are approximately low-rank. The projection dimension k becomes a critical hyperparameter balancing quality and speed.
- Performer provides more flexibility through its kernel approximation approach, particularly when attention patterns don't exhibit strong low-rank structure. The choice of random feature map (ϕ) affects both computational cost and approximation quality.
Empirical studies show that both methods can achieve 95%+ of the original transformer's accuracy while reducing memory usage by orders of magnitude for sequences beyond 1024 tokens. The Performer's kernel approach has proven particularly effective in domains requiring modeling of complex, non-local dependencies such as protein sequence modeling and high-resolution image generation.

2.2 Mathematical Formulation of Linformer Attention
The Linformer architecture introduces a low-rank approximation of the self-attention mechanism to reduce the quadratic complexity of standard Transformers. The key insight is that the attention matrix can be projected into a lower-dimensional space without significant loss in performance. This section derives the mathematical formulation step-by-step.
Standard Self-Attention Recap
Given input sequences X ∈ ℝn×d where n is the sequence length and d is the embedding dimension, the standard self-attention computes:
where Q, K, V are linear projections of X, and the attention matrix A = QKT ∈ ℝn×n exhibits O(n2) memory complexity.
Low-Rank Projection
Linformer approximates the attention matrix by factorizing it through learned projection matrices E, F ∈ ℝk×n where k ≪ n. The projected keys and values become:
with K, V ∈ ℝn×d and E, F reducing the sequence dimension from n to k.
Modified Attention Computation
The attention scores are now computed as:
This reduces the intermediate attention matrix from size n×n to n×k, lowering memory usage from O(n2) to O(nk).
Theoretical Justification
The effectiveness of this approximation stems from the Johnson-Lindenstrauss lemma, which guarantees that pairwise distances can be preserved under random projections. For the attention matrix A, there exist low-rank matrices PQ, PK ∈ ℝn×k such that:
with high probability when k = O(ϵ−2 log n).
Practical Implementation
In practice, Linformer uses shared projections across layers and heads to further reduce parameters. The projection matrices can be:
- Fixed (e.g., random Gaussian matrices)
- Learned (trained end-to-end)
- Hybrid (initialized randomly then fine-tuned)
For sequences longer than the trained n, the projections can be applied to overlapping chunks of the input.

Performance and Efficiency Trade-offs
Computational Complexity Analysis
The standard Transformer's self-attention mechanism scales quadratically with sequence length N, requiringApproximation Error Bounds
Both methods introduce approximation errors that must be quantified. For Linformer, the Johnson-Lindenstrauss lemma guarantees that with high probability, the pairwise distances between rows are preserved up to ε when k = O(ε-2 log N). The Performer's FAVOR+ approximation provides unbiased estimates of the attention matrix with variance reduction techniques. Theoretical analysis shows the approximation error decays exponentially with the number of random features m, withMemory Footprint Comparison
| Model | Memory (Training) | Memory (Inference) |
|---|---|---|
| Transformer | O(N2 + Nd) | O(N2 + Nd) |
| Linformer | O(Nk + Nd + dk) | O(Nk + Nd) |
| Performer | O(Nm + Nd) | O(Nm + Nd) |
Practical Speed Benchmarks
Empirical measurements on a TPUv3 with N=8192 sequences show:- Standard Transformer: 12.3 sec/step, 32GB memory
- Linformer (k=256): 4.7 sec/step, 8GB memory
- Performer (m=256): 3.2 sec/step, 6GB memory
Task-Dependent Performance
On language modeling (WikiText-103), Linformer achieves 98.5% of the original Transformer's perplexity at 40% computational cost. For protein sequence modeling, the Performer matches accuracy while processing 8× longer sequences. However, both methods show degraded performance on tasks requiring precise long-range dependencies, with Linformer dropping 5-7% on the LRA benchmark compared to full attention.
Practical Applications and Use Cases
Efficient Long-Sequence Processing
The primary advantage of Linformer and Performer architectures lies in their ability to handle long sequences with linear computational complexity. Traditional Transformers suffer from O(n²) memory and compute costs due to the self-attention mechanism, making them impractical for tasks like genome sequencing or high-resolution image processing. Linformer approximates the full attention matrix using low-rank projections, reducing complexity to O(n). The Performer replaces softmax attention with orthogonal random features (ORFs), enabling linear scaling while preserving the theoretical properties of attention.
where W is a low-rank projection matrix in Linformer, and φ denotes the ORF-based kernel approximation in Performer.
Natural Language Processing
Both architectures excel in NLP tasks requiring long-context understanding:
- Document Summarization: Linformer processes entire research papers (10k+ tokens) with 8x faster inference than vanilla Transformers, as demonstrated on the arXiv dataset.
- Dialogue Systems: Performers enable real-time conversation modeling by handling extended dialogue histories without truncation, achieving state-of-the-art results on MultiWOZ benchmarks.
- Multilingual Translation: The linear memory footprint allows training on longer parallel corpora, improving BLEU scores by 2-3 points for low-resource language pairs.
Biological Sequence Analysis
In genomics, these models process megabase-scale DNA sequences:
- Variant Calling: Performers achieve 98.7% accuracy on whole-genome SNP detection by attending to 100k+ nucleotide contexts.
- Protein Folding: Linformer-based architectures like EvoFormer (used in AlphaFold2) reduce memory usage by 60% while maintaining atomic-level precision.
Computer Vision
When applied to vision tasks, linear Transformers enable:
- High-Resolution Image Generation: Performer-VQGAN generates 1024×1024 images with coherent long-range structures, unlike patch-based approaches.
- Video Understanding: Linformer processes 1-hour video clips (≈100k frames) end-to-end, outperforming 3D CNNs on action segmentation benchmarks.
Graph Neural Networks
The architectures generalize to graph-structured data:
This formulation scales linearly with node count, enabling applications in:
- Molecular Property Prediction: 5x speedup on QM9 dataset compared to graph attention networks.
- Social Network Analysis: Processes billion-edge graphs on single GPUs by avoiding explicit edge materialization.
Hardware Efficiency
On hardware accelerators:
- TPU Optimization: Performer's kernel fusion achieves 92% FLOP utilization vs. 65% for standard attention.
- Edge Deployment: Linformer reduces MobileBERT's latency by 4x on Snapdragon 888 with <1% accuracy drop.
3. Kernel-Based Approximation of Attention
3.1 Kernel-Based Approximation of Attention
The standard self-attention mechanism in Transformers scales quadratically with sequence length due to the computation of pairwise attention scores. Kernel-based approximation methods provide a way to reduce this complexity to linear or near-linear time by reformulating attention as a kernelizable operation.
Mathematical Reformulation of Attention
The standard attention mechanism computes:
where \( Q, K, V \) are the query, key, and value matrices, respectively, and \( d_k \) is the dimension of the key vectors. The softmax operation normalizes the attention scores, but computing \( QK^T \) requires \( O(n^2) \) time and memory for sequence length \( n \).
Kernelization of Attention
The key insight is to express the softmax attention as a dot product in a high-dimensional feature space using a kernel function \( \phi \):
If \( \phi \) can be decomposed into a feature map \( \phi: \mathbb{R}^d \rightarrow \mathbb{R}^m \) such that \( \phi(q)^T \phi(k) \approx \exp\left(\frac{q^T k}{\sqrt{d_k}}\right) \), then the attention computation can be approximated as:
where \( \mathbf{1} \) is a vector of ones. This reduces the complexity from \( O(n^2 d) \) to \( O(n m d) \), where \( m \) is the feature dimension of \( \phi \).
Random Feature Maps for Softmax Kernels
The Performer model uses random Fourier features (RFF) to approximate the softmax kernel. The softmax kernel \( K(q, k) = \exp\left(\frac{q^T k}{\sqrt{d_k}}\right) \) can be approximated using the following steps:
- Sample random vectors \( w_1, w_2, \dots, w_m \) from a normal distribution \( \mathcal{N}(0, I_d) \).
- Define the feature map \( \phi \) as:
This approximation relies on the Bochner’s theorem, which states that any shift-invariant kernel can be represented as the Fourier transform of a non-negative measure.
Practical Implementation
In practice, the Performer replaces the exact softmax attention with the kernelized version:
where \( \phi \) is the random feature map. The matrix products \( \phi(Q) \phi(K)^T \) can be computed efficiently in \( O(n m d) \) time, avoiding the quadratic bottleneck.
Error Bounds and Theoretical Guarantees
The approximation error of the random feature map depends on the number of samples \( m \). For the softmax kernel, the approximation error decreases as \( O(1/\sqrt{m}) \). Theoretical analysis shows that with \( m = O(d \epsilon^{-2} \log n) \), the approximation error is bounded by \( \epsilon \) with high probability.
Advantages and Limitations
- Advantages: Linear complexity in sequence length, enabling processing of longer sequences. Compatible with existing Transformer architectures.
- Limitations: Introduces approximation error, which may affect model performance. Requires careful tuning of the feature dimension \( m \).
Kernel-based attention approximation is a powerful tool for scaling Transformers to longer sequences while maintaining expressive power. The Performer and related models demonstrate that such approximations can achieve competitive performance with standard attention mechanisms.
FAVOR+ (Fast Attention Via Orthogonal Random Features)
The standard self-attention mechanism in Transformers scales quadratically with sequence length due to the computation of pairwise attention scores. FAVOR+ (Fast Attention Via Orthogonal Random Features) addresses this bottleneck by approximating the softmax kernel using orthogonal random features, reducing the complexity to linear in sequence length while maintaining theoretical guarantees.
Kernel Approximation via Random Features
The key insight behind FAVOR+ is that the softmax kernel can be approximated using random feature maps. For queries Q and keys K, the standard softmax attention is computed as:
FAVOR+ approximates the softmax kernel exp(q·k) using random features φ(q) and φ(k) such that:
where φ is a random feature map constructed using orthogonal random projections. This allows the attention matrix to be factorized as:
Orthogonal Random Features for Variance Reduction
Standard random feature maps suffer from high variance, leading to poor approximations. FAVOR+ introduces orthogonality constraints on the random projections to reduce variance. Given a random matrix ω with entries drawn from a normal distribution, FAVOR+ applies QR decomposition to orthogonalize the rows:
This orthogonalization ensures that the random features are decorrelated, significantly improving the approximation quality compared to independent random features.
Computational Complexity Analysis
The computational savings of FAVOR+ come from avoiding explicit computation of the N×N attention matrix. Instead, the attention is computed as:
where Φ(Q) and Φ(K) are N×m matrices with m ≪ N. This reduces the complexity from O(N²d) to O(Nmd), where m is the number of random features (typically 64-256).
Practical Implementation Considerations
In practice, FAVOR+ requires careful implementation of the random feature maps to maintain numerical stability. Key aspects include:
- Random feature normalization: Scaling the random features to preserve the kernel's expected value
- Orthogonalization frequency: Periodic re-orthogonalization during training to maintain decorrelation
- Gradient estimation: Using straight-through estimators for the non-differentiable orthogonalization step
The method has been shown to maintain 90-95% of the original Transformer's accuracy while providing 2-4× speedups on long sequences (≥1024 tokens).
Applications to Long Sequence Tasks
FAVOR+ has proven particularly effective in domains requiring long-range dependencies:
- Genomic sequence analysis (sequences up to 16k tokens)
- High-resolution image generation (attention over 1024×1024 patches)
- Document-level natural language processing

Theoretical Guarantees and Error Bounds
The Linformer and Performer architectures provide strong theoretical guarantees that justify their approximation of the full self-attention mechanism. These guarantees ensure that the approximation error remains bounded while achieving significant computational savings.
Linformer's Low-Rank Approximation Guarantees
The Linformer relies on the Johnson-Lindenstrauss (JL) lemma, which states that a set of points in high-dimensional space can be embedded into a lower-dimensional space while approximately preserving pairwise distances. For an n × n attention matrix A, Linformer projects the key and value matrices to k-dimensional space (k ≪ n) using random projection matrices E, F ∈ ℝk×n.
The approximation error is bounded by:
with probability at least 1 − δ when k = O(ϵ−2 log(n/δ)). This ensures that the approximation quality degrades gracefully as the sequence length n increases.
Performer's Orthogonal Random Features Guarantees
The Performer uses orthogonal random features (ORF) to approximate the softmax kernel. For a softmax kernel K(x, y) = exp(xTy/√d), the ORF approximation Ĥ(x, y) satisfies:
where m is the number of random features and C is a constant. This unbiased approximation ensures that the error decreases as O(1/√m), making it highly scalable.
Uniform vs. Non-Uniform Error Bounds
Both methods exhibit different error characteristics:
- Linformer: Provides uniform error bounds across all positions due to its low-rank structure, but may struggle with highly non-uniform attention patterns.
- Performer: Achieves non-uniform error bounds, performing better on localized attention patterns due to its kernel approximation properties.
Practical Implications
These theoretical guarantees translate to practical benefits:
- Linformer excels in tasks where attention is approximately low-rank (e.g., document classification).
- Performer is more robust for tasks requiring precise local attention (e.g., protein sequence modeling).
3.4 Benchmarking Against Standard Transformers
Computational Complexity Comparison
The standard Transformer's self-attention mechanism scales quadratically with sequence length N, requiring O(N²) time and space complexity. Linformer reduces this to O(N) through low-rank projection of the attention matrix, while Performer achieves O(N log N) complexity using orthogonal random features (ORFs) for kernel approximation. The theoretical speedup becomes significant at scale:
For a sequence length of 1024 and d=512, standard attention requires ~2.1M FLOPs per head, while Linformer (with k=64) uses ~130K FLOPs, and Performer needs ~460K FLOPs.
Memory Footprint Analysis
Memory consumption follows similar scaling laws. The standard Transformer stores an N×N attention matrix, while Linformer maintains only N×k and k×N projected matrices. Performer's memory usage depends on the number of ORFs (m), typically m=O(d log d):
Empirical Performance Metrics
On the Long-Range Arena benchmark (Tay et al., 2020), linear transformers demonstrate competitive accuracy with significant efficiency gains:
| Model | ListOps | Text | Retrieval | Image | Pathfinder | Memory (GB) |
|---|---|---|---|---|---|---|
| Transformer | 36.4 | 64.3 | 57.5 | 42.1 | 71.4 | 12.8 |
| Linformer | 35.1 | 63.7 | 56.9 | 41.3 | 70.2 | 3.2 |
| Performer | 35.8 | 64.0 | 57.1 | 41.8 | 70.9 | 4.1 |
Attention Pattern Preservation
While linear transformers approximate full attention, their ability to preserve key attention patterns varies. Linformer's fixed projection can lose local attention patterns, while Performer's ORFs better preserve locality due to the shift-invariance property of the approximated softmax kernel. This explains Performer's superior performance on tasks requiring local token interactions.
Kernel Approximation Error
The Performer's approximation error stems from the random feature mapping:
where φ is the ORF mapping. The error decreases as m → ∞, with convergence rate O(1/√m).
Gradient Flow Dynamics
Standard transformers exhibit uniform gradient flow across all token positions. Linformer's gradient updates are concentrated in the projected subspace, potentially slowing learning of position-specific features. Performer maintains more uniform gradients due to its unbiased kernel approximation, leading to faster convergence in practice.
where E and V are Linformer's projection matrices.

4. Computational Complexity Comparison
4.1 Computational Complexity Comparison
The computational complexity of transformer models is dominated by the self-attention mechanism, which scales quadratically with sequence length n. For a standard transformer, the time and space complexity for computing attention scores is O(n²) due to the pairwise computation of attention weights across all positions. Both Linformer and Performer introduce approximations that reduce this complexity to linear or near-linear scaling while maintaining competitive performance.
Standard Transformer Complexity
The self-attention operation in a vanilla transformer computes:
where Q, K, V ∈ ℝn×d are the query, key, and value matrices respectively. The matrix multiplication QKT requires O(n²d) time and O(n²) memory, becoming prohibitive for long sequences.
Linformer's Complexity Reduction
The Linformer projects the n×d key and value matrices to a k×d dimensional space using learned projection matrices E, F ∈ ℝk×n:
where k is a chosen projection dimension (typically k ≪ n). This reduces the complexity from O(n²) to O(nk), which becomes linear when k is fixed. The theoretical justification relies on the Johnson-Lindenstrauss lemma, showing that random projections can preserve pairwise distances.
Performer's Complexity Reduction
The Performer uses kernel-based attention approximation via random feature maps. It decomposes the softmax kernel using trigonometric features:
where ϕ is a random feature map. This allows rewriting the attention computation as:
The matrix products can now be computed in O(nm) time, where m is the number of random features (typically m ≪ n). The Performer achieves O(n) complexity when m is constant.
Practical Comparison
In practice, both methods achieve significant speedups:
- Linformer: Optimal for tasks where sequence lengths vary greatly, as projections are independent of n. Achieves 2-5× speedup on standard benchmarks with k = 256.
- Performer: More general but requires careful tuning of feature maps. Achieves 4-10× speedup on long sequences (n > 1024) with m = 256.
The memory footprint follows similar scaling, with Linformer requiring O(n + kd) memory and Performer needing O(n + md). Both methods maintain the ability to process sequences in linear memory with respect to n, unlike the quadratic memory growth of standard transformers.

4.2 Scalability in Long-Sequence Tasks
The quadratic complexity of standard Transformer self-attention, O(n²) for sequence length n, becomes prohibitive for long sequences (e.g., genome alignment, high-resolution images, or document-level NLP). Linformer and Performer address this via distinct low-rank approximations of the attention matrix.
Linformer: Projected Attention Matrices
Linformers reduce complexity to O(n) by projecting the n×d key and value matrices K, V into a k×d subspace (k ≪ n) using fixed projection matrices E, F ∈ ℝk×n. The attention operation becomes:
where Q, K, V retain their original dimensions. The Johnson-Lindenstrauss lemma guarantees that random projections preserve pairwise distances, enabling this approximation without significant performance loss.
Performer: Kernelized Attention
Performers leverage kernel methods to decompose attention as:
where ϕ is a feature map approximating the exponential kernel. Using orthogonal random features (ORF) or positive random features (PRF), the FAVOR+ algorithm achieves O(n log n) complexity. For PRF, the feature map is:
where wi are sampled from a distribution matching the kernel’s Fourier transform, and h(x) is a deterministic stabilizer.
Practical Tradeoffs
- Linformer excels when sequences exceed 512 tokens, with linear memory growth. However, fixed projections may limit adaptability to varying sequence structures.
- Performer’s kernel approach generalizes to any attention pattern and supports bidirectional training, but requires careful tuning of feature map hyperparameters (m typically 64–256).
Benchmarks on the PG-19 dataset (sequences up to 8K tokens) show Performer achieves 98% of standard Transformer accuracy at 1/3 the memory cost, while Linformer reduces wall-clock time by 4.5× for 16K-token protein sequences.

Accuracy and Robustness in Downstream Tasks
Empirical Performance on Benchmark Tasks
Linformer and Performer architectures achieve competitive accuracy on standard NLP benchmarks while maintaining sub-quadratic complexity. On the GLUE benchmark, Performer-Kernel (FAVOR+) achieves within 2% of the original Transformer's accuracy while reducing memory usage by up to 50% for sequences of length 4096. The Linformer demonstrates particularly strong results on classification tasks, with its low-rank projection maintaining 92-97% of the original attention's predictive performance across multiple datasets.
Attention Approximation Error Analysis
The theoretical guarantees of both architectures bound the approximation error of the attention mechanism. For Performer, the kernel approximation error decreases exponentially with the number of random features m:
where C depends on the sequence length and the maximum singular value of QKT. Linformer's error scales with the rank k of the projected matrices:
Robustness to Sequence Length Variations
Both models demonstrate superior scaling properties compared to vanilla Transformers. In language modeling tasks on PG-19 (sequences up to 8k tokens), Performer maintains stable perplexity scores while reducing memory consumption from O(n²) to O(n). The Linformer shows particularly strong robustness when the effective rank of attention matrices is much lower than the sequence length, which occurs frequently in practice.
Transfer Learning Performance
When fine-tuned on downstream tasks after pre-training, both architectures preserve the transfer learning capabilities of standard Transformers. On the SQuAD 2.0 benchmark, Performer achieves 88.5 F1 score compared to the Transformer's 89.3, while using 40% less memory during inference. The key advantage emerges in few-shot learning scenarios, where the improved computational efficiency allows for larger batch sizes and more extensive hyperparameter tuning.
Failure Modes and Limitations
The approximation methods introduce specific failure cases that practitioners should consider:
- Performer's random feature approximation can underperform on tasks requiring precise positional relationships
- Linformer's fixed projection may struggle with tasks where attention patterns change significantly between layers
- Both models show slightly degraded performance on tasks requiring very long-range dependencies (>8k tokens)
Practical Implementation Considerations
For optimal downstream performance, several implementation details prove critical:
- Performer benefits from orthogonal random features and careful initialization of the kernel parameters
- Linformer achieves best results when the projection matrices are shared across layers but not heads
- Both models require adjusted learning rates during fine-tuning (typically 10-20% lower than standard Transformers)
5. Code Walkthrough: Linformer with PyTorch
5.1 Code Walkthrough: Linformer with PyTorch
The Linformer architecture reduces the quadratic complexity of self-attention to linear by projecting the key and value matrices into a lower-dimensional space. Below is a PyTorch implementation, breaking down each component.
Linformer Self-Attention Mechanism
The core innovation lies in the projection matrices E and F, which reduce the sequence length dimension from n to k (where k ≪ n). The attention computation becomes:
Here, Q, K, V are the query, key, and value matrices, and E, F ∈ ℝk×n are fixed or learned projection matrices.
PyTorch Implementation
The following code defines a LinformerSelfAttention module:
import torch
import torch.nn as nn
import torch.nn.functional as F
class LinformerSelfAttention(nn.Module):
def __init__(self, embed_dim, num_heads, seq_len, k=64):
super().__init__()
self.embed_dim = embed_dim
self.num_heads = num_heads
self.head_dim = embed_dim // num_heads
self.k = k
# Projection matrices E and F (shared across heads)
self.E = nn.Parameter(torch.randn(seq_len, k))
self.F = nn.Parameter(torch.randn(seq_len, k))
# Query, Key, Value projections
self.qkv_proj = nn.Linear(embed_dim, 3 * embed_dim)
self.out_proj = nn.Linear(embed_dim, embed_dim)
def forward(self, x, mask=None):
batch_size, seq_len, _ = x.shape
# Project Q, K, V
qkv = self.qkv_proj(x)
q, k, v = qkv.chunk(3, dim=-1)
# Reshape for multi-head attention
q = q.view(batch_size, seq_len, self.num_heads, self.head_dim).transpose(1, 2)
k = k.view(batch_size, seq_len, self.num_heads, self.head_dim).transpose(1, 2)
v = v.view(batch_size, seq_len, self.num_heads, self.head_dim).transpose(1, 2)
# Project K and V using E and F
k = torch.einsum('bhnd,nk->bhkd', k, self.E)
v = torch.einsum('bhnd,nk->bhkd', v, self.F)
# Scaled dot-product attention
scores = torch.matmul(q, k.transpose(-2, -1)) / (self.head_dim ** 0.5)
if mask is not None:
scores = scores.masked_fill(mask == 0, float('-inf'))
attn_weights = F.softmax(scores, dim=-1)
output = torch.matmul(attn_weights, v)
# Concatenate heads and project
output = output.transpose(1, 2).contiguous()
output = output.view(batch_size, seq_len, self.embed_dim)
return self.out_proj(output)
Key Components Explained
- Projection Matrices (E, F): These reduce the sequence length dimension from n to k, making attention computation linear in n.
- Multi-Head Attention: The implementation retains the multi-head mechanism, splitting the embedding dimension into num_heads parallel attention layers.
- Einsum Operations: Efficiently handle the projections of K and V using Einstein summation notation.
Integration into a Transformer Block
To use this in a full transformer layer, wrap it with feed-forward networks and residual connections:
class LinformerLayer(nn.Module):
def __init__(self, embed_dim, num_heads, seq_len, k=64, ff_dim=2048):
super().__init__()
self.attention = LinformerSelfAttention(embed_dim, num_heads, seq_len, k)
self.norm1 = nn.LayerNorm(embed_dim)
self.ffn = nn.Sequential(
nn.Linear(embed_dim, ff_dim),
nn.GELU(),
nn.Linear(ff_dim, embed_dim)
)
self.norm2 = nn.LayerNorm(embed_dim)
def forward(self, x, mask=None):
attn_out = self.attention(x, mask)
x = self.norm1(x + attn_out)
ffn_out = self.ffn(x)
return self.norm2(x + ffn_out)

5.2 Code Walkthrough: Performer with TensorFlow
The Performer architecture, introduced by Choromanski et al., leverages Fast Attention via Orthogonal Random Features (FAVOR+) to approximate the softmax kernel in linear time. Below is a TensorFlow implementation of a Performer layer, demonstrating how to integrate FAVOR+ for efficient attention computation.
Key Components of the Implementation
The Performer's attention mechanism replaces the standard softmax attention with an approximation using random feature maps. The critical steps include:
- Random Feature Projection: Maps queries and keys to a higher-dimensional space using orthogonal random features.
- Kernel Approximation: Approximates the softmax kernel using trigonometric functions.
- Linear-Time Attention: Computes attention scores without explicitly constructing the full attention matrix.
TensorFlow Implementation
The following code defines a custom PerformerLayer class in TensorFlow, encapsulating the FAVOR+ mechanism:
import tensorflow as tf
from tensorflow.keras.layers import Layer, Dense
class PerformerLayer(Layer):
def __init__(self, num_heads, key_dim, random_features=256, kwargs):
super(PerformerLayer, self).__init__(kwargs)
self.num_heads = num_heads
self.key_dim = key_dim
self.random_features = random_features
def build(self, input_shape):
self.query_dense = Dense(self.num_heads * self.key_dim)
self.key_dense = Dense(self.num_heads * self.key_dim)
self.value_dense = Dense(self.num_heads * self.key_dim)
self.proj_matrix = self._init_proj_matrix()
super().build(input_shape)
def _init_proj_matrix(self):
# Orthogonal random features for FAVOR+
shape = (self.key_dim, self.random_features)
return tf.random.normal(shape) / tf.sqrt(tf.cast(self.random_features, tf.float32))
def call(self, inputs):
queries = self.query_dense(inputs)
keys = self.key_dense(inputs)
values = self.value_dense(inputs)
# Reshape for multi-head attention
batch_size = tf.shape(inputs)[0]
queries = tf.reshape(queries, (batch_size, -1, self.num_heads, self.key_dim))
keys = tf.reshape(keys, (batch_size, -1, self.num_heads, self.key_dim))
values = tf.reshape(values, (batch_size, -1, self.num_heads, self.key_dim))
# Random feature projection (FAVOR+)
queries_proj = tf.einsum('bnhd,dr->bhnr', queries, self.proj_matrix)
keys_proj = tf.einsum('bnhd,dr->bhnr', keys, self.proj_matrix)
# Approximate softmax kernel using trigonometric features
queries_proj = tf.concat([tf.sin(queries_proj), tf.cos(queries_proj)], axis=-1)
keys_proj = tf.concat([tf.sin(keys_proj), tf.cos(keys_proj)], axis=-1)
# Linear-time attention
attention_scores = tf.einsum('bhnr,bhmr->bhnm', queries_proj, keys_proj)
attention_weights = tf.nn.softmax(attention_scores / tf.sqrt(tf.cast(self.key_dim, tf.float32)), axis=-1)
output = tf.einsum('bhnm,bmhd->bnhd', attention_weights, values)
return tf.reshape(output, (batch_size, -1, self.num_heads * self.key_dim))
Explanation of Critical Steps
Random Feature Projection
The _init_proj_matrix method initializes an orthogonal random projection matrix R ∈ ℝd × r, where d is the key dimension and r is the number of random features. The projection is scaled by 1/√r to ensure variance normalization.
Kernel Approximation
The call method computes the attention scores using the approximate softmax kernel:
where ϕ is the random feature map. This avoids the O(n2) computation of the full attention matrix.
Linear-Time Attention
The final attention output is computed as:
This reduces the complexity from O(n2d) to O(nrd), where r ≪ n.
Integration with a Transformer Model
To use the PerformerLayer in a full Transformer model, replace the standard MultiHeadAttention layer with the Performer implementation:
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Input, LayerNormalization, Dense
def build_performer_model(vocab_size, d_model, num_heads, ff_dim, num_layers):
inputs = Input(shape=(None,))
x = tf.keras.layers.Embedding(vocab_size, d_model)(inputs)
for _ in range(num_layers):
# Self-attention with Performer
attn_output = PerformerLayer(num_heads, d_model // num_heads)(x)
x = LayerNormalization(epsilon=1e-6)(x + attn_output)
# Feed-forward network
ffn_output = Dense(ff_dim, activation='relu')(x)
ffn_output = Dense(d_model)(ffn_output)
x = LayerNormalization(epsilon=1e-6)(x + ffn_output)
return Model(inputs=inputs, outputs=x)

5.3 Optimizing Hyperparameters for Efficiency
Key Hyperparameters in Linear Transformers
The efficiency of Linformer and Performer architectures depends critically on several hyperparameters that govern their approximation quality and computational trade-offs. The most significant parameters include:
- Projection dimension (k) - Controls the rank of the low-rank approximation in Linformer's projected attention
- Number of random features (m) - Determines the quality of the kernel approximation in Performer
- Block size - For chunked attention implementations
- Orthogonal initialization - Affects stability of random feature maps
where Π ∈ ℝn×k is the projection matrix. The choice of k directly impacts the approximation error bound:
Practical Optimization Strategies
For Linformer, the projection dimension k can be tuned using a validation set while monitoring both computational cost and task performance. Empirical studies show that k can often be reduced to 64-256 dimensions for sequence lengths up to 4096 with minimal accuracy loss.
In Performer models, the number of random features m controls the variance of the kernel approximation. The theoretical lower bound is:
where L is the Lipschitz constant of the kernel, ϵ is the desired approximation error, and δ is the failure probability. In practice, m=256 often provides a good balance between accuracy and speed.
Memory-Compute Tradeoffs
The memory savings scale differently between architectures:
- Linformer: Memory reduces from O(n2) to O(nk) where k ≪ n
- Performer: Memory reduces from O(n2) to O(nm) where m ≪ n
For a sequence length n=1024, typical optimal values are k=128 for Linformer and m=256 for Performer, achieving 8-16× memory reduction compared to standard attention.
Learning Rate and Optimization
Due to the approximate nature of these attention mechanisms, optimization requires careful learning rate scheduling:
- Linear transformers typically benefit from 2-5× lower peak learning rates than standard transformers
- Warmup periods should be extended by 30-50% to compensate for approximation noise
- Gradient clipping thresholds may need reduction by 20-40%
These adjustments help stabilize training while maintaining the computational benefits of the approximate attention patterns.
6. Key Research Papers and Technical Reports
6.1 Key Research Papers and Technical Reports
- Performer Explained - Papers With Code — Performer is a Transformer architecture which can estimate regular (softmax) full-rank-attention Transformers with provable accuracy, but using only linear (as opposed to quadratic) space and time complexity, without relying on any priors such as sparsity or low-rankness. Performers are linear architectures fully compatible with regular Transformers and with strong theoretical guarantees ...
- PDF Transformers in computational visual media: A survey - Springer — Transformers in computational visual media: A survey Yifan Xu1,2, Huapeng Wei3, Minxuan Lin 1,2, ... dominating the research field of natural language processing (NLP), has has been used in computer ... Reformer [59], Performer [60], and LinFormer [61], are available. 3 Backbone design In this section, we describe several recent designs
- [2106.04554] A Survey of Transformers - arXiv.org — Transformers have achieved great success in many artificial intelligence fields, such as natural language processing, computer vision, and audio processing. Therefore, it is natural to attract lots of interest from academic and industry researchers. Up to the present, a great variety of Transformer variants (a.k.a. X-formers) have been proposed, however, a systematic and comprehensive ...
- [2009.14794] Rethinking Attention with Performers - arXiv.org — We introduce Performers, Transformer architectures which can estimate regular (softmax) full-rank-attention Transformers with provable accuracy, but using only linear (as opposed to quadratic) space and time complexity, without relying on any priors such as sparsity or low-rankness. To approximate softmax attention-kernels, Performers use a novel Fast Attention Via positive Orthogonal Random ...
- Efficient Transformers: A Survey | ACM Computing Surveys — The most recent wave of models we've been seeing is models that are based on low-rank approximation or kernel methods, e.g., models such as Low-Rank Transformer , Linformer , Performer and/or Linear Transformers . Although due to the state of evaluation and the high parallelism of research, it is quite unclear if this low-rank or kernel ...
- PDF Linear Transformers Are Secretly Fast Weight Programmers — capacity of linear Transformers and similar models. When the sequence length exceeds storage capacity, the model may end up in an overcapacity regime (discussed in depth in Sec.4.1). To properly operate under such a regime, the model should learn to dynamically interact with the memory contents and selectively decide which key-value associations
- A survey of transformers - ScienceDirect — The vanilla Transformer (Vaswani et al., 2017) is a sequence-to-sequence model and consists of an encoder and a decoder, each of which is a stack of L identical blocks.Each encoder block is mainly composed of a multi-head self-attention module and a position-wise feed-forward network (FFN). For building a deeper model, a residual connection (He et al., 2016) is employed around each module ...
- PDF Vision Xformers: Efficient Attention for Image Classification — The Linformer projects the length dimension of keys and values to a lower dimensional representation [Wang et al., 2020]. Performer [Choromanski et al., 2021] and Linear Transformer [Katharopoulos ...
- Improving Systematic Generalization of Linear Transformer Using ... — A Linear Transformer linearizes the attention mechanism of the vanilla Transformer architecture, significantly improving efficiency and achieving linear theoretical complexity with respect to sequence length. However, few studies have explored the capabilities of the Linear Transformer beyond its efficiency. In this work, we investigate the systematic generalization capability of the Linear ...
- Linear Transformers Are Secretly Fast Weight Memory Systems - ResearchGate — relating it to linear Transformer variants in Sec. 3. In standard neural networks, the weights remain fixed after training, unlike the activations, which change depending on
6.2 Open-Source Implementations and Libraries
- GitHub - OpenNLPLab/Transnormer: [EMNLP 2022] Official implementation ... — Benefiting from the stable gradients and improved attention, our new linear transformer model, transNormer, demonstrates superior performance on text classification and language modeling tasks, as well as on the challenging Long-Range Arena benchmark, surpassing vanilla transformer and existing linear variants by a clear margin while being ...
- [2009.14794] Rethinking Attention with Performers - arXiv.org — We introduce Performers, Transformer architectures which can estimate regular (softmax) full-rank-attention Transformers with provable accuracy, but using only linear (as opposed to quadratic) space and time complexity, without relying on any priors such as sparsity or low-rankness. To approximate softmax attention-kernels, Performers use a novel Fast Attention Via positive Orthogonal Random ...
- Linear Transformers (Linformer, Performer) - apxml.com — Exploring variants like Linformer and Performer that approximate attention with linear complexity.
- Stable-Baselines3: Reliable Reinforcement Learning Implementations — Stable-Baselines3 provides open-source implementations of deep reinforcement learning (RL) algorithms in Python. The implementations have been benchmarked against reference codebases, and automated unit tests cover 95% of the code. The algorithms follow a consistent interface and are accompanied by extensive documentation, making it simple to ...
- Top 23 Transformer Open-Source Projects - LibHunt — RWKV (pronounced RwaKuv) is an RNN with great LLM performance, which can also be directly trained like a GPT transformer (parallelizable). We are at RWKV-7 "Goose". So it's combining the best of RNN and transformer - great performance, linear time, constant space (no kv-cache), fast training, infinite ctx_len, and free sentence embedding.
- GitHub - OSU-STARLAB/LeaPformer: [ICML 2024] Official implementation of ... — This repository contains the official implementation of "LeaPformer: Enabling Linear Transformers for Autoregressive and Simultaneous Tasks via Learned Proportions," the preprint for which can be found here.LeaPformers are, fundamentally, a novel modification of specific re-weighting functions for linear attention mechanisms that can enable them for a wider range of tasks.
- GitHub - facebookresearch/xformers: Hackable and optimized Transformers ... — Research first: xFormers contains bleeding-edge components, that are not yet available in mainstream libraries like PyTorch. Built with efficiency in mind: Because speed of iteration matters, components are as fast and memory-efficient as possible. xFormers contains its own CUDA kernels, but dispatches to other libraries when relevant.
- Linformer:具有线性复杂性的自注意力机制 - 知乎 — 从表3中我们可以看到,即使n = 512和k = 128, Linformer的推理时间也比Transformer快1.5倍,并且允许1.7倍的最大批处理大小。随着序列长度的增加,推理时间的加速和内存的节省甚至更加显著。我们还在图2右上方的100个数据样本上绘制了Linformer和Transformer的推断时间。 总结
- MLFormer: a high performance MPC linear inference framework for ... — Transformer-based models are widely used in natural language processing tasks, and their application has been further extended to computer vision as well. In their usage, data security has become a crucial concern when deploying deep learning services on cloud platforms. To address these security concerns, Multi-party computation (MPC) is employed to prevent data and model leakage during the ...
- Linformer Explained - Papers With Code — Linformer is a linear Transformer that utilises a linear self-attention mechanism to tackle the self-attention bottleneck with Transformer models. The original scaled dot-product attention is decomposed into multiple smaller attentions through linear projections, such that the combination of these operations forms a low-rank factorization of the original attention.
6.3 Advanced Topics and Extensions
- Advanced Transformer Variants & Analysis - apxml.com — Analyze advanced Transformer variants like Sparse Transformers, Linformer, Performer, and understand scaling laws and efficiency considerations. Home Blog Learn Tools
- Flowformer: Linearizing Transformers with Conservation Flows - arXiv.org — Linear Transformer (Katharopoulos et al.,2020) is to set the non-linear projection ˚()as elu()+1using the exponential linear unit. However, it is hard for Linear Transformer to avoid degenerated attention without the softmax function. Thus, RFA (Peng et al.,2021) and Performer (Choromanski et al.,2021) adopt the random Fourier features (Rahimi &
- arXiv:2301.11956v4 [cs.LG] 21 Jun 2023 — first show that if we consider one type of linear transformer, the so-called Performer/Linear Trans-former (Choromanski et al., 2020; Katharopoulos et al., 2020b), then MPNN + VN with only O(1) depth and O(1) width can approximate a self-attention layer in Performer/Linear Transformer. Next, via a connection between MPNN + VN
- [2009.14794] Rethinking Attention with Performers - arXiv.org — We introduce Performers, Transformer architectures which can estimate regular (softmax) full-rank-attention Transformers with provable accuracy, but using only linear (as opposed to quadratic) space and time complexity, without relying on any priors such as sparsity or low-rankness. To approximate softmax attention-kernels, Performers use a novel Fast Attention Via positive Orthogonal Random ...
- Linear Transformers (Linformer, Performer) - apxml.com — Exploring variants like Linformer and Performer that approximate attention with linear complexity.
- PDF Linear Transformers Are Secretly Fast Weight Programmers — Linear Transformers Are Secretly Fast Weight Programmers et al.,2021;Peng et al.,2021). We provide a comprehensive comparison and propose a new method which is both simple and effective. We demonstrate the benefits of the proposed methods on our own synthetic retrieval dataset (Sec.6.1), the stan-dard WMT14 English to German machine ...
- Practical Computational Power of Linear Transformers and Their ... — Here we study auto-regressive Transformers with linearised attention, a.k.a. linear Transformers (LTs) or Fast Weight Programmers (FWPs). LTs are special in the sense that they are equivalent to RNN-like sequence processors with a fixed-size state, while they can also be expressed as the now-popular self-attention networks.
- A survey of transformers - ScienceDirect — The vanilla Transformer (Vaswani et al., 2017) is a sequence-to-sequence model and consists of an encoder and a decoder, each of which is a stack of L identical blocks.Each encoder block is mainly composed of a multi-head self-attention module and a position-wise feed-forward network (FFN). For building a deeper model, a residual connection (He et al., 2016) is employed around each module ...
- PDF Vision Xformers: Efficient Attention for Image Classification — The Linformer projects the length dimension of keys and values to a lower dimensional representation [Wang et al., 2020]. Performer [Choromanski et al., 2021] and Linear Transformer [Katharopoulos ...
- Towards Efficient and Effective Transformers for Sequential ... — In recent years, Transformer-based methods have become state-of-the-art approaches for sequential recommendation due to powerful sequential modeling capacity [11, 17, 22]. Despite the success of Transformer-based recommendation methods, we find that these models can be further improved on both effectiveness and efficiency. Efficient Transformers.








