1 // Copyright 2020 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
11 #include <xnnpack.h>
12 #include <xnnpack/log.h>
13 #include <xnnpack/operator.h>
14 #include <xnnpack/params.h>
15 #include <xnnpack/subgraph.h>
16 #include <xnnpack/subgraph-validation.h>
17
18
create_sigmoid_operator(const struct xnn_node * node,const struct xnn_value * values,size_t num_values,struct xnn_operator_data * opdata,const struct xnn_caches * caches)19 static enum xnn_status create_sigmoid_operator(
20 const struct xnn_node* node,
21 const struct xnn_value* values,
22 size_t num_values,
23 struct xnn_operator_data* opdata,
24 const struct xnn_caches* caches)
25 {
26 assert(node->num_inputs == 1);
27 const uint32_t input_id = node->inputs[0];
28 assert(input_id != XNN_INVALID_VALUE_ID);
29 assert(input_id < num_values);
30
31 assert(node->num_outputs == 1);
32 const uint32_t output_id = node->outputs[0];
33 assert(output_id != XNN_INVALID_VALUE_ID);
34 assert(output_id < num_values);
35
36 const size_t num_input_dims = values[input_id].shape.num_dims;
37 const size_t channel_dim = num_input_dims == 0 ? 1 : values[input_id].shape.dim[num_input_dims - 1];
38
39 enum xnn_status status;
40 switch (node->compute_type) {
41 #ifndef XNN_NO_F16_OPERATORS
42 case xnn_compute_type_fp16:
43 status = xnn_create_sigmoid_nc_f16(
44 channel_dim /* channels */, channel_dim /* input stride */, channel_dim /* output stride */,
45 node->flags,
46 &opdata->operator_objects[0]);
47 break;
48 #endif // !defined(XNN_NO_F16_OPERATORS)
49 case xnn_compute_type_fp32:
50 status = xnn_create_sigmoid_nc_f32(
51 channel_dim /* channels */, channel_dim /* input stride */, channel_dim /* output stride */,
52 node->flags,
53 &opdata->operator_objects[0]);
54 break;
55 #ifndef XNN_NO_QS8_OPERATORS
56 case xnn_compute_type_qs8:
57 {
58 status = xnn_create_sigmoid_nc_qs8(
59 channel_dim /* channels */, channel_dim /* input stride */, channel_dim /* output stride */,
60 (int8_t) values[input_id].quantization.zero_point,
61 values[input_id].quantization.scale,
62 (int8_t) values[output_id].quantization.zero_point,
63 values[output_id].quantization.scale,
64 INT8_MIN, INT8_MAX,
65 node->flags,
66 &opdata->operator_objects[0]);
67 break;
68 }
69 #endif // !defined(XNN_NO_QS8_OPERATORS)
70 #ifndef XNN_NO_QU8_OPERATORS
71 case xnn_compute_type_qu8:
72 {
73 status = xnn_create_sigmoid_nc_qu8(
74 channel_dim /* channels */, channel_dim /* input stride */, channel_dim /* output stride */,
75 (uint8_t) values[input_id].quantization.zero_point,
76 values[input_id].quantization.scale,
77 (uint8_t) values[output_id].quantization.zero_point,
78 values[output_id].quantization.scale,
79 0, UINT8_MAX,
80 node->flags,
81 &opdata->operator_objects[0]);
82 break;
83 }
84 #endif // !defined(XNN_NO_QU8_OPERATORS)
85 default:
86 XNN_UNREACHABLE;
87 }
88 if (status == xnn_status_success) {
89 opdata->batch_size = xnn_shape_multiply_non_channel_dims(&values[input_id].shape);
90 opdata->inputs[0] = input_id;
91 opdata->outputs[0] = output_id;
92 }
93 return status;
94 }
95
setup_sigmoid_operator(const struct xnn_operator_data * opdata,const struct xnn_blob * blobs,size_t num_blobs,pthreadpool_t threadpool)96 static enum xnn_status setup_sigmoid_operator(
97 const struct xnn_operator_data* opdata,
98 const struct xnn_blob* blobs,
99 size_t num_blobs,
100 pthreadpool_t threadpool)
101 {
102 const uint32_t input_id = opdata->inputs[0];
103 assert(input_id != XNN_INVALID_VALUE_ID);
104 assert(input_id < num_blobs);
105
106 const uint32_t output_id = opdata->outputs[0];
107 assert(output_id != XNN_INVALID_VALUE_ID);
108 assert(output_id < num_blobs);
109
110 const struct xnn_blob* input_blob = blobs + input_id;
111 const void* input_data = input_blob->data;
112 assert(input_data != NULL);
113
114 const struct xnn_blob* output_blob = blobs + output_id;
115 void* output_data = output_blob->data;
116 assert(output_data != NULL);
117
118 switch (opdata->operator_objects[0]->type) {
119 #ifndef XNN_NO_F16_OPERATORS
120 case xnn_operator_type_sigmoid_nc_f16:
121 return xnn_setup_sigmoid_nc_f16(
122 opdata->operator_objects[0],
123 opdata->batch_size,
124 input_data,
125 output_data,
126 threadpool);
127 #endif // !defined(XNN_NO_F16_OPERATORS)
128 case xnn_operator_type_sigmoid_nc_f32:
129 return xnn_setup_sigmoid_nc_f32(
130 opdata->operator_objects[0],
131 opdata->batch_size,
132 input_data,
133 output_data,
134 threadpool);
135 #ifndef XNN_NO_QS8_OPERATORS
136 case xnn_operator_type_sigmoid_nc_qs8:
137 return xnn_setup_sigmoid_nc_qs8(
138 opdata->operator_objects[0],
139 opdata->batch_size,
140 input_data,
141 output_data,
142 threadpool);
143 #endif // !defined(XNN_NO_QS8_OPERATORS)
144 #ifndef XNN_NO_QU8_OPERATORS
145 case xnn_operator_type_sigmoid_nc_qu8:
146 return xnn_setup_sigmoid_nc_qu8(
147 opdata->operator_objects[0],
148 opdata->batch_size,
149 input_data,
150 output_data,
151 threadpool);
152 break;
153 #endif // !defined(XNN_NO_QU8_OPERATORS)
154 default:
155 XNN_UNREACHABLE;
156 }
157 }
158
xnn_define_sigmoid(xnn_subgraph_t subgraph,uint32_t input_id,uint32_t output_id,uint32_t flags)159 enum xnn_status xnn_define_sigmoid(
160 xnn_subgraph_t subgraph,
161 uint32_t input_id,
162 uint32_t output_id,
163 uint32_t flags)
164 {
165 enum xnn_status status;
166 if ((status = xnn_subgraph_check_xnnpack_initialized(xnn_node_type_sigmoid)) != xnn_status_success) {
167 return status;
168 }
169
170 if ((status = xnn_subgraph_check_input_node_id(xnn_node_type_sigmoid, input_id, subgraph->num_values)) !=
171 xnn_status_success) {
172 return status;
173 }
174
175 const struct xnn_value* input_value = &subgraph->values[input_id];
176 status = xnn_subgraph_check_input_type_dense(xnn_node_type_sigmoid, input_id, input_value);
177 if (status != xnn_status_success) {
178 return status;
179 }
180
181 switch (input_value->datatype) {
182 case xnn_datatype_fp32:
183 #ifndef XNN_NO_QS8_OPERATORS
184 case xnn_datatype_qint8:
185 #endif // !defined(XNN_NO_QS8_OPERATORS)
186 #ifndef XNN_NO_QU8_OPERATORS
187 case xnn_datatype_quint8:
188 #endif // !defined(XNN_NO_QU8_OPERATORS)
189 break;
190 default:
191 xnn_log_error(
192 "failed to define %s operator with input ID #%" PRIu32 ": unsupported Value datatype %s (%d)",
193 xnn_node_type_to_string(xnn_node_type_sigmoid), input_id,
194 xnn_datatype_to_string(input_value->datatype), input_value->datatype);
195 return xnn_status_invalid_parameter;
196 }
197
198 status = xnn_subgraph_check_output_node_id(xnn_node_type_sigmoid, output_id, subgraph->num_values);
199 if (status != xnn_status_success) {
200 return status;
201 }
202
203 const struct xnn_value* output_value = &subgraph->values[output_id];
204 status = xnn_subgraph_check_output_type_dense(xnn_node_type_sigmoid, output_id, output_value);
205 if (status != xnn_status_success) {
206 return status;
207 }
208
209 enum xnn_compute_type compute_type = xnn_compute_type_invalid;
210 switch (output_value->datatype) {
211 case xnn_datatype_fp32:
212 compute_type = xnn_compute_type_fp32;
213 break;
214 #ifndef XNN_NO_QS8_OPERATORS
215 case xnn_datatype_qint8:
216 compute_type = xnn_compute_type_qs8;
217 break;
218 #endif // !defined(XNN_NO_QS8_OPERATORS)
219 #ifndef XNN_NO_QU8_OPERATORS
220 case xnn_datatype_quint8:
221 compute_type = xnn_compute_type_qu8;
222 break;
223 #endif // !defined(XNN_NO_QU8_OPERATORS)
224 default:
225 xnn_log_error(
226 "failed to define %s operator with output ID #%" PRIu32 ": unsupported Value datatype %s (%d)",
227 xnn_node_type_to_string(xnn_node_type_sigmoid), output_id,
228 xnn_datatype_to_string(output_value->datatype), output_value->datatype);
229 return xnn_status_invalid_parameter;
230 }
231
232 status = xnn_subgraph_check_datatype_matches(xnn_node_type_subtract, input_id, input_value, output_id, output_value);
233 if (status != xnn_status_success) {
234 return status;
235 }
236
237 struct xnn_node* node = xnn_subgraph_new_node(subgraph);
238 if (node == NULL) {
239 return xnn_status_out_of_memory;
240 }
241
242 node->type = xnn_node_type_sigmoid;
243 node->compute_type = compute_type;
244 node->num_inputs = 1;
245 node->inputs[0] = input_id;
246 node->num_outputs = 1;
247 node->outputs[0] = output_id;
248 node->flags = flags;
249
250 node->create = create_sigmoid_operator;
251 node->setup = setup_sigmoid_operator;
252
253 return xnn_status_success;
254 }
255