174 lines
4.7 KiB
C++

#include "channel_quant.hpp"
#include <limits>
#include <utility>
namespace channel_quant {
namespace {
constexpr int INVALID = -22;
constexpr int RANGE_ERROR = -34;
constexpr std::int64_t SAMPLE_LIMIT = 1000000000;
bool shape_dimensions(const Tensor &tensor)
{
for (std::size_t dimension : tensor.shape) {
if (dimension > 16) {
return false;
}
}
return true;
}
bool physical_data(const Tensor &tensor)
{
if (tensor.storage.size() > 8192) {
return false;
}
for (std::int64_t value : tensor.storage) {
if (value < -SAMPLE_LIMIT || value > SAMPLE_LIMIT) {
return false;
}
}
return true;
}
std::size_t logical_size(const std::vector<std::size_t> &shape)
{
for (std::size_t dimension : shape) {
if (dimension == 0) {
return 0;
}
}
std::size_t count = 1;
for (std::size_t dimension : shape) {
if (dimension > 4096 / count) {
return 4097;
}
count *= dimension;
}
return count;
}
bool address_range(const Tensor &tensor, std::size_t elements)
{
if (tensor.origin > tensor.storage.size()) {
return false;
}
if (elements == 0) {
return true;
}
std::size_t last = tensor.origin;
const std::size_t maximum = std::numeric_limits<std::size_t>::max();
for (std::size_t index = 0; index < tensor.shape.size(); ++index) {
const std::size_t coordinate = tensor.shape[index] - 1;
const std::size_t stride = tensor.strides[index];
if (coordinate != 0 && stride > (maximum - last) / coordinate) {
return false;
}
last += coordinate * stride;
}
return last < tensor.storage.size();
}
struct Position {
std::size_t offset;
std::size_t channel;
};
Position locate(const Layout &layout, std::size_t linear)
{
Position position{layout.origin, 0};
for (std::size_t reverse = layout.shape.size(); reverse > 0; --reverse) {
const std::size_t dimension = reverse - 1;
const std::size_t coordinate = linear % layout.shape[dimension];
linear /= layout.shape[dimension];
position.offset += coordinate * layout.strides[dimension];
if (dimension == static_cast<std::size_t>(layout.axis)) {
position.channel = coordinate;
}
}
return position;
}
}
int inspect_layout(const Tensor &tensor, int axis, Layout &out)
{
const std::size_t rank = tensor.shape.size();
if (rank == 0 || rank > 4 || tensor.strides.size() != rank) {
return INVALID;
}
if (!shape_dimensions(tensor)) {
return RANGE_ERROR;
}
const int signed_rank = static_cast<int>(rank);
if (axis < -signed_rank || axis >= signed_rank) {
return INVALID;
}
if (axis < 0) {
axis = -axis - 1;
}
const std::size_t channels = tensor.shape[static_cast<std::size_t>(axis)];
if (channels == 0) {
return INVALID;
}
const std::size_t elements = logical_size(tensor.shape);
if (elements > 4096) {
return RANGE_ERROR;
}
if (!physical_data(tensor)) {
return RANGE_ERROR;
}
if (!address_range(tensor, elements)) {
return RANGE_ERROR;
}
Layout result;
result.shape = tensor.shape;
result.strides = tensor.strides;
result.origin = tensor.origin;
result.axis = axis;
result.elements = elements;
result.channels = channels;
out = std::move(result);
return 0;
}
int gather(const Tensor &tensor, int axis, std::vector<std::int64_t> &values,
std::vector<std::size_t> &channels)
{
Layout layout;
const int result = inspect_layout(tensor, axis, layout);
if (result != 0) {
return result;
}
std::vector<std::int64_t> collected;
std::vector<std::size_t> selected;
collected.reserve(layout.elements);
selected.reserve(layout.elements);
for (std::size_t index = 0; index < layout.elements; ++index) {
const Position position = locate(layout, index);
collected.push_back(tensor.storage[position.offset]);
selected.push_back(position.channel);
}
values.swap(collected);
channels.swap(selected);
return 0;
}
int element(const Tensor &tensor, int axis, std::size_t index,
std::int64_t &value, std::size_t &channel)
{
Layout layout;
const int result = inspect_layout(tensor, axis, layout);
if (result != 0) {
return result;
}
if (index >= layout.elements) {
return RANGE_ERROR;
}
const Position position = locate(layout, index);
const std::int64_t observed = tensor.storage[position.offset];
value = observed;
channel = position.channel;
return 0;
}
}