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 <stddef.h>
7 #include <stdint.h>
8
9 #include <algorithm>
10 #include <array>
11 #include <functional>
12 #include <limits>
13 #include <memory>
14 #include <numeric>
15 #include <random>
16 #include <vector>
17
18 #include <xnnpack.h>
19 #include <xnnpack/node-type.h>
20 #include <xnnpack/operator.h>
21 #include <xnnpack/subgraph.h>
22
23 #include "subgraph-binary-tester.h"
24 #include <gtest/gtest.h>
25
26 using SquaredDifferenceTestF32 = BinaryTest<float>;
27
TEST_F(SquaredDifferenceTestF32,define)28 TEST_F(SquaredDifferenceTestF32, define)
29 {
30 ASSERT_EQ(xnn_status_success, xnn_initialize(/*allocator=*/nullptr));
31
32 xnn_subgraph_t subgraph = nullptr;
33 ASSERT_EQ(xnn_status_success, xnn_create_subgraph(3, /*flags=*/0, &subgraph));
34 std::unique_ptr<xnn_subgraph, decltype(&xnn_delete_subgraph)> auto_subgraph(subgraph, xnn_delete_subgraph);
35
36 uint32_t input1_id = XNN_INVALID_NODE_ID;
37 ASSERT_EQ(
38 xnn_status_success, xnn_define_tensor_value(
39 subgraph, xnn_datatype_fp32, input1_dims.size(), input1_dims.data(), nullptr,
40 /*external_id=*/0, /*flags=*/0, &input1_id));
41 ASSERT_NE(input1_id, XNN_INVALID_NODE_ID);
42
43 uint32_t input2_id = XNN_INVALID_NODE_ID;
44 ASSERT_EQ(
45 xnn_status_success, xnn_define_tensor_value(
46 subgraph, xnn_datatype_fp32, input2_dims.size(), input2_dims.data(), nullptr,
47 /*external_id=*/0, /*flags=*/0, &input2_id));
48 ASSERT_NE(input2_id, XNN_INVALID_NODE_ID);
49
50 uint32_t output_id = XNN_INVALID_NODE_ID;
51 ASSERT_EQ(
52 xnn_status_success,
53 xnn_define_tensor_value(
54 subgraph, xnn_datatype_fp32, output_dims.size(), output_dims.data(), nullptr, XNN_INVALID_VALUE_ID, /*flags=*/0, &output_id));
55 ASSERT_NE(output_id, XNN_INVALID_NODE_ID);
56
57 ASSERT_EQ(
58 xnn_status_success,
59 xnn_define_squared_difference(subgraph, input1_id, input2_id, output_id, /*flags=*/0));
60
61 ASSERT_EQ(subgraph->num_nodes, 1);
62 const struct xnn_node* node = &subgraph->nodes[0];
63 ASSERT_EQ(node->type, xnn_node_type_squared_difference);
64 ASSERT_EQ(node->compute_type, xnn_compute_type_fp32);
65 ASSERT_EQ(node->num_inputs, 2);
66 ASSERT_EQ(node->inputs[0], input1_id);
67 ASSERT_EQ(node->inputs[1], input2_id);
68 ASSERT_EQ(node->num_outputs, 1);
69 ASSERT_EQ(node->outputs[0], output_id);
70 ASSERT_EQ(node->flags, 0);
71 }
72
TEST_F(SquaredDifferenceTestF32,matches_operator_api)73 TEST_F(SquaredDifferenceTestF32, matches_operator_api)
74 {
75 std::generate(input1.begin(), input1.end(), [&]() { return f32dist(rng); });
76 std::generate(input2.begin(), input2.end(), [&]() { return f32dist(rng); });
77 std::fill(operator_output.begin(), operator_output.end(), nanf(""));
78 std::fill(subgraph_output.begin(), subgraph_output.end(), nanf(""));
79
80 ASSERT_EQ(xnn_status_success, xnn_initialize(/*allocator=*/nullptr));
81
82 xnn_operator_t op = nullptr;
83
84 // Call operator API.
85 ASSERT_EQ(xnn_status_success, xnn_create_squared_difference_nd_f32(0, &op));
86 std::unique_ptr<xnn_operator, decltype(&xnn_delete_operator)> auto_op(op, xnn_delete_operator);
87
88 ASSERT_EQ(
89 xnn_status_success, xnn_setup_squared_difference_nd_f32(
90 op, input1_dims.size(), input1_dims.data(), input2_dims.size(), input2_dims.data(),
91 input1.data(), input2.data(), operator_output.data(), nullptr /* thread pool */));
92
93 ASSERT_EQ(xnn_status_success, xnn_run_operator(op, nullptr /* thread pool */));
94
95 // Call subgraph API.
96 xnn_subgraph_t subgraph = nullptr;
97 ASSERT_EQ(xnn_status_success, xnn_create_subgraph(3, /*flags=*/0, &subgraph));
98 std::unique_ptr<xnn_subgraph, decltype(&xnn_delete_subgraph)> auto_subgraph(subgraph, xnn_delete_subgraph);
99
100 uint32_t input1_id = XNN_INVALID_NODE_ID;
101 ASSERT_EQ(
102 xnn_status_success, xnn_define_tensor_value(
103 subgraph, xnn_datatype_fp32, input1_dims.size(), input1_dims.data(), nullptr,
104 /*external_id=*/0, /*flags=*/XNN_VALUE_FLAG_EXTERNAL_INPUT, &input1_id));
105 ASSERT_NE(input1_id, XNN_INVALID_NODE_ID);
106
107 uint32_t input2_id = XNN_INVALID_NODE_ID;
108 ASSERT_EQ(
109 xnn_status_success, xnn_define_tensor_value(
110 subgraph, xnn_datatype_fp32, input2_dims.size(), input2_dims.data(), nullptr,
111 /*external_id=*/1, /*flags=*/XNN_VALUE_FLAG_EXTERNAL_INPUT, &input2_id));
112 ASSERT_NE(input2_id, XNN_INVALID_NODE_ID);
113
114 uint32_t output_id = XNN_INVALID_NODE_ID;
115 ASSERT_EQ(
116 xnn_status_success,
117 xnn_define_tensor_value(
118 subgraph, xnn_datatype_fp32, output_dims.size(), output_dims.data(), nullptr, /*external_id=*/2,
119 /*flags=*/XNN_VALUE_FLAG_EXTERNAL_OUTPUT, &output_id));
120 ASSERT_NE(output_id, XNN_INVALID_NODE_ID);
121
122 ASSERT_EQ(
123 xnn_status_success,
124 xnn_define_squared_difference(subgraph, input1_id, input2_id, output_id, /*flags=*/0));
125
126 xnn_runtime_t runtime = nullptr;
127 ASSERT_EQ(xnn_status_success, xnn_create_runtime_v3(subgraph, nullptr, nullptr, /*flags=*/0, &runtime));
128 ASSERT_NE(nullptr, runtime);
129 std::unique_ptr<xnn_runtime, decltype(&xnn_delete_runtime)> auto_runtime(runtime, xnn_delete_runtime);
130 std::array<xnn_external_value, 3> external = {
131 xnn_external_value{input1_id, input1.data()}, xnn_external_value{input2_id, input2.data()},
132 xnn_external_value{output_id, subgraph_output.data()}};
133 ASSERT_EQ(xnn_status_success, xnn_setup_runtime(runtime, external.size(), external.data()));
134 ASSERT_EQ(xnn_status_success, xnn_invoke_runtime(runtime));
135
136 // Check outputs match.
137 ASSERT_EQ(subgraph_output, operator_output);
138 }
139