blob: ba9aa5791f0d09b245e8f26224c85316da7328d5 [file] [log] [blame]
Marat Dukhan1d75a542020-02-03 12:23:01 -08001// 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#pragma once
7
8#include <stddef.h>
9#include <stdint.h>
10
11#include <xnnpack.h>
12
13#define XNN_MAX_INPUTS 3
Marat Dukhan5cb16e72020-05-05 16:41:57 -070014#define XNN_MAX_OUTPUTS 2
Marat Dukhan1d75a542020-02-03 12:23:01 -080015
16#define XNN_MAX_RUNTIME_INPUTS 2
Marat Dukhan5cb16e72020-05-05 16:41:57 -070017#define XNN_MAX_RUNTIME_OUTPUTS 2
Marat Dukhan1d75a542020-02-03 12:23:01 -080018
Marat Dukhan1f198722020-05-24 14:07:03 -070019#define XNN_INVALID_NODE_ID UINT32_MAX
20
XNNPACK Teamab8c4c82020-10-09 08:05:51 -070021#ifdef __cplusplus
22extern "C" {
23#endif
24
Marat Dukhan1d75a542020-02-03 12:23:01 -080025struct xnn_shape {
26 size_t num_dims;
27 size_t dim[XNN_MAX_TENSOR_DIMS];
28};
29
30enum xnn_value_type {
31 xnn_value_type_invalid = 0,
32 xnn_value_type_dense_tensor = 1,
33};
34
Marat Dukhan9de90e02020-06-18 16:04:12 -070035enum xnn_layout_type {
36 xnn_layout_type_nhwc = 0,
37 xnn_layout_type_nchw = 1,
38};
39
Marat Dukhan1d75a542020-02-03 12:23:01 -080040/// Abstraction for a collections of elements produced and consumed by nodes.
41struct xnn_value {
42 /// Unique ID for the value.
43 uint32_t id;
44 /// Type of the collection of elements.
45 ///
46 /// Currently only dense tensors are supported.
47 /// Other types (e.g. sparse tensors) might be supported in the future.
48 enum xnn_value_type type;
49 /// Type of elements in the collection.
50 enum xnn_datatype datatype;
51 /// Tensor shape.
52 struct xnn_shape shape;
53 /// Binary features of the tensor. Supported values are any combination of:
54 /// - XNN_VALUE_FLAG_EXTERNAL_INPUT
55 /// - XNN_VALUE_FLAG_EXTERNAL_OUTPUT
56 uint32_t flags;
57 /// Static initialization data. Must be null for non-static values.
58 const void* data;
Marat Dukhan1f198722020-05-24 14:07:03 -070059 /// Index of the Subgraph node that produced the value, or XNN_INVALID_NODE_ID is the Value is an external input.
60 uint32_t producer;
61 /// Index of the first Node that consume the value, or XNN_INVALID_NODE_ID if the Value has no consumers within the
62 /// graph (e.g. Value is an external output).
63 uint32_t first_consumer;
64 /// Number of Nodes that consume the value.
65 /// If multiple inputs in a Node refer to this Value as input, the Node is counted as consumer multiple times.
66 /// If the Value is an external output, it counts as having an extra consumer.
67 uint32_t num_consumers;
Marat Dukhan9de90e02020-06-18 16:04:12 -070068 uint32_t num_nchw_compatible_consumers;
69 enum xnn_layout_type layout;
Marat Dukhan1d75a542020-02-03 12:23:01 -080070};
71
72struct xnn_blob {
73 /// Size in bytes.
74 size_t size;
75 /// Data pointer.
76 void* data;
77 bool external;
78};
79
80enum xnn_node_type {
81 xnn_node_type_invalid = 0,
Marat Dukhan5fab4092020-06-10 01:28:28 -070082 xnn_node_type_abs,
Marat Dukhan54dcb462020-02-10 11:06:12 -080083 xnn_node_type_add2,
Marat Dukhan5cb16e72020-05-05 16:41:57 -070084 xnn_node_type_argmax_pooling_2d,
Marat Dukhan21d3bd62020-02-29 00:39:39 -080085 xnn_node_type_average_pooling_2d,
Marat Dukhan5fab4092020-06-10 01:28:28 -070086 xnn_node_type_bankers_rounding,
87 xnn_node_type_ceiling,
Marat Dukhan52bd86f2020-02-11 18:21:51 -080088 xnn_node_type_clamp,
Marat Dukhan1d75a542020-02-03 12:23:01 -080089 xnn_node_type_convolution_2d,
Marat Dukhanf5870842020-04-27 18:19:54 -070090 xnn_node_type_deconvolution_2d,
Marat Dukhan1d75a542020-02-03 12:23:01 -080091 xnn_node_type_depthwise_convolution_2d,
Marat Dukhan9d3a4592020-06-05 16:52:42 -070092 xnn_node_type_divide,
Marat Dukhan38c07ec2020-04-23 16:44:32 -070093 xnn_node_type_fully_connected,
Marat Dukhan5fab4092020-06-10 01:28:28 -070094 xnn_node_type_floor,
Marat Dukhana059b7d2020-06-11 11:41:27 -070095 xnn_node_type_global_average_pooling_2d,
Marat Dukhan52bd86f2020-02-11 18:21:51 -080096 xnn_node_type_hardswish,
Marat Dukhan5bbebac2020-06-10 19:42:15 -070097 xnn_node_type_leaky_relu,
Marat Dukhan21d3bd62020-02-29 00:39:39 -080098 xnn_node_type_max_pooling_2d,
Marat Dukhan9d3a4592020-06-05 16:52:42 -070099 xnn_node_type_maximum2,
100 xnn_node_type_minimum2,
101 xnn_node_type_multiply2,
Marat Dukhan5fab4092020-06-10 01:28:28 -0700102 xnn_node_type_negate,
Marat Dukhan2fd2ba12020-02-10 13:14:45 -0800103 xnn_node_type_prelu,
Marat Dukhan52bd86f2020-02-11 18:21:51 -0800104 xnn_node_type_sigmoid,
105 xnn_node_type_softmax,
Marat Dukhanaff24e22020-07-23 01:43:58 -0700106 xnn_node_type_static_constant_pad,
Marat Dukhand27202d2020-07-09 23:43:40 -0700107 xnn_node_type_static_reshape,
Marat Dukhanaff24e22020-07-23 01:43:58 -0700108 xnn_node_type_static_resize_bilinear_2d,
Marat Dukhan5fab4092020-06-10 01:28:28 -0700109 xnn_node_type_square,
Marat Dukhan51a01c62020-07-09 03:26:57 -0700110 xnn_node_type_square_root,
Marat Dukhan9d3a4592020-06-05 16:52:42 -0700111 xnn_node_type_squared_difference,
112 xnn_node_type_subtract,
Marat Dukhan5cb16e72020-05-05 16:41:57 -0700113 xnn_node_type_unpooling_2d,
Marat Dukhan1d75a542020-02-03 12:23:01 -0800114};
115
116struct xnn_node {
117 enum xnn_node_type type;
118 uint32_t id;
119 /// Static parameters of the operator node.
120 union {
121 struct {
122 uint32_t input_padding_top;
123 uint32_t input_padding_right;
124 uint32_t input_padding_bottom;
125 uint32_t input_padding_left;
126 uint32_t kernel_height;
127 uint32_t kernel_width;
128 uint32_t subsampling_height;
129 uint32_t subsampling_width;
130 uint32_t dilation_height;
131 uint32_t dilation_width;
132 uint32_t groups;
133 size_t group_input_channels;
134 size_t group_output_channels;
Marat Dukhan1d75a542020-02-03 12:23:01 -0800135 } convolution_2d;
136 struct {
Marat Dukhanf5870842020-04-27 18:19:54 -0700137 uint32_t padding_top;
138 uint32_t padding_right;
139 uint32_t padding_bottom;
140 uint32_t padding_left;
141 uint32_t adjustment_height;
142 uint32_t adjustment_width;
143 uint32_t kernel_height;
144 uint32_t kernel_width;
145 uint32_t upsampling_height;
146 uint32_t upsampling_width;
147 uint32_t dilation_height;
148 uint32_t dilation_width;
149 uint32_t groups;
150 size_t group_input_channels;
151 size_t group_output_channels;
152 } deconvolution_2d;
153 struct {
Marat Dukhan1d75a542020-02-03 12:23:01 -0800154 uint32_t input_padding_top;
155 uint32_t input_padding_right;
156 uint32_t input_padding_bottom;
157 uint32_t input_padding_left;
158 uint32_t kernel_height;
159 uint32_t kernel_width;
160 uint32_t subsampling_height;
161 uint32_t subsampling_width;
162 uint32_t dilation_height;
163 uint32_t dilation_width;
164 uint32_t depth_multiplier;
165 size_t input_channels;
Marat Dukhan1d75a542020-02-03 12:23:01 -0800166 } depthwise_convolution_2d;
Marat Dukhan21d3bd62020-02-29 00:39:39 -0800167 struct {
Marat Dukhanb389f352020-05-14 02:58:46 -0700168 uint32_t padding_top;
169 uint32_t padding_right;
170 uint32_t padding_bottom;
171 uint32_t padding_left;
Marat Dukhan21d3bd62020-02-29 00:39:39 -0800172 uint32_t pooling_height;
173 uint32_t pooling_width;
174 uint32_t stride_height;
175 uint32_t stride_width;
176 uint32_t dilation_height;
177 uint32_t dilation_width;
178 } pooling_2d;
Marat Dukhanab2946c2020-05-21 20:04:13 -0700179 struct {
Marat Dukhan5bbebac2020-06-10 19:42:15 -0700180 float negative_slope;
181 } leaky_relu;
182 struct {
Marat Dukhanab2946c2020-05-21 20:04:13 -0700183 size_t pre_paddings[XNN_MAX_TENSOR_DIMS];
184 size_t post_paddings[XNN_MAX_TENSOR_DIMS];
185 uint32_t padding_value;
186 } static_pad;
Marat Dukhand27202d2020-07-09 23:43:40 -0700187 struct {
188 struct xnn_shape new_shape;
189 } static_reshape;
Marat Dukhanaff24e22020-07-23 01:43:58 -0700190 struct {
191 size_t new_height;
192 size_t new_width;
193 } static_resize;
Marat Dukhan1d75a542020-02-03 12:23:01 -0800194 } params;
Marat Dukhan54dcb462020-02-10 11:06:12 -0800195 struct {
196 float output_min;
197 float output_max;
198 } activation;
Marat Dukhan1d75a542020-02-03 12:23:01 -0800199 /// Value IDs for node inputs.
Marat Dukhan05b98302020-04-22 17:41:14 -0700200 uint32_t inputs[XNN_MAX_INPUTS];
Marat Dukhan1d75a542020-02-03 12:23:01 -0800201 uint32_t num_inputs;
202 /// Value IDs for node outputs.
Chao Mei0dc8f472020-05-07 01:09:46 -0700203 uint32_t outputs[XNN_MAX_OUTPUTS];
Marat Dukhan1d75a542020-02-03 12:23:01 -0800204 uint32_t num_outputs;
205 uint32_t flags;
Marat Dukhan9de90e02020-06-18 16:04:12 -0700206 uint32_t layout_flags;
207 uint32_t cluster_leader;
Marat Dukhan1d75a542020-02-03 12:23:01 -0800208};
209
210struct xnn_operator_data {
Marat Dukhan54cf5102020-05-21 20:29:25 -0700211 xnn_operator_t operator_object;
Marat Dukhan1d75a542020-02-03 12:23:01 -0800212 size_t batch_size;
213 size_t input_height;
214 size_t input_width;
Marat Dukhanaff24e22020-07-23 01:43:58 -0700215 size_t output_height;
216 size_t output_width;
Marat Dukhan54dcb462020-02-10 11:06:12 -0800217 struct xnn_shape shape1;
218 struct xnn_shape shape2;
Marat Dukhanab2946c2020-05-21 20:04:13 -0700219 size_t pre_paddings[XNN_MAX_TENSOR_DIMS];
220 size_t post_paddings[XNN_MAX_TENSOR_DIMS];
Marat Dukhanf5870842020-04-27 18:19:54 -0700221 uint32_t adjustment_height;
222 uint32_t adjustment_width;
Marat Dukhan1d75a542020-02-03 12:23:01 -0800223 uint32_t inputs[XNN_MAX_RUNTIME_INPUTS];
224 uint32_t outputs[XNN_MAX_RUNTIME_OUTPUTS];
225};
226
227struct xnn_subgraph {
228 /// Number of Value IDs reserved for communication with external graph representation.
229 /// Values created during subgraph transformation avoid using IDs in [0, reserved_value_ids-1] range.
230 uint32_t external_value_ids;
231
232 uint32_t num_reserved_values;
233 uint32_t num_values;
234 struct xnn_value* values;
235
236 uint32_t num_reserved_nodes;
237 uint32_t num_nodes;
238 struct xnn_node* nodes;
239};
240
241/// Runtime is a combination of an execution plan for subgraph Nodes and a memory manager for subgraph Values.
242struct xnn_runtime {
243 uint32_t num_external_values;
244
245 /// List of operators in the execution plan, in execution order.
Marat Dukhan54cf5102020-05-21 20:29:25 -0700246 struct xnn_operator_data* opdata;
Marat Dukhan1d75a542020-02-03 12:23:01 -0800247 /// Number of operators in the execution plan.
248 size_t num_ops;
249
250 struct xnn_blob* blobs;
251 size_t num_blobs;
252
253 void* workspace;
Marat Dukhan022c6592020-02-05 18:07:41 -0800254
255 pthreadpool_t threadpool;
Marat Dukhan1d75a542020-02-03 12:23:01 -0800256};
257
258struct xnn_value* xnn_subgraph_new_internal_value(xnn_subgraph_t subgraph);
259
260struct xnn_node* xnn_subgraph_new_node(xnn_subgraph_t subgraph);
261
262size_t xnn_tensor_get_size(
263 xnn_subgraph_t subgraph,
264 uint32_t value_id);
Marat Dukhan1f198722020-05-24 14:07:03 -0700265
266enum xnn_status xnn_subgraph_optimize(xnn_subgraph_t subgraph, uint32_t flags);
267
XNNPACK Teamab8c4c82020-10-09 08:05:51 -0700268void xnn_subgraph_rewrite_for_nchw(xnn_subgraph_t subgraph);
269
Marat Dukhan1f198722020-05-24 14:07:03 -0700270void xnn_node_clear(struct xnn_node* node);
271void xnn_value_clear(struct xnn_value* value);
XNNPACK Teamab8c4c82020-10-09 08:05:51 -0700272
273
274#ifdef __cplusplus
275} // extern "C"
276#endif