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