spec-decode-ops

Fused sampling and speculative-decoding verification for autoregressive decoding on NVIDIA Ampere and newer (sm_80 / sm_86 / sm_89 / sm_90).

sample executes the standard logits pipeline (repetition penalty -> temperature -> top-k -> top-p -> min-p -> categorical draw) as one optional segmented sort plus one kernel. All three filters are prefixes of the sorted order, so their cutoffs resolve in a single walk, and the draw uses the Gumbel-argmax identity, which requires neither normalization nor multinomial. The eager transformers chain performs the same pipeline as six or more full-vocabulary kernel passes. Greedy decoding and unfiltered sampling take sort-free single-kernel paths.

verify implements canonical speculative-decoding rejection sampling (Leviathan et al. 2023; Chen et al. 2023): draft token x_i is accepted with probability min(1, p_t(x_i)/p_d(x_i)); the first rejection is replaced by a sample from the renormalized residual max(p_t - p_d, 0); full acceptance appends a bonus token from the target's final position. The emitted stream is distributed exactly as the target model's distribution.

Randomness is counter-based Philox parameterized by (seed, offset); results are bitwise reproducible for fixed seed, offset, shape, and architecture.

Usage

import torch
from kernels import get_kernel

sdo = get_kernel("phanerozoic/spec-decode-ops", version=1, trust_remote_code=True)

logits = model(input_ids).logits[:, -1]                  # [B, V]
tok = sdo.sample(logits, temperature=0.8, top_k=50, top_p=0.95,
                 seed=1234, offset=step)                 # [B] int64

version selects the release branch; trust_remote_code is required by kernels for publishers without the trusted-publisher mark.

Speculative verification:

# draft model proposed draft_tokens [B, k]; both models were run over them
accept_len, out = sdo.verify(target_logits,              # [B, k+1, V]
                             draft_logits,               # [B, k,   V]
                             draft_tokens, temperature=0.8,
                             seed=1234, offset=step)
# out[b, :accept_len[b]+1] are the emitted tokens for sequence b

Filtered speculative decoding composes through filter_logits: apply the same filter settings to both models' logits, then call verify with temperature 1.

API

Function Purpose
sample(logits, temperature, top_k, top_p, min_p, repetition_penalty, prev_tokens, seed, offset) fused pipeline, one token per row
filter_logits(...) the masked, temperature-scaled logits the eager processor chain would produce
verify(target_logits, draft_logits, draft_tokens, temperature, seed, offset) rejection-sampling verification, exact target distribution

Semantics

Filter order and boundary behavior match the transformers processors: penalty before temperature; top-p keeps the smallest sorted prefix whose cumulative probability reaches top_p, including the crossing token; min-p keeps p >= min_p * p_max; each filter keeps at least one token. top-p normalizes over the top-k prefix, matching warper application order.

Measured behavior

On an L4, torch 2.12, V = 50257 unless stated:

  • Kept-set equivalence with the transformers processor chain is exact across temperature/top-k/top-p/min-p/penalty combinations on non-degenerate inputs. At fully tied logits the kept sets are equal-sized subsets of the same exchangeable tie class (the reference's own subset is sort-order dependent); at a top-p boundary falling inside float32 cumsum precision the symmetric difference carries < 1e-6 probability mass.
  • Sampler distribution: total variation distance to the processor-chain analytic distribution <= 1.9e-3 at 4.2e6 draws across filter settings, with zero probability mass on masked tokens.
  • Verifier: emitted first-token distribution matches the target distribution to TVD 3.4e-3 at 2.1e6 trials with a deliberately mismatched draft; acceptance rate matches the analytic sum(min(p_t, p_d)) to 4e-4; the k=4 acceptance-length distribution matches the analytic law to 3e-4.
  • Determinism: identical (seed, offset) reproduce bitwise; changing offset changes draws.
  • Latency (V = 128256, median of 100): full filter pipeline 0.94 ms vs 0.90/1.19 ms for the eager chain at M=1/M=8 (0.96x/1.27x); the sort-free unfiltered path runs 0.14 ms (6.4x/8.2x). The filtered path is sort-dominated at M=1; a selection-based variant is planned.

Requirements

  • NVIDIA GPU with compute capability 8.0+.
  • logits in f32, bf16, or f16; M * V < 2^31 per call.

License

Apache-2.0.

Downloads last month
-
apache-2.0
Supported hardwares new
CUDA
8.08.68.99.010.012.0
GPU
B300
288GB
NVIDIA SXM
B200
192GB
NVIDIA SXM
H200
141GB
NVIDIA SXM
H100
80GB
GPU
H800
80GB
GPU
H20
96GB
GPU
L40s
48GB
GPU
L40
48GB
GPU
L20
48GB
GPU
L4
24GB
DGX Spark
GB10
128GB
GPU
RTX PRO 6000 WS
96GB
GPU
RTX PRO 6000 Max-Q
96GB
GPU
RTX PRO 5000
48GB
GPU
RTX PRO 4500 WS
32GB
GPU
RTX PRO 4000
24GB
GPU
RTX PRO 4000 SFF
24GB
GPU
RTX PRO 2000
16GB
GPU
RTX 6000 Ada
48GB
GPU
RTX 5880 Ada
48GB
RTX
RTX 5000 Ada
32GB
GPU
RTX 4500 Ada
24GB
RTX
RTX 4000 Ada
20GB
RTX
RTX 4000 SFF Ada
20GB
GPU
RTX 3500 Ada Mobile
12GB
GPU
RTX 2000 Ada
16GB
GPU
RTX A6000
48GB
GPU
RTX A5000
8GB
GPU
RTX A5000 Max-Q
16GB
GPU
RTX A5000 Mobile
16GB
GPU
RTX A4000
16GB
GPU
RTX A4000 Max-Q
8GB
GPU
RTX A4000 Mobile
8GB
GPU
RTX A3000 Mobile
6GB
GPU
RTX A2000
6GB
GPU
RTX A2000 Embedded
4GB
GPU
RTX A2000 Max-Q
4GB
GPU
RTX A2000 Mobile
4GB
GPU
A800
40GB
GPU
A100
80GB
GPU
A40
48GB
GPU
A30
24GB
GPU
A10
24GB
GPU
A2
16GB
RTX
RTX 5090
32GB
RTX
RTX 5090 D
32GB
RTX
RTX 5090 Mobile
24GB
RTX
RTX 5080
16GB
RTX
RTX 5080 Mobile
16GB
RTX
RTX 5070
12GB
RTX
RTX 5070 Mobile
8GB
RTX
RTX 5070 Ti
16GB
RTX
RTX 5070 Ti Mobile
12GB
RTX
RTX 5060 Ti
16GB
RTX
RTX 5060
8GB
RTX
RTX 5060 Mobile
8GB
RTX
RTX 5050
8GB
RTX
RTX 5050 Mobile
8GB
RTX
RTX 4090
24GB
RTX
RTX 4090D
24GB
RTX
RTX 4090 Mobile
16GB
RTX
RTX 4080 SUPER
16GB
RTX
RTX 4080
16GB
RTX
RTX 4080 Mobile
12GB
RTX
RTX 4070
12GB
RTX
RTX 4070 Mobile
8GB
RTX
RTX 4070 Ti
12GB
RTX
RTX 4070 Super
12GB
RTX
RTX 4070 Ti Super
16GB
RTX
RTX 4060
8GB
RTX
RTX 4060 Ti
8GB
RTX
RTX 4090 Laptop
16GB
RTX
RTX 4080 Laptop
12GB
RTX
RTX 4070 Laptop
8GB
RTX
RTX 4060 Laptop
8GB
RTX
RTX 4050 Laptop
6GB
RTX
RTX 3090
24GB
RTX
RTX 3090 Ti
24GB
RTX
RTX 3080
12GB
RTX
RTX 3080 Ti
12GB
RTX
RTX 3080 Mobile
16GB
RTX
RTX 3070
8GB
RTX
RTX 3070 Ti
8GB
RTX
RTX 3070 Ti Mobile
8GB
RTX
RTX 3060 Ti
8GB
RTX
RTX 3060
12GB
RTX
RTX 3060 Mobile
6GB
RTX
RTX 3050 Mobile
4GB
GPU
RTX 2050 Mobile
4GB
Jetson
Jetson AGX Orin 64GB
64GB
Jetson
Jetson AGX Orin 32GB
32GB
Jetson
Jetson Orin NX 16GB
16GB
Jetson
Jetson Orin NX 8GB
8GB
Jetson
Jetson Orin Nano 8GB
8GB
Jetson
Jetson Orin Nano 4GB
4GB
OS
linux
Arch
x86_64