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 <math.h>
7 #include <stddef.h>
8 #include <stdint.h>
9
10 #include <xnnpack.h>
11 #include <xnnpack/log.h>
12 #include <xnnpack/params.h>
13 #include <xnnpack/subgraph.h>
14
15
16
create_abs_operator(const struct xnn_node * node,const struct xnn_value * values,size_t num_values,struct xnn_operator_data * opdata)17 static enum xnn_status create_abs_operator(
18 const struct xnn_node* node,
19 const struct xnn_value* values,
20 size_t num_values,
21 struct xnn_operator_data* opdata)
22 {
23 assert(node->compute_type == xnn_compute_type_fp32);
24
25 assert(node->num_inputs == 1);
26 const uint32_t input_id = node->inputs[0];
27 assert(input_id != XNN_INVALID_VALUE_ID);
28 assert(input_id < num_values);
29
30 assert(node->num_outputs == 1);
31 const uint32_t output_id = node->outputs[0];
32 assert(output_id != XNN_INVALID_VALUE_ID);
33 assert(output_id < num_values);
34
35 const size_t num_input_dims = values[input_id].shape.num_dims;
36 const size_t channel_dim = num_input_dims == 0 ? 1 : values[input_id].shape.dim[num_input_dims - 1];
37
38 const enum xnn_status status = xnn_create_abs_nc_f32(
39 channel_dim /* channels */, channel_dim /* input stride */, channel_dim /* output stride */,
40 node->flags,
41 &opdata->operator_object);
42 if (status == xnn_status_success) {
43 opdata->batch_size = xnn_shape_multiply_non_channel_dims(&values[input_id].shape);
44 opdata->inputs[0] = input_id;
45 opdata->outputs[0] = output_id;
46 }
47 return status;
48 }
49
setup_abs_operator(const struct xnn_operator_data * opdata,const struct xnn_blob * blobs,size_t num_blobs,pthreadpool_t threadpool)50 static enum xnn_status setup_abs_operator(
51 const struct xnn_operator_data* opdata,
52 const struct xnn_blob* blobs,
53 size_t num_blobs,
54 pthreadpool_t threadpool)
55 {
56 const uint32_t input_id = opdata->inputs[0];
57 assert(input_id != XNN_INVALID_VALUE_ID);
58 assert(input_id < num_blobs);
59
60 const uint32_t output_id = opdata->outputs[0];
61 assert(output_id != XNN_INVALID_VALUE_ID);
62 assert(output_id < num_blobs);
63
64 const struct xnn_blob* input_blob = blobs + input_id;
65 const void* input_data = input_blob->data;
66 assert(input_data != NULL);
67
68 const struct xnn_blob* output_blob = blobs + output_id;
69 void* output_data = output_blob->data;
70 assert(output_data != NULL);
71
72 return xnn_setup_abs_nc_f32(
73 opdata->operator_object,
74 opdata->batch_size,
75 input_data,
76 output_data,
77 threadpool);
78 }
79
xnn_define_abs(xnn_subgraph_t subgraph,uint32_t input_id,uint32_t output_id,uint32_t flags)80 enum xnn_status xnn_define_abs(
81 xnn_subgraph_t subgraph,
82 uint32_t input_id,
83 uint32_t output_id,
84 uint32_t flags)
85 {
86 if ((xnn_params.init_flags & XNN_INIT_FLAG_XNNPACK) == 0) {
87 xnn_log_error("failed to define %s operator: XNNPACK is not initialized",
88 xnn_node_type_to_string(xnn_node_type_abs));
89 return xnn_status_uninitialized;
90 }
91
92 if (input_id >= subgraph->num_values) {
93 xnn_log_error(
94 "failed to define %s operator with input ID #%" PRIu32 ": invalid Value ID",
95 xnn_node_type_to_string(xnn_node_type_abs), input_id);
96 return xnn_status_invalid_parameter;
97 }
98
99 const struct xnn_value* input_value = &subgraph->values[input_id];
100 if (input_value->type != xnn_value_type_dense_tensor) {
101 xnn_log_error(
102 "failed to define %s operator with input ID #%" PRIu32 ": unsupported Value type %d (expected dense tensor)",
103 xnn_node_type_to_string(xnn_node_type_abs), input_id, input_value->type);
104 return xnn_status_invalid_parameter;
105 }
106
107 switch (input_value->datatype) {
108 case xnn_datatype_fp32:
109 break;
110 default:
111 xnn_log_error(
112 "failed to define %s operator with input ID #%" PRIu32 ": unsupported Value datatype %s (%d)",
113 xnn_node_type_to_string(xnn_node_type_abs), input_id,
114 xnn_datatype_to_string(input_value->datatype), input_value->datatype);
115 return xnn_status_invalid_parameter;
116 }
117
118 if (output_id >= subgraph->num_values) {
119 xnn_log_error(
120 "failed to define %s operator with output ID #%" PRIu32 ": invalid Value ID",
121 xnn_node_type_to_string(xnn_node_type_abs), output_id);
122 return xnn_status_invalid_parameter;
123 }
124
125 const struct xnn_value* output_value = &subgraph->values[output_id];
126 if (output_value->type != xnn_value_type_dense_tensor) {
127 xnn_log_error(
128 "failed to define %s operator with output ID #%" PRIu32 ": unsupported Value type %d (expected dense tensor)",
129 xnn_node_type_to_string(xnn_node_type_abs), output_id, output_value->type);
130 return xnn_status_invalid_parameter;
131 }
132
133 switch (output_value->datatype) {
134 case xnn_datatype_fp32:
135 break;
136 default:
137 xnn_log_error(
138 "failed to define %s operator with output ID #%" PRIu32 ": unsupported Value datatype %s (%d)",
139 xnn_node_type_to_string(xnn_node_type_abs), output_id,
140 xnn_datatype_to_string(output_value->datatype), output_value->datatype);
141 return xnn_status_invalid_parameter;
142 }
143
144 struct xnn_node* node = xnn_subgraph_new_node(subgraph);
145 if (node == NULL) {
146 return xnn_status_out_of_memory;
147 }
148
149 node->type = xnn_node_type_abs;
150 node->compute_type = xnn_compute_type_fp32;
151 node->num_inputs = 1;
152 node->inputs[0] = input_id;
153 node->num_outputs = 1;
154 node->outputs[0] = output_id;
155 node->flags = flags;
156
157 node->create = create_abs_operator;
158 node->setup = setup_abs_operator;
159
160 return xnn_status_success;
161 }
162