blob: 475367ece52eac5991bded6b5624dc658cc711a1 [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
Finn Williamsf24effa2020-07-03 10:12:03 +01007#include <armnn/BackendOptions.hpp>
Jim Flynn906f9462019-05-10 13:55:21 +01008#include <armnn/Deprecated.hpp>
David Beckf0b48452018-10-19 15:20:56 +01009#include <armnn/DescriptorsFwd.hpp>
jimfly01e9e7bfd2019-01-24 22:29:33 +000010#include <armnn/ILayerVisitor.hpp>
Finn Williamsb454c5c2021-02-09 15:56:23 +000011#include <armnn/IStrategy.hpp>
Matthew Bentham313e1c82019-03-25 17:37:47 +000012#include <armnn/NetworkFwd.hpp>
13#include <armnn/Optional.hpp>
14#include <armnn/TensorFwd.hpp>
Jan Eilers6a71bb52021-10-26 17:41:18 +010015#include <armnn/Logging.hpp>
Nikhil Raj2e241752022-02-01 16:42:15 +000016#include <armnn/backends/TensorHandle.hpp>
telsoa014fcda012018-03-09 14:13:49 +000017
18#include <memory>
telsoa01c577f2c2018-08-31 09:22:23 +010019#include <vector>
telsoa014fcda012018-03-09 14:13:49 +000020
21namespace armnn
22{
telsoa014fcda012018-03-09 14:13:49 +000023/// @brief An input connection slot for a layer.
24/// The input slot can be connected to an output slot of the preceding layer in the graph.
25/// Only one connection to the input slot is allowed.
26class IInputSlot
27{
28public:
29 virtual const IOutputSlot* GetConnection() const = 0;
30 virtual IOutputSlot* GetConnection() = 0;
Francis Murtagh9d74ba62022-01-19 16:31:58 +000031 virtual const IConnectableLayer& GetOwningIConnectableLayer() const = 0;
telsoa014fcda012018-03-09 14:13:49 +000032
33protected:
telsoa01c577f2c2018-08-31 09:22:23 +010034 /// Not user deletable.
35 ~IInputSlot() {}
telsoa014fcda012018-03-09 14:13:49 +000036};
37
38/// @brief An output connection slot for a layer.
39/// The output slot may be connected to 1 or more input slots of subsequent layers in the graph.
40class IOutputSlot
41{
42public:
43 virtual unsigned int GetNumConnections() const = 0;
44 virtual const IInputSlot* GetConnection(unsigned int index) const = 0;
Keith Davisb4dd5cc2022-04-07 11:32:00 +010045 virtual IInputSlot* GetConnection(unsigned int outputindex) = 0;
telsoa014fcda012018-03-09 14:13:49 +000046
47 virtual void SetTensorInfo(const TensorInfo& tensorInfo) = 0;
48 virtual const TensorInfo& GetTensorInfo() const = 0;
49 virtual bool IsTensorInfoSet() const = 0;
50
51 virtual int Connect(IInputSlot& destination) = 0;
52 virtual void Disconnect(IInputSlot& slot) = 0;
53
Mike Kelly8c1701a2019-02-11 17:01:27 +000054 virtual unsigned int CalculateIndexOnOwner() const = 0;
55
56 virtual LayerGuid GetOwningLayerGuid() const = 0;
57
Francis Murtagh56ccf682021-12-13 18:48:12 +000058 virtual const IConnectableLayer& GetOwningIConnectableLayer() const = 0;
59
telsoa014fcda012018-03-09 14:13:49 +000060protected:
telsoa01c577f2c2018-08-31 09:22:23 +010061 /// Not user deletable.
62 ~IOutputSlot() {}
telsoa014fcda012018-03-09 14:13:49 +000063};
64
65/// @brief Interface for a layer that is connectable to other layers via InputSlots and OutputSlots.
66class IConnectableLayer
67{
68public:
Derek Lamberti4a9e24b2020-01-03 16:53:38 +000069 /// Returns the name of the layer
telsoa014fcda012018-03-09 14:13:49 +000070 virtual const char* GetName() const = 0;
71
Derek Lamberti4a9e24b2020-01-03 16:53:38 +000072 /// Returns the number of connectable input slots
telsoa014fcda012018-03-09 14:13:49 +000073 virtual unsigned int GetNumInputSlots() const = 0;
Derek Lamberti4a9e24b2020-01-03 16:53:38 +000074
75 /// Returns the number of connectable output slots
telsoa014fcda012018-03-09 14:13:49 +000076 virtual unsigned int GetNumOutputSlots() const = 0;
77
Derek Lamberti4a9e24b2020-01-03 16:53:38 +000078 /// Get a const input slot handle by slot index
telsoa014fcda012018-03-09 14:13:49 +000079 virtual const IInputSlot& GetInputSlot(unsigned int index) const = 0;
Derek Lamberti4a9e24b2020-01-03 16:53:38 +000080
81 /// Get the input slot handle by slot index
telsoa014fcda012018-03-09 14:13:49 +000082 virtual IInputSlot& GetInputSlot(unsigned int index) = 0;
83
Derek Lamberti4a9e24b2020-01-03 16:53:38 +000084 /// Get the const output slot handle by slot index
telsoa014fcda012018-03-09 14:13:49 +000085 virtual const IOutputSlot& GetOutputSlot(unsigned int index) const = 0;
Derek Lamberti4a9e24b2020-01-03 16:53:38 +000086
87 /// Get the output slot handle by slot index
telsoa014fcda012018-03-09 14:13:49 +000088 virtual IOutputSlot& GetOutputSlot(unsigned int index) = 0;
89
Derek Lamberti4a9e24b2020-01-03 16:53:38 +000090 /// Infer the shape of the output(s) based on the provided input shape(s)
telsoa01c577f2c2018-08-31 09:22:23 +010091 virtual std::vector<TensorShape> InferOutputShapes(const std::vector<TensorShape>& inputShapes) const = 0;
92
Derek Lamberti4a9e24b2020-01-03 16:53:38 +000093 /// Returns the unique id of the layer
surmeh01bceff2f2018-03-29 16:29:27 +010094 virtual LayerGuid GetGuid() const = 0;
jimfly01e9e7bfd2019-01-24 22:29:33 +000095
Jan Eilers1b2654f2021-09-24 15:45:46 +010096 // The Accept function needs to be wrapped in a no warn macro to avoid deprecation warnings from
97 // the deprecated ILayerVisitor which is used in the function.
98 ARMNN_NO_DEPRECATE_WARN_BEGIN
Derek Lamberti4a9e24b2020-01-03 16:53:38 +000099 /// Apply a visitor to this layer
Jan Eilers1b2654f2021-09-24 15:45:46 +0100100 ARMNN_DEPRECATED_MSG_REMOVAL_DATE("Accept is deprecated. The ILayerVisitor that works in conjunction with this "
101 "Accept function is deprecated. Use IStrategy in combination with "
102 "ExecuteStrategy instead, which is an ABI/API stable version of the "
103 "visitor pattern.",
104 "22.05")
jimfly01e9e7bfd2019-01-24 22:29:33 +0000105 virtual void Accept(ILayerVisitor& visitor) const = 0;
Jan Eilers1b2654f2021-09-24 15:45:46 +0100106 ARMNN_NO_DEPRECATE_WARN_END
Derek Lamberti4a9e24b2020-01-03 16:53:38 +0000107
Finn Williamsb454c5c2021-02-09 15:56:23 +0000108 /// Apply a visitor to this layer
109 virtual void ExecuteStrategy(IStrategy& strategy) const = 0;
110
Derek Lamberti4a9e24b2020-01-03 16:53:38 +0000111 /// Provide a hint for the optimizer as to which backend to prefer for this layer
112 virtual void BackendSelectionHint(Optional<BackendId> backend) = 0;
Finn Williamsb454c5c2021-02-09 15:56:23 +0000113
114 /// Returns the armnn::LayerType of this layer
115 virtual LayerType GetType() const = 0;
116
Jim Flynne4665962022-01-31 16:08:53 +0000117 /// If the layer has a descriptor return it.
118 /// The base descriptor can then be cast to the correct descriptor class.
119 /// If the layer has no associated descriptor a struct of type NullDescriptor will be returned.
120 /// Note: NullDescriptors can be detected because they return true when
121 /// the BaseDescriptor IsNull function is invoked.
122 virtual const BaseDescriptor& GetParameters() const = 0;
123
Nikhil Raj2e241752022-02-01 16:42:15 +0000124 using ConstantTensors = std::vector<std::reference_wrapper<std::shared_ptr<ConstTensorHandle>>>;
125
126 // Returns ConstantTensors of this Layer if it has any, otherwise returns empty vector.
127 virtual ConstantTensors GetConstantTensorsByRef() = 0;
128
telsoa014fcda012018-03-09 14:13:49 +0000129protected:
telsoa01c577f2c2018-08-31 09:22:23 +0100130 /// Objects are not deletable via the handle
131 ~IConnectableLayer() {}
telsoa014fcda012018-03-09 14:13:49 +0000132};
133
telsoa014fcda012018-03-09 14:13:49 +0000134
Jan Eilersb1c62f12021-10-26 14:56:47 +0100135/// ArmNN performs an optimization on each model/network before it gets loaded for execution. OptimizerOptions provides
136/// a set of features that allows the user to customize this optimization on a per model basis.
telsoa01c577f2c2018-08-31 09:22:23 +0100137struct OptimizerOptions
138{
Matteo Martincigh49124022019-01-11 13:25:59 +0000139 OptimizerOptions()
140 : m_ReduceFp32ToFp16(false)
141 , m_Debug(false)
Narumol Prangnawaratbc7ffb52020-03-20 15:01:01 +0000142 , m_ReduceFp32ToBf16(false)
Teresa Charlincdc01492020-06-09 18:00:20 +0100143 , m_shapeInferenceMethod(armnn::ShapeInferenceMethod::ValidateOnly)
Narumol Prangnawarata2493a02020-08-19 14:39:07 +0100144 , m_ImportEnabled(false)
Sadik Armagan045f6be2020-09-10 13:37:32 +0100145 , m_ModelOptions()
Derek Lambertif1e0ad32021-10-13 18:02:25 +0100146 , m_ProfilingEnabled(false)
Colm Donelan03bf98a2022-05-30 15:20:36 +0100147 , m_ExportEnabled(false)
keidav01738c2e62018-12-11 16:14:20 +0000148 {}
telsoa01c577f2c2018-08-31 09:22:23 +0100149
Sadik Armagan045f6be2020-09-10 13:37:32 +0100150 OptimizerOptions(bool reduceFp32ToFp16, bool debug, bool reduceFp32ToBf16, bool importEnabled,
Colm Donelan03bf98a2022-05-30 15:20:36 +0100151 ModelOptions modelOptions = {}, bool exportEnabled = false)
Narumol Prangnawaratea063df2020-08-21 10:03:49 +0100152 : m_ReduceFp32ToFp16(reduceFp32ToFp16)
153 , m_Debug(debug)
154 , m_ReduceFp32ToBf16(reduceFp32ToBf16)
155 , m_shapeInferenceMethod(armnn::ShapeInferenceMethod::ValidateOnly)
156 , m_ImportEnabled(importEnabled)
Sadik Armagan045f6be2020-09-10 13:37:32 +0100157 , m_ModelOptions(modelOptions)
Derek Lambertif1e0ad32021-10-13 18:02:25 +0100158 , m_ProfilingEnabled(false)
Colm Donelan03bf98a2022-05-30 15:20:36 +0100159 , m_ExportEnabled(exportEnabled)
Narumol Prangnawaratea063df2020-08-21 10:03:49 +0100160 {
161 if (m_ReduceFp32ToFp16 && m_ReduceFp32ToBf16)
162 {
163 throw InvalidArgumentException("BFloat16 and Float16 optimization cannot be enabled at the same time.");
164 }
165 }
166
Teresa Charlincdc01492020-06-09 18:00:20 +0100167 OptimizerOptions(bool reduceFp32ToFp16, bool debug, bool reduceFp32ToBf16 = false,
Narumol Prangnawarata2493a02020-08-19 14:39:07 +0100168 ShapeInferenceMethod shapeInferenceMethod = armnn::ShapeInferenceMethod::ValidateOnly,
Colm Donelan03bf98a2022-05-30 15:20:36 +0100169 bool importEnabled = false, ModelOptions modelOptions = {}, bool exportEnabled = false)
telsoa01c577f2c2018-08-31 09:22:23 +0100170 : m_ReduceFp32ToFp16(reduceFp32ToFp16)
keidav01738c2e62018-12-11 16:14:20 +0000171 , m_Debug(debug)
Narumol Prangnawaratbc7ffb52020-03-20 15:01:01 +0000172 , m_ReduceFp32ToBf16(reduceFp32ToBf16)
Teresa Charlincdc01492020-06-09 18:00:20 +0100173 , m_shapeInferenceMethod(shapeInferenceMethod)
Narumol Prangnawarata2493a02020-08-19 14:39:07 +0100174 , m_ImportEnabled(importEnabled)
Sadik Armagan045f6be2020-09-10 13:37:32 +0100175 , m_ModelOptions(modelOptions)
Derek Lambertif1e0ad32021-10-13 18:02:25 +0100176 , m_ProfilingEnabled(false)
Colm Donelan03bf98a2022-05-30 15:20:36 +0100177 , m_ExportEnabled(exportEnabled)
Narumol Prangnawaratbc7ffb52020-03-20 15:01:01 +0000178 {
179 if (m_ReduceFp32ToFp16 && m_ReduceFp32ToBf16)
180 {
181 throw InvalidArgumentException("BFloat16 and Float16 optimization cannot be enabled at the same time.");
182 }
183 }
telsoa01c577f2c2018-08-31 09:22:23 +0100184
Jan Eilers6a71bb52021-10-26 17:41:18 +0100185 const std::string ToString() const
186 {
187 std::stringstream stream;
188 stream << "OptimizerOptions: \n";
189 stream << "\tReduceFp32ToFp16: " << m_ReduceFp32ToFp16 << "\n";
190 stream << "\tReduceFp32ToBf16: " << m_ReduceFp32ToBf16 << "\n";
Jan Eilers17d34da2021-12-08 16:15:12 +0000191 stream << "\tDebug: " << m_Debug << "\n";
192 stream << "\tShapeInferenceMethod: " <<
Jan Eilers6a71bb52021-10-26 17:41:18 +0100193 (m_shapeInferenceMethod == ShapeInferenceMethod::ValidateOnly ? "ValidateOnly" : "InferAndValidate") << "\n";
194 stream << "\tImportEnabled: " << m_ImportEnabled << "\n";
Colm Donelan03bf98a2022-05-30 15:20:36 +0100195 stream << "\tExportEnabled: " << m_ExportEnabled << "\n";
Jan Eilers6a71bb52021-10-26 17:41:18 +0100196 stream << "\tProfilingEnabled: " << m_ProfilingEnabled << "\n";
197
198 stream << "\tModelOptions: \n";
199 for (auto optionsGroup : m_ModelOptions)
200 {
201 for (size_t i=0; i < optionsGroup.GetOptionCount(); i++)
202 {
203 const armnn::BackendOptions::BackendOption option = optionsGroup.GetOption(i);
Jan Eilers17d34da2021-12-08 16:15:12 +0000204 stream << "\t\tBackend: " << optionsGroup.GetBackendId() << "\n"
205 << "\t\t\tOption: " << option.GetName() << "\n"
206 << "\t\t\tValue: " << std::string(option.GetValue().ToString()) << "\n";
Jan Eilers6a71bb52021-10-26 17:41:18 +0100207 }
208 }
209
210 return stream.str();
211 }
212
Jan Eilersb1c62f12021-10-26 14:56:47 +0100213 /// Reduces all Fp32 operators in the model to Fp16 for faster processing.
214 /// @Note This feature works best if all operators of the model are in Fp32. ArmNN will add conversion layers
215 /// between layers that weren't in Fp32 in the first place or if the operator is not supported in Fp16.
216 /// The overhead of these conversions can lead to a slower overall performance if too many conversions are
217 /// required.
telsoa01c577f2c2018-08-31 09:22:23 +0100218 bool m_ReduceFp32ToFp16;
keidav01738c2e62018-12-11 16:14:20 +0000219
220 // Add debug data for easier troubleshooting
221 bool m_Debug;
Narumol Prangnawaratbc7ffb52020-03-20 15:01:01 +0000222
Jan Eilersb1c62f12021-10-26 14:56:47 +0100223 /// Reduces all Fp32 operators in the model to Bf16 for faster processing.
224 /// @Note This feature works best if all operators of the model are in Fp32. ArmNN will add conversion layers
225 /// between layers that weren't in Fp32 in the first place or if the operator is not supported in Bf16.
226 /// The overhead of these conversions can lead to a slower overall performance if too many conversions are
227 /// required.
Narumol Prangnawaratbc7ffb52020-03-20 15:01:01 +0000228 bool m_ReduceFp32ToBf16;
Teresa Charlincdc01492020-06-09 18:00:20 +0100229
230 // Infer output size when not available
231 ShapeInferenceMethod m_shapeInferenceMethod;
Narumol Prangnawarata2493a02020-08-19 14:39:07 +0100232
233 // Enable Import
234 bool m_ImportEnabled;
Sadik Armagan045f6be2020-09-10 13:37:32 +0100235
236 // Enable Model Options
237 ModelOptions m_ModelOptions;
Derek Lambertif1e0ad32021-10-13 18:02:25 +0100238
239 // Enable profiling dump of the optimizer phase
240 bool m_ProfilingEnabled;
Colm Donelan03bf98a2022-05-30 15:20:36 +0100241
242 // Enable Export
243 bool m_ExportEnabled;
telsoa01c577f2c2018-08-31 09:22:23 +0100244};
telsoa014fcda012018-03-09 14:13:49 +0000245
Francis Murtagh3d2b4b22021-02-15 18:23:17 +0000246class IWorkloadFactory;
247class NetworkImpl;
248using INetworkPtr = std::unique_ptr<INetwork, void(*)(INetwork* network)>;
249using IOptimizedNetworkPtr = std::unique_ptr<IOptimizedNetwork, void(*)(IOptimizedNetwork* network)>;
250
Cathal Corbett18655b82021-12-13 13:03:22 +0000251using CompiledBlobDeleter = std::function<void(const void*)>;
252using CompiledBlobPtr = std::unique_ptr<void, CompiledBlobDeleter>;
253
Francis Murtagh3d2b4b22021-02-15 18:23:17 +0000254/// Main network class which provides the interface for building up a neural network.
255/// This object is subsequently required by the IRuntime::Load() method.
256class INetwork
257{
258public:
259 static INetwork* CreateRaw(NetworkOptions networkOptions = {});
260 static INetworkPtr Create(NetworkOptions networkOptions = {});
261 static void Destroy(INetwork* network);
262
263 Status PrintGraph();
264
265 /// Adds an input layer to the network.
266 /// @param id - User generated id to uniquely identify a particular input. The same id needs to be specified.
267 /// when passing the inputs to the IRuntime::EnqueueWorkload() function.
268 /// @param name - Optional name for the layer.
269 /// @return - Interface for configuring the layer.
270 IConnectableLayer* AddInputLayer(LayerBindingId id, const char* name = nullptr);
271
272 /// Adds an ArgMinMax layer to the network.
273 /// @param desc - Parameters for the L2 normalization operation.
274 /// @param name - Optional name for the layer.
275 /// @return - Interface for configuring the layer.
276 IConnectableLayer* AddArgMinMaxLayer(const ArgMinMaxDescriptor& desc,
277 const char* name = nullptr);
278
mathad01b392e982021-04-07 12:07:30 +0100279 /// Adds a cast layer to the network.
280 /// @param name - Optional name for the layer.
281 /// @return - Interface for configuring the layer.
282 IConnectableLayer* AddCastLayer(const char* name = nullptr);
283
Francis Murtagh3d2b4b22021-02-15 18:23:17 +0000284 /// Add a Comparison layer to the network.
285 /// @param name - Optional name for the layer.
286 /// @param desc - Descriptor for the comparison operation.
287 /// @return - Interface for configuring the layer.
288 IConnectableLayer* AddComparisonLayer(const ComparisonDescriptor& comparisonDescriptor,
289 const char* name = nullptr);
290
291 /// Adds a concatenation layer to the network.
292 /// @param concatDescriptor - ConcatDescriptor (synonym for OriginsDescriptor) to configure the concatenation
293 /// process. Number of Views must be equal to the number of inputs, and their order
294 /// must match - e.g. first view corresponds to the first input, second view to the
295 /// second input, etc....
296 /// @param name - Optional name for the layer.
297 /// @return - Interface for configuring the layer.
298 IConnectableLayer* AddConcatLayer(const ConcatDescriptor& concatDescriptor,
299 const char* name = nullptr);
300
301 /// Adds a 2D convolution layer to the network.
302 /// @param convolution2dDescriptor - Description of the 2D convolution layer.
Keith Davisb4dd5cc2022-04-07 11:32:00 +0100303 /// @param name - Optional name for the layer.
304 /// @return - Interface for configuring the layer.
305 IConnectableLayer* AddConvolution2dLayer(const Convolution2dDescriptor& convolution2dDescriptor,
306 const char* name = nullptr);
307
308 /// Adds a 2D convolution layer to the network.
309 /// @param convolution2dDescriptor - Description of the 2D convolution layer.
Francis Murtagh3d2b4b22021-02-15 18:23:17 +0000310 /// @param weights - Tensor for the weights data.
311 /// @param biases - Optional tensor for the bias data. If specified, must match the output tensor shape.
312 /// @param name - Optional name for the layer.
313 /// @return - Interface for configuring the layer.
Keith Davisb4dd5cc2022-04-07 11:32:00 +0100314 ARMNN_DEPRECATED_MSG_REMOVAL_DATE("This AddConvolution2dLayer overload is deprecated", "22.08")
Francis Murtagh3d2b4b22021-02-15 18:23:17 +0000315 IConnectableLayer* AddConvolution2dLayer(const Convolution2dDescriptor& convolution2dDescriptor,
316 const ConstTensor& weights,
317 const Optional<ConstTensor>& biases,
318 const char* name = nullptr);
319
Jan Eilers1b2654f2021-09-24 15:45:46 +0100320 ARMNN_DEPRECATED_MSG_REMOVAL_DATE("This AddConvolution2dLayer overload is deprecated", "22.08")
Francis Murtagh3d2b4b22021-02-15 18:23:17 +0000321 IConnectableLayer* AddConvolution2dLayer(const Convolution2dDescriptor& convolution2dDescriptor,
322 const ConstTensor& weights,
323 const char* name = nullptr);
324
Jan Eilers1b2654f2021-09-24 15:45:46 +0100325 ARMNN_DEPRECATED_MSG_REMOVAL_DATE("This AddConvolution2dLayer overload is deprecated", "22.08")
Francis Murtagh3d2b4b22021-02-15 18:23:17 +0000326 IConnectableLayer* AddConvolution2dLayer(const Convolution2dDescriptor& convolution2dDescriptor,
327 const ConstTensor& weights,
328 const ConstTensor& biases,
329 const char* name = nullptr);
330
Matthew Sloyanb63a3112021-09-08 13:05:51 +0100331 /// Adds a 3D convolution layer to the network.
332 /// @param convolution3dDescriptor - Description of the 3D convolution layer.
Matthew Sloyanb63a3112021-09-08 13:05:51 +0100333 /// @param name - Optional name for the layer.
334 /// @return - Interface for configuring the layer.
335 IConnectableLayer* AddConvolution3dLayer(const Convolution3dDescriptor& convolution3dDescriptor,
Matthew Sloyanb63a3112021-09-08 13:05:51 +0100336 const char* name = nullptr);
337
Francis Murtagh3d2b4b22021-02-15 18:23:17 +0000338 /// Adds a depth to space layer to the network.
339 /// @param depthToSpaceDescriptor - Parameters for the depth to space operation.
340 /// @param name - Optional name for the layer.
341 /// @return - Interface for configuring the layer.
342 IConnectableLayer* AddDepthToSpaceLayer(const DepthToSpaceDescriptor& depthToSpaceDescriptor,
343 const char* name = nullptr);
344
345 /// Adds a 2D depthwise convolution layer to the network.
346 /// @param convolution2dDescriptor - Description of the 2D depthwise convolution layer.
Cathal Corbett06902652022-04-14 17:55:11 +0100347 /// @param name - Optional name for the layer.
348 /// @return - Interface for configuring the layer.
Keith Davisb4dd5cc2022-04-07 11:32:00 +0100349 IConnectableLayer* AddDepthwiseConvolution2dLayer(const DepthwiseConvolution2dDescriptor& convolution2dDescriptor,
350 const char* name = nullptr);
Cathal Corbett06902652022-04-14 17:55:11 +0100351
352 /// Adds a 2D depthwise convolution layer to the network.
353 /// @param convolution2dDescriptor - Description of the 2D depthwise convolution layer.
Francis Murtagh3d2b4b22021-02-15 18:23:17 +0000354 /// @param weights - Tensor for the weights. Expected format: [channelMultiplier, inputChannels, height, width].
355 /// @param biases Optional tensor for the bias data. If specified, must match the output tensor shape.
356 /// @param name - Optional name for the layer.
357 /// @return - Interface for configuring the layer.
Cathal Corbett06902652022-04-14 17:55:11 +0100358 ARMNN_DEPRECATED_MSG("This AddDepthwiseConvolution2dLayer overload is deprecated")
Francis Murtagh3d2b4b22021-02-15 18:23:17 +0000359 IConnectableLayer* AddDepthwiseConvolution2dLayer(
360 const DepthwiseConvolution2dDescriptor& convolution2dDescriptor,
361 const ConstTensor& weights,
362 const Optional<ConstTensor>& biases,
363 const char* name = nullptr);
364
Francis Murtagh3d2b4b22021-02-15 18:23:17 +0000365 /// Adds a Dequantize layer to the network.
366 /// @return - Interface for configuring the layer.
367 IConnectableLayer* AddDequantizeLayer(const char* name = nullptr);
368
369 /// Adds a Detection PostProcess layer to the network.
370 /// @param descriptor - Description of the Detection PostProcess layer.
371 /// @param anchors - Tensor for anchors.
372 /// @param name - Optional name for the layer.
373 /// @return - Interface for configuring the layer.
374 IConnectableLayer* AddDetectionPostProcessLayer(
375 const DetectionPostProcessDescriptor& descriptor,
376 const ConstTensor& anchors,
377 const char* name = nullptr);
378
379 /// Add an ElementwiseUnary layer to the network.
380 /// @param name - Optional name for the layer.
381 /// @param desc - Descriptor for the elementwiseUnary operation.
382 /// @return - Interface for configuring the layer.
383 IConnectableLayer* AddElementwiseUnaryLayer(const ElementwiseUnaryDescriptor& elementwiseUnaryDescriptor,
384 const char* name = nullptr);
385
386 /// Add an Fill layer to the network.
387 /// @param name - Optional name for the layer.
388 /// @param fillDescriptor - Descriptor for the fill operation.
389 /// @return - Interface for configuring the layer.
390 IConnectableLayer* AddFillLayer(const FillDescriptor& fillDescriptor,
391 const char* name = nullptr);
392
Matthew Sloyan81beae32021-07-13 19:46:11 +0100393
Francis Murtagh3d2b4b22021-02-15 18:23:17 +0000394 /// Adds a fully connected layer to the network.
395 /// @param fullyConnectedDescriptor - Description of the fully connected layer.
Sadik Armaganf0a6dec2021-03-25 07:46:55 +0000396 /// @return - Interface for configuring the layer.
Matthew Sloyan57d2c7e2021-08-12 17:41:04 +0100397 ///
398 /// @note Weights and biases are passed in as inputs. If they are constant tensors you can simply store
399 /// them in a ConstantLayer as seen below. A full example can be found in samples/SimpleSample.cpp.
400 ///
401 /// @code
402 /// // Make sure the IsConstant flag is set on the weightsInfo before passing it to the ConstTensor.
403 /// ConstTensor weights(weightsInfo, weightsData);
404 ///
405 /// // Constant layer that now holds weights data for FullyConnected
406 /// IConnectableLayer* const constantWeightsLayer = myNetwork->AddConstantLayer(weights, "weights");
407 ///
408 /// FullyConnectedDescriptor fullyConnectedDesc;
409 /// IConnectableLayer* const fullyConnectedLayer = myNetwork->AddFullyConnectedLayer(fullyConnectedDesc,
410 /// "fully connected");
411 /// IConnectableLayer* InputLayer = myNetwork->AddInputLayer(0);
412 /// InputLayer->GetOutputSlot(0).Connect(fullyConnectedLayer->GetInputSlot(0));
413 /// constantWeightsLayer->GetOutputSlot(0).Connect(fullyConnectedLayer->GetInputSlot(1));
414 /// @endcode
Sadik Armaganf0a6dec2021-03-25 07:46:55 +0000415 IConnectableLayer* AddFullyConnectedLayer(const FullyConnectedDescriptor& fullyConnectedDescriptor,
Matthew Sloyan81beae32021-07-13 19:46:11 +0100416 const char* name = nullptr);
417
Francis Murtagh3d2b4b22021-02-15 18:23:17 +0000418 /// Adds a permute layer to the network.
419 /// @param permuteDescriptor - PermuteDescriptor to configure the permute.
420 /// @param name - Optional name for the layer.
421 /// @return - Interface for configuring the layer.
422 IConnectableLayer* AddPermuteLayer(const PermuteDescriptor& permuteDescriptor,
423 const char* name = nullptr);
424
425 /// Adds a batch to space ND layer to the network.
426 /// @param batchToSpaceNdDescriptor - Description of the layer.
427 /// @param name - Optional name for the layer.
428 /// @return - Interface for configuring the layer.
429 IConnectableLayer* AddBatchToSpaceNdLayer(const BatchToSpaceNdDescriptor& batchToSpaceNdDescriptor,
430 const char* name = nullptr);
431
Tamás Nyíri7b885b32021-10-26 14:47:57 +0100432 /// Adds a 2D pooling layer to the network.
Francis Murtagh3d2b4b22021-02-15 18:23:17 +0000433 /// @param pooling2dDescriptor - Pooling2dDescriptor to configure the pooling.
434 /// @param name - Optional name for the layer.
435 /// @return - Interface for configuring the layer.
436 IConnectableLayer* AddPooling2dLayer(const Pooling2dDescriptor& pooling2dDescriptor,
437 const char* name = nullptr);
438
Tamás Nyíri7b885b32021-10-26 14:47:57 +0100439 /// Adds a 3D pooling layer to the network.
440 /// @param pooling3dDescriptor - Pooling3dDescriptor to configure the pooling.
441 /// @param name - Optional name for the layer.
442 /// @return - Interface for configuring the layer.
443 IConnectableLayer* AddPooling3dLayer(const Pooling3dDescriptor& pooling3dDescriptor,
444 const char* name = nullptr);
445
Cathal Corbett18655b82021-12-13 13:03:22 +0000446 /// Adds a Precompiled layer to the network.
447 /// Method use is for backend users.
448 /// @param preCompiledDescriptor - PreCompiledDescriptor contains parameters for the Precompiled layer.
449 /// @param compiledBlobPtr - CompiledBlobPtr pre-compiled object set for the Precompiled layer.
450 /// @param backend - optional BackendId set for the Precompiled layer.
451 /// @return - Interface for configuring the layer.
452 IConnectableLayer* AddPrecompiledLayer(const PreCompiledDescriptor& preCompiledDescriptor,
Cathal Corbett3ea01072022-01-06 10:29:43 +0000453 CompiledBlobPtr compiledBlobPtr,
Cathal Corbettcbfd7182021-12-15 17:12:59 +0000454 const Optional<BackendId>& backend,
455 const char* name = nullptr);
Cathal Corbett18655b82021-12-13 13:03:22 +0000456
Francis Murtagh3d2b4b22021-02-15 18:23:17 +0000457 /// Adds an activation layer to the network.
458 /// @param activationDescriptor - ActivationDescriptor to configure the activation.
459 /// @param name - Optional name for the layer.
460 /// @return - Interface for configuring the layer.
461 IConnectableLayer* AddActivationLayer(const ActivationDescriptor& activationDescriptor,
462 const char* name = nullptr);
463
464 /// Adds a normalization layer to the network.
465 /// @param normalizationDescriptor - NormalizationDescriptor to configure the normalization.
466 /// @param name - Optional name for the layer.
467 /// @return - Interface for configuring the layer.
468 IConnectableLayer* AddNormalizationLayer(const NormalizationDescriptor& normalizationDescriptor,
469 const char* name = nullptr);
470
471 /// Adds a slice layer to the network.
472 /// @param sliceDescriptor - SliceDescriptor to configure the slice operation.
473 /// @param name - Optional name for the layer.
474 /// @return - Interface for configuring the layer.
475 IConnectableLayer* AddSliceLayer(const SliceDescriptor& sliceDescriptor, const char* name = nullptr);
476
477 /// Adds a softmax layer to the network.
478 /// If the data type is QAsymm8, then the output quantization parameters
479 /// must have a scale of 1/256 and an offset of 0
480 /// @param softmaxDescriptor - SoftmaxDescriptor to configure the softmax.
481 /// @param name - Optional name for the layer.
482 /// @return - Interface for configuring the layer.
483 IConnectableLayer* AddSoftmaxLayer(const SoftmaxDescriptor& softmaxDescriptor,
484 const char* name = nullptr);
485
486 /// Adds a splitter layer to the network.
487 /// @param splitterDescriptor - ViewsDescriptor to configure the splitting process.
488 /// Number of Views must be equal to the number of outputs,
489 /// and their order must match - e.g. first view corresponds to
490 /// the first output, second view to the second output, etc....
491 /// @param name - Optional name for the layer.
492 /// @return - Interface for configuring the layer.
493 IConnectableLayer* AddSplitterLayer(const ViewsDescriptor& splitterDescriptor,
494 const char* name = nullptr);
495
496 /// Adds a merge layer to the network.
497 /// @param name - Optional name for the layer.
498 /// @return - Interface for configuring the layer.
499 IConnectableLayer* AddMergeLayer(const char* name = nullptr);
500
Francis Murtagh3d2b4b22021-02-15 18:23:17 +0000501 /// Adds an addition layer to the network.
502 /// @param name - Optional name for the layer.
503 /// @return - Interface for configuring the layer.
504 IConnectableLayer* AddAdditionLayer(const char* name = nullptr);
505
506 /// Adds a multiplication layer to the network.
507 /// @param name - Optional name for the layer.
508 /// @return - Interface for configuring the layer.
509 IConnectableLayer* AddMultiplicationLayer(const char* name = nullptr);
510
511 /// Adds a batch normalization layer to the network.
512 /// @param mean - Pre-calculated mean for each channel.
513 /// @param variance - Pre-calculated variance for each channel.
514 /// @param beta - Per-channel additive factor.
515 /// @param gamma - Per-channel multiplicative factor.
516 /// @return - Interface for configuring the layer.
517 /// @param name - Optional name for the layer.
518 IConnectableLayer* AddBatchNormalizationLayer(const BatchNormalizationDescriptor& desc,
519 const ConstTensor& mean,
520 const ConstTensor& variance,
521 const ConstTensor& beta,
522 const ConstTensor& gamma,
523 const char* name = nullptr);
524
525 /// Adds a rank layer to the network.
526 /// @param name - Optional name for the layer.
527 /// @return - Interface for configuring the layer.
528 IConnectableLayer* AddRankLayer(const char* name = nullptr);
529
Francis Murtagh3d2b4b22021-02-15 18:23:17 +0000530 /// Adds a resize layer to the network.
531 /// @param resizeDescriptor - Parameters for the resize operation.
532 /// @param name - Optional name for the layer.
533 /// @return - Interface for configuring the layer.
534 IConnectableLayer* AddResizeLayer(const ResizeDescriptor& resizeDescriptor,
535 const char* name = nullptr);
536
537 /// Adds a reduce layer to the network.
538 /// @param ReduceDescriptor - Parameters for the reduce operation.
539 /// @param name - Optional name for the layer.
540 /// @return - Interface for configuring the layer.
541 IConnectableLayer* AddReduceLayer(const ReduceDescriptor& reduceDescriptor,
542 const char* name = nullptr);
543
544 /// Adds an instance normalization layer to the network.
545 /// @param desc - Parameters for the instance normalization operation.
546 /// @param name - Optional name for the layer.
547 /// @return - Interface for configuring the layer.
548 IConnectableLayer* AddInstanceNormalizationLayer(const InstanceNormalizationDescriptor& desc,
549 const char* name = nullptr);
550
551 /// Adds an L2 normalization layer to the network.
552 /// Normalization is performed along dimension 1, but requires a 4d input.
553 /// @param desc - Parameters for the L2 normalization operation.
554 /// @param name - Optional name for the layer.
555 /// @return - Interface for configuring the layer.
556 IConnectableLayer* AddL2NormalizationLayer(const L2NormalizationDescriptor& desc,
557 const char* name = nullptr);
558
559 /// Adds a log softmax layer to the network.
560 /// @param logSoftmaxDescriptor - LogSoftmaxDescriptor to configure the log softmax.
561 /// @param name - Optional name for the layer.
562 /// @return - Interface for configuring the layer.
563 IConnectableLayer* AddLogSoftmaxLayer(const LogSoftmaxDescriptor& logSoftmaxDescriptor,
564 const char* name = nullptr);
565
566 /// Adds a layer with no inputs and a single output, which always corresponds to
567 /// the passed in constant tensor.
568 /// @param input - Tensor to be provided as the only output of the layer. The layer will maintain
569 /// its own copy of the tensor data, meaning the memory referenced by @a input can
570 /// be freed or reused after this function is called.
571 /// @param name - Optional name for the layer.
572 /// @return - Interface for configuring the layer.
573 IConnectableLayer* AddConstantLayer(const ConstTensor& input,
574 const char* name = nullptr);
575
576 /// Adds a reshape layer to the network.
577 /// @param reshapeDescriptor - Parameters for the reshape operation.
578 /// @param name - Optional name for the layer.
579 /// @return - Interface for configuring the layer.
580 IConnectableLayer* AddReshapeLayer(const ReshapeDescriptor& reshapeDescriptor,
581 const char* name = nullptr);
582
Keith Davis3ae3f972021-05-21 16:33:48 +0100583 /// Adds a shape layer to the network.
584 /// @param name - Optional name for the layer.
585 /// @return - Interface for configuring the layer.
586 IConnectableLayer* AddShapeLayer(const char* name = nullptr);
587
Francis Murtagh3d2b4b22021-02-15 18:23:17 +0000588 /// Adds a space to batch layer to the network.
589 /// @param spaceToBatchNdDescriptor - Parameters for the space to batch operation.
590 /// @param name - Optional name for the layer.
591 /// @return - Interface for configuring the layer.
592 IConnectableLayer* AddSpaceToBatchNdLayer(const SpaceToBatchNdDescriptor& spaceToBatchNdDescriptor,
593 const char* name = nullptr);
594
595 /// Adds a space to depth layer to the network.
596 /// @param spaceToDepthDescriptor - Parameters for the space to depth operation.
597 /// @param name - Optional name for the layer.
598 /// @return - Interface for configuring the layer.
599 IConnectableLayer* AddSpaceToDepthLayer(const SpaceToDepthDescriptor& spaceToDepthDescriptor,
600 const char* name = nullptr);
601
602 /// Adds a floor layer to the network.
603 /// @param name - Optional name for the layer.
604 /// @return - Interface for configuring the layer.
605 IConnectableLayer* AddFloorLayer(const char* name = nullptr);
606
607 /// Adds an output layer to the network.
608 /// @param id - User generated id to uniquely identify a particular output. The same id needs to be specified
609 /// when passing the outputs to the IRuntime::EnqueueWorkload() function.
610 /// @param name - Optional name for the layer.
611 /// @return - Interface for configuring the layer.
612 IConnectableLayer* AddOutputLayer(LayerBindingId id, const char* name = nullptr);
613
614 /// Add a Lstm layer to the network
615 /// @param descriptor - Parameters for the Lstm operation
616 /// @param params - Weights and biases for the LSTM cell
617 /// @param name - Optional name for the layer
618 /// @return - Interface for configuring the layer.
619 IConnectableLayer* AddLstmLayer(const LstmDescriptor& descriptor,
620 const LstmInputParams& params,
621 const char* name = nullptr);
622
623 /// Adds a division layer to the network.
624 /// @param name - Optional name for the layer.
625 /// @return - Interface for configuring the layer.
626 IConnectableLayer* AddDivisionLayer(const char* name = nullptr);
627
628 /// Adds a subtraction layer to the network.
629 /// @param name - Optional name for the layer.
630 /// @return - Interface for configuring the layer.
631 IConnectableLayer* AddSubtractionLayer(const char* name = nullptr);
632
633 /// Add a Maximum layer to the network.
634 /// @param name - Optional name for the layer.
635 /// @return - Interface for configuring the layer.
636 IConnectableLayer* AddMaximumLayer(const char* name = nullptr);
637
638 /// Add a Mean layer to the network.
639 /// @param meanDescriptor - Parameters for the mean operation.
640 /// @param name - Optional name for the layer.
641 /// @return - Interface for configuring the layer.
642 IConnectableLayer* AddMeanLayer(const MeanDescriptor& meanDescriptor, const char* name = nullptr);
643
644 /// Adds a fully pad layer to the network.
645 /// @param paddings - n by 2 tensor, where n is the rank of the input tensor,
646 /// such that paddings[i,0] indicates the amount of padding to add in front of dimonsion i, and
647 /// paddings[i,1] indicates the amount of padding to add after the end of dimension i
648 /// @param name - Optional name for the layer.
649 /// @return - Interface for configuring the layer.
650 IConnectableLayer* AddPadLayer(const PadDescriptor& padDescriptor,
651 const char* name = nullptr);
652
653 /// Add a quantize layer to the network
654 ///@param name - Optional name for the layer.
655 /// @return - Interface for configuring the layer.
656 IConnectableLayer* AddQuantizeLayer(const char* name = nullptr);
657
658 /// Adds a strided slice layer to the network.
659 /// @param StridedSliceDescriptor - Parameters for the strided slice operation.
660 /// @param name - Optional name for the layer.
661 /// @return - Interface for configuring the layer.
662 IConnectableLayer* AddStridedSliceLayer(const StridedSliceDescriptor& stridedSliceDescriptor,
663 const char* name = nullptr);
664
665 /// Add a Minimum layer to the network.
666 /// @param name - Optional name for the layer.
667 /// @return - Interface for configuring the layer.
668 IConnectableLayer* AddMinimumLayer(const char* name = nullptr);
669
Francis Murtagh3d2b4b22021-02-15 18:23:17 +0000670 /// Add Gather layer to the network.
671 /// @param descriptor - Description of the gather layer.
672 /// @param name - Optional name for the layer.
673 /// @return - Interface for configuring the layer.
674 IConnectableLayer* AddGatherLayer(const GatherDescriptor& descriptor,
675 const char* name = nullptr);
676
Teresa Charlinb2d3ec52022-04-12 22:07:09 +0100677 /// Add GatherNd layer to the network.
678 /// @param name - Optional name for the layer.
679 /// @return - Interface for configuring the layer.
680 IConnectableLayer* AddGatherNdLayer(const char* name = nullptr);
681
Francis Murtagh3d2b4b22021-02-15 18:23:17 +0000682 /// Adds a switch layer to the network.
683 /// @param name - Optional name for the layer.
684 /// @return - Interface for configuring the layer.
685 IConnectableLayer* AddSwitchLayer(const char* name = nullptr);
686
687 /// Adds a PReLU layer to the network.
688 /// @param name - Optional name for the layer.
689 /// @return - Interface for configuring the layer.
690 IConnectableLayer* AddPreluLayer(const char* name = nullptr);
691
692 /// Adds a 2D transpose convolution layer to the network.
693 /// @param descriptor - Description of the 2D transpose convolution layer.
694 /// @param weights - Tensor for the weights data.
695 /// @param biases - Optional tensor for the bias data.
696 /// @param name - Optional name for the layer.
697 /// @return - Interface for configuring the layer.
698 IConnectableLayer* AddTransposeConvolution2dLayer(const TransposeConvolution2dDescriptor& descriptor,
699 const ConstTensor& weights,
700 const Optional<ConstTensor>& biases,
701 const char* name = nullptr);
702
703 /// Adds a transpose layer to the network.
704 /// @param transposeDescriptor - TransposeDescriptor to configure the transpose.
705 /// @param name - Optional name for the layer.
706 /// @return - Interface for configuring the layer.
707 IConnectableLayer* AddTransposeLayer(const TransposeDescriptor& transposeDescriptor,
708 const char* name = nullptr);
709
710 /// Adds a stack layer to the network.
711 /// @param descriptor - Description of the stack layer.
712 /// @param name - Optional name for the layer.
713 /// @return - Interface for configuring the layer.
714 IConnectableLayer* AddStackLayer(const StackDescriptor& descriptor,
715 const char* name = nullptr);
716
717 /// Add a stand-in layer for a type unknown to the Arm NN framework.
718 /// Note: Due to the nature of this layer, no validation can be performed by the framework.
719 /// Furthermore, Any model containing this layer cannot make use of dynamic tensors since the
720 /// tensor sizes cannot be inferred.
721 /// @descriptor - Descriptor for the StandIn layer.
722 /// @return - Interface for configuring the layer.
723 IConnectableLayer* AddStandInLayer(const StandInDescriptor& descriptor,
724 const char* name = nullptr);
725
726 /// Add a QuantizedLstm layer to the network
727 /// @param params - The weights and biases for the Quantized LSTM cell
728 /// @param name - Optional name for the layer
729 /// @return - Interface for configuring the layer.
730 IConnectableLayer* AddQuantizedLstmLayer(const QuantizedLstmInputParams& params,
731 const char* name = nullptr);
732
733 /// Add a QLstm layer to the network
734 /// @param descriptor - Parameters for the QLstm operation
735 /// @param params - Weights and biases for the layer
736 /// @param name - Optional name for the layer
737 /// @return - Interface for configuring the layer.
738 IConnectableLayer* AddQLstmLayer(const QLstmDescriptor& descriptor,
739 const LstmInputParams& params,
740 const char* name = nullptr);
741
742 /// Adds a Logical Binary layer to the network.
743 /// @param descriptor - Description of the Logical Binary layer.
744 /// @param name - Optional name for the layer.
745 /// @return - Interface for configuring the layer.
746 IConnectableLayer* AddLogicalBinaryLayer(const LogicalBinaryDescriptor& descriptor,
747 const char* name = nullptr);
748
Narumol Prangnawarat8ed39ae2021-07-15 16:16:25 +0100749 /// Add a UnidirectionalSequenceLstm layer to the network
750 /// @param descriptor - Parameters for the UnidirectionalSequenceLstm operation
751 /// @param params - Weights and biases for the UnidirectionalSequenceLstm
752 /// @param name - Optional name for the layer
753 /// @return - Interface for configuring the layer.
754 IConnectableLayer* AddUnidirectionalSequenceLstmLayer(const UnidirectionalSequenceLstmDescriptor& descriptor,
755 const LstmInputParams& params,
756 const char* name = nullptr);
757
Simon Obute51f67772021-09-03 15:50:13 +0100758 /// Add a ChannelShuffle layer to the network
759 /// @param descriptor - Parameters for the ChannelShuffle operation
760 /// @param name - Optional name for the layer
761 /// @return - Interface for configuring the layer
762 IConnectableLayer* AddChannelShuffleLayer(const ChannelShuffleDescriptor& descriptor,
763 const char* name = nullptr);
764
Jan Eilers1b2654f2021-09-24 15:45:46 +0100765 // The Accept function needs to be wrapped in a no warn macro to avoid deprecation warnings from
766 // the deprecated ILayerVisitor which is used in the function.
767 ARMNN_NO_DEPRECATE_WARN_BEGIN
768 /// Apply a visitor to this layer
769 ARMNN_DEPRECATED_MSG_REMOVAL_DATE("Accept is deprecated. The ILayerVisitor that works in conjunction with this "
770 "Accept function is deprecated. Use IStrategy in combination with "
771 "ExecuteStrategy instead, which is an ABI/API stable version of the "
772 "visitor pattern.",
773 "22.05")
Francis Murtagh3d2b4b22021-02-15 18:23:17 +0000774 void Accept(ILayerVisitor& visitor) const;
Jan Eilers1b2654f2021-09-24 15:45:46 +0100775 ARMNN_NO_DEPRECATE_WARN_END
Francis Murtagh3d2b4b22021-02-15 18:23:17 +0000776
777 void ExecuteStrategy(IStrategy& strategy) const;
778
779protected:
780 ~INetwork();
781
Francis Murtagh3d2b4b22021-02-15 18:23:17 +0000782 friend void VisitLayersTopologically(const INetwork* inputNetwork, IStrategy& strategy);
783 friend class TestConnectionPreservation;
784 friend TensorInfo GetInputTensorInfo(const INetwork* network);
785 friend IOptimizedNetworkPtr Optimize(const INetwork& network,
786 const std::vector<BackendId>& backendPreferences,
787 const IDeviceSpec& deviceSpec,
788 const OptimizerOptions& options,
789 Optional<std::vector<std::string>&> messages);
790
791 INetwork(NetworkOptions networkOptions = {});
792
793 std::unique_ptr<NetworkImpl> pNetworkImpl;
794};
795
Mike Kelly386ff1a2021-03-29 15:04:50 +0100796namespace experimental
797{
Sadik Armagana0042512021-03-30 11:05:36 +0100798class AsyncNetworkImpl;
Mike Kelly386ff1a2021-03-29 15:04:50 +0100799class WorkingMemHandle;
800}
801
Francis Murtagh3d2b4b22021-02-15 18:23:17 +0000802struct BackendSettings;
803struct OptimizationResult;
804class OptimizedNetworkImpl;
Derek Lambertie155bbf2021-10-13 14:32:12 +0100805class IProfiler;
Francis Murtagh3d2b4b22021-02-15 18:23:17 +0000806class IOptimizedNetwork
807{
808public:
809 static void Destroy(IOptimizedNetwork* network);
810
811 Status PrintGraph();
812 Status SerializeToDot(std::ostream& stream) const;
813
Cathal Corbett5aa9fd72022-02-25 15:33:28 +0000814 arm::pipe::ProfilingGuid GetGuid() const;
Francis Murtagh3d2b4b22021-02-15 18:23:17 +0000815
Sadik Armaganb7851f92021-10-06 16:37:02 +0100816 size_t GetNumInputs() const;
817 size_t GetNumOutputs() const;
818
Mike Kelly0d677db2021-06-27 22:39:21 +0100819 // Creates a copy of the IOptimizedNetwork. The IOptimizedNetwork will not be reoptimized,
820 // the provided ModelOptions will only be used when creating a LoadedNetwork.
821 IOptimizedNetwork(const IOptimizedNetwork& other, const ModelOptions& modelOptions);
Francis Murtagh3d2b4b22021-02-15 18:23:17 +0000822 IOptimizedNetwork(std::unique_ptr<Graph> graph);
823 IOptimizedNetwork(std::unique_ptr<OptimizedNetworkImpl> impl);
824 ~IOptimizedNetwork();
825
Derek Lambertie155bbf2021-10-13 14:32:12 +0100826 const std::shared_ptr<IProfiler>& GetProfiler() const;
827
Francis Murtagh3d2b4b22021-02-15 18:23:17 +0000828protected:
829 friend class LoadedNetwork;
Mike Kelly386ff1a2021-03-29 15:04:50 +0100830
Sadik Armagana0042512021-03-30 11:05:36 +0100831 friend class experimental::AsyncNetworkImpl;
Mike Kelly386ff1a2021-03-29 15:04:50 +0100832 friend class experimental::WorkingMemHandle;
833
Francis Murtagh3d2b4b22021-02-15 18:23:17 +0000834 friend Graph& GetGraphForTesting(IOptimizedNetwork* optNetPtr);
835 friend ModelOptions& GetModelOptionsForTesting(IOptimizedNetwork* optNetPtr);
836 friend IOptimizedNetworkPtr Optimize(const INetwork& inNetwork,
837 const std::vector<BackendId>& backendPreferences,
838 const IDeviceSpec& deviceSpec,
839 const OptimizerOptions& options,
840 Optional<std::vector<std::string>&> messages);
Cathal Corbetta3f4fba2022-03-21 09:27:08 +0000841 friend IOptimizedNetworkPtr Optimize(const Graph& inGraph,
842 const std::vector<BackendId>& backendPreferences,
843 const IDeviceSpec& deviceSpec,
844 const OptimizerOptions& options,
845 Optional<std::vector<std::string>&> messages);
Francis Murtagh3d2b4b22021-02-15 18:23:17 +0000846
Francis Murtagh3d2b4b22021-02-15 18:23:17 +0000847 IOptimizedNetwork(std::unique_ptr<Graph> graph, const ModelOptions& modelOptions);
848
849 std::unique_ptr<OptimizedNetworkImpl> pOptimizedNetworkImpl;
850};
851
telsoa014fcda012018-03-09 14:13:49 +0000852/// Create an optimized version of the network
853/// @param network INetwork description of the network to be optimized.
telsoa01c577f2c2018-08-31 09:22:23 +0100854/// @param backendPreferences The choice of the backend ordered by user preferences.
855/// @param deviceSpec DeviceSpec object as queried from the runtime. See IRuntime::GetDeviceSpec()
Rob Hughes23214432019-11-05 11:27:36 +0000856/// @param messages If there are failures or warnings a string describing same will be added to the vector
telsoa01c577f2c2018-08-31 09:22:23 +0100857/// @param options OptimizerOptions object with optimizer configuration options
telsoa014fcda012018-03-09 14:13:49 +0000858/// @return An IOptimizedNetworkPtr interface to the optimized network, throws an exception derived from
859/// armnn::Exception if process fails.
telsoa014fcda012018-03-09 14:13:49 +0000860
telsoa01c577f2c2018-08-31 09:22:23 +0100861IOptimizedNetworkPtr Optimize(const INetwork& network,
David Beckf0b48452018-10-19 15:20:56 +0100862 const std::vector<BackendId>& backendPreferences,
telsoa01c577f2c2018-08-31 09:22:23 +0100863 const IDeviceSpec& deviceSpec,
jimfly016b0b53d2018-10-08 14:43:01 +0100864 const OptimizerOptions& options = OptimizerOptions(),
Rob Hughes23214432019-11-05 11:27:36 +0000865 Optional<std::vector<std::string>&> messages = EmptyOptional());
Cathal Corbetta3f4fba2022-03-21 09:27:08 +0000866
867/// Create an optimized version of the network
868/// @param inGraph Graph to be optimized.
869/// @param backendPreferences The choice of the backend ordered by user preferences.
870/// @param deviceSpec DeviceSpec object as queried from the runtime. See IRuntime::GetDeviceSpec()
871/// @param messages If there are failures or warnings a string describing same will be added to the vector
872/// @param options OptimizerOptions object with optimizer configuration options
873/// @return An IOptimizedNetworkPtr interface to the optimized network, throws an exception derived from
874/// armnn::Exception if process fails.
875
876IOptimizedNetworkPtr Optimize(const Graph& inGraph,
877 const std::vector<BackendId>& backendPreferences,
878 const IDeviceSpec& deviceSpec,
879 const OptimizerOptions& options,
880 Optional<std::vector<std::string>&> messages = EmptyOptional());
telsoa014fcda012018-03-09 14:13:49 +0000881} //namespace armnn