blob: eb1d39d2f61af0e343a9be3573a56434dd5ca3e8 [file] [log] [blame]
Laurent Carlier749294b2020-06-01 09:03:17 +01001//
Teresa Charlin52664732020-06-29 16:27:03 +01002// Copyright © 2017 Arm Ltd and Contributors. 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
7#include <armnn/DescriptorsFwd.hpp>
telsoa01c577f2c2018-08-31 09:22:23 +01008#include <armnn/LstmParams.hpp>
James Conroyee18dc82019-07-17 11:27:46 +01009#include <armnn/QuantizedLstmParams.hpp>
telsoa014fcda012018-03-09 14:13:49 +000010#include <armnn/TensorFwd.hpp>
11#include <armnn/Types.hpp>
12
13#include <armnn/INetwork.hpp>
14
15#include <string>
16#include <vector>
Derek Lamberti84da38b2019-06-13 11:40:08 +010017#include <map>
telsoa014fcda012018-03-09 14:13:49 +000018#include <memory>
19
Derek Lamberti4a9e24b2020-01-03 16:53:38 +000020#include "Graph.hpp"
telsoa014fcda012018-03-09 14:13:49 +000021#include "Layer.hpp"
Francis Murtagh3d2b4b22021-02-15 18:23:17 +000022#include "OptimizedNetworkImpl.hpp"
telsoa014fcda012018-03-09 14:13:49 +000023
24namespace armnn
25{
26class Graph;
27
Keith Davise4f39fd2021-06-10 16:27:30 +010028using NetworkImplPtr = std::unique_ptr<NetworkImpl, void (*)(NetworkImpl* network)>;
Francis Murtagh3d2b4b22021-02-15 18:23:17 +000029
telsoa01c577f2c2018-08-31 09:22:23 +010030/// Private implementation of INetwork.
Francis Murtagh3d2b4b22021-02-15 18:23:17 +000031class NetworkImpl
telsoa014fcda012018-03-09 14:13:49 +000032{
33public:
Francis Murtagh3d2b4b22021-02-15 18:23:17 +000034 NetworkImpl(NetworkOptions networkOptions = {});
35 ~NetworkImpl();
telsoa014fcda012018-03-09 14:13:49 +000036
Keith Davise4f39fd2021-06-10 16:27:30 +010037 const Graph& GetGraph() const
38 { return *m_Graph; }
telsoa014fcda012018-03-09 14:13:49 +000039
Francis Murtagh3d2b4b22021-02-15 18:23:17 +000040 Status PrintGraph();
telsoa014fcda012018-03-09 14:13:49 +000041
Keith Davise4f39fd2021-06-10 16:27:30 +010042 IConnectableLayer* AddInputLayer(LayerBindingId id, const char* name = nullptr);
43
Keith Davise4f39fd2021-06-10 16:27:30 +010044 IConnectableLayer* AddActivationLayer(const ActivationDescriptor& activationDescriptor,
45 const char* name = nullptr);
46
47 IConnectableLayer* AddAdditionLayer(const char* name = nullptr);
telsoa014fcda012018-03-09 14:13:49 +000048
Nikhil Rajee391d52019-09-05 17:50:44 +010049 IConnectableLayer* AddArgMinMaxLayer(const ArgMinMaxDescriptor& desc,
Francis Murtagh3d2b4b22021-02-15 18:23:17 +000050 const char* name = nullptr);
Nikhil Rajee391d52019-09-05 17:50:44 +010051
Keith Davise4f39fd2021-06-10 16:27:30 +010052 IConnectableLayer* AddBatchNormalizationLayer(const BatchNormalizationDescriptor& desc,
53 const ConstTensor& mean,
54 const ConstTensor& variance,
55 const ConstTensor& beta,
56 const ConstTensor& gamma,
57 const char* name = nullptr);
58
Éanna Ó Catháin4e1e1362018-11-12 11:36:34 +000059 IConnectableLayer* AddBatchToSpaceNdLayer(const BatchToSpaceNdDescriptor& batchToSpaceNdDescriptor,
Francis Murtagh3d2b4b22021-02-15 18:23:17 +000060 const char* name = nullptr);
Éanna Ó Catháin4e1e1362018-11-12 11:36:34 +000061
mathad01b392e982021-04-07 12:07:30 +010062 IConnectableLayer* AddCastLayer(const char* name = nullptr);
63
Simon Obute51f67772021-09-03 15:50:13 +010064 IConnectableLayer* AddChannelShuffleLayer(const ChannelShuffleDescriptor& channelShuffleDescriptor,
65 const char* name = nullptr);
66
Aron Virginas-Tar77bfb5e2019-10-16 17:45:38 +010067 IConnectableLayer* AddComparisonLayer(const ComparisonDescriptor& comparisonDescriptor,
Francis Murtagh3d2b4b22021-02-15 18:23:17 +000068 const char* name = nullptr);
Aron Virginas-Tar77bfb5e2019-10-16 17:45:38 +010069
Jim Flynne242f2d2019-05-22 14:24:13 +010070 IConnectableLayer* AddConcatLayer(const ConcatDescriptor& concatDescriptor,
Francis Murtagh3d2b4b22021-02-15 18:23:17 +000071 const char* name = nullptr);
Jim Flynn906f9462019-05-10 13:55:21 +010072
telsoa014fcda012018-03-09 14:13:49 +000073 IConnectableLayer* AddConvolution2dLayer(const Convolution2dDescriptor& convolution2dDescriptor,
Aron Virginas-Tarad402702019-02-22 17:03:44 +000074 const ConstTensor& weights,
75 const Optional<ConstTensor>& biases,
Francis Murtagh3d2b4b22021-02-15 18:23:17 +000076 const char* name = nullptr);
Aron Virginas-Tarad402702019-02-22 17:03:44 +000077
Jan Eilers1b2654f2021-09-24 15:45:46 +010078 ARMNN_DEPRECATED_MSG_REMOVAL_DATE("This AddConvolution2dLayer overload is deprecated", "22.08")
Aron Virginas-Tarad402702019-02-22 17:03:44 +000079 IConnectableLayer* AddConvolution2dLayer(const Convolution2dDescriptor& convolution2dDescriptor,
80 const ConstTensor& weights,
Francis Murtagh3d2b4b22021-02-15 18:23:17 +000081 const char* name = nullptr);
Aron Virginas-Tarad402702019-02-22 17:03:44 +000082
Jan Eilers1b2654f2021-09-24 15:45:46 +010083 ARMNN_DEPRECATED_MSG_REMOVAL_DATE("This AddConvolution2dLayer overload is deprecated", "22.08")
Aron Virginas-Tarad402702019-02-22 17:03:44 +000084 IConnectableLayer* AddConvolution2dLayer(const Convolution2dDescriptor& convolution2dDescriptor,
85 const ConstTensor& weights,
86 const ConstTensor& biases,
Francis Murtagh3d2b4b22021-02-15 18:23:17 +000087 const char* name = nullptr);
Aron Virginas-Tarad402702019-02-22 17:03:44 +000088
Matthew Sloyanb63a3112021-09-08 13:05:51 +010089 IConnectableLayer* AddConvolution3dLayer(const Convolution3dDescriptor& convolution3dDescriptor,
90 const ConstTensor& weights,
91 const Optional<ConstTensor>& biases,
92 const char* name = nullptr);
93
Keith Davise4f39fd2021-06-10 16:27:30 +010094 IConnectableLayer* AddConstantLayer(const ConstTensor& input, const char* name = nullptr);
95
Aron Virginas-Tardd6247f2019-09-19 14:31:17 +010096 IConnectableLayer* AddDepthToSpaceLayer(const DepthToSpaceDescriptor& depthToSpaceDescriptor,
Francis Murtagh3d2b4b22021-02-15 18:23:17 +000097 const char* name = nullptr);
Aron Virginas-Tardd6247f2019-09-19 14:31:17 +010098
Aron Virginas-Tarad402702019-02-22 17:03:44 +000099 IConnectableLayer* AddDepthwiseConvolution2dLayer(
100 const DepthwiseConvolution2dDescriptor& convolution2dDescriptor,
101 const ConstTensor& weights,
102 const Optional<ConstTensor>& biases,
Francis Murtagh3d2b4b22021-02-15 18:23:17 +0000103 const char* name = nullptr);
Aron Virginas-Tarad402702019-02-22 17:03:44 +0000104
Francis Murtagh3d2b4b22021-02-15 18:23:17 +0000105 IConnectableLayer* AddDequantizeLayer(const char* name = nullptr);
Nattapat Chaimanowonge4294fd2019-03-28 09:56:53 +0000106
Narumol Prangnawarat94dd5d82019-01-23 18:06:26 +0000107 IConnectableLayer* AddDetectionPostProcessLayer(
108 const DetectionPostProcessDescriptor& descriptor,
Narumol Prangnawarat6d302bf2019-02-04 11:46:26 +0000109 const ConstTensor& anchors,
Francis Murtagh3d2b4b22021-02-15 18:23:17 +0000110 const char* name = nullptr);
Narumol Prangnawarat94dd5d82019-01-23 18:06:26 +0000111
Keith Davise4f39fd2021-06-10 16:27:30 +0100112 IConnectableLayer* AddDivisionLayer(const char* name = nullptr);
113
josh minor4a3c6102020-01-06 16:40:46 -0600114 IConnectableLayer* AddElementwiseUnaryLayer(const ElementwiseUnaryDescriptor& elementwiseUnaryDescriptor,
Francis Murtagh3d2b4b22021-02-15 18:23:17 +0000115 const char* name = nullptr);
josh minor4a3c6102020-01-06 16:40:46 -0600116
Keith Davise4f39fd2021-06-10 16:27:30 +0100117 IConnectableLayer* AddMergeLayer(const char* name = nullptr);
118
Ryan OSheaec6c6802020-06-05 17:17:06 +0100119 IConnectableLayer* AddFillLayer(const FillDescriptor& fillDescriptor,
Francis Murtagh3d2b4b22021-02-15 18:23:17 +0000120 const char* name = nullptr);
Ryan OSheaec6c6802020-06-05 17:17:06 +0100121
Keith Davise4f39fd2021-06-10 16:27:30 +0100122 IConnectableLayer* AddFloorLayer(const char* name = nullptr);
123
telsoa014fcda012018-03-09 14:13:49 +0000124 IConnectableLayer* AddFullyConnectedLayer(const FullyConnectedDescriptor& fullyConnectedDescriptor,
Matthew Sloyan81beae32021-07-13 19:46:11 +0100125 const char* name = nullptr);
126
127 IConnectableLayer* AddFullyConnectedLayer(const FullyConnectedDescriptor& fullyConnectedDescriptor,
Sadik Armaganf0a6dec2021-03-25 07:46:55 +0000128 const Optional<ConstTensor>& weights,
129 const Optional<ConstTensor>& biases,
130 const char* name = nullptr);
131
Teresa Charlin52664732020-06-29 16:27:03 +0100132 IConnectableLayer* AddGatherLayer(const GatherDescriptor& gatherDescriptor,
Francis Murtagh3d2b4b22021-02-15 18:23:17 +0000133 const char* name = nullptr);
Teresa Charlin52664732020-06-29 16:27:03 +0100134
Kevin Mayce5045a2019-10-02 14:07:47 +0100135 IConnectableLayer* AddInstanceNormalizationLayer(const InstanceNormalizationDescriptor& desc,
Francis Murtagh3d2b4b22021-02-15 18:23:17 +0000136 const char* name = nullptr);
Kevin Mayce5045a2019-10-02 14:07:47 +0100137
Matteo Martincighbcd3c852018-09-28 14:14:12 +0100138 IConnectableLayer* AddL2NormalizationLayer(const L2NormalizationDescriptor& desc,
Francis Murtagh3d2b4b22021-02-15 18:23:17 +0000139 const char* name = nullptr);
telsoa014fcda012018-03-09 14:13:49 +0000140
Aron Virginas-Tarf982dea2019-10-11 14:07:53 +0100141 IConnectableLayer* AddLogSoftmaxLayer(const LogSoftmaxDescriptor& logSoftmaxDescriptor,
Francis Murtagh3d2b4b22021-02-15 18:23:17 +0000142 const char* name = nullptr);
Aron Virginas-Tarf982dea2019-10-11 14:07:53 +0100143
Keith Davise4f39fd2021-06-10 16:27:30 +0100144 IConnectableLayer* AddLogicalBinaryLayer(const LogicalBinaryDescriptor& logicalBinaryDescriptor,
145 const char* name = nullptr);
146
147 IConnectableLayer* AddLstmLayer(const LstmDescriptor& descriptor,
148 const LstmInputParams& params,
149 const char* name = nullptr);
150
151 IConnectableLayer* AddMaximumLayer(const char* name = nullptr);
152
153 IConnectableLayer* AddMeanLayer(const MeanDescriptor& meanDescriptor, const char* name = nullptr);
154
155 IConnectableLayer* AddMinimumLayer(const char* name = nullptr);
156
Keith Davise4f39fd2021-06-10 16:27:30 +0100157 IConnectableLayer* AddMultiplicationLayer(const char* name = nullptr);
158
159 IConnectableLayer* AddNormalizationLayer(const NormalizationDescriptor& normalizationDescriptor,
160 const char* name = nullptr);
161
162 IConnectableLayer* AddOutputLayer(LayerBindingId id, const char* name = nullptr);
163
164 IConnectableLayer* AddPadLayer(const PadDescriptor& padDescriptor, const char* name = nullptr);
165
166 IConnectableLayer* AddPermuteLayer(const PermuteDescriptor& permuteDescriptor,
167 const char* name = nullptr);
168
169 IConnectableLayer* AddPooling2dLayer(const Pooling2dDescriptor& pooling2dDescriptor,
170 const char* name = nullptr);
171
172 IConnectableLayer* AddPreluLayer(const char* name = nullptr);
173
174 IConnectableLayer* AddQuantizeLayer(const char* name = nullptr);
175
176 IConnectableLayer* AddQLstmLayer(const QLstmDescriptor& descriptor,
177 const LstmInputParams& params,
178 const char* name = nullptr);
179
180 IConnectableLayer* AddQuantizedLstmLayer(const QuantizedLstmInputParams& params,
181 const char* name = nullptr);
182
183 IConnectableLayer* AddRankLayer(const char* name = nullptr);
184
185 IConnectableLayer* AddReduceLayer(const ReduceDescriptor& reduceDescriptor,
186 const char* name = nullptr);
187
Keith Davise4f39fd2021-06-10 16:27:30 +0100188 IConnectableLayer* AddResizeLayer(const ResizeDescriptor& resizeDescriptor,
189 const char* name = nullptr);
telsoa014fcda012018-03-09 14:13:49 +0000190
191 IConnectableLayer* AddReshapeLayer(const ReshapeDescriptor& reshapeDescriptor,
Francis Murtagh3d2b4b22021-02-15 18:23:17 +0000192 const char* name = nullptr);
telsoa014fcda012018-03-09 14:13:49 +0000193
Keith Davise4f39fd2021-06-10 16:27:30 +0100194 IConnectableLayer* AddShapeLayer(const char* name = nullptr);
195
196 IConnectableLayer* AddSliceLayer(const SliceDescriptor& sliceDescriptor, const char* name = nullptr);
197
198 IConnectableLayer* AddSoftmaxLayer(const SoftmaxDescriptor& softmaxDescriptor,
199 const char* name = nullptr);
200
201 IConnectableLayer* AddSplitterLayer(const ViewsDescriptor& splitterDescriptor,
202 const char* name = nullptr);
203
Nattapat Chaimanowong207ef9a2018-11-02 10:57:25 +0000204 IConnectableLayer* AddSpaceToBatchNdLayer(const SpaceToBatchNdDescriptor& spaceToBatchNdDescriptor,
Francis Murtagh3d2b4b22021-02-15 18:23:17 +0000205 const char* name = nullptr);
Nattapat Chaimanowong207ef9a2018-11-02 10:57:25 +0000206
Aron Virginas-Tar972af152019-06-11 14:14:03 +0100207 IConnectableLayer* AddSpaceToDepthLayer(const SpaceToDepthDescriptor& spaceToDepthDescriptor,
Francis Murtagh3d2b4b22021-02-15 18:23:17 +0000208 const char* name = nullptr);
Aron Virginas-Tar972af152019-06-11 14:14:03 +0100209
Keith Davise4f39fd2021-06-10 16:27:30 +0100210 IConnectableLayer* AddStackLayer(const StackDescriptor& stackDescriptor,
211 const char* name = nullptr);
telsoa014fcda012018-03-09 14:13:49 +0000212
Keith Davise4f39fd2021-06-10 16:27:30 +0100213 IConnectableLayer* AddStandInLayer(const StandInDescriptor& descriptor,
214 const char* name = nullptr);
Derek Lambertia9cca6a2019-03-25 15:41:58 +0000215
Conor Kennedy430b5d82018-11-14 15:28:28 +0000216 IConnectableLayer* AddStridedSliceLayer(const StridedSliceDescriptor& stridedSliceDescriptor,
Francis Murtagh3d2b4b22021-02-15 18:23:17 +0000217 const char* name = nullptr);
Conor Kennedy430b5d82018-11-14 15:28:28 +0000218
Keith Davise4f39fd2021-06-10 16:27:30 +0100219 IConnectableLayer* AddSubtractionLayer(const char* name = nullptr);
Nattapat Chaimanowong1f886302019-04-05 13:37:19 +0100220
Francis Murtagh3d2b4b22021-02-15 18:23:17 +0000221 IConnectableLayer* AddSwitchLayer(const char* name = nullptr);
Sadik Armaganeff363d2019-04-05 15:25:46 +0100222
Aron Virginas-Tar639fb042019-06-20 14:28:19 +0100223 IConnectableLayer* AddTransposeConvolution2dLayer(const TransposeConvolution2dDescriptor& descriptor,
224 const ConstTensor& weights,
225 const Optional<ConstTensor>& biases,
Francis Murtagh3d2b4b22021-02-15 18:23:17 +0000226 const char* name = nullptr);
Aron Virginas-Tar639fb042019-06-20 14:28:19 +0100227
Mike Kellyc9ea45a2020-02-28 18:11:58 +0000228 IConnectableLayer* AddTransposeLayer(const TransposeDescriptor& transposeDescriptor,
Francis Murtagh3d2b4b22021-02-15 18:23:17 +0000229 const char* name = nullptr);
Mike Kellyc9ea45a2020-02-28 18:11:58 +0000230
Narumol Prangnawarat8ed39ae2021-07-15 16:16:25 +0100231 IConnectableLayer* AddUnidirectionalSequenceLstmLayer(const UnidirectionalSequenceLstmDescriptor& descriptor,
232 const LstmInputParams& params,
233 const char* name = nullptr);
234
Jan Eilers1b2654f2021-09-24 15:45:46 +0100235 ARMNN_NO_DEPRECATE_WARN_BEGIN
Francis Murtagh3d2b4b22021-02-15 18:23:17 +0000236 void Accept(ILayerVisitor& visitor) const;
Jan Eilers1b2654f2021-09-24 15:45:46 +0100237 ARMNN_NO_DEPRECATE_WARN_END
Mike Kelly8c1701a2019-02-11 17:01:27 +0000238
Francis Murtagh3d2b4b22021-02-15 18:23:17 +0000239 void ExecuteStrategy(IStrategy& strategy) const;
Finn Williamsb454c5c2021-02-09 15:56:23 +0000240
telsoa014fcda012018-03-09 14:13:49 +0000241private:
telsoa014fcda012018-03-09 14:13:49 +0000242 IConnectableLayer* AddConvolution2dLayerImpl(const Convolution2dDescriptor& convolution2dDescriptor,
Aron Virginas-Tarad402702019-02-22 17:03:44 +0000243 const ConstTensor& weights,
244 const Optional<ConstTensor>& biases,
245 const char* name);
telsoa014fcda012018-03-09 14:13:49 +0000246
Keith Davise4f39fd2021-06-10 16:27:30 +0100247 IConnectableLayer* AddDepthwiseConvolution2dLayerImpl(const DepthwiseConvolution2dDescriptor& conv2dDescriptor,
248 const ConstTensor& weights,
249 const Optional<ConstTensor>& biases,
250 const char* name);
251
Finn Williamsf24effa2020-07-03 10:12:03 +0100252 bool GetShapeInferenceMethod();
253 NetworkOptions m_NetworkOptions;
254
telsoa014fcda012018-03-09 14:13:49 +0000255 std::unique_ptr<Graph> m_Graph;
Keith Davise4f39fd2021-06-10 16:27:30 +0100256 ModelOptions m_ModelOptions;
telsoa014fcda012018-03-09 14:13:49 +0000257};
258
Derek Lamberti84da38b2019-06-13 11:40:08 +0100259struct OptimizationResult
260{
261 bool m_Warning;
262 bool m_Error;
263
Derek Lamberti4a9e24b2020-01-03 16:53:38 +0000264 OptimizationResult(bool warning, bool error)
Keith Davise4f39fd2021-06-10 16:27:30 +0100265 : m_Warning(warning), m_Error(error)
Derek Lamberti84da38b2019-06-13 11:40:08 +0100266 {}
Derek Lamberti4a9e24b2020-01-03 16:53:38 +0000267
268 OptimizationResult()
269 : OptimizationResult(false, false)
270 {}
271
Keith Davise4f39fd2021-06-10 16:27:30 +0100272 bool IsOk() const
273 { return !m_Warning && !m_Error; }
274 bool IsWarningOnly() const
275 { return m_Warning && !m_Error; }
276 bool IsError() const
277 { return m_Error; }
Derek Lamberti4a9e24b2020-01-03 16:53:38 +0000278
Derek Lamberti84da38b2019-06-13 11:40:08 +0100279};
280
281using BackendsMap = std::map<BackendId, std::unique_ptr<class IBackendInternal>>;
282
283BackendsMap CreateSupportedBackends(TensorHandleFactoryRegistry& handleFactoryRegistry,
284 struct BackendSettings& backendSettings);
285
286OptimizationResult SelectTensorHandleStrategy(Graph& optGraph,
287 BackendsMap& backends,
288 TensorHandleFactoryRegistry& registry,
Narumol Prangnawarata2493a02020-08-19 14:39:07 +0100289 bool importEnabled,
Derek Lamberti84da38b2019-06-13 11:40:08 +0100290 Optional<std::vector<std::string>&> errMessages);
291
Francis Murtagh3d2b4b22021-02-15 18:23:17 +0000292OptimizationResult AssignBackends(OptimizedNetworkImpl* optNetObjPtr,
Derek Lamberti4a9e24b2020-01-03 16:53:38 +0000293 BackendSettings& backendSettings,
294 Graph::Iterator& firstLayer,
295 Graph::Iterator& lastLayer,
296 Optional<std::vector<std::string>&> errMessages);
297
telsoa014fcda012018-03-09 14:13:49 +0000298} // namespace armnn