Skip to content

13.7 Rejection sampling and residual distributions

A language model’s sampler emits one token per forward pass, and each forward pass reads every weight once. The fastest way to decode more quickly without changing what the model says is to let something cheap guess ahead: a draft (an n-gram table, a prompt lookup, a small model) proposes several tokens, and the target model checks all of them in a single forward pass. For greedy decoding the check is easy, since you only ask whether the draft guessed the argmax. For sampled decoding it is subtle. The accepted text must be distributed exactly as if the target had sampled it alone, or the speedup silently changes the model’s output distribution, and no evaluation would ever attribute that drift to the decoding trick.

Concepts from other sections (11)

This section uses these ideas. Each has a short definition and a link to the section that explains it in full. Tap a dotted term in the text to preview it.

This section supplies the piece of probability that makes the check exact: an acceptance test and a residual distribution. You will derive von Neumann’s classic rejection sampler, adapt it to the setting where there is no time to retry, and prove (first algebraically, then by enumerating a small example by hand) that the result samples the target distribution for every draft. The code lives in course/solutions/python/tinyllm/prob/rejection.py, with the signatures and error rules in the contract course/contracts/py/tinyllm/prob/rejection.pyi and the draw order in spec/sampling.md (draw accounting).

This section builds on Categorical sampling: inverse CDF, Gumbel-max, alias method, whose sample_categorical, inverse CDF, and UniformSource it reuses (see sampling.py). It also assumes the language of Random variables, expectation, variance, and normal draws by Box-Muller and the uniforms produced by Modular arithmetic, hashing, and PCG32 in Python.

Learning objectives

  • Derive von Neumann’s rejection sampler and show that the number of tries is geometric with mean mm.
  • Explain why the acceptance test must be the strict comparison u<ru < r for floating-point uniforms.
  • Derive the residual distribution max⁡(0,p−q)/Z\max(0, p - q)/Z and prove that accept-or-resample outputs exactly pp.
  • Show that the acceptance probability equals ∑imin⁡(pi,qi)=1−TV(p,q)\sum_i \min(p_i, q_i) = 1 - \mathrm{TV}(p, q).
  • Implement the five functions so that their sums and draw order match a Rust port bit for bit.

The section uses the following symbols throughout.

SymbolMeaningType / shape
nnnumber of ids (the vocabulary)integer
p∈Δn−1p \in \Delta^{n-1}the target distribution (the big model’s sampler output)float64[n], sums to 1
q∈Δn−1q \in \Delta^{n-1}the proposal distribution (the draft)float64[n], sums to 1
xxa draft token, drawn from qqinteger in [0,n)[0, n)
u,u′u, u'independent uniforms on [0,1)[0, 1)float
rx=px/qxr_x = p_x / q_xthe acceptance ratio of xxfloat ≥0\ge 0
α=∑imin⁡(pi,qi)\alpha = \sum_i \min(p_i, q_i)the acceptance probabilityfloat in [0,1][0, 1]
TV(p,q)=12∑i∣pi−qi∣\mathrm{TV}(p, q) = \tfrac12 \sum_i \lvert p_i - q_i\rverttotal variation distancefloat in [0,1][0, 1]
res(p,q)i=max⁡(0,pi−qi)/Z\mathrm{res}(p, q)_i = \max(0, p_i - q_i) / Zthe residual distribution, Z=∑imax⁡(0,pi−qi)Z = \sum_i \max(0, p_i - q_i)float64[n]
mman envelope: pi≤mqip_i \le m q_i for every iifloat ≥1\ge 1

Start with the classic problem. Suppose qq is cheap to sample but you want samples of pp. If pi≤m qip_i \le m\, q_i for every ii, repeat the following: draw x∼qx \sim q, draw uu, and accept xx when u<px/(m qx)u < p_x / (m\, q_x). On one try,

P(propose x and accept)=qx⋅pxm qx=pxm,P(accept)=∑xpxm=1m.P(\text{propose } x \text{ and accept}) = q_x \cdot \frac{p_x}{m\, q_x} = \frac{p_x}{m}, \qquad P(\text{accept}) = \sum_x \frac{p_x}{m} = \frac{1}{m}.

Conditioned on acceptance, xx has probability (px/m)/(1/m)=px(p_x/m)/(1/m) = p_x: exactly the target. Each try is an independent coin with success probability 1/m1/m, so the number of tries is geometric, with mean mm and variance m(m−1)m(m - 1). A loose envelope costs time, never correctness. An envelope that is too small (px>mqxp_x > m q_x for some xx) is a different matter: it caps the acceptance of xx at 1 and under-samples it, which is why the contract checks the envelope before drawing anything.

Every acceptance in this section is a comparison of a uniform against a ratio, so it pays to get that comparison exactly right. For uu uniform on [0,1)[0, 1) and any r≥0r \ge 0, P(u<r)=min⁡(1,r)P(u < r) = \min(1, r). The comparison must be strict. For a continuous uu, P(u≤r)P(u \le r) would be the same, but floating-point uniforms are discrete (PCG32 gives multiples of 2−532^{-53}), and the point u=0u = 0 combined with px=0p_x = 0 would accept a token the target never produces. Notice also what happens at the other end: with r=px/qx≥1r = p_x / q_x \ge 1, every u<1u < 1 is below it, so an under-proposed token is always kept.

Speculative decoding cannot use von Neumann’s retry loop as it stands. The target’s forward pass has already happened, and a rejected position must produce a token right away; another try would mean another forward pass. So change the procedure: keep x∼qx \sim q with probability min⁡(1,px/qx)\min(1, p_x/q_x), and on rejection draw from a carefully chosen fallback. To find that fallback, look at what acceptance keeps. The kept mass of token tt is

qtmin⁡ ⁣(1,ptqt)=min⁡(pt,qt),q_t \min\!\left(1, \frac{p_t}{q_t}\right) = \min(p_t, q_t),

and the total kept mass is α=∑tmin⁡(pt,qt)\alpha = \sum_t \min(p_t, q_t). Since min⁡(a,b)=12(a+b−∣a−b∣)\min(a, b) = \tfrac12(a + b - \lvert a - b\rvert) and both distributions sum to 1, α=1−TV(p,q)\alpha = 1 - \mathrm{TV}(p, q): the closer the draft is to the target, the more of it is accepted. What is missing from pp after acceptance is pt−min⁡(pt,qt)=max⁡(0,pt−qt)p_t - \min(p_t, q_t) = \max(0, p_t - q_t), which sums to Z=1−αZ = 1 - \alpha. Normalizing that missing mass gives the residual distribution.

You now have both halves of the procedure, and they fit together exactly. Draw x∼qx \sim q, accept with probability min⁡(1,px/qx)\min(1, p_x/q_x), and otherwise draw t∼res(p,q)t \sim \mathrm{res}(p, q). Then

P(output=t)=min⁡(pt,qt)⏟accepted t+(1−α)⏟rejected⋅max⁡(0,pt−qt)1−α=min⁡(pt,qt)+max⁡(0,pt−qt)=pt.P(\text{output} = t) = \underbrace{\min(p_t, q_t)}_{\text{accepted } t} + \underbrace{(1 - \alpha)}_{\text{rejected}} \cdot \frac{\max(0, p_t - q_t)}{1 - \alpha} = \min(p_t, q_t) + \max(0, p_t - q_t) = p_t.

Nothing was assumed about qq beyond its being a distribution: a bad draft makes the method slow, never wrong. When p=qp = q, Z=0Z = 0 and the residual is undefined, but then α=1\alpha = 1 and the residual is never sampled; the contract returns pp in that case so that the function stays total.

Correct in distribution is not quite enough. The same request, with the same seed, should accept and reject the same drafts in the Python reference and in the Rust engine of Speculative decoding in the engine in AI Engineering. The sums in α\alpha and ZZ are therefore left-to-right loops over ascending ids in float64, and uu, u′u' come from the request’s generator in the order fixed by Speculative decoding: n-gram, prompt-lookup, and model drafts. The Rust engine repeats the same arithmetic. numpy’s np.sum sums pairwise and can differ in the last bit, which would move a boundary of the residual’s CDF and change a draw.

The theorem is short enough that it is worth checking on numbers. Take four ids, with target p=[12,14,14,0]p = [\tfrac12, \tfrac14, \tfrac14, 0] and draft q=[14,12,0,14]q = [\tfrac14, \tfrac12, 0, \tfrac14]. The table lays out each token’s ratio, its chance of being kept when proposed, the mass it keeps, and the mass left missing.

id ttptp_tqtq_trt=pt/qtr_t = p_t/q_tP(keep∣x=t)P(\text{keep} \mid x = t)kept mass min⁡(pt,qt)\min(p_t, q_t)max⁡(0,pt−qt)\max(0, p_t - q_t)
01/21/4211/41/4
11/41/21/21/21/40
21/40(never proposed)01/4
301/40000

The acceptance probability is α=14+14=12\alpha = \tfrac14 + \tfrac14 = \tfrac12; the residual mass is Z=12Z = \tfrac12, and the residual is [12,0,12,0][\tfrac12, 0, \tfrac12, 0]. Now add up the output distribution token by token. Id 0 is kept with mass 14\tfrac14, plus the rejected mass 12\tfrac12 times its residual probability 12\tfrac12, for a total of 12\tfrac12. Id 1 is kept with mass 14\tfrac14 and has residual 0, so it gets 14\tfrac14. Id 2 is never proposed but comes back through the residual: 12⋅12=14\tfrac12 \cdot \tfrac12 = \tfrac14. Id 3 is proposed a quarter of the time and always rejected, so it gets 0. That is exactly pp.

Follow one concrete draw too. The draft proposes x=1x = 1; u=0.75≥r1=0.5u = 0.75 \ge r_1 = 0.5 rejects it, and u′=0.75u' = 0.75 walks the residual’s CDF [0.5,0.5,1.0,1.0][0.5, 0.5, 1.0, 1.0] to id 2. With u=0.25u = 0.25 instead, id 1 is kept and u′u' is never looked at. These are the first cases the tests check.

The module exposes five functions: the single acceptance test, the acceptance probability, the residual, one speculative step, and a full von Neumann sampler.

python/tinyllm/prob/rejection.py
def rejection_accept(p_x: float, q_x: float, u: float) -> bool # u < p_x / q_x
def acceptance_probability(p: ArrayLike, q: ArrayLike) -> float # sum min(p, q)
def residual_distribution(p: ArrayLike, q: ArrayLike) -> NDArray # normalize(max(0, p - q)); p if p == q
def speculative_step(p, q, x: int, u_accept: float, u_resample: float) -> tuple[int, bool]
def rejection_sample(p, q, m: float, rng: UniformSource, max_tries: int = 10000) -> tuple[int, int]

speculative_step resamples with sample_categorical from Categorical sampling. rejection_sample draws exactly two uniforms per try, proposal first. The contract has every error rule.

The implementation follows the derivation closely, and each choice traces back to one of the principles above.

First, both inputs are distributions over one vocabulary. Every function starts by checking pp and qq with the rules from Categorical sampling (1-D, finite, non-negative, summing to 1) and that their shapes match. A draft over a different vocabulary is a bug, not a case to handle.

Second, acceptance is one strict comparison. As shown above, u < p_x / q_x has probability exactly min⁡(1,r)\min(1, r) for uu on the discrete grid in [0,1)[0, 1). No explicit min is needed, because u<1≤ru < 1 \le r already accepts. If qx=0q_x = 0, the draft proposed a token it gave probability 0, so the function raises rather than divides.

Third, the acceptance probability and the residual’s normalizer are left-to-right sums: a plain loop over ascending ids, so the Rust port gets the same last bit and the residual’s CDF has the same boundaries.

Fourth, the residual is max⁡(0,p−q)/Z\max(0, p - q)/Z, or pp when Z=0Z = 0. Z=0Z = 0 happens exactly when p≤qp \le q everywhere, which for two distributions means p=qp = q; then nothing is ever rejected, and returning a copy of pp keeps the function total without aliasing the caller’s array.

Fifth, one speculative step is accept, or one draw from the residual. If the acceptance test passes, return xx without reading u′u'; otherwise call sample_categorical(residual, u'). That draw order is part of the parity contract with the Python and Rust speculative decoders.

Sixth, von Neumann needs a checked envelope and two uniforms per try. Check m≥1m \ge 1 and p≤mqp \le mq everywhere first, then loop: propose with one uniform, accept with another when u<px/(mqx)u < p_x/(m q_x), and return the token and the number of tries. A bounded loop turns a pathological envelope into an error rather than a hang.

The first two functions are the acceptance test and its probability.

def rejection_accept(p_x: float, q_x: float, u: float) -> bool:
_check_u(u, "u") # u in [0, 1)
if not q_x > 0.0:
raise ValueError(f"q_x must be > 0 (q proposed this token), got {q_x!r}")
if not p_x >= 0.0:
raise ValueError(f"p_x must be >= 0, got {p_x!r}")
# P(u < r) = min(1, r) for u uniform on [0, 1): strict, so p_x = 0 (r = 0)
# never accepts and r >= 1 always does.
return u < p_x / q_x
def acceptance_probability(p: ArrayLike, q: ArrayLike) -> float:
a, b = _pair(p, q) # two checked distributions, same shape
s = 0.0
for pi, qi in zip(a.tolist(), b.tolist()): # ascending ids, left to right
s += min(pi, qi) # the kept mass of token i
return s # = 1 - TV(p, q)

On the hand example, the per-token kept masses are 14,14,0,0\tfrac14, \tfrac14, 0, 0, so α=12\alpha = \tfrac12. The residual and the speculative step come next.

def residual_distribution(p: ArrayLike, q: ArrayLike) -> NDArray:
a, b = _pair(p, q)
r = np.maximum(a - b, 0.0) # what acceptance left missing from p
z = 0.0
for v in r.tolist(): # not np.sum: pairwise summation differs in the last bit
z += v
if z <= 0.0:
# p == q: nothing is ever rejected, but the result must stay a distribution.
return a.copy()
return r / z
def speculative_step(p, q, x: int, u_accept: float, u_resample: float) -> tuple[int, bool]:
a, b = _pair(p, q)
if not 0 <= x < a.size:
raise ValueError(f"draft token {x} outside [0, {a.size})")
_check_u(u_resample, "u_resample") # validated even when not used
if rejection_accept(float(a[x]), float(b[x]), u_accept):
return int(x), True # accepted: u_resample is not read
return sample_categorical(residual_distribution(a, b), u_resample), False

Trace the hand example’s draw through this code: x=1x = 1, and u=0.75≥0.5u = 0.75 \ge 0.5 rejects; the residual is [12,0,12,0][\tfrac12, 0, \tfrac12, 0] with running sums 0.5,0.5,1.0,1.00.5, 0.5, 1.0, 1.0, and u′=0.75u' = 0.75 first falls below 1.01.0 at id 2. With u=0.25<0.5u = 0.25 < 0.5 the function returns (1, True). In the disjoint-support case α=0\alpha = 0, r=pr = p and Z=1Z = 1, so the residual is pp itself: a useless draft is always rejected and the output is still a draw of pp.

Finally, the von Neumann sampler checks its envelope and loops.

def rejection_sample(p, q, m: float, rng, max_tries: int = 10000) -> tuple[int, int]:
a, b = _pair(p, q)
if not m >= 1.0: # an envelope below 1 cannot cover p
raise ValueError(f"the envelope m must be >= 1, got {m!r}")
if (a > m * b).any(): # p_x > m q_x: x would be under-sampled
i = int(np.flatnonzero(a > m * b)[0])
raise ValueError(f"m = {m} is not an envelope: p[{i}] = {a[i]} > m q[{i}] = {m * b[i]}")
for tries in range(1, max_tries + 1):
x = sample_categorical(b, rng.uniform()) # proposal uniform first
if rng.uniform() < a[x] / (m * b[x]): # then the acceptance uniform; b[x] > 0 here
return x, tries
raise RuntimeError(f"no acceptance in {max_tries} tries")

sample_categorical never returns an id with qx=0q_x = 0 (its comparison is strict), so the division is safe; and the envelope check guarantees that any id with px>0p_x > 0 has qx>0q_x > 0. The full solution is rejection.py.

Consider the cost first. rejection_accept is O(1)O(1); acceptance_probability and residual_distribution are O(n)O(n); speculative_step is O(n)O(n) (validation, the residual, and an inverse-CDF draw) and is only computed in full on rejection. rejection_sample costs O(n)O(n) checks plus a geometric number of tries with mean mm, each O(n)O(n) for the inverse CDF.

The residual beats a retry loop for speculative decoding. A von Neumann retry would also be exact, but each retry would need another target forward pass; the residual produces a token from the pass already done, so every verified position yields exactly one token.

Explicit loops are chosen over np.sum deliberately. numpy’s pairwise sum is more accurate for long arrays, but it is not what the Rust port computes. The sums here are of at most a vocabulary of non-negative terms, where left-to-right error is tiny, and bit parity matters more.

One tradeoff is accepted knowingly: validating pp and qq on every call costs O(n)O(n) per step. For a 50 000-token vocabulary that is small next to a forward pass, and it turns a draft/target vocabulary mismatch into an immediate error.

A correct implementation has to survive a specific set of checks. The hand example anchors the definitions: its ratios, α=1/2\alpha = 1/2, and the residual [1/2,0,1/2,0][1/2, 0, 1/2, 0] must come out exactly, and its output distribution, enumerated exactly, must equal pp. Strict acceptance is a boundary check: u=ru = r rejects and px=0p_x = 0 never accepts, because discrete uniforms make ≤\le wrong. When the target dominates, r≥1r \ge 1 must accept every u<1u < 1, so under-proposed tokens are always kept. Bad acceptance arguments (qx=0q_x = 0, u∉[0,1)u \notin [0, 1), px<0p_x < 0) must raise, since a silent answer biases the output.

The statistical checks cover the theorem itself. The enumerated acceptance rate must equal ∑min⁡(p,q)\sum \min(p, q), because the speculative decoder reports it as its acceptance rate. The residual must be a distribution (non-negative, summing to 1, with Z=1−αZ = 1 - \alpha), since sample_categorical demands one. For random pp and qq in eighths, the speculative output distribution must equal pp exactly by enumeration, and 20000 seeded draws on arbitrary distributions must match pp under a chi-square test. For von Neumann, one try must accept xx with probability exactly px/mp_x/m, and the mean number of tries must equal mm within 4 standard errors.

The remaining boundary and unit checks protect parity and the edges. p=qp = q must give a copy of pp: no 0/0, and no aliasing of the caller’s array. Disjoint supports give α=0\alpha = 0 and residual pp, so a useless draft is slow, not wrong. The normalizer must be a left-to-right loop over ascending ids for bit parity with the Rust port. Mismatched shapes and non-distributions must raise, because draft and target vocabularies must match. An accepted draft must not look at u′u', because the draw order is part of the parity contract, and a draft xx outside the vocabulary or with qx=0q_x = 0 must raise, because draft and proposal disagreeing is a bug. Von Neumann must draw the proposal uniform first and then the acceptance uniform, two per try, to be reproducible across languages, and it must raise for px>mqxp_x > m q_x, for m<1m < 1, and when nothing is accepted within max_tries, since a wrong envelope silently biases.

The classic mistakes are worth naming one by one, with the line of the solution that avoids each.

Warning: Accepting on u≤ru \le r accepts a token with px=0p_x = 0 whenever u=0u = 0. The solution returns u < p_x / q_x.

Warning: Writing the ratio upside down, or “capping” it by inverting it when it exceeds 1, keeps over-proposed tokens and drops under-proposed ones. The ratio is p_x / q_x, and u<1u < 1 does the capping for free.

The residual has its own traps. Using max⁡(0,q−p)\max(0, q - p) or ∣p−q∣\lvert p - q\rvert resamples the tokens the draft over-proposed; the solution computes np.maximum(a - b, 0.0). Forgetting to normalize makes sample_categorical reject the residual, or leaves its CDF ending below 1; the solution divides r / z. Handling p=qp = q as 0/0 produces NaN, and returning the caller’s array lets a later in-place edit corrupt the target; the solution returns a.copy() when z <= 0.0.

On rejection, resampling from pp, from qq, or simply returning the draft means the output is no longer pp, because accepted tokens are effectively counted twice; the solution samples residual_distribution(a, b). Running von Neumann without mm, or without checking it, makes accepted tokens follow min⁡(p,q)\min(p, q)-ish weights rather than pp; the solution accepts on a[x] / (m * b[x]) after checking m >= 1 and a <= m * b. And summing with numpy’s pairwise np.sum produces last-bit differences against the Rust port, which the explicit left-to-right loops avoid.

The residual draw uses sample_categorical and the UniformSource generator type from Categorical sampling, and every probability above rests on the uniform random variables and independence of Random variables, expectation, variance, and normal draws by Box-Muller. Looking forward, Speculative decoding: n-gram, prompt-lookup, and model drafts in AI Engineering calls one speculative_step per draft position and reports acceptance_probability as its acceptance rate, building on the token sampler of Sampling and logit processors. Speculative decoding in the engine ports the same logic to Rust and is held to the same accept and reject decisions.

Production systems use the same ideas at larger scale. vLLM’s RejectionSampler (vllm/v1/sample/rejection_sampler.py) performs batched acceptance over a whole draft tree on the GPU, recovering tokens from the residual. Hugging Face assisted generation uses the same residual for model drafts in _speculative_sampling (transformers/generation/utils.py). NumPy’s Gamma and binomial samplers (numpy/random/src/distributions/distributions.c) use rejection with tight squeeze functions to avoid evaluating the target.

  • Devroye, Non-Uniform Random Variate Generation (1986), ch. 2.3.
  • Leviathan, Kalman, and Matias, “Fast Inference from Transformers via Speculative Decoding” (2023), appendix A.1.
  • Chen et al., “Accelerating Large Language Model Decoding with Speculative Sampling” (2023).
  • Rejection sampling: drawing from a proposal qq and accepting each draw with a probability chosen so that accepted draws follow the target pp.
  • Envelope: a constant m≥1m \ge 1 with pi≤mqip_i \le m q_i for all ii; von Neumann’s sampler needs one, and its mean cost is mm tries.
  • Acceptance ratio: rx=px/qxr_x = p_x / q_x; the draft token xx is kept with probability min⁡(1,rx)\min(1, r_x).
  • Acceptance probability: α=∑imin⁡(pi,qi)\alpha = \sum_i \min(p_i, q_i), the chance a draft token is kept.
  • Total variation distance: TV(p,q)=12∑i∣pi−qi∣\mathrm{TV}(p, q) = \tfrac12 \sum_i \lvert p_i - q_i\rvert; it satisfies α=1−TV(p,q)\alpha = 1 - \mathrm{TV}(p, q).
  • Residual distribution: max⁡(0,p−q)/Z\max(0, p - q)/Z, the normalized mass that acceptance leaves missing from pp.
  • Keeping a draft x∼qx \sim q with probability min⁡(1,px/qx)\min(1, p_x/q_x) keeps exactly min⁡(px,qx)\min(p_x, q_x) of every token’s mass; the total kept is ∑xmin⁡(px,qx)=1−TV(p,q)\sum_x \min(p_x, q_x) = 1 - \mathrm{TV}(p, q).
  • What rejection removes is exactly the residual max⁡(0,p−q)\max(0, p - q), so resampling from its normalized form restores pp token for token: the output of speculative_step is distributed as pp for every qq.
  • “Accept when u<ru < r” with uu uniform on [0,1)[0, 1) has probability exactly min⁡(1,r)\min(1, r); "u≤ru \le r" counts one extra point and accepts tokens pp never produces.
  • Von Neumann’s sampler accepts a proposal with probability px/(mqx)p_x / (m q_x); each try succeeds with probability 1/m1/m, so the cost is a geometric number of tries with mean mm.
  • Left-to-right sums and a fixed draw order make the Python and Rust implementations agree on every decision for the same seed.
  1. Why is the residual max⁡(0,p−q)\max(0, p - q) and not max⁡(0,q−p)\max(0, q - p) or ∣p−q∣\lvert p - q\rvert?

    Answer

    After acceptance each token has min⁡(pt,qt)\min(p_t, q_t) of its target mass; what is missing is pt−min⁡(pt,qt)=max⁡(0,pt−qt)p_t - \min(p_t, q_t) = \max(0, p_t - q_t), the tokens the draft under-proposed. max⁡(0,q−p)\max(0, q - p) would resample the tokens the draft over-proposed, which already have more than enough, and ∣p−q∣\lvert p - q \rvert mixes both; neither adds up to pp.

  2. Why does rejection_accept need no min(1, ...)?

    Answer

    uu is always below 1, so when r≥1r \ge 1 the test u < r is always true: the acceptance probability is already min⁡(1,r)\min(1, r). Writing an explicit cap is harmless, but “capping” by inverting the ratio when it exceeds 1 would turn the always-accept case into a partial one and drop under-proposed tokens.

  3. On acceptance, why must speculative_step return without using u′u'?

    Answer

    Correctness: the theorem’s accept branch outputs xx itself, contributing min⁡(px,qx)\min(p_x, q_x); letting u′u' change the result there would replace accepted tokens with residual draws and break that term. Parity: the contract says u′u' matters only on rejection, so the Python speculative decoder and the Rust engine agree on every decision for the same uniforms.

  4. Why does residual_distribution return a.copy() when Z=0Z = 0 and not a?

    Answer

    Z=0Z = 0 means p=qp = q, so the residual is never sampled, but the function must still return a valid distribution and pp is the natural choice. Returning the caller’s array would let a later in-place edit of the result (a mask, a temperature) silently change the target distribution itself.

  5. In von Neumann’s sampler, what does a loose envelope (large mm) cost, and what does a too-small one break?

    Answer

    A loose envelope only costs time: each try accepts with probability 1/m1/m, so the mean number of tries is mm. A too-small one breaks correctness: for an xx with px>mqxp_x > m q_x the acceptance ratio exceeds 1 and is effectively capped, so xx is drawn with probability below pxp_x and the others are over-sampled. That is why the function checks the envelope before drawing.

  6. In the hand example, q2=0q_2 = 0 but the output still produces id 2 a quarter of the time. Where does that probability come from?

    Answer

    Entirely from the residual. Id 2 is never proposed, so its kept mass is 0, but its missing mass max⁡(0,p2−q2)=14\max(0, p_2 - q_2) = \tfrac14 makes up half of Z=12Z = \tfrac12, so its residual probability is 12\tfrac12. The rejection probability is 1−α=121 - \alpha = \tfrac12, giving 12⋅12=14=p2\tfrac12 \cdot \tfrac12 = \tfrac14 = p_2.