87. Speculative Decoding Verification medium
Implement the token verification step of speculative decoding. A draft model proposes tokens;
the target model evaluates them in one forward pass and accepts or rejects each. Given
sequences, produce the verified output tokens. Probability tensors are float32;
token tensors are int32.
Notation for each sequence , at each draft position :
- t_i = \texttt{draft_tokens}[b, i] — the token proposed by the draft model
- p_i(v) = \texttt{draft_probs}[b, i, v] — draft model's probability for token
- q_i(v) = \texttt{target_probs}[b, i, v] — target model's probability for token
- u_i = \texttt{uniform_samples}[b, i] — pre-generated sample for position
For each sequence , process positions left-to-right:
- Compute acceptance probability:
- If : accept , continue to position .
- If : reject, stop. Sample replacement from: using inverse CDF with r = \texttt{uniform_samples}[b, T]. If is all zeros, use uniform .
- If all tokens accepted: sample a bonus token from using \texttt{uniform_samples}[b, T].
Write results into output_tokens[b, :] (shape ): accepted/resampled tokens
fill positions through the accepted count (inclusive), remaining positions are zero.
Implementation Requirements
- Implement
solve(draft_tokens, draft_probs, target_probs, uniform_samples, output_tokens, B, T, V). - Do not change the function signature or use external libraries beyond the standard GPU frameworks.
- Write results into the provided
output_tokensbuffer (shape[B, T+1],int32). - Memory layout is row-major:
draft_probs[b, i, v]is at offsetb*T*V + i*V + v. - Inverse CDF sampling: given distribution (already normalized), find the smallest index where , where r = \texttt{uniform_samples}[b, T]. Clamp the result to .
- If the adjusted distribution is all zeros (i.e., everywhere), fall back to the uniform distribution over tokens.
Example
Input:
\text{draft_tokens} = [1, 2, 0]
Draft probabilities and target probabilities per position: \text{uniform_samples} = \begin{bmatrix} 0.50 & 0.70 & 0.30 & 0.90 \end{bmatrix}
Position 0 (draft token = 1): . Since , accept token 1.
Position 1 (draft token = 2): . Since , reject. Resample from adjusted distribution: With r = \text{uniform_samples}[0, T] = 0.90, inverse CDF gives token 3.
Output: \text{output_tokens} = \begin{bmatrix} 1 & 3 & 0 & 0 \end{bmatrix}
Constraints
- 1 ≤
B≤ 256 - 1 ≤
T≤ 16 - 2 ≤
V≤ 131,072 draft_probs[b, i, :]andtarget_probs[b, i, :]are valid probability distributions (non-negative, sum to 1)draft_probs[b, i, draft_tokens[b, i]]> 0 for allb,iuniform_samplesvalues are in- All floating-point tensors use
float32; token tensors useint32 - Performance is measured with
B= 64,T= 8,V= 32,768
Starter code (CUDA)
#include <cuda_runtime.h>
// draft_tokens, draft_probs, target_probs, uniform_samples, output_tokens are device pointers
extern "C" void solve(const int* draft_tokens, const float* draft_probs, const float* target_probs,
const float* uniform_samples, int* output_tokens, int B, int T, int V) {}
From LeetGPU, CC BY-NC-ND 4.0.