Skip to main content 홈 크리에이터 adu2021 skillxiv flash-sampling-efficient-decoding
flash-sampling-efficient-decoding Fuse categorical sampling directly into LM-head matrix multiplication to eliminate logits materialization. Use Gumbel noise during computation and hierarchical reduction to achieve 19% token-level latency reduction.
설치로 이동 Skills Marketplace 커뮤니티가 만든 AI 스킬을 발견하고 탐색하세요.
Codex 또는 Claude로 설치 이 Prompt를 복사해 Codex, Claude 또는 다른 어시스턴트에 붙여 넣으면 Skill 페이지를 검토하고 설치를 진행할 수 있습니다.
직접 명령은 검토 Prompt를 거치지 않습니다. 실행하기 전에 소스를 확인하세요.
npx skills add https://github.com/ADu2021/skillXiv --skill flash-sampling-efficient-decoding명령은 한 줄로 유지됩니다. 복사하기 전에 가로로 스크롤해 전체 내용을 확인하세요.
로컬 사본을 원하시나요? SkillsMP에서 현재 제공할 수 있는 파일을 다운로드하세요.
Zip 다운로드 다운로드 중... name flash-sampling-efficient-decoding title FlashSampling: Fast and Memory-Efficient Exact Sampling version 0.0.2 engine skillxiv-v0.0.2-claude-opus-4.6 license MIT url https://arxiv.org/abs/2603.15854 keywords ["Sampling Efficiency","Exact Sampling","Gumbel Trick","Bandwidth Optimization","Token Generation"] description Fuse categorical sampling directly into LM-head matrix multiplication to eliminate logits materialization. Use Gumbel noise during computation and hierarchical reduction to achieve 19% token-level latency reduction.
FlashSampling: Fusing Sampling into Matrix Multiplication
Sampling from the output logits is a hidden performance bottleneck in language model inference. Current approaches materialize large logits tensors in high-bandwidth memory (HBM), consuming significant memory bandwidth and requiring multiple GPU kernels after LM-head computation. FlashSampling solves this by fusing sampling directly into the LM-head matrix multiplication: logits are computed tile-by-tile on-chip, Gumbel noise is applied during computation, and only the maximum per-tile is tracked—enabling exact categorical sampling without materializing full logits. This achieves up to 19% reduction in per-token latency across modern GPUs.
The approach remains mathematically exact because argmax decomposes over partitions (max of maximums equals global maximum).
Core Concept
FlashSampling exploits the mathematical property that sampling from a categorical distribution can be reformulated as:
Standard Sampling:
logits = LM_head(hidden) # [vocab_size]
probs = softmax(logits)
token = categorical_sample(probs)
FlashSampling:
# Compute logits tile-by-tile, maintaining only running maximum
max_logit = -inf
max_idx = -1
for tile_idx in range(num_tiles):
tile_logits = LM_head_partial(hidden, tile_idx)
tile_logits += Gumbel_noise
tile_max_idx = argmax(tile_logits)
tile_max = tile_logits[tile_max_idx]
if tile_max > max_logit:
max_logit = tile_max
max_idx = tile_idx * tile_size + tile_max_idx
# Token with highest Gumbel-perturbed logit is sampled token
token = max_idx
Because argmax commutes with Gumbel perturbation, this is mathematically equivalent to exact sampling.
Architecture Overview
LM-Head Tiling — Decompose matrix multiplication into independent tiles
On-Chip Computation — Keep tiles in fast cache, avoid HBM spills
Gumbel Noise Injection — Apply noise during tile computation
Running Maximization — Track (value, index) of best tile element
Grouped Variant — Hierarchical reduction for tensor parallelism
Kernel Fusion — Single GPU kernel replaces compute + memory stages
Batch Processing — Maintain efficiency across batch dimensions
Implementation Steps
Start by implementing the basic tile-based sampling logic.
import torch
import torch.nn.functional F
numpy np
:
( ):
.vocab_size = vocab_size
.hidden_dim = hidden_dim
.tile_size = tile_size
.num_tiles = (vocab_size + tile_size - ) // tile_size
( ):
u = torch.rand_like(logits)
gumbel_noise = -torch.log(-torch.log(u + ) + )
perturbed = (logits + gumbel_noise) / temperature
torch.argmax(perturbed, dim=- )
( ) -> torch.Tensor:
batch_size = hidden.size( )
best_indices = torch.zeros(batch_size, dtype=torch.long)
best_scores = torch.full((batch_size,), ( ))
tile_idx ( .num_tiles):
start_vocab = tile_idx * .tile_size
end_vocab = (start_vocab + .tile_size, .vocab_size)
tile_weights = lm_head_weight[start_vocab:end_vocab, :]
tile_logits = torch.matmul(hidden, tile_weights.t())
u = torch.rand_like(tile_logits)
gumbel = -torch.log(-torch.log(u + ) + )
tile_scores = (tile_logits + gumbel) / temperature
tile_best_scores, tile_best_local_idx = torch. (tile_scores, dim= )
improved = tile_best_scores > best_scores
best_scores[improved] = tile_best_scores[improved]
best_indices[improved] = (
start_vocab + tile_best_local_idx[improved]
)
best_indices
( ) -> torch.Tensor:
group_vocab_size = .vocab_size // group_size
best_indices = torch.zeros(hidden.size( ), dtype=torch.long)
best_scores = torch.full((hidden.size( ),), ( ))
group_idx (group_size):
group_start = group_idx * group_vocab_size
group_end = (group_idx + ) * group_vocab_size
group_weights = lm_head_weight[group_start:group_end, :]
tile_in_group ((group_vocab_size + .tile_size - )
// .tile_size):
tile_start = group_start + tile_in_group * .tile_size
tile_end = (tile_start + .tile_size, group_end)
tile_weights = lm_head_weight[tile_start:tile_end, :]
tile_logits = torch.matmul(hidden, tile_weights.t())
u = torch.rand_like(tile_logits)
gumbel = -torch.log(-torch.log(u + ) + )
tile_scores = (tile_logits + gumbel) / temperature
tile_best_scores, tile_best_idx = torch. (tile_scores, dim= )
improved = tile_best_scores > best_scores
best_scores[improved] = tile_best_scores[improved]
best_indices[improved] = tile_start + tile_best_idx[improved]
best_indices
as
import
as
class
FlashSampler
"""Efficient sampling via tile-based computation."""
def
__init__
self, vocab_size, hidden_dim, tile_size=256
self
self
self
self
1
@staticmethod
def
gumbel_sample
logits: torch.Tensor, temperature: float = 1.0
"""Sample using Gumbel-max trick with temperature scaling."""
1e-20
1e-20
return
1
def
sample_tiled
self, hidden: torch.Tensor, lm_head_weight: torch.Tensor,
temperature: float = 1.0
"""
Sample from categorical distribution using tile-based computation.
Args:
hidden: [batch_size, hidden_dim]
lm_head_weight: [vocab_size, hidden_dim]
temperature: sampling temperature
"""
0
float
'-inf'
for
in
range
self
self
min
self
self
1e-20
1e-20
max
1
return
def
sample_grouped
self, hidden: torch.Tensor,
lm_head_weight: torch.Tensor,
temperature: float = 1.0 ,
group_size: int = 4
"""
Grouped variant for tensor-parallel settings.
Each device handles vocab_size // group_size tokens.
"""
self
0
0
float
'-inf'
for
in
range
1
for
in
range
self
1
self
self
min
self
1e-20
1e-20
max
1
return
Now implement the kernel-fused version that operates at the hardware level.
class FusedFlashSamplingKernel :
"""Fused kernel combining LM-head and sampling."""
def __init__ (self, vocab_size, hidden_dim, tile_size=256 , device='cuda' ):
self .vocab_size = vocab_size
self .hidden_dim = hidden_dim
self .tile_size = tile_size
self .device = device
self .tile_buffer = torch.zeros((tile_size, hidden_dim),
device=device)
self .scratch_space = torch.zeros((tile_size,), device=device)
def fused_lm_head_sample (self, hidden: torch.Tensor,
lm_head_weight: torch.Tensor,
lm_head_bias: torch.Tensor = None ,
temperature: float = 1.0 ) -> torch.Tensor:
"""
Single fused kernel: LM-head matrix multiplication + sampling.
This represents what would be implemented in CUDA/Triton.
"""
batch_size = hidden.size(0 )
best_indices = torch.zeros(batch_size, dtype=torch.long,
device=self .device)
best_scores = torch.full((batch_size,), float ('-inf' ),
device=self .device)
for tile_idx in range ((self .vocab_size + self .tile_size - 1 )
// self .tile_size):
start_vocab = tile_idx * self .tile_size
end_vocab = min (start_vocab + self .tile_size, self .vocab_size)
tile_vocab_size = end_vocab - start_vocab
tile_weights = lm_head_weight[start_vocab:end_vocab, :]
tile_logits = torch.matmul(hidden, tile_weights.t())
if lm_head_bias is not None :
tile_bias = lm_head_bias[start_vocab:end_vocab]
tile_logits = tile_logits + tile_bias
u = torch.rand_like(tile_logits)
gumbel = -torch.log(-torch.log(u + 1e-20 ) + 1e-20 )
tile_scores = (tile_logits + gumbel) / temperature
tile_max_scores, tile_max_indices = torch.max (tile_scores, dim=1 )
improved = tile_max_scores > best_scores
best_scores[improved] = tile_max_scores[improved]
best_indices[improved] = start_vocab + tile_max_indices[improved]
return best_indices
def benchmark_vs_standard (self, batch_size=32 , hidden_dim=4096 ,
vocab_size=128000 , num_iterations=100 ):
"""Compare latency with standard sampling."""
import time
hidden = torch.randn(batch_size, hidden_dim, device=self .device)
lm_head_weight = torch.randn(vocab_size, hidden_dim,
device=self .device)
for _ in range (5 ):
_ = self .fused_lm_head_sample(hidden, lm_head_weight)
torch.cuda.synchronize()
start = time.time()
for _ in range (num_iterations):
_ = self .fused_lm_head_sample(hidden, lm_head_weight)
torch.cuda.synchronize()
flash_time = time.time() - start
torch.cuda.synchronize()
start = time.time()
for _ in range (num_iterations):
logits = torch.matmul(hidden, lm_head_weight.t())
u = torch.rand_like(logits)
gumbel = -torch.log(-torch.log(u + 1e-20 ) + 1e-20 )
_ = torch.argmax(logits + gumbel, dim=-1 )
torch.cuda.synchronize()
standard_time = time.time() - start
speedup = standard_time / flash_time
reduction_percent = (1 - 1 /speedup) * 100
print (f"FlashSampling: {flash_time:.3 f} s" )
print (f"Standard: {standard_time:.3 f} s" )
print (f"Speedup: {speedup:.2 f} x ({reduction_percent:.1 f} % reduction)" )
return speedup
Finally, integrate into inference pipeline and demonstrate usage.
class VLLMWithFlashSampling :
"""vLLM-compatible interface with FlashSampling."""
def __init__ (self, model_name, use_flash_sampling=True ):
self .model = load_model(model_name)
self .tokenizer = load_tokenizer(model_name)
self .use_flash_sampling = use_flash_sampling
if use_flash_sampling:
vocab_size = len (self .tokenizer)
hidden_dim = self .model.config.hidden_size
self .sampler = FusedFlashSamplingKernel(vocab_size, hidden_dim)
def generate (self, prompt: str , max_tokens=100 , temperature=0.7 ):
"""Generate with optional FlashSampling."""
input_ids = self .tokenizer.encode(prompt)
generated = input_ids.copy()
for _ in range (max_tokens):
with torch.no_grad():
outputs = self .model(torch.tensor([generated]))
hidden = outputs.hidden_states[-1 ][:, -1 , :]
lm_head_weight = self .model.lm_head.weight
if self .use_flash_sampling:
next_token = self .sampler.fused_lm_head_sample(
hidden, lm_head_weight, temperature=temperature
)
else :
logits = torch.matmul(hidden, lm_head_weight.t())
u = torch.rand_like(logits)
gumbel = -torch.log(-torch.log(u + 1e-20 ) + 1e-20 )
next_token = torch.argmax(logits + gumbel, dim=-1 )
generated.append(next_token.item())
if next_token.item() == self .tokenizer.eos_token_id:
break
return self .tokenizer.decode(generated)
Practical Guidance Hyperparameters and When to Use:
Tile size 256-512 works well; smaller tiles improve cache locality, larger reduce kernel launch overhead
Temperature 0.7-1.0 for typical generation; lower temperatures sharpen distribution
Use when performing high-throughput inference on modern GPUs (H100, B200, etc.)
Particularly effective for large vocabulary models (100K+ tokens)
Benefit amplifies with larger batch sizes (less kernel launch overhead amortization)
For CPU inference (no CUDA optimization benefit)
For very small vocabulary models (< 10K) where logits materialization is already fast
When using older GPU architectures without good on-chip memory
Gumbel noise generation becoming bottleneck; batch random number generation across tiles
Tile boundaries causing alignment issues; use aligned tile sizes
Numerical instability in log-probability computation; use log-sum-exp tricks
Not accounting for biases; apply bias during tile computation
Reference