pytorch/caffe2/quantization/server/dynamic_histogram.h
Summer Deng 82b6300fea Disable openmp in static and dynamic histograms (#30072)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/30072

Fix the test failure with mode/opt-lto by disabling openmp in both static and dynamic histograms. We will just use single thread in histogram processing as it's the common use case.

Test Plan:
```
buck run mode/opt caffe2/caffe2/fb/fbgemm/numerical_debugger/workflows:int8_static_quantization_exporter -- --model-dir /mnt/public/summerdeng/ads/ --model-name downsized_ins_97293388_0.predictor --run --iter 10  --dataset-path /mnt/public/summerdeng/ads/ctr_instagram_story_int8/dataset/train/dataset_115764229_10 --hive-path="hive://ad_delivery/ig_ad_prefiltered_training_data_orc_injected/ds=2019-09-09/pipeline=ctr_instagram_story_click_only_model_opt_out_df" --collect-histogram --activation-histogram-file=/mnt/public/summerdeng/ads/ctr_instagram_story_int8/activation_histograms/dummy_debug_OOM.txt
```
```
buck test mode/opt-lto caffe2/caffe2/quantization/server:dynamic_histogram_test -- --run-disabled
```

Reviewed By: hx89

Differential Revision: D18554614

fbshipit-source-id: cfff51174154e753b7123b4ec502b88ffc508917
2019-11-19 00:32:46 -08:00

73 lines
1.9 KiB
C++

#pragma once
#include <memory>
#include <vector>
namespace dnnlowp {
/**
* bin_width = (max - min)/nbins
* ith bin (zero-based indexing) contains [i*bin_width, (i+1)*bin_width)
* with an exception that (nbins - 1)th bin contains
* [(nbins-1)*bin_width, nbins*bin_width]
*
*/
class Histogram {
public:
Histogram(int nbins, float min, float max)
: min_(min), max_(max), histogram_(nbins) {}
Histogram(float min, float max, const std::vector<uint64_t>& bins)
: min_(min), max_(max), histogram_(bins) {}
void Add(float f, uint64_t cnt = 1);
/**
* This version collects histogram with single thread
*/
void Add(const float* f, int len);
float Min() const {
return min_;
}
float Max() const {
return max_;
}
const std::vector<uint64_t>* GetHistogram() const {
return &histogram_;
}
private:
float min_, max_;
std::vector<uint64_t> histogram_;
};
/// An equi-width histogram where the spread of bins change over time when
/// we see new min or max values.
class DynamicHistogram {
public:
DynamicHistogram(int nbins);
void Add(float f);
void Add(const float* f, int len);
/// Indicate we're not dynamically adjusting histogram bins any more and
/// return the current static histogram.
const Histogram* Finalize();
private:
/// Dynamic histogram is implemented by the series of static histograms
/// and expands from the old histogram to new histogram when
/// we see a new extremum.
/// An invariant: the beginning of the first bin of histograms_[i] exactly
/// matches with the beginning of a bin in histograms_[i+1]. The end of the
/// last bin of histograms_[i] exactly matches with the end of a bin in
/// histograms_[i+1].
std::unique_ptr<Histogram> histogram_;
int nbins_;
float min_, max_;
std::unique_ptr<Histogram> final_histogram_;
}; // class DynamicHistogram
} // namespace dnnlowp