Skip to main content
Back

FlashSampling Cuts Token Generation Time by 10% Without Approximations

FlashSampling fuses categorical sampling into the LM head matmul, skipping the logits tensor entirely and cutting time per token by up to 10% in vLLM.

Pro

  • FlashSampling fuses exact categorical sampling into the LM-head matmul, never writing logits to HBM.
  • Uses Gumbel-Max trick: add noise, take argmax per tile, reduce. No softmax needed.
  • Peak vLLM speedup of 10.2% TPOT on Qwen3-1.7B, 8.7% on Qwen3-8B (B200).
  • Kernel-level speedups up to 2.23x vs multinomial sampling across H100, H200, B200, B300.
  • Tensor-parallel variant replaces logits all-gather with peer-to-peer writes overlapping the GEMM.
  • Open source Triton implementation at github.com/FlashSampling/FlashSampling; exactness verified on GSM8K.

Sampling the next token from a language model looks trivial on paper: take logits, softmax them, pick one index. But at serving time, that innocent step becomes a bandwidth hog. FlashSampling is a new primitive that folds the entire sample into the language model head matrix multiply, so the full logits tensor is never written to GPU memory. The result is an exact sampler, no approximations, that shaves up to 10% off time per output token in vLLM.

The hidden tax on every token

Modern LLMs decode against vocabularies of 100k to 250k tokens. At each step, the LM head produces a logits matrix of shape [batch, vocab], and the sampler then reads it back to normalize, scan, and pick one index per row. Recent measurements confirm the cost: sampling can account for over 10% of token generation time even on a single GPU, and 20 to 38% in tensor-parallel settings where logits must be gathered across ranks.

The pain is not arithmetic. It is memory traffic. Materializing the [B,V] logits tensor and launching extra kernels to normalize and sample from it adds memory traffic but no useful model computation, since the logits are immediately discarded after a single sample is drawn. In this regime, the separate sampler is pure overhead. In multi-GPU setups it gets worse, because ranks must all-gather their logit shards before anyone can sample.

You never needed softmax anyway

The trick underneath FlashSampling is old. The Gumbel-Max identity says that if you add independent Gumbel noise to your logits and take the argmax, you get an exact sample from the categorical distribution defined by softmax(logits). No normalization constant. No cumulative sum. Just an argmax over perturbed scores.

That reframing matters because argmax has a property softmax does not: it decomposes over partitions. The max of the whole vector equals the max of the per-tile maxes. So you can compute logits tile by tile, perturb them on chip, keep only the winning score and index per tile, and reduce over tiles at the end. The full logits tensor never needs to exist in high-bandwidth memory.

How the kernel is built

FlashSampling implements this as a two-stage design fused into the LM head epilogue:

  1. Stage 1, the fused kernel: compute one batch tile and one vocabulary tile on chip, apply deterministic transforms, add Gumbel noise, and keep the tile-local maximizer for each row.
  2. Stage 2, the reduction: reduce over vocabulary-tile candidates to obtain one global sample per row.

The intermediate buffer written to memory has shape roughly [batch, number of vocab tiles], which is tiny compared with [batch, vocab]. Temperature scaling, additive biases, and masking still work because they are deterministic transforms applied before the Gumbel perturbation. Random numbers come from a counter-based generator keyed on the output position, so runs are reproducible.

Scaling across GPUs without an all-gather

Tensor-parallel decoding shards the LM head weight matrix along the vocabulary dimension. Baselines run the matmul on each shard and then all-gather the full logits before sampling, which is a collective that stalls the pipeline. FlashSampling replaces that with a hierarchical factorization: each shard is treated as a group, and the categorical over the full vocabulary is factored into (pick a shard) then (pick within the shard).

Each rank produces a local exact sample plus a scalar log-sum-exp of its shard logits, and only these small summaries move between GPUs. Mechanically, per-tile point-to-point writes over NVLink fan out candidates as the matmul is still running, so cross-GPU communication overlaps with compute and HBM loads. Communication scales with the number of shards, not the vocabulary size.

What the benchmarks show

The authors evaluate on H100, H200, B200, and B300 GPUs using a Qwen3-style configuration (hidden size 4096, vocab 151936). Two things stand out.

First, kernel-level speedups are largest in the memory-bound decode regime. For batch size 64 or smaller, FlashSampling is faster than all baselines on all GPUs. The peak speedup versus standard multinomial sampling is 2.23x on B300, and 1.74x versus the FlashInfer top-k/top-p kernel on B200. Against FlashInfer's Gumbel-Max variant, which already skips softmax on materialized logits, the win is smaller because the remaining gap comes purely from avoiding materialization.

Second, the advantage narrows as batches grow. At batch 256, the workload starts to become compute-bound and the authors' Triton matmul is less efficient than cuBLAS. This trade-off is deliberate: Triton is platform-agnostic (AMD, Intel GPUs), so the cuBLAS gap is a trade-off for portability.

End-to-end vLLM results

Kernel microbenchmarks are one thing. The paper also drops FlashSampling into vLLM by replacing the LM head projection plus sampling step, and measures time per output token on the AIME22-24 problems.

ModelSetupPeak TPOT reduction
Qwen3-1.7B1 x B20010.2%
Qwen3-8B1 x B2008.7%
Qwen3-32B2 x B2002.9%
Llama-3.3-70B2 x B2002.7%

The pattern is clean: the smaller the model, the bigger the win, because the LM head takes a larger fraction of decode time relative to attention and feed-forward layers. For 70B-class models, attention and FFN dominate, so removing the sampling overhead only nudges the total.

Is it actually exact?

The authors verify this two ways. At the kernel level, they draw 10,000 samples from a vocab of size 512 and run a chi-squared goodness-of-fit test against a PyTorch reference, finding no significant difference. End to end, they run 1,319 questions from the GSM8K dataset using Qwen3-1.7B and check answers with an LLM judge. FlashSampling achieves 89.4% accuracy versus 89.6% for the baseline. That gap is not statistically significant under a paired bootstrap test.

When it helps and when it does not

FlashSampling is most useful for:

  • Low- and medium-batch decode workloads, where the LM head is memory-bandwidth-bound.
  • Small to mid-size models where sampling is a meaningful fraction of per-token time.
  • Tensor-parallel setups that currently pay for an all-gather of logits before sampling.
  • Portable deployments across NVIDIA and non-NVIDIA GPUs, since the kernel is written in Triton.

It is less compelling when:

  • Batches are large and the LM head is already compute-bound; cuBLAS still edges out the Triton matmul there.
  • Attention and FFN dominate decode time, as in 70B-plus models, where the ceiling on savings is small.
  • You need greedy decoding, which bypasses sampling entirely and does not benefit.

What is actually new

The Gumbel-Max trick and IO-aware kernel fusion are both well known individually. FlashAttention pioneered avoiding materialization in the attention path, and follow-ups like Cut Your Losses and the Liger Kernel applied the same fusion pattern to training-time cross entropy. The contribution here is applying it to inference-time sampling, plus a clean hierarchical factorization that makes online and tensor-parallel variants exact rather than approximate.

The code is available on GitHub, implemented in Triton with vLLM integration scripts. Current limitations: FP8 and MXFP4 inputs are not yet implemented, and the top-k extension is proven correct in the appendix but not yet coded. For anyone shipping a serving stack that is bottlenecked on the LM head, this looks like a drop-in improvement worth trying.