blob: 39044434d023ee0f1e419672492d39c12c6b25c4 [file] [log] [blame]
// Copyright (c) Facebook, Inc. and its affiliates.
// All rights reserved.
//
// Copyright 2019 Google LLC
//
// This source code is licensed under the BSD-style license found in the
// LICENSE file in the root directory of this source tree.
#pragma once
#include <gtest/gtest.h>
#include <algorithm>
#include <cassert>
#include <cstddef>
#include <cstdlib>
#include <functional>
#include <limits>
#include <random>
#include <vector>
#include <xnnpack.h>
#include <xnnpack/params-init.h>
#include <xnnpack/params.h>
class MaxPoolMicrokernelTester {
public:
inline MaxPoolMicrokernelTester& output_pixels(size_t output_pixels) {
assert(output_pixels != 0);
this->output_pixels_ = output_pixels;
return *this;
}
inline size_t output_pixels() const {
return this->output_pixels_;
}
inline MaxPoolMicrokernelTester& step(size_t step) {
assert(step != 0);
this->step_ = step;
return *this;
}
inline size_t step() const {
return this->step_;
}
inline MaxPoolMicrokernelTester& input_offset(size_t input_offset) {
assert(input_offset != 0);
this->input_offset_ = input_offset;
return *this;
}
inline size_t input_offset() const {
return this->input_offset_;
}
inline MaxPoolMicrokernelTester& pooling_elements(size_t pooling_elements) {
assert(pooling_elements != 0);
this->pooling_elements_ = pooling_elements;
return *this;
}
inline size_t pooling_elements() const {
return this->pooling_elements_;
}
inline size_t packed_pooling_elements() const {
if (pooling_elements() <= primary_pooling_tile()) {
return primary_pooling_tile();
} else {
return (pooling_elements() - primary_pooling_tile()) % incremental_pooling_tile() == 0 ? pooling_elements() : ((pooling_elements() - primary_pooling_tile()) / incremental_pooling_tile() + 1) * incremental_pooling_tile() + primary_pooling_tile();
}
}
inline MaxPoolMicrokernelTester& pooling_tile(size_t primary_tile, size_t incremental_tile) {
assert(primary_tile != 0);
this->primary_pooling_tile_ = primary_tile;
this->incremental_pooling_tile_ = incremental_tile;
return *this;
}
inline MaxPoolMicrokernelTester& primary_pooling_tile(size_t primary_pooling_tile) {
assert(primary_pooling_tile != 0);
this->primary_pooling_tile_ = primary_pooling_tile;
return *this;
}
inline size_t primary_pooling_tile() const {
return this->primary_pooling_tile_;
}
inline MaxPoolMicrokernelTester& incremental_pooling_tile(size_t incremental_pooling_tile) {
assert(incremental_pooling_tile != 0);
this->incremental_pooling_tile_ = incremental_pooling_tile;
return *this;
}
inline size_t incremental_pooling_tile() const {
return this->incremental_pooling_tile_;
}
inline MaxPoolMicrokernelTester& channels(size_t channels) {
assert(channels != 0);
this->channels_ = channels;
return *this;
}
inline size_t channels() const {
return this->channels_;
}
inline MaxPoolMicrokernelTester& output_stride(size_t output_stride) {
assert(output_stride != 0);
this->output_stride_ = output_stride;
return *this;
}
inline size_t output_stride() const {
if (this->output_stride_ == 0) {
return channels();
} else {
assert(this->output_stride_ >= channels());
return this->output_stride_;
}
}
inline MaxPoolMicrokernelTester& qmin(uint8_t qmin) {
this->qmin_ = qmin;
return *this;
}
inline uint8_t qmin() const {
return this->qmin_;
}
inline MaxPoolMicrokernelTester& qmax(uint8_t qmax) {
this->qmax_ = qmax;
return *this;
}
inline uint8_t qmax() const {
return this->qmax_;
}
inline MaxPoolMicrokernelTester& iterations(size_t iterations) {
this->iterations_ = iterations;
return *this;
}
inline size_t iterations() const {
return this->iterations_;
}
void Test(xnn_s8_maxpool_ukernel_function maxpool, xnn_init_s8_minmax_params_fn init_params) const {
std::random_device random_device;
auto rng = std::mt19937(random_device());
auto i8rng = std::bind(
std::uniform_int_distribution<int32_t>(std::numeric_limits<int8_t>::min(), std::numeric_limits<int8_t>::max()),
std::ref(rng));
std::vector<const int8_t*> indirect_input((output_pixels() - 1) * step() + packed_pooling_elements());
std::vector<int8_t> input(XNN_EXTRA_BYTES / sizeof(int8_t) +
indirect_input.size() * channels());
std::vector<int8_t> output(XNN_EXTRA_BYTES / sizeof(int8_t) +
(output_pixels() - 1) * output_stride() + channels());
std::vector<int8_t> output_ref(output_pixels() * channels());
for (size_t iteration = 0; iteration < iterations(); iteration++) {
do {
std::generate(input.begin(), input.end(), std::ref(i8rng));
} while (input.size() > 1 && *std::max_element(input.cbegin(), input.cend()) == *std::min_element(input.cbegin(), input.cend()));
std::fill(output.begin(), output.end(), 0xA5);
for (size_t i = 0; i < (output_pixels() - 1) * step() + pooling_elements(); i++) {
indirect_input[i] = input.data() + i * channels() - input_offset();
}
std::shuffle(indirect_input.begin(),
indirect_input.begin() + (output_pixels() - 1) * step() + pooling_elements(), rng);
// Prepare parameters.
xnn_s8_minmax_params params;
init_params(&params, int8_t(qmin() - 0x80), int8_t(qmax() - 0x80));
// Compute reference results.
for (size_t x = 0; x < output_pixels(); x++) {
for (size_t c = 0; c < channels(); c++) {
int8_t max_value = std::numeric_limits<int8_t>::min();
for (size_t p = 0; p < pooling_elements(); p++) {
max_value = std::max(max_value, indirect_input[x * step() + p][c + input_offset()]);
}
max_value = std::min(max_value, int8_t(qmax() - 0x80));
max_value = std::max(max_value, int8_t(qmin() - 0x80));
output_ref[x * channels() + c] = max_value;
}
}
// Call optimized micro-kernel.
maxpool(output_pixels(), pooling_elements(), channels(),
indirect_input.data(), input_offset() * sizeof(int8_t), output.data(),
(step() - packed_pooling_elements()) * sizeof(void*),
(output_stride() - channels()) * sizeof(int8_t),
&params);
// Verify results.
for (size_t x = 0; x < output_pixels(); x++) {
for (size_t c = 0; c < channels(); c++) {
ASSERT_GE(int32_t(output[x * output_stride() + c]), int32_t(qmin() - 0x80))
<< "at pixel " << x << " / " << output_pixels() << ", channel " << c << " / " << channels()
<< ", pooling elements = " << pooling_elements() << ", step = " << step()
<< ", input offset = " << input_offset();
ASSERT_LE(int32_t(output[x * output_stride() + c]), int32_t(qmax() - 0x80))
<< "at pixel " << x << " / " << output_pixels() << ", channel " << c << " / " << channels()
<< ", pooling elements = " << pooling_elements() << ", step = " << step()
<< ", input offset = " << input_offset();
ASSERT_EQ(int32_t(output_ref[x * channels() + c]), int32_t(output[x * output_stride() + c]))
<< "at pixel " << x << " / " << output_pixels() << ", channel " << c << " / " << channels()
<< ", pooling elements = " << pooling_elements() << ", step = " << step()
<< ", input offset = " << input_offset();
}
}
}
}
void Test(xnn_u8_maxpool_ukernel_function maxpool, xnn_init_u8_minmax_params_fn init_params) const {
std::random_device random_device;
auto rng = std::mt19937(random_device());
auto u8rng = std::bind(std::uniform_int_distribution<uint32_t>(0, std::numeric_limits<uint8_t>::max()), rng);
std::vector<const uint8_t*> indirect_input((output_pixels() - 1) * step() + packed_pooling_elements());
std::vector<uint8_t> input(XNN_EXTRA_BYTES / sizeof(uint8_t) +
indirect_input.size() * channels());
std::vector<uint8_t> output(XNN_EXTRA_BYTES / sizeof(uint8_t) +
(output_pixels() - 1) * output_stride() + channels());
std::vector<uint8_t> output_ref(output_pixels() * channels());
for (size_t iteration = 0; iteration < iterations(); iteration++) {
do {
std::generate(input.begin(), input.end(), std::ref(u8rng));
} while (input.size() > 1 && *std::max_element(input.cbegin(), input.cend()) == *std::min_element(input.cbegin(), input.cend()));
std::fill(output.begin(), output.end(), 0xA5);
for (size_t i = 0; i < (output_pixels() - 1) * step() + pooling_elements(); i++) {
indirect_input[i] = input.data() + i * channels() - input_offset();
}
std::shuffle(indirect_input.begin(),
indirect_input.begin() + (output_pixels() - 1) * step() + pooling_elements(), rng);
// Prepare parameters.
xnn_u8_minmax_params params;
init_params(&params, qmin(), qmax());
// Compute reference results.
for (size_t x = 0; x < output_pixels(); x++) {
for (size_t c = 0; c < channels(); c++) {
uint8_t max_value = 0;
for (size_t p = 0; p < pooling_elements(); p++) {
max_value = std::max(max_value, indirect_input[x * step() + p][c + input_offset()]);
}
max_value = std::min(max_value, qmax());
max_value = std::max(max_value, qmin());
output_ref[x * channels() + c] = max_value;
}
}
// Call optimized micro-kernel.
maxpool(output_pixels(), pooling_elements(), channels(),
indirect_input.data(), input_offset() * sizeof(uint8_t), output.data(),
(step() - packed_pooling_elements()) * sizeof(void*),
(output_stride() - channels()) * sizeof(uint8_t),
&params);
// Verify results.
for (size_t x = 0; x < output_pixels(); x++) {
for (size_t c = 0; c < channels(); c++) {
ASSERT_GE(uint32_t(output[x * output_stride() + c]), uint32_t(qmin()))
<< "at pixel " << x << " / " << output_pixels() << ", channel " << c << " / " << channels()
<< ", pooling elements = " << pooling_elements() << ", step = " << step()
<< ", input offset = " << input_offset();
ASSERT_LE(uint32_t(output[x * output_stride() + c]), uint32_t(qmax()))
<< "at pixel " << x << " / " << output_pixels() << ", channel " << c << " / " << channels()
<< ", pooling elements = " << pooling_elements() << ", step = " << step()
<< ", input offset = " << input_offset();
ASSERT_EQ(uint32_t(output_ref[x * channels() + c]), uint32_t(output[x * output_stride() + c]))
<< "at pixel " << x << " / " << output_pixels() << ", channel " << c << " / " << channels()
<< ", pooling elements = " << pooling_elements() << ", step = " << step()
<< ", input offset = " << input_offset();
}
}
}
}
void Test(xnn_f32_maxpool_ukernel_function maxpool, xnn_init_f32_minmax_params_fn init_params) const {
std::random_device random_device;
auto rng = std::mt19937(random_device());
auto f32rng = std::bind(std::uniform_real_distribution<float>(0.0f, 1.0f), rng);
std::vector<const float*> indirect_input((output_pixels() - 1) * step() + packed_pooling_elements());
std::vector<float> input(XNN_EXTRA_BYTES / sizeof(float) +
((output_pixels() - 1) * step() + pooling_elements()) * channels());
std::vector<float> output(XNN_EXTRA_BYTES / sizeof(float) +
(output_pixels() - 1) * output_stride() + channels());
std::vector<float> output_ref(output_pixels() * channels());
for (size_t iteration = 0; iteration < iterations(); iteration++) {
std::generate(input.begin(), input.end(), std::ref(f32rng));
std::fill(output.begin(), output.end(), nanf(""));
for (size_t i = 0; i < (output_pixels() - 1) * step() + pooling_elements(); i++) {
indirect_input[i] = input.data() + i * channels() - input_offset();
}
std::shuffle(indirect_input.begin(),
indirect_input.begin() + (output_pixels() - 1) * step() + pooling_elements(), rng);
// Compute reference results, without clamping.
for (size_t x = 0; x < output_pixels(); x++) {
for (size_t c = 0; c < channels(); c++) {
float max_value = -std::numeric_limits<float>::infinity();
for (size_t p = 0; p < pooling_elements(); p++) {
max_value = std::max(max_value, indirect_input[x * step() + p][c + input_offset()]);
}
output_ref[x * channels() + c] = max_value;
}
}
// Compute clamping parameters.
const float accumulated_min = *std::min_element(output_ref.cbegin(), output_ref.cend());
const float accumulated_max = *std::max_element(output_ref.cbegin(), output_ref.cend());
const float accumulated_range = accumulated_max - accumulated_min;
const float output_min = accumulated_min + float(qmin()) / 255.0f * accumulated_range;
const float output_max = accumulated_max - float(255 - qmax()) / 255.0f * accumulated_range;
// Prepare parameters.
xnn_f32_minmax_params params;
init_params(&params, output_min, output_max);
// Clamp reference results.
for (float& output_value : output_ref) {
output_value = std::max(std::min(output_value, output_max), output_min);
}
// Call optimized micro-kernel.
maxpool(output_pixels(), pooling_elements(), channels(),
indirect_input.data(), input_offset() * sizeof(float), output.data(),
(step() - packed_pooling_elements()) * sizeof(void*),
(output_stride() - channels()) * sizeof(float),
&params);
// Verify results.
for (size_t x = 0; x < output_pixels(); x++) {
for (size_t c = 0; c < channels(); c++) {
ASSERT_GE(output[x * output_stride() + c], output_min)
<< "at pixel " << x << " / " << output_pixels() << ", channel " << c << " / " << channels()
<< ", pooling elements = " << pooling_elements() << ", step = " << step()
<< ", input offset = " << input_offset();
ASSERT_LE(output[x * output_stride() + c], output_max)
<< "at pixel " << x << " / " << output_pixels() << ", channel " << c << " / " << channels()
<< ", pooling elements = " << pooling_elements() << ", step = " << step()
<< ", input offset = " << input_offset();
ASSERT_EQ(output_ref[x * channels() + c], output[x * output_stride() + c])
<< "at pixel " << x << " / " << output_pixels() << ", channel " << c << " / " << channels()
<< ", pooling elements = " << pooling_elements() << ", step = " << step()
<< ", input offset = " << input_offset();
}
}
}
}
private:
size_t output_pixels_{1};
size_t pooling_elements_{1};
size_t channels_{1};
size_t input_offset_{0};
size_t step_{1};
size_t primary_pooling_tile_{1};
size_t incremental_pooling_tile_{1};
size_t output_stride_{0};
uint8_t qmin_{0};
uint8_t qmax_{255};
size_t iterations_{3};
};