Repository navigation
Add Top-H edge-case tests using the shared reference - #1
Andreas-Hum merged 1 commit into
Conversation
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]>
|
Thank you for your contribution 🤗! CI Security Gate — waiting for maintainer approvalCI 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
left a comment
There was a problem hiding this comment.
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), |
There was a problem hiding this comment.
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):
| ("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), |
There was a problem hiding this comment.
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:
| ("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), |
There was a problem hiding this comment.
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:
| ("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), |
|
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. |
|
Awesome, thank you for handling this! I’m glad these cases help
…---Original---
From: "Andreas Svanberg ***@***.***>
Date: Sun, Nov 8, 2026 12:22 PM
To: ***@***.***>;
Cc: ***@***.******@***.***>;
Subject: Re: [Andreas-Hum/transformers] Add Top-H edge-case tests using the shared reference (PR #1)
Andreas-Hum left a comment (Andreas-Hum/transformers#1)
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.
—
Reply to this email directly, view it on GitHub, or unsubscribe.
Triage notifications, keep track of coding agent tasks and review pull requests on the go with GitHub Mobile for iOS and Android. Download it today!
You are receiving this because you authored the thread.Message ID: ***@***.***>
|
Adds edge-case coverage on top of your
fix-toph-criterionbranch, 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.