File size: 9,151 Bytes
8b7c501 |
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 202 203 204 205 206 207 208 |
// Copyright 2022 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.
#include <algorithm>
#include <array>
#include <cstddef>
#include <cstdint>
#include <limits>
#include <memory>
#include <random>
#include <vector>
#include <gtest/gtest.h>
#include <xnnpack.h>
#include <xnnpack/node-type.h>
#include <xnnpack/operator-utils.h>
#include <xnnpack/operator.h>
#include <xnnpack/subgraph.h>
class AveragePoolingTestF32 : public ::testing::Test {
protected:
AveragePoolingTestF32()
{
random_device = std::make_unique<std::random_device>();
rng = std::mt19937((*random_device)());
input_size_dist = std::uniform_int_distribution<uint32_t>(10, 15);
pooling_size_dist = std::uniform_int_distribution<uint32_t>(2, 5);
stride_dist = std::uniform_int_distribution<uint32_t>(1, 2);
batch_size = input_size_dist(rng);
input_height = input_size_dist(rng);
input_width = input_size_dist(rng);
channels = input_size_dist(rng);
pooling_height = pooling_size_dist(rng);
pooling_width = pooling_size_dist(rng);
// Avoid padding == pooling dimension because it will result in NaNs and cause comparison to fail.
input_padding_top = std::uniform_int_distribution<uint32_t>(0, pooling_height - 1)(rng);
input_padding_right = std::uniform_int_distribution<uint32_t>(0, pooling_width - 1)(rng);
input_padding_bottom = std::uniform_int_distribution<uint32_t>(0, pooling_height - 1)(rng);
input_padding_left = std::uniform_int_distribution<uint32_t>(0, pooling_width - 1)(rng);
stride_height = stride_dist(rng);
stride_width = stride_dist(rng);
output_height = xnn_compute_convolution_output_dimension(
input_padding_top + input_height + input_padding_bottom, pooling_height, 1, stride_height);
output_width = xnn_compute_convolution_output_dimension(
input_padding_left + input_width + input_padding_right, pooling_width, 1, stride_width);
output_min = std::uniform_real_distribution<float>(-255.0f, 0.0f)(rng);
output_max = std::uniform_real_distribution<float>(0.1f, 255.0f)(rng);
input_dims = {batch_size, input_height, input_width, channels};
output_dims = {batch_size, output_height, output_width, channels};
input = std::vector<float>(XNN_EXTRA_BYTES / sizeof(float) + batch_size * input_height * input_width * channels);
operator_output = std::vector<float>(batch_size * output_height * output_width * channels);
subgraph_output = std::vector<float>(batch_size * output_height * output_width * channels);
}
std::unique_ptr<std::random_device> random_device;
std::mt19937 rng;
std::uniform_int_distribution<uint32_t> input_size_dist;
std::uniform_int_distribution<uint32_t> pooling_size_dist;
std::uniform_int_distribution<uint32_t> stride_dist;
uint32_t batch_size;
uint32_t input_height;
uint32_t input_width;
uint32_t channels;
uint32_t pooling_height;
uint32_t pooling_width;
uint32_t output_height;
uint32_t output_width;
uint32_t stride_height;
uint32_t stride_width;
std::array<size_t, 4> input_dims;
std::array<size_t, 4> output_dims;
uint32_t input_padding_top;
uint32_t input_padding_right;
uint32_t input_padding_bottom;
uint32_t input_padding_left;
float output_min;
float output_max;
uint32_t input_id;
uint32_t output_id;
std::vector<float> input;
std::vector<float> operator_output;
std::vector<float> subgraph_output;
};
TEST_F(AveragePoolingTestF32, define)
{
ASSERT_EQ(xnn_status_success, xnn_initialize(/*allocator=*/nullptr));
xnn_subgraph_t subgraph = nullptr;
ASSERT_EQ(xnn_status_success, xnn_create_subgraph(/*external_value_ids=*/2, /*flags=*/0, &subgraph));
std::unique_ptr<xnn_subgraph, decltype(&xnn_delete_subgraph)> auto_subgraph(subgraph, xnn_delete_subgraph);
input_id = XNN_INVALID_NODE_ID;
ASSERT_EQ(
xnn_status_success, xnn_define_tensor_value(
subgraph, xnn_datatype_fp32, input_dims.size(), input_dims.data(), nullptr, 0,
/*flags=*/XNN_VALUE_FLAG_EXTERNAL_INPUT, &input_id));
ASSERT_NE(input_id, XNN_INVALID_NODE_ID);
output_id = XNN_INVALID_NODE_ID;
ASSERT_EQ(
xnn_status_success, xnn_define_tensor_value(
subgraph, xnn_datatype_fp32, output_dims.size(), output_dims.data(), nullptr, 1,
/*flags=*/XNN_VALUE_FLAG_EXTERNAL_OUTPUT, &output_id));
ASSERT_NE(output_id, XNN_INVALID_NODE_ID);
ASSERT_EQ(
xnn_status_success,
xnn_define_average_pooling_2d(
subgraph, input_padding_top, input_padding_right, input_padding_bottom, input_padding_left, pooling_height,
pooling_width, stride_height, stride_width, output_min, output_max, input_id, output_id,
/*flags=*/0));
ASSERT_EQ(subgraph->num_nodes, 1);
const struct xnn_node* node = &subgraph->nodes[0];
ASSERT_EQ(node->type, xnn_node_type_average_pooling_2d);
ASSERT_EQ(node->compute_type, xnn_compute_type_fp32);
ASSERT_EQ(node->params.pooling_2d.padding_top, input_padding_top);
ASSERT_EQ(node->params.pooling_2d.padding_right, input_padding_right);
ASSERT_EQ(node->params.pooling_2d.padding_bottom, input_padding_bottom);
ASSERT_EQ(node->params.pooling_2d.padding_left, input_padding_left);
ASSERT_EQ(node->params.pooling_2d.pooling_height, pooling_height);
ASSERT_EQ(node->params.pooling_2d.pooling_width, pooling_width);
ASSERT_EQ(node->params.pooling_2d.stride_height, stride_height);
ASSERT_EQ(node->params.pooling_2d.stride_width, stride_width);
ASSERT_EQ(node->activation.output_min, output_min);
ASSERT_EQ(node->activation.output_max, output_max);
ASSERT_EQ(node->num_inputs, 1);
ASSERT_EQ(node->inputs[0], input_id);
ASSERT_EQ(node->num_outputs, 1);
ASSERT_EQ(node->outputs[0], output_id);
ASSERT_EQ(node->flags, 0);
}
TEST_F(AveragePoolingTestF32, matches_operator_api)
{
std::uniform_real_distribution<float> f32dist;
std::generate(input.begin(), input.end(), [&]() { return f32dist(rng); });
std::fill(operator_output.begin(), operator_output.end(), nanf(""));
std::fill(subgraph_output.begin(), subgraph_output.end(), nanf(""));
ASSERT_EQ(xnn_status_success, xnn_initialize(/*allocator=*/nullptr));
// Call operator API.
xnn_operator_t op = nullptr;
const xnn_status status = xnn_create_average_pooling2d_nhwc_f32(
input_padding_top, input_padding_right, input_padding_bottom, input_padding_left, pooling_height, pooling_width,
stride_height, stride_width, channels, channels, channels, output_min, output_max, /*flags=*/0, &op);
if (status == xnn_status_unsupported_hardware) {
GTEST_SKIP();
}
ASSERT_EQ(xnn_status_success, status);
ASSERT_NE(nullptr, op);
std::unique_ptr<xnn_operator, decltype(&xnn_delete_operator)> auto_op(op, xnn_delete_operator);
ASSERT_EQ(
xnn_status_success,
xnn_reshape_average_pooling2d_nhwc_f32(
op, batch_size, input_height, input_width, /*output_height_out=*/nullptr, /*output_width_out=*/nullptr,
/*threadpool=*/nullptr));
ASSERT_EQ(xnn_status_success, xnn_setup_average_pooling2d_nhwc_f32(op, input.data(), operator_output.data()));
ASSERT_EQ(xnn_status_success, xnn_run_operator(op, /*threadpool=*/nullptr));
// Call subgraph API.
xnn_subgraph_t subgraph = nullptr;
ASSERT_EQ(xnn_status_success, xnn_create_subgraph(/*external_value_ids=*/2, /*flags=*/0, &subgraph));
std::unique_ptr<xnn_subgraph, decltype(&xnn_delete_subgraph)> auto_subgraph(subgraph, xnn_delete_subgraph);
input_id = XNN_INVALID_NODE_ID;
ASSERT_EQ(
xnn_status_success, xnn_define_tensor_value(
subgraph, xnn_datatype_fp32, input_dims.size(), input_dims.data(), nullptr, /*external_id=*/0,
/*flags=*/XNN_VALUE_FLAG_EXTERNAL_INPUT, &input_id));
ASSERT_NE(input_id, XNN_INVALID_NODE_ID);
output_id = XNN_INVALID_NODE_ID;
ASSERT_EQ(
xnn_status_success,
xnn_define_tensor_value(
subgraph, xnn_datatype_fp32, output_dims.size(), output_dims.data(), nullptr, /*external_id=*/1,
/*flags=*/XNN_VALUE_FLAG_EXTERNAL_OUTPUT, &output_id));
ASSERT_NE(output_id, XNN_INVALID_NODE_ID);
xnn_runtime_t runtime = nullptr;
ASSERT_EQ(
xnn_status_success,
xnn_define_average_pooling_2d(
subgraph, input_padding_top, input_padding_right, input_padding_bottom, input_padding_left, pooling_height,
pooling_width, stride_height, stride_width, output_min, output_max, input_id, output_id,
/*flags=*/0));
ASSERT_EQ(xnn_status_success, xnn_create_runtime_v3(subgraph, nullptr, nullptr, /*flags=*/0, &runtime));
ASSERT_NE(nullptr, runtime);
std::unique_ptr<xnn_runtime, decltype(&xnn_delete_runtime)> auto_runtime(runtime, xnn_delete_runtime);
std::array<xnn_external_value, 2> external = {
xnn_external_value{input_id, input.data()}, xnn_external_value{output_id, subgraph_output.data()}};
ASSERT_EQ(xnn_status_success, xnn_setup_runtime(runtime, external.size(), external.data()));
ASSERT_EQ(xnn_status_success, xnn_invoke_runtime(runtime));
ASSERT_EQ(subgraph_output, operator_output);
}
|