blob: 17e8384c9542b3947b9cbfa3e4456d17b64cac6d [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
19struct xnn_shape {
20 size_t num_dims;
21 size_t dim[XNN_MAX_TENSOR_DIMS];
22};
23
24enum xnn_value_type {
25 xnn_value_type_invalid = 0,
26 xnn_value_type_dense_tensor = 1,
27};
28
29/// Abstraction for a collections of elements produced and consumed by nodes.
30struct xnn_value {
31 /// Unique ID for the value.
32 uint32_t id;
33 /// Type of the collection of elements.
34 ///
35 /// Currently only dense tensors are supported.
36 /// Other types (e.g. sparse tensors) might be supported in the future.
37 enum xnn_value_type type;
38 /// Type of elements in the collection.
39 enum xnn_datatype datatype;
40 /// Tensor shape.
41 struct xnn_shape shape;
42 /// Binary features of the tensor. Supported values are any combination of:
43 /// - XNN_VALUE_FLAG_EXTERNAL_INPUT
44 /// - XNN_VALUE_FLAG_EXTERNAL_OUTPUT
45 uint32_t flags;
46 /// Static initialization data. Must be null for non-static values.
47 const void* data;
48};
49
50struct xnn_blob {
51 /// Size in bytes.
52 size_t size;
53 /// Data pointer.
54 void* data;
55 bool external;
56};
57
58enum xnn_node_type {
59 xnn_node_type_invalid = 0,
Marat Dukhan54dcb462020-02-10 11:06:12 -080060 xnn_node_type_add2,
Marat Dukhan5cb16e72020-05-05 16:41:57 -070061 xnn_node_type_argmax_pooling_2d,
Marat Dukhan21d3bd62020-02-29 00:39:39 -080062 xnn_node_type_average_pooling_2d,
Marat Dukhan52bd86f2020-02-11 18:21:51 -080063 xnn_node_type_clamp,
Marat Dukhanab2946c2020-05-21 20:04:13 -070064 xnn_node_type_constant_pad,
Marat Dukhan1d75a542020-02-03 12:23:01 -080065 xnn_node_type_convolution_2d,
Marat Dukhanf5870842020-04-27 18:19:54 -070066 xnn_node_type_deconvolution_2d,
Marat Dukhan1d75a542020-02-03 12:23:01 -080067 xnn_node_type_depthwise_convolution_2d,
Marat Dukhan38c07ec2020-04-23 16:44:32 -070068 xnn_node_type_fully_connected,
Marat Dukhan52bd86f2020-02-11 18:21:51 -080069 xnn_node_type_hardswish,
Marat Dukhan54dcb462020-02-10 11:06:12 -080070 xnn_node_type_multiply2,
Marat Dukhan21d3bd62020-02-29 00:39:39 -080071 xnn_node_type_max_pooling_2d,
Marat Dukhan2fd2ba12020-02-10 13:14:45 -080072 xnn_node_type_prelu,
Marat Dukhan52bd86f2020-02-11 18:21:51 -080073 xnn_node_type_sigmoid,
74 xnn_node_type_softmax,
Marat Dukhan5cb16e72020-05-05 16:41:57 -070075 xnn_node_type_unpooling_2d,
Marat Dukhan1d75a542020-02-03 12:23:01 -080076};
77
78struct xnn_node {
79 enum xnn_node_type type;
80 uint32_t id;
81 /// Static parameters of the operator node.
82 union {
83 struct {
84 uint32_t input_padding_top;
85 uint32_t input_padding_right;
86 uint32_t input_padding_bottom;
87 uint32_t input_padding_left;
88 uint32_t kernel_height;
89 uint32_t kernel_width;
90 uint32_t subsampling_height;
91 uint32_t subsampling_width;
92 uint32_t dilation_height;
93 uint32_t dilation_width;
94 uint32_t groups;
95 size_t group_input_channels;
96 size_t group_output_channels;
Marat Dukhan1d75a542020-02-03 12:23:01 -080097 } convolution_2d;
98 struct {
Marat Dukhanf5870842020-04-27 18:19:54 -070099 uint32_t padding_top;
100 uint32_t padding_right;
101 uint32_t padding_bottom;
102 uint32_t padding_left;
103 uint32_t adjustment_height;
104 uint32_t adjustment_width;
105 uint32_t kernel_height;
106 uint32_t kernel_width;
107 uint32_t upsampling_height;
108 uint32_t upsampling_width;
109 uint32_t dilation_height;
110 uint32_t dilation_width;
111 uint32_t groups;
112 size_t group_input_channels;
113 size_t group_output_channels;
114 } deconvolution_2d;
115 struct {
Marat Dukhan1d75a542020-02-03 12:23:01 -0800116 uint32_t input_padding_top;
117 uint32_t input_padding_right;
118 uint32_t input_padding_bottom;
119 uint32_t input_padding_left;
120 uint32_t kernel_height;
121 uint32_t kernel_width;
122 uint32_t subsampling_height;
123 uint32_t subsampling_width;
124 uint32_t dilation_height;
125 uint32_t dilation_width;
126 uint32_t depth_multiplier;
127 size_t input_channels;
Marat Dukhan1d75a542020-02-03 12:23:01 -0800128 } depthwise_convolution_2d;
Marat Dukhan21d3bd62020-02-29 00:39:39 -0800129 struct {
Marat Dukhanb389f352020-05-14 02:58:46 -0700130 uint32_t padding_top;
131 uint32_t padding_right;
132 uint32_t padding_bottom;
133 uint32_t padding_left;
Marat Dukhan21d3bd62020-02-29 00:39:39 -0800134 uint32_t pooling_height;
135 uint32_t pooling_width;
136 uint32_t stride_height;
137 uint32_t stride_width;
138 uint32_t dilation_height;
139 uint32_t dilation_width;
140 } pooling_2d;
Marat Dukhanab2946c2020-05-21 20:04:13 -0700141 struct {
142 size_t pre_paddings[XNN_MAX_TENSOR_DIMS];
143 size_t post_paddings[XNN_MAX_TENSOR_DIMS];
144 uint32_t padding_value;
145 } static_pad;
Marat Dukhan1d75a542020-02-03 12:23:01 -0800146 } params;
Marat Dukhan54dcb462020-02-10 11:06:12 -0800147 struct {
148 float output_min;
149 float output_max;
150 } activation;
Marat Dukhan1d75a542020-02-03 12:23:01 -0800151 /// Value IDs for node inputs.
Marat Dukhan05b98302020-04-22 17:41:14 -0700152 uint32_t inputs[XNN_MAX_INPUTS];
Marat Dukhan1d75a542020-02-03 12:23:01 -0800153 uint32_t num_inputs;
154 /// Value IDs for node outputs.
Chao Mei0dc8f472020-05-07 01:09:46 -0700155 uint32_t outputs[XNN_MAX_OUTPUTS];
Marat Dukhan1d75a542020-02-03 12:23:01 -0800156 uint32_t num_outputs;
157 uint32_t flags;
158};
159
160struct xnn_operator_data {
161 xnn_operator_t op;
162 size_t batch_size;
163 size_t input_height;
164 size_t input_width;
Marat Dukhan54dcb462020-02-10 11:06:12 -0800165 struct xnn_shape shape1;
166 struct xnn_shape shape2;
Marat Dukhanab2946c2020-05-21 20:04:13 -0700167 size_t pre_paddings[XNN_MAX_TENSOR_DIMS];
168 size_t post_paddings[XNN_MAX_TENSOR_DIMS];
Marat Dukhanf5870842020-04-27 18:19:54 -0700169 uint32_t adjustment_height;
170 uint32_t adjustment_width;
Marat Dukhan1d75a542020-02-03 12:23:01 -0800171 uint32_t inputs[XNN_MAX_RUNTIME_INPUTS];
172 uint32_t outputs[XNN_MAX_RUNTIME_OUTPUTS];
173};
174
175struct xnn_subgraph {
176 /// Number of Value IDs reserved for communication with external graph representation.
177 /// Values created during subgraph transformation avoid using IDs in [0, reserved_value_ids-1] range.
178 uint32_t external_value_ids;
179
180 uint32_t num_reserved_values;
181 uint32_t num_values;
182 struct xnn_value* values;
183
184 uint32_t num_reserved_nodes;
185 uint32_t num_nodes;
186 struct xnn_node* nodes;
187};
188
189/// Runtime is a combination of an execution plan for subgraph Nodes and a memory manager for subgraph Values.
190struct xnn_runtime {
191 uint32_t num_external_values;
192
193 /// List of operators in the execution plan, in execution order.
194 struct xnn_operator_data* ops;
195 /// Number of operators in the execution plan.
196 size_t num_ops;
197
198 struct xnn_blob* blobs;
199 size_t num_blobs;
200
201 void* workspace;
Marat Dukhan022c6592020-02-05 18:07:41 -0800202
203 pthreadpool_t threadpool;
Marat Dukhan1d75a542020-02-03 12:23:01 -0800204};
205
206struct xnn_value* xnn_subgraph_new_internal_value(xnn_subgraph_t subgraph);
207
208struct xnn_node* xnn_subgraph_new_node(xnn_subgraph_t subgraph);
209
210size_t xnn_tensor_get_size(
211 xnn_subgraph_t subgraph,
212 uint32_t value_id);