25 #include <doctest/doctest.h> 27 using namespace armnn;
32 void CreateLSTMLayerHelper(
Graph &graph,
bool CifgEnabled)
43 unsigned int batchSize = 3;
44 unsigned int inputSize = 2;
45 unsigned int numUnits = 4;
46 unsigned int outputSize = 4;
77 if (!layerDesc.m_CifgEnabled)
90 if (layerDesc.m_ProjectionEnabled)
100 if (layerDesc.m_PeepholeEnabled)
102 if (!layerDesc.m_CifgEnabled)
129 armnn::TensorInfo lstmTensorInfoScratchBuff({ batchSize, numUnits * (layerDesc.m_CifgEnabled ? 3 : 4) },
132 Connect(input, layer, lstmTensorInfo1, 0, 0);
133 Connect(cellStateIn, layer, lstmTensorInfo2, 0, 1);
134 Connect(outputStateIn, layer, lstmTensorInfo3, 0, 2);
135 Connect(layer, scratchBuffer, lstmTensorInfoScratchBuff, 0, 0);
136 Connect(layer, outputStateOut, lstmTensorInfo3, 1, 0);
137 Connect(layer, cellStateOut, lstmTensorInfo2, 2, 0);
138 Connect(layer, output, lstmTensorInfo3, 3, 0);
166 template <
typename NamePolicy>
171 m_BackendCapabilities(NamePolicy::GetIdStatic(), {{
"NullCapability",
false}}),
172 m_CustomAllocator(
false) {};
174 m_BackendCapabilities(capabilities),
175 m_CustomAllocator(false) {};
180 return NamePolicy::GetIdStatic();
184 return GetIdStatic();
205 return std::make_shared<MockLayerSupport>();
215 return m_BackendCapabilities;
218 virtual bool UseCustomMemoryAllocator(std::shared_ptr<ICustomAllocator> allocator,
222 m_CustomAllocator =
true;
223 return m_CustomAllocator;
227 bool m_CustomAllocator;
230 template <
typename NamePolicy>
234 NoProtectedModeMockBackend() : m_BackendCapabilities(NamePolicy::GetIdStatic(), {{
"NullCapability",
false}}) {};
235 NoProtectedModeMockBackend(
const BackendCapabilities& capabilities) : m_BackendCapabilities(capabilities) {};
236 ~NoProtectedModeMockBackend() =
default;
240 return NamePolicy::GetIdStatic();
244 return GetIdStatic();
265 return std::make_shared<MockLayerSupport>();
275 return m_BackendCapabilities;
287 TEST_CASE(
"LSTMValidateTensorShapesFromInputsCIFGDisabledTest")
292 CreateLSTMLayerHelper(graph,
false);
298 TEST_CASE(
"LSTMValidateTensorShapesFromInputsCIFGEnabledTest")
303 CreateLSTMLayerHelper(graph,
true);
309 TEST_CASE(
"InsertConvertersTest")
323 ->GetOutputHandler().SetTensorInfo(
info);
332 ->GetOutputHandler().SetTensorInfo(
info);
337 &IsLayerOfType<armnn::InputLayer>,
338 &IsLayerOfType<armnn::InputLayer>,
339 &IsLayerOfType<armnn::MemCopyLayer>,
340 &IsLayerOfType<armnn::FloorLayer>,
341 &IsLayerOfType<armnn::AdditionLayer>,
342 &IsLayerOfType<armnn::OutputLayer>));
345 for (
auto& layer : graph)
355 for (
auto& layer : graph)
365 for (
auto& layer : graph)
387 &IsLayerOfType<armnn::InputLayer>,
388 &IsLayerOfType<armnn::InputLayer>,
389 &IsLayerOfType<armnn::ConvertFp16ToFp32Layer>,
390 &IsLayerOfType<armnn::MemCopyLayer>,
391 &IsLayerOfType<armnn::ConvertFp16ToFp32Layer>,
392 &IsLayerOfType<armnn::FloorLayer>,
393 &IsLayerOfType<armnn::ConvertFp32ToFp16Layer>,
394 &IsLayerOfType<armnn::ConvertFp16ToFp32Layer>,
395 &IsLayerOfType<armnn::AdditionLayer>,
396 &IsLayerOfType<armnn::ConvertFp32ToFp16Layer>,
397 &IsLayerOfType<armnn::OutputLayer>));
400 void CreateConvolution2dGraph(
Graph &graph,
const unsigned int* inputShape,
401 const unsigned int* weightsShape,
const unsigned int* outputShape,
407 std::vector<float> weightsVector(90);
422 layer->
m_Weight = std::make_unique<armnn::ScopedTensorHandle>(weights);
430 TEST_CASE(
"Conv2dValidateTensorShapesFromInputs")
433 const unsigned int inputShape[] = { 1, 3, 8, 16 };
434 const unsigned int weightsShape[] = { 2, 3, 5, 3 };
435 const unsigned int outputShape[] = { 1, 2, 4, 14 };
436 CreateConvolution2dGraph(graph, inputShape, weightsShape, outputShape);
441 TEST_CASE(
"Conv2dValidateTensorShapesFromInputsNhwc")
444 const unsigned int inputShape[] = { 1, 8, 16, 3 };
445 const unsigned int weightsShape[] = { 2, 5, 3, 3 };
446 const unsigned int outputShape[] = { 1, 4, 14, 2 };
447 CreateConvolution2dGraph(graph, inputShape, weightsShape, outputShape,
DataLayout::NHWC);
452 void CreateDepthwiseConvolution2dGraph(
Graph &graph,
const unsigned int* inputShape,
453 const unsigned int* weightsShape,
const unsigned int* outputShape,
459 std::vector<float> weightsVector(18);
474 layer->
m_Weight = std::make_unique<armnn::ScopedTensorHandle>(weights);
482 TEST_CASE(
"DepthwiseConv2dValidateTensorShapesFromInputs")
485 const unsigned int inputShape[] = { 1, 2, 3, 3 };
486 const unsigned int weightsShape[] = { 1, 3, 3, 2 };
487 const unsigned int outputShape[] = { 1, 2, 1, 1 };
488 CreateDepthwiseConvolution2dGraph(graph, inputShape, weightsShape, outputShape);
493 TEST_CASE(
"DepthwiseConv2dValidateTensorShapesFromInputsNhwc")
496 const unsigned int inputShape[] = { 1, 3, 3, 2 };
497 const unsigned int weightsShape[] = { 1, 3, 3, 2 };
498 const unsigned int outputShape[] = { 1, 1, 1, 2 };
499 CreateDepthwiseConvolution2dGraph(graph, inputShape, weightsShape, outputShape,
DataLayout::NHWC);
504 void CreatePooling2dGraph(
Graph& graph,
const unsigned int* inputShape,
const unsigned int* outputShape,
532 TEST_CASE(
"Pooling2dValidateTensorShapesFromInputs")
535 const unsigned int inputShape[] = { 5, 3, 52, 60 };
536 const unsigned int outputShape[] = { 5, 3, 11, 13 };
542 TEST_CASE(
"Pooling2dValidateTensorShapesFromInputsNhwc")
545 const unsigned int inputShape[] = { 5, 52, 60, 3 };
546 const unsigned int outputShape[] = { 5, 11, 13, 3 };
552 void CreateResizeBilinearGraph(
Graph& graph,
553 const unsigned int* inputShape,
554 const unsigned int* outputShape,
577 TEST_CASE(
"ResizeBilinearValidateTensorShapesFromInputs")
580 const unsigned int inputShape[] = { 1, 2, 4, 5 };
581 const unsigned int outputShape[] = { 1, 2, 3, 4 };
582 CreateResizeBilinearGraph(graph, inputShape, outputShape);
587 TEST_CASE(
"ResizeBilinearValidateTensorShapesFromInputsNhwc")
590 const unsigned int inputShape[] = { 1, 4, 5, 2 };
591 const unsigned int outputShape[] = { 1, 3, 4, 2 };
592 CreateResizeBilinearGraph(graph, inputShape, outputShape,
DataLayout::NHWC);
597 void CreateGatherGraph(
Graph& graph,
618 TEST_CASE(
"GatherValidateTensorShapesFromInputs")
625 CreateGatherGraph(graph, paramsInfo, indicesInfo, outputInfo);
630 TEST_CASE(
"GatherValidateTensorShapesFromInputs1DParams")
637 CreateGatherGraph(graph, paramsInfo, indicesInfo, outputInfo);
642 TEST_CASE(
"GatherValidateTensorShapesFromInputsMultiDimIndices")
649 CreateGatherGraph(graph, paramsInfo, indicesInfo, outputInfo);
654 TEST_CASE(
"DetectionPostProcessValidateTensorShapes")
659 std::vector<uint8_t> anchorsVector(40);
677 layer->
m_Anchors = std::make_unique<armnn::ScopedTensorHandle>(anchors);
683 input0->GetOutputSlot().Connect(layer->
GetInputSlot(0));
689 TEST_CASE(
"BackendCapabilityTest")
703 TEST_CASE(
"BackendHintTest")
705 class TestBackendAssignment :
public StrategyBase<NoThrowStrategy>
711 const std::vector<armnn::ConstTensor>& constants,
720 auto inputLayer = PolymorphicDowncast<const InputLayer*>(layer);
721 const auto connectedLayerBackendId = inputLayer->GetOutputSlot(0).GetOwningLayer().GetBackendId();
722 CHECK((inputLayer->GetBackendId() == connectedLayerBackendId));
727 auto outputLayer = PolymorphicDowncast<const OutputLayer*>(layer);
728 CHECK((outputLayer->GetBackendId() ==
"MockBackend"));
733 auto activation = PolymorphicDowncast<const ActivationLayer*>(layer);
734 CHECK((activation->GetBackendId() ==
"CustomBackend"));
765 backendRegistry.Register(
"MockBackend", []() {
return std::make_unique<MockBackend<MockPolicy>>(); });
767 backendRegistry.Register(
"CustomBackend", []() {
return std::make_unique<MockBackend<CustomPolicy>>(); });
774 std::unique_ptr<Graph> graph = std::make_unique<Graph>();
775 auto input = graph->AddLayer<
InputLayer>(0,
"input");
777 auto output = graph->AddLayer<
OutputLayer>(0,
"output");
779 BackendId customBackendId(
"CustomBackend");
790 std::vector<BackendId> prefs{
"MockBackend",
"CustomBackend" };
792 BackendIdSet availableBackends = {
"CustomBackend",
"MockBackend" };
810 TestBackendAssignment visitor;
811 for (
auto it = firstLayer; it != lastLayer; ++it)
813 (*it)->ExecuteStrategy(visitor);
816 backendRegistry.Deregister(
"MockBackend");
817 backendRegistry.Deregister(
"CustomBackend");
821 TEST_CASE(
"OptimizeForExclusiveConnectionsFuseTest")
823 using namespace armnn;
831 const unsigned int inputDimensionSizes[] = { 1, 4, 4, 3 };
832 const unsigned int weightsDimensionSizes[] = { 1, 2, 2, 3 };
833 const unsigned int outputDimensionSizes[] = { 1, 3, 3, 1 };
834 const unsigned int outputChannelSize[] = { outputDimensionSizes[3] };
839 std::vector<float> weightsVector = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
842 std::vector<float> betaVector = { 0.1f };
843 std::vector<float> gammaVector = { 0.5f };
844 std::vector<float> meanVector = { 0 };
845 std::vector<float> varianceVector = { 1 };
862 conv->m_Weight = std::make_unique<ScopedTensorHandle>(weights);
863 batchNorm->m_Beta = std::make_unique<ScopedTensorHandle>(beta);
864 batchNorm->m_Gamma = std::make_unique<ScopedTensorHandle>(gamma);
865 batchNorm->m_Mean = std::make_unique<ScopedTensorHandle>(mean);
866 batchNorm->m_Variance = std::make_unique<ScopedTensorHandle>(variance);
867 if (convolution2dDescriptor.m_BiasEnabled)
869 std::vector<float> biasVector = { 11 };
871 conv->m_Bias = std::make_unique<ScopedTensorHandle>(bias);
881 &IsLayerOfType<InputLayer>,
882 &IsLayerOfType<Convolution2dLayer>,
883 &IsLayerOfType<BatchNormalizationLayer>,
884 &IsLayerOfType<OutputLayer>));
889 auto checkFusedConv2d = [](
const armnn::Layer*
const layer) ->
bool {
890 return IsLayerOfType<armnn::Convolution2dLayer>(layer) &&
891 (layer->
GetNameStr() ==
"fused-batchNorm-into-convolution");
896 &IsLayerOfType<InputLayer>,
898 &IsLayerOfType<OutputLayer>));
902 TEST_CASE(
"OptimizeForExclusiveConnectionsWithoutFuseTest")
923 &IsLayerOfType<armnn::InputLayer>,
924 &IsLayerOfType<armnn::Convolution2dLayer>,
925 &IsLayerOfType<armnn::BatchNormalizationLayer>,
926 &IsLayerOfType<armnn::OutputLayer>,
927 &IsLayerOfType<armnn::OutputLayer>));
933 &IsLayerOfType<armnn::InputLayer>,
934 &IsLayerOfType<armnn::Convolution2dLayer>,
935 &IsLayerOfType<armnn::BatchNormalizationLayer>,
936 &IsLayerOfType<armnn::OutputLayer>,
937 &IsLayerOfType<armnn::OutputLayer>));
TEST_SUITE("TestConstTensorLayerVisitor")
std::shared_ptr< ConstTensorHandle > m_ForgetGateBias
A unique pointer to represent 1D weights tensor with dimensions [num_units].
Iterator begin()
Returns iterator pointing to the beginning of the list. Lowercase for range-based for loops...
std::shared_ptr< ConstTensorHandle > m_OutputGateBias
A unique pointer to represent 1D weights tensor with dimensions [num_units].
bool m_BiasEnabled
Enable/disable bias.
bool m_ProjectionEnabled
Enable/disable the projection layer.
DataLayout m_DataLayout
The data layout to be used (NCHW, NHWC).
OptimizationResult AssignBackends(OptimizedNetworkImpl *optNetObjPtr, BackendSettings &backendSettings, Graph::Iterator &firstLayer, Graph::Iterator &lastLayer, Optional< std::vector< std::string > &> errMessages)
bool HasCapability(const std::string &name, const BackendCapabilities &capabilities)
Convenience function to check if a capability exists in a BackendCapabilites struct.
LstmBasicParameters m_BasicParameters
This layer represents a batch normalization operation.
std::unique_ptr< IWorkloadFactory > IWorkloadFactoryPtr
Interface for a layer that is connectable to other layers via InputSlots and OutputSlots.
uint32_t m_PadBottom
Padding bottom value in the height dimension.
std::vector< ConvertFp32ToFp16Layer * > InsertConvertFp32ToFp16LayersAfter(Graph &graph, Layer &layer)
bool m_BiasEnabled
Enable/disable bias.
std::shared_ptr< ConstTensorHandle > m_CellToForgetWeights
A unique pointer to represent 1D weights tensor with dimensions [num_units].
std::vector< ConvertFp16ToFp32Layer * > InsertConvertFp16ToFp32LayersBefore(Graph &graph, Layer &layer, bool expectCorrectInputType)
uint32_t m_PadLeft
Padding left value in the width dimension.
Optimizer::Optimizations MakeOptimizations(Args &&... args)
bool IsOutputSupported(const TensorInfo &, Optional< std::string &>) const override
float m_ClippingThresProj
Clipping threshold value for the projection.
std::unordered_set< BackendId > BackendIdSet
void BackendSelectionHint(Optional< BackendId > backend) final
Provide a hint for the optimizer as to which backend to prefer for this layer.
DataLayout m_DataLayout
The data layout to be used (NCHW, NHWC).
Optional< const BackendOptions::BackendOption > GetCapability(const std::string &backendCapabilityName, const BackendCapabilities &capabilities)
Returns a BackendCapability if the backend lists the capability The BackendCapability must then be in...
This layer represents a depthwise convolution 2d operation.
LayerT * AddLayer(Args &&... args)
Adds a new layer, of type LayerType, to the graph constructed with the arguments passed.
uint32_t m_PoolWidth
Pooling width value.
ConstIterator cbegin() const
Returns const iterator pointing to the beginning of the list. Lowercase for range-based for loops...
A Convolution2dDescriptor for the Convolution2dLayer.
int Connect(InputSlot &destination)
ResizeMethod m_Method
The Interpolation method to use (Bilinear, NearestNeighbor).
bool IsActivationSupported(const TensorInfo &input, const TensorInfo &output, const ActivationDescriptor &descriptor, Optional< std::string &> reasonIfUnsupported=EmptyOptional()) const override
static void Pass(Graph &graph, const Optimizations &optimizations)
The padding fields don't count and are ignored.
bool IsInputSupported(const TensorInfo &, Optional< std::string &>) const override
PaddingMethod m_PaddingMethod
The padding method to be used. (Exclude, IgnoreValue).
DataLayout m_DataLayout
The data layout to be used (NCHW, NHWC).
OptimizeForExclusiveConnection< Convolution2dLayer, BatchNormalizationLayer, FuseBatchNorm< Convolution2dLayer, armnn::DataType::Float32 > > FuseBatchNormIntoConvolution2DFloat32
std::shared_ptr< ConstTensorHandle > m_Weight
A unique pointer to store Weight values.
This layer represents an activation operation with the specified activation function.
BackendRegistry & BackendRegistryInstance()
uint32_t m_PadTop
Padding top value in the height dimension.
std::shared_ptr< ConstTensorHandle > m_ProjectionWeights
A unique pointer to represent 2D weights tensor with dimensions [output_size, num_units].
This layer represents a detection postprocess operator.
Copyright (c) 2021 ARM Limited and Contributors.
std::shared_ptr< ConstTensorHandle > m_InputToCellWeights
A unique pointer to represent 2D weights tensor with dimensions [input_size, num_units].
std::unique_ptr< IMemoryManager > IMemoryManagerUniquePtr
This layer represents a LSTM operation.
void IgnoreUnused(Ts &&...)
LayerList::const_iterator Iterator
uint32_t m_StrideX
Stride value when proceeding through input for the width dimension.
int LayerBindingId
Type of identifiers for bindable layers (inputs, outputs).
A ResizeBilinearDescriptor for the ResizeBilinearLayer.
std::shared_ptr< ConstTensorHandle > m_InputGateBias
A unique pointer to represent 1D weights tensor with dimensions [num_units].
Base class for all descriptors.
Strategy base class with empty implementations.
std::shared_ptr< ConstTensorHandle > m_CellToOutputWeights
A unique pointer to represent 1D weights tensor with dimensions [num_units].
The SubgraphView class represents a subgraph of a Graph.
uint32_t m_PoolHeight
Pooling height value.
uint32_t m_MaxDetections
Maximum numbers of detections.
const InputSlot & GetInputSlot(unsigned int index) const override
Get a const input slot handle by slot index.
uint32_t m_StrideX
Stride value when proceeding through input for the width dimension.
std::shared_ptr< ConstTensorHandle > m_RecurrentToCellWeights
A unique pointer to represent 2D weights tensor with dimensions [output_size, num_units].
uint32_t m_StrideX
Stride value when proceeding through input for the width dimension.
A layer user-provided data can be bound to (e.g. inputs, outputs).
std::shared_ptr< ConstTensorHandle > m_CellBias
A unique pointer to represent 1D weights tensor with dimensions [num_units].
This layer represents a Gather operator.
An LstmDescriptor for the LstmLayer.
uint32_t m_PadRight
Padding right value in the width dimension.
std::shared_ptr< IMemoryManager > IMemoryManagerSharedPtr
DataType GetDataType() const
A tensor defined by a TensorInfo (shape and data type) and an immutable backing store.
const std::string & GetNameStr() const
LayerType GetType() const override
Returns the armnn::LayerType of this layer.
uint32_t m_TargetWidth
Target width value.
A GatherDescriptor for the GatherLayer.
std::shared_ptr< ConstTensorHandle > m_RecurrentToInputWeights
A unique pointer to represent 2D weights tensor with dimensions [input_size, num_units].
bool m_PeepholeEnabled
Enable/disable peephole.
This layer represents a memory copy operation.
#define ARMNN_ASSERT(COND)
std::shared_ptr< ConstTensorHandle > m_RecurrentToOutputWeights
A unique pointer to represent 2D weights tensor with dimensions [output_size, num_units].
std::shared_ptr< ConstTensorHandle > m_InputToInputWeights
A unique pointer to represent 2D weights tensor with dimensions [input_size, num_units].
An ActivationDescriptor for the ActivationLayer.
This layer represents a floor operation.
std::shared_ptr< ConstTensorHandle > m_ProjectionBias
A unique pointer to represent 1D weights tensor with dimensions [output_size].
uint32_t m_TargetHeight
Target height value.
uint32_t m_ActivationFunc
The activation function to use.
uint32_t m_StrideY
Stride value when proceeding through input for the height dimension.
virtual LayerType GetType() const =0
Returns the armnn::LayerType of this layer.
This layer represents a pooling 2d operation.
float m_ClippingThresCell
Clipping threshold value for the cell state.
DataLayout m_DataLayout
The data layout to be used (NCHW, NHWC).
This layer represents an addition operation.
LstmOptPeepholeParameters m_PeepholeParameters
void SetTensorInfo(const TensorInfo &tensorInfo)
Sets the TensorInfo used by this output handler.
std::shared_ptr< ILayerSupport > ILayerSupportSharedPtr
Struct for the users to pass backend specific options.
LstmOptProjectionParameters m_ProjectionParameters
bool m_CifgEnabled
Enable/disable cifg (coupled input & forget gate).
EmptyOptional is used to initialize the Optional class in case we want to have default value for an O...
std::shared_ptr< ConstTensorHandle > m_InputToForgetWeights
A unique pointer to represent 2D weights tensor with dimensions [input_size, num_units].
PoolingAlgorithm m_PoolType
The pooling algorithm to use (Max. Average, L2).
uint32_t m_StrideY
Stride value when proceeding through input for the height dimension.
std::shared_ptr< ConstTensorHandle > m_RecurrentToForgetWeights
A unique pointer to represent 2D weights tensor with dimensions [output_size, num_units].
std::shared_ptr< ConstTensorHandle > m_Weight
A unique pointer to store Weight values.
const OutputHandler & GetOutputHandler(unsigned int i=0) const
Iterator end()
Returns iterator pointing to the end of the list. Lowercase for range-based for loops.
bool CheckSequence(const armnn::Graph::ConstIterator first, const armnn::Graph::ConstIterator last)
void SetTensorInfo(const TensorInfo &tensorInfo) override
DataType GetDataType() const
const OutputSlot & GetOutputSlot(unsigned int index=0) const override
Get the const output slot handle by slot index.
LstmOptCifgParameters m_CifgParameters
ConstIterator cend() const
Returns const iterator pointing to the end of the list. Lowercase for range-based for loops...
This layer represents a convolution 2d operation.
void Connect(armnn::IConnectableLayer *from, armnn::IConnectableLayer *to, const armnn::TensorInfo &tensorInfo, unsigned int fromIndex, unsigned int toIndex)
A Pooling2dDescriptor for the Pooling2dLayer.
std::shared_ptr< ConstTensorHandle > m_CellToInputWeights
A unique pointer to represent 1D weights tensor with dimensions [num_units].
size_t GetNumLayers() const
DataLayout m_DataLayout
The data layout to be used (NCHW, NHWC).
std::shared_ptr< ConstTensorHandle > m_Anchors
A unique pointer to store Anchor values.
const TensorInfo & GetTensorInfo() const override
static INetworkPtr Create(NetworkOptions networkOptions={})
const char * GetLayerTypeAsCString(LayerType type)
ActivationFunction m_Function
The activation function to use (Sigmoid, TanH, Linear, ReLu, BoundedReLu, SoftReLu, LeakyReLu, Abs, Sqrt, Square, Elu).
uint32_t m_StrideY
Stride value when proceeding through input for the height dimension.
A DepthwiseConvolution2dDescriptor for the DepthwiseConvolution2dLayer.
A BatchNormalizationDescriptor for the BatchNormalizationLayer.
This layer represents a resize operation.
std::shared_ptr< ConstTensorHandle > m_InputToOutputWeights
A unique pointer to represent 2D weights tensor with dimensions [input_size, num_units].
std::unique_ptr< IBackendContext > IBackendContextPtr