| // 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 <numeric> |
| #include <random> |
| |
| #include <xnnpack.h> |
| #include <xnnpack/node-type.h> |
| #include <xnnpack/operator.h> |
| #include <xnnpack/subgraph.h> |
| |
| #include <gtest/gtest.h> |
| |
| class PreluTestF32 : public ::testing::Test { |
| protected: |
| void SetUp() override |
| { |
| random_device = std::unique_ptr<std::random_device>(new std::random_device()); |
| rng = std::mt19937((*random_device)()); |
| dim_dist = std::uniform_int_distribution<size_t>(1, 9); |
| input_dims = RandomShape(4); |
| output_dims = input_dims; |
| batch_size = input_dims[0] * input_dims[1] * input_dims[2]; |
| channels = input_dims[3]; |
| slope_dims = {channels}; |
| input = std::vector<float>(XNN_EXTRA_BYTES / sizeof(float) + NumElements(input_dims)); |
| slope = std::vector<float>(channels); |
| operator_output = std::vector<float>(NumElements(output_dims)); |
| subgraph_output = std::vector<float>(operator_output.size()); |
| } |
| |
| 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<size_t> dim_dist; |
| |
| std::vector<size_t> output_dims; |
| std::vector<size_t> input_dims; |
| std::vector<size_t> slope_dims; |
| std::vector<float> input; |
| std::vector<float> slope; |
| std::vector<float> operator_output; |
| std::vector<float> subgraph_output; |
| size_t channels; |
| size_t batch_size; |
| }; |
| |
| TEST_F(PreluTestF32, 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=*/3, /*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, 0, |
| /*flags=*/XNN_VALUE_FLAG_EXTERNAL_INPUT, &input_id)); |
| ASSERT_NE(input_id, XNN_INVALID_NODE_ID); |
| |
| uint32_t slope_id = XNN_INVALID_NODE_ID; |
| ASSERT_EQ( |
| xnn_status_success, xnn_define_tensor_value( |
| subgraph, xnn_datatype_fp32, slope_dims.size(), slope_dims.data(), slope.data(), 1, |
| /*flags=*/0, &slope_id)); |
| ASSERT_NE(slope_id, XNN_INVALID_NODE_ID); |
| |
| uint32_t output_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, 2, |
| /*flags=*/XNN_VALUE_FLAG_EXTERNAL_OUTPUT, &output_id)); |
| ASSERT_NE(output_id, XNN_INVALID_NODE_ID); |
| |
| ASSERT_EQ(xnn_status_success, xnn_define_prelu(subgraph, input_id, slope_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_prelu); |
| ASSERT_EQ(node->compute_type, xnn_compute_type_fp32); |
| ASSERT_EQ(node->num_inputs, 2); |
| ASSERT_EQ(node->inputs[0], input_id); |
| ASSERT_EQ(node->inputs[1], slope_id); |
| ASSERT_EQ(node->num_outputs, 1); |
| ASSERT_EQ(node->outputs[0], output_id); |
| ASSERT_EQ(node->flags, 0); |
| } |
| |
| TEST_F(PreluTestF32, matches_operator_api) |
| { |
| std::uniform_real_distribution<float> f32idist(-1.0f, 1.0f); |
| std::uniform_real_distribution<float> f32wdist(0.25f, 0.75f); |
| std::generate(input.begin(), input.end(), [&]() { return f32idist(rng); }); |
| std::generate(slope.begin(), slope.end(), [&]() { return f32wdist(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_prelu_nc_f32(channels, channels, channels, slope.data(), /*flags=*/0, nullptr, &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_setup_prelu_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(/*external_value_ids=*/3, /*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=*/XNN_VALUE_FLAG_EXTERNAL_INPUT, &input_id)); |
| ASSERT_NE(input_id, XNN_INVALID_NODE_ID); |
| |
| uint32_t slope_id = XNN_INVALID_NODE_ID; |
| ASSERT_EQ( |
| xnn_status_success, |
| xnn_define_tensor_value( |
| subgraph, xnn_datatype_fp32, slope_dims.size(), slope_dims.data(), slope.data(), /*external_id=*/1, |
| /*flags=*/0, &slope_id)); |
| ASSERT_NE(slope_id, XNN_INVALID_NODE_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=*/2, |
| /*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_prelu(subgraph, input_id, slope_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); |
| } |