blob: 0508cbf15348ad950752fe6cd0a2f397104722c3 [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 Dukhan1d75a542020-02-03 12:23:01 -080064 xnn_node_type_convolution_2d,
Marat Dukhanf5870842020-04-27 18:19:54 -070065 xnn_node_type_deconvolution_2d,
Marat Dukhan1d75a542020-02-03 12:23:01 -080066 xnn_node_type_depthwise_convolution_2d,
Marat Dukhan38c07ec2020-04-23 16:44:32 -070067 xnn_node_type_fully_connected,
Marat Dukhan52bd86f2020-02-11 18:21:51 -080068 xnn_node_type_hardswish,
Marat Dukhan54dcb462020-02-10 11:06:12 -080069 xnn_node_type_multiply2,
Marat Dukhan21d3bd62020-02-29 00:39:39 -080070 xnn_node_type_max_pooling_2d,
Marat Dukhan2fd2ba12020-02-10 13:14:45 -080071 xnn_node_type_prelu,
Marat Dukhan52bd86f2020-02-11 18:21:51 -080072 xnn_node_type_sigmoid,
73 xnn_node_type_softmax,
Marat Dukhan5cb16e72020-05-05 16:41:57 -070074 xnn_node_type_unpooling_2d,
Marat Dukhan1d75a542020-02-03 12:23:01 -080075};
76
77struct xnn_node {
78 enum xnn_node_type type;
79 uint32_t id;
80 /// Static parameters of the operator node.
81 union {
82 struct {
83 uint32_t input_padding_top;
84 uint32_t input_padding_right;
85 uint32_t input_padding_bottom;
86 uint32_t input_padding_left;
87 uint32_t kernel_height;
88 uint32_t kernel_width;
89 uint32_t subsampling_height;
90 uint32_t subsampling_width;
91 uint32_t dilation_height;
92 uint32_t dilation_width;
93 uint32_t groups;
94 size_t group_input_channels;
95 size_t group_output_channels;
Marat Dukhan1d75a542020-02-03 12:23:01 -080096 } convolution_2d;
97 struct {
Marat Dukhanf5870842020-04-27 18:19:54 -070098 uint32_t padding_top;
99 uint32_t padding_right;
100 uint32_t padding_bottom;
101 uint32_t padding_left;
102 uint32_t adjustment_height;
103 uint32_t adjustment_width;
104 uint32_t kernel_height;
105 uint32_t kernel_width;
106 uint32_t upsampling_height;
107 uint32_t upsampling_width;
108 uint32_t dilation_height;
109 uint32_t dilation_width;
110 uint32_t groups;
111 size_t group_input_channels;
112 size_t group_output_channels;
113 } deconvolution_2d;
114 struct {
Marat Dukhan1d75a542020-02-03 12:23:01 -0800115 uint32_t input_padding_top;
116 uint32_t input_padding_right;
117 uint32_t input_padding_bottom;
118 uint32_t input_padding_left;
119 uint32_t kernel_height;
120 uint32_t kernel_width;
121 uint32_t subsampling_height;
122 uint32_t subsampling_width;
123 uint32_t dilation_height;
124 uint32_t dilation_width;
125 uint32_t depth_multiplier;
126 size_t input_channels;
Marat Dukhan1d75a542020-02-03 12:23:01 -0800127 } depthwise_convolution_2d;
Marat Dukhan21d3bd62020-02-29 00:39:39 -0800128 struct {
Marat Dukhanb389f352020-05-14 02:58:46 -0700129 uint32_t padding_top;
130 uint32_t padding_right;
131 uint32_t padding_bottom;
132 uint32_t padding_left;
Marat Dukhan21d3bd62020-02-29 00:39:39 -0800133 uint32_t pooling_height;
134 uint32_t pooling_width;
135 uint32_t stride_height;
136 uint32_t stride_width;
137 uint32_t dilation_height;
138 uint32_t dilation_width;
139 } pooling_2d;
Marat Dukhan1d75a542020-02-03 12:23:01 -0800140 } params;
Marat Dukhan54dcb462020-02-10 11:06:12 -0800141 struct {
142 float output_min;
143 float output_max;
144 } activation;
Marat Dukhan1d75a542020-02-03 12:23:01 -0800145 /// Value IDs for node inputs.
Marat Dukhan05b98302020-04-22 17:41:14 -0700146 uint32_t inputs[XNN_MAX_INPUTS];
Marat Dukhan1d75a542020-02-03 12:23:01 -0800147 uint32_t num_inputs;
148 /// Value IDs for node outputs.
Chao Mei0dc8f472020-05-07 01:09:46 -0700149 uint32_t outputs[XNN_MAX_OUTPUTS];
Marat Dukhan1d75a542020-02-03 12:23:01 -0800150 uint32_t num_outputs;
151 uint32_t flags;
152};
153
154struct xnn_operator_data {
155 xnn_operator_t op;
156 size_t batch_size;
157 size_t input_height;
158 size_t input_width;
Marat Dukhan54dcb462020-02-10 11:06:12 -0800159 struct xnn_shape shape1;
160 struct xnn_shape shape2;
Marat Dukhanf5870842020-04-27 18:19:54 -0700161 uint32_t adjustment_height;
162 uint32_t adjustment_width;
Marat Dukhan1d75a542020-02-03 12:23:01 -0800163 uint32_t inputs[XNN_MAX_RUNTIME_INPUTS];
164 uint32_t outputs[XNN_MAX_RUNTIME_OUTPUTS];
165};
166
167struct xnn_subgraph {
168 /// Number of Value IDs reserved for communication with external graph representation.
169 /// Values created during subgraph transformation avoid using IDs in [0, reserved_value_ids-1] range.
170 uint32_t external_value_ids;
171
172 uint32_t num_reserved_values;
173 uint32_t num_values;
174 struct xnn_value* values;
175
176 uint32_t num_reserved_nodes;
177 uint32_t num_nodes;
178 struct xnn_node* nodes;
179};
180
181/// Runtime is a combination of an execution plan for subgraph Nodes and a memory manager for subgraph Values.
182struct xnn_runtime {
183 uint32_t num_external_values;
184
185 /// List of operators in the execution plan, in execution order.
186 struct xnn_operator_data* ops;
187 /// Number of operators in the execution plan.
188 size_t num_ops;
189
190 struct xnn_blob* blobs;
191 size_t num_blobs;
192
193 void* workspace;
Marat Dukhan022c6592020-02-05 18:07:41 -0800194
195 pthreadpool_t threadpool;
Marat Dukhan1d75a542020-02-03 12:23:01 -0800196};
197
198struct xnn_value* xnn_subgraph_new_internal_value(xnn_subgraph_t subgraph);
199
200struct xnn_node* xnn_subgraph_new_node(xnn_subgraph_t subgraph);
201
202size_t xnn_tensor_get_size(
203 xnn_subgraph_t subgraph,
204 uint32_t value_id);