127 lines
4.3 KiB
C++

#include "channel_quant.hpp"
#include <algorithm>
namespace channel_quant {
Calibrator::Calibrator() { reset(); }
void Calibrator::reset() {
count_ = 0;
category_ = -1;
current_.fill(Range{});
saved_ranges_.fill(Range{});
has_saved_ = false;
}
int Calibrator::configure(std::size_t channels, int mode) {
if (!channels || channels > 16 || (mode != SYMMETRIC && mode != AFFINE)) return -22;
reset();
count_ = channels;
category_ = mode;
return 0;
}
int Calibrator::update(const Tensor &tensor, int axis) {
if (category_ < 0) return -19;
Layout view{};
int error = inspect_layout(tensor,axis,view);
if (error) return error;
if (view.channels != count_) return -22;
std::vector<std::int64_t> values;
std::vector<std::size_t> channels;
error = gather(tensor,axis,values,channels);
if (error) return error;
auto next = current_;
for (std::size_t i = 0; i < values.size(); ++i) {
Range &range = next[channels[i]];
if (range.count == COUNT_LIMIT) return -34;
if (!range.count) range.minimum = range.maximum = values[i];
else {
range.minimum = std::min(range.minimum,values[i]);
range.maximum = std::max(range.maximum,values[i]);
}
++range.count;
}
current_ = next;
return 0;
}
int Calibrator::merge(const Calibrator &other) {
if (category_ < 0 || other.category_ < 0) return -19;
if (count_ != other.count_ || category_ != other.category_) return -22;
auto next = current_;
for (std::size_t i = 0; i < count_; ++i) {
const Range &source = other.current_[i];
Range &target = next[i];
if (source.count > COUNT_LIMIT - target.count) return -34;
if (!source.count) continue;
if (!target.count) target = source;
else {
target.minimum = std::min(target.minimum,source.minimum);
target.maximum = std::max(target.maximum,source.maximum);
target.count += source.count;
}
}
current_ = next;
return 0;
}
int Calibrator::checkpoint() {
if (category_ < 0) return -19;
saved_ranges_ = current_;
has_saved_ = true;
return 0;
}
int Calibrator::restore() {
if (category_ < 0) return -19;
if (!has_saved_) return -2;
current_ = saved_ranges_;
return 0;
}
int Calibrator::clear() {
if (category_ < 0) return -19;
current_.fill(Range{});
return 0;
}
int Calibrator::discard_checkpoint() {
if (category_ < 0) return -19;
has_saved_ = false;
return 0;
}
int Calibrator::query(std::size_t channel, Range &out) const {
if (category_ < 0) return -19;
if (channel >= count_) return -22;
out = current_[channel];
return 0;
}
int Calibrator::parameters(std::vector<QuantParam> &out) const {
if (category_ < 0) return -19;
for (std::size_t i = 0; i < count_; ++i) if (!current_[i].count) return -61;
std::vector<QuantParam> result;
for (std::size_t i = 0; i < count_; ++i) {
auto low = current_[i].minimum;
auto high = current_[i].maximum;
if (category_ == SYMMETRIC) {
auto magnitude = std::max(low < 0 ? -low : low, high < 0 ? -high : high);
result.push_back(magnitude ? QuantParam{static_cast<std::uint64_t>(magnitude),127,0,-127,127}
: QuantParam{1,1,0,-127,127});
} else {
low = std::min<std::int64_t>(0,low);
high = std::max<std::int64_t>(0,high);
auto width = high - low;
if (!width) result.push_back({1,1,0,0,255});
else {
std::int64_t numerator = -low * 255;
auto zero = numerator / width;
auto remainder = numerator % width;
if (2 * remainder > width || (2 * remainder == width && zero % 2)) ++zero;
zero = std::max<std::int64_t>(0,std::min<std::int64_t>(255,zero));
result.push_back({static_cast<std::uint64_t>(width),255,static_cast<int>(zero),0,255});
}
}
}
out.swap(result);
return 0;
}
std::size_t Calibrator::channels() const { return count_; }
int Calibrator::mode() const { return category_; }
std::uint64_t Calibrator::total() const {
std::uint64_t value = 0;
for (std::size_t i = 0; i < count_; ++i) value += current_[i].count;
return value;
}
}