| // 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> // For std::generate, std::min. |
| #include <array> // For std::array. |
| #include <cmath> // For std::lrintf. |
| #include <cstddef> // For size_t. |
| #include <cstdint> // For uint32_t. |
| #include <limits> // For std::numeric_limits. |
| #include <memory> // For std::unique_ptr. |
| #include <numeric> // For std::accumulate. |
| #include <random> // For std::random_device, std::mt19937, std::uniform_real_distribution. |
| #include <vector> // For std::vector. |
| |
| #include <xnnpack.h> |
| #include <xnnpack/operator.h> |
| #include <xnnpack/requantization.h> |
| #include <xnnpack/subgraph.h> |
| |
| #include <gtest/gtest.h> |
| |
| template <class T, class BiasType = T> class FullyConnectedTestBase : public ::testing::Test { |
| protected: |
| FullyConnectedTestBase() |
| { |
| random_device = std::unique_ptr<std::random_device>(new std::random_device()); |
| rng = std::mt19937((*random_device)()); |
| input_size_dist = std::uniform_int_distribution<uint32_t>(10, 15); |
| kernel_size_dist = std::uniform_int_distribution<uint32_t>(1, 5); |
| stride_dist = std::uniform_int_distribution<uint32_t>(1, 2); |
| f32dist = std::uniform_real_distribution<float>(0.1f, 1.0f); |
| scale_dist = std::uniform_real_distribution<float>(1.0f, 5.0f); |
| i32dist = std::uniform_int_distribution<int32_t>(-10000, 10000); |
| auto shape_dist = std::uniform_int_distribution<size_t>(2, XNN_MAX_TENSOR_DIMS); |
| dim_dist = std::uniform_int_distribution<size_t>(5, 15); |
| i8dist = |
| std::uniform_int_distribution<int32_t>(std::numeric_limits<int8_t>::min(), std::numeric_limits<int8_t>::max()); |
| w8dist = |
| std::uniform_int_distribution<int32_t>(-std::numeric_limits<uint8_t>::max(), std::numeric_limits<uint8_t>::max()); |
| |
| output_min = -std::numeric_limits<float>::infinity(); |
| output_max = std::numeric_limits<float>::infinity(); |
| |
| size_t num_input_dims = shape_dist(rng); |
| input_dims = RandomShape(num_input_dims); |
| assert(input_dims.size() >= 2); |
| output_channels = dim_dist(rng); |
| input_channels = input_dims.back(); |
| kernel_dims = {output_channels, input_channels}; |
| output_dims = input_dims; |
| output_dims[output_dims.size() - 1] = output_channels; |
| |
| batch_size = NumElements(input_dims) / input_channels; |
| |
| input = std::vector<T>(XNN_EXTRA_BYTES / sizeof(T) + NumElements(input_dims)); |
| kernel = std::vector<T>(input_channels * output_channels); |
| bias = std::vector<BiasType>(output_channels); |
| operator_output = std::vector<T>(NumElements(output_dims)); |
| subgraph_output = std::vector<T>(operator_output.size()); |
| accumulators = std::vector<int32_t>(batch_size * output_channels); |
| } |
| |
| std::vector<size_t> RandomShape(size_t num_dims) |
| { |
| std::vector<size_t> dims(num_dims); |
| std::generate(dims.begin(), dims.end(), [&] { return dim_dist(rng); }); |
| return dims; |
| } |
| |
| size_t NumElements(std::vector<size_t>& dims) |
| { |
| return std::accumulate(dims.begin(), dims.end(), size_t(1), std::multiplies<size_t>()); |
| } |
| |
| 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> kernel_size_dist; |
| std::uniform_int_distribution<uint32_t> stride_dist; |
| std::uniform_int_distribution<int32_t> i32dist; |
| std::uniform_real_distribution<float> f32dist; |
| std::uniform_real_distribution<float> scale_dist; |
| std::uniform_int_distribution<size_t> dim_dist; |
| std::uniform_int_distribution<int32_t> i8dist; |
| std::uniform_int_distribution<int32_t> u8dist; |
| std::uniform_int_distribution<int32_t> w8dist; |
| |
| uint32_t batch_size; |
| size_t input_channels; |
| size_t output_channels; |
| |
| float output_min; |
| float output_max; |
| |
| std::vector<size_t> input_dims; |
| std::vector<size_t> kernel_dims; |
| std::vector<size_t> bias_dims; |
| std::vector<size_t> output_dims; |
| |
| std::vector<T> input; |
| std::vector<T> kernel; |
| std::vector<BiasType> bias; |
| std::vector<T> operator_output; |
| std::vector<T> subgraph_output; |
| std::vector<int32_t> accumulators; |
| }; |
| |
| template <class T> class QuantizedFullyConnectedTestBase : public FullyConnectedTestBase<T, int32_t> { |
| protected: |
| void initialize_accumulators_from_bias() |
| { |
| for (size_t i = 0; i < this->batch_size; i++) { |
| for (size_t oc = 0; oc < this->output_channels; oc++) { |
| this->accumulators[i * this->output_channels + oc] = this->bias[oc]; |
| } |
| } |
| } |
| }; |
| |
| using FullyConnectedTestQS8 = QuantizedFullyConnectedTestBase<int8_t>; |
| using FullyConnectedTestQU8 = QuantizedFullyConnectedTestBase<uint8_t>; |
| using FullyConnectedTestF32 = FullyConnectedTestBase<float>; |
| |
| TEST_F(FullyConnectedTestQS8, define) |
| { |
| ASSERT_EQ(xnn_status_success, xnn_initialize(/*allocator=*/nullptr)); |
| |
| xnn_subgraph_t subgraph = nullptr; |
| ASSERT_EQ(xnn_status_success, xnn_create_subgraph(4, /*flags=*/0, &subgraph)); |
| std::unique_ptr<xnn_subgraph, decltype(&xnn_delete_subgraph)> auto_subgraph(subgraph, xnn_delete_subgraph); |
| |
| uint32_t input_id = XNN_INVALID_NODE_ID; |
| ASSERT_EQ( |
| xnn_status_success, xnn_define_quantized_tensor_value( |
| subgraph, xnn_datatype_qint8, 0, 1.0f, input_dims.size(), input_dims.data(), nullptr, |
| /*external_id=*/0, /*flags=*/0, &input_id)); |
| ASSERT_NE(input_id, XNN_INVALID_NODE_ID); |
| |
| uint32_t kernel_id = XNN_INVALID_NODE_ID; |
| ASSERT_EQ( |
| xnn_status_success, xnn_define_quantized_tensor_value( |
| subgraph, xnn_datatype_qint8, 0, 1.0f, kernel_dims.size(), kernel_dims.data(), kernel.data(), |
| /*external_id=*/1, /*flags=*/0, &kernel_id)); |
| |
| uint32_t bias_id = XNN_INVALID_NODE_ID; |
| ASSERT_EQ( |
| xnn_status_success, xnn_define_quantized_tensor_value( |
| subgraph, xnn_datatype_qint32, 0, 1.0f, bias_dims.size(), bias_dims.data(), bias.data(), |
| /*external_id=*/2, /*flags=*/0, &bias_id)); |
| |
| uint32_t output_id = XNN_INVALID_NODE_ID; |
| ASSERT_EQ( |
| xnn_status_success, xnn_define_quantized_tensor_value( |
| subgraph, xnn_datatype_qint8, 0, 1.0f, output_dims.size(), output_dims.data(), nullptr, |
| /*external_id=*/3, /*flags=*/0, &output_id)); |
| ASSERT_NE(output_id, XNN_INVALID_NODE_ID); |
| |
| ASSERT_EQ( |
| xnn_status_success, |
| xnn_define_fully_connected(subgraph, output_min, output_max, input_id, kernel_id, bias_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_fully_connected); |
| ASSERT_EQ(node->compute_type, xnn_compute_type_qs8); |
| ASSERT_EQ(node->activation.output_min, output_min); |
| ASSERT_EQ(node->activation.output_max, output_max); |
| ASSERT_EQ(node->num_inputs, 3); |
| ASSERT_EQ(node->inputs[0], input_id); |
| ASSERT_EQ(node->inputs[1], kernel_id); |
| ASSERT_EQ(node->inputs[2], bias_id); |
| ASSERT_EQ(node->num_outputs, 1); |
| ASSERT_EQ(node->outputs[0], output_id); |
| ASSERT_EQ(node->flags, 0); |
| } |
| |
| TEST_F(FullyConnectedTestQU8, define) |
| { |
| ASSERT_EQ(xnn_status_success, xnn_initialize(/*allocator=*/nullptr)); |
| |
| xnn_subgraph_t subgraph = nullptr; |
| ASSERT_EQ(xnn_status_success, xnn_create_subgraph(4, /*flags=*/0, &subgraph)); |
| std::unique_ptr<xnn_subgraph, decltype(&xnn_delete_subgraph)> auto_subgraph(subgraph, xnn_delete_subgraph); |
| |
| uint32_t input_id = XNN_INVALID_NODE_ID; |
| ASSERT_EQ( |
| xnn_status_success, xnn_define_quantized_tensor_value( |
| subgraph, xnn_datatype_quint8, 0, 1.0f, input_dims.size(), input_dims.data(), nullptr, |
| /*external_id=*/0, /*flags=*/0, &input_id)); |
| ASSERT_NE(input_id, XNN_INVALID_NODE_ID); |
| |
| uint32_t kernel_id = XNN_INVALID_NODE_ID; |
| ASSERT_EQ( |
| xnn_status_success, xnn_define_quantized_tensor_value( |
| subgraph, xnn_datatype_quint8, 0, 1.0f, kernel_dims.size(), kernel_dims.data(), kernel.data(), |
| /*external_id=*/1, /*flags=*/0, &kernel_id)); |
| |
| uint32_t bias_id = XNN_INVALID_NODE_ID; |
| ASSERT_EQ( |
| xnn_status_success, xnn_define_quantized_tensor_value( |
| subgraph, xnn_datatype_qint32, 0, 1.0f, bias_dims.size(), bias_dims.data(), bias.data(), |
| /*external_id=*/2, /*flags=*/0, &bias_id)); |
| |
| uint32_t output_id = XNN_INVALID_NODE_ID; |
| ASSERT_EQ( |
| xnn_status_success, xnn_define_quantized_tensor_value( |
| subgraph, xnn_datatype_quint8, 0, 1.0f, output_dims.size(), output_dims.data(), nullptr, |
| /*external_id=*/3, /*flags=*/0, &output_id)); |
| ASSERT_NE(output_id, XNN_INVALID_NODE_ID); |
| |
| ASSERT_EQ( |
| xnn_status_success, xnn_define_fully_connected( |
| subgraph, output_min, output_max, input_id, kernel_id, bias_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_fully_connected); |
| ASSERT_EQ(node->compute_type, xnn_compute_type_qu8); |
| ASSERT_EQ(node->activation.output_min, output_min); |
| ASSERT_EQ(node->activation.output_max, output_max); |
| ASSERT_EQ(node->num_inputs, 3); |
| ASSERT_EQ(node->inputs[0], input_id); |
| ASSERT_EQ(node->inputs[1], kernel_id); |
| ASSERT_EQ(node->inputs[2], bias_id); |
| ASSERT_EQ(node->num_outputs, 1); |
| ASSERT_EQ(node->outputs[0], output_id); |
| ASSERT_EQ(node->flags, 0); |
| } |
| |
| TEST_F(FullyConnectedTestF32, define) |
| { |
| ASSERT_EQ(xnn_status_success, xnn_initialize(/*allocator=*/nullptr)); |
| |
| xnn_subgraph_t subgraph = nullptr; |
| ASSERT_EQ(xnn_status_success, xnn_create_subgraph(4, /*flags=*/0, &subgraph)); |
| std::unique_ptr<xnn_subgraph, decltype(&xnn_delete_subgraph)> auto_subgraph(subgraph, xnn_delete_subgraph); |
| |
| uint32_t 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=*/0, &input_id)); |
| ASSERT_NE(input_id, XNN_INVALID_NODE_ID); |
| |
| uint32_t kernel_id = XNN_INVALID_NODE_ID; |
| ASSERT_EQ( |
| xnn_status_success, |
| xnn_define_tensor_value( |
| subgraph, xnn_datatype_fp32, kernel_dims.size(), kernel_dims.data(), kernel.data(), /*external_id=*/1, |
| /*flags=*/0, &kernel_id)); |
| |
| uint32_t bias_id = XNN_INVALID_NODE_ID; |
| ASSERT_EQ( |
| xnn_status_success, xnn_define_tensor_value( |
| subgraph, xnn_datatype_fp32, bias_dims.size(), bias_dims.data(), bias.data(), |
| /*external_id=*/2, /*flags=*/0, &bias_id)); |
| |
| uint32_t 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=*/3, /*flags=*/0, &output_id)); |
| ASSERT_NE(output_id, XNN_INVALID_NODE_ID); |
| |
| ASSERT_EQ( |
| xnn_status_success, |
| xnn_define_fully_connected(subgraph, output_min, output_max, input_id, kernel_id, bias_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_fully_connected); |
| ASSERT_EQ(node->compute_type, xnn_compute_type_fp32); |
| ASSERT_EQ(node->activation.output_min, output_min); |
| ASSERT_EQ(node->activation.output_max, output_max); |
| ASSERT_EQ(node->num_inputs, 3); |
| ASSERT_EQ(node->inputs[0], input_id); |
| ASSERT_EQ(node->inputs[1], kernel_id); |
| ASSERT_EQ(node->inputs[2], bias_id); |
| ASSERT_EQ(node->num_outputs, 1); |
| ASSERT_EQ(node->outputs[0], output_id); |
| ASSERT_EQ(node->flags, 0); |
| } |
| |
| TEST_F(FullyConnectedTestQS8, matches_operator_api) |
| { |
| ASSERT_EQ(xnn_status_success, xnn_initialize(/*allocator=*/nullptr)); |
| |
| xnn_operator_t op = nullptr; |
| |
| std::generate(input.begin(), input.end(), [&]() { return i8dist(rng); }); |
| std::generate(kernel.begin(), kernel.end(), [&]() { return w8dist(rng); }); |
| std::generate(bias.begin(), bias.end(), [&]() { return i32dist(rng); }); |
| std::fill(operator_output.begin(), operator_output.end(), INT8_C(0xA5)); |
| std::fill(subgraph_output.begin(), subgraph_output.end(), INT8_C(0xA5)); |
| const int8_t input_zero_point = -1; |
| const float input_scale = scale_dist(rng); |
| const float kernel_scale = scale_dist(rng); |
| |
| // Compute reference results, without renormalization. |
| initialize_accumulators_from_bias(); |
| for (size_t i = 0; i < batch_size; i++) { |
| for (size_t oc = 0; oc < output_channels; oc++) { |
| for (size_t ic = 0; ic < input_channels; ic++) { |
| accumulators[i * output_channels + oc] += |
| (int32_t(input[i * input_channels + ic]) - int32_t(input_zero_point)) * |
| int32_t(kernel[oc * input_channels + ic]); |
| } |
| } |
| } |
| |
| // Compute renormalization parameters. |
| const int32_t accumulated_min = *std::min_element(accumulators.cbegin(), accumulators.cend()); |
| const int32_t accumulated_max = *std::max_element(accumulators.cbegin(), accumulators.cend()); |
| |
| float output_scale = double(uint32_t(accumulated_max - accumulated_min)) / 255.0; |
| int8_t output_zero_point = int8_t(std::max( |
| std::min( |
| lrint(-0.5 - 0.5 * double(accumulated_min + accumulated_max) / output_scale), |
| long(std::numeric_limits<int8_t>::max())), |
| long(std::numeric_limits<int8_t>::min()))); |
| const int8_t quantized_output_min = xnn_qs8_quantize(output_min, output_scale, output_zero_point); |
| const int8_t quantized_output_max = xnn_qs8_quantize(output_max, output_scale, output_zero_point); |
| |
| // Call operator API. |
| const xnn_status status = xnn_create_fully_connected_nc_qs8( |
| input_channels, output_channels, input_channels, output_channels, input_zero_point, input_scale, kernel_scale, |
| kernel.data(), bias.data(), output_zero_point, output_scale, quantized_output_min, quantized_output_max, |
| /*flags=*/0, nullptr, &op); |
| std::unique_ptr<xnn_operator, decltype(&xnn_delete_operator)> auto_op(op, xnn_delete_operator); |
| |
| if (status == xnn_status_unsupported_hardware) { |
| GTEST_SKIP(); |
| } |
| |
| ASSERT_EQ(xnn_status_success, status); |
| ASSERT_NE(nullptr, op); |
| ASSERT_EQ( |
| xnn_status_success, xnn_setup_fully_connected_nc_qs8( |
| op, batch_size, input.data(), operator_output.data(), |
| /*threadpool=*/nullptr)); |
| |
| 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(4, /*flags=*/0, &subgraph)); |
| std::unique_ptr<xnn_subgraph, decltype(&xnn_delete_subgraph)> auto_subgraph(subgraph, xnn_delete_subgraph); |
| |
| uint32_t input_id = XNN_INVALID_NODE_ID; |
| ASSERT_EQ( |
| xnn_status_success, xnn_define_quantized_tensor_value( |
| subgraph, xnn_datatype_qint8, input_zero_point, input_scale, input_dims.size(), |
| input_dims.data(), nullptr, /*external_id=*/0, XNN_VALUE_FLAG_EXTERNAL_INPUT, &input_id)); |
| ASSERT_NE(input_id, XNN_INVALID_NODE_ID); |
| |
| uint32_t kernel_id = XNN_INVALID_NODE_ID; |
| ASSERT_EQ( |
| xnn_status_success, xnn_define_quantized_tensor_value( |
| subgraph, xnn_datatype_qint8, 0, kernel_scale, kernel_dims.size(), kernel_dims.data(), |
| kernel.data(), /*external_id=*/1, /*flags=*/0, &kernel_id)); |
| |
| uint32_t bias_id = XNN_INVALID_NODE_ID; |
| ASSERT_EQ( |
| xnn_status_success, xnn_define_quantized_tensor_value( |
| subgraph, xnn_datatype_qint32, 0, kernel_scale, bias_dims.size(), bias_dims.data(), |
| bias.data(), /*external_id=*/2, /*flags=*/0, &bias_id)); |
| |
| uint32_t output_id = XNN_INVALID_NODE_ID; |
| ASSERT_EQ( |
| xnn_status_success, xnn_define_quantized_tensor_value( |
| subgraph, xnn_datatype_qint8, output_zero_point, output_scale, output_dims.size(), |
| output_dims.data(), nullptr, /*external_id=*/3, XNN_VALUE_FLAG_EXTERNAL_OUTPUT, &output_id)); |
| ASSERT_NE(output_id, XNN_INVALID_NODE_ID); |
| ASSERT_EQ( |
| xnn_status_success, |
| xnn_define_fully_connected(subgraph, output_min, output_max, input_id, kernel_id, bias_id, output_id, /*flags=*/0)); |
| |
| xnn_runtime_t runtime = nullptr; |
| 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)); |
| |
| // Check outputs match. |
| for (size_t i = 0; i < operator_output.size(); i++) { |
| ASSERT_EQ(subgraph_output[i], operator_output[i]); |
| } |
| } |
| |
| TEST_F(FullyConnectedTestQU8, matches_operator_api) |
| { |
| ASSERT_EQ(xnn_status_success, xnn_initialize(/*allocator=*/nullptr)); |
| |
| xnn_operator_t op = nullptr; |
| |
| std::generate(input.begin(), input.end(), [&]() { return u8dist(rng); }); |
| std::generate(kernel.begin(), kernel.end(), [&]() { return u8dist(rng); }); |
| std::generate(bias.begin(), bias.end(), [&]() { return i32dist(rng); }); |
| std::fill(operator_output.begin(), operator_output.end(), UINT8_C(0xA5)); |
| std::fill(subgraph_output.begin(), subgraph_output.end(), UINT8_C(0xA5)); |
| const uint8_t input_zero_point = u8dist(rng); |
| const uint8_t kernel_zero_point = 0; |
| const float input_scale = scale_dist(rng); |
| const float kernel_scale = scale_dist(rng); |
| |
| // Compute reference results, without renormalization. |
| initialize_accumulators_from_bias(); |
| for (size_t i = 0; i < batch_size; i++) { |
| for (size_t oc = 0; oc < output_channels; oc++) { |
| for (size_t ic = 0; ic < input_channels; ic++) { |
| accumulators[i * output_channels + oc] += |
| (int32_t(input[i * input_channels + ic]) - int32_t(input_zero_point)) * |
| (int32_t(kernel[oc * input_channels + ic]) - int32_t(kernel_zero_point)); |
| } |
| } |
| } |
| |
| // Compute renormalization parameters. |
| const int32_t accumulated_min = *std::min_element(accumulators.cbegin(), accumulators.cend()); |
| const int32_t accumulated_max = *std::max_element(accumulators.cbegin(), accumulators.cend()); |
| |
| const double output_scale = double(uint32_t(accumulated_max - accumulated_min)) / 255.0; |
| const uint8_t output_zero_point = uint8_t(std::max( |
| std::min( |
| lrint(127.5 - 0.5 * double(accumulated_min + accumulated_max) / output_scale), |
| long(std::numeric_limits<uint8_t>::max())), |
| long(std::numeric_limits<uint8_t>::min()))); |
| const uint8_t quantized_output_min = xnn_qu8_quantize(output_min, output_scale, output_zero_point); |
| const uint8_t quantized_output_max = xnn_qu8_quantize(output_max, output_scale, output_zero_point); |
| |
| // Call operator API. |
| const xnn_status status = xnn_create_fully_connected_nc_qu8( |
| input_channels, output_channels, input_channels, output_channels, input_zero_point, input_scale, kernel_zero_point, |
| kernel_scale, kernel.data(), bias.data(), output_zero_point, output_scale, quantized_output_min, |
| quantized_output_max, /*flags=*/0, nullptr, &op); |
| std::unique_ptr<xnn_operator, decltype(&xnn_delete_operator)> auto_op(op, xnn_delete_operator); |
| |
| if (status == xnn_status_unsupported_hardware) { |
| GTEST_SKIP(); |
| } |
| |
| ASSERT_EQ(xnn_status_success, status); |
| ASSERT_NE(nullptr, op); |
| ASSERT_EQ( |
| xnn_status_success, xnn_setup_fully_connected_nc_qu8( |
| op, batch_size, input.data(), operator_output.data(), |
| /*threadpool=*/nullptr)); |
| |
| 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(4, /*flags=*/0, &subgraph)); |
| std::unique_ptr<xnn_subgraph, decltype(&xnn_delete_subgraph)> auto_subgraph(subgraph, xnn_delete_subgraph); |
| |
| uint32_t input_id = XNN_INVALID_NODE_ID; |
| ASSERT_EQ( |
| xnn_status_success, xnn_define_quantized_tensor_value( |
| subgraph, xnn_datatype_quint8, input_zero_point, input_scale, input_dims.size(), |
| input_dims.data(), nullptr, /*external_id=*/0, XNN_VALUE_FLAG_EXTERNAL_INPUT, &input_id)); |
| ASSERT_NE(input_id, XNN_INVALID_NODE_ID); |
| |
| uint32_t kernel_id = XNN_INVALID_NODE_ID; |
| ASSERT_EQ( |
| xnn_status_success, xnn_define_quantized_tensor_value( |
| subgraph, xnn_datatype_quint8, 0, kernel_scale, kernel_dims.size(), kernel_dims.data(), |
| kernel.data(), /*external_id=*/1, /*flags=*/0, &kernel_id)); |
| |
| uint32_t bias_id = XNN_INVALID_NODE_ID; |
| ASSERT_EQ( |
| xnn_status_success, xnn_define_quantized_tensor_value( |
| subgraph, xnn_datatype_qint32, 0, kernel_scale, bias_dims.size(), bias_dims.data(), |
| bias.data(), /*external_id=*/2, /*flags=*/0, &bias_id)); |
| |
| uint32_t output_id = XNN_INVALID_NODE_ID; |
| ASSERT_EQ( |
| xnn_status_success, xnn_define_quantized_tensor_value( |
| subgraph, xnn_datatype_quint8, output_zero_point, output_scale, output_dims.size(), |
| output_dims.data(), nullptr, /*external_id=*/3, XNN_VALUE_FLAG_EXTERNAL_OUTPUT, &output_id)); |
| ASSERT_NE(output_id, XNN_INVALID_NODE_ID); |
| ASSERT_EQ( |
| xnn_status_success, |
| xnn_define_fully_connected(subgraph, output_min, output_max, input_id, kernel_id, bias_id, output_id, /*flags=*/0)); |
| |
| xnn_runtime_t runtime = nullptr; |
| 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)); |
| |
| // Check outputs match. |
| for (size_t i = 0; i < operator_output.size(); i++) { |
| ASSERT_EQ(subgraph_output[i], operator_output[i]); |
| } |
| } |
| |
| TEST_F(FullyConnectedTestF32, matches_operator_api) |
| { |
| ASSERT_EQ(xnn_status_success, xnn_initialize(/*allocator=*/nullptr)); |
| |
| xnn_operator_t op = nullptr; |
| |
| std::generate(input.begin(), input.end(), [&]() { return f32dist(rng); }); |
| std::generate(kernel.begin(), kernel.end(), [&]() { return f32dist(rng); }); |
| std::generate(bias.begin(), bias.end(), [&]() { return f32dist(rng); }); |
| std::fill(operator_output.begin(), operator_output.end(), nanf("")); |
| std::fill(subgraph_output.begin(), subgraph_output.end(), nanf("")); |
| |
| // Call operator API. |
| const xnn_status status = xnn_create_fully_connected_nc_f32( |
| input_channels, output_channels, input_channels, output_channels, kernel.data(), bias.data(), output_min, |
| output_max, |
| /*flags=*/0, nullptr, &op); |
| std::unique_ptr<xnn_operator, decltype(&xnn_delete_operator)> auto_op(op, xnn_delete_operator); |
| |
| if (status == xnn_status_unsupported_hardware) { |
| GTEST_SKIP(); |
| } |
| |
| ASSERT_EQ(xnn_status_success, status); |
| ASSERT_NE(nullptr, op); |
| ASSERT_EQ( |
| xnn_status_success, xnn_setup_fully_connected_nc_f32( |
| op, batch_size, input.data(), operator_output.data(), |
| /*threadpool=*/nullptr)); |
| |
| 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(4, /*flags=*/0, &subgraph)); |
| std::unique_ptr<xnn_subgraph, decltype(&xnn_delete_subgraph)> auto_subgraph(subgraph, xnn_delete_subgraph); |
| |
| uint32_t 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, XNN_VALUE_FLAG_EXTERNAL_INPUT, &input_id)); |
| ASSERT_NE(input_id, XNN_INVALID_NODE_ID); |
| |
| uint32_t kernel_id = XNN_INVALID_NODE_ID; |
| ASSERT_EQ( |
| xnn_status_success, xnn_define_tensor_value( |
| subgraph, xnn_datatype_fp32, kernel_dims.size(), kernel_dims.data(), kernel.data(), |
| /*external_id=*/1, /*flags=*/0, &kernel_id)); |
| |
| uint32_t bias_id = XNN_INVALID_NODE_ID; |
| ASSERT_EQ( |
| xnn_status_success, xnn_define_tensor_value( |
| subgraph, xnn_datatype_fp32, bias_dims.size(), bias_dims.data(), bias.data(), |
| /*external_id=*/2, /*flags=*/0, &bias_id)); |
| |
| uint32_t 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=*/3, XNN_VALUE_FLAG_EXTERNAL_OUTPUT, &output_id)); |
| ASSERT_NE(output_id, XNN_INVALID_NODE_ID); |
| ASSERT_EQ( |
| xnn_status_success, |
| xnn_define_fully_connected(subgraph, output_min, output_max, input_id, kernel_id, bias_id, output_id, /*flags=*/0)); |
| |
| xnn_runtime_t runtime = nullptr; |
| 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)); |
| |
| // Check outputs match. |
| for (size_t i = 0; i < operator_output.size(); i++) { |
| ASSERT_EQ(subgraph_output[i], operator_output[i]); |
| } |
| } |