From 2e24175c683bca42496104591d6b702dad360b8e Mon Sep 17 00:00:00 2001 From: Nikhil Raj Date: Tue, 1 Feb 2022 16:42:15 +0000 Subject: IVGCVSW-6724 Accessing ConstTensors from IConnectableLayer Signed-off-by: Nikhil Raj Change-Id: I01f42a520d15c6dabd2f77c7715c91b8f7026476 --- delegate/src/MultiLayerFacade.hpp | 5 +++++ include/armnn/INetwork.hpp | 6 ++++++ src/armnn/Layer.hpp | 4 ++-- src/armnn/layers/BatchNormalizationLayer.cpp | 1 + src/armnn/layers/ConstantLayer.hpp | 1 + src/armnn/layers/Convolution2dLayer.cpp | 1 + src/armnn/layers/DepthwiseConvolution2dLayer.cpp | 1 + src/armnn/layers/DetectionPostProcessLayer.cpp | 1 + src/armnn/layers/FullyConnectedLayer.cpp | 1 + src/armnn/layers/LstmLayer.cpp | 1 + src/armnn/layers/QLstmLayer.cpp | 1 + src/armnn/layers/QuantizedLstmLayer.cpp | 1 + src/armnn/layers/TransposeConvolution2dLayer.cpp | 1 + .../layers/UnidirectionalSequenceLstmLayer.cpp | 1 + src/armnn/test/GraphTests.cpp | 25 ++++++++++++++++++++++ src/armnn/test/InferOutputTests.hpp | 2 +- 16 files changed, 50 insertions(+), 3 deletions(-) diff --git a/delegate/src/MultiLayerFacade.hpp b/delegate/src/MultiLayerFacade.hpp index ccd011a669..02be26cefd 100644 --- a/delegate/src/MultiLayerFacade.hpp +++ b/delegate/src/MultiLayerFacade.hpp @@ -131,6 +131,11 @@ public: virtual const armnn::BaseDescriptor& GetParameters() const override { return m_NullDescriptor; } +protected: + /// Retrieve the handles to the constant values stored by the layer. + /// @return A vector of the constant tensors stored by this layer. + ConstantTensors GetConstantTensorsByRef() override { return {}; } + private: armnn::IConnectableLayer* m_FirstLayer; armnn::IConnectableLayer* m_LastLayer; diff --git a/include/armnn/INetwork.hpp b/include/armnn/INetwork.hpp index 073f119ef4..6a2193cbc6 100644 --- a/include/armnn/INetwork.hpp +++ b/include/armnn/INetwork.hpp @@ -13,6 +13,7 @@ #include #include #include +#include #include #include @@ -119,6 +120,11 @@ public: /// the BaseDescriptor IsNull function is invoked. virtual const BaseDescriptor& GetParameters() const = 0; + using ConstantTensors = std::vector>>; + + // Returns ConstantTensors of this Layer if it has any, otherwise returns empty vector. + virtual ConstantTensors GetConstantTensorsByRef() = 0; + protected: /// Objects are not deletable via the handle ~IConnectableLayer() {} diff --git a/src/armnn/Layer.hpp b/src/armnn/Layer.hpp index ecfa1d9a5b..23aa86a414 100644 --- a/src/armnn/Layer.hpp +++ b/src/armnn/Layer.hpp @@ -394,8 +394,8 @@ protected: LayerType* CloneBase(Graph& graph, Params&& ... params) const; // Retrieve the Handles to the constants - using ConstantTensors = std::vector>>; - virtual ConstantTensors GetConstantTensorsByRef() {return ConstantTensors(); }; + // Marking this as override and having this here keeps IConnectable abstract with only pure virtual function + virtual ConstantTensors GetConstantTensorsByRef() override {return ConstantTensors(); }; // "Blob" AdditionalInfoObjectPtr m_AdditionalInfoObject; diff --git a/src/armnn/layers/BatchNormalizationLayer.cpp b/src/armnn/layers/BatchNormalizationLayer.cpp index 18d167f8cb..15a42dd46a 100644 --- a/src/armnn/layers/BatchNormalizationLayer.cpp +++ b/src/armnn/layers/BatchNormalizationLayer.cpp @@ -67,6 +67,7 @@ void BatchNormalizationLayer::ValidateTensorShapesFromInputs() Layer::ConstantTensors BatchNormalizationLayer::GetConstantTensorsByRef() { + // For API stability DO NOT ALTER order and add new members to the end of vector return {m_Mean, m_Variance, m_Beta, m_Gamma}; } diff --git a/src/armnn/layers/ConstantLayer.hpp b/src/armnn/layers/ConstantLayer.hpp index a9a9d37f54..d3dd8cf47a 100644 --- a/src/armnn/layers/ConstantLayer.hpp +++ b/src/armnn/layers/ConstantLayer.hpp @@ -56,6 +56,7 @@ protected: ~ConstantLayer() = default; /// Retrieve the handles to the constant values stored by the layer. + // For API stability DO NOT ALTER order and add new members to the end of vector ConstantTensors GetConstantTensorsByRef() override { return {m_LayerOutput}; } }; diff --git a/src/armnn/layers/Convolution2dLayer.cpp b/src/armnn/layers/Convolution2dLayer.cpp index 68e1cb5339..ef5db8e9b9 100644 --- a/src/armnn/layers/Convolution2dLayer.cpp +++ b/src/armnn/layers/Convolution2dLayer.cpp @@ -140,6 +140,7 @@ void Convolution2dLayer::ValidateTensorShapesFromInputs() Layer::ConstantTensors Convolution2dLayer::GetConstantTensorsByRef() { + // For API stability DO NOT ALTER order and add new members to the end of vector return {m_Weight, m_Bias}; } diff --git a/src/armnn/layers/DepthwiseConvolution2dLayer.cpp b/src/armnn/layers/DepthwiseConvolution2dLayer.cpp index db14e22b29..b23661b4a8 100644 --- a/src/armnn/layers/DepthwiseConvolution2dLayer.cpp +++ b/src/armnn/layers/DepthwiseConvolution2dLayer.cpp @@ -145,6 +145,7 @@ void DepthwiseConvolution2dLayer::ValidateTensorShapesFromInputs() Layer::ConstantTensors DepthwiseConvolution2dLayer::GetConstantTensorsByRef() { + // For API stability DO NOT ALTER order and add new members to the end of vector return {m_Weight, m_Bias}; } diff --git a/src/armnn/layers/DetectionPostProcessLayer.cpp b/src/armnn/layers/DetectionPostProcessLayer.cpp index 833ef43597..58f261cc05 100644 --- a/src/armnn/layers/DetectionPostProcessLayer.cpp +++ b/src/armnn/layers/DetectionPostProcessLayer.cpp @@ -75,6 +75,7 @@ void DetectionPostProcessLayer::ValidateTensorShapesFromInputs() Layer::ConstantTensors DetectionPostProcessLayer::GetConstantTensorsByRef() { + // For API stability DO NOT ALTER order and add new members to the end of vector return { m_Anchors }; } diff --git a/src/armnn/layers/FullyConnectedLayer.cpp b/src/armnn/layers/FullyConnectedLayer.cpp index 6a9c3b07e4..b1ae974cd6 100644 --- a/src/armnn/layers/FullyConnectedLayer.cpp +++ b/src/armnn/layers/FullyConnectedLayer.cpp @@ -77,6 +77,7 @@ void FullyConnectedLayer::ValidateTensorShapesFromInputs() Layer::ConstantTensors FullyConnectedLayer::GetConstantTensorsByRef() { + // For API stability DO NOT ALTER order and add new members to the end of vector return {m_Weight, m_Bias}; } diff --git a/src/armnn/layers/LstmLayer.cpp b/src/armnn/layers/LstmLayer.cpp index 46c7574cf8..06e5e8e5d0 100644 --- a/src/armnn/layers/LstmLayer.cpp +++ b/src/armnn/layers/LstmLayer.cpp @@ -269,6 +269,7 @@ void LstmLayer::ValidateTensorShapesFromInputs() Layer::ConstantTensors LstmLayer::GetConstantTensorsByRef() { + // For API stability DO NOT ALTER order and add new members to the end of vector return {m_BasicParameters.m_InputToForgetWeights, m_BasicParameters.m_InputToCellWeights, m_BasicParameters.m_InputToOutputWeights, diff --git a/src/armnn/layers/QLstmLayer.cpp b/src/armnn/layers/QLstmLayer.cpp index 17031fa112..eb33227b48 100644 --- a/src/armnn/layers/QLstmLayer.cpp +++ b/src/armnn/layers/QLstmLayer.cpp @@ -271,6 +271,7 @@ void QLstmLayer::ValidateTensorShapesFromInputs() Layer::ConstantTensors QLstmLayer::GetConstantTensorsByRef() { + // For API stability DO NOT ALTER order and add new members to the end of vector return {m_BasicParameters.m_InputToForgetWeights, m_BasicParameters.m_InputToCellWeights, m_BasicParameters.m_InputToOutputWeights, diff --git a/src/armnn/layers/QuantizedLstmLayer.cpp b/src/armnn/layers/QuantizedLstmLayer.cpp index 7fd39f14b1..e9b9d1c6b9 100644 --- a/src/armnn/layers/QuantizedLstmLayer.cpp +++ b/src/armnn/layers/QuantizedLstmLayer.cpp @@ -150,6 +150,7 @@ void QuantizedLstmLayer::ValidateTensorShapesFromInputs() Layer::ConstantTensors QuantizedLstmLayer::GetConstantTensorsByRef() { + // For API stability DO NOT ALTER order and add new members to the end of vector return { m_QuantizedLstmParameters.m_InputToInputWeights, diff --git a/src/armnn/layers/TransposeConvolution2dLayer.cpp b/src/armnn/layers/TransposeConvolution2dLayer.cpp index a1f07f9eca..1cbaf342cd 100644 --- a/src/armnn/layers/TransposeConvolution2dLayer.cpp +++ b/src/armnn/layers/TransposeConvolution2dLayer.cpp @@ -118,6 +118,7 @@ void TransposeConvolution2dLayer::ValidateTensorShapesFromInputs() Layer::ConstantTensors TransposeConvolution2dLayer::GetConstantTensorsByRef() { + // For API stability DO NOT ALTER order and add new members to the end of vector return {m_Weight, m_Bias}; } diff --git a/src/armnn/layers/UnidirectionalSequenceLstmLayer.cpp b/src/armnn/layers/UnidirectionalSequenceLstmLayer.cpp index c9aaa8c171..199961449e 100644 --- a/src/armnn/layers/UnidirectionalSequenceLstmLayer.cpp +++ b/src/armnn/layers/UnidirectionalSequenceLstmLayer.cpp @@ -276,6 +276,7 @@ void UnidirectionalSequenceLstmLayer::ValidateTensorShapesFromInputs() Layer::ConstantTensors UnidirectionalSequenceLstmLayer::GetConstantTensorsByRef() { + // For API stability DO NOT ALTER order and add new members to the end of vector return {m_BasicParameters.m_InputToForgetWeights, m_BasicParameters.m_InputToCellWeights, m_BasicParameters.m_InputToOutputWeights, diff --git a/src/armnn/test/GraphTests.cpp b/src/armnn/test/GraphTests.cpp index 6b3e611017..d3dd499850 100644 --- a/src/armnn/test/GraphTests.cpp +++ b/src/armnn/test/GraphTests.cpp @@ -614,4 +614,29 @@ TEST_CASE("CheckGraphConstTensorSharing") CHECK(*sharedWeightPtr == 1); } +TEST_CASE("IConnectableLayerConstantTensorsByRef") +{ + using namespace armnn; + INetworkPtr net(INetwork::Create()); + + std::vector falseData = {3}; + ConstTensor falseTensor(TensorInfo({1}, DataType::Boolean, 0.0f, 0, true), falseData); + IConnectableLayer* constLayer = net->AddConstantLayer(falseTensor, "const"); + constLayer->GetOutputSlot(0).SetTensorInfo(TensorInfo({1, 1, 1, 1}, DataType::Boolean)); + + const TensorInfo& constInfo = constLayer->GetOutputSlot(0).GetTensorInfo(); + + const void* weightData = constLayer->GetConstantTensorsByRef()[0].get()->GetConstTensor(); + auto weightValue = reinterpret_cast(weightData); + CHECK(weightValue[0] == 3); + TensorInfo weightsInfo = constInfo; + ConstTensor weights(weightsInfo, weightData); + DepthwiseConvolution2dDescriptor desc; + const auto depthwiseLayer = net->AddDepthwiseConvolution2dLayer(desc, weights, EmptyOptional(), "Depthwise"); + + const void* resultData = depthwiseLayer->GetConstantTensorsByRef()[0].get()->GetConstTensor(); + auto resultValue = reinterpret_cast(resultData); + CHECK(resultValue[0] == 3); +} + } diff --git a/src/armnn/test/InferOutputTests.hpp b/src/armnn/test/InferOutputTests.hpp index 799739b9ef..7ae2aa72b4 100644 --- a/src/armnn/test/InferOutputTests.hpp +++ b/src/armnn/test/InferOutputTests.hpp @@ -700,4 +700,4 @@ void QuantizedLstmInferOutputShapeTest() CHECK(actualOutShapes.size() == 2); CHECK(expectedOutShapes[0] == actualOutShapes[0]); CHECK(expectedOutShapes[1] == actualOutShapes[1]); -} +} \ No newline at end of file -- cgit v1.2.1