blob: ea8779031758541ba0712d0435b5a4ad4fef9d64 [file] [log] [blame]
XNNPACK Teamb455b122019-09-27 18:10:33 -07001// Copyright (c) Facebook, Inc. and its affiliates.
2// All rights reserved.
3//
4// Copyright 2019 Google LLC
5//
6// This source code is licensed under the BSD-style license found in the
7// LICENSE file in the root directory of this source tree.
8
9#include <assert.h>
10#include <stddef.h>
11#include <stdint.h>
12#include <string.h>
13
14#include <xnnpack.h>
Marat Dukhand0cf9bd2020-04-13 20:19:19 -070015#include <xnnpack/allocator.h>
XNNPACK Teamb455b122019-09-27 18:10:33 -070016#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/compute.h>
22
23
Marat Dukhan49a59692020-03-06 16:58:33 -080024void xnn_compute_grouped_gemm(
Marat Dukhanf196d012020-04-15 11:50:03 -070025 const struct gemm_context context[restrict XNN_MIN_ELEMENTS(1)],
XNNPACK Teamb455b122019-09-27 18:10:33 -070026 size_t group_index,
27 size_t mr_block_start,
28 size_t nr_block_start,
29 size_t mr_block_size,
30 size_t nr_block_size)
31{
32 const size_t k_scaled = context->k_scaled;
33 const size_t a_stride = context->a_stride;
34 const size_t cm_stride = context->cm_stride;
35
Marat Dukhan05702cf2020-03-26 15:41:33 -070036 context->ukernel.function[XNN_UARCH_DEFAULT](
XNNPACK Teamb455b122019-09-27 18:10:33 -070037 mr_block_size,
38 nr_block_size,
39 k_scaled,
40 (const void*) ((uintptr_t) context->a + mr_block_start * a_stride + group_index * k_scaled),
41 a_stride,
42 (const void*) ((uintptr_t) context->packed_w + nr_block_start * context->w_stride + group_index * context->wg_stride),
43 (void*) ((uintptr_t) context->c + mr_block_start * cm_stride + (nr_block_start << context->log2_csize) + group_index * context->cg_stride),
44 cm_stride,
45 context->cn_stride,
46 &context->params);
47}
48
49void xnn_compute_gemm(
Marat Dukhanf196d012020-04-15 11:50:03 -070050 const struct gemm_context context[restrict XNN_MIN_ELEMENTS(1)],
XNNPACK Teamb455b122019-09-27 18:10:33 -070051 size_t mr_block_start,
52 size_t nr_block_start,
53 size_t mr_block_size,
54 size_t nr_block_size)
55{
56 const size_t a_stride = context->a_stride;
57 const size_t cm_stride = context->cm_stride;
58
Marat Dukhan05702cf2020-03-26 15:41:33 -070059 context->ukernel.function[XNN_UARCH_DEFAULT](
XNNPACK Teamb455b122019-09-27 18:10:33 -070060 mr_block_size,
61 nr_block_size,
62 context->k_scaled,
63 (const void*) ((uintptr_t) context->a + mr_block_start * a_stride),
64 a_stride,
65 (const void*) ((uintptr_t) context->packed_w + nr_block_start * context->w_stride),
66 (void*) ((uintptr_t) context->c + mr_block_start * cm_stride + (nr_block_start << context->log2_csize)),
67 cm_stride,
68 context->cn_stride,
69 &context->params);
70}
71
72void xnn_compute_spmm(
Marat Dukhanf196d012020-04-15 11:50:03 -070073 const struct spmm_context context[restrict XNN_MIN_ELEMENTS(1)],
XNNPACK Teamb455b122019-09-27 18:10:33 -070074 size_t batch_index,
75 size_t mr_block_start,
76 size_t mr_block_size)
77{
78 context->ukernel(
79 mr_block_size,
80 context->n,
Marat Dukhane8bfcc82020-11-16 12:28:13 -080081 (const void*) ((uintptr_t) context->input + batch_index * context->batched_input_stride + mr_block_start),
82 context->nonzero_weights,
XNNPACK Teamb455b122019-09-27 18:10:33 -070083 context->input_increments,
84 context->output_channel_nonzeros,
Marat Dukhane8bfcc82020-11-16 12:28:13 -080085 (void*) ((uintptr_t) context->output + batch_index * context->batched_output_stride + mr_block_start),
86 context->scaled_m,
XNNPACK Teamb455b122019-09-27 18:10:33 -070087 &context->params);
88}
89
Fabio Riccardic2146cc2020-08-19 11:15:24 -070090void xnn_compute_grouped_batch_igemm(
Marat Dukhanf196d012020-04-15 11:50:03 -070091 const struct igemm_context context[restrict XNN_MIN_ELEMENTS(1)],
XNNPACK Teamb455b122019-09-27 18:10:33 -070092 size_t batch_index,
93 size_t group_index,
94 size_t mr_block_start,
95 size_t nr_block_start,
96 size_t mr_block_size,
97 size_t nr_block_size)
98{
99 const size_t ks = context->ks;
100 const size_t cm_stride = context->cm_stride;
101
Marat Dukhan05702cf2020-03-26 15:41:33 -0700102 context->ukernel.function[XNN_UARCH_DEFAULT](
XNNPACK Teamb455b122019-09-27 18:10:33 -0700103 mr_block_size,
104 nr_block_size,
105 context->kc,
106 context->ks_scaled,
107 (const void**) ((uintptr_t) context->indirect_a + mr_block_start * ks * sizeof(void*)),
108 (const void*) ((uintptr_t) context->packed_w + nr_block_start * context->w_stride + group_index * context->gw_stride),
109 (void*) ((uintptr_t) context->c + group_index * context->gc_stride + batch_index * context->bc_stride + mr_block_start * cm_stride + (nr_block_start << context->log2_csize)),
110 cm_stride,
111 context->cn_stride,
112 context->a_offset + group_index * context->ga_stride + batch_index * context->ba_stride,
113 context->zero,
114 &context->params);
115}
116
Fabio Riccardic2146cc2020-08-19 11:15:24 -0700117void xnn_compute_grouped_igemm(
118 const struct igemm_context context[restrict XNN_MIN_ELEMENTS(1)],
119 size_t group_index,
120 size_t mr_block_start,
121 size_t nr_block_start,
122 size_t mr_block_size,
123 size_t nr_block_size)
124{
125 const size_t ks = context->ks;
126 const size_t cm_stride = context->cm_stride;
127
128 context->ukernel.function[XNN_UARCH_DEFAULT](
129 mr_block_size,
130 nr_block_size,
131 context->kc,
132 context->ks_scaled,
133 (const void**) ((uintptr_t) context->indirect_a + mr_block_start * ks * sizeof(void*)),
134 (const void*) ((uintptr_t) context->packed_w + nr_block_start * context->w_stride + group_index * context->gw_stride),
135 (void*) ((uintptr_t) context->c + group_index * context->gc_stride + mr_block_start * cm_stride + (nr_block_start << context->log2_csize)),
136 cm_stride,
137 context->cn_stride,
138 context->a_offset + group_index * context->ga_stride,
139 context->zero,
140 &context->params);
141}
142
143void xnn_compute_batch_igemm(
Marat Dukhanf196d012020-04-15 11:50:03 -0700144 const struct igemm_context context[restrict XNN_MIN_ELEMENTS(1)],
XNNPACK Teamb455b122019-09-27 18:10:33 -0700145 size_t batch_index,
146 size_t mr_block_start,
147 size_t nr_block_start,
148 size_t mr_block_size,
149 size_t nr_block_size)
150{
151 const size_t ks = context->ks;
152 const size_t cm_stride = context->cm_stride;
153
Marat Dukhan05702cf2020-03-26 15:41:33 -0700154 context->ukernel.function[XNN_UARCH_DEFAULT](
XNNPACK Teamb455b122019-09-27 18:10:33 -0700155 mr_block_size,
156 nr_block_size,
157 context->kc,
158 context->ks_scaled,
159 (const void**) ((uintptr_t) context->indirect_a + mr_block_start * ks * sizeof(void*)),
160 (const void*) ((uintptr_t) context->packed_w + nr_block_start * context->w_stride),
161 (void*) ((uintptr_t) context->c + batch_index * context->bc_stride + mr_block_start * cm_stride + (nr_block_start << context->log2_csize)),
162 cm_stride,
163 context->cn_stride,
164 context->a_offset + batch_index * context->ba_stride,
165 context->zero,
166 &context->params);
167}
168
Fabio Riccardic2146cc2020-08-19 11:15:24 -0700169void xnn_compute_igemm(
170 const struct igemm_context context[restrict XNN_MIN_ELEMENTS(1)],
171 size_t mr_block_start,
172 size_t nr_block_start,
173 size_t mr_block_size,
174 size_t nr_block_size)
175{
176 const size_t ks = context->ks;
177 const size_t cm_stride = context->cm_stride;
178
179 context->ukernel.function[XNN_UARCH_DEFAULT](
180 mr_block_size,
181 nr_block_size,
182 context->kc,
183 context->ks_scaled,
184 (const void**) ((uintptr_t) context->indirect_a + mr_block_start * ks * sizeof(void*)),
185 (const void*) ((uintptr_t) context->packed_w + nr_block_start * context->w_stride),
186 (void*) ((uintptr_t) context->c + mr_block_start * cm_stride + (nr_block_start << context->log2_csize)),
187 cm_stride,
188 context->cn_stride,
189 context->a_offset,
190 context->zero,
191 &context->params);
192}
193
Marat Dukhan49a59692020-03-06 16:58:33 -0800194void xnn_compute_grouped_subgemm2d(
Marat Dukhanf196d012020-04-15 11:50:03 -0700195 const struct subgemm_context context[restrict XNN_MIN_ELEMENTS(1)],
Marat Dukhan29954272020-02-13 17:56:11 -0800196 size_t batch_index,
197 size_t group_index,
198 size_t subkernel_index,
199 size_t slice_y,
200 size_t slice_x_start,
201 size_t nc_block_start,
202 size_t slice_x_max,
203 size_t nc_block_size)
204{
205 const struct subconvolution_params* subconvolution_params = &context->subconvolution_params[subkernel_index];
206
207 if XNN_UNLIKELY(slice_y >= subconvolution_params->slice_height) {
208 return;
209 }
210
211 const size_t slice_width = subconvolution_params->slice_width;
212 if XNN_UNLIKELY(slice_x_start >= slice_width) {
213 return;
214 }
215 const size_t slice_x_size = min(slice_x_max, slice_width - slice_x_start);
216
217 const size_t ax_stride = context->ax_stride;
218 const size_t cx_stride = context->cx_stride;
Marat Dukhan05702cf2020-03-26 15:41:33 -0700219 context->ukernel.function[XNN_UARCH_DEFAULT](
Marat Dukhan29954272020-02-13 17:56:11 -0800220 slice_x_size,
221 nc_block_size,
222 context->kc,
223 (const void*) ((uintptr_t) context->a + group_index * context->ga_stride + slice_y * context->ay_stride + slice_x_start * ax_stride + batch_index * context->ba_stride),
224 ax_stride,
225 (const void*) ((uintptr_t) subconvolution_params->weights + nc_block_start * subconvolution_params->w_stride + group_index * context->gw_stride),
226 (void*) ((uintptr_t) subconvolution_params->output + group_index * context->gc_stride + slice_y * context->cy_stride + slice_x_start * cx_stride + batch_index * context->bc_stride + (nc_block_start << context->log2_csize)),
227 cx_stride,
228 context->cn_stride,
229 &context->params);
230}
231
232void xnn_compute_subgemm2d(
Marat Dukhanf196d012020-04-15 11:50:03 -0700233 const struct subgemm_context context[restrict XNN_MIN_ELEMENTS(1)],
Marat Dukhan29954272020-02-13 17:56:11 -0800234 size_t batch_index,
235 size_t subkernel_index,
236 size_t slice_y,
237 size_t slice_x_start,
238 size_t nc_block_start,
239 size_t slice_x_max,
240 size_t nc_block_size)
241{
242 const struct subconvolution_params* subconvolution_params = &context->subconvolution_params[subkernel_index];
243
244 if XNN_UNLIKELY(slice_y >= subconvolution_params->slice_height) {
245 return;
246 }
247
248 const size_t slice_width = subconvolution_params->slice_width;
249 if XNN_UNLIKELY(slice_x_start >= slice_width) {
250 return;
251 }
252 const size_t slice_x_size = min(slice_x_max, slice_width - slice_x_start);
253
254 const size_t ax_stride = context->ax_stride;
255 const size_t cx_stride = context->cx_stride;
Marat Dukhan05702cf2020-03-26 15:41:33 -0700256 context->ukernel.function[XNN_UARCH_DEFAULT](
Marat Dukhan29954272020-02-13 17:56:11 -0800257 slice_x_size,
258 nc_block_size,
259 context->kc,
260 (const void*) ((uintptr_t) context->a + slice_y * context->ay_stride + slice_x_start * ax_stride + batch_index * context->ba_stride),
261 ax_stride,
262 (const void*) ((uintptr_t) subconvolution_params->weights + nc_block_start * subconvolution_params->w_stride),
263 (void*) ((uintptr_t) subconvolution_params->output + slice_y * context->cy_stride + slice_x_start * cx_stride + batch_index * context->bc_stride + (nc_block_start << context->log2_csize)),
264 cx_stride,
265 context->cn_stride,
266 &context->params);
267}
268
Marat Dukhan49a59692020-03-06 16:58:33 -0800269void xnn_compute_grouped_subconv2d(
Marat Dukhanf196d012020-04-15 11:50:03 -0700270 const struct subconv_context context[restrict XNN_MIN_ELEMENTS(1)],
XNNPACK Teamb455b122019-09-27 18:10:33 -0700271 size_t batch_index,
272 size_t group_index,
273 size_t subkernel_index,
274 size_t slice_y,
275 size_t slice_x_start,
276 size_t nc_block_start,
277 size_t slice_x_max,
278 size_t nc_block_size)
279{
280 const struct subconvolution_params* subconvolution_params = &context->subconvolution_params[subkernel_index];
281
282 if XNN_UNLIKELY(slice_y >= subconvolution_params->slice_height) {
283 return;
284 }
285
286 const size_t slice_width = subconvolution_params->slice_width;
287 if XNN_UNLIKELY(slice_x_start >= slice_width) {
288 return;
289 }
290 const size_t slice_x_size = min(slice_x_max, slice_width - slice_x_start);
291
292 const size_t cx_stride = context->cx_stride;
Marat Dukhan05702cf2020-03-26 15:41:33 -0700293 context->ukernel.function[XNN_UARCH_DEFAULT](
XNNPACK Teamb455b122019-09-27 18:10:33 -0700294 slice_x_size,
295 nc_block_size,
296 context->kc,
297 subconvolution_params->scaled_kernel_size,
298 (const void**) ((uintptr_t) subconvolution_params->indirection_buffer + slice_y * subconvolution_params->indirection_y_stride + slice_x_start * subconvolution_params->indirection_x_stride),
299 (const void*) ((uintptr_t) subconvolution_params->weights + nc_block_start * subconvolution_params->w_stride + group_index * context->gw_stride),
300 (void*) ((uintptr_t) subconvolution_params->output + group_index * context->gc_stride + slice_y * context->cy_stride + slice_x_start * cx_stride + batch_index * context->bc_stride + (nc_block_start << context->log2_csize)),
301 cx_stride,
302 context->cn_stride,
303 context->a_offset + group_index * context->ga_stride + batch_index * context->ba_stride,
304 context->zero,
305 &context->params);
306}
307
308void xnn_compute_subconv2d(
Marat Dukhanf196d012020-04-15 11:50:03 -0700309 const struct subconv_context context[restrict XNN_MIN_ELEMENTS(1)],
XNNPACK Teamb455b122019-09-27 18:10:33 -0700310 size_t batch_index,
311 size_t subkernel_index,
312 size_t slice_y,
313 size_t slice_x_start,
314 size_t nc_block_start,
315 size_t slice_x_max,
316 size_t nc_block_size)
317{
318 const struct subconvolution_params* subconvolution_params = &context->subconvolution_params[subkernel_index];
319
320 if XNN_UNLIKELY(slice_y >= subconvolution_params->slice_height) {
321 return;
322 }
323
324 const size_t slice_width = subconvolution_params->slice_width;
325 if XNN_UNLIKELY(slice_x_start >= slice_width) {
326 return;
327 }
328 const size_t slice_x_size = min(slice_x_max, slice_width - slice_x_start);
329
330 const size_t cx_stride = context->cx_stride;
Marat Dukhan05702cf2020-03-26 15:41:33 -0700331 context->ukernel.function[XNN_UARCH_DEFAULT](
XNNPACK Teamb455b122019-09-27 18:10:33 -0700332 slice_x_size,
333 nc_block_size,
334 context->kc,
335 subconvolution_params->scaled_kernel_size,
336 (const void**) ((uintptr_t) subconvolution_params->indirection_buffer + slice_y * subconvolution_params->indirection_y_stride + slice_x_start * subconvolution_params->indirection_x_stride),
337 (const void*) ((uintptr_t) subconvolution_params->weights + nc_block_start * subconvolution_params->w_stride),
338 (void*) ((uintptr_t) subconvolution_params->output + slice_y * context->cy_stride + slice_x_start * cx_stride + batch_index * context->bc_stride + (nc_block_start << context->log2_csize)),
339 cx_stride,
340 context->cn_stride,
341 context->a_offset + batch_index * context->ba_stride,
342 context->zero,
343 &context->params);
344}
345
Marat Dukhan1f29b802020-05-15 23:46:39 -0700346void xnn_compute_conv2d_hwc2chw(
347 const struct conv2d_context context[restrict XNN_MIN_ELEMENTS(1)],
XNNPACK Teamb455b122019-09-27 18:10:33 -0700348 size_t batch_index,
349 size_t output_y_start,
350 size_t output_y_slice)
351{
Marat Dukhan1f29b802020-05-15 23:46:39 -0700352 context->hwc2chw_ukernel(
XNNPACK Teamb455b122019-09-27 18:10:33 -0700353 context->input_height,
354 context->input_width,
355 output_y_start,
356 output_y_start + output_y_slice,
357 (const void*) ((uintptr_t) context->input + batch_index * context->input_batch_stride),
358 context->zero,
359 context->packed_weights,
360 (void*) ((uintptr_t) context->output + batch_index * context->output_batch_stride),
361 context->input_padding_top,
362 context->output_channels,
363 context->output_height_stride,
364 context->output_channel_stride,
365 &context->params);
366}
367
368void xnn_compute_dwconv_unipass(
Marat Dukhanf196d012020-04-15 11:50:03 -0700369 const struct dwconv_context context[restrict XNN_MIN_ELEMENTS(1)],
Marat Dukhanc79427c2020-10-15 09:04:21 -0700370 size_t batch_index,
XNNPACK Teamb455b122019-09-27 18:10:33 -0700371 size_t output_y)
372{
Marat Dukhanc79427c2020-10-15 09:04:21 -0700373 const void** indirect_input =
374 (const void**) ((uintptr_t) context->indirect_input + output_y * context->indirect_input_height_stride);
375 const size_t input_offset = context->input_offset + batch_index * context->input_batch_stride;
376 void* output = (void*) ((uintptr_t) context->output +
377 batch_index * context->output_batch_stride + output_y * context->output_height_stride);
378
XNNPACK Teamb455b122019-09-27 18:10:33 -0700379 context->unipass_ukernel(
Marat Dukhanc79427c2020-10-15 09:04:21 -0700380 context->groups, context->output_width,
381 indirect_input, context->packed_weights, output,
382 context->indirect_input_width_stride, context->output_increment,
383 input_offset, context->zero,
XNNPACK Teamb455b122019-09-27 18:10:33 -0700384 &context->params);
385}
386
Marat Dukhan1f29b802020-05-15 23:46:39 -0700387void xnn_compute_dwconv2d_chw(
Marat Dukhanf196d012020-04-15 11:50:03 -0700388 const struct dwconv2d_context context[restrict XNN_MIN_ELEMENTS(1)],
XNNPACK Teamb455b122019-09-27 18:10:33 -0700389 size_t batch_index,
390 size_t channel)
391{
Marat Dukhan1f29b802020-05-15 23:46:39 -0700392 context->chw_ukernel(
Erich Elseneda9c112020-05-11 04:40:25 -0700393 context->input_height,
XNNPACK Teamb455b122019-09-27 18:10:33 -0700394 context->input_width,
395 (const void*) ((uintptr_t) context->input + channel * context->input_channel_stride + batch_index * context->input_batch_stride),
396 (const void*) ((uintptr_t) context->packed_weights + channel * context->weights_channel_stride),
Erich Elsen4e5db3d2020-05-07 08:57:47 -0700397 context->zero,
XNNPACK Teamb455b122019-09-27 18:10:33 -0700398 (void*) ((uintptr_t) context->output + channel * context->output_channel_stride + batch_index * context->output_batch_stride),
Erich Elsen4e5db3d2020-05-07 08:57:47 -0700399 context->input_padding_top,
XNNPACK Teamb455b122019-09-27 18:10:33 -0700400 &context->params);
401}
402
Marat Dukhan0e521172020-11-25 13:10:04 -0800403void xnn_compute_depthtospace2d_hwc_contiguous(
404 const struct depthtospace2d_hwc_context* context,
405 size_t batch_input_y,
406 size_t input_x,
407 size_t block_y)
408{
409 const size_t input_width = context->input_width;
410 const size_t elements = context->elements;
411 const void* input = (const void*) ((uintptr_t) context->input +
412 (batch_input_y * input_width + input_x) * context->input_width_stride + block_y * elements);
413 void* output = (void*) ((uintptr_t) context->output +
414 ((batch_input_y * context->block_size + block_y) * input_width + input_x) * elements);
415
416 context->ukernel(
417 elements,
418 input,
419 output,
420 NULL);
421}
422
423void xnn_compute_depthtospace2d_hwc_strided(
424 const struct depthtospace2d_hwc_context* context,
425 size_t batch_input_y,
426 size_t input_x,
427 size_t block_y,
428 size_t block_x)
429{
430 const size_t block_size = context->block_size;
431 const size_t elements = context->elements;
432 const void* input = (const void*) ((uintptr_t) context->input +
433 batch_input_y * context->input_height_stride + input_x * context->input_width_stride + (block_y * block_size + block_x) * elements);
434 void* output = (void*) ((uintptr_t) context->output +
435 (batch_input_y * block_size + block_y) * context->output_height_stride +
436 (input_x * block_size + block_x) * context->output_width_stride);
437
438 context->ukernel(
439 elements,
440 input,
441 output,
442 NULL);
443}
444
Marat Dukhanad71b9a2020-11-20 00:01:51 -0800445void xnn_compute_depthtospace2d_chw2hwc(
446 const struct depthtospace2d_chw2hwc_context* context,
Artsiom Ablavatskibbe85062020-11-05 14:07:37 -0800447 size_t batch_index)
448{
449 context->ukernel(
450 context->output_channels,
451 context->input_height,
452 context->input_width,
453 context->block_size,
454 (const void*) ((uintptr_t) context->input + batch_index * context->input_batch_stride),
455 (void*) ((uintptr_t) context->output + batch_index * context->output_batch_stride),
Marat Dukhan77e93a22021-02-26 11:13:55 -0800456 context->output_channel_stride);
Artsiom Ablavatskibbe85062020-11-05 14:07:37 -0800457}
458
XNNPACK Teamb455b122019-09-27 18:10:33 -0700459void xnn_compute_argmax_pooling_unipass(
Marat Dukhanf196d012020-04-15 11:50:03 -0700460 const struct argmax_pooling_context context[restrict XNN_MIN_ELEMENTS(1)],
XNNPACK Teamb455b122019-09-27 18:10:33 -0700461 size_t batch_index,
462 size_t output_y)
463{
Marat Dukhan329da642019-11-19 21:44:39 -0800464 const void** indirect_input = (const void**) ((uintptr_t) context->indirect_input +
465 output_y * context->indirect_input_height_stride);
466 const size_t input_offset = context->input_offset + batch_index * context->input_batch_stride;
467 void* output = (void*) ((uintptr_t) context->output +
468 batch_index * context->output_batch_stride + output_y * context->output_height_stride);
469 uint32_t* index = (uint32_t*) ((uintptr_t) context->index +
470 batch_index * context->index_batch_stride + output_y * context->index_height_stride);
XNNPACK Teamb455b122019-09-27 18:10:33 -0700471
472 context->unipass_ukernel(
473 context->output_width, context->pooling_size, context->channels,
Marat Dukhan329da642019-11-19 21:44:39 -0800474 indirect_input, input_offset, output, index,
Marat Dukhan447c4f52020-07-17 01:07:28 -0700475 context->input_increment, context->output_increment);
XNNPACK Teamb455b122019-09-27 18:10:33 -0700476}
477
478void xnn_compute_argmax_pooling_multipass(
Marat Dukhanf196d012020-04-15 11:50:03 -0700479 const struct argmax_pooling_context context[restrict XNN_MIN_ELEMENTS(1)],
XNNPACK Teamb455b122019-09-27 18:10:33 -0700480 size_t batch_index,
481 size_t output_y)
482{
Marat Dukhan329da642019-11-19 21:44:39 -0800483 const void** indirect_input = (const void**) ((uintptr_t) context->indirect_input +
484 output_y * context->indirect_input_height_stride);
Frank Barchardd5360722020-05-17 16:10:36 -0700485 const size_t input_offset = context->input_offset + batch_index * context->input_batch_stride;
Marat Dukhan329da642019-11-19 21:44:39 -0800486 void* output = (void*) ((uintptr_t) context->output +
487 batch_index * context->output_batch_stride + output_y * context->output_height_stride);
488 uint32_t* index = (uint32_t*) ((uintptr_t) context->index +
489 batch_index * context->index_batch_stride + output_y * context->index_height_stride);
XNNPACK Teamb455b122019-09-27 18:10:33 -0700490
Marat Dukhand0cf9bd2020-04-13 20:19:19 -0700491 void* multipass_accumulation_buffer = XNN_SIMD_ALLOCA(context->channels * sizeof(float) + XNN_EXTRA_BYTES);
492 void* multipass_index_buffer = XNN_SIMD_ALLOCA(context->channels * sizeof(uint32_t) + XNN_EXTRA_BYTES);
XNNPACK Teamb455b122019-09-27 18:10:33 -0700493
494 context->multipass_ukernel(
495 context->output_width, context->pooling_size, context->channels,
Marat Dukhan329da642019-11-19 21:44:39 -0800496 indirect_input, input_offset, multipass_accumulation_buffer, multipass_index_buffer, output, index,
Marat Dukhan447c4f52020-07-17 01:07:28 -0700497 context->input_increment, context->output_increment);
XNNPACK Teamb455b122019-09-27 18:10:33 -0700498}
499
500void xnn_compute_max_pooling(
Marat Dukhanf196d012020-04-15 11:50:03 -0700501 const struct max_pooling_context context[restrict XNN_MIN_ELEMENTS(1)],
XNNPACK Teamb455b122019-09-27 18:10:33 -0700502 size_t batch_index,
503 size_t output_y)
504{
Marat Dukhan329da642019-11-19 21:44:39 -0800505 const void** indirect_input = (const void**) ((uintptr_t) context->indirect_input +
506 output_y * context->indirect_input_height_stride);
507 const size_t input_offset = context->input_offset + batch_index * context->input_batch_stride;
508 void* output = (void*) ((uintptr_t) context->output +
509 batch_index * context->output_batch_stride + output_y * context->output_height_stride);
XNNPACK Teamb455b122019-09-27 18:10:33 -0700510
511 context->ukernel(
512 context->output_width, context->pooling_size, context->channels,
Marat Dukhan329da642019-11-19 21:44:39 -0800513 indirect_input, input_offset, output,
XNNPACK Teamb455b122019-09-27 18:10:33 -0700514 context->input_increment, context->output_increment,
515 &context->params);
516}
517
518void xnn_compute_unpooling(
Marat Dukhanf196d012020-04-15 11:50:03 -0700519 const struct unpooling_context context[restrict XNN_MIN_ELEMENTS(1)],
XNNPACK Teamb455b122019-09-27 18:10:33 -0700520 size_t input_y,
521 size_t input_x)
522{
523 const void* input = (const void*) ((uintptr_t) context->input +
524 input_y * context->input_height_stride + input_x * context->input_width_stride);
525 const uint32_t* index = (const uint32_t*) ((uintptr_t) context->index +
526 input_y * context->index_height_stride + input_x * context->index_width_stride);
527 void** indirect_output =
528 (void**) ((uintptr_t) context->indirect_output +
529 input_y * context->indirect_output_height_stride + input_x * context->indirect_output_width_stride);
530
531 context->ukernel(
532 context->pooling_size,
533 context->channels,
534 context->fill_value,
535 input, index, indirect_output);
536}
537
538void xnn_compute_average_pooling_unipass(
Marat Dukhanf196d012020-04-15 11:50:03 -0700539 const struct average_pooling_context context[restrict XNN_MIN_ELEMENTS(1)],
XNNPACK Teamb455b122019-09-27 18:10:33 -0700540 size_t batch_index,
541 size_t output_y)
542{
543 const void** indirect_input =
Marat Dukhan96171aa2020-02-27 18:26:48 -0800544 (const void**) ((uintptr_t) context->indirect_input + output_y * context->indirect_input_height_stride);
545 const size_t input_offset = context->input_offset + batch_index * context->input_batch_stride;
546 void* output = (void*) ((uintptr_t) context->output +
547 batch_index * context->output_batch_stride + output_y * context->output_height_stride);
XNNPACK Teamb455b122019-09-27 18:10:33 -0700548
549 context->unipass_ukernel(
550 context->output_width, context->pooling_size, context->channels,
Marat Dukhan96171aa2020-02-27 18:26:48 -0800551 indirect_input, input_offset, context->zero, output,
XNNPACK Teamb455b122019-09-27 18:10:33 -0700552 context->input_increment, context->output_increment,
553 &context->params);
554}
555
556void xnn_compute_average_pooling_multipass(
Marat Dukhanf196d012020-04-15 11:50:03 -0700557 const struct average_pooling_context context[restrict XNN_MIN_ELEMENTS(1)],
XNNPACK Teamb455b122019-09-27 18:10:33 -0700558 size_t batch_index,
559 size_t output_y)
560{
561 const void** indirect_input =
Marat Dukhan96171aa2020-02-27 18:26:48 -0800562 (const void**) ((uintptr_t) context->indirect_input + output_y * context->indirect_input_height_stride);
563 const size_t input_offset = context->input_offset + batch_index * context->input_batch_stride;
564 void* output = (void*) ((uintptr_t) context->output +
565 batch_index * context->output_batch_stride + output_y * context->output_height_stride);
Marat Dukhand0cf9bd2020-04-13 20:19:19 -0700566
567 void* multipass_buffer =
568 XNN_SIMD_ALLOCA(context->channels * sizeof(int32_t) + XNN_EXTRA_BYTES * sizeof(int32_t) / sizeof(uint8_t));
XNNPACK Teamb455b122019-09-27 18:10:33 -0700569
570 context->multipass_ukernel(
571 context->output_width, context->pooling_size, context->channels,
Marat Dukhan96171aa2020-02-27 18:26:48 -0800572 indirect_input, input_offset, context->zero, multipass_buffer, output,
XNNPACK Teamb455b122019-09-27 18:10:33 -0700573 context->input_increment, context->output_increment,
574 &context->params);
575}
576
577void xnn_compute_pixelwise_average_pooling_unipass(
Marat Dukhanf196d012020-04-15 11:50:03 -0700578 const struct pixelwise_average_pooling_context context[restrict XNN_MIN_ELEMENTS(1)],
XNNPACK Teamb455b122019-09-27 18:10:33 -0700579 size_t batch_index,
580 size_t output_y)
581{
582 const void** indirect_input =
Marat Dukhan96171aa2020-02-27 18:26:48 -0800583 (const void**) ((uintptr_t) context->indirect_input + output_y * context->indirect_input_height_stride);
584 const size_t input_offset = context->input_offset + batch_index * context->input_batch_stride;
XNNPACK Teamb455b122019-09-27 18:10:33 -0700585 const void* pixelwise_buffer =
586 (const void*) ((uintptr_t) context->pixelwise_buffer + output_y * context->pixelwise_buffer_height_stride);
Marat Dukhan96171aa2020-02-27 18:26:48 -0800587 void* output = (void*) ((uintptr_t) context->output +
588 batch_index * context->output_batch_stride + output_y * context->output_height_stride);
XNNPACK Teamb455b122019-09-27 18:10:33 -0700589
590 context->unipass_ukernel(
591 context->output_width, context->pooling_size, context->channels,
Marat Dukhan96171aa2020-02-27 18:26:48 -0800592 indirect_input, input_offset, context->zero, pixelwise_buffer, output,
XNNPACK Teamb455b122019-09-27 18:10:33 -0700593 context->input_increment, context->output_increment,
594 &context->params);
595}
596
597void xnn_compute_pixelwise_average_pooling_multipass(
Marat Dukhanf196d012020-04-15 11:50:03 -0700598 const struct pixelwise_average_pooling_context context[restrict XNN_MIN_ELEMENTS(1)],
XNNPACK Teamb455b122019-09-27 18:10:33 -0700599 size_t batch_index,
600 size_t output_y)
601{
602 const void** indirect_input =
Marat Dukhan96171aa2020-02-27 18:26:48 -0800603 (const void**) ((uintptr_t) context->indirect_input + output_y * context->indirect_input_height_stride);
604 const size_t input_offset = context->input_offset + batch_index * context->input_batch_stride;
XNNPACK Teamb455b122019-09-27 18:10:33 -0700605 const void* pixelwise_buffer =
606 (const void*) ((uintptr_t) context->pixelwise_buffer + output_y * context->pixelwise_buffer_height_stride);
Marat Dukhan96171aa2020-02-27 18:26:48 -0800607 void* output = (void*) ((uintptr_t) context->output +
608 batch_index * context->output_batch_stride + output_y * context->output_height_stride);
Marat Dukhand0cf9bd2020-04-13 20:19:19 -0700609
610 void* multipass_buffer = XNN_SIMD_ALLOCA(context->channels * sizeof(int32_t) + XNN_EXTRA_BYTES * sizeof(int32_t) / sizeof(uint8_t));
XNNPACK Teamb455b122019-09-27 18:10:33 -0700611
612 context->multipass_ukernel(
613 context->output_width, context->pooling_size, context->channels,
Marat Dukhan96171aa2020-02-27 18:26:48 -0800614 indirect_input, input_offset, context->zero, pixelwise_buffer, multipass_buffer, output,
XNNPACK Teamb455b122019-09-27 18:10:33 -0700615 context->input_increment, context->output_increment,
616 &context->params);
617}
618
Marat Dukhanefc47b82019-11-18 09:25:38 -0800619void xnn_compute_global_average_pooling_nwc_unipass(
Marat Dukhanf196d012020-04-15 11:50:03 -0700620 const struct global_average_pooling_nwc_context context[restrict XNN_MIN_ELEMENTS(1)],
XNNPACK Teamb455b122019-09-27 18:10:33 -0700621 size_t batch_index)
622{
623 const void* input =
624 (const void*) ((uintptr_t) context->input + batch_index * context->input_batch_stride);
625 void* output =
626 (void*) ((uintptr_t) context->output + batch_index * context->output_batch_stride);
627
628 context->unipass_ukernel(
629 context->input_elements,
630 context->channels,
631 input,
632 context->input_pixel_stride,
633 context->zero,
634 output,
635 &context->params);
636}
637
Marat Dukhanefc47b82019-11-18 09:25:38 -0800638void xnn_compute_global_average_pooling_nwc_multipass(
Marat Dukhanf196d012020-04-15 11:50:03 -0700639 const struct global_average_pooling_nwc_context context[restrict XNN_MIN_ELEMENTS(1)],
XNNPACK Teamb455b122019-09-27 18:10:33 -0700640 size_t batch_index)
641{
642 const void* input =
643 (const void*) ((uintptr_t) context->input + batch_index * context->input_batch_stride);
644 void* output =
645 (void*) ((uintptr_t) context->output + batch_index * context->output_batch_stride);
Marat Dukhand0cf9bd2020-04-13 20:19:19 -0700646
647 void* multipass_buffer =
648 XNN_SIMD_ALLOCA(context->channels * sizeof(int32_t) + XNN_EXTRA_BYTES * sizeof(int32_t) / sizeof(uint8_t));
XNNPACK Teamb455b122019-09-27 18:10:33 -0700649
650 context->multipass_ukernel(
651 context->input_elements,
652 context->channels,
653 input,
654 context->input_pixel_stride,
655 context->zero,
656 multipass_buffer,
657 output,
658 &context->params);
659}
660
Marat Dukhanefc47b82019-11-18 09:25:38 -0800661void xnn_compute_global_average_pooling_ncw(
Marat Dukhanf196d012020-04-15 11:50:03 -0700662 const struct global_average_pooling_ncw_context context[restrict XNN_MIN_ELEMENTS(1)],
XNNPACK Teamb455b122019-09-27 18:10:33 -0700663 size_t batch_index,
664 size_t channels_start,
665 size_t channels_slice)
666{
Marat Dukhanefc47b82019-11-18 09:25:38 -0800667 const void* input = (const void*) ((uintptr_t) context->input +
668 channels_start * context->input_channel_stride + batch_index * context->input_batch_stride);
669 void* output = (void*) ((uintptr_t) context->output +
670 channels_start * context->output_channel_stride + batch_index * context->output_batch_stride);
XNNPACK Teamb455b122019-09-27 18:10:33 -0700671
672 context->ukernel(
673 context->input_elements,
674 channels_slice,
675 input,
676 output,
677 &context->params);
678}
679
Marat Dukhan69722492019-11-11 19:55:50 -0800680void xnn_compute_resize_bilinear(
Marat Dukhanf196d012020-04-15 11:50:03 -0700681 const struct resize_bilinear_context context[restrict XNN_MIN_ELEMENTS(1)],
Marat Dukhan69722492019-11-11 19:55:50 -0800682 size_t batch_index,
683 size_t pixel_start,
684 size_t pixel_range)
685{
686 void* output =
687 (void*) ((uintptr_t) context->output + pixel_start * context->output_pixel_stride + batch_index * context->output_batch_stride);
688
689 context->ukernel(
690 pixel_range,
691 context->scaled_channels,
692 context->indirect_input + pixel_start * 4,
693 context->input_offset + batch_index * context->input_batch_stride,
Marat Dukhan02072e62020-04-14 02:59:11 -0700694 (const void*) ((uintptr_t) context->packed_weights + (pixel_start << context->log2_wsize)),
Marat Dukhan69722492019-11-11 19:55:50 -0800695 output,
696 context->output_pixel_stride - context->scaled_channels);
697}
698
Artsiom Ablavatski97918102020-10-27 15:52:59 -0700699void xnn_compute_resize_bilinear_chw(
700 const struct resize_bilinear_chw_context context[restrict XNN_MIN_ELEMENTS(1)],
701 size_t batch_index,
702 size_t channel_start,
703 size_t channel_range)
704{
705 void* output =
706 (void*) ((uintptr_t) context->output + channel_start * context->output_channel_stride + batch_index * context->output_batch_stride);
707 const size_t input_offset = context->input_offset + batch_index * context->input_batch_stride + channel_start * context->input_channel_stride;
708
709 context->ukernel(
710 context->output_pixels,
711 channel_range,
712 context->indirect_input,
713 input_offset,
714 context->packed_weights,
715 output,
716 context->input_channel_stride);
717}
718
XNNPACK Teamb455b122019-09-27 18:10:33 -0700719void xnn_compute_prelu(
Marat Dukhanf196d012020-04-15 11:50:03 -0700720 const struct prelu_context context[restrict XNN_MIN_ELEMENTS(1)],
XNNPACK Teamb455b122019-09-27 18:10:33 -0700721 size_t batch_start,
722 size_t batch_range)
723{
724 const size_t x_stride = context->x_stride;
725 const size_t y_stride = context->y_stride;
726 const void* x = (const void*) ((uintptr_t) context->x + x_stride * batch_start);
727 void* y = (void*) ((uintptr_t) context->y + y_stride * batch_start);
728
Marat Dukhanc8230a42020-02-24 00:00:35 -0800729 context->ukernel(batch_range, context->n, x, x_stride, context->w, y, y_stride);
XNNPACK Teamb455b122019-09-27 18:10:33 -0700730}
731
Marat Dukhan4662b192020-05-21 15:52:03 -0700732void xnn_compute_pad_5d(
733 const struct pad_context context[restrict XNN_MIN_ELEMENTS(1)],
Marat Dukhan892310c2020-12-05 15:53:20 -0800734 size_t i, size_t j, size_t k, size_t l, size_t m)
Marat Dukhan4662b192020-05-21 15:52:03 -0700735{
Marat Dukhan4662b192020-05-21 15:52:03 -0700736 const void* input = (const void*) ((uintptr_t) context->input +
737 i * context->input_stride[4] + j * context->input_stride[3] + k * context->input_stride[2] + l * context->input_stride[1] + m * context->input_stride[0]);
738 void* output = (void*) ((uintptr_t) context->output +
739 i * context->output_stride[4] + j * context->output_stride[3] + k * context->output_stride[2] + l * context->output_stride[1] + m * context->output_stride[0]);
740
741 const size_t i_padding = context->pre_paddings[5];
742 const size_t j_padding = context->pre_paddings[4];
743 const size_t k_padding = context->pre_paddings[3];
744 const size_t l_padding = context->pre_paddings[2];
745 const size_t m_padding = context->pre_paddings[1];
746
747 const size_t i_size = context->input_size[5];
748 const size_t j_size = context->input_size[4];
749 const size_t k_size = context->input_size[3];
750 const size_t l_size = context->input_size[2];
751 const size_t m_size = context->input_size[1];
752
753 if XNN_LIKELY(i - i_padding < i_size && j - j_padding < j_size && k - k_padding < k_size &&
754 l - l_padding < l_size && m - m_padding < m_size)
755 {
756 context->pad_ukernel(
757 1 /* rows */,
758 context->input_size[0], context->pre_paddings[0], context->post_paddings[0],
Marat Dukhan0461f2d2021-08-08 12:36:29 -0700759 input, 0 /* input stride */, output, 0 /* output stride */,
760 context->padding_value);
Marat Dukhan4662b192020-05-21 15:52:03 -0700761 } else {
Marat Dukhan933051b2021-08-07 16:26:15 -0700762 context->fill_ukernel(1 /* rows */, context->output_size[0], output, 0 /* output stride */, context->padding_value);
Marat Dukhan4662b192020-05-21 15:52:03 -0700763 }
764}
765
Marat Dukhanfc2b96e2019-12-03 12:04:04 -0800766void xnn_compute_elementwise_binary_5d(
Marat Dukhanf196d012020-04-15 11:50:03 -0700767 const struct elementwise_binary_context context[restrict XNN_MIN_ELEMENTS(1)],
Marat Dukhan892310c2020-12-05 15:53:20 -0800768 size_t i, size_t j, size_t k, size_t l, size_t m)
Marat Dukhanca2733c2019-11-15 23:21:17 -0800769{
Marat Dukhanca2733c2019-11-15 23:21:17 -0800770 const void* a = (const void*) ((uintptr_t) context->a +
Marat Dukhanfc2b96e2019-12-03 12:04:04 -0800771 i * context->a_stride[0] + j * context->a_stride[1] + k * context->a_stride[2] + l * context->a_stride[3] + m * context->a_stride[4]);
Marat Dukhanca2733c2019-11-15 23:21:17 -0800772 const void* b = (const void*) ((uintptr_t) context->b +
Marat Dukhanfc2b96e2019-12-03 12:04:04 -0800773 i * context->b_stride[0] + j * context->b_stride[1] + k * context->b_stride[2] + l * context->b_stride[3] + m * context->b_stride[4]);
Marat Dukhanca2733c2019-11-15 23:21:17 -0800774 void* y = (void*) ((uintptr_t) context->y +
Marat Dukhanfc2b96e2019-12-03 12:04:04 -0800775 i * context->y_stride[0] + j * context->y_stride[1] + k * context->y_stride[2] + l * context->y_stride[3] + m * context->y_stride[4]);
Marat Dukhanca2733c2019-11-15 23:21:17 -0800776 context->ukernel(context->elements, a, b, y, &context->params);
777}
778
XNNPACK Teamb455b122019-09-27 18:10:33 -0700779void xnn_compute_channel_shuffle_fixed(
Marat Dukhanf196d012020-04-15 11:50:03 -0700780 const struct channel_shuffle_context context[restrict XNN_MIN_ELEMENTS(1)],
XNNPACK Teamb455b122019-09-27 18:10:33 -0700781 size_t index)
782{
783 const void* x = (const void*) ((uintptr_t) context->x + index * context->x_stride);
784 void* y = (void*) ((uintptr_t) context->y + index * context->y_stride);
785
786 context->fixed_ukernel(context->n, x, y);
787}
788
789void xnn_compute_channel_shuffle_variable(
Marat Dukhanf196d012020-04-15 11:50:03 -0700790 const struct channel_shuffle_context context[restrict XNN_MIN_ELEMENTS(1)],
XNNPACK Teamb455b122019-09-27 18:10:33 -0700791 size_t index)
792{
793 const void* x = (const void*) ((uintptr_t) context->x + index * context->x_stride);
794 void* y = (void*) ((uintptr_t) context->y + index * context->y_stride);
795
796 context->variable_ukernel(context->n, context->m, x, y);
797}
798
799void xnn_compute_lut_strided(
Marat Dukhanf196d012020-04-15 11:50:03 -0700800 const struct lut_strided_context context[restrict XNN_MIN_ELEMENTS(1)],
XNNPACK Teamb455b122019-09-27 18:10:33 -0700801 size_t batch_index)
802{
803 const void* x = (const void*) ((uintptr_t) context->x + context->x_stride * batch_index);
804 void* y = (void*) ((uintptr_t) context->y + context->y_stride * batch_index);
805
Marat Dukhand67539d2021-09-08 23:06:03 -0700806 context->ukernel(context->n, x, y, context->t);
XNNPACK Teamb455b122019-09-27 18:10:33 -0700807}
808
809void xnn_compute_lut_contiguous(
Marat Dukhanf196d012020-04-15 11:50:03 -0700810 const struct lut_contiguous_context context[restrict XNN_MIN_ELEMENTS(1)],
XNNPACK Teamb455b122019-09-27 18:10:33 -0700811 size_t offset,
812 size_t size)
813{
814 const void* x = (const void*) ((uintptr_t) context->x + offset);
815 void* y = (void*) ((uintptr_t) context->y + offset);
816
Marat Dukhand67539d2021-09-08 23:06:03 -0700817 context->ukernel(size, x, y, context->t);
XNNPACK Teamb455b122019-09-27 18:10:33 -0700818}
819
820void xnn_compute_univector_strided(
Marat Dukhanf196d012020-04-15 11:50:03 -0700821 const struct univector_strided_context context[restrict XNN_MIN_ELEMENTS(1)],
XNNPACK Teamb455b122019-09-27 18:10:33 -0700822 size_t batch_index,
Marat Dukhan57256c52021-12-08 15:04:17 -0800823 size_t batch_range)
XNNPACK Teamb455b122019-09-27 18:10:33 -0700824{
Marat Dukhan57256c52021-12-08 15:04:17 -0800825 const size_t x_stride = context->x_stride;
826 const size_t y_stride = context->y_stride;
XNNPACK Teamb455b122019-09-27 18:10:33 -0700827
Marat Dukhan57256c52021-12-08 15:04:17 -0800828 const void* x = (const void*) ((uintptr_t) context->x + x_stride * batch_index);
829 void* y = (void*) ((uintptr_t) context->y + y_stride * batch_index);
830 do {
831 context->ukernel(context->n, x, y, &context->params);
832 x = (const void*) ((uintptr_t) x + x_stride);
833 y = (void*) ((uintptr_t) y + y_stride);
834 } while (--batch_range != 0);
XNNPACK Teamb455b122019-09-27 18:10:33 -0700835}
836
837void xnn_compute_univector_contiguous(
Marat Dukhanf196d012020-04-15 11:50:03 -0700838 const struct univector_contiguous_context context[restrict XNN_MIN_ELEMENTS(1)],
XNNPACK Teamb455b122019-09-27 18:10:33 -0700839 size_t offset,
840 size_t size)
841{
Marat Dukhan354b2632021-12-08 14:35:13 -0800842 const uint32_t log2_xsize = context->log2_xsize;
843 const uint32_t log2_ysize = context->log2_ysize;
Marat Dukhan4bdc9f52021-12-09 15:36:06 -0800844 const void* x = (const void*) ((uintptr_t) context->x + offset);
845 void* y = (void*) ((uintptr_t) context->y + ((offset >> log2_xsize) << log2_ysize));
XNNPACK Teamb455b122019-09-27 18:10:33 -0700846 context->ukernel(size, x, y, &context->params);
847}
848
Marat Dukhanfd8e6892020-01-27 15:25:25 -0800849void xnn_compute_u8_softmax(
Marat Dukhanf196d012020-04-15 11:50:03 -0700850 const struct u8_softmax_context context[restrict XNN_MIN_ELEMENTS(1)],
XNNPACK Teamb455b122019-09-27 18:10:33 -0700851 size_t batch_index)
852{
853 const uint8_t* x = (const uint8_t*) ((uintptr_t) context->x + context->x_stride * batch_index);
854 uint8_t* y = (uint8_t*) ((uintptr_t) context->y + context->y_stride * batch_index);
855 const size_t n = context->n;
856
857 uint8_t x_max = 0;
858 context->rmax_ukernel(n, x, &x_max);
859 const size_t adjustment = x_max ^ 255;
860 const uint32_t* t = (const uint32_t*) context->t + adjustment;
861 context->lut_norm_ukernel(n, x, t, y);
862}
863
Marat Dukhanfd8e6892020-01-27 15:25:25 -0800864void xnn_compute_f32_three_pass_softmax(
Marat Dukhanf196d012020-04-15 11:50:03 -0700865 const struct f32_three_pass_softmax_context context[restrict XNN_MIN_ELEMENTS(1)],
Marat Dukhan1edc4542020-01-27 12:40:13 -0800866 size_t batch_index)
867{
868 const float* x = (const float*) ((uintptr_t) context->x + context->x_stride * batch_index);
869 float* y = (float*) ((uintptr_t) context->y + context->y_stride * batch_index);
870 const size_t n = context->n;
871
872 // First pass: reduce-max
873 float x_max;
874 context->rmax_ukernel(n, x, &x_max);
875
876 // Second pass: reduce-add & store exp(x-x_max)
877 float y_sum;
Marat Dukhan4a5c7712022-01-05 22:43:13 -0800878 context->raddstoreexpminusmax_ukernel(n, x, &x_max, y, &y_sum, &context->expminus_params);
Marat Dukhan1edc4542020-01-27 12:40:13 -0800879
880 // Third pass: scale y
881 const float y_scale = 1.0f / y_sum;
Marat Dukhan4a5c7712022-01-05 22:43:13 -0800882 context->vmulc_ukernel(n, y, &y_scale, y, &context->minmax_params);
Marat Dukhan1edc4542020-01-27 12:40:13 -0800883}
884
XNNPACK Teamb455b122019-09-27 18:10:33 -0700885void xnn_compute_vmulcaddc(
Marat Dukhanf196d012020-04-15 11:50:03 -0700886 const struct vmulcaddc_context context[restrict XNN_MIN_ELEMENTS(1)],
XNNPACK Teamb455b122019-09-27 18:10:33 -0700887 size_t batch_start,
888 size_t batch_size)
889{
890 const size_t x_stride = context->x_stride;
891 const size_t y_stride = context->y_stride;
892
893 const void* x = (const void*) ((uintptr_t) context->x + x_stride * batch_start);
894 void* y = (void*) ((uintptr_t) context->y + y_stride * batch_start);
895
896 context->ukernel(
897 batch_size,
898 context->n,
899 x, x_stride,
900 context->w,
901 y, y_stride,
902 &context->params);
903}
904
Marat Dukhan05702cf2020-03-26 15:41:33 -0700905#if XNN_MAX_UARCH_TYPES > 1
906 void xnn_compute_hmp_grouped_gemm(
Marat Dukhanf196d012020-04-15 11:50:03 -0700907 const struct gemm_context context[restrict XNN_MIN_ELEMENTS(1)],
Marat Dukhan05702cf2020-03-26 15:41:33 -0700908 uint32_t uarch_index,
909 size_t group_index,
910 size_t mr_block_start,
911 size_t nr_block_start,
912 size_t mr_block_size,
913 size_t nr_block_size)
914 {
915 const size_t k_scaled = context->k_scaled;
916 const size_t a_stride = context->a_stride;
917 const size_t cm_stride = context->cm_stride;
918
919 context->ukernel.function[uarch_index](
920 mr_block_size,
921 nr_block_size,
922 k_scaled,
923 (const void*) ((uintptr_t) context->a + mr_block_start * a_stride + group_index * k_scaled),
924 a_stride,
925 (const void*) ((uintptr_t) context->packed_w + nr_block_start * context->w_stride + group_index * context->wg_stride),
926 (void*) ((uintptr_t) context->c + mr_block_start * cm_stride + (nr_block_start << context->log2_csize) + group_index * context->cg_stride),
927 cm_stride,
928 context->cn_stride,
929 &context->params);
930 }
931
932 void xnn_compute_hmp_gemm(
Marat Dukhanf196d012020-04-15 11:50:03 -0700933 const struct gemm_context context[restrict XNN_MIN_ELEMENTS(1)],
Marat Dukhan05702cf2020-03-26 15:41:33 -0700934 uint32_t uarch_index,
935 size_t mr_block_start,
936 size_t nr_block_start,
937 size_t mr_block_size,
938 size_t nr_block_size)
939 {
940 const size_t a_stride = context->a_stride;
941 const size_t cm_stride = context->cm_stride;
942
943 context->ukernel.function[uarch_index](
944 mr_block_size,
945 nr_block_size,
946 context->k_scaled,
947 (const void*) ((uintptr_t) context->a + mr_block_start * a_stride),
948 a_stride,
949 (const void*) ((uintptr_t) context->packed_w + nr_block_start * context->w_stride),
950 (void*) ((uintptr_t) context->c + mr_block_start * cm_stride + (nr_block_start << context->log2_csize)),
951 cm_stride,
952 context->cn_stride,
953 &context->params);
954 }
955
Fabio Riccardic2146cc2020-08-19 11:15:24 -0700956 void xnn_compute_hmp_grouped_batch_igemm(
Marat Dukhanf196d012020-04-15 11:50:03 -0700957 const struct igemm_context context[restrict XNN_MIN_ELEMENTS(1)],
Marat Dukhan05702cf2020-03-26 15:41:33 -0700958 uint32_t uarch_index,
959 size_t batch_index,
960 size_t group_index,
961 size_t mr_block_start,
962 size_t nr_block_start,
963 size_t mr_block_size,
964 size_t nr_block_size)
965 {
966 const size_t ks = context->ks;
967 const size_t cm_stride = context->cm_stride;
968
969 context->ukernel.function[uarch_index](
970 mr_block_size,
971 nr_block_size,
972 context->kc,
973 context->ks_scaled,
974 (const void**) ((uintptr_t) context->indirect_a + mr_block_start * ks * sizeof(void*)),
975 (const void*) ((uintptr_t) context->packed_w + nr_block_start * context->w_stride + group_index * context->gw_stride),
976 (void*) ((uintptr_t) context->c + group_index * context->gc_stride + batch_index * context->bc_stride + mr_block_start * cm_stride + (nr_block_start << context->log2_csize)),
977 cm_stride,
978 context->cn_stride,
979 context->a_offset + group_index * context->ga_stride + batch_index * context->ba_stride,
980 context->zero,
981 &context->params);
982 }
983
Fabio Riccardic2146cc2020-08-19 11:15:24 -0700984 void xnn_compute_hmp_grouped_igemm(
985 const struct igemm_context context[restrict XNN_MIN_ELEMENTS(1)],
986 uint32_t uarch_index,
987 size_t group_index,
988 size_t mr_block_start,
989 size_t nr_block_start,
990 size_t mr_block_size,
991 size_t nr_block_size)
992 {
993 const size_t ks = context->ks;
994 const size_t cm_stride = context->cm_stride;
995
996 context->ukernel.function[uarch_index](
997 mr_block_size,
998 nr_block_size,
999 context->kc,
1000 context->ks_scaled,
1001 (const void**) ((uintptr_t) context->indirect_a + mr_block_start * ks * sizeof(void*)),
1002 (const void*) ((uintptr_t) context->packed_w + nr_block_start * context->w_stride + group_index * context->gw_stride),
1003 (void*) ((uintptr_t) context->c + group_index * context->gc_stride + mr_block_start * cm_stride + (nr_block_start << context->log2_csize)),
1004 cm_stride,
1005 context->cn_stride,
1006 context->a_offset + group_index * context->ga_stride,
1007 context->zero,
1008 &context->params);
1009 }
1010
1011 void xnn_compute_batch_hmp_igemm(
Marat Dukhanf196d012020-04-15 11:50:03 -07001012 const struct igemm_context context[restrict XNN_MIN_ELEMENTS(1)],
Marat Dukhan05702cf2020-03-26 15:41:33 -07001013 uint32_t uarch_index,
1014 size_t batch_index,
1015 size_t mr_block_start,
1016 size_t nr_block_start,
1017 size_t mr_block_size,
1018 size_t nr_block_size)
1019 {
1020 const size_t ks = context->ks;
1021 const size_t cm_stride = context->cm_stride;
1022
1023 context->ukernel.function[uarch_index](
1024 mr_block_size,
1025 nr_block_size,
1026 context->kc,
1027 context->ks_scaled,
1028 (const void**) ((uintptr_t) context->indirect_a + mr_block_start * ks * sizeof(void*)),
1029 (const void*) ((uintptr_t) context->packed_w + nr_block_start * context->w_stride),
1030 (void*) ((uintptr_t) context->c + batch_index * context->bc_stride + mr_block_start * cm_stride + (nr_block_start << context->log2_csize)),
1031 cm_stride,
1032 context->cn_stride,
1033 context->a_offset + batch_index * context->ba_stride,
1034 context->zero,
1035 &context->params);
1036 }
Fabio Riccardic2146cc2020-08-19 11:15:24 -07001037
1038 void xnn_compute_hmp_igemm(
1039 const struct igemm_context context[restrict XNN_MIN_ELEMENTS(1)],
1040 uint32_t uarch_index,
1041 size_t mr_block_start,
1042 size_t nr_block_start,
1043 size_t mr_block_size,
1044 size_t nr_block_size)
1045 {
1046 const size_t ks = context->ks;
1047 const size_t cm_stride = context->cm_stride;
1048
1049 context->ukernel.function[uarch_index](
1050 mr_block_size,
1051 nr_block_size,
1052 context->kc,
1053 context->ks_scaled,
1054 (const void**) ((uintptr_t) context->indirect_a + mr_block_start * ks * sizeof(void*)),
1055 (const void*) ((uintptr_t) context->packed_w + nr_block_start * context->w_stride),
1056 (void*) ((uintptr_t) context->c + mr_block_start * cm_stride + (nr_block_start << context->log2_csize)),
1057 cm_stride,
1058 context->cn_stride,
1059 context->a_offset,
1060 context->zero,
1061 &context->params);
1062 }
Marat Dukhan05702cf2020-03-26 15:41:33 -07001063#endif // XNN_MAX_UARCH_TYPES > 1
1064
XNNPACK Teamb455b122019-09-27 18:10:33 -07001065enum xnn_status xnn_run_operator(xnn_operator_t op, pthreadpool_t threadpool)
1066{
Marat Dukhan854fb6b2020-06-19 12:33:44 -07001067 if ((xnn_params.init_flags & XNN_INIT_FLAG_XNNPACK) == 0) {
XNNPACK Teamb455b122019-09-27 18:10:33 -07001068 xnn_log_error("failed to run operator: XNNPACK is not initialized");
1069 return xnn_status_uninitialized;
1070 }
1071 switch (op->state) {
1072 case xnn_run_state_invalid:
1073 xnn_log_error("failed to run operator: operator was not successfully setup");
1074 return xnn_status_invalid_state;
1075 case xnn_run_state_ready:
1076 break;
1077 case xnn_run_state_skip:
1078 return xnn_status_success;
1079 }
1080
Marat Dukhan942359e2021-06-09 00:38:56 -07001081 uint32_t flags = PTHREADPOOL_FLAG_DISABLE_DENORMALS;
1082 if (op->flags & XNN_FLAG_YIELD_WORKERS) {
1083 flags |= PTHREADPOOL_FLAG_YIELD_WORKERS;
1084 }
XNNPACK Teamb455b122019-09-27 18:10:33 -07001085 switch (op->compute.type) {
1086 case xnn_parallelization_type_invalid:
1087 break;
1088 case xnn_parallelization_type_1d:
1089 assert(op->compute.range[0] != 0);
1090 pthreadpool_parallelize_1d(
1091 threadpool,
1092 op->compute.task_1d,
1093 &op->context,
1094 op->compute.range[0],
Marat Dukhan942359e2021-06-09 00:38:56 -07001095 flags);
XNNPACK Teamb455b122019-09-27 18:10:33 -07001096 break;
1097 case xnn_parallelization_type_1d_tile_1d:
1098 assert(op->compute.range[0] != 0);
1099 assert(op->compute.tile[0] != 0);
1100 pthreadpool_parallelize_1d_tile_1d(
1101 threadpool,
1102 op->compute.task_1d_tile_1d,
1103 &op->context,
1104 op->compute.range[0],
1105 op->compute.tile[0],
Marat Dukhan942359e2021-06-09 00:38:56 -07001106 flags);
XNNPACK Teamb455b122019-09-27 18:10:33 -07001107 break;
1108 case xnn_parallelization_type_2d:
1109 assert(op->compute.range[0] != 0);
1110 assert(op->compute.range[1] != 0);
1111 pthreadpool_parallelize_2d(
1112 threadpool,
1113 op->compute.task_2d,
1114 &op->context,
1115 op->compute.range[0], op->compute.range[1],
Marat Dukhan942359e2021-06-09 00:38:56 -07001116 flags);
XNNPACK Teamb455b122019-09-27 18:10:33 -07001117 break;
1118 case xnn_parallelization_type_2d_tile_1d:
1119 assert(op->compute.range[0] != 0);
1120 assert(op->compute.range[1] != 0);
1121 assert(op->compute.tile[0] != 0);
1122 pthreadpool_parallelize_2d_tile_1d(
1123 threadpool,
1124 op->compute.task_2d_tile_1d,
1125 &op->context,
1126 op->compute.range[0], op->compute.range[1],
1127 op->compute.tile[0],
Marat Dukhan942359e2021-06-09 00:38:56 -07001128 flags);
XNNPACK Teamb455b122019-09-27 18:10:33 -07001129 break;
1130 case xnn_parallelization_type_2d_tile_2d:
1131 assert(op->compute.range[0] != 0);
1132 assert(op->compute.range[1] != 0);
1133 assert(op->compute.tile[0] != 0);
1134 assert(op->compute.tile[1] != 0);
1135 pthreadpool_parallelize_2d_tile_2d(
1136 threadpool,
1137 op->compute.task_2d_tile_2d,
1138 &op->context,
1139 op->compute.range[0], op->compute.range[1],
1140 op->compute.tile[0], op->compute.tile[1],
Marat Dukhan942359e2021-06-09 00:38:56 -07001141 flags);
XNNPACK Teamb455b122019-09-27 18:10:33 -07001142 break;
Marat Dukhan0e521172020-11-25 13:10:04 -08001143 case xnn_parallelization_type_3d:
1144 assert(op->compute.range[0] != 0);
1145 assert(op->compute.range[1] != 0);
1146 assert(op->compute.range[2] != 0);
1147 pthreadpool_parallelize_3d(
1148 threadpool,
1149 op->compute.task_3d,
1150 &op->context,
1151 op->compute.range[0], op->compute.range[1], op->compute.range[2],
Marat Dukhan942359e2021-06-09 00:38:56 -07001152 flags);
Marat Dukhan0e521172020-11-25 13:10:04 -08001153 break;
XNNPACK Teamb455b122019-09-27 18:10:33 -07001154 case xnn_parallelization_type_3d_tile_2d:
1155 assert(op->compute.range[0] != 0);
1156 assert(op->compute.range[1] != 0);
1157 assert(op->compute.range[2] != 0);
1158 assert(op->compute.tile[0] != 0);
1159 assert(op->compute.tile[1] != 0);
1160 pthreadpool_parallelize_3d_tile_2d(
1161 threadpool,
1162 op->compute.task_3d_tile_2d,
1163 &op->context,
1164 op->compute.range[0], op->compute.range[1], op->compute.range[2],
1165 op->compute.tile[0], op->compute.tile[1],
Marat Dukhan942359e2021-06-09 00:38:56 -07001166 flags);
XNNPACK Teamb455b122019-09-27 18:10:33 -07001167 break;
Marat Dukhan0e521172020-11-25 13:10:04 -08001168 case xnn_parallelization_type_4d:
1169 assert(op->compute.range[0] != 0);
1170 assert(op->compute.range[1] != 0);
1171 assert(op->compute.range[2] != 0);
1172 assert(op->compute.range[3] != 0);
1173 pthreadpool_parallelize_4d(
1174 threadpool,
1175 op->compute.task_4d,
1176 &op->context,
1177 op->compute.range[0], op->compute.range[1], op->compute.range[2], op->compute.range[3],
Marat Dukhan942359e2021-06-09 00:38:56 -07001178 flags);
Marat Dukhan0e521172020-11-25 13:10:04 -08001179 break;
XNNPACK Teamb455b122019-09-27 18:10:33 -07001180 case xnn_parallelization_type_4d_tile_2d:
1181 assert(op->compute.range[0] != 0);
1182 assert(op->compute.range[1] != 0);
1183 assert(op->compute.range[2] != 0);
1184 assert(op->compute.range[3] != 0);
1185 assert(op->compute.tile[0] != 0);
1186 assert(op->compute.tile[1] != 0);
1187 pthreadpool_parallelize_4d_tile_2d(
1188 threadpool,
1189 op->compute.task_4d_tile_2d,
1190 &op->context,
1191 op->compute.range[0], op->compute.range[1], op->compute.range[2], op->compute.range[3],
1192 op->compute.tile[0], op->compute.tile[1],
Marat Dukhan942359e2021-06-09 00:38:56 -07001193 flags);
XNNPACK Teamb455b122019-09-27 18:10:33 -07001194 break;
Marat Dukhan892310c2020-12-05 15:53:20 -08001195 case xnn_parallelization_type_5d:
1196 assert(op->compute.range[0] != 0);
1197 assert(op->compute.range[1] != 0);
1198 assert(op->compute.range[2] != 0);
1199 assert(op->compute.range[3] != 0);
1200 assert(op->compute.range[4] != 0);
1201 pthreadpool_parallelize_5d(
1202 threadpool,
1203 op->compute.task_5d,
1204 &op->context,
1205 op->compute.range[0], op->compute.range[1], op->compute.range[2], op->compute.range[3], op->compute.range[4],
Marat Dukhan942359e2021-06-09 00:38:56 -07001206 flags);
Marat Dukhan892310c2020-12-05 15:53:20 -08001207 break;
XNNPACK Teamb455b122019-09-27 18:10:33 -07001208 case xnn_parallelization_type_5d_tile_2d:
1209 assert(op->compute.range[0] != 0);
1210 assert(op->compute.range[1] != 0);
1211 assert(op->compute.range[2] != 0);
1212 assert(op->compute.range[3] != 0);
1213 assert(op->compute.range[4] != 0);
1214 assert(op->compute.tile[0] != 0);
1215 assert(op->compute.tile[1] != 0);
1216 pthreadpool_parallelize_5d_tile_2d(
1217 threadpool,
1218 op->compute.task_5d_tile_2d,
1219 &op->context,
1220 op->compute.range[0], op->compute.range[1], op->compute.range[2], op->compute.range[3], op->compute.range[4],
1221 op->compute.tile[0], op->compute.tile[1],
Marat Dukhan942359e2021-06-09 00:38:56 -07001222 flags);
XNNPACK Teamb455b122019-09-27 18:10:33 -07001223 break;
1224 case xnn_parallelization_type_6d_tile_2d:
1225 assert(op->compute.range[0] != 0);
1226 assert(op->compute.range[1] != 0);
1227 assert(op->compute.range[2] != 0);
1228 assert(op->compute.range[3] != 0);
1229 assert(op->compute.range[4] != 0);
1230 assert(op->compute.range[5] != 0);
1231 assert(op->compute.tile[0] != 0);
1232 assert(op->compute.tile[1] != 0);
1233 pthreadpool_parallelize_6d_tile_2d(
1234 threadpool,
1235 op->compute.task_6d_tile_2d,
1236 &op->context,
1237 op->compute.range[0], op->compute.range[1], op->compute.range[2], op->compute.range[3], op->compute.range[4], op->compute.range[5],
1238 op->compute.tile[0], op->compute.tile[1],
Marat Dukhan942359e2021-06-09 00:38:56 -07001239 flags);
XNNPACK Teamb455b122019-09-27 18:10:33 -07001240 break;
Marat Dukhan05702cf2020-03-26 15:41:33 -07001241#if XNN_MAX_UARCH_TYPES > 1
1242 case xnn_parallelization_type_2d_tile_2d_with_uarch:
1243 assert(op->compute.range[0] != 0);
1244 assert(op->compute.range[1] != 0);
1245 assert(op->compute.tile[0] != 0);
1246 assert(op->compute.tile[1] != 0);
1247 pthreadpool_parallelize_2d_tile_2d_with_uarch(
1248 threadpool,
1249 op->compute.task_2d_tile_2d_with_id,
1250 &op->context,
1251 0 /* default uarch index */, XNN_MAX_UARCH_TYPES - 1,
1252 op->compute.range[0], op->compute.range[1],
1253 op->compute.tile[0], op->compute.tile[1],
Marat Dukhan942359e2021-06-09 00:38:56 -07001254 flags);
Marat Dukhan05702cf2020-03-26 15:41:33 -07001255 break;
1256 case xnn_parallelization_type_3d_tile_2d_with_uarch:
1257 assert(op->compute.range[0] != 0);
1258 assert(op->compute.range[1] != 0);
1259 assert(op->compute.range[2] != 0);
1260 assert(op->compute.tile[0] != 0);
1261 assert(op->compute.tile[1] != 0);
1262 pthreadpool_parallelize_3d_tile_2d_with_uarch(
1263 threadpool,
1264 op->compute.task_3d_tile_2d_with_id,
1265 &op->context,
1266 0 /* default uarch index */, XNN_MAX_UARCH_TYPES - 1,
1267 op->compute.range[0], op->compute.range[1], op->compute.range[2],
1268 op->compute.tile[0], op->compute.tile[1],
Marat Dukhan942359e2021-06-09 00:38:56 -07001269 flags);
Marat Dukhan05702cf2020-03-26 15:41:33 -07001270 break;
1271 case xnn_parallelization_type_4d_tile_2d_with_uarch:
1272 assert(op->compute.range[0] != 0);
1273 assert(op->compute.range[1] != 0);
1274 assert(op->compute.range[2] != 0);
1275 assert(op->compute.range[3] != 0);
1276 assert(op->compute.tile[0] != 0);
1277 assert(op->compute.tile[1] != 0);
1278 pthreadpool_parallelize_4d_tile_2d_with_uarch(
1279 threadpool,
1280 op->compute.task_4d_tile_2d_with_id,
1281 &op->context,
1282 0 /* default uarch index */, XNN_MAX_UARCH_TYPES - 1,
1283 op->compute.range[0], op->compute.range[1], op->compute.range[2], op->compute.range[3],
1284 op->compute.tile[0], op->compute.tile[1],
Marat Dukhan942359e2021-06-09 00:38:56 -07001285 flags);
Marat Dukhan05702cf2020-03-26 15:41:33 -07001286 break;
1287#endif // XNN_MAX_UARCH_TYPES > 1
XNNPACK Teamb455b122019-09-27 18:10:33 -07001288 default:
1289 XNN_UNREACHABLE;
1290 }
1291 return xnn_status_success;
1292}