• 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 <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/params-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   global_average_pooling_op->params.f32_gavgpool = xnn_init_f32_gavgpool_params(nanf(""), output_min, output_max, 0);
78 
79   global_average_pooling_op->type = xnn_operator_type_global_average_pooling_ncw_f32;
80 
81   global_average_pooling_op->state = xnn_run_state_invalid;
82 
83   *global_average_pooling_op_out = global_average_pooling_op;
84   return xnn_status_success;
85 
86 error:
87   xnn_delete_operator(global_average_pooling_op);
88   return status;
89 }
90 
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)91 enum xnn_status xnn_setup_global_average_pooling_ncw_f32(
92     xnn_operator_t global_average_pooling_op,
93     size_t batch_size,
94     size_t width,
95     const float* input,
96     float* output,
97     pthreadpool_t threadpool)
98 {
99   if (global_average_pooling_op->type != xnn_operator_type_global_average_pooling_ncw_f32) {
100     xnn_log_error("failed to setup operator: operator type mismatch (expected %s, got %s)",
101       xnn_operator_type_to_string(xnn_operator_type_global_average_pooling_ncw_f32),
102       xnn_operator_type_to_string(global_average_pooling_op->type));
103     return xnn_status_invalid_parameter;
104   }
105   global_average_pooling_op->state = xnn_run_state_invalid;
106 
107   if ((xnn_params.init_flags & XNN_INIT_FLAG_XNNPACK) == 0) {
108     xnn_log_error("failed to setup %s operator: XNNPACK is not initialized",
109       xnn_operator_type_to_string(xnn_operator_type_global_average_pooling_ncw_f32));
110     return xnn_status_uninitialized;
111   }
112 
113   if (width == 0) {
114     xnn_log_error(
115       "failed to setup %s operator with width %zu: width must be non-zero",
116       xnn_operator_type_to_string(xnn_operator_type_global_average_pooling_ncw_f32), width);
117     return xnn_status_invalid_parameter;
118   }
119 
120   if (batch_size == 0) {
121     global_average_pooling_op->state = xnn_run_state_skip;
122     return xnn_status_success;
123   }
124 
125   xnn_update_f32_gavgpool_params(&global_average_pooling_op->params.f32_gavgpool,
126     1.0f / (float) width, width);
127 
128   global_average_pooling_op->context.global_average_pooling_ncw = (struct global_average_pooling_ncw_context) {
129     .input_elements = width * sizeof(float),
130     .input = input,
131     .input_channel_stride = width * sizeof(float),
132     .input_batch_stride = global_average_pooling_op->channels * width * sizeof(float),
133     .output = output,
134     .output_channel_stride = sizeof(float),
135     .output_batch_stride = global_average_pooling_op->channels * sizeof(float),
136     .ukernel = xnn_params.f32.gavgpool_cw.ukernel,
137     .params.f32 = global_average_pooling_op->params.f32_gavgpool,
138   };
139 
140   global_average_pooling_op->compute.type = xnn_parallelization_type_2d_tile_1d;
141   global_average_pooling_op->compute.task_2d_tile_1d =
142     (pthreadpool_task_2d_tile_1d_t) xnn_compute_global_average_pooling_ncw;
143   global_average_pooling_op->compute.range[0] = batch_size;
144   global_average_pooling_op->compute.range[1] = global_average_pooling_op->channels;
145   global_average_pooling_op->compute.tile[0] = global_average_pooling_op->channels; //xnn_params.f32.gavgpool_cw.channel_tile;
146 
147   global_average_pooling_op->state = xnn_run_state_ready;
148 
149   return xnn_status_success;
150 }
151