10. Norms, distances and inequalities

An error bound says “this vector is no longer than that number”. To say it we need a length for vectors (a norm) and a few inequalities that turn an unknown quantity into a known one: the triangle inequality for sums, Cauchy–Schwarz and Hölder for dot products. RefineKV uses exactly these. The triangle inequality bounds the error in the values by \(V_C = \max_{i \in C} \|v_i\|_2\) (section 10.2); a weighted Cauchy–Schwarz bounds the logit spread \(\delta_C\) from a few stored numbers per cluster, and a box bound does the same with per-coordinate minima and maxima (section 10.6, which follows the paper's Appendix A.7 symbol for symbol). Chapter 19 assembles them. Vectors and dot products are from chapter 8, diagonal matrices from section 9.5.

10.1 Norms

A norm measures the size of a vector with one number. Several are useful, and they share three properties, which are all a bound needs.

Definition. A norm on \(\mathbb{R}^d\) is a function \(x \mapsto \|x\|\) with, for all \(x, y\) and real \(c\):

The distance between \(x\) and \(y\) in that norm is \(\|x - y\|\).

The three used in this book:

\[ \|x\|_1 = \sum_{j=1}^{d} |x_j| , \qquad \|x\|_2 = \Big(\sum_{j=1}^{d} x_j^2\Big)^{1/2} , \qquad \|x\|_\infty = \max_{j} |x_j| . \]

They are all instances of \(\|x\|_p = (\sum_j |x_j|^p)^{1/p}\) for \(p \ge 1\), with \(\|x\|_\infty\) the limit as \(p \to \infty\): the largest term dominates the sum. Each costs \(d\) operations. For \(x = (3, -4, 0, 1)\): \(\|x\|_1 = 8\), \(\|x\|_2 = \sqrt{26} \approx 5.099\), \(\|x\|_\infty = 4\).

The unit ball \(\{x : \|x\| \le 1\}\) shows a norm's shape:

1 1 ‖x‖₁ ≤ 1 ‖x‖₂ ≤ 1 ‖x‖∞ ≤ 1
Figure 10.1. Unit balls in the plane: the diamond \(|x_1| + |x_2| \le 1\) inside the disk \(x_1^2 + x_2^2 \le 1\) inside the square \(\max(|x_1|, |x_2|) \le 1\). A smaller ball means a larger norm.

The nesting of the balls is the chain

\[ \|x\|_\infty \le \|x\|_2 \le \|x\|_1 \le \sqrt{d}\,\|x\|_2 \le d\,\|x\|_\infty . \]

The first holds because the largest square is one of the terms of \(\sum_j x_j^2\); the second because \((\sum_j |x_j|)^2\) contains every \(x_j^2\) plus nonnegative cross terms; the third is Cauchy–Schwarz (section 10.3); the last because each \(x_j^2 \le \|x\|_\infty^2\). The constants matter in high dimension. At \(d = 128\), \(\|x\|_1\) can be 11.3 times \(\|x\|_2\) (when all \(|x_j|\) are equal) or equal to it (when one coordinate carries everything). Whether a vector's energy is spread or concentrated decides which norm is small, and section 10.6 turns on exactly this. (The program norms.c in the sources checks the chain and the triangle inequality in all three norms on 100 000 random vectors: no violation.)

10.2 The triangle inequality

The direct path is never longer than a detour: \(\|a + b\| \le \|a\| + \|b\|\). For \(\|\cdot\|_1\) and \(\|\cdot\|_\infty\) it follows from \(|a_j + b_j| \le |a_j| + |b_j|\) coordinate by coordinate. For \(\|\cdot\|_2\) it follows from Cauchy–Schwarz (section 10.3):

\[ \|a + b\|_2^2 = \|a\|_2^2 + 2\,a^\top b + \|b\|_2^2 \le \|a\|_2^2 + 2\|a\|_2\|b\|_2 + \|b\|_2^2 = (\|a\|_2 + \|b\|_2)^2 , \]

with equality exactly when \(a\) and \(b\) point the same way. Applied repeatedly, and with \(\|w_i v_i\| = |w_i|\,\|v_i\|\):

Fact. For any vectors \(v_i\) and real weights \(w_i\), \[ \Big\| \sum_i w_i v_i \Big\| \le \sum_i |w_i|\,\|v_i\| \le \Big(\max_i \|v_i\|\Big) \sum_i |w_i| . \] The second step replaces each length by the largest; it is Hölder's inequality with \(p = 1\), \(q = \infty\) (section 10.5).

This is how to bound a weighted sum of vectors knowing only the weights and one number, the largest length. Two cases recur.

Why accept a bound this crude? The first step is an equality only if all the \(w_i v_i\) point the same way, which, with weights of both signs, essentially never happens; so the bound is usually loose. But it needs one stored number per cluster, \(V_C\), against \(|C|\,d_v\) for the values themselves. For one random cluster of 32 values in \(\mathbb{R}^{64}\) (in norms.c) the three quantities were \(0.218 \le 1.184 \le 1.346\): the true distortion is a sixth of the bound, and the bound costs nothing to evaluate.

10.3 Cauchy–Schwarz

Theorem (Cauchy–Schwarz). For \(a, b \in \mathbb{R}^d\), \[ |a^\top b| \le \|a\|_2\,\|b\|_2 , \] with equality exactly when one of them is a multiple of the other [steele].

Proof. If \(b = 0\) both sides are 0. Otherwise, for every real \(t\), the squared length of \(a - t b\) is not negative:

\[ 0 \le \|a - t b\|_2^2 = \|b\|_2^2\, t^2 - 2\,(a^\top b)\, t + \|a\|_2^2 . \]

This is a quadratic in \(t\) that never goes below zero, so it has at most one real root, and its discriminant is not positive: \(4(a^\top b)^2 - 4\|a\|_2^2\|b\|_2^2 \le 0\). That is the inequality squared. Equality means the discriminant is 0: some \(t\) gives \(\|a - tb\|_2 = 0\), i.e. \(a = tb\). \(\square\)

What it is for: it bounds a dot product, which depends on how two vectors line up, by lengths, which each vector has alone. Three uses in this book:

The bound is attained when \(r\) is parallel to \(q\), so given only \(\|r\|_2 \le R\) nothing better than \(\|q\|_2 R\) is possible. To do better, store more about the shape of the cluster. That is the next two sections.

10.4 Weighted Cauchy–Schwarz

Coordinates of keys have very different scales: some channels vary by 0.01, some by 10. A bound that measures every coordinate with the same ruler is dominated by the widest one. The fix is to give each coordinate its own scale \(w_j > 0\).

Theorem (weighted Cauchy–Schwarz). For real \(a_j, b_j\) and \(w_j > 0\), \[ \Big(\sum_{j=1}^{d} a_j b_j\Big)^2 \le \Big(\sum_{j=1}^{d} a_j^2 w_j^2\Big) \Big(\sum_{j=1}^{d} \frac{b_j^2}{w_j^2}\Big) , \] with equality exactly when \(b_j = c\,a_j w_j^2\) for one constant \(c\) (or \(a = 0\)).

Proof. Write \(a_j b_j = (a_j w_j)(b_j / w_j)\) and apply Cauchy–Schwarz to the vectors \(\alpha = (a_j w_j)_j\) and \(\beta = (b_j / w_j)_j\). Equality needs \(\beta = c\,\alpha\), i.e. \(b_j / w_j = c\,a_j w_j\). \(\square\)

In matrix language (section 9.5) it is \(|a^\top b| = |(Wa)^\top (W^{-1} b)| \le \|Wa\|_2\,\|W^{-1}b\|_2\) with \(W = \operatorname{diag}(w)\): move the scales onto one side and their reciprocals onto the other, then use plain Cauchy–Schwarz. The factor \(\|W^{-1}b\|_2 = (\sum_j b_j^2/w_j^2)^{1/2}\) is the weighted length of \(b\), small when every \(|b_j|\) is small compared with its own \(w_j\).

What it is for: when \(b\) belongs to a family known in advance (the residuals of a cluster) and \(a\) arrives later (the query), choose \(w\) to fit the family. Then \(\sum_j b_j^2/w_j^2\) can be bounded once, stored as one number, and the query pays \(d\) multiply-adds for \(\sum_j a_j^2 w_j^2\). Any \(w > 0\) gives a valid bound; a good \(w\) gives a tight one. With all \(w_j\) equal it is plain Cauchy–Schwarz, the ball bound above.

10.5 Hölder

Cauchy–Schwarz pairs \(\ell_2\) with \(\ell_2\). Hölder's inequality pairs other norms.

Theorem (Hölder). If \(1 \le p, q \le \infty\) and \(1/p + 1/q = 1\), then \(|\sum_j a_j b_j| \le \|a\|_p\,\|b\|_q\) [hlp], [steele].

\(p = q = 2\) is Cauchy–Schwarz. The case this book uses is \(p = 1\), \(q = \infty\), whose proof is one line:

\[ \Big|\sum_j a_j b_j\Big| \le \sum_j |a_j|\,|b_j| \le \Big(\max_j |b_j|\Big) \sum_j |a_j| . \]

Equality holds when \(b_j = c \operatorname{sign}(a_j)\): every \(|b_j|\) at the maximum, with the sign that makes every term add. Read it as: a weighted sum is at most the total weight times the largest item. It appears three times:

For general \(p\) the proof goes through Young's inequality \(xy \le x^p/p + y^q/q\), a consequence of the convexity of \(\exp\) (chapter 11). The random checks in cs.c (100 000 cases each of Cauchy–Schwarz, the weighted form, and Hölder with \(p = 1\) and \(p = 3\)) found every ratio of left to right side at most 1, and exactly 1 up to \(10^{-15}\) in each stated equality case.

10.6 Bounding a dot product over a box and an ellipsoid

The problem RefineKV must solve: for a cluster \(C\) with mean key \(\bar k_C\) and residuals \(r_i = k_i - \bar k_C\) (section 8.6), bound

\[ \delta_C = \max_{i \in C} \frac{|q^\top (k_i - \bar k_C)|}{\sqrt{d}} \]

for any query \(q\), in \(O(d)\) work and without reading the keys. The method is general: find a simple set \(S\) that contains every residual, stored as a few numbers, and compute \(\max_{r \in S} |q^\top r|\) by a formula. The result bounds \(\delta_C\) because every \(r_i\) is in \(S\). It is the best bound available from \(S\) alone if some point of \(S\) attains it, so “how tight” becomes “how well does \(S\) fit the residuals”. The paper uses two shapes, both aligned with the coordinate axes ([refinekv], Appendix A.7).

q 1.48 1.22 1.12 box ellipse max over keys
Figure 10.2. Six centered keys \(r_i\) (dots), the box of their per-coordinate ranges (blue), and the ellipse \(\sum_j r_j^2 / w_j^2 \le \rho^2\) (red). For \(q = (1, 0.6)\) the dashed lines are where \(q^\top r\) equals the largest value over the box (1.48, at the corner circled blue), over the ellipse (1.22, at the circled red point) and over the actual keys (1.12). Each set's maximum is attained on its boundary; the gap to 1.12 is the bound's looseness.

The box

Store the coordinate-wise minimum and maximum of the keys, \(k_C^{\min}\) and \(k_C^{\max}\) (two vectors). Every residual lies in the box \(l_j \le r_j \le u_j\) with \(l_j = k^{\min}_{C,j} - \bar k_{C,j}\) and \(u_j = k^{\max}_{C,j} - \bar k_{C,j}\). Over a box, the maximum of \(q^\top r = \sum_j q_j r_j\) separates: each term depends on its own \(r_j\), and a linear function of one variable on an interval is largest at an endpoint, \(u_j\) if \(q_j > 0\), \(l_j\) otherwise. So

\[ \max_{l \le r \le u} q^\top r = \sum_{j=1}^{d} \max(q_j l_j,\ q_j u_j) , \qquad \min_{l \le r \le u} q^\top r = \sum_{j=1}^{d} \min(q_j l_j,\ q_j u_j) , \]

both attained at a corner of the box. This is interval arithmetic: the product of \(q_j\) with the interval \([l_j, u_j]\) is the interval between \(q_j l_j\) and \(q_j u_j\), and sums of intervals add endpoints. Interval arithmetic usually overestimates because a variable used twice is treated as two independent ones; here each \(r_j\) appears once, so over the box the result is exact.

The paper writes it with raw keys (Appendix A.7.1, equations 80–83). With \(s_C = q^\top \bar k_C / \sqrt{d}\),

\[ s_C^{\mathrm{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^{\mathrm{lower}} = \frac{1}{\sqrt{d}} \sum_{j=1}^{d} \min\big(q_j k^{\min}_{C,j},\ q_j k^{\max}_{C,j}\big), \] \[ \delta_C^{\mathrm{box}}(q) = \max\big( |s_C^{\mathrm{upper}} - s_C|,\ |s_C^{\mathrm{lower}} - s_C| \big) \ge \delta_C . \]

It is the same bound: subtracting \(s_C = \sum_j q_j \bar k_{C,j} / \sqrt{d}\) term by term turns \(\max(q_j k^{\min}_{C,j}, q_j k^{\max}_{C,j}) - q_j \bar k_{C,j}\) into \(\max(q_j l_j, q_j u_j)\). Cost: \(2d\) stored numbers per cluster, \(O(d)\) per query. Quest [quest] keeps the same per-channel minimum and maximum per page of the KV cache, and bounds \(\max_i q^\top k_i\) (no mean subtracted) by \(\sum_j \max(q_j k^{\min}_j, q_j k^{\max}_j)\) to decide which pages to load (section 17.6); the paper compares against a “Quest-style” logit bound of this kind as a cluster score (Table 2). The weakness, as the paper says, is that the corner may combine extremes that come from different keys: no key need be near it.

The diagonal ellipsoid

Store per-coordinate radii and one scalar (Appendix A.7.2, equations 84–87; there the dimension is written \(d_k\), our \(d\)):

\[ w_{C,j} = \max_{i \in C} |r_{i,j}| , \qquad \rho_C = \max_{i \in C} \Big( \sum_{j=1}^{d_k} \frac{r_{i,j}^2}{w_{C,j}^2} \Big)^{1/2} . \]

\(w_C\) puts each coordinate on its own scale (a coordinate with \(w_{C,j} = 0\) has \(r_{i,j} = 0\) for every key and is dropped). \(\rho_C\) is the largest residual in that normalized scale, so every residual lies in the diagonal ellipsoid \(\sum_j r_j^2 / w_{C,j}^2 \le \rho_C^2\) of section 9.5. Now apply section 10.4 with \(a = q\), \(b = r_i\), \(w = w_C\):

\[ |q^\top r_i| = \Big| \sum_{j=1}^{d_k} (q_j w_{C,j}) \frac{r_{i,j}}{w_{C,j}} \Big| \le \Big( \sum_{j=1}^{d_k} q_j^2 w_{C,j}^2 \Big)^{1/2} \Big( \sum_{j=1}^{d_k} \frac{r_{i,j}^2}{w_{C,j}^2} \Big)^{1/2} \le \rho_C \Big( \sum_{j=1}^{d_k} q_j^2 w_{C,j}^2 \Big)^{1/2} , \] \[ \delta_C^{\mathrm{ell}}(q) = \frac{\rho_C}{\sqrt{d_k}} \Big( \sum_{j=1}^{d_k} q_j^2 w_{C,j}^2 \Big)^{1/2} \ge \max_{i \in C} \frac{|q^\top (k_i - \bar k_C)|}{\sqrt{d_k}} = \delta_C . \]

It is the exact maximum over the ellipsoid. The equality case of section 10.4, \(r_j = c\,q_j w_{C,j}^2\), scaled to the boundary, is the point

\[ r^*_j = \frac{\rho_C\, q_j w_{C,j}^2}{\big(\sum_l q_l^2 w_{C,l}^2\big)^{1/2}} , \qquad \sum_j \frac{(r^*_j)^2}{w_{C,j}^2} = \rho_C^2 , \qquad q^\top r^* = \rho_C \Big(\sum_j q_j^2 w_{C,j}^2\Big)^{1/2} . \]

Cost: \(d + 1\) stored numbers, \(O(d)\) per query. Two facts bound \(\rho_C\): \(\rho_C \ge 1\), because the key that attains \(w_{C,j}\) has a term \(r_{i,j}^2 / w_{C,j}^2 = 1\); and \(\rho_C \le \sqrt{d_k}\), because every term is at most 1. \(\rho_C = \sqrt{d_k}\) exactly when one key is at the extreme of every coordinate at once (an outlier key, or keys spread along one line).

The program bounds.c in the sources checks all of this on 100 000 random clusters: no key ever exceeded either bound; the box corner and \(r^*\) attain the formulas (relative error \(\le 10^{-15}\)); the paper's raw-key form of the box equals the residual form; and \(\rho_C\) stayed in \([1, \sqrt{d}]\), reaching \(\sqrt{d}\) on the clusters with an outlier key.

Which is tighter

Put \(x_j = q_j w_{C,j}\), the query measured in the cluster's scales. Since \(|r_{i,j}| \le w_{C,j}\), the residual box lies inside the symmetric box \(|r_j| \le w_{C,j}\), so by Hölder (section 10.5) the box bound is at most \(\|x\|_1 / \sqrt{d}\). The ellipsoid bound is \(\rho_C \|x\|_2 / \sqrt{d}\). Hence:

\[ \text{the box is tighter whenever } \ \frac{\|x\|_1}{\|x\|_2} \le \rho_C , \]

and both sides run from 1 to \(\sqrt{d}\) (section 10.1). The ratio \(\|x\|_1 / \|x\|_2\) is near 1 when the scaled query sits in a few channels and near \(\sqrt{d}\) when it is spread evenly; \(\rho_C\) is near 1 when no key is extreme in many coordinates at once and near \(\sqrt{d}\) when one key is extreme in all. So the ellipsoid wins for spread queries against clusters whose extremes are scattered over different keys, the situation the paper's remark about the box describes; the box wins for concentrated queries, and for clusters with a key that is extreme everywhere. On random clusters of 32 keys in \(\mathbb{R}^{128}\):

#include <math.h>
#include <stdio.h>
#include <stdlib.h>

#define D 128                     /* d_k */
#define C 32                      /* |C|: the paper's block size */

static double gauss(void)
{
    double u = (rand() + 1.0) / (RAND_MAX + 2.0), v = (rand() + 1.0) / (RAND_MAX + 2.0);
    return sqrt(-2 * log(u)) * cos(6.283185307179586 * v);
}

/* keys: 0 dense, 1 one spike per key, 2 along one line.  query: 0 dense, 1 one spike.
   out: delta_C, delta_box, delta_ell, rho_C, |x|_1/|x|_2 with x_j = q_j w_Cj */
static void trial(int keys, int query, double out[5])
{
    double k[C][D], kb[D] = { 0 }, q[D], u[D], lo[D], hi[D], w[D];
    double rho = 0, dev = 0, up = 0, dn = 0, qw = 0, l1 = 0;
    for (int j = 0; j < D; j++) { u[j] = gauss(); q[j] = query ? 0.1 * gauss() : gauss(); }
    if (query) q[rand() % D] += 5;
    for (int i = 0; i < C; i++) {
        double t = gauss();
        for (int j = 0; j < D; j++)
            k[i][j] = keys == 0 ? gauss() : keys == 2 ? t * u[j] + 0.1 * gauss() : 0.1 * gauss();
        if (keys == 1) k[i][rand() % D] += 3 * gauss();
        for (int j = 0; j < D; j++) kb[j] += k[i][j] / C;           /* mean key */
    }
    for (int j = 0; j < D; j++) { lo[j] = INFINITY; hi[j] = -INFINITY; }
    for (int i = 0; i < C; i++)
        for (int j = 0; j < D; j++) {                               /* r_ij = k_ij - kbar_j */
            k[i][j] -= kb[j];
            lo[j] = fmin(lo[j], k[i][j]); hi[j] = fmax(hi[j], k[i][j]);
        }
    for (int j = 0; j < D; j++) w[j] = fmax(-lo[j], hi[j]);         /* w_Cj */
    for (int i = 0; i < C; i++) {
        double n = 0, s = 0;
        for (int j = 0; j < D; j++) { n += k[i][j] * k[i][j] / (w[j] * w[j]); s += q[j] * k[i][j]; }
        rho = fmax(rho, sqrt(n));                                   /* rho_C */
        dev = fmax(dev, fabs(s));                                   /* max |q.r_i| */
    }
    for (int j = 0; j < D; j++) {
        up += fmax(q[j] * lo[j], q[j] * hi[j]);                     /* max over the box */
        dn += fmin(q[j] * lo[j], q[j] * hi[j]);                     /* min over the box */
        qw += q[j] * q[j] * w[j] * w[j];
        l1 += fabs(q[j]) * w[j];
    }
    out[0] = dev / sqrt(D);
    out[1] = fmax(fabs(up), fabs(dn)) / sqrt(D);
    out[2] = rho * sqrt(qw) / sqrt(D);
    out[3] = rho;
    out[4] = l1 / sqrt(qw);
}

int main(void)
{
    const char *name[4] = { "dense keys, dense q", "dense keys, spiky q",
                            "spiky keys, dense q", "keys on a line, dense q" };
    int kind[4][2] = { { 0, 0 }, { 0, 1 }, { 1, 0 }, { 2, 0 } };
    srand(10);
    printf("%-24s %7s %7s %7s %6s %7s %11s %5s\n", "", "delta_C", "box", "ell", "rho_C", "l1/l2",
           "box tighter", "fails");
    for (int s = 0; s < 4; s++) {
        double m[5] = { 0 }, o[5];
        int box = 0, fail = 0, T = 2000;
        for (int t = 0; t < T; t++) {
            trial(kind[s][0], kind[s][1], o);
            for (int i = 0; i < 5; i++) m[i] += o[i] / T;
            box += o[1] < o[2];
            fail += o[1] < o[0] * (1 - 1e-12) || o[2] < o[0] * (1 - 1e-12);
        }
        printf("%-24s %7.3f %7.3f %7.3f %6.2f %7.2f %10.1f%% %5d\n", name[s], m[0], m[1], m[2],
               m[3], m[4], 100.0 * box / T, fail);
    }
    return 0;
}
                         delta_C     box     ell  rho_C   l1/l2 box tighter fails
dense keys, dense q        2.298  18.905  12.893   5.50    8.88        0.0%     0
dense keys, spiky q        1.050   2.879   5.777   5.50    3.00      100.0%     0
spiky keys, dense q        0.830   4.757   7.097   5.08    4.75       99.9%     0
keys on a line, dense q    1.903  15.360  24.647  10.71    7.38      100.0%     0

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 it maps to the math. trial draws one cluster and one query of a given kind, then follows the two subsections literally. It forms \(\bar k_C\), overwrites each key with its residual \(r_i\) while recording \(l_j\) and \(u_j\) (the residual box), takes \(w_{C,j} = \max(-l_j, u_j) = \max_i |r_{i,j}|\), and in one pass over the keys gets \(\rho_C\) and the true \(\max_i |q^\top r_i|\). The last loop accumulates the box sums \(\sum_j \max(q_j l_j, q_j u_j)\) and \(\sum_j \min(\cdot)\), the ellipsoid's \(\sum_j q_j^2 w_{C,j}^2\), and \(\|x\|_1 = \sum_j |q_j| w_{C,j}\) for the ratio. All three deviations are divided by \(\sqrt{d}\), as in \(\delta_C\). The four kinds are built to move the two ratios: dense Gaussian keys and queries (both spread); a query with one channel 50 times the others (\(\|x\|_1/\|x\|_2\) small); keys with small noise plus one large spike each in a random channel; keys \(k_i = t_i u + \text{noise}\) along one direction \(u\) (every coordinate reaches its extreme at the same key, \(\rho_C\) near \(\sqrt{d}\)). The fails column counts trials where a bound fell below the true \(\delta_C\), with a relative slack of \(10^{-12}\) for rounding: there were none.

The table follows the rule: the box wins (on average) exactly in the rows where \(\|x\|_1/\|x\|_2\) is below \(\rho_C\). It also shows the price of summaries. Even the better bound is 3 to 8 times the true \(\delta_C\), because both sets are much larger than the cloud of residuals inside them. On real data the paper measured the same picture on Llama-3.1-8B with blocks of 32 tokens (RULER at 128K context): mean \(\delta_C\) of \(21.92 \pm 5.31\) from the box, \(23.48 \pm 7.59\) from the ellipsoid, and \(4.17 \pm 1.79\) for the exact value ([refinekv], Table 11). The box was slightly tighter and gave better accuracy (Tables 2 and 3), so it is the default \(\delta_C^{\mathrm{bound}}\); the exact \(\delta_C\), an oracle that reads every key, would be better still. Since the risk score is used only to rank clusters, a bound that is loose by a similar factor for every cluster still ranks well (chapter 20).

Watch out. Both bounds depend on the coordinate axes. Rotating all keys and queries by one orthogonal matrix changes neither the logits nor \(\delta_C\) (section 9.6), but it changes \(k_C^{\min}\), \(k_C^{\max}\) and \(w_C\), and so both bounds. In the usual implementations RoPE rotates keys pair by pair before they are cached, so the bounds are computed on the rotated keys that attention actually uses.

Exercises

  1. Compute \(\|x\|_1\), \(\|x\|_2\), \(\|x\|_\infty\) for \(x = (1, -2, 2)\) and check the chain of section 10.1.
    Answer

    5, 3, 2. Chain: \(2 \le 3 \le 5 \le \sqrt{3}\cdot 3 \approx 5.20 \le 3 \cdot 2 = 6\).

  2. Show \(\|x\|_2 \le \sqrt{d}\,\|x\|_\infty\). When is it an equality?
    Answer

    \(\sum_j x_j^2 \le \sum_j \|x\|_\infty^2 = d\,\|x\|_\infty^2\). Equality iff every \(|x_j| = \|x\|_\infty\): all magnitudes equal.

  3. Show that a convex combination \(o = \sum_i \alpha_i v_i\) has \(\|o\|_2 \le \max_i \|v_i\|_2\). When is it an equality?
    Answer

    \(\|o\| \le \sum_i \alpha_i \|v_i\| \le \max_i \|v_i\| \sum_i \alpha_i = \max_i \|v_i\|\). Equality needs both steps tight: all \(v_i\) with \(\alpha_i > 0\) point the same way (triangle) and have the maximal length (second step), so they are the same vector.

  4. Derive the equality condition of weighted Cauchy–Schwarz, and find \(c\) when \(\sum_j b_j^2 / w_j^2 = \rho^2\).
    Answer

    Equality in Cauchy–Schwarz for \(\alpha_j = a_j w_j\), \(\beta_j = b_j / w_j\) needs \(\beta = c\alpha\): \(b_j = c\,a_j w_j^2\). Then \(\sum_j b_j^2/w_j^2 = c^2 \sum_j a_j^2 w_j^2 = \rho^2\), so \(c = \rho / (\sum_j a_j^2 w_j^2)^{1/2}\): the point \(r^*\) of section 10.6.

  5. For the six keys of figure 10.2, \((1, 0.2)\), \((-0.8, 0.5)\), \((0.3, -0.9)\), \((-0.5, -0.3)\), \((0.6, 0.8)\), \((-0.6, -0.3)\), and \(q = (1, 0.6)\), compute \(\bar k_C\), \(l\), \(u\), \(w_C\), \(\rho_C\), the range of \(q^\top r_i\), the box range, and the ellipsoid bound (without the \(1/\sqrt{d}\)).
    Answer

    \(\bar k_C = 0\), so \(r_i = k_i\); \(l = (-0.8, -0.9)\), \(u = (1, 0.8)\), \(w_C = (1, 0.9)\), \(\rho_C \approx 1.0724\) (from the key \((0.3, -0.9)\)). \(q^\top r_i \in [-0.78, 1.12]\); the box gives \([-1.34, 1.48]\); the ellipsoid \(\pm 1.2188\). Here the ellipsoid is tighter: \(\|x\|_1/\|x\|_2 = 1.54/1.137 \approx 1.36 > \rho_C\).

  6. Prove \(1 \le \rho_C \le \sqrt{d_k}\).
    Answer

    Every term \(r_{i,j}^2 / w_{C,j}^2 \le 1\) because \(w_{C,j}\) is the largest \(|r_{i,j}|\), so each sum is at most \(d_k\). For the lower bound pick any \(j\) and the key \(i\) with \(|r_{i,j}| = w_{C,j}\): its sum contains a term equal to 1.

  7. Show that the ball bound \(\|q\|_2 \max_i \|r_i\|_2\) is the ellipsoid bound with all \(w_{C,j}\) equal to one constant. Is either always better?
    Answer

    With \(w_{C,j} = c\), \(\rho_C = \max_i \|r_i\|_2 / c\) and \((\sum_j q_j^2 c^2)^{1/2} = c\|q\|_2\); the product is \(\|q\|_2 \max_i \|r_i\|_2\). Neither always wins: the paper's \(w_C\) fits clusters whose coordinates have different spreads; equal weights are better when the spreads are equal and \(\rho_C\) is inflated by the normalization. The ball bound is also unchanged by rotations, unlike the paper's.

  8. Quest bounds \(\max_i q^\top k_i\) by \(\sum_j \max(q_j k^{\min}_j, q_j k^{\max}_j)\). Prove it is an upper bound, and say why it ranks pages by how large their keys can score rather than by how much their scores vary.
    Answer

    Each \(k_i\) is in the box \([k^{\min}, k^{\max}]\), and the maximum of \(q^\top k\) over the box is that sum (section 10.6, with \(k\) for \(r\)). It bounds the top score itself, the right quantity for deciding which pages to keep when unread pages are dropped. RefineKV approximates unread clusters by their mean instead, so it needs the spread around the mean, \(\delta_C\), which is why it subtracts \(s_C\).

References