1 // Copyright 2019 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 <assert.h>
7 #include <math.h>
8 #include <stdbool.h>
9 #include <stddef.h>
10 #include <stdint.h>
11 #include <stdlib.h>
12 #include <string.h>
13
14 #include <xnnpack.h>
15 #include <xnnpack/allocator.h>
16 #include <xnnpack/operator.h>
17 #include <xnnpack/log.h>
18 #include <xnnpack/common.h>
19 #include <xnnpack/math.h>
20 #include <xnnpack/params.h>
21 #include <xnnpack/indirection.h>
22
23
compute_output_dimension(size_t input_dimension,size_t input_padding_dimension,size_t kernel_dimension)24 static inline size_t compute_output_dimension(
25 size_t input_dimension,
26 size_t input_padding_dimension,
27 size_t kernel_dimension)
28 {
29 return doz(kernel_dimension * input_dimension, input_padding_dimension);
30 }
31
xnn_create_unpooling2d_nhwc_x32(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,size_t channels,size_t input_pixel_stride,size_t output_pixel_stride,uint32_t flags,xnn_operator_t * unpooling_op_out)32 enum xnn_status xnn_create_unpooling2d_nhwc_x32(
33 uint32_t input_padding_top,
34 uint32_t input_padding_right,
35 uint32_t input_padding_bottom,
36 uint32_t input_padding_left,
37 uint32_t pooling_height,
38 uint32_t pooling_width,
39 size_t channels,
40 size_t input_pixel_stride,
41 size_t output_pixel_stride,
42 uint32_t flags,
43 xnn_operator_t* unpooling_op_out)
44 {
45 xnn_operator_t unpooling_op = NULL;
46 enum xnn_status status = xnn_status_uninitialized;
47
48 if ((xnn_params.init_flags & XNN_INIT_FLAG_XNNPACK) == 0) {
49 xnn_log_error("failed to create %s operator: XNNPACK is not initialized",
50 xnn_operator_type_to_string(xnn_operator_type_unpooling_nhwc_x32));
51 goto error;
52 }
53
54 status = xnn_status_invalid_parameter;
55
56 const uint32_t pooling_size = pooling_height * pooling_width;
57 if (pooling_size == 0) {
58 xnn_log_error(
59 "failed to create %s operator with %" PRIu32 "x%" PRIu32 " pooling size: "
60 "pooling size dimensions must be non-zero",
61 xnn_operator_type_to_string(xnn_operator_type_unpooling_nhwc_x32), pooling_width, pooling_height);
62 goto error;
63 }
64
65 if (pooling_size == 1) {
66 xnn_log_error(
67 "failed to create %s operator with 1 pooling element: 1x1 unpooling is meaningless",
68 xnn_operator_type_to_string(xnn_operator_type_unpooling_nhwc_x32));
69 goto error;
70 }
71
72 if (channels == 0) {
73 xnn_log_error(
74 "failed to create %s operator with %zu channels: number of channels must be non-zero",
75 xnn_operator_type_to_string(xnn_operator_type_unpooling_nhwc_x32), channels);
76 goto error;
77 }
78
79 if (input_pixel_stride < channels) {
80 xnn_log_error(
81 "failed to create %s operator with input pixel stride of %zu: "
82 "stride must be at least as large as the number of channels (%zu)",
83 xnn_operator_type_to_string(xnn_operator_type_unpooling_nhwc_x32), input_pixel_stride, channels);
84 goto error;
85 }
86
87 if (output_pixel_stride < channels) {
88 xnn_log_error(
89 "failed to create %s operator with output pixel stride of %zu: "
90 "stride must be at least as large as the number of channels (%zu)",
91 xnn_operator_type_to_string(xnn_operator_type_unpooling_nhwc_x32), output_pixel_stride, channels);
92 goto error;
93 }
94
95 status = xnn_status_out_of_memory;
96
97 unpooling_op = xnn_allocate_zero_simd_memory(sizeof(struct xnn_operator));
98 if (unpooling_op == NULL) {
99 xnn_log_error(
100 "failed to allocate %zu bytes for %s operator descriptor",
101 sizeof(struct xnn_operator), xnn_operator_type_to_string(xnn_operator_type_unpooling_nhwc_x32));
102 goto error;
103 }
104
105 unpooling_op->padding_top = input_padding_top;
106 unpooling_op->padding_right = input_padding_right;
107 unpooling_op->padding_bottom = input_padding_bottom;
108 unpooling_op->padding_left = input_padding_left;
109
110 unpooling_op->kernel_height = pooling_height;
111 unpooling_op->kernel_width = pooling_width;
112 unpooling_op->channels = channels;
113 unpooling_op->input_pixel_stride = input_pixel_stride;
114 unpooling_op->output_pixel_stride = output_pixel_stride;
115
116 unpooling_op->type = xnn_operator_type_unpooling_nhwc_x32;
117
118 unpooling_op->state = xnn_run_state_invalid;
119
120 *unpooling_op_out = unpooling_op;
121 return xnn_status_success;
122
123 error:
124 xnn_delete_operator(unpooling_op);
125 return status;
126 }
127
xnn_setup_unpooling2d_nhwc_x32(xnn_operator_t unpooling_op,size_t batch_size,size_t input_height,size_t input_width,const void * input,const uint32_t * index,void * output,pthreadpool_t threadpool)128 enum xnn_status xnn_setup_unpooling2d_nhwc_x32(
129 xnn_operator_t unpooling_op,
130 size_t batch_size,
131 size_t input_height,
132 size_t input_width,
133 const void* input,
134 const uint32_t* index,
135 void* output,
136 pthreadpool_t threadpool)
137 {
138 if (unpooling_op->type != xnn_operator_type_unpooling_nhwc_x32) {
139 xnn_log_error("failed to setup operator: operator type mismatch (expected %s, got %s)",
140 xnn_operator_type_to_string(xnn_operator_type_unpooling_nhwc_x32),
141 xnn_operator_type_to_string(unpooling_op->type));
142 return xnn_status_invalid_parameter;
143 }
144 unpooling_op->state = xnn_run_state_invalid;
145
146 if ((xnn_params.init_flags & XNN_INIT_FLAG_XNNPACK) == 0) {
147 xnn_log_error("failed to setup %s operator: XNNPACK is not initialized",
148 xnn_operator_type_to_string(xnn_operator_type_unpooling_nhwc_x32));
149 return xnn_status_uninitialized;
150 }
151
152 if (input_width == 0 || input_height == 0) {
153 xnn_log_error(
154 "failed to setup %s operator with %zux%zu input: input dimensions must be non-zero",
155 xnn_operator_type_to_string(xnn_operator_type_unpooling_nhwc_x32), input_width, input_height);
156 return xnn_status_invalid_parameter;
157 }
158
159 if (batch_size == 0) {
160 unpooling_op->state = xnn_run_state_skip;
161 return xnn_status_success;
162 }
163
164 unpooling_op->batch_size = batch_size;
165 unpooling_op->input_height = input_height;
166 unpooling_op->input_width = input_width;
167 unpooling_op->input = input;
168
169 unpooling_op->output_height = compute_output_dimension(
170 input_height, unpooling_op->padding_top + unpooling_op->padding_bottom,
171 unpooling_op->kernel_height);
172 unpooling_op->output_width = compute_output_dimension(
173 input_width, unpooling_op->padding_left + unpooling_op->padding_right,
174 unpooling_op->kernel_width);
175 unpooling_op->output = output;
176
177 size_t valid_batch_size = 0;
178 if (output == unpooling_op->last_output &&
179 input_height == unpooling_op->last_input_height &&
180 input_width == unpooling_op->last_input_width)
181 {
182 valid_batch_size = unpooling_op->valid_batch_size;
183 if (batch_size <= valid_batch_size) {
184 unpooling_op->compute.range[0] = batch_size * input_height;
185 unpooling_op->state = xnn_run_state_ready;
186 return xnn_status_success;
187 }
188 }
189
190 const size_t pooling_height = unpooling_op->kernel_height;
191 const size_t pooling_width = unpooling_op->kernel_width;
192 const size_t pooling_size = pooling_height * pooling_width;
193
194 const size_t indirection_buffer_size = sizeof(void*) * (batch_size * input_height * input_width * pooling_size);
195 void** indirection_buffer = (void**) xnn_reallocate_memory(unpooling_op->indirection_buffer, indirection_buffer_size);
196 if (indirection_buffer == NULL) {
197 xnn_log_error(
198 "failed to allocate %zu bytes for %s operator indirection buffer",
199 indirection_buffer_size, xnn_operator_type_to_string(xnn_operator_type_unpooling_nhwc_x32));
200 return xnn_status_out_of_memory;
201 }
202 unpooling_op->indirection_buffer = (const void**) indirection_buffer;
203
204 xnn_indirection_init_unpool2d(unpooling_op, valid_batch_size, 2 /* log2(sizeof(type32)) */);
205
206 const size_t channels = unpooling_op->channels;
207 const size_t input_pixel_stride_in_bytes = unpooling_op->input_pixel_stride * sizeof(float);
208 unpooling_op->context.unpooling = (struct unpooling_context) {
209 .input = input,
210 .input_height_stride = input_width * input_pixel_stride_in_bytes,
211 .input_width_stride = input_pixel_stride_in_bytes,
212 .index = index,
213 .index_height_stride = input_width * channels * sizeof(uint32_t),
214 .index_width_stride = channels * sizeof(uint32_t),
215 .indirect_output = indirection_buffer,
216 .indirect_output_height_stride = input_width * pooling_size * sizeof(void*),
217 .indirect_output_width_stride = pooling_size * sizeof(void*),
218 .pooling_size = pooling_size,
219 .channels = channels,
220 .fill_value = 0,
221 .ukernel = xnn_params.x32.unpool,
222 };
223 unpooling_op->compute.type = xnn_parallelization_type_2d;
224 unpooling_op->compute.task_2d = (pthreadpool_task_2d_t) xnn_compute_unpooling;
225 unpooling_op->compute.range[0] = batch_size * input_height;
226 unpooling_op->compute.range[1] = input_width;
227 unpooling_op->state = xnn_run_state_ready;
228
229 unpooling_op->last_output = output;
230 unpooling_op->last_input_height = input_height;
231 unpooling_op->last_input_width = input_width;
232 unpooling_op->valid_batch_size = max(valid_batch_size, batch_size);
233
234 return xnn_status_success;
235 }
236