1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
|
#include "dynamic_histogram.h"
#include "dnnlowp_op.h"
#include <cassert>
#include <cmath>
#include <limits>
namespace dnnlowp {
using namespace std;
void Histogram::Add(float f, uint64_t cnt) {
int nbins = histogram_.size();
// NOLINTNEXTLINE(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions)
float bin_width = (max_ - min_) / nbins;
int bin = bin_width == 0
? 0
: std::min(static_cast<int>((f - min_) / bin_width), nbins - 1);
bin = std::max(0, bin);
assert(bin >= 0);
histogram_[bin] += cnt;
}
void Histogram::Add(const float* f, int len) {
int nbins = histogram_.size();
// NOLINTNEXTLINE(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions)
float bin_width = (max_ - min_) / nbins;
if (bin_width > 0.0) {
uint64_t* my_histogram = nullptr;
my_histogram = histogram_.data();
for (auto i = 0; i < len; ++i) {
int bin =
std::min(static_cast<int>((f[i] - min_) / bin_width), nbins - 1);
bin = std::max(0, bin);
++my_histogram[bin];
}
} else {
histogram_[0] += len;
}
}
void RemapHistograms(Histogram& src_hist, Histogram& dst_hist) {
auto src_bins = *(src_hist.GetHistogram());
float src_bin_width = (src_hist.Max() - src_hist.Min()) / src_bins.size();
float dst_bin_width =
(dst_hist.Max() - dst_hist.Min()) / dst_hist.GetHistogram()->size();
// NOLINTNEXTLINE(clang-diagnostic-sign-compare)
for (int i = 0; i < src_bins.size(); ++i) {
if (src_bins[i] == 0) {
continue;
}
// NOLINTNEXTLINE(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions)
float src_bin_begin = src_hist.Min() + src_bin_width * i;
float src_bin_end = src_bin_begin + src_bin_width;
// dst_bin corresponds to the beginning of the src_bin
// dst_bin2 corresponds to the end of the src_bin
int dst_bin = dst_bin_width == 0
? 0
// NOLINTNEXTLINE(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions)
: (src_bin_begin - dst_hist.Min()) / dst_bin_width;
// NOLINTNEXTLINE(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions)
float dst_bin_begin = dst_hist.Min() + dst_bin_width * dst_bin;
float dst_bin_end = dst_bin_begin + dst_bin_width;
int dst_bin2 =
// NOLINTNEXTLINE(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions)
dst_bin_width == 0 ? 0 : (src_bin_end - dst_hist.Min()) / dst_bin_width;
// 1 src_bin is mapped to at most 2 dst bin
assert(dst_bin2 <= dst_bin + 2);
(void)dst_bin2;
// dst_bin_cnt is the count from src_bin that should go to dst_bin
// The remainder should go to dst_bin2
// rint is the fastest way to round
// (https://stackoverflow.com/questions/485525/round-for-float-in-c/5849630)
uint64_t dst_bin_cnt = (src_bin_width == 0 || dst_bin_width == 0)
? src_bins[i]
: std::min(
static_cast<uint64_t>(rint(
(dst_bin_end - src_bin_begin) / src_bin_width * src_bins[i])),
src_bins[i]);
dst_hist.Add(dst_bin_begin + dst_bin_width / 2, dst_bin_cnt);
if (dst_bin_cnt < src_bins[i]) {
dst_hist.Add(dst_bin_end + dst_bin_width / 2, src_bins[i] - dst_bin_cnt);
}
}
}
static const int OVER_BINNING_FACTOR = 4;
DynamicHistogram::DynamicHistogram(int nbins)
: nbins_(nbins),
min_(numeric_limits<float>::max()),
max_(numeric_limits<float>::lowest()) {
assert(nbins_ > 0);
}
void DynamicHistogram::Add(float f) {
min_ = std::min(min_, f);
max_ = std::max(max_, f);
if (histogram_ == nullptr) {
histogram_ =
std::make_unique<Histogram>(nbins_ * OVER_BINNING_FACTOR, min_, max_);
histogram_->Add(f);
return;
}
Histogram curr_hist = *histogram_;
float new_min = curr_hist.Min(), new_max = curr_hist.Max();
if (f < curr_hist.Min() || f > curr_hist.Max()) {
float old_spread = curr_hist.Max() - curr_hist.Min();
if (f < curr_hist.Min()) {
if (old_spread == 0) {
new_min = f;
} else {
new_min = curr_hist.Min() -
ceil((curr_hist.Min() - f) / old_spread) * old_spread;
}
} else {
if (old_spread == 0) {
new_max = f;
} else {
new_max = curr_hist.Max() +
ceil((f - curr_hist.Max()) / old_spread) * old_spread;
}
}
new_min = std::max(numeric_limits<float>::lowest(), new_min);
new_max = std::min(numeric_limits<float>::max(), new_max);
// NOLINTNEXTLINE(modernize-make-unique)
histogram_.reset(
new Histogram(curr_hist.GetHistogram()->size(), new_min, new_max));
RemapHistograms(curr_hist, *histogram_);
}
histogram_->Add(f);
}
void DynamicHistogram::Add(const float* f, int len) {
float minimum = min_, maximum = max_;
for (int i = 0; i < len; ++i) {
minimum = std::min(f[i], minimum);
maximum = std::max(f[i], maximum);
}
min_ = std::max(numeric_limits<float>::lowest(), minimum);
max_ = std::min(numeric_limits<float>::max(), maximum);
if (histogram_ == nullptr) {
histogram_ =
std::make_unique<Histogram>(nbins_ * OVER_BINNING_FACTOR, min_, max_);
histogram_->Add(f, len);
return;
}
Histogram curr_hist = *histogram_;
float new_min = curr_hist.Min(), new_max = curr_hist.Max();
if (min_ < curr_hist.Min() || max_ > curr_hist.Max()) {
float old_spread = curr_hist.Max() - curr_hist.Min();
if (min_ < curr_hist.Min()) {
if (old_spread == 0.0f) {
new_min = min_;
} else {
new_min = curr_hist.Min() -
ceil((curr_hist.Min() - min_) / old_spread) * old_spread;
}
}
if (max_ > curr_hist.Max()) {
old_spread = curr_hist.Max() - new_min;
if (old_spread == 0.0f) {
new_max = max_;
} else {
new_max = curr_hist.Max() +
ceil((max_ - curr_hist.Max()) / old_spread) * old_spread;
}
}
new_min = std::max(numeric_limits<float>::lowest(), new_min);
new_max = std::min(numeric_limits<float>::max(), new_max);
// NOLINTNEXTLINE(modernize-make-unique)
histogram_.reset(
new Histogram(curr_hist.GetHistogram()->size(), new_min, new_max));
RemapHistograms(curr_hist, *histogram_);
}
histogram_->Add(f, len);
}
const Histogram* DynamicHistogram::Finalize() {
if (final_histogram_.get()) {
return final_histogram_.get();
}
// NOLINTNEXTLINE(modernize-make-unique)
final_histogram_.reset(new Histogram(nbins_, min_, max_));
if (histogram_.get()) {
RemapHistograms(*histogram_, *final_histogram_);
}
return final_histogram_.get();
}
} // namespace dnnlowp
|