Skip to content

Add Top-H edge-case tests using the shared reference - #1

Merged
Andreas-Hum merged 1 commit into
Andreas-Hum:fix-toph-criterionfrom
0718lol:test/50228-author-edge-cases
Nov 8, 2026
Merged

Andreas-Hum merged 1 commit into
Andreas-Hum:fix-toph-criterionfrom
0718lol:test/50228-author-edge-cases

Conversation

@0718lol

@0718lol 0718lol commented Nov 4, 2026 •

Copy link
Copy Markdown

🤗 Transformers CI

Adds edge-case coverage on top of your fix-toph-criterion branch, following your invitation in huggingface#50228 (comment).

The existing parameterized reference test now covers the uniform N=16 / alpha=0.5 boundary, a 200-token vocabulary hitting the candidate cap, alpha=1, masked logits, and a sharply peaked distribution. Separate tests reuse your reference helper for batch independence, shift/permutation invariance, float16 and bfloat16, and check that input logits and retained output values are preserved. Only the test file changes; your implementation and reference helper are unchanged.

Validation on macOS arm64, Python 3.12.15 and PyTorch 2.15.0 (CPU):

  • python -m pytest tests/generation/test_logits_process.py -q: 59 passed, 4 existing softmax-dimension warnings.
  • make style: all five checks passed.
  • git diff --check: passed.

AI disclosure: OpenAI Codex assisted with implementation and local validation. This PR targets your fork branch as explicitly invited; it is not a separate upstream fix PR. No CUDA/MPS or full-project validation is claimed.

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]>
@github-actions

github-actions Bot commented Nov 4, 2026

Copy link
Copy Markdown

Thank you for your contribution 🤗!

CI Security Gate — waiting for maintainer approval

CI is approved automatically only for authors who already have a contribution merged in this repository. A maintainer will review this PR and approve CI to run on it.

@Andreas-Hum Andreas-Hum left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, this is a great set of cases! I ran it on CPU (Linux, Python 3.13, torch 2.15): all 59 tests pass with the fix. Against the unfixed warper, 6 of the 11 Top-H tests fail, including your batch/invariance test and both low-precision tests, so they really pin down the bug. ruff check and ruff format --check are clean.

One change before I merge: two of the cases sit exactly on the threshold, so whether they pass depends on float32 rounding. On CPU, the margin at the last kept token is 0 for uniform_boundary and 1.2e-7 for full_support. A GPU kernel that sums in a different order could flip them by one ulp. That was my suggestion in the issue, so my bad. Suggestions below.

[
# uniform over 50 tokens: keeps 50**0.4 -> 4 tokens, not 0.4 * 50 = 20
("uniform_50", lambda: torch.zeros(50), 0.4),
("uniform_boundary", lambda: torch.zeros(16), 0.5),

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With N = 16 and alpha = 0.5, H(q_4) = log 4 = 0.5 * log 16 exactly, so this is a tie. Testing both sides of it keeps the boundary check and leaves a margin of about 0.03 nats each way (160.49 = 3.89 -> 3 tokens, 160.51 = 4.11 -> 4 tokens):

Suggested change
("uniform_boundary", lambda: torch.zeros(16), 0.5),
# uniform over 16 tokens, just below / above 16**0.5 = 4: keeps 3 and 4 tokens
("uniform_16_below", lambda: torch.zeros(16), 0.49),
("uniform_16_above", lambda: torch.zeros(16), 0.51),

("uniform_50", lambda: torch.zeros(50), 0.4),
("uniform_boundary", lambda: torch.zeros(16), 0.5),
("candidate_cap", lambda: torch.zeros(200), 1.0),
("full_support", lambda: torch.tensor([0.0, -0.5, -1.0, -2.0]), 1.0),

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At alpha = 1 the last token is always a tie, because H(q_N) = H(p) by definition. So this case only passes when the two entropies round the same way. candidate_cap already covers alpha = 1 with a large margin (0.69 nats), so I'd drop this one:

Suggested change
("full_support", lambda: torch.tensor([0.0, -0.5, -1.0, -2.0]), 1.0),

("candidate_cap", lambda: torch.zeros(200), 1.0),
("full_support", lambda: torch.tensor([0.0, -0.5, -1.0, -2.0]), 1.0),
("masked_logits", lambda: torch.tensor([2.0, 1.0, 0.0, -float("inf")]), 0.9),
("peaked_logits", lambda: -100.0 * torch.arange(16, dtype=torch.float), 0.4),

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One more, sorry for not catching it in the first pass. With -100 * arange(16) every token after the first has probability about e^-100 ≈ 4e-44, which is subnormal in float32 (and can be flushed to zero on GPU). The keep/drop decision then happens on numbers around 1e-42, and the case passes regardless, since the warper always keeps the first token (selection_mask[:, 0] = True). It passes on the unfixed code too.

-3 * arange(16) is still sharply peaked (keeps 1 token) but stays in the normal float range, with margins of 0.08 below and 0.11 above the threshold:

Suggested change
("peaked_logits", lambda: -100.0 * torch.arange(16, dtype=torch.float), 0.4),
("peaked_logits", lambda: -3.0 * torch.arange(16, dtype=torch.float), 0.4),

@Andreas-Hum
Andreas-Hum merged commit 8da5789 into Andreas-Hum:fix-toph-criterion Nov 8, 2026
12 checks passed
@Andreas-Hum

Copy link
Copy Markdown
Owner

Thanks again for these cases! Since this had been open a few days, I went ahead and applied the three suggestions myself and merged it together with #2. Your commit is kept as-is, so you'll be credited when it goes upstream.

@0718lol

0718lol commented Nov 8, 2026 via email

Copy link
Copy Markdown
Author

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.

2 participants