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 <cstddef>
9 #include <cstdint>
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 CeilingTestF32 = UnaryTest<float>;
23
TEST_F(CeilingTestF32,define)24 TEST_F(CeilingTestF32, 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_ceiling(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_ceiling);
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(CeilingTestF32,matches_operator_api)59 TEST_F(CeilingTestF32, matches_operator_api)
60 {
61 std::uniform_real_distribution<float> f32dist(-5.0f, -0.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 = xnn_create_ceiling_nc_f32(channels, channels, channels, /*flags=*/0, &op);
71 if (status == xnn_status_unsupported_hardware) {
72 GTEST_SKIP();
73 }
74
75 ASSERT_EQ(xnn_status_success, status);
76 ASSERT_NE(nullptr, op);
77 std::unique_ptr<xnn_operator, decltype(&xnn_delete_operator)> auto_op(op, xnn_delete_operator);
78
79 ASSERT_EQ(
80 xnn_status_success,
81 xnn_setup_ceiling_nc_f32(op, batch_size, input.data(), operator_output.data(), /*threadpool=*/nullptr));
82
83 ASSERT_EQ(xnn_status_success, xnn_run_operator(op, /*threadpool=*/nullptr));
84
85 // Call subgraph API.
86 xnn_subgraph_t subgraph = nullptr;
87 ASSERT_EQ(xnn_status_success, xnn_create_subgraph(/*external_value_ids=*/2, /*flags=*/0, &subgraph));
88 std::unique_ptr<xnn_subgraph, decltype(&xnn_delete_subgraph)> auto_subgraph(subgraph, xnn_delete_subgraph);
89 input_id = XNN_INVALID_NODE_ID;
90 ASSERT_EQ(
91 xnn_status_success, xnn_define_tensor_value(
92 subgraph, xnn_datatype_fp32, dims.size(), dims.data(), nullptr, /*external_id=*/0,
93 /*flags=*/XNN_VALUE_FLAG_EXTERNAL_INPUT, &input_id));
94 ASSERT_NE(input_id, XNN_INVALID_NODE_ID);
95
96 output_id = XNN_INVALID_NODE_ID;
97 ASSERT_EQ(
98 xnn_status_success, xnn_define_tensor_value(
99 subgraph, xnn_datatype_fp32, dims.size(), dims.data(), nullptr, /*external_id=*/1,
100 /*flags=*/XNN_VALUE_FLAG_EXTERNAL_OUTPUT, &output_id));
101 ASSERT_NE(output_id, XNN_INVALID_NODE_ID);
102
103 xnn_runtime_t runtime = nullptr;
104 ASSERT_EQ(xnn_status_success, xnn_define_ceiling(subgraph, input_id, output_id, /*flags=*/0));
105 ASSERT_EQ(xnn_status_success, xnn_create_runtime_v3(subgraph, nullptr, nullptr, /*flags=*/0, &runtime));
106 ASSERT_NE(nullptr, runtime);
107 std::unique_ptr<xnn_runtime, decltype(&xnn_delete_runtime)> auto_runtime(runtime, xnn_delete_runtime);
108 std::array<xnn_external_value, 2> external = {
109 xnn_external_value{input_id, input.data()}, xnn_external_value{output_id, subgraph_output.data()}};
110 ASSERT_EQ(xnn_status_success, xnn_setup_runtime(runtime, external.size(), external.data()));
111 ASSERT_EQ(xnn_status_success, xnn_invoke_runtime(runtime));
112
113 ASSERT_EQ(subgraph_output, operator_output);
114 }
115