Spectraformer: A Unified Random Feature Framework for Transformer

## Abstract

Abstract Linearization of attention using various kernel approximation and kernel learning techniques has shown promise. Past methods use a subset of combinations of component functions and weight matrices within the random features paradigm. We identify the need for a systematic comparison of different combinations of weight matrices and component functions for attention learning in Transformer. In this work, we introduce Spectraformer , a unified framework for approximating and learning the kernel function in linearized attention of the Transformer. We experiment with broad classes of component functions and weight matrices for three textual tasks in the LRA benchmark. Our empirical findings indicate that different kernels are good at different tasks and that kernel choice is fundamental to performant models. Our code is available at: https://anonymous.4open.science/r/spectraformer-8A97 .

## 1 Introduction

Transformer (Vaswani et al., ) has revolutionized the landscape of natural language processing (NLP) and forms the basis of almost all state-of-the-art language models. Its influence has reached beyond NLP into computer vision (Han et al., ), speech processing (Lin et al., ) and other fields. Compared to its precursors like the LSTMs, Transformer leverages parallelism since it is fully based on the attention mechanism. Therefore, the performance of Transformer is tied to the effective use of attention. Attention in the Original Transformer (referred to as ‘OT’ hereafter) uses the softmax function which is quadratic in time complexity. However, softmax is not fundamental to attention. The softmax serves the role of a compatibility function, which captures the degree of compatibility between two tokens (see Equations 1 and 2). Extensive experiments with other compatibility functions show that the softmax is not the only possibility, and that the ‘best’ compatibility function depends on the task (Tsai et al., ).

Replacing the softmax formulation with alternatives may allow us to reduce the time complexity of attention computation. In this paper, we focus on the subset of alternatives that employs kernel functions. Kernel functions have been historically used in machine learning algorithms to simplify the estimation of parameters (Hofmann et al., ). Tsai et al. () show that attention can be formulated using kernels and then be linearized (Katharopoulos et al., ), given the feature map of the respective attention kernel. Random feature-based algorithms (Liu et al., ) are a family of algorithms, inspired by spectral analysis, which provides the feature map associated with a kernel. A random feature consists of three components: component function, weight matrix, and component weight (see Section 3). Due to their robustness and flexibility, random features give rise to ‘random feature Transformers’, the family of Transformers with linearized attention via random features (hereby abbreviated as RF), first with the work by Choromanski et al. (). This is seen to lead to three strands of research: (A) Optimization of weight matrices either by making the matrices have lower time or space complexity, or by incorporating beneficial properties like orthogonality to reduce variance (Yu et al., ; Choromanski et al., ; ; Reid et al., ); (B) Enhancement of component functions by either: (i) finding a function with a tighter variance or approximation bound, or (ii) be engineered to output with numerical stability and be bounded (Choromanski et al., ; Likhosherstov et al., ; ); (C) Parameterization of weight matrices to perform kernel learning instead of kernel approximation (Chowdhury et al., ). (A) and (B) seek to improve approximation quality via approximation error and variance reduction. (C) dispenses with the approximation of the attention kernel (typically the softmax), and makes the kernel itself learnable, to attain a better performance. However, the three have been explored separately, creating a situation where there are different ways to construct the random features. Additionally, past works only explore certain combinations of component functions and weight matrices, creating disjoint overlaps and gaps in the literature. Finally, the weight matrices are often those explicitly designed in the context of kernelized attention in the Transformer (Yu et al., ; Choromanski et al., ; ; Reid et al., ), ignoring those which are popular in the kernel methods literature (e.g., Liu et al. ()). These limitations necessitate a unified framework that allows the comparison of different combinations in search of the best-performing one.

We introduce Spectraformer, a unified framework of random feature-based attention in Transformer. It utilizes spectral analysis-inspired random features to construct linearized attention in Transformers. Spectraformer allows for the experimentation of various combinations of weight matrices and component functions. We will open-source our codebase upon acceptance. Through our benchmarking experiments on three textual Long Range Arena (Tay et al., ) tasks and 18 combinations of weight matrices and component functions, Spectraformer enables novel combinations of component functions and weight matrices from the kernel literature to be experimented easily in the Transformer setting. This process makes possible the discovery of combinations that outperform existing random feature-based Transformers in terms of performance, training time, and peak memory consumption. Various novel Spectraformer combinations are more accurate than the previous SOTA models, SADERF-ORF (a.k.a., FAVOR# (Likhosherstov et al., )), OPRF-ORF (a.k.a., FAVOR++ (Likhosherstov et al., )), PosRF-ORF (a.k.a., FAVOR+ (Choromanski et al., )), whilst significantly reducing training time and memory consumption over both. We hope that Spectraformer will enable the mechanism to benchmark more random feature-based Transformers for these and other tasks. Our contributions are:

- 1.
Our novel framework, Spectraformer, which generalizes over all past works in linearized attention with kernel approximation and kernel learning.
- 2.
We demonstrate how Spectraformer can discover novel SOTA models in the random feature Transformer family for the LRA benchmark and that these models are more performant than existing random feature Transformers.
- 3.
We empirically identify the relationship between performant combinations and task characteristics, aiding the right kernel choice when tackling a task.
- 4.
Our code is currently available for review, but they will be publicly released upon acceptance. We detail guide on how to incorporate future kernel works into Spectraformer. With this work, every future kernel work that is introduced can easily be incorporated into the transformer.

## 2 Preliminaries

We first cover the theoretical foundations to this work. A list of commonly used acronyms is available in Table 1.

**Table 1: Table of acronyms**
| Acronym | Description |
| --- | --- |
| OT (Vaswani et al., ) | Original Transformer |
| RF (Rahimi & Recht, ) | Random Feature method |
| RBF | Radial Basis Function kernel (also called the Gaussian kernel) |
| TrigRF (Rahimi & Recht, ) | Trigonometric RF (component function) |
| PosRF (Choromanski et al., ) | Positive RF (component function) |
| OPRF (Likhosherstov et al., ) | Optimized positive RF (component function) |
| SADERF (Likhosherstov et al., ) | Simplified Asymmetric Dense-Exponential RF (component function) |
| ORF (Yu et al., ) | Orthogonal RF (weight matrix) |
| SORF (Yu et al., ) | Structural ORF (weight matrix) |
| QMC (Avron et al., ) | Quasi- Monte Carlo (weight matrix) |
| MM (Shen et al., ) (Liu et al., ) | Moment Matching (weight matrix) |
| SGQ (Dao et al., ) | Sparse Grid Quadrature (weight matrix) |
| FastFoodL(Yang et al., ) | FastFood Learnable (weight matrix) |

### 2.1 Kernelized Attention

OT attention (Vaswani et al., ) can be defined using the unified attention model (Galassi et al., ) as follows. Given a collection of inputs and a learning objective, attention mechanism learns the attention matrix A which captures the associative information between pairs of tokens in a sequence of length $N$, with each row $\textbf{A}_{i}$ being the representation of a token $i$ with every other token $j\in[1,N]$. This is done by creating three learnable representations of each token $i$ called query ($\bm{q}_{i}$), key ($\bm{k}_{i}$), and value ($\bm{v}_{i}$). The attention representation $\textbf{A}_{i}$ is calculated as a weighted sum of value $\bm{v}_{j}$ given attention weights $a_{j}$. The general equation for $\textbf{A}_{i}$ is given as:

$$ $\begin{array}[]{cccc}\bm{A}_{i}=\sum_{j=1}^{N}\frac{f(\bm{q}_{i},\bm{k}_{j})}{ \sum_{l}f(\bm{q}_{i},\bm{k}_{l})}\bm{v}_{j}=\sum_{j=1}^{N}a_{j}\bm{v}_{j};&a=g (e);&e=f(\bm{q}_{i},\bm{k}_{j})\end{array}$ (1) $$

Attention weights are generated using a distribution function $g$ applied on an energy score $e$. The energy score is captured by applying a compatibility function on a query $\bm{q}_{i}$ and a set of keys K, in order to compute a score of a specific relationship between the pairs. The attention in the OT is called the ‘scaled dot-product’ which defines the compatibility function $f$ and the distribution function $g$ as follows:

$$ $\begin{array}[]{cc}f(\bm{q}_{i},\textbf{K})=\frac{\bm{q}_{i}^{T}\textbf{K}}{ \sqrt{d_{k}}}&g(\bm{x}_{j})=\frac{\exp(\bm{x}_{j})}{\sum_{l}\exp(\bm{x}_{l})} \end{array}$ (2) $$

We can then have the attention matrix (with $\exp(.)$ being applied element-wise) as:

$$ $\begin{array}[]{cc}\bm{A}_{i}=\sum_{j=1}^{N}\frac{\exp(\bm{q}_{i}\bm{k}_{j}^{T }/\sqrt{d_{k}})}{\sum_{l}\exp(\bm{q}_{i}\bm{k}_{l}^{T}/\sqrt{d_{k}})}\bm{v}_{j }&A=softmax(\frac{\bm{QK}^{T}}{\sqrt{d_{k}}})\bm{V}\end{array}$ (3) $$

with $\bm{Q},\bm{K},\bm{V}$ being the matrix of queries, keys, and values respectively with each row being $\bm{q}_{i},\bm{k}_{i},\bm{v}_{i}$.

We now discuss the intrinsic connection between kernel and attention. With the compatibility function $f$ as a kernel $f(\bm{q}_{i},\bm{k}_{j})=\mathcal{K}(\bm{q}_{i},\bm{k}_{j})$, Equation 1 can be rewritten as:

$$ $\bm{A}_{i}=\sum_{j=1}^{N}\frac{\mathcal{K}(\bm{q}_{i},\bm{k}_{j})}{\sum_{l} \mathcal{K}(\bm{q}_{i},\bm{k}_{l})}\bm{v}_{j}$ (4) $$

As shown in Tsai et al. (), this is indeed the Nadaraya-Watson Kernel Estimator, with $\mathbb{E}_{p(\bm{k}_{j}|\bm{q}_{i})}[\bm{v}_{j}|X=\bm{k}_{j}]=\bm{A}_{i}$, $l_{i}\left(\bm{k}_{j})=p(\bm{k}_{j}|\bm{q}_{i}\right)$, $Y_{i}=\bm{v}_{j}$:

$$ $\begin{array}[]{ccc}\mathbb{E}_{l_{i}}[Y_{i}|X=\bm{u}]=\sum_{i=1}^{N}l_{i}(\bm {u})Y_{i};&l_{i}(\bm{u})=\frac{\mathcal{K}(\frac{\bm{u}-\bm{x}_{i}}{h})}{\sum_ {j=1}^{n}\mathcal{K}(\frac{\bm{u}-\bm{x}_{j}}{h})}\end{array}$ (5) $$

Specifically, the scaled dot-product attention in the OT in Equation 3 corresponds to the softmax kernel (see Equation 6).

$$ $\mathcal{K}_{softmax}(\bm{q}_{i},\bm{k}_{j})=\exp(\bm{q}_{i}^{T}\bm{k}_{j}/ \sqrt{d_{k}})$ (6) $$

### 2.2 Linearizing Kernelized Attention via Random Features

Since a kernel is the inner product of two vectors in some space with respect to some feature map $\phi$, i.e., $\mathcal{K}(\bm{x},\bm{y})=\phi(\bm{x})\phi(\bm{y})^{T}$, then we can rewrite Equation 4 as:

$$ $\begin{array}[]{c}\bm{A}_{i}=\frac{\sum_{j}\phi(\bm{q}_{i})\phi(\bm{k}_{j})^{T }\bm{v}_{j}}{\sum_{l}\phi(\bm{q}_{i})\phi(\bm{k}_{l})^{T}}=\frac{\phi(\bm{q}_{ i})\sum_{j}\phi(\bm{k}_{j})\otimes\bm{v}_{j}}{\phi(\bm{q}_{i})\sum_{l}\phi(\bm {k}_{l})^{T}}\end{array}$ (7) $$

If we pre-compute $\sum_{j}\phi(\bm{k}_{j})\otimes\bm{v}_{j}$ and $\sum_{l}\phi(\bm{k}_{l})^{T}$, the entire term becomes $O(1)$ and we only need to compute Equation 7 $N$ times for each $i$, resulting in an attention matrix calculated in linear time $O(N)$. We now refer to Equation 7 as linearized attention (Katharopoulos et al., ).

All that is left is to retrieve the feature map $\phi$, the process of which is called kernel approximation (see Section 2.3), which corresponds to the kernels we specify. There are many popular kernels from which we can choose, but typically, we want to approximate the softmax kernel in Equation 6. However, since the softmax relates to the $\mathcal{K}_{RBF}(\bm{x},\bm{y})=\exp(-\frac{||\bm{x}-\bm{y}||^{2}}{2})$ via the following equation:

$$ $\mathcal{K}_{softmax}(\bm{x},\bm{y})=\exp(\frac{||\bm{x}||^{2}}{2})\mathcal{K} _{RBF}(\bm{x},\bm{y})\exp(\frac{||\bm{y}||^{2}}{2})$ (8) $$

Approximating the RBF is easier than the softmax directly in the method we introduce in Section 2.3, hence we approximate the softmax indirectly via the RBF in Equation 8.

Oftentimes, however, we might not want to specify a kernel, since kernel choice is indeed a hyperparameter and the softmax is by no means essential or necessary as shown in Tsai et al. (). Then kernel learning would be a more suitable option, the detail of which is discussed in Section 2.4. Notwithstanding, we first introduce the main kernel approximation technique: random features.

### 2.3 Random Features for Kernel Approximation

Of the kernel approximation family of methods, the most successful and appropriate for linearized attention in Transformer has been the random features approach (Rahimi & Recht, ). Estimating kernels using random features relies on a fundamental insight from harmonic analysis called Bochner’s theorem (Rudin, ) stated as follows:

A continuous shift-invariant kernel $\mathcal{K}(\bm{x},\bm{y})=\mathcal{K}(\bm{x}-\bm{y})$ on $\mathbb{R}^{d}$ is positive definite if and only if $\mathcal{K}(\bm{\delta})$ is the Fourier transform of a non-negative measure $p(\bm{\omega})$. If $\mathcal{K}(\bm{\delta})$ is properly scaled, that is $\mathcal{K}(0)=1$ then the measure $p(\bm{\omega})$ is a proper probability distribution.

inline]define unambiguously the dimension of x and phi here, also the dimension before and after approximation of the features map

$$ $\mathcal{K}(\bm{\delta})=\int p(\bm{\omega})\exp(i\bm{\omega\delta})\mathrm{d} \bm{\omega}$ (9) $$

When approximating the integration in this equation via Monte Carlo sampling with $s$ samples, $\bm{\omega}\sim p(\bm{\omega})$, we can obtain the feature map:

$$ $\begin{array}[]{ll}\mathcal{K}(\bm{\delta})&=\mathbb{E}[\phi_{\bm{\omega}}(\bm {x})^{T}\phi_{\bm{\omega}}(\bm{y})]\approx<\phi_{\bm{\omega}}(\bm{x}),\phi_{ \bm{\omega}}(\bm{y})>\\ \phi_{\bm{\omega}}(\bm{x})\!&=\!\frac{1}{\sqrt{s}}\big{[}\exp(-\mbox{i}{\bm{ \omega}}^{\!\top}_{1}\bm{x}),\ldots,\exp(-\mbox{i}{\bm{\omega}}^{\!\top}_{s} \bm{x})]^{\!\top}\end{array}$ (10) $$

### 2.4 Random Features for Kernel Learning

Previous random feature techniques are robust in approximating kernels for specific learning problems. However, kernels may be chosen using heuristics and by convention from a popular subset. The kernel is very much a hyperparameter. This could pose a problem since there is no free lunch in learning as much as in kernel choice (Schölkopf & Smola, ). Tsai et al. () has shown experimental results that this is also true in the context of attention in Transformer. Hence, instead of picking a kernel for the attention, we could learn the kernel via kernel learning an established kernel methods technique. Kernel learning has shown to be effective in general (Tompkins et al., ; Wilson & Adams, ; Oliva et al., ) and in the case of attention via Gaussian Mixture Model (GMM), learnable weight matrices (FastFood), deep generative model (DGM) (Chowdhury et al., ). Kernel learning is achieved by making the weight matrix $\bm{W}$ learnable where $\bm{\omega}_{i}$ is the output of a function parameterizing $p(\bm{\omega})$.

The kernelized attention via random features discussed here is a popular approach, however it is by no means the only one. A more detailed discussion is offered in related work in Section A.2. Specifically, Section A.2.1 provides alternative kernelized attention formulations and Section A.2.2 provides alternative kernel approximations to random features.

## 3 Spectraformer

Spectraformer is a unified random feature framework that allows for the combination of any weight matrix with any component function to be used as kernelized attention in Transformer. The general equation for random feature is shown in Equation 11, generalized from Equation 10, based on Liu et al. (); Choromanski et al. ().

$$ $\begin{array}[]{rl}\phi_{\bm{\omega}}(\bm{x})=&\frac{1}{\sqrt{s}}\big{[}a_{1}f _{1}(\bm{\omega}_{1},\bm{x}),...,a_{s}f_{1}(\bm{\omega}_{s},\bm{x}),...,\\ &a_{1}f_{l}(\bm{\omega}_{1},\bm{x}),...,a_{s}f_{l}(\bm{\omega}_{s},\bm{x})]^{ \!\top}\\ \bm{W}=&[\bm{\omega}_{1},...,\bm{\omega}_{s}]^{T}\in\mathbb{R}^{s*d}\end{array}$ (11) $$

Spectraformer is shown in Figure 1. The attention of the OT (on the left-hand side) is replaced by the attention of the Spectraformer which pre-computes the Hadamard (element-wise) product of values $\bm{v}$ and transformed keys $\phi(\bm{k})$ scaled by $\phi(\bm{k})$, the product of which is multiplied with the transformed queries $\phi(\bm{q})$. The linear map $\phi$ is composed from weight matrices and component functions. The linear map has $s$ components with $a$ being the component weight (see Equation 11). The component weight $a$ can either be fixed (typically at $a=1$) or learnable as proposed by various kernel works (Liu et al., ). Due to the fact that the majority of random feature Transformers (e.g., Likhosherstov et al. ()) have $a=1$, we follow this setting for consistent comparison between combinations.

The figure and the equation together highlight the three strands of research as mentioned previously in Section 1: (A) Weight matrix approximator: constructing $\bm{W}$ more effectively; (B) Weight matrix learner: parameterizing $\bm{W}$ instead of sampling; (C) Component function: constructing $f_{j}$ more effectively.

### 3.1 Weight Matrices

In Spectraformer, the weight matrix $\bm{W}$ which is either an approximator (from the families of (e.g., ORF and SORF (Yu et al., )), Unit-cube (e.g., QMC (Avron et al., ), MM (Shen et al., )), Quadrature (e.g., SGQ (Dao et al., )). or a learner (FastFoodL (Chowdhury et al., )).

Figure: Figure 1: Spectraformer framework based on Equation 11
Refer to caption: extracted/5906227/resource/spectraformer.png

We term the matrix $\bm{W}$ as weight matrix instead of random matrix since although $\bm{W}$ acts like a random matrix ($\bm{\omega}_{i}\sim p(.)$, see Equation 10), it is not guaranteed to be one. In the case of weight matrix approximator, they approximate a random matrix and in the case of weight matrix learner, they parameterize a weight matrix, thereby implicitly learning a distribution $p(.)$ associated with such $\bm{W}$. The weight matrix is given in Definition 3.1.

###### Definition 3.1 .

Provided that a matrix can substitute for a random matrix in Equation 11, and the solution for this equation given such matrix and $f$ as TrigRF being the solution for Equation 9, i.e., as an unbiased estimator of $\mathcal{K}$, and TrigRF being $f_{1}=f_{l}=\exp(-i\bm{\omega}^{T}\bm{x})$ (see Equation 10) then such a matrix is a weight matrix.

It follows from Definition 3.1 that for any weight matrix $\bm{W}$, $f=TrigRF$ is a valid component function. Spectraformer enables weight matrix approximation in terms of three families based on how they solve the intractable integral in Equation 9:

- •
Monte Carlo sampling-based weight matrix involves approximating the integral of Equation 9 using Monte Carlo sampling with the solution given by Equation 10 (Rahimi & Recht, ) with $\bm{W}$ being the ‘Base’ random matrix of $p(.)$, $\bm{\omega}_{i}\sim p(.)$. The random matrix $\bm{W}$ can be constructed more efficiently (either reducing time or space complexity) with FastFood (Le et al., ). The random matrix $\bm{W}$ can also be enforced to form geometrical couplings such as orthogonality to reduce approximation error with ORF, SORF (Yu et al., ). Both these methods produce valid random matrices.
- •
Unit-cube sampling-based weight matrix transforms Equation 9 to an integral on the unit cube $[0,1]^{d}$, then performs the approximation with uniform and independent points, thus reducing variance. The first approach is QMC (Avron et al., ), where $\bm{W}:\bm{\omega}_{i}=\Phi^{-1}(\bm{t}_{i})$, $\Phi$ being the cumulative distribution function (CDF) associated with $p(.)$ and $\bm{t}_{i}$ being a low discrepancy sequence. $\Phi^{-1}(\bm{t}_{i})\sim p(.)$ whilst having a lower variance than direct sampling from $p(.)$. MM (Shen et al., ; Liu et al., ) improves over QMC by replacing $\Phi^{-1}$ with a moment matching scheme $\widetilde{\Phi}^{-1}$ on $\Phi^{-1}$.
- •
Quadrature-based weight matrix uses quadrature rules with non-uniform deterministic weights. The main approach explored is SGQ (Dao et al., ) which uses one-dimensional Gaussian quadrature rule by assuming $\mathcal{K}$ factorizes with respect to the dimensions. Smolyak rule is further implemented to alleviate the curse of dimensionality.

Liu et al. () show that in various kernel benchmark datasets (among combination with TrigRF), QMC and ORF have consistently high performance. Due to the significant number of weight matrices in random features literature, we decide to cover only the most prominent ones. Appendix A.3 provides the technical details of the approximators discussed here and those that we left out.

Spectraformer also allows to learn weight matrices by learning $p(.)$ via parameterizing $\bm{W}$. Yang et al. () introduce several of these parametrization schemes, which are then adapted into the attention setting by Chowdhury et al. (). The best performing method is via making the constituent matrices of FastFood learnable, which we term FastFoodL. In general, a weight matrix learner is valid as long as it can implicitly model any distribution. In addition the TrigRF, PosRF has also been applied on FastFoodL. Technical details and other learners we have not covered are explored in Appendix A.4.
We now explore existing works which have developed from Rahimi & Recht () (see Equation 9, 10) and how they lead to the development of Spectraformer.

###### Definition 3.1 .

### 3.2 Component functions

Spectraformer incorporates component functions $f$ such as PosRF, OPRF, SADERF. Component functions in Spectraformer combine each weight matrix row $\bm{\omega}_{i}$ with the input $\bm{x}$. The base case of the component function is TrigRF. Component function is given in Definition 3.2

###### Definition 3.2 .

A function of $\bm{x}$ with respect to $\bm{\omega}$ is a valid component function if and only if $\mathcal{K}\approx\phi_{\bm{\omega}}(\bm{x})^{T}\phi_{\bm{\omega}}(\bm{y})$ given that $\bm{\omega}_{i}\sim p(.)$ in Equation 10.

It follows that all component functions have provided theoretical guarantees given that $\bm{W}$ is a random matrix. In addition, PosRF, OPRF, and SADERF have also been proven theoretically to approximate $\mathcal{K}_{RBF}$ given $\bm{W}$ being the ORF.

While TrigRF(Rahimi & Recht, ) is the base component function and guarantees $\mathcal{K}(\bm{\delta})$ to be close to $\phi_{TrigRF}(\bm{x})\cdot\phi_{TrigRF}(\bm{y})$ due to uniform convergence, it is typically evaluated using only the real component of $\exp(-i\bm{\omega}_{j}^{T}\bm{x})$. This leads to $\cos$ and $\sin$ functions which can produce unstable behavior, especially when $K(\bm{\delta})$ is close to 0, where variance is infinite (Choromanski et al., ; Likhosherstov et al., ). Therefore, TrigRF is avoided in Spectraformer.

- •
PosRF (Choromanski et al., ) is introduced in Spectraformer as a simple modification over TrigRF to counter this instability. PosRF guarantees positive function output and its variance approaching 0 as $K(\bm{\delta})$ approaches 0. Likhosherstov et al. () generalize TrigRF and PosRF to GERF.
- •
OPRF (Likhosherstov et al., ) is one of the solution to the reduction of the variance equation of GERF. OPRF is both positive and bounded (whereas PosRF is positive and unbounded). Likhosherstov et al. () generalize GERF to dense matrix instead of scalar parameters leading to DERF.
- •
SADERF (Likhosherstov et al., ) is one of the solutions to the reduction of the variance equation of DERF. Whilst DERF has many solutions, they all rely on operations which are incompatible with many deep learning libraries on GPU and TPU like SVD and eigen decompositions. SADERF is an extension of GERF with a tighter variance bound on DERF.

The alternatives for component functions allow Spectraformer to approximate the RBF with $p(.)$ corresponding to the Gaussian. Likhosherstov et al. () and Likhosherstov et al. () show that SADERF-ORF is the SOTA combination followed by OPRF-ORF and PosRF-ORF. Technical details and other component function(s) not covered are explored in Appendix A.5.

**Table 2: Summary of research gap in kernelized attention. Rows are the component functions, columns are the weight matrices. For each cell, the top item is the cited work for this combination for typical kernel tasks, while the bottom item is the cited work for Transformer-based tasks. $\times$ indicates no prior work for the combination. Spectraformer explores all combinations listed here, excluding ‘Base’ due to exhaustive work as seen in the table.**
|  |  | TrigRF | PosRF | OPRF | SADERF |
| --- | --- | --- | --- | --- | --- |
| Monte Carlo | Base | (Rahimi & Recht, ) | (Choromanski et al., ) | (Likhosherstov et al., ) | (Likhosherstov et al., ) |
|  |  | (Choromanski et al., ) | (Choromanski et al., ) | (Likhosherstov et al., ) | (Likhosherstov et al., ) |
|  | ORF | (Yu et al., ) | (Choromanski et al., ) | (Likhosherstov et al., ) | (Likhosherstov et al., ) |
|  |  | (Choromanski et al., ) | (Choromanski et al., ) | (Likhosherstov et al., ) | (Likhosherstov et al., ) |
|  | SORF | (Yu et al., ) | $\times$ | $\times$ | $\times$ |
|  |  | $\times$ | $\times$ | $\times$ | $\times$ |
| Unit-cube | QMC | (Avron et al., ) | $\times$ | $\times$ | $\times$ |
|  |  | $\times$ | $\times$ | $\times$ | $\times$ |
|  | MM | (Shen et al., ) | $\times$ | $\times$ | $\times$ |
|  |  | $\times$ | $\times$ | $\times$ | $\times$ |
| Quadrature | SGQ | (Dao et al., ) | $\times$ | $\times$ | $\times$ |
|  |  | $\times$ | $\times$ | $\times$ | $\times$ |
| Learner | FastFoodL | (Yang et al., ) | $\times$ | $\times$ | $\times$ |
|  |  | (Chowdhury et al., ) | (Chowdhury et al., ) | $\times$ | $\times$ |

###### Definition 3.2 .

### 3.3 Utility

There has been an unsystematic attempt at combining component functions and weight matrices. On one hand, most component functions have had theoretical and experimental results in combining with Base and ORF. On the other hand, most weight matrices have had results in combining with TrigRF or PosRF. This means that a lot of potential combinations have not been studied previously. Table 2 shows the wide research gap across 14 combinations which have never been studied in either the kernel or Transformer setting. Spectraformer highlights this research gap. Excluding TrigRF due to its instability and Base due to its under-performance, we conduct experiments on 18 combinations.

Spectraformer is a generic random feature framework that allows for the combination of any weight matrix with any component function provided that the weight matrix satisfies Definition 3.1 and the component function satisfies Definition 3.2. We note that Spectraformer is an experimental rather than a theoretical framework. Due to the large number of weight matrices and component functions, it is infeasible for the time being to derive a general mathematical proof for the bounds and convergence of Spectraformer. Therefore, we seek to validate its feasibility and show its ability to discover novel and performant combination via experimental results.

### 3.4 Adding New Code to Spectraformer

We now discuss how to add a new component function or weight matrix to the base code for further implementation. New component functions need to satisfy Definition 3.2 and new weight matrices need to satisfy Definition 3.1.

#### 3.4.1 Component Function

- 1.
Add the new component function f to src/models/component_functions.py, the arguments should include data (the input), and other optional parameters.
- 2.
Import f and add a new entry to FastAttention.comp_functions[f_name] = f in src/models/attention_performer.py (line 176)

#### 3.4.2 Weight Matrix

- 1.
Add the new weight matrix w to src/models/weight_matrix_approx.py or src/models/weight_matrix_learner.py, the arguments should include nb_rows (number of rows), nb_cols (number of columns) and device.
- 2.
Import w and add the if clause and $w$ function call in src/models/attention_performer.py (line 208)

## 4 Results

### 4.1 Experiment Details

**Table 3: Experimental results of Spectraformer variants on the LRA benchmark, over five seeds. We report mean accuracy (on the test set), mean time (training time (hour)), and mean memory (peak memory consumption (GB)). $L$: ListOps, $T$: Text, $R$: Retrieval, and $\mu$ for the average of the three. Entries are ranked by $\mu$ accuracy. The previous RF SOTAs (SADERF-ORF (Likhosherstov et al., ) and OPRF-ORF (Likhosherstov et al., )) are in bold. We note that via Spectraformer, we find various variants outperforming these previous RF SOTAs in terms of accuracy, with different combinations offering different trade-off between accuracy, time, and memory. Entries are sorted by mean accuracy.**
|  | Accuracy (%) $\uparrow$ | Time (hour) $\downarrow$ | Memory (GB) $\downarrow$ |  |  |  |  |  |  |  |  |  |
| --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- |
|  | L | T | R | $\mu$ | L | T | R | $\mu$ | L | T | R | $\mu$ |
| OPRF-FastFoodL | 37.55 (0.48) | 64.41 (0.62) | 77.70 (0.33) | 59.89 | 1.07 | 2.07 | 2.12 | 1.75 | 0.86 | 1.72 | 1.68 | 1.42 |
| OPRF-MM | 38.08 (0.53) | 60.40 (0.85) | 81.09 (0.18) | 59.86 | 0.68 | 1.26 | 1.24 | 1.06 | 1.36 | 2.71 | 2.56 | 2.21 |
| PosRF-MM | 37.06 (0.37) | 61.87 (1.79) | 80.58 (0.53) | 59.84 | 0.56 | 1.05 | 1.06 | 0.89 | 1.17 | 2.31 | 2.10 | 1.86 |
| OPRF-ORF | 38.34 (0.22) | 60.16 (0.79) | 80.88 (0.17) | 59.80 | 0.68 | 1.26 | 1.25 | 1.06 | 1.36 | 2.71 | 2.56 | 2.21 |
| SADERF-QMC | 37.37 (0.38) | 61.14 (1.48) | 80.84 (0.11) | 59.78 | 0.68 | 1.25 | 1.29 | 1.07 | 1.44 | 2.86 | 2.69 | 2.33 |
| PosRF-QMC | 37.11 (0.09) | 61.69 (0.96) | 80.55 (0.13) | 59.78 | 0.56 | 1.05 | 1.05 | 0.89 | 1.17 | 2.31 | 2.10 | 1.86 |
| SADERF-MM | 37.10 (0.22) | 60.68 (1.88) | 81.13 (0.17) | 59.64 | 0.68 | 1.25 | 1.29 | 1.07 | 1.44 | 2.86 | 2.69 | 2.33 |
| SADERF-ORF | 37.10 (0.19) | 60.39 (2.08) | 81.05 (0.22) | 59.51 | 0.68 | 1.25 | 1.28 | 1.07 | 1.44 | 2.86 | 2.69 | 2.33 |
| OPRF-QMC | 37.69 (0.62) | 59.94 (0.59) | 80.38 (0.49) | 59.34 | 0.68 | 1.26 | 1.26 | 1.07 | 1.36 | 2.71 | 2.56 | 2.21 |
| SADERF-SGQ | 37.11 (0.21) | 62.46 (0.54) | 78.38 (0.25) | 59.32 | 0.68 | 1.25 | 1.27 | 1.07 | 1.44 | 2.86 | 2.69 | 2.33 |
| SADERF-FastFoodL | 36.02 (1.38) | 64.63 (0.18) | 76.99 (0.61) | 59.21 | 1.07 | 2.08 | 2.16 | 1.77 | 0.92 | 1.84 | 1.80 | 1.52 |
| OPRF-SGQ | 37.10 (0.23) | 61.25 (0.54) | 78.69 (0.54) | 59.01 | 0.67 | 1.26 | 1.25 | 1.06 | 1.36 | 2.71 | 2.56 | 2.21 |
| PosRF-ORF | 34.35 (5.96) | 60.30 (0.97) | 80.45 (0.22) | 58.37 | 0.56 | 1.05 | 1.06 | 0.89 | 1.17 | 2.31 | 2.10 | 1.86 |
| PosRF-FastFoodL | 33.46 (3.70) | 64.65 (0.36) | 76.95 (0.48) | 58.35 | 1.02 | 1.98 | 2.03 | 1.68 | 0.79 | 1.57 | 1.53 | 1.30 |
| SADERF-SORF | 33.30 (0.98) | 64.70 (0.36) | 74.71 (1.90) | 57.57 | 0.68 | 1.24 | 1.28 | 1.07 | 1.44 | 2.86 | 2.69 | 2.33 |
| PosRF-SGQ | 28.64 (7.54) | 62.38 (0.53) | 78.28 (0.20) | 56.43 | 0.56 | 1.05 | 1.05 | 0.89 | 1.17 | 2.31 | 2.10 | 1.86 |
| OPRF-SORF | 27.91 (3.26) | 64.76 (0.66) | 75.92 (1.74) | 56.20 | 0.67 | 1.26 | 1.24 | 1.06 | 1.36 | 2.71 | 2.56 | 2.21 |
| PosRF-SORF | 21.27 (6.65) | 62.99 (0.40) | 67.10 (1.11) | 50.45 | 0.56 | 1.05 | 1.05 | 0.89 | 1.17 | 2.31 | 2.10 | 1.86 |

The LRA covers tasks of different sequence lengths, difficulty, and objective and it is designed to evaluate efficient Transformers, of which Spectraformer belongs to. Specifically, we evaluate the models on three textual LRA tasks: ListOps (Nangia & Bowman, ), Text (Byte-Level Text Classification, using the IMDb review dataset) (Howard & Ruder, ), and Retrieval (Byte-Level Document Retrieval, using the ACL Anthology Network dataset) (Radev et al., ). We discuss the task characteristics in greater details in Section 4.2. All our models are experimented on NVIDIA V100 and 12 CPU with 20GB of memory. Our codebase is based on and includes the adapted or original implementation from Chen et al. (); Chowdhury et al. (); Liu et al. (); Choromanski et al. (); Likhosherstov et al. (; ); Wang et al. (); Xiong et al. (), Kitaev et al. (); Thomas et al. (); Zhou et al. () (Apache License, Version 2.0).

We run our experiments on 5 different seeds due to computational limitation. The parameters are identical to Chen et al. () and chosen to limit the parameter count to account for the marked training time of multiple models. All the parameters for Spectraformer variants are kept identical for fair comparison, with the only difference being their random feature component. Our code is implemented in Python 3.12 and Pytorch, we use the Transformer base from Chen et al. (). Our hyperparameters are available in Table 8. The model name convention is [component function]-[weight matrix] (e.g., OPRF-FastFoodL).

Our result in Table 3 shows that the top mean accuracy models, ranked in descending order, are: OPRF-FastFoodL, OPRF-MM, PosRF-MM, OPRF-ORF, SADERF-QMC. Our mean accuracy result indicates that novel Spectraformer combinations have outperformed the highlighted previous SOTA, i.e., OPRF-ORF and SADERF-ORF. However, we are aware that mean accuracy is insufficient in offering a comprehensive picture. Therefore, we first proceed with analyzing the statistics of the tasks before analyzing the significance of these results.

### 4.2 Task Characteristics

We first analyze the characteristics of the three textual classification LRA (Tay et al., ) tasks: ListOps (Nangia & Bowman, ), Text (Byte-Level Text Classification, using the IMDb review dataset) (Howard & Ruder, ), and Retrieval (Byte-Level Document Retrieval, using the ACL Anthology Network dataset) (Radev et al., ). We take into consideration three main characteristics:

- •
Number of output classes. ListOps is a task which inputs a sequence containing brackets and simple mathematical operators, with the output being a integer between 1 and 10, hence it has 10 classes. Text is a sentiment classification task on an IMDb review dataset with the class being either negative or positive, hence it has 2 classes. Retrieval inputs two sequences and the output is either False (meaning the sequences are not of the same document) or True (meaning the sequences are of the same document).
- •
Maximum sequence size. ListOps is 2K; Text is 4K; whilst Retrieval requires the processing of two sequences each of 4K length making the total maximum sequence size being 8K.
- •
Required attention span. This is the ‘mean distance between the query token and the attended tokens, scaled by attention weights’. The average values for required attention span calculated by Tay et al. () is: ListOps being 0.8, Text being 0.3, and Retrieval being 1.3.

**Table 4: Mean Pearson correlation between model performance statistics and each task characteristic across weight matrices and component functions. Model performance statistics include ‘acc’ (accuracy), ‘time’ (training time), ‘mem’ (peak memory consumption). Task characteristics are ‘size’ (maximum sequence size), ‘span’ (required attention span), and ‘classes’ (number of output classes). Correlation is calculated for each weight matrix or component function and the average value over weight matrix or component function respectively displayed. Left is weight matrix $W$, right is component function $f$.**
| $W$ | acc | time | mem |
| --- | --- | --- | --- |
| size | 0.93 | 0.74 | 0.64 |
| span | 0.37 | 0.02 | -0.10 |
| classes | -0.91 | -0.96 | -0.94 |

We then calculate Pearson correlation of accuracy, training time, and memory consumption against these characteristics. Our result is shown in Table 4. The table indicates that: (1) contrary to intuition, longer sequence size gives higher accuracy but this is not because of increasing required attention span and they somewhat increase training time and peak memory consumption; (2) increasing the number of classes decreases accuracy, training time, and peak memory consumption.

### 4.3 Analysis of Combinations

**Table 5: Mean statistics across weight matrices for each task. Task is denoted on the top left cell of each table. Mean statistics include ‘acc’ (accuracy (%)), ‘time’ (training time (hour)), and ‘mem’ (peak memory consumption (GB)).**
| ListOps | acc | time | mem |
| --- | --- | --- | --- |
| SORF | 27.49 | 0.64 | 1.32 |
| SGQ | 34.28 | 0.64 | 1.32 |
| FastFoodL | 35.68 | 1.05 | 0.86 |
| ORF | 36.59 | 0.64 | 1.32 |
| QMC | 37.39 | 0.64 | 1.32 |
| MM | 37.41 | 0.64 | 1.32 |

We now explore the suitability of weight matrices and component functions depending on task characteristics. Table 5 shows the mean statistics across weight matrices for each task. We notice that weight matrices form two different groups which align with the required attention span differences. ListOps and Retrieval are tasks with high required attention span, ListOps being 0.8 and Retrieval being 1.3. In both these tasks, we observe that ORF, QMC, and MM perform comparatively against each other and a lot better against SORF, SGQ, and FastFoodL. However, the Text task, which has a much lower required attention span of 0.3, has much higher performance from the SORF-SGQ-FastFoodL group, whilst ORF, QMC, and MM perform much poorly on this task. Based on this observation, we can conclude that ORF, QMC, and MM are suited for high required attention span; whilst SORF, SGQ, and FastFoodL are suited for low required attention span. Analyzing mean statistics per task across component functions do not show any task-specific trend (refer to Table 9 for more information).

**Table 6: Summary statistics of component functions $f$ (top) and weight matrices $W$ (bottom) in mean accuracy (on the test set), mean time (training time (hour)), and mean memory (peak memory consumption (GB)). $f$ and $W$ entries are sorted by mean accuracy respectively.**
|  |  | Accuracy (%) $\uparrow$ | Time (hour) $\downarrow$ | Memory (GB) $\downarrow$ |
| --- | --- | --- | --- | --- |
| $f$ | SADERF | 59.17 | 1.19 | 2.19 |
| $f$ | OPRF | 59.02 | 1.18 | 2.08 |
| $f$ | PosRF | 57.20 | 1.02 | 1.77 |
| $W$ | MM | 59.78 | 1.01 | 2.13 |
| $W$ | QMC | 59.63 | 1.01 | 2.13 |
| $W$ | ORF | 59.22 | 1.01 | 2.13 |
| $W$ | FastFoodL | 59.15 | 1.73 | 1.41 |
| $W$ | SGQ | 58.25 | 1.01 | 2.13 |
| $W$ | SORF | 54.74 | 1.00 | 2.13 |

In analyzing efficiency, we follow the definition of efficient Transformers in Tay et al. () in terms of memory and computation, the latter interpreted as computational cost. The overall trend of accuracy, time, and memory of component functions is that they increase linearly from PosRF, OPRF, to SADERF (see Table 6). Hence, when limited computational resource is a concern, PosRF is a better option. Training time and peak memory consumption relies more heavily on component functions than weight matrices, with the exception of FastFoodL. FastFoodL takes significantly less memory consumption at the expense of training time. The accuracy trend for weight matrices is as described above. Table 6 also shows that whilst FastFoodL generally does not work as well due to its low required attention span, its high performance in the task of Text which is low in required attention span and the anomalously performant OPRF-FastFoodL leads to a data skew in the final ranking in Table 3.

Comparing between combinations, we find that for ListOps, OPRF-ORF is the most accurate (38.34%), followed by OPRF-MM (38.08%) and OPRF-QMC (37.69%). OPRF-ORF, being the previous SOTA, is undoubtedly the best choice here since other models maintain the same training time and peak memory consumption with lower accuracy. For Text, the most accurate model is OPRF-SORF (64.76%), followed by SADERF-SORF (64.70%) and PosRF-FastFoodL (64.65%). These all outperform the previous SOTAs (OPRF-ORF and SADERF-ORF) by $4\%+$ whilst maintaining a diversity of training time and peak memory consumption options. Both OPRF-SORF and SADERF-SORF maintain the same level of training time and peak memory consumption as the SOTAs, PosRF-FastFoodL offers much less peak memory consumption at the expense of longer training time. For Retrieval, the most accurate model is SADERF-MM (81.13%), followed by OPRF-MM (81.09%) and SADERF-ORF (81.05%). Whilst these models only perform slightly better than the SOTA SADERF-ORF, OPRF-MM offers less peak memory consumption compared to SADERF-ORF.

## 5 Conclusion

We present Spectraformer, a framework for approximating and learning the attention kernel in the Transformer. Our paper generalizes past works and presents empirical findings on different component function and weight matrix combinations. We experiment with 18 combinations, of which 14 are novel. Novel combinations perform competitively against, if not more performant than the previous SOTA in the family for LRA textual tasks. We show empirically that the choice of performant weight matrix depends on the required attention span of the task. Our results and the extensibility of our work shows the viability of Spectraformer as a unifying random feature framework of Transformer.

## Appendix A Appendix

### A.1 Future work, limitations, and broader impact

Spectraformer provides an experimental framework for random features in the Transformer. However, we have not provided formal mathematical proofs for the bounds the these combinations due to the number of combinations discussed. We hope Spectraformer inspires future work to generalizes the proof in such a framework.

Spectraformer has covered a significant number of combinations, however, we have not covered all weight matrices and component functions in the literature. Spectraformer can serve as a foundation for further random feature Transformer research using the techiques not yet experimented with but covered in Appendices A.3, A.4, A.5.

Due to the significant number of experimented models, we are unable to perform hyper-parameter fine-tuning. Future work can benefit from extending the scope of the current research to other tasks, i.e., other non-textual LRA tasks (image classification, path-finder, and path-x) and short-context datasets/ tasks such as GLUE (Linzen et al., ). Transformer-based architecture can also benefit from Spectraformer. Past random feature Transformer works (Likhosherstov et al., ; ) have experimented with conformer and vision Transformer with ViT. Jeevan & Sethi () provides a good foundation to extend our work. Spectraformer, although currently experimented in the Transformer setting, can also extend to non-parametric kernel classification. It could also benefit from ablation studies like the comparison variance in Chowdhury et al. () or in Yao et al. ()

Given the complex nature of language models in the ethical, energy, and societal domains, our work should be used responsibly (Weidinger et al., ).s

### A.2 Related Work

Spectraformer is based on several theoretical assumptions and choice of mathematical framework and approach. This section discusses alternatives to the choices of Spectraformer. Specifically, Appendix A.2.1 discusses alternative kernelized formulations to Section 2.1; Appendix A.2.2 discusses alternative kernel approximation techniques to random features in Section A.2.2. There are also some component functions and weight matrices which are not covered in the experiments. We introduce them in the related work section in Appendices A.3, A.4, A.5.

#### A.2.1 Alternative Kernelized Attentions

In addition to the kernelized formulation of attention as shown in Section 2.1, based on our literature survey, we have identified the following three alternative kernel-based formulations of attention.

Nadaraya-Watson kernel estimator in integration form (Nguyen et al., ): Given the Gaussian kernel, where the probability of a single variable and the joint probability of two variables are estimated using kernel density estimation with the Gaussian kernel, then we have Equation 12, leading to the formulation of FourierFormer.

$$ $\begin{array}[]{ll}\phi(\bm{\delta})&=exp(-||\bm{\delta}||^{2}/2\sigma^{2})\\ \hat{r}_{n}(\bm{k})&=\int\frac{\bm{v}p(\bm{v},\bm{k})}{p(\bm{k})}dv=\frac{\sum _{j=1}^{N}\hat{\bm{v}}_{j}\phi(\bm{k}-\bm{k}_{j})}{\sum_{j=1}^{N}\phi(\bm{k}- \bm{k}_{j})}\\ \hat{r}_{n}(\bm{q}_{i})&=\sum_{j=1}^{N}softmax(\bm{q}_{i}^{T}\bm{k}_{j}/\sigma ^{2})\hat{\bm{v}}_{j}\end{array}$ (12) $$

Gaussian decomposition (Song et al., ): the attention mechanism can also be decomposed into the Gaussian kernel directly from Equation 4 without appealing to the use of kernel estimator (Song et al., ). Equation 13 leads to Implicit Kernel Attention (IKA).

$$ $A_{i}=\sum_{j=1}^{N}\frac{1}{Z_{l}(\bm{q}_{i},\bm{K})}exp(\frac{-||\bm{q}_{i}- \bm{k}_{j}||^{2}_{2}}{2\sqrt{d_{k}}})exp(\frac{||\bm{q}_{i}||^{2}_{p=2}+||\bm{ k}_{j}||^{2}_{p=2}}{2\sqrt{d_{k}}})\bm{v}_{j}$ (13) $$

Non-local operation (Wang et al., ) is popular decomposition of attention in computer vision. This is the basis for Vision Transformer and related work. Non-local operation itself a generalisation of the non-local means used in image denoising (Buades et al., ). Non-local operation is essentially an operation that operates on the entire input range, instead of a specific window which is characteristic of convolutional operations (see Equation 14). Indeed, the attention vector output $\bm{y}$ can be defined using $f$ which is the softmax (see Equation 15) in the non-local operation.

$$ $\mathbf{\bm{y}}_{i}=\frac{1}{C(\bm{x})}\sum_{\forall j}f(\bm{x}_{i},\bm{x}_{j} )g(\bm{x}_{j})$ (14) $$

$$ $\mathbf{\bm{y}}=softmax(\bm{x}^{T}\bm{W}_{\theta}^{T}\bm{W}_{\theta}\bm{x})g( \bm{x})$ (15) $$

We will show that the non-local operation in Equation 14 is a generalisation of the Nadaraya-Watson kernel estimator. Specifically, when defining $C(\bm{x})=\sum_{l}\mathcal{K}(\bm{q}_{i},\bm{k}_{l}),f=\mathcal{K}(\bm{q}_{i},
\bm{k}_{l}),g=\bm{v}_{j}$, then we obtain the original Nadaraya-Watson kernel estimator in Equation 4. The variety of kernel decompositions of attention demonstrate the robustness of the kernel interpretation of attention and hence opening up an alley in attention improvement through the lens of kernel.

#### A.2.2 Kernel Approximation Beyond Random Features

Kernel approximation is presented in Section 2.3 through the use of random features. However, there has been other kernel approximation techniques outside of random features. These include greedy basis selection (Smola & Schökopf, ), divide and conquer (Hsieh et al., ; Zhang et al., ), and nyström methods (Williams & Seeger, ) (which led to the development of Skyformer (Chen et al., ) and Nyströmformer (Xiong et al., )).

Figure: Figure 2: Spectraformer weight matrices. Red tick denotes techniques which have been explored in the literature. Green tick denotes techniques which are explored in this work. Please refer to Appendix A.3 for references.
Refer to caption: extracted/5906227/resource/weight_matrix.png

inline]MAYBE: Table here like table 2 comparison of different kernel space and time complexities combined with table 3 comparison of convergence rates and required random features for kernel approximation error in (Liu et al., )

### A.3 Technical Details of Weight Approximators

Spectraformer weight matrices are shown in Figure 2. Weight matrices are either approximators or learners. There are three approaches to approximators: Monte Carlo sampling, Unit-cube sampling, and quadrature. We have discussed these approaches in detail in Section 3.1. Here we provide a more specific classification schemes for each method.

- •
Monte Carlo sampling-based weight matrices are improvements over Base and subdivided into two types: structural and geometric methods. Structural methods decompose the Base random matrices $P$ into smaller matrices which reduce time and/ or space complexity. Structural matrices includes FastFoodF, SCRF and P-Model. Among these, we have explored the learner variant FastFoodL of FastFood. Geometric methods enforce certain geometrical couplings along some dimensions in the construction of random matrices to reduce the approximation error. Geometric methods include: ROM (ORF, SORF) and SimRF. Among these, we have explored the main ROM variants - ORF and SORF.
- •
Unit-cube sampling-based weight matrices reformulate the Bochner integral in Equation 9 as Equation A.3 with uniform and independent points. This approach includes QMC, MM, and SSF. We have not covered SSF in our experiments.
- •
Quadrature-based weight matrices estimates the intractable integral using non-uniform weights with deterministic rules. There are two approaches: SGQ and SSR. We have not covered SSR in our experiments.

inline]update the equations so that we use the same dimension for the number of components and so on
inline]Generative should be DGM, add Base to the graph

We now explore these weight matrices in the order specified above.

Base (Rahimi & Recht, ) is the direct sampling from a distribution $\bm{P}_{ij}\sim p$, corresponding to the kernel $\mathcal{K}$ without any additional adjustment, and in approximating the RBF, $P=G$ where $G$ is the normal distribution.

$$ $W_{Base}=\frac{1}{\sigma}\bm{P}$ (16) $$

FastFoodF (Le et al., ) makes use of Hadamard and diagonal matrices to speed up Gaussian matrices $G$ constructions for RFF in $O(n\log{d})$ (100 times faster) with $O(n)$ space (1000 times less space) given $n\geq d$ against Base. However, FastFoodF increases variance and approximation error and decreases the concentration bound.

$$ $W_{FastFood_{L}}=\frac{1}{\sigma}\bm{SHG\Pi HB}$ (17) $$

where $\bm{H}$ is the Walsh-Hadamard matrix, $\bm{\Pi}\in\{0,1\}^{d*d}$ is a permutation matrix, and $\bm{S,G,B}$ are diagonal random matrices, with the diagonal entries being $\{+-1\}$ entries on $\bm{B}$, random Gaussian entries on $\bm{G}$, and a random scaling matrix on $\bm{S}$, $\bm{S}_{ii}=s_{i}||\bm{G}||^{-1/2}_{Frob}$, $s_{i}\sim(2\pi)^{\frac{-d}{2}}\bm{A}^{-1}_{d-1}r^{d-1}e^{-\frac{r^{2}}{2}}$.

SCRF (Signed Circulant Matrix Projection) (Feng et al., ) maintains a high matrix construction speed and $O(s)$ space ($s=t*d$) whilst maintaining the variance of Base, an improvement over FastFoodF. It does this by making use of circulant matrices.

$$ $\bm{W}_{SCRF}=[\bm{P}^{(1)},\bm{P}^{(2)},...,\bm{P}^{(t)}],t=d|D$ (18) $$

where $\bm{P}^{(i)}$ is a signed circulant Gaussian matrix, with the first column of circulant random matrix $\bm{C}^{(i)_{[d]}}$ of $\bm{P}^{(i)}$ drawn randomly from a Gaussian distribution $\mathcal{N}(\bm{0},\bm{I}/\sigma^{2})$. The circulant matrix via Discrete Fourier Transform can be defined as:

$$ $\begin{array}[]{cc}\bm{C}=\frac{1}{m}\bm{F}^{*}diag(\bm{Fc})\bm{F}\\ \bm{F}=[e^{i\frac{2\pi}{m}kn}]^{m-1}_{k,n=0}\end{array}$ (19) $$

P-Model (Choromanski & Sindhwani, ) generalizes over both circulant matrices (including SCRF), FastFoodF, along with other Gaussian and semi-Gaussian structured matrix methods.

$$ $\bm{W}_{P_{model}}=[\bm{g^{T}P_{1}},\bm{g^{T}P_{2}},...,\bm{g^{T}P_{s}}]^{T}$ (20) $$

where $\bm{g}\in\mathbb{R}^{a}$ is a Gaussian vector and a sequence of $m$ $\bm{P}_{i}\in\mathbb{R}^{a*d}$ matrices with L2 norm columns, where $P_{i}$ represents a P-model. Semi-Gaussian structured matrices including Toeplitz, Hankel, and other Toeplitz-like matrices are an open area of research for random matrices.

ORF (Orthogonal RF) (Yu et al., ) decreases the approximation error significantly compared to Base. This is done via replacing $G$ with a properly scaled random orthogonal matrix. However, generating orthogonal matrices become costly quickly as the number of dimensions increases.

$$ $\bm{W}_{ORF}=\frac{1}{\sigma}\bm{SQ}$ (21) $$

where $\bm{Q}$ is a uniformly distributed random orthogonal matrix (on the Stiefel manifold) obtained from the QR decomposition of $\bm{G}$, the set of rows of $\bm{Q}$ forming a basis in $\mathbb{R}^{d}$, and $\bm{S}$ is a diagonal matrix with entries sampled i.i.d from the $\chi$-distribution with $d$ degrees of freedom, thus making the rows of $\bm{SQ}$ and $\bm{G}$ identically distributed.

SORF (Structured ORF) (Yu et al., ) decreases time and space complexity of ORF (from $O(d^{2})$ to $O(d\log{d})$ with almost no extra memory cost) by imposing structure on the orthogonal matrices, inspired by structural methods. SORF is unbiased with large $d$. We replace $\bm{S}$ with $\sqrt{d}$ and $\bm{Q}$ with structured matrix $\bm{HD}_{1}\bm{HD}_{2}\bm{HD}_{3}$

$$ $\bm{W}_{SORF}=\frac{\sqrt{d}}{\sigma}\bm{HD}_{1}\bm{HD}_{2}\bm{HD}_{3}$ (22) $$

where $\bm{D}_{i}\in\mathbb{R}^{d*d},i=1,2,3$ is diagonal ’sign-flipping‘ matrices with diagonal entries sampled from the Rademacher distribution, $\bm{H}$ is the normalized Walsh-Hadamard matrix.

ROM (Random Orthogonal Embeddings) (Choromanski et al., ) generalizes SORF ($t=3$) to:

$$ $\bm{W}_{ROM}=\frac{\sqrt{d}}{\sigma}\prod_{i=1}^{t}\bm{SD}_{i}$ (23) $$

where $\bm{S}$ is a class of $L_{2}$-normalized versions of Kronecker product matrices, of which the Hadamard $\bm{H}$ is a representative of, and $\bm{D}$ being independent diagonal matrices. When $\bm{S}=\bm{H}$ and $t=3$, we obtain SORF used in our experiments. Another ROM variant is S-Hybrid, which we do not experiment with, given as

$$ $\bm{W}_{S_{Hybrid}}=\bm{SD}_{k}^{(\mathcal{U})}\prod_{i=1}^{k-1}\bm{SD}_{i}^{( \mathcal{R})}$ (24) $$

where $\bm{D}_{k}^{(\mathcal{U})}$ is a diagonal matrix with i.i.d. $Unif(\mathcal{S}^{1})$ on the unit circle of $\mathbb{C}$. When being used in the component function, instead of calculating $\phi(\bm{x})^{T}\phi(\bm{y})$ in Equation 10. We model the equation as $Real(\overline{\phi(\bm{x})}^{T}\phi(\bm{y}))$.

SimRF (Simplex RF) (Reid et al., ) is developed to be a positive RF with the most optimal solution, the MSE of which is the lowest in the geometric methods. This is an alternative to the ROM-based geometric methods. Two variants of SimRF are: weight-independent and weight-dependent. SimRF is defined as:

$$ $\bm{W}_{simp}=\bm{DSR}$ (25) $$

where $\bm{D}\in\mathbb{R}^{d*d}=diag(\bm{\omega}_{i}),\bm{R}\in\mathbb{R}^{d*d}$ being a random orthogonal matrix drawn from Haar measure on $O(d)$ and $\bm{S}\in\mathbb{R}^{d*d}$ with row $\bm{s}_{i}$:

$$ $\bm{s}_{i}=\begin{cases}\sqrt{\frac{d}{d-1}}\bm{e}_{i}-\frac{\sqrt{d}+1}{(d-1) ^{3/2}}(1,...,1,0)^{T}&1\leq i<d\\ \frac{1}{\sqrt{d-1}}(1,1,...,1,0)^{T}&i=d\end{cases}$ (26) $$

being unit vectors which are manifestly normalized and subtend obtuse angles. SimRF variants are defined based on how $\bm{\omega}_{i}$ is constructed. For SimRFindep, $\bm{\omega}_{i}\sim\chi_{d}$. For Simdep, we permit the random vector direction $\{\hat{\bm{\omega}}_{i}\}$ to be correlated with norms $\{\bm{\omega}_{i}\}$:

$$ $\bm{\omega}_{i}=-\frac{\sum_{j\neq i}\omega_{j}}{||\sum_{j\neq i}\omega_{j}||_ {2}}\bm{\omega}_{i},i=1,...,d$ (27) $$

QMC (Quasi-Monte Carlo) (Avron et al., ) evaluates on a low discrepancy sequence (e.g., Halton, Sobol’ Faure, and Niederreiter) of points instead of random points in Monte Carlo. Although the approximation error is only reduced minimally, QMC has been shown to perform better than MC in high dimensions and does not have undesirable clustering effect. To calculate QMC, we first assume that $p(\bm{x})=\prod^{d}_{j=1}p_{j}(\bm{x}_{j})$ factorizes with respect to the dimensions with $p_{j}(.)$ being a univariate density function. Then we define:

$$ $\Phi^{-1}(\bm{t})=\left(\Phi_{1}^{-1}\left(\bm{t}_{1}\right),\ldots,\Phi_{d}^{ -1}\left(\bm{t}_{d}\right)\right)\in\mathbb{R}^{d}$ (28) $$

where $\Phi_{j}$ being the cumulative distribution function of $p_{j}$, $\bm{t}_{1},\bm{t}_{2},...,\bm{t}_{s}\in[0,1]^{d}$ being a low discrepancy sequence, and $\bm{\omega}_{i}=\Phi^{-1}(\bm{t}_{i})$. We can thus transform the integral on $\mathbb{R}^{d}$ in Equation 9 to an integral on the unit cube $[0,1]^{d}$ as

$$ $\mathcal{K}(\bm{x}-\bm{x}^{\prime})=\int_{[0,1]^{d}}\exp\big{(}\mbox{i}(\bm{x} -\bm{x}^{\prime})^{\!\top}\Phi^{-1}(\bm{t})\big{)}\mbox{d}\bm{t}$ $$

The weight matrix then can be defined as:

$$ $\bm{W}_{\text{QMC}}=[\Phi^{-1}(\bm{t}_{1}),\Phi^{-1}(\bm{t}_{2}),\ldots,\Phi^{ -1}(\bm{t}_{s})]^{\!\top}\in\mathbb{R}^{s\times d}\;.$ $$

QMC can be further improved by a sub-grouped based rank-one lattice construction which improved complexity (Lyu et al., ).

MM (Moment Matching) (Shen et al., ; Liu et al., ) improves over QMC by removing the undesirable clustering effect and having the same approximation error with less features. This is done by replacing $\Phi^{-1}$ with a moment matching scheme $\widetilde{\Phi}^{-1}$:

$$ $\bm{W}_{\text{MM}}=[\widetilde{\Phi}^{-1}(\bm{t}_{1}),\widetilde{\Phi}^{-1}( \bm{t}_{2}),\ldots,\widetilde{\Phi}^{-1}(\bm{t}_{s})]^{\!\top}\in\mathbb{R}^{s \times d}$ $$

where $\widetilde{\Phi}^{-1}(\bm{t}_{i})=\tilde{\bm{A}}^{-1}(\Phi^{-1}(\bm{t}_{i})-
\tilde{\bm{\mu}})$ can be constructed using moment matching with sample mean $\tilde{\bm{\mu}}=\frac{1}{s}\sum_{i=1}^{s}\Phi^{-1}(\bm{t}_{i})$ and the square root of the sample covariance matrix $\tilde{\bm{A}}$ satisfying $\tilde{\bm{A}}\tilde{\bm{A}}^{\!\top}=\text{Cov}(\Phi^{-1}(\bm{t}_{i})-\tilde{
\bm{\mu}})$.

SSF (Spherical Structured Features) (Lyu, ) improves over QMC by including rotation-invariant kernel as well as in terms of complexity. Rotation-invariant property suggests the construction of feature maps using spherical equal weight approximation using Riesz s-energy on a d-dimensional sphere $\mathbb{S}^{d}:=\{\bm{x}\in\mathbb{R}^{d+1}|||\bm{x}||_{2}=1\}$. Specifically, we construct $\{\bm{v}_{i}\}^{s}_{i=1}$ asymptotically uniformly distributed on the sphere and obtain $\bm{V}:=[\bm{v}_{1},\bm{v}_{2},\ldots,\bm{v}_{s}]\in\mathbb{S}^{(d\times s}$, that $\Phi^{-1}(t)$ uses the one-dimensional QMC point, we have the weight matrix:

$$ $\bm{W}_{\text{SSF}}=[\Phi^{-1}(t)\bm{v}_{1},\Phi^{-1}(t)\bm{v}_{2},\ldots,\Phi ^{-1}(t)\bm{v}_{s}]^{\!\top}\in\mathbb{R}^{s\times(d+1)},$ $$

SGQ (Sparse Grid Quadrature) (Dao et al., ) evolves from GQ. GQ (Gaussian Quadrature) (Dao et al., ) assumes that the kernel $k$ factorizes with respect to the dimensions and thus can be approximated by a one-dimensional Gaussian quadrature rule. However the total number of points $s$ scale exponentially with the dimensions. However GQ suffers form the curse of dimensionality. This is alleviated using Smolyak rule resulting in SGQ. Assuming the third-degree SGQ using symmetric univariate quadrature points $\left\{-\hat{p}_{1},0,\hat{p}_{1}\right\}$ with weights $(\hat{a}_{1},\hat{a}_{0},\hat{a}_{1})$, then we have:

$$ $\bm{W}_{\text{SGQ}}\!=\![\bm{0}_{d},\hat{p}_{1}\bm{e}_{1},\ldots,\hat{p}_{1} \bm{e}_{d},-\hat{p}_{1}\bm{e}_{1},\ldots,-\hat{p}_{1}\bm{e}_{d}]^{\!\top}\!\in \!\mathbb{R}^{(2d+1)\times d}\!$ $$

given that $\mathbb{e}_{i}$ is the d-dimensional standard basis vector with the i^th element being 1.

SSR (Spherical-Radial Rules) (Munkhoeva et al., ) transforms Equation 10 into a double integral over a hyper-sphere and the real line. This leads to the following approximation:

$$ $\bm{W}_{\text{SSR}}=\bm{\vartheta}\otimes\begin{bmatrix}(\bm{QV})^{\top}\\ -(\bm{Q}\bm{V})^{\top}\end{bmatrix}\in\mathbb{R}^{(2d+1)\times d},$ $$

where $\bm{\vartheta}=[\vartheta_{1},\vartheta_{2},\ldots,\vartheta_{s}]$, $\bm{V}=[\bm{v}_{1},\bm{v}_{2},\ldots,\bm{v}_{d+1}]$, $\vartheta\sim\chi(d+2)$ and $\{\bm{v}_{i}\}_{i=1}^{d+1}$ being vertices of a unit regular d-simplex randomly rotated by $\bm{Q}$ (a random orthogonal matrix).

### A.4 Technical Details of Weight Learners

We give a non-mathematical definition of weight matrix learner in Section 3.1: weight matrix learner is any function which parameterize distributions. This allows for the discovery and future experimentation with other potential weight matrix learners in Spectraformer and random feature method in general. Weight matrix learner comes from a long line of work in kernel methods, namely, kernel learning via random features. Typically, a component function $f$ is chosen and a weight matrix $\bm{W}$ is produced via kernel approximation, providing us with the feature map $\phi$. $\phi$ can then be combined with input $\bm{x}$ and weight $\bm{v}$ to fit the learning objective. Kernel learning, however, imposes an additional objective: learning the weight matrix $\bm{W}$.

The approach to the dual objectives separates the kernel learning methods into two-stage and one-stage methods (Liu et al., ). Two-stage methods (e.g., Yu et al. (); Wilson & Adams (); Bullins et al. ()) solves the dual objective separately: $\bm{W}$ is typically first learned via a kernel alignment scheme (Wang et al., ), then we solve for $\bm{v}$. One-stage methods (e.g., Chowdhury et al. (); Tompkins et al. ()), on the other hand, solves the dual objective in parallel: $p(.)$ corresponding to $\bm{W}$ is parameterized then $\bm{v}$ is solved typically. It is not plausible to apply kernel alignment (Wang et al., ) on Transformer architectures. Therefore, only one-stage methods can be considered in the context of weight matrix learner. Two kernel learning methods, GMM and FastFoodL, have been experimented in the non-Transformer setting (Yang et al., ), then adapted into the Transformer setting (Chowdhury et al., ). DGM (Deep Generative Model) has also been effective in modeling distributions (Kingma & Welling, ). It too has been adapted by Chowdhury et al. () for the Transformer setting. Due to the superior performance of FastFoodL with this technique, we have only experimented with this technique. We now explore them.

FastFoodL (see Equation 17) is proposed to be learnable by Yang et al. () with two variations: FSARD (scaling matrix $\bm{S}$ previously sampled from chi-squared is made learnable) and FSGBARD (optimizes the marginal likelihood with respect to the diagonal matrices $\bm{G}$ and $\bm{B}$). It is further adapted by Chowdhury et al. () to make $\bm{S}$ or $\bm{S},\bm{G},\bm{B}$ learnable parameters. In our experiments, we maintain this set up.

GMM (Gaussian Mixture Model) (Wilson & Adams, ; Oliva et al., ) is a known universal approximator of probability distribution. Specifically, we can consider $\bm{W}_{GMM}$ being composed of $s$ components, with each component $c$ (with $C$ total components) being a Gaussian with mean vector $\mu_{c}$ and covariance matrix $\Sigma_{c}$ and component weight $\pi_{c}$. $p(\bm{\omega})$ can then be approximated as $p(\bm{\omega})=\sum_{c=1}^{C}\pi_{c}\mathcal{N}(\mu_{c},\Sigma_{c})$ (Chowdhury et al., ). Assuming $\pi_{c}=\frac{1}{C}$, the weight matrix with learnable weights $\Sigma$ and $\mu$ can be parameterized as:

$$ $\bm{W}_{GMM}=[\Sigma_{1}n+\mu_{1},...,\Sigma_{C}n+\mu_{C}]^{T},n\sim\mathcal{N }(\bm{0},\bm{I})$ (29) $$

Another formulation of GMM is proposed in Yang et al. () in combination with FastFood, albeit it extends beyond constructing the weight matrix.

DGM (Deep Generative Model) (Kingma & Welling, ) allows for a generic distribution modeling using neural network. DGM is initially proposed to make differentiating sampling processes possible via the reparametrization trick, where the generator $g$ creates samples from the target distribution $\bm{\omega}_{m}=g(\bm{n}_{m})$ via transforming from a noise distribution $\bm{n}_{m}\sim p_{0}(.)$.

In addition to the techniques discussed above, there are also some other prominent one-stage methods (e.g., INN, BBQ). INN (Invertible Neural Network) (Xu et al., ) is another universal approximator of probability distribution, consisting of invertible operations transforming samples from a known distribution to a different more complex distribution. BBQ (Black Box Quantile) (Tompkins et al., ) follows the QMC scheme and models $\Phi^{-1}$ using parameterized quantile function.

### A.5 Technical Details of Component Functions

Figure: Figure 3: Spectraformer component functions. Red tick denotes techniques which have been explored in the literature. Green tick denotes techniques which are explored in this work. Please refer to Appendix A.5 for references.
Refer to caption: extracted/5906227/resource/component_function.png

Component functions discussed in Section 3.2 all belong to CDERF. However, there are other component functions including DIRF and HRF (see Figure 3) which we do not explore in our experiments. We now investigate the theoretical details of these component functions, the CDERF family is introduced in their order of discovery.

CDERF (Complex dense exponential random feature) is given in Equation 30, where $\bm{\omega},\bm{x}\in\mathbb{R}^{d}$, $p=\mathcal{N}(0,1)^{d}$, $k\in\{1,2\}$ and $\mathbb{S}_{d_{c}}$ being a set of $d*d$ complex symmetric matrices. The parameters and constraints of different CDERF functions are specified in Table 7:

$$ $f_{DE}^{(k)}(\bm{\omega},\bm{x})=Real(D\exp(\bm{\omega}^{T}\bm{A}\bm{\omega}+ \bm{\omega}^{T}\bm{B}^{(k)}\bm{x}+\bm{x}^{T}\bm{C}^{(k)}\bm{x}))$ (30) $$

**Table 7: CDERF component functions**
|  | $\bm{A}$ | $\bm{B}^{(1)}$ | $\bm{B}^{(2)}$ | $\bm{C}^{(1)}$ | $\bm{C}^{(2)}$ | $D$ | $\bm{x}$ | $\bm{y}$ |
| --- | --- | --- | --- | --- | --- | --- | --- | --- |
| CDERF | $\mathbb{S}_{d_{C}}$ | $\mathbb{C}^{d*d}$ | $\mathbb{C}^{d*d}$ | $\mathbb{R}$ | $\bm{x}$ | $\bm{y}$ |  |  |
| DERF (Likhosherstov et al., ) | $\mathbb{S}_{d}$ | $\mathbb{R}^{d*d}$ | $\mathbb{R}^{d*d}$ | $\mathbb{R}$ | $\bm{x}$ | $\bm{y}$ |  |  |
| SADERF (Likhosherstov et al., ) | $A_{GE}\bm{I}_{d}$ | $B_{GE}\bm{I}_{d}$ | $C_{GE}\bm{I}_{d}$ | $D_{GE}$ | $\Psi\bm{x}$ | $\Psi^{-1}\bm{y}$ |  |  |
| GERF (Likhosherstov et al., ) | $A_{GE}\bm{I}_{d}$ | $B_{GE}^{(1)}\bm{I}_{d}$ | $B_{GE}^{(2)}\bm{I}_{d}$ | $C_{GE}\bm{I}_{d}$ | $D_{GE}$ | $\bm{x}$ | $\bm{y}$ |  |
| TrigRF (Rahimi & Recht, ) | $0\bm{I}_{d}$ | $i\bm{I}_{d}$ | $-i\bm{I}_{d}$ | $0\bm{I}_{d}$ | $1$ | $\bm{x}$ | $\bm{y}$ |  |
| PosRF (Choromanski et al., ) | $0\bm{I}_{d}$ | $1\bm{I}_{d}$ | $-1\bm{I}_{d}$ | $1$ | $\bm{x}$ | $\bm{y}$ |  |  |
| OPRF (Likhosherstov et al., ) | $0\bm{I}_{d}$ | $1\bm{I}_{d}$ | $-1\bm{I}_{d}$ | $1$ | $\bm{x}$ | $\bm{y}$ |  |  |

TrigRF (Trigonometric RF) (Rahimi & Recht, ) is the base RFF implementation.

$$ $\begin{array}[]{c}f_{TrigRF}^{(1)}(\bm{\omega},\bm{x})=\sqrt{2}\cos(\bm{\omega }^{T}\bm{x}+\bm{b})\\ f_{TrigRF}^{(2)}(\bm{\omega},\bm{y})=\sqrt{2}\sin(\bm{\omega}^{T}\bm{y}+\bm{b} )\\ \bm{b}\sim Uniform(0,2\pi)\end{array}$ (31) $$

The use of the trigonometric sine and cosine functions leads to unstable behavior when the inputs have negative dimension-values. This can be further exacerbated when the values TrigRF try to approximate are close to 0 (since most values are of low significance). This causes the variance to approach infinity (Choromanski et al., ; Likhosherstov et al., ). Therefore, we do not want to use TrigRF to perform kernel approximation in attention.

PosRF (Positive RF) (Choromanski et al., ) fixes the problem of TrigRF by enforcing positive component function output in the softmax. The variance of PosRF (in contrast to the variance of TrigRF approaching infinity) approaches 0 as the approximated value of the Softmax kernel approaches 0. PosRF has two forms: Positive RF Base (PosRF-B) (see Equation 32) and Positive RF Hyperbolic (PosRF-Hyp), which is multi-component, i.e., Equation 11: $l=2$, (see Equation 33).

$$ $\begin{array}[]{c}f_{PosRF_{B}}(\bm{\omega},\bm{x})=\exp(\bm{\omega}^{T}\bm{x} -\frac{||\bm{x}||^{2}}{2})\end{array}$ (32) $$

$$ $\begin{array}[]{ll}f_{1,PosRF_{Hyp}}(\bm{\omega},\bm{x})=\exp(\bm{\omega}^{T} \bm{x}-||\bm{x}||^{2})\\ f_{2,PosRF_{Hyp}}(\bm{\omega},\bm{x})=\exp(-\bm{\omega}^{T}\bm{x}-||\bm{x}||^{ 2})\end{array}$ (33) $$

We only use PosRF-B in our experiments.

GERF (Generalized exponential RF) (Likhosherstov et al., ) generalizes both TrigRF and PosRF with Equation 34.

$$ $\begin{array}[]{c}f_{GERF}^{(1)}(\bm{\omega},\bm{x})=D\exp(A||\bm{\omega}||^{2 }+B\bm{\omega}^{T}\bm{x}+\bm{C}||\bm{x}||^{2})\\ f_{GERF}^{(2)}(\bm{\omega},\bm{y})=D\exp(\bm{A}||\bm{\omega}||^{2}+sB\bm{ \omega}^{T}\bm{y}+\bm{C}||\bm{y}||^{2})\\ \\ Re(1-4A)>0,\bm{B}=\sqrt{s(1-4\bm{A})}\\ \bm{C}=-(s+1)/2,D=(\sqrt[4]{1-4\bm{A}})^{d}\\ \bm{A}\in\mathbb{C},s\in\{-1,+1\}\end{array}$ (34) $$

where $\sqrt{.}$ and $\sqrt[n]{.}$ denoting a principal root with a complex argument

OPRF (Optimized positive RF) (Likhosherstov et al., ) is the solution to the minimization of the variance of GERF. Specifically it is defined as Equation 34 with $s=+1$, $||\bm{x}+\bm{y}||^{2}>0$, and $\bm{A}\in\mathbb{R}$ defined in terms of $p^{*}$ as:

$$ $\begin{array}[]{cc}\bm{A}=(1-1/p^{*})/8\\ p^{*}=(\sqrt{(2||\bm{x}+\bm{y}||^{2}+d)^{2}+8d||\bm{x}+\bm{y}||^{2}}-2||\bm{x} +\bm{y}||^{2}-d)\\ /(4||\bm{x}+\bm{y}||^{2})\end{array}$ (35) $$

Whilst PosRF is not bounded, OPRF is. OPRF can provide $e^{60}$ $\times$ variance reduction in estimating the Softmax compared to TrigRF.

DERF (Dense-exponential random features) (Likhosherstov et al., ) extends GERF and replace $\bm{A},\bm{B},\bm{C}$ with dense matrices. DERF is CDERF when $\bm{B},C$ are in the real instead of the complex plane. Minimizing the variance of DERF leads to two approaches: ADERF and SDERF. However both these approaches rely on SVD and eigen decompositions which are not extensively supported on GPU and deep learning libraries. Therefore SADERF is proposed.

SADERF (simplified ADERF) (Likhosherstov et al., ) is a special case of ADERF and extends GERF, requiring only basic unary operations in addition.

$$ $\begin{array}[]{c}f_{SADE}^{(1)}(\bm{\omega},\bm{x})=f_{GE}^{(1)}(\bm{\omega}, \Psi\bm{x}),f_{SADE}^{(2)}(\bm{\omega},\bm{y})=f_{GE}^{(2)}(\bm{\omega},\Psi^{ -1}\bm{y})\\ \Psi_{l,l}^{*}=(\sum_{j}(\bm{y}_{l}^{(i)})^{2}/\sum_{i}(\bm{x}_{l}^{(i)})^{2}) ^{1/4}\end{array}$ (36) $$

DIRF (Discretely-induced random features) (Likhosherstov et al., ) are based on the assumption of $p(\bm{\omega})$ being a discrete distribution with $\bm{\omega}_{1},...,\bm{\omega}_{d}$ being i.i.d., $\mathbb{P}(\bm{\omega}_{l}=k)=p_{k}$, $\sum_{k=0}^{\infty}p_{k}=1$, $p_{k}>0,k\in\{0\}\cup\mathbb{N}$. By making use of the Taylor series, we can define DIRF as:

$$ $f_{DI}(\bm{\omega},\bm{x})=\exp(-\frac{||\bm{x}||^{2}}{2})\prod_{l=1}^{d}\bm{x }_{i}^{\bm{\omega}_{l}}(\bm{\omega}_{l}!)^{\frac{-1}{2}}p_{\bm{\omega}_{l}}^{ \frac{-1}{2}}$ (37) $$

DIRF has two variations: PoisRF (Poisson RF) which requires $p(.)$ to be the Poisson distribution and GeomRF (Geometric RF) which requires $p(.)$ to be the geometric distribution. Since the many weight matrices introduced in Section 3.1 do not readily approximate these distributions, we decide to leave DIRF out of the paper.

HRF (Hybrid RF) (Choromanski et al., ) combines multiple base estimators, similar to multiple kernel learning methods (Bach et al., ), in order to provide the most accurate approximation in regions of interest. Choromanski et al. on solving softmax approximation using HRF. HRF is constructed using a weighted combination $\epsilon$ of $p+1$ base estimators $\hat{SM}^{k}(\bm{x},\bm{y})$ and $\lambda$-coefficients, $\Lambda$ $p$ binary estimators $\lambda^{k}:\mathbb{R}^{d}\times\mathbb{R}^{d}\rightarrow[0,1]$, both constructed independently:

$$ $\hat{SM}^{\epsilon,\Lambda}=\sum_{k=1}^{p}\hat{\lambda}^{k}(\bm{x},\bm{y})\hat {SM}^{k}(\bm{x},\bm{y})+(1-\sum_{k=1}^{p}\hat{\lambda}^{k}(\bm{x},\bm{y}))\hat {SM}^{p+1}(\bm{x},\bm{y})$ (38) $$

Since HRF requires the base estimators $SM(\bm{x},\bm{y})$ to be unbiased, the easiest solution is the complex exponential $\hat{SM}^{cexp}_{m}$ which can be derived directly from the expectation of the softmax as follows:

$$ $\begin{array}[]{cc}\hat{SM}^{cexp}_{m}(\bm{x},\bm{y})=\psi^{m}_{A}(\bm{x})^{T} \psi^{m}_{(\bm{A}^{T})^{-1}}(\bm{y})\\ \psi^{m}_{\bm{M}}(u)=\frac{1}{\sqrt{m}}\exp(-\frac{(\bm{Mu})^{2}}{2})\\ (\exp(\bm{\omega}^{T}_{i}\bm{Mu}),...,\exp(\bm{\omega}_{m}^{T}\bm{Mu}))^{T}, \bm{\omega}_{i}\sim\mathcal{N}(\bm{0},\bm{I}_{d})\end{array}$ (39) $$

To construct $\lambda$-coefficients, three methods are proposed: simultaneous accurate approximation of both small and large values, Gaussian Lambda Coefficients, and adaption to data admitting clustering structure.

### A.6 Experimental Details in Spectraformer

**Table 8: Hyper-parameters for Spectraformer, following the setting from Chen et al. () due to limited computational power. $L$ stands for ListOps, $T$ for Text, and $R$ for Retrieval. The seed that can be used to reproduce our experiments is 42.**
|  | L | T | R |
| --- | --- | --- | --- |
| Embedding dim. | 64 | 64 | 64 |
| Transformer dim | 64 | 64 | 64 |
| Hidden dim | 128 | 128 | 128 |
| Head dim | 32 | 32 | 32 |
| Num. heads | 2 | 2 | 2 |
| Num. layers | 2 | 2 | 2 |
| Vocabulary size | 32 | 512 | 512 |
| Sequence length | 2000 | 4000 | 4000 |
| Dropout rate | 0.1 | 0.1 | 0.1 |
| Att. dropout rate | 0.1 | 0.1 | 0.1 |
| Pooling mode | mean |  |  |
| Batch size | 32 | 32 | 16 |
| Learning rate | 0.0001 | 0.0001 | 0.0002 |
| Warmup steps | 1000 | 80 | 800 |
| Learning rate decay | linear |  |  |
| Weight decay | 0 | 0 | 0 |
| Evaluation freq. | 500 | 500 | 1000 |
| Num. epochs | 50k | 50K | 50k |
| Num. init steps | 1k | 3K | 3k |
| Num. eval steps | 62 | 200 | 300 |
| Patience | 10 | 10 | 10 |
| Num. features | 128 | 128 | 128 |

**Table 9: Mean statistics across component functions for each task. Task is denoted on the top left cell of each table. Mean statistics include ‘acc’ (accuracy), ‘time’ (training time), and ‘mem’ (peak memory consumption).**
| ListOps | acc | time | mem |
| --- | --- | --- | --- |
| PosRF | 31.98 | 0.63 | 1.10 |
| OPRF | 36.11 | 0.74 | 1.28 |
| SADERF | 36.33 | 0.75 | 1.35 |

## Peer Reviews

confidence: 4
contribution: 2
initial_rating: 3
presentation: 4
questions: Please see the weakness section, my ratings for the paper is based on the weakness (especially the second point).
soundness: 3
strengths: -- The paper’s proposed unification exposes gaps which can be filled with novel combination of linear kernel methods

-- The paper is well written and easy to follow

-- On LRA benchmark the novel combinations show some promise
summary: This paper investigates the linearization of attention mechanisms in Transformers using kernel approximation. The authors propose to unify the wide range of work on linearizing the attention kernel in a unified framework. To that end they propose to look at the linear kernel methods as approximating the weight or the component function. In the process they propose new linear methods that fill the gap. Their empirical analysis showcases that other combination methods do perform slightly better than previous SOTA methods.
weaknesses: -- Novelty: The formulation is somewhat of a repeat of chowdhury et al 2022’s formulation. Even though it is more complete with more component functions and weights. 

-- Empirical evaluation is insufficient. LRA dataset on its own is not sufficient to evaluate which combination works best. The LRA benchmark is old and doesn’t satisfy the the current requirements. IMHO for this paper to pass the acceptance threshold, I would want a much more thorough evaluation, on multiple benchmark dataset and multiple modalities. Then any conclusion made from the analysis would be useful for the practitioner.

-- it is unfortunate that there isn't a clear winner or new novel recommendation that came out of the emperical analysis (which the author would recommend to a practitioner)

---

confidence: 4
contribution: 2
initial_rating: 6
presentation: 3
questions: 1. What is the extra computational and memory footprint corresponding to the additional parameters introduced in this mechanism ?
2. The most general formula presented in the paper (Eq. 11) does not provide an unbiased estimation of the softmax kernel. Is there a subset of the instantiations coming from Eq. 11 that is a strict superset of the previously known low-rank linear attention mechanisms providing unbiased estimation of the softmax kernel and still providing unbiasedness ?
3. The Authors say: " Our empirical findings indicate that different kernels are good at different tasks and that kernel choice is fundamental to performant models". More details would be important. What can we say about right kernels for particular applications (e.g. NLP, Vision, long-range-, short-range-attention tasks, etc.). Did the conducted experiments provide any additional insight to shed new light on that ?
soundness: 3
strengths: The research on linear low-rank attention methods for Transformers is important for several practical reasons (fast inference, e.g. for on-device-deployment, etc.) and this paper aims to improve the existing methods in the field. The presented extension is sound and the idea to represent projections as learnable vectors rather than vectors sampled from a fixed probabilistic distribution is a neat idea.  The experimental section presents a comprehensive comparison with several related methods.
summary: The paper presents a family of Transformers, called Spectraformers, introducing a new class of linear low-rank attention mechanisms. In Spectraformer, the nonlinear map defining the transformation of queries/keys combines various nonlinear functions (applied to the queries/keys projected via Gaussian or learned projections). This formulation is general enough (Eq. 11) to cover as special cases various mechanisms introduced before for the unbiased estimation of the softmax kernel (including in particular celebrated positive random features). The Authors test presented mechanism on three tasks from the Long Range Arena benchmark.
weaknesses: The conclusions are stated pretty vaguely, we read: " Our empirical findings indicate that different kernels are good at different tasks and that kernel choice is fundamental to performant models". This is not a particularly informative statement. Learning the projections rather than taking them from a fixed distribution might introduce additional computational costs. This should be discussed in depth in the paper. Finally, LRA is a pretty old benchmark for testing long-range-attention Transformers. The paper would benefit from applying more recent benchmarks.

---

confidence: 3
contribution: 2
initial_rating: 3
presentation: 3
questions: * Based on Table 3, the performances seems significantly different depending on the combination of the component functions and a learnable weight matrix. Is there a guideline for selecting the best combination before training? 





* When employing  $Spectraformer$,  it is unclear how to pick the better combinated component functions and weight matrix that can outperform the standard attention.
soundness: 2
strengths: * This work presents the framework generalizing the random-feature based attention method.
summary: The kernel approximation, based on Bochner’s theorem, enables the computation of linearized attention to be understood as a similarity computation using component functions $\phi(\cdot)$ and a learnable weight matrix $W$. Based on this interpretation, recent works have improved attention methods by presenting new component functions and parameterizations of the weight matrix.

This work further explores how various combinations of weight matrices and component functions, proposed in previous works, can improve the attention method, and refers to it as $Spectraformer$.
weaknesses: * This work recombines the component functions $\phi(\cdot)$ and a learnable weight matrix $W$ presented in existing random-feature attention method, and does not present new idea for improving attention. Thus, the novelty of this work itself seems marginal.



* The benefits of exploring other combinations are not convincingly demonstrated. While it is possible that certain unexplored combinations of component functions and learnable weight matrices could improve accuracy, training time, or memory efficiency for some condition, this work does not clarify when or why certain combinations might be advantageous.

---

confidence: 3
contribution: 2
initial_rating: 3
presentation: 2
questions: In addition to the questions above, I have a few more additional questions. 

- Why for any $\mathbf{W}$, $f$ = Trig a valid component function? (line 263)
- In line 284, what does it mean “among combination with trig RF”? This is true for positive random features as well (see Sec 4.2 in [1])
- Finally training the kernel to mimic the spikyness of softmax can lead to performance gains as observed in [2]. Does similar observations work in this setting? 

[1] Rethinking Attention with Performers. Choromanski et al. ICLR 2021


[2] The Hedgehog & the Porcupine: Expressive Linear Attentions with Softmax Mimicry. Zhang et al. ICLR 2024
soundness: 2
strengths: -  Good overview and discussion about various random feature mechanisms for linearizing attention in Transformers.
- Interesting observation that there is no clear random feature method excelling at all the tasks.
summary: Attention mechanism in Transformers have quadratic time complexity in the sequence length, which can create computational bottlenecks. Various methods have been proposed to address this, with kernel-based approaches for "linearizing" attention gaining significant interest in the ML community. In this work, the authors introduce Spectraformer, an unified framework for approximating and learning the kernel function in linear Transformers. The authors explore a range of sampling techniques and random features, demonstrating that different tasks benefit from different combinations of kernel methods, with no single method emerging as superior across all cases. The empirical results are evaluated using the LRA benchmark.
weaknesses: - Please rewrite Sec 3.4 in terms of pseudocode. Please do not point to specific lines of code.
- Writing needs to be improved, for example the concepts are mentioned before they are defined. Def 3.2 defines a valid component function even though this concept was mentioned in line 262-263.
- QMC is explored in the context of shift-invariant kernels in [1] and also in general random features  [2]. It feels incremental without any theoretical results as the authors are merely combining different matrices (that have been used to reduce variance) in some well-known modern random feature mechanism. It would be an extremely valuable work if the authors show that incorporating these matrices would lower variance over the orthogonal ones. 
- The empirical results needs to be stronger. Specifically : 
    * How does these methods perform in simply approximating the softmax kernel. 
    * We generally see a quality gap when training Performers from scratch compared to regular transformers, does any of this combination close the gap? For ex. see performance gap between Performer-ViT vs Regular ViT (Fig 8. in [3]).
- As pointed out by the authors, different combinations work well for various tasks thus the paper should provide some guidance for practitioners on how to choose the right combination.

[1] Quasi-Monte Carlo Feature Maps for Shift-Invariant Kernels. Avron et al. JMLR 2016.

[2] Hybrid Random Features. Choromanski et al. ICLR 2022.

[3] From block-Toeplitz matrices to differential equations on graphs: towards a general theory for scalable masked Transformers. Choromanski et al. ICML 2022.