Skip to content

Fix TopHLogitsWarper: renormalized prefix entropy and full-distribution budget - #50471

Open
Andreas-Hum wants to merge 8 commits into
huggingface:mainfrom
Andreas-Hum:fix-toph-criterion
Open

Andreas-Hum wants to merge 8 commits into
huggingface:mainfrom
Andreas-Hum:fix-toph-criterion

Conversation

@Andreas-Hum

@Andreas-Hum Andreas-Hum commented Nov 8, 2026 •

Copy link
Copy Markdown
Contributor

🤗 Transformers CI

What does this PR do?

Fixes #50228. TopHLogitsWarper didn't implement the rule from the Top-H paper (arXiv:2509.02510) or from its own docstring. The rule is to keep the largest prefix of the sorted tokens whose renormalized entropy is at most top_h times the entropy of the full distribution. There were two problems:

  1. Budget: the budget tau was computed from the top-100 candidates only, not from the full distribution. On long-tailed vocabularies this made it too small.
  2. Prefix entropy: the prefix was compared using the unnormalized partial sum -sum p_i log p_i, not the entropy of the renormalized prefix q = p_{1..k} / P_k. Since H(q_k) = (1/P_k) * sum_{i<=k} (-p_i log p_i) + log P_k, this is a two-line change:
cumulative_mass = torch.cumsum(probs, dim=-1)
cumulative_entropy = torch.cumsum(entropy_terms, dim=-1) / cumulative_mass + torch.log(cumulative_mass)

The docstring in GenerationConfig also said "smallest prefix", but the rule keeps the largest one, so that line is corrected as well.

Tests. tests/generation/test_logits_process.py gets a module-level reference, top_h_reference_num_kept, which is a plain loop over Algorithm 1 of the paper. The warper is checked against it on:

  • uniform, Zipf and exponentially decaying distributions (cases sit away from exact entropy ties, so they don't depend on float rounding);
  • the 100-token candidate cap and masked -inf logits;
  • batches, permutation and shift invariance;
  • fp16 and bf16;
  • monotonicity in top_h, input validation and filter_value.

Thanks to @0718lol and @Spartan2588, who wrote many of these cases (their commits are kept in this branch).

python -m pytest tests/generation/test_logits_process.py
62 passed

Against the current main, 8 of the 14 Top-H tests fail.

Before submitting

Who can review?

@Cyrilvallez @Rocketknight1

Andreas-Hum and others added 8 commits November 3, 2026 18:42
Extend the shared reference cases on Andreas-Hum/fix-toph-criterion and cover batch independence, shift/permutation invariance and half precision.

Co-authored-by: OpenAI Codex <[email protected]>
test: add Top-H monotonicity, exponential decay, and validation checks
# Conflicts:
#	tests/generation/test_logits_process.py

This branch has not been deployed

No deployments
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.

TopHLogitsWarper does not implement the documented Top-H rule (it bounds the unnormalized partial entropy, and takes H(p) from the top 100 tokens only)

3 participants