Skip to content

Fix percentiles landing one rank high from cumulative-sum rounding - #1194

Merged
mergify[bot] merged 3 commits into
vllm-project:mainfrom
David-Wu1119:fix/percentile-cumsum-rounding
Oct 16, 2026
Merged

mergify[bot] merged 3 commits into
vllm-project:mainfrom
David-Wu1119:fix/percentile-cumsum-rounding

Conversation

@David-Wu1119

@David-Wu1119 David-Wu1119 commented Sep 29, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Percentiles.from_pdf found each percentile with np.searchsorted(np.cumsum(probabilities), p, side="left"). The running sum of equal weights often lands just below an exact step: ten weights of 1/20 add up to 0.49999999999999994. So the lookup skipped the value that reaches p and 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 epsilon from 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 in Percentiles.from_pdf.
  • tests/unit/schemas/test_statistics.py: TestPercentiles.test_from_pdf_equal_weights_exact_ranks (regression marker) checks all eleven percentiles against the exact inverse-CDF rank, ceil(p * n) computed with fractions.Fraction, for 10, 20, 100, 300, 2000 and 5000 equal-weight values.
  • src/guidellm/benchmark/outputs/html_report/report.js: the same tolerance in safePercentile, 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 by npm test, so the tox JS env picks it up): checks safePercentile against exact ranks.

Test Plan

  • The new test fails for all six sample counts on main and 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 the plot and perf extras aren't installed.
  • ruff 0.15.20 (check and format) and mypy 2.1.0 --check-untyped-defs on statistics.py are clean.
  • JS: the new test fails on the previous commit (11 vs 10) and passes now; npm test in tests/js (smoke test plus the new one) passes with a freshly generated fixture. Against exact ceil(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


  • "I certify that all code in this PR is my own, except as noted below."

Use of AI

  • Includes code generated or substantially modified by an AI agent
  • Includes tests generated or substantially modified by an AI agent

NOTE: the Generated-by or Assisted-by trailers should be used in git commit messages when code or tests were generated or substantially modified by an AI agent, as described in the project's DEVELOPING.md file.

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

Keep percentiles on their exact rank despite cumsum rounding

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]>

commit f98eff7
Author: David-Wu1119 [email protected]
Date: Tue Sep 29 16:18:54 2026 -0400

Apply the same rounding tolerance to the HTML report's percentiles

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]>

commit b8a255c
Author: David-Wu1119 [email protected]
Date: Wed Oct 14 21:40:20 2026 -0400

Run every .mjs file in tests/js instead of listing them

Suggested in review.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: David-Wu1119 <[email protected]>

Co-Authored-By: Claude Opus 5.5 [email protected]
Generated-by: Claude Code claude-opus-5-5
Signed-off-by: David-Wu1119 [email protected]

Copilot AI balanced review requested due to automatic review settings September 29, 2026 03:16

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@HarshShroff HarshShroff left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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).

@David-Wu1119

Copy link
Copy Markdown
Contributor Author

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.

  • report.js: safePercentile now subtracts cdfProbs.length * Number.EPSILON before the lookup, the same bound the Python side uses.
  • tests/js/safe_percentile.test.mjs: report.js is a single closure with nothing to import, so the test reads finiteNumbers and safePercentile out of the file. It checks the n=20 median, n=10 p90, n=300 p10/p95/p99 and a small duplicate-value case against exact ranks. On the previous commit it fails (11 vs 10); now it passes. I added it to the npm test script so the existing tox JS env runs it next to the smoke test. Both pass locally with a freshly generated fixture.

I also ran your sweep on the JS function, against exact ceil(q * n) ranks at all eleven percentiles:

  • n = 1–399, 1000, 2000, 5000 and 10000: the previous version was off in 279 of 4433 cases, this one in none.
  • 300 random duplicate-value samples: off in 16 of 3300 before, none now.

I kept the epsilon rather than switching to integer cumulative counts, so the two implementations stay identical and the change stays small.

jaredoconnell
jaredoconnell previously approved these changes Sep 29, 2026
@mergify

mergify Bot commented Sep 29, 2026

Copy link
Copy Markdown
Contributor

Received a single approval. Will automatically queue for merge once two maintainers approve, all change requests are resolved, and DCO passes.

@mergify

mergify Bot commented Sep 29, 2026 •

Copy link
Copy Markdown
Contributor

Queued — the merge queue status continues in this comment ↓.

@David-Wu1119

Copy link
Copy Markdown
Contributor Author

@jaredoconnell my rebase dismissed your review, sorry about that. #1163 renamed percentile_probs to PERCENTILE_PROBABILITIES and conflicted with this branch. git range-diff shows that rename in the conflict resolution is the only change since the commit you reviewed (75b5c99 → a373c53); the JS commit is identical. The statistics tests (204 passed) and both JS tests pass on the rebased branch.

sjmonson
sjmonson previously approved these changes Oct 9, 2026

@sjmonson sjmonson left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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.

@mergify

mergify Bot commented Oct 9, 2026

Copy link
Copy Markdown
Contributor

Received a single approval. Will automatically queue for merge once two maintainers approve, all change requests are resolved, and DCO passes.

Comment thread tests/js/package.json Resolved
David-Wu1119 and others added 2 commits October 11, 2026 12:15
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]>
@David-Wu1119

Copy link
Copy Markdown
Contributor Author

@sjmonson thanks, rebased onto main (f98eff7). The only conflict was tests/js/package.json: #1195 added language_switcher.test.mjs to the test script, so it now lists both that and safe_percentile.test.mjs. tests/unit/schemas/test_statistics.py and those two JS tests pass locally.

Suggested in review.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: David-Wu1119 <[email protected]>
@David-Wu1119

Copy link
Copy Markdown
Contributor Author

@sjmonson took your suggestion in b8a255c: the test script is now node --test *.mjs. npm runs it from tests/js (also with --prefix tests/js, as tox does), so the shell glob picks up all three test files. Run the way the tox env does it (fixture generated, then npm test --prefix tests/js): 5 tests, 5 passed. The rebase dismissed your earlier approval; could you take another look when you have a moment?

@mergify

mergify Bot commented Oct 15, 2026

Copy link
Copy Markdown
Contributor

Received a single approval. Will automatically queue for merge once two maintainers approve, all change requests are resolved, and DCO passes.

@mergify

mergify Bot commented Oct 16, 2026 •

Copy link
Copy Markdown
Contributor

Merge Queue Status

  • ✅ Entered queue — 2026-10-16 12:27 UTC · Rule: default · triggered by merge protections
  • ✅ Checks passed · on draft merge queue: checking #1194 on main (88ba018) #1323
  • ✅ Merged — 2026-10-16 12:34 UTC · at 89c7b7f718ba9f8586be6b0af9fc8da2ea613972 · squash

This pull request spent 6 minutes 55 seconds in the queue, including 6 minutes 6 seconds running CI.

Required conditions to merge
  • github-review-approved [🛡 GitHub repository ruleset rule Merge Requirements]
  • any of [🛡 GitHub repository ruleset rule Merge Requirements]:
    • check-success = @github-actions/quality (3.10) / quality-checks
    • check-neutral = @github-actions/quality (3.10) / quality-checks
    • check-skipped = @github-actions/quality (3.10) / quality-checks
  • any of [🛡 GitHub repository ruleset rule Merge Requirements]:
    • check-success = @github-actions/tests (3.10) / e2e-tests
    • check-neutral = @github-actions/tests (3.10) / e2e-tests
    • check-skipped = @github-actions/tests (3.10) / e2e-tests
  • any of [🛡 GitHub repository ruleset rule Merge Requirements]:
    • check-success = @github-actions/tests (3.10) / integration-tests
    • check-neutral = @github-actions/tests (3.10) / integration-tests
    • check-skipped = @github-actions/tests (3.10) / integration-tests
  • any of [🛡 GitHub repository ruleset rule Merge Requirements]:
    • check-success = @github-actions/tests (3.10) / unit-tests
    • check-neutral = @github-actions/tests (3.10) / unit-tests
    • check-skipped = @github-actions/tests (3.10) / unit-tests
  • any of [🛡 GitHub repository ruleset rule Merge Requirements]:
    • check-success = @github-actions/update-description
    • check-neutral = @github-actions/update-description
    • check-skipped = @github-actions/update-description

@mergify mergify Bot added the queued label Oct 16, 2026
@mergify mergify Bot mentioned this pull request Oct 16, 2026
17 of 27 tasks
@mergify
mergify Bot merged commit 89c7b7f into vllm-project:main Oct 16, 2026
9 checks passed
@mergify mergify Bot removed the queued label Oct 16, 2026
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.

Percentiles are often one sample too high: cumulative-sum rounding in Percentiles.from_pdf

6 participants