Linear Attention › Kernel Feature Maps
Language AI HandbookAll chapters Linear Attention › Kernel Feature MapsOne section · Sections in this chapter4 / 15 ↓
Reading tools Definitions & inline links
Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.
Article links
Make inline references clickable
The choice of feature map determines the properties of linear attention. Different feature maps approximate different similarity functions, each with trade-offs between computational efficiency and how well they mimic softmax behavior.
The Kernel Interpretation
The connection between feature maps and attention kernels comes from the theory of reproducing kernel Hilbert spaces (RKHS). A kernel function measures similarity between two vectors, and Mercer's theorem tells us that any positive semi-definite kernel can be written as:
where:
- : a kernel function that takes two vectors and returns a scalar similarity score
- : input vectors to compare
- : a feature map that lifts inputs to a (possibly infinite-dimensional) feature space
- : the inner product in the feature space
This result, known as Mercer's theorem, tells us that for any valid kernel function, there exists a corresponding feature map. Choosing a feature map implicitly defines a similarity kernel, and vice versa.
Standard softmax attention uses an exponential kernel (ignoring the scaling factor):
where is the dot product between query and key vectors. The exponential function ensures the similarity is always positive and amplifies differences between scores.
To approximate this with linear attention, we need a feature map such that:
Finding such a feature map is the central challenge of linear attention design. The closer the approximation, the more closely linear attention mimics softmax behavior.
Common Feature Maps
Several feature maps have been proposed, each with different properties:
1. ELU + 1 (Efficient Linear Units)
This simple feature map ensures all features are positive (required for the normalization to work properly). It doesn't approximate the exponential kernel well but is computationally cheap.
2. ReLU
Even simpler than ELU, but can produce zero features, potentially causing numerical issues in the denominator.
3. Random Fourier Features (Performer)
where:
- : the input vector (query or key)
- : the number of random frequency vectors
- : random frequency vectors sampled from a standard normal distribution
- : the projection of input onto the -th random direction
- : normalization factor to control the variance of the approximation
The output has dimension (sine and cosine for each frequency). This provides an unbiased approximation to the Gaussian kernel. With additional scaling, it can approximate the softmax kernel.
4. Positive Random Features (FAVOR+)
To ensure non-negativity while approximating the exponential kernel, Performer uses:
where:
- : the input vector
- : the squared Euclidean norm of the input
- : a normalization factor that depends on the input magnitude
- : random projection vectors sampled from a standard normal distribution
- : ensures all features are strictly positive
- : the number of random features
This construction gives , an unbiased estimator of the softmax kernel. The key insight is that by using exponentials instead of sines and cosines, all features remain positive, which is necessary for the normalization in attention to work correctly.
def elu_feature_map(x):
"""ELU + 1 feature map: simple, always positive."""
return np.maximum(x, 0) + np.exp(np.minimum(x, 0))
def relu_feature_map(x):
"""ReLU feature map: simplest possible, but can be zero."""
return np.maximum(x, 0)
def random_fourier_features(x, random_matrix, scale=1.0):
"""
Random Fourier features for approximating Gaussian/softmax kernel.
Args:
x: Input of shape (..., d)
random_matrix: Random projection matrix of shape (d, m)
scale: Scaling factor for input
Returns:
Features of shape (..., 2m)
"""
projection = x @ random_matrix * scale
return np.concatenate(
[np.sin(projection), np.cos(projection)], axis=-1
) / np.sqrt(random_matrix.shape[1])
def positive_random_features(x, random_matrix):
"""
Positive random features (FAVOR+) for unbiased softmax approximation.
Args:
x: Input of shape (..., d)
random_matrix: Random projection matrix of shape (d, m)
Returns:
Features of shape (..., m)
"""
# Normalize input
norm_sq = np.sum(x**2, axis=-1, keepdims=True)
projection = x @ random_matrix
# Positive features with normalization factor
features = np.exp(projection - norm_sq / 2)
return features / np.sqrt(random_matrix.shape[1])# Compare feature dimensions
d = 64
m = 128 # Number of random featuresFeature Map Comparison: Feature Map Input Dim Output Dim -------------------------------------------------- ELU + 1 64 64 ReLU 64 64 Random Fourier 64 256 Positive Random (FAVOR+) 64 128


The visualizations reveal key properties of the feature maps. ELU+1 produces strictly positive outputs for all inputs, which is essential for attention normalization. ReLU is simpler but zeros out negative inputs, losing information and potentially causing numerical issues. Neither simple feature map accurately approximates the exponential kernel, especially for larger dot products, which explains why they produce different attention patterns than softmax.