2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // See LICENSE file in the project root for full license information.
5 #include "TfParser.hpp"
7 #include <armnn/INetwork.hpp>
8 #include <armnn/Utils.hpp>
9 #include <armnn/TypesUtils.hpp>
10 #include <armnn/Exceptions.hpp>
11 #include <armnn/Descriptors.hpp>
13 #include <GraphTopologicalSort.hpp>
14 #include <Permute.hpp>
16 #include <google/protobuf/io/zero_copy_stream_impl.h>
17 #include <google/protobuf/text_format.h>
19 #include "tensorflow/core/framework/graph.pb.h"
20 #include "tensorflow/core/framework/node_def.pb.h"
21 #include "tensorflow/core/framework/types.pb.h"
22 #include "tensorflow/core/framework/tensor.pb.h"
23 #include "tensorflow/core/framework/tensor_shape.pb.h"
25 #include <boost/assert.hpp>
26 #include <boost/format.hpp>
27 #include <boost/core/ignore_unused.hpp>
28 #include <boost/log/trivial.hpp>
29 #include <boost/numeric/conversion/cast.hpp>
30 #include <boost/polymorphic_cast.hpp>
37 using namespace armnn;
39 namespace armnnTfParser
44 const PermutationVector NHWCToArmNN = { 0, 2, 3, 1 };
45 const PermutationVector ArmNNToNHWC = { 0, 3, 1, 2 };
47 IConnectableLayer* AddSwizzleLayer(INetwork& network, IOutputSlot& input, const PermutationVector& mapping,
48 const std::string& name)
51 IConnectableLayer* const layer = network.AddPermuteLayer(mapping, name.c_str());
53 // Connect intput to swizzle layer
54 input.Connect(layer->GetInputSlot(0));
56 // Setup swizzled output
57 const TensorInfo outInfo = armnnUtils::Permuted(input.GetTensorInfo(), mapping);
58 layer->GetOutputSlot(0).SetTensorInfo(outInfo);
63 IConnectableLayer* SwizzleInDeswizzleOut(INetwork& network, IOutputSlot& input, IConnectableLayer& layer,
64 const std::string& name)
67 IConnectableLayer* const swizzleLayer = AddSwizzleLayer(network, input, NHWCToArmNN, "swizzle_for-" + name);
69 // Connect swizzledInput to layer
70 swizzleLayer->GetOutputSlot(0).Connect(layer.GetInputSlot(0));
72 // Add deswizzle layer
73 IConnectableLayer* const deswizzleLayer = AddSwizzleLayer(network, layer.GetOutputSlot(0), ArmNNToNHWC,
74 "deswizzle_for-" + name);
76 return deswizzleLayer;
79 template <typename Callable>
80 void ReadMandatoryNodeAttributeImpl(const tensorflow::NodeDef& nodeDef,
81 const std::string& attribName,
82 tensorflow::AttrValue::ValueCase expectedValueCase,
85 auto iter = nodeDef.attr().find(attribName);
86 if (iter != nodeDef.attr().end())
88 const auto& attrValue = iter->second;
89 if (attrValue.value_case() == expectedValueCase)
95 throw ParseException(boost::str(boost::format(
96 "Attribute %1% of node %2% expected to have %3% as tensorflow::AttrValue::ValueCase, "
97 "but found %4% instead")
100 % static_cast<int>(expectedValueCase)
101 % static_cast<int>(attrValue.value_case())));
106 throw ParseException(boost::str(boost::format("Could not find required attribute %1% in node %2%")
107 % attribName % nodeDef.name()));
111 template <typename Callable>
112 void ReadOptionalNodeAttributeImpl(const tensorflow::NodeDef& nodeDef,
113 const std::string& attribName,
114 tensorflow::AttrValue::ValueCase expectedValueCase,
117 auto iter = nodeDef.attr().find(attribName);
118 if (iter != nodeDef.attr().end())
120 const auto& attrValue = iter->second;
121 if (attrValue.value_case() == expectedValueCase)
127 throw ParseException(boost::str(boost::format(
128 "Attribute %1% of node %2% expected to have %3% as tensorflow::AttrValue::ValueCase, "
129 "but found %4% instead")
132 % static_cast<int>(expectedValueCase)
133 % static_cast<int>(attrValue.value_case())));
138 float ReadMandatoryNodeFloatAttribute(const tensorflow::NodeDef& nodeDef, const std::string& name)
140 float attribValue = 0.0f;
141 ReadMandatoryNodeAttributeImpl(nodeDef, name, tensorflow::AttrValue::kF,
142 [&attribValue](const tensorflow::AttrValue& attrValue)
144 attribValue = attrValue.f();
149 uint32_t ReadMandatoryNodeUint32Attribute(const tensorflow::NodeDef& nodeDef, const std::string& name)
151 uint32_t attribValue = 0u;
152 ReadMandatoryNodeAttributeImpl(nodeDef, name, tensorflow::AttrValue::kI,
153 [&attribValue](const tensorflow::AttrValue& attrValue)
155 attribValue = static_cast<uint32_t>(attrValue.i());
160 std::string ReadMandatoryNodeStringAttribute(const tensorflow::NodeDef& nodeDef, const std::string& name)
162 std::string attribValue = "";
163 ReadMandatoryNodeAttributeImpl(nodeDef, name, tensorflow::AttrValue::kS,
164 [&attribValue](const tensorflow::AttrValue& attrValue)
166 attribValue = attrValue.s();
171 std::vector<uint32_t> ReadMandatoryNodeUint32ListAttribute(const tensorflow::NodeDef& nodeDef,
172 const std::string& name)
174 std::vector<uint32_t> attriList;
175 ReadMandatoryNodeAttributeImpl(nodeDef, name, tensorflow::AttrValue::kList,
176 [&attriList](const tensorflow::AttrValue& attrValue)
178 for (int attriNum = 0; attriNum < attrValue.list().i_size(); ++attriNum)
180 attriList.push_back(static_cast<uint32_t>(attrValue.list().i(attriNum)));
187 std::vector<uint32_t> ReadOptionalNodeUint32ListAttribute(const tensorflow::NodeDef& nodeDef,
188 const std::string& name)
190 std::vector<uint32_t> attriList;
191 ReadOptionalNodeAttributeImpl(nodeDef, name, tensorflow::AttrValue::kList,
192 [&attriList](const tensorflow::AttrValue& attrValue)
194 for (int attriNum = 0; attriNum < attrValue.list().i_size(); ++attriNum)
196 attriList.push_back(static_cast<uint32_t>(attrValue.list().i(attriNum)));
203 bool ReadOptionalNodeBoolAttribute(const tensorflow::NodeDef& nodeDef,
204 const std::string& name,
205 bool defaultValue = false)
207 bool attribValue = defaultValue;
208 ReadOptionalNodeAttributeImpl(nodeDef, name, tensorflow::AttrValue::kB,
209 [&attribValue](const tensorflow::AttrValue& attrValue)
211 attribValue = attrValue.b();
216 tensorflow::DataType ReadMandatoryNodeTypeAttribute(const tensorflow::NodeDef& nodeDef, const std::string& name)
218 tensorflow::DataType attribValue = tensorflow::DT_INVALID;
219 ReadMandatoryNodeAttributeImpl(nodeDef, name, tensorflow::AttrValue::kType,
220 [&attribValue](const tensorflow::AttrValue& attrValue)
222 attribValue = attrValue.type();
227 TensorInfo PrepareReshape(const TensorInfo& input, const std::vector<int32_t>& targetDims)
229 std::vector<unsigned int> outDims(targetDims.begin(), targetDims.end());
230 const auto stretchDim = std::find(targetDims.begin(), targetDims.end(), -1);
232 if (stretchDim != targetDims.end())
234 if (std::find(std::next(stretchDim), targetDims.end(), -1) != targetDims.end())
236 throw ParseException("At most one component of shape can be -1");
239 auto targetNumElements = boost::numeric_cast<unsigned int>(std::accumulate(targetDims.begin(), targetDims.end(),
240 -1, std::multiplies<int32_t>()));
241 auto stretchIndex = static_cast<size_t>(std::distance(targetDims.begin(), stretchDim));
242 outDims[stretchIndex] = input.GetNumElements() / targetNumElements;
245 TensorInfo reshapeInfo = input;
246 reshapeInfo.SetShape(TensorShape{ static_cast<unsigned int>(outDims.size()), outDims.data() });
251 // We need the input0Slot to guide the reshape for input1Slot
252 IOutputSlot* BroadcastForAddandMul(IOutputSlot* input0Slot, IOutputSlot* input1Slot, bool isNHWC, INetwork& m_Network,
253 const tensorflow::NodeDef& nodeDef)
255 const TensorInfo& input1Info = input1Slot->GetTensorInfo();
256 const TensorInfo inputTensorInfo = input0Slot->GetTensorInfo();
257 const unsigned int matchDim = inputTensorInfo.GetNumDimensions() - (isNHWC ? 1 : 3);
258 std::array<unsigned int, MaxNumOfTensorDimensions> reshapedDimensions;
259 std::fill_n(reshapedDimensions.begin(), inputTensorInfo.GetNumDimensions(), 1);
260 reshapedDimensions[matchDim] = input1Info.GetShape()[0];
262 armnn::TensorInfo reshapedInfo = input1Info;
263 reshapedInfo.SetShape(TensorShape{ inputTensorInfo.GetNumDimensions(), reshapedDimensions.data() });
265 const std::string reshapeLayerName = "reshape_for-" + nodeDef.name();
266 ReshapeDescriptor reshapeDesc;
267 reshapeDesc.m_TargetShape = reshapedInfo.GetShape();
268 IConnectableLayer* const reshapeLayer = m_Network.AddReshapeLayer(reshapeDesc, reshapeLayerName.c_str());
270 input1Slot->Connect(reshapeLayer->GetInputSlot(0));
271 reshapeLayer->GetOutputSlot(0).SetTensorInfo(reshapedInfo);
273 input1Slot = &reshapeLayer->GetOutputSlot(0);
278 OutputId ParseOutputId(const std::string & name)
280 unsigned int outputNum = 0;
281 size_t colonPos = name.find_last_of(":");
282 if (colonPos != std::string::npos)
284 int n = std::stoi(name.substr(colonPos+1));
287 throw ParseException("Output tensor id is out of range for "+name);
289 outputNum = static_cast<unsigned int>(n);
291 return OutputId(name.substr(0,colonPos),outputNum);
296 const std::map<std::string, TfParser::OperationParsingFunction> TfParser::ms_OperationNameToParsingFunctions = {
297 { "Const", &TfParser::ParseConst },
298 { "Add", &TfParser::ParseAdd },
299 { "BiasAdd", &TfParser::ParseBiasAdd },
300 { "Identity", &TfParser::ParseIdentity },
301 { "Conv2D", &TfParser::ParseConv2D },
302 { "DepthwiseConv2dNative", &TfParser::ParseDepthwiseConv2D },
303 { "FusedBatchNorm", &TfParser::ParseFusedBatchNorm },
304 { "ConcatV2", &TfParser::ParseConcat },
305 { "LRN", &TfParser::ParseLrn },
306 { "MatMul", &TfParser::ParseMatMul },
307 { "Mul", &TfParser::ParseMul },
308 { "Placeholder", &TfParser::ParsePlaceholder },
309 { "Relu", &TfParser::ParseRelu },
310 { "Relu6", &TfParser::ParseRelu6 },
311 { "Reshape", &TfParser::ParseReshape },
312 { "ResizeBilinear", &TfParser::ParseResizeBilinear },
313 { "Shape", &TfParser::ParseShape },
314 { "Squeeze", &TfParser::ParseSqueeze },
315 { "Sigmoid", &TfParser::ParseSigmoid },
316 { "Softmax", &TfParser::ParseSoftmax },
317 { "Softplus", &TfParser::ParseSoftplus },
318 { "Tanh", &TfParser::ParseTanh },
319 { "MaxPool", &TfParser::ParseMaxPool },
320 { "AvgPool", &TfParser::ParseAvgPool },
323 ITfParser* ITfParser::CreateRaw()
325 return new TfParser();
328 ITfParserPtr ITfParser::Create()
330 return ITfParserPtr(CreateRaw(), &ITfParser::Destroy);
333 void ITfParser::Destroy(ITfParser* parser)
338 inline void CalculateSamePadding(uint32_t inputSize, uint32_t stride,
339 uint32_t filterSize, bool samePadding,
340 uint32_t* paddingFront, uint32_t* paddingBack) {
345 uint32_t outputSize = (inputSize + stride - 1) / stride;
346 uint32_t temp = (outputSize - 1) * stride + filterSize;
347 if (temp > inputSize) {
348 *paddingFront = (temp - inputSize) / 2;
349 *paddingBack = (temp - inputSize) - *paddingFront;
354 void CalcPadding(uint32_t input, uint32_t kernel, uint32_t stride, uint32_t& outPadHead, uint32_t& outPadTail,
357 CalculateSamePadding(input, stride, kernel, samePadding, &outPadHead, &outPadTail);
360 /// An Abstract base class which represents a single tensorflow operation (node)
361 /// that has been (potentially partially) converted to Armnn.
362 /// It may not yet have been fully converted into actual Armnn layers.
363 class ParsedTfOperation
366 ParsedTfOperation(TfParser* parser, const tensorflow::NodeDef& node)
372 virtual ~ParsedTfOperation() {};
374 const tensorflow::NodeDef& GetNode() const { return m_Node; }
376 /// Gets the ArmNN IOutputSlot corresponding to the given output index of the Tensorflow operation.
377 /// This may result in the creation of Armnn layers if this was deferred (e.g. see ParsedConstTfOperation).
378 virtual IOutputSlot& ResolveArmnnOutputSlot(unsigned int tfOutputIndex) = 0;
380 /// If this operation is an Identity then this will follow return the 'parent' operation (recursively).
381 virtual ParsedTfOperation* ResolveIdentityOperations()
388 const tensorflow::NodeDef& m_Node;
391 /// An ParsedTfOperation where the Armnn equivalent is a single layer,
392 /// with output slots that correspond directly to the Tf node outputs.
393 class SingleLayerParsedTfOperation : public ParsedTfOperation
396 SingleLayerParsedTfOperation(TfParser* parser, const tensorflow::NodeDef& node, IConnectableLayer* layer)
397 : ParsedTfOperation(parser, node)
402 IOutputSlot& ResolveArmnnOutputSlot(unsigned int tfOutputIndex) override
404 BOOST_ASSERT(m_Layer);
405 // Assume one-to-one mapping between Tf and armnn output slots.
406 unsigned int armnnOutputSlotIdx = tfOutputIndex;
407 if (armnnOutputSlotIdx >= m_Layer->GetNumOutputSlots())
409 throw ParseException(
410 boost::str(boost::format("The requested output slot #%1% "
411 "for %2% does not exist") % armnnOutputSlotIdx % m_Layer->GetName()));
413 return m_Layer->GetOutputSlot(armnnOutputSlotIdx);
417 IConnectableLayer* m_Layer;
420 /// A SingleLayerParsedTfOperation for deferred layer creation
421 class DeferredSingleLayerParsedTfOperation : public SingleLayerParsedTfOperation
424 DeferredSingleLayerParsedTfOperation(TfParser* parser, const tensorflow::NodeDef& node)
425 : SingleLayerParsedTfOperation(parser, node, nullptr)
429 IOutputSlot& ResolveArmnnOutputSlot(unsigned int tfOutputIndex) override
433 CreateLayerDeferred();
435 return SingleLayerParsedTfOperation::ResolveArmnnOutputSlot(tfOutputIndex);
439 virtual void CreateLayerDeferred() = 0;
444 : m_Network(nullptr, nullptr)
449 const tensorflow::NodeDef* TfParser::ResolveIdentityNode(const tensorflow::NodeDef* nodeDef)
451 if (nodeDef->op() != "Identity")
456 if (nodeDef->input_size() != 1)
458 throw ParseException("Identity node does not have correct amount of inputs!");
461 auto it = m_NodesByName.find(nodeDef->input(0));
462 if (it != m_NodesByName.end())
464 const tensorflow::NodeDef* inputNode = it->second;
465 return ResolveIdentityNode(inputNode);
469 throw ParseException("Cannot find what the Identity node is linked to!");
473 std::vector<OutputOfConstNodeDef>
474 TfParser::GetTfInputNodes(const tensorflow::NodeDef& nodeDef) const
476 std::vector<OutputOfConstNodeDef> ret;
478 ret.reserve(boost::numeric_cast<size_t>(nodeDef.input_size()));
479 for (int j = 0; j < nodeDef.input_size(); ++j)
481 OutputId outputId = ParseOutputId(nodeDef.input(j));
482 auto inputIt = m_NodesByName.find(outputId.m_IndexedValue);
483 if (inputIt == m_NodesByName.end())
485 throw ParseException(
486 "Can't find node '" + nodeDef.input(j) +
487 "', which is listed as an input of '" + nodeDef.name() + "'");
489 ret.push_back(OutputOfConstNodeDef(inputIt->second,outputId.m_Index));
495 std::vector<OutputOfParsedTfOperation>
496 TfParser::GetInputParsedTfOperationsChecked(const tensorflow::NodeDef& nodeDef,
497 std::size_t expectedNumInputs)
499 // Fetch the tensorflow nodes connected as inputs and validate the size.
500 std::vector<OutputOfConstNodeDef> nodes = GetTfInputNodes(nodeDef);
501 const std::size_t numInputs = nodes.size();
502 if (numInputs != expectedNumInputs)
504 throw ParseException(boost::str(boost::format("Unexpected number of inputs for node %1%. "
505 "Expected %2%, found %3%") % nodeDef.name() % expectedNumInputs % numInputs));
507 // Fetch the corresponding ParsedTfOperation operations
508 std::vector<OutputOfParsedTfOperation> result;
509 for (auto&& node : nodes)
511 auto it = m_ParsedTfOperations.find(node.m_IndexedValue->name());
512 if (it == m_ParsedTfOperations.end())
514 throw ParseException("Node with name '" + node.m_IndexedValue->name() + "' has not been parsed");
516 ParsedTfOperation* parsedOp = it->second.get();
517 // Transparently 'skip' any Identity operations. This simplifies the logic inside the ParseXXX() functions.
518 parsedOp = parsedOp->ResolveIdentityOperations();
519 result.push_back(OutputOfParsedTfOperation(parsedOp,node.m_Index));
524 ParsedTfOperationPtr TfParser::ParseAdd(const tensorflow::NodeDef& nodeDef, const tensorflow::GraphDef& graphDef)
526 std::vector<OutputOfParsedTfOperation> inputs = GetInputParsedTfOperationsChecked(nodeDef, 2);
528 // If one of the inputs is a MatMul and the other is a const, then we handle both nodes together as FullyConnected
529 if (inputs[0].m_IndexedValue->GetNode().op() == "MatMul" &&
530 HasParsedConstTensor<float>(inputs[1].m_IndexedValue->GetNode().name()))
532 IConnectableLayer* layer =
533 AddFullyConnectedLayer(inputs[0].m_IndexedValue->GetNode(),
534 &nodeDef,nodeDef.name().c_str());
535 return std::make_unique<SingleLayerParsedTfOperation>(this, nodeDef, layer);
537 else if (HasParsedConstTensor<float>(inputs[0].m_IndexedValue->GetNode().name()) &&
538 inputs[1].m_IndexedValue->GetNode().op() == "MatMul")
540 IConnectableLayer* layer =
541 AddFullyConnectedLayer(inputs[1].m_IndexedValue->GetNode(),
542 &nodeDef,nodeDef.name().c_str());
543 return std::make_unique<SingleLayerParsedTfOperation>(this, nodeDef, layer);
547 // Otherwise it's just a regular addition
548 return AddAdditionLayer(nodeDef);
552 ParsedTfOperationPtr TfParser::ParseBiasAdd(const tensorflow::NodeDef& nodeDef, const tensorflow::GraphDef& graphDef)
554 return AddAdditionLayer(nodeDef, true);
557 /// An ParsedTfOperation which forwards to another (used for Identity nodes).
558 class ParsedIdentityTfOperation : public ParsedTfOperation
561 ParsedIdentityTfOperation(TfParser* parser, const tensorflow::NodeDef& node, ParsedTfOperation* representative)
562 : ParsedTfOperation(parser, node)
563 , m_Representative(representative)
567 virtual IOutputSlot& ResolveArmnnOutputSlot(unsigned int tfOutputIndex) override
569 BOOST_ASSERT(m_Representative);
570 return m_Representative->ResolveArmnnOutputSlot(tfOutputIndex);
573 virtual ParsedTfOperation* ResolveIdentityOperations() override
575 return m_Representative->ResolveIdentityOperations();
579 ParsedTfOperation* m_Representative;
582 ParsedTfOperationPtr TfParser::ParseIdentity(const tensorflow::NodeDef& nodeDef, const tensorflow::GraphDef& graphDef)
584 std::vector<OutputOfParsedTfOperation> inputs = GetInputParsedTfOperationsChecked(nodeDef, 1);
585 // Any requests for the output slots of this node should be forwarded to the node connected as input.
586 return std::make_unique<ParsedIdentityTfOperation>(this, nodeDef, inputs[0].m_IndexedValue);
589 /// An ParsedTfOperation for a Const node.
590 /// Creation of the armnn ConstLayer is deferred until it is actually needed, because Const nodes are mostly used
591 /// for weight inputs to MatMul/Conv2D nodes and in these cases armnn doesn't need a ConstLayer.
592 template <typename T>
593 class ParsedConstTfOperation : public DeferredSingleLayerParsedTfOperation
596 ParsedConstTfOperation(TfParser* parser, const tensorflow::NodeDef& node,
597 const T* tensorData, const TensorInfo& tensorInfo)
598 : DeferredSingleLayerParsedTfOperation(parser, node),
599 m_Storage(tensorData, tensorData + tensorInfo.GetNumElements()),
600 m_TensorInfo(tensorInfo)
602 BOOST_ASSERT(tensorInfo.GetDataType() == GetDataType<T>());
605 void CreateLayerDeferred() override
607 BOOST_ASSERT(m_Layer == nullptr);
608 m_Layer = m_Parser->m_Network->AddConstantLayer(ConstTensor(m_TensorInfo, m_Storage), m_Node.name().c_str());
609 m_Layer->GetOutputSlot(0).SetTensorInfo(m_TensorInfo);
612 ConstTensor GetConstTensor(bool swizzleForConvolutionWeights, std::vector<T>& outputTensorData) const
614 // Mappings from TensorFlow filter tensors to the ArmNN filter tensors.
615 // Tensorflow weights are [H, W, In, Out]
616 // ArmNN weights are [Out, In, H, W]
617 static const PermutationVector HWIOToOIHW = {2, 3, 1, 0};
619 const TensorInfo outInfo = swizzleForConvolutionWeights
620 ? armnnUtils::Permuted(m_TensorInfo, HWIOToOIHW)
623 outputTensorData.resize(m_TensorInfo.GetNumElements());
625 // Copy or swizzle from the permanent storage into the storage the caller provided.
626 if (swizzleForConvolutionWeights)
628 armnnUtils::Permute(outInfo.GetShape(), HWIOToOIHW, m_Storage.data(), outputTensorData.data());
632 memcpy(outputTensorData.data(), m_Storage.data(), m_TensorInfo.GetNumBytes());
634 // Update the result to point to the user provided storage
635 ConstTensor constTensor(outInfo, outputTensorData);
640 ///< Manages the lifetime of the tensor data.
641 std::vector<T> m_Storage;
642 ///< Describes the layout of the tensor and points to the data in m_Storage.
643 TensorInfo m_TensorInfo;
646 DataType ConvertTfTensorDataType(const tensorflow::DataType tfDataType)
650 case tensorflow::DT_FLOAT:
651 return DataType::Float32;
653 case tensorflow::DT_INT32:
654 return DataType::Signed32;
657 throw ParseException(boost::str(
658 boost::format("Unknown DataType %1% for node")
659 % tensorflow::DataType_Name(tfDataType)));
663 struct ParseTfTensorValueList
665 template<typename DataType>
667 const tensorflow::TensorProto& tfTensor,
668 unsigned int dstElements,
669 std::vector<int8_t>& outputData);
671 template <typename DataType>
672 static void ReadData(const void* srcData, unsigned int numSrcElements,
673 std::vector<int8_t>& dstData, unsigned int numDstElements)
675 // If there are no entries in the list, perform no action
676 if (numSrcElements == 0)
681 // If no size was provided, use the length of the value list
682 if (numDstElements == 0)
684 numDstElements = numSrcElements;
688 dstData.resize(std::max(numSrcElements, numDstElements) * sizeof(DataType));
690 const DataType* srcTensor = reinterpret_cast<const DataType*>(srcData);
691 DataType* dstTensor = reinterpret_cast<DataType*>(dstData.data());
693 // Copy the value list entries into the destination
694 std::copy(srcTensor, srcTensor + numSrcElements, dstTensor);
696 if (numDstElements > numSrcElements)
698 // Use the last element in the list to fill the remaining entries
699 std::fill(dstTensor + numSrcElements, dstTensor + numDstElements, srcTensor[numSrcElements - 1]);
706 void ParseTfTensorValueList::Parse<float>(const tensorflow::TensorProto& tfTensor,
707 unsigned int dstElements, std::vector<int8_t>& outputData)
709 ReadData<float>(tfTensor.float_val().data(), static_cast<unsigned int>(tfTensor.float_val_size()),
710 outputData, dstElements);
714 void ParseTfTensorValueList::Parse<int32_t>(const tensorflow::TensorProto& tfTensor,
715 unsigned int dstElements, std::vector<int8_t>& outputData)
717 ReadData<int32_t>(tfTensor.int_val().data(), static_cast<unsigned int>(tfTensor.int_val_size()),
718 outputData, dstElements);
721 template <template<typename> class OperatorType, typename T = int8_t>
722 struct MakeTfOperation
724 template<typename DataType, class... Args>
725 inline static std::unique_ptr<OperatorType<DataType>> Parse(TfParser* parser, const tensorflow::NodeDef& node,
728 return std::make_unique<OperatorType<DataType>>(parser, node, std::forward<Args>(args)...);
733 struct MakeTfOperation<ParsedConstTfOperation>
735 template<typename DataType, class... Args>
736 inline static std::unique_ptr<ParsedConstTfOperation<DataType>> Parse(TfParser* parser,
737 const tensorflow::NodeDef& node, const std::vector<int8_t>& tensorData, const TensorInfo& tensorInfo)
739 return std::make_unique<ParsedConstTfOperation<DataType>>(parser, node,
740 reinterpret_cast<const DataType*>(tensorData.data()), tensorInfo);
744 template <class FuncType>
745 struct InvokeParseFunction
747 template<class ResType, class... Args>
748 inline static ResType Result(DataType dataType, Args&&... args)
750 if (dataType == DataType::Float32)
752 return FuncType::template Parse<float>(std::forward<Args>(args)...);
754 else if (dataType == DataType::Signed32)
756 return FuncType::template Parse<int32_t>(std::forward<Args>(args)...);
762 template<class... Args>
763 inline static void Result(DataType dataType, Args&&... args)
765 if (dataType == DataType::Float32)
767 FuncType::template Parse<float>(std::forward<Args>(args)...);
769 else if (dataType == DataType::Signed32)
771 FuncType::template Parse<int32_t>(std::forward<Args>(args)...);
776 ParsedTfOperationPtr TfParser::ParseConst(const tensorflow::NodeDef& nodeDef, const tensorflow::GraphDef& graphDef)
778 BOOST_ASSERT(nodeDef.op() == "Const");
780 if (nodeDef.attr().count("value") == 0)
782 throw ParseException(boost::str(
783 boost::format("Value not found for Const node - %1%")
787 const tensorflow::TensorProto& tfTensor = nodeDef.attr().at("value").tensor();
788 const tensorflow::TensorShapeProto& tfTensorShape = tfTensor.tensor_shape();
789 const tensorflow::DataType tfDataType = ReadMandatoryNodeTypeAttribute(nodeDef, "dtype");
791 const auto GetDimensionSize = [](auto& d) { return d.size(); };
793 std::vector<unsigned int> dimensionSizes;
794 std::transform(tfTensorShape.dim().begin(), tfTensorShape.dim().end(),
795 std::back_inserter(dimensionSizes), GetDimensionSize);
797 // Calculate number of elements
798 const DataType dataType = ConvertTfTensorDataType(tfDataType);
799 unsigned int numElements = 0U;
801 if (!dimensionSizes.empty())
803 numElements = std::accumulate(dimensionSizes.begin(), dimensionSizes.end(),
804 1U, std::multiplies<unsigned int>());
807 std::vector<int8_t> tensorData;
809 // Get tensor data from the list of values attribute
810 if (tfTensor.tensor_content().empty())
812 InvokeParseFunction<ParseTfTensorValueList>::Result<void>(dataType, tfTensor, numElements, tensorData);
814 // If the tensor shape is not defined, but there is a value list, then interpret the data as a 1D
815 // tensor of the provided number of elements
816 if (numElements == 0)
818 const unsigned int tfNumElements = static_cast<unsigned int>(tensorData.size()) / GetDataTypeSize(dataType);
819 dimensionSizes.push_back(tfNumElements);
822 // Get tensor data from tensor content attribute
825 tensorData.assign(tfTensor.tensor_content().begin(), tfTensor.tensor_content().end());
827 // Check if a tensor shape is defined for the tensor content
828 if (numElements == 0)
830 throw ParseException(boost::str(
831 boost::format("No tensor shape found for Const node - %1%")
836 // Const node requires at least a list of values or a content attribute
837 if (tensorData.empty())
839 throw ParseException(boost::str(
840 boost::format("No tensor data found for Const node - %1%")
844 const TensorInfo tensorInfo(static_cast<unsigned int>(dimensionSizes.size()), dimensionSizes.data(), dataType);
846 // If we have a list of values, then the length of the list must be
847 // less than or equal to the number of elements implied by the shape argument
848 if (tensorData.size() > tensorInfo.GetNumBytes())
850 throw ParseException(boost::str(
851 boost::format("Number of elements (%1%) should be less than or equal \
852 to the number of elements implied by the shape argument (%2%) for Const node - %3%")
853 % (tensorData.size() / GetDataTypeSize(dataType))
854 % tensorInfo.GetNumElements()
858 return InvokeParseFunction<MakeTfOperation<ParsedConstTfOperation>>::Result<ParsedTfOperationPtr>(
859 dataType, this, nodeDef, tensorData, tensorInfo);
862 template<typename Type>
863 bool TfParser::HasParsedConstTensor(const std::string & nodeName) const
865 auto it = m_ParsedTfOperations.find(nodeName);
866 if (it == m_ParsedTfOperations.end() ||
867 dynamic_cast<ParsedConstTfOperation<Type>*>(it->second.get()) == nullptr)
877 ParsedTfOperationPtr TfParser::ParseConv2D(const tensorflow::NodeDef& nodeDef,
878 const tensorflow::GraphDef& graphDef)
880 std::vector<OutputOfParsedTfOperation> inputs = GetInputParsedTfOperationsChecked(nodeDef, 2);
881 IOutputSlot& inputSlot = inputs[0].m_IndexedValue->ResolveArmnnOutputSlot(inputs[0].m_Index);
882 TensorInfo inputTensorInfo = inputSlot.GetTensorInfo();
884 if (!HasParsedConstTensor<float>(inputs[1].m_IndexedValue->GetNode().name()))
886 throw ParseException("ArmNN only supports Convolution layers with constant weights");
888 ParsedConstTfOperation<float>* weightNode =
889 boost::polymorphic_downcast<ParsedConstTfOperation<float> *>(inputs[1].m_IndexedValue);
891 std::string paddingString = ReadMandatoryNodeStringAttribute(nodeDef, "padding");
892 std::string dataFormat = ReadMandatoryNodeStringAttribute(nodeDef, "data_format");
893 std::vector<uint32_t> strides = ReadMandatoryNodeUint32ListAttribute(nodeDef, "strides");
895 // read the dilations, if present - only [1,1,1,1] (the default) is supported
896 std::vector<uint32_t> dilations = ReadOptionalNodeUint32ListAttribute(nodeDef, "dilations");
897 if (!dilations.empty())
899 for (auto dilation : dilations)
903 throw ParseException("ArmNN only supports Convolution layers with dilations [1,1,1,1]");
908 Convolution2dDescriptor desc;
909 desc.m_BiasEnabled = false;
911 if (dataFormat == "NHWC")
913 desc.m_StrideX = strides[2];
914 desc.m_StrideY = strides[1];
915 // Swizzle input to supported memory layout
916 inputTensorInfo = armnnUtils::Permuted(inputSlot.GetTensorInfo(), NHWCToArmNN);
918 else if (dataFormat == "NCHW")
920 desc.m_StrideX = strides[3];
921 desc.m_StrideY = strides[2];
925 throw ParseException("Unsupported data format passed for Conv2D. Only NHWC and NCHW supported");
928 uint32_t inputHeight = inputTensorInfo.GetShape()[2];
929 uint32_t inputWidth = inputTensorInfo.GetShape()[3];
931 std::vector<float> outputTensorData;
933 ConstTensor weightTensor = weightNode->GetConstTensor(true, outputTensorData);
935 uint32_t weightHeight = weightTensor.GetShape()[2];
936 uint32_t weightWidth = weightTensor.GetShape()[3];
938 bool padding = false;
939 TensorInfo outputInfo;
940 if (paddingString == "SAME")
943 outputInfo = TensorInfo({ inputTensorInfo.GetShape()[0],
944 weightTensor.GetShape()[0],
945 static_cast<uint32_t>(ceil(
946 static_cast<float>(inputHeight) /
947 static_cast<float>(desc.m_StrideY))),
948 static_cast<uint32_t>(ceil(
949 static_cast<float>(inputWidth) /
950 static_cast<float>(desc.m_StrideX)))
951 }, DataType::Float32);
953 else if (paddingString == "VALID")
956 outputInfo = TensorInfo({ inputTensorInfo.GetShape()[0],
957 weightTensor.GetShape()[0],
958 static_cast<uint32_t>(ceil(
959 static_cast<float>(inputHeight - weightHeight + 1) /
960 static_cast<float>(desc.m_StrideY))),
961 static_cast<uint32_t>(ceil(
962 static_cast<float>(inputWidth - weightWidth + 1) /
963 static_cast<float>(desc.m_StrideX)))
964 }, DataType::Float32);
968 throw ParseException("Only 'SAME' and 'VALID' padding supported");
971 CalcPadding(inputHeight, weightHeight, desc.m_StrideY, desc.m_PadTop, desc.m_PadBottom, padding);
972 CalcPadding(inputWidth, weightWidth, desc.m_StrideX, desc.m_PadLeft, desc.m_PadRight, padding);
974 IConnectableLayer* layer = m_Network->AddConvolution2dLayer(desc, weightTensor, nodeDef.name().c_str());
975 layer->GetOutputSlot(0).SetTensorInfo(outputInfo);
977 if (dataFormat == "NHWC")
979 layer = SwizzleInDeswizzleOut(*m_Network, inputSlot, *layer, nodeDef.name());
983 inputSlot.Connect(layer->GetInputSlot(0));
986 return std::make_unique<SingleLayerParsedTfOperation>(this, nodeDef, layer);
989 ParsedTfOperationPtr TfParser::ParseDepthwiseConv2D(const tensorflow::NodeDef& nodeDef,
990 const tensorflow::GraphDef& graphDef)
992 std::vector<OutputOfParsedTfOperation> inputs = GetInputParsedTfOperationsChecked(nodeDef, 2);
993 IOutputSlot& inputSlot = inputs[0].m_IndexedValue->ResolveArmnnOutputSlot(inputs[0].m_Index);
994 TensorInfo inputTensorInfo = inputSlot.GetTensorInfo();
996 if (!HasParsedConstTensor<float>(inputs[1].m_IndexedValue->GetNode().name()))
998 throw ParseException("ArmNN only supports Depthwise Convolution layers with constant weights");
1000 ParsedConstTfOperation<float>* weightNode =
1001 boost::polymorphic_downcast<ParsedConstTfOperation<float> *>(inputs[1].m_IndexedValue);
1004 std::string paddingString = ReadMandatoryNodeStringAttribute(nodeDef, "padding");
1005 std::string dataFormat = ReadMandatoryNodeStringAttribute(nodeDef, "data_format");
1006 std::vector<uint32_t> strides = ReadMandatoryNodeUint32ListAttribute(nodeDef, "strides");
1008 DepthwiseConvolution2dDescriptor desc;
1009 desc.m_BiasEnabled = false;
1011 if (dataFormat == "NHWC")
1013 desc.m_StrideX = strides[2];
1014 desc.m_StrideY = strides[1];
1015 // Swizzle input to supported memory layout
1016 inputTensorInfo = armnnUtils::Permuted(inputSlot.GetTensorInfo(), NHWCToArmNN);
1018 else if (dataFormat == "NCHW")
1020 desc.m_StrideX = strides[3];
1021 desc.m_StrideY = strides[2];
1025 throw ParseException("Unsupported data format passed for DepthwiseConv2dNative. Only NHWC and NCHW supported");
1028 uint32_t inputHeight = inputTensorInfo.GetShape()[2];
1029 uint32_t inputWidth = inputTensorInfo.GetShape()[3];
1031 std::vector<float> outputTensorData;
1033 ConstTensor weightTensor = weightNode->GetConstTensor(true, outputTensorData);
1035 uint32_t weightHeight = weightTensor.GetShape()[2];
1036 uint32_t weightWidth = weightTensor.GetShape()[3];
1038 bool padding = false;
1039 TensorInfo outputInfo;
1040 if (paddingString == "SAME")
1043 outputInfo = TensorInfo({ inputTensorInfo.GetShape()[0],
1044 weightTensor.GetShape()[0] * weightTensor.GetShape()[1],
1045 static_cast<uint32_t>(ceil(
1046 static_cast<float>(inputHeight) /
1047 static_cast<float>(desc.m_StrideY))),
1048 static_cast<uint32_t>(ceil(
1049 static_cast<float>(inputWidth) /
1050 static_cast<float>(desc.m_StrideX)))
1051 }, DataType::Float32);
1053 else if (paddingString == "VALID")
1056 outputInfo = TensorInfo({ inputTensorInfo.GetShape()[0],
1057 weightTensor.GetShape()[0] * weightTensor.GetShape()[1],
1058 static_cast<uint32_t>(ceil(
1059 static_cast<float>(inputHeight - weightHeight + 1) /
1060 static_cast<float>(desc.m_StrideY))),
1061 static_cast<uint32_t>(ceil(
1062 static_cast<float>(inputWidth - weightWidth + 1) /
1063 static_cast<float>(desc.m_StrideX)))
1064 }, DataType::Float32);
1068 throw ParseException("Only 'SAME' and 'VALID' padding supported");
1071 CalcPadding(inputHeight, weightHeight, desc.m_StrideY, desc.m_PadTop, desc.m_PadBottom, padding);
1072 CalcPadding(inputWidth, weightWidth, desc.m_StrideX, desc.m_PadLeft, desc.m_PadRight, padding);
1074 IConnectableLayer* layer = m_Network->AddDepthwiseConvolution2dLayer(desc, weightTensor, nodeDef.name().c_str());
1075 layer->GetOutputSlot(0).SetTensorInfo(outputInfo);
1077 if (dataFormat == "NHWC")
1079 layer = SwizzleInDeswizzleOut(*m_Network, inputSlot, *layer, nodeDef.name());
1083 inputSlot.Connect(layer->GetInputSlot(0));
1086 return std::make_unique<SingleLayerParsedTfOperation>(this, nodeDef, layer);
1089 ParsedTfOperationPtr TfParser::ParseFusedBatchNorm(const tensorflow::NodeDef& nodeDef,
1090 const tensorflow::GraphDef& graphDef)
1092 std::vector<OutputOfParsedTfOperation> inputs = GetInputParsedTfOperationsChecked(nodeDef, 5);
1094 if (!HasParsedConstTensor<float>(inputs[1].m_IndexedValue->GetNode().name()))
1096 throw ParseException("ArmNN only supports FusedBatchNormalization layers with constant scale");
1098 ParsedConstTfOperation<float>* scaleNode =
1099 boost::polymorphic_downcast<ParsedConstTfOperation<float> *>(inputs[1].m_IndexedValue);
1101 if (!HasParsedConstTensor<float>(inputs[2].m_IndexedValue->GetNode().name()))
1103 throw ParseException("ArmNN only supports FusedBatchNormalization layers with constant offset");
1105 ParsedConstTfOperation<float>* offsetNode =
1106 boost::polymorphic_downcast<ParsedConstTfOperation<float> *>(inputs[2].m_IndexedValue);
1108 if (!HasParsedConstTensor<float>(inputs[3].m_IndexedValue->GetNode().name()))
1110 throw ParseException("ArmNN only supports FusedBatchNormalization layers with constant mean");
1112 ParsedConstTfOperation<float>* meanNode =
1113 boost::polymorphic_downcast<ParsedConstTfOperation<float> *>(inputs[3].m_IndexedValue);
1115 if (!HasParsedConstTensor<float>(inputs[4].m_IndexedValue->GetNode().name()))
1117 throw ParseException("ArmNN only supports FusedBatchNormalization layers with constant variance");
1119 ParsedConstTfOperation<float>* varianceNode =
1120 boost::polymorphic_downcast<ParsedConstTfOperation<float> *>(inputs[4].m_IndexedValue);
1122 // The descriptor only has the epsilon attribute
1123 BatchNormalizationDescriptor desc;
1124 desc.m_Eps = ReadMandatoryNodeFloatAttribute(nodeDef, "epsilon");
1126 // data for the parsed tensor args (scale, offset, mean, variance) must be stored locally until the layer is added
1127 std::vector<float> scaleTensorData;
1128 ConstTensor scaleTensor = scaleNode->GetConstTensor(false, scaleTensorData);
1130 std::vector<float> offsetTensorData;
1131 ConstTensor offsetTensor = offsetNode->GetConstTensor(false, offsetTensorData);
1133 std::vector<float> meanTensorData;
1134 ConstTensor meanTensor = meanNode->GetConstTensor(false, meanTensorData);
1136 std::vector<float> varianceTensorData;
1137 ConstTensor varianceTensor = varianceNode->GetConstTensor(false, varianceTensorData);
1139 IConnectableLayer* layer = m_Network->AddBatchNormalizationLayer(desc,
1144 nodeDef.name().c_str());
1146 IOutputSlot& inputSlot = inputs[0].m_IndexedValue->ResolveArmnnOutputSlot(inputs[0].m_Index);
1148 const std::string dataFormat = ReadMandatoryNodeStringAttribute(nodeDef, "data_format");
1150 if (dataFormat == "NHWC")
1152 const TensorInfo outputTensorInfo = armnnUtils::Permuted(inputSlot.GetTensorInfo(), NHWCToArmNN);
1153 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1154 layer = SwizzleInDeswizzleOut(*m_Network, inputSlot, *layer, nodeDef.name());
1158 layer->GetOutputSlot(0).SetTensorInfo(inputSlot.GetTensorInfo());
1159 inputSlot.Connect(layer->GetInputSlot(0));
1162 return std::make_unique<SingleLayerParsedTfOperation>(this, nodeDef, layer);
1165 ParsedTfOperationPtr TfParser::ParseConcat(const tensorflow::NodeDef& nodeDef,
1166 const tensorflow::GraphDef& graphDef)
1168 std::vector<OutputOfConstNodeDef> nodes = GetTfInputNodes(nodeDef);
1169 // In tensorflow, we have the last input of the Concat layer as the axis for concatenation
1170 unsigned int numInputs = static_cast<unsigned int>(nodes.size());
1171 unsigned int numConcatView = numInputs - 1;
1173 OriginsDescriptor concatDescriptor(static_cast<uint32_t>(numConcatView), MaxNumOfTensorDimensions);
1174 std::vector<unsigned int>mergeDimSizes(MaxNumOfTensorDimensions, 0u);
1176 unsigned int mergeDim = 0;
1177 std::vector<OutputOfParsedTfOperation> inputs = GetInputParsedTfOperationsChecked(nodeDef, numInputs);
1179 // The last input is the axis for concatenation
1180 if (!HasParsedConstTensor<int32_t>(inputs[numInputs - 1].m_IndexedValue->GetNode().name()))
1182 throw ParseException("ArmNN only supports Concat with constant axis");
1184 ParsedConstTfOperation<int32_t>* shapeNode =
1185 boost::polymorphic_downcast<ParsedConstTfOperation<int32_t>*>(inputs[numInputs - 1].m_IndexedValue);
1187 std::vector<int32_t> axisTensorData;
1188 ConstTensor axisTensor = shapeNode->GetConstTensor(false, axisTensorData);
1190 // This concatDim indicates the data format: 3 is the NHWC, 1 is the NCHW
1191 const unsigned int concatDimInput = static_cast<unsigned int>(axisTensorData[0]);
1193 // Armnn supports concatenation along the channel dimension for data format NHWC and NCHW
1194 if (concatDimInput == 0 || concatDimInput == 2)
1196 throw ParseException("The dimension for concatenation is not supported by Armnn");
1199 // This is the only concatDim we support in Armnn
1200 const unsigned int concatDim = 1;
1201 for (unsigned int viewIndex = 0; viewIndex < numConcatView; ++viewIndex)
1203 // need to double check whether it should be
1204 IOutputSlot& inputSlot =
1205 inputs[viewIndex].m_IndexedValue->ResolveArmnnOutputSlot(inputs[viewIndex].m_Index);
1206 TensorInfo inputTensorInfo = inputSlot.GetTensorInfo();
1208 if (inputTensorInfo.GetNumDimensions() != MaxNumOfTensorDimensions)
1210 throw ParseException("The number of dimensions for input tensors of the concatenation op should be 4");
1213 if (concatDimInput == 3)
1215 inputTensorInfo = armnnUtils::Permuted(inputTensorInfo, NHWCToArmNN);
1218 for (unsigned int dim = 0; dim < MaxNumOfTensorDimensions; ++dim)
1220 mergeDimSizes[dim] = inputTensorInfo.GetShape()[dim];
1223 for (unsigned int j = 0; j < concatDim; ++j)
1225 concatDescriptor.SetViewOriginCoord(viewIndex, j, 0);
1228 concatDescriptor.SetViewOriginCoord(viewIndex, concatDim, mergeDim);
1229 mergeDim += mergeDimSizes[concatDim];
1231 for (unsigned int j = concatDim+1; j < MaxNumOfTensorDimensions; ++j)
1233 concatDescriptor.SetViewOriginCoord(viewIndex, j, 0);
1237 mergeDimSizes[concatDim] = mergeDim;
1238 armnn::IConnectableLayer *layer = m_Network->AddMergerLayer(concatDescriptor, nodeDef.name().c_str());
1240 layer->GetOutputSlot(0).SetTensorInfo(armnn::TensorInfo(MaxNumOfTensorDimensions, mergeDimSizes.data(),
1241 DataType::Float32));
1243 for (unsigned int v = 0; v < numConcatView; ++v)
1245 IOutputSlot& inputSlot = inputs[v].m_IndexedValue->ResolveArmnnOutputSlot(inputs[v].m_Index);
1246 if (concatDimInput == 3)
1248 IConnectableLayer* const swizzleLayer = AddSwizzleLayer(*m_Network, inputSlot, NHWCToArmNN,
1249 "swizzle_for-" + nodeDef.name());
1250 swizzleLayer->GetOutputSlot(0).Connect(layer->GetInputSlot(v));
1254 inputSlot.Connect(layer->GetInputSlot(v));
1258 if (concatDimInput == 3)
1260 IConnectableLayer* const deswizzleLayer = AddSwizzleLayer(*m_Network, layer->GetOutputSlot(0), ArmNNToNHWC,
1261 "deswizzle_for-" + nodeDef.name());
1262 layer = deswizzleLayer;
1265 return std::make_unique<SingleLayerParsedTfOperation>(this, nodeDef, layer);
1268 ParsedTfOperationPtr TfParser::ParseShape(const tensorflow::NodeDef& nodeDef,
1269 const tensorflow::GraphDef& graphDef)
1271 // Note: The Shape layer is handled in a special way, because:
1272 // 1. ARMNN doesn't support int32 tensors which it outputs
1273 // 2. ARMNN works with statically shaped tensors which are known at parse time
1274 // 3. because of 1. and 2. we treat the output of Shape as a temporary const int32
1275 // tensor which may be used as an input to other ops, most likely a Reshape
1277 const tensorflow::DataType tfDataType = ReadMandatoryNodeTypeAttribute(nodeDef, "out_type");
1278 if (tfDataType != tensorflow::DT_INT32)
1280 throw ParseException("Armnn only supports DT_INT32 as out_type");
1283 const std::vector<OutputOfParsedTfOperation> inputs = GetInputParsedTfOperationsChecked(nodeDef, 1);
1284 IOutputSlot& prevLayerOutputSlot = inputs[0].m_IndexedValue->ResolveArmnnOutputSlot(inputs[0].m_Index);
1285 const TensorInfo& prevLayerTensorInfo = prevLayerOutputSlot.GetTensorInfo();
1286 unsigned int prevLayerDimensions = prevLayerTensorInfo.GetNumDimensions();
1288 std::vector<int32_t> shapeTensorData;
1289 shapeTensorData.reserve(prevLayerDimensions);
1291 for (unsigned int i=0; i<prevLayerDimensions; ++i)
1293 shapeTensorData.push_back(static_cast<int32_t>(prevLayerTensorInfo.GetShape()[i]));
1296 TensorInfo shapeTensorInfo(1, &prevLayerDimensions, DataType::Signed32);
1298 return std::make_unique<ParsedConstTfOperation<int32_t>>(this,
1300 &shapeTensorData[0],
1304 ParsedTfOperationPtr TfParser::ParseReshape(const tensorflow::NodeDef& nodeDef,
1305 const tensorflow::GraphDef& graphDef)
1307 std::vector<OutputOfParsedTfOperation> inputs = GetInputParsedTfOperationsChecked(nodeDef, 2);
1308 ParsedTfOperation* inputNode = inputs[0].m_IndexedValue;
1310 if (!HasParsedConstTensor<int32_t>(inputs[1].m_IndexedValue->GetNode().name()))
1312 throw ParseException("ArmNN only supports Reshape layers with constant shapes");
1314 ParsedConstTfOperation<int32_t>* shapeNode =
1315 boost::polymorphic_downcast<ParsedConstTfOperation<int32_t>*>(inputs[1].m_IndexedValue);
1317 armnn::IOutputSlot& prevLayerOutputSlot = inputNode->ResolveArmnnOutputSlot(inputs[0].m_Index);
1318 TensorInfo inputTensorInfo = prevLayerOutputSlot.GetTensorInfo();
1320 std::vector<int32_t> shapeTensorData;
1321 ConstTensor shapeTensor = shapeNode->GetConstTensor(false, shapeTensorData);
1322 const TensorInfo outputTensorInfo = PrepareReshape(inputTensorInfo, shapeTensorData);
1324 TensorShape targetShape = outputTensorInfo.GetShape();
1325 ReshapeDescriptor reshapeDesc;
1326 reshapeDesc.m_TargetShape = targetShape;
1328 IConnectableLayer* layer = m_Network->AddReshapeLayer(reshapeDesc, nodeDef.name().c_str());
1329 prevLayerOutputSlot.Connect(layer->GetInputSlot(0));
1330 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1332 return std::make_unique<SingleLayerParsedTfOperation>(this, nodeDef, layer);
1335 ParsedTfOperationPtr TfParser::ParseResizeBilinear(const tensorflow::NodeDef& nodeDef,
1336 const tensorflow::GraphDef& graphDef)
1338 std::vector<OutputOfParsedTfOperation> inputs = GetInputParsedTfOperationsChecked(nodeDef, 2);
1340 if (!HasParsedConstTensor<int32_t>(inputs[1].m_IndexedValue->GetNode().name()))
1342 throw ParseException("ArmNN only supports ResizeBilinear layers with constant sizes");
1344 ParsedConstTfOperation<int32_t>* sizeNode =
1345 boost::polymorphic_downcast<ParsedConstTfOperation<int32_t>*>(inputs[1].m_IndexedValue);
1347 // Check the align_corners attribute is not set
1348 if (ReadOptionalNodeBoolAttribute(nodeDef, "align_corners", false))
1350 throw ParseException("ArmNN only supports ResizeBilinear layers with align_corners set to false");
1353 // data for the parsed tensor args (size) must be stored locally
1354 std::vector<int32_t> sizeTensorData;
1355 ConstTensor sizeTensor = sizeNode->GetConstTensor(false, sizeTensorData);
1357 // The descriptor only has target height and width attributes, which we get from the size tensor
1358 ResizeBilinearDescriptor desc;
1359 desc.m_TargetHeight = static_cast<uint32_t> (sizeTensorData[0]);
1360 desc.m_TargetWidth = static_cast<uint32_t> (sizeTensorData[1]);
1362 IConnectableLayer* layer = m_Network->AddResizeBilinearLayer(desc, nodeDef.name().c_str());
1364 IOutputSlot& inputSlot = inputs[0].m_IndexedValue->ResolveArmnnOutputSlot(inputs[0].m_Index);
1365 TensorInfo inputTensorInfo = inputSlot.GetTensorInfo();
1366 // the input shape is always in BHWC format, this will be swizzled below; for now,
1367 // get the batch and channels to make up the ArmNN output shape with the target size
1368 unsigned int outBatch = inputTensorInfo.GetShape()[0];
1369 unsigned int outChannels = inputTensorInfo.GetShape()[3];
1370 unsigned int outHeight = desc.m_TargetHeight;
1371 unsigned int outWidth = desc.m_TargetWidth;
1372 TensorShape outShape({outBatch, outChannels, outHeight, outWidth});
1373 // The output DataType is always Float32, regardless of the input DataType
1374 const TensorInfo outputTensorInfo(outShape, armnn::DataType::Float32);
1375 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1377 // TensorFlow ResizeBilinear input is always in BHWC format, so add swizzle and deswizzle layers
1378 layer = SwizzleInDeswizzleOut(*m_Network, inputSlot, *layer, nodeDef.name());
1380 return std::make_unique<SingleLayerParsedTfOperation>(this, nodeDef, layer);
1383 TensorInfo OutputShapeOfSqueeze(const tensorflow::NodeDef& nodeDef, TensorInfo inputTensorInfo)
1385 BOOST_ASSERT(nodeDef.op() == "Squeeze");
1386 tensorflow::DataType tfDataType = ReadMandatoryNodeTypeAttribute(nodeDef, "T");
1389 if (tfDataType == tensorflow::DT_FLOAT)
1391 type = DataType::Float32;
1393 else if (tfDataType == tensorflow::DT_INT32)
1395 type = DataType::Signed32;
1399 throw ParseException(boost::str(
1400 boost::format("Unsupported DataType %1% for Squeeze operation")
1401 % tensorflow::DataType_Name(tfDataType)));
1404 std::vector<uint32_t> squeezeDims = ReadOptionalNodeUint32ListAttribute(nodeDef, "squeeze_dims");
1405 if (squeezeDims.empty())
1407 for(unsigned int i = 0; i < inputTensorInfo.GetNumDimensions(); i++)
1409 if (inputTensorInfo.GetShape()[i] == 1)
1411 squeezeDims.push_back(i);
1416 std::vector<uint32_t> outputDims;
1417 for(unsigned int i = 0; i < inputTensorInfo.GetNumDimensions(); i++)
1419 bool includeDimension = (std::find(squeezeDims.begin(), squeezeDims.end(), i) == squeezeDims.end());
1420 if (includeDimension)
1422 outputDims.push_back(inputTensorInfo.GetShape()[i]);
1426 if (outputDims.size() > 4)
1428 throw ParseException("Unsupported shape for Squeeze");
1431 TensorInfo outTensorInfo = TensorInfo(boost::numeric_cast<unsigned int>(outputDims.size()),
1435 return outTensorInfo;
1438 ParsedTfOperationPtr TfParser::ParseSqueeze(const tensorflow::NodeDef& nodeDef, const tensorflow::GraphDef& graphDef)
1440 std::vector<OutputOfParsedTfOperation> inputs = GetInputParsedTfOperationsChecked(nodeDef, 1);
1442 IOutputSlot& prevLayerOutputSlot = inputs[0].m_IndexedValue->ResolveArmnnOutputSlot(inputs[0].m_Index);
1443 TensorInfo inputTensorInfo = prevLayerOutputSlot.GetTensorInfo();
1445 TensorInfo outputInfo;
1446 outputInfo = OutputShapeOfSqueeze(nodeDef, inputTensorInfo);
1448 ReshapeDescriptor reshapeDesc;
1449 reshapeDesc.m_TargetShape = outputInfo.GetShape();
1450 IConnectableLayer* layer = m_Network->AddReshapeLayer(reshapeDesc, nodeDef.name().c_str());
1451 prevLayerOutputSlot.Connect(layer->GetInputSlot(0));
1452 layer->GetOutputSlot(0).SetTensorInfo(outputInfo);
1454 return std::make_unique<SingleLayerParsedTfOperation>(this, nodeDef, layer);
1457 ParsedTfOperationPtr TfParser::ParseLrn(const tensorflow::NodeDef& nodeDef, const tensorflow::GraphDef& graphDef)
1459 std::vector<OutputOfParsedTfOperation> inputs = GetInputParsedTfOperationsChecked(nodeDef, 1);
1461 NormalizationDescriptor normalizationDescriptor;
1462 normalizationDescriptor.m_NormMethodType = NormalizationAlgorithmMethod::LocalBrightness;
1463 normalizationDescriptor.m_NormChannelType = NormalizationAlgorithmChannel::Across;
1464 normalizationDescriptor.m_Alpha = ReadMandatoryNodeFloatAttribute(nodeDef, "alpha");
1465 normalizationDescriptor.m_Beta = ReadMandatoryNodeFloatAttribute(nodeDef, "beta");
1466 normalizationDescriptor.m_K = ReadMandatoryNodeFloatAttribute(nodeDef, "bias");
1467 normalizationDescriptor.m_NormSize = ReadMandatoryNodeUint32Attribute(nodeDef, "depth_radius");
1469 // The window size must be an odd value. For a window size of (2 * n + 1), TensorFlow defines depth_radius = n.
1470 normalizationDescriptor.m_NormSize = normalizationDescriptor.m_NormSize * 2 + 1;
1472 IOutputSlot& prevLayerOutputSlot = inputs[0].m_IndexedValue->ResolveArmnnOutputSlot(inputs[0].m_Index);
1474 IConnectableLayer* layer = m_Network->AddNormalizationLayer(normalizationDescriptor,
1475 nodeDef.name().c_str());
1477 const TensorInfo permutedInfo = armnnUtils::Permuted(prevLayerOutputSlot.GetTensorInfo(), NHWCToArmNN);
1478 layer->GetOutputSlot(0).SetTensorInfo(permutedInfo);
1480 layer = SwizzleInDeswizzleOut(*m_Network, prevLayerOutputSlot, *layer, nodeDef.name());
1482 return std::make_unique<SingleLayerParsedTfOperation>(this, nodeDef, layer);
1485 /// An ParsedTfOperation for a MatMul node.
1486 /// Creation of the armnn FullyConnected layer is deferred until it is actually needed, because MatMul nodes are
1487 /// often used for the first part of a biased FullyConnected (MatMul followed by Add) and in these cases armnn doesn't
1488 /// need a separate layer for the MatMul.
1489 class ParsedMatMulTfOperation : public DeferredSingleLayerParsedTfOperation
1492 ParsedMatMulTfOperation(TfParser* parser, const tensorflow::NodeDef& node)
1493 : DeferredSingleLayerParsedTfOperation(parser, node)
1497 void CreateLayerDeferred() override
1499 BOOST_ASSERT(m_Layer == nullptr);
1500 m_Layer = m_Parser->AddFullyConnectedLayer(m_Node, nullptr, m_Node.name().c_str());
1504 ParsedTfOperationPtr TfParser::ParseMatMul(const tensorflow::NodeDef& nodeDef, const tensorflow::GraphDef& graphDef)
1506 // Defer the creation of the layer (see ParsedMatMulTfOperation).
1507 return std::make_unique<ParsedMatMulTfOperation>(this, nodeDef);
1510 ParsedTfOperationPtr TfParser::ParseMul(const tensorflow::NodeDef& nodeDef, const tensorflow::GraphDef& graphDef)
1512 boost::ignore_unused(graphDef);
1514 std::vector<OutputOfParsedTfOperation> inputs = GetInputParsedTfOperationsChecked(nodeDef, 2);
1516 IConnectableLayer* const layer = m_Network->AddMultiplicationLayer(nodeDef.name().c_str());
1517 IOutputSlot* input0Slot = &inputs[0].m_IndexedValue->ResolveArmnnOutputSlot(inputs[0].m_Index);
1518 IOutputSlot* input1Slot = &inputs[1].m_IndexedValue->ResolveArmnnOutputSlot(inputs[1].m_Index);
1520 auto const input0NumDims = input0Slot->GetTensorInfo().GetNumDimensions();
1521 auto const input1NumDims = input1Slot->GetTensorInfo().GetNumDimensions();
1523 if (input0NumDims < input1NumDims)
1525 const bool isNHWC = true;
1526 input0Slot = BroadcastForAddandMul(input1Slot, input0Slot, isNHWC, *m_Network, nodeDef);
1528 if (input1NumDims < input0NumDims)
1530 const bool isNHWC = true;
1531 input1Slot = BroadcastForAddandMul(input0Slot, input1Slot, isNHWC, *m_Network, nodeDef);
1534 input0Slot->Connect(layer->GetInputSlot(0));
1535 input1Slot->Connect(layer->GetInputSlot(1));
1537 if (input0NumDims < input1NumDims)
1539 layer->GetOutputSlot(0).SetTensorInfo(input1Slot->GetTensorInfo());
1543 layer->GetOutputSlot(0).SetTensorInfo(input0Slot->GetTensorInfo());
1545 return std::make_unique<SingleLayerParsedTfOperation>(this, nodeDef, layer);
1548 ParsedTfOperationPtr TfParser::ParsePlaceholder(const tensorflow::NodeDef& nodeDef,
1549 const tensorflow::GraphDef& graphDef)
1551 boost::ignore_unused(graphDef);
1553 std::vector<OutputOfParsedTfOperation> inputs = GetInputParsedTfOperationsChecked(nodeDef, 0);
1555 const LayerBindingId layerId = boost::numeric_cast<LayerBindingId>(m_NetworkInputsBindingInfo.size());
1557 auto it = m_InputShapes.find(nodeDef.name());
1558 if (it == m_InputShapes.end())
1560 throw ParseException("Missing input shape for Placeholder '" + nodeDef.name() + "'");
1562 TensorInfo tensorInfo(it->second, DataType::Float32);
1564 IConnectableLayer* const layer = m_Network->AddInputLayer(layerId, nodeDef.name().c_str());
1566 layer->GetOutputSlot(0).SetTensorInfo(tensorInfo);
1568 TrackInputBinding(layer, layerId, tensorInfo);
1570 return std::make_unique<SingleLayerParsedTfOperation>(this, nodeDef, layer);
1573 ParsedTfOperationPtr TfParser::ParseRelu(const tensorflow::NodeDef& nodeDef,
1574 const tensorflow::GraphDef& graphDef)
1576 boost::ignore_unused(graphDef);
1578 ActivationDescriptor activationDesc;
1579 activationDesc.m_Function = ActivationFunction::ReLu;
1580 return AddActivationLayer(nodeDef, activationDesc);
1583 ParsedTfOperationPtr TfParser::ParseRelu6(const tensorflow::NodeDef& nodeDef,
1584 const tensorflow::GraphDef& graphDef)
1586 boost::ignore_unused(graphDef);
1588 ActivationDescriptor activationDesc;
1589 activationDesc.m_Function = ActivationFunction::BoundedReLu;
1590 activationDesc.m_A = 6.0f;
1591 activationDesc.m_B = 0.0f;
1593 return AddActivationLayer(nodeDef, activationDesc);
1596 ParsedTfOperationPtr TfParser::ParseSigmoid(const tensorflow::NodeDef& nodeDef,
1597 const tensorflow::GraphDef& graphDef)
1599 boost::ignore_unused(graphDef);
1601 ActivationDescriptor activationDesc;
1602 activationDesc.m_Function = ActivationFunction::Sigmoid;
1604 return AddActivationLayer(nodeDef, activationDesc);
1607 ParsedTfOperationPtr TfParser::ParseSoftmax(const tensorflow::NodeDef& nodeDef,
1608 const tensorflow::GraphDef& graphDef)
1610 boost::ignore_unused(graphDef);
1612 std::vector<OutputOfParsedTfOperation> inputs = GetInputParsedTfOperationsChecked(nodeDef, 1);
1614 SoftmaxDescriptor softmaxDescriptor;
1615 IConnectableLayer* const layer = m_Network->AddSoftmaxLayer(softmaxDescriptor, nodeDef.name().c_str());
1617 IOutputSlot& prevLayerSlot = inputs[0].m_IndexedValue->ResolveArmnnOutputSlot(inputs[0].m_Index);
1618 prevLayerSlot.Connect(layer->GetInputSlot(0));
1619 layer->GetOutputSlot(0).SetTensorInfo(prevLayerSlot.GetTensorInfo());
1621 return std::make_unique<SingleLayerParsedTfOperation>(this, nodeDef, layer);
1624 ParsedTfOperationPtr TfParser::ParseSoftplus(const tensorflow::NodeDef& nodeDef,
1625 const tensorflow::GraphDef& graphDef)
1627 boost::ignore_unused(graphDef);
1629 ActivationDescriptor activationDesc;
1630 activationDesc.m_Function = ActivationFunction::SoftReLu;
1632 return AddActivationLayer(nodeDef, activationDesc);
1635 ParsedTfOperationPtr TfParser::ParseTanh(const tensorflow::NodeDef& nodeDef, const tensorflow::GraphDef& graphDef)
1637 boost::ignore_unused(graphDef);
1639 ActivationDescriptor activationDesc;
1640 activationDesc.m_Function = ActivationFunction::TanH;
1641 activationDesc.m_A = 1.0f;
1642 activationDesc.m_B = 1.0f;
1644 return AddActivationLayer(nodeDef, activationDesc);
1647 ParsedTfOperationPtr TfParser::AddActivationLayer(const tensorflow::NodeDef& nodeDef,
1648 ActivationDescriptor& activationDesc)
1650 std::vector<OutputOfParsedTfOperation> inputs = GetInputParsedTfOperationsChecked(nodeDef, 1);
1652 IConnectableLayer* const layer = m_Network->AddActivationLayer(activationDesc, nodeDef.name().c_str());
1654 IOutputSlot& prevLayerOutputSlot = inputs[0].m_IndexedValue->ResolveArmnnOutputSlot(inputs[0].m_Index);
1655 prevLayerOutputSlot.Connect(layer->GetInputSlot(0));
1656 layer->GetOutputSlot(0).SetTensorInfo(prevLayerOutputSlot.GetTensorInfo());
1657 return std::make_unique<SingleLayerParsedTfOperation>(this, nodeDef, layer);
1660 ParsedTfOperationPtr TfParser::ParseMaxPool(const tensorflow::NodeDef& nodeDef,
1661 const tensorflow::GraphDef& graphDef)
1663 return ParsePooling2d(nodeDef, graphDef, PoolingAlgorithm::Max);
1666 ParsedTfOperationPtr TfParser::ParseAvgPool(const tensorflow::NodeDef& nodeDef,
1667 const tensorflow::GraphDef& graphDef)
1669 return ParsePooling2d(nodeDef, graphDef, PoolingAlgorithm::Average);
1672 ParsedTfOperationPtr TfParser::ParsePooling2d(const tensorflow::NodeDef& nodeDef,
1673 const tensorflow::GraphDef& graphDef, PoolingAlgorithm pooltype)
1675 std::vector<OutputOfParsedTfOperation> inputs = GetInputParsedTfOperationsChecked(nodeDef, 1);
1676 IOutputSlot& inputSlot = inputs[0].m_IndexedValue->ResolveArmnnOutputSlot(inputs[0].m_Index);
1677 TensorInfo inputTensorInfo = inputSlot.GetTensorInfo();
1679 if (inputs.size() != 1)
1681 throw ParseException("2D Pooling expects one input!");
1684 std::string paddingString = ReadMandatoryNodeStringAttribute(nodeDef, "padding");
1685 std::string dataFormat = ReadMandatoryNodeStringAttribute(nodeDef, "data_format");
1686 std::vector<uint32_t> strides = ReadMandatoryNodeUint32ListAttribute(nodeDef, "strides");
1687 std::vector<uint32_t> ksize = ReadMandatoryNodeUint32ListAttribute(nodeDef, "ksize"); // size of pool windows
1689 Pooling2dDescriptor pooling2dDescriptor;
1690 pooling2dDescriptor.m_PoolType = pooltype;
1691 pooling2dDescriptor.m_PaddingMethod = PaddingMethod::Exclude;
1692 pooling2dDescriptor.m_OutputShapeRounding = OutputShapeRounding::Floor;
1694 if (dataFormat == "NHWC")
1696 pooling2dDescriptor.m_StrideX = strides[2];
1697 pooling2dDescriptor.m_StrideY = strides[1];
1698 pooling2dDescriptor.m_PoolWidth = ksize[2];
1699 pooling2dDescriptor.m_PoolHeight = ksize[1];
1700 // Swizzle input to supported memory layout
1701 inputTensorInfo = armnnUtils::Permuted(inputSlot.GetTensorInfo(), NHWCToArmNN);
1703 else if (dataFormat == "NCHW")
1705 pooling2dDescriptor.m_StrideX = strides[3];
1706 pooling2dDescriptor.m_StrideY = strides[2];
1707 pooling2dDescriptor.m_PoolWidth = ksize[3];
1708 pooling2dDescriptor.m_PoolHeight = ksize[2];
1712 throw ParseException("Only NHWC or NCHW supported for Pooling2d");
1715 uint32_t inputHeight = inputTensorInfo.GetShape()[2];
1716 uint32_t inputWidth = inputTensorInfo.GetShape()[3];
1718 bool padding = false;
1719 TensorInfo outputInfo;
1720 if (paddingString == "SAME")
1723 outputInfo = TensorInfo({ inputTensorInfo.GetShape()[0],
1724 inputTensorInfo.GetShape()[1],
1725 static_cast<uint32_t>(ceil(
1726 static_cast<float>(inputHeight) /
1727 static_cast<float>(pooling2dDescriptor.m_StrideY))),
1728 static_cast<uint32_t>(ceil(
1729 static_cast<float>(inputWidth) /
1730 static_cast<float>(pooling2dDescriptor.m_StrideX)))
1731 }, DataType::Float32);
1733 else if (paddingString == "VALID")
1736 outputInfo = TensorInfo({ inputTensorInfo.GetShape()[0],
1737 inputTensorInfo.GetShape()[1],
1738 static_cast<uint32_t>(ceil(
1739 static_cast<float>(inputHeight - pooling2dDescriptor.m_PoolHeight + 1) /
1740 static_cast<float>(pooling2dDescriptor.m_StrideY))),
1741 static_cast<uint32_t>(ceil(
1742 static_cast<float>(inputWidth - pooling2dDescriptor.m_PoolWidth + 1) /
1743 static_cast<float>(pooling2dDescriptor.m_StrideX)))
1744 }, DataType::Float32);
1748 throw ParseException("Only 'SAME' and 'VALID' padding supported");
1751 CalcPadding(inputWidth, pooling2dDescriptor.m_PoolWidth, pooling2dDescriptor.m_StrideX,
1752 pooling2dDescriptor.m_PadLeft, pooling2dDescriptor.m_PadRight, padding);
1753 CalcPadding(inputHeight, pooling2dDescriptor.m_PoolHeight, pooling2dDescriptor.m_StrideY,
1754 pooling2dDescriptor.m_PadTop, pooling2dDescriptor.m_PadBottom, padding);
1757 IConnectableLayer* layer = m_Network->AddPooling2dLayer(pooling2dDescriptor, nodeDef.name().c_str());
1758 if (layer == nullptr)
1760 throw ParseException("Failed to add pooling2d layer");
1763 layer->GetOutputSlot(0).SetTensorInfo(outputInfo);
1765 if (dataFormat == "NHWC")
1767 layer = SwizzleInDeswizzleOut(*m_Network, inputSlot, *layer, nodeDef.name());
1771 inputSlot.Connect(layer->GetInputSlot(0));
1774 return std::make_unique<SingleLayerParsedTfOperation>(this, nodeDef, layer);
1777 ParsedTfOperationPtr TfParser::AddAdditionLayer(const tensorflow::NodeDef& nodeDef, bool isBiasAdd)
1779 std::vector<OutputOfParsedTfOperation> inputs = GetInputParsedTfOperationsChecked(nodeDef, 2);
1781 IOutputSlot* input0Slot = &inputs[0].m_IndexedValue->ResolveArmnnOutputSlot(inputs[0].m_Index);
1782 IOutputSlot* input1Slot = &inputs[1].m_IndexedValue->ResolveArmnnOutputSlot(inputs[1].m_Index);
1784 const TensorInfo& input0Info = input0Slot->GetTensorInfo();
1785 const TensorInfo& input1Info = input1Slot->GetTensorInfo();
1789 // BiasAdd takes bias as a 1D tensor. We need to add a reshape layer to create a 4D tensor
1790 // with the same data in the correct dimension for broadcast in addition.
1791 if(input1Info.GetNumDimensions() != 1)
1793 throw ParseException("Unsupported bias for BiasAdd. It should be a 1D vector.");
1796 const std::string dataFormat = ReadMandatoryNodeStringAttribute(nodeDef, "data_format");
1797 const bool isNHWC = (dataFormat == "NHWC");
1798 const bool isNCHW = (dataFormat == "NCHW");
1800 if (!isNHWC && ! isNCHW)
1802 throw ParseException("Only NHWC or NCHW supported for BiasAdd");
1805 input1Slot = BroadcastForAddandMul(input0Slot, input1Slot, isNHWC, *m_Network, nodeDef);
1809 if (input0Info.GetNumDimensions() == 1)
1811 const bool isNHWC = true;
1812 input0Slot = BroadcastForAddandMul(input1Slot, input0Slot, isNHWC, *m_Network, nodeDef);
1815 if (input1Info.GetNumDimensions() == 1)
1817 const bool isNHWC = true;
1818 input1Slot = BroadcastForAddandMul(input0Slot, input1Slot, isNHWC, *m_Network, nodeDef);
1822 IConnectableLayer* const layer = m_Network->AddAdditionLayer(nodeDef.name().c_str());
1824 input0Slot->Connect(layer->GetInputSlot(0));
1825 input1Slot->Connect(layer->GetInputSlot(1));
1827 if (input0Info.GetNumDimensions() == 1 && isBiasAdd == false)
1829 layer->GetOutputSlot(0).SetTensorInfo(input1Slot->GetTensorInfo());
1833 layer->GetOutputSlot(0).SetTensorInfo(input0Slot->GetTensorInfo());
1836 return std::make_unique<SingleLayerParsedTfOperation>(this, nodeDef, layer);
1839 IConnectableLayer* TfParser::AddFullyConnectedLayer(const tensorflow::NodeDef& matMulNodeDef,
1840 const tensorflow::NodeDef* addNodeDef, const char* armnnLayerName)
1842 // find bias const (if applicable)
1843 ParsedConstTfOperation<float>* biasNode = nullptr;
1844 if (addNodeDef != nullptr)
1846 std::vector<OutputOfParsedTfOperation> addInputs = GetInputParsedTfOperationsChecked(*addNodeDef, 2);
1848 if (HasParsedConstTensor<float>(addInputs[0].m_IndexedValue->GetNode().name()))
1850 biasNode = boost::polymorphic_downcast<ParsedConstTfOperation<float>*>(addInputs[0].m_IndexedValue);
1852 else if (HasParsedConstTensor<float>(addInputs[1].m_IndexedValue->GetNode().name()))
1854 biasNode = boost::polymorphic_downcast<ParsedConstTfOperation<float>*>(addInputs[1].m_IndexedValue);
1858 throw ParseException("ArmNN only supports fully connected layers with constant bias");
1862 // find matmul inputs
1863 ParsedConstTfOperation<float>* weightNode = nullptr;
1864 ParsedTfOperation* inputNode = nullptr;
1865 unsigned int inputIdx = 0;
1866 std::vector<OutputOfParsedTfOperation> mulInputs = GetInputParsedTfOperationsChecked(matMulNodeDef, 2);
1867 if (HasParsedConstTensor<float>(mulInputs[0].m_IndexedValue->GetNode().name()))
1869 weightNode = boost::polymorphic_downcast<ParsedConstTfOperation<float>*>(mulInputs[0].m_IndexedValue);
1870 inputNode = mulInputs[1].m_IndexedValue;
1871 inputIdx = mulInputs[1].m_Index;
1873 else if (HasParsedConstTensor<float>(mulInputs[1].m_IndexedValue->GetNode().name()))
1875 weightNode = boost::polymorphic_downcast<ParsedConstTfOperation<float>*>(mulInputs[1].m_IndexedValue);
1876 inputNode = mulInputs[0].m_IndexedValue;
1877 inputIdx = mulInputs[0].m_Index;
1881 throw ParseException("ArmNN only supports fully connected layers with constant weights");
1884 std::vector<float> weightTensorData;
1886 ConstTensor weights = weightNode->GetConstTensor(false, weightTensorData);
1888 FullyConnectedDescriptor desc;
1889 desc.m_BiasEnabled = addNodeDef != nullptr;
1891 IConnectableLayer* layer = nullptr;
1893 if (addNodeDef != nullptr)
1895 std::vector<float> biasTensorData;
1896 ConstTensor biases = biasNode->GetConstTensor(false, biasTensorData);
1898 if (weights.GetShape()[1] != biases.GetShape()[0])
1900 throw ParseException("shape of matmul and bias do not match");
1903 layer = m_Network->AddFullyConnectedLayer(desc, weights, biases, armnnLayerName);
1907 layer = m_Network->AddFullyConnectedLayer(desc, weights, armnnLayerName);
1910 BOOST_ASSERT(layer != nullptr);
1912 inputNode->ResolveArmnnOutputSlot(inputIdx).Connect(layer->GetInputSlot(0));
1913 unsigned int batches = inputNode->ResolveArmnnOutputSlot(inputIdx).GetTensorInfo().GetShape()[0];
1916 TensorInfo outputInfo({ batches, weights.GetShape()[1] }, DataType::Float32);
1917 layer->GetOutputSlot(0).SetTensorInfo(outputInfo);
1921 void TfParser::LoadNodeDef(const tensorflow::NodeDef& nodeDef, const tensorflow::GraphDef& graphDef)
1923 // get the type of the node (assume float)
1924 tensorflow::DataType type = tensorflow::DT_FLOAT;
1925 if (nodeDef.attr().count("T") != 0)
1927 auto attr = nodeDef.attr().at("T");
1930 else if (nodeDef.attr().count("dtype") != 0)
1932 auto attr = nodeDef.attr().at("dtype");
1936 if (type != tensorflow::DT_FLOAT && nodeDef.op() != "Const")
1938 throw ParseException("Currently only FLOAT is supported for tensorflow nodes (apart from Const)");
1941 const std::string& operation = nodeDef.op();
1942 auto it = ms_OperationNameToParsingFunctions.find(operation);
1943 if (it != ms_OperationNameToParsingFunctions.end())
1945 auto func = it->second;
1946 ParsedTfOperationPtr parsedTfOperation = (this->*func)(nodeDef, graphDef);
1947 ParsedTfOperation* parsedTfOperationRaw = parsedTfOperation.get();
1949 // Store the parsed operation so that dependent layers can connect to it
1950 auto it = m_ParsedTfOperations.find(nodeDef.name());
1951 if (it != m_ParsedTfOperations.end())
1953 throw ParseException(boost::str(boost::format("Name %1% used by more than one node") % nodeDef.name()));
1955 m_ParsedTfOperations[nodeDef.name()] = std::move(parsedTfOperation);
1957 // If this node was requested as an output from the network then add an ArmNN output layer
1958 if (std::find(m_RequestedOutputs.begin(), m_RequestedOutputs.end(), nodeDef.name()) !=
1959 m_RequestedOutputs.end())
1961 auto outId = ParseOutputId(nodeDef.name());
1962 const LayerBindingId layerId = boost::numeric_cast<LayerBindingId>(m_NetworkOutputsBindingInfo.size());
1963 IOutputSlot& prevSlot = parsedTfOperationRaw->ResolveArmnnOutputSlot(outId.m_Index);
1965 TensorInfo tensorInfo = prevSlot.GetTensorInfo();
1967 IConnectableLayer* outputLayer = m_Network->AddOutputLayer(layerId, nodeDef.name().c_str());
1969 prevSlot.Connect(outputLayer->GetInputSlot(0));
1971 TrackOutputBinding(outputLayer, layerId, tensorInfo);
1976 throw ParseException(boost::str(
1977 boost::format("Unsupported operation %1% in tensorflow::GraphDef") % operation));
1981 void TfParser::LoadGraphDef(const tensorflow::GraphDef& graphDef)
1983 // add all nodes to our map
1984 m_NodesByName.clear();
1985 m_NetworkInputsBindingInfo.clear();
1986 m_NetworkOutputsBindingInfo.clear();
1988 for (int i = 0; i < graphDef.node_size(); ++i)
1990 const tensorflow::NodeDef& node = graphDef.node(i);
1991 m_NodesByName[node.name()] = &node;
1994 // Find the output nodes the user requested
1995 std::vector<const tensorflow::NodeDef*> targetNodes;
1996 for (const std::string& requestedOutputName : m_RequestedOutputs)
1998 auto nodeIt = m_NodesByName.find(requestedOutputName);
1999 if (nodeIt == m_NodesByName.end())
2001 throw ParseException("Couldn't find requested output node '" + requestedOutputName + "' in graph");
2003 targetNodes.push_back(nodeIt->second);
2006 // Sort them into a linear ordering such that all inputs of a node are before the node itself
2007 std::vector<const tensorflow::NodeDef*> sortedNodes;
2008 if (!armnnUtils::GraphTopologicalSort<const tensorflow::NodeDef*>(
2010 [this](const tensorflow::NodeDef* node)
2012 auto outputs = GetTfInputNodes(*node);
2013 std::vector<const tensorflow::NodeDef*> nodesOnly;
2014 for (const auto & o : outputs) {
2015 nodesOnly.push_back(o.m_IndexedValue);
2021 throw ParseException("Cycle detected in graph");
2024 // Parse each node in order, knowing that all inputs of a node will be processed before the node itself
2025 for (const auto& it : sortedNodes)
2027 const tensorflow::NodeDef& currentNode = *it;
2028 LoadNodeDef(currentNode, graphDef);
2032 INetworkPtr TfParser::CreateNetworkFromTextFile(const char* graphFile,
2033 const std::map<std::string, TensorShape>& inputShapes,
2034 const std::vector<std::string>& requestedOutputs)
2036 FILE* fd = fopen(graphFile, "r");
2040 std::stringstream error;
2041 error << "Graph file " << graphFile << " failed to open";
2042 throw FileNotFoundException(error.str());
2045 // Parse the file into a message
2046 tensorflow::GraphDef graphDef;
2047 auto input = new google::protobuf::io::FileInputStream(fileno(fd));
2048 bool success = google::protobuf::TextFormat::Parse(input, &graphDef);
2054 std::stringstream error;
2055 error << "Failed to parse graph file";
2056 throw ParseException(error.str());
2059 return CreateNetworkFromGraphDef(graphDef, inputShapes, requestedOutputs);
2062 INetworkPtr TfParser::CreateNetworkFromString(const char* protoText,
2063 const std::map<std::string, TensorShape>& inputShapes,
2064 const std::vector<std::string>& requestedOutputs)
2066 // Parse the string into a message
2067 tensorflow::GraphDef graphDef;
2068 bool success = google::protobuf::TextFormat::ParseFromString(protoText, &graphDef);
2072 std::stringstream error;
2073 error << "Failed to parse graph file";
2074 throw ParseException(error.str());
2077 return CreateNetworkFromGraphDef(graphDef, inputShapes, requestedOutputs);
2080 INetworkPtr TfParser::CreateNetworkFromBinaryFile(const char* graphFile,
2081 const std::map<std::string, TensorShape>& inputShapes,
2082 const std::vector<std::string>& requestedOutputs)
2084 FILE* fd = fopen(graphFile, "rb");
2088 std::stringstream error;
2089 error << "Graph file " << graphFile << " failed to open";
2090 throw FileNotFoundException(error.str());
2093 // Parse the file into a message
2094 tensorflow::GraphDef graphDef;
2096 google::protobuf::io::FileInputStream inStream(fileno(fd));
2097 google::protobuf::io::CodedInputStream codedStream(&inStream);
2098 codedStream.SetTotalBytesLimit(INT_MAX, INT_MAX);
2099 bool success = graphDef.ParseFromCodedStream(&codedStream);
2104 std::stringstream error;
2105 error << "Failed to parse protobuf file" << graphFile;
2106 throw ParseException(error.str());
2109 return CreateNetworkFromGraphDef(graphDef, inputShapes, requestedOutputs);
2112 INetworkPtr TfParser::CreateNetworkFromGraphDef(const tensorflow::GraphDef& graphDef,
2113 const std::map<std::string, TensorShape>& inputShapes,
2114 const std::vector<std::string>& requestedOutputs)
2116 m_Network = INetwork::Create();
2118 m_InputShapes = inputShapes;
2119 if (requestedOutputs.size() == 0)
2121 throw ParseException("requestedOutputs must have at least one entry");
2123 m_RequestedOutputs = requestedOutputs;
2127 LoadGraphDef(graphDef);
2129 catch (const ParseException& e)
2137 return std::move(m_Network);
2140 void TfParser::Cleanup()
2142 // cleanup, in case we reuse this parser
2143 m_InputShapes.clear();
2144 m_RequestedOutputs.clear();
2145 m_NodesByName.clear();
2146 m_ParsedTfOperations.clear();
2149 BindingPointInfo TfParser::GetNetworkInputBindingInfo(const std::string& name) const
2151 return GetBindingInfo(name, "input", m_NetworkInputsBindingInfo);
2154 BindingPointInfo TfParser::GetNetworkOutputBindingInfo(const std::string& name) const
2156 return GetBindingInfo(name, "output", m_NetworkOutputsBindingInfo);
2159 std::pair<LayerBindingId, TensorInfo> TfParser::GetBindingInfo(const std::string& layerName,
2160 const char* bindingPointDesc,
2161 const std::unordered_map<std::string, BindingPointInfo>& nameToBindingInfo)
2163 auto it = nameToBindingInfo.find(layerName);
2164 if (it == nameToBindingInfo.end())
2166 throw InvalidArgumentException(boost::str(boost::format("Unknown %1% '%2%'") % bindingPointDesc % layerName));
2171 void TfParser::TrackInputBinding(IConnectableLayer* layer, LayerBindingId id, const TensorInfo& tensorInfo)
2173 return TrackBindingPoint(layer, id, tensorInfo, "input", m_NetworkInputsBindingInfo);
2176 void TfParser::TrackOutputBinding(IConnectableLayer* layer, LayerBindingId id, const TensorInfo& tensorInfo)
2178 return TrackBindingPoint(layer, id, tensorInfo, "output", m_NetworkOutputsBindingInfo);
2181 void TfParser::TrackBindingPoint(IConnectableLayer* layer,
2183 const TensorInfo& tensorInfo,
2184 const char* bindingPointDesc,
2185 std::unordered_map<std::string, BindingPointInfo>& nameToBindingInfo)
2187 const std::string layerName = layer->GetName();
2188 auto it = nameToBindingInfo.find(layerName);
2189 if (it == nameToBindingInfo.end())
2191 nameToBindingInfo[layerName] = std::make_pair(id, tensorInfo);
2195 throw ParseException(boost::str(
2196 boost::format("Id %1% used by more than one %2% layer") % id % bindingPointDesc));
2200 } // namespace armnnTfParser