FlashSampling: Fast and Memory-Efficient Exact Sampling
Organizations: LMU Munich · FlashSampling · Princeton University
Abstract
Sampling from a categorical distribution is mathematically simple, but in large-vocabulary decoding, it often triggers extra memory traffic and extra kernels after the LM head. We present FlashSampling, an exact sampling primitive that fuses sampling into the LM-head matmul and never materializes the logits tensor in HBM. The method is simple: compute logits tile-by-tile on chip, add Gumbel noise, keep only one maximizer per row and per vocabulary tile, and finish with a small reduction over tiles. In tensor-parallel decoding, FlashSampling replaces the all-gather of logits with streaming peer-to-peer writes: This overlaps GPU-to-GPU communication with computation and HBM loads across up to 8 GPUs, with near-ideal scaling at large batch sizes. Our kernel is exact because decomposes over partitions; grouped variants for online and tensor-parallel settings are exact by hierarchical factorization of the categorical distribution. FlashSampling demonstrates kernel-level speedups on decode workloads across 4 different datacenter GPUs (H100, H200, B200, B300), and in end-to-end vLLM experiments, it reduces time per output token by up to on the models we test. These results show that exact sampling, with no approximation, can be integrated into the matmul itself, consolidating the bandwidth-bound sampling step in an efficient epilogue.
Figures & tables
| \toprule | FlashSampling | Multinomial Sampling | FI2 (Gumbel-Max) | |||
|---|---|---|---|---|---|---|
| \cmidrule (lr)2-3 \cmidrule (lr)4-5 \cmidrule (lr)6-7 | matmul (%) | sampl. (%) | matmul (%) | sampl. (%) | matmul (%) | sampl. (%) |
| \midrule 1 | 97.7 | 2.3 | 93.7 | 6.3 | 94.7 | 5.3 |
| 16 | 97.7 | 2.3 | 87.1 | 12.9 | 93.4 | 6.6 |
| 64 | 93.6 | 6.0 | 71.3 | 28.7 | 88.6 | 11.4 |
| 256 | 93.4 | 6.2 | 73.1 | 26.9 | 88.2 | 11.8 |
| \bottomrule | ||||||
Appendix figures & tables10 assets
Supplementary material from the paper’s appendix.
Appendix
| \toprule Level | Capacity | Bandwidth |
|---|---|---|
| \midrule Registers/SRAM | 256 KB / SM | 100 TB/s |
| L2 cache | 50 MB | 12 TB/s |
| HBM3 | 80 GB | 3.35 TB/s |
| \bottomrule |
| \toprule | H100 | H200 | B200 | B300 |
| \midrule Architecture | Hopper | Hopper | Blackwell | Blackwell |
| HBM capacity (GB) | 80 | 141 | 192 | 288 |
| HBM bandwidth (TB/s) | 3.35 | 4.8 | 8.0 | 8.0 |
| Peak BF16 dense (TFLOP/s) | 989 | 989 | 2,250 | 2,250 |
| Ops:byte ratio | 295 | 206 | 281 | 281 |
| \bottomrule |
| \toprule | vs. Multinomial Sampling | vs. FI1 (top- /top- ) | vs. FI2 (Gumbel-Max) | |||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| \cmidrule (lr)2-5 \cmidrule (lr)6-9 \cmidrule (lr)10-13 | H100 | H200 | B200 | B300 | H100 | H200 | B200 | B300 | H100 | H200 | B200 | B300 |
| \midrule 1 | 1.29 | 1.35 | 1.58 | 1.55 | 1.34 | 1.39 | 1.65 | 1.57 | 1.20 | 1.24 | 1.35 | 1.33 |
| 2 | 1.28 | 1.34 | 1.52 | 1.50 | 1.32 | 1.44 | 1.58 | 1.56 | 1.19 | 1.22 | 1.32 | 1.29 |
| 4 | 1.28 | 1.35 | 1.58 | 1.54 | 1.32 | 1.42 | 1.62 | 1.61 | 1.19 | 1.23 | 1.37 | 1.32 |
| 8 | 1.31 | 1.38 | 1.58 | 1.55 | 1.35 | 1.48 | 1.63 | 1.60 | 1.19 | 1.24 | 1.37 | 1.33 |
| 16 | 1.35 | 1.44 | 1.66 | 1.63 | 1.37 | 1.49 | 1.70 | 1.65 | 1.20 | 1.25 | 1.39 | 1.35 |
| \toprule | vs. Multinomial Sampling | vs. FI1 (top- /top- ) | vs. FI2 (Gumbel-Max) | |||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| \cmidrule (lr)2-5 \cmidrule (lr)6-9 \cmidrule (lr)10-13 | H100 | H200 | B200 | B300 | H100 | H200 | B200 | B300 | H100 | H200 | B200 | B300 |
| \midrule 1 | 1.23 | 1.26 | 1.45 | 1.41 | 1.20 | 1.25 | 1.36 | 1.30 | 1.14 | 1.13 | 1.21 | 1.19 |
| 2 | 1.22 | 1.23 | 1.41 | 1.39 | 1.21 | 1.21 | 1.33 | 1.29 | 1.13 | 1.11 | 1.21 | 1.18 |
| 4 | 1.22 | 1.24 | 1.35 | 1.34 | 1.20 | 1.21 | 1.31 | 1.28 | 1.13 | 1.12 | 1.15 | 1.13 |
| 8 | 1.23 | 1.25 | 1.37 | 1.36 | 1.21 | 1.23 | 1.30 | 1.29 | 1.13 | 1.12 | 1.14 | 1.13 |
| 16 | 1.24 | 1.27 | 1.40 | 1.39 | 1.21 | 1.25 | 1.32 | 1.30 | 1.14 | 1.13 | 1.15 | 1.15 |
| \toprule | Method | TP=1 | TP=2 | TP=4 | TP=8 |
|---|---|---|---|---|---|
| \midrule \multirow 4*16 | FlashSampling | 333.8 | 203.8 | 125.8 | 87.1 |
| FI1 | 438.3 | 321.5 | 254.1 | 215.1 | |
| FI2 | 385.0 | 268.4 | 200.7 | 164.2 | |
| Multinomial | 461.8 | 344.1 | 274.5 | 241.7 | |
| \midrule \multirow 4*64 | FlashSampling | 344.1 | 215.9 | 136.2 | 96.4 |
| FI1 | 494.8 | 383.1 | 313.3 | 258.0 |
| \toprule | Qwen3-1.7B (TP1) | Qwen3-8B (TP1) | Qwen3-32B (TP2) | Llama-3.3-70B (TP2) | ||||
|---|---|---|---|---|---|---|---|---|
| \cmidrule (lr)2-3 \cmidrule (lr)4-5 \cmidrule (lr)6-7 \cmidrule (lr)8-9 | Base | FlashSampling | Base | FlashSampling | Base | FlashSampling | Base | FlashSampling |
| \midrule 1 | 1.82 | 1.68 | 3.91 | 3.62 | 7.73 | 7.64 | 14.21 | 14.10 |
| 2 | 1.81 | 1.67 | 3.93 | 3.61 | 7.85 | 7.66 | 14.38 | 14.24 |
| 4 | 1.81 | 1.66 | 4.01 | 3.68 | 7.91 | 7.79 | 14.60 | 14.42 |
| 8 | 1.83 | 1.67 | 4.06 | 3.74 | 8.00 | 7.82 | 14.77 | 14.62 |
| 16 | 1.84 | 1.68 | 4.24 | 3.87 | 8.30 | 8.08 | 15.15 | 14.76 |
| \toprule | Qwen3-1.7B (TP1) | Qwen3-8B (TP1) | Qwen3-32B (TP2) | Llama-3.3-70B (TP2) |
|---|---|---|---|---|
| \midrule 1 | % | % | % | % |
| 2 | % | % | % | % |
| 4 | % | % | % | % |
| 8 | % | % | % | % |
| 16 | % | % | % | % |
| 32 | % | % | % | % |
| \toprule | , | , | ||
|---|---|---|---|---|
| \cmidrule (lr)2-3 \cmidrule (lr)4-5 | Predicted | Measured | Predicted | Measured |
| \midrule 1 | 0.02% | 0.5% | 0.05% | 0.9% |
| 4 | 0.10% | 0.7% | 0.20% | 1.2% |
| 16 | 0.39% | 1.6% | 0.78% | 2.1% |
| 64 | 1.56% | 3.6% | 3.13% | 4.8% |
| 128 | 3.13% | 5.4% | 6.25% | 8.9% |
| \toprule Asset | Reference | License |
|---|---|---|
| \midrule PyTorch | Paszke et al. [2019] | \href https://github.com/pytorch/pytorch/blob/main/LICENSEBSD-3-Clause |
| Triton | Tillet et al. [2019] | \href https://github.com/triton-lang/triton/blob/main/LICENSEMIT |
| vLLM | Kwon et al. [2023] | \href https://github.com/vllm-project/vllm/blob/main/LICENSEApache-2.0 |
| FlashInfer | Ye et al. [2025] | \href https://github.com/flashinfer-ai/flashinfer/blob/main/LICENSEApache-2.0 |
| Qwen3 (1.7B, 8B, 32B) | Yang et al. [2025] | \href https://huggingface.co/Qwen/Qwen3-8B/blob/main/LICENSEApache-2.0 |
| Llama-3.3-70B | Grattafiori et al. [2024] | \href https://www.llama.com/llama3_3/license/Llama 3.3 Community License |
LLM Inference Acceleration
LLM inference acceleration develops algorithms and systems that reduce language-model latency, memory use, and serving cost while preserving output quality.
See all 1,084 papers