The bottleneck of attention mechanisms, which are the fundamental building block of modern language models, is their quadratic complexity in terms of context size. There has thus been an increasing interest in the compression of the context, also called KV-cache compression. In this article, we consider the problem of compressing the context of attention mechanisms by first formulating it in the Hilbert space associated with its kernel. In this formalism, attention can be represented as the ratio of two linear maps whose interpolant is also the ratio of interpolants, this allows us to consider the interpolation of the linear maps directly. We thus introduce two novel KV-cache compression methods which we call GAIM and Q-DAIM, based on the Generalized Empirical Interpolation Method (GEIM) and the Q-Discrete Empirical Interpolation Method (Q-DEIM), respectively. As the cost of these methods is quadratic in terms of context length, we propose surrogate models based on the Taylor series, the Performer and the Random Fourier Features method which allow the compression to be done in linear time. Finally, we extend this result to the streaming attention setting, providing new methods which allow one to penalize the selection of interpolation nodes based on the previously interpolated context and to update the previous interpolation based on new information. We then provide numerical results on the convergence of the residuals and the output error in future queries and preliminary results on the LongBench-E benchmark showing performance comparable to the state-of-the-art methods. These experiments suggest that the range revealed by our Q-DAIM method could help identify where information is concentrated in the KV cache, thus enabling an efficient compression.
Figures & tables
Projection method
W
AW
BW
GAIM
ΠLϕ
E˘LL
E˘LKVηaug
PG least squares
ΣK
E˘LKE˘KL
E˘LKE˘KKVηaug
Q-DAIM revealed-range
IL/ΠU
FP,:FP,:T
FP,:FTVηaug
Least squares
-
E˘KL
E˘KKVηaug
WildCat τ scaling
Pτ=LϕTEτ−2L,L−1Lϕτ−2
E˘τ−1L,τ−1L
E˘τ−1L,τ−1KVη,τaug
Table 1: Matrices and projectors associated with the different projection methods.
Deflation
M
G
S˘
R
None
K˘seg,ϕTVsegaug
VsegaugTE˘KsegKsegVsegaug
E˘KsegKseg
E˘KsegKsegVsegaug
Basis
ΠLϕ⊥K˘seg,ϕTVsegaug
VsegaugTS˘LVsegaug
S˘L
S˘LVsegaug
Range
ΠU⊥K˘seg,ϕTVsegaug
VsegaugTS˘UVsegaug
S˘U
S˘UVsegaug
Table 2: Modified matrices for each deflation type.
Basis (Galerkin) / Range / PG least squares / least squares
§ 3.2
Table 3: Parameters for interpolation of streaming attention.
Matrix
Size
Post-Compression
Nodes
L
n×d
Weights
Waug
n×(d+1)
Running mean
μg
d
Range-correction weights
Wˉ,WˉNorm
d+1
Sink
(K,V)sink
2tsinkd
Table 4: Data in memory per head and per layer kept throughout the context compression.
Selection
Residual instantiation
Selection
Residual update
Total
Exact kernel
GAIM
O(Nseg2d)
O(Nsegnsegd)
O(Nsegnseg(nseg+d))
O(Nseg(Nsegd+nseg2))
Q-DAIM †
O(Nsegd(Nseg+d))
O(Nseg(nseg+r))
O(Nsegrmin(nseg,r))
O(Nsegd(Nseg+d))
Surrogate
GAIM-LIN
O(NsegDd)
O(Nsegnsegd)
O(NsegnsegD)
O(NsegD(d+nseg))
GAIM-LIN-O ∗
O(NsegDd)
O(Nsegnsegd)
O(NsegnsegD+nseg3)
O(NsegD(d+nseg)+nseg3)
Table 5: Computational cost of each step, for a single head, per segment. The selection and residual update costs are summed over the nseg picks, and the surrogate costs assume D≥d .
Layer
Variant
n=32
n=128
n=512
All
0
v2
23 (17)
22 (16)
20 (7)
17
7
v2
27 (10)
26 (10)
24 (8)
18
14
v2
28 (6)
28 (6)
26 (2)
23
21
v2
28 (7)
28 (8)
28 (4)
26
Table 6: Number of query heads per layer (out of 28 ) on which range correction lowers the maximum error in L∞ norm, maxq∈Q∥Att(q,K,V)−AttL(q,K,V)∥∞ . GAIM exact v2, nodes selected in the L∞ score norm, tsink=32 . In parentheses we give the heads for which the error decreases by more than 10% . Last column: heads improved under every variant and every budget.
GAIM exact v4
Q-DAIM exact v5
Layer
n=32
n=128
n=512
All
n=32
n=128
n=512
All
0
9 (4)
9 (8)
8 (4)
6
25 (19)
25 (19)
16 (14)
15
7
20 (6)
22 (11)
24 (14)
18
21 (5)
24 (12)
25 (20)
19
14
25 (9)
24 (17)
21 (17)
19
25 (0)
28 (7)
28 (16)
25
21
25 (4)
21 (15)
22 (17)
17
28 (1)
28 (10)
26 (19)
26
Table 7: Number of query heads per layer (out of 28 ) on which the temperature-scaled projection lowers the maximum error in L∞ norm, maxq∈Q∥Att(q,K,V)−AttL(q,K,V)∥∞ . GAIM exact v4 with the nodes selected in the L∞ score norm, and Q-DAIM exact v5, tsink=32 , RC on, the projection is solved in the kernel rescaled as in [ SM26 ] , τ≈2.04 . In parentheses we give the heads for which the error decreases by more than 10% . The last column is the number of heads that improved for all n .
Selection method
Nseg
Deflation
RoPE Strip.
Centering
τ
RC
Proj.
Qasper
MFQA
HotpotQA
2Wiki
GovRep
MultiNews
TREC
TriviaQA
SAMSum
PCount
PRetr
LCC
RepoB-P
Mean
% dense
Dense
–
–
–
–
–
–
–
54.9
61.8
57.1
60.8
34.7
26.9
72.0
86.7
46.7
16.0
100.0
68.6
65.6
57.84
100.0
GAIM exact [v3, L∞ ]
128
range
off
(c,c)
1
on
Gal.
45.8
58.4
48.2
36.0
31.1
23.2
64.0
85.5
41.3
4.0
80.0
52.7
56.1
48.18
83.3
GAIM-LIN [v1, L∞ ]
128 †
none
off
(c,c)
1
on
Gal.
48.8
59.3
52.2
36.4
30.0
22.7
64.0
88.8
41.2
8.0
84.0
47.2
53.0
48.88
84.5
128 †
none
off
(c,c)
1
on
none
40.5
57.7
49.2
47.3
30.9
23.6
68.0
85.0
37.5
8.0
92.0
44.2
56.6
49.27
85.2
128 †
none
off
(c,c)
1
off
Gal.
43.9
52.3
48.0
43.7
30.3
23.1
60.0
86.8
39.0
8.0
92.0
52.5
55.3
48.84
84.4
128 †
none
off
(c,c)
1
off
none
46.5
54.3
45.8
38.5
31.1
23.2
68.0
84.9
37.8
4.0
80.0
51.4
61.4
48.23
83.4
Table 8: LongBench-E - 25 prompts per task at 25% context compression. Key centers are given as the tuple (selection, solve), c = segment mean, g = global mean. The Exact and CompressKV rows are the values reported in Table 4 of [ SM26 ] (25% compression) on the complete benchmark (compared to our 25 samples/task). For comparison, the percentages rows give, per task, our best method on average (Q-DAIM exact, Nseg=64 ) and the best score per task relative to Dense, and CompressKV relative to Exact. Proj.: Galerkin/basis projection (Gal.), none (each node keeps its own value with a unit weight, [VL∣1] ), or Q-DAIM’s revealed-range projection restricted to the revealed rank (range r ). Methods optionally use the τ temperature scaled kernel for comparison with WildCat [ SM26 ] . RP-Cholesky corresponds to an implementation of the WildCat selection and projection on our harness. † : run on PyTorch 2.14, whose GPU (MPS) attention kernels round differently.