blob: 86b106f1eeb4ef4f3abb8ddccca1f3a54bb728d3 [file] [log] [blame]
telsoa014fcda012018-03-09 14:13:49 +00001//
2// Copyright © 2017 Arm Ltd. All rights reserved.
David Beckecb56cd2018-09-05 12:52:57 +01003// SPDX-License-Identifier: MIT
telsoa014fcda012018-03-09 14:13:49 +00004//
5#pragma once
6
David Beckf0b48452018-10-19 15:20:56 +01007#include <armnn/NetworkFwd.hpp>
8#include <armnn/DescriptorsFwd.hpp>
9#include <armnn/TensorFwd.hpp>
10#include <armnn/Optional.hpp>
telsoa014fcda012018-03-09 14:13:49 +000011
David Beckf0b48452018-10-19 15:20:56 +010012#include <armnn/Types.hpp>
telsoa014fcda012018-03-09 14:13:49 +000013
14#include <memory>
telsoa01c577f2c2018-08-31 09:22:23 +010015#include <vector>
telsoa014fcda012018-03-09 14:13:49 +000016
17namespace armnn
18{
19
20/// @brief An input connection slot for a layer.
21/// The input slot can be connected to an output slot of the preceding layer in the graph.
22/// Only one connection to the input slot is allowed.
23class IInputSlot
24{
25public:
26 virtual const IOutputSlot* GetConnection() const = 0;
27 virtual IOutputSlot* GetConnection() = 0;
28
29protected:
telsoa01c577f2c2018-08-31 09:22:23 +010030 /// Not user deletable.
31 ~IInputSlot() {}
telsoa014fcda012018-03-09 14:13:49 +000032};
33
34/// @brief An output connection slot for a layer.
35/// The output slot may be connected to 1 or more input slots of subsequent layers in the graph.
36class IOutputSlot
37{
38public:
39 virtual unsigned int GetNumConnections() const = 0;
40 virtual const IInputSlot* GetConnection(unsigned int index) const = 0;
41 virtual IInputSlot* GetConnection(unsigned int index) = 0;
42
43 virtual void SetTensorInfo(const TensorInfo& tensorInfo) = 0;
44 virtual const TensorInfo& GetTensorInfo() const = 0;
45 virtual bool IsTensorInfoSet() const = 0;
46
47 virtual int Connect(IInputSlot& destination) = 0;
48 virtual void Disconnect(IInputSlot& slot) = 0;
49
50protected:
telsoa01c577f2c2018-08-31 09:22:23 +010051 /// Not user deletable.
52 ~IOutputSlot() {}
telsoa014fcda012018-03-09 14:13:49 +000053};
54
55/// @brief Interface for a layer that is connectable to other layers via InputSlots and OutputSlots.
56class IConnectableLayer
57{
58public:
59 virtual const char* GetName() const = 0;
60
61 virtual unsigned int GetNumInputSlots() const = 0;
62 virtual unsigned int GetNumOutputSlots() const = 0;
63
64 virtual const IInputSlot& GetInputSlot(unsigned int index) const = 0;
65 virtual IInputSlot& GetInputSlot(unsigned int index) = 0;
66
67 virtual const IOutputSlot& GetOutputSlot(unsigned int index) const = 0;
68 virtual IOutputSlot& GetOutputSlot(unsigned int index) = 0;
69
telsoa01c577f2c2018-08-31 09:22:23 +010070 virtual std::vector<TensorShape> InferOutputShapes(const std::vector<TensorShape>& inputShapes) const = 0;
71
surmeh01bceff2f2018-03-29 16:29:27 +010072 virtual LayerGuid GetGuid() const = 0;
telsoa014fcda012018-03-09 14:13:49 +000073protected:
telsoa01c577f2c2018-08-31 09:22:23 +010074 /// Objects are not deletable via the handle
75 ~IConnectableLayer() {}
telsoa014fcda012018-03-09 14:13:49 +000076};
77
78using INetworkPtr = std::unique_ptr<INetwork, void(*)(INetwork* network)>;
79
80/// Main network class which provides the interface for building up a neural network.
81/// This object is subsequently required by the IRuntime::Load() method.
82class INetwork
83{
84public:
85 static INetwork* CreateRaw();
86 static INetworkPtr Create();
87 static void Destroy(INetwork* network);
88
89 virtual Status PrintGraph() = 0;
90
telsoa01c577f2c2018-08-31 09:22:23 +010091 /// Adds an input layer to the network.
92 /// @param id - User generated id to uniquely identify a particular input. The same id needs to be specified.
telsoa014fcda012018-03-09 14:13:49 +000093 /// when passing the inputs to the IRuntime::EnqueueWorkload() function.
telsoa01c577f2c2018-08-31 09:22:23 +010094 /// @param name - Optional name for the layer.
95 /// @return - Interface for configuring the layer.
telsoa014fcda012018-03-09 14:13:49 +000096 virtual IConnectableLayer* AddInputLayer(LayerBindingId id, const char* name = nullptr) = 0;
97
telsoa01c577f2c2018-08-31 09:22:23 +010098 /// Adds a 2D convolution layer to the network.
99 /// @param convolution2dDescriptor - Description of the 2D convolution layer.
100 /// @param weights - Tensor for the weights data.
101 /// @param biases - (Optional) Tensor for the bias data. Must match the output tensor shape.
102 /// @param name - Optional name for the layer.
103 /// @return - Interface for configuring the layer.
telsoa014fcda012018-03-09 14:13:49 +0000104 virtual IConnectableLayer* AddConvolution2dLayer(const Convolution2dDescriptor& convolution2dDescriptor,
105 const ConstTensor& weights,
106 const char* name = nullptr) = 0;
107
108 virtual IConnectableLayer* AddConvolution2dLayer(const Convolution2dDescriptor& convolution2dDescriptor,
109 const ConstTensor& weights,
110 const ConstTensor& biases,
111 const char* name = nullptr) = 0;
112
telsoa01c577f2c2018-08-31 09:22:23 +0100113 /// Adds a 2D depthwise convolution layer to the network.
114 /// @param convolution2dDescriptor - Description of the 2D depthwise convolution layer.
Rob Hughes144c01b2018-11-21 13:34:24 +0000115 /// @param weights - Tensor for the weights. Expected format: [channelMultiplier, inputChannels, height, width].
telsoa01c577f2c2018-08-31 09:22:23 +0100116 /// @param biases (Optional) - Tensor for the bias data. Must match the output tensor shape.
117 /// @param name - Optional name for the layer.
118 /// @return - Interface for configuring the layer.
telsoa014fcda012018-03-09 14:13:49 +0000119 virtual IConnectableLayer* AddDepthwiseConvolution2dLayer(
120 const DepthwiseConvolution2dDescriptor& convolution2dDescriptor,
121 const ConstTensor& weights,
122 const char* name = nullptr) = 0;
123
124 virtual IConnectableLayer* AddDepthwiseConvolution2dLayer(
125 const DepthwiseConvolution2dDescriptor& convolution2dDescriptor,
126 const ConstTensor& weights,
127 const ConstTensor& biases,
128 const char* name = nullptr) = 0;
129
telsoa01c577f2c2018-08-31 09:22:23 +0100130 /// Adds a fully connected layer to the network.
131 /// @param fullyConnectedDescriptor - Description of the fully connected layer.
132 /// @param weights - Tensor for the weights data.
133 /// @param biases - (Optional) Tensor for the bias data.
134 /// @param name - Optional name for the layer.
135 /// @return - Interface for configuring the layer.
telsoa014fcda012018-03-09 14:13:49 +0000136 virtual IConnectableLayer* AddFullyConnectedLayer(const FullyConnectedDescriptor& fullyConnectedDescriptor,
137 const ConstTensor& weights,
138 const char* name = nullptr) = 0;
139
140 virtual IConnectableLayer* AddFullyConnectedLayer(const FullyConnectedDescriptor& fullyConnectedDescriptor,
141 const ConstTensor& weights,
142 const ConstTensor& biases,
143 const char* name = nullptr) = 0;
144
telsoa01c577f2c2018-08-31 09:22:23 +0100145 /// Adds a permute layer to the network.
146 /// @param permuteDescriptor - PermuteDescriptor to configure the permute.
147 /// @param name - Optional name for the layer.
148 /// @return - Interface for configuring the layer.
telsoa014fcda012018-03-09 14:13:49 +0000149 virtual IConnectableLayer* AddPermuteLayer(const PermuteDescriptor& permuteDescriptor,
150 const char* name = nullptr) = 0;
151
Éanna Ó Catháin4e1e1362018-11-12 11:36:34 +0000152 /// Adds a batch to space ND layer to the network.
153 /// @param batchToSpaceNdDescriptor - Description of the layer.
154 /// @param name - Optional name for the layer.
155 /// @return - Interface for configuring the layer.
156 virtual IConnectableLayer* AddBatchToSpaceNdLayer(const BatchToSpaceNdDescriptor& batchToSpaceNdDescriptor,
157 const char* name = nullptr) = 0;
158
telsoa01c577f2c2018-08-31 09:22:23 +0100159 /// Adds a pooling layer to the network.
160 /// @param pooling2dDescriptor - Pooling2dDescriptor to configure the pooling.
161 /// @param name - Optional name for the layer.
162 /// @return - Interface for configuring the layer.
telsoa014fcda012018-03-09 14:13:49 +0000163 virtual IConnectableLayer* AddPooling2dLayer(const Pooling2dDescriptor& pooling2dDescriptor,
164 const char* name = nullptr) = 0;
165
telsoa01c577f2c2018-08-31 09:22:23 +0100166 /// Adds an activation layer to the network.
167 /// @param activationDescriptor - ActivationDescriptor to configure the activation.
168 /// @param name - Optional name for the layer.
169 /// @return - Interface for configuring the layer.
telsoa014fcda012018-03-09 14:13:49 +0000170 virtual IConnectableLayer* AddActivationLayer(const ActivationDescriptor& activationDescriptor,
171 const char* name = nullptr) = 0;
172
telsoa01c577f2c2018-08-31 09:22:23 +0100173 /// Adds a normalization layer to the network.
174 /// @param normalizationDescriptor - NormalizationDescriptor to configure the normalization.
175 /// @param name - Optional name for the layer.
176 /// @return - Interface for configuring the layer.
telsoa014fcda012018-03-09 14:13:49 +0000177 virtual IConnectableLayer* AddNormalizationLayer(const NormalizationDescriptor& normalizationDescriptor,
178 const char* name = nullptr) = 0;
179
telsoa01c577f2c2018-08-31 09:22:23 +0100180 /// Adds a softmax layer to the network.
181 /// @param softmaxDescriptor - SoftmaxDescriptor to configure the softmax.
182 /// @param name - Optional name for the layer.
183 /// @return - Interface for configuring the layer.
telsoa014fcda012018-03-09 14:13:49 +0000184 virtual IConnectableLayer* AddSoftmaxLayer(const SoftmaxDescriptor& softmaxDescriptor,
185 const char* name = nullptr) = 0;
186
telsoa01c577f2c2018-08-31 09:22:23 +0100187 /// Adds a splitter layer to the network.
188 /// @param splitterDescriptor - WindowsDescriptor to configure the splitting process.
189 /// Number of Views must be equal to the number of outputs,
190 /// and their order must match - e.g. first view corresponds to
191 /// the first output, second view to the second output, etc....
192 /// @param name - Optional name for the layer.
193 /// @return - Interface for configuring the layer.
telsoa014fcda012018-03-09 14:13:49 +0000194 virtual IConnectableLayer* AddSplitterLayer(const ViewsDescriptor& splitterDescriptor
195 , const char* name = nullptr) = 0;
196
telsoa01c577f2c2018-08-31 09:22:23 +0100197 /// Adds a merger layer to the network.
198 /// @param mergerDescriptor - WindowsDescriptor to configure the merging process. Number of Views must be equal to
telsoa014fcda012018-03-09 14:13:49 +0000199 /// the number of inputs, and their order must match - e.g. first view corresponds to
200 /// the first input, second view to the second input, etc....
telsoa01c577f2c2018-08-31 09:22:23 +0100201 /// @param name - Optional name for the layer.
202 /// @return - Interface for configuring the layer.
telsoa014fcda012018-03-09 14:13:49 +0000203 virtual IConnectableLayer* AddMergerLayer(const OriginsDescriptor& mergerDescriptor,
204 const char* name = nullptr) = 0;
205
telsoa01c577f2c2018-08-31 09:22:23 +0100206 /// Adds an addition layer to the network.
207 /// @param name - Optional name for the layer.
208 /// @return - Interface for configuring the layer.
telsoa014fcda012018-03-09 14:13:49 +0000209 virtual IConnectableLayer* AddAdditionLayer(const char* name = nullptr) = 0;
210
telsoa01c577f2c2018-08-31 09:22:23 +0100211 /// Adds a multiplication layer to the network.
212 /// @param name - Optional name for the layer.
213 /// @return - Interface for configuring the layer.
telsoa014fcda012018-03-09 14:13:49 +0000214 virtual IConnectableLayer* AddMultiplicationLayer(const char* name = nullptr) = 0;
215
telsoa01c577f2c2018-08-31 09:22:23 +0100216 /// Adds a batch normalization layer to the network.
217 /// @param mean - Pre-calculated mean for each channel.
218 /// @param variance - Pre-calculated variance for each channel.
219 /// @param beta - Per-channel additive factor.
220 /// @param gamma - Per-channel multiplicative factor.
221 /// @return - Interface for configuring the layer.
222 /// @param name - Optional name for the layer.
telsoa014fcda012018-03-09 14:13:49 +0000223 virtual IConnectableLayer* AddBatchNormalizationLayer(const BatchNormalizationDescriptor& desc,
224 const ConstTensor& mean,
225 const ConstTensor& variance,
226 const ConstTensor& beta,
227 const ConstTensor& gamma,
228 const char* name = nullptr) = 0;
229
telsoa01c577f2c2018-08-31 09:22:23 +0100230 /// Adds a resize bilinear layer to the network.
231 /// @param resizeDesc - Parameters for the resize operation.
232 /// @param name - Optional name for the layer.
233 /// @return - Interface for configuring the layer.
telsoa014fcda012018-03-09 14:13:49 +0000234 virtual IConnectableLayer* AddResizeBilinearLayer(const ResizeBilinearDescriptor& resizeDesc,
235 const char* name = nullptr) = 0;
236
telsoa01c577f2c2018-08-31 09:22:23 +0100237 /// Adds an L2 normalization layer to the network.
telsoa014fcda012018-03-09 14:13:49 +0000238 /// Normalization is performed along dimension 1, but requires a 4d input.
Matteo Martincighbcd3c852018-09-28 14:14:12 +0100239 /// @param desc - Parameters for the L2 normalization operation.
telsoa01c577f2c2018-08-31 09:22:23 +0100240 /// @param name - Optional name for the layer.
241 /// @return - Interface for configuring the layer.
Matteo Martincighbcd3c852018-09-28 14:14:12 +0100242 virtual IConnectableLayer* AddL2NormalizationLayer(const L2NormalizationDescriptor& desc,
243 const char* name = nullptr) = 0;
telsoa014fcda012018-03-09 14:13:49 +0000244
245 /// Adds a layer with no inputs and a single output, which always corresponds to
246 /// the passed in constant tensor.
telsoa01c577f2c2018-08-31 09:22:23 +0100247 /// @param input - Tensor to be provided as the only output of the layer. The layer will maintain
248 /// its own copy of the tensor data, meaning the memory referenced by @a input can
249 /// be freed or reused after this function is called.
250 /// @param name - Optional name for the layer.
251 /// @return - Interface for configuring the layer.
telsoa014fcda012018-03-09 14:13:49 +0000252 virtual IConnectableLayer* AddConstantLayer(const ConstTensor& input,
253 const char* name = nullptr) = 0;
254
telsoa01c577f2c2018-08-31 09:22:23 +0100255 /// Adds a reshape layer to the network.
256 /// @param reshapeDescriptor - Parameters for the reshape operation.
257 /// @param name - Optional name for the layer.
258 /// @return - Interface for configuring the layer.
telsoa014fcda012018-03-09 14:13:49 +0000259 virtual IConnectableLayer* AddReshapeLayer(const ReshapeDescriptor& reshapeDescriptor,
260 const char* name = nullptr) = 0;
261
Nattapat Chaimanowong207ef9a2018-11-02 10:57:25 +0000262 /// Adds a space to batch layer to the network.
263 /// @param spaceToBatchNdDescriptor - Parameters for the space to batch operation.
264 /// @param name - Optional name for the layer.
265 /// @return - Interface for configuring the layer.
266 virtual IConnectableLayer* AddSpaceToBatchNdLayer(const SpaceToBatchNdDescriptor& spaceToBatchNdDescriptor,
267 const char* name = nullptr) = 0;
268
telsoa01c577f2c2018-08-31 09:22:23 +0100269 /// Adds a floor layer to the network.
270 /// @param name - Optional name for the layer.
271 /// @return - Interface for configuring the layer.
telsoa014fcda012018-03-09 14:13:49 +0000272 virtual IConnectableLayer* AddFloorLayer(const char* name = nullptr) = 0;
273
telsoa01c577f2c2018-08-31 09:22:23 +0100274 /// Adds an output layer to the network.
275 /// @param id - User generated id to uniquely identify a particular output. The same id needs to be specified
telsoa014fcda012018-03-09 14:13:49 +0000276 /// when passing the outputs to the IRuntime::EnqueueWorkload() function.
telsoa01c577f2c2018-08-31 09:22:23 +0100277 /// @param name - Optional name for the layer.
278 /// @return - Interface for configuring the layer.
279 virtual IConnectableLayer* AddOutputLayer(LayerBindingId id, const char* name = nullptr) = 0;
280
281 /// Add a Lstm layer to the network
282 /// @param descriptor Parameters for the Lstm operation
telsoa014fcda012018-03-09 14:13:49 +0000283 /// @param name Optional name for the layer
284 /// @return Interface for configuring the layer.
telsoa01c577f2c2018-08-31 09:22:23 +0100285 virtual IConnectableLayer* AddLstmLayer(const LstmDescriptor& descriptor,
286 const LstmInputParams& params,
287 const char* name = nullptr) = 0;
telsoa014fcda012018-03-09 14:13:49 +0000288
Francis Murtaghe7a86a42018-08-29 12:42:10 +0100289 /// Adds a division layer to the network.
290 /// @param name - Optional name for the layer.
291 /// @return - Interface for configuring the layer.
292 virtual IConnectableLayer* AddDivisionLayer(const char* name = nullptr) = 0;
293
David Beck19526222018-09-12 16:00:08 +0100294 /// Adds a subtraction layer to the network.
295 /// @param name - Optional name for the layer.
296 /// @return - Interface for configuring the layer.
297 virtual IConnectableLayer* AddSubtractionLayer(const char* name = nullptr) = 0;
298
Nattapat Chaimanowong5a4304a2018-11-28 10:44:37 +0000299 /// Add a Maximum layer to the network.
300 /// @param name - Optional name for the layer.
301 /// @ return - Interface for configuring the layer.
302 virtual IConnectableLayer* AddMaximumLayer(const char* name = nullptr) = 0;
303
narpra0132b90462018-09-13 11:07:48 +0100304 /// Add a Mean layer to the network.
305 /// @param meanDescriptor - Parameters for the mean operation.
306 /// @param name - Optional name for the layer.
307 /// @ return - Interface for configuring the layer.
308 virtual IConnectableLayer* AddMeanLayer(const MeanDescriptor& meanDescriptor, const char* name = nullptr) = 0;
309
Mohamed Nour Abouelseoud5662c202018-09-24 13:30:09 +0100310 /// Adds a fully pad layer to the network.
311 /// @param paddings - n by 2 tensor, where n is the rank of the input tensor,
312 /// such that paddings[i,0] indicates the amount of padding to add in front of dimonsion i, and
313 /// paddings[i,1] indicates the amount of padding to add after the end of dimension i
314 /// @param name - Optional name for the layer.
315 /// @return - Interface for configuring the layer.
316 virtual IConnectableLayer* AddPadLayer(const PadDescriptor& padDescriptor,
317 const char* name = nullptr) = 0;
318
Conor Kennedy430b5d82018-11-14 15:28:28 +0000319 /// Adds a strided slice layer to the network.
320 /// @param StridedSliceDescriptor - Parameters for the strided slice operation.
321 /// @param name - Optional name for the layer.
322 /// @return - Interface for configuring the layer.
323 virtual IConnectableLayer* AddStridedSliceLayer(const StridedSliceDescriptor& stridedSliceDescriptor,
324 const char* name = nullptr) = 0;
325
telsoa014fcda012018-03-09 14:13:49 +0000326protected:
327 ~INetwork() {}
328};
329
330using IOptimizedNetworkPtr = std::unique_ptr<IOptimizedNetwork, void(*)(IOptimizedNetwork* network)>;
331
332class IOptimizedNetwork
333{
334public:
335 static void Destroy(IOptimizedNetwork* network);
336
337 virtual Status PrintGraph() = 0;
surmeh01bceff2f2018-03-29 16:29:27 +0100338 virtual Status SerializeToDot(std::ostream& stream) const = 0;
telsoa014fcda012018-03-09 14:13:49 +0000339
telsoa01c577f2c2018-08-31 09:22:23 +0100340
telsoa014fcda012018-03-09 14:13:49 +0000341protected:
342 ~IOptimizedNetwork() {}
343};
344
telsoa01c577f2c2018-08-31 09:22:23 +0100345struct OptimizerOptions
346{
347 OptimizerOptions() : m_ReduceFp32ToFp16(false) {}
348
349 OptimizerOptions(bool reduceFp32ToFp16)
350 : m_ReduceFp32ToFp16(reduceFp32ToFp16)
351 {
352 }
353
354 // Reduce Fp32 data to Fp16 for faster processing
355 bool m_ReduceFp32ToFp16;
356};
telsoa014fcda012018-03-09 14:13:49 +0000357
358/// Create an optimized version of the network
359/// @param network INetwork description of the network to be optimized.
telsoa01c577f2c2018-08-31 09:22:23 +0100360/// @param backendPreferences The choice of the backend ordered by user preferences.
361/// @param deviceSpec DeviceSpec object as queried from the runtime. See IRuntime::GetDeviceSpec()
jimfly016b0b53d2018-10-08 14:43:01 +0100362/// @param errMessages if there are failures or warnings a string describing same will be added to the vector
telsoa01c577f2c2018-08-31 09:22:23 +0100363/// @param options OptimizerOptions object with optimizer configuration options
telsoa014fcda012018-03-09 14:13:49 +0000364/// @return An IOptimizedNetworkPtr interface to the optimized network, throws an exception derived from
365/// armnn::Exception if process fails.
telsoa014fcda012018-03-09 14:13:49 +0000366
telsoa01c577f2c2018-08-31 09:22:23 +0100367IOptimizedNetworkPtr Optimize(const INetwork& network,
David Beckf0b48452018-10-19 15:20:56 +0100368 const std::vector<BackendId>& backendPreferences,
telsoa01c577f2c2018-08-31 09:22:23 +0100369 const IDeviceSpec& deviceSpec,
jimfly016b0b53d2018-10-08 14:43:01 +0100370 const OptimizerOptions& options = OptimizerOptions(),
371 Optional<std::vector<std::string>&> errMessages = EmptyOptional());
telsoa014fcda012018-03-09 14:13:49 +0000372} //namespace armnn