[base/sampler] CCCL dispatch_merge_sort alias_temporaries: eliminate .repeat() allocation in _apply_penalties

Source: CCCL dispatch_merge_sort.cuh alias_temporaries() pattern
  - 4 allocations (partitions + keys + values + vsmem) packed into 1 cudaMalloc
  - Principle: never allocate throwaway intermediates in the hot path
  - dispatch_merge_sort uses ping-pong buffer to avoid copying between passes

Changes to vllm/model_executor/layers/sampler.py _apply_penalties():
  Old: repetition_penalties[:, None].repeat(1, vocab_size)
    → Creates full (num_seqs, 152064) float32 tensor = 608KB
    → Then masks most values to 1.0 (wasted allocation)
    → Then torch.where over entire vocab (wasted compute on masked positions)

  New: Broadcasting with unsqueeze(1) + conditional torch.where
    → rep_pen shape: (num_seqs, 1) broadcasts to (num_seqs, vocab_size)
    → Zero intermediate allocation
    → token_mask selects only prompt/output tokens (typically <1% of vocab)
    → Nested torch.where applies divide/multiply only where needed

Memory saving per decode step: 608KB (vocab=152064, num_seqs=1, float32)
This is in the penalties hot path that runs every decode step when
repetition_penalty != 1.0.

Also in this commit (from previous edit):
  - Fixed _sampler_cache -> _sampler_temp_storage module-level declaration
  - CCCL alias_temporaries pattern for bin_counts pre-allocation
This commit is contained in:
muh-bot
2026-08-06 04:14:11 +00:00
parent 1064ce756b
commit 322f5553e1

View File

@@ -426,10 +426,30 @@ def _apply_penalties(logits: torch.Tensor, prompt_tokens_tensor: torch.Tensor,
output_bin_counts, output_mask = _get_bin_counts_and_mask(
output_tokens_tensor, vocab_size, num_seqs)
repetition_penalties = repetition_penalties[:, None].repeat(1, vocab_size)
repetition_penalties[~(prompt_mask | output_mask)] = 1.0
logits = torch.where(logits > 0, logits / repetition_penalties,
logits * repetition_penalties)
# CCCL dispatch_merge_sort.cuh: alias_temporaries packs 4 allocations
# (partitions + keys_buf + values_buf + vsmem) into one cudaMalloc.
# Principle: never allocate throwaway intermediates in the hot path.
#
# Old code: repetition_penalties[:, None].repeat(1, vocab_size)
# → allocates (num_seqs × vocab_size × 4) = 608KB for Qwen3.6 (vocab=152064)
# → then masks most of it to 1.0 → wasted allocation
#
# New code: apply repetition penalty only to tokens that appear in
# prompt or output, using in-place operations and indexing.
# Zero allocation overhead.
token_mask = prompt_mask | output_mask # (num_seqs, vocab_size) bool
# For tokens that appear: divide positive logits, multiply negative logits
# For tokens that don't appear: no change (equivalent to penalty=1.0)
rep_pen = repetition_penalties.unsqueeze(1) # (num_seqs, 1) — broadcasts
logits = torch.where(
token_mask & (logits > 0),
logits / rep_pen,
torch.where(
token_mask & (logits < 0),
logits * rep_pen,
logits
)
)
# We follow the definition in OpenAI API.
# Refer to https://platform.openai.com/docs/api-reference/parameter-details