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_deconvolution_2d(xnn_subgraph_t subgraph,uint32_t padding_top,uint32_t padding_right,uint32_t padding_bottom,uint32_t padding_left,uint32_t adjustment_height,uint32_t adjustment_width,uint32_t kernel_height,uint32_t kernel_width,uint32_t upsampling_height,uint32_t upsampling_width,uint32_t dilation_height,uint32_t dilation_width,uint32_t groups,size_t group_input_channels,size_t group_output_channels,float output_min,float output_max,uint32_t input_id,uint32_t filter_id,uint32_t bias_id,uint32_t output_id,uint32_t flags)16 enum xnn_status xnn_define_deconvolution_2d(
17   xnn_subgraph_t subgraph,
18   uint32_t padding_top,
19   uint32_t padding_right,
20   uint32_t padding_bottom,
21   uint32_t padding_left,
22   uint32_t adjustment_height,
23   uint32_t adjustment_width,
24   uint32_t kernel_height,
25   uint32_t kernel_width,
26   uint32_t upsampling_height,
27   uint32_t upsampling_width,
28   uint32_t dilation_height,
29   uint32_t dilation_width,
30   uint32_t groups,
31   size_t group_input_channels,
32   size_t group_output_channels,
33   float output_min,
34   float output_max,
35   uint32_t input_id,
36   uint32_t filter_id,
37   uint32_t bias_id,
38   uint32_t output_id,
39   uint32_t flags)
40 {
41   if ((xnn_params.init_flags & XNN_INIT_FLAG_XNNPACK) == 0) {
42     xnn_log_error("failed to define %s operator: XNNPACK is not initialized",
43       xnn_node_type_to_string(xnn_node_type_deconvolution_2d));
44     return xnn_status_uninitialized;
45   }
46 
47   if (kernel_width == 0 || kernel_height == 0) {
48     xnn_log_error(
49       "failed to define %s operator with %" PRIu32 "x%" PRIu32 " kernel: kernel dimensions must be non-zero",
50       xnn_node_type_to_string(xnn_node_type_deconvolution_2d), kernel_width, kernel_height);
51     return xnn_status_invalid_parameter;
52   }
53 
54   if (upsampling_width == 0 || upsampling_height == 0) {
55     xnn_log_error(
56       "failed to define %s operator with %" PRIu32 "x%" PRIu32 " upsampling: upsampling dimensions must be non-zero",
57       xnn_node_type_to_string(xnn_node_type_deconvolution_2d), upsampling_width, upsampling_height);
58     return xnn_status_invalid_parameter;
59   }
60 
61   if (dilation_width == 0 || dilation_height == 0) {
62     xnn_log_error(
63       "failed to define %s operator with %" PRIu32 "x%" PRIu32 " dilation: dilation dimensions must be non-zero",
64       xnn_node_type_to_string(xnn_node_type_deconvolution_2d), dilation_width, dilation_height);
65     return xnn_status_invalid_parameter;
66   }
67 
68   if (groups == 0) {
69     xnn_log_error(
70       "failed to define %s operator with %" PRIu32 " groups: number of groups must be non-zero",
71       xnn_node_type_to_string(xnn_node_type_deconvolution_2d), groups);
72     return xnn_status_invalid_parameter;
73   }
74 
75   if (group_input_channels == 0) {
76     xnn_log_error(
77       "failed to define %s operator with %zu input channels per group: number of channels must be non-zero",
78       xnn_node_type_to_string(xnn_node_type_deconvolution_2d), group_input_channels);
79     return xnn_status_invalid_parameter;
80   }
81 
82   if (group_output_channels == 0) {
83     xnn_log_error(
84       "failed to define %s operator with %zu output channels per group: number of channels must be non-zero",
85       xnn_node_type_to_string(xnn_node_type_deconvolution_2d), group_output_channels);
86     return xnn_status_invalid_parameter;
87   }
88 
89   if (isnan(output_min)) {
90     xnn_log_error(
91       "failed to define %s operator with NaN output lower bound: lower bound must be non-NaN",
92       xnn_node_type_to_string(xnn_node_type_deconvolution_2d));
93     return xnn_status_invalid_parameter;
94   }
95 
96   if (isnan(output_max)) {
97     xnn_log_error(
98       "failed to define %s operator with NaN output upper bound: upper bound must be non-NaN",
99       xnn_node_type_to_string(xnn_node_type_deconvolution_2d));
100     return xnn_status_invalid_parameter;
101   }
102 
103   if (output_min >= output_max) {
104     xnn_log_error(
105       "failed to define %s operator with [%.7g, %.7g] output range: lower bound must be below upper bound",
106       xnn_node_type_to_string(xnn_node_type_deconvolution_2d), output_min, output_max);
107     return xnn_status_invalid_parameter;
108   }
109 
110   if (input_id >= subgraph->num_values) {
111     xnn_log_error(
112       "failed to define %s operator with input ID #%" PRIu32 ": invalid Value ID",
113       xnn_node_type_to_string(xnn_node_type_deconvolution_2d), input_id);
114     return xnn_status_invalid_parameter;
115   }
116 
117   if (filter_id >= subgraph->num_values) {
118     xnn_log_error(
119       "failed to define %s operator with filter ID #%" PRIu32 ": invalid Value ID",
120       xnn_node_type_to_string(xnn_node_type_deconvolution_2d), filter_id);
121     return xnn_status_invalid_parameter;
122   }
123 
124   if (bias_id >= subgraph->num_values) {
125     xnn_log_error(
126       "failed to define %s operator with bias ID #%" PRIu32 ": invalid Value ID",
127       xnn_node_type_to_string(xnn_node_type_deconvolution_2d), bias_id);
128     return xnn_status_invalid_parameter;
129   }
130 
131   if (output_id >= subgraph->num_values) {
132     xnn_log_error(
133       "failed to define %s operator with output ID #%" PRIu32 ": invalid Value ID",
134       xnn_node_type_to_string(xnn_node_type_deconvolution_2d), output_id);
135     return xnn_status_invalid_parameter;
136   }
137 
138   struct xnn_node* node = xnn_subgraph_new_node(subgraph);
139   if (node == NULL) {
140     return xnn_status_out_of_memory;
141   }
142 
143   node->type = xnn_node_type_deconvolution_2d;
144   node->params.deconvolution_2d.padding_top = padding_top;
145   node->params.deconvolution_2d.padding_right = padding_right;
146   node->params.deconvolution_2d.padding_bottom = padding_bottom;
147   node->params.deconvolution_2d.padding_left = padding_left;
148   node->params.deconvolution_2d.kernel_height = kernel_height;
149   node->params.deconvolution_2d.kernel_width = kernel_width;
150   node->params.deconvolution_2d.upsampling_height = upsampling_height;
151   node->params.deconvolution_2d.upsampling_width = upsampling_width;
152   node->params.deconvolution_2d.dilation_height = dilation_height;
153   node->params.deconvolution_2d.dilation_width = dilation_width;
154   node->params.deconvolution_2d.groups = groups;
155   node->params.deconvolution_2d.group_input_channels = group_input_channels;
156   node->params.deconvolution_2d.group_output_channels = group_output_channels;
157   node->activation.output_min = output_min;
158   node->activation.output_max = output_max;
159   node->num_inputs = 3;
160   node->inputs[0] = input_id;
161   node->inputs[1] = filter_id;
162   node->inputs[2] = bias_id;
163   node->num_outputs = 1;
164   node->outputs[0] = output_id;
165   node->flags = flags;
166 
167   return xnn_status_success;
168 };
169