Papers
Topics
Authors
Recent
Search
2000 character limit reached

FlashSampling: Efficient Sampling in LLMs

Updated 18 March 2026
  • FlashSampling is an exact sampling primitive that integrates the Gumbel-Max trick into the LM-head matmul to enable efficient, on-chip token selection.
  • It partitions the vocabulary into tiles to perform in-register Gumbel perturbations, reducing memory traffic and eliminating separate post-processing steps.
  • The approach supports tensor-parallel and online contexts, achieving up to 2× kernel speedups and significant end-to-end latency improvements.

FlashSampling is an exact sampling primitive designed to accelerate large-vocabulary decoding in neural LLMs by fusing the categorical sampling step directly into the language-model head (LM-head) matrix multiplication (matmul), thereby eliminating the overhead associated with separate post-processing kernels and the materialization of logits tensors in high-bandwidth memory (HBM) (Ruiz et al., 16 Mar 2026). The approach exploits the mathematical decomposability of the Gumbel-Max trick and implements a highly memory- and bandwidth-efficient sampling strategy suitable for both standard and tensor-parallel transformer decoders.

1. High-Level Algorithmic Strategy

In conventional LLM decoding, the output token distribution is sampled via a two-stage process: (a) a General Matrix Multiply (GEMM) computes the logits matrix Y=HWY = H W^\top of shape [B,V][B, V] (with BB the batch size and VV the vocabulary size), which is then materialized in HBM; (b) a series of additional kernels apply temperature scaling, bias, masking, softmax (or cumulative distribution function), and finally, stochastic sampling.

FlashSampling eliminates extraneous memory traffic and kernel launches by fusion: sampling is implemented as an epilogue to the matmul using an on-chip tile-by-tile approach. The vocabulary is partitioned into TT disjoint tiles {V1,,VT}\{V_1, \dots, V_T\} of size KtileK_{\rm tile}. For each tile, partial dot products are computed in-register, noise is injected via the Gumbel-Max trick, and only the per-tile maximizer (score and index) is stored in HBM. A final lightweight reduction kernel gathers these TT candidates per row, returning the global sample via argmax\arg\max. This construction guarantees exactness (not an approximation), as argmax\arg\max is associative across a partition.

2. Mathematical Foundations and Sampling Decomposition

FlashSampling employs the Gumbel-Max trick for exact categorical sampling:

[B,V][B, V]0

[B,V][B, V]1

Here, [B,V][B, V]2 denotes the transformed logit for index [B,V][B, V]3 (i.e., post temperature/bias/mask). Partitioning [B,V][B, V]4 into tiles, the global maximizer is computed as the maximum among the tile-local maxima:

[B,V][B, V]5

[B,V][B, V]6

By the max-stability property of Gumbel distributions, this two-stage procedure is mathematically equivalent to global Gumbel-Max on [B,V][B, V]7 (see Lemma 4.6 in (Ruiz et al., 16 Mar 2026)).

Hierarchical (grouped) decompositions allow further flexibility. If the vocabulary is split into [B,V][B, V]8 groups [B,V][B, V]9, one defines

BB0

BB1

Sampling can proceed by first selecting a group via BB2, followed by a within-group argmax. Online merging facilitates BB3-memory sampling over streamed groups, and tensor-parallel schemes treat each device's vocabulary shard as a group, needing only an BB4-sized all-gather.

3. Implementation Specifics

To avoid full logits materialization, FlashSampling employs a custom Triton or CUDA kernel:

  • Each BB5 subblock is computed in registers/SRAM, perturbed with Gumbel samples, and immediately reduced to a BB6 identifier (best score and index).
  • Per-row, an array of BB7 tile-local maxima is accumulated. A minimal BB8 reduction over these yields the sample token for each batch row.
  • Deterministic transformations (temperature, bias, or masking by setting logits to BB9) are applied in-tile.
  • A counter-based RNG (Philox) indexed by VV0 generates reproducible Gumbel variates, ensuring thread safety.
  • All dot-products and score perturbations use FP32 accumulators for numerical stability, supporting BF16 activations/weights if present.
  • The kernel is designed to substitute the standard matmul + “sample_from_logits” pipeline in transformer decoders.

4. Performance and Latency Benchmarks

FlashSampling achieves considerable kernel speedup and latency reduction in multiple workflows (Ruiz et al., 16 Mar 2026):

Kernel-level speedups (for VV1, VV2):

Baseline H100 H200 B200 B300
Multinomial (cuBLAS + softmax + multinomial) 1.60× 1.60× 1.84× 1.81×
FlashInfer top-k/top-p 2.52×
FlashInfer Gumbel-Max 1.39× 1.37×
  • At batch size VV3, speedups of 1.25× (H100), 1.28× (H200), and 1.46× (B200/B300) versus Multinomial.
  • In the latency-sensitive decode regime (VV4), baseline sampling overhead is 12–29% of kernel time; FlashSampling reduces this to 2–6%.
  • End-to-end vLLM experiments (B200 GPU): up to 19% reduction in time-per-output-token (TPOT) for Qwen3-1.7B, 3–7% for Qwen3-8B, 1–2% for Qwen3-32B, and 0.3–2.4% for GPT-OSS-120B. The gain is largest for smaller LMs where LM-head matmul is a larger runtime fraction.

5. Memory, Bandwidth, and Architectural Insights

Baseline decode traffic incurs at least VV5 floats (write/read of logits), with VV6 bytes in BF16. Relative to mandatory LM-head weight fetch (VV7 bytes), fusion offers a memory bandwidth saving of VV8 (VV93.1% for TT0, TT1).

Empirically, performance gains exceed what is explainable by traffic reduction alone: measured end-to-end speedups are 50–80% over naive sampling kernels. The predominant advantages derive from the elimination of kernel launches, inter-kernel synchronization, and full-HBM passes.

In memory-bound regimes (TT2), FlashSampling achieves 85–95% of peak HBM throughput, compared to 65–75% for baseline approaches. The algorithm thus tracks closer to the roofline bandwidth bound due to full fusion of post-processing.

Limitations: For TT3, GEMM efficiency is dominated by vendor-optimized BLAS (e.g., cuBLAS), and the Triton-based kernel can be outperformed by the baseline pipeline. Nucleus (top-TT4) sampling, which requires a non-tileable global softmax/cumsum, typically applies top-TT5 with large TT6 as a bottleneck mitigation, followed by a tiny softmax/top-TT7 post-filtering. This procedure is orthogonal to the core FlashSampling method.

6. Integration in Online and Tensor-Parallel Contexts

FlashSampling's hierarchical reduction approach facilitates online group merging and compatibility with tensor-parallel sharding: distributed settings treat each worker’s vocabulary chunk as a group. Each rank computes local maxima and log-masses; a subsequent all-gather and Gumbel-Max over TT8 scalars yields the correct global sample without TT9 communication. Grouped or hierarchical variants (Group-Gumbel-Max) are mathematically exact by independent max-stability of Gumbels, preserving the categorical distribution over the full vocabulary.

7. Summary and Practical Impact

FlashSampling demonstrates that mathematically exact categorical sampling is achievable as an integral lightweight epilogue to the LM-head matmul, removing explicit post-processing bottlenecks and on-HBM tensor materialization (Ruiz et al., 16 Mar 2026). By exploiting the associativity of {V1,,VT}\{V_1, \dots, V_T\}0 and hierarchical Gumbel-Max decompositions, it ensures equivalence to traditional full-vocabulary sampling, while enabling up to 2× kernel speedups and up to 19% end-to-end token latency reduction in vLLM workloads. The method provides a robust, general-purpose primitive for efficient sampling on modern GPU architectures, with strong empirical and theoretical guarantees of correctness and performance.

Definition Search Book Streamline Icon: https://streamlinehq.com
References (1)

Topic to Video (Beta)

No one has generated a video about this topic yet.

Whiteboard

No one has generated a whiteboard explanation for this topic yet.

Follow Topic

Get notified by email when new papers are published related to FlashSampling.