TL;DR
Standard Transformers choke on long sequences because their attention mechanism scales quadratically, while faster "linear" alternatives sacrifice accuracy by relying on rigid mathematical approximations. Flexformer bridges this gap by turning the frequencies inside Fourier-based linear attention into trainable parameters, letting the model sculpt its own optimal attention kernel directly from data. The result is a model with linear time and memory scaling that cannot only match standard softmax attention—it can actually beat it.
What Problem Does This Paper Address?
At the heart of every Transformer is the softmax attention mechanism, where every token looks at every other token to figure out what matters. If your sequence has $N$ tokens, building that $N \times N$ comparison grid costs $\mathcal{O}(N^2)$ in both time and memory. Double your text length, quadruple your GPU bill.
To escape this quadratic trap, researchers developed kernel-based linear attention. Instead of computing pairwise similarities before multiplying by values, you pass queries ($Q$) and keys ($K$) through a feature map $\phi(\cdot)$ and change the order of matrix multiplication: compute $\phi(K)^\top V$ first, then multiply by $\phi(Q)$. Boom—linear $\mathcal{O}(N)$ scaling.
Here’s the catch: what should $\phi$ be? Early methods like Performer used Random Fourier Features (RFF)—sampling random frequencies from a fixed Gaussian distribution—to approximate standard softmax attention. Not only is this approximation noisy, it assumes softmax is the undisputed king of attention patterns in the first place. Newer methods tried making kernels partially learnable using empirically chosen "spiky" functions, but lacked theoretical guarantees of expressiveness. How do you build a linear attention kernel flexible enough to learn any useful similarity pattern directly from data?
Methods
Flexformer’s core trick comes from classical harmonic analysis. Bochner’s theorem tells us that any well-behaved stationary kernel (one where similarity only depends on the difference $x - y$) is just the Fourier transform of a non-negative spectral density—essentially a distribution of frequencies.
While standard RFF methods approximate softmax by freezing those frequencies as random draws from a Gaussian, Flexformer asks: why not learn the frequencies via backpropagation?
The authors decompose the softmax kernel into vector-norm scaling terms (which keep attention sharp and "spiky") and a similarity kernel, introducing two variants:
- $\text{Flexformer}_\text{s}$ (Stationary): Replaces fixed random frequencies $\{\omega_i\}$ with end-to-end trainable frequency parameters. Learning the frequencies is mathematically equivalent to learning the optimal stationary kernel from data.
- $\text{Flexformer}_\text{n}$ (Nonstationary): Stationary kernels only care about the distance $x - y$, which can be limiting. Invoking Yaglom’s theorem (the generalization of Bochner's theorem to nonstationary kernels), the authors introduce pairs of learnable frequencies $\{(\omega_{1i}, \omega_{2i})\}$ alongside a learnable temperature parameter $\tau$ for the norm scaling. This allows similarity to depend on the exact positions of $x$ and $y$ in feature space, not just their difference.
Datasets
The authors evaluate Flexformer across standard benchmarks—no new datasets are introduced:
- Long Range Arena (LRA): Five long-context classification tasks (sequences of 1K to 4K tokens), including ListOps, byte-level Text classification, Document Retrieval, pixel-by-pixel Image classification, and Pathfinder.
- WikiText-103: Used for autoregressive language modeling at two scales (41M and 247M parameters).
- GLUE Benchmark: Eight natural language understanding tasks (CoLA, SST-2, MRPC, STS-B, QQP, MNLI, QNLI, RTE) used to test softmax distillation and cross-domain transfer with a pretrained 125M RoBERTa model.
Key Insights and Ideas
- Softmax is a floor, not a ceiling: Because a Gaussian distribution of frequencies is a valid state inside Flexformer's parameter space, its kernel family strictly contains the unbiased softmax estimator. At worst, it can mimic softmax; at best, it discovers task-specific patterns that work even better.
- Learnable kernels love scale: In language modeling, as the model grew from 41M to 247M parameters, Flexformer went from nearly tying the vanilla Transformer to decisively beating it—suggesting larger models are better equipped to exploit the extra expressiveness of learnable spectral features.
- Drop-in retrofitting works—and transfers: You don't have to train from scratch. By distilling attention weights from a pretrained RoBERTa model, Flexformer recovers softmax performance. Better yet, an attention kernel distilled on one dataset (like QQP) transfers cleanly when fine-tuned on completely different tasks (like CoLA), proving the learned kernels capture generalizable linguistic structure rather than overfitting.
Key Results
- Long-Sequence Classification (LRA): $\text{Flexformer}_\text{n}$ achieves the highest average accuracy at 59.99%, beating the vanilla Transformer (56.55%) and showing a 4.4% relative improvement over the strongest linear baseline, Hedgehog (57.47%).
- Speed & Memory: On LRA Document Retrieval ($N = 4000$), $\text{Flexformer}_\text{n}$ delivers a 2.6$\times$ training speedup and an 84% reduction in GPU memory (3.4 GB vs. 21.2 GB) compared to a standard Transformer.
- Language Modeling (WikiText-103): On the 247M-parameter setup, $\text{Flexformer}_\text{n}$ achieves a test perplexity of 22.3, outperforming both Hedgehog (24.2) and the vanilla softmax Transformer (23.8).
- Distillation & Transfer (GLUE): Distilled into RoBERTa, $\text{Flexformer}_\text{n}$ recovers 97.9% of the original model's average score (83.7 vs. 85.5), obliterating unlearnable RFF replacements like Performer (63.3), and actually beats standard RoBERTa on CoLA (63.3 vs. 61.9). In cross-domain kernel transfer, it beats Hedgehog across all eight GLUE tasks.
Limitations and Future Work
Flexformer’s mathematical foundation—both Bochner's and Yaglom's theorems—relies on the assumption that the learned attention kernel is positive definite. However, the authors candidly note there is no theoretical proof that positive definite kernels are optimal for attention mechanisms. Exploring whether relaxing this assumption unlocks even better attention dynamics—and establishing rigorous theory around it—remains an open question for future work.
Summary updated