1 // 2 // Copyright © 2017 Arm Ltd and Contributors. All rights reserved. 3 // SPDX-License-Identifier: MIT 4 // 5 #pragma once 6 7 #include <armnn/BackendOptions.hpp> 8 #include <armnn/Deprecated.hpp> 9 #include <armnn/DescriptorsFwd.hpp> 10 #include <armnn/ILayerVisitor.hpp> 11 #include <armnn/NetworkFwd.hpp> 12 #include <armnn/Optional.hpp> 13 #include <armnn/TensorFwd.hpp> 14 #include <armnn/Types.hpp> 15 16 #include <memory> 17 #include <vector> 18 19 namespace armnn 20 { 21 /// @brief An input connection slot for a layer. 22 /// The input slot can be connected to an output slot of the preceding layer in the graph. 23 /// Only one connection to the input slot is allowed. 24 class IInputSlot 25 { 26 public: 27 virtual const IOutputSlot* GetConnection() const = 0; 28 virtual IOutputSlot* GetConnection() = 0; 29 30 protected: 31 /// Not user deletable. ~IInputSlot()32 ~IInputSlot() {} 33 }; 34 35 /// @brief An output connection slot for a layer. 36 /// The output slot may be connected to 1 or more input slots of subsequent layers in the graph. 37 class IOutputSlot 38 { 39 public: 40 virtual unsigned int GetNumConnections() const = 0; 41 virtual const IInputSlot* GetConnection(unsigned int index) const = 0; 42 virtual IInputSlot* GetConnection(unsigned int index) = 0; 43 44 virtual void SetTensorInfo(const TensorInfo& tensorInfo) = 0; 45 virtual const TensorInfo& GetTensorInfo() const = 0; 46 virtual bool IsTensorInfoSet() const = 0; 47 48 virtual int Connect(IInputSlot& destination) = 0; 49 virtual void Disconnect(IInputSlot& slot) = 0; 50 51 virtual unsigned int CalculateIndexOnOwner() const = 0; 52 53 virtual LayerGuid GetOwningLayerGuid() const = 0; 54 55 protected: 56 /// Not user deletable. ~IOutputSlot()57 ~IOutputSlot() {} 58 }; 59 60 /// @brief Interface for a layer that is connectable to other layers via InputSlots and OutputSlots. 61 class IConnectableLayer 62 { 63 public: 64 /// Returns the name of the layer 65 virtual const char* GetName() const = 0; 66 67 /// Returns the number of connectable input slots 68 virtual unsigned int GetNumInputSlots() const = 0; 69 70 /// Returns the number of connectable output slots 71 virtual unsigned int GetNumOutputSlots() const = 0; 72 73 /// Get a const input slot handle by slot index 74 virtual const IInputSlot& GetInputSlot(unsigned int index) const = 0; 75 76 /// Get the input slot handle by slot index 77 virtual IInputSlot& GetInputSlot(unsigned int index) = 0; 78 79 /// Get the const output slot handle by slot index 80 virtual const IOutputSlot& GetOutputSlot(unsigned int index) const = 0; 81 82 /// Get the output slot handle by slot index 83 virtual IOutputSlot& GetOutputSlot(unsigned int index) = 0; 84 85 /// Infer the shape of the output(s) based on the provided input shape(s) 86 virtual std::vector<TensorShape> InferOutputShapes(const std::vector<TensorShape>& inputShapes) const = 0; 87 88 /// Returns the unique id of the layer 89 virtual LayerGuid GetGuid() const = 0; 90 91 /// Apply a visitor to this layer 92 virtual void Accept(ILayerVisitor& visitor) const = 0; 93 94 /// Provide a hint for the optimizer as to which backend to prefer for this layer 95 virtual void BackendSelectionHint(Optional<BackendId> backend) = 0; 96 protected: 97 /// Objects are not deletable via the handle ~IConnectableLayer()98 ~IConnectableLayer() {} 99 }; 100 101 using INetworkPtr = std::unique_ptr<INetwork, void(*)(INetwork* network)>; 102 103 /// Main network class which provides the interface for building up a neural network. 104 /// This object is subsequently required by the IRuntime::Load() method. 105 class INetwork 106 { 107 public: 108 static INetwork* CreateRaw(NetworkOptions networkOptions = {}); 109 static INetworkPtr Create(NetworkOptions networkOptions = {}); 110 static void Destroy(INetwork* network); 111 112 virtual Status PrintGraph() = 0; 113 114 /// Adds an input layer to the network. 115 /// @param id - User generated id to uniquely identify a particular input. The same id needs to be specified. 116 /// when passing the inputs to the IRuntime::EnqueueWorkload() function. 117 /// @param name - Optional name for the layer. 118 /// @return - Interface for configuring the layer. 119 virtual IConnectableLayer* AddInputLayer(LayerBindingId id, const char* name = nullptr) = 0; 120 121 /// Adds an ArgMinMax layer to the network. 122 /// @param desc - Parameters for the L2 normalization operation. 123 /// @param name - Optional name for the layer. 124 /// @return - Interface for configuring the layer. 125 virtual IConnectableLayer* AddArgMinMaxLayer(const ArgMinMaxDescriptor& desc, 126 const char* name = nullptr) = 0; 127 128 /// Add a Comparison layer to the network. 129 /// @param name - Optional name for the layer. 130 /// @param desc - Descriptor for the comparison operation. 131 /// @return - Interface for configuring the layer. 132 virtual IConnectableLayer* AddComparisonLayer(const ComparisonDescriptor& comparisonDescriptor, 133 const char* name = nullptr) = 0; 134 135 /// Adds a concatenation layer to the network. 136 /// @param concatDescriptor - ConcatDescriptor (synonym for OriginsDescriptor) to configure the concatenation 137 /// process. Number of Views must be equal to the number of inputs, and their order 138 /// must match - e.g. first view corresponds to the first input, second view to the 139 /// second input, etc.... 140 /// @param name - Optional name for the layer. 141 /// @return - Interface for configuring the layer. 142 virtual IConnectableLayer* AddConcatLayer(const ConcatDescriptor& concatDescriptor, 143 const char* name = nullptr) = 0; 144 145 /// Adds a 2D convolution layer to the network. 146 /// @param convolution2dDescriptor - Description of the 2D convolution layer. 147 /// @param weights - Tensor for the weights data. 148 /// @param biases - Optional tensor for the bias data. If specified, must match the output tensor shape. 149 /// @param name - Optional name for the layer. 150 /// @return - Interface for configuring the layer. 151 virtual IConnectableLayer* AddConvolution2dLayer(const Convolution2dDescriptor& convolution2dDescriptor, 152 const ConstTensor& weights, 153 const Optional<ConstTensor>& biases, 154 const char* name = nullptr) = 0; 155 156 ARMNN_DEPRECATED_MSG("This AddConvolution2dLayer overload is deprecated") 157 virtual IConnectableLayer* AddConvolution2dLayer(const Convolution2dDescriptor& convolution2dDescriptor, 158 const ConstTensor& weights, 159 const char* name = nullptr) = 0; 160 161 ARMNN_DEPRECATED_MSG("This AddConvolution2dLayer overload is deprecated") 162 virtual IConnectableLayer* AddConvolution2dLayer(const Convolution2dDescriptor& convolution2dDescriptor, 163 const ConstTensor& weights, 164 const ConstTensor& biases, 165 const char* name = nullptr) = 0; 166 167 /// Adds a depth to space layer to the network. 168 /// @param depthToSpaceDescriptor - Parameters for the depth to space operation. 169 /// @param name - Optional name for the layer. 170 /// @return - Interface for configuring the layer. 171 virtual IConnectableLayer* AddDepthToSpaceLayer(const DepthToSpaceDescriptor& depthToSpaceDescriptor, 172 const char* name = nullptr) = 0; 173 174 /// Adds a 2D depthwise convolution layer to the network. 175 /// @param convolution2dDescriptor - Description of the 2D depthwise convolution layer. 176 /// @param weights - Tensor for the weights. Expected format: [channelMultiplier, inputChannels, height, width]. 177 /// @param biases Optional tensor for the bias data. If specified, must match the output tensor shape. 178 /// @param name - Optional name for the layer. 179 /// @return - Interface for configuring the layer. 180 virtual IConnectableLayer* AddDepthwiseConvolution2dLayer( 181 const DepthwiseConvolution2dDescriptor& convolution2dDescriptor, 182 const ConstTensor& weights, 183 const Optional<ConstTensor>& biases, 184 const char* name = nullptr) = 0; 185 186 ARMNN_DEPRECATED_MSG("This AddDepthwiseConvolution2dLayer overload is deprecated") 187 virtual IConnectableLayer* AddDepthwiseConvolution2dLayer( 188 const DepthwiseConvolution2dDescriptor& convolution2dDescriptor, 189 const ConstTensor& weights, 190 const char* name = nullptr) = 0; 191 192 ARMNN_DEPRECATED_MSG("This AddDepthwiseConvolution2dLayer overload is deprecated") 193 virtual IConnectableLayer* AddDepthwiseConvolution2dLayer( 194 const DepthwiseConvolution2dDescriptor& convolution2dDescriptor, 195 const ConstTensor& weights, 196 const ConstTensor& biases, 197 const char* name = nullptr) = 0; 198 199 /// Adds a Dequantize layer to the network. 200 /// @return - Interface for configuring the layer. 201 virtual IConnectableLayer* AddDequantizeLayer(const char* name = nullptr) = 0; 202 203 /// Adds a Detection PostProcess layer to the network. 204 /// @param descriptor - Description of the Detection PostProcess layer. 205 /// @param anchors - Tensor for anchors. 206 /// @param name - Optional name for the layer. 207 /// @return - Interface for configuring the layer. 208 virtual IConnectableLayer* AddDetectionPostProcessLayer( 209 const DetectionPostProcessDescriptor& descriptor, 210 const ConstTensor& anchors, 211 const char* name = nullptr) = 0; 212 213 /// Add an ElementwiseUnary layer to the network. 214 /// @param name - Optional name for the layer. 215 /// @param desc - Descriptor for the elementwiseUnary operation. 216 /// @return - Interface for configuring the layer. 217 virtual IConnectableLayer* AddElementwiseUnaryLayer(const ElementwiseUnaryDescriptor& elementwiseUnaryDescriptor, 218 const char* name = nullptr) = 0; 219 220 /// Add an Fill layer to the network. 221 /// @param name - Optional name for the layer. 222 /// @param fillDescriptor - Descriptor for the fill operation. 223 /// @return - Interface for configuring the layer. 224 virtual IConnectableLayer* AddFillLayer(const FillDescriptor& fillDescriptor, 225 const char* name = nullptr) = 0; 226 227 /// Adds a fully connected layer to the network. 228 /// @param fullyConnectedDescriptor - Description of the fully connected layer. 229 /// @param weights - Tensor for the weights data. 230 /// @param biases - Optional tensor for the bias data. 231 /// @param name - Optional name for the layer. 232 /// @return - Interface for configuring the layer. 233 virtual IConnectableLayer* AddFullyConnectedLayer(const FullyConnectedDescriptor& fullyConnectedDescriptor, 234 const ConstTensor& weights, 235 const Optional<ConstTensor>& biases, 236 const char* name = nullptr) = 0; 237 238 ARMNN_DEPRECATED_MSG("This AddFullyConnectedLayer overload is deprecated") 239 virtual IConnectableLayer* AddFullyConnectedLayer(const FullyConnectedDescriptor& fullyConnectedDescriptor, 240 const ConstTensor& weights, 241 const char* name = nullptr) = 0; 242 243 ARMNN_DEPRECATED_MSG("This AddFullyConnectedLayer overload is deprecated") 244 virtual IConnectableLayer* AddFullyConnectedLayer(const FullyConnectedDescriptor& fullyConnectedDescriptor, 245 const ConstTensor& weights, 246 const ConstTensor& biases, 247 const char* name = nullptr) = 0; 248 249 /// Adds a permute layer to the network. 250 /// @param permuteDescriptor - PermuteDescriptor to configure the permute. 251 /// @param name - Optional name for the layer. 252 /// @return - Interface for configuring the layer. 253 virtual IConnectableLayer* AddPermuteLayer(const PermuteDescriptor& permuteDescriptor, 254 const char* name = nullptr) = 0; 255 256 /// Adds a batch to space ND layer to the network. 257 /// @param batchToSpaceNdDescriptor - Description of the layer. 258 /// @param name - Optional name for the layer. 259 /// @return - Interface for configuring the layer. 260 virtual IConnectableLayer* AddBatchToSpaceNdLayer(const BatchToSpaceNdDescriptor& batchToSpaceNdDescriptor, 261 const char* name = nullptr) = 0; 262 263 /// Adds a pooling layer to the network. 264 /// @param pooling2dDescriptor - Pooling2dDescriptor to configure the pooling. 265 /// @param name - Optional name for the layer. 266 /// @return - Interface for configuring the layer. 267 virtual IConnectableLayer* AddPooling2dLayer(const Pooling2dDescriptor& pooling2dDescriptor, 268 const char* name = nullptr) = 0; 269 270 /// Adds an activation layer to the network. 271 /// @param activationDescriptor - ActivationDescriptor to configure the activation. 272 /// @param name - Optional name for the layer. 273 /// @return - Interface for configuring the layer. 274 virtual IConnectableLayer* AddActivationLayer(const ActivationDescriptor& activationDescriptor, 275 const char* name = nullptr) = 0; 276 277 /// Adds a normalization layer to the network. 278 /// @param normalizationDescriptor - NormalizationDescriptor to configure the normalization. 279 /// @param name - Optional name for the layer. 280 /// @return - Interface for configuring the layer. 281 virtual IConnectableLayer* AddNormalizationLayer(const NormalizationDescriptor& normalizationDescriptor, 282 const char* name = nullptr) = 0; 283 284 /// Adds a slice layer to the network. 285 /// @param sliceDescriptor - SliceDescriptor to configure the slice operation. 286 /// @param name - Optional name for the layer. 287 /// @return - Interface for configuring the layer. 288 virtual IConnectableLayer* AddSliceLayer(const SliceDescriptor& sliceDescriptor, const char* name = nullptr) = 0; 289 290 /// Adds a softmax layer to the network. 291 /// If the data type is QAsymm8, then the output quantization parameters 292 /// must have a scale of 1/256 and an offset of 0 293 /// @param softmaxDescriptor - SoftmaxDescriptor to configure the softmax. 294 /// @param name - Optional name for the layer. 295 /// @return - Interface for configuring the layer. 296 virtual IConnectableLayer* AddSoftmaxLayer(const SoftmaxDescriptor& softmaxDescriptor, 297 const char* name = nullptr) = 0; 298 299 /// Adds a splitter layer to the network. 300 /// @param splitterDescriptor - ViewsDescriptor to configure the splitting process. 301 /// Number of Views must be equal to the number of outputs, 302 /// and their order must match - e.g. first view corresponds to 303 /// the first output, second view to the second output, etc.... 304 /// @param name - Optional name for the layer. 305 /// @return - Interface for configuring the layer. 306 virtual IConnectableLayer* AddSplitterLayer(const ViewsDescriptor& splitterDescriptor, 307 const char* name = nullptr) = 0; 308 309 /// Adds a merge layer to the network. 310 /// @param name - Optional name for the layer. 311 /// @return - Interface for configuring the layer. 312 virtual IConnectableLayer* AddMergeLayer(const char* name = nullptr) = 0; 313 314 /// Adds a concat layer to the network. 315 /// @param mergerDescriptor - MergerDescriptor (synonym for OriginsDescriptor) to configure the concatenation 316 /// process. Number of Views must be equal to the number of inputs, and their order 317 /// must match - e.g. first view corresponds to the first input, second view to the 318 /// second input, etc.... 319 /// @param name - Optional name for the layer. 320 /// @return - Interface for configuring the layer. 321 ARMNN_DEPRECATED_MSG("Use AddConcatLayer instead") 322 virtual IConnectableLayer* AddMergerLayer(const MergerDescriptor& mergerDescriptor, 323 const char* name = nullptr) = 0; 324 325 /// Add absolute layer to the network. 326 /// @param name - Optional name for the layer. 327 /// @return - Interface for configuring the layer. 328 ARMNN_DEPRECATED_MSG("Use AddElementwiseUnaryLayer instead") 329 virtual IConnectableLayer* AddAbsLayer(const char* name = nullptr) = 0; 330 331 /// Adds an addition layer to the network. 332 /// @param name - Optional name for the layer. 333 /// @return - Interface for configuring the layer. 334 virtual IConnectableLayer* AddAdditionLayer(const char* name = nullptr) = 0; 335 336 /// Adds a multiplication layer to the network. 337 /// @param name - Optional name for the layer. 338 /// @return - Interface for configuring the layer. 339 virtual IConnectableLayer* AddMultiplicationLayer(const char* name = nullptr) = 0; 340 341 /// Adds a batch normalization layer to the network. 342 /// @param mean - Pre-calculated mean for each channel. 343 /// @param variance - Pre-calculated variance for each channel. 344 /// @param beta - Per-channel additive factor. 345 /// @param gamma - Per-channel multiplicative factor. 346 /// @return - Interface for configuring the layer. 347 /// @param name - Optional name for the layer. 348 virtual IConnectableLayer* AddBatchNormalizationLayer(const BatchNormalizationDescriptor& desc, 349 const ConstTensor& mean, 350 const ConstTensor& variance, 351 const ConstTensor& beta, 352 const ConstTensor& gamma, 353 const char* name = nullptr) = 0; 354 355 /// Adds a rank layer to the network. 356 /// @param name - Optional name for the layer. 357 /// @return - Interface for configuring the layer. 358 virtual IConnectableLayer* AddRankLayer(const char* name = nullptr) = 0; 359 360 /// Adds a resize bilinear layer to the network. 361 /// @param resizeDesc - Parameters for the resize operation. 362 /// @param name - Optional name for the layer. 363 /// @return - Interface for configuring the layer. 364 ARMNN_DEPRECATED_MSG("Use AddResizeLayer instead") 365 virtual IConnectableLayer* AddResizeBilinearLayer(const ResizeBilinearDescriptor& resizeDesc, 366 const char* name = nullptr) = 0; 367 368 /// Adds a resize layer to the network. 369 /// @param resizeDescriptor - Parameters for the resize operation. 370 /// @param name - Optional name for the layer. 371 /// @return - Interface for configuring the layer. 372 virtual IConnectableLayer* AddResizeLayer(const ResizeDescriptor& resizeDescriptor, 373 const char* name = nullptr) = 0; 374 375 /// Adds an instance normalization layer to the network. 376 /// @param desc - Parameters for the instance normalization operation. 377 /// @param name - Optional name for the layer. 378 /// @return - Interface for configuring the layer. 379 virtual IConnectableLayer* AddInstanceNormalizationLayer(const InstanceNormalizationDescriptor& desc, 380 const char* name = nullptr) = 0; 381 382 /// Adds an L2 normalization layer to the network. 383 /// Normalization is performed along dimension 1, but requires a 4d input. 384 /// @param desc - Parameters for the L2 normalization operation. 385 /// @param name - Optional name for the layer. 386 /// @return - Interface for configuring the layer. 387 virtual IConnectableLayer* AddL2NormalizationLayer(const L2NormalizationDescriptor& desc, 388 const char* name = nullptr) = 0; 389 390 /// Adds a log softmax layer to the network. 391 /// @param logSoftmaxDescriptor - LogSoftmaxDescriptor to configure the log softmax. 392 /// @param name - Optional name for the layer. 393 /// @return - Interface for configuring the layer. 394 virtual IConnectableLayer* AddLogSoftmaxLayer(const LogSoftmaxDescriptor& logSoftmaxDescriptor, 395 const char* name = nullptr) = 0; 396 397 /// Adds a layer with no inputs and a single output, which always corresponds to 398 /// the passed in constant tensor. 399 /// @param input - Tensor to be provided as the only output of the layer. The layer will maintain 400 /// its own copy of the tensor data, meaning the memory referenced by @a input can 401 /// be freed or reused after this function is called. 402 /// @param name - Optional name for the layer. 403 /// @return - Interface for configuring the layer. 404 virtual IConnectableLayer* AddConstantLayer(const ConstTensor& input, 405 const char* name = nullptr) = 0; 406 407 /// Adds a reshape layer to the network. 408 /// @param reshapeDescriptor - Parameters for the reshape operation. 409 /// @param name - Optional name for the layer. 410 /// @return - Interface for configuring the layer. 411 virtual IConnectableLayer* AddReshapeLayer(const ReshapeDescriptor& reshapeDescriptor, 412 const char* name = nullptr) = 0; 413 414 /// Adds a space to batch layer to the network. 415 /// @param spaceToBatchNdDescriptor - Parameters for the space to batch operation. 416 /// @param name - Optional name for the layer. 417 /// @return - Interface for configuring the layer. 418 virtual IConnectableLayer* AddSpaceToBatchNdLayer(const SpaceToBatchNdDescriptor& spaceToBatchNdDescriptor, 419 const char* name = nullptr) = 0; 420 421 /// Adds a space to depth layer to the network. 422 /// @param spaceToDepthDescriptor - Parameters for the space to depth operation. 423 /// @param name - Optional name for the layer. 424 /// @return - Interface for configuring the layer. 425 virtual IConnectableLayer* AddSpaceToDepthLayer(const SpaceToDepthDescriptor& spaceToDepthDescriptor, 426 const char* name = nullptr) = 0; 427 428 /// Adds a floor layer to the network. 429 /// @param name - Optional name for the layer. 430 /// @return - Interface for configuring the layer. 431 virtual IConnectableLayer* AddFloorLayer(const char* name = nullptr) = 0; 432 433 /// Adds an output layer to the network. 434 /// @param id - User generated id to uniquely identify a particular output. The same id needs to be specified 435 /// when passing the outputs to the IRuntime::EnqueueWorkload() function. 436 /// @param name - Optional name for the layer. 437 /// @return - Interface for configuring the layer. 438 virtual IConnectableLayer* AddOutputLayer(LayerBindingId id, const char* name = nullptr) = 0; 439 440 /// Add a Lstm layer to the network 441 /// @param descriptor - Parameters for the Lstm operation 442 /// @param params - Weights and biases for the LSTM cell 443 /// @param name - Optional name for the layer 444 /// @return - Interface for configuring the layer. 445 virtual IConnectableLayer* AddLstmLayer(const LstmDescriptor& descriptor, 446 const LstmInputParams& params, 447 const char* name = nullptr) = 0; 448 449 /// Adds a division layer to the network. 450 /// @param name - Optional name for the layer. 451 /// @return - Interface for configuring the layer. 452 virtual IConnectableLayer* AddDivisionLayer(const char* name = nullptr) = 0; 453 454 /// Adds a subtraction layer to the network. 455 /// @param name - Optional name for the layer. 456 /// @return - Interface for configuring the layer. 457 virtual IConnectableLayer* AddSubtractionLayer(const char* name = nullptr) = 0; 458 459 /// Add a Maximum layer to the network. 460 /// @param name - Optional name for the layer. 461 /// @return - Interface for configuring the layer. 462 virtual IConnectableLayer* AddMaximumLayer(const char* name = nullptr) = 0; 463 464 /// Add a Mean layer to the network. 465 /// @param meanDescriptor - Parameters for the mean operation. 466 /// @param name - Optional name for the layer. 467 /// @return - Interface for configuring the layer. 468 virtual IConnectableLayer* AddMeanLayer(const MeanDescriptor& meanDescriptor, const char* name = nullptr) = 0; 469 470 /// Adds a fully pad layer to the network. 471 /// @param paddings - n by 2 tensor, where n is the rank of the input tensor, 472 /// such that paddings[i,0] indicates the amount of padding to add in front of dimonsion i, and 473 /// paddings[i,1] indicates the amount of padding to add after the end of dimension i 474 /// @param name - Optional name for the layer. 475 /// @return - Interface for configuring the layer. 476 virtual IConnectableLayer* AddPadLayer(const PadDescriptor& padDescriptor, 477 const char* name = nullptr) = 0; 478 479 /// Add a quantize layer to the network 480 ///@param name - Optional name for the layer. 481 /// @return - Interface for configuring the layer. 482 virtual IConnectableLayer* AddQuantizeLayer(const char* name = nullptr) = 0; 483 484 /// Adds a strided slice layer to the network. 485 /// @param StridedSliceDescriptor - Parameters for the strided slice operation. 486 /// @param name - Optional name for the layer. 487 /// @return - Interface for configuring the layer. 488 virtual IConnectableLayer* AddStridedSliceLayer(const StridedSliceDescriptor& stridedSliceDescriptor, 489 const char* name = nullptr) = 0; 490 491 /// Add a Minimum layer to the network. 492 /// @param name - Optional name for the layer. 493 /// @return - Interface for configuring the layer. 494 virtual IConnectableLayer* AddMinimumLayer(const char* name = nullptr) = 0; 495 496 /// Add a Greater layer to the network. 497 /// @param name - Optional name for the layer. 498 /// @return - Interface for configuring the layer. 499 ARMNN_DEPRECATED_MSG("Use AddComparisonLayer instead") 500 virtual IConnectableLayer* AddGreaterLayer(const char* name = nullptr) = 0; 501 502 /// Add a Equal layer to the network. 503 /// @param name - Optional name for the layer. 504 /// @return - Interface for configuring the layer. 505 ARMNN_DEPRECATED_MSG("Use AddComparisonLayer instead") 506 virtual IConnectableLayer* AddEqualLayer(const char* name = nullptr) = 0; 507 508 /// Add Reciprocal of square root layer to the network. 509 /// @param name - Optional name for the layer. 510 /// @return - Interface for configuring the layer. 511 ARMNN_DEPRECATED_MSG("Use AddElementwiseUnaryLayer instead") 512 virtual IConnectableLayer* AddRsqrtLayer(const char* name = nullptr) = 0; 513 514 /// Add Gather layer to the network. 515 /// @param name - Optional name for the layer. 516 /// @return - Interface for configuring the layer. 517 ARMNN_DEPRECATED_MSG("Use AddGatherLayer with descriptor instead") 518 virtual IConnectableLayer* AddGatherLayer(const char* name = nullptr) = 0; 519 520 /// Add Gather layer to the network. 521 /// @param descriptor - Description of the gather layer. 522 /// @param name - Optional name for the layer. 523 /// @return - Interface for configuring the layer. 524 virtual IConnectableLayer* AddGatherLayer(const GatherDescriptor& descriptor, 525 const char* name = nullptr) = 0; 526 527 /// Adds a switch layer to the network. 528 /// @param name - Optional name for the layer. 529 /// @return - Interface for configuring the layer. 530 virtual IConnectableLayer* AddSwitchLayer(const char* name = nullptr) = 0; 531 532 /// Adds a PReLU layer to the network. 533 /// @param name - Optional name for the layer. 534 /// @return - Interface for configuring the layer. 535 virtual IConnectableLayer* AddPreluLayer(const char* name = nullptr) = 0; 536 537 /// Adds a 2D transpose convolution layer to the network. 538 /// @param descriptor - Description of the 2D transpose convolution layer. 539 /// @param weights - Tensor for the weights data. 540 /// @param biases - Optional tensor for the bias data. 541 /// @param name - Optional name for the layer. 542 /// @return - Interface for configuring the layer. 543 virtual IConnectableLayer* AddTransposeConvolution2dLayer(const TransposeConvolution2dDescriptor& descriptor, 544 const ConstTensor& weights, 545 const Optional<ConstTensor>& biases, 546 const char* name = nullptr) = 0; 547 548 /// Adds a transpose layer to the network. 549 /// @param transposeDescriptor - TransposeDescriptor to configure the transpose. 550 /// @param name - Optional name for the layer. 551 /// @return - Interface for configuring the layer. 552 virtual IConnectableLayer* AddTransposeLayer(const TransposeDescriptor& transposeDescriptor, 553 const char* name = nullptr) = 0; 554 555 /// Adds a stack layer to the network. 556 /// @param descriptor - Description of the stack layer. 557 /// @param name - Optional name for the layer. 558 /// @return - Interface for configuring the layer. 559 virtual IConnectableLayer* AddStackLayer(const StackDescriptor& descriptor, 560 const char* name = nullptr) = 0; 561 562 /// Add a stand-in layer for a type unknown to the Arm NN framework. 563 /// Note: Due to the nature of this layer, no validation can be performed by the framework. 564 /// Furthermore, Any model containing this layer cannot make use of dynamic tensors since the 565 /// tensor sizes cannot be inferred. 566 /// @descriptor - Descriptor for the StandIn layer. 567 /// @return - Interface for configuring the layer. 568 virtual IConnectableLayer* AddStandInLayer(const StandInDescriptor& descriptor, 569 const char* name = nullptr) = 0; 570 571 /// Add a QuantizedLstm layer to the network 572 /// @param params - The weights and biases for the Quantized LSTM cell 573 /// @param name - Optional name for the layer 574 /// @return - Interface for configuring the layer. 575 virtual IConnectableLayer* AddQuantizedLstmLayer(const QuantizedLstmInputParams& params, 576 const char* name = nullptr) = 0; 577 578 /// Add a QLstm layer to the network 579 /// @param descriptor - Parameters for the QLstm operation 580 /// @param params - Weights and biases for the layer 581 /// @param name - Optional name for the layer 582 /// @return - Interface for configuring the layer. 583 virtual IConnectableLayer* AddQLstmLayer(const QLstmDescriptor& descriptor, 584 const LstmInputParams& params, 585 const char* name = nullptr) = 0; 586 587 /// Adds a Logical Binary layer to the network. 588 /// @param descriptor - Description of the Logical Binary layer. 589 /// @param name - Optional name for the layer. 590 /// @return - Interface for configuring the layer. 591 virtual IConnectableLayer* AddLogicalBinaryLayer(const LogicalBinaryDescriptor& descriptor, 592 const char* name = nullptr) = 0; 593 594 virtual void Accept(ILayerVisitor& visitor) const = 0; 595 596 protected: ~INetwork()597 ~INetwork() {} 598 }; 599 600 using IOptimizedNetworkPtr = std::unique_ptr<IOptimizedNetwork, void(*)(IOptimizedNetwork* network)>; 601 602 class IOptimizedNetwork 603 { 604 public: 605 static void Destroy(IOptimizedNetwork* network); 606 607 virtual Status PrintGraph() = 0; 608 virtual Status SerializeToDot(std::ostream& stream) const = 0; 609 610 virtual profiling::ProfilingGuid GetGuid() const = 0; 611 612 protected: ~IOptimizedNetwork()613 ~IOptimizedNetwork() {} 614 }; 615 616 struct OptimizerOptions 617 { OptimizerOptionsarmnn::OptimizerOptions618 OptimizerOptions() 619 : m_ReduceFp32ToFp16(false) 620 , m_Debug(false) 621 , m_ReduceFp32ToBf16(false) 622 , m_shapeInferenceMethod(armnn::ShapeInferenceMethod::ValidateOnly) 623 , m_ImportEnabled(false) 624 , m_ModelOptions() 625 {} 626 OptimizerOptionsarmnn::OptimizerOptions627 OptimizerOptions(bool reduceFp32ToFp16, bool debug, bool reduceFp32ToBf16, bool importEnabled, 628 ModelOptions modelOptions = {}) 629 : m_ReduceFp32ToFp16(reduceFp32ToFp16) 630 , m_Debug(debug) 631 , m_ReduceFp32ToBf16(reduceFp32ToBf16) 632 , m_shapeInferenceMethod(armnn::ShapeInferenceMethod::ValidateOnly) 633 , m_ImportEnabled(importEnabled) 634 , m_ModelOptions(modelOptions) 635 { 636 if (m_ReduceFp32ToFp16 && m_ReduceFp32ToBf16) 637 { 638 throw InvalidArgumentException("BFloat16 and Float16 optimization cannot be enabled at the same time."); 639 } 640 } 641 OptimizerOptionsarmnn::OptimizerOptions642 OptimizerOptions(bool reduceFp32ToFp16, bool debug, bool reduceFp32ToBf16 = false, 643 ShapeInferenceMethod shapeInferenceMethod = armnn::ShapeInferenceMethod::ValidateOnly, 644 bool importEnabled = false, ModelOptions modelOptions = {}) 645 : m_ReduceFp32ToFp16(reduceFp32ToFp16) 646 , m_Debug(debug) 647 , m_ReduceFp32ToBf16(reduceFp32ToBf16) 648 , m_shapeInferenceMethod(shapeInferenceMethod) 649 , m_ImportEnabled(importEnabled) 650 , m_ModelOptions(modelOptions) 651 { 652 if (m_ReduceFp32ToFp16 && m_ReduceFp32ToBf16) 653 { 654 throw InvalidArgumentException("BFloat16 and Float16 optimization cannot be enabled at the same time."); 655 } 656 } 657 658 // Reduce Fp32 data to Fp16 for faster processing 659 bool m_ReduceFp32ToFp16; 660 661 // Add debug data for easier troubleshooting 662 bool m_Debug; 663 664 // Reduce Fp32 data to Bf16 for faster processing 665 bool m_ReduceFp32ToBf16; 666 667 // Infer output size when not available 668 ShapeInferenceMethod m_shapeInferenceMethod; 669 670 // Enable Import 671 bool m_ImportEnabled; 672 673 // Enable Model Options 674 ModelOptions m_ModelOptions; 675 }; 676 677 /// Create an optimized version of the network 678 /// @param network INetwork description of the network to be optimized. 679 /// @param backendPreferences The choice of the backend ordered by user preferences. 680 /// @param deviceSpec DeviceSpec object as queried from the runtime. See IRuntime::GetDeviceSpec() 681 /// @param messages If there are failures or warnings a string describing same will be added to the vector 682 /// @param options OptimizerOptions object with optimizer configuration options 683 /// @return An IOptimizedNetworkPtr interface to the optimized network, throws an exception derived from 684 /// armnn::Exception if process fails. 685 686 IOptimizedNetworkPtr Optimize(const INetwork& network, 687 const std::vector<BackendId>& backendPreferences, 688 const IDeviceSpec& deviceSpec, 689 const OptimizerOptions& options = OptimizerOptions(), 690 Optional<std::vector<std::string>&> messages = EmptyOptional()); 691 } //namespace armnn 692