223 lines
7.3 KiB
C++
223 lines
7.3 KiB
C++
#include "channel_quant.hpp"
|
|
#include <algorithm>
|
|
#include <cstdint>
|
|
#include <utility>
|
|
#include <vector>
|
|
|
|
namespace channel_quant {
|
|
namespace {
|
|
constexpr int INVALID = -22;
|
|
constexpr int RANGE_ERROR = -34;
|
|
constexpr std::uint64_t SCALE_LIMIT = 2000000000;
|
|
|
|
bool valid_interval(const QuantParam ¶m)
|
|
{
|
|
return (param.qmin == -127 && param.qmax == 127) ||
|
|
(param.qmin == 0 && param.qmax == 255);
|
|
}
|
|
|
|
int validate_params(const std::vector<QuantParam> ¶ms,
|
|
std::size_t channels)
|
|
{
|
|
if (params.size() != channels) {
|
|
return INVALID;
|
|
}
|
|
for (const QuantParam ¶m : params) {
|
|
if (param.numerator == 0 || param.numerator > SCALE_LIMIT) {
|
|
return INVALID;
|
|
}
|
|
if (param.denominator == 0 || param.denominator > 255) {
|
|
return INVALID;
|
|
}
|
|
if (!valid_interval(param)) {
|
|
return INVALID;
|
|
}
|
|
if (param.zero < param.qmin || param.zero > param.qmax) {
|
|
return INVALID;
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
std::int64_t round_ratio(std::int64_t numerator, std::uint64_t denominator)
|
|
{
|
|
const bool negative = numerator < 0;
|
|
const std::uint64_t absolute = negative ?
|
|
static_cast<std::uint64_t>(-numerator) :
|
|
static_cast<std::uint64_t>(numerator);
|
|
const std::uint64_t quotient = absolute / denominator;
|
|
const std::uint64_t remainder = absolute % denominator;
|
|
const bool above = remainder > denominator - remainder;
|
|
const bool tied_odd = remainder == denominator - remainder &&
|
|
(quotient & 1u) != 0;
|
|
const std::int64_t rounded = static_cast<std::int64_t>(
|
|
quotient + (above || tied_odd ? 1 : 0));
|
|
return negative ? -rounded : rounded;
|
|
}
|
|
|
|
int clipped(std::int64_t value, const QuantParam ¶m)
|
|
{
|
|
if (value < param.qmin) {
|
|
return param.qmin;
|
|
}
|
|
if (value > param.qmax) {
|
|
return param.qmax;
|
|
}
|
|
return static_cast<int>(value);
|
|
}
|
|
|
|
int validate_samples(const std::vector<std::int64_t> &values,
|
|
const std::vector<std::size_t> &channels,
|
|
const std::vector<QuantParam> ¶ms)
|
|
{
|
|
for (std::size_t index = 0; index < values.size(); ++index) {
|
|
const QuantParam ¶m = params[channels[index]];
|
|
if (values[index] < param.qmin || values[index] > param.qmax) {
|
|
return RANGE_ERROR;
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
struct Prepared {
|
|
Layout layout;
|
|
std::vector<std::int64_t> values;
|
|
std::vector<std::size_t> channels;
|
|
};
|
|
|
|
int prepare_layout(const Tensor &tensor, int axis, Prepared &prepared)
|
|
{
|
|
return inspect_layout(tensor, axis, prepared.layout);
|
|
}
|
|
|
|
int collect(const Tensor &tensor, int axis, Prepared &prepared)
|
|
{
|
|
return gather(tensor, axis, prepared.values, prepared.channels);
|
|
}
|
|
|
|
int convert_sample(std::int64_t input, const QuantParam ¶m)
|
|
{
|
|
const std::int64_t numerator = input *
|
|
static_cast<std::int64_t>(param.denominator);
|
|
const std::int64_t rounded = round_ratio(numerator, param.numerator);
|
|
return clipped(rounded + param.zero, param);
|
|
}
|
|
|
|
std::int64_t reconstruct_sample(std::int64_t input, const QuantParam ¶m)
|
|
{
|
|
const std::int64_t numerator = (input - param.zero) *
|
|
static_cast<std::int64_t>(param.numerator);
|
|
return round_ratio(numerator, param.denominator);
|
|
}
|
|
|
|
int rescale_sample(std::int64_t input, const QuantParam &source,
|
|
const QuantParam &destination)
|
|
{
|
|
const std::int64_t centered = input - source.zero;
|
|
const std::int64_t numerator = centered *
|
|
static_cast<std::int64_t>(source.numerator) *
|
|
static_cast<std::int64_t>(destination.denominator);
|
|
const std::uint64_t denominator = source.denominator * destination.numerator;
|
|
const std::int64_t rounded = round_ratio(numerator, denominator);
|
|
return clipped(rounded + destination.zero, destination);
|
|
}
|
|
}
|
|
|
|
int quantize(const Tensor &tensor, int axis, const std::vector<QuantParam> ¶ms,
|
|
std::vector<int> &out)
|
|
{
|
|
Prepared prepared;
|
|
const int layout_status = prepare_layout(tensor, axis, prepared);
|
|
if (layout_status != 0) {
|
|
return layout_status;
|
|
}
|
|
const int params_status = validate_params(params, prepared.layout.channels);
|
|
if (params_status != 0) {
|
|
return params_status;
|
|
}
|
|
const int collect_status = collect(tensor, axis, prepared);
|
|
if (collect_status != 0) {
|
|
return collect_status;
|
|
}
|
|
std::vector<int> converted;
|
|
converted.reserve(prepared.layout.elements);
|
|
for (std::size_t index = 0; index < prepared.values.size(); ++index) {
|
|
const std::size_t channel = prepared.channels[index];
|
|
const QuantParam ¶m = params[channel];
|
|
converted.push_back(convert_sample(prepared.values[index], param));
|
|
}
|
|
out.swap(converted);
|
|
return 0;
|
|
}
|
|
|
|
int dequantize(const Tensor &tensor, int axis, const std::vector<QuantParam> ¶ms,
|
|
std::vector<std::int64_t> &out)
|
|
{
|
|
Prepared prepared;
|
|
const int layout_status = prepare_layout(tensor, axis, prepared);
|
|
if (layout_status != 0) {
|
|
return layout_status;
|
|
}
|
|
const int params_status = validate_params(params, prepared.layout.channels);
|
|
if (params_status != 0) {
|
|
return params_status;
|
|
}
|
|
const int collect_status = collect(tensor, axis, prepared);
|
|
if (collect_status != 0) {
|
|
return collect_status;
|
|
}
|
|
const int samples_status = validate_samples(prepared.values,
|
|
prepared.channels, params);
|
|
if (samples_status != 0) {
|
|
return samples_status;
|
|
}
|
|
std::vector<std::int64_t> converted;
|
|
converted.reserve(prepared.layout.elements);
|
|
for (std::size_t index = 0; index < prepared.values.size(); ++index) {
|
|
const std::size_t channel = prepared.channels[index];
|
|
const QuantParam ¶m = params[channel];
|
|
converted.push_back(reconstruct_sample(prepared.values[index], param));
|
|
}
|
|
out.swap(converted);
|
|
return 0;
|
|
}
|
|
|
|
int requantize(const Tensor &tensor, int axis,
|
|
const std::vector<QuantParam> &source,
|
|
const std::vector<QuantParam> &destination,
|
|
std::vector<int> &out)
|
|
{
|
|
Prepared prepared;
|
|
const int layout_status = prepare_layout(tensor, axis, prepared);
|
|
if (layout_status != 0) {
|
|
return layout_status;
|
|
}
|
|
const int source_status = validate_params(source, prepared.layout.channels);
|
|
if (source_status != 0) {
|
|
return source_status;
|
|
}
|
|
const int destination_status = validate_params(destination, prepared.layout.channels);
|
|
if (destination_status != 0) {
|
|
return destination_status;
|
|
}
|
|
const int collect_status = collect(tensor, axis, prepared);
|
|
if (collect_status != 0) {
|
|
return collect_status;
|
|
}
|
|
const int samples_status = validate_samples(prepared.values,
|
|
prepared.channels, source);
|
|
if (samples_status != 0) {
|
|
return samples_status;
|
|
}
|
|
std::vector<int> converted;
|
|
converted.reserve(prepared.layout.elements);
|
|
for (std::size_t index = 0; index < prepared.values.size(); ++index) {
|
|
const std::size_t channel = prepared.channels[index];
|
|
converted.push_back(rescale_sample(prepared.values[index],
|
|
source[channel], destination[channel]));
|
|
}
|
|
out.swap(converted);
|
|
return 0;
|
|
}
|
|
}
|