Marat Dukhan | 1d75a54 | 2020-02-03 12:23:01 -0800 | [diff] [blame] | 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 | #pragma once |
| 7 | |
| 8 | #include <stddef.h> |
| 9 | #include <stdint.h> |
| 10 | |
| 11 | #include <xnnpack.h> |
| 12 | |
| 13 | #define XNN_MAX_INPUTS 3 |
Marat Dukhan | 5cb16e7 | 2020-05-05 16:41:57 -0700 | [diff] [blame] | 14 | #define XNN_MAX_OUTPUTS 2 |
Marat Dukhan | 1d75a54 | 2020-02-03 12:23:01 -0800 | [diff] [blame] | 15 | |
| 16 | #define XNN_MAX_RUNTIME_INPUTS 2 |
Marat Dukhan | 5cb16e7 | 2020-05-05 16:41:57 -0700 | [diff] [blame] | 17 | #define XNN_MAX_RUNTIME_OUTPUTS 2 |
Marat Dukhan | 1d75a54 | 2020-02-03 12:23:01 -0800 | [diff] [blame] | 18 | |
| 19 | struct xnn_shape { |
| 20 | size_t num_dims; |
| 21 | size_t dim[XNN_MAX_TENSOR_DIMS]; |
| 22 | }; |
| 23 | |
| 24 | enum 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. |
| 30 | struct 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 | |
| 50 | struct xnn_blob { |
| 51 | /// Size in bytes. |
| 52 | size_t size; |
| 53 | /// Data pointer. |
| 54 | void* data; |
| 55 | bool external; |
| 56 | }; |
| 57 | |
| 58 | enum xnn_node_type { |
| 59 | xnn_node_type_invalid = 0, |
Marat Dukhan | 54dcb46 | 2020-02-10 11:06:12 -0800 | [diff] [blame] | 60 | xnn_node_type_add2, |
Marat Dukhan | 5cb16e7 | 2020-05-05 16:41:57 -0700 | [diff] [blame] | 61 | xnn_node_type_argmax_pooling_2d, |
Marat Dukhan | 21d3bd6 | 2020-02-29 00:39:39 -0800 | [diff] [blame] | 62 | xnn_node_type_average_pooling_2d, |
Marat Dukhan | 52bd86f | 2020-02-11 18:21:51 -0800 | [diff] [blame] | 63 | xnn_node_type_clamp, |
Marat Dukhan | ab2946c | 2020-05-21 20:04:13 -0700 | [diff] [blame^] | 64 | xnn_node_type_constant_pad, |
Marat Dukhan | 1d75a54 | 2020-02-03 12:23:01 -0800 | [diff] [blame] | 65 | xnn_node_type_convolution_2d, |
Marat Dukhan | f587084 | 2020-04-27 18:19:54 -0700 | [diff] [blame] | 66 | xnn_node_type_deconvolution_2d, |
Marat Dukhan | 1d75a54 | 2020-02-03 12:23:01 -0800 | [diff] [blame] | 67 | xnn_node_type_depthwise_convolution_2d, |
Marat Dukhan | 38c07ec | 2020-04-23 16:44:32 -0700 | [diff] [blame] | 68 | xnn_node_type_fully_connected, |
Marat Dukhan | 52bd86f | 2020-02-11 18:21:51 -0800 | [diff] [blame] | 69 | xnn_node_type_hardswish, |
Marat Dukhan | 54dcb46 | 2020-02-10 11:06:12 -0800 | [diff] [blame] | 70 | xnn_node_type_multiply2, |
Marat Dukhan | 21d3bd6 | 2020-02-29 00:39:39 -0800 | [diff] [blame] | 71 | xnn_node_type_max_pooling_2d, |
Marat Dukhan | 2fd2ba1 | 2020-02-10 13:14:45 -0800 | [diff] [blame] | 72 | xnn_node_type_prelu, |
Marat Dukhan | 52bd86f | 2020-02-11 18:21:51 -0800 | [diff] [blame] | 73 | xnn_node_type_sigmoid, |
| 74 | xnn_node_type_softmax, |
Marat Dukhan | 5cb16e7 | 2020-05-05 16:41:57 -0700 | [diff] [blame] | 75 | xnn_node_type_unpooling_2d, |
Marat Dukhan | 1d75a54 | 2020-02-03 12:23:01 -0800 | [diff] [blame] | 76 | }; |
| 77 | |
| 78 | struct 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 Dukhan | 1d75a54 | 2020-02-03 12:23:01 -0800 | [diff] [blame] | 97 | } convolution_2d; |
| 98 | struct { |
Marat Dukhan | f587084 | 2020-04-27 18:19:54 -0700 | [diff] [blame] | 99 | 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 Dukhan | 1d75a54 | 2020-02-03 12:23:01 -0800 | [diff] [blame] | 116 | 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 Dukhan | 1d75a54 | 2020-02-03 12:23:01 -0800 | [diff] [blame] | 128 | } depthwise_convolution_2d; |
Marat Dukhan | 21d3bd6 | 2020-02-29 00:39:39 -0800 | [diff] [blame] | 129 | struct { |
Marat Dukhan | b389f35 | 2020-05-14 02:58:46 -0700 | [diff] [blame] | 130 | uint32_t padding_top; |
| 131 | uint32_t padding_right; |
| 132 | uint32_t padding_bottom; |
| 133 | uint32_t padding_left; |
Marat Dukhan | 21d3bd6 | 2020-02-29 00:39:39 -0800 | [diff] [blame] | 134 | 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 Dukhan | ab2946c | 2020-05-21 20:04:13 -0700 | [diff] [blame^] | 141 | 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 Dukhan | 1d75a54 | 2020-02-03 12:23:01 -0800 | [diff] [blame] | 146 | } params; |
Marat Dukhan | 54dcb46 | 2020-02-10 11:06:12 -0800 | [diff] [blame] | 147 | struct { |
| 148 | float output_min; |
| 149 | float output_max; |
| 150 | } activation; |
Marat Dukhan | 1d75a54 | 2020-02-03 12:23:01 -0800 | [diff] [blame] | 151 | /// Value IDs for node inputs. |
Marat Dukhan | 05b9830 | 2020-04-22 17:41:14 -0700 | [diff] [blame] | 152 | uint32_t inputs[XNN_MAX_INPUTS]; |
Marat Dukhan | 1d75a54 | 2020-02-03 12:23:01 -0800 | [diff] [blame] | 153 | uint32_t num_inputs; |
| 154 | /// Value IDs for node outputs. |
Chao Mei | 0dc8f47 | 2020-05-07 01:09:46 -0700 | [diff] [blame] | 155 | uint32_t outputs[XNN_MAX_OUTPUTS]; |
Marat Dukhan | 1d75a54 | 2020-02-03 12:23:01 -0800 | [diff] [blame] | 156 | uint32_t num_outputs; |
| 157 | uint32_t flags; |
| 158 | }; |
| 159 | |
| 160 | struct xnn_operator_data { |
| 161 | xnn_operator_t op; |
| 162 | size_t batch_size; |
| 163 | size_t input_height; |
| 164 | size_t input_width; |
Marat Dukhan | 54dcb46 | 2020-02-10 11:06:12 -0800 | [diff] [blame] | 165 | struct xnn_shape shape1; |
| 166 | struct xnn_shape shape2; |
Marat Dukhan | ab2946c | 2020-05-21 20:04:13 -0700 | [diff] [blame^] | 167 | size_t pre_paddings[XNN_MAX_TENSOR_DIMS]; |
| 168 | size_t post_paddings[XNN_MAX_TENSOR_DIMS]; |
Marat Dukhan | f587084 | 2020-04-27 18:19:54 -0700 | [diff] [blame] | 169 | uint32_t adjustment_height; |
| 170 | uint32_t adjustment_width; |
Marat Dukhan | 1d75a54 | 2020-02-03 12:23:01 -0800 | [diff] [blame] | 171 | uint32_t inputs[XNN_MAX_RUNTIME_INPUTS]; |
| 172 | uint32_t outputs[XNN_MAX_RUNTIME_OUTPUTS]; |
| 173 | }; |
| 174 | |
| 175 | struct 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. |
| 190 | struct 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 Dukhan | 022c659 | 2020-02-05 18:07:41 -0800 | [diff] [blame] | 202 | |
| 203 | pthreadpool_t threadpool; |
Marat Dukhan | 1d75a54 | 2020-02-03 12:23:01 -0800 | [diff] [blame] | 204 | }; |
| 205 | |
| 206 | struct xnn_value* xnn_subgraph_new_internal_value(xnn_subgraph_t subgraph); |
| 207 | |
| 208 | struct xnn_node* xnn_subgraph_new_node(xnn_subgraph_t subgraph); |
| 209 | |
| 210 | size_t xnn_tensor_get_size( |
| 211 | xnn_subgraph_t subgraph, |
| 212 | uint32_t value_id); |