Electric Sheaves

Lecture 7 — Tokenization, Sampling, and Scaling Laws

Project connection. Project Step 7 trains a byte-pair-encoding tokenizer, retrains the model on its tokens, implements temperature, top-kk, and nucleus sampling, and pools model sweeps into a small-scale scaling-law plot. BPE measurements below use the course’s 1,115,394-character Shakespeare corpus.

Chapter overview. Tokenization determines the random variables modeled by the network; sampling determines how a modeled distribution is turned back into text; scaling laws describe empirical changes in loss as model, data, and compute grow. In each case, an engineering choice has a precise mathematical formulation.

0. Notation

A tokenizer has a finite token set, called its vocabulary, and maps raw text to a finite sequence of token identifiers. The token count of a corpus is the length of this sequence. Throughout Sections 1 and 2, NchN_{\mathrm{ch}} denotes the number of raw characters and NtokN_{\mathrm{tok}} the number of tokens.

1. Byte-pair encoding

Suppose the current corpus representation is a token sequence (u1,,uN)(u_1,\ldots,u_N). An adjacent pair occurrence of (a,b)(a,b) is an index ii with (ui,ui+1)=(a,b)(u_i,u_{i+1})=(a,b). Replacing occurrences is performed from left to right and uses non-overlapping pairs.

Definition 1.1 (byte-pair encoding). Byte-pair encoding (BPE) starts from a base character or byte vocabulary and the corresponding tokenized corpus. For each of MM rounds:

  1. count every adjacent token pair;
  2. choose a pair with maximal count;
  3. introduce a new token representing its concatenation; and
  4. replace its non-overlapping occurrences from left to right.

The ordered list of chosen pairs is the merge list. To encode new text, start from base tokens and apply the learned merges in training order. Decoding recursively expands every merged token into base tokens and then into text.

Proposition 1.2 (token-count reduction). If one merge replaces rr non-overlapping occurrences of a selected pair, then it reduces the corpus token count by exactly rr. For a pair of distinct tokens, adjacent occurrences cannot overlap, so its adjacent-pair count equals rr and BPE’s choice maximizes the immediate token-count reduction among such pairs.

Proof. Each replacement turns two tokens into one and therefore removes one token. Non-overlap makes the rr reductions disjoint. If aba\neq b, two occurrences of (a,b)(a,b) cannot share a token, because a shared middle token would have to equal both aa and bb. \square

Remark 1.3 (self-overlap). For a pair (a,a)(a,a), the adjacent count can exceed the number of non-overlapping replacements: aaa has two adjacent occurrences of aa but only one left-to-right replacement. Thus BPE is exactly greedy for the adjacent-count surrogate and usually, but not literally in every self-overlapping case, for one-step token reduction. There is no global optimality theorem analogous to Huffman coding. A Huffman code is a prefix code minimizing expected codeword length for a fixed symbol distribution; BPE instead changes the segmentation of an entire corpus, so it solves a different combinatorial problem.

Remark 1.4 (merge order and losslessness). Later tokens are defined in terms of earlier tokens, so applying available merges in a different order can produce a different segmentation. Retaining the learned order makes encoding deterministic. Because every new token stores the pair from which it was formed, recursive expansion proves exact decoding and hence losslessness.

For this corpus, the first ten merges are

 0 'e '   1 'th'   2 't '   3 's '   4 'd '
 5 ', '   6 'ou'   7 'er'   8 'in'   9 'y '

followed by an, :\n, or, o , en, \n\n, and others.

merges MMvocabulary sizetokenscharacters/token
64129743,7561.500
256321568,2101.963
512577487,9612.286
10241089414,3222.692

Corpus aside 1.5. Frequent word-final pairs, the play-format tokens :\n and \n\n, and eventually whole words appear without linguistic annotation. The table also shows diminishing returns: increasing from 256 to 1,024 merges improves characters per token by about 37%, not by a factor of four. Tokens are frequency artifacts, so they may split digits, whitespace, or rare words in ways that do not match human categories.

2. Tokenization and the unit of loss

Suppose a model assigns average log loss (cross-entropy) Ltok\mathcal L_{\mathrm{tok}} nats per token to an encoded corpus. By the coding interpretation of Lecture 1, its total ideal code length is NtokLtokN_{\mathrm{tok}}\mathcal L_{\mathrm{tok}} nats.

Proposition 2.1 (bits-per-character conversion). The same corpus code has rate

bpc=Ltokln2NtokNch\operatorname{bpc} =\frac{\mathcal L_{\mathrm{tok}}}{\ln2} \frac{N_{\mathrm{tok}}}{N_{\mathrm{ch}}}

bits per character.

Proof. Divide the total code length by ln2\ln2 to convert nats to bits, then divide by the number of raw characters. Losslessness ensures that the token sequence and raw corpus describe the same object. \square

Corollary 2.2. If a tokenizer halves the token count, its nats-per-token loss may approximately double without worsening bits per character. Consequently, per-token perplexities are not comparable across different tokenizers.

Project remark 2.3. At M=256M=256, the BPE corpus has 568,210 tokens and vocabulary size 321. Each token carries about 1.96 characters. With the same TmaxT_{\max}, its context window spans roughly twice as many characters, but each prediction is correspondingly more information-rich. Step 7 compares the character and BPE models only after applying Proposition 2.1.

3. Decoding from a predictive distribution

Let zRVz\in\mathbb R^V be the next-token logits and p=softmax(z)p=\operatorname{softmax}(z). A decoding rule maps this conditional distribution to a selected or sampled next token.

For τ>0\tau>0, define the temperature-τ\tau distribution by

pτ=softmax(z/τ).p_\tau=\operatorname{softmax}(z/\tau).

For aΔV1a\in\Delta^{V-1}, let H(a)=bablogabH(a)=-\sum_ba_b\log a_b be its entropy.

Proposition 3.1 (Gibbs variational characterization). For every τ>0\tau>0,

pτ=arg maxaΔV1{a,z+τH(a)}.p_\tau =\operatorname*{arg\,max}_{a\in\Delta^{V-1}} \{\langle a,z\rangle+\tau H(a)\}.

Proof. Put Z=bezb/τZ=\sum_be^{z_b/\tau}. Since log(pτ)b=zb/τlogZ\log(p_\tau)_b=z_b/\tau-\log Z, every aΔV1a\in\Delta^{V-1} satisfies

a,z+τH(a)=τlogZτDKL(apτ).\langle a,z\rangle+\tau H(a) =\tau\log Z-\tau D_{\mathrm{KL}}(a\|p_\tau).

Gibbs inequality makes the right side at most τlogZ\tau\log Z, with equality exactly when a=pτa=p_\tau. Thus the maximizer exists and is unique. \square

Remark 3.2 (temperature limits). As τ\tau\to\infty, entropy dominates and pτp_\tau tends to uniform. As τ0+\tau\to0^+, it concentrates uniformly on the indices attaining maxbzb\max_bz_b; with a unique maximizer it tends to that one-hot distribution. Temperature is therefore an entropy—score tradeoff, exactly as in attention.

Two truncation rules remove part of a distribution’s tail before sampling.

Definition 3.3 (top-kk sampling). For an integer 1kV1\leq k\leq V, keep the kk indices having the largest logits, set all other logits to -\infty, apply softmax, and sample from the resulting distribution.

Definition 3.4 (nucleus sampling). Sort tokens in non-increasing order of probability. For p0(0,1]p_0\in(0,1], keep the smallest prefix whose cumulative probability is at least p0p_0, discard the remaining tokens, renormalize, and sample. This is also called top-pp sampling.

Nucleus sampling adapts the number of retained tokens to the concentration of the current conditional distribution.

Probability aside 3.5 (typical sets and entropy rate). Let X1,X2,X_1,X_2,\ldots be independent and identically distributed (iid) with token law qq, and put Zt=logq(Xt)Z_t=-\log q(X_t). Then E[Zt]=H(q)\mathbb E[Z_t]=H(q). The weak law of large numbers states that averages of iid finite-variance variables converge in probability to their common mean; explicitly, for every ε>0\varepsilon>0,

P(1Tt=1TZtH(q)>ε)0.\mathbb P\left( \left|\frac1T\sum_{t=1}^TZ_t-H(q)\right|>\varepsilon \right)\longrightarrow0.

Since q(X1,,XT)=tq(Xt)q(X_1,\ldots,X_T)=\prod_tq(X_t), this is equivalent to

1Tlogq(X1,,XT)H(q)-\frac1T\log q(X_1,\ldots,X_T)\longrightarrow H(q)

in probability. Hence most mass lies on a typical set of roughly eTH(q)e^{TH(q)} sequences, each with probability about eTH(q)e^{-TH(q)}.

For a dependent source, the long-run quantity replacing H(q)H(q) is its entropy rate

Hrate=limT1TH(X1,,XT).H_{\mathrm{rate}} =\lim_{T\to\infty}\frac1T H(X_1,\ldots,X_T).

A source is stationary if its finite-dimensional laws are invariant under time shifts. For a stationary finite-alphabet source, the rate also equals

Hrate=limTH(XTX<T);H_{\mathrm{rate}} =\lim_{T\to\infty}H(X_T\mid X_{<T});

these conditional entropies decrease as the available context grows. The source is ergodic if long-run sample averages converge to their corresponding expectations. The Shannon—McMillan—Breiman theorem gives stationary ergodic sources the analogous asymptotic equipartition property:

1Tlogp(X1,,XT)Hrate-\frac1T\log p(X_1,\ldots,X_T) \longrightarrow H_{\mathrm{rate}}

almost surely. For an iid source the rate is H(q)H(q); for a stationary first-order Markov chain with transition matrix θ\theta and stationary distribution π\pi, it is aπ(a)H(θa,)\sum_a\pi(a)H(\theta_{a,\cdot}).

Entropy rate is a property of the source, not another model score. Lecture 1’s Lbest\mathcal L_{\mathrm{best}} is the empirical one-step conditional estimate obtained from a fixed corpus. Its bigram value, 3.54 bits per character, lies well above Shannon’s roughly one-bit estimate because one character of context leaves much uncertainty. Increasing empirical context on a fixed corpus eventually drives the training floor toward zero through memorization, not toward the true source rate. The single highest-probability sequence likewise need not resemble a typical draw.

Decoding remark 3.6. Greedy decoding chooses an argmax at every step. Beam search retains a fixed number of prefixes having the largest accumulated log-probabilities and expands only those prefixes; it is a heuristic for finding a high-probability complete sequence. Language models often make these modes repetitive, while moderate sampling yields more typical text. Step 7 records this degeneration directly. The training objective remains likelihood; the decoding rule determines which region of the learned distribution is exposed.

4. Empirical scaling laws

A positive quantity f(s)f(s) obeys a power law in scale ss if f(s)csαf(s)\approx cs^{-\alpha} for constants c,α>0c,\alpha>0. Taking logarithms gives

logf(s)logcαlogs,\log f(s)\approx\log c-\alpha\log s,

so a power law appears as a straight line on log—log axes.

Let NN denote non-embedding parameter count, DD the number of training tokens, and CC the training compute.

Observation 4.1 (Kaplan scaling laws). Across substantial empirical ranges in which other resources are not limiting, language-model loss was fit by relations of the form

L(N)(NcN)αN,L(D)(DcD)αD,L(C)(CcC)αC.L(N)\approx\left(\frac{N_c}{N}\right)^{\alpha_N}, \qquad L(D)\approx\left(\frac{D_c}{D}\right)^{\alpha_D}, \qquad L(C)\approx\left(\frac{C_c}{C}\right)^{\alpha_C}.

Here Nc,Dc,Cc>0N_c,D_c,C_c>0 are fitted scale constants and αN,αD,αC>0\alpha_N,\alpha_D,\alpha_C>0 are fitted exponents. The fitted exponents are small; Kaplan et al. reported, for example, αN0.076\alpha_N\approx0.076 in one regime.

Remark 4.1 (status). These are empirical fits rather than universal theorems. The straightness over many orders of magnitude is the notable phenomenon. Small exponents mean that a fixed multiplicative loss reduction requires a large multiplicative increase in scale.

To allocate a fixed compute budget, consider a joint loss model. Let E,A,B,α,βE,A,B,\alpha,\beta be positive constants, with EE representing an irreducible loss floor.

Proposition 4.2 (compute-optimal Chinchilla allocation). Suppose

L(N,D)=E+ANα+BDβL(N,D)=E+\frac{A}{N^\alpha}+\frac{B}{D^\beta}

and the dominant training-compute constraint is C=6NDC=6ND. Then the unique positive minimizer satisfies

NCβ/(α+β),DCα/(α+β).N^*\propto C^{\beta/(\alpha+\beta)}, \qquad D^*\propto C^{\alpha/(\alpha+\beta)}.

More precisely,

(N)α+β=αAβB(C6)β.(N^*)^{\alpha+\beta} =\frac{\alpha A}{\beta B}\left(\frac C6\right)^\beta.

Proof. Substitute D=C/(6N)D=C/(6N) and omit the constant EE:

f(N)=ANα+B(6NC)β.f(N)=AN^{-\alpha}+B\left(\frac{6N}{C}\right)^\beta.

The stationary equation is

αANα1+βB6βCβNβ1=0,-\alpha AN^{-\alpha-1} +\beta B6^\beta C^{-\beta}N^{\beta-1}=0,

which rearranges to the displayed formula. The first term dominates as N0+N\to0^+ and the second as NN\to\infty, so this stationary point is the unique global minimizer. Substituting it into D=C/(6N)D=C/(6N) gives the second scaling relation. \square

When αβ\alpha\approx\beta, both NN^* and DD^* grow approximately as C1/2C^{1/2}.

Historical aside 4.3. Hoffmann et al. found exponents close enough to equal that compute-optimal model size and data should grow together. GPT-3 used about 175 billion parameters and 300 billion tokens; Chinchilla used about 70 billion parameters and 1.4 trillion tokens and performed better at a comparable training budget. Their fitted constants led to the approximate rule of twenty training tokens per parameter and motivated training smaller models on substantially more data than earlier practice.

Caveat 4.4. The constants and exponents depend on architecture, data, and optimizer. The law must bend near the irreducible floor and when data are repeatedly reused. The objective also omits inference cost; a model serving many queries may rationally be smaller and trained for longer than the training-only optimum.

5. A course-scale scaling law

Step 7 plots the best validation loss against parameter count for controlled Step 6 sweeps, for example d{32,64,128,256}d\in\{32,64,128,256\} at fixed depth. A line is fit only through the non-saturated points on log—log axes.

Project remark 5.1. The fitted exponent need not resemble the Kaplan value: the dataset, architecture, and scale differ. The useful questions are whether a straight segment is visible and where it bends. Very small models are capacity-limited; very large models exhaust the 1.1 MB corpus and widen the training—validation gap. This is a laptop-scale instance of the model—data allocation problem in Proposition 4.2.

Summary

BPE is a deterministic ordered merge scheme that greedily compresses token count, subject to a small self-overlap qualification. Tokenizer changes alter the unit of per-token loss, so bits per character is the appropriate cross-tokenizer metric. Temperature sampling is an entropy-regularized soft argmax; top-kk and nucleus rules truncate its tail. Typical-set and entropy-rate reasoning explains why a modal sequence may be less representative than a sample. Empirical loss curves often follow power laws, and the Chinchilla loss model yields a closed-form compute-optimal split between parameters and data.

Exercises (paired with Step 7)

A star marks a Project Step 7 task.

  1. ★ Implement BPE training, encoding, and decoding. Verify exact round trip on unseen text and reproduce the compression table.
  2. Give a short merge list and input for which applying learned merges out of training order changes the segmentation.
  3. ★ Prove Proposition 2.1 and compare the character and BPE models in bits per character.
  4. ★ Prove Proposition 3.1, including both temperature limits. Implement temperature, top-kk, and nucleus sampling.
  5. Find a greedy-decoding repetition loop and compare the probability scale of the greedy sequence with the typical eTHe^{-TH} scale.
  6. ★ Derive Proposition 4.2 and fit a power-law exponent to the class sweep. Explain where the fitted line must bend at both ends.
  7. Construct a corpus on which two-step lookahead produces a smaller final BPE token count than the greedy pair choice.
  8. For γ>0\gamma>0, add an inference-cost term γNQ\gamma NQ for QQ expected queries to the compute-allocation problem and determine qualitatively how the optimal NN^* changes.

Pointers

Sennrich, Haddow, and Birch (2016) for BPE in NLP; Holtzman et al., The Curious Case of Neural Text Degeneration (2020); Kaplan et al., Scaling Laws for Neural Language Models (2020); Hoffmann et al., Training Compute-Optimal Large Language Models (2022); Cover and Thomas, Chapter 3, for the asymptotic equipartition property. See the resources page and Project Step 7.