• 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 #pragma once
7 
8 #include <stddef.h>
9 #include <stdint.h>
10 
11 #include <xnnpack.h>
12 
13 #define XNN_MAX_INPUTS 3
14 #define XNN_MAX_OUTPUTS 1
15 
16 #define XNN_MAX_RUNTIME_INPUTS 2
17 #define XNN_MAX_RUNTIME_OUTPUTS 1
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,
60   xnn_node_type_add2,
61   xnn_node_type_clamp,
62   xnn_node_type_convolution_2d,
63   xnn_node_type_depthwise_convolution_2d,
64   xnn_node_type_hardswish,
65   xnn_node_type_multiply2,
66   xnn_node_type_prelu,
67   xnn_node_type_sigmoid,
68   xnn_node_type_softmax,
69 };
70 
71 struct xnn_node {
72   enum xnn_node_type type;
73   uint32_t id;
74   /// Static parameters of the operator node.
75   union {
76     struct {
77       uint32_t input_padding_top;
78       uint32_t input_padding_right;
79       uint32_t input_padding_bottom;
80       uint32_t input_padding_left;
81       uint32_t kernel_height;
82       uint32_t kernel_width;
83       uint32_t subsampling_height;
84       uint32_t subsampling_width;
85       uint32_t dilation_height;
86       uint32_t dilation_width;
87       uint32_t groups;
88       size_t group_input_channels;
89       size_t group_output_channels;
90     } convolution_2d;
91     struct {
92       uint32_t input_padding_top;
93       uint32_t input_padding_right;
94       uint32_t input_padding_bottom;
95       uint32_t input_padding_left;
96       uint32_t kernel_height;
97       uint32_t kernel_width;
98       uint32_t subsampling_height;
99       uint32_t subsampling_width;
100       uint32_t dilation_height;
101       uint32_t dilation_width;
102       uint32_t depth_multiplier;
103       size_t input_channels;
104     } depthwise_convolution_2d;
105   } params;
106   struct {
107     float output_min;
108     float output_max;
109   } activation;
110   /// Value IDs for node inputs.
111   union {
112     uint32_t raw[XNN_MAX_INPUTS];
113     struct {
114       uint32_t input;
115       uint32_t filter;
116       uint32_t bias;
117     } convolution_2d;
118   } inputs;
119   uint32_t num_inputs;
120   /// Value IDs for node outputs.
121   union {
122     struct {
123       uint32_t output;
124     } convolution_2d;
125     uint32_t raw[XNN_MAX_OUTPUTS];
126   } outputs;
127   uint32_t num_outputs;
128   uint32_t flags;
129 };
130 
131 struct xnn_operator_data {
132   xnn_operator_t op;
133   size_t batch_size;
134   size_t input_height;
135   size_t input_width;
136   struct xnn_shape shape1;
137   struct xnn_shape shape2;
138   uint32_t inputs[XNN_MAX_RUNTIME_INPUTS];
139   uint32_t outputs[XNN_MAX_RUNTIME_OUTPUTS];
140 };
141 
142 struct xnn_subgraph {
143   /// Number of Value IDs reserved for communication with external graph representation.
144   /// Values created during subgraph transformation avoid using IDs in [0, reserved_value_ids-1] range.
145   uint32_t external_value_ids;
146 
147   uint32_t num_reserved_values;
148   uint32_t num_values;
149   struct xnn_value* values;
150 
151   uint32_t num_reserved_nodes;
152   uint32_t num_nodes;
153   struct xnn_node* nodes;
154 };
155 
156 /// Runtime is a combination of an execution plan for subgraph Nodes and a memory manager for subgraph Values.
157 struct xnn_runtime {
158   uint32_t num_external_values;
159 
160   /// List of operators in the execution plan, in execution order.
161   struct xnn_operator_data* ops;
162   /// Number of operators in the execution plan.
163   size_t num_ops;
164 
165   struct xnn_blob* blobs;
166   size_t num_blobs;
167 
168   void* workspace;
169 
170   pthreadpool_t threadpool;
171 };
172 
173 struct xnn_value* xnn_subgraph_new_internal_value(xnn_subgraph_t subgraph);
174 
175 struct xnn_node* xnn_subgraph_new_node(xnn_subgraph_t subgraph);
176 
177 size_t xnn_tensor_get_size(
178   xnn_subgraph_t subgraph,
179   uint32_t value_id);
180