Repository navigation
Fix percentiles landing one rank high from cumulative-sum rounding - #1194
Conversation
There was a problem hiding this comment.
Thanks for this, and for the write-up in #1193. I pulled the branch and checked it.
On Python 3.12 the statistics tests pass (188 passed, 8 skipped, up from 182 on main). I also compared from_values against exact ceil(p * n) ranks (via Fraction) for every n from 1 to 399 plus 1000, 2000, 5000, 10000 and 100000, all eleven percentiles. Main is off by one rank in 286 of 4444 cases and this branch in none. With duplicated values (300 random samples, 5 to 3000 points, 3300 percentile checks) main is off in 32 and the branch in none, so the tolerance holds up when the weights aren't uniform.
One thing this doesn't cover: safePercentile in src/guidellm/benchmark/outputs/html_report/report.js (around line 2503) is a hand copy of the same lookup. It builds running += w / total and takes the first index where cdfProbs[idx] >= quantile, and its comment says it matches DistributionSummary.from_values. It's the same cumulative sum, so it has the same drift. I don't have node here, so I replayed the identical double arithmetic in Python: n=20 median gives 11 (exact 10), n=10 p90 gives 10 (exact 9), n=300 p10 gives 31 (30) and p99 gives 298 (297). It feeds the per-turn p95/p99 values for latency, TTFT and ITL in the HTML report, so those would stay one rank high after this merges.
Could you apply the same tolerance there, or would you rather I do it as a follow-up? Comparing integer cumulative counts against ceil(q * n) would remove the epsilon entirely, but that's a bigger change than this PR needs. A small JS check of the same n=20 case would also keep the two from drifting apart again.
Nothing else from me. The lint output on this branch matches main (the same 7 PLR0917 warnings).
|
Thanks for checking this so thoroughly, and for catching the JS copy. It's the same bug, so I fixed it here in 75b5c99 instead of leaving it for a follow-up.
I also ran your sweep on the JS function, against exact
I kept the epsilon rather than switching to integer cumulative counts, so the two implementations stay identical and the change stays small. |
|
Received a single approval. Will automatically queue for merge once two maintainers approve, all change requests are resolved, and DCO passes. |
|
Queued — the merge queue status continues in this comment ↓. |
75b5c99 to
a373c53
Compare
|
@jaredoconnell my rebase dismissed your review, sorry about that. #1163 renamed |
sjmonson
left a comment
There was a problem hiding this comment.
I don't think this is really a problem that needs solving, but also I don't really have any reasons against merging. Need a rebase though due to conflict.
|
Received a single approval. Will automatically queue for merge once two maintainers approve, all change requests are resolved, and DCO passes. |
Percentiles.from_pdf picked the first value whose cumulative probability reached each target, but the running sum of equal weights often lands just below an exact step, so the lookup moved to the next value. For the values 1..20 the median came out as 11, and for 300 equal-weight samples p10, p25, p75, p90, p95 and p99 were each one rank too high. Allow for the rounding with a tolerance of len(pdf) machine epsilons, which is far below the probability of any single value. Co-Authored-By: Claude Opus 5.5 <[email protected]> Generated-by: Claude Code claude-opus-5-5 Signed-off-by: David-Wu1119 <[email protected]>
safePercentile in report.js is a hand copy of Percentiles.from_pdf and builds the same running sum of 1/n, so it has the same drift: for the values 1..20 the median came out as 11, and for 300 values p10 and p99 were one rank high. It feeds the per-turn p95/p99 latency, TTFT and ITL columns of the HTML report. Subtract the same len(cdf) * epsilon tolerance before the lookup. Add tests/js/safe_percentile.test.mjs, which reads the function out of report.js and checks it against exact ceil(q * n) ranks, and run it from the npm test script. Reported by HarshShroff in review. Co-Authored-By: Claude Opus 5.5 <[email protected]> Generated-by: Claude Code claude-opus-5-5 Signed-off-by: David-Wu1119 <[email protected]>
a373c53 to
f98eff7
Compare
Suggested in review. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: David-Wu1119 <[email protected]>
|
@sjmonson took your suggestion in b8a255c: the |
|
Received a single approval. Will automatically queue for merge once two maintainers approve, all change requests are resolved, and DCO passes. |
Merge Queue Status
This pull request spent 6 minutes 55 seconds in the queue, including 6 minutes 6 seconds running CI. Required conditions to merge
|
Summary
Percentiles.from_pdffound each percentile withnp.searchsorted(np.cumsum(probabilities), p, side="left"). The running sum of equal weights often lands just below an exact step: ten weights of1/20add up to0.49999999999999994. So the lookup skipped the value that reachespand returned the next one. The shift is always upward, and which percentiles it hits depends on the sample count. For the values 1..20 the median was 11. For 300 samples, p10, p25, p75, p90, p95 and p99 were each one rank high (see #1193).The fix subtracts a tolerance of
len(cdf) * machine epsilonfrom each target before the search. That bound on the cumulative-sum error stays far below the probability of any single value (about 2e-10 even for a million samples, against a per-value probability of 1e-6), so it only absorbs the rounding.Details
src/guidellm/schemas/base/statistics.py: tolerance inPercentiles.from_pdf.tests/unit/schemas/test_statistics.py:TestPercentiles.test_from_pdf_equal_weights_exact_ranks(regressionmarker) checks all eleven percentiles against the exact inverse-CDF rank,ceil(p * n)computed withfractions.Fraction, for 10, 20, 100, 300, 2000 and 5000 equal-weight values.src/guidellm/benchmark/outputs/html_report/report.js: the same tolerance insafePercentile, the HTML report's hand copy of this lookup (it feeds the per-turn p95/p99 latency, TTFT and ITL columns). Found by HarshShroff in review.tests/js/safe_percentile.test.mjs(run bynpm test, so the tox JS env picks it up): checkssafePercentileagainst exact ranks.Test Plan
mainand passes with this change, with numpy 2.2.6 (the locked version) and numpy 2.5.pytest tests/unit/schemas/test_statistics.py: 188 passed, 8 skipped.pytest tests/unit: 3283 passed, with the same 32 failures before and after the change. They come from my environment: torchcodec, and theplotandperfextras aren't installed.--check-untyped-defsonstatistics.pyare clean.npm testintests/js(smoke test plus the new one) passes with a freshly generated fixture. Against exactceil(q * n)ranks for n = 1–399, 1000, 2000, 5000 and 10000 at all eleven percentiles, the old JS was off in 279 of 4433 cases and the new one in none.Related Issues
Use of AI
The commit carries
Generated-by: Claude Code claude-opus-5-5.🤖 Generated with Claude Code
git log
commit b1a67a7
Author: David-Wu1119 [email protected]
Date: Mon Sep 28 21:24:08 2026 -0400
commit f98eff7
Author: David-Wu1119 [email protected]
Date: Tue Sep 29 16:18:54 2026 -0400
commit b8a255c
Author: David-Wu1119 [email protected]
Date: Wed Oct 14 21:40:20 2026 -0400
Co-Authored-By: Claude Opus 5.5 [email protected]
Generated-by: Claude Code claude-opus-5-5
Signed-off-by: David-Wu1119 [email protected]