[SpecDecode] Restore residual-logits comments in _resample_kernel (#58166)

Signed-off-by: Lucas Wilkinson <lwilkins@redhat.com>
Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
Lucas Wilkinson
2026-09-22 08:07:31 -07:00
committed by GitHub
co-authored by Claude Fable 5.1
parent e4340e41c9
commit 5c121e962a
@@ -806,7 +806,10 @@ def _resample_kernel(
other=float("-inf"),
).to(tl.float32)
# Compute the residual logits to resample the rejected token from.
if is_bonus or not is_valid_rejected_draft:
# Bonus token (no rejections) or -1 placeholder token. In either case,
# directly use the target logits.
residual_logits = target_logits
elif HAS_DRAFT_LOGITS:
# draft_logits is stored pre-temperature, so apply scale first.
@@ -836,7 +839,12 @@ def _resample_kernel(
)
target_log_probs += log_p_tau
draft_log_probs = draft_logits - draft_lse
# Compute log(max(p - q, 0)) without subtracting probabilities.
# Compute the residual:
# r(x) = max(p(x) - q(x), 0)
# Gumbel sampling needs logits, so we compute it in log space:
# log(r(x)) = log(max(exp(log_p(x)) - exp(log_q(x)), 0))
# The more numerically stable form is:
# log(max(exp(a) - exp(b), 0)) = a + log(max(1 - exp(b - a), 0))
ratio = tl.exp(draft_log_probs - target_log_probs)
residual_logits = tl.where(
ratio < 1.0,
@@ -844,13 +852,20 @@ def _resample_kernel(
float("-inf"),
).to(tl.float32)
else:
# The block-verification factor is constant and cancels on normalization.
# One-hot draft. The residual is just the target distribution with
# the rejected draft token probability zeroed out.
# NOTE: During block verification, the residual becomes:
# 0 if x == rejected_draft_token
# p_tau * M_b(x) / Z otherwise
# Therefore p_tau is a constant that cancels under normalization,
# and does not need to be applied.
residual_logits = tl.where(
block != rejected_draft_token,
target_logits,
float("-inf"),
).to(tl.float32)
# Resample the rejected/bonus token.
if WATERMARK:
# Padded and greedy rows retain the stock draw.
is_watermarked = (