• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/common.h>
18 #include <xnnpack/log.h>
19 #include <xnnpack/math.h>
20 #include <xnnpack/params-init.h>
21 #include <xnnpack/params.h>
22 #include <xnnpack/indirection.h>
23 
24 
compute_output_dimension(size_t padded_input_dimension,size_t kernel_dimension)25 static inline size_t compute_output_dimension(
26     size_t padded_input_dimension,
27     size_t kernel_dimension)
28 {
29   return padded_input_dimension / kernel_dimension;
30 }
31 
select_ukernel(size_t pooling_size,const struct argmaxpool_parameters * ukernel)32 static const struct argmaxpool_parameters* select_ukernel(
33     size_t pooling_size,
34     const struct argmaxpool_parameters* ukernel)
35 {
36   while (ukernel->qr == 0 && ukernel->mr < pooling_size) {
37     ukernel++;
38   }
39   return ukernel;
40 }
41 
xnn_create_argmax_pooling2d_nhwc_f32(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 * argmax_pooling_op_out)42 enum xnn_status xnn_create_argmax_pooling2d_nhwc_f32(
43     uint32_t input_padding_top,
44     uint32_t input_padding_right,
45     uint32_t input_padding_bottom,
46     uint32_t input_padding_left,
47     uint32_t pooling_height,
48     uint32_t pooling_width,
49     size_t channels,
50     size_t input_pixel_stride,
51     size_t output_pixel_stride,
52     uint32_t flags,
53     xnn_operator_t* argmax_pooling_op_out)
54 {
55   xnn_operator_t argmax_pooling_op = NULL;
56   enum xnn_status status = xnn_status_uninitialized;
57 
58   if ((xnn_params.init_flags & XNN_INIT_FLAG_XNNPACK) == 0) {
59     xnn_log_error("failed to create %s operator: XNNPACK is not initialized",
60       xnn_operator_type_to_string(xnn_operator_type_argmax_pooling_nhwc_f32));
61     goto error;
62   }
63 
64   status = xnn_status_invalid_parameter;
65 
66   const uint32_t pooling_size = pooling_height * pooling_width;
67   if (pooling_size == 0) {
68     xnn_log_error(
69       "failed to create %s operator with %" PRIu32 "x%" PRIu32 " pooling size: "
70       "pooling size dimensions must be non-zero",
71       xnn_operator_type_to_string(xnn_operator_type_argmax_pooling_nhwc_f32), pooling_width, pooling_height);
72     goto error;
73   }
74 
75   if (pooling_size == 1) {
76     xnn_log_error(
77       "failed to create %s operator with 1 pooling element: 1x1 pooling is meaningless",
78       xnn_operator_type_to_string(xnn_operator_type_argmax_pooling_nhwc_f32));
79     goto error;
80   }
81 
82   if (channels == 0) {
83     xnn_log_error(
84       "failed to create %s operator with %zu channels: number of channels must be non-zero",
85       xnn_operator_type_to_string(xnn_operator_type_argmax_pooling_nhwc_f32), channels);
86     goto error;
87   }
88 
89   if (input_pixel_stride < channels) {
90     xnn_log_error(
91       "failed to create %s operator with input pixel stride of %zu: "
92       "stride must be at least as large as the number of channels (%zu)",
93       xnn_operator_type_to_string(xnn_operator_type_argmax_pooling_nhwc_f32), input_pixel_stride, channels);
94     goto error;
95   }
96 
97   if (output_pixel_stride < channels) {
98     xnn_log_error(
99       "failed to create %s operator with output pixel stride of %zu: "
100       "stride must be at least as large as the number of channels (%zu)",
101       xnn_operator_type_to_string(xnn_operator_type_argmax_pooling_nhwc_f32), output_pixel_stride, channels);
102     goto error;
103   }
104 
105   const bool any_padding = (input_padding_left | input_padding_top | input_padding_right | input_padding_bottom) != 0;
106   if ((flags & XNN_FLAG_TENSORFLOW_SAME_PADDING) != 0) {
107     if (any_padding) {
108       xnn_log_error(
109         "failed to create %s operator with %" PRIu32 "+%" PRIu32 "x%" PRIu32 "+%" PRIu32" padding: "
110         "TensorFlow SAME padding can't be combined with explicit padding specification",
111         xnn_operator_type_to_string(xnn_operator_type_argmax_pooling_nhwc_f32),
112         input_padding_top, input_padding_left, input_padding_bottom, input_padding_right);
113       goto error;
114     }
115   }
116 
117   status = xnn_status_out_of_memory;
118 
119   argmax_pooling_op = xnn_allocate_zero_simd_memory(sizeof(struct xnn_operator));
120   if (argmax_pooling_op == NULL) {
121     xnn_log_error(
122       "failed to allocate %zu bytes for %s operator descriptor",
123       sizeof(struct xnn_operator), xnn_operator_type_to_string(xnn_operator_type_argmax_pooling_nhwc_f32));
124     goto error;
125   }
126 
127   argmax_pooling_op->padding_top = input_padding_top;
128   argmax_pooling_op->padding_right = input_padding_right;
129   argmax_pooling_op->padding_bottom = input_padding_bottom;
130   argmax_pooling_op->padding_left = input_padding_left;
131 
132   argmax_pooling_op->kernel_height = pooling_height;
133   argmax_pooling_op->kernel_width = pooling_width;
134   argmax_pooling_op->stride_height = pooling_height;
135   argmax_pooling_op->stride_width = pooling_width;
136   argmax_pooling_op->dilation_height = 1;
137   argmax_pooling_op->dilation_width = 1;
138   argmax_pooling_op->channels = channels;
139   argmax_pooling_op->input_pixel_stride = input_pixel_stride;
140   argmax_pooling_op->output_pixel_stride = output_pixel_stride;
141 
142   argmax_pooling_op->type = xnn_operator_type_argmax_pooling_nhwc_f32;
143   argmax_pooling_op->flags = flags;
144 
145   argmax_pooling_op->state = xnn_run_state_invalid;
146 
147   *argmax_pooling_op_out = argmax_pooling_op;
148   return xnn_status_success;
149 
150 error:
151   xnn_delete_operator(argmax_pooling_op);
152   return status;
153 }
154 
xnn_setup_argmax_pooling2d_nhwc_f32(xnn_operator_t argmax_pooling_op,size_t batch_size,size_t input_height,size_t input_width,const float * input,float * output,uint32_t * index,pthreadpool_t threadpool)155 enum xnn_status xnn_setup_argmax_pooling2d_nhwc_f32(
156     xnn_operator_t argmax_pooling_op,
157     size_t batch_size,
158     size_t input_height,
159     size_t input_width,
160     const float* input,
161     float* output,
162     uint32_t* index,
163     pthreadpool_t threadpool)
164 {
165   if (argmax_pooling_op->type != xnn_operator_type_argmax_pooling_nhwc_f32) {
166     xnn_log_error("failed to setup operator: operator type mismatch (expected %s, got %s)",
167       xnn_operator_type_to_string(xnn_operator_type_argmax_pooling_nhwc_f32),
168       xnn_operator_type_to_string(argmax_pooling_op->type));
169     return xnn_status_invalid_parameter;
170   }
171   argmax_pooling_op->state = xnn_run_state_invalid;
172 
173   if ((xnn_params.init_flags & XNN_INIT_FLAG_XNNPACK) == 0) {
174     xnn_log_error("failed to setup %s operator: XNNPACK is not initialized",
175       xnn_operator_type_to_string(xnn_operator_type_argmax_pooling_nhwc_f32));
176     return xnn_status_uninitialized;
177   }
178 
179   if (input_width == 0 || input_height == 0) {
180     xnn_log_error(
181       "failed to setup %s operator with %zux%zu input: input dimensions must be non-zero",
182       xnn_operator_type_to_string(xnn_operator_type_argmax_pooling_nhwc_f32), input_width, input_height);
183     return xnn_status_invalid_parameter;
184   }
185 
186   if (batch_size == 0) {
187     argmax_pooling_op->state = xnn_run_state_skip;
188     return xnn_status_success;
189   }
190 
191   argmax_pooling_op->batch_size = batch_size;
192   argmax_pooling_op->input_height = input_height;
193   argmax_pooling_op->input_width = input_width;
194   argmax_pooling_op->input = input;
195 
196   const size_t pooling_height = argmax_pooling_op->kernel_height;
197   const size_t pooling_width = argmax_pooling_op->kernel_width;
198 
199   if (argmax_pooling_op->flags & XNN_FLAG_TENSORFLOW_SAME_PADDING) {
200     argmax_pooling_op->output_height = divide_round_up(input_height, pooling_height);
201     argmax_pooling_op->output_width = divide_round_up(input_width, pooling_width);
202 
203     const uint32_t padding_height = argmax_pooling_op->output_height * pooling_height - input_height;
204     const uint32_t padding_width = argmax_pooling_op->output_width * pooling_width - input_width;
205     argmax_pooling_op->padding_top = padding_height / 2;
206     argmax_pooling_op->padding_left = padding_width / 2;
207     argmax_pooling_op->padding_bottom = padding_height - argmax_pooling_op->padding_top;
208     argmax_pooling_op->padding_right = padding_width - argmax_pooling_op->padding_left;
209   } else {
210     argmax_pooling_op->output_height = compute_output_dimension(
211         argmax_pooling_op->padding_top + input_height + argmax_pooling_op->padding_bottom,
212         argmax_pooling_op->kernel_height);
213     argmax_pooling_op->output_width = compute_output_dimension(
214         argmax_pooling_op->padding_left + input_width + argmax_pooling_op->padding_right,
215         argmax_pooling_op->kernel_width);
216   }
217 
218   const size_t pooling_size = pooling_height * pooling_width;
219   const size_t output_height = argmax_pooling_op->output_height;
220   const size_t output_width = argmax_pooling_op->output_width;
221   const struct argmaxpool_parameters* ukernel = select_ukernel(pooling_size, xnn_params.f32.argmaxpool);
222   const uint32_t mr = ukernel->mr;
223 
224   const size_t step_width = pooling_width;
225   const size_t step_height = pooling_size + (output_width - 1) * step_width * pooling_height;
226 
227   if (input_height != argmax_pooling_op->last_input_height ||
228       input_width != argmax_pooling_op->last_input_width)
229   {
230     // Micro-kernel may read up to (mr - 1) elements after the end of indirection buffer.
231     const size_t indirection_buffer_size = sizeof(void*) * ((mr - 1) + output_height * step_height);
232 
233     const void** indirection_buffer =
234       (const void**) xnn_reallocate_memory(argmax_pooling_op->indirection_buffer, indirection_buffer_size);
235     if (indirection_buffer == NULL) {
236       xnn_log_error(
237         "failed to allocate %zu bytes for %s operator indirection buffer",
238         indirection_buffer_size, xnn_operator_type_to_string(xnn_operator_type_argmax_pooling_nhwc_f32));
239       return xnn_status_out_of_memory;
240     }
241     argmax_pooling_op->indirection_buffer = indirection_buffer;
242 
243     xnn_indirection_init_maxpool2d(argmax_pooling_op, step_height, step_width, 2 /* log2(sizeof(float)) */);
244 
245     argmax_pooling_op->last_input = input;
246     argmax_pooling_op->last_input_height = input_height;
247     argmax_pooling_op->last_input_width = input_width;
248   }
249 
250   const size_t channels = argmax_pooling_op->channels;
251 
252   const size_t indirect_input_height_stride = step_height * sizeof(void*);
253   const size_t output_width_stride = argmax_pooling_op->output_pixel_stride * sizeof(float);
254   const size_t output_height_stride = output_width * output_width_stride;
255   const size_t index_height_stride = output_width * channels * sizeof(uint32_t);
256 
257   const uint32_t qr = ukernel->qr;
258   const size_t multipass_adjustment = qr == 0 ? 0 : round_up(pooling_size - mr, qr) + mr - qr;
259   argmax_pooling_op->context.argmax_pooling = (struct argmax_pooling_context) {
260     .indirect_input = argmax_pooling_op->indirection_buffer,
261     .indirect_input_height_stride = indirect_input_height_stride,
262     .input_offset = (size_t) ((uintptr_t) input - (uintptr_t) argmax_pooling_op->last_input),
263     .input_batch_stride = input_height * input_width * argmax_pooling_op->input_pixel_stride * sizeof(float),
264     .output = output,
265     .output_batch_stride = output_height * output_height_stride,
266     .output_height_stride = output_height_stride,
267     .output_width = output_width,
268     .index = index,
269     .index_batch_stride = output_height * index_height_stride,
270     .index_height_stride = index_height_stride,
271     .pooling_size = pooling_size,
272     .channels = channels,
273     .input_increment = (pooling_height * step_width - multipass_adjustment) * sizeof(void*),
274     .output_increment = output_width_stride - channels * sizeof(float),
275   };
276   argmax_pooling_op->compute.type = xnn_parallelization_type_2d;
277   argmax_pooling_op->compute.range[0] = batch_size;
278   argmax_pooling_op->compute.range[1] = output_height;
279 
280   if (pooling_size <= mr) {
281     argmax_pooling_op->context.argmax_pooling.unipass_ukernel = ukernel->up;
282     argmax_pooling_op->compute.task_2d = (pthreadpool_task_2d_t) xnn_compute_argmax_pooling_unipass;
283   } else {
284     argmax_pooling_op->context.argmax_pooling.multipass_ukernel = ukernel->mp;
285     argmax_pooling_op->compute.task_2d = (pthreadpool_task_2d_t) xnn_compute_argmax_pooling_multipass;
286   }
287   argmax_pooling_op->state = xnn_run_state_ready;
288 
289   return xnn_status_success;
290 }
291 
292