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