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_hardswish_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_hardswish_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->compute_type == xnn_compute_type_fp32);
27
28 assert(node->num_inputs == 1);
29 const uint32_t input_id = node->inputs[0];
30 assert(input_id != XNN_INVALID_VALUE_ID);
31 assert(input_id < num_values);
32
33 assert(node->num_outputs == 1);
34 const uint32_t output_id = node->outputs[0];
35 assert(output_id != XNN_INVALID_VALUE_ID);
36 assert(output_id < num_values);
37
38 const size_t num_input_dims = values[input_id].shape.num_dims;
39 const size_t channel_dim = num_input_dims == 0 ? 1 : values[input_id].shape.dim[num_input_dims - 1];
40
41 enum xnn_status status;
42 switch (node->compute_type) {
43 case xnn_compute_type_fp32:
44 status = xnn_create_hardswish_nc_f32(
45 channel_dim /* channels */, channel_dim /* input stride */, channel_dim /* output stride */,
46 node->flags,
47 &opdata->operator_objects[0]);
48 break;
49 #ifndef XNN_NO_F16_OPERATORS
50 case xnn_compute_type_fp16:
51 status = xnn_create_hardswish_nc_f16(
52 channel_dim /* channels */, channel_dim /* input stride */, channel_dim /* output stride */,
53 node->flags,
54 &opdata->operator_objects[0]);
55 break;
56 #endif // !defined(XNN_NO_F16_OPERATORS)
57 default:
58 XNN_UNREACHABLE;
59 }
60 if (status == xnn_status_success) {
61 opdata->batch_size = xnn_shape_multiply_non_channel_dims(&values[input_id].shape);
62 opdata->inputs[0] = input_id;
63 opdata->outputs[0] = output_id;
64 }
65 return status;
66 }
67
setup_hardswish_operator(const struct xnn_operator_data * opdata,const struct xnn_blob * blobs,size_t num_blobs,pthreadpool_t threadpool)68 static enum xnn_status setup_hardswish_operator(
69 const struct xnn_operator_data* opdata,
70 const struct xnn_blob* blobs,
71 size_t num_blobs,
72 pthreadpool_t threadpool)
73 {
74 const uint32_t input_id = opdata->inputs[0];
75 assert(input_id != XNN_INVALID_VALUE_ID);
76 assert(input_id < num_blobs);
77
78 const uint32_t output_id = opdata->outputs[0];
79 assert(output_id != XNN_INVALID_VALUE_ID);
80 assert(output_id < num_blobs);
81
82 const struct xnn_blob* input_blob = blobs + input_id;
83 const void* input_data = input_blob->data;
84 assert(input_data != NULL);
85
86 const struct xnn_blob* output_blob = blobs + output_id;
87 void* output_data = output_blob->data;
88 assert(output_data != NULL);
89
90 switch (opdata->operator_objects[0]->type) {
91 case xnn_operator_type_hardswish_nc_f32:
92 return xnn_setup_hardswish_nc_f32(
93 opdata->operator_objects[0],
94 opdata->batch_size,
95 input_data,
96 output_data,
97 threadpool);
98 #ifndef XNN_NO_F16_OPERATORS
99 case xnn_operator_type_hardswish_nc_f16:
100 return xnn_setup_hardswish_nc_f16(
101 opdata->operator_objects[0],
102 opdata->batch_size,
103 input_data,
104 output_data,
105 threadpool);
106 #endif // !defined(XNN_NO_F16_OPERATORS)
107 default:
108 XNN_UNREACHABLE;
109 }
110 }
111
xnn_define_hardswish(xnn_subgraph_t subgraph,uint32_t input_id,uint32_t output_id,uint32_t flags)112 enum xnn_status xnn_define_hardswish(
113 xnn_subgraph_t subgraph,
114 uint32_t input_id,
115 uint32_t output_id,
116 uint32_t flags)
117 {
118 enum xnn_status status;
119 if ((status = xnn_subgraph_check_xnnpack_initialized(xnn_node_type_hardswish)) != xnn_status_success) {
120 return status;
121 }
122
123 if ((status = xnn_subgraph_check_input_node_id(xnn_node_type_hardswish, input_id, subgraph->num_values)) !=
124 xnn_status_success) {
125 return status;
126 }
127
128 const struct xnn_value* input_value = &subgraph->values[input_id];
129 status = xnn_subgraph_check_input_type_dense(xnn_node_type_hardswish, input_id, input_value);
130 if (status != xnn_status_success) {
131 return status;
132 }
133
134 switch (input_value->datatype) {
135 case xnn_datatype_fp32:
136 break;
137 default:
138 xnn_log_error(
139 "failed to define %s operator with input ID #%" PRIu32 ": unsupported Value datatype %s (%d)",
140 xnn_node_type_to_string(xnn_node_type_hardswish), input_id,
141 xnn_datatype_to_string(input_value->datatype), input_value->datatype);
142 return xnn_status_invalid_parameter;
143 }
144
145 status = xnn_subgraph_check_output_node_id(xnn_node_type_hardswish, output_id, subgraph->num_values);
146 if (status != xnn_status_success) {
147 return status;
148 }
149
150 const struct xnn_value* output_value = &subgraph->values[output_id];
151 status = xnn_subgraph_check_output_type_dense(xnn_node_type_hardswish, output_id, output_value);
152 if (status != xnn_status_success) {
153 return status;
154 }
155
156 switch (output_value->datatype) {
157 case xnn_datatype_fp32:
158 break;
159 default:
160 xnn_log_error(
161 "failed to define %s operator with output ID #%" PRIu32 ": unsupported Value datatype %s (%d)",
162 xnn_node_type_to_string(xnn_node_type_hardswish), output_id,
163 xnn_datatype_to_string(output_value->datatype), output_value->datatype);
164 return xnn_status_invalid_parameter;
165 }
166
167 struct xnn_node* node = xnn_subgraph_new_node(subgraph);
168 if (node == NULL) {
169 return xnn_status_out_of_memory;
170 }
171
172 node->type = xnn_node_type_hardswish;
173 node->compute_type = xnn_compute_type_fp32;
174 node->num_inputs = 1;
175 node->inputs[0] = input_id;
176 node->num_outputs = 1;
177 node->outputs[0] = output_id;
178 node->flags = flags;
179
180 node->create = create_hardswish_operator;
181 node->setup = setup_hardswish_operator;
182
183 return xnn_status_success;
184 }
185