Fix cumulative bin accumulation and bin count bookkeeping in metrics.general.histogram - #2006
Open
Nicholas022400701 wants to merge 2 commits into
Open
Nicholas022400701 wants to merge 2 commits into
Nicholas022400701 wants to merge 2 commits into
Conversation
…tion _high_memory_bin_reduction_cdf assigned the size of the current input to the last cumulative bin instead of adding it, unlike its low memory twin. When cdf() is given more than one input, or when _count_bins accumulates into existing counts, the last bin then holds only the last input and the normalization divides by the wrong total. Histogram.update also stored the number of bin edges as number_of_bins, one more than the number of bins. Signed-off-by: 区梓灏 <116372750+Nicholas022400701@users.noreply.github.com>
The tests check that cdf() normalizes by the size of all inputs, that the low and high memory reductions agree when they add to existing counts, and that Histogram.update leaves number_of_bins equal to the number of bins. Signed-off-by: 区梓灏 <116372750+Nicholas022400701@users.noreply.github.com>
Nicholas022400701
requested review from
NickGeneva and
dallasfoster
as code owners
September 18, 2026 14:11
Contributor
CODEOWNERS review mapCurrent for commit ⏳ @dallasfoster — 1 file(s)
⏳ @NickGeneva — 1 file(s)
No CODEOWNER
Comment |
Contributor
|
The PR appears safe to merge, with both fixes consistent with existing histogram contracts and covered by focused regression tests. SummaryThis PR fixes two histogram bookkeeping errors and adds focused regression coverage.
Reviews (1) · Last reviewed commit: "Add regression tests for cumulative hist..." |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PhysicsNeMo Pull Request
Description
closes #2005
_high_memory_bin_reduction_cdfinphysicsnemo/metrics/general/histogram.pywrote the size of the current input into the last cumulative bin with=while every other bin, and the whole low memory twin_low_memory_bin_reduction_cdf, accumulate with+=._count_binsalways tries the high memory routine first, so whenever counts are accumulated across calls the last bin held only the last input. The publiccdffunction hits this as soon as it gets more than one input, because_compute_counts_cdffeeds the running counts back into_count_binsfor each input. Aftercdf(x, y, bins=10)the last bin heldlen(y)while the other bins countedxandytogether, and the normalizationcounts / counts[-1]produced values above one. With 10 samples inxand 5 inythe largest value was 2.8.Histogram.updatestoredself.bin_edges.shape[0]asnumber_of_bins, which is the number of edges, while__init__usesbins.shape[0] - 1. After an update that extends the bin range,number_of_binswas one larger than the number of rows incounts, and a later__call__built the histogram with one bin more than before.Changes:
_high_memory_bin_reduction_cdfaddsinputs.shape[0]to the last bin instead of assigning it, matching the low memory routine and the pdf routines.Histogram.updatestoresself.bin_edges.shape[0] - 1.test/metrics/test_histogram_cdf.pychecks thatcdf(x, y)stays at or below one and matches a reference built from the concatenated samples, that the low and high memory routines agree when they add to a non zero existingcountsfor both the pdf and the cdf case, and thatHistogram.updateleavesnumber_of_bins,bin_edgesandcountsconsistent.Nothing else in the file changed. The existing
test_histogramintest/metrics/test_metrics_general.pycompares the low and high memory routines, but it passes the samecountstensor object to both calls and each routine mutates and returns that tensor, so it compares one tensor with itself. I left that test alone here and can tighten it in this PR if you want.Verification on a CPU GitHub Actions runner with torch 2.14.0+cpu and Python 3.12:
test_histogramandtest_climate_efipass,ruff format --check,ruff checkandinterrogatepass on the touched files, and the reproducer printsCDF_MAX 1.0andNBINS 91 91 91: https://github.com/Nicholas022400701/oss-ci/actions/runs/35354064109main, 3 of the 4 cases fail and the reproducer printsCDF_MAX 2.799999952316284andNBINS 92 91 91: https://github.com/Nicholas022400701/oss-ci/actions/runs/35354063983I did not touch CHANGELOG.md in this PR. I can add a line under Fixed in the 2.3.0 section if you want it here.
AI disclosure: I used an AI coding agent to help write this patch, the tests and this description. I have read the change and the tests myself and I will answer review comments personally.
Checklist
cdfnow does what its docstring already says for several inputs.Dependencies
None.
Review Process
All PRs are reviewed by the PhysicsNeMo team before merging.
Depending on which files are changed, GitHub may automatically assign a maintainer for review.
We are also testing AI-based code review tools (e.g., Greptile), which may add automated comments with a confidence score.
This score reflects the AI’s assessment of merge readiness and is not a qualitative judgment of your work, nor is
it an indication that the PR will be accepted / rejected.
AI-generated feedback should be reviewed critically for usefulness.
You are not required to respond to every AI comment, but they are intended to help both authors and reviewers.
Please react to Greptile comments with 👍 or 👎 to provide feedback on their accuracy.