1 // Copyright 2020 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 <math.h>
7 #include <stddef.h>
8 #include <stdint.h>
9 
10 #include <xnnpack.h>
11 #include <xnnpack/log.h>
12 #include <xnnpack/params.h>
13 #include <xnnpack/subgraph.h>
14 
15 
xnn_define_global_average_pooling_2d(xnn_subgraph_t subgraph,float output_min,float output_max,uint32_t input_id,uint32_t output_id,uint32_t flags)16 enum xnn_status xnn_define_global_average_pooling_2d(
17   xnn_subgraph_t subgraph,
18   float output_min,
19   float output_max,
20   uint32_t input_id,
21   uint32_t output_id,
22   uint32_t flags)
23 {
24   if ((xnn_params.init_flags & XNN_INIT_FLAG_XNNPACK) == 0) {
25     xnn_log_error("failed to define %s operator: XNNPACK is not initialized",
26       xnn_node_type_to_string(xnn_node_type_global_average_pooling_2d));
27     return xnn_status_uninitialized;
28   }
29 
30   if (isnan(output_min)) {
31     xnn_log_error(
32       "failed to define %s operator with NaN output lower bound: lower bound must be non-NaN",
33       xnn_node_type_to_string(xnn_node_type_global_average_pooling_2d));
34     return xnn_status_invalid_parameter;
35   }
36 
37   if (isnan(output_max)) {
38     xnn_log_error(
39       "failed to define %s operator with NaN output upper bound: upper bound must be non-NaN",
40       xnn_node_type_to_string(xnn_node_type_global_average_pooling_2d));
41     return xnn_status_invalid_parameter;
42   }
43 
44   if (output_min >= output_max) {
45     xnn_log_error(
46       "failed to define %s operator with [%.7g, %.7g] output range: lower bound must be below upper bound",
47       xnn_node_type_to_string(xnn_node_type_global_average_pooling_2d), output_min, output_max);
48     return xnn_status_invalid_parameter;
49   }
50 
51   if (input_id >= subgraph->num_values) {
52     xnn_log_error(
53       "failed to define %s operator with input ID #%" PRIu32 ": invalid Value ID",
54       xnn_node_type_to_string(xnn_node_type_global_average_pooling_2d), input_id);
55     return xnn_status_invalid_parameter;
56   }
57 
58   if (output_id >= subgraph->num_values) {
59     xnn_log_error(
60       "failed to define %s operator with output ID #%" PRIu32 ": invalid Value ID",
61       xnn_node_type_to_string(xnn_node_type_global_average_pooling_2d), output_id);
62     return xnn_status_invalid_parameter;
63   }
64 
65   struct xnn_node* node = xnn_subgraph_new_node(subgraph);
66   if (node == NULL) {
67     return xnn_status_out_of_memory;
68   }
69 
70   node->type = xnn_node_type_global_average_pooling_2d;
71   node->activation.output_min = output_min;
72   node->activation.output_max = output_max;
73   node->num_inputs = 1;
74   node->inputs[0] = input_id;
75   node->num_outputs = 1;
76   node->outputs[0] = output_id;
77   node->flags = flags;
78 
79   return xnn_status_success;
80 }
81