summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJongsoo Park <jongsoo@fb.com>2019-03-31 21:25:17 -0700
committerFacebook Github Bot <facebook-github-bot@users.noreply.github.com>2019-03-31 21:32:54 -0700
commit505d50ea907973ce471d3c2f0b7406b08f570ee5 (patch)
treef42f429f37a6a1668e0fcc700891c7e29ad9000d
parent6841537933a8099e8ddcdfdd5f4b2f1101989990 (diff)
downloadpytorch-505d50ea907973ce471d3c2f0b7406b08f570ee5.tar.gz
pytorch-505d50ea907973ce471d3c2f0b7406b08f570ee5.tar.bz2
pytorch-505d50ea907973ce471d3c2f0b7406b08f570ee5.zip
handle a rare case of histogram min is inf/nan (#18239)
Summary: Pull Request resolved: https://github.com/pytorch/pytorch/pull/18239 When min is inf or nan, we get UBSAN errors Reviewed By: csummersea Differential Revision: D14537668 fbshipit-source-id: e70ffb5ecd2b10793356070c69fdabf8f25b203e
-rw-r--r--caffe2/quantization/server/activation_distribution_observer.cc13
-rw-r--r--caffe2/quantization/server/activation_distribution_observer.h7
2 files changed, 14 insertions, 6 deletions
diff --git a/caffe2/quantization/server/activation_distribution_observer.cc b/caffe2/quantization/server/activation_distribution_observer.cc
index 900f73078c..37f7180b77 100644
--- a/caffe2/quantization/server/activation_distribution_observer.cc
+++ b/caffe2/quantization/server/activation_distribution_observer.cc
@@ -342,6 +342,10 @@ void HistogramNetObserver::DumpAndReset_(
<< " has an empty range: min " << hist->Min()
<< " and max " << hist->Max();
}
+ if (hist->GetHistogram()->empty()) {
+ LOG(WARNING) << "Histogram of "
+ << info->min_max_info.tensor_infos[i].name << " is empty";
+ }
ostringstream ost;
ost << op_index << " " << info->min_max_info.type << " " << i << " "
@@ -352,15 +356,11 @@ void HistogramNetObserver::DumpAndReset_(
ost << " " << c;
}
- f << ost.str() << endl;
if (print_total_min_max) {
LOG(INFO) << this << " " << ost.str();
}
- if (hist->GetHistogram()->empty()) {
- LOG(WARNING) << "Histogram of "
- << info->min_max_info.tensor_infos[i].name << " is empty";
- }
+ f << ost.str() << endl;
if (!print_total_min_max) {
info->histograms[i] = DynamicHistogram(hist->GetHistogram()->size());
@@ -575,7 +575,8 @@ RegisterQuantizationParamsWithHistogramNetObserver::
qparams = qfactory->ChooseQuantizationParams(hist, is_weight);
} else {
qparams.scale = 0.1f;
- qparams.zero_point = -min / qparams.scale;
+ qparams.zero_point =
+ (isinf(min) || isnan(min)) ? 0 : (-min / qparams.scale);
qparams.precision = 8;
}
diff --git a/caffe2/quantization/server/activation_distribution_observer.h b/caffe2/quantization/server/activation_distribution_observer.h
index 6162e62d08..72ecab330e 100644
--- a/caffe2/quantization/server/activation_distribution_observer.h
+++ b/caffe2/quantization/server/activation_distribution_observer.h
@@ -100,6 +100,13 @@ class HistogramObserver final : public ObserverBase<OperatorBase> {
class HistogramNetObserver final : public NetObserver {
public:
+ /**
+ * @params mul_nets true if we expect multiple nets with the same name so
+ * we include extra information in the file name to
+ * distinghuish them
+ * @params dump_freq if not -1 we dump histogram every dump_freq invocation
+ * of the net
+ */
explicit HistogramNetObserver(
NetBase* subject,
const std::string& out_file_name,