From b20d1d4888c270d4d57a0bdcc011ded89a2f5b38 Mon Sep 17 00:00:00 2001 From: Matthew Sloyan Date: Mon, 9 Aug 2021 15:33:41 +0100 Subject: IVGCVSW-6119 ConstTensorsAsInput: FullyConnected Bug Fix * Updated FullyConnected layer member variables when cloning as some backends still require them. * Added SetConstant call when using deprecated AddFullyConnectedLayer method to ensure backwards compatibility. * Added SetConstant to SimpleSample to ensure it runs on all backends. Signed-off-by: Matthew Sloyan Change-Id: Ie7b4e4b868f23f8fcf9c41ffd12e2ea9ea53afca --- samples/SimpleSample.cpp | 3 ++- src/armnn/BackendHelper.cpp | 6 ++++++ src/armnn/Network.cpp | 12 ++++++++++-- src/armnn/layers/FullyConnectedLayer.cpp | 5 +++++ 4 files changed, 23 insertions(+), 3 deletions(-) diff --git a/samples/SimpleSample.cpp b/samples/SimpleSample.cpp index 6bdc2983bb..3f94b53ca1 100644 --- a/samples/SimpleSample.cpp +++ b/samples/SimpleSample.cpp @@ -29,6 +29,7 @@ int main() float weightsData[] = {1.0f}; // Identity TensorInfo weightsInfo(TensorShape({1, 1}), DataType::Float32); + weightsInfo.SetConstant(); ConstTensor weights(weightsInfo, weightsData); // Constant layer that now holds weights data for FullyConnected @@ -54,7 +55,7 @@ int main() TensorInfo outputTensorInfo(TensorShape({1, 1}), DataType::Float32); fullyConnectedLayer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); - constantWeightsLayer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); + constantWeightsLayer->GetOutputSlot(0).SetTensorInfo(weightsInfo); // Optimise ArmNN network IOptimizedNetworkPtr optNet = Optimize(*myNetwork, {Compute::CpuRef}, run->GetDeviceSpec()); diff --git a/src/armnn/BackendHelper.cpp b/src/armnn/BackendHelper.cpp index 9ab30f8fb2..594d76916c 100644 --- a/src/armnn/BackendHelper.cpp +++ b/src/armnn/BackendHelper.cpp @@ -407,12 +407,18 @@ bool LayerSupportHandle::IsFullyConnectedSupported(const TensorInfo& input, { if(!weights.IsConstant()) { + reasonIfUnsupported.value() = + "This backend might not support non constant weights. " + "If weights are constant make sure to set IsConstant when creating TensorInfo"; return false; } if(descriptor.m_BiasEnabled) { if(!biases.IsConstant()) { + reasonIfUnsupported.value() = + "This backend might not support non constant bias. " + "If bias are constant make sure to set IsConstant when creating TensorInfo"; return false; } } diff --git a/src/armnn/Network.cpp b/src/armnn/Network.cpp index 22a71c4923..365f1bdfa1 100644 --- a/src/armnn/Network.cpp +++ b/src/armnn/Network.cpp @@ -1805,7 +1805,11 @@ IConnectableLayer* NetworkImpl::AddFullyConnectedLayer(const FullyConnectedDescr { weightsLayer = m_Graph->AddLayer("Weights"); weightsLayer->m_LayerOutput = std::make_shared(weights.value()); - weightsLayer->GetOutputSlot(0).SetTensorInfo(weightsLayer->m_LayerOutput->GetTensorInfo()); + + TensorInfo weightsInfo = weightsLayer->m_LayerOutput->GetTensorInfo(); + weightsInfo.SetConstant(); + + weightsLayer->GetOutputSlot(0).SetTensorInfo(weightsInfo); } else if (fullyConnectedDescriptor.m_ConstantWeights) { @@ -1817,7 +1821,11 @@ IConnectableLayer* NetworkImpl::AddFullyConnectedLayer(const FullyConnectedDescr { biasLayer = m_Graph->AddLayer("Biases"); biasLayer->m_LayerOutput = std::make_shared(biases.value()); - biasLayer->GetOutputSlot(0).SetTensorInfo(biasLayer->m_LayerOutput->GetTensorInfo()); + + TensorInfo biasInfo = biasLayer->m_LayerOutput->GetTensorInfo(); + biasInfo.SetConstant(); + + biasLayer->GetOutputSlot(0).SetTensorInfo(biasInfo); } if (numInputs < 2) diff --git a/src/armnn/layers/FullyConnectedLayer.cpp b/src/armnn/layers/FullyConnectedLayer.cpp index 8dfb011730..259d4149c8 100644 --- a/src/armnn/layers/FullyConnectedLayer.cpp +++ b/src/armnn/layers/FullyConnectedLayer.cpp @@ -38,6 +38,11 @@ std::unique_ptr FullyConnectedLayer::CreateWorkload(const IWorkloadFa FullyConnectedLayer* FullyConnectedLayer::Clone(Graph& graph) const { auto layer = CloneBase(graph, m_Param, GetName()); + layer->m_Weight = m_Weight ? m_Weight : nullptr; + if (layer->m_Param.m_BiasEnabled) + { + layer->m_Bias = m_Bias ? m_Bias : nullptr; + } return std::move(layer); } -- cgit v1.2.1