Python / PyTorch 工业标准参考实现答案 (含测试验证与边界断言)
import torch
import random
def verify_speculative_tokens(
draft_tokens: list[int],
draft_probs: torch.Tensor,
target_probs: torch.Tensor
) -> list[int]:
"""
Rejection sampling verification for Speculative Decoding.
draft_probs: (K, VocabSize)
target_probs: (K + 1, VocabSize) - includes extra position for next token
"""
accepted_tokens = []
K = len(draft_tokens)
for i in range(K):
tok = draft_tokens[i]
p_draft = draft_probs[i, tok].item()
p_target = target_probs[i, tok].item()
# Acceptance criterion: accept with prob min(1, p_target / p_draft)
acceptance_prob = min(1.0, p_target / (p_draft + 1e-12))
if random.random() < acceptance_prob:
accepted_tokens.append(tok)
else:
# Rejection: resample token from normalized max(0, p_target - p_draft)
diff = torch.clamp(target_probs[i] - draft_probs[i], min=0.0)
norm_diff = diff / (diff.sum() + 1e-12)
bonus_token = torch.multinomial(norm_diff, 1).item()
accepted_tokens.append(bonus_token)
return accepted_tokens
# If all K tokens accepted, sample K+1-th token from target distribution
next_tok = torch.multinomial(target_probs[K], 1).item()
accepted_tokens.append(next_tok)
return accepted_tokens
# --- Verification & Test Case ---
if __name__ == "__main__":
K, V = 3, 10
draft_tokens = [2, 5, 8]
draft_p = torch.full((K, V), 0.1)
target_p = torch.full((K + 1, V), 0.1)
res = verify_speculative_tokens(draft_tokens, draft_p, target_p)
assert len(res) >= 1
print("Speculative decoding verification output:", res)