Skip to content

Entropy calibration mutates the stored NumPy histogram #2654

Closed

Description

@sylvesterkaczmarek

Problem

With HistogramCalibrator(torch_hist=False), computing an entropy threshold changes the collected histogram. _compute_amax_entropy uses calib_hist[:], a NumPy view, then replaces its first bin. Later percentile/MSE queries and subsequent collection therefore operate on modified counts.

Reproduced on main at 6a1068746dd0e746fe4bcb79152b86bdcf58cb41, using CPU tensors.

Reproduction

import numpy as np
import torch
from modelopt.torch.quantization.calib import HistogramCalibrator

calibrator = HistogramCalibrator(4, None, False, num_bins=32, torch_hist=False)
calibrator.collect(torch.cat((torch.zeros(100), torch.arange(1, 33).float())))
original = calibrator._calib_hist.copy()
calibrator.compute_amax("entropy", start_bin=16)
np.testing.assert_array_equal(calibrator._calib_hist, original)
# Fails: the first bin changes from 100 samples to 1.

The entropy calculation should use a private copy of the histogram. Its existing first-bin adjustment can remain local to that calculation. The PyTorch histogram backend already avoids modifying persistent counts because compute_amax converts its histogram to a fresh array.

This is separate from #2369, which handles zero-only initial batches in the PyTorch collector. The proposed fix changes only the entropy helper, with a regression covering persistent counts, repeated threshold queries, and continued collection.

Activity

  1. added a commit that references this issue on Oct 26, 2026
    cc016cc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions