Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Fix wrong percentile values returned during calibration #10847

Merged
merged 6 commits into from
Mar 11, 2022
Merged
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions onnxruntime/python/tools/quantization/calibrate.py
Original file line number Diff line number Diff line change
Expand Up @@ -599,19 +599,19 @@ def compute_percentile(self):

print("Number of tensors : {}".format(len(histogram_dict)))
print("Number of histogram bins : {}".format(self.num_bins))
print("Percentile : {}".format(percentile))
print("Percentile : ({},{})".format(100.0 - percentile, percentile))

for tensor, histogram in histogram_dict.items():
hist = histogram[0]
hist_edges = histogram[1]
total = hist.sum()
cdf = np.cumsum(hist/total)
if self.symmetric:
idx_right = np.searchsorted(cdf, percentile/100)
idx_right = np.searchsorted(cdf, np.percentile(cdf, percentile))
thresholds_dict[tensor] = (-float(hist_edges[idx_right]), float(hist_edges[idx_right]))
else:
idx_right = np.searchsorted(cdf, percentile/200)
idx_left = np.searchsorted(cdf, (1.0 - percentile/200))
idx_right = np.searchsorted(cdf, np.percentile(cdf, percentile))
Copy link
Member

Choose a reason for hiding this comment

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

ah, this is because of a math error. It was intent to be:

  percent_to_cut_one_side = (100.0 - percentile)/200.0
  idx_right = np.searchsorted(cdf, 1.0 - percent_to_cut_one_side)
  idx_left = np.searchsorted(cdf, percent_to_cut_one_side)

Copy link
Member

Choose a reason for hiding this comment

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

@mfuntowicz, thanks for the fix. Could you please try if this work? It is simpler. @chilo-ms, we need to add unit test to cover this later.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, testing right now.

Copy link
Contributor Author

@mfuntowicz mfuntowicz Mar 11, 2022

Choose a reason for hiding this comment

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

Didn't look at a unit test because I'm not so familiar with the structure of the tests you have in ORT, but will definitively look at it for future PRs, sorry about that.

Copy link
Member

Choose a reason for hiding this comment

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

We will add it. Thanks for the fix.

idx_left = np.searchsorted(cdf, np.percentile(cdf, 100.0 - percentile))
thresholds_dict[tensor] = (float(hist_edges[idx_left]), float(hist_edges[idx_right]))

# Plot histogram for debug only
Expand Down