Repository navigation
test: add Top-H monotonicity, exponential decay, and validation checks - #2
Conversation
|
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. |
|
Thanks @Andreas-Hum! Following your layout, I expanded the test suite directly around the shared reference helper op_h_reference_num_kept:
All 18 Top-H tests pass in ~0.97s, the full est_logits_process.py suite passes (66 passed), and |
Andreas-Hum
left a comment
There was a problem hiding this comment.
Thanks for putting this together, there are some really useful checks in here! I ran it on CPU (Linux, Python 3.13, torch 2.15): all 66 tests pass with the fix, and 5 of the Top-H tests fail on the unfixed warper. ruff is clean.
I'd like to keep this PR focused, though, since everything here ends up in the upstream PR for a 3-line fix. A few points:
1. Exact ties on the threshold. Four cases put the last kept token exactly on H(q_k) = alpha * H(p), so whether they pass depends on float32 rounding, and they could flip on GPU:
uniform_16_boundary(alpha = 0.5):H(q_4) = log 4 = 0.5 * log 16, margin 0 on CPU. (That one was my suggestion in the issue, my bad.)test_top_h_candidate_capwith N = 50 and N = 100 at alpha = 1: at alpha = 1 the last token is always a tie, sinceH(q_N) = H(p). Margins are 2e-7 and 0.test_top_h_tied_logits, 25 equal logits at alpha = 0.5:25 ** 0.5 = 5exactly, and the 5th token is dropped by 4e-7. It only passes because the double-precision reference happens to round the same way.
Moving off the tie keeps the boundary check, e.g. alpha = 0.49 / 0.51 for N = 16 (keeps 3 and 4 tokens, about 0.03 nats of margin each way), and only checking the cap for N > 100.
2. Overlap with #1. #1 (opened earlier) already adds N = 16, the 200-token cap, masked -inf logits, peaked logits, batch independence, shift/permutation invariance and half precision, and the two PRs conflict in the case list. I'm planning to merge #1 first.
3. Weaker checks. test_top_h_numerical_stability uses an unseeded torch.randn and only checks for no NaNs and at least one kept token, and test_top_h_candidate_cap repeats the flat_200_cap case.
What I'd love to keep after rebasing onto the branch once #1 is in:
test_top_h_monotonicity: a larger alpha never keeps fewer tokens. Nothing else covers that.- The
exponential_decaycase: a second long-tailed shape, and it catches the bug. - The constructor validation and custom
filter_valuechecks.
Could you trim it down to those, with the ties moved off the threshold? Happy to review again after that.
2c7f057 to
7781c74
Compare
|
Thanks for the review and the clear guidance @Andreas-Hum! Completely agree with keeping this PR tightly focused and upstream-friendly. I have updated the branch with the requested trimming:
All 6 Top-H tests pass cleanly locally (pytest -k top_h), and |
Andreas-Hum
left a comment
There was a problem hiding this comment.
Thanks, this looks great! I reran it on CPU: all 54 tests pass with the fix, and exponential_decay fails on the unfixed warper alongside the two original cases, so it catches the bug. Monotonicity and validation pass on both, as expected for properties the old rule also had. I also checked the gap to the threshold for every alpha and distribution in the monotonicity grid, and the smallest is 1.5e-3, so none of them are near a rounding tie. ruff is clean.
Plan from here:
- #1 goes in first, since it was opened earlier. I've left a few small suggestions there, and I'm waiting for those.
- Once #1 is merged, this PR will have a small conflict, because both add cases to the same
parameterized.expandlist. You don't need to do anything: I'll resolve it when merging (keeping both sets of cases) and rerun the tests. - Then I'll open the upstream PR for huggingface#50228 from this branch, and both of you will show up as commit authors.
If #1 stalls for a long time, I'll merge this one first instead, so it doesn't sit waiting.
|
Merged, thanks @Spartan2588! #1 is merged as well, so both sets of tests are now on |
|
Thanks again @Andreas-Hum for the detailed review and guidance throughout this contribution! I really enjoyed working on the Top-H regression tests, especially understanding the entropy criterion and designing property-based checks for monotonicity and distribution behaviour. I'd love to continue contributing to Transformers. If there are any open issues, regression-testing gaps, or smaller implementation tasks where I could help, I'd be very interested in taking them up. Looking forward to contributing more! |
|
Thanks for the tests! They're now part of the upstream PR, huggingface#50471, and you're credited as co-author. I don't have anything else to hand out right now, but issues labelled "Good First Issue" are a good place to start. |
Description
This is an independent test-only contribution complementing the Top-H entropy criterion fix for Issue huggingface#50228 on branch ix-toph-criterion.
Context & Problem Solved by the Fix
Top-H sampling (arXiv:2509.02510) dynamically bounds the retained prefix distribution$q_k$ by retaining the largest sorted token prefix satisfying:$q_k = p_{1:k} / \sum_{i=1}^k p_i$ is the renormalized prefix distribution, and \mathcal{H}(p) is the Shannon entropy over the full next-token distribution.
\mathcal{H}(q_k) \le \alpha \cdot \mathcal{H}(p)
where
The original implementation had two critical flaws:
Coverage Added
Building directly on the loop-based independent reference helper op_h_reference_num_kept, this PR adds regression coverage covering:
Test Results
uff (zero violations).