A convex function is one whose graph bends upward everywhere, like \(e^x\) or \(x^2\). Two facts about such a graph do most of the work in this chapter: it lies below every chord and above every tangent. Averaged over many points, the tangent fact becomes Jensen's inequality and the chord fact becomes an upper bound. That pair is exactly what [refinekv] needs to bound how wrong a cluster's representative can be: Lemma A.2 of the paper (the mass correction is at most \(\cosh \delta_C\)) is the chord of \(e^x\), averaged, and the core of Lemma A.3 (the \(2\tanh(\delta/2)\) bound) is the chord of \(|r - 1|\), averaged. Section 11.6 does both; chapter 19 puts them into the paper's error bound. On the way, section 11.5 meets log-sum-exp, the smooth maximum behind softmax (chapter 15).
A set \(S\) (of numbers, or of points in \(\mathbb{R}^d\)) is convex if the straight segment between any two of its points stays inside it. An interval is convex; so is a box, a ball, a filled triangle. A ring, an L-shape or two separate intervals are not.
The points of the segment from \(x\) to \(y\) are the convex combinations \(\lambda x + (1 - \lambda) y\) with \(0 \le \lambda \le 1\) (section 8.2). With more points, \(\sum_i w_i x_i\) with \(w_i \ge 0\) and \(\sum_i w_i = 1\). One convex set matters more than any other in this book: the set of all such weight vectors \(w\), the probability simplex. Every softmax output, every vector of attention weights \(\alpha_1, \dots, \alpha_N\), is a point of it, and the attention output \(o = \sum_i \alpha_i v_i\) is a convex combination of the values (chapter 12).
Definition. \(f\) is convex on an interval \(I\) if for all \(x, y \in I\) and \(0 \le \lambda \le 1\)
\[ f(\lambda x + (1 - \lambda) y) \le \lambda f(x) + (1 - \lambda) f(y). \]It is strictly convex if the inequality is strict whenever \(x \ne y\) and \(0 < \lambda < 1\). It is concave if \(-f\) is convex.
Read the two sides as two ways to evaluate at the point a fraction \(\lambda\) of the way from \(y\) to \(x\): the left side asks the curve, the right side asks the straight line through \((x, f(x))\) and \((y, f(y))\). Convex means the curve never pokes above that line. Equivalently, the region above the graph is a convex set. \(x^2\), \(e^x\), \(|x|\) and \(-\ln x\) are convex; \(\ln x\) and \(\sqrt{x}\) are concave; \(x^3\) is neither on all of \(\mathbb{R}\).
Convexity is preserved by the operations you would use to build functions: a sum of convex functions is convex, so is a positive multiple, a maximum of convex functions, and a convex function of an affine argument, \(f(a x + b)\) [boyd-cvx]. Each follows from the definition in a line. A minimum of convex functions is not convex in general: two V-shapes side by side make a W.
The definition says: on the interval between two points, a convex graph lies below the chord joining them. Differentiable convex functions also lie above every tangent, over their whole domain:
\[ f(x) \ge f(a) + f'(a)(x - a) \quad \text{for all } x. \]Why: the slope of a chord from \(a\) to \(x\) grows as \(x\) moves right (that is what “bends upward” means), and as \(x \to a\) it tends to \(f'(a)\). So for \(x > a\) the chord slope \(\frac{f(x) - f(a)}{x - a}\) is at least \(f'(a)\), and for \(x < a\) at most \(f'(a)\); multiplying out gives the tangent inequality on both sides. For \(e^x\) at \(a = 0\) it is the inequality of section 4.4:
\[ e^x \ge 1 + x. \]Now take \(e^x\) on the interval \([-\delta, \delta]\). The chord joins \((-\delta, e^{-\delta})\) to \((\delta, e^{\delta})\). Its height at the middle is the average of the ends, \(\tfrac12(e^\delta + e^{-\delta}) = \cosh\delta\), and its slope is \(\frac{e^\delta - e^{-\delta}}{2\delta} = \frac{\sinh\delta}{\delta}\) (section 4.5). So on that interval \(e^x\) is squeezed between two lines:
\[ 1 + x \;\le\; e^x \;\le\; \cosh\delta + \frac{x}{\delta}\sinh\delta \qquad (-\delta \le x \le \delta). \]The paper writes the chord as \(\frac{\delta + x}{2\delta} e^{\delta} + \frac{\delta - x}{2\delta} e^{-\delta}\) (its equation 58): the two end values weighted by how close \(x\) is to each end. Expand it and it is the same line. For \(\delta = 1\):
| \(x\) | \(1 + x\) | \(e^x\) | chord |
|---|---|---|---|
| −1 | 0 | 0.36788 | 0.36788 |
| −0.5 | 0.5 | 0.60653 | 0.95548 |
| 0 | 1 | 1 | 1.54308 |
| 0.5 | 1.5 | 1.64872 | 2.13068 |
| 1 | 2 | 2.71828 | 2.71828 |
The chord touches \(e^x\) at \(\pm\delta\), the tangent at 0, and in between the gaps are widest near the middle (a check on 200 001 points finds no violation of either inequality). Outside \([-\delta, \delta]\) the chord's extension lies below \(e^x\): the chord bound holds only on the interval it spans, which is why the paper needs every logit deviation inside \([-\delta_C, \delta_C]\).
Checking the definition for all \(x, y, \lambda\) is not practical by hand. There is a test you can run on a formula:
Theorem. If \(f'' \ge 0\) on an interval, \(f\) is convex there. If \(f'' > 0\), it is strictly convex [spivak].
\(f'' \ge 0\) means \(f'\) never decreases: the slope only turns upward. Then for \(a < c < b\) the mean value theorem (section 2.8) gives a point in \((a, c)\) and one in \((c, b)\) where \(f'\) equals the chord slopes of the two halves; the right one is no smaller, so the curve bends up and stays under the chord from \(a\) to \(b\). The examples:
| \(f\) | \(f''\) | convex where |
|---|---|---|
| \(x^2\) | \(2\) | everywhere |
| \(e^x\), \(\cosh x\) | \(e^x\), \(\cosh x\) | everywhere |
| \(-\ln x\) | \(1/x^2\) | \(x > 0\) |
| \(\ln(1 + e^x)\) | \(\sigma(x)(1 - \sigma(x))\), \(\sigma(x) = 1/(1 + e^{-x})\) | everywhere |
| \(x^3\) | \(6x\) | \(x \ge 0\) only |
| \(\sqrt{x}\) | \(-\tfrac14 x^{-3/2}\) | nowhere (concave) |
\(|x|\) is convex but has no derivative at 0, so the test does not apply there; the definition does. A random test of the definition at 100 000 triples \((x, y, \lambda)\) finds no point above a chord for the first four rows, while \(x^3\) on \([-3, 3]\) rises above a chord by up to 13.5 and \(\sqrt x\) on \([0.1, 5]\) by 0.35.
For a function of several variables the test becomes: the matrix of second partial derivatives (the Hessian) has \(z^\top H z \ge 0\) for every direction \(z\). Equivalently, \(f\) is convex if its restriction to every line, \(t \mapsto f(x + t z)\), is convex. That is how log-sum-exp is proved convex in [boyd-cvx], chapter 3.
The definition of convexity is about two points. Jensen's inequality is the same statement for any number of points and any weights.
Theorem (Jensen). Let \(f\) be convex on an interval containing \(x_1, \dots, x_n\), and let \(w_i \ge 0\) with \(\sum_i w_i = 1\). Then
\[ f\Big(\sum_{i=1}^n w_i x_i\Big) \;\le\; \sum_{i=1}^n w_i f(x_i). \]If \(f\) is strictly convex, equality holds only when all \(x_i\) with \(w_i > 0\) are equal [hlp].
“\(f\) of the average is at most the average of \(f\).” In the language of chapter 12, \(f(\mathbb{E}X) \le \mathbb{E} f(X)\).
The proof, for differentiable \(f\), is the tangent line. Let \(\bar x = \sum_i w_i x_i\). Each \(f(x_i)\) is above the tangent at \(\bar x\): \(f(x_i) \ge f(\bar x) + f'(\bar x)(x_i - \bar x)\). Multiply by \(w_i\) and add. The linear term averages to \(f'(\bar x)(\sum_i w_i x_i - \bar x) = 0\), leaving \(\sum_i w_i f(x_i) \ge f(\bar x)\). Nothing else is needed: a linear function commutes with averaging, so whatever lies above a line on average lies above that line at the average.
Three uses recur in this book.
Corollary (AM–GM). For \(x_i > 0\) and weights \(w_i \ge 0\) summing to 1, \(\prod_i x_i^{w_i} \le \sum_i w_i x_i\). With equal weights: \(\sqrt[n]{x_1 \cdots x_n} \le \frac{1}{n}(x_1 + \dots + x_n)\).
The geometric mean never exceeds the arithmetic mean: for \(1, 4, 16\) it is \(\sqrt[3]{64} = 4\), well below the arithmetic mean 7. It reappears in section 13.6 as the right way to average speedups.
For \(x \in \mathbb{R}^n\), the log-sum-exp is
\[ \operatorname{LSE}(x) = \ln \sum_{i=1}^n e^{x_i}. \]It is the logarithm of a softmax denominator: with logits \(s_i\), the paper's total mass is \(Z = \sum_j e^{s_j} = e^{\operatorname{LSE}(s)}\). Two facts about it matter.
It is a smooth maximum. Let \(M = \max_i x_i\). The sum \(\sum_i e^{x_i}\) contains the term \(e^M\) and has \(n\) terms none larger than \(e^M\), so \(e^M \le \sum_i e^{x_i} \le n e^M\). Taking logs:
\[ \max_i x_i \;\le\; \operatorname{LSE}(x) \;\le\; \max_i x_i + \ln n. \]The left end is reached when one entry dominates, the right end when all are equal. Scaling sharpens it: \(T \operatorname{LSE}(x / T)\) is within \(T \ln n\) of the max, so it tends to the max as the temperature \(T \to 0\) (section 12.6). Unlike the max, LSE has a derivative everywhere, and its gradient is the softmax of \(x\) (section 7.6).
It is convex. The Hessian argument is in [boyd-cvx], chapter 3. For \(n = 2\) you can see it: \(\operatorname{LSE}(0, x) = \ln(1 + e^x)\), the “softplus”, whose second derivative \(\sigma(x)(1 - \sigma(x))\) is positive (table in section 11.3). It is a rounded corner of \(\max(0, x)\):
An example: for \(x = (1, 3, 2.5, -1)\), \(\operatorname{LSE}(x) = 3.5654\), between \(\max = 3\) and \(3 + \ln 4 = 4.3863\); at \(T = 0.1\), \(T \operatorname{LSE}(x/T) = 3.00067\). Computed as written, \(e^{x_i}\) overflows for \(x_i > 709\). Shifting fixes it: \(\operatorname{LSE}(x + c) = \operatorname{LSE}(x) + c\) for any constant \(c\), so \(\operatorname{LSE}(x) = M + \ln \sum_i e^{x_i - M}\), where every exponent is \(\le 0\) and one term is exactly 1. For \(x = (1000, 999, 0)\) this gives \(1000 + \ln(1 + e^{-1}) \approx 1000.3133\) where the direct form returns infinity. Section 15.2 builds softmax on this.
The paper notes that a cluster's representative “has logit \(s_C + \log n_C\)”: its approximate mass \(\hat z_C = n_C e^{s_C}\) has logarithm \(s_C + \ln n_C\). The exact mass has logarithm \(\ln z_C = \operatorname{LSE}_{i \in C}(s_i)\). The smooth-max bound puts it below \(\max_{i \in C} s_i + \ln n_C\); Jensen puts it above \(s_C + \ln n_C\) (next section). Their difference is \(\ln m_C\), the log of the mass correction.
This section is the one the paper leans on, and section 4.6 previewed it. Combine the chord and the tangent of \(e^x\) on \([-\delta, \delta]\) (section 11.2) with averaging (section 11.4):
Lemma. Let \(x_1, \dots, x_n \in [-\delta, \delta]\) and weights \(w_i \ge 0\), \(\sum_i w_i = 1\), with \(\sum_i w_i x_i = 0\). Then
\[ 1 \;\le\; \sum_{i=1}^n w_i e^{x_i} \;\le\; \cosh\delta. \]The lower bound is Jensen: \(\sum_i w_i e^{x_i} \ge e^{\sum_i w_i x_i} = e^0 = 1\). The upper bound averages the chord: each \(e^{x_i} \le \cosh\delta + \frac{x_i}{\delta}\sinh\delta\), and the weighted average of the right side is \(\cosh\delta + \frac{\sinh\delta}{\delta}\sum_i w_i x_i = \cosh\delta\). Again the linear part averages to the line's value at the mean.
The upper bound is reached when every \(x_i\) sits at an end, \(\pm\delta\), half the weight on each: then the average of \(e^{x_i}\) is \(\tfrac12(e^\delta + e^{-\delta}) = \cosh\delta\) exactly. The values may spread anywhere in the interval; the worst case puts them all at its ends.
Now the paper's notation (Appendix A.1). A query \(q\), a cluster \(C\) of \(n_C = |C|\) keys with mean \(\bar k_C\). The token logits and the representative logit are
\[ s_i = \frac{q^\top k_i}{\sqrt d}, \qquad s_C = \frac{q^\top \bar k_C}{\sqrt d}. \]Under full attention the cluster's unnormalized mass is \(z_C = \sum_{i \in C} e^{s_i}\). Replacing every key by \(\bar k_C\) gives \(\hat z_C = n_C e^{s_C}\) instead. Their ratio is the mass correction factor (the paper's equations 16 and 50):
\[ m_C = \frac{z_C}{\hat z_C} = \frac{1}{n_C} \sum_{i \in C} e^{s_i - s_C}. \]It says by what factor the representative misjudges the cluster's share of the softmax before normalization: \(m_C = 1\) means the representative carries exactly the right mass, \(m_C = 1.2\) means the true cluster has 20% more mass than the representative claims. Set \(x_i = s_i - s_C\). Because the dot product is linear, the mean of the \(s_i\) over the cluster is \(q^\top \bar k_C / \sqrt d = s_C\), so the \(x_i\) have mean 0 (equal weights \(1/n_C\)). The logit spread \(\delta_C = \max_{i \in C} |s_i - s_C|\) (the paper's equation 20) puts every \(x_i\) in \([-\delta_C, \delta_C]\). The lemma applies:
Lemma A.2 of [refinekv] (cluster mass correction bound). \(\;1 \le m_C \le \cosh(\delta_C)\).
The paper proves the upper bound exactly as above, by averaging the secant line of its equation 58. The lower bound is its remark under equation 38 that \(\Delta z_C = z_C - \hat z_C \ge 0\) by Jensen: a representative never overestimates its cluster's mass. Since \(\cosh\delta_C - 1 \approx \delta_C^2/2\) (section 4.6), a cluster whose logits spread by \(\delta_C = 0.1\) has its mass misjudged by at most 0.5%: the error is second order in the spread. Seeing the lemma on a cluster shows something the proof does not: how far a typical cluster sits from the worst case. The program builds one cluster of 32 random keys in \(d = 64\):
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#define D 64 /* key dimension d */
#define NC 32 /* cluster size n_C */
static double unif(void) { return 2.0 * rand() / RAND_MAX - 1; } /* in [-1, 1] */
int main(void)
{
double q[D], k[NC][D], kbar[D] = { 0 }, sC = 0, smean = 0, dC = 0, mC = 0;
srand(4);
for (int j = 0; j < D; j++) q[j] = unif();
for (int i = 0; i < NC; i++)
for (int j = 0; j < D; j++) { k[i][j] = unif(); kbar[j] += k[i][j] / NC; }
for (int j = 0; j < D; j++) sC += q[j] * kbar[j] / sqrt(D); /* representative logit */
for (int i = 0; i < NC; i++) {
double si = 0;
for (int j = 0; j < D; j++) si += q[j] * k[i][j] / sqrt(D); /* token logit s_i */
smean += si / NC;
if (fabs(si - sC) > dC) dC = fabs(si - sC); /* delta_C */
mC += exp(si - sC) / NC; /* m_C = z_C / zhat_C */
}
printf("s_C = %.6f, mean of s_i = %.6f\n", sC, smean);
printf("delta_C = %.4f: 1 <= m_C = %.4f <= cosh(delta_C) = %.4f\n", dC, mC, cosh(dC));
return 0;
}
s_C = 0.076904, mean of s_i = 0.076904 delta_C = 1.0785: 1 <= m_C = 1.0752 <= cosh(delta_C) = 1.6401
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 paper. kbar is \(\bar k_C\) (equation 5), accumulated as \(k_i / n_C\). sC is computed from \(\bar k_C\) alone: the representative's logit, the only thing a cluster-level method can afford per query. The loop over tokens computes the \(s_i\) that RefineKV is trying not to compute (it would mean reading every key), here only to measure the truth. The program never forms \(z_C\) or \(\hat z_C\): it averages \(e^{s_i - s_C}\), which is the same ratio with \(e^{s_C}\) divided out of both. That keeps the exponents near 0, where nothing overflows, and makes visible that only the deviations \(x_i = s_i - s_C\) matter. The first line checks \(s_C = \frac{1}{n_C}\sum_i s_i\), the zero-mean condition the lemma needs, which holds because the dot product is linear. The second shows the bound is loose here: logits spread fairly evenly over \([-\delta_C, \delta_C]\) give \(m_C = 1.075\) against \(\cosh\delta_C = 1.640\). \(\cosh\delta_C\) is the worst case, reached only when the logits split into two clumps at the ends; over 100 000 random zero-mean clusters the ratio \(m_C / \cosh\delta_C\) came within \(10^{-4}\) of 1 but never exceeded it.
In the paper, \(m_C\) enters the mass-redistribution error through \(\sum_C \hat p_C (m_C - 1) / \sum_C \hat p_C m_C = 1 - 1/\sum_C \hat p_C m_C\) (its equations 19 and 21); because \(t \mapsto 1 - 1/t\) increases, each \(m_C\) may be replaced by its bound \(\cosh\delta_C\). Section 19.3 walks through it.
The paper's second lemma bounds how far softmax weights over logits in \([-\delta, \delta]\) can be from uniform (section 12.5 states it; section 19.5 uses it). Its proof has one step that is pure convexity. Scale the weights to \(r_i = n w_i\), so the \(r_i\) average to 1. Let \(a = \min_i r_i\) and \(b = \max_i r_i\); since the average is 1, \(0 < a \le 1 \le b\). The claim (the paper's equation 72) is
\[ \frac12 \cdot \frac1n \sum_{i=1}^n |r_i - 1| \;\le\; \frac{(1 - a)(b - 1)}{b - a}. \]The function \(g(r) = |r - 1|\) is convex: a V with its corner at 1. On \([a, b]\) it lies below its chord \(\ell\), the line through \((a, 1 - a)\) and \((b, b - 1)\). Average over \(i\): \(\frac1n \sum_i |r_i - 1| \le \frac1n \sum_i \ell(r_i) = \ell(1)\), because \(\ell\) is linear and the \(r_i\) average to 1. The chord's height at 1 is
\[ \ell(1) = (1 - a) + \frac{(b - 1) - (1 - a)}{b - a}(1 - a) = \frac{2(1 - a)(b - 1)}{b - a}. \]That is the claim. It is the cosh lemma's argument with a V in place of the exponential.
Equality holds when every \(r_i\) is at \(a\) or \(b\): a two-point distribution, a fraction \(\frac{1 - a}{b - a}\) of them at \(b\) (that fraction makes the mean 1).
The extremal step. The softmax only tells us the ratio: \(w_i / w_j = e^{x_i - x_j} \le e^{2\delta}\) (the paper's equation 67), so \(R = b / a \le e^{2\delta}\). The worst \(a\) for a given \(R\) is found by calculus. With \(b = R a\),
\[ \frac{(1 - a)(Ra - 1)}{Ra - a} = \frac{R + 1 - Ra - 1/a}{R - 1}, \]whose derivative in \(a\) is zero where \(R = 1/a^2\): at \(a = 1/\sqrt R\), \(b = \sqrt R\). The worst case is symmetric on a log scale: \(a\) and \(b\) are equally far from 1 by a factor. The maximum value is \(\frac{R + 1 - 2\sqrt R}{R - 1} = \frac{(\sqrt R - 1)^2}{(\sqrt R - 1)(\sqrt R + 1)} = \frac{\sqrt R - 1}{\sqrt R + 1}\). That grows with \(R\), so put in the largest ratio, \(\sqrt R = e^\delta\):
\[ \frac{\sqrt R - 1}{\sqrt R + 1} \le \frac{e^\delta - 1}{e^\delta + 1} = \tanh\frac{\delta}{2}. \](Multiply top and bottom by \(e^{-\delta/2}\) to see the tanh, section 4.5.) Undoing the scaling, \(\sum_i |w_i - \tfrac1n| = \tfrac1n \sum_i |r_i - 1| \le 2\tanh(\delta/2)\).
Each step checks numerically. Over 100 000 random arrays \(r\) with mean 1, \(\frac12 \cdot \frac1n\sum|r_i - 1|\) reaches at most 0.99995 of the chord bound; the two-point case \(a = 0.5\), \(b = 3\) meets it exactly (0.4). A brute-force search over a million values of \(a\) finds the maximizer of the bound at a fixed ratio:
| \(R = b/a\) | best \(a\) found | \(1/\sqrt R\) | maximum found | \((\sqrt R - 1)/(\sqrt R + 1)\) |
|---|---|---|---|---|
| 2 | 0.70711 | 0.70711 | 0.171573 | 0.171573 |
| 8 | 0.35355 | 0.35355 | 0.477592 | 0.477592 |
| 32 | 0.17678 | 0.17678 | 0.699558 | 0.699558 |
Watch out. Both lemmas are worst-case bounds, tight only when the logits pile up at \(\pm\delta\). Real clusters are usually far from that, so the paper's error bound overestimates the true error, sometimes by a lot. That is acceptable for its purpose, ranking clusters by risk, and section 13.5 looks at how the paper measured whether the ranking is right.
\(x^4\): \(f'' = 12x^2 \ge 0\), convex. \(e^{-x}\): \(f'' = e^{-x} > 0\), convex. \(\ln(1 + e^x)\): \(f'' = \sigma(x)(1 - \sigma(x)) > 0\), convex. \(\sin x\): \(f'' = -\sin x\) changes sign, neither.
Let \(h = \max(f, g)\) and \(z = \lambda x + (1 - \lambda) y\). Then \(f(z) \le \lambda f(x) + (1 - \lambda) f(y) \le \lambda h(x) + (1 - \lambda) h(y)\), and the same for \(g(z)\). So their max \(h(z)\) is also at most \(\lambda h(x) + (1 - \lambda) h(y)\).
Apply it to \(|x_i|\): \(\big(\frac1n \sum |x_i|\big)^2 \le \frac1n \sum x_i^2\), so \(\|x\|_1 \le \sqrt n \, \|x\|_2\). It is the same bound Cauchy–Schwarz gives.
The mean is 0 and \(\delta_C = 0.3\). Pairing \(\pm\) terms, \(m_C = \frac12(\cosh 0.3 + \cosh 0.1) = 1.02517\), below \(\cosh 0.3 = 1.04534\). Only the outer two sit at the ends, so the bound is not reached.
\(\delta^2 / 2 \le 0.01\) gives \(\delta \le \sqrt{0.02} \approx 0.141\). Exactly, \(\cosh 0.141 - 1 = 0.00996\). The approximation is good here: at \(\delta = 0.1\) the two are 0.005004 and 0.005000.
\(\ln \sum e^{x_i + c} = \ln(e^c \sum e^{x_i}) = c + \operatorname{LSE}(x)\). With \(c = -M\): \(\operatorname{LSE}(x) = M + \operatorname{LSE}(x - M)\), which is the trick; no term overflows because every \(x_i - M \le 0\).
\(e^{x_i}\) are \(3, \frac13, \frac13, \frac13\), total 4, so \(w = (\frac34, \frac1{12}, \frac1{12}, \frac1{12})\). Then \(\sum |w_i - \frac14| = \frac12 + 3 \cdot \frac16 = 1\), and \(2\tanh(\frac{\ln 3}{2}) = 2 \cdot \frac{3 - 1}{3 + 1} = 1\). Equality: the \(r_i = 4 w_i\) are \(3\) and \(\frac13\), which is \(a = 1/\sqrt R\), \(b = \sqrt R\) with \(R = 9 = e^{2\delta}\), the extremal case. In general a fraction \(1/(1 + e^\delta)\) of the logits at \(+\delta\) attains the bound.