diff options
author | Matthew Sloyan <matthew.sloyan@arm.com> | 2021-07-13 19:46:11 +0100 |
---|---|---|
committer | Matthew Sloyan <matthew.sloyan@arm.com> | 2021-08-06 09:25:26 +0000 |
commit | 81beae3a870004795275e9266bc43d845b9f78db (patch) | |
tree | 70af86f3c36c8e330c72770e6f1419ca7b2a4bb8 /delegate/src | |
parent | 95e9efc28ce70a8cda93e722f5ce90ebc96bdd95 (diff) | |
download | armnn-81beae3a870004795275e9266bc43d845b9f78db.tar.gz |
IVGCVSW-6119 ConstTensorsAsInput: FullyConnected
* Constant weights and biases are now stored as Constant layers.
* Updated Serializer, Deserializer and unit tests to reflect this.
* Updated TfLiteDelegate, TfLiteParser and OnnxParser.
* Updated Schema with IsConstant and ConstantTensorsAsInputs.
* Updated Ref backend to handle constant weights and
bias as inputs rather than reading from member variables.
* Added dynamic or constant input EndToEnd tests.
!android-nn-driver:5959
Signed-off-by: Matthew Sloyan <matthew.sloyan@arm.com>
Change-Id: Ibf3cf437df1100e4b322b0d303c575c6339f9696
Diffstat (limited to 'delegate/src')
-rw-r--r-- | delegate/src/DelegateUtils.hpp | 5 | ||||
-rw-r--r-- | delegate/src/FullyConnected.hpp | 43 | ||||
-rw-r--r-- | delegate/src/test/FullyConnectedTest.cpp | 10 |
3 files changed, 37 insertions, 21 deletions
diff --git a/delegate/src/DelegateUtils.hpp b/delegate/src/DelegateUtils.hpp index b04baac36e..2d1651842a 100644 --- a/delegate/src/DelegateUtils.hpp +++ b/delegate/src/DelegateUtils.hpp @@ -482,6 +482,11 @@ armnn::ConstTensor CreateConstTensor(const TfLiteTensor* tfLiteTensor, "TfLiteArmnnDelegate: Not constant allocation type: " + std::to_string(tfLiteTensor->allocation_type)); } + if(tflite::IsConstantTensor(tfLiteTensor)) + { + tensorInfo.SetConstant(); + } + if (permutationVector.has_value() && permutationVector.value().GetSize() > 0 && permutationData != nullptr) { // Permute tensor info diff --git a/delegate/src/FullyConnected.hpp b/delegate/src/FullyConnected.hpp index e94304fb21..49686d6eaf 100644 --- a/delegate/src/FullyConnected.hpp +++ b/delegate/src/FullyConnected.hpp @@ -130,30 +130,39 @@ TfLiteStatus VisitFullyConnectedOperator(DelegateData& delegateData, return isSupported ? kTfLiteOk : kTfLiteError; } - armnn::Optional<armnn::ConstTensor> optionalWeights = armnn::EmptyOptional(); - armnn::Optional<armnn::ConstTensor> optionalBiases = armnn::EmptyOptional(); - if(descriptor.m_ConstantWeights) + armnn::IConnectableLayer* layer = delegateData.m_Network->AddFullyConnectedLayer(descriptor); + ARMNN_ASSERT(layer != nullptr); + + // Add a constant layer for weights and biases if inputs are constant. + if (isConstantWeights) { auto weightsTensor = CreateConstTensor(&tfLiteWeightsTensor, weightsTensorInfo, armnn::Optional<armnn::PermutationVector&>()); - optionalWeights = armnn::Optional<armnn::ConstTensor>(weightsTensor); - if (biasEnabled) + armnn::IConnectableLayer* weightsLayer = delegateData.m_Network->AddConstantLayer(weightsTensor); + + weightsLayer->GetOutputSlot(0).Connect(layer->GetInputSlot(1u)); + weightsLayer->GetOutputSlot(0).SetTensorInfo(weightsTensorInfo); + } + + if (biasEnabled) + { + const TfLiteTensor& tfLiteBiasTensor = tfLiteTensors[tfLiteNode->inputs->data[2]]; + if(tflite::IsConstantTensor(&tfLiteBiasTensor)) { - const TfLiteTensor& tfLiteBiasTensor = tfLiteTensors[tfLiteNode->inputs->data[2]]; auto biasTensor = CreateConstTensor(&tfLiteBiasTensor, biasTensorInfo, armnn::Optional<armnn::PermutationVector&>()); - optionalBiases = armnn::Optional<armnn::ConstTensor>(biasTensor); + + armnn::IConnectableLayer* biasLayer = delegateData.m_Network->AddConstantLayer(biasTensor); + ARMNN_ASSERT(biasLayer != nullptr); + + biasLayer->GetOutputSlot(0).Connect(layer->GetInputSlot(2u)); + biasLayer->GetOutputSlot(0).SetTensorInfo(biasTensorInfo); } } - armnn::IConnectableLayer* layer = delegateData.m_Network->AddFullyConnectedLayer(descriptor, - optionalWeights, - optionalBiases); - ARMNN_ASSERT(layer != nullptr); - armnn::IOutputSlot& outputSlot = layer->GetOutputSlot(0); outputSlot.SetTensorInfo(outputTensorInfo); @@ -171,13 +180,15 @@ TfLiteStatus VisitFullyConnectedOperator(DelegateData& delegateData, // Connect delegateData.m_OutputSlotForNode[tfLiteNode->inputs->data[0]]->Connect(reshapeLayer->GetInputSlot(0)); reshapeLayer->GetOutputSlot(0).Connect(layer->GetInputSlot(0)); + if (!descriptor.m_ConstantWeights) { delegateData.m_OutputSlotForNode[tfLiteNode->inputs->data[1]]->Connect(layer->GetInputSlot(1)); - if (biasEnabled) - { - delegateData.m_OutputSlotForNode[tfLiteNode->inputs->data[2]]->Connect(layer->GetInputSlot(2)); - } + } + + if (biasEnabled && !tflite::IsConstantTensor(&tfLiteTensors[tfLiteNode->inputs->data[2]])) + { + delegateData.m_OutputSlotForNode[tfLiteNode->inputs->data[2]]->Connect(layer->GetInputSlot(2)); } delegateData.m_OutputSlotForNode[tfLiteNode->outputs->data[0]] = &outputSlot; } diff --git a/delegate/src/test/FullyConnectedTest.cpp b/delegate/src/test/FullyConnectedTest.cpp index 3bea250988..c300bc72bf 100644 --- a/delegate/src/test/FullyConnectedTest.cpp +++ b/delegate/src/test/FullyConnectedTest.cpp @@ -34,7 +34,7 @@ void FullyConnectedFp32Test(std::vector<armnn::BackendId>& backends, bool consta constantWeights); } -void FullyConnectedActicationTest(std::vector<armnn::BackendId>& backends, bool constantWeights = true) +void FullyConnectedActivationTest(std::vector<armnn::BackendId>& backends, bool constantWeights = true) { std::vector<int32_t> inputTensorShape { 1, 4, 1, 1 }; std::vector<int32_t> weightsTensorShape { 1, 4 }; @@ -106,7 +106,7 @@ TEST_CASE ("FullyConnected_Int8_GpuAcc_Test") TEST_CASE ("FullyConnected_Activation_GpuAcc_Test") { std::vector<armnn::BackendId> backends = { armnn::Compute::GpuAcc }; - FullyConnectedActicationTest(backends); + FullyConnectedActivationTest(backends); } } // End of TEST_SUITE("FullyConnected_GpuAccTests") @@ -129,7 +129,7 @@ TEST_CASE ("FullyConnected_Int8_CpuAcc_Test") TEST_CASE ("FullyConnected_Activation_CpuAcc_Test") { std::vector<armnn::BackendId> backends = { armnn::Compute::CpuAcc }; - FullyConnectedActicationTest(backends); + FullyConnectedActivationTest(backends); } } // End of TEST_SUITE("FullyConnected_CpuAccTests") @@ -152,7 +152,7 @@ TEST_CASE ("FullyConnected_Int8_CpuRef_Test") TEST_CASE ("FullyConnected_Activation_CpuRef_Test") { std::vector<armnn::BackendId> backends = { armnn::Compute::CpuRef }; - FullyConnectedActicationTest(backends); + FullyConnectedActivationTest(backends); } TEST_CASE ("FullyConnected_Weights_As_Inputs_FP32_CpuRef_Test") @@ -170,7 +170,7 @@ TEST_CASE ("FullyConnected_Weights_As_Inputs_Int8_CpuRef_Test") TEST_CASE ("FullyConnected_Weights_As_Inputs_Activation_CpuRef_Test") { std::vector<armnn::BackendId> backends = { armnn::Compute::CpuRef }; - FullyConnectedActicationTest(backends, false); + FullyConnectedActivationTest(backends, false); } } // End of TEST_SUITE("FullyConnected_CpuRefTests") |