12. Probability on finite sets

Attention weights are nonnegative and sum to 1: they are a probability distribution over the tokens, and the attention output is an average under it. Seen that way, the paper's tools are standard probability. Its split of the output by clusters is the law of total expectation (section 12.4); its Lemma A.3 bounds a distance between two distributions (section 12.5); and the \(\sqrt d\) in every attention layer comes from the variance of a dot product (section 12.7, used in section 16.4). Everything here lives on a finite set, so a distribution is an array and every integral is a loop. Only the last section, on the normal distribution, needs a density.

12.1 Distributions as arrays

A finite probability space is a set of outcomes \(\Omega = \{1, \dots, n\}\) (tokens, die faces, cache lines) and an array of probabilities.

Definition. A distribution on \(\{1, \dots, n\}\) is an array \(p_1, \dots, p_n\) with \(p_i \ge 0\) and \(\sum_i p_i = 1\). An event is a subset \(A\); its probability is \(P(A) = \sum_{i \in A} p_i\). A random variable is a function of the outcome: an array \(x_1, \dots, x_n\) of the same length, read as “\(X = x_i\) when outcome \(i\) happens”.

The distributions on \(n\) points form the probability simplex of section 11.1. The uniform distribution has \(p_i = 1/n\). A one-hot distribution puts 1 on a single outcome. The attention weights \(\alpha_1, \dots, \alpha_N\) of a query over \(N\) cached tokens are a distribution on the tokens; a random variable is any per-token array: the logit \(s_i\), a coordinate of the value \(v_i\), the indicator of “token \(i\) is in cluster \(C\)”.

“Random” is only a way of speaking. Nothing in attention is drawn at random: the weights are computed. But every fact of this chapter is a fact about weighted sums, and the language of probability names them and comes with their properties ready-made.

12.2 Expectation

Definition. The expectation (mean) of \(X\) under \(p\) is \(\mathbb{E}X = \sum_{i=1}^n p_i x_i\). For a vector-valued \(X\) (an array of vectors) it is the same sum, coordinate by coordinate.

It is the weighted average, a dot product of the probability array with the value array. The attention output

\[ o = \sum_{i=1}^N \alpha_i v_i \]

is the expectation of the value vector when the token is drawn with probabilities \(\alpha\): attention is a soft lookup that returns the average value instead of one entry (section 16.2).

Linearity. For random variables \(X, Y\) on the same space and constants \(a, b, c\): \(\mathbb{E}[aX + bY + c] = a\,\mathbb{E}X + b\,\mathbb{E}Y + c\).

It is the distributive law: \(\sum_i p_i (a x_i + b y_i + c) = a \sum_i p_i x_i + b \sum_i p_i y_i + c \sum_i p_i\), and \(\sum_i p_i = 1\). No independence or any other condition is needed. It is why \(s_C\), the logit of the mean key, equals the mean of the logits (section 11.6): \(q^\top k / \sqrt d\) is linear in \(k\). Nonlinear functions do not commute with \(\mathbb{E}\): \(\mathbb{E}[X^2] \ne (\mathbb{E}X)^2\), \(\mathbb{E}\max(X, Y) \ne \max(\mathbb{E}X, \mathbb{E}Y)\), and for convex \(f\), Jensen says which way the gap goes: \(f(\mathbb{E}X) \le \mathbb{E}f(X)\) (section 11.4).

Two random variables \(X, Y\) need a joint distribution: probabilities \(p_{xy}\) for the pairs. They are independent if \(p_{xy} = P(X = x)\,P(Y = y)\) for all pairs: knowing one tells nothing about the other. Two fair dice thrown separately are independent, with 36 equally likely pairs. For independent variables, \(\mathbb{E}[XY] = \mathbb{E}X \, \mathbb{E}Y\) (the double sum factors).

12.3 Variance

The mean says where a distribution sits. The variance says how far it spreads around that place.

Definition. \(\operatorname{Var}X = \mathbb{E}\big[(X - \mathbb{E}X)^2\big] = \mathbb{E}[X^2] - (\mathbb{E}X)^2\). The standard deviation is \(\sigma = \sqrt{\operatorname{Var}X}\), in the units of \(X\). The covariance of \(X\) and \(Y\) is \(\operatorname{Cov}(X, Y) = \mathbb{E}\big[(X - \mathbb{E}X)(Y - \mathbb{E}Y)\big]\).

The second form of the variance follows by expanding the square and using linearity, with \(\mu = \mathbb{E}X\): \(\mathbb{E}[X^2 - 2\mu X + \mu^2] = \mathbb{E}[X^2] - 2\mu^2 + \mu^2\). That the variance is \(\ge 0\) is Jensen's inequality for \(x^2\). The rules:

One fair die, and two independent dice (36 equally likely pairs), worked by enumeration:

Variable\(\mathbb{E}\)\(\operatorname{Var}\)by the rules
\(X\), one die3.5\(35/12 \approx 2.9167\)
\(X + Y\)7\(35/6 \approx 5.8333\)variances add (independent)
\(3X - 2\)8.526.25\(3 \cdot 3.5 - 2\); \(9 \cdot 35/12\)
\(\max(X, Y)\)\(161/36 \approx 4.472\)not \(\max(3.5, 3.5)\): max is not linear

In the machine. \(\mathbb{E}[X^2] - (\mathbb{E}X)^2\) is a subtraction of two nearly equal numbers when the mean is large and the spread small, and loses digits to cancellation (section 14.4). Compute the mean first and then average \((x_i - \mu)^2\) (two passes); section 13.2 shows the failure.

12.4 Conditioning and mixtures

Restricting attention to an event \(B\) and renormalizing gives a new distribution.

Definition. For an event \(B\) with \(P(B) > 0\), the conditional distribution given \(B\) is \(p_{i \mid B} = p_i / P(B)\) for \(i \in B\) and 0 outside. The conditional expectation is \(\mathbb{E}[X \mid B] = \sum_{i \in B} p_{i \mid B}\, x_i\), the mean of \(X\) over \(B\) alone.

Now split \(\Omega\) into disjoint blocks \(C_1, \dots, C_m\) covering it (a partition, section 18.1). Each outcome is in exactly one block, so the overall mean is the block means weighted by the block probabilities:

Law of total expectation. \(\displaystyle \mathbb{E}X = \sum_{C} P(C)\,\mathbb{E}[X \mid C].\)

Proof: \(\sum_C P(C) \sum_{i \in C} \frac{p_i}{P(C)} x_i = \sum_C \sum_{i \in C} p_i x_i = \sum_i p_i x_i\). As an algorithm: to draw an outcome, first draw a block \(C\) with probability \(P(C)\), then an outcome inside it from the conditional distribution. A distribution described this way is a mixture: the \(P(C)\) are the mixing weights, and the conditional distributions are its components.

The paper's split of the attention output

This is exactly how [refinekv] (Section 3.2 and Appendix A.1) writes attention over a partition \(\mathcal{C}\) of the KV cache into clusters. In its notation, with \(s_i = q^\top k_i / \sqrt d\), \(z_C = \sum_{i \in C} e^{s_i}\) and \(Z = \sum_C z_C\):

\[ p_C = \frac{z_C}{Z} = \sum_{i \in C} \alpha_i, \qquad w_{i \mid C} = \frac{e^{s_i}}{z_C} = \frac{\alpha_i}{p_C}, \qquad v_C = \sum_{i \in C} w_{i \mid C}\, v_i, \qquad o = \sum_{C \in \mathcal{C}} p_C\, v_C. \]
p = 0.199 p = 0.801 0.319
Figure 12.1. Attention weights \(\alpha_i\) of one query over 8 tokens in two clusters of four (the data of the program below). A cluster's mass \(p_C\) is the sum of its bars; \(w_{i \mid C}\) is a bar's share of its own cluster.

The identity is exact, but it is worth seeing in floating point, with the paper's quantities computed the way the paper defines them. The program takes 8 tokens with 2-dimensional keys, a query, 2-dimensional values, and two clusters of four:

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

#define N 8      /* tokens */
#define D 2      /* key dimension d */
#define B 4      /* cluster size: C = {0..3} and {4..7} */

int main(void)
{
    double q[D] = { 1.5, -0.5 };
    double k[N][D] = { { 1, 0 }, { 0.5, 1 }, { 1.5, 0.5 }, { -1, 0 },
                       { 2, -1 }, { 1.8, -0.6 }, { 0, 2 }, { 2.4, -0.2 } };
    double v[N][2] = { { 1, 0 }, { 0, 1 }, { 1, 1 }, { -1, 0 },
                       { 0, -1 }, { 2, 0 }, { 1, -1 }, { 0.5, 0.5 } };
    double s[N], Z = 0, o[2] = { 0, 0 }, mix[2] = { 0, 0 };
    for (int i = 0; i < N; i++) {
        s[i] = (q[0] * k[i][0] + q[1] * k[i][1]) / sqrt(D);   /* s_i = q.k_i / sqrt d */
        Z += exp(s[i]);
    }
    for (int i = 0; i < N; i++) {                               /* o = sum alpha_i v_i */
        double alpha = exp(s[i]) / Z;
        o[0] += alpha * v[i][0];
        o[1] += alpha * v[i][1];
    }
    for (int c = 0; c < N / B; c++) {
        double zC = 0, vC[2] = { 0, 0 };
        for (int i = c * B; i < c * B + B; i++) zC += exp(s[i]);
        for (int i = c * B; i < c * B + B; i++) {               /* w_{i|C} = e^{s_i} / z_C */
            vC[0] += exp(s[i]) / zC * v[i][0];
            vC[1] += exp(s[i]) / zC * v[i][1];
        }
        double pC = zC / Z;
        printf("cluster %d: p_C = %.4f, v_C = (%.4f, %.4f)\n", c, pC, vC[0], vC[1]);
        mix[0] += pC * vC[0];
        mix[1] += pC * vC[1];
    }
    printf("sum alpha_i v_i = (%.15f, %.15f)\n", o[0], o[1]);
    printf("sum p_C v_C     = (%.15f, %.15f)\n", mix[0], mix[1]);
    return 0;
}
cluster 0: p_C = 0.1989, v_C = (0.7792, 0.6213)
cluster 1: p_C = 0.8011, v_C = (0.6982, -0.1608)
sum alpha_i v_i = (0.714347323019728, -0.005220981872090)
sum p_C v_C     = (0.714347323019728, -0.005220981872090)

How it maps to the formulas. The first loop computes the logits \(s_i = q^\top k_i / \sqrt d\) and the normalizer \(Z = \sum_j e^{s_j}\): one pass over all keys, as full attention must. The second loop forms \(\alpha_i = e^{s_i} / Z\) and accumulates \(o = \sum_i \alpha_i v_i\), the expectation of \(v\) under \(\alpha\). The cluster loop never uses \(\alpha\) or \(Z\) to build \(v_C\): it normalizes inside the cluster by \(z_C\), so \(e^{s_i} / z_C\) is \(w_{i \mid C}\), the conditional distribution, and the inner sum is \(v_C = \mathbb{E}[v \mid C]\). Only then does \(Z\) enter, through \(p_C = z_C / Z\), the mixing weight. That split is the point: \(v_C\) depends only on the cluster's own logits, \(p_C\) on how the cluster compares with everything else, which is why the paper can bound the two sources of error separately. The two printed outputs agree to the last digit shown. The cluster masses add to 1, and the second cluster holds 80% of the attention because its keys point along \(q\).

The paper's approximation \(\hat o = \sum_C \hat p_C \bar v_C\) (equation 11) has the same mixture shape with both ingredients replaced: the mixing weights by \(\hat p_C\), the masses the representatives \((\bar k_C, \bar v_C)\) earn, and the component means by \(\bar v_C\), the plain average of the cluster's values, which is the expectation under the uniform distribution on \(C\). Its error decomposition (equation 14),

\[ o - \hat o = \underbrace{\sum_C (p_C - \hat p_C)\, v_C}_{\text{wrong mixing weights}} \;+\; \underbrace{\sum_C \hat p_C\, (v_C - \bar v_C)}_{\text{wrong component means}}, \]

is obtained by adding and subtracting \(\tilde o = \sum_C \hat p_C v_C\). The first part is controlled by the mass correction \(m_C\) (section 11.6). The second is a difference between two expectations of the same values under two distributions on \(C\), \(w_{\cdot \mid C}\) and uniform. The next section measures that.

12.5 Total variation distance

How different are two distributions \(p\) and \(q\) on the same \(n\) points? The most useful answer for bounding averages is:

Definition. The total variation distance is \(d_{TV}(p, q) = \frac12 \sum_{i=1}^n |p_i - q_i|\).

The \(\frac12\) is there because the differences cancel in total: \(\sum_i (p_i - q_i) = 0\), so the points where \(p\) exceeds \(q\) hold exactly as much excess as the points where \(q\) exceeds \(p\). \(d_{TV}\) is that excess: the amount of probability you would have to move to turn \(p\) into \(q\). It is 0 for equal distributions and 1 for distributions on disjoint points. Equivalently, it is the largest difference the two can make to the probability of any event [levin-peres]:

\[ d_{TV}(p, q) = \max_{A \subseteq \Omega} |P(A) - Q(A)|, \]

reached by \(A = \{i : p_i > q_i\}\). What makes it useful is a bound on expectations. For any array \(f\),

\[ \Big|\sum_i p_i f_i - \sum_i q_i f_i\Big| = \Big|\sum_i (p_i - q_i) f_i\Big| \le \sum_i |p_i - q_i| \, |f_i| \le 2\, d_{TV}(p, q) \max_i |f_i|. \]

For vectors \(v_i\) the same steps with the triangle inequality (section 10.2) give \(\big\|\sum_i (p_i - q_i) v_i\big\|_2 \le \max_i \|v_i\|_2 \sum_i |p_i - q_i|\). With \(p = w_{\cdot \mid C}\) and \(q\) uniform on \(C\), this is the paper's step from equation 25 to 27:

\[ \|v_C - \bar v_C\|_2 \le V_C \sum_{i \in C} \Big| w_{i \mid C} - \frac{1}{n_C} \Big|, \qquad V_C = \max_{i \in C} \|v_i\|_2. \]

The sum on the right is the \(L_1\) distance to uniform, \(\sum_i |w_i - \frac1n| = 2\, d_{TV}(w, \text{uniform})\). How large can it be when \(w\) is a softmax of logits that differ little?

Lemma A.3 of [refinekv] (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}\), then

\[ \sum_{i=1}^n \Big| w_i - \frac1n \Big| \le 2 \tanh\frac{\delta}{2}. \]

Applied with \(x_i = s_i - s_C\) and \(\delta = \delta_C\) (shifting all logits by \(s_C\) does not change the softmax, section 12.6), it gives the paper's equation 30: \(\|v_C - \bar v_C\|_2 \le 2 V_C \tanh(\delta_C / 2)\). For small \(\delta\), \(2\tanh(\delta/2) \approx \delta\): logits within \(\delta = 0.2\) of each other's center give \(\sum_i |w_i - \frac1n| \le 0.199\), so \(d_{TV} \le 0.0997\): less than 10% of the mass is moved away from uniform. The proof is a convexity argument, done in section 11.6 and assembled in section 19.5.

The bound cannot be improved. Its worst case puts a fraction \(1/(1 + e^\delta)\) of the logits at \(+\delta\) and the rest at \(-\delta\) (exercise 7 of chapter 11). A numerical check over 100 000 random logit arrays (\(n\) up to 64, half of each pushed to \(\pm\delta\)) and that worst case, \(n = 1000\):

\(\delta\)\(2\tanh(\delta/2)\)random: largest \(\sum|w_i - \frac1n|\) / boundworst case: \(\sum|w_i - \frac1n|\)
0.250.2487060.99970.248706 (438 of 1000 at \(+\delta\))
10.9242340.99860.924234 (269 of 1000)
41.9280550.92811.928055 (18 of 1000)

The same check confirms the two forms of \(d_{TV}\) on an 8-point example (both 0.35, the maximum taken over all 256 events) and that \(f = \operatorname{sign}(p - q)\) attains the expectation bound.

12.6 Softmax as a distribution

Softmax turns any real array (logits, scores) into a distribution:

\[ w_i = \frac{e^{s_i / T}}{\sum_{j=1}^n e^{s_j / T}}, \qquad T > 0. \]

Each \(w_i\) is positive, they sum to 1, and a larger logit gets a larger weight. Physics met this form first: the Gibbs or Boltzmann distribution gives a state of energy \(E_i\) the probability \(\propto e^{-E_i / kT}\) at temperature \(T\). Logits play the role of negative energies, and \(T\) keeps its name: temperature. Three properties carry the rest of the book.

Shift invariance is exact in \(\mathbb{R}\). In doubles it is exact only while no exponential overflows, and running the formula shows where that ends:

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

#define N 4

static void softmax(const double *s, double T, double *w)   /* as written: no max trick */
{
    double z = 0;
    for (int i = 0; i < N; i++) z += exp(s[i] / T);
    for (int i = 0; i < N; i++) w[i] = exp(s[i] / T) / z;
}

int main(void)
{
    double s[N] = { 2.0, 1.0, 0.5, -1.0 }, w[N];
    double Ts[] = { 0.1, 0.5, 1, 2, 10, 100 };
    for (int t = 0; t < 6; t++) {
        softmax(s, Ts[t], w);
        printf("T = %-5g w = %.4f %.4f %.4f %.4f\n", Ts[t], w[0], w[1], w[2], w[3]);
    }
    double sh[N], w2[N], diff = 0;
    for (int i = 0; i < N; i++) sh[i] = s[i] - 3.7;       /* shift every logit */
    softmax(s, 1, w); softmax(sh, 1, w2);
    for (int i = 0; i < N; i++) if (fabs(w[i] - w2[i]) > diff) diff = fabs(w[i] - w2[i]);
    printf("shift by -3.7: max weight change %.3g\n", diff);
    for (int i = 0; i < N; i++) sh[i] = s[i] + 1000;
    softmax(sh, 1, w2);
    printf("shift by +1000: w = %g %g %g %g\n", w2[0], w2[1], w2[2], w2[3]);
    printf("ln(w0/w1) = %.12f, s0 - s1 = %g\n", log(w[0] / w[1]), s[0] - s[1]);
    return 0;
}
T = 0.1   w = 1.0000 0.0000 0.0000 0.0000
T = 0.5   w = 0.8420 0.1140 0.0419 0.0021
T = 1     w = 0.6095 0.2242 0.1360 0.0303
T = 2     w = 0.4344 0.2635 0.2052 0.0969
T = 10    w = 0.2852 0.2581 0.2455 0.2113
T = 100   w = 0.2534 0.2509 0.2497 0.2460
shift by -3.7: max weight change 1.11e-16
shift by +1000: w = nan nan nan nan
ln(w0/w1) = 1.000000000000, s0 - s1 = 1

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.

The function is the definition, term for term: exp(s[i] / T) is \(e^{s_i/T}\), z the denominator, and dividing by it is what makes the array a distribution. It deliberately skips the max trick, to show what the trick is for. The temperature loop shows the two limits: at \(T = 0.1\) the logit gap of 1 between the first two entries becomes a factor \(e^{10} \approx 22\,000\) and the output is one-hot to four digits; at \(T = 100\) every ratio \(w_i / w_j = e^{(s_i - s_j)/100}\) is within 3.1% of 1 (the widest gap, \(s_0 - s_3 = 3\), gives \(e^{0.03}\)). The last line checks the log-ratio identity: only the difference \(s_0 - s_1\) survives. Shifting every logit by \(-3.7\) changes the weights by one rounding error. Shifting by \(+1000\) changes nothing mathematically, but \(e^{1001}\) overflows to infinity and \(\infty / \infty\) is NaN: the formula is right and the evaluation order is wrong. Since any shift is allowed, shift by \(-\max_i s_i\) first; section 15.2 does it.

12.7 Random vectors and their dot products

A random vector is an array of random variables, \(q = (q_1, \dots, q_d)\). Its expectation is taken coordinate by coordinate. Why does attention divide \(q^\top k\) by \(\sqrt d\)? Model the coordinates of a query and a key as independent, each with mean 0 and variance 1, and look at

\[ q^\top k = \sum_{j=1}^d q_j k_j. \]

Fact. If the \(2d\) coordinates of \(q\) and \(k\) are independent with mean 0 and variance 1, then \(\mathbb{E}[q^\top k] = 0\) and \(\operatorname{Var}(q^\top k) = d\). So \(q^\top k / \sqrt d\) has variance 1 for every \(d\).

The standard deviation of a raw dot product grows like \(\sqrt d\): about 11 for \(d = 128\). Softmax at temperature 1 over logits that spread so widely is nearly one-hot: over 1024 such random logits the largest weight averages 0.80, against 0.015 for logits of variance 1 (simulated). A near one-hot softmax has nearly vanishing gradients. Dividing by \(\sqrt d\) keeps the logits at unit scale whatever the head dimension [vaswani]. Section 16.4 continues from here.

A simulation of 200 000 pairs per row agrees (normal coordinates drawn with the Box–Muller method of section 12.8; also coordinates that are \(\pm 1\) with equal probability, which have the same mean and variance, to show that nothing else about the distribution matters):

\(d\)mean of \(q^\top k\)\(\operatorname{Var}(q^\top k)\), normal\(\operatorname{Var}(q^\top k / \sqrt d)\)\(\operatorname{Var}(q^\top k)\), \(\pm 1\)
160.00915.970.99816.04
64−0.00264.301.00564.58
1280.010127.720.998127.79

Watch out. This is a model at initialization, not a description of a trained network: learned queries and keys are neither independent nor of variance 1, and some coordinates are much larger than others. The \(\sqrt d\) is a convention chosen by this argument; the paper's logits \(s_i\) include it, and its bounds hold for any logits.

12.8 Densities and the normal distribution

A quantity like a dot product of random reals can take any value, not one of \(n\). Its distribution is described by a density: a function \(f \ge 0\) with \(\int_{-\infty}^{\infty} f(x)\,dx = 1\), such that

\[ P(a \le X \le b) = \int_a^b f(x)\,dx, \qquad \mathbb{E}X = \int_{-\infty}^{\infty} x f(x)\,dx. \]

Think of a histogram with bins of width \(h\): bin \([x, x + h)\) holds probability about \(f(x) h\), and as \(h \to 0\) the sums over bins become integrals (section 3.1). Every finite formula of this chapter carries over with \(\sum_i p_i\) replaced by \(\int f(x)\,dx\). A single value has probability 0; only intervals have mass.

The standard normal (Gaussian) density is

\[ \varphi(x) = \frac{1}{\sqrt{2\pi}} e^{-x^2/2}. \]

The constant \(1/\sqrt{2\pi}\) is there because \(\int_{-\infty}^\infty e^{-x^2/2}\,dx = \sqrt{2\pi} \approx 2.5066\) (section 3.6). It has mean 0 and variance 1; \(\mu + \sigma X\) is normal with mean \(\mu\) and standard deviation \(\sigma\), written \(N(\mu, \sigma^2)\). About 68.3%, 95.4% and 99.7% of its mass lies within 1, 2 and 3 standard deviations of the mean.

−1 0 1 −2 2 68.3%
Figure 12.2. The standard normal density \(\varphi\). The shaded area, between \(-1\) and 1, is 0.683.

The normal distribution is everywhere because of the central limit theorem: a sum of many independent terms with finite variance, centered and scaled to variance 1, is approximately standard normal, whatever the terms' own distribution [blitzstein]. The dot product \(q^\top k / \sqrt d\) of the previous section is such a sum, so for large \(d\) it is close to \(N(0, 1)\).

To sample it, the Box–Muller method turns two independent uniform numbers \(U_1, U_2\) in \((0, 1)\) into \(\sqrt{-2 \ln U_1}\, \cos(2\pi U_2)\), which is exactly \(N(0, 1)\) [knuth2]. Why it works, in one line: the pair \((\sqrt{-2\ln U_1}\cos 2\pi U_2,\ \sqrt{-2\ln U_1}\sin 2\pi U_2)\) is a point at a uniformly random angle and a radius \(R\) with \(P(R > r) = e^{-r^2/2}\), which is exactly how two independent standard normals look in polar coordinates, since their joint density \(\frac{1}{2\pi}e^{-(x^2 + y^2)/2}\) depends only on the radius. The uniform must avoid 0, where the log is \(-\infty\).

A million Box–Muller samples (uniforms from a 64-bit splitmix generator, not C's rand(), whose quality varies between systems) match the theory:

quantitysampledexact
\(P(|X| \le 1)\)0.68310.6827
\(P(|X| \le 2)\)0.95450.9545
\(P(|X| \le 3)\)0.99730.9973
density on \([0, 0.5)\), at 0.250.38290.3867
density on \([1.5, 2)\), at 1.750.08770.0863
\(P(|q^\top k / \sqrt{128}| \le 1)\), 100 000 pairs0.6826\(\approx 0.6827\) (CLT)

(The exact probabilities are \(\operatorname{erf}(k/\sqrt 2)\). A bin's sampled density is its count over \(\text{samples} \times \text{width}\), compared with \(\varphi\) at its middle; the small gaps are the curvature of \(\varphi\) across the bin.) The midpoint rule with step \(10^{-3}\) on \([-10, 10]\) gives \(\int \varphi = 1.0000000000\) and variance \(1.0000000000\).

Exercises

  1. An indicator variable is 1 with probability \(p\) and 0 otherwise. Find its mean and variance.
    Answer

    \(\mathbb{E}X = p\). \(X^2 = X\), so \(\mathbb{E}X^2 = p\) and \(\operatorname{Var}X = p - p^2 = p(1 - p)\), largest at \(p = \frac12\).

  2. Two clusters have masses \(p_C = 0.3, 0.7\) and conditional value means \(v_C = 1, -1\) (scalars). What is \(o\)? If an approximation gets the means right but uses masses \(0.5, 0.5\), what is the error?
    Answer

    \(o = 0.3 - 0.7 = -0.4\). The approximation gives 0, an error of 0.4, all from the first (mass) term of the decomposition.

  3. What is \(d_{TV}\) between a one-hot distribution on \(n\) points and the uniform one?
    Answer

    \(\frac12\big[(1 - \frac1n) + (n - 1)\frac1n\big] = 1 - \frac1n\). It tends to 1, the largest possible distance.

  4. Show that \(\sum_i |w_i - \frac1n| \le 2\) for any distribution \(w\), so Lemma A.3 is only informative because \(2\tanh(\delta/2) < 2\).
    Answer

    \(\sum_i |w_i - \frac1n| \le \sum_i w_i + \sum_i \frac1n = 2\). And \(\tanh < 1\), so the lemma's bound is below 2 for every finite \(\delta\), and close to \(\delta\) for small \(\delta\).

  5. (The paper.) A cluster has \(\delta_C = 0.2\) and \(V_C = 3\). Bound \(\|v_C - \bar v_C\|_2\).
    Answer

    \(2 V_C \tanh(\delta_C / 2) = 3 \cdot 2\tanh(0.1) = 3 \times 0.1993 = 0.598\).

  6. If the coordinates of \(q\) have variance \(\sigma_q^2\) and those of \(k\) variance \(\sigma_k^2\) (independent, mean 0), what is \(\operatorname{Var}(q^\top k / \sqrt d)\)?
    Answer

    Each term has variance \(\sigma_q^2 \sigma_k^2\); \(d\) of them add to \(d \sigma_q^2 \sigma_k^2\); dividing by \(\sqrt d\) divides the variance by \(d\): \(\sigma_q^2 \sigma_k^2\). The scaling removes the dependence on \(d\), not on the coordinate scales.

  7. Softmax over logits \(s_i\) at temperature \(T\) is the same as softmax at temperature 1 over which logits? What is the temperature of attention as a function of \(q^\top k_i\)?
    Answer

    Over \(s_i / T\). Attention is softmax of \(q^\top k_i\) at temperature \(\sqrt d\).

  8. For \(X\) standard normal, what is \(P(X > 2)\)? Use the 95.4% rule and symmetry.
    Answer

    \(P(|X| > 2) = 1 - 0.9545 = 0.0455\), half of it on each side: 0.0228.

References