• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 SoftmaxTestF32 = UnaryTest<float, /*OutputType=*/float, /*min_dim=*/1>;
23 
TEST_F(SoftmaxTestF32,define)24 TEST_F(SoftmaxTestF32, 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_softmax(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_softmax);
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(SoftmaxTestF32,matches_operator_api)59 TEST_F(SoftmaxTestF32, matches_operator_api)
60 {
61   // Choose such range that expf(x[i]) overflows, but expf(x[i] - x_max) doesn't.
62   // However, the range is still narrow enough that single-precision exp doesn't overflow.
63   std::uniform_real_distribution<float> f32dist(90.0f, 100.0f);
64   std::generate(input.begin(), input.end(), [&]() { return f32dist(rng); });
65   std::fill(operator_output.begin(), operator_output.end(), nanf(""));
66   std::fill(subgraph_output.begin(), subgraph_output.end(), nanf(""));
67 
68   ASSERT_EQ(xnn_status_success, xnn_initialize(/*allocator=*/nullptr));
69 
70   // Call operator API.
71   xnn_operator_t op = nullptr;
72   const xnn_status status = xnn_create_softmax_nc_f32(channels, channels, channels, /*flags=*/0, &op);
73   if (status == xnn_status_unsupported_hardware) {
74     GTEST_SKIP();
75   }
76 
77   ASSERT_EQ(xnn_status_success, status);
78   ASSERT_NE(nullptr, op);
79   std::unique_ptr<xnn_operator, decltype(&xnn_delete_operator)> auto_op(op, xnn_delete_operator);
80 
81   ASSERT_EQ(
82     xnn_status_success,
83     xnn_setup_softmax_nc_f32(op, batch_size, input.data(), operator_output.data(), /*threadpool=*/nullptr));
84 
85   ASSERT_EQ(xnn_status_success, xnn_run_operator(op, /*threadpool=*/nullptr));
86 
87   // Call subgraph API.
88   xnn_subgraph_t subgraph = nullptr;
89   ASSERT_EQ(xnn_status_success, xnn_create_subgraph(/*external_value_ids=*/2, /*flags=*/0, &subgraph));
90   std::unique_ptr<xnn_subgraph, decltype(&xnn_delete_subgraph)> auto_subgraph(subgraph, xnn_delete_subgraph);
91   input_id = XNN_INVALID_NODE_ID;
92   ASSERT_EQ(
93     xnn_status_success, xnn_define_tensor_value(
94                           subgraph, xnn_datatype_fp32, dims.size(), dims.data(), nullptr, /*external_id=*/0,
95                           /*flags=*/XNN_VALUE_FLAG_EXTERNAL_INPUT, &input_id));
96   ASSERT_NE(input_id, XNN_INVALID_NODE_ID);
97 
98   output_id = XNN_INVALID_NODE_ID;
99   ASSERT_EQ(
100     xnn_status_success, xnn_define_tensor_value(
101                           subgraph, xnn_datatype_fp32, dims.size(), dims.data(), nullptr, /*external_id=*/1,
102                           /*flags=*/XNN_VALUE_FLAG_EXTERNAL_OUTPUT, &output_id));
103   ASSERT_NE(output_id, XNN_INVALID_NODE_ID);
104 
105   xnn_runtime_t runtime = nullptr;
106   ASSERT_EQ(xnn_status_success, xnn_define_softmax(subgraph, input_id, output_id, /*flags=*/0));
107   ASSERT_EQ(xnn_status_success, xnn_create_runtime_v3(subgraph, nullptr, nullptr, /*flags=*/0, &runtime));
108   ASSERT_NE(nullptr, runtime);
109   std::unique_ptr<xnn_runtime, decltype(&xnn_delete_runtime)> auto_runtime(runtime, xnn_delete_runtime);
110   std::array<xnn_external_value, 2> external = {
111     xnn_external_value{input_id, input.data()}, xnn_external_value{output_id, subgraph_output.data()}};
112   ASSERT_EQ(xnn_status_success, xnn_setup_runtime(runtime, external.size(), external.data()));
113   ASSERT_EQ(xnn_status_success, xnn_invoke_runtime(runtime));
114 
115   ASSERT_EQ(subgraph_output, operator_output);
116 }
117