kefirski.me
ACLPaper 1520249 min read

Linear Transformers with Learnable Kernel Functions

A tiny change to a linear attention kernel lets the model learn which tokens deserve zero attention, improving long-context recall without paying the quadratic bill.

Attention without the quadratic bill

Full attention computes a similarity score between every query and every earlier key, sim(qi,kj)=exp(qikj/d)\operatorname{sim}(q_i,k_j)=\exp(q_i^{\top}k_j/\sqrt{d}), so the cost of a forward pass grows as O(N2)\mathcal{O}(N^2) with sequence length NN. A linear transformer swaps the exponential for a factorized kernel, sim(q,k)=ϕ(q)ϕ(k)\operatorname{sim}(q,k)=\phi(q)^{\top}\phi(k). Once the score factorizes, the sums re-associate: everything the model has read collapses into a running state Si=jiϕ(kj)vjS_i=\sum_{j\le i}\phi(k_j)v_j^{\top}, and the output at position ii is a read of that state, yiϕ(qi)Siy_i\propto\phi(q_i)^{\top}S_i.

The price of that speed is paid entirely by the feature map. Whatever ϕ\phi keeps, the state keeps; whatever ϕ\phi fails to suppress lingers as noise for every later query. Choosing the kernel is not an implementation detail. It is the mechanism.

One pass through linear attention

O(N) mechanism
Past tokens are folded into the state S once and never revisited; the current query only reads it. ReBased changes exactly one component of this machine: the kernel the query and keys pass through.

We build on Based, a linear-attention hybrid introduced by Arora and colleagues that mixes short convolutions with a second-order polynomial kernel. Before our work it was the strongest subquadratic performer on associative recall, and it still lost ground to full attention as contexts grew and hidden states shrank. That remaining gap is what we set out to understand.

The probe that separates recall from fluency

Perplexity is a blunt instrument for this question, because a model can lower it with local pattern-matching while never learning precise long-range routing. Multi-Query Associative Recall, or MQAR, is the sharper probe: a sequence writes down key-value pairs, distractors accumulate, and much later the keys reappear as questions.

What MQAR asks of a model

The recall probe
Answering requires routing information from the exact position where the pair was stored. Full attention does this natively; every efficient substitute has to earn it.

The probe is unforgiving at scale. At sequence length 2,048 with a hidden size of 64, every subquadratic architecture we tested collapses on MQAR while full attention still solves it; in our runs Mamba and RWKV fail the task across all tested sizes. Whatever separates these architectures, this task measures it directly.

A kernel that cannot say zero

Based scores a pair with a truncated Taylor expansion of the exponential, sim(q,k)=1+qk+12(qk)2\operatorname{sim}(q,k)=1+q^{\top}k+\tfrac{1}{2}(q^{\top}k)^2. Written in the dot product t=qkt=q^{\top}k, this is a parabola whose minimum sits at t=1t=-1, and the minimum value is 0.50.5. No pair of tokens, however unrelated, can score below it.

Across a few dozen positions that floor is a rounding error. Across two thousand it is a chorus: every irrelevant token contributes at least half a unit of unnormalized weight, and the one token that should be retrieved must outshout the crowd. Subtracting the constant would drop the floor to zero, but the root would stay pinned at t=1t=-1, an arbitrary point that queries and keys would then have to organize their whole geometry around.

Three similarity curves over the same dot product

similarity weight
Based kernelexp(t), the targeta learned parabola (ReBased)
Three similarity curves over the same dot product The exponential keeps falling as the dot product drops; the Based parabola stops at 0.5 no matter how unrelated the pair is. A learned parabola can put its zero wherever training needs it, here at t = -0.8. Based kernel: undefined, undefined; undefined, undefined; undefined, undefined; undefined, undefined; undefined, undefined; undefined, undefined; undefined, undefined; undefined, undefined; undefined, undefined; undefined, undefined; undefined, undefined; undefined, undefined; undefined, undefined; undefined, undefined; undefined, undefined; undefined, undefined; undefined, undefined; undefined, undefined exp(t), the target: undefined, undefined; undefined, undefined; undefined, undefined; undefined, undefined; undefined, undefined; undefined, undefined; undefined, undefined; undefined, undefined; undefined, undefined; undefined, undefined; undefined, undefined; undefined, undefined; undefined, undefined; undefined, undefined; undefined, undefined; undefined, undefined; undefined, undefined; undefined, undefined a learned parabola (ReBased): undefined, undefined; undefined, undefined; undefined, undefined; undefined, undefined; undefined, undefined; undefined, undefined; undefined, undefined; undefined, undefined; undefined, undefined; undefined, undefined; undefined, undefined; undefined, undefined; undefined, undefined; undefined, undefined; undefined, undefined; undefined, undefined; undefined, undefined; undefined, undefined 01234 -3-2-101 Based floor 0.5 Based kernelexp(t), the targeta learned parabola (ReBased) dot product t between query and key similarity weight
The exponential keeps falling as the dot product drops; the Based parabola stops at 0.5 no matter how unrelated the pair is. A learned parabola can put its zero wherever training needs it, here at t = -0.8.

Let training place the parabola

In ReBased we stop hand-picking the curve. We reduce the kernel to an element-wise square and put a learnable affine map in front of it, ϕ(x)=(γnorm(x)+β)2\phi(x)=(\gamma\,\operatorname{norm}(x)+\beta)^2, with separate γ\gamma and β\beta for queries and keys. The result is still non-negative, so the linear-attention rearrangement survives untouched, but training now decides where each parabola touches zero.

Normalization is the quiet half of the trick: it pins the feature scale so a single learned γ\gamma and β\beta fit the whole distribution. Our ablation pulls the pieces apart. A raw square is actually worse than Based at length 2,048, with mean accuracy 0.44 against 0.51. Normalization alone lifts it to 0.58, the affine map alone reaches 0.60, and the full combination gets 0.61 there and 0.63 at length 256.

Every ingredient of the kernel, ablated

mean MQAR accuracy
sequence length 256sequence length 2048
Every ingredient of the kernel, ablated Mean MQAR accuracy over hidden sizes 16, 24, 32, and 48, five seeds each. A raw square underperforms Based; normalization and the learned affine map each help; together they are the only variant that holds its accuracy from length 256 to 2,048. Based: length 256 0.58; length 2048 0.51 x²: length 256 0.54; length 2048 0.44 norm(x)²: length 256 0.57; length 2048 0.58 (γ·x)²: length 256 0.51; length 2048 0.48 (γ·x+β)²: length 256 0.61; length 2048 0.60 ReBased: length 256 0.63; length 2048 0.61 00.20.40.60.8 0.58length 256: Based, 0.580.51length 2048: Based, 0.51Based0.54length 256: x², 0.540.44length 2048: x², 0.440.57length 256: norm(x)², 0.570.58length 2048: norm(x)², 0.58norm(x)²0.51length 256: (γ·x)², 0.510.48length 2048: (γ·x)², 0.48(γ·x)²0.61length 256: (γ·x+β)², 0.610.60length 2048: (γ·x+β)², 0.60(γ·x+β)²0.63length 256: ReBased, 0.630.61length 2048: ReBased, 0.61ReBased mean MQAR accuracy
Mean MQAR accuracy over hidden sizes 16, 24, 32, and 48, five seeds each. A raw square underperforms Based; normalization and the learned affine map each help; together they are the only variant that holds its accuracy from length 256 to 2,048.

Recall improves where the floor used to bite

MQAR accuracy at sequence length 2048

accuracy
ReBasedBased
MQAR accuracy at sequence length 2048 At the longest tested context the learned kernel is worth up to 18 accuracy points at the tight hidden sizes, 0.58 against 0.40 at dimension 24. By dimension 48 both models saturate; capacity hides the kernel's flaws. ReBased: 16, 0.04; 24, 0.58; 32, 0.83; 48, 0.99 Based: 16, 0.02; 24, 0.40; 32, 0.66; 48, 0.99 00.250.50.751 16243248 ReBased: 16, 0.04ReBased: 24, 0.58ReBased: 32, 0.83ReBased: 48, 0.99Based: 16, 0.02Based: 24, 0.40Based: 32, 0.66Based: 48, 0.99 hidden dimension accuracy
At the longest tested context the learned kernel is worth up to 18 accuracy points at the tight hidden sizes, 0.58 against 0.40 at dimension 24. By dimension 48 both models saturate; capacity hides the kernel's flaws.

The same ordering holds across our full sweep, sequence lengths 128 to 2,048 and hidden sizes 64 to 512: ReBased matches or beats every subquadratic baseline we tested, with the margin widest exactly where the kernel floor bites, long sequences and small states. Full attention remains ahead, and at length 2,048 with hidden size 64 it is the only architecture that still solves the task.

Attention maps say the same thing quantitatively. Over 10,000 MQAR examples we measured intersection-over-union between each model's attention pattern and the ground-truth retrieval positions: full attention scores 0.999, an almost exact lookup. Based reaches 0.157 and ReBased 0.173, sharper than its predecessor but still a diffuse approximation of the routing that attention performs exactly.

Real text moves the same direction

On the Pile we split perplexity by whether a token requires recalling something seen earlier in the context. ReBased improves both slices: associative perplexity moves from 3.27 to 3.25 and the non-associative rest from 37.02 to 36.73, for 12.90 overall against 12.99 for Based. Full attention sits at 11.98, so the ranking that MQAR predicted transfers to real text at the 160M-parameter scale we trained.

Pile perplexity, split by token type

non-associative perplexity, lower is better
reference architecturesour kernel
Pile perplexity, split by token type Lower-left is better on both axes. ReBased improves on Based for both recall tokens and ordinary tokens; full attention keeps a clear lead on the recall slice, matching the MQAR picture. reference architectures: full attention, AR 3.07, non-AR 33.95; Based, AR 3.27, non-AR 37.02 our kernel: ReBased, AR 3.25, non-AR 36.73 3334.2535.536.753833.093.173.263.35 full attentionreference architectures: full attention, AR 3.07, non-AR 33.95Basedreference architectures: Based, AR 3.27, non-AR 37.02ReBasedour kernel: ReBased, AR 3.25, non-AR 36.73 associative perplexity (recall tokens), lower is better non-associative perplexity, lower is better
Lower-left is better on both axes. ReBased improves on Based for both recall tokens and ordinary tokens; full attention keeps a clear lead on the recall slice, matching the MQAR picture.

One-shot evaluation moves the same direction. The LM Evaluation Harness macro average rises from 41.3 to 42.1, and every SuperGLUE task we report improves, with the largest jumps on RTE and BoolQ. The margins are small but uniformly signed: the kernel change does not trade general capability for recall.

One-shot SuperGLUE, task by task

accuracy (ReCoRD: F1)
BasedReBased
55.8 56.7
WSC
46.5 46.9
WiC
47.6 53.1
RTE
62.7 62.8
ReCoRD
51.5 51.9
MultiRC
66.0 67.0
COPA
48.3 52.0
BoolQ
Every task improves. RTE and BoolQ move by four to five points; the rest inch forward. Consistency, not magnitude, is the signal here.

What we did not claim

We tested academic-scale models, and we make no production claims: no inference-latency numbers, no evidence about behavior after much longer pretraining, and copying-heavy workloads remain untested. The recurrent state also still scales with hidden size, so linear in sequence length does not mean free.

What we can defend is narrow and useful. A fixed kernel imposes an avoidable floor on how thoroughly a linear transformer can ignore a token; letting training place the zero removes that floor and reliably helps recall. The distance that remains to full attention is not fine print to hide. It is the measurement that tells us where the next kernel should go.

Linear attention gets better when its kernel can truly ignore a token, and the gap that remains to full attention is the finding, not the fine print.