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_argmax_pooling_2d(xnn_subgraph_t subgraph,uint32_t input_padding_top,uint32_t input_padding_right,uint32_t input_padding_bottom,uint32_t input_padding_left,uint32_t pooling_height,uint32_t pooling_width,uint32_t input_id,uint32_t output_value_id,uint32_t output_index_id,uint32_t flags)16 enum xnn_status xnn_define_argmax_pooling_2d(
17   xnn_subgraph_t subgraph,
18   uint32_t input_padding_top,
19   uint32_t input_padding_right,
20   uint32_t input_padding_bottom,
21   uint32_t input_padding_left,
22   uint32_t pooling_height,
23   uint32_t pooling_width,
24   uint32_t input_id,
25   uint32_t output_value_id,
26   uint32_t output_index_id,
27   uint32_t flags)
28 {
29   if ((xnn_params.init_flags & XNN_INIT_FLAG_XNNPACK) == 0) {
30     xnn_log_error("failed to define %s operator: XNNPACK is not initialized",
31       xnn_node_type_to_string(xnn_node_type_argmax_pooling_2d));
32     return xnn_status_uninitialized;
33   }
34 
35   const uint32_t pooling_size = pooling_height * pooling_width;
36   if (pooling_size == 0) {
37     xnn_log_error(
38       "failed to define %s operator with %" PRIu32 "x%" PRIu32 " pooling size: "
39       "pooling size dimensions must be non-zero",
40       xnn_node_type_to_string(xnn_node_type_argmax_pooling_2d), pooling_width, pooling_height);
41     return xnn_status_invalid_parameter;
42   }
43 
44   if (pooling_size == 1) {
45     xnn_log_error(
46       "failed to define %s operator with 1 pooling element: 1x1 pooling is meaningless",
47       xnn_node_type_to_string(xnn_node_type_argmax_pooling_2d));
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_argmax_pooling_2d), input_id);
55     return xnn_status_invalid_parameter;
56   }
57 
58   if (output_value_id >= subgraph->num_values) {
59     xnn_log_error(
60       "failed to define %s operator with output value ID #%" PRIu32 ": invalid Value ID",
61       xnn_node_type_to_string(xnn_node_type_argmax_pooling_2d), output_value_id);
62     return xnn_status_invalid_parameter;
63   }
64 
65   if (output_index_id >= subgraph->num_values) {
66     xnn_log_error(
67       "failed to define %s operator with output index ID #%" PRIu32 ": invalid Value ID",
68       xnn_node_type_to_string(xnn_node_type_argmax_pooling_2d), output_index_id);
69     return xnn_status_invalid_parameter;
70   }
71 
72   struct xnn_node* node = xnn_subgraph_new_node(subgraph);
73   if (node == NULL) {
74     return xnn_status_out_of_memory;
75   }
76 
77   node->type = xnn_node_type_argmax_pooling_2d;
78   node->params.pooling_2d.padding_top = input_padding_top;
79   node->params.pooling_2d.padding_right = input_padding_right;
80   node->params.pooling_2d.padding_bottom = input_padding_bottom;
81   node->params.pooling_2d.padding_left = input_padding_left;
82   node->params.pooling_2d.pooling_height = pooling_height;
83   node->params.pooling_2d.pooling_width = pooling_width;
84   node->num_inputs = 1;
85   node->inputs[0] = input_id;
86   node->num_outputs = 2;
87   node->outputs[0] = output_value_id;
88   node->outputs[1] = output_index_id;
89   node->flags = flags;
90 
91   return xnn_status_success;
92 }
93