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 <stddef.h>
9 #include <stdint.h>
10 #include <stdlib.h>
11
12 #include <xnnpack.h>
13 #include <xnnpack/allocator.h>
14 #include <xnnpack/log.h>
15 #include <xnnpack/operator.h>
16 #include <xnnpack/microparams-init.h>
17 #include <xnnpack/params.h>
18
19
xnn_create_global_average_pooling_ncw_f32(size_t channels,float output_min,float output_max,uint32_t flags,xnn_operator_t * global_average_pooling_op_out)20 enum xnn_status xnn_create_global_average_pooling_ncw_f32(
21 size_t channels,
22 float output_min,
23 float output_max,
24 uint32_t flags,
25 xnn_operator_t* global_average_pooling_op_out)
26 {
27 xnn_operator_t global_average_pooling_op = NULL;
28 enum xnn_status status = xnn_status_uninitialized;
29
30 if ((xnn_params.init_flags & XNN_INIT_FLAG_XNNPACK) == 0) {
31 xnn_log_error("failed to create %s operator: XNNPACK is not initialized",
32 xnn_operator_type_to_string(xnn_operator_type_global_average_pooling_ncw_f32));
33 goto error;
34 }
35
36 status = xnn_status_invalid_parameter;
37
38 if (channels == 0) {
39 xnn_log_error(
40 "failed to create %s operator with %zu channels: number of channels must be non-zero",
41 xnn_operator_type_to_string(xnn_operator_type_global_average_pooling_ncw_f32), channels);
42 goto error;
43 }
44
45 if (isnan(output_min)) {
46 xnn_log_error(
47 "failed to create %s operator with NaN output lower bound: lower bound must be non-NaN",
48 xnn_operator_type_to_string(xnn_operator_type_global_average_pooling_ncw_f32));
49 goto error;
50 }
51
52 if (isnan(output_max)) {
53 xnn_log_error(
54 "failed to create %s operator with NaN output upper bound: upper bound must be non-NaN",
55 xnn_operator_type_to_string(xnn_operator_type_global_average_pooling_ncw_f32));
56 goto error;
57 }
58
59 if (output_min >= output_max) {
60 xnn_log_error(
61 "failed to create %s operator with [%.7g, %.7g] output range: lower bound must be below upper bound",
62 xnn_operator_type_to_string(xnn_operator_type_global_average_pooling_ncw_f32), output_min, output_max);
63 goto error;
64 }
65
66 status = xnn_status_out_of_memory;
67
68 global_average_pooling_op = xnn_allocate_zero_simd_memory(sizeof(struct xnn_operator));
69 if (global_average_pooling_op == NULL) {
70 xnn_log_error(
71 "failed to allocate %zu bytes for %s operator descriptor",
72 sizeof(struct xnn_operator), xnn_operator_type_to_string(xnn_operator_type_global_average_pooling_ncw_f32));
73 goto error;
74 }
75
76 global_average_pooling_op->channels = channels;
77 xnn_init_f32_gavgpool_params(&global_average_pooling_op->params.f32_gavgpool, nanf(""), output_min, output_max, 0);
78
79 global_average_pooling_op->type = xnn_operator_type_global_average_pooling_ncw_f32;
80 global_average_pooling_op->flags = flags;
81
82 global_average_pooling_op->state = xnn_run_state_invalid;
83
84 *global_average_pooling_op_out = global_average_pooling_op;
85 return xnn_status_success;
86
87 error:
88 xnn_delete_operator(global_average_pooling_op);
89 return status;
90 }
91
xnn_setup_global_average_pooling_ncw_f32(xnn_operator_t global_average_pooling_op,size_t batch_size,size_t width,const float * input,float * output,pthreadpool_t threadpool)92 enum xnn_status xnn_setup_global_average_pooling_ncw_f32(
93 xnn_operator_t global_average_pooling_op,
94 size_t batch_size,
95 size_t width,
96 const float* input,
97 float* output,
98 pthreadpool_t threadpool)
99 {
100 if (global_average_pooling_op->type != xnn_operator_type_global_average_pooling_ncw_f32) {
101 xnn_log_error("failed to setup operator: operator type mismatch (expected %s, got %s)",
102 xnn_operator_type_to_string(xnn_operator_type_global_average_pooling_ncw_f32),
103 xnn_operator_type_to_string(global_average_pooling_op->type));
104 return xnn_status_invalid_parameter;
105 }
106 global_average_pooling_op->state = xnn_run_state_invalid;
107
108 if ((xnn_params.init_flags & XNN_INIT_FLAG_XNNPACK) == 0) {
109 xnn_log_error("failed to setup %s operator: XNNPACK is not initialized",
110 xnn_operator_type_to_string(xnn_operator_type_global_average_pooling_ncw_f32));
111 return xnn_status_uninitialized;
112 }
113
114 if (width == 0) {
115 xnn_log_error(
116 "failed to setup %s operator with width %zu: width must be non-zero",
117 xnn_operator_type_to_string(xnn_operator_type_global_average_pooling_ncw_f32), width);
118 return xnn_status_invalid_parameter;
119 }
120
121 if (batch_size == 0) {
122 global_average_pooling_op->state = xnn_run_state_skip;
123 return xnn_status_success;
124 }
125
126 xnn_update_f32_gavgpool_params(&global_average_pooling_op->params.f32_gavgpool,
127 1.0f / (float) width, width);
128
129 global_average_pooling_op->context.global_average_pooling_ncw = (struct global_average_pooling_ncw_context) {
130 .input_elements = width * sizeof(float),
131 .input = input,
132 .input_channel_stride = width * sizeof(float),
133 .input_batch_stride = global_average_pooling_op->channels * width * sizeof(float),
134 .output = output,
135 .output_channel_stride = sizeof(float),
136 .output_batch_stride = global_average_pooling_op->channels * sizeof(float),
137 .ukernel = xnn_params.f32.gavgpool_cw.ukernel,
138 .params.f32 = global_average_pooling_op->params.f32_gavgpool,
139 };
140
141 global_average_pooling_op->compute.type = xnn_parallelization_type_2d_tile_1d;
142 global_average_pooling_op->compute.task_2d_tile_1d =
143 (pthreadpool_task_2d_tile_1d_t) xnn_compute_global_average_pooling_ncw;
144 global_average_pooling_op->compute.range[0] = batch_size;
145 global_average_pooling_op->compute.range[1] = global_average_pooling_op->channels;
146 global_average_pooling_op->compute.tile[0] = global_average_pooling_op->channels; //xnn_params.f32.gavgpool_cw.channel_tile;
147
148 global_average_pooling_op->state = xnn_run_state_ready;
149
150 return xnn_status_success;
151 }
152