LeetKernels
ProblemsStart solving
Problems

87. Speculative Decoding Verification medium

Implement the token verification step of speculative decoding. A draft model proposes T tokens; the target model evaluates them in one forward pass and accepts or rejects each. Given B sequences, produce the verified output tokens. Probability tensors are float32; token tensors are int32.

Notation for each sequence b, at each draft position i=0,…,T−1:

  • 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 v
  • q_i(v) = \texttt{target_probs}[b, i, v] — target model's probability for token v
  • u_i = \texttt{uniform_samples}[b, i] — pre-generated U[0,1) sample for position i
pos 0 pos 1 pos 2 pos 3 draft t₀ t₁ t₂ t₃ probs p(t₀) = 0.60 q(t₀) = 0.50 p(t₁) = 0.50 q(t₁) = 0.20 not reached not reached α, test α = .50/.60 = .83 u=0.1 < .83 ✓ α = .20/.50 = .40 u=0.7 ≥ .40 ✗ skipped skipped reject at pos 1 → stop, resample from adj(v) = max(0, q(v) − p(v)) normalize adj, inverse-CDF sample using u[b, T] → replacement token t₁′ output t₀ t₁′ 0 0 p = draft prob q = target prob α = min(1, q/p) ■ accepted ■ resampled ■ pad If all T tokens accepted: sample bonus token from q at last position using u[b, T]

For each sequence b, process positions i=0,1,…,T−1 left-to-right:

  1. Compute acceptance probability: αi​=min(1,pi​(ti​)qi​(ti​)​)
  2. If ui​<αi​: accept ti​, continue to position i+1.
  3. If ui​≥αi​: reject, stop. Sample replacement from: adj(v)=∑v′​max(0,qi​(v′)−pi​(v′))max(0,qi​(v)−pi​(v))​ using inverse CDF with r = \texttt{uniform_samples}[b, T]. If adj is all zeros, use uniform 1/V.
  4. If all T tokens accepted: sample a bonus token from qT−1​ using \texttt{uniform_samples}[b, T].

Write results into output_tokens[b, :] (shape [B,T+1]): accepted/resampled tokens fill positions 0 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_tokens buffer (shape [B, T+1], int32).
  • Memory layout is row-major: draft_probs[b, i, v] is at offset b*T*V + i*V + v.
  • Inverse CDF sampling: given distribution adj (already normalized), find the smallest index k where ∑v=0k​adj(v)≥r, where r = \texttt{uniform_samples}[b, T]. Clamp the result to [0,V−1].
  • If the adjusted distribution is all zeros (i.e., qi​≤pi​ everywhere), fall back to the uniform distribution over V tokens.

Example

Input: B=1,T=3,V=4

\text{draft_tokens} = [1, 2, 0]

Draft probabilities pi​ and target probabilities qi​ per position: p0​=[0.10​0.60​0.20​0.10​],q0​=[0.10​0.50​0.20​0.20​] p1​=[0.10​0.20​0.50​0.20​],q1​=[0.30​0.20​0.20​0.30​] \text{uniform_samples} = \begin{bmatrix} 0.50 & 0.70 & 0.30 & 0.90 \end{bmatrix}

Position 0 (draft token = 1): α0​=min(1,p0​(1)q0​(1)​)=min(1,0.600.50​)≈0.833. Since u0​=0.50<0.833, accept token 1.

Position 1 (draft token = 2): α1​=min(1,p1​(2)q1​(2)​)=min(1,0.500.20​)=0.40. Since u1​=0.70≥0.40, reject. Resample from adjusted distribution: adj(v)=max(0,q1​(v)−p1​(v))=[0.20,0,0,0.10] normalized=[32​,0,0,31​],CDF=[0.667,0.667,0.667,1.0] 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, :] and target_probs[b, i, :] are valid probability distributions (non-negative, sum to 1)
  • draft_probs[b, i, draft_tokens[b, i]] > 0 for all b, i
  • uniform_samples values are in [0,1)
  • All floating-point tensors use float32; token tensors use int32
  • 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) {}

Solve it on your GPU

Previous: LoRA LinearNext: Causal Depthwise Conv1d

From LeetGPU, CC BY-NC-ND 4.0.