blob: 8bd56d367b7316cc7deacd5728de84b313fd0093 [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>
Francis Murtagha49ff082022-01-17 17:08:01 +00008#include <armnn/INetwork.hpp>
telsoa01c577f2c2018-08-31 09:22:23 +01009#include <armnn/LstmParams.hpp>
James Conroyee18dc82019-07-17 11:27:46 +010010#include <armnn/QuantizedLstmParams.hpp>
telsoa014fcda012018-03-09 14:13:49 +000011#include <armnn/TensorFwd.hpp>
12#include <armnn/Types.hpp>
13
Francis Murtagha49ff082022-01-17 17:08:01 +000014#include <Graph.hpp>
15#include <Layer.hpp>
16#include <OptimizedNetworkImpl.hpp>
17#include <armnn/backends/SubgraphView.hpp>
telsoa014fcda012018-03-09 14:13:49 +000018
19#include <string>
20#include <vector>
Derek Lamberti84da38b2019-06-13 11:40:08 +010021#include <map>
telsoa014fcda012018-03-09 14:13:49 +000022#include <memory>
23
telsoa014fcda012018-03-09 14:13:49 +000024namespace 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
Samuel Yap6b478092022-07-06 15:36:03 +010052 IConnectableLayer* AddBatchMatMulLayer(const BatchMatMulDescriptor& desc,
53 const char* name = nullptr);
54
Keith Davise4f39fd2021-06-10 16:27:30 +010055 IConnectableLayer* AddBatchNormalizationLayer(const BatchNormalizationDescriptor& desc,
56 const ConstTensor& mean,
57 const ConstTensor& variance,
58 const ConstTensor& beta,
59 const ConstTensor& gamma,
60 const char* name = nullptr);
61
Éanna Ó Catháin4e1e1362018-11-12 11:36:34 +000062 IConnectableLayer* AddBatchToSpaceNdLayer(const BatchToSpaceNdDescriptor& batchToSpaceNdDescriptor,
Francis Murtagh3d2b4b22021-02-15 18:23:17 +000063 const char* name = nullptr);
Éanna Ó Catháin4e1e1362018-11-12 11:36:34 +000064
mathad01b392e982021-04-07 12:07:30 +010065 IConnectableLayer* AddCastLayer(const char* name = nullptr);
66
Simon Obute51f67772021-09-03 15:50:13 +010067 IConnectableLayer* AddChannelShuffleLayer(const ChannelShuffleDescriptor& channelShuffleDescriptor,
68 const char* name = nullptr);
69
Aron Virginas-Tar77bfb5e2019-10-16 17:45:38 +010070 IConnectableLayer* AddComparisonLayer(const ComparisonDescriptor& comparisonDescriptor,
Francis Murtagh3d2b4b22021-02-15 18:23:17 +000071 const char* name = nullptr);
Aron Virginas-Tar77bfb5e2019-10-16 17:45:38 +010072
Jim Flynne242f2d2019-05-22 14:24:13 +010073 IConnectableLayer* AddConcatLayer(const ConcatDescriptor& concatDescriptor,
Francis Murtagh3d2b4b22021-02-15 18:23:17 +000074 const char* name = nullptr);
Jim Flynn906f9462019-05-10 13:55:21 +010075
telsoa014fcda012018-03-09 14:13:49 +000076 IConnectableLayer* AddConvolution2dLayer(const Convolution2dDescriptor& convolution2dDescriptor,
Keith Davisb4dd5cc2022-04-07 11:32:00 +010077 const char* name = nullptr);
78
Teresa Charlin71b386d2022-09-28 10:25:40 +010079 ARMNN_DEPRECATED_MSG_REMOVAL_DATE("This AddConvolution2dLayer overload is deprecated", "23.02")
Keith Davisb4dd5cc2022-04-07 11:32:00 +010080 IConnectableLayer* AddConvolution2dLayer(const Convolution2dDescriptor& convolution2dDescriptor,
Aron Virginas-Tarad402702019-02-22 17:03:44 +000081 const ConstTensor& weights,
82 const Optional<ConstTensor>& biases,
Francis Murtagh3d2b4b22021-02-15 18:23:17 +000083 const char* name = nullptr);
Aron Virginas-Tarad402702019-02-22 17:03:44 +000084
Teresa Charlin71b386d2022-09-28 10:25:40 +010085 ARMNN_DEPRECATED_MSG_REMOVAL_DATE("This AddConvolution2dLayer overload is deprecated", "23.02")
Aron Virginas-Tarad402702019-02-22 17:03:44 +000086 IConnectableLayer* AddConvolution2dLayer(const Convolution2dDescriptor& convolution2dDescriptor,
87 const ConstTensor& weights,
Francis Murtagh3d2b4b22021-02-15 18:23:17 +000088 const char* name = nullptr);
Aron Virginas-Tarad402702019-02-22 17:03:44 +000089
Teresa Charlin71b386d2022-09-28 10:25:40 +010090 ARMNN_DEPRECATED_MSG_REMOVAL_DATE("This AddConvolution2dLayer overload is deprecated", "23.02")
Aron Virginas-Tarad402702019-02-22 17:03:44 +000091 IConnectableLayer* AddConvolution2dLayer(const Convolution2dDescriptor& convolution2dDescriptor,
92 const ConstTensor& weights,
93 const ConstTensor& biases,
Francis Murtagh3d2b4b22021-02-15 18:23:17 +000094 const char* name = nullptr);
Aron Virginas-Tarad402702019-02-22 17:03:44 +000095
Matthew Sloyanb63a3112021-09-08 13:05:51 +010096 IConnectableLayer* AddConvolution3dLayer(const Convolution3dDescriptor& convolution3dDescriptor,
Matthew Sloyanb63a3112021-09-08 13:05:51 +010097 const char* name = nullptr);
98
Keith Davise4f39fd2021-06-10 16:27:30 +010099 IConnectableLayer* AddConstantLayer(const ConstTensor& input, const char* name = nullptr);
100
Aron Virginas-Tardd6247f2019-09-19 14:31:17 +0100101 IConnectableLayer* AddDepthToSpaceLayer(const DepthToSpaceDescriptor& depthToSpaceDescriptor,
Francis Murtagh3d2b4b22021-02-15 18:23:17 +0000102 const char* name = nullptr);
Aron Virginas-Tardd6247f2019-09-19 14:31:17 +0100103
Aron Virginas-Tarad402702019-02-22 17:03:44 +0000104 IConnectableLayer* AddDepthwiseConvolution2dLayer(
105 const DepthwiseConvolution2dDescriptor& convolution2dDescriptor,
Cathal Corbett06902652022-04-14 17:55:11 +0100106 const char* name = nullptr);
107
108 ARMNN_DEPRECATED_MSG("This AddDepthwiseConvolution2dLayer overload is deprecated")
109 IConnectableLayer* AddDepthwiseConvolution2dLayer(
110 const DepthwiseConvolution2dDescriptor& convolution2dDescriptor,
Aron Virginas-Tarad402702019-02-22 17:03:44 +0000111 const ConstTensor& weights,
112 const Optional<ConstTensor>& biases,
Francis Murtagh3d2b4b22021-02-15 18:23:17 +0000113 const char* name = nullptr);
Aron Virginas-Tarad402702019-02-22 17:03:44 +0000114
Francis Murtagh3d2b4b22021-02-15 18:23:17 +0000115 IConnectableLayer* AddDequantizeLayer(const char* name = nullptr);
Nattapat Chaimanowonge4294fd2019-03-28 09:56:53 +0000116
Narumol Prangnawarat94dd5d82019-01-23 18:06:26 +0000117 IConnectableLayer* AddDetectionPostProcessLayer(
118 const DetectionPostProcessDescriptor& descriptor,
Narumol Prangnawarat6d302bf2019-02-04 11:46:26 +0000119 const ConstTensor& anchors,
Francis Murtagh3d2b4b22021-02-15 18:23:17 +0000120 const char* name = nullptr);
Narumol Prangnawarat94dd5d82019-01-23 18:06:26 +0000121
Keith Davise4f39fd2021-06-10 16:27:30 +0100122 IConnectableLayer* AddDivisionLayer(const char* name = nullptr);
123
josh minor4a3c6102020-01-06 16:40:46 -0600124 IConnectableLayer* AddElementwiseUnaryLayer(const ElementwiseUnaryDescriptor& elementwiseUnaryDescriptor,
Francis Murtagh3d2b4b22021-02-15 18:23:17 +0000125 const char* name = nullptr);
josh minor4a3c6102020-01-06 16:40:46 -0600126
Keith Davise4f39fd2021-06-10 16:27:30 +0100127 IConnectableLayer* AddMergeLayer(const char* name = nullptr);
128
Ryan OSheaec6c6802020-06-05 17:17:06 +0100129 IConnectableLayer* AddFillLayer(const FillDescriptor& fillDescriptor,
Francis Murtagh3d2b4b22021-02-15 18:23:17 +0000130 const char* name = nullptr);
Ryan OSheaec6c6802020-06-05 17:17:06 +0100131
Keith Davise4f39fd2021-06-10 16:27:30 +0100132 IConnectableLayer* AddFloorLayer(const char* name = nullptr);
133
telsoa014fcda012018-03-09 14:13:49 +0000134 IConnectableLayer* AddFullyConnectedLayer(const FullyConnectedDescriptor& fullyConnectedDescriptor,
Matthew Sloyan81beae32021-07-13 19:46:11 +0100135 const char* name = nullptr);
136
137 IConnectableLayer* AddFullyConnectedLayer(const FullyConnectedDescriptor& fullyConnectedDescriptor,
Sadik Armaganf0a6dec2021-03-25 07:46:55 +0000138 const Optional<ConstTensor>& weights,
139 const Optional<ConstTensor>& biases,
140 const char* name = nullptr);
141
Teresa Charlin52664732020-06-29 16:27:03 +0100142 IConnectableLayer* AddGatherLayer(const GatherDescriptor& gatherDescriptor,
Francis Murtagh3d2b4b22021-02-15 18:23:17 +0000143 const char* name = nullptr);
Teresa Charlin52664732020-06-29 16:27:03 +0100144
Teresa Charlinb2d3ec52022-04-12 22:07:09 +0100145 IConnectableLayer* AddGatherNdLayer(const char* name = nullptr);
146
Kevin Mayce5045a2019-10-02 14:07:47 +0100147 IConnectableLayer* AddInstanceNormalizationLayer(const InstanceNormalizationDescriptor& desc,
Francis Murtagh3d2b4b22021-02-15 18:23:17 +0000148 const char* name = nullptr);
Kevin Mayce5045a2019-10-02 14:07:47 +0100149
Matteo Martincighbcd3c852018-09-28 14:14:12 +0100150 IConnectableLayer* AddL2NormalizationLayer(const L2NormalizationDescriptor& desc,
Francis Murtagh3d2b4b22021-02-15 18:23:17 +0000151 const char* name = nullptr);
telsoa014fcda012018-03-09 14:13:49 +0000152
Aron Virginas-Tarf982dea2019-10-11 14:07:53 +0100153 IConnectableLayer* AddLogSoftmaxLayer(const LogSoftmaxDescriptor& logSoftmaxDescriptor,
Francis Murtagh3d2b4b22021-02-15 18:23:17 +0000154 const char* name = nullptr);
Aron Virginas-Tarf982dea2019-10-11 14:07:53 +0100155
Keith Davise4f39fd2021-06-10 16:27:30 +0100156 IConnectableLayer* AddLogicalBinaryLayer(const LogicalBinaryDescriptor& logicalBinaryDescriptor,
157 const char* name = nullptr);
158
159 IConnectableLayer* AddLstmLayer(const LstmDescriptor& descriptor,
160 const LstmInputParams& params,
161 const char* name = nullptr);
162
163 IConnectableLayer* AddMaximumLayer(const char* name = nullptr);
164
165 IConnectableLayer* AddMeanLayer(const MeanDescriptor& meanDescriptor, const char* name = nullptr);
166
167 IConnectableLayer* AddMinimumLayer(const char* name = nullptr);
168
Keith Davise4f39fd2021-06-10 16:27:30 +0100169 IConnectableLayer* AddMultiplicationLayer(const char* name = nullptr);
170
171 IConnectableLayer* AddNormalizationLayer(const NormalizationDescriptor& normalizationDescriptor,
172 const char* name = nullptr);
173
174 IConnectableLayer* AddOutputLayer(LayerBindingId id, const char* name = nullptr);
175
176 IConnectableLayer* AddPadLayer(const PadDescriptor& padDescriptor, const char* name = nullptr);
177
178 IConnectableLayer* AddPermuteLayer(const PermuteDescriptor& permuteDescriptor,
179 const char* name = nullptr);
180
181 IConnectableLayer* AddPooling2dLayer(const Pooling2dDescriptor& pooling2dDescriptor,
182 const char* name = nullptr);
183
Tamás Nyíri7b885b32021-10-26 14:47:57 +0100184 IConnectableLayer* AddPooling3dLayer(const Pooling3dDescriptor& pooling3dDescriptor,
185 const char* name = nullptr);
186
Cathal Corbett18655b82021-12-13 13:03:22 +0000187 IConnectableLayer* AddPrecompiledLayer(const PreCompiledDescriptor& preCompiledDescriptor,
Cathal Corbett3ea01072022-01-06 10:29:43 +0000188 CompiledBlobPtr compiledBlobPtr,
Cathal Corbettcbfd7182021-12-15 17:12:59 +0000189 const Optional<BackendId>& backend,
190 const char* name = nullptr);
Cathal Corbett18655b82021-12-13 13:03:22 +0000191
Keith Davise4f39fd2021-06-10 16:27:30 +0100192 IConnectableLayer* AddPreluLayer(const char* name = nullptr);
193
194 IConnectableLayer* AddQuantizeLayer(const char* name = nullptr);
195
196 IConnectableLayer* AddQLstmLayer(const QLstmDescriptor& descriptor,
197 const LstmInputParams& params,
198 const char* name = nullptr);
199
200 IConnectableLayer* AddQuantizedLstmLayer(const QuantizedLstmInputParams& params,
201 const char* name = nullptr);
202
203 IConnectableLayer* AddRankLayer(const char* name = nullptr);
204
205 IConnectableLayer* AddReduceLayer(const ReduceDescriptor& reduceDescriptor,
206 const char* name = nullptr);
207
Keith Davise4f39fd2021-06-10 16:27:30 +0100208 IConnectableLayer* AddResizeLayer(const ResizeDescriptor& resizeDescriptor,
209 const char* name = nullptr);
telsoa014fcda012018-03-09 14:13:49 +0000210
211 IConnectableLayer* AddReshapeLayer(const ReshapeDescriptor& reshapeDescriptor,
Francis Murtagh3d2b4b22021-02-15 18:23:17 +0000212 const char* name = nullptr);
telsoa014fcda012018-03-09 14:13:49 +0000213
Keith Davise4f39fd2021-06-10 16:27:30 +0100214 IConnectableLayer* AddShapeLayer(const char* name = nullptr);
215
216 IConnectableLayer* AddSliceLayer(const SliceDescriptor& sliceDescriptor, const char* name = nullptr);
217
218 IConnectableLayer* AddSoftmaxLayer(const SoftmaxDescriptor& softmaxDescriptor,
219 const char* name = nullptr);
220
221 IConnectableLayer* AddSplitterLayer(const ViewsDescriptor& splitterDescriptor,
222 const char* name = nullptr);
223
Nattapat Chaimanowong207ef9a2018-11-02 10:57:25 +0000224 IConnectableLayer* AddSpaceToBatchNdLayer(const SpaceToBatchNdDescriptor& spaceToBatchNdDescriptor,
Francis Murtagh3d2b4b22021-02-15 18:23:17 +0000225 const char* name = nullptr);
Nattapat Chaimanowong207ef9a2018-11-02 10:57:25 +0000226
Aron Virginas-Tar972af152019-06-11 14:14:03 +0100227 IConnectableLayer* AddSpaceToDepthLayer(const SpaceToDepthDescriptor& spaceToDepthDescriptor,
Francis Murtagh3d2b4b22021-02-15 18:23:17 +0000228 const char* name = nullptr);
Aron Virginas-Tar972af152019-06-11 14:14:03 +0100229
Keith Davise4f39fd2021-06-10 16:27:30 +0100230 IConnectableLayer* AddStackLayer(const StackDescriptor& stackDescriptor,
231 const char* name = nullptr);
telsoa014fcda012018-03-09 14:13:49 +0000232
Keith Davise4f39fd2021-06-10 16:27:30 +0100233 IConnectableLayer* AddStandInLayer(const StandInDescriptor& descriptor,
234 const char* name = nullptr);
Derek Lambertia9cca6a2019-03-25 15:41:58 +0000235
Conor Kennedy430b5d82018-11-14 15:28:28 +0000236 IConnectableLayer* AddStridedSliceLayer(const StridedSliceDescriptor& stridedSliceDescriptor,
Francis Murtagh3d2b4b22021-02-15 18:23:17 +0000237 const char* name = nullptr);
Conor Kennedy430b5d82018-11-14 15:28:28 +0000238
Keith Davise4f39fd2021-06-10 16:27:30 +0100239 IConnectableLayer* AddSubtractionLayer(const char* name = nullptr);
Nattapat Chaimanowong1f886302019-04-05 13:37:19 +0100240
Francis Murtagh3d2b4b22021-02-15 18:23:17 +0000241 IConnectableLayer* AddSwitchLayer(const char* name = nullptr);
Sadik Armaganeff363d2019-04-05 15:25:46 +0100242
Aron Virginas-Tar639fb042019-06-20 14:28:19 +0100243 IConnectableLayer* AddTransposeConvolution2dLayer(const TransposeConvolution2dDescriptor& descriptor,
244 const ConstTensor& weights,
245 const Optional<ConstTensor>& biases,
Francis Murtagh3d2b4b22021-02-15 18:23:17 +0000246 const char* name = nullptr);
Aron Virginas-Tar639fb042019-06-20 14:28:19 +0100247
Mike Kellyc9ea45a2020-02-28 18:11:58 +0000248 IConnectableLayer* AddTransposeLayer(const TransposeDescriptor& transposeDescriptor,
Francis Murtagh3d2b4b22021-02-15 18:23:17 +0000249 const char* name = nullptr);
Mike Kellyc9ea45a2020-02-28 18:11:58 +0000250
Narumol Prangnawarat8ed39ae2021-07-15 16:16:25 +0100251 IConnectableLayer* AddUnidirectionalSequenceLstmLayer(const UnidirectionalSequenceLstmDescriptor& descriptor,
252 const LstmInputParams& params,
253 const char* name = nullptr);
254
Cathal Corbetta3f4fba2022-03-21 09:27:08 +0000255 IConnectableLayer* AddConvertFp16ToFp32Layer(const char* name = nullptr);
256
257 IConnectableLayer* AddConvertFp32ToFp16Layer(const char* name = nullptr);
258
Francis Murtagh3d2b4b22021-02-15 18:23:17 +0000259 void ExecuteStrategy(IStrategy& strategy) const;
Finn Williamsb454c5c2021-02-09 15:56:23 +0000260
telsoa014fcda012018-03-09 14:13:49 +0000261private:
telsoa014fcda012018-03-09 14:13:49 +0000262
Finn Williamsf24effa2020-07-03 10:12:03 +0100263 bool GetShapeInferenceMethod();
Mike Kelly80512b02022-05-16 23:10:42 +0100264 bool GetAllowExpandedDims();
Finn Williamsf24effa2020-07-03 10:12:03 +0100265 NetworkOptions m_NetworkOptions;
266
telsoa014fcda012018-03-09 14:13:49 +0000267 std::unique_ptr<Graph> m_Graph;
Keith Davise4f39fd2021-06-10 16:27:30 +0100268 ModelOptions m_ModelOptions;
telsoa014fcda012018-03-09 14:13:49 +0000269};
270
Derek Lamberti84da38b2019-06-13 11:40:08 +0100271struct OptimizationResult
272{
273 bool m_Warning;
274 bool m_Error;
275
Derek Lamberti4a9e24b2020-01-03 16:53:38 +0000276 OptimizationResult(bool warning, bool error)
Keith Davise4f39fd2021-06-10 16:27:30 +0100277 : m_Warning(warning), m_Error(error)
Derek Lamberti84da38b2019-06-13 11:40:08 +0100278 {}
Derek Lamberti4a9e24b2020-01-03 16:53:38 +0000279
280 OptimizationResult()
281 : OptimizationResult(false, false)
282 {}
283
Keith Davise4f39fd2021-06-10 16:27:30 +0100284 bool IsOk() const
285 { return !m_Warning && !m_Error; }
286 bool IsWarningOnly() const
287 { return m_Warning && !m_Error; }
288 bool IsError() const
289 { return m_Error; }
Derek Lamberti4a9e24b2020-01-03 16:53:38 +0000290
Derek Lamberti84da38b2019-06-13 11:40:08 +0100291};
292
293using BackendsMap = std::map<BackendId, std::unique_ptr<class IBackendInternal>>;
294
295BackendsMap CreateSupportedBackends(TensorHandleFactoryRegistry& handleFactoryRegistry,
296 struct BackendSettings& backendSettings);
297
298OptimizationResult SelectTensorHandleStrategy(Graph& optGraph,
299 BackendsMap& backends,
300 TensorHandleFactoryRegistry& registry,
Narumol Prangnawarata2493a02020-08-19 14:39:07 +0100301 bool importEnabled,
Francis Murtagh626bd902022-06-21 13:16:23 +0000302 bool exportEnabled,
Derek Lamberti84da38b2019-06-13 11:40:08 +0100303 Optional<std::vector<std::string>&> errMessages);
304
Francis Murtagh3d2b4b22021-02-15 18:23:17 +0000305OptimizationResult AssignBackends(OptimizedNetworkImpl* optNetObjPtr,
Derek Lamberti4a9e24b2020-01-03 16:53:38 +0000306 BackendSettings& backendSettings,
307 Graph::Iterator& firstLayer,
308 Graph::Iterator& lastLayer,
309 Optional<std::vector<std::string>&> errMessages);
310
Francis Murtagh56ccf682021-12-13 18:48:12 +0000311
312OptimizationResult AssignBackends(OptimizedNetworkImpl* optNetObjPtr,
313 BackendSettings& backendSettings,
314 SubgraphView::IConnectableLayerIterator& firstLayer,
315 SubgraphView::IConnectableLayerIterator& lastLayer,
316 Optional<std::vector<std::string>&> errMessages);
317
telsoa014fcda012018-03-09 14:13:49 +0000318} // namespace armnn