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