1 // Copyright 2022 Google LLC
2 //
3 // This source code is licensed under the BSD-style license found in the
4 // LICENSE file in the root directory of this source tree.
5
6 #include <algorithm>
7 #include <array>
8 #include <cstdint>
9 #include <cstddef>
10 #include <limits>
11 #include <memory>
12 #include <random>
13
14 #include <xnnpack.h>
15 #include <xnnpack/node-type.h>
16 #include <xnnpack/operator.h>
17 #include <xnnpack/subgraph.h>
18
19 #include "subgraph-unary-tester.h"
20 #include <gtest/gtest.h>
21
22 using SquareRootTestF32 = UnaryTest<float>;
23
TEST_F(SquareRootTestF32,define)24 TEST_F(SquareRootTestF32, define)
25 {
26 ASSERT_EQ(xnn_status_success, xnn_initialize(/*allocator=*/nullptr));
27
28 xnn_subgraph_t subgraph = nullptr;
29 ASSERT_EQ(xnn_status_success, xnn_create_subgraph(/*external_value_ids=*/2, /*flags=*/0, &subgraph));
30 std::unique_ptr<xnn_subgraph, decltype(&xnn_delete_subgraph)> auto_subgraph(subgraph, xnn_delete_subgraph);
31
32 input_id = XNN_INVALID_NODE_ID;
33 ASSERT_EQ(
34 xnn_status_success, xnn_define_tensor_value(
35 subgraph, xnn_datatype_fp32, dims.size(), dims.data(), nullptr, 0,
36 /*flags=*/XNN_VALUE_FLAG_EXTERNAL_INPUT, &input_id));
37 ASSERT_NE(input_id, XNN_INVALID_NODE_ID);
38
39 output_id = XNN_INVALID_NODE_ID;
40 ASSERT_EQ(
41 xnn_status_success, xnn_define_tensor_value(
42 subgraph, xnn_datatype_fp32, dims.size(), dims.data(), nullptr, 1,
43 /*flags=*/XNN_VALUE_FLAG_EXTERNAL_OUTPUT, &output_id));
44 ASSERT_NE(output_id, XNN_INVALID_NODE_ID);
45
46 ASSERT_EQ(xnn_status_success, xnn_define_square_root(subgraph, input_id, output_id, /*flags=*/0));
47
48 ASSERT_EQ(subgraph->num_nodes, 1);
49 const struct xnn_node* node = &subgraph->nodes[0];
50 ASSERT_EQ(node->type, xnn_node_type_square_root);
51 ASSERT_EQ(node->compute_type, xnn_compute_type_fp32);
52 ASSERT_EQ(node->num_inputs, 1);
53 ASSERT_EQ(node->inputs[0], input_id);
54 ASSERT_EQ(node->num_outputs, 1);
55 ASSERT_EQ(node->outputs[0], output_id);
56 ASSERT_EQ(node->flags, 0);
57 }
58
TEST_F(SquareRootTestF32,matches_operator_api)59 TEST_F(SquareRootTestF32, matches_operator_api)
60 {
61 std::uniform_real_distribution<float> f32dist(0.1f, 5.0f);
62 std::generate(input.begin(), input.end(), [&]() { return f32dist(rng); });
63 std::fill(operator_output.begin(), operator_output.end(), nanf(""));
64 std::fill(subgraph_output.begin(), subgraph_output.end(), nanf(""));
65
66 ASSERT_EQ(xnn_status_success, xnn_initialize(/*allocator=*/nullptr));
67
68 // Call operator API.
69 xnn_operator_t op = nullptr;
70 const xnn_status status =
71 xnn_create_square_root_nc_f32(channels, channels, channels, /*flags=*/0, &op);
72 if (status == xnn_status_unsupported_hardware) {
73 GTEST_SKIP();
74 }
75
76 ASSERT_EQ(xnn_status_success, status);
77 ASSERT_NE(nullptr, op);
78 std::unique_ptr<xnn_operator, decltype(&xnn_delete_operator)> auto_op(op, xnn_delete_operator);
79
80 ASSERT_EQ(
81 xnn_status_success,
82 xnn_setup_square_root_nc_f32(op, batch_size, input.data(), operator_output.data(), /*threadpool=*/nullptr));
83
84 ASSERT_EQ(xnn_status_success, xnn_run_operator(op, /*threadpool=*/nullptr));
85
86 // Call subgraph API.
87 xnn_subgraph_t subgraph = nullptr;
88 ASSERT_EQ(xnn_status_success, xnn_create_subgraph(/*external_value_ids=*/2, /*flags=*/0, &subgraph));
89 std::unique_ptr<xnn_subgraph, decltype(&xnn_delete_subgraph)> auto_subgraph(subgraph, xnn_delete_subgraph);
90 input_id = XNN_INVALID_NODE_ID;
91 ASSERT_EQ(
92 xnn_status_success, xnn_define_tensor_value(
93 subgraph, xnn_datatype_fp32, dims.size(), dims.data(), nullptr, /*external_id=*/0,
94 /*flags=*/XNN_VALUE_FLAG_EXTERNAL_INPUT, &input_id));
95 ASSERT_NE(input_id, XNN_INVALID_NODE_ID);
96
97 output_id = XNN_INVALID_NODE_ID;
98 ASSERT_EQ(
99 xnn_status_success, xnn_define_tensor_value(
100 subgraph, xnn_datatype_fp32, dims.size(), dims.data(), nullptr, /*external_id=*/1,
101 /*flags=*/XNN_VALUE_FLAG_EXTERNAL_OUTPUT, &output_id));
102 ASSERT_NE(output_id, XNN_INVALID_NODE_ID);
103
104 xnn_runtime_t runtime = nullptr;
105 ASSERT_EQ(xnn_status_success, xnn_define_square_root(subgraph, input_id, output_id, /*flags=*/0));
106 ASSERT_EQ(xnn_status_success, xnn_create_runtime_v3(subgraph, nullptr, nullptr, /*flags=*/0, &runtime));
107 ASSERT_NE(nullptr, runtime);
108 std::unique_ptr<xnn_runtime, decltype(&xnn_delete_runtime)> auto_runtime(runtime, xnn_delete_runtime);
109 std::array<xnn_external_value, 2> external = {
110 xnn_external_value{input_id, input.data()}, xnn_external_value{output_id, subgraph_output.data()}};
111 ASSERT_EQ(xnn_status_success, xnn_setup_runtime(runtime, external.size(), external.data()));
112 ASSERT_EQ(xnn_status_success, xnn_invoke_runtime(runtime));
113
114 ASSERT_EQ(subgraph_output, operator_output);
115 }
116