diff options
Diffstat (limited to 'src/armnnTfParser/TfParser.cpp')
-rw-r--r-- | src/armnnTfParser/TfParser.cpp | 141 |
1 files changed, 64 insertions, 77 deletions
diff --git a/src/armnnTfParser/TfParser.cpp b/src/armnnTfParser/TfParser.cpp index 210b825e43..3c7d16e7b4 100644 --- a/src/armnnTfParser/TfParser.cpp +++ b/src/armnnTfParser/TfParser.cpp @@ -48,37 +48,6 @@ namespace const PermutationVector NHWCToArmNN = { 0, 2, 3, 1 }; const PermutationVector ArmNNToNHWC = { 0, 3, 1, 2 }; -IConnectableLayer* AddSwizzleLayer(INetwork& network, IOutputSlot& input, const PermutationVector& mapping, - const std::string& name) -{ - // Adds swizzle layer. - IConnectableLayer* const layer = network.AddPermuteLayer(mapping, name.c_str()); - - // Connects intput to swizzle layer. - input.Connect(layer->GetInputSlot(0)); - - // Sets up swizzled output. - const TensorInfo outInfo = armnnUtils::Permuted(input.GetTensorInfo(), mapping); - layer->GetOutputSlot(0).SetTensorInfo(outInfo); - - return layer; -} - -IConnectableLayer* SwizzleInDeswizzleOut(INetwork& network, IOutputSlot& input, IConnectableLayer& layer, - const std::string& name) -{ - // Adds swizzle layer. - IConnectableLayer* const swizzleLayer = AddSwizzleLayer(network, input, NHWCToArmNN, "swizzle_for-" + name); - - // Connects swizzledInput to layer. - swizzleLayer->GetOutputSlot(0).Connect(layer.GetInputSlot(0)); - - // Adds deswizzle layer. - IConnectableLayer* const deswizzleLayer = AddSwizzleLayer(network, layer.GetOutputSlot(0), ArmNNToNHWC, - "deswizzle_for-" + name); - - return deswizzleLayer; -} template <typename Callable> void ReadMandatoryNodeAttributeImpl(const tensorflow::NodeDef& nodeDef, @@ -1181,10 +1150,10 @@ ParsedTfOperationPtr TfParser::ParseDepthwiseConv2D(const tensorflow::NodeDef& n % nodeDef.name() % CHECK_LOCATION().AsString())); } + ParsedConstTfOperation<float>* weightNode = boost::polymorphic_downcast<ParsedConstTfOperation<float> *>(inputs[1].m_IndexedValue); - std::string paddingString = ReadMandatoryNodeStringAttribute(nodeDef, "padding"); std::string dataFormat = ReadMandatoryNodeStringAttribute(nodeDef, "data_format"); std::vector<uint32_t> strides = ReadMandatoryNodeUint32ListAttribute(nodeDef, "strides"); @@ -1194,59 +1163,85 @@ ParsedTfOperationPtr TfParser::ParseDepthwiseConv2D(const tensorflow::NodeDef& n CHECK_DATA_FORMAT(nodeDef, dataFormat, "DepthwiseConv2dNative"); - if (dataFormat == "NHWC") - { - desc.m_StrideX = strides[2]; - desc.m_StrideY = strides[1]; - // Swizzles input to supported memory layout. - inputTensorInfo = armnnUtils::Permuted(inputSlot.GetTensorInfo(), NHWCToArmNN); - } - else if (dataFormat == "NCHW") - { - desc.m_StrideX = strides[3]; - desc.m_StrideY = strides[2]; - } + DataLayout dataLayout = dataFormat == "NHWC" ? DataLayout::NHWC : DataLayout::NCHW; - uint32_t inputHeight = inputTensorInfo.GetShape()[2]; - uint32_t inputWidth = inputTensorInfo.GetShape()[3]; + desc.m_DataLayout = dataLayout; - std::vector<float> outputTensorData; + DataLayoutIndexed dataLayoutIndexed(dataLayout); + + desc.m_StrideX = strides[dataLayoutIndexed.GetWidthIndex()]; + desc.m_StrideY = strides[dataLayoutIndexed.GetHeightIndex()]; + + uint32_t inputHeight = inputTensorInfo.GetShape()[dataLayoutIndexed.GetHeightIndex()]; + uint32_t inputWidth = inputTensorInfo.GetShape()[dataLayoutIndexed.GetWidthIndex()]; + + // Mappings from TensorFlow filter tensors to the ArmNN filter tensors. + // Tensorflow weights are [H, W, In, Out]. + // ArmNN weights have to be [Out, H, W, In] when the data layout is NHWC, + // and [Out, In, H, W] when the data layout is NCHW. + PermutationVector permutationVector = + dataLayout == DataLayout::NHWC ? + std::initializer_list<unsigned int>{ 1, 2, 3, 0 } : // NHWC: [H, W, In, Out] -> [Out, H, W, In] + std::initializer_list<unsigned int>{ 2, 3, 1, 0 }; // NCHW: [H, W, In, Out] -> [Out, In, H, W] + + // Swizzle the tensor using the given permutation vector. + const TensorInfo& weightTensorInfo = weightNode->GetTensorInfo(); + const TensorInfo weightTensorSwizzledInfo = armnnUtils::Permuted(weightTensorInfo, permutationVector); + + // Swizzles the content of the tensor's permanent storage into a local storage. + std::vector<float> weightTensorSwizzledData(weightTensorInfo.GetNumElements()); + armnnUtils::Permute(weightTensorSwizzledInfo.GetShape(), permutationVector, + weightNode->GetStorage(), weightTensorSwizzledData.data()); - ConstTensor weightTensor = weightNode->GetConstTensor(true, outputTensorData); + // Create a weight tensor with the newly swizzled data. + ConstTensor weightTensor(weightTensorSwizzledInfo, weightTensorSwizzledData); - uint32_t weightHeight = weightTensor.GetShape()[2]; - uint32_t weightWidth = weightTensor.GetShape()[3]; + uint32_t weightHeight = weightTensor.GetShape()[dataLayoutIndexed.GetHeightIndex()]; + uint32_t weightWidth = weightTensor.GetShape()[dataLayoutIndexed.GetWidthIndex()]; bool padding = false; TensorInfo outputInfo; + unsigned int outputHeight = 0; + unsigned int outputWidth = 0; CHECK_PADDING_TYPE(nodeDef, paddingString); if (paddingString == "SAME") { padding = true; - outputInfo = TensorInfo({ inputTensorInfo.GetShape()[0], - weightTensor.GetShape()[0] * weightTensor.GetShape()[1], - static_cast<uint32_t>(ceil( - static_cast<float>(inputHeight) / - static_cast<float>(desc.m_StrideY))), - static_cast<uint32_t>(ceil( - static_cast<float>(inputWidth) / - static_cast<float>(desc.m_StrideX))) - }, DataType::Float32); + + outputHeight = static_cast<uint32_t>(ceil(static_cast<float>(inputHeight) / + static_cast<float>(desc.m_StrideY))); + outputWidth = static_cast<uint32_t>(ceil(static_cast<float>(inputWidth) / + static_cast<float>(desc.m_StrideX))); } else if (paddingString == "VALID") { padding = false; - outputInfo = TensorInfo({ inputTensorInfo.GetShape()[0], - weightTensor.GetShape()[0] * weightTensor.GetShape()[1], - static_cast<uint32_t>(ceil( - static_cast<float>(inputHeight - weightHeight + 1) / - static_cast<float>(desc.m_StrideY))), - static_cast<uint32_t>(ceil( - static_cast<float>(inputWidth - weightWidth + 1) / - static_cast<float>(desc.m_StrideX))) - }, DataType::Float32); + + outputHeight = static_cast<uint32_t>(ceil(static_cast<float>(inputHeight - weightHeight + 1) / + static_cast<float>(desc.m_StrideY))); + outputWidth = static_cast<uint32_t>(ceil(static_cast<float>(inputWidth - weightWidth + 1) / + static_cast<float>(desc.m_StrideX))); + } + + switch (dataLayout) + { + case DataLayout::NHWC: + outputInfo = TensorInfo({ inputTensorInfo.GetShape()[0], + outputHeight, + outputWidth, + weightTensor.GetShape()[0] * weightTensor.GetShape()[3]}, + DataType::Float32); + break; + case DataLayout::NCHW: + default: + outputInfo = TensorInfo({ inputTensorInfo.GetShape()[0], + weightTensor.GetShape()[0] * weightTensor.GetShape()[1], + outputHeight, + outputWidth }, + DataType::Float32); + break; } CalcPadding(inputHeight, weightHeight, desc.m_StrideY, desc.m_PadTop, desc.m_PadBottom, padding); @@ -1254,15 +1249,7 @@ ParsedTfOperationPtr TfParser::ParseDepthwiseConv2D(const tensorflow::NodeDef& n IConnectableLayer* layer = m_Network->AddDepthwiseConvolution2dLayer(desc, weightTensor, nodeDef.name().c_str()); layer->GetOutputSlot(0).SetTensorInfo(outputInfo); - - if (dataFormat == "NHWC") - { - layer = SwizzleInDeswizzleOut(*m_Network, inputSlot, *layer, nodeDef.name()); - } - else - { - inputSlot.Connect(layer->GetInputSlot(0)); - } + inputSlot.Connect(layer->GetInputSlot(0)); return std::make_unique<SingleLayerParsedTfOperation>(this, nodeDef, layer); } |