Skip to content

Add TopNSigmaLogitsWarper and top_n_sigma generation config support#45172

Closed
ezylopx5 wants to merge 2 commits intohuggingface:mainfrom
ezylopx5:feature/top-n-sigma-logits-warper
Closed

Add TopNSigmaLogitsWarper and top_n_sigma generation config support#45172
ezylopx5 wants to merge 2 commits intohuggingface:mainfrom
ezylopx5:feature/top-n-sigma-logits-warper

Conversation

@ezylopx5
Copy link
Copy Markdown

@ezylopx5 ezylopx5 commented Apr 1, 2026

Problem

Transformers currently provides sampling filters such as top-k, top-p, min-p, and top-h, but does not include top-n-sigma sampling from "Top-nσ: Not All Logits Are You Need".

This makes it harder to use a temperature-invariant truncation rule based directly on pre-softmax logits.

Closes #39850.

Root Cause

There is no logits warper implementing a dynamic threshold of:

max(logits) - n_sigma * std(logits)

and no corresponding GenerationConfig parameter routed through GenerationMixin._get_logits_processor().

Fix

This PR adds TopNSigmaLogitsWarper and integrates it into generation config and sampling flow.

Code changes

  • Add TopNSigmaLogitsWarper to generation logits processors.
  • Add top_n_sigma field and validation in GenerationConfig.
  • Add top_n_sigma sampling integration in GenerationMixin.
  • Export TopNSigmaLogitsWarper from generation and top-level package init files.
  • Add tests for behavior, validation, edge cases, and config serialization.

Files changed

  • src/transformers/generation/logits_process.py
  • src/transformers/generation/configuration_utils.py
  • src/transformers/generation/utils.py
  • src/transformers/generation/__init__.py
  • src/transformers/__init__.py
  • tests/generation/test_logits_process.py
  • tests/generation/test_configuration_utils.py

Validation

Ran locally in editable install environment:

python -m pytest tests/generation/test_logits_process.py -k "top_n_sigma" -x -v
python -m pytest tests/generation/test_configuration_utils.py -k "top_n_sigma" -x -v
python -m pytest tests/generation/test_logits_process.py -x -v
python -m pytest tests/generation/test_configuration_utils.py -x -v

Results:

  • test_logits_process.py: 50 passed
  • test_configuration_utils.py: 34 passed, 5 skipped

Also ran style checks:

make style

Risks

  • Behavior is additive and opt-in (top_n_sigma defaults to None).
  • Existing generation paths remain unchanged unless top_n_sigma is explicitly set.
  • Includes validation for non-negative values to fail early on invalid input.

Out of Scope

  • Benchmarking quality/performance on large models.
  • Interactions benchmarking vs top-p/min-p/top-h beyond functional correctness.

ezylopx5 added 2 commits April 2, 2026 00:47
Signed-off-by: HarshRathva <harshrathvaai@gmail.com>
Signed-off-by: HarshRathva <harshrathvaai@gmail.com>
@Rocketknight1
Copy link
Copy Markdown
Member

Please don't ask your code agent to find old issues and "fix" them!

@ezylopx5 ezylopx5 deleted the feature/top-n-sigma-logits-warper branch April 2, 2026 13:01
@ezylopx5 ezylopx5 restored the feature/top-n-sigma-logits-warper branch April 4, 2026 06:04
@ezylopx5
Copy link
Copy Markdown
Author

ezylopx5 commented Apr 4, 2026

@Rocketknight1 thanks for the feedback, and sorry for the noise.You are right that I handled the process poorly. I moved too fast without prior discussion on the issue, and that made this look agent-first.
For transparency: I did use an agent for implementation support, but I personally reviewed the diff, ran the tests locally, fixed the failing repository-consistency check, and re-ran the relevant test files before opening the PR. So the technical validation was real on my side, but I understand that process and trust come first.
and i have closed the PR and deleted the branch. Going forward I will only propose changes after prior discussion on active issues and with smaller, clearly scoped contributions.
I appreciate the guidance and your time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support topNSigma sampling in generate

2 participants