This chapter reads Section 3.2 and Appendix A of [refinekv] line by line. The paper replaces each block of the KV cache by one averaged key and value, and asks how far the attention output can move. Its answer is one inequality, Eq. 4: the error is at most \(2 \sum_C R_C\), a sum of one risk score per cluster. Each \(R_C\) can be computed from a few numbers stored per cluster, and chapter 20 turns it into an algorithm: expand the clusters whose score is high.
Every step uses a tool from an earlier chapter: the triangle inequality, Jensen, a chord of \(e^x\), the identities of cosh and tanh, total variation, weighted Cauchy–Schwarz. Each section gives the paper's statement in its own symbols, says what it means, names the tool, and does the algebra with no step skipped. Equation numbers in parentheses, like (22), are the paper's. A C program at the end of section 19.6 checks every step on 20,000 random cases.
The derivation is correct. The gaps we found are small and are pointed out where they occur: a division by zero not excluded, a symbol used for two things, two steps stated without the algebra, and a monotonicity claim that holds for the sum but not for each score.
One attention head, one query \(q \in \mathbb{R}^d\), \(N\) cached keys \(k_i \in \mathbb{R}^d\) and values \(v_i \in \mathbb{R}^{d_v}\). The exact output is attention as in section 16.3, the paper's Eq. 1:
\[ o = \sum_{i=1}^{N} \alpha_i v_i, \qquad \alpha_i = \frac{\exp(q^\top k_i/\sqrt d)}{\sum_{j=1}^{N} \exp(q^\top k_j/\sqrt d)}. \qquad (1) \]The cache is split into clusters \(\mathcal{C} = \{C_1, \dots, C_m\}\): every token in exactly one (a partition, chapter 18). Appendix A.1 names everything that follows.
Definition (the paper's symbols). For a cluster \(C\) with \(n_C = |C|\) tokens:
Section 3.2 writes \(p_C\), \(v_C\), \(\hat p_C\) out in full as Eq. 2; they are the same as (7)–(10) with the exponentials expanded. For instance
\[ \hat p_C = \frac{|C|\, e^{q^\top \bar k_C/\sqrt d}}{\sum_{C' \in \mathcal{C}} |C'|\, e^{q^\top \bar k_{C'}/\sqrt d}}. \qquad (2) \]What \(\hat o\) is. Attention over \(m\) stand-in tokens, one per cluster, with key \(\bar k_C\), value \(\bar v_C\), and weight multiplied by \(n_C\). Multiplying \(e^{s_C}\) by \(n_C\) is adding \(\log n_C\) to the logit, as the paper notes after (7): the stand-in counts as \(n_C\) identical copies of \((\bar k_C, \bar v_C)\). It is the attention over representatives of section 18.5. It reads \(m\) key/value pairs instead of \(N\).
Why \(o = \sum_C p_C v_C\). Split each weight as \(\alpha_i = \frac{z_C}{Z} \cdot \frac{e^{s_i}}{z_C} = p_C\, w_{i|C}\) for \(i \in C\): first pick a cluster with probability \(p_C\), then a token inside it with probability \(w_{i|C} = e^{s_i}/z_C\). It is a mixture (section 12.4). Grouping the sum (1) by cluster gives \(o = \sum_C p_C \sum_{i \in C} w_{i|C} v_i = \sum_C p_C v_C\).
Two facts are used over and over.
Fact 1 (mean logit). \(\frac{1}{n_C}\sum_{i \in C} s_i = s_C\). The logit is linear in the key, so the mean of the logits is the logit of the mean key: \(\frac{1}{n_C}\sum_i q^\top k_i/\sqrt d = q^\top (\frac{1}{n_C}\sum_i k_i)/\sqrt d\) (section 8.6).
Fact 2 (convex combinations stay inside). If \(\lambda_i \ge 0\) and \(\sum \lambda_i = 1\), then \(\|\sum \lambda_i u_i\| \le \sum \lambda_i \|u_i\| \le \max_i \|u_i\|\), by the triangle inequality (section 10.2). So \(\|v_C\|, \|\bar v_C\| \le V_C\), and \(\|o\|, \|\hat o\| \le V_{\max}\) (section 8.2).
Fact 2 already gives a bound, the trivial bound: \(\|o - \hat o\|_2 \le \|o\| + \|\hat o\| \le 2V_{\max}\). Any useful bound must beat it.
\(o\) and \(\hat o\) differ in two ways at once: the cluster weights (\(p_C\) against \(\hat p_C\)) and the cluster values (\(v_C\) against \(\bar v_C\)). The standard move is to change one thing at a time through a middle point. Appendix A.2 defines the auxiliary output
\[ \tilde o = \sum_{C} \hat p_C v_C \qquad (15) \](approximate weights, exact values), adds and subtracts it, and gets Eq. 3 (= (13)–(14)):
\[ o - \hat o = (o - \tilde o) + (\tilde o - \hat o) = \underbrace{\sum_C (p_C - \hat p_C)\, v_C}_{\text{inter-cluster mass redistribution}} + \underbrace{\sum_C \hat p_C\, (v_C - \bar v_C)}_{\text{intra-cluster value distortion}}. \qquad (3) \]The first term is the right values with the wrong weights; the second, the right (approximate) weights on the wrong values. Each line is pure algebra: \(o - \tilde o = \sum p_C v_C - \sum \hat p_C v_C\), and \(\tilde o - \hat o = \sum \hat p_C v_C - \sum \hat p_C \bar v_C\). The triangle inequality then splits the norm (33):
\[ \|o - \hat o\|_2 \le \|o - \tilde o\|_2 + \|\tilde o - \hat o\|_2. \qquad (33) \]Sections 19.3–19.5 bound the first piece, 19.6 the second.
A.2 points out the asymmetry that drives the next section. The second term has the property we want: if all logits in \(C\) equal \(s_C\), then \(w_{i|C} = 1/n_C\), \(v_C = \bar v_C\), and \(C\)'s term is zero. The first does not. \(p_C - \hat p_C\) can be nonzero for a cluster that is represented perfectly, because both are divided by a total (\(Z\) or \(\hat Z\)) that every other cluster's error changes. Exercise 1 has a two-cluster example. A score per cluster needs the first term rewritten so that each piece is caused by one cluster.
How badly does the representative misjudge a cluster's mass? Their ratio is the mass correction factor (16), rewritten with \(z_C\) and \(\hat z_C\) expanded as in (50):
\[ m_C = \frac{z_C}{\hat z_C} = \frac{\sum_{i \in C} e^{s_i}}{n_C\, e^{s_C}} = \frac{1}{n_C}\sum_{i \in C} e^{s_i - s_C}. \qquad (16, 50) \]It is the mean of \(e^{x_i}\) where \(x_i = s_i - s_C\) have mean 0 (Fact 1). Jensen's inequality for the convex \(e^x\) (section 11.4): the mean of \(e^{x_i}\) is at least \(e\) to the mean of \(x_i\), which is \(e^0 = 1\). So
\[ m_C \ge 1, \qquad \Delta z_C = z_C - \hat z_C \ge 0. \qquad (38) \]In words: averaging the keys before the exponential always underestimates a cluster's mass. (A.5 says this in one sentence, “using Jensen's inequality with \(1/n_C \sum s_i = s_C\)”.) The total ratio is (51):
\[ M = \frac{Z}{\hat Z} = \frac{\sum_C m_C \hat z_C}{\hat Z} = \sum_C \hat p_C m_C, \qquad (51) \]using \(z_C = m_C \hat z_C\) and \(\hat z_C / \hat Z = \hat p_C\). \(M \ge 1\) too: it is a \(\hat p\)-weighted average of numbers \(\ge 1\).
Lemma A.1 (Core Lemma). The mass-redistribution error regroups exactly by source cluster:
\[ o - \tilde o = \sum_{C} \hat p_C \,\frac{m_C - 1}{\sum_{C'} \hat p_{C'} m_{C'}}\, (v_C - \tilde o). \qquad (17) \]A cluster with \(m_C = 1\) contributes exactly zero.
Read it as forces. Cluster \(C\) was given too little mass, by the factor \(m_C\); restoring it pulls the output from \(\tilde o\) toward \(C\)'s own value \(v_C\), with strength \(\hat p_C (m_C - 1)/M\). Each term now depends on one cluster's error \(m_C - 1\) (and on the shared \(M\)).
Start from one cluster's weight error and write the exact mass as approximate plus correction, \(z_C = \hat z_C + \Delta z_C\):
\[ \begin{aligned} p_C - \hat p_C &= \frac{z_C}{Z} - \hat p_C && (39)\ \text{definition of } p_C \\ &= \frac{\Delta z_C + \hat z_C - \hat p_C Z}{Z} && (40)\ \text{common denominator } Z \\ &= \frac{\Delta z_C + \hat p_C \hat Z - \hat p_C Z}{Z} && (41)\ \hat z_C = \hat p_C \hat Z \text{ by (10)} \\ &= \frac{\Delta z_C}{Z} - \frac{\hat p_C \sum_{C'} \Delta z_{C'}}{Z} && (42)\ Z - \hat Z = \textstyle\sum_{C'} \Delta z_{C'}. \end{aligned} \]The first part of (42) is \(C\)'s own error; the second is everyone's error leaking in through the normalizer (the paper's labels: “effect of cluster \(C\)”, “effect of changed global normalization”). To collect by cause, (43) defines the effect of cluster \(C'\) on cluster \(C\)'s weight,
\[ T_{C,C'} = \frac{\Delta z_{C'}}{Z}\left(\mathbf{1}[C = C'] - \hat p_C\right), \qquad (43) \]where \(\mathbf{1}[C = C']\) is 1 if the clusters are the same, else 0. Summing over \(C'\): the term \(C' = C\) gives \(\frac{\Delta z_C}{Z}(1 - \hat p_C)\), the others give \(-\hat p_C \frac{\Delta z_{C'}}{Z}\); together that is (42). So \(p_C - \hat p_C = \sum_{C'} T_{C,C'}\) (44). Multiply by \(v_C\), sum over \(C\), and swap the two sums (45)–(47):
\[ \begin{aligned} o - \tilde o = \sum_C (p_C - \hat p_C) v_C &= \sum_{C'} \frac{\Delta z_{C'}}{Z} \sum_C \left(\mathbf{1}[C = C'] - \hat p_C\right) v_C && (47) \\ &= \sum_{C'} \frac{\Delta z_{C'}}{Z} \Big(v_{C'} - \sum_C \hat p_C v_C\Big) = \sum_{C'} \frac{\Delta z_{C'}}{Z} (v_{C'} - \tilde o). && (48, 49) \end{aligned} \]The indicator picks \(v_{C'}\) out of the inner sum; the rest of the inner sum is \(\tilde o\) by (15). Last, express \(\Delta z_C / Z\) with the paper's quantities, using \(\Delta z_C = z_C - \hat z_C = \hat z_C (m_C - 1)\) and \(Z = M \hat Z\):
\[ \frac{\Delta z_C}{Z} = \frac{\hat z_C (m_C - 1)}{\hat Z M} = \frac{\hat p_C (m_C - 1)}{M}, \qquad (52\text{–}54) \]which, put into (49), is (17).
The same lemma in three lines (our derivation, not the paper's). From \(z_C = m_C \hat z_C\) and \(Z = M\hat Z\): \(p_C = \hat p_C m_C / M\). Then
\[ o - \tilde o = \sum_C \hat p_C \Big(\frac{m_C}{M} - 1\Big) v_C = \sum_C \hat p_C \frac{m_C - 1}{M} v_C - \frac{M - 1}{M}\,\tilde o, \]since \(\frac{m_C}{M} - 1 = \frac{m_C - 1}{M} - \frac{M - 1}{M}\). And \(\sum_C \hat p_C \frac{m_C - 1}{M} = \frac{M - 1}{M}\) (as \(\sum \hat p_C = 1\)), so the last term is \(\sum_C \hat p_C \frac{m_C - 1}{M}\tilde o\) and the two combine into (17). It also shows that \(o\) is a convex combination: \(o = \frac{1}{M}\tilde o + \sum_C \frac{\hat p_C (m_C - 1)}{M} v_C\), with weights that sum to 1.
Take norms in (17). The weights \(\hat p_C (m_C - 1)/M\) are \(\ge 0\) because \(m_C \ge 1\), so the triangle inequality gives \(\|o - \tilde o\| \le \sum_C \frac{\hat p_C (m_C - 1)}{M} \|v_C - \tilde o\|\). By Fact 2, \(\|v_C - \tilde o\| \le \|v_C\| + \|\tilde o\| \le 2V_{\max}\). So (19):
\[ \|o - \tilde o\|_2 \le 2V_{\max}\, \frac{\sum_C \hat p_C (m_C - 1)}{\sum_C \hat p_C m_C} = 2V_{\max}\left(1 - \frac{1}{M}\right), \qquad (19, 21) \]where (21) uses \(\sum_C \hat p_C (m_C - 1) = M - 1\). Lemma A.2 (next section) gives \(m_C \le \cosh \delta_C\), hence \(M \le S\) with \(S = \sum_C \hat p_C \cosh \delta_C\) (our name; the paper writes the sum out). As \(t \mapsto 1 - 1/t\) increases, \(1 - 1/M \le 1 - 1/S = \sum_C \hat p_C (\cosh\delta_C - 1)/S\):
\[ \|o - \tilde o\|_2 \le 2V_{\max}\, \frac{\sum_C \hat p_C (\cosh \delta_C - 1)}{\sum_C \hat p_C \cosh \delta_C}. \qquad (22) \]The right side is below \(2V_{\max}\) always; it is small when every important cluster (large \(\hat p_C\)) has a small spread.
Watch out. The step \(\|v_C - \tilde o\| \le 2V_{\max}\) is generous: \(V_C + V_{\max}\) is also true and smaller. The paper keeps \(2V_{\max}\) so that the mass term has one shared factor. It is slack, not an error.
Lemma A.2 (Cluster Mass Correction Bound). With \(\delta_C = \max_{i \in C} |s_i - s_C|\) as in (55), the mass correction factor \(m_C = \frac{1}{n_C}\sum_{i \in C} e^{s_i - s_C}\) satisfies \(m_C \le \cosh(\delta_C)\). (56)
In words: if no logit in the cluster strays more than \(\delta_C\) from the representative's, the mass is underestimated by at most the factor \(\cosh \delta_C = (e^{\delta_C} + e^{-\delta_C})/2\) (section 4.5). The tool is the chord of a convex function (section 11.6).
Proof (A.6), with the gaps filled. Let \(x_i = s_i - s_C\). By Fact 1 their mean is 0 (57), and by the definition of \(\delta_C\), \(x_i \in [-\delta_C, \delta_C]\). If \(\delta_C = 0\) every \(x_i = 0\) and \(m_C = 1 = \cosh 0\); the paper divides by \(\delta_C\) below without excluding this case. Otherwise write \(x\) as a mix of the two ends, \(x = \frac{\delta_C + x}{2\delta_C}\delta_C + \frac{\delta_C - x}{2\delta_C}(-\delta_C)\), with weights \(\ge 0\) that sum to 1. Convexity of \(e^x\) puts the curve below the chord (58):
\[ e^{x} \le \frac{\delta_C + x}{2\delta_C}\, e^{\delta_C} + \frac{\delta_C - x}{2\delta_C}\, e^{-\delta_C}. \qquad (58) \]The right side is a straight line in \(x\). Average it over the cluster, (59)–(61): the average of a straight line is the line at the average \(x\), and the average \(x\) is 0, so (62)–(63):
\[ m_C = \frac{1}{n_C}\sum_{i \in C} e^{x_i} \le \frac{\delta_C}{2\delta_C} e^{\delta_C} + \frac{\delta_C}{2\delta_C} e^{-\delta_C} = \frac{e^{\delta_C} + e^{-\delta_C}}{2} = \cosh \delta_C. \]It is tight. Half the tokens at \(x = +\delta\) and half at \(-\delta\) have mean 0 and give \(m_C = \cosh\delta\) exactly: every point sits at an end of the chord. Mean zero is what buys the cosh. Without it the best bound is \(m_C \le e^{\delta_C}\), much larger: at \(\delta = 2\), \(e^2 \approx 7.39\) against \(\cosh 2 \approx 3.76\).
Its size. For small \(\delta\), \(\cosh\delta - 1 \approx \delta^2/2\) (section 4.6): the mass error is second order in the spread. For large \(\delta\), \(\cosh\delta \approx e^\delta/2\): it explodes, and a cluster with a wide spread can hide almost all of its mass.
The program below computes both lemmas for the extremal logits. The m(half +-d) column equals cosh: Lemma A.2 is reached. The next three columns belong to section 19.5.
/* lemmas.c: Lemmas A.2 and A.3 of RefineKV, and the logits that make them tight. */
#include <math.h>
#include <stdio.h>
#define N 100000
/* m = mean of e^{x_i}; tv = sum |w_i - 1/n|, w = softmax(x) */
static void stats(const double *x, int n, double *m, double *tv)
{
double z = 0;
*m = 0; *tv = 0;
for (int i = 0; i < n; i++) z += exp(x[i]);
*m = z / n;
for (int i = 0; i < n; i++) *tv += fabs(exp(x[i]) / z - 1.0 / n);
}
int main(void)
{
static double x[N];
double ds[] = { 0.1, 0.5, 1, 2, 4, 8 }, m, tv;
printf("delta cosh m(half +-d) 2tanh(d/2) tv(two-point) tv(+-d halves)\n");
for (int k = 0; k < 6; k++) {
double d = ds[k];
for (int i = 0; i < N; i++) x[i] = i < N / 2 ? d : -d; /* mean 0 */
stats(x, N, &m, &tv);
double tvh = tv, mh = m;
int f = (int)lround(N / (1 + exp(d))); /* share at +d */
for (int i = 0; i < N; i++) x[i] = i < f ? d : -d;
stats(x, N, &m, &tv);
printf("%5.1f %9.4f %9.4f %11.4f %12.4f %14.4f\n", d, cosh(d), mh, 2 * tanh(d / 2), tv, tvh);
}
return 0;
}
delta cosh m(half +-d) 2tanh(d/2) tv(two-point) tv(+-d halves) 0.1 1.0050 1.0050 0.0999 0.0999 0.0997 0.5 1.1276 1.1276 0.4898 0.4898 0.4621 1.0 1.5431 1.5431 0.9242 0.9242 0.7616 2.0 3.7622 3.7622 1.5232 1.5232 0.9640 4.0 27.3082 27.3082 1.9281 1.9281 0.9993 8.0 1490.4792 1490.4792 1.9987 1.9987 1.0000
How the program is built. stats computes the left sides of both lemmas from one logit vector \(x\): m is the mean of \(e^{x_i}\), which is \(m_C\) when \(x_i = s_i - s_C\); tv is \(\sum_i |w_i - 1/n|\) with \(w = \mathrm{softmax}(x)\). The two loops in main build the two extremal cases found in the proofs, not random data, because the point is to see the bounds reached.
m must equal \(\cosh\delta\). Its tv is the last column: what Lemma A.3 gives when the logits have mean 0.tv must equal \(2\tanh(\delta/2)\). Its mean is not 0, so its m says nothing about Lemma A.2 and is not printed.Lemma A.3 (Softmax deviation from uniform). If \(|x_i| \le \delta\) for \(i = 1, \dots, n\) and \(w_i = e^{x_i}/\sum_j e^{x_j}\) (65), then
\[ \sum_{i=1}^{n} \Big| w_i - \frac{1}{n} \Big| \le 2\tanh\Big(\frac{\delta}{2}\Big). \qquad (66) \]In words: logits within \(\pm\delta\) of each other's centre give a softmax close to uniform. The left side is twice the total variation distance from the uniform distribution (section 12.5), so the lemma says \(\mathrm{TV}(w, \text{uniform}) \le \tanh(\delta/2)\): below \(\delta/2\), and never above 1. The tools are a chord again (section 11.6), a one-variable maximum, and the tanh identity of section 4.6.
Proof (A.6), every step.
It is tight. The extremes of steps 4 and 6 are reached together when a share \(1/(1 + e^\delta)\) of the logits sit at \(+\delta\) and the rest at \(-\delta\): then \(r\) takes only the values \(e^{\pm\delta}\). The tv(two-point) column of the program in section 19.4 equals \(2\tanh(\delta/2)\) to four digits.
Watch out. Lemma A.3 does not use mean zero, but where the paper applies it (section 19.6) the \(x_i = s_i - s_C\) do have mean 0, and the tight case above does not (its mean is \(-\delta\tanh(\delta/2)\)). With mean 0, the even split \(\pm\delta\) gives only \(\tanh\delta\) (last column), which stays below 1 while \(2\tanh(\delta/2)\) approaches 2. For small \(\delta\) both are \(\approx \delta\), so the slack matters only for wide clusters.
Now the second term of (3). Appendix A.4 writes the exact cluster output with the within-cluster weights (23) \(w_{i|C} = e^{s_i}/z_C\), which sum to 1, and subtracts the plain mean (24)–(25):
\[ v_C - \bar v_C = \sum_{i \in C} w_{i|C} v_i - \sum_{i \in C}\frac{1}{n_C} v_i = \sum_{i \in C}\Big(w_{i|C} - \frac{1}{n_C}\Big) v_i. \qquad (25) \]The triangle inequality with \(\|c\,v\| = |c|\,\|v\|\) (section 10.2), then \(\|v_i\| \le V_C\), give (26)–(27):
\[ \|v_C - \bar v_C\|_2 \le \sum_{i \in C}\Big|w_{i|C} - \frac{1}{n_C}\Big|\, \|v_i\|_2 \le V_C \sum_{i \in C}\Big|w_{i|C} - \frac{1}{n_C}\Big|. \qquad (27) \]Multiplying top and bottom of \(w_{i|C}\) by \(e^{-s_C}\) shows it is the softmax of \(x_i = s_i - s_C\) (28), with \(|x_i| \le \delta_C\). Lemma A.3 gives (29), so (30):
\[ \|v_C - \bar v_C\|_2 \le 2V_C \tanh\Big(\frac{\delta_C}{2}\Big). \qquad (30) \]Summing over clusters with the triangle inequality and \(\hat p_C \ge 0\), (31)–(32):
\[ \|\tilde o - \hat o\|_2 = \Big\|\sum_C \hat p_C (v_C - \bar v_C)\Big\|_2 \le \sum_C \hat p_C \|v_C - \bar v_C\|_2 \le 2\sum_C \hat p_C V_C \tanh\Big(\frac{\delta_C}{2}\Big). \qquad (32) \]Putting it together. (33) with (22) and (32), and both sums brought under one \(\sum_C\) with the common factor 2 taken out:
\[ \|o - \hat o\|_2 \le 2\sum_{C} \hat p_C \left( V_{\max}\frac{\cosh(\delta_C) - 1}{\sum_{C'} \hat p_{C'} \cosh(\delta_{C'})} + V_C \tanh\Big(\frac{\delta_C}{2}\Big) \right). \qquad (35) \]Watch out. (27) could be sharper (our remark). Because \(\sum_i (w_{i|C} - \frac{1}{n_C}) = 0\), any fixed vector can be subtracted from every \(v_i\) in (25) without changing it; subtracting \(\bar v_C\) gives the bound with \(\max_i \|v_i - \bar v_C\|\), the spread of the values, in place of \(V_C\), their size. The paper's version is correct, just looser when values are large but alike.
The program draws 20,000 random cases: 2 to 24 clusters of 1 to 64 tokens, keys around a random centre with a random spread, values around a random centre, a query of random scale, \(d = 16\), \(d_v = 8\). It computes every quantity of this chapter exactly and checks each identity and inequality. max l/r is the largest ratio of left side to right side: below 1 means the inequality held everywhere.
/* bound.c: every step of RefineKV's Appendix A, checked on random clusters. */
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#define D 16 /* key dimension d */
#define DV 8 /* value dimension d_v */
#define NC 64 /* most tokens per cluster */
#define MC 24 /* most clusters */
#define TRIALS 20000
#define NCHK 11
static double U(void) { return (rand() + 0.5) / (RAND_MAX + 1.0); }
static double G(void) { return sqrt(-2 * log(U())) * cos(6.283185307179586 * U()); }
static double dot(const double *a, const double *b, int n)
{ double s = 0; for (int j = 0; j < n; j++) s += a[j] * b[j]; return s; }
static int cmp(const void *a, const void *b)
{ double x = *(const double *)a, y = *(const double *)b; return (x > y) - (x < y); }
static const char *name[NCHK] = {
"m_C >= 1 (Eq. 38, 50)", "m_C <= cosh(delta_C) (Lemma A.2)",
"sum |w - 1/n| <= 2 tanh(d/2) (Eq. 29)", "|v_C - vbar_C| <= 2V_C tanh (Eq. 30)",
"|o - otilde| <= Eq. 22", "|otilde - ohat| <= Eq. 32",
"|o - ohat| <= 2 sum R_C exact (Eq. 35)", "delta_C <= delta_box (Eq. 83)",
"delta_C <= delta_ell (Eq. 91)", "|o - ohat| <= 2 sum R_C box (Eq. 37)",
"|o - ohat| <= 2 sum R_C ell (Eq. 37)" };
static double worst[NCHK];
static long bad[NCHK];
static void check(int c, double lhs, double rhs)
{
if (lhs > rhs * (1 + 1e-9) + 1e-12) bad[c]++;
if (rhs > 0 && lhs / rhs > worst[c]) worst[c] = lhs / rhs;
}
static int m, n[MC];
static double k[MC][NC][D], v[MC][NC][DV], q[D], ph[MC], V[MC], Vmax;
/* 2 * sum_C R_C (Eq. 4) for logit spreads dl[] */
static double bound(const double *dl)
{
double den = 0, r = 0;
for (int c = 0; c < m; c++) den += ph[c] * cosh(dl[c]);
for (int c = 0; c < m; c++)
r += ph[c] * (Vmax * (cosh(dl[c]) - 1) / den + V[c] * tanh(dl[c] / 2));
return 2 * r;
}
int main(void)
{
static double rex[TRIALS], rbox[TRIALS], qb[TRIALS * MC], qe[TRIALS * MC];
long nq = 0;
double res = 0, sq = sqrt(D);
long vac_ex = 0, vac_box = 0;
srand(19);
for (int t = 0; t < TRIALS; t++) {
double qs = 0.5 + 2.5 * U(), s[MC][NC], sC[MC], smax = -1e300;
m = 2 + rand() % (MC - 1);
for (int j = 0; j < D; j++) q[j] = qs * G();
for (int c = 0; c < m; c++) { /* cluster: centre + spread */
double ck[D], cv[DV], sp = U() * U();
n[c] = 1 + rand() % NC;
for (int j = 0; j < D; j++) ck[j] = 1.5 * G();
for (int j = 0; j < DV; j++) cv[j] = G();
for (int i = 0; i < n[c]; i++) {
for (int j = 0; j < D; j++) k[c][i][j] = ck[j] + sp * G();
for (int j = 0; j < DV; j++) v[c][i][j] = cv[j] + 0.5 * G();
}
}
double kb[MC][D], vb[MC][DV], vC[MC][DV], z[MC], zh[MC], mC[MC];
double dl[MC], dbox[MC], dell[MC], Z = 0, Zh = 0, M = 0;
Vmax = 0;
for (int c = 0; c < m; c++) { /* means, logits, Eq. 5-7 */
for (int j = 0; j < D; j++) {
kb[c][j] = 0;
for (int i = 0; i < n[c]; i++) kb[c][j] += k[c][i][j] / n[c];
}
sC[c] = dot(q, kb[c], D) / sq;
if (sC[c] + log(n[c]) > smax) smax = sC[c] + log(n[c]);
V[c] = 0;
for (int i = 0; i < n[c]; i++) {
s[c][i] = dot(q, k[c][i], D) / sq;
if (s[c][i] > smax) smax = s[c][i];
double nv = sqrt(dot(v[c][i], v[c][i], DV));
if (nv > V[c]) V[c] = nv;
}
if (V[c] > Vmax) Vmax = V[c];
}
for (int c = 0; c < m; c++) { /* masses, Eq. 7-10 */
z[c] = 0; zh[c] = n[c] * exp(sC[c] - smax); mC[c] = 0; dl[c] = 0;
for (int j = 0; j < DV; j++) vC[c][j] = vb[c][j] = 0;
for (int i = 0; i < n[c]; i++) {
double e = exp(s[c][i] - smax);
z[c] += e;
mC[c] += exp(s[c][i] - sC[c]) / n[c];
if (fabs(s[c][i] - sC[c]) > dl[c]) dl[c] = fabs(s[c][i] - sC[c]);
for (int j = 0; j < DV; j++) { vC[c][j] += e * v[c][i][j]; vb[c][j] += v[c][i][j] / n[c]; }
}
for (int j = 0; j < DV; j++) vC[c][j] /= z[c];
Z += z[c]; Zh += zh[c];
}
double o[DV] = {0}, oh[DV] = {0}, ot[DV] = {0}, p[MC];
for (int c = 0; c < m; c++) {
p[c] = z[c] / Z; ph[c] = zh[c] / Zh; M += ph[c] * mC[c];
for (int j = 0; j < DV; j++) {
o[j] += p[c] * vC[c][j]; oh[j] += ph[c] * vb[c][j]; ot[j] += ph[c] * vC[c][j];
}
}
double e1 = 0, e2 = 0, e = 0, r17 = 0, r3 = 0, rhs22, rhs32 = 0, den = 0;
for (int j = 0; j < DV; j++) {
double a = 0, b = 0; /* right sides of Eq. 17 and Eq. 3 */
for (int c = 0; c < m; c++) {
a += ph[c] * (mC[c] - 1) / M * (vC[c][j] - ot[j]);
b += (p[c] - ph[c]) * vC[c][j] + ph[c] * (vC[c][j] - vb[c][j]);
}
r17 += pow(o[j] - ot[j] - a, 2); r3 += pow(o[j] - oh[j] - b, 2);
e1 += pow(o[j] - ot[j], 2); e2 += pow(ot[j] - oh[j], 2); e += pow(o[j] - oh[j], 2);
}
if (sqrt(r17 + r3) > res) res = sqrt(r17 + r3);
e1 = sqrt(e1); e2 = sqrt(e2); e = sqrt(e);
for (int c = 0; c < m; c++) {
double tv = 0, dv2 = 0, su = 0, sl = 0, w[D], rho = 0, qw = 0;
check(0, 1, mC[c]);
check(1, mC[c], cosh(dl[c]));
for (int i = 0; i < n[c]; i++) tv += fabs(exp(s[c][i] - smax) / z[c] - 1.0 / n[c]);
check(2, tv, 2 * tanh(dl[c] / 2));
for (int j = 0; j < DV; j++) dv2 += pow(vC[c][j] - vb[c][j], 2);
check(3, sqrt(dv2), 2 * V[c] * tanh(dl[c] / 2));
for (int j = 0; j < D; j++) { /* box (A.7.1) and ellipsoid (A.7.2) */
double lo = 1e300, hi = -1e300;
w[j] = 0;
for (int i = 0; i < n[c]; i++) {
if (k[c][i][j] < lo) lo = k[c][i][j];
if (k[c][i][j] > hi) hi = k[c][i][j];
if (fabs(k[c][i][j] - kb[c][j]) > w[j]) w[j] = fabs(k[c][i][j] - kb[c][j]);
}
su += fmax(q[j] * lo, q[j] * hi) / sq; sl += fmin(q[j] * lo, q[j] * hi) / sq;
qw += q[j] * q[j] * w[j] * w[j];
}
for (int i = 0; i < n[c]; i++) {
double r2 = 0;
for (int j = 0; j < D; j++)
if (w[j] > 0) r2 += pow((k[c][i][j] - kb[c][j]) / w[j], 2);
if (sqrt(r2) > rho) rho = sqrt(r2);
}
dbox[c] = fmax(fabs(su - sC[c]), fabs(sl - sC[c]));
dell[c] = rho * sqrt(qw) / sq;
check(7, dl[c], dbox[c]); check(8, dl[c], dell[c]);
if (dl[c] > 0) { qb[nq] = dbox[c] / dl[c]; qe[nq++] = dell[c] / dl[c]; }
den += ph[c] * cosh(dl[c]); rhs32 += 2 * ph[c] * V[c] * tanh(dl[c] / 2);
}
rhs22 = 2 * Vmax * (1 - 1 / den);
check(4, e1, rhs22); check(5, e2, rhs32);
check(6, e, bound(dl)); check(9, e, bound(dbox)); check(10, e, bound(dell));
rex[t] = bound(dl) / e; rbox[t] = bound(dbox) / e;
vac_ex += bound(dl) > 2 * Vmax; vac_box += bound(dbox) > 2 * Vmax;
}
printf("%d trials; identities Eq. 3 and Eq. 17: largest residual %.1e\n", TRIALS, res);
printf("%-44s %10s %8s\n", "inequality", "violations", "max l/r");
for (int c = 0; c < NCHK; c++) printf("%-44s %10ld %8.4f\n", name[c], bad[c], worst[c]);
qsort(rex, TRIALS, sizeof *rex, cmp); qsort(rbox, TRIALS, sizeof *rbox, cmp);
printf("bound / |o - ohat| min 10%% 50%% 90%%\n");
printf("exact delta %7.2f %6.2f %6.2f %6.1f\n", rex[0], rex[TRIALS / 10], rex[TRIALS / 2], rex[9 * TRIALS / 10]);
printf("box delta %7.2f %6.2f %6.1f %6.0f\n", rbox[0], rbox[TRIALS / 10], rbox[TRIALS / 2], rbox[9 * TRIALS / 10]);
printf("bound above the trivial 2 Vmax: exact %.1f%%, box %.1f%%\n",
100.0 * vac_ex / TRIALS, 100.0 * vac_box / TRIALS);
qsort(qb, nq, sizeof *qb, cmp); qsort(qe, nq, sizeof *qe, cmp);
printf("delta_box / delta: median %.2f, 90%% %.2f; delta_ell / delta: median %.2f, 90%% %.2f\n",
qb[nq / 2], qb[9 * nq / 10], qe[nq / 2], qe[9 * nq / 10]);
return 0;
}
20000 trials; identities Eq. 3 and Eq. 17: largest residual 3.4e-15 inequality violations max l/r m_C >= 1 (Eq. 38, 50) 0 1.0000 m_C <= cosh(delta_C) (Lemma A.2) 0 1.0000 sum |w - 1/n| <= 2 tanh(d/2) (Eq. 29) 0 1.0000 |v_C - vbar_C| <= 2V_C tanh (Eq. 30) 0 0.7706 |o - otilde| <= Eq. 22 0 0.5002 |otilde - ohat| <= Eq. 32 0 0.4150 |o - ohat| <= 2 sum R_C exact (Eq. 35) 0 0.3430 delta_C <= delta_box (Eq. 83) 0 0.9890 delta_C <= delta_ell (Eq. 91) 0 0.8936 |o - ohat| <= 2 sum R_C box (Eq. 37) 0 0.3324 |o - ohat| <= 2 sum R_C ell (Eq. 37) 0 0.3329 bound / |o - ohat| min 10% 50% 90% exact delta 2.92 20.47 54.63 113.2 box delta 3.01 31.43 138.9 412 bound above the trivial 2 Vmax: exact 27.0%, box 82.6% delta_box / delta: median 3.06, 90% 4.04; delta_ell / delta: median 2.40, 90% 3.35
Printed on FreeBSD 15.1 on amd64 (Core i5-9400), clang 19. It depends on the machine: elsewhere the timings, the random numbers or the last digits differ.
How the program is built. The variables carry the paper's names: s[c][i] is \(s_i\), sC \(s_C\), z and zh are \(z_C\) and \(\hat z_C\), mC \(m_C\), p and ph \(p_C\) and \(\hat p_C\), vC and vb \(v_C\) and \(\bar v_C\), o, ot, oh are \(o\), \(\tilde o\), \(\hat o\), dl \(\delta_C\), den \(S\), M \(M\).
sp \(= U^2\) for \(U\) uniform on \([0, 1]\), which favours tight clusters: those are where Lemmas A.2 and A.3 come close to equality, so where a wrong inequality would show. Sizes start at 1 on purpose: a one-token cluster has \(\delta_C = 0\), the case the paper's proof of Lemma A.2 divides by. Values have their own centres, so \(V_C\) differs from cluster to cluster and from \(V_{\max}\).smax is the largest of all \(s_i\) and all \(s_C + \log n_C\). Neither \(Z\) nor \(\hat Z\) can overflow, and the shift cancels in every ratio: \(p_C\), \(\hat p_C\), \(v_C\) (the max trick of section 15.2). \(m_C\) is computed separately as the mean of \(e^{s_i - s_C}\), the form of (50), not as z/zh, so the lemmas are tested on the expression they state.check, which counts a violation only beyond a relative \(10^{-9}\): the equality cases (singletons) would otherwise fail by one rounding.bound is Eq. 4 as printed: one \(R_C\) per cluster, each with the shared denominator \(S\), summed and doubled. It is not the shorter form \(2V_{\max}(1 - 1/S) + \dots\), so it tests the paper's own formula. The same function is called with dl, dbox and dell: that substitution is Eq. 37.lo and hi per coordinate for the box, the radii w[j] and rho for the ellipsoid, skipping coordinates with \(w_{C,j} = 0\).Reading the output:
Eq. 35 still contains \(\delta_C\), which needs every key of the cluster. Section 19.8 builds a cheap \(\delta_C^{\text{bound}} \ge \delta_C\) (36). Replacing \(\delta_C\) by it gives the paper's main formula, Eq. 4 (= (37), (92)):
\[ \|o - \hat o\|_2 \le 2\sum_{C} \underbrace{\hat p_C \left( V_{\max}\frac{\cosh(\delta_C^{\text{bound}}) - 1}{\sum_{C'} \hat p_{C'} \cosh(\delta_{C'}^{\text{bound}})} + V_C \tanh\Big(\frac{\delta_C^{\text{bound}}}{2}\Big) \right)}_{\text{Risk Score } R_C \text{ for cluster } C}. \qquad (4) \]Why the replacement is allowed. The paper says the bound is “monotonically non-decreasing with respect to each \(\delta_C\)”. True of the sum, and easy to see once it is rewritten with (21): \(2\sum_C R_C = 2V_{\max}(1 - 1/S) + 2\sum_C \hat p_C V_C \tanh(\delta_C/2)\), where \(S = \sum_C \hat p_C \cosh\delta_C\) grows with every \(\delta_C\), and so do \(1 - 1/S\) and each tanh. It is not true of each \(R_C\): raising another cluster's \(\delta_{C'}\) raises \(S\) and lowers \(R_C\). The program mono.c (exercise 5) shows \(R_1\) falling from 0.4446 to 0.2778 while \(\delta_2\) goes from 0 to 3 and the sum rises. So a single \(R_C\) computed with bounds is not an upper bound on anything about \(C\) alone; only the sum is a bound.
What it measures (Section 3.3). \(R_C\) is importance times distortion. \(\hat p_C\), the global importance, is the share of attention the representative gets; it needs only \(\bar k_C\) and \(n_C\). The bracket, the local distortion, depends only on the spread \(\delta_C^{\text{bound}}\) (plus \(V_C\), \(V_{\max}\) and the shared \(S\)). It has two parts with different shapes:
So a tight cluster is scored mostly by its value part, linear in the spread; a wide one by its mass part, exponential in the spread. An important cluster must be tight to score low; an unimportant one may be wide.
The threshold. Section 4 turns the score into a rule: expand a cluster (read its real keys and values) when \(R_C > \tau\), keep its representative otherwise. \(R_C\) has the units of a value vector's norm, so \(\tau\) is an absolute error tolerance in those units, not a relative one. Figure 2 (left) of the paper plots clusters by \(\delta_C^{\text{bound}}\) and \(\hat p_C\) (log scale) with the line \(R_C = \tau\). The line is tilted, and the large-\(\delta\) form says why: there \(R_C \approx \hat p_C V_{\max} e^{\delta_C}/(2S)\), so \(R_C = \tau\) is \(\log \hat p_C + \delta_C = \text{const}\), a line of slope \(-1\). This is the simplification \(\tilde R_C\) of Appendix C.4.1, Eq. 93.
What happens after expanding. The bound holds for any partition. An expanded cluster can be seen as \(n_C\) one-token clusters, each with \(\delta = 0\) and so \(R = 0\); Eq. 4 then bounds the mixed output too, with \(\hat p\) and \(S\) recomputed. The scores computed before expanding are no longer those terms, since \(S\) changed. Section 7 of the paper lists this as “mixed-state re-scoring”, future work (section 20.5).
\(\delta_C\) is the largest \(|q^\top (k_i - \bar k_C)|/\sqrt d\) over the cluster: a maximum of dot products over a set of keys, for a query not known in advance. Store a shape that contains all the keys, and bound the dot product over the shape (section 10.6). Appendix A.7 gives two shapes.
Store \(k_C^{\min}\) and \(k_C^{\max}\), the coordinate-wise minimum and maximum over the cluster. Coordinate \(j\) of \(q^\top k_i\) is \(q_j k_{i,j}\) with \(k_{i,j} \in [k^{\min}_{C,j}, k^{\max}_{C,j}]\); it is largest at one end of the range and smallest at the other, which end depending on the sign of \(q_j\). Summing over \(j\), every token's logit lies between (81) and (82):
\[ s_C^{\text{upper}} = \frac{1}{\sqrt d}\sum_{j=1}^{d}\max\big(q_j k^{\min}_{C,j},\, q_j k^{\max}_{C,j}\big), \qquad s_C^{\text{lower}} = \frac{1}{\sqrt d}\sum_{j=1}^{d}\min\big(q_j k^{\min}_{C,j},\, q_j k^{\max}_{C,j}\big). \]\(s_C\) is in the same range (the mean key is in the box), so every \(|s_i - s_C|\) is at most the larger distance from \(s_C\) to an end (83):
\[ \delta_C^{\text{box}}(q) = \max\big(|s_C^{\text{upper}} - s_C|,\ |s_C^{\text{lower}} - s_C|\big) \ge \delta_C. \qquad (83) \]Cost: two \(d\)-vectors per cluster and \(O(d)\) work per query, the same summary Quest uses to estimate page scores [quest]. It is loose when the coordinate extremes come from different keys: the corner it scores may contain no key.
Centre the keys, \(r_i = k_i - \bar k_C\), and store the per-coordinate radii \(w_{C,j} = \max_{i} |r_{i,j}|\) (84) and one scalar (85),
\[ \rho_C = \max_{i \in C}\Big(\sum_{j=1}^{d_k} \frac{r_{i,j}^2}{w_{C,j}^2}\Big)^{1/2}, \]so that all centred keys lie in the ellipsoid \(\sum_j r_j^2/w_{C,j}^2 \le \rho_C^2\) (86). (A coordinate with \(w_{C,j} = 0\) is zero in every key and is left out, as the paper says.) \(\rho_C\) is between 1 and \(\sqrt{d}\): each term of the sum is at most 1, and the key that sets \(w_{C,j}\) has a term equal to 1. Weighted Cauchy–Schwarz (section 10.4) splits each product as \((q_j w_{C,j}) \cdot (r_{i,j}/w_{C,j})\), (88)–(90):
\[ |q^\top r_i| = \Big|\sum_j (q_j w_{C,j})\frac{r_{i,j}}{w_{C,j}}\Big| \le \Big(\sum_j q_j^2 w_{C,j}^2\Big)^{1/2}\Big(\sum_j \frac{r_{i,j}^2}{w_{C,j}^2}\Big)^{1/2} \le \rho_C\Big(\sum_j q_j^2 w_{C,j}^2\Big)^{1/2}, \]which, divided by \(\sqrt{d_k}\), is (87) and (91): \(\delta_C^{\text{ell}}(q) = \frac{\rho_C}{\sqrt{d_k}}(\sum_j q_j^2 w_{C,j}^2)^{1/2} \ge \delta_C\). One vector and one scalar per cluster, \(O(d_k)\) per query. A.7.2 writes \(d_k\) for the key dimension that the rest of the paper calls \(d\), and the start of A.7 names the bound \(D_C(q)\) once before calling it \(\delta_C^{\text{bound}}\); both are the same objects.
How loose. On the random clusters of section 19.6 the last line of the output shows the median \(\delta^{\text{box}}/\delta\) at 3.06 and \(\delta^{\text{ell}}/\delta\) at 2.40; on those isotropic keys the ellipsoid is tighter. On real keys the paper finds the opposite (Table 11: mean \(\delta\) of 21.92 for the box, 23.48 for the ellipsoid, 4.17 exact, on Llama-3.1-8B), and the box ranks clusters better (Table 2). Which shape wins depends on the keys. Either way the looseness then passes through cosh, where a factor of 5 in \(\delta\) is a factor of about \(e^{17.75}\) in the mass part: this is why the computable bound is far above the trivial one in practice (section 20.5).
\(z_A = \hat z_A = 1\), \(z_B = e + e^{-1} \approx 3.086\), \(\hat z_B = 2\). \(p_A = 1/4.086 \approx 0.2447\), \(\hat p_A = 1/3\). A is exact (\(m_A = 1\)) yet its weight is wrong, because B's error changed the normalizer. \(m_B = 1.5431 = \cosh 1\): Lemma A.2 is tight here (mono.c prints these numbers). Lemma A.1 charges all of \(o - \tilde o\) to B.
\(M \ge 1\), so \(0 \le 1 - 1/M < 1\). \(M = Z/\hat Z = 4.086/3 \approx 1.362\), so the factor is \(1 - 1/1.362 \approx 0.266\).
\(w_1 = e^\delta/(e^\delta + e^{-\delta})\), so \(w_1 - \frac12 = \frac{e^\delta - e^{-\delta}}{2(e^\delta + e^{-\delta})} = \frac12\tanh\delta\) and the sum is \(\tanh\delta\). With \(t = \tanh(\delta/2)\), \(\tanh\delta = 2t/(1 + t^2) \le 2t\).
Each \(e^{x_i} \le e^{\delta_C}\), so their mean is too. It ignores that the \(x_i\) average to 0; using it moves the bound from the top of the curve down to the chord's midpoint, \(\cosh\delta_C < e^{\delta_C}\).
mono.c prints:
delta_2 = 0: R_1 = 0.4446 R_2 = 0.0000 2(R_1 + R_2) = 0.8892 delta_2 = 1: R_1 = 0.4070 R_2 = 0.4070 2(R_1 + R_2) = 1.6281 delta_2 = 2: R_1 = 0.3334 R_2 = 0.9014 2(R_1 + R_2) = 2.4697 delta_2 = 3: R_1 = 0.2778 R_2 = 1.2335 2(R_1 + R_2) = 3.0228 p_A = 0.2447 phat_A = 0.3333 m_B = 1.5431 cosh 1 = 1.5431
The total grows with \(\delta_2\), so replacing each \(\delta\) by an upper bound keeps the bound valid. \(R_1\) falls, though cluster 1 did not change: a score is relative to the others through \(S\).
Per coordinate, max/min of \(q_j k_j\): \((1, 0)\), \((0, -2)\), \((0, 0)\), \((1, -1)\). \(s^{\text{upper}} = 2/2 = 1\), \(s^{\text{lower}} = -3/2 = -1.5\), \(s_C = (0.5 - 1 + 0 + 0)/2 = -0.25\). \(\delta^{\text{box}} = \max(1.25, 1.25) = 1.25\). The third coordinate, wide in the box, costs nothing because \(q_3 = 0\): the bound is query-dependent.
\(R_C \approx \hat p_C(V_{\max}\delta_C^2/2 + V_C\delta_C/2)\). The value part is first order, the mass part second order, so for tight clusters the value distortion dominates.
Section 19.3, “a shorter route”: \(o = \frac{1}{M}\tilde o + \sum_C \frac{\hat p_C(m_C - 1)}{M}v_C\), weights \(\ge 0\) summing to \(\frac1M + \frac{M-1}{M} = 1\). \(M = 1\) forces every \(m_C = 1\) (an average of numbers \(\ge 1\) equal to 1), so \(o = \tilde o\): no mass error.