Skip to content

[Example] Add Top-n-sigma logit truncation custom logits processor - #7

Open
Jackie2049 wants to merge 1349 commits into
mainfrom
feature/top-n-sigma-logits-processor
Open

[Example] Add Top-n-sigma logit truncation custom logits processor#7
Jackie2049 wants to merge 1349 commits into
mainfrom
feature/top-n-sigma-logits-processor

Conversation

@Jackie2049

@Jackie2049 Jackie2049 commented Jun 4, 2026

Copy link
Copy Markdown
Owner

Summary

Closes #2

Adds an example demonstrating Top-nσ as a custom logits processor in vLLM, based on the paper:

Tang et al., "Top-nσ: Not All Logits Are You Need", ACL 2025. arXiv:2411.07641

What is Top-nσ?

Top-nσ is a logit-space dynamic truncation method that uses the standard deviation of the logit distribution to set a filtering threshold:

threshold = max_logit - n * std_logit
logits[logits < threshold] = -inf

Unlike probability-space filters (top_p, min_p), it operates before softmax and adapts to distribution "peakiness":

  • Sharp distribution (model certain): small std → narrow threshold → fewer candidates
  • Flat distribution (model uncertain): small std → wide threshold → more candidates

How it compares to other truncation methods

Method Space Truncation rule Adaptive?
top_k Probability Keep top-k highest-probability tokens ❌ Fixed count
top_p (nucleus) Probability Keep tokens until cumulative probability ≥ p ✅ Adaptive count only
min_p Probability Keep tokens with prob ≥ max_prob × min_p ✅ Relative threshold
top_nσ Logit threshold = max_logit − n × std ✅ Dual-adaptive
temperature Logit logits / T — rescales distribution steepness ❌ No truncation

Key distinction: top_k/top_p/min_p operate after softmax (probability space); top_nσ operates before softmax (logit space).

Why Top-nσ?

  • Truncation before softmax — top_nσ filters in logit space first, then softmax only processes the surviving candidates (~2.5% of vocab). In contrast, top_p/min_p must compute a full-vocab softmax first, then truncate in probability space.
  • Dual adaptivity — the std-based threshold adjusts both how many and how loosely candidates are kept in one formula: sharp distributions auto-narrow, flat ones auto-widen. top_k fixes the count; top_p adapts the count but not the threshold tightness.
  • Temperature-stable — candidate count stays consistent across temperature scaling (logit std scales proportionally), while top_p/min_p candidate counts vary significantly with T.
  • Argmax-invariant — the top-1 token always survives (0 std from itself), so greedy decoding can skip this processor entirely.

Changes

  • New file: examples/features/logits_processor/top_n_sigma.py
  • apply() method uses vectorized Torch operations (not Python per-row loop)

Key properties

  • Argmax-invariant: doesn't affect greedy decoding
  • Uses vLLM's extra_args mechanism (no SamplingParams changes needed)
  • Handles edge cases: NaN/Inf logits, zero std, n≤0 validation

Usage

from vllm import LLM, SamplingParams

llm = LLM(
    model="facebook/opt-125m",
    logits_processors=[TopNSigmaLogitsProcessor],
)

sampling_params = SamplingParams(
    temperature=0.8,
    extra_args={"top_n_sigma": 2.0}
)

Testing

  • Example runs with facebook/opt-125m
  • Vectorized apply() is bit-for-bit identical to row-by-row loop across FP32/FP16 configs
  • Argmax-invariant verified across 32 configurations (all dtype/sharpness/n combinations)
  • Edge cases handled: NaN/Inf logits, std=0, n≤0 validation, empty req_info

@Jackie2049
Jackie2049 force-pushed the feature/top-n-sigma-logits-processor branch 5 times, most recently from eefc3f0 to 2e95e18 Compare June 10, 2026 12:28
@Codekiing
Codekiing force-pushed the feature/top-n-sigma-logits-processor branch from e513f58 to 2683269 Compare June 11, 2026 08:38
@Jackie2049
Jackie2049 force-pushed the feature/top-n-sigma-logits-processor branch 2 times, most recently from 1d2f067 to 9813229 Compare June 11, 2026 13:37
@Codekiing
Codekiing force-pushed the feature/top-n-sigma-logits-processor branch from 9813229 to 1e74d63 Compare June 11, 2026 13:48
@Jackie2049
Jackie2049 force-pushed the feature/top-n-sigma-logits-processor branch from e00cfff to f53ea0c Compare June 12, 2026 01:09
@Codekiing
Codekiing force-pushed the feature/top-n-sigma-logits-processor branch 5 times, most recently from a3c15ac to 2f09705 Compare June 12, 2026 02:48
AndreasKaratzas and others added 14 commits July 2, 2026 17:45
Signed-off-by: Andreas Karatzas <akaratza@amd.com>
Signed-off-by: Andreas Karatzas <akaratza@amd.com>
Signed-off-by: Andreas Karatzas <akaratza@amd.com>
Signed-off-by: Adam Baranowski <adam.baranowski@poolside.ai>
)

Signed-off-by: wenjun.liu <wenjun.liu@intel.com>
Signed-off-by: zengxian <xiangdong.zeng@intel.com>
Co-authored-by: zengxian <xiangdong.zeng@intel.com>
Signed-off-by: Nick Hill <nickhill123@gmail.com>
…ate (vllm-project#47485)

Signed-off-by: mgoin <mgoin64@gmail.com>
Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
…g window (vllm-project#47217)

Signed-off-by: Luciano Martins <lucianommartins@users.noreply.github.com>
Co-authored-by: Luciano Martins <lucianommartins@users.noreply.github.com>
…er (vllm-project#44682)

Co-authored-by: Bugen Zhao <i@bugenzhao.com>
Signed-off-by: Ting Sun <suntcrick@gmail.com>
Signed-off-by: Bugen Zhao <i@bugenzhao.com>
…mitive-level profiling (vllm-project#47467)

Signed-off-by: Evgeny Parshutin <eugeny.parshutin@intel.com>
Signed-off-by: zengxian <xiangdong.zeng@intel.com>
cleonard530 and others added 30 commits July 8, 2026 10:56
Signed-off-by: Kaihang Jiang <kaihangj@login-lyris02.lyris.clusters.nvidia.com>
…t#46661)

Signed-off-by: Thien Tran <gau.nernst@yahoo.com.sg>
Signed-off-by: Tyler Michael Smith <tyler@tylermsmith.com>
Signed-off-by: Tyler Michael Smith <tlrmchlsmth@gmail.com>
Signed-off-by: Itay Etelis <itay.etelis@ibm.com>
Signed-off-by: Itay Etelis <92247226+Etelis@users.noreply.github.com>
Signed-off-by: Or Ozeri <oro@il.ibm.com>
Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-authored-by: Itay Etelis <92247226+Etelis@users.noreply.github.com>
Co-authored-by: Itay Etelis <itay.etelis@ibm.com>
Co-authored-by: Or Ozeri <oro@il.ibm.com>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
…-project#46718)

Signed-off-by: LopezCastroRoberto <rocastro@redhat.com>
Signed-off-by: Roberto L. Castro <38211239+LopezCastroRoberto@users.noreply.github.com>
Signed-off-by: Martin Hickey <martin.hickey@ie.ibm.com>
Co-authored-by: Harry Mellor <19981378+hmellor@users.noreply.github.com>
Signed-off-by: Andreas Karatzas <akaratza@amd.com>
…lm-project#48010)

Signed-off-by: Harry Mellor <19981378+hmellor@users.noreply.github.com>
…8046)

Signed-off-by: Nick Hill <nickhill123@gmail.com>
Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
…llm-project#45880)

Signed-off-by: zixi-qi <zixi@inferact.ai>
Co-authored-by: Nick Hill <nickhill123@gmail.com>
…-project#47874)

Signed-off-by: stefankoncarevic <stefan.koncarevic@amd.com>
Signed-off-by: Andreas Karatzas <akaratza@amd.com>
Co-authored-by: Andreas Karatzas <akaratza@amd.com>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
…GLE_TOKEN_DECODE (vllm-project#47144)

Signed-off-by: Dino Music <Dino.Music@amd.com>
Co-authored-by: Andreas Karatzas <akaratza@amd.com>
…n LoRA shrink (vllm-project#47944)

Signed-off-by: Chaojun Zhang <chaojun.zhang@intel.com>
Signed-off-by: jiang1.li <jiang1.li@intel.com>
…roject#42478)

Signed-off-by: JooHo Lee <BWAAEEEK@users.noreply.github.com>
Signed-off-by: JooHo Lee <jooho414@gmail.com>
Co-authored-by: JooHo Lee <BWAAEEEK@users.noreply.github.com>
Signed-off-by: Xianbao QIAN <xianbao.qian@gmail.com>
Signed-off-by: Harry Mellor <19981378+hmellor@users.noreply.github.com>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
Co-authored-by: Harry Mellor <19981378+hmellor@users.noreply.github.com>
Signed-off-by: wenjun.liu <wenjun.liu@intel.com>
Signed-off-by: jun,du <jun.du@intel.com>
Co-authored-by: jun,du <jun.du@intel.com>
Co-authored-by: Kunshang Ji <kunshang.ji@intel.com>
Signed-off-by: Benjamin Chislett <bchislett@nvidia.com>
Signed-off-by: Benjamin Chislett <chislett.ben@gmail.com>
Co-authored-by: Wentao Ye <44945378+yewentao256@users.noreply.github.com>
…ect#46415)

Signed-off-by: muhammadfawaz1 <135441198+muhammadfawaz1@users.noreply.github.com>
Co-authored-by: Mahad Durrani <114791389+mahadrehmann@users.noreply.github.com>
…code (vllm-project#46694)

Signed-off-by: chaunceyjiang <chaunceyjiang@gmail.com>
Co-authored-by: Nick Hill <nickhill123@gmail.com>
…llm-project#48096)

Signed-off-by: Xianbao QIAN <xianbao.qian@gmail.com>
Signed-off-by: Harry Mellor <19981378+hmellor@users.noreply.github.com>
Signed-off-by: Cyrus Leung <tlleungac@connect.ust.hk>
Co-authored-by: Harry Mellor <19981378+hmellor@users.noreply.github.com>
Co-authored-by: Cyrus Leung <tlleungac@connect.ust.hk>
…ject#48100)

Signed-off-by: Harry Mellor <19981378+hmellor@users.noreply.github.com>
… aren't misclassified as prefills (vllm-project#47381)

Signed-off-by: Woosuk Kwon <woosuk@inferact.ai>
Signed-off-by: Nick Hill <nickhill123@gmail.com>
Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
Co-authored-by: Nick Hill <nickhill123@gmail.com>
…lm-project#48101)

Signed-off-by: khluu <khluu000@gmail.com>
Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
…n-CUDA builds (vllm-project#47296)

Signed-off-by: Tsvika Shapira <tsvika@moonmath.ai>
Co-authored-by: Claude <noreply@anthropic.com>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[RFC] Add Top-nσ logit truncation example via custom logits processor