diff options
author | Nikhil Raj <nikhil.raj@arm.com> | 2021-04-19 16:59:48 +0100 |
---|---|---|
committer | Nikhil Raj <nikhil.raj@arm.com> | 2021-04-27 17:37:11 +0100 |
commit | 5d955cf70ae0c5558d4f431f0fc6bd4552cd43a5 (patch) | |
tree | 4fb59200899808b8b008d6f48322d0d799b8b631 /src/armnnTfParser | |
parent | 4a621c43174b6bdd9dc0bff839b245bc2139d6a6 (diff) | |
download | armnn-5d955cf70ae0c5558d4f431f0fc6bd4552cd43a5.tar.gz |
IVGCVSW-5721 Remove the Tensorflow Parser from ArmNN
Signed-off-by: Nikhil Raj <nikhil.raj@arm.com>
Change-Id: Ida37d3ee3a1af0c75aa905199bd861726c646846
Diffstat (limited to 'src/armnnTfParser')
44 files changed, 0 insertions, 11768 deletions
diff --git a/src/armnnTfParser/TfParser.cpp b/src/armnnTfParser/TfParser.cpp deleted file mode 100755 index 1e566fe943..0000000000 --- a/src/armnnTfParser/TfParser.cpp +++ /dev/null @@ -1,3745 +0,0 @@ -// -// Copyright © 2017 Arm Ltd and Contributors. All rights reserved. -// SPDX-License-Identifier: MIT -// - -#include "TfParser.hpp" - -#include "armnnTfParser/Version.hpp" - -#include <armnn/TypesUtils.hpp> -#include <armnn/Descriptors.hpp> - -#include <armnnUtils/Permute.hpp> -#include <armnnUtils/DataLayoutIndexed.hpp> -#include <armnnUtils/Transpose.hpp> -#include <armnn/utility/IgnoreUnused.hpp> -#include <armnn/utility/NumericCast.hpp> -#include <armnn/utility/PolymorphicDowncast.hpp> - -#include <GraphTopologicalSort.hpp> -#include <ParserHelper.hpp> - -#include <google/protobuf/io/zero_copy_stream_impl.h> -#include <google/protobuf/text_format.h> - -#include <tensorflow/core/framework/graph.pb.h> - -#include <fmt/core.h> -#include <fmt/format.h> -#include <iostream> -#include <numeric> - -using namespace armnnUtils; -using namespace armnn; - -namespace armnnTfParser -{ - -ITfParser::ITfParser() : pTfParserImpl(new ITfParser::TfParserImpl()){} - -ITfParser::~ITfParser() = default; - -ITfParser *ITfParser::CreateRaw() -{ - return new ITfParser(); -} - -ITfParserPtr ITfParser::Create() -{ - return ITfParserPtr(CreateRaw(), &ITfParser::Destroy); -} - -void ITfParser::Destroy(ITfParser *parser) -{ - delete parser; -} - -armnn::INetworkPtr ITfParser::CreateNetworkFromTextFile(const char* graphFile, - const std::map<std::string, armnn::TensorShape>& inputShapes, - const std::vector<std::string>& requestedOutputs) -{ - return pTfParserImpl->CreateNetworkFromTextFile(graphFile, inputShapes, requestedOutputs); -} - -armnn::INetworkPtr ITfParser::CreateNetworkFromBinaryFile(const char* graphFile, - const std::map<std::string, armnn::TensorShape>& inputShapes, - const std::vector<std::string>& requestedOutputs) -{ - return pTfParserImpl->CreateNetworkFromBinaryFile(graphFile, inputShapes, requestedOutputs); -} - -armnn::INetworkPtr ITfParser::CreateNetworkFromString(const char* protoText, - const std::map<std::string, armnn::TensorShape>& inputShapes, - const std::vector<std::string>& requestedOutputs) -{ - return pTfParserImpl->CreateNetworkFromString(protoText, inputShapes, requestedOutputs); -} - -BindingPointInfo ITfParser::GetNetworkInputBindingInfo(const std::string& name) const -{ - return pTfParserImpl->GetNetworkInputBindingInfo(name); -} - -BindingPointInfo ITfParser::GetNetworkOutputBindingInfo(const std::string& name) const -{ - return pTfParserImpl->GetNetworkOutputBindingInfo(name); -} -namespace -{ - -const PermutationVector NHWCToArmNN = { 0, 2, 3, 1 }; -const PermutationVector ArmNNToNHWC = { 0, 3, 1, 2 }; - - -template <typename Callable> -void ReadMandatoryNodeAttributeImpl(const tensorflow::NodeDef& nodeDef, - const std::string& attribName, - tensorflow::AttrValue::ValueCase expectedValueCase, - Callable callable) -{ - auto iter = nodeDef.attr().find(attribName); - if (iter != nodeDef.attr().end()) - { - const auto& attrValue = iter->second; - if (attrValue.value_case() == expectedValueCase) - { - callable(attrValue); - } - else - { - throw ParseException( - fmt::format("Attribute {} of node {} expected to have {} as tensorflow::AttrValue::ValueCase, " - "but found {} instead {}", - attribName, - nodeDef.name(), - static_cast<int>(expectedValueCase), - static_cast<int>(attrValue.value_case()), - CHECK_LOCATION().AsString())); - } - } - else - { - throw ParseException( - fmt::format("Could not find required attribute {} in node {} {}", - attribName, - nodeDef.name(), - CHECK_LOCATION().AsString())); - } -} - -template <typename Callable> -void ReadOptionalNodeAttributeImpl(const tensorflow::NodeDef& nodeDef, - const std::string& attribName, - tensorflow::AttrValue::ValueCase expectedValueCase, - Callable callable) -{ - auto iter = nodeDef.attr().find(attribName); - if (iter != nodeDef.attr().end()) - { - const auto& attrValue = iter->second; - if (attrValue.value_case() == expectedValueCase) - { - callable(attrValue); - } - else - { - throw ParseException( - fmt::format("Attribute {} of node {} expected to have {} as tensorflow::AttrValue::ValueCase, " - "but found {} instead {}", - attribName, - nodeDef.name(), - static_cast<int>(expectedValueCase), - static_cast<int>(attrValue.value_case()), - CHECK_LOCATION().AsString())); - } - } -} - -float ReadMandatoryNodeFloatAttribute(const tensorflow::NodeDef& nodeDef, const std::string& name) -{ - float attribValue = 0.0f; - ReadMandatoryNodeAttributeImpl(nodeDef, name, tensorflow::AttrValue::kF, - [&attribValue](const tensorflow::AttrValue& attrValue) - { - attribValue = attrValue.f(); - }); - return attribValue; -} - -int32_t ReadMandatoryNodeInt32Attribute(const tensorflow::NodeDef& nodeDef, const std::string& name) -{ - int32_t attribValue = 0u; - ReadMandatoryNodeAttributeImpl(nodeDef, name, tensorflow::AttrValue::kI, - [&attribValue](const tensorflow::AttrValue& attrValue) - { - attribValue = static_cast<int32_t>(attrValue.i()); - }); - return attribValue; -} - -bool ReadMandatoryNodeBoolAttribute(const tensorflow::NodeDef& nodeDef, const std::string& name) -{ - bool attribValue = false; - ReadMandatoryNodeAttributeImpl(nodeDef, name, tensorflow::AttrValue::kB, - [&attribValue](const tensorflow::AttrValue& attrValue) - { - attribValue = static_cast<bool>(attrValue.b()); - }); - return attribValue; -} - -uint32_t ReadMandatoryNodeUint32Attribute(const tensorflow::NodeDef& nodeDef, const std::string& name) -{ - uint32_t attribValue = 0u; - ReadMandatoryNodeAttributeImpl(nodeDef, name, tensorflow::AttrValue::kI, - [&attribValue](const tensorflow::AttrValue& attrValue) - { - attribValue = static_cast<uint32_t>(attrValue.i()); - }); - return attribValue; -} - -std::string ReadMandatoryNodeStringAttribute(const tensorflow::NodeDef& nodeDef, const std::string& name) -{ - std::string attribValue = ""; - ReadMandatoryNodeAttributeImpl(nodeDef, name, tensorflow::AttrValue::kS, - [&attribValue](const tensorflow::AttrValue& attrValue) - { - attribValue = attrValue.s(); - }); - return attribValue; -} - -std::vector<uint32_t> ReadMandatoryNodeUint32ListAttribute(const tensorflow::NodeDef& nodeDef, - const std::string& name) -{ - std::vector<uint32_t> attriList; - ReadMandatoryNodeAttributeImpl(nodeDef, name, tensorflow::AttrValue::kList, - [&attriList](const tensorflow::AttrValue& attrValue) - { - for (int attriNum = 0; attriNum < attrValue.list().i_size(); ++attriNum) - { - attriList.push_back(static_cast<uint32_t>(attrValue.list().i(attriNum))); - } - }); - - return attriList; -} - -std::vector<uint32_t> ReadOptionalNodeUint32ListAttribute(const tensorflow::NodeDef& nodeDef, - const std::string& name) -{ - std::vector<uint32_t> attriList; - ReadOptionalNodeAttributeImpl(nodeDef, name, tensorflow::AttrValue::kList, - [&attriList](const tensorflow::AttrValue& attrValue) - { - for (int attriNum = 0; attriNum < attrValue.list().i_size(); ++attriNum) - { - attriList.push_back(static_cast<uint32_t>(attrValue.list().i(attriNum))); - } - }); - - return attriList; -} - -std::string ReadOptionalNodeStringAttribute(const tensorflow::NodeDef& nodeDef, - const std::string& name, - const std::string& defaultValue = "") -{ - std::string attribValue = defaultValue; - ReadOptionalNodeAttributeImpl(nodeDef, name, tensorflow::AttrValue::kS, - [&attribValue](const tensorflow::AttrValue& attrValue) - { - attribValue = attrValue.s(); - }); - return attribValue; -} - -bool ReadOptionalNodeBoolAttribute(const tensorflow::NodeDef& nodeDef, - const std::string& name, - bool defaultValue = false) -{ - bool attribValue = defaultValue; - ReadOptionalNodeAttributeImpl(nodeDef, name, tensorflow::AttrValue::kB, - [&attribValue](const tensorflow::AttrValue& attrValue) - { - attribValue = attrValue.b(); - }); - return attribValue; -} - -tensorflow::DataType ReadMandatoryNodeTypeAttribute(const tensorflow::NodeDef& nodeDef, const std::string& name) -{ - tensorflow::DataType attribValue = tensorflow::DT_INVALID; - ReadMandatoryNodeAttributeImpl(nodeDef, name, tensorflow::AttrValue::kType, - [&attribValue](const tensorflow::AttrValue& attrValue) - { - attribValue = attrValue.type(); - }); - return attribValue; -} - -TensorInfo PrepareReshape(const TensorInfo& input, const std::vector<int32_t>& targetDims) -{ - std::vector<unsigned int> outDims(targetDims.begin(), targetDims.end()); - const auto stretchDim = std::find(targetDims.begin(), targetDims.end(), -1); - - if (stretchDim != targetDims.end()) - { - if (std::find(std::next(stretchDim), targetDims.end(), -1) != targetDims.end()) - { - throw ParseException( - fmt::format("At most one component of shape can be -1 {}", - CHECK_LOCATION().AsString())); - } - - auto targetNumElements = - armnn::numeric_cast<unsigned int>( - std::accumulate(targetDims.begin(), targetDims.end(), -1, std::multiplies<int32_t>())); - auto stretchIndex = static_cast<size_t>(std::distance(targetDims.begin(), stretchDim)); - outDims[stretchIndex] = input.GetNumElements() / targetNumElements; - } - - TensorInfo reshapeInfo = input; - reshapeInfo.SetShape(TensorShape{ static_cast<unsigned int>(outDims.size()), outDims.data() }); - - return reshapeInfo; -} - -// We need the input0Slot to guide the reshape for input1Slot. -IOutputSlot* AddBroadcastReshapeLayer(IOutputSlot* input0Slot, IOutputSlot* input1Slot, bool isNHWC, - INetwork& m_Network, const tensorflow::NodeDef& nodeDef) -{ - const TensorInfo& input1Info = input1Slot->GetTensorInfo(); - const TensorInfo inputTensorInfo = input0Slot->GetTensorInfo(); - const unsigned int matchDim = inputTensorInfo.GetNumDimensions() - (isNHWC ? 1 : 3); - std::array<unsigned int, MaxNumOfTensorDimensions> reshapedDimensions; - std::fill_n(reshapedDimensions.begin(), inputTensorInfo.GetNumDimensions(), 1); - reshapedDimensions[matchDim] = input1Info.GetShape()[0]; - - armnn::TensorInfo reshapedInfo = input1Info; - reshapedInfo.SetShape(TensorShape{ inputTensorInfo.GetNumDimensions(), reshapedDimensions.data() }); - - const std::string reshapeLayerName = "reshape_for-" + nodeDef.name(); - ReshapeDescriptor reshapeDesc; - reshapeDesc.m_TargetShape = reshapedInfo.GetShape(); - IConnectableLayer* const reshapeLayer = m_Network.AddReshapeLayer(reshapeDesc, reshapeLayerName.c_str()); - - input1Slot->Connect(reshapeLayer->GetInputSlot(0)); - reshapeLayer->GetOutputSlot(0).SetTensorInfo(reshapedInfo); - - input1Slot = &reshapeLayer->GetOutputSlot(0); - - return input1Slot; -} - -OutputId ParseOutputId(const std::string & name) -{ - unsigned int outputNum = 0; - size_t colonPos = name.find_last_of(":"); - if (colonPos != std::string::npos) - { - int n = std::stoi(name.substr(colonPos+1)); - if (n<0 || n>100) - { - throw ParseException( - fmt::format("Output tensor id is out of range for {} {}", - name, - CHECK_LOCATION().AsString())); - } - outputNum = static_cast<unsigned int>(n); - } - return OutputId(name.substr(0,colonPos),outputNum); -} - -#define CHECK_DATA_FORMAT(NODE_DEF, FORMAT, NODE_TYPE) \ - if( FORMAT != "NHWC" && FORMAT != "NCHW" ) \ - { \ - throw ParseException( \ - fmt::format("Unsupported data format {} passed for {} node {}. " \ - "Only NHWC and NCHW supported {}", \ - FORMAT, \ - NODE_TYPE, \ - NODE_DEF.name(), \ - CHECK_LOCATION().AsString())); \ - } - -#define CHECK_PADDING_TYPE(NODE_DEF, PADDING) \ - if(PADDING != "SAME" && PADDING != "VALID" ) \ - { \ - throw ParseException( \ - fmt::format("Only 'SAME' and 'VALID' padding supported. Got {} for {} {}", \ - PADDING, \ - NODE_DEF.name(), \ - CHECK_LOCATION().AsString())); \ - } \ - -} // namespace - -const std::map<std::string, ITfParser::TfParserImpl::OperationParsingFunction> - ITfParser::TfParserImpl::ms_OperationNameToParsingFunctions = { - { "Const", &TfParserImpl::ParseConst }, - { "Add", &TfParserImpl::ParseAdd }, - { "AddN", &TfParserImpl::ParseAddN }, - { "BiasAdd", &TfParserImpl::ParseBiasAdd }, - { "Identity", &TfParserImpl::ParseIdentity }, - { "Conv2D", &TfParserImpl::ParseConv2D }, - { "DepthwiseConv2dNative", &TfParserImpl::ParseDepthwiseConv2D }, - { "ExpandDims", &TfParserImpl::ParseExpandDims }, - { "FusedBatchNorm", &TfParserImpl::ParseFusedBatchNorm }, - { "Gather", &TfParserImpl::ParseGather}, - { "Greater", &TfParserImpl::ParseGreater}, - { "ConcatV2", &TfParserImpl::ParseConcat }, - { "LRN", &TfParserImpl::ParseLrn }, - { "MatMul", &TfParserImpl::ParseMatMul }, - { "Mean", &TfParserImpl::ParseMean }, - { "Mul", &TfParserImpl::ParseMul }, - { "Placeholder", &TfParserImpl::ParsePlaceholder }, - { "RealDiv", &TfParserImpl::ParseRealDiv }, - { "Relu", &TfParserImpl::ParseRelu }, - { "Relu6", &TfParserImpl::ParseRelu6 }, - { "Reshape", &TfParserImpl::ParseReshape }, - { "ResizeBilinear", &TfParserImpl::ParseResizeBilinear }, - { "Rsqrt", &TfParserImpl::ParseRsqrt }, - { "Shape", &TfParserImpl::ParseShape }, - { "Squeeze", &TfParserImpl::ParseSqueeze }, - { "Sigmoid", &TfParserImpl::ParseSigmoid }, - { "Softmax", &TfParserImpl::ParseSoftmax }, - { "Softplus", &TfParserImpl::ParseSoftplus }, - { "Split", &TfParserImpl::ParseSplit }, - { "StridedSlice", &TfParserImpl::ParseStridedSlice }, - { "Tanh", &TfParserImpl::ParseTanh }, - { "MaxPool", &TfParserImpl::ParseMaxPool }, - { "AvgPool", &TfParserImpl::ParseAvgPool }, - { "Maximum", &TfParserImpl::ParseMaximum }, - { "Minimum", &TfParserImpl::ParseMinimum }, - { "Equal", &TfParserImpl::ParseEqual }, - { "Pad", &TfParserImpl::ParsePad }, - { "Sub", &TfParserImpl::ParseSub }, - { "Pack" , &TfParserImpl::ParseStack }, - { "Stack", &TfParserImpl::ParseStack }, - { "Transpose", &TfParserImpl::ParseTranspose }, -}; - -const std::list<std::string> ITfParser::TfParserImpl::m_ControlInputs = { - "Assert" -}; - -void CalcPadding(uint32_t inputSize, - uint32_t filterSize, - uint32_t stride, - uint32_t dilation, - uint32_t& paddingFront, - uint32_t& paddingBack, - bool samePadding) -{ - paddingFront = 0; - paddingBack = 0; - if (samePadding) - { - uint32_t outputSize = (inputSize + stride - 1) / stride; - uint32_t dilatedSize = filterSize + (dilation - 1) * (filterSize - 1); - uint32_t temp = (outputSize - 1) * stride + dilatedSize; - if (temp > inputSize) - { - paddingFront = (temp - inputSize) / 2; - paddingBack = (temp - inputSize) - paddingFront; - } - } -} - -/// An Abstract base class which represents a single tensorflow operation (node) -/// that has been (potentially partially) converted to Armnn. -/// It may not yet have been fully converted into actual Armnn layers. -class ParsedTfOperation -{ -public: - ParsedTfOperation(ITfParser::TfParserImpl* parser, const tensorflow::NodeDef& node) - : m_Parser(parser) - , m_Node(node) - { - } - - virtual ~ParsedTfOperation() {}; - - const tensorflow::NodeDef& GetNode() const { return m_Node; } - - /// Gets the ArmNN IOutputSlot corresponding to the given output index of the Tensorflow operation. - /// This may result in the creation of Armnn layers if this was deferred (e.g. see ParsedConstTfOperation). - virtual IOutputSlot& ResolveArmnnOutputSlot(unsigned int tfOutputIndex) = 0; - - /// If this operation is an Identity then this will follow return the 'parent' operation (recursively). - virtual ParsedTfOperation* ResolveIdentityOperations() - { - return this; - } - -protected: - ITfParser::TfParserImpl* m_Parser; - const tensorflow::NodeDef& m_Node; -}; - -/// An ParsedTfOperation where the Armnn equivalent is a single layer, -/// with output slots that correspond directly to the Tf node outputs. -class SingleLayerParsedTfOperation : public ParsedTfOperation -{ -public: - SingleLayerParsedTfOperation(ITfParser::TfParserImpl* parser, - const tensorflow::NodeDef& node, - IConnectableLayer* layer) - : ParsedTfOperation(parser, node) - , m_Layer(layer) - { - } - - IOutputSlot& ResolveArmnnOutputSlot(unsigned int tfOutputIndex) override - { - ARMNN_ASSERT(m_Layer); - // Assumes one-to-one mapping between Tf and armnn output slots. - unsigned int armnnOutputSlotIdx = tfOutputIndex; - if (armnnOutputSlotIdx >= m_Layer->GetNumOutputSlots()) - { - throw ParseException( - fmt::format("The requested output slot #{} " - "for {} does not exist {}", - armnnOutputSlotIdx, - m_Layer->GetName(), - CHECK_LOCATION().AsString())); - } - return m_Layer->GetOutputSlot(armnnOutputSlotIdx); - } - -protected: - IConnectableLayer* m_Layer; -}; - -/// A SingleLayerParsedTfOperation for deferred layer creation. -class DeferredSingleLayerParsedTfOperation : public SingleLayerParsedTfOperation -{ -public: - DeferredSingleLayerParsedTfOperation(ITfParser::TfParserImpl* parser, const tensorflow::NodeDef& node) - : SingleLayerParsedTfOperation(parser, node, nullptr) - { - } - - IOutputSlot& ResolveArmnnOutputSlot(unsigned int tfOutputIndex) override - { - if (!m_Layer) - { - CreateLayerDeferred(); - } - return SingleLayerParsedTfOperation::ResolveArmnnOutputSlot(tfOutputIndex); - } - -private: - virtual void CreateLayerDeferred() = 0; -}; - - -ITfParser::TfParserImpl::TfParserImpl() - : m_Network(nullptr, nullptr) -{ -} - - -const tensorflow::NodeDef* ITfParser::TfParserImpl::ResolveIdentityNode(const tensorflow::NodeDef* nodeDef) -{ - if (nodeDef->op() != "Identity") - { - return nodeDef; - } - - if (nodeDef->input_size() != 1) - { - throw ParseException( - fmt::format("Identity node should have a single input! {} has {} inputs {}", - nodeDef->name(), - nodeDef->input_size(), - CHECK_LOCATION().AsString())); - } - - auto it = m_NodesByName.find(nodeDef->input(0)); - if (it != m_NodesByName.end()) - { - const tensorflow::NodeDef* inputNode = it->second; - return ResolveIdentityNode(inputNode); - } - else - { - throw ParseException( - fmt::format("Cannot find what the Identity node {} is linked to! {}", - nodeDef->name(), - CHECK_LOCATION().AsString())); - } -} - -std::vector<OutputOfConstNodeDef> -ITfParser::TfParserImpl::GetTfInputNodes(const tensorflow::NodeDef& nodeDef) const -{ - std::vector<OutputOfConstNodeDef> ret; - - if (nodeDef.op() == "Const") - { - // For some reason const node can have "Control Inputs". We ignore them for now. - return ret; - } - - ret.reserve(armnn::numeric_cast<size_t>(nodeDef.input_size())); - for (int j = 0; j < nodeDef.input_size(); ++j) - { - OutputId outputId = ParseOutputId(nodeDef.input(j)); - - if (nodeDef.input(j)[0] == '^') // I couldn't find a better test for control inputs. - { - // We currently allow Control Input from TensorFlow graph but we ignore them from ArmNN graph. - continue; - } - - auto inputIt = m_NodesByName.find(outputId.m_IndexedValue); - if (inputIt == m_NodesByName.end()) - { - throw ParseException( - fmt::format("Can't find node '{}', which is listed as an input of '{}' {}", - nodeDef.input(j), - nodeDef.name(), - CHECK_LOCATION().AsString())); - } - ret.push_back(OutputOfConstNodeDef(inputIt->second,outputId.m_Index)); - } - - return ret; -} - -std::vector<OutputOfParsedTfOperation> -ITfParser::TfParserImpl::GetInputParsedTfOperationsChecked(const tensorflow::NodeDef& nodeDef, - std::size_t expectedNumInputs) -{ - // Fetches the tensorflow nodes connected as inputs and validate the size. - std::vector<OutputOfConstNodeDef> nodes = GetTfInputNodes(nodeDef); - const std::size_t numInputs = nodes.size(); - if (numInputs != expectedNumInputs) - { - throw ParseException( - fmt::format("Unexpected number of inputs for node {}. Expected {}, found {} {}", - nodeDef.name(), - expectedNumInputs, - numInputs, - CHECK_LOCATION().AsString())); - } - // Fetches the corresponding ParsedTfOperation operations - std::vector<OutputOfParsedTfOperation> result; - for (auto&& node : nodes) - { - auto it = m_ParsedTfOperations.find(node.m_IndexedValue->name()); - if (it == m_ParsedTfOperations.end()) - { - throw ParseException( - fmt::format("Node with name '{}' has not been parsed {}", - node.m_IndexedValue->name(), - CHECK_LOCATION().AsString())); - } - ParsedTfOperation* parsedOp = it->second.get(); - // Transparently 'skip' any Identity operations. This simplifies the logic inside the ParseXXX() functions. - parsedOp = parsedOp->ResolveIdentityOperations(); - result.push_back(OutputOfParsedTfOperation(parsedOp,node.m_Index)); - } - return result; -} - -IConnectableLayer* ITfParser::TfParserImpl::CreateAdditionLayer( - const tensorflow::NodeDef& nodeDef, - IOutputSlot* input0Slot, - IOutputSlot* input1Slot, - const std::string& layerName) -{ - const TensorInfo& input0Info = input0Slot->GetTensorInfo(); - const TensorInfo& input1Info = input1Slot->GetTensorInfo(); - - const unsigned int input0Dim = input0Info.GetNumDimensions(); - const unsigned int input1Dim = input1Info.GetNumDimensions(); - if (input0Dim != input1Dim) - { - // broadcasting where input0 and input1 have different number of dimensions - // is only supported for 1D and 4D tensors pair - if (input0Dim == 1 && input1Dim == 4) - { - input0Slot = AddBroadcastReshapeLayer(input1Slot, input0Slot, true, *m_Network, nodeDef); - } - else if (input0Dim == 4 && input1Dim == 1) - { - input1Slot = AddBroadcastReshapeLayer(input0Slot, input1Slot, true, *m_Network, nodeDef); - } - else - { - throw ParseException( - fmt::format("Unsupported broadcast configuration for {} operation {} {}", - layerName, - nodeDef.name(), - CHECK_LOCATION().AsString())); - } - } - IConnectableLayer* const layer = m_Network->AddAdditionLayer(layerName.c_str()); - - input0Slot->Connect(layer->GetInputSlot(0)); - input1Slot->Connect(layer->GetInputSlot(1)); - - // Ensure the output tensor has the correct dimensions even if a broadcast has been done - TensorInfo outputInfo = input0Slot->GetTensorInfo(); - std::vector<unsigned int> outputShape; - - const TensorShape& input0Shape = input0Slot->GetTensorInfo().GetShape(); - const TensorShape& input1Shape = input1Slot->GetTensorInfo().GetShape(); - - for (unsigned int i = 0; i < input0Shape.GetNumDimensions(); i++) - { - outputShape.push_back(std::max(input0Shape[i], input1Shape[i])); - } - - outputInfo.SetShape(TensorShape(input0Shape.GetNumDimensions(), outputShape.data())); - layer->GetOutputSlot(0).SetTensorInfo(outputInfo); - - return layer; -} - -IConnectableLayer* ITfParser::TfParserImpl::CreateAdditionLayer( - const tensorflow::NodeDef& nodeDef, - IConnectableLayer* layerOne, - IConnectableLayer* layerTwo, - unsigned int numberOfAddition, - unsigned long numberOfLayersToConnect, - bool isOdd) -{ - IOutputSlot* input0Slot = &layerOne->GetOutputSlot(0); - IOutputSlot* input1Slot = &layerTwo->GetOutputSlot(0); - std::string layerName(nodeDef.name()); - if (isOdd || numberOfLayersToConnect != 2) - { - // we are not connecting the final layer - layerName.append("_addN_").append(std::to_string(numberOfAddition)); - } - return CreateAdditionLayer(nodeDef, input0Slot, input1Slot, layerName); -} - -IConnectableLayer* ITfParser::TfParserImpl::CreateAdditionLayer( - const tensorflow::NodeDef& nodeDef, - const OutputOfParsedTfOperation& opOne, - const OutputOfParsedTfOperation& opTwo, - unsigned int numberOfAddition) -{ - IOutputSlot* input0Slot = &opOne.m_IndexedValue->ResolveArmnnOutputSlot(opOne.m_Index); - IOutputSlot* input1Slot = &opTwo.m_IndexedValue->ResolveArmnnOutputSlot(opTwo.m_Index); - std::string layerName(nodeDef.name()); - layerName.append("_addN_").append(std::to_string(numberOfAddition)); - return CreateAdditionLayer(nodeDef, input0Slot, input1Slot, layerName); -} - -IConnectableLayer* ITfParser::TfParserImpl::CreateAdditionLayer( - const tensorflow::NodeDef& nodeDef, - const OutputOfParsedTfOperation& op, - IConnectableLayer* layer) -{ - IOutputSlot* input0Slot = &op.m_IndexedValue->ResolveArmnnOutputSlot(op.m_Index); - IOutputSlot* input1Slot = &layer->GetOutputSlot(0); - return CreateAdditionLayer(nodeDef, input0Slot, input1Slot, nodeDef.name()); -} - -ParsedTfOperationPtr ITfParser::TfParserImpl::ParseAddN(const tensorflow::NodeDef& nodeDef, - const tensorflow::GraphDef& graphDef) -{ - IgnoreUnused(graphDef); - uint32_t numberOfInputs = ReadMandatoryNodeUint32Attribute(nodeDef, "N"); - if (numberOfInputs < 2) - { - // should never happen - throw ParseException( - fmt::format("AddN Node with name '{}' has less than two ({}) inputs {}", - nodeDef.name(), - std::to_string(numberOfInputs), - CHECK_LOCATION().AsString())); - } - else if (numberOfInputs == 2) - { - //this is the same as a simple Add operation - return AddAdditionLayer(nodeDef, false); - } - else - { - // build a binary tree of Add layers and return the final Add as the return from the function - // if we have an odd number of inputs then the final Add will consist of a layer connecting to an - // OutputOfParsedTfOperation, otherwise it will be two layers being added together - std::vector<OutputOfParsedTfOperation> inputs = GetInputParsedTfOperationsChecked(nodeDef, numberOfInputs); - unsigned int numberOfAdditions = 0; - std::vector<IConnectableLayer*> layers; - // NOTE: at this point we will have a minimum of three inputs - for (unsigned int i = 0; i < numberOfInputs; ++i) - { - // every time i is odd we have two inputs to process. - bool onSecondItem = i % 2; - if (onSecondItem) - { - ++numberOfAdditions; - IConnectableLayer* newLayer = CreateAdditionLayer( - nodeDef, inputs[ i - 1], inputs[i], numberOfAdditions); - layers.push_back(newLayer); - } - } - - std::vector<IConnectableLayer*> layersToConnect(layers); - unsigned long numberOfLayersToConnect = layersToConnect.size(); - bool isOdd = numberOfInputs % 2; - - while (numberOfLayersToConnect > 1) - { - layers.clear(); - for (unsigned long i = 0; i < numberOfLayersToConnect; ++i) { - bool onSecondItem = i % 2; - if (onSecondItem) { - ++numberOfAdditions; - IConnectableLayer* newLayer = CreateAdditionLayer( - nodeDef, - layersToConnect[i - 1], - layersToConnect[i], - numberOfAdditions, - numberOfLayersToConnect, - isOdd); - layers.push_back(newLayer); - } - } - //OK... need to go again... maybe - layersToConnect = layers; - numberOfLayersToConnect = layersToConnect.size(); - } - IConnectableLayer* finalLayer = layersToConnect[0]; - // if we had an odd number of inputs we need to connect the final layer to the - // last OutputOfParsedTfOperation in order to create the last Add layer we will - // be handing back. - if (isOdd) - { - // connect the final layer to the last op - finalLayer = CreateAdditionLayer(nodeDef, inputs[numberOfInputs - 1], finalLayer); - } - return std::make_unique<SingleLayerParsedTfOperation>(this, nodeDef, finalLayer); - } -} - -ParsedTfOperationPtr ITfParser::TfParserImpl::ParseAdd(const tensorflow::NodeDef& nodeDef, - const tensorflow::GraphDef& graphDef) -{ - IgnoreUnused(graphDef); - std::vector<OutputOfParsedTfOperation> inputs = GetInputParsedTfOperationsChecked(nodeDef, 2); - - // If one of the inputs is a MatMul and the other is a const, then we handle both nodes - // together as FullyConnected. - if (inputs[0].m_IndexedValue->GetNode().op() == "MatMul" && - HasParsedConstTensor<float>(inputs[1].m_IndexedValue->GetNode().name())) - { - IConnectableLayer* layer = - AddFullyConnectedLayer(inputs[0].m_IndexedValue->GetNode(), - &nodeDef,nodeDef.name().c_str()); - return std::make_unique<SingleLayerParsedTfOperation>(this, nodeDef, layer); - } - else if (HasParsedConstTensor<float>(inputs[0].m_IndexedValue->GetNode().name()) && - inputs[1].m_IndexedValue->GetNode().op() == "MatMul") - { - IConnectableLayer* layer = - AddFullyConnectedLayer(inputs[1].m_IndexedValue->GetNode(), - &nodeDef,nodeDef.name().c_str()); - return std::make_unique<SingleLayerParsedTfOperation>(this, nodeDef, layer); - } - else - { - // Otherwise it's just a regular addition. - return AddAdditionLayer(nodeDef); - } -} - -ParsedTfOperationPtr ITfParser::TfParserImpl::ParseBiasAdd(const tensorflow::NodeDef& nodeDef, - const tensorflow::GraphDef& graphDef) -{ - IgnoreUnused(graphDef); - return AddAdditionLayer(nodeDef, true); -} - -/// An ParsedTfOperation which forwards to another (used for Identity nodes). -class ParsedIdentityTfOperation : public ParsedTfOperation -{ -public: - ParsedIdentityTfOperation(ITfParser::TfParserImpl* parser, - const tensorflow::NodeDef& node, - ParsedTfOperation* representative) - : ParsedTfOperation(parser, node) - , m_Representative(representative) - { - } - - virtual IOutputSlot& ResolveArmnnOutputSlot(unsigned int tfOutputIndex) override - { - ARMNN_ASSERT(m_Representative); - return m_Representative->ResolveArmnnOutputSlot(tfOutputIndex); - } - - virtual ParsedTfOperation* ResolveIdentityOperations() override - { - return m_Representative->ResolveIdentityOperations(); - } - -private: - ParsedTfOperation* m_Representative; -}; - -ParsedTfOperationPtr ITfParser::TfParserImpl::ParseIdentity(const tensorflow::NodeDef& nodeDef, - const tensorflow::GraphDef& graphDef) -{ - IgnoreUnused(graphDef); - std::vector<OutputOfParsedTfOperation> inputs = GetInputParsedTfOperationsChecked(nodeDef, 1); - // Any requests for the output slots of this node should be forwarded to the node connected as input. - return std::make_unique<ParsedIdentityTfOperation>(this, nodeDef, inputs[0].m_IndexedValue); -} - -/// An ParsedTfOperation for a Const node. -/// Creation of the armnn ConstLayer is deferred until it is actually needed, because Const nodes are mostly used -/// for weight inputs to MatMul/Conv2D nodes and in these cases armnn doesn't need a ConstLayer. -template <typename T> -class ParsedConstTfOperation : public DeferredSingleLayerParsedTfOperation -{ -public: - ParsedConstTfOperation(ITfParser::TfParserImpl* parser, const tensorflow::NodeDef& node, - const T* tensorData, const TensorInfo& tensorInfo) - : DeferredSingleLayerParsedTfOperation(parser, node), - m_Storage(tensorData, tensorData + tensorInfo.GetNumElements()), - m_TensorInfo(tensorInfo) - { - ARMNN_ASSERT(GetDataTypeSize(tensorInfo.GetDataType()) == sizeof(T)); - } - - void CreateLayerDeferred() override - { - ARMNN_ASSERT(m_Layer == nullptr); - m_Layer = m_Parser->m_Network->AddConstantLayer(ConstTensor(m_TensorInfo, m_Storage), - m_Node.name().c_str()); - m_Layer->GetOutputSlot(0).SetTensorInfo(m_TensorInfo); - } - - ConstTensor GetConstTensor(std::vector<T>& outputTensorData) const - { - outputTensorData.resize(m_TensorInfo.GetNumElements()); - - memcpy(outputTensorData.data(), m_Storage.data(), m_TensorInfo.GetNumBytes()); - - // Updates the result to point to the user provided storage. - ConstTensor constTensor(m_TensorInfo, outputTensorData); - return constTensor; - } - - const T* GetStorage() const - { - return m_Storage.data(); - } - - const TensorInfo& GetTensorInfo() const - { - return m_TensorInfo; - } - -private: - ///< Manages the lifetime of the tensor data. - std::vector<T> m_Storage; - ///< Describes the layout of the tensor and points to the data in m_Storage. - TensorInfo m_TensorInfo; -}; - -DataType ConvertTfTensorDataType(const tensorflow::DataType tfDataType, - const tensorflow::NodeDef& nodeDef) -{ - switch (tfDataType) - { - case tensorflow::DT_FLOAT: - return DataType::Float32; - break; - case tensorflow::DT_INT32: - return DataType::Signed32; - break; - default: - throw ParseException( - fmt::format("Unknown DataType {} for node {} {}", - tensorflow::DataType_Name(tfDataType), - nodeDef.name(), - CHECK_LOCATION().AsString())); - } -} - -struct ParseTfTensorValueList -{ - template<typename DataType> - static void Parse( - const tensorflow::TensorProto& tfTensor, - unsigned int dstElements, - std::vector<int8_t>& outputData); - - template <typename DataType> - static void ReadData(const void* srcData, unsigned int numSrcElements, - std::vector<int8_t>& dstData, unsigned int numDstElements) - { - // If there are no entries in the list, perform no action. - if (numSrcElements == 0) - { - return; - } - - // If no size was provided, use the length of the value list. - if (numDstElements == 0) - { - numDstElements = numSrcElements; - } - - // Allocates memory. - dstData.resize(std::max(numSrcElements, numDstElements) * sizeof(DataType)); - - const DataType* srcTensor = reinterpret_cast<const DataType*>(srcData); - DataType* dstTensor = reinterpret_cast<DataType*>(dstData.data()); - - // Copies the value list entries into the destination. - std::copy(srcTensor, srcTensor + numSrcElements, dstTensor); - - if (numDstElements > numSrcElements) - { - // Uses the last element in the list to fill the remaining entries. - std::fill(dstTensor + numSrcElements, dstTensor + numDstElements, srcTensor[numSrcElements - 1]); - } - } - -}; - -template <> -void ParseTfTensorValueList::Parse<float>(const tensorflow::TensorProto& tfTensor, - unsigned int dstElements, std::vector<int8_t>& outputData) -{ - ReadData<float>(tfTensor.float_val().data(), static_cast<unsigned int>(tfTensor.float_val_size()), - outputData, dstElements); -} - -template <> -void ParseTfTensorValueList::Parse<int32_t>(const tensorflow::TensorProto& tfTensor, - unsigned int dstElements, std::vector<int8_t>& outputData) -{ - ReadData<int32_t>(tfTensor.int_val().data(), static_cast<unsigned int>(tfTensor.int_val_size()), - outputData, dstElements); -} - -template <template<typename> class OperatorType, typename T = int8_t> -struct MakeTfOperation -{ - template<typename DataType, class... Args> - inline static std::unique_ptr<OperatorType<DataType>> Parse(ITfParser::TfParserImpl* parser, - const tensorflow::NodeDef& node, - Args&&... args) - { - return std::make_unique<OperatorType<DataType>>(parser, node, std::forward<Args>(args)...); - } -}; - -template <> -struct MakeTfOperation<ParsedConstTfOperation> -{ - template<typename DataType, class... Args> - inline static std::unique_ptr<ParsedConstTfOperation<DataType>> Parse(ITfParser::TfParserImpl* parser, - const tensorflow::NodeDef& node, const std::vector<int8_t>& tensorData, const TensorInfo& tensorInfo) - { - return std::make_unique<ParsedConstTfOperation<DataType>>(parser, node, - reinterpret_cast<const DataType*>(tensorData.data()), tensorInfo); - } -}; - -template <class FuncType> -struct InvokeParseFunction -{ - template<class ResType, class... Args> - inline static ResType Result(DataType dataType, Args&&... args) - { - if (dataType == DataType::Float32) - { - return FuncType::template Parse<float>(std::forward<Args>(args)...); - } - else if (dataType == DataType::Signed32) - { - return FuncType::template Parse<int32_t>(std::forward<Args>(args)...); - } - - return ResType(); - } - - template<class... Args> - inline static void Result(DataType dataType, Args&&... args) - { - if (dataType == DataType::Float32) - { - FuncType::template Parse<float>(std::forward<Args>(args)...); - } - else if (dataType == DataType::Signed32) - { - FuncType::template Parse<int32_t>(std::forward<Args>(args)...); - } - } -}; - -ParsedTfOperationPtr ITfParser::TfParserImpl::ParseConst(const tensorflow::NodeDef& nodeDef, - const tensorflow::GraphDef& graphDef) -{ - IgnoreUnused(graphDef); - ARMNN_ASSERT(nodeDef.op() == "Const"); - - if (nodeDef.attr().count("value") == 0) - { - throw ParseException( - fmt::format("Value not found for Const node - {} {}", - nodeDef.name(), - CHECK_LOCATION().AsString())); - } - - const tensorflow::TensorProto& tfTensor = nodeDef.attr().at("value").tensor(); - const tensorflow::TensorShapeProto& tfTensorShape = tfTensor.tensor_shape(); - const tensorflow::DataType tfDataType = ReadMandatoryNodeTypeAttribute(nodeDef, "dtype"); - - const auto GetDimensionSize = [](auto& d) { return d.size(); }; - - std::vector<unsigned int> dimensionSizes; - std::transform(tfTensorShape.dim().begin(), tfTensorShape.dim().end(), - std::back_inserter(dimensionSizes), GetDimensionSize); - - // Calculates number of elements. - const DataType dataType = ConvertTfTensorDataType(tfDataType, nodeDef); - unsigned int numElements = 0U; - - if (!dimensionSizes.empty()) - { - numElements = std::accumulate(dimensionSizes.begin(), dimensionSizes.end(), - 1U, std::multiplies<unsigned int>()); - } - - std::vector<int8_t> tensorData; - - // Get tensor data from the list of values attribute. - if (tfTensor.tensor_content().empty()) - { - InvokeParseFunction<ParseTfTensorValueList>::Result<void>(dataType, tfTensor, numElements, tensorData); - - // If the tensor shape is not defined, but there is a value list, then interpret the data as a 1D - // tensor of the provided number of elements. - if (numElements == 0) - { - const unsigned int tfNumElements = - static_cast<unsigned int>(tensorData.size()) / GetDataTypeSize(dataType); - dimensionSizes.push_back(tfNumElements); - } - } - // Gets tensor data from tensor content attribute. - else - { - tensorData.assign(tfTensor.tensor_content().begin(), tfTensor.tensor_content().end()); - - // Checks if a tensor shape is defined for the tensor content. - if (numElements == 0) - { - throw ParseException( - fmt::format("No tensor shape found for Const node - {} {}", - nodeDef.name(), - CHECK_LOCATION().AsString())); - } - } - - // Const node requires at least a list of values or a content attribute. - if (tensorData.empty()) - { - throw ParseException( - fmt::format("No tensor data found for Const node - {} {}", - nodeDef.name(), - CHECK_LOCATION().AsString())); - } - - const TensorInfo tensorInfo(static_cast<unsigned int>(dimensionSizes.size()), - dimensionSizes.data(), - dataType); - - // If we have a list of values, then the length of the list must be - // less than or equal to the number of elements implied by the shape argument. - if (tensorData.size() > tensorInfo.GetNumBytes()) - { - throw ParseException( - fmt::format("Number of elements ({}) should be less than or equal " - "to the number of elements implied by the shape argument ({}) for Const node - {} {}", - (tensorData.size() / GetDataTypeSize(dataType)), - tensorInfo.GetNumElements(), - nodeDef.name(), - CHECK_LOCATION().AsString())); - } - - return InvokeParseFunction<MakeTfOperation<ParsedConstTfOperation>>::Result<ParsedTfOperationPtr>( - dataType, this, nodeDef, tensorData, tensorInfo); -} - -template<typename Type> -bool ITfParser::TfParserImpl::HasParsedConstTensor(const std::string & nodeName) const -{ - auto it = m_ParsedTfOperations.find(nodeName); - if (it == m_ParsedTfOperations.end()) - { - return false; - } - return dynamic_cast<ParsedConstTfOperation<Type>*>(it->second.get()) != nullptr; -} - -template<typename Type> -bool ITfParser::TfParserImpl::HasParsedConstTensor(ParsedTfOperation* parsedTfOpPtr) const -{ - return dynamic_cast<ParsedConstTfOperation<Type>*>(parsedTfOpPtr) != nullptr; -} - -unsigned int ITfParser::TfParserImpl::GetConstInputIndex(const std::vector<OutputOfParsedTfOperation>& inputs) -{ - for (unsigned int i = 0; i < inputs.size(); i++) - { - if (HasParsedConstTensor<int32_t>(inputs[i].m_IndexedValue->GetNode().name())) - { - return i; - } - } - throw ParseException( - fmt::format("ArmNN only supports operators with constant axis. {}", - CHECK_LOCATION().AsString())); - -} - -ParsedTfOperationPtr ITfParser::TfParserImpl::ParseConv2D(const tensorflow::NodeDef& nodeDef, - const tensorflow::GraphDef& graphDef) -{ - IgnoreUnused(graphDef); - std::vector<OutputOfParsedTfOperation> inputs = GetInputParsedTfOperationsChecked(nodeDef, 2); - IOutputSlot& inputSlot = inputs[0].m_IndexedValue->ResolveArmnnOutputSlot(inputs[0].m_Index); - TensorInfo inputTensorInfo = inputSlot.GetTensorInfo(); - - if (!HasParsedConstTensor<float>(inputs[1].m_IndexedValue->GetNode().name())) - { - throw ParseException( - fmt::format("ArmNN only supports Convolution layers with constant weights for {}, input {} {}", - nodeDef.name(), - inputs[1].m_IndexedValue->GetNode().name(), - CHECK_LOCATION().AsString())); - } - ParsedConstTfOperation<float>* weightNode = - PolymorphicDowncast<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"); - - Convolution2dDescriptor desc; - desc.m_BiasEnabled = false; - - CHECK_DATA_FORMAT(nodeDef, dataFormat, "Conv2D"); - - DataLayout dataLayout = dataFormat == "NHWC" ? DataLayout::NHWC : DataLayout::NCHW; - - desc.m_DataLayout = dataLayout; - - DataLayoutIndexed dataLayoutIndexed(dataLayout); - - desc.m_StrideX = strides[dataLayoutIndexed.GetWidthIndex()]; - desc.m_StrideY = strides[dataLayoutIndexed.GetHeightIndex()]; - - std::vector<uint32_t> dilations = ReadOptionalNodeUint32ListAttribute(nodeDef, "dilations"); - if (!dilations.empty()) - { - desc.m_DilationX = dilations[dataLayoutIndexed.GetWidthIndex()]; - desc.m_DilationY = dilations[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(), sizeof(float)); - - // Create a weight tensor with the newly swizzled data. - ConstTensor weightTensor(weightTensorSwizzledInfo, weightTensorSwizzledData); - - 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; - } - else if (paddingString == "VALID") - { - padding = false; - } - - CalcPadding(inputHeight, weightHeight, desc.m_StrideY, desc.m_DilationY, desc.m_PadTop, desc.m_PadBottom, padding); - CalcPadding(inputWidth, weightWidth, desc.m_StrideX, desc.m_DilationX, desc.m_PadLeft, desc.m_PadRight, padding); - - // Calculate output height and width - unsigned int dilatedFilterWidth = weightWidth + (desc.m_DilationX - 1) * (weightWidth - 1); - unsigned int readWidth = (inputWidth + desc.m_PadLeft + desc.m_PadRight) - dilatedFilterWidth; - outputWidth = 1 + (readWidth / desc.m_StrideX); - - unsigned int dilatedFilterHeight = weightHeight + (desc.m_DilationY - 1) * (weightHeight - 1); - unsigned int readHeight = (inputHeight + desc.m_PadTop + desc.m_PadBottom) - dilatedFilterHeight; - outputHeight = 1 + (readHeight / desc.m_StrideY); - - switch (dataLayout) - { - case DataLayout::NHWC: - outputInfo = TensorInfo({ inputTensorInfo.GetShape()[0], - outputHeight, - outputWidth, - weightTensor.GetShape()[0] }, - DataType::Float32); - break; - case DataLayout::NCHW: - default: - outputInfo = TensorInfo({ inputTensorInfo.GetShape()[0], - weightTensor.GetShape()[0], - outputHeight, - outputWidth }, - DataType::Float32); - break; - } - - IConnectableLayer* layer = m_Network->AddConvolution2dLayer(desc, - weightTensor, - EmptyOptional(), - nodeDef.name().c_str()); - layer->GetOutputSlot(0).SetTensorInfo(outputInfo); - inputSlot.Connect(layer->GetInputSlot(0)); - - return std::make_unique<SingleLayerParsedTfOperation>(this, nodeDef, layer); -} - -ParsedTfOperationPtr ITfParser::TfParserImpl::ParseDepthwiseConv2D(const tensorflow::NodeDef& nodeDef, - const tensorflow::GraphDef& graphDef) -{ - IgnoreUnused(graphDef); - std::vector<OutputOfParsedTfOperation> inputs = GetInputParsedTfOperationsChecked(nodeDef, 2); - IOutputSlot& inputSlot = inputs[0].m_IndexedValue->ResolveArmnnOutputSlot(inputs[0].m_Index); - TensorInfo inputTensorInfo = inputSlot.GetTensorInfo(); - - if (!HasParsedConstTensor<float>(inputs[1].m_IndexedValue->GetNode().name())) - { - throw ParseException( - fmt::format("ArmNN only supports Depthwise Convolution layer with constant weights. " - "Non const input found {} for node {} {}", - inputs[1].m_IndexedValue->GetNode().name(), - nodeDef.name(), - CHECK_LOCATION().AsString())); - } - - ParsedConstTfOperation<float>* weightNode = - PolymorphicDowncast<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"); - - DepthwiseConvolution2dDescriptor desc; - desc.m_BiasEnabled = false; - - CHECK_DATA_FORMAT(nodeDef, dataFormat, "DepthwiseConv2dNative"); - - DataLayout dataLayout = dataFormat == "NHWC" ? DataLayout::NHWC : DataLayout::NCHW; - - desc.m_DataLayout = dataLayout; - - DataLayoutIndexed dataLayoutIndexed(dataLayout); - - desc.m_StrideX = strides[dataLayoutIndexed.GetWidthIndex()]; - desc.m_StrideY = strides[dataLayoutIndexed.GetHeightIndex()]; - std::vector<uint32_t> dilations = ReadOptionalNodeUint32ListAttribute(nodeDef, "dilations"); - if (!dilations.empty()) - { - desc.m_DilationX = dilations[dataLayoutIndexed.GetWidthIndex()]; - desc.m_DilationY = dilations[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 come in the format [H, W, I, M]. - // ArmNN weights have to be [M, I, H, W]. - PermutationVector permutationVector{ 2, 3, 1, 0 }; // [H, W, I, M] -> [M, I, 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(), sizeof(float)); - - // 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]; - - bool padding = false; - TensorInfo outputInfo; - unsigned int outputHeight = 0; - unsigned int outputWidth = 0; - - CHECK_PADDING_TYPE(nodeDef, paddingString); - - if (paddingString == "SAME") - { - padding = true; - } - else if (paddingString == "VALID") - { - padding = false; - } - - CalcPadding(inputHeight, weightHeight, desc.m_StrideY, desc.m_DilationY, desc.m_PadTop, desc.m_PadBottom, padding); - CalcPadding(inputWidth, weightWidth, desc.m_StrideX, desc.m_DilationX, desc.m_PadLeft, desc.m_PadRight, padding); - - // Calculate output height and width - unsigned int dilatedFilterWidth = weightWidth + (desc.m_DilationX - 1) * (weightWidth - 1); - unsigned int readWidth = (inputWidth + desc.m_PadLeft + desc.m_PadRight) - dilatedFilterWidth; - outputWidth = 1 + (readWidth / desc.m_StrideX); - - unsigned int dilatedFilterHeight = weightHeight + (desc.m_DilationY - 1) * (weightHeight - 1); - unsigned int readHeight = (inputHeight + desc.m_PadTop + desc.m_PadBottom) - dilatedFilterHeight; - outputHeight = 1 + (readHeight / desc.m_StrideY); - - switch (dataLayout) - { - case DataLayout::NHWC: - outputInfo = TensorInfo({ inputTensorInfo.GetShape()[0], - outputHeight, - outputWidth, - weightTensor.GetShape()[0] * weightTensor.GetShape()[1]}, - DataType::Float32); - break; - case DataLayout::NCHW: - default: - outputInfo = TensorInfo({ inputTensorInfo.GetShape()[0], - weightTensor.GetShape()[0] * weightTensor.GetShape()[1], - outputHeight, - outputWidth }, - DataType::Float32); - break; - } - - IConnectableLayer* layer = m_Network->AddDepthwiseConvolution2dLayer(desc, - weightTensor, - EmptyOptional(), - nodeDef.name().c_str()); - layer->GetOutputSlot(0).SetTensorInfo(outputInfo); - inputSlot.Connect(layer->GetInputSlot(0)); - - return std::make_unique<SingleLayerParsedTfOperation>(this, nodeDef, layer); -} - -TensorInfo OutputShapeOfExpandDims(const tensorflow::NodeDef& nodeDef, - TensorInfo inputTensorInfo, - std::int32_t expandDim) -{ - ARMNN_ASSERT(nodeDef.op() == "ExpandDims"); - - if (inputTensorInfo.GetNumDimensions() > 4) { - throw ParseException( - fmt::format("Unsupported number of dimensions: {} for input shape for ExpandDims {} {}", - inputTensorInfo.GetNumDimensions(), - nodeDef.name(), - CHECK_LOCATION().AsString())); - } - - std::int32_t inputDimSize = armnn::numeric_cast<int32_t>(inputTensorInfo.GetNumDimensions()); - std::vector<uint32_t> outputDims; - - // expandDim operation requires: -1-input.dims() <= dim <= input.dims() - if (expandDim >= -1 - inputDimSize && expandDim <= inputDimSize) - { - // add current input shape to outputDims - for (unsigned int i = 0; i < inputTensorInfo.GetNumDimensions(); ++i) { - auto currentDimension = inputTensorInfo.GetShape()[i]; - outputDims.push_back(currentDimension); - } - - // insert a dimension of 1 at index 'expandDim' of inputs shape - if (expandDim >= 0) - { - auto getPosition = std::next(outputDims.begin() + 0, expandDim); - outputDims.insert(getPosition, 1); - } - - // if negative number for 'expandDim' then count backwards from the last element - // and insert 1 dimension at index 'expandDim' - if (expandDim < 0) - { - int outputDimSize = armnn::numeric_cast<int>(outputDims.size() + 1); - auto getPosition = std::next(outputDims.begin() + outputDimSize, expandDim); - outputDims.insert(getPosition, 1); - } - } - else - { - throw InvalidArgumentException( - fmt::format("Cannot expand dimension {} in input tensor with {} dimension {}", - expandDim, - inputDimSize, - CHECK_LOCATION().AsString())); - } - - if (outputDims.size() > 4) - { - throw ParseException( - fmt::format("Unsupported number of dimensions: {} for output shape for ExpandDims {} {}", - outputDims.size(), - nodeDef.name(), - CHECK_LOCATION().AsString())); - } - - TensorShape outShape = TensorShape(static_cast<unsigned int>(outputDims.size()), - outputDims.data()); - - TensorInfo outTensorInfo = inputTensorInfo; - outTensorInfo.SetShape(outShape); - - return outTensorInfo; -} - -ParsedTfOperationPtr ITfParser::TfParserImpl::ParseExpandDims(const tensorflow::NodeDef& nodeDef, - const tensorflow::GraphDef& graphDef) -{ - IgnoreUnused(graphDef); - - // Number of inputs can either - // be 1 - that indicates that the axis parameter is passed as an attribute of the operation - // or 2 - which means that the axis parameter is passed as a second input - std::vector<OutputOfConstNodeDef> nodes = GetTfInputNodes(nodeDef); - const std::size_t numInputs = nodes.size(); - std::vector<OutputOfParsedTfOperation> inputs; - std::int32_t expandDim; // axis or dim parameter. Describes which dimension to expand. - if (numInputs == 1) - { - inputs = GetInputParsedTfOperationsChecked(nodeDef, 1); - expandDim = ReadMandatoryNodeInt32Attribute(nodeDef, "Tdim"); - } - else - { - inputs = GetInputParsedTfOperationsChecked(nodeDef, 2); - - // make sure data type is int32 - IOutputSlot& prevLayerOutputSlot = inputs[1].m_IndexedValue->ResolveArmnnOutputSlot(inputs[1].m_Index); - TensorInfo inputTensorInfo = prevLayerOutputSlot.GetTensorInfo(); - - if (inputTensorInfo.GetDataType()!=armnn::DataType::Signed32) - { - throw ParseException( - fmt::format("The axis parameter of ExpandDims operation given as second input is not of type int32." - " Input {0} Node {1} {2}", - inputs[1].m_IndexedValue->GetNode().name(), - nodeDef.name(), - CHECK_LOCATION().AsString())); - } - - // ensure the second input is a constant value - if (!HasParsedConstTensor<int32_t>(inputs[1].m_IndexedValue->GetNode().name())) - { - throw ParseException( - fmt::format("ArmNN only supports ExpandDims layers with constant axis/dim parameter. " - "Input {0} Node {1} {2}", - inputs[1].m_IndexedValue->GetNode().name(), - nodeDef.name(), - CHECK_LOCATION().AsString())); - } - - // make sure the second input is scalar or contains only a single value - // (we don't support expand dims for multiple axis but we don't care what shape the - // given tensor has as long as there is only a single value in it - // e.g. a tensor like this [[[1]]] is completely fine) - if (inputTensorInfo.GetNumElements() != 1) - { - throw ParseException( - fmt::format("The axis parameter of ExpandDims operation given as second input is not " - "allowed to hold more than one value. " - "Input {0} Node {1} {2}", - inputs[1].m_IndexedValue->GetNode().name(), - nodeDef.name(), - CHECK_LOCATION().AsString())); - } - - ParsedConstTfOperation<int32_t>* expandDimsNode = - PolymorphicDowncast<ParsedConstTfOperation<int32_t>*>(inputs[1].m_IndexedValue); - - memcpy(&expandDim, expandDimsNode->GetStorage(), sizeof(expandDim)); - } - - // First input is the vector that should be expanded by another dimension - IOutputSlot& prevLayerOutputSlot = inputs[0].m_IndexedValue->ResolveArmnnOutputSlot(inputs[0].m_Index); - TensorInfo inputTensorInfo = prevLayerOutputSlot.GetTensorInfo(); - - TensorInfo outputInfo; - outputInfo = OutputShapeOfExpandDims(nodeDef, inputTensorInfo, expandDim); - - ReshapeDescriptor reshapeDesc; - reshapeDesc.m_TargetShape = outputInfo.GetShape(); - IConnectableLayer* layer = m_Network->AddReshapeLayer(reshapeDesc, nodeDef.name().c_str()); - prevLayerOutputSlot.Connect(layer->GetInputSlot(0)); - layer->GetOutputSlot(0).SetTensorInfo(outputInfo); - - return std::make_unique<SingleLayerParsedTfOperation>(this, nodeDef, layer); -} - -ParsedTfOperationPtr ITfParser::TfParserImpl::ParseFusedBatchNorm(const tensorflow::NodeDef& nodeDef, - const tensorflow::GraphDef& graphDef) -{ - IgnoreUnused(graphDef); - std::vector<OutputOfParsedTfOperation> inputs = GetInputParsedTfOperationsChecked(nodeDef, 5); - - if (!HasParsedConstTensor<float>(inputs[1].m_IndexedValue->GetNode().name())) - { - throw ParseException( - fmt::format("ArmNN only supports FusedBatchNormalization layers with constant scale. " - "Input {}. Node {} {}", - inputs[1].m_IndexedValue->GetNode().name(), - nodeDef.name(), - CHECK_LOCATION().AsString())); - } - ParsedConstTfOperation<float>* scaleNode = - PolymorphicDowncast<ParsedConstTfOperation<float> *>(inputs[1].m_IndexedValue); - - if (!HasParsedConstTensor<float>(inputs[2].m_IndexedValue->GetNode().name())) - { - throw ParseException( - fmt::format("ArmNN only supports FusedBatchNormalization layers with constant offset. " - "Input {}. Node {} {}", - inputs[2].m_IndexedValue->GetNode().name(), - nodeDef.name(), - CHECK_LOCATION().AsString())); - } - ParsedConstTfOperation<float>* offsetNode = - PolymorphicDowncast<ParsedConstTfOperation<float> *>(inputs[2].m_IndexedValue); - - if (!HasParsedConstTensor<float>(inputs[3].m_IndexedValue->GetNode().name())) - { - throw ParseException( - fmt::format("ArmNN only supports FusedBatchNormalization layers with constant mean. " - "Input {}. Node {} {}", - inputs[3].m_IndexedValue->GetNode().name(), - nodeDef.name(), - CHECK_LOCATION().AsString())); - } - ParsedConstTfOperation<float>* meanNode = - PolymorphicDowncast<ParsedConstTfOperation<float> *>(inputs[3].m_IndexedValue); - - if (!HasParsedConstTensor<float>(inputs[4].m_IndexedValue->GetNode().name())) - { - throw ParseException( - fmt::format("ArmNN only supports FusedBatchNormalization layers with constant variance. " - "Input {}. Node {} {}", - inputs[4].m_IndexedValue->GetNode().name(), - nodeDef.name(), - CHECK_LOCATION().AsString())); - } - ParsedConstTfOperation<float>* varianceNode = - PolymorphicDowncast<ParsedConstTfOperation<float> *>(inputs[4].m_IndexedValue); - - const std::string dataFormat = ReadOptionalNodeStringAttribute(nodeDef, "data_format", "NHWC"); - CHECK_DATA_FORMAT(nodeDef, dataFormat, "FusedBatchNorm"); - - // The descriptor only has the epsilon attribute. - BatchNormalizationDescriptor desc; - desc.m_Eps = ReadMandatoryNodeFloatAttribute(nodeDef, "epsilon"); - desc.m_DataLayout = dataFormat == "NHWC" ? DataLayout::NHWC : DataLayout::NCHW; - - // Data for the parsed tensor args (scale, offset, mean, variance) must be stored - // locally until the layer is added. - std::vector<float> scaleTensorData; - ConstTensor scaleTensor = scaleNode->GetConstTensor(scaleTensorData); - - std::vector<float> offsetTensorData; - ConstTensor offsetTensor = offsetNode->GetConstTensor(offsetTensorData); - - std::vector<float> meanTensorData; - ConstTensor meanTensor = meanNode->GetConstTensor(meanTensorData); - - std::vector<float> varianceTensorData; - ConstTensor varianceTensor = varianceNode->GetConstTensor(varianceTensorData); - - IConnectableLayer* layer = m_Network->AddBatchNormalizationLayer(desc, - meanTensor, - varianceTensor, - offsetTensor, - scaleTensor, - nodeDef.name().c_str()); - - IOutputSlot& inputSlot = inputs[0].m_IndexedValue->ResolveArmnnOutputSlot(inputs[0].m_Index); - - layer->GetOutputSlot(0).SetTensorInfo(inputSlot.GetTensorInfo()); - inputSlot.Connect(layer->GetInputSlot(0)); - - return std::make_unique<SingleLayerParsedTfOperation>(this, nodeDef, layer); -} - -bool ITfParser::TfParserImpl::IsSupportedLeakyReluPattern(const tensorflow::NodeDef& mulNodeDef, - size_t alphaLayerIndex, - const OutputOfParsedTfOperation& otherOp, - armnn::IOutputSlot** outputOfLeakyRelu, - armnn::ActivationDescriptor & desc) -{ - const tensorflow::NodeDef& otherNodeDef = otherOp.m_IndexedValue->GetNode(); - - // Verifying all these assumptions hold: - // - // 1, the mulNodeDef is an elementwise multiplication node "Mul" - // 2, the alphaLayerIndex selects a constant node from the inputs of the "Mul" node - // 3, the inputLayerIndex selects a layer which has the same name as otherNodeDef - // - - if (mulNodeDef.op() == "Mul") - { - size_t otherLayerIndex = (alphaLayerIndex == 0 ? 1 : 0); - std::vector<OutputOfParsedTfOperation> inputs = GetInputParsedTfOperationsChecked(mulNodeDef, 2); - - ARMNN_ASSERT(inputs.size() == 2); - ARMNN_ASSERT((otherLayerIndex == 0 || alphaLayerIndex == 0)); - ARMNN_ASSERT((otherLayerIndex == 1 || alphaLayerIndex == 1)); - ARMNN_ASSERT(((otherLayerIndex + alphaLayerIndex) == 1)); - - if (inputs[otherLayerIndex].m_IndexedValue->GetNode().name() == otherNodeDef.name()) - { - if (HasParsedConstTensor<float>(inputs[alphaLayerIndex].m_IndexedValue->GetNode().name())) - { - ParsedConstTfOperation<float>* alpha = - PolymorphicDowncast<ParsedConstTfOperation<float> *>( - inputs[alphaLayerIndex].m_IndexedValue); - - std::vector<float> const_data; - ConstTensor const_tensor = alpha->GetConstTensor(const_data); - - if (const_data.size() == 1) - { - desc.m_Function = ActivationFunction::LeakyReLu; - desc.m_A = const_data[0]; - - *outputOfLeakyRelu = &(otherOp.m_IndexedValue->ResolveArmnnOutputSlot(otherOp.m_Index)); - return true; - } - } - } - } - return false; -} - -ParsedTfOperationPtr ITfParser::TfParserImpl::ParseMaximum(const tensorflow::NodeDef& nodeDef, - const tensorflow::GraphDef& graphDef) -{ - IgnoreUnused(graphDef); - std::vector<OutputOfParsedTfOperation> inputs = GetInputParsedTfOperationsChecked(nodeDef, 2); - if (inputs.size() != 2) - { - throw ParseException( - fmt::format("Maximum expects two inputs!. Got {} for Node {} {}", - inputs.size(), - nodeDef.name(), - CHECK_LOCATION().AsString())); - } - - auto inputNode0 = inputs[0].m_IndexedValue->GetNode(); - auto inputNode1 = inputs[1].m_IndexedValue->GetNode(); - IOutputSlot* outputOfLeakyRelu = nullptr; - - ActivationDescriptor desc; - - // A max node may be part of a LeakyRelu, with one input as a multiplication with a scalar constant, - // i.e. one of the four possible scenarios: - // 1, max(mul(a, x), x) - // 2, max(mul(x, a), x) - // 3, max(x, mul(a, x)) - // 4, max(x, mul(x, a)) - // These are handled by an activation layer. - - if (IsSupportedLeakyReluPattern(inputNode0, 0, inputs[1], &outputOfLeakyRelu, desc) || - IsSupportedLeakyReluPattern(inputNode0, 1, inputs[1], &outputOfLeakyRelu, desc) || - IsSupportedLeakyReluPattern(inputNode1, 0, inputs[0], &outputOfLeakyRelu, desc) || - IsSupportedLeakyReluPattern(inputNode1, 1, inputs[0], &outputOfLeakyRelu, desc)) - { - ARMNN_ASSERT(outputOfLeakyRelu != nullptr); - - IConnectableLayer* const layer = m_Network->AddActivationLayer(desc, nodeDef.name().c_str()); - outputOfLeakyRelu->Connect(layer->GetInputSlot(0)); - layer->GetOutputSlot(0).SetTensorInfo(outputOfLeakyRelu->GetTensorInfo()); - return std::make_unique<SingleLayerParsedTfOperation>(this, nodeDef, layer); - } - else - { - // Anything else is just a maximum layer. - - return AddMaximumLayer(nodeDef); - } -} - -std::pair<armnn::IOutputSlot*, armnn::IOutputSlot*> ITfParser::TfParserImpl::ProcessElementwiseInputSlots( - const tensorflow::NodeDef& nodeDef, const std::string& layerName) -{ - std::vector<OutputOfParsedTfOperation> inputs = GetInputParsedTfOperationsChecked(nodeDef, 2); - - IOutputSlot* input0Slot = &inputs[0].m_IndexedValue->ResolveArmnnOutputSlot(inputs[0].m_Index); - IOutputSlot* input1Slot = &inputs[1].m_IndexedValue->ResolveArmnnOutputSlot(inputs[1].m_Index); - const unsigned int input0Dim = input0Slot->GetTensorInfo().GetNumDimensions(); - const unsigned int input1Dim = input1Slot->GetTensorInfo().GetNumDimensions(); - - if (input0Dim != input1Dim) - { - // broadcasting where input0 and input1 have different number of dimensions - // is only supported for 1D and 4D tensors pair - if (input0Dim == 1 && input1Dim == 4) - { - input0Slot = AddBroadcastReshapeLayer(input1Slot, input0Slot, true, *m_Network, nodeDef); - } - else if (input0Dim == 4 && input1Dim == 1) - { - input1Slot = AddBroadcastReshapeLayer(input0Slot, input1Slot, true, *m_Network, nodeDef); - } - else - { - throw ParseException( - fmt::format("Unsupported broadcast configuration for {} operation {} {}", - layerName, - nodeDef.name(), - CHECK_LOCATION().AsString())); - } - } - return {input0Slot, input1Slot}; -} - -ParsedTfOperationPtr ITfParser::TfParserImpl::ProcessComparisonLayer( - IOutputSlot* input0Slot, - IOutputSlot* input1Slot, - IConnectableLayer* const layer, - const tensorflow::NodeDef& nodeDef) -{ - input0Slot->Connect(layer->GetInputSlot(0)); - input1Slot->Connect(layer->GetInputSlot(1)); - - TensorInfo outputInfo = input0Slot->GetTensorInfo(); - outputInfo.SetDataType(DataType::Boolean); - std::vector<unsigned int> outputShape; - - const TensorShape& input0Shape = input0Slot->GetTensorInfo().GetShape(); - const TensorShape& input1Shape = input1Slot->GetTensorInfo().GetShape(); - - for (unsigned int i = 0; i < input0Shape.GetNumDimensions(); i++) - { - outputShape.push_back(std::max(input0Shape[i], input1Shape[i])); - } - - outputInfo.SetShape(TensorShape(input0Shape.GetNumDimensions(), outputShape.data())); - layer->GetOutputSlot(0).SetTensorInfo(outputInfo); - - return std::make_unique<SingleLayerParsedTfOperation>(this, nodeDef, layer); -} - -ParsedTfOperationPtr ITfParser::TfParserImpl::ProcessElementwiseLayer( - IOutputSlot* input0Slot, - IOutputSlot* input1Slot, - IConnectableLayer* const layer, - const tensorflow::NodeDef& nodeDef) -{ - input0Slot->Connect(layer->GetInputSlot(0)); - input1Slot->Connect(layer->GetInputSlot(1)); - - TensorInfo outputInfo = input0Slot->GetTensorInfo(); - std::vector<unsigned int> outputShape; - - const TensorShape& input0Shape = input0Slot->GetTensorInfo().GetShape(); - const TensorShape& input1Shape = input1Slot->GetTensorInfo().GetShape(); - - for (unsigned int i = 0; i < input0Shape.GetNumDimensions(); i++) - { - outputShape.push_back(std::max(input0Shape[i], input1Shape[i])); - } - - outputInfo.SetShape(TensorShape(input0Shape.GetNumDimensions(), outputShape.data())); - layer->GetOutputSlot(0).SetTensorInfo(outputInfo); - - return std::make_unique<SingleLayerParsedTfOperation>(this, nodeDef, layer); -} - -ParsedTfOperationPtr ITfParser::TfParserImpl::ParseGather(const tensorflow::NodeDef& nodeDef, - const tensorflow::GraphDef& graphDef) -{ - IgnoreUnused(graphDef); - std::vector<OutputOfParsedTfOperation> inputs = GetInputParsedTfOperationsChecked(nodeDef, 2); - IOutputSlot& params = inputs[0].m_IndexedValue->ResolveArmnnOutputSlot(inputs[0].m_Index); - IOutputSlot& indices = inputs[1].m_IndexedValue->ResolveArmnnOutputSlot(inputs[1].m_Index); - GatherDescriptor descriptor; - descriptor.m_Axis = ReadMandatoryNodeInt32Attribute(nodeDef, "axis"); - - // Infer shape of output tensor - unsigned int paramsDim = params.GetTensorInfo().GetNumDimensions(); - unsigned int indicesDim = indices.GetTensorInfo().GetNumDimensions(); - unsigned int outputDim = paramsDim - 1 + indicesDim; - - std::vector<unsigned int> dimSizes; - - for (unsigned int i = 0; i < indicesDim; ++i) - { - dimSizes.push_back(indices.GetTensorInfo().GetShape()[i]); - } - for (unsigned int i = 1; i < paramsDim; ++i) - { - dimSizes.push_back(params.GetTensorInfo().GetShape()[i]); - } - - const TensorShape& inferredShape = TensorShape(outputDim, dimSizes.data()); - - const TensorInfo inferredOutputInfo(inferredShape, params.GetTensorInfo().GetDataType()); - - IConnectableLayer* const layer = m_Network->AddGatherLayer(descriptor, nodeDef.name().c_str()); - layer->GetOutputSlot(0).SetTensorInfo(inferredOutputInfo); - - params.Connect(layer->GetInputSlot(0)); - indices.Connect(layer->GetInputSlot(1)); - - return std::make_unique<SingleLayerParsedTfOperation>(this, nodeDef, layer); -} - -ParsedTfOperationPtr ITfParser::TfParserImpl::ParseGreater(const tensorflow::NodeDef& nodeDef, - const tensorflow::GraphDef& graphDef) -{ - IgnoreUnused(graphDef); - std::pair<armnn::IOutputSlot*, armnn::IOutputSlot*> inputLayers = ProcessElementwiseInputSlots(nodeDef, "Greater"); - IOutputSlot* input0Slot = inputLayers.first; - IOutputSlot* input1Slot = inputLayers.second; - - ComparisonDescriptor descriptor(ComparisonOperation::Greater); - IConnectableLayer* const layer = m_Network->AddComparisonLayer(descriptor, nodeDef.name().c_str()); - - return ProcessComparisonLayer(input0Slot, input1Slot, layer, nodeDef); -} - -ParsedTfOperationPtr ITfParser::TfParserImpl::ParseEqual(const tensorflow::NodeDef& nodeDef, - const tensorflow::GraphDef& graphDef) -{ - IgnoreUnused(graphDef); - std::pair<armnn::IOutputSlot*, armnn::IOutputSlot*> inputLayers = ProcessElementwiseInputSlots(nodeDef, "Equal"); - IOutputSlot* input0Slot = inputLayers.first; - IOutputSlot* input1Slot = inputLayers.second; - - ComparisonDescriptor descriptor(ComparisonOperation::Equal); - IConnectableLayer* const layer = m_Network->AddComparisonLayer(descriptor, nodeDef.name().c_str()); - - return ProcessComparisonLayer(input0Slot, input1Slot, layer, nodeDef); -} - -ParsedTfOperationPtr ITfParser::TfParserImpl::ParseMinimum(const tensorflow::NodeDef& nodeDef, - const tensorflow::GraphDef& graphDef) -{ - IgnoreUnused(graphDef); - std::pair<armnn::IOutputSlot*, armnn::IOutputSlot*> inputLayers = ProcessElementwiseInputSlots(nodeDef, "Minimum"); - IOutputSlot* input0Slot = inputLayers.first; - IOutputSlot* input1Slot = inputLayers.second; - - IConnectableLayer* const layer = m_Network->AddMinimumLayer(nodeDef.name().c_str()); - - return ProcessElementwiseLayer(input0Slot, input1Slot, layer, nodeDef); -} - -ParsedTfOperationPtr ITfParser::TfParserImpl::ParseSub(const tensorflow::NodeDef& nodeDef, - const tensorflow::GraphDef& graphDef) -{ - IgnoreUnused(graphDef); - std::vector<OutputOfParsedTfOperation> inputs = GetInputParsedTfOperationsChecked(nodeDef, 2); - - IOutputSlot* input0Slot = &inputs[0].m_IndexedValue->ResolveArmnnOutputSlot(inputs[0].m_Index); - IOutputSlot* input1Slot = &inputs[1].m_IndexedValue->ResolveArmnnOutputSlot(inputs[1].m_Index); - - const TensorInfo& input0Info = input0Slot->GetTensorInfo(); - const TensorInfo& input1Info = input1Slot->GetTensorInfo(); - - if (input0Info.GetNumDimensions() == 1) - { - const bool isNHWC = true; - input0Slot = AddBroadcastReshapeLayer(input1Slot, input0Slot, isNHWC, *m_Network, nodeDef); - } - - if (input1Info.GetNumDimensions() == 1) - { - const bool isNHWC = true; - input1Slot = AddBroadcastReshapeLayer(input0Slot, input1Slot, isNHWC, *m_Network, nodeDef); - } - - IConnectableLayer* const layer = m_Network->AddSubtractionLayer(nodeDef.name().c_str()); - - input0Slot->Connect(layer->GetInputSlot(0)); - input1Slot->Connect(layer->GetInputSlot(1)); - - if (input0Info.GetNumDimensions() == 1) - { - layer->GetOutputSlot(0).SetTensorInfo(input1Slot->GetTensorInfo()); - } - else - { - layer->GetOutputSlot(0).SetTensorInfo(input0Slot->GetTensorInfo()); - } - - return std::make_unique<SingleLayerParsedTfOperation>(this, nodeDef, layer); -} - -ParsedTfOperationPtr ITfParser::TfParserImpl::ParseStack(const tensorflow::NodeDef& nodeDef, - const tensorflow::GraphDef& graphDef) -{ - IgnoreUnused(graphDef); - std::vector<OutputOfConstNodeDef> nodes = GetTfInputNodes(nodeDef); - - unsigned int numInputs = static_cast<unsigned int>(nodes.size()); - if (numInputs < 1) - { - throw ParseException( - fmt::format("Pack/Stack expects at least one input. Got {} for Node {} {}", - numInputs, - nodeDef.name(), - CHECK_LOCATION().AsString())); - } - - std::vector<OutputOfParsedTfOperation> inputs = GetInputParsedTfOperationsChecked(nodeDef, numInputs); - // Use the tensor shape of the first input as the "correct" input shape in the descriptor - IOutputSlot* input0Slot = &inputs[0].m_IndexedValue->ResolveArmnnOutputSlot(inputs[0].m_Index); - const TensorInfo& inputTensorInfo = input0Slot->GetTensorInfo(); - auto numDimensions = inputTensorInfo.GetShape().GetNumDimensions(); - - // validate axis - int32_t axis = ReadMandatoryNodeInt32Attribute(nodeDef, "axis"); - const int sNumDimensions = (static_cast<int>(numDimensions) + 1); - if (!(axis < sNumDimensions && axis >= -sNumDimensions)) - { - throw ParseException( - fmt::format("Axis index is not in range. Got {} for Node {} {}", - axis, - nodeDef.name(), - CHECK_LOCATION().AsString())); - } - - if (axis < 0) - { - axis = static_cast<int32_t>(numDimensions) + axis + 1; - } - - StackDescriptor stackDescriptor; - stackDescriptor.m_Axis = static_cast<uint32_t>(axis); - stackDescriptor.m_NumInputs = static_cast<uint32_t>(numInputs); - stackDescriptor.m_InputShape = inputTensorInfo.GetShape(); - - const unsigned int supportedNumDims = 4; - for (unsigned int viewIndex = 0; viewIndex < numInputs; ++viewIndex) - { - IOutputSlot& inputSlot = inputs[viewIndex].m_IndexedValue->ResolveArmnnOutputSlot(inputs[viewIndex].m_Index); - TensorInfo inputTensorInfo = inputSlot.GetTensorInfo(); - - // Double check dimensions of the tensors - if (inputTensorInfo.GetNumDimensions() >= supportedNumDims) - { - throw armnn::ParseException( - fmt::format("The number of dimensions: {} for input tensors of the " - "Pack/Stack op. Number of dimensions should be less than {} {}", - inputTensorInfo.GetNumDimensions(), - supportedNumDims, - CHECK_LOCATION().AsString())); - } - } - - std::vector<unsigned int> outputDimensions; - for (unsigned int i = 0; i < stackDescriptor.m_InputShape.GetNumDimensions(); ++i) - { - outputDimensions.push_back(stackDescriptor.m_InputShape[i]); - } - outputDimensions.insert(outputDimensions.begin() + axis, numInputs); - - // add Stack Layer - IConnectableLayer* const layer = m_Network->AddStackLayer(stackDescriptor, nodeDef.name().c_str()); - - for (unsigned int viewIndex = 0; viewIndex < numInputs; ++viewIndex) - { - IOutputSlot& inputSlot = inputs[viewIndex].m_IndexedValue->ResolveArmnnOutputSlot(inputs[viewIndex].m_Index); - inputSlot.Connect(layer->GetInputSlot(viewIndex)); - } - - layer->GetOutputSlot(0).SetTensorInfo( - armnn::TensorInfo(static_cast<uint32_t>(outputDimensions.size()), - outputDimensions.data(), - inputTensorInfo.GetDataType())); - - return std::make_unique<SingleLayerParsedTfOperation>(this, nodeDef, layer); -} - -ParsedTfOperationPtr ITfParser::TfParserImpl::ParseTranspose(const tensorflow::NodeDef& nodeDef, - const tensorflow::GraphDef& graphDef) -{ - IgnoreUnused(graphDef); - - auto inputs = GetInputParsedTfOperationsChecked(nodeDef, 2); - const auto inputCount = inputs.size(); - - if (inputCount != 2) - { - throw ParseException( - fmt::format("The number of given input is {}. It should be two for Transpose op." - "Node {} {}", - inputCount, - nodeDef.name(), - CHECK_LOCATION().AsString())); - } - - auto* input0Slot = &inputs[0].m_IndexedValue->ResolveArmnnOutputSlot(inputs[0].m_Index); - - const auto constInput = inputs[GetConstInputIndex(inputs)]; - auto* permuteVectorInput = - PolymorphicDowncast<ParsedConstTfOperation<int32_t>*>(constInput.m_IndexedValue); - const auto& permuteVectorInfo = permuteVectorInput->GetTensorInfo(); - - std::vector<int32_t> permuteVectorData; - permuteVectorInput->GetConstTensor(permuteVectorData); - - std::vector<unsigned int> armnnPermuteVectorData(permuteVectorData.begin(), permuteVectorData.end()); - - const auto permutationVector = PermutationVector(armnnPermuteVectorData.data(), permuteVectorInfo.GetNumElements()); - const auto desc = TransposeDescriptor(permutationVector); - - auto* layer = m_Network->AddTransposeLayer(desc, nodeDef.name().c_str()); - ARMNN_ASSERT(layer); - - input0Slot->Connect(layer->GetInputSlot(0)); - - const auto& input0Info = input0Slot->GetTensorInfo(); - armnn::TensorInfo outputInfo {input0Info}; - outputInfo.SetShape(armnnUtils::TransposeTensorShape(input0Info.GetShape(), desc.m_DimMappings)); - layer->GetOutputSlot(0).SetTensorInfo(outputInfo); - - return std::make_unique<SingleLayerParsedTfOperation>(this, nodeDef, layer); -} - -unsigned int CheckPaddingTensor(const ConstTensor& paddingTensor, - const TensorInfo& inputTensorInfo, - const std::string& nodeName) -{ - unsigned int rank = paddingTensor.GetShape()[0]; - unsigned int expectedRank = inputTensorInfo.GetNumDimensions(); - if (rank != expectedRank) - { - throw ParseException( - fmt::format("Expected the padding tensor to be of rank {} not {} on Node {} {}.", - expectedRank, - rank, - nodeName, - CHECK_LOCATION().AsString())); - } - unsigned int second = paddingTensor.GetShape()[1]; - if (second != 2) - { - throw ParseException( - fmt::format("Expected the padding tensor to be of dimensions " - "[{1}, 2] not [{1}, {2}] on Node {3} {4}.", - rank, - second, - nodeName, - CHECK_LOCATION().AsString())); - } - return rank; -} - -TensorInfo CalculatePaddedOutputTensorInfo(const TensorInfo& inputTensorInfo, - const std::vector<std::pair<unsigned int, unsigned int>>& padList) -{ - unsigned int numDims = inputTensorInfo.GetNumDimensions(); - std::vector<unsigned int> outDims; - for (unsigned int i = 0; i < numDims; ++i) - { - unsigned int dimSize = inputTensorInfo.GetShape()[i]; - const std::pair<unsigned int, unsigned int>& dimPadding = padList[i]; - dimSize += dimPadding.first; - dimSize += dimPadding.second; - outDims.push_back(dimSize); - } - TensorInfo paddedTensorInfo = inputTensorInfo; - unsigned int outDimsSize = static_cast<unsigned int>(outDims.size()); - paddedTensorInfo.SetShape(TensorShape{ outDimsSize, outDims.data() }); - return paddedTensorInfo; -} - -ParsedTfOperationPtr ITfParser::TfParserImpl::ParsePad(const tensorflow::NodeDef& nodeDef, - const tensorflow::GraphDef& graphDef) -{ - IgnoreUnused(graphDef); - // input consists of: - // input[0] the tensor which will be padded - // input[1] the tensor holding the padding values - std::vector<OutputOfParsedTfOperation> inputs = GetInputParsedTfOperationsChecked(nodeDef, 2); - IOutputSlot& previousLayerOutputSlot = inputs[0].m_IndexedValue->ResolveArmnnOutputSlot(inputs[0].m_Index); - TensorInfo inputTensorInfo = previousLayerOutputSlot.GetTensorInfo(); - if (!HasParsedConstTensor<int32_t>(inputs[1].m_IndexedValue)) - { - throw ParseException( - fmt::format("ArmNN only supports Pad with constant padding. " - "Input {}. Node {} {}", - inputs[1].m_IndexedValue->GetNode().name(), - nodeDef.name(), - CHECK_LOCATION().AsString())); - - } - ParsedConstTfOperation<int32_t>* paddingTensorOp = - PolymorphicDowncast<ParsedConstTfOperation<int32_t>*>(inputs[1].m_IndexedValue); - - std::vector<int32_t> paddingTensorData; - ConstTensor paddingTensor = paddingTensorOp->GetConstTensor(paddingTensorData); - // paddings is an integer tensor with shape [n, 2], where n is the rank of tensor - // and should match the rank of the input tensor that is being padded. - // For each dimension D of input, paddings[D, 0] indicates how many values to add - // before the contents of tensor in that dimension, and paddings[D, 1] indicates how - // many values to add after the contents of tensor in that dimension - // This needs to be translated into a padList for ACL - std::vector<std::pair<unsigned int, unsigned int>> padList; - unsigned int rank = CheckPaddingTensor(paddingTensor, inputTensorInfo, nodeDef.name()); - for (unsigned int i = 0; i < rank; ++i) - { - std::pair<unsigned int, unsigned int> paddingForDim; - for (unsigned int j = 0; j < 2; j++) - { - unsigned int index = (i * 2) + j; - int paddingAmount = paddingTensorData[index]; - // make sure we can cast to an unsigned value - if (paddingAmount < 0) - { - throw ParseException( - fmt::format("Negative amount {} specified at [{}, {}] of padding tensor on Node {} {}.", - paddingAmount, - i, - j, - nodeDef.name(), - CHECK_LOCATION().AsString())); - } - if (j == 0) - { - paddingForDim.first = static_cast<unsigned int>(paddingAmount); - } - else - { - paddingForDim.second = static_cast<unsigned int>(paddingAmount); - } - } - padList.push_back(paddingForDim); - } - PadDescriptor padDescriptor(padList); - IConnectableLayer* layer = m_Network->AddPadLayer(padDescriptor, nodeDef.name().c_str()); - previousLayerOutputSlot.Connect(layer->GetInputSlot(0)); - // Use the padding to calculate the new output tensor shape - TensorInfo outputTensorInfo = CalculatePaddedOutputTensorInfo(inputTensorInfo, padList); - layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); - return std::make_unique<SingleLayerParsedTfOperation>(this, nodeDef, layer); -} - -ParsedTfOperationPtr ITfParser::TfParserImpl::ParseConcat(const tensorflow::NodeDef& nodeDef, - const tensorflow::GraphDef& graphDef) -{ - IgnoreUnused(graphDef); - std::vector<OutputOfConstNodeDef> nodes = GetTfInputNodes(nodeDef); - - // In tensorflow, we have the last input of the Concat layer as the axis for concatenation. - unsigned int numInputs = static_cast<unsigned int>(nodes.size()); - - std::vector<OutputOfParsedTfOperation> inputs = GetInputParsedTfOperationsChecked(nodeDef, numInputs); - - // Constant tensor index - unsigned int index = GetConstInputIndex(inputs); - // Get the axis tensor data - ParsedConstTfOperation<int32_t>* shapeNode = - PolymorphicDowncast<ParsedConstTfOperation<int32_t>*>(inputs[index].m_IndexedValue); - - std::vector<int32_t> axisTensorData; - shapeNode->GetConstTensor(axisTensorData); - - // This concatDim indicates the data format: 3 is the NHWC, 1 is the NCHW. - const unsigned int concatDim = static_cast<unsigned int>(axisTensorData[0]); - - // Armnn supports concatenation along the channel dimension for data formats NHWC and NCHW. - if (concatDim == 0 || concatDim == 2) - { - throw ParseException( - fmt::format("Dimension {} for concatenation is not supported by Armnn. " - "Node {} {}", - concatDim, - nodeDef.name(), - CHECK_LOCATION().AsString())); - } - - const unsigned int supportedNumDims = 4; - unsigned int numConcatViews = numInputs - 1; - OriginsDescriptor concatDescriptor(static_cast<uint32_t>(numConcatViews), supportedNumDims); - concatDescriptor.SetConcatAxis(concatDim); - TensorShape mergeDims(supportedNumDims); - unsigned int mergeDim = 0; - for (unsigned int viewIndex = 0; viewIndex < numConcatViews; ++viewIndex) - { - // Need to double check whether it should be - IOutputSlot& inputSlot = inputs[viewIndex].m_IndexedValue->ResolveArmnnOutputSlot(inputs[viewIndex].m_Index); - TensorInfo inputTensorInfo = inputSlot.GetTensorInfo(); - - // Double check dimensions of the tensors - if (inputTensorInfo.GetNumDimensions() != supportedNumDims) - { - throw armnn::ParseException( - fmt::format("The number of dimensions: {} for input tensors of the " - "concatenation op should be {} {}", - inputTensorInfo.GetNumDimensions(), - supportedNumDims, - CHECK_LOCATION().AsString())); - } - - // Copy the input tensor shape to mergeDimSizes and initialize the view origin coordinates for the current input - mergeDims = inputTensorInfo.GetShape(); - unsigned int* viewOrigin = const_cast<unsigned int*>(concatDescriptor.GetViewOrigin(viewIndex)); - std::fill(viewOrigin, viewOrigin + supportedNumDims, 0); - - // Update the view origin coordinates and the merge dimension value - concatDescriptor.SetViewOriginCoord(viewIndex, concatDim, mergeDim); - mergeDim += mergeDims[concatDim]; - } - - // Update the output shape - mergeDims[concatDim] = mergeDim; - armnn::IConnectableLayer *layer = m_Network->AddConcatLayer(concatDescriptor, nodeDef.name().c_str()); - - layer->GetOutputSlot(0).SetTensorInfo(armnn::TensorInfo(mergeDims, DataType::Float32)); - - for (unsigned int viewIndex = 0; viewIndex < numConcatViews; ++viewIndex) - { - IOutputSlot& inputSlot = inputs[viewIndex].m_IndexedValue->ResolveArmnnOutputSlot(inputs[viewIndex].m_Index); - inputSlot.Connect(layer->GetInputSlot(viewIndex)); - } - - return std::make_unique<SingleLayerParsedTfOperation>(this, nodeDef, layer); -} - -ParsedTfOperationPtr ITfParser::TfParserImpl::ParseShape(const tensorflow::NodeDef& nodeDef, - const tensorflow::GraphDef& graphDef) -{ - IgnoreUnused(graphDef); - // Note: the Shape layer is handled in a special way, because: - // 1. ARMNN doesn't support int32 tensors which it outputs. - // 2. ARMNN works with statically shaped tensors which are known at parse time. - // 3. because of 1. and 2. we treat the output of Shape as a temporary const int32 - // tensor which may be used as an input to other ops, most likely a Reshape. - - const tensorflow::DataType tfDataType = ReadMandatoryNodeTypeAttribute(nodeDef, "out_type"); - if (tfDataType != tensorflow::DT_INT32) - { - throw ParseException( - fmt::format("Armnn only supports DT_INT32 as out_type. Got {} for Node {} {}", - tensorflow::DataType_Name(tfDataType), - nodeDef.name(), - CHECK_LOCATION().AsString())); - } - - const std::vector<OutputOfParsedTfOperation> inputs = GetInputParsedTfOperationsChecked(nodeDef, 1); - IOutputSlot& prevLayerOutputSlot = inputs[0].m_IndexedValue->ResolveArmnnOutputSlot(inputs[0].m_Index); - const TensorInfo& prevLayerTensorInfo = prevLayerOutputSlot.GetTensorInfo(); - unsigned int prevLayerDimensions = prevLayerTensorInfo.GetNumDimensions(); - - std::vector<int32_t> shapeTensorData; - shapeTensorData.reserve(prevLayerDimensions); - - for (unsigned int i=0; i<prevLayerDimensions; ++i) - { - shapeTensorData.push_back(static_cast<int32_t>(prevLayerTensorInfo.GetShape()[i])); - } - - TensorInfo shapeTensorInfo(1, &prevLayerDimensions, DataType::Signed32); - - return std::make_unique<ParsedConstTfOperation<int32_t>>(this, - nodeDef, - &shapeTensorData[0], - shapeTensorInfo); -} - -ParsedTfOperationPtr ITfParser::TfParserImpl::ParseReshape(const tensorflow::NodeDef& nodeDef, - const tensorflow::GraphDef& graphDef) -{ - IgnoreUnused(graphDef); - std::vector<OutputOfParsedTfOperation> inputs = GetInputParsedTfOperationsChecked(nodeDef, 2); - ParsedTfOperation* inputNode = inputs[0].m_IndexedValue; - - if (!HasParsedConstTensor<int32_t>(inputs[1].m_IndexedValue->GetNode().name())) - { - throw ParseException( - fmt::format("ArmNN only supports Reshape layers with constant shapes. " - "Input {} Node {} {}", - inputs[1].m_IndexedValue->GetNode().name(), - nodeDef.name(), - CHECK_LOCATION().AsString())); - } - ParsedConstTfOperation<int32_t>* shapeNode = - PolymorphicDowncast<ParsedConstTfOperation<int32_t>*>(inputs[1].m_IndexedValue); - - armnn::IOutputSlot& prevLayerOutputSlot = inputNode->ResolveArmnnOutputSlot(inputs[0].m_Index); - TensorInfo inputTensorInfo = prevLayerOutputSlot.GetTensorInfo(); - - std::vector<int32_t> shapeTensorData; - ConstTensor shapeTensor = shapeNode->GetConstTensor(shapeTensorData); - const TensorInfo outputTensorInfo = PrepareReshape(inputTensorInfo, shapeTensorData); - - TensorShape targetShape = outputTensorInfo.GetShape(); - ReshapeDescriptor reshapeDesc; - reshapeDesc.m_TargetShape = targetShape; - - IConnectableLayer* layer = m_Network->AddReshapeLayer(reshapeDesc, nodeDef.name().c_str()); - prevLayerOutputSlot.Connect(layer->GetInputSlot(0)); - layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); - - return std::make_unique<SingleLayerParsedTfOperation>(this, nodeDef, layer); -} - -ParsedTfOperationPtr ITfParser::TfParserImpl::ParseResizeBilinear(const tensorflow::NodeDef& nodeDef, - const tensorflow::GraphDef& graphDef) -{ - IgnoreUnused(graphDef); - std::vector<OutputOfParsedTfOperation> inputs = GetInputParsedTfOperationsChecked(nodeDef, 2); - - if (!HasParsedConstTensor<int32_t>(inputs[1].m_IndexedValue->GetNode().name())) - { - throw ParseException( - fmt::format("ArmNN only supports ResizeBilinear layers with constant sizes. " - "Input {}. Node {} {}", - inputs[1].m_IndexedValue->GetNode().name(), - nodeDef.name(), - CHECK_LOCATION().AsString())); - } - ParsedConstTfOperation<int32_t>* sizeNode = - PolymorphicDowncast<ParsedConstTfOperation<int32_t>*>(inputs[1].m_IndexedValue); - - // Checks the align_corners attribute is not set. - if (ReadOptionalNodeBoolAttribute(nodeDef, "align_corners", false)) - { - throw ParseException( - fmt::format("ArmNN only supports ResizeBilinear layers with align_corners set to false. " - "Node {} {}", - nodeDef.name(), - CHECK_LOCATION().AsString())); - } - - // Data for the parsed tensor args (size) must be stored locally. - std::vector<int32_t> sizeTensorData; - ConstTensor sizeTensor = sizeNode->GetConstTensor(sizeTensorData); - - // The descriptor only has target height and width attributes, which we get from the size tensor. - ResizeDescriptor desc; - desc.m_Method = armnn::ResizeMethod::Bilinear; - desc.m_TargetHeight = static_cast<uint32_t> (sizeTensorData[0]); - desc.m_TargetWidth = static_cast<uint32_t> (sizeTensorData[1]); - desc.m_DataLayout = armnn::DataLayout::NHWC; - - IConnectableLayer* layer = m_Network->AddResizeLayer(desc, nodeDef.name().c_str()); - - IOutputSlot& inputSlot = inputs[0].m_IndexedValue->ResolveArmnnOutputSlot(inputs[0].m_Index); - TensorInfo inputTensorInfo = inputSlot.GetTensorInfo(); - // The input shape is always in BHWC format, this will be swizzled below; for now, - // get the batch and channels to make up the ArmNN output shape with the target size. - unsigned int outBatch = inputTensorInfo.GetShape()[0]; - unsigned int outChannels = inputTensorInfo.GetShape()[3]; - unsigned int outHeight = desc.m_TargetHeight; - unsigned int outWidth = desc.m_TargetWidth; - TensorShape outShape({outBatch, outHeight, outWidth, outChannels }); - // The output DataType is always Float32, regardless of the input DataType. - const TensorInfo outputTensorInfo(outShape, armnn::DataType::Float32); - layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); - - inputSlot.Connect(layer->GetInputSlot(0)); - - return std::make_unique<SingleLayerParsedTfOperation>(this, nodeDef, layer); -} - -TensorInfo OutputShapeOfSqueeze(const tensorflow::NodeDef& nodeDef, TensorInfo inputTensorInfo) -{ - ARMNN_ASSERT(nodeDef.op() == "Squeeze"); - tensorflow::DataType tfDataType = ReadMandatoryNodeTypeAttribute(nodeDef, "T"); - - DataType type; - if (tfDataType == tensorflow::DT_FLOAT) - { - type = DataType::Float32; - } - else if (tfDataType == tensorflow::DT_INT32) - { - type = DataType::Signed32; - } - else - { - throw ParseException( - fmt::format("Unsupported DataType {} for Squeeze operation {} {}", - tensorflow::DataType_Name(tfDataType), - nodeDef.name(), - CHECK_LOCATION().AsString())); - } - - - if (inputTensorInfo.GetNumDimensions() > 4) - { - throw ParseException( - fmt::format("Unsupported number of dimensions: {} for input shape for Squeeze {} {}", - inputTensorInfo.GetNumDimensions(), - nodeDef.name(), - CHECK_LOCATION().AsString())); - } - - std::vector<uint32_t> squeezeDims = ReadOptionalNodeUint32ListAttribute(nodeDef, "squeeze_dims"); - static const uint32_t dimensionSequence[] = { 0, 1, 2, 3 }; - - if (squeezeDims.empty()) - { - squeezeDims.assign(dimensionSequence, - dimensionSequence+inputTensorInfo.GetNumDimensions()); - } - - std::vector<uint32_t> outputDims; - for(unsigned int i = 0; i < inputTensorInfo.GetNumDimensions(); i++) - { - bool skipSqueeze = (std::find(squeezeDims.begin(), squeezeDims.end(), i) == squeezeDims.end()); - auto currentDimension = inputTensorInfo.GetShape()[i]; - if (skipSqueeze || currentDimension != 1) - { - outputDims.push_back(currentDimension); - } - } - - if (outputDims.size() > 4) - { - throw ParseException( - fmt::format("Unsupported number of dimensions: {} for output shape for Squeeze {} {}", - outputDims.size(), - nodeDef.name(), - CHECK_LOCATION().AsString())); - } - - TensorShape outShape = TensorShape(static_cast<unsigned int>(outputDims.size()), - outputDims.data()); - - TensorInfo outTensorInfo = inputTensorInfo; - outTensorInfo.SetShape(outShape); - outTensorInfo.SetDataType(type); - - return outTensorInfo; -} - -ParsedTfOperationPtr ITfParser::TfParserImpl::ParseSqueeze(const tensorflow::NodeDef& nodeDef, - const tensorflow::GraphDef& graphDef) -{ - IgnoreUnused(graphDef); - std::vector<OutputOfParsedTfOperation> inputs = GetInputParsedTfOperationsChecked(nodeDef, 1); - - IOutputSlot& prevLayerOutputSlot = inputs[0].m_IndexedValue->ResolveArmnnOutputSlot(inputs[0].m_Index); - TensorInfo inputTensorInfo = prevLayerOutputSlot.GetTensorInfo(); - - TensorInfo outputInfo; - outputInfo = OutputShapeOfSqueeze(nodeDef, inputTensorInfo); - - ReshapeDescriptor reshapeDesc; - reshapeDesc.m_TargetShape = outputInfo.GetShape(); - IConnectableLayer* layer = m_Network->AddReshapeLayer(reshapeDesc, nodeDef.name().c_str()); - prevLayerOutputSlot.Connect(layer->GetInputSlot(0)); - layer->GetOutputSlot(0).SetTensorInfo(outputInfo); - - return std::make_unique<SingleLayerParsedTfOperation>(this, nodeDef, layer); -} - -ParsedTfOperationPtr ITfParser::TfParserImpl::ParseLrn(const tensorflow::NodeDef& nodeDef, - const tensorflow::GraphDef& graphDef) -{ - IgnoreUnused(graphDef); - std::vector<OutputOfParsedTfOperation> inputs = GetInputParsedTfOperationsChecked(nodeDef, 1); - - NormalizationDescriptor normalizationDescriptor; - normalizationDescriptor.m_NormMethodType = NormalizationAlgorithmMethod::LocalBrightness; - normalizationDescriptor.m_NormChannelType = NormalizationAlgorithmChannel::Across; - normalizationDescriptor.m_Alpha = ReadMandatoryNodeFloatAttribute(nodeDef, "alpha"); - normalizationDescriptor.m_Beta = ReadMandatoryNodeFloatAttribute(nodeDef, "beta"); - normalizationDescriptor.m_K = ReadMandatoryNodeFloatAttribute(nodeDef, "bias"); - normalizationDescriptor.m_NormSize = ReadMandatoryNodeUint32Attribute(nodeDef, "depth_radius"); - normalizationDescriptor.m_DataLayout = armnn::DataLayout::NHWC; - - // The window size must be an odd value. For a window size of (2 * n + 1), TensorFlow defines depth_radius = n. - normalizationDescriptor.m_NormSize = normalizationDescriptor.m_NormSize * 2 + 1; - - IOutputSlot& prevLayerOutputSlot = inputs[0].m_IndexedValue->ResolveArmnnOutputSlot(inputs[0].m_Index); - IConnectableLayer* layer = m_Network->AddNormalizationLayer(normalizationDescriptor, - nodeDef.name().c_str()); - prevLayerOutputSlot.Connect(layer->GetInputSlot(0)); - layer->GetOutputSlot(0).SetTensorInfo(prevLayerOutputSlot.GetTensorInfo()); - - return std::make_unique<SingleLayerParsedTfOperation>(this, nodeDef, layer); -} - -/// An ParsedTfOperation for a MatMul node. -/// Creation of the armnn FullyConnected layer is deferred until it is actually needed, because -/// MatMul nodes are often used for the first part of a biased FullyConnected (MatMul followed -/// by Add) and in these cases armnn doesn't need a separate layer for the MatMul. -/// -class ParsedMatMulTfOperation : public DeferredSingleLayerParsedTfOperation -{ -public: - ParsedMatMulTfOperation(ITfParser::TfParserImpl* parser, const tensorflow::NodeDef& node) - : DeferredSingleLayerParsedTfOperation(parser, node) - { - } - - void CreateLayerDeferred() override - { - ARMNN_ASSERT(m_Layer == nullptr); - m_Layer = m_Parser->AddFullyConnectedLayer(m_Node, nullptr, m_Node.name().c_str()); - } -}; - -ParsedTfOperationPtr ITfParser::TfParserImpl::ParseMatMul(const tensorflow::NodeDef& nodeDef, - const tensorflow::GraphDef& graphDef) -{ - IgnoreUnused(graphDef); - - // Defers the creation of the layer (see ParsedMatMulTfOperation). - return std::make_unique<ParsedMatMulTfOperation>(this, nodeDef); -} - -ParsedTfOperationPtr ITfParser::TfParserImpl::ParseMean(const tensorflow::NodeDef& nodeDef, - const tensorflow::GraphDef& graphDef) -{ - IgnoreUnused(graphDef); - std::vector<OutputOfParsedTfOperation> inputs = GetInputParsedTfOperationsChecked(nodeDef, 2); - IOutputSlot& inputSlot = inputs[0].m_IndexedValue->ResolveArmnnOutputSlot(inputs[0].m_Index); - TensorInfo inputTensorInfo = inputSlot.GetTensorInfo(); - - if (inputs.size() != 2) - { - throw ParseException( - fmt::format("Mean expects two inputs!. Got {} for Node {} {}", - inputs.size(), - nodeDef.name(), - CHECK_LOCATION().AsString())); - } - - bool keepDims = ReadMandatoryNodeBoolAttribute(nodeDef, "keep_dims"); - - ParsedConstTfOperation<int32_t>* axisNode = - PolymorphicDowncast<ParsedConstTfOperation<int32_t>*>(inputs[1].m_IndexedValue); - - const TensorInfo& axisTensorInfo = axisNode->GetTensorInfo(); - - ConstTensor axisTensor(axisTensorInfo, axisNode->GetStorage()); - const int* axisData = static_cast<const int*>(axisTensor.GetMemoryArea()); - - TensorInfo outputTensorInfo; - MeanDescriptor meanDescriptor; - meanDescriptor.m_KeepDims = keepDims; - - // Negative axis values are supported so that the process requires - // to convert them into the corresponding positive ones. - // Duplicate values are also removed. - std::vector<int> rawAxisVector(axisData, axisData + axisTensorInfo.GetNumElements()); - std::set<unsigned int> positiveAxisSet; - int rank = static_cast<int>(inputTensorInfo.GetNumDimensions()); - - std::transform(rawAxisVector.begin(), rawAxisVector.end(), - std::inserter(positiveAxisSet, positiveAxisSet.begin()), - [rank](int i) -> unsigned int { return static_cast<unsigned int>((i + rank) % rank); }); - - CalculateReducedOutputTensoInfo(inputTensorInfo, positiveAxisSet, keepDims, outputTensorInfo); - - if (inputTensorInfo.GetNumDimensions() > positiveAxisSet.size()) - { - meanDescriptor.m_Axis.assign(positiveAxisSet.begin(), positiveAxisSet.end()); - } - - IConnectableLayer* layer = m_Network->AddMeanLayer(meanDescriptor, nodeDef.name().c_str()); - layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); - inputSlot.Connect(layer->GetInputSlot(0)); - - return std::make_unique<SingleLayerParsedTfOperation>(this, nodeDef, layer); -} - -/// An ParsedTfOperation for a Mul node. -/// Creation of the armnn Mul layer is deferred until it is actually needed, because Mul nodes -/// are also used for the first part of a leaky relu activation function (Mul followed by Maximum) -/// and in these cases armnn doesn't need a separate layer for the Mul. -/// -class ParsedMulTfOperation : public DeferredSingleLayerParsedTfOperation -{ -public: - ParsedMulTfOperation(ITfParser::TfParserImpl* parser, const tensorflow::NodeDef& node) - : DeferredSingleLayerParsedTfOperation(parser, node) - { - } - - void CreateLayerDeferred() override - { - ARMNN_ASSERT(m_Layer == nullptr); - m_Layer = m_Parser->AddMultiplicationLayer(m_Node); - } -}; - -ParsedTfOperationPtr ITfParser::TfParserImpl::ParseMul(const tensorflow::NodeDef& nodeDef, - const tensorflow::GraphDef& graphDef) -{ - IgnoreUnused(graphDef); - - return std::make_unique<ParsedMulTfOperation>(this, nodeDef); -} - -ParsedTfOperationPtr ITfParser::TfParserImpl::ParsePlaceholder(const tensorflow::NodeDef& nodeDef, - const tensorflow::GraphDef& graphDef) -{ - IgnoreUnused(graphDef); - - std::vector<OutputOfParsedTfOperation> inputs = GetInputParsedTfOperationsChecked(nodeDef, 0); - - const LayerBindingId layerId = armnn::numeric_cast<LayerBindingId>(m_NetworkInputsBindingInfo.size()); - - auto it = m_InputShapes.find(nodeDef.name()); - if (it == m_InputShapes.end()) - { - throw ParseException( - fmt::format("Missing input shape for Placeholder '{}' {}", - nodeDef.name(), - CHECK_LOCATION().AsString())); - } - TensorInfo tensorInfo(it->second, DataType::Float32); - - IConnectableLayer* const layer = m_Network->AddInputLayer(layerId, nodeDef.name().c_str()); - - layer->GetOutputSlot(0).SetTensorInfo(tensorInfo); - - TrackInputBinding(layer, layerId, tensorInfo); - - return std::make_unique<SingleLayerParsedTfOperation>(this, nodeDef, layer); -} - -ParsedTfOperationPtr ITfParser::TfParserImpl::ParseRealDiv(const tensorflow::NodeDef& nodeDef, - const tensorflow::GraphDef& graphDef) -{ - IgnoreUnused(graphDef); - return AddRealDivLayer(nodeDef); -} - -ParsedTfOperationPtr ITfParser::TfParserImpl::ParseRelu(const tensorflow::NodeDef& nodeDef, - const tensorflow::GraphDef& graphDef) -{ - IgnoreUnused(graphDef); - - ActivationDescriptor activationDesc; - activationDesc.m_Function = ActivationFunction::ReLu; - return AddActivationLayer(nodeDef, activationDesc); -} - -ParsedTfOperationPtr ITfParser::TfParserImpl::ParseRelu6(const tensorflow::NodeDef& nodeDef, - const tensorflow::GraphDef& graphDef) -{ - IgnoreUnused(graphDef); - - ActivationDescriptor activationDesc; - activationDesc.m_Function = ActivationFunction::BoundedReLu; - activationDesc.m_A = 6.0f; - activationDesc.m_B = 0.0f; - - return AddActivationLayer(nodeDef, activationDesc); -} - -ParsedTfOperationPtr ITfParser::TfParserImpl::ParseSigmoid(const tensorflow::NodeDef& nodeDef, - const tensorflow::GraphDef& graphDef) -{ - IgnoreUnused(graphDef); - - ActivationDescriptor activationDesc; - activationDesc.m_Function = ActivationFunction::Sigmoid; - - return AddActivationLayer(nodeDef, activationDesc); -} - -ParsedTfOperationPtr ITfParser::TfParserImpl::ParseRsqrt(const tensorflow::NodeDef &nodeDef, - const tensorflow::GraphDef &graphDef) -{ - IgnoreUnused(graphDef); - - std::vector<OutputOfParsedTfOperation> inputs = GetInputParsedTfOperationsChecked(nodeDef, 1); - - ElementwiseUnaryDescriptor descriptor(UnaryOperation::Rsqrt); - IConnectableLayer* const layer = m_Network->AddElementwiseUnaryLayer(descriptor, nodeDef.name().c_str()); - - IOutputSlot& prevLayerOutputSlot = inputs[0].m_IndexedValue->ResolveArmnnOutputSlot(inputs[0].m_Index); - prevLayerOutputSlot.Connect(layer->GetInputSlot(0)); - layer->GetOutputSlot(0).SetTensorInfo(prevLayerOutputSlot.GetTensorInfo()); - - return std::make_unique<SingleLayerParsedTfOperation>(this, nodeDef, layer); -} - -ParsedTfOperationPtr ITfParser::TfParserImpl::ParseSoftmax(const tensorflow::NodeDef& nodeDef, - const tensorflow::GraphDef& graphDef) -{ - IgnoreUnused(graphDef); - - std::vector<OutputOfParsedTfOperation> inputs = GetInputParsedTfOperationsChecked(nodeDef, 1); - - SoftmaxDescriptor softmaxDescriptor; - IConnectableLayer* const layer = m_Network->AddSoftmaxLayer(softmaxDescriptor, nodeDef.name().c_str()); - - IOutputSlot& prevLayerSlot = inputs[0].m_IndexedValue->ResolveArmnnOutputSlot(inputs[0].m_Index); - prevLayerSlot.Connect(layer->GetInputSlot(0)); - layer->GetOutputSlot(0).SetTensorInfo(prevLayerSlot.GetTensorInfo()); - - return std::make_unique<SingleLayerParsedTfOperation>(this, nodeDef, layer); -} - -ParsedTfOperationPtr ITfParser::TfParserImpl::ParseSplit(const tensorflow::NodeDef& nodeDef, - const tensorflow::GraphDef& graphDef) -{ - IgnoreUnused(graphDef); - - std::vector<OutputOfConstNodeDef> nodes = GetTfInputNodes(nodeDef); - unsigned int numInputs = static_cast<unsigned int>(nodes.size()); - std::vector<OutputOfParsedTfOperation> inputs = GetInputParsedTfOperationsChecked(nodeDef, numInputs); - - // Constant tensor index - unsigned int index = GetConstInputIndex(inputs); - // Get the axis tensor data - ParsedConstTfOperation<int32_t>* shapeNode = - PolymorphicDowncast<ParsedConstTfOperation<int32_t>*>(inputs[index].m_IndexedValue); - - std::vector<int32_t> axisTensorData; - shapeNode->GetConstTensor(axisTensorData); - - // This splitDim indicates the data format: 3 is the NHWC, 1 is the NCHW. - const unsigned int splitDim = static_cast<unsigned int>(axisTensorData[0]); - - // Armnn supports split along the channel dimension for data formats NHWC and NCHW. - if (splitDim == 0 || splitDim == 2) - { - throw armnn::ParseException( - fmt::format("Dimension {} for split is not supported by Armnn. " - "Node {} {}", - splitDim, - nodeDef.name(), - CHECK_LOCATION().AsString())); - } - - // As Armnn only supports splitter outputs of the same shape, therefore num_split will be limited to an integer. - uint32_t num_split = ReadMandatoryNodeUint32Attribute(nodeDef, "num_split"); - - IOutputSlot& inputSlot = inputs[1 - index].m_IndexedValue->ResolveArmnnOutputSlot(inputs[1 - index].m_Index); - TensorInfo inputTensorInfo = inputSlot.GetTensorInfo(); - - const unsigned int supportedNumDims = 4; - auto inputDimSize = inputTensorInfo.GetNumDimensions(); - - if (inputDimSize != supportedNumDims) - { - throw armnn::ParseException( - fmt::format("The number of dimensions: {} for input tensors of the " - "split op should be {} {}", - inputTensorInfo.GetNumDimensions(), - supportedNumDims, - CHECK_LOCATION().AsString())); - } - - std::vector<unsigned int> splitterDimSizes(inputDimSize); - - // Add current input shape to splitterDimSizes - for (unsigned int i = 0; i < inputDimSize; ++i) - { - splitterDimSizes[i] = inputTensorInfo.GetShape()[i]; - } - - if (splitterDimSizes[splitDim] % num_split != 0) - { - throw ParseException("Number of splits must evenly divide the dimension"); - } - splitterDimSizes[splitDim] /= num_split; - - SplitterDescriptor splitDesc(num_split); - for (unsigned int g = 0; g < num_split; ++g) - { - // Set the size of the views. - for (unsigned int dimIdx = 0; dimIdx < splitterDimSizes.size(); ++dimIdx) - { - splitDesc.SetViewSize(g, dimIdx, splitterDimSizes[dimIdx]); - } - splitDesc.SetViewOriginCoord(g, splitDim, splitterDimSizes[splitDim] * g); - } - - IConnectableLayer *layer = m_Network->AddSplitterLayer(splitDesc, nodeDef.name().c_str()); - - inputSlot.Connect(layer->GetInputSlot(0)); - - TensorShape outShape = TensorShape(static_cast<unsigned int>(splitterDimSizes.size()), - splitterDimSizes.data()); - - for (unsigned int i = 0; i < layer->GetNumOutputSlots(); ++i) - { - layer->GetOutputSlot(i).SetTensorInfo(armnn::TensorInfo(outShape, inputTensorInfo.GetDataType())); - } - - return std::make_unique<SingleLayerParsedTfOperation>(this, nodeDef, layer); -} - -ParsedTfOperationPtr ITfParser::TfParserImpl::ParseSoftplus(const tensorflow::NodeDef& nodeDef, - const tensorflow::GraphDef& graphDef) -{ - IgnoreUnused(graphDef); - - ActivationDescriptor activationDesc; - activationDesc.m_Function = ActivationFunction::SoftReLu; - - return AddActivationLayer(nodeDef, activationDesc); -} - -ParsedTfOperationPtr ITfParser::TfParserImpl::ParseStridedSlice(const tensorflow::NodeDef& nodeDef, - const tensorflow::GraphDef& graphDef) -{ - IgnoreUnused(graphDef); - - std::vector<OutputOfConstNodeDef> nodes = GetTfInputNodes(nodeDef); - unsigned int numInputs = static_cast<unsigned int>(nodes.size()); - std::vector<OutputOfParsedTfOperation> inputs = GetInputParsedTfOperationsChecked(nodeDef, numInputs); - - ParsedConstTfOperation<int32_t>* beginNode = - PolymorphicDowncast<ParsedConstTfOperation<int32_t> *>(inputs[1].m_IndexedValue); - std::vector<int32_t> beginTensorData; - beginNode->GetConstTensor(beginTensorData); - - ParsedConstTfOperation<int32_t>* endNode = - PolymorphicDowncast<ParsedConstTfOperation<int32_t> *>(inputs[2].m_IndexedValue); - std::vector<int32_t> endTensorData; - endNode->GetConstTensor(endTensorData); - - ParsedConstTfOperation<int32_t>* stridesNode = - PolymorphicDowncast<ParsedConstTfOperation<int32_t> *>(inputs[3].m_IndexedValue); - std::vector<int32_t> stridesTensorData; - stridesNode->GetConstTensor(stridesTensorData); - - StridedSliceDescriptor desc; - desc.m_Begin = beginTensorData; - desc.m_End = endTensorData; - desc.m_Stride = stridesTensorData; - desc.m_BeginMask = ReadMandatoryNodeInt32Attribute(nodeDef, "begin_mask"); - desc.m_EndMask = ReadMandatoryNodeInt32Attribute(nodeDef, "end_mask"); - desc.m_EllipsisMask = ReadMandatoryNodeInt32Attribute(nodeDef, "ellipsis_mask"); - desc.m_NewAxisMask = ReadMandatoryNodeInt32Attribute(nodeDef, "new_axis_mask"); - desc.m_ShrinkAxisMask = ReadMandatoryNodeInt32Attribute(nodeDef, "shrink_axis_mask"); - desc.m_DataLayout = armnn::DataLayout::NHWC; - IConnectableLayer* const layer = m_Network->AddStridedSliceLayer(desc, nodeDef.name().c_str()); - - IOutputSlot& prevLayerSlot = inputs[0].m_IndexedValue->ResolveArmnnOutputSlot(inputs[0].m_Index); - TensorInfo inputTensorInfo = prevLayerSlot.GetTensorInfo(); - - TensorInfo outputTensorInfo; - CalculateStridedSliceOutputTensorInfo(inputTensorInfo, desc, outputTensorInfo); - - prevLayerSlot.Connect(layer->GetInputSlot(0)); - layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); - - return std::make_unique<SingleLayerParsedTfOperation>(this, nodeDef, layer); -} - -ParsedTfOperationPtr ITfParser::TfParserImpl::ParseTanh(const tensorflow::NodeDef& nodeDef, - const tensorflow::GraphDef& graphDef) -{ - IgnoreUnused(graphDef); - - ActivationDescriptor activationDesc; - activationDesc.m_Function = ActivationFunction::TanH; - activationDesc.m_A = 1.0f; - activationDesc.m_B = 1.0f; - - return AddActivationLayer(nodeDef, activationDesc); -} - -ParsedTfOperationPtr ITfParser::TfParserImpl::AddActivationLayer(const tensorflow::NodeDef& nodeDef, - ActivationDescriptor& activationDesc) -{ - std::vector<OutputOfParsedTfOperation> inputs = GetInputParsedTfOperationsChecked(nodeDef, 1); - - IConnectableLayer* const layer = m_Network->AddActivationLayer(activationDesc, nodeDef.name().c_str()); - - IOutputSlot& prevLayerOutputSlot = inputs[0].m_IndexedValue->ResolveArmnnOutputSlot(inputs[0].m_Index); - prevLayerOutputSlot.Connect(layer->GetInputSlot(0)); - layer->GetOutputSlot(0).SetTensorInfo(prevLayerOutputSlot.GetTensorInfo()); - return std::make_unique<SingleLayerParsedTfOperation>(this, nodeDef, layer); -} - -ParsedTfOperationPtr ITfParser::TfParserImpl::ParseMaxPool(const tensorflow::NodeDef& nodeDef, - const tensorflow::GraphDef& graphDef) -{ - return ParsePooling2d(nodeDef, graphDef, PoolingAlgorithm::Max); -} - -ParsedTfOperationPtr ITfParser::TfParserImpl::ParseAvgPool(const tensorflow::NodeDef& nodeDef, - const tensorflow::GraphDef& graphDef) -{ - return ParsePooling2d(nodeDef, graphDef, PoolingAlgorithm::Average); -} - -ParsedTfOperationPtr ITfParser::TfParserImpl::ParsePooling2d(const tensorflow::NodeDef& nodeDef, - const tensorflow::GraphDef& graphDef, PoolingAlgorithm pooltype) -{ - IgnoreUnused(graphDef); - - std::vector<OutputOfParsedTfOperation> inputs = GetInputParsedTfOperationsChecked(nodeDef, 1); - IOutputSlot& inputSlot = inputs[0].m_IndexedValue->ResolveArmnnOutputSlot(inputs[0].m_Index); - TensorInfo inputTensorInfo = inputSlot.GetTensorInfo(); - - if (inputs.size() != 1) - { - throw ParseException( - fmt::format("2D Pooling expects one input!. Got {} for Node {} {}", - inputs.size(), - nodeDef.name(), - CHECK_LOCATION().AsString())); - } - - std::string paddingString = ReadMandatoryNodeStringAttribute(nodeDef, "padding"); - std::string dataFormat = ReadMandatoryNodeStringAttribute(nodeDef, "data_format"); - std::vector<uint32_t> strides = ReadMandatoryNodeUint32ListAttribute(nodeDef, "strides"); - std::vector<uint32_t> ksize = ReadMandatoryNodeUint32ListAttribute(nodeDef, "ksize"); // size of pool windows - - Pooling2dDescriptor pooling2dDescriptor; - pooling2dDescriptor.m_PoolType = pooltype; - pooling2dDescriptor.m_PaddingMethod = PaddingMethod::Exclude; - pooling2dDescriptor.m_OutputShapeRounding = OutputShapeRounding::Floor; - - CHECK_DATA_FORMAT(nodeDef, dataFormat, "Pooling2D"); - DataLayout dataLayout = dataFormat == "NHWC" ? DataLayout::NHWC : DataLayout::NCHW; - pooling2dDescriptor.m_DataLayout = dataLayout; - DataLayoutIndexed dataLayoutIndexed(dataLayout); - - pooling2dDescriptor.m_StrideX = strides[dataLayoutIndexed.GetWidthIndex()]; - pooling2dDescriptor.m_StrideY = strides[dataLayoutIndexed.GetHeightIndex()]; - pooling2dDescriptor.m_PoolWidth = ksize[dataLayoutIndexed.GetWidthIndex()]; - pooling2dDescriptor.m_PoolHeight = ksize[dataLayoutIndexed.GetHeightIndex()]; - - uint32_t inputHeight = inputTensorInfo.GetShape()[dataLayoutIndexed.GetHeightIndex()]; - uint32_t inputWidth = inputTensorInfo.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; - - outputHeight = static_cast<uint32_t>(ceil(static_cast<float>(inputHeight) / - static_cast<float>(pooling2dDescriptor.m_StrideY))); - outputWidth = static_cast<uint32_t>(ceil(static_cast<float>(inputWidth) / - static_cast<float>(pooling2dDescriptor.m_StrideX))); - } - else if (paddingString == "VALID") - { - padding = false; - - outputHeight = static_cast<uint32_t>(ceil( - static_cast<float>(inputHeight - pooling2dDescriptor.m_PoolHeight + 1) / - static_cast<float>(pooling2dDescriptor.m_StrideY))); - outputWidth = static_cast<uint32_t>(ceil( - static_cast<float>(inputWidth - pooling2dDescriptor.m_PoolWidth + 1) / - static_cast<float>(pooling2dDescriptor.m_StrideX))); - } - - switch (dataLayout) - { - case DataLayout::NHWC: - outputInfo = TensorInfo({ inputTensorInfo.GetShape()[0], - outputHeight, - outputWidth, - inputTensorInfo.GetShape()[3] }, - DataType::Float32); - break; - case DataLayout::NCHW: - outputInfo = TensorInfo({ inputTensorInfo.GetShape()[0], - inputTensorInfo.GetShape()[1], - outputHeight, - outputWidth }, - DataType::Float32); - break; - } - - CalcPadding(inputWidth, pooling2dDescriptor.m_PoolWidth, pooling2dDescriptor.m_StrideX, 1u, - pooling2dDescriptor.m_PadLeft, pooling2dDescriptor.m_PadRight, padding); - CalcPadding(inputHeight, pooling2dDescriptor.m_PoolHeight, pooling2dDescriptor.m_StrideY, 1u, - pooling2dDescriptor.m_PadTop, pooling2dDescriptor.m_PadBottom, padding); - - - IConnectableLayer* layer = m_Network->AddPooling2dLayer(pooling2dDescriptor, nodeDef.name().c_str()); - if (layer == nullptr) - { - throw ParseException( - fmt::format("Failed to add pooling2d layer for {} {}", - nodeDef.name(), - CHECK_LOCATION().AsString())); - } - - layer->GetOutputSlot(0).SetTensorInfo(outputInfo); - - inputSlot.Connect(layer->GetInputSlot(0)); - - return std::make_unique<SingleLayerParsedTfOperation>(this, nodeDef, layer); -} - -ParsedTfOperationPtr ITfParser::TfParserImpl::AddAdditionLayer(const tensorflow::NodeDef& nodeDef, bool isBiasAdd) -{ - std::vector<OutputOfParsedTfOperation> inputs = GetInputParsedTfOperationsChecked(nodeDef, 2); - - IOutputSlot* input0Slot = &inputs[0].m_IndexedValue->ResolveArmnnOutputSlot(inputs[0].m_Index); - IOutputSlot* input1Slot = &inputs[1].m_IndexedValue->ResolveArmnnOutputSlot(inputs[1].m_Index); - - const TensorInfo& input0Info = input0Slot->GetTensorInfo(); - const TensorInfo& input1Info = input1Slot->GetTensorInfo(); - - if (isBiasAdd) - { - // BiasAdd takes bias as a 1D tensor. We need to add a reshape layer to create a 4D tensor - // with the same data in the correct dimension for broadcast in addition. - if(input1Info.GetNumDimensions() != 1) - { - throw ParseException( - fmt::format("Unsupported bias for BiasAdd. It should be a 1D vector. " - "Got {} dimensions for input {}. Node {} {}", - input1Info.GetNumDimensions(), - inputs[1].m_IndexedValue->GetNode().name(), - nodeDef.name(), - CHECK_LOCATION().AsString())); - } - - const std::string dataFormat = ReadMandatoryNodeStringAttribute(nodeDef, "data_format"); - - CHECK_DATA_FORMAT(nodeDef, dataFormat, "BiasAdd"); - input1Slot = AddBroadcastReshapeLayer(input0Slot, input1Slot, dataFormat == "NHWC", *m_Network, nodeDef); - } - else - { - if (input0Info.GetNumDimensions() == 1) - { - const bool isNHWC = true; - input0Slot = AddBroadcastReshapeLayer(input1Slot, input0Slot, isNHWC, *m_Network, nodeDef); - } - - if (input1Info.GetNumDimensions() == 1) - { - const bool isNHWC = true; - input1Slot = AddBroadcastReshapeLayer(input0Slot, input1Slot, isNHWC, *m_Network, nodeDef); - } - } - - IConnectableLayer* const layer = m_Network->AddAdditionLayer(nodeDef.name().c_str()); - - input0Slot->Connect(layer->GetInputSlot(0)); - input1Slot->Connect(layer->GetInputSlot(1)); - - if (input0Info.GetNumDimensions() == input1Info.GetNumDimensions()) - { - const TensorShape& input0Shape = input0Info.GetShape(); - const TensorShape& input1Shape = input1Info.GetShape(); - - std::vector<unsigned int> outputShape; - outputShape.reserve(input0Shape.GetNumDimensions()); - TensorInfo outputInfo(input0Info); - - for (unsigned int i = 0; i < input0Shape.GetNumDimensions(); i++) - { - outputShape.push_back(std::max(input0Shape[i], input1Shape[i])); - } - - outputInfo.SetShape(TensorShape(input0Shape.GetNumDimensions(), outputShape.data())); - - layer->GetOutputSlot(0).SetTensorInfo(outputInfo); - } - else if (input0Info.GetNumDimensions() == 1 && isBiasAdd == false) - { - layer->GetOutputSlot(0).SetTensorInfo(input1Slot->GetTensorInfo()); - } - else - { - layer->GetOutputSlot(0).SetTensorInfo(input0Slot->GetTensorInfo()); - } - - return std::make_unique<SingleLayerParsedTfOperation>(this, nodeDef, layer); -} - -ParsedTfOperationPtr ITfParser::TfParserImpl::AddRealDivLayer(const tensorflow::NodeDef& nodeDef) -{ - std::vector<OutputOfParsedTfOperation> inputs = GetInputParsedTfOperationsChecked(nodeDef, 2); - - IConnectableLayer* const layer = m_Network->AddDivisionLayer(nodeDef.name().c_str()); - IOutputSlot* input0Slot = &inputs[0].m_IndexedValue->ResolveArmnnOutputSlot(inputs[0].m_Index); - IOutputSlot* input1Slot = &inputs[1].m_IndexedValue->ResolveArmnnOutputSlot(inputs[1].m_Index); - - auto const input0NumDims = input0Slot->GetTensorInfo().GetNumDimensions(); - auto const input1NumDims = input1Slot->GetTensorInfo().GetNumDimensions(); - - - if (input0NumDims < input1NumDims) - { - const bool isNHWC = true; - input0Slot = AddBroadcastReshapeLayer(input1Slot, input0Slot, isNHWC, *m_Network, nodeDef); - } - if (input1NumDims < input0NumDims) - { - const bool isNHWC = true; - input1Slot = AddBroadcastReshapeLayer(input0Slot, input1Slot, isNHWC, *m_Network, nodeDef); - } - - input0Slot->Connect(layer->GetInputSlot(0)); - input1Slot->Connect(layer->GetInputSlot(1)); - - if (input0NumDims < input1NumDims) - { - layer->GetOutputSlot(0).SetTensorInfo(input1Slot->GetTensorInfo()); - } - else - { - layer->GetOutputSlot(0).SetTensorInfo(input0Slot->GetTensorInfo()); - - } - return std::make_unique<SingleLayerParsedTfOperation>(this, nodeDef, layer); -} - -ParsedTfOperationPtr ITfParser::TfParserImpl::AddMaximumLayer(const tensorflow::NodeDef& nodeDef) -{ - std::vector<OutputOfParsedTfOperation> inputs = GetInputParsedTfOperationsChecked(nodeDef, 2); - - IOutputSlot* input0Slot = &inputs[0].m_IndexedValue->ResolveArmnnOutputSlot(inputs[0].m_Index); - IOutputSlot* input1Slot = &inputs[1].m_IndexedValue->ResolveArmnnOutputSlot(inputs[1].m_Index); - - auto const input0NumDims = input0Slot->GetTensorInfo().GetNumDimensions(); - auto const input1NumDims = input1Slot->GetTensorInfo().GetNumDimensions(); - - if (input0NumDims < input1NumDims) - { - const bool isNHWC = true; - input0Slot = AddBroadcastReshapeLayer(input1Slot, input0Slot, isNHWC, *m_Network, nodeDef); - } - if (input1NumDims < input0NumDims) - { - const bool isNHWC = true; - input1Slot = AddBroadcastReshapeLayer(input0Slot, input1Slot, isNHWC, *m_Network, nodeDef); - } - - IConnectableLayer* const layer = m_Network->AddMaximumLayer(nodeDef.name().c_str()); - - input0Slot->Connect(layer->GetInputSlot(0)); - input1Slot->Connect(layer->GetInputSlot(1)); - - TensorInfo outputInfo = input0Slot->GetTensorInfo(); - std::vector<unsigned int> outputShape; - - const TensorShape& input0Shape = input0Slot->GetTensorInfo().GetShape(); - const TensorShape& input1Shape = input1Slot->GetTensorInfo().GetShape(); - - for (unsigned int i = 0; i < input0Shape.GetNumDimensions(); i++) - { - outputShape.push_back(std::max(input0Shape[i], input1Shape[i])); - } - - outputInfo.SetShape(TensorShape(input0Shape.GetNumDimensions(), outputShape.data())); - layer->GetOutputSlot(0).SetTensorInfo(outputInfo); - - return std::make_unique<SingleLayerParsedTfOperation>(this, nodeDef, layer); -} - -IConnectableLayer* ITfParser::TfParserImpl::AddMultiplicationLayer(const tensorflow::NodeDef& nodeDef) -{ - std::vector<OutputOfParsedTfOperation> inputs = GetInputParsedTfOperationsChecked(nodeDef, 2); - - IConnectableLayer* const layer = m_Network->AddMultiplicationLayer(nodeDef.name().c_str()); - IOutputSlot* input0Slot = &inputs[0].m_IndexedValue->ResolveArmnnOutputSlot(inputs[0].m_Index); - IOutputSlot* input1Slot = &inputs[1].m_IndexedValue->ResolveArmnnOutputSlot(inputs[1].m_Index); - - auto const input0NumDims = input0Slot->GetTensorInfo().GetNumDimensions(); - auto const input1NumDims = input1Slot->GetTensorInfo().GetNumDimensions(); - - if (input0NumDims < input1NumDims) - { - const bool isNHWC = true; - input0Slot = AddBroadcastReshapeLayer(input1Slot, input0Slot, isNHWC, *m_Network, nodeDef); - } - if (input1NumDims < input0NumDims) - { - const bool isNHWC = true; - input1Slot = AddBroadcastReshapeLayer(input0Slot, input1Slot, isNHWC, *m_Network, nodeDef); - } - - input0Slot->Connect(layer->GetInputSlot(0)); - input1Slot->Connect(layer->GetInputSlot(1)); - - if (input0NumDims < input1NumDims) - { - layer->GetOutputSlot(0).SetTensorInfo(input1Slot->GetTensorInfo()); - } - else - { - layer->GetOutputSlot(0).SetTensorInfo(input0Slot->GetTensorInfo()); - } - return layer; -} - -IConnectableLayer* ITfParser::TfParserImpl::AddFullyConnectedLayer(const tensorflow::NodeDef& matMulNodeDef, - const tensorflow::NodeDef* addNodeDef, const char* armnnLayerName) -{ - // Finds bias const (if applicable). - ParsedConstTfOperation<float>* biasNode = nullptr; - if (addNodeDef != nullptr) - { - std::vector<OutputOfParsedTfOperation> addInputs = GetInputParsedTfOperationsChecked(*addNodeDef, 2); - // Finds our inputs. - if (HasParsedConstTensor<float>(addInputs[0].m_IndexedValue->GetNode().name())) - { - biasNode = PolymorphicDowncast<ParsedConstTfOperation<float>*>(addInputs[0].m_IndexedValue); - } - else if (HasParsedConstTensor<float>(addInputs[1].m_IndexedValue->GetNode().name())) - { - biasNode = PolymorphicDowncast<ParsedConstTfOperation<float>*>(addInputs[1].m_IndexedValue); - } - else - { - throw ParseException( - fmt::format("ArmNN only supports fully connected layers with constant bias. " - "Inputs {} and {}. AddNode {}. MatMulNode {} {}", - addInputs[0].m_IndexedValue->GetNode().name(), - addInputs[1].m_IndexedValue->GetNode().name(), - addNodeDef->name(), - matMulNodeDef.name(), - CHECK_LOCATION().AsString())); - } - } - - // Finds matmul inputs. - ParsedConstTfOperation<float>* weightNode = nullptr; - ParsedTfOperation* inputNode = nullptr; - unsigned int inputIdx = 0; - std::vector<OutputOfParsedTfOperation> mulInputs = GetInputParsedTfOperationsChecked(matMulNodeDef, 2); - if (HasParsedConstTensor<float>(mulInputs[0].m_IndexedValue->GetNode().name())) - { - weightNode = PolymorphicDowncast<ParsedConstTfOperation<float>*>(mulInputs[0].m_IndexedValue); - inputNode = mulInputs[1].m_IndexedValue; - inputIdx = mulInputs[1].m_Index; - } - else if (HasParsedConstTensor<float>(mulInputs[1].m_IndexedValue->GetNode().name())) - { - weightNode = PolymorphicDowncast<ParsedConstTfOperation<float>*>(mulInputs[1].m_IndexedValue); - inputNode = mulInputs[0].m_IndexedValue; - inputIdx = mulInputs[0].m_Index; - } - else - { - throw ParseException( - fmt::format("ArmNN only supports fully connected layers with constant weights. " - "Inputs {} and {}. MatMulNode {} {}", - mulInputs[0].m_IndexedValue->GetNode().name(), - mulInputs[1].m_IndexedValue->GetNode().name(), - matMulNodeDef.name(), - CHECK_LOCATION().AsString())); - } - - std::vector<float> weightTensorData; - // Handles weight. - ConstTensor weights = weightNode->GetConstTensor(weightTensorData); - - FullyConnectedDescriptor desc; - desc.m_BiasEnabled = addNodeDef != nullptr; - - IConnectableLayer* layer = nullptr; - Optional<ConstTensor> optionalBiases; - std::vector<float> biasTensorData; - // Makes the layer. - if (addNodeDef != nullptr) - { - ConstTensor biases = biasNode->GetConstTensor(biasTensorData); - - if (weights.GetShape()[1] != biases.GetShape()[0]) - { - throw ParseException( - fmt::format("Shape of matmul weights and bias do not match. " - "AddNode {}. MatMulNode {} {}", - addNodeDef->name(), - matMulNodeDef.name(), - CHECK_LOCATION().AsString())); - } - - optionalBiases = Optional<ConstTensor>(biases); - } - layer = m_Network->AddFullyConnectedLayer(desc, weights, optionalBiases, armnnLayerName); - - ARMNN_ASSERT(layer != nullptr); - - inputNode->ResolveArmnnOutputSlot(inputIdx).Connect(layer->GetInputSlot(0)); - unsigned int batches = inputNode->ResolveArmnnOutputSlot(inputIdx).GetTensorInfo().GetShape()[0]; - - // Handles output. - TensorInfo outputInfo({ batches, weights.GetShape()[1] }, DataType::Float32); - layer->GetOutputSlot(0).SetTensorInfo(outputInfo); - return layer; -} - -void ITfParser::TfParserImpl::LoadNodeDef(const tensorflow::NodeDef& nodeDef, const tensorflow::GraphDef& graphDef) -{ - // Gets the type of the node (assume float). - tensorflow::DataType type = tensorflow::DT_FLOAT; - if (nodeDef.attr().count("T") != 0) - { - auto attr = nodeDef.attr().at("T"); - type = attr.type(); - } - else if (nodeDef.attr().count("dtype") != 0) - { - auto attr = nodeDef.attr().at("dtype"); - type = attr.type(); - } - - if ((type != tensorflow::DT_FLOAT && type != tensorflow::DT_INT32) && nodeDef.op() != "Const") - { - throw ParseException( - fmt::format("Currently only FLOAT and INT32 are supported for tensorflow nodes (apart from Const). " - "Got {} for Node {} {}", - tensorflow::DataType_Name(type), - nodeDef.name(), - CHECK_LOCATION().AsString())); - } - - const std::string& operation = nodeDef.op(); - auto itControlInput = std::find(m_ControlInputs.begin(), m_ControlInputs.end(), operation); - if (itControlInput != m_ControlInputs.end()) - { - // We currently allow Control Input from TensorFlow graph but we ignore them from ArmNN graph. - return; - } - auto it = ms_OperationNameToParsingFunctions.find(operation); - if (it != ms_OperationNameToParsingFunctions.end()) - { - auto func = it->second; - ParsedTfOperationPtr parsedTfOperation = (this->*func)(nodeDef, graphDef); - ParsedTfOperation* parsedTfOperationRaw = parsedTfOperation.get(); - - // Stores the parsed operation so that dependent layers can connect to it. - auto it = m_ParsedTfOperations.find(nodeDef.name()); - if (it != m_ParsedTfOperations.end()) - { - throw ParseException(fmt::format("Name {} used by more than one node", nodeDef.name())); - } - m_ParsedTfOperations[nodeDef.name()] = std::move(parsedTfOperation); - - // If this node was requested as an output from the network, then adds an ArmNN output layer. - if (std::find(m_RequestedOutputs.begin(), m_RequestedOutputs.end(), nodeDef.name()) != - m_RequestedOutputs.end()) - { - auto outId = ParseOutputId(nodeDef.name()); - const LayerBindingId layerId = armnn::numeric_cast<LayerBindingId>(m_NetworkOutputsBindingInfo.size()); - IOutputSlot& prevSlot = parsedTfOperationRaw->ResolveArmnnOutputSlot(outId.m_Index); - - TensorInfo tensorInfo = prevSlot.GetTensorInfo(); - - IConnectableLayer* outputLayer = m_Network->AddOutputLayer(layerId, nodeDef.name().c_str()); - - prevSlot.Connect(outputLayer->GetInputSlot(0)); - - TrackOutputBinding(outputLayer, layerId, tensorInfo); - } - } - else - { - throw ParseException( - fmt::format("Unsupported operation {} in tensorflow::GraphDef {}", - operation, - CHECK_LOCATION().AsString())); - } -} - -void ITfParser::TfParserImpl::LoadGraphDef(const tensorflow::GraphDef& graphDef) -{ - // Adds all nodes to our map. - m_NodesByName.clear(); - m_NetworkInputsBindingInfo.clear(); - m_NetworkOutputsBindingInfo.clear(); - - for (int i = 0; i < graphDef.node_size(); ++i) - { - const tensorflow::NodeDef& node = graphDef.node(i); - m_NodesByName[node.name()] = &node; - } - - // Checks that the input nodes the user has requested exist. - for (const auto& pair : m_InputShapes) - { - const std::string& requestedInputName = pair.first; - auto nodeIt = m_NodesByName.find(requestedInputName); - if (nodeIt == m_NodesByName.end()) - { - throw ParseException( - fmt::format("Couldn't find requested input node '{}' in graph {}", - requestedInputName, - CHECK_LOCATION().AsString())); - } - } - - // Finds the output nodes the user requested. - std::vector<const tensorflow::NodeDef*> targetNodes; - for (const std::string& requestedOutputName : m_RequestedOutputs) - { - auto nodeIt = m_NodesByName.find(requestedOutputName); - if (nodeIt == m_NodesByName.end()) - { - throw ParseException( - fmt::format("Couldn't find requested output node '{}' in graph {}", - requestedOutputName, - CHECK_LOCATION().AsString())); - } - targetNodes.push_back(nodeIt->second); - } - - // Sorts them into a linear ordering such that all inputs of a node are before the node itself. - std::vector<const tensorflow::NodeDef*> sortedNodes; - if (!armnnUtils::GraphTopologicalSort<const tensorflow::NodeDef*>( - targetNodes, - [this](const tensorflow::NodeDef* node) - { - auto outputs = GetTfInputNodes(*node); - std::vector<const tensorflow::NodeDef*> nodesOnly; - for (const auto & o : outputs) { - nodesOnly.push_back(o.m_IndexedValue); - } - return nodesOnly; - }, - sortedNodes)) - { - throw ParseException( - fmt::format("Cycle detected in graph {}", - CHECK_LOCATION().AsString())); - } - - // Parses each node in order, knowing that all inputs of a node will be processed before the node itself. - for (const auto& it : sortedNodes) - { - const tensorflow::NodeDef& currentNode = *it; - LoadNodeDef(currentNode, graphDef); - } -} - -INetworkPtr ITfParser::TfParserImpl::CreateNetworkFromTextFile(const char* graphFile, - const std::map<std::string, TensorShape>& inputShapes, - const std::vector<std::string>& requestedOutputs) -{ - FILE* fd = fopen(graphFile, "r"); - - if (fd == nullptr) - { - throw FileNotFoundException( - fmt::format("Graph file {} failed to open {}", - graphFile, - CHECK_LOCATION().AsString())); - } - - // Parses the file into a message. - tensorflow::GraphDef graphDef; - auto input = new google::protobuf::io::FileInputStream(fileno(fd)); - bool success = google::protobuf::TextFormat::Parse(input, &graphDef); - delete input; - fclose(fd); - - if (!success) - { - throw ParseException( - fmt::format("Failed to parse graph file {}", - CHECK_LOCATION().AsString())); - } - - return CreateNetworkFromGraphDef(graphDef, inputShapes, requestedOutputs); -} - -INetworkPtr ITfParser::TfParserImpl::CreateNetworkFromString(const char* protoText, - const std::map<std::string, TensorShape>& inputShapes, - const std::vector<std::string>& requestedOutputs) -{ - // Parses the string into a message. - tensorflow::GraphDef graphDef; - bool success = google::protobuf::TextFormat::ParseFromString(protoText, &graphDef); - - if (!success) - { - throw ParseException( - fmt::format("Failed to parse graph file {}", - CHECK_LOCATION().AsString())); - } - - return CreateNetworkFromGraphDef(graphDef, inputShapes, requestedOutputs); -} - -INetworkPtr ITfParser::TfParserImpl::CreateNetworkFromBinaryFile(const char* graphFile, - const std::map<std::string, TensorShape>& inputShapes, - const std::vector<std::string>& requestedOutputs) -{ - FILE* fd = fopen(graphFile, "rb"); - - if (fd == nullptr) - { - throw FileNotFoundException( - fmt::format("Graph file {} failed to open {}", - graphFile, - CHECK_LOCATION().AsString())); - } - - // Parses the file into a message. - tensorflow::GraphDef graphDef; - - google::protobuf::io::FileInputStream inStream(fileno(fd)); - google::protobuf::io::CodedInputStream codedStream(&inStream); - codedStream.SetTotalBytesLimit(INT_MAX); - bool success = graphDef.ParseFromCodedStream(&codedStream); - fclose(fd); - - if (!success) - { - throw ParseException( - fmt::format("Failed to parse protobuf file {} {}", - graphFile, - CHECK_LOCATION().AsString())); - } - - return CreateNetworkFromGraphDef(graphDef, inputShapes, requestedOutputs); -} - -INetworkPtr ITfParser::TfParserImpl::CreateNetworkFromGraphDef(const tensorflow::GraphDef& graphDef, - const std::map<std::string, TensorShape>& inputShapes, - const std::vector<std::string>& requestedOutputs) -{ - m_Network = INetwork::Create(); - - m_InputShapes = inputShapes; - if (requestedOutputs.size() == 0) - { - throw ParseException( - fmt::format("requestedOutputs must have at least one entry {}", - CHECK_LOCATION().AsString())); - } - m_RequestedOutputs = requestedOutputs; - - try - { - LoadGraphDef(graphDef); - } - catch (const ParseException& e) - { - Cleanup(); - throw e; - } - - Cleanup(); - - return std::move(m_Network); -} - -void ITfParser::TfParserImpl::Cleanup() -{ - // Cleanup, in case we reuse this parser. - m_InputShapes.clear(); - m_RequestedOutputs.clear(); - m_NodesByName.clear(); - m_ParsedTfOperations.clear(); -} - -BindingPointInfo ITfParser::TfParserImpl::GetNetworkInputBindingInfo(const std::string& name) const -{ - return GetBindingInfo(name, "input", m_NetworkInputsBindingInfo); -} - -BindingPointInfo ITfParser::TfParserImpl::GetNetworkOutputBindingInfo(const std::string& name) const -{ - return GetBindingInfo(name, "output", m_NetworkOutputsBindingInfo); -} - -std::pair<LayerBindingId, TensorInfo> ITfParser::TfParserImpl::GetBindingInfo(const std::string& layerName, - const char* bindingPointDesc, - const std::unordered_map<std::string, BindingPointInfo>& nameToBindingInfo) -{ - auto it = nameToBindingInfo.find(layerName); - if (it == nameToBindingInfo.end()) - { - throw InvalidArgumentException( - fmt::format("Unknown {} '{}' {}", - bindingPointDesc, - layerName, - CHECK_LOCATION().AsString())); - } - return it->second; -} - -void ITfParser::TfParserImpl::TrackInputBinding(IConnectableLayer* layer, - LayerBindingId id, - const TensorInfo& tensorInfo) -{ - return TrackBindingPoint(layer, id, tensorInfo, "input", m_NetworkInputsBindingInfo); -} - -void ITfParser::TfParserImpl::TrackOutputBinding(IConnectableLayer* layer, - LayerBindingId id, - const TensorInfo& tensorInfo) -{ - return TrackBindingPoint(layer, id, tensorInfo, "output", m_NetworkOutputsBindingInfo); -} - -void ITfParser::TfParserImpl::TrackBindingPoint(IConnectableLayer* layer, - LayerBindingId id, - const TensorInfo& tensorInfo, - const char* bindingPointDesc, - std::unordered_map<std::string, BindingPointInfo>& nameToBindingInfo) -{ - const std::string layerName = layer->GetName(); - auto it = nameToBindingInfo.find(layerName); - if (it == nameToBindingInfo.end()) - { - nameToBindingInfo[layerName] = std::make_pair(id, tensorInfo); - } - else - { - throw ParseException( - fmt::format("Id {} used by more than one {} layer {}", - id, - bindingPointDesc, - CHECK_LOCATION().AsString())); - } -} - -const std::string ITfParser::TfParserImpl::GetVersion() -{ - return TF_PARSER_VERSION; -} - -} // namespace armnnTfParser diff --git a/src/armnnTfParser/TfParser.hpp b/src/armnnTfParser/TfParser.hpp deleted file mode 100644 index 31e074de93..0000000000 --- a/src/armnnTfParser/TfParser.hpp +++ /dev/null @@ -1,276 +0,0 @@ -// -// Copyright © 2017 Arm Ltd. All rights reserved. -// SPDX-License-Identifier: MIT -// -#pragma once - -#include "armnnTfParser/ITfParser.hpp" - -#include "armnn/Types.hpp" -#include "armnn/Tensor.hpp" -#include "armnn/INetwork.hpp" - -#include <list> -#include <map> -#include <memory> -#include <unordered_map> -#include <utility> -#include <vector> - -namespace armnn -{ -class TensorInfo; -} - -namespace tensorflow -{ -class GraphDef; -class NodeDef; -} - -namespace armnnTfParser -{ - -class ParsedTfOperation; -using ParsedTfOperationPtr = std::unique_ptr<ParsedTfOperation>; - -/// -/// WithOutputTensorIndex wraps a value and an index. The purpose of -/// this template is to signify that, in Tensorflow, the input name of -/// a layer has the convention of 'inputTensorName:#index', where the -/// #index can be omitted and it implicitly means the 0 output of -/// the referenced layer. By supporting this notation we can handle -/// layers with multiple outputs, such as Split. -/// -template <typename T> -struct WithOutputTensorIndex -{ - T m_IndexedValue; - unsigned int m_Index; - - WithOutputTensorIndex(const T & value, unsigned int index) - : m_IndexedValue{value} - , m_Index{index} {} - - WithOutputTensorIndex(T && value, unsigned int index) - : m_IndexedValue{value} - , m_Index{index} {} -}; - -using OutputOfParsedTfOperation = WithOutputTensorIndex<ParsedTfOperation *>; -using OutputOfConstNodeDef = WithOutputTensorIndex<const tensorflow::NodeDef*>; -using OutputId = WithOutputTensorIndex<std::string>; - -struct ITfParser::TfParserImpl -{ -public: - /// Creates the network from a protobuf text file on the disk. - armnn::INetworkPtr CreateNetworkFromTextFile( - const char* graphFile, - const std::map<std::string, armnn::TensorShape>& inputShapes, - const std::vector<std::string>& requestedOutputs); - - /// Creates the network from a protobuf binary file on the disk. - armnn::INetworkPtr CreateNetworkFromBinaryFile( - const char* graphFile, - const std::map<std::string, armnn::TensorShape>& inputShapes, - const std::vector<std::string>& requestedOutputs); - - /// Creates the network directly from protobuf text in a string. Useful for debugging/testing. - armnn::INetworkPtr CreateNetworkFromString( - const char* protoText, - const std::map<std::string, armnn::TensorShape>& inputShapes, - const std::vector<std::string>& requestedOutputs); - - /// Retrieves binding info (layer id and tensor info) for the network input identified by the given layer name. - BindingPointInfo GetNetworkInputBindingInfo(const std::string& name) const; - - /// Retrieves binding info (layer id and tensor info) for the network output identified by the given layer name. - BindingPointInfo GetNetworkOutputBindingInfo(const std::string& name) const; - - /// Retrieve version in X.Y.Z form - static const std::string GetVersion(); - - TfParserImpl(); - ~TfParserImpl() = default; - - TfParserImpl(const TfParserImpl&) = delete; - TfParserImpl& operator=(const TfParserImpl&) = delete; - - /// Parses a GraphDef loaded into memory from one of the other CreateNetwork*. - armnn::INetworkPtr CreateNetworkFromGraphDef(const tensorflow::GraphDef& graphDef, - const std::map<std::string, armnn::TensorShape>& inputShapes, - const std::vector<std::string>& requestedOutputs); - - /// Sets up variables and then performs BFS to parse all nodes. - void LoadGraphDef(const tensorflow::GraphDef& graphDef); - - /// Parses a given node, assuming nodes before it in the graph have been done. - void LoadNodeDef(const tensorflow::NodeDef& nodeDef, const tensorflow::GraphDef& graphDef); - - /// Handling identity layers as the input for Conv2D layer. - const tensorflow::NodeDef* ResolveIdentityNode(const tensorflow::NodeDef* nodeDef); - /// Finds the nodes connected as inputs of the given node in the graph. - std::vector<OutputOfConstNodeDef> GetTfInputNodes(const tensorflow::NodeDef& nodeDef) const; - /// Finds the IParsedTfOperations for the nodes connected as inputs of the given node in the graph, - /// and throws an exception if the number of inputs does not match the expected one. - /// This will automatically resolve any identity nodes. The result vector contains the parsed operation - /// together with the output tensor index to make the connection unambiguous. - std::vector<OutputOfParsedTfOperation> GetInputParsedTfOperationsChecked(const tensorflow::NodeDef& nodeDef, - std::size_t expectedNumInputs); - - ParsedTfOperationPtr ParseConst(const tensorflow::NodeDef& nodeDef, const tensorflow::GraphDef& graphDef); - - /// Checks if there is a pre-parsed const tensor available with the given name and Type. - template<typename Type> - bool HasParsedConstTensor(const std::string & nodeName) const; - template<typename Type> - bool HasParsedConstTensor(ParsedTfOperation* parsedTfOpPtr) const; - - unsigned int GetConstInputIndex(const std::vector<OutputOfParsedTfOperation>& inputs); - - ParsedTfOperationPtr ParseAdd(const tensorflow::NodeDef& nodeDef, const tensorflow::GraphDef& graphDef); - ParsedTfOperationPtr ParseAddN(const tensorflow::NodeDef& nodeDef, const tensorflow::GraphDef& graphDef); - ParsedTfOperationPtr ParseBiasAdd(const tensorflow::NodeDef& nodeDef, const tensorflow::GraphDef& graphDef); - ParsedTfOperationPtr ParseConv2D(const tensorflow::NodeDef& nodeDef, const tensorflow::GraphDef& graphDef); - ParsedTfOperationPtr ParseDepthwiseConv2D(const tensorflow::NodeDef& nodeDef, - const tensorflow::GraphDef& graphDef); - ParsedTfOperationPtr ParseExpandDims(const tensorflow::NodeDef& nodeDef, const tensorflow::GraphDef& graphDef); - ParsedTfOperationPtr ParseFusedBatchNorm(const tensorflow::NodeDef& nodeDef, const tensorflow::GraphDef& graphDef); - ParsedTfOperationPtr ParseConcat(const tensorflow::NodeDef& nodeDef, const tensorflow::GraphDef& graphDef); - ParsedTfOperationPtr ParseIdentity(const tensorflow::NodeDef& nodeDef, const tensorflow::GraphDef& graphDef); - ParsedTfOperationPtr ParseLrn(const tensorflow::NodeDef& nodeDef, const tensorflow::GraphDef& graphDef); - ParsedTfOperationPtr ParseMatMul(const tensorflow::NodeDef& nodeDef, const tensorflow::GraphDef& graphDef); - ParsedTfOperationPtr ParseMean(const tensorflow::NodeDef& nodeDef, const tensorflow::GraphDef& graphDef); - ParsedTfOperationPtr ParseMul(const tensorflow::NodeDef& nodeDef, const tensorflow::GraphDef& graphDef); - ParsedTfOperationPtr ParsePlaceholder(const tensorflow::NodeDef& nodeDef, const tensorflow::GraphDef& graphDef); - ParsedTfOperationPtr ParseRealDiv(const tensorflow::NodeDef& nodeDef, const tensorflow::GraphDef& graphDef); - ParsedTfOperationPtr ParseRelu(const tensorflow::NodeDef& nodeDef, const tensorflow::GraphDef& graphDef); - ParsedTfOperationPtr ParseRelu6(const tensorflow::NodeDef& nodeDef, const tensorflow::GraphDef& graphDef); - ParsedTfOperationPtr ParseReshape(const tensorflow::NodeDef& nodeDef, const tensorflow::GraphDef& graphDef); - ParsedTfOperationPtr ParseResizeBilinear(const tensorflow::NodeDef& nodeDef, const tensorflow::GraphDef& graphDef); - ParsedTfOperationPtr ParseRsqrt(const tensorflow::NodeDef& nodeDef, const tensorflow::GraphDef& graphDef); - ParsedTfOperationPtr ParseShape(const tensorflow::NodeDef& nodeDef, const tensorflow::GraphDef& graphDef); - ParsedTfOperationPtr ParseSqueeze(const tensorflow::NodeDef& nodeDef, const tensorflow::GraphDef& graphDef); - ParsedTfOperationPtr ParseSigmoid(const tensorflow::NodeDef& nodeDef, const tensorflow::GraphDef& graphDef); - ParsedTfOperationPtr ParseSoftmax(const tensorflow::NodeDef& nodeDef, const tensorflow::GraphDef& graphDef); - ParsedTfOperationPtr ParseSoftplus(const tensorflow::NodeDef& nodeDef, const tensorflow::GraphDef& graphDef); - ParsedTfOperationPtr ParseSplit(const tensorflow::NodeDef& nodeDef, const tensorflow::GraphDef& graphDef); - ParsedTfOperationPtr ParseStridedSlice(const tensorflow::NodeDef& nodeDef, const tensorflow::GraphDef& graphDef); - ParsedTfOperationPtr ParseTanh(const tensorflow::NodeDef& nodeDef, const tensorflow::GraphDef& graphDef); - ParsedTfOperationPtr ParseMaxPool(const tensorflow::NodeDef& nodeDef, const tensorflow::GraphDef& graphDef); - ParsedTfOperationPtr ParseAvgPool(const tensorflow::NodeDef& nodeDef, const tensorflow::GraphDef& graphDef); - ParsedTfOperationPtr ParsePooling2d(const tensorflow::NodeDef& nodeDef, - const tensorflow::GraphDef& graphDef, - armnn::PoolingAlgorithm pooltype); - ParsedTfOperationPtr ParseEqual(const tensorflow::NodeDef& nodeDef, const tensorflow::GraphDef& graphDef); - ParsedTfOperationPtr ParseMaximum(const tensorflow::NodeDef& nodeDef, const tensorflow::GraphDef& graphDef); - ParsedTfOperationPtr ParseMinimum(const tensorflow::NodeDef& nodeDef, const tensorflow::GraphDef& graphDef); - ParsedTfOperationPtr ParseGather(const tensorflow::NodeDef& nodeDef, const tensorflow::GraphDef& graphDef); - ParsedTfOperationPtr ParseGreater(const tensorflow::NodeDef& nodeDef, const tensorflow::GraphDef& graphDef); - ParsedTfOperationPtr ParsePad(const tensorflow::NodeDef& nodeDef, const tensorflow::GraphDef& graphDef); - ParsedTfOperationPtr ParseSub(const tensorflow::NodeDef& nodeDef, const tensorflow::GraphDef& graphDef); - ParsedTfOperationPtr ParseStack(const tensorflow::NodeDef& nodeDef, const tensorflow::GraphDef& graphDef); - ParsedTfOperationPtr ParseTranspose(const tensorflow::NodeDef& nodeDef, const tensorflow::GraphDef& graphDef); - ParsedTfOperationPtr AddActivationLayer(const tensorflow::NodeDef& nodeDef, armnn::ActivationDescriptor& desc); - ParsedTfOperationPtr AddAdditionLayer(const tensorflow::NodeDef& nodeDef, bool isBiasAdd = false); - ParsedTfOperationPtr AddRealDivLayer(const tensorflow::NodeDef& nodeDef); - ParsedTfOperationPtr AddMaximumLayer(const tensorflow::NodeDef& nodeDef); - - armnn::IConnectableLayer* AddMultiplicationLayer(const tensorflow::NodeDef& nodeDef); - - armnn::IConnectableLayer* AddFullyConnectedLayer(const tensorflow::NodeDef& matMulNodeDef, - const tensorflow::NodeDef* addNodeDef, const char* armnnLayerName); - - bool IsSupportedLeakyReluPattern(const tensorflow::NodeDef& mulNodeDef, - size_t alphaLayerIndex, - const OutputOfParsedTfOperation& otherOp, - armnn::IOutputSlot** outputOfLeakyRelu, - armnn::ActivationDescriptor & desc); - - std::pair<armnn::IOutputSlot*, armnn::IOutputSlot*> ProcessElementwiseInputSlots( - const tensorflow::NodeDef& nodeDef, const std::string& layerName); - - ParsedTfOperationPtr ProcessComparisonLayer( - armnn::IOutputSlot* input0Slot, - armnn::IOutputSlot* input1Slot, - armnn::IConnectableLayer* const layer, - const tensorflow::NodeDef& nodeDef); - - ParsedTfOperationPtr ProcessElementwiseLayer( - armnn::IOutputSlot* input0Slot, - armnn::IOutputSlot* input1Slot, - armnn::IConnectableLayer* const layer, - const tensorflow::NodeDef& nodeDef); - - armnn::IConnectableLayer* CreateAdditionLayer( - const tensorflow::NodeDef& nodeDef, - armnn::IOutputSlot* input0Slot, - armnn::IOutputSlot* input1Slot, - const std::string& layerName); - - armnn::IConnectableLayer* CreateAdditionLayer( - const tensorflow::NodeDef& nodeDef, - const OutputOfParsedTfOperation& opOne, - const OutputOfParsedTfOperation& opTwo, - unsigned int numberOfAddition); - - armnn::IConnectableLayer* CreateAdditionLayer( - const tensorflow::NodeDef& nodeDef, - armnn::IConnectableLayer* layerOne, - armnn::IConnectableLayer* layerTwo, - unsigned int numberOfAddition, - unsigned long numberOfLayersToConnect, - bool isOdd); - - armnn::IConnectableLayer* CreateAdditionLayer( - const tensorflow::NodeDef& nodeDef, - const OutputOfParsedTfOperation& op, - armnn::IConnectableLayer* layer); - - static std::pair<armnn::LayerBindingId, armnn::TensorInfo> GetBindingInfo(const std::string& layerName, - const char* bindingPointDesc, - const std::unordered_map<std::string, BindingPointInfo>& nameToBindingInfo); - - void TrackInputBinding(armnn::IConnectableLayer* layer, - armnn::LayerBindingId id, - const armnn::TensorInfo& tensorInfo); - - void TrackOutputBinding(armnn::IConnectableLayer* layer, - armnn::LayerBindingId id, - const armnn::TensorInfo& tensorInfo); - - static void TrackBindingPoint(armnn::IConnectableLayer* layer, armnn::LayerBindingId id, - const armnn::TensorInfo& tensorInfo, - const char* bindingPointDesc, - std::unordered_map<std::string, BindingPointInfo>& nameToBindingInfo); - - void Cleanup(); - - /// The network we're building. Gets cleared after it is passed to the user. - armnn::INetworkPtr m_Network; - - using OperationParsingFunction = ParsedTfOperationPtr(TfParserImpl::*)(const tensorflow::NodeDef& nodeDef, - const tensorflow::GraphDef& graphDef); - - /// Map of TensorFlow operation names to parsing member functions. - static const std::map<std::string, OperationParsingFunction> ms_OperationNameToParsingFunctions; - - static const std::list<std::string> m_ControlInputs; - - std::map<std::string, armnn::TensorShape> m_InputShapes; - std::vector<std::string> m_RequestedOutputs; - - /// Map of nodes extracted from the GraphDef to speed up parsing. - std::unordered_map<std::string, const tensorflow::NodeDef*> m_NodesByName; - - std::unordered_map<std::string, ParsedTfOperationPtr> m_ParsedTfOperations; - - /// Maps input layer names to their corresponding ids and tensor info. - std::unordered_map<std::string, BindingPointInfo> m_NetworkInputsBindingInfo; - - /// Maps output layer names to their corresponding ids and tensor info. - std::unordered_map<std::string, BindingPointInfo> m_NetworkOutputsBindingInfo; -}; - -} diff --git a/src/armnnTfParser/test/Activations.cpp b/src/armnnTfParser/test/Activations.cpp deleted file mode 100644 index d5ebb24787..0000000000 --- a/src/armnnTfParser/test/Activations.cpp +++ /dev/null @@ -1,109 +0,0 @@ -// -// Copyright © 2017 Arm Ltd. All rights reserved. -// SPDX-License-Identifier: MIT -// - -#include <boost/test/unit_test.hpp> -#include "armnnTfParser/ITfParser.hpp" -#include "ParserPrototxtFixture.hpp" - -BOOST_AUTO_TEST_SUITE(TensorflowParser) - -struct ActivationFixture : public armnnUtils::ParserPrototxtFixture<armnnTfParser::ITfParser> -{ - explicit ActivationFixture(const char* activationFunction) - { - m_Prototext = "node {\n" - " name: \"Placeholder\"\n" - " op: \"Placeholder\"\n" - " attr {\n" - " key: \"dtype\"\n" - " value {\n" - " type: DT_FLOAT\n" - " }\n" - " }\n" - " attr {\n" - " key: \"shape\"\n" - " value {\n" - " shape {\n" - " unknown_rank: true\n" - " }\n" - " }\n" - " }\n" - "}\n" - "node {\n" - " name: \""; - m_Prototext.append(activationFunction); - m_Prototext.append("\"\n" - " op: \""); - m_Prototext.append(activationFunction); - m_Prototext.append("\"\n" - " input: \"Placeholder\"\n" - " attr {\n" - " key: \"T\"\n" - " value {\n" - " type: DT_FLOAT\n" - " }\n" - " }\n" - "}\n"); - - SetupSingleInputSingleOutput({ 1, 7 }, "Placeholder", activationFunction); - } -}; - - -struct ReLuFixture : ActivationFixture -{ - ReLuFixture() : ActivationFixture("Relu") {} -}; -BOOST_FIXTURE_TEST_CASE(ParseReLu, ReLuFixture) -{ - RunTest<2>({ -1.0f, -0.5f, 1.25f, -3.0f, 0.0f, 0.5f, -0.75f }, - { 0.0f, 0.0f, 1.25f, 0.0f, 0.0f, 0.5f, 0.0f }); -} - - -struct ReLu6Fixture : ActivationFixture -{ - ReLu6Fixture() : ActivationFixture("Relu6") {} -}; -BOOST_FIXTURE_TEST_CASE(ParseReLu6, ReLu6Fixture) -{ - RunTest<2>({ -1.0f, -0.5f, 7.25f, -3.0f, 0.0f, 0.5f, -0.75f }, - { 0.0f, 0.0f, 6.0f, 0.0f, 0.0f, 0.5f, 0.0f }); -} - - -struct SigmoidFixture : ActivationFixture -{ - SigmoidFixture() : ActivationFixture("Sigmoid") {} -}; -BOOST_FIXTURE_TEST_CASE(ParseSigmoid, SigmoidFixture) -{ - RunTest<2>({ -0.1f, -0.2f, -0.3f, -0.4f, 0.1f, 0.2f, 0.3f }, - { 0.4750208f, 0.45016602f, 0.42555749f, 0.40131235f, 0.52497917f, 0.54983395f, 0.57444251f }); -} - - -struct SoftplusFixture : ActivationFixture -{ - SoftplusFixture() : ActivationFixture("Softplus") {} -}; -BOOST_FIXTURE_TEST_CASE(ParseSoftplus, SoftplusFixture) -{ - RunTest<2>({ -0.1f, -0.2f, -0.3f, -0.4f, 0.1f, 0.2f, 0.3f }, - { 0.64439666f, 0.59813893f, 0.55435526f, 0.51301527f, 0.74439669f, 0.7981388f, 0.85435522f }); -} - - -struct TanhFixture : ActivationFixture -{ - TanhFixture() : ActivationFixture("Tanh") {} -}; -BOOST_FIXTURE_TEST_CASE(ParseTanh, TanhFixture) -{ - RunTest<2>({ -0.1f, -0.2f, -0.3f, -0.4f, 0.1f, 0.2f, 0.3f }, - { -0.09966799f, -0.19737528f, -0.29131261f, -0.379949f, 0.09966799f, 0.19737528f, 0.29131261f }); -} - -BOOST_AUTO_TEST_SUITE_END() diff --git a/src/armnnTfParser/test/AddN.cpp b/src/armnnTfParser/test/AddN.cpp deleted file mode 100644 index 16b1124e24..0000000000 --- a/src/armnnTfParser/test/AddN.cpp +++ /dev/null @@ -1,180 +0,0 @@ -// -// Copyright © 2017 Arm Ltd. All rights reserved. -// SPDX-License-Identifier: MIT -// - -#include <armnn/utility/Assert.hpp> -#include <boost/test/unit_test.hpp> - -#include "armnnTfParser/ITfParser.hpp" -#include "ParserPrototxtFixture.hpp" - -#include <map> -#include <string> - - -BOOST_AUTO_TEST_SUITE(TensorflowParser) - -struct AddNFixture : public armnnUtils::ParserPrototxtFixture<armnnTfParser::ITfParser> -{ - AddNFixture(const std::vector<armnn::TensorShape> inputShapes, unsigned int numberOfInputs) - { - ARMNN_ASSERT(inputShapes.size() == numberOfInputs); - m_Prototext = ""; - for (unsigned int i = 0; i < numberOfInputs; i++) - { - m_Prototext.append("node { \n"); - m_Prototext.append(" name: \"input").append(std::to_string(i)).append("\"\n"); - m_Prototext += R"( op: "Placeholder" - attr { - key: "dtype" - value { - type: DT_FLOAT - } - } - attr { - key: "shape" - value { - shape { - } - } - } -} -)"; - } - m_Prototext += R"(node { - name: "output" - op: "AddN" -)"; - for (unsigned int i = 0; i < numberOfInputs; i++) - { - m_Prototext.append(" input: \"input").append(std::to_string(i)).append("\"\n"); - } - m_Prototext += R"( attr { - key: "N" - value { -)"; - m_Prototext.append(" i: ").append(std::to_string(numberOfInputs)).append("\n"); - m_Prototext += R"( } - } - attr { - key: "T" - value { - type: DT_FLOAT - } - } -})"; - - std::map<std::string, armnn::TensorShape> inputs; - for (unsigned int i = 0; i < numberOfInputs; i++) - { - std::string name("input"); - name.append(std::to_string(i)); - inputs.emplace(std::make_pair(name, inputShapes[i])); - } - Setup(inputs, {"output"}); - } - -}; - -// try with 2, 3, 5 and 8 inputs -struct FiveTwoDimInputsFixture : AddNFixture -{ - FiveTwoDimInputsFixture() : AddNFixture({ { 2, 2 }, { 2, 2 }, { 2, 2 }, { 2, 2 }, { 2, 2 } }, 5) {} -}; - - -BOOST_FIXTURE_TEST_CASE(FiveTwoDimInputs, FiveTwoDimInputsFixture) -{ - RunTest<2>({ { "input0", { 1.0, 2.0, 3.0, 4.0 } }, - { "input1", { 1.0, 5.0, 2.0, 2.0 } }, - { "input2", { 1.0, 1.0, 2.0, 2.0 } }, - { "input3", { 3.0, 7.0, 1.0, 2.0 } }, - { "input4", { 8.0, 0.0, -2.0, -3.0 } } }, - { { "output", { 14.0, 15.0, 6.0, 7.0 } } }); -} - -struct TwoTwoDimInputsFixture : AddNFixture -{ - TwoTwoDimInputsFixture() : AddNFixture({ { 2, 2 }, { 2, 2 } }, 2) {} -}; - -BOOST_FIXTURE_TEST_CASE(TwoTwoDimInputs, TwoTwoDimInputsFixture) -{ - RunTest<2>({ { "input0", { 1.0, 2.0, 3.0, 4.0 } }, - { "input1", { 1.0, 5.0, 2.0, 2.0 } } }, - { { "output", { 2.0, 7.0, 5.0, 6.0 } } }); -} - -struct ThreeTwoDimInputsFixture : AddNFixture -{ - ThreeTwoDimInputsFixture() : AddNFixture({ { 2, 2 }, { 2, 2 }, { 2, 2 } }, 3) {} -}; - -BOOST_FIXTURE_TEST_CASE(ThreeTwoDimInputs, ThreeTwoDimInputsFixture) -{ - RunTest<2>({ { "input0", { 1.0, 2.0, 3.0, 4.0 } }, - { "input1", { 1.0, 5.0, 2.0, 2.0 } }, - { "input2", { 1.0, 1.0, 2.0, 2.0 } } }, - { { "output", { 3.0, 8.0, 7.0, 8.0 } } }); -} - -struct EightTwoDimInputsFixture : AddNFixture -{ - EightTwoDimInputsFixture() : AddNFixture({ { 2, 2 }, { 2, 2 }, { 2, 2 }, { 2, 2 }, - { 2, 2 }, { 2, 2 }, { 2, 2 }, { 2, 2 } }, 8) {} -}; - -BOOST_FIXTURE_TEST_CASE(EightTwoDimInputs, EightTwoDimInputsFixture) -{ - RunTest<2>({ { "input0", { 1.0, 2.0, 3.0, 4.0 } }, - { "input1", { 1.0, 5.0, 2.0, 2.0 } }, - { "input2", { 1.0, 1.0, 2.0, 2.0 } }, - { "input3", { 3.0, 7.0, 1.0, 2.0 } }, - { "input4", { 8.0, 0.0, -2.0, -3.0 } }, - { "input5", {-3.0, 2.0, -1.0, -5.0 } }, - { "input6", { 1.0, 6.0, 2.0, 2.0 } }, - { "input7", {-19.0, 7.0, 1.0, -10.0 } } }, - { { "output", {-7.0, 30.0, 8.0, -6.0 } } }); -} - -struct ThreeInputBroadcast1D4D4DInputsFixture : AddNFixture -{ - ThreeInputBroadcast1D4D4DInputsFixture() : AddNFixture({ { 1 }, { 1, 1, 2, 2 }, { 1, 1, 2, 2 } }, 3) {} -}; - -BOOST_FIXTURE_TEST_CASE(ThreeInputBroadcast1D4D4DInputs, ThreeInputBroadcast1D4D4DInputsFixture) -{ - RunTest<4>({ { "input0", { 1.0 } }, - { "input1", { 1.0, 5.0, 2.0, 2.0 } }, - { "input2", { 1.0, 1.0, 2.0, 2.0 } } }, - { { "output", { 3.0, 7.0, 5.0, 5.0 } } }); -} - -struct ThreeInputBroadcast4D1D4DInputsFixture : AddNFixture -{ - ThreeInputBroadcast4D1D4DInputsFixture() : AddNFixture({ { 1, 1, 2, 2 }, { 1 }, { 1, 1, 2, 2 } }, 3) {} -}; - -BOOST_FIXTURE_TEST_CASE(ThreeInputBroadcast4D1D4DInputs, ThreeInputBroadcast4D1D4DInputsFixture) -{ - RunTest<4>({ { "input0", { 1.0, 3.0, 9.0, 4.0 } }, - { "input1", {-2.0 } }, - { "input2", { 1.0, 1.0, 2.0, 2.0 } } }, - { { "output", { 0.0, 2.0, 9.0, 4.0 } } }); -} - -struct ThreeInputBroadcast4D4D1DInputsFixture : AddNFixture -{ - ThreeInputBroadcast4D4D1DInputsFixture() : AddNFixture({ { 1, 1, 2, 2 }, { 1, 1, 2, 2 }, { 1 } }, 3) {} -}; - -BOOST_FIXTURE_TEST_CASE(ThreeInputBroadcast4D4D1DInputs, ThreeInputBroadcast4D4D1DInputsFixture) -{ - RunTest<4>({ { "input0", { 1.0, 5.0, 2.0, 2.0 } }, - { "input1", { 1.0, 1.0, 2.0, 2.0 } }, - { "input2", { 1.0 } } }, - { { "output", { 3.0, 7.0, 5.0, 5.0 } } }); -} - -BOOST_AUTO_TEST_SUITE_END() diff --git a/src/armnnTfParser/test/Addition.cpp b/src/armnnTfParser/test/Addition.cpp deleted file mode 100644 index f5c51dc602..0000000000 --- a/src/armnnTfParser/test/Addition.cpp +++ /dev/null @@ -1,78 +0,0 @@ -// -// Copyright © 2017 Arm Ltd. All rights reserved. -// SPDX-License-Identifier: MIT -// - -#include <boost/test/unit_test.hpp> -#include "armnnTfParser/ITfParser.hpp" -#include "ParserPrototxtFixture.hpp" - -BOOST_AUTO_TEST_SUITE(TensorflowParser) - -struct AdditionFixture : public armnnUtils::ParserPrototxtFixture<armnnTfParser::ITfParser> -{ - AdditionFixture() - { - m_Prototext = "node { \n" - " name: \"graphInput\" \n" - " op: \"Placeholder\" \n" - " attr { \n" - " key: \"dtype\" \n" - " value { \n" - " type: DT_FLOAT \n" - " } \n" - " } \n" - " attr { \n" - " key: \"shape\" \n" - " value { \n" - " shape { \n" - " } \n" - " } \n" - " } \n" - " } \n" - " node { \n" - " name: \"softmax1\" \n" - " op: \"Softmax\" \n" - " input: \"graphInput\" \n" - " attr { \n" - " key: \"T\" \n" - " value { \n" - " type: DT_FLOAT \n" - " } \n" - " } \n" - " }\n" - " node {\n" - " name: \"softmax2\"\n" - " op : \"Softmax\"\n" - " input: \"graphInput\"\n" - " attr { \n" - " key: \"T\" \n" - " value { \n" - " type: DT_FLOAT \n" - " } \n" - " } \n" - " }\n" - " node {\n" - " name: \"addition\"\n" - " op : \"Add\"\n" - " input: \"softmax1\"\n" - " input: \"softmax2\"\n" - " attr { \n" - " key: \"T\" \n" - " value { \n" - " type: DT_FLOAT \n" - " } \n" - " } \n" - " }\n"; - - SetupSingleInputSingleOutput({ 1, 7 }, "graphInput", "addition"); - } -}; - -BOOST_FIXTURE_TEST_CASE(ParseAddition, AdditionFixture) -{ - RunTest<2>({ 0, 0, 10000, 0, 0, 0, 0 }, { 0, 0, 2, 0, 0, 0, 0 }); -} - - -BOOST_AUTO_TEST_SUITE_END() diff --git a/src/armnnTfParser/test/Assert.cpp b/src/armnnTfParser/test/Assert.cpp deleted file mode 100644 index 0665be7c7e..0000000000 --- a/src/armnnTfParser/test/Assert.cpp +++ /dev/null @@ -1,299 +0,0 @@ -// -// Copyright © 2017 Arm Ltd. All rights reserved. -// SPDX-License-Identifier: MIT -// - -#include "armnnTfParser/ITfParser.hpp" -#include "ParserPrototxtFixture.hpp" -#include "test/GraphUtils.hpp" - -#include <armnn/utility/PolymorphicDowncast.hpp> - -#include <boost/test/unit_test.hpp> - -BOOST_AUTO_TEST_SUITE(TensorflowParser) - -struct AssertSimpleFixture : public armnnUtils::ParserPrototxtFixture<armnnTfParser::ITfParser> -{ - AssertSimpleFixture() - { - // Placeholder AssertInput - // | \ / - // Add ------ Assert - - m_Prototext = R"( - node { - name: "Placeholder" - op: "Placeholder" - attr { - key: "dtype" - value { - type: DT_FLOAT - } - } - attr { - key: "shape" - value { - shape { - unknown_rank: true - } - } - } - } - node { - name: "AssertInput" - op: "Const" - attr { - key: "dtype" - value { - type: DT_FLOAT - } - } - attr { - key: "value" - value { - tensor { - dtype: DT_FLOAT - tensor_shape { - dim { - size: 1 - } - } - float_val: 17.0 - } - } - } - } - node { - name: "Assert" - op: "Assert" - input: "Placeholder" - input: "AssertInput" - attr { - key: "T" - value { - type: DT_FLOAT - } - } - } - node { - name: "Add" - op: "Add" - input: "Placeholder" - input: "Placeholder" - input: "^Assert" - attr { - key: "T" - value { - type: DT_FLOAT - } - } - })"; - } -}; - -BOOST_FIXTURE_TEST_CASE(AssertSimpleTest, AssertSimpleFixture) -{ - SetupSingleInputSingleOutput({ 1, 1, 1, 4 }, "Placeholder", "Add"); - RunTest<4>({ 1.0f, 2.0f, 3.0f, 4.0f }, { 2.0f, 4.0f, 6.0f, 8.0f }); -} - -BOOST_FIXTURE_TEST_CASE(AssertSimpleGraphStructureTest, AssertSimpleFixture) -{ - auto optimized = SetupOptimizedNetwork({ { "Placeholder", { 1, 1, 1, 4 } } }, { "Add" }); - - armnn::Graph& graph = GetGraphForTesting(optimized.get()); - - BOOST_TEST((graph.GetNumInputs() == 1)); - BOOST_TEST((graph.GetNumOutputs() == 1)); - BOOST_TEST((graph.GetNumLayers() == 3)); - - armnn::Layer* inputLayer = GetFirstLayerWithName(graph, "Placeholder"); - BOOST_TEST((inputLayer->GetType() == armnn::LayerType::Input)); - BOOST_TEST(CheckNumberOfInputSlot(inputLayer, 0)); - BOOST_TEST(CheckNumberOfOutputSlot(inputLayer, 1)); - - armnn::Layer* addLayer = GetFirstLayerWithName(graph, "Add"); - BOOST_TEST((addLayer->GetType() == armnn::LayerType::Addition)); - BOOST_TEST(CheckNumberOfInputSlot(addLayer, 2)); - BOOST_TEST(CheckNumberOfOutputSlot(addLayer, 1)); - - armnn::TensorInfo tensorInfo(armnn::TensorShape({1, 1, 1, 4}), armnn::DataType::Float32); - BOOST_TEST(IsConnected(inputLayer, addLayer, 0, 0, tensorInfo)); - BOOST_TEST(IsConnected(inputLayer, addLayer, 0, 1, tensorInfo)); - - for (auto&& outputLayer : graph.GetOutputLayers()) - { - BOOST_TEST(IsConnected(addLayer, const_cast<armnn::OutputLayer*>(outputLayer), 0, 0, tensorInfo)); - } -} - -struct AssertFixture : public armnnUtils::ParserPrototxtFixture<armnnTfParser::ITfParser> -{ - AssertFixture() - { - // Input0 Input1 Input2 - // | \ / | - // | Sub ------ Assert - // \ / / - // Output ------- - - m_Prototext = R"( - node { - name: "Input0" - op: "Placeholder" - attr { - key: "dtype" - value { - type: DT_FLOAT - } - } - attr { - key: "shape" - value { - shape { - unknown_rank: true - } - } - } - } - node { - name: "Input1" - op: "Placeholder" - attr { - key: "dtype" - value { - type: DT_FLOAT - } - } - attr { - key: "shape" - value { - shape { - unknown_rank: true - } - } - } - } - node { - name: "Sub" - op: "Sub" - input: "Input0" - input: "Input1" - attr { - key: "T" - value { - type: DT_FLOAT - } - } - } - node { - name: "Input2" - op: "Placeholder" - attr { - key: "dtype" - value { - type: DT_FLOAT - } - } - attr { - key: "shape" - value { - shape { - unknown_rank: true - } - } - } - } - node { - name: "Assert" - op: "Assert" - input: "Input2" - input: "Sub" - attr { - key: "T" - value { - type: DT_FLOAT - } - } - } - node { - name: "Output" - op: "Add" - input: "Input0" - input: "Sub" - input: "^Assert" - attr { - key: "T" - value { - type: DT_FLOAT - } - } - })"; - - - } -}; - -BOOST_FIXTURE_TEST_CASE(AssertTest, AssertFixture) -{ - Setup({ { "Input0", { 1, 1, 2, 2 } }, - { "Input1", { 1, 1, 2, 2 } } }, - { "Output" }); - - RunTest<4>({ { "Input0", { 4.0f, 3.0f, - 2.0f, 1.0f } }, - - { "Input1", { 1.0f, 2.0f, - 3.0f, 4.0f } } }, - - { { "Output", { 7.0f, 4.0f, - 1.0f, -2.0f } } }); -} - -BOOST_FIXTURE_TEST_CASE(AssertGraphStructureTest, AssertFixture) -{ - auto optimized = SetupOptimizedNetwork({ { "Input0", { 1, 1, 2, 2 } }, - { "Input1", { 1, 1, 2, 2 } } }, - { "Output" }); - - armnn::Graph& graph = GetGraphForTesting(optimized.get()); - - BOOST_TEST((graph.GetNumInputs() == 2)); - BOOST_TEST((graph.GetNumOutputs() == 1)); - BOOST_TEST((graph.GetNumLayers() == 5)); - - armnn::Layer* inputLayer0 = GetFirstLayerWithName(graph, "Input0"); - BOOST_TEST((inputLayer0->GetType() == armnn::LayerType::Input)); - BOOST_TEST(CheckNumberOfInputSlot(inputLayer0, 0)); - BOOST_TEST(CheckNumberOfOutputSlot(inputLayer0, 1)); - - armnn::Layer* inputLayer1 = GetFirstLayerWithName(graph, "Input1"); - BOOST_TEST((inputLayer1->GetType() == armnn::LayerType::Input)); - BOOST_TEST(CheckNumberOfInputSlot(inputLayer1, 0)); - BOOST_TEST(CheckNumberOfOutputSlot(inputLayer1, 1)); - - armnn::Layer* subLayer = GetFirstLayerWithName(graph, "Sub"); - BOOST_TEST((subLayer->GetType() == armnn::LayerType::Subtraction)); - BOOST_TEST(CheckNumberOfInputSlot(subLayer, 2)); - BOOST_TEST(CheckNumberOfOutputSlot(subLayer, 1)); - - armnn::Layer* addLayer = GetFirstLayerWithName(graph, "Output"); - BOOST_TEST((addLayer->GetType() == armnn::LayerType::Addition)); - BOOST_TEST(CheckNumberOfInputSlot(addLayer, 2)); - BOOST_TEST(CheckNumberOfOutputSlot(addLayer, 1)); - - armnn::TensorInfo tensorInfo(armnn::TensorShape({1, 1, 2, 2}), armnn::DataType::Float32); - BOOST_TEST(IsConnected(inputLayer0, subLayer, 0, 0, tensorInfo)); - BOOST_TEST(IsConnected(inputLayer1, subLayer, 0, 1, tensorInfo)); - BOOST_TEST(IsConnected(inputLayer0, addLayer, 0, 0, tensorInfo)); - BOOST_TEST(IsConnected(subLayer, addLayer, 0, 1, tensorInfo)); - - for (auto&& outputLayer : graph.GetOutputLayers()) - { - BOOST_TEST(IsConnected(addLayer, const_cast<armnn::OutputLayer*>(outputLayer), 0, 0, tensorInfo)); - } -} - - -BOOST_AUTO_TEST_SUITE_END() diff --git a/src/armnnTfParser/test/BiasAdd.cpp b/src/armnnTfParser/test/BiasAdd.cpp deleted file mode 100644 index 81dcad4cda..0000000000 --- a/src/armnnTfParser/test/BiasAdd.cpp +++ /dev/null @@ -1,104 +0,0 @@ -// -// Copyright © 2017 Arm Ltd. All rights reserved. -// SPDX-License-Identifier: MIT -// - -#include <boost/test/unit_test.hpp> -#include "armnnTfParser/ITfParser.hpp" -#include "ParserPrototxtFixture.hpp" - -BOOST_AUTO_TEST_SUITE(TensorflowParser) - -struct BiasAddFixture : public armnnUtils::ParserPrototxtFixture<armnnTfParser::ITfParser> -{ - explicit BiasAddFixture(const std::string& dataFormat) - { - m_Prototext = R"( -node { - name: "graphInput" - op: "Placeholder" - attr { - key: "dtype" - value { - type: DT_FLOAT - } - } - attr { - key: "shape" - value { - shape { - } - } - } -} -node { - name: "bias" - op: "Const" - attr { - key: "dtype" - value { - type: DT_FLOAT - } - } - attr { - key: "value" - value { - tensor { - dtype: DT_FLOAT - tensor_shape { - dim { - size: 3 - } - } - float_val: 1 - float_val: 2 - float_val: 3 - } - } - } -} -node { - name: "biasAdd" - op : "BiasAdd" - input: "graphInput" - input: "bias" - attr { - key: "T" - value { - type: DT_FLOAT - } - } - attr { - key: "data_format" - value { - s: ")" + dataFormat + R"(" - } - } -} -)"; - - SetupSingleInputSingleOutput({ 1, 3, 1, 3 }, "graphInput", "biasAdd"); - } -}; - -struct BiasAddFixtureNCHW : BiasAddFixture -{ - BiasAddFixtureNCHW() : BiasAddFixture("NCHW") {} -}; - -struct BiasAddFixtureNHWC : BiasAddFixture -{ - BiasAddFixtureNHWC() : BiasAddFixture("NHWC") {} -}; - -BOOST_FIXTURE_TEST_CASE(ParseBiasAddNCHW, BiasAddFixtureNCHW) -{ - RunTest<4>(std::vector<float>(9), { 1, 1, 1, 2, 2, 2, 3, 3, 3 }); -} - -BOOST_FIXTURE_TEST_CASE(ParseBiasAddNHWC, BiasAddFixtureNHWC) -{ - RunTest<4>(std::vector<float>(9), { 1, 2, 3, 1, 2, 3, 1, 2, 3 }); -} - -BOOST_AUTO_TEST_SUITE_END() diff --git a/src/armnnTfParser/test/BroadcastForAdd.cpp b/src/armnnTfParser/test/BroadcastForAdd.cpp deleted file mode 100644 index 36cba9df4e..0000000000 --- a/src/armnnTfParser/test/BroadcastForAdd.cpp +++ /dev/null @@ -1,149 +0,0 @@ -// -// Copyright © 2017 Arm Ltd. All rights reserved. -// SPDX-License-Identifier: MIT -// - -#include <boost/test/unit_test.hpp> -#include "armnnTfParser/ITfParser.hpp" -#include "ParserPrototxtFixture.hpp" -// This is a special case for add, which supports broadcasting. -BOOST_AUTO_TEST_SUITE(TensorflowParser) - -struct BroadcastForAddFixtureSlot1 : public armnnUtils::ParserPrototxtFixture<armnnTfParser::ITfParser> -{ - BroadcastForAddFixtureSlot1() - { - m_Prototext = R"( - node { - name: "graphInput" - op: "Placeholder" - attr { - key: "dtype" - value { - type: DT_FLOAT - } - } - attr { - key: "shape" - value { - shape { - } - } - } - } - node { - name: "Const_1" - op: "Const" - attr { - key: "dtype" - value { - type: DT_FLOAT - } - } - attr { - key: "value" - value { - tensor { - dtype: DT_FLOAT - tensor_shape { - } - float_val: 4.0 - float_val: 5.0 - } - } - } - } - node { - name: "Add" - op: "Add" - input: "graphInput" - input: "Const_1" - attr { - key: "T" - value { - type: DT_FLOAT - } - } - } - )"; - - SetupSingleInputSingleOutput({ 1, 2, 2, 2 }, "graphInput", "Add"); - } -}; - -struct BroadcastForAddFixtureSlot0 : public armnnUtils::ParserPrototxtFixture<armnnTfParser::ITfParser> -{ - BroadcastForAddFixtureSlot0() - { - m_Prototext = R"( - node { - name: "graphInput" - op: "Placeholder" - attr { - key: "dtype" - value { - type: DT_FLOAT - } - } - attr { - key: "shape" - value { - shape { - } - } - } - } - node { - name: "Const_1" - op: "Const" - attr { - key: "dtype" - value { - type: DT_FLOAT - } - } - attr { - key: "value" - value { - tensor { - dtype: DT_FLOAT - tensor_shape { - } - float_val: 4.0 - float_val: 5.0 - } - } - } - } - node { - name: "Add" - op: "Add" - input: "Const_1" - input: "graphInput" - attr { - key: "T" - value { - type: DT_FLOAT - } - } - } - )"; - - SetupSingleInputSingleOutput({ 1, 2, 2, 2 }, "graphInput", "Add"); - } -}; - - -BOOST_FIXTURE_TEST_CASE(ParseBroadcastForAddition1, BroadcastForAddFixtureSlot1) -{ - RunTest<4>({ 1.0, 1.0, 2.0, 2.0, 3.0, 3.0, 4.0, 4.0 }, { 5.0, 6.0, 6.0, 7.0, 7.0, 8.0, 8.0, 9.0 }); -} - -BOOST_FIXTURE_TEST_CASE(ParseBroadcastForAddition0, BroadcastForAddFixtureSlot0) -{ - RunTest<4>({ 1.0, 1.0, 2.0, 2.0, 3.0, 3.0, 4.0, 4.0 }, { 5.0, 6.0, 6.0, 7.0, 7.0, 8.0, 8.0, 9.0 }); -} - - - -BOOST_AUTO_TEST_SUITE_END() diff --git a/src/armnnTfParser/test/Concat.cpp b/src/armnnTfParser/test/Concat.cpp deleted file mode 100644 index 2d4a95ba0a..0000000000 --- a/src/armnnTfParser/test/Concat.cpp +++ /dev/null @@ -1,183 +0,0 @@ -// -// Copyright © 2017 Arm Ltd. All rights reserved. -// SPDX-License-Identifier: MIT -// - -#include <boost/test/unit_test.hpp> -#include "armnnTfParser/ITfParser.hpp" -#include "ParserPrototxtFixture.hpp" - -BOOST_AUTO_TEST_SUITE(TensorflowParser) - -struct ConcatFixture : public armnnUtils::ParserPrototxtFixture<armnnTfParser::ITfParser> -{ - explicit ConcatFixture(const armnn::TensorShape& inputShape0, const armnn::TensorShape& inputShape1, - unsigned int concatDim) - { - m_Prototext = R"( - node { - name: "graphInput0" - op: "Placeholder" - attr { - key: "dtype" - value { - type: DT_FLOAT - } - } - attr { - key: "shape" - value { - shape { - } - } - } - } - node { - name: "graphInput1" - op: "Placeholder" - attr { - key: "dtype" - value { - type: DT_FLOAT - } - } - attr { - key: "shape" - value { - shape { - } - } - } - } - node { - name: "concat/axis" - op: "Const" - attr { - key: "dtype" - value { - type: DT_INT32 - } - } - attr { - key: "value" - value { - tensor { - dtype: DT_INT32 - tensor_shape { - } - int_val: )"; - - m_Prototext += std::to_string(concatDim); - - m_Prototext += R"( - } - } - } - } - node { - name: "concat" - op: "ConcatV2" - input: "graphInput0" - input: "graphInput1" - input: "concat/axis" - attr { - key: "N" - value { - i: 2 - } - } - attr { - key: "T" - value { - type: DT_FLOAT - } - } - attr { - key: "Tidx" - value { - type: DT_FLOAT - } - } - } - )"; - - Setup({{"graphInput0", inputShape0 }, - {"graphInput1", inputShape1 }}, {"concat"}); - } -}; - -struct ConcatFixtureNCHW : ConcatFixture -{ - ConcatFixtureNCHW() : ConcatFixture({ 1, 1, 2, 2 }, { 1, 1, 2, 2 }, 1 ) {} -}; - -struct ConcatFixtureNHWC : ConcatFixture -{ - ConcatFixtureNHWC() : ConcatFixture({ 1, 1, 2, 2 }, { 1, 1, 2, 2 }, 3 ) {} -}; - -BOOST_FIXTURE_TEST_CASE(ParseConcatNCHW, ConcatFixtureNCHW) -{ - RunTest<4>({{"graphInput0", {0.0, 1.0, 2.0, 3.0}}, - {"graphInput1", {4.0, 5.0, 6.0, 7.0}}}, - {{"concat", { 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0 }}}); -} - -BOOST_FIXTURE_TEST_CASE(ParseConcatNHWC, ConcatFixtureNHWC) -{ - RunTest<4>({{"graphInput0", {0.0, 1.0, 2.0, 3.0}}, - {"graphInput1", {4.0, 5.0, 6.0, 7.0}}}, - {{"concat", { 0.0, 1.0, 4.0, 5.0, 2.0, 3.0, 6.0, 7.0 }}}); -} - -struct ConcatFixtureDim1 : ConcatFixture -{ - ConcatFixtureDim1() : ConcatFixture({ 1, 2, 3, 4 }, { 1, 2, 3, 4 }, 1) {} -}; - -struct ConcatFixtureDim3 : ConcatFixture -{ - ConcatFixtureDim3() : ConcatFixture({ 1, 2, 3, 4 }, { 1, 2, 3, 4 }, 3) {} -}; - -BOOST_FIXTURE_TEST_CASE(ParseConcatDim1, ConcatFixtureDim1) -{ - RunTest<4>({ { "graphInput0", { 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, - 12.0, 13.0, 14.0, 15.0, 16.0, 17.0, 18.0, 19.0, 20.0, 21.0, 22.0, 23.0 } }, - { "graphInput1", { 50.0, 51.0, 52.0, 53.0, 54.0, 55.0, 56.0, 57.0, 58.0, 59.0, 60.0, 61.0, - 62.0, 63.0, 64.0, 65.0, 66.0, 67.0, 68.0, 69.0, 70.0, 71.0, 72.0, 73.0 } } }, - { { "concat", { 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, - 12.0, 13.0, 14.0, 15.0, 16.0, 17.0, 18.0, 19.0, 20.0, 21.0, 22.0, 23.0, - 50.0, 51.0, 52.0, 53.0, 54.0, 55.0, 56.0, 57.0, 58.0, 59.0, 60.0, 61.0, - 62.0, 63.0, 64.0, 65.0, 66.0, 67.0, 68.0, 69.0, 70.0, 71.0, 72.0, 73.0 } } }); -} - -BOOST_FIXTURE_TEST_CASE(ParseConcatDim3, ConcatFixtureDim3) -{ - RunTest<4>({ { "graphInput0", { 0.0, 1.0, 2.0, 3.0, - 4.0, 5.0, 6.0, 7.0, - 8.0, 9.0, 10.0, 11.0, - 12.0, 13.0, 14.0, 15.0, - 16.0, 17.0, 18.0, 19.0, - 20.0, 21.0, 22.0, 23.0 } }, - { "graphInput1", { 50.0, 51.0, 52.0, 53.0, - 54.0, 55.0, 56.0, 57.0, - 58.0, 59.0, 60.0, 61.0, - 62.0, 63.0, 64.0, 65.0, - 66.0, 67.0, 68.0, 69.0, - 70.0, 71.0, 72.0, 73.0 } } }, - { { "concat", { 0.0, 1.0, 2.0, 3.0, - 50.0, 51.0, 52.0, 53.0, - 4.0, 5.0, 6.0, 7.0, - 54.0, 55.0, 56.0, 57.0, - 8.0, 9.0, 10.0, 11.0, - 58.0, 59.0, 60.0, 61.0, - 12.0, 13.0, 14.0, 15.0, - 62.0, 63.0, 64.0, 65.0, - 16.0, 17.0, 18.0, 19.0, - 66.0, 67.0, 68.0, 69.0, - 20.0, 21.0, 22.0, 23.0, - 70.0, 71.0, 72.0, 73.0 } } }); -} - -BOOST_AUTO_TEST_SUITE_END()
\ No newline at end of file diff --git a/src/armnnTfParser/test/ConcatOfConcats.cpp b/src/armnnTfParser/test/ConcatOfConcats.cpp deleted file mode 100644 index b038698c01..0000000000 --- a/src/armnnTfParser/test/ConcatOfConcats.cpp +++ /dev/null @@ -1,316 +0,0 @@ -// -// Copyright © 2017 Arm Ltd. All rights reserved. -// SPDX-License-Identifier: MIT -// - -#include <boost/test/unit_test.hpp> -#include "armnnTfParser/ITfParser.hpp" -#include "ParserPrototxtFixture.hpp" - -BOOST_AUTO_TEST_SUITE(TensorflowParser) - -struct ConcatOfConcatsFixture : public armnnUtils::ParserPrototxtFixture<armnnTfParser::ITfParser> -{ - explicit ConcatOfConcatsFixture(const armnn::TensorShape& inputShape0, const armnn::TensorShape& inputShape1, - const armnn::TensorShape& inputShape2, const armnn::TensorShape& inputShape3, - unsigned int concatDim) - { - m_Prototext = R"( - node { - name: "graphInput0" - op: "Placeholder" - attr { - key: "dtype" - value { - type: DT_FLOAT - } - } - attr { - key: "shape" - value { - shape { - } - } - } - } - node { - name: "graphInput1" - op: "Placeholder" - attr { - key: "dtype" - value { - type: DT_FLOAT - } - } - attr { - key: "shape" - value { - shape { - } - } - } - } - node { - name: "graphInput2" - op: "Placeholder" - attr { - key: "dtype" - value { - type: DT_FLOAT - } - } - attr { - key: "shape" - value { - shape { - } - } - } - } - node { - name: "graphInput3" - op: "Placeholder" - attr { - key: "dtype" - value { - type: DT_FLOAT - } - } - attr { - key: "shape" - value { - shape { - } - } - } - } - node { - name: "Relu" - op: "Relu" - input: "graphInput0" - attr { - key: "T" - value { - type: DT_FLOAT - } - } - } - node { - name: "Relu_1" - op: "Relu" - input: "graphInput1" - attr { - key: "T" - value { - type: DT_FLOAT - } - } - } - node { - name: "Relu_2" - op: "Relu" - input: "graphInput2" - attr { - key: "T" - value { - type: DT_FLOAT - } - } - } - node { - name: "Relu_3" - op: "Relu" - input: "graphInput3" - attr { - key: "T" - value { - type: DT_FLOAT - } - } - } - node { - name: "concat/axis" - op: "Const" - attr { - key: "dtype" - value { - type: DT_INT32 - } - } - attr { - key: "value" - value { - tensor { - dtype: DT_INT32 - tensor_shape { - } - int_val: )"; - m_Prototext += std::to_string(concatDim); - m_Prototext += R"( - } - } - } - } - node { - name: "concat" - op: "ConcatV2" - input: "Relu" - input: "Relu_1" - input: "concat/axis" - attr { - key: "N" - value { - i: 2 - } - } - attr { - key: "T" - value { - type: DT_FLOAT - } - } - attr { - key: "Tidx" - value { - type: DT_INT32 - } - } - } - node { - name: "concat_1/axis" - op: "Const" - attr { - key: "dtype" - value { - type: DT_INT32 - } - } - attr { - key: "value" - value { - tensor { - dtype: DT_INT32 - tensor_shape { - } - int_val: )"; - m_Prototext += std::to_string(concatDim); - m_Prototext += R"( - } - } - } - } - node { - name: "concat_1" - op: "ConcatV2" - input: "Relu_2" - input: "Relu_3" - input: "concat_1/axis" - attr { - key: "N" - value { - i: 2 - } - } - attr { - key: "T" - value { - type: DT_FLOAT - } - } - attr { - key: "Tidx" - value { - type: DT_INT32 - } - } - } - node { - name: "concat_2/axis" - op: "Const" - attr { - key: "dtype" - value { - type: DT_INT32 - } - } - attr { - key: "value" - value { - tensor { - dtype: DT_INT32 - tensor_shape { - } - int_val: )"; - m_Prototext += std::to_string(concatDim); - m_Prototext += R"( - } - } - } - } - node { - name: "concat_2" - op: "ConcatV2" - input: "concat" - input: "concat_1" - input: "concat_2/axis" - attr { - key: "N" - value { - i: 2 - } - } - attr { - key: "T" - value { - type: DT_FLOAT - } - } - attr { - key: "Tidx" - value { - type: DT_INT32 - } - } - } - )"; - - Setup({{ "graphInput0", inputShape0 }, - { "graphInput1", inputShape1 }, - { "graphInput2", inputShape2 }, - { "graphInput3", inputShape3}}, {"concat_2"}); - } -}; - -struct ConcatOfConcatsFixtureNCHW : ConcatOfConcatsFixture -{ - ConcatOfConcatsFixtureNCHW() : ConcatOfConcatsFixture({ 1, 1, 2, 2 }, { 1, 1, 2, 2 }, { 1, 1, 2, 2 }, - { 1, 1, 2, 2 }, 1 ) {} -}; - -struct ConcatOfConcatsFixtureNHWC : ConcatOfConcatsFixture -{ - ConcatOfConcatsFixtureNHWC() : ConcatOfConcatsFixture({ 1, 1, 2, 2 }, { 1, 1, 2, 2 }, { 1, 1, 2, 2 }, - { 1, 1, 2, 2 }, 3 ) {} -}; - -BOOST_FIXTURE_TEST_CASE(ParseConcatOfConcatsNCHW, ConcatOfConcatsFixtureNCHW) -{ - RunTest<4>({{"graphInput0", {0.0, 1.0, 2.0, 3.0}}, - {"graphInput1", {4.0, 5.0, 6.0, 7.0}}, - {"graphInput2", {8.0, 9.0, 10.0, 11.0}}, - {"graphInput3", {12.0, 13.0, 14.0, 15.0}}}, - {{"concat_2", { 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, - 8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0 }}}); -} - -BOOST_FIXTURE_TEST_CASE(ParseConcatOfConcatsNHWC, ConcatOfConcatsFixtureNHWC) -{ - RunTest<4>({{"graphInput0", {0.0, 1.0, 2.0, 3.0}}, - {"graphInput1", {4.0, 5.0, 6.0, 7.0}}, - {"graphInput2", {8.0, 9.0, 10.0, 11.0}}, - {"graphInput3", {12.0, 13.0, 14.0, 15.0}}}, - {{"concat_2", { 0.0, 1.0, 4.0, 5.0, 8.0, 9.0, 12.0, 13.0, - 2.0, 3.0, 6.0, 7.0, 10.0, 11.0, 14.0, 15.0 }}}); -} - -BOOST_AUTO_TEST_SUITE_END() diff --git a/src/armnnTfParser/test/Constant.cpp b/src/armnnTfParser/test/Constant.cpp deleted file mode 100644 index 5c06d8c876..0000000000 --- a/src/armnnTfParser/test/Constant.cpp +++ /dev/null @@ -1,321 +0,0 @@ -// -// Copyright © 2017 Arm Ltd. All rights reserved. -// SPDX-License-Identifier: MIT -// - -#include <boost/test/unit_test.hpp> - -#include "armnnTfParser/ITfParser.hpp" - -#include "ParserPrototxtFixture.hpp" - -BOOST_AUTO_TEST_SUITE(TensorflowParser) - -// Tests that a Const node in Tensorflow can be converted to a ConstLayer in armnn (as opposed to most -// Const nodes which are used as weight inputs for convolutions etc. and are therefore not converted to -// armnn ConstLayers). -struct ConstantFixture : public armnnUtils::ParserPrototxtFixture<armnnTfParser::ITfParser> -{ - ConstantFixture() - { - // Input = tf.placeholder(tf.float32, name = "input") - // Const = tf.constant([17], tf.float32, [1]) - // Output = tf.add(input, const, name = "output") - m_Prototext = - R"( -node { - name: "input" - op: "Placeholder" - attr { - key: "dtype" - value { - type: DT_FLOAT - } - } - attr { - key: "shape" - value { - shape { - unknown_rank: true - } - } - } -} -node { - name: "Const" - op: "Const" - attr { - key: "dtype" - value { - type: DT_FLOAT - } - } - attr { - key: "value" - value { - tensor { - dtype: DT_FLOAT - tensor_shape { - dim { - size: 1 - } - } - float_val: 17.0 - } - } - } -} -node { - name: "output" - op: "Add" - input: "input" - input: "Const" - attr { - key: "T" - value { - type: DT_FLOAT - } - } -} - )"; - SetupSingleInputSingleOutput({ 1 }, "input", "output"); - } -}; - -BOOST_FIXTURE_TEST_CASE(Constant, ConstantFixture) -{ - RunTest<1>({1}, {18}); -} - - -// Tests that a single Const node in Tensorflow can be used twice by a dependant node. This should result in only -// a single armnn ConstLayer being created. -struct ConstantReusedFixture : public armnnUtils::ParserPrototxtFixture<armnnTfParser::ITfParser> -{ - ConstantReusedFixture() - { - // Const = tf.constant([17], tf.float32, [1]) - // Output = tf.add(const, const, name = "output") - m_Prototext = - R"( -node { - name: "Const" - op: "Const" - attr { - key: "dtype" - value { - type: DT_FLOAT - } - } - attr { - key: "value" - value { - tensor { - dtype: DT_FLOAT - tensor_shape { - dim { - size: 1 - } - } - float_val: 17.0 - } - } - } -} -node { - name: "output" - op: "Add" - input: "Const" - input: "Const" - attr { - key: "T" - value { - type: DT_FLOAT - } - } -} - )"; - Setup({}, { "output" }); - } -}; - -BOOST_FIXTURE_TEST_CASE(ConstantReused, ConstantReusedFixture) -{ - RunTest<1>({}, { { "output", { 34 } } }); -} - -template <int ListSize> -struct ConstantValueListFixture : public armnnUtils::ParserPrototxtFixture<armnnTfParser::ITfParser> -{ - ConstantValueListFixture() - { - m_Prototext = - R"( -node { - name: "output" - op: "Const" - attr { - key: "dtype" - value { - type: DT_FLOAT - } - } - attr { - key: "value" - value { - tensor { - dtype: DT_FLOAT - tensor_shape { - dim { - size: 2 - } - dim { - size: 3 - } - })"; - - double value = 0.75; - for (int i = 0; i < ListSize; i++, value += 0.25) - { - m_Prototext += std::string("float_val : ") + std::to_string(value) + "\n"; - } - - m_Prototext += - R"( - } - } - } -} - )"; - Setup({}, { "output" }); - } -}; - -using ConstantSingleValueListFixture = ConstantValueListFixture<1>; -using ConstantMultipleValueListFixture = ConstantValueListFixture<4>; -using ConstantMaxValueListFixture = ConstantValueListFixture<6>; - -BOOST_FIXTURE_TEST_CASE(ConstantSingleValueList, ConstantSingleValueListFixture) -{ - RunTest<2>({}, { { "output", { 0.75f, 0.75f, 0.75f, 0.75f, 0.75f, 0.75f } } }); -} -BOOST_FIXTURE_TEST_CASE(ConstantMultipleValueList, ConstantMultipleValueListFixture) -{ - RunTest<2>({}, { { "output", { 0.75f, 1.f, 1.25f, 1.5f, 1.5f, 1.5f } } }); -} -BOOST_FIXTURE_TEST_CASE(ConstantMaxValueList, ConstantMaxValueListFixture) -{ - RunTest<2>({}, { { "output", { 0.75f, 1.f, 1.25f, 1.50f, 1.75f, 2.f } } }); -} - -template <bool WithShape, bool WithContent, bool WithValueList> -struct ConstantCreateFixture : public armnnUtils::ParserPrototxtFixture<armnnTfParser::ITfParser> -{ - ConstantCreateFixture() - { - m_Prototext = - R"( -node { - name: "output" - op: "Const" - attr { - key: "dtype" - value { - type: DT_FLOAT - } - } - attr { - key: "value" - value { - tensor { - dtype: DT_FLOAT - )"; - - if (WithShape) - { - m_Prototext += - R"( -tensor_shape { - dim { - size: 2 - } - dim { - size: 2 - } -} - )"; - } - else - { - m_Prototext += - R"( -tensor_shape { -} - )"; - } - - if (WithContent) - { - m_Prototext += - R"( -tensor_content: "\000\000\200?\000\000\200?\000\000\200?\000\000\200?\000\000\200?" - )"; - } - - if (WithValueList) - { - m_Prototext += - R"( -float_val: 1.0 -float_val: 1.0 -float_val: 1.0 -float_val: 1.0 -float_val: 1.0 - )"; - } - - m_Prototext += - R"( - } - } - } -} - )"; - } -}; - -using ConstantCreateNoValueListFixture = ConstantCreateFixture<true, false, true>; -using ConstantCreateNoValueList2Fixture = ConstantCreateFixture<true, false, false>; -using ConstantCreateNoContentFixture = ConstantCreateFixture<true, true, false>; -using ConstantCreateNoContent2Fixture = ConstantCreateFixture<true, false, false>; -using ConstantCreateNoShapeFixture = ConstantCreateFixture<false, false, false>; -using ConstantCreateNoShape2Fixture = ConstantCreateFixture<false, true, false>; -using ConstantCreateNoShape3Fixture = ConstantCreateFixture<false, false, true>; - -BOOST_FIXTURE_TEST_CASE(ConstantCreateInvalidValueList, ConstantCreateNoValueListFixture) -{ - BOOST_REQUIRE_THROW(Setup({}, { "output" }), armnn::ParseException); -} -BOOST_FIXTURE_TEST_CASE(ConstantCreateInvalidValueList2, ConstantCreateNoValueList2Fixture) -{ - BOOST_REQUIRE_THROW(Setup({}, { "output" }), armnn::ParseException); -} -BOOST_FIXTURE_TEST_CASE(ConstantCreateInvalidContent, ConstantCreateNoContentFixture) -{ - BOOST_REQUIRE_THROW(Setup({}, { "output" }), armnn::ParseException); -} -BOOST_FIXTURE_TEST_CASE(ConstantCreateInvalidShape, ConstantCreateNoShapeFixture) -{ - BOOST_REQUIRE_THROW(Setup({}, { "output" }), armnn::ParseException); -} -BOOST_FIXTURE_TEST_CASE(ConstantCreateNoShape2, ConstantCreateNoShape2Fixture) -{ - BOOST_REQUIRE_THROW(Setup({}, { "output" }), armnn::ParseException); -} -BOOST_FIXTURE_TEST_CASE(ConstantCreateNoShape3, ConstantCreateNoShape3Fixture) -{ - Setup({}, { "output" }); - RunTest<1>({}, { { "output", { 1.f, 1.f, 1.f, 1.f, 1.f } } }); -} - -BOOST_AUTO_TEST_SUITE_END() diff --git a/src/armnnTfParser/test/Convolution2d.cpp b/src/armnnTfParser/test/Convolution2d.cpp deleted file mode 100644 index c58615f990..0000000000 --- a/src/armnnTfParser/test/Convolution2d.cpp +++ /dev/null @@ -1,444 +0,0 @@ -// -// Copyright © 2017 Arm Ltd. All rights reserved. -// SPDX-License-Identifier: MIT -// - -#include <boost/test/unit_test.hpp> -#include "armnnTfParser/ITfParser.hpp" -#include "ParserPrototxtFixture.hpp" - -#include <array> -#include <string> -#include <iostream> - -BOOST_AUTO_TEST_SUITE(TensorflowParser) - -struct Convolution2dFixture : public armnnUtils::ParserPrototxtFixture<armnnTfParser::ITfParser> -{ - explicit Convolution2dFixture(const std::string& dataLayout, const std::string& paddingType) - : Convolution2dFixture(dataLayout, paddingType, 1) - {} - - // Dilation: 0 - dilations attribute is not included; - // Dilation: >0 - dilations attribute set to [1,v,v,1], where v is the value of the dilation arg - explicit Convolution2dFixture(const std::string& dataLayout, const std::string& paddingType, - int stride, int dilation = 0) - { - std::string strideString (" i: 1 \n" - " i: 1 \n"); - if (dataLayout == "NHWC") - { - strideString.append(" i: " + std::to_string(stride) + " \n" - " i: 1 \n"); - } - else // dataLayout == "NCHW" - { - strideString.append(" i: 1 \n" - " i: " + std::to_string(stride) + " \n"); - } - - std::string dilationString; - if (dataLayout == "NHWC") - { - dilationString.append(" i: 1 \n" - " i: " + std::to_string(dilation) + " \n" - " i: " + std::to_string(dilation) + " \n" - " i: 1 \n"); - } - else // dataLayout == "NCHW" - { - dilationString.append(" i: 1 \n" - " i: 1 \n" - " i: " + std::to_string(dilation) + " \n" - " i: " + std::to_string(dilation) + " \n"); - } - - m_Prototext = "node { \n" - " name: \"graphInput\" \n" - " op: \"Placeholder\" \n" - " attr { \n" - " key: \"dtype\" \n" - " value { \n" - " type: DT_FLOAT \n" - " } \n" - " } \n" - " attr { \n" - " key: \"shape\" \n" - " value { \n" - " shape { \n" - " } \n" - " } \n" - " } \n" - " } \n" - " node { \n" - " name: \"Const_1\" \n" - " op: \"Const\" \n" - " attr { \n" - " key: \"dtype\" \n" - " value { \n" - " type: DT_FLOAT \n" - " } \n" - " } \n" - " attr { \n" - " key: \"value\" \n" - " value { \n" - " tensor { \n" - " dtype: DT_FLOAT \n" - " tensor_shape { \n" - " dim { \n" - " size: 1 \n" - " } \n" - " dim { \n" - " size: 3 \n" - " } \n" - " dim { \n" - " size: 1 \n" - " } \n" - " dim { \n" - " size: 1 \n" - " } \n" - " } \n" - " tensor_content: \"\\000\\000\\000?\\000\\000\\200?\\000\\000\\000?\" \n" - " } \n" - " } \n" - " } \n" - "} \n" - "node { \n" - " name: \"potato\" \n" - " op: \"Conv2D\" \n" - " input: \"graphInput\" \n" - " input: \"Const_1\" \n" - " attr { \n" - " key: \"T\" \n" - " value { \n" - " type: DT_FLOAT \n" - " } \n" - " } \n" - " attr { \n" - " key: \"data_format\" \n" - " value { \n" - " s: \""; - m_Prototext.append(dataLayout); - m_Prototext.append("\"\n" - " } \n" - " } \n" - " attr { \n" - " key: \"padding\" \n" - " value { \n" - " s: \""); - m_Prototext.append(paddingType); - m_Prototext.append("\"\n" - " } \n" - " } \n" - " attr { \n" - " key: \"strides\" \n" - " value { \n" - " list { \n"); - m_Prototext.append(strideString); - - m_Prototext.append(" } \n" - " } \n" - " } \n"); - - if (dilation > 0) - { - m_Prototext.append(" attr { \n" - " key: \"dilations\" \n" - " value { \n" - " list { \n"); - m_Prototext.append(dilationString); - - m_Prototext.append(" } \n" - " } \n" - " } \n"); - } - m_Prototext.append(" attr { \n" - " key: \"use_cudnn_on_gpu\" \n" - " value { \n" - " b: false \n" - " } \n" - " } \n" - "} \n"); - - // Manual height computation based on stride parameter. - ARMNN_ASSERT_MSG(stride == 1 || stride == 2, "Add support for strides other than 1 or 2."); - std::array<unsigned int, 4> dims; - if (dataLayout == "NHWC") - { - dims = { 1u, (stride == 2 ? 3u : 2u), 3u, 1u }; - } - else // dataLayout == "NCHW" - { - dims = { 1u, 1u, (stride == 2 ? 3u : 2u), 3u }; - } - - SetupSingleInputSingleOutput(armnn::TensorShape(4, dims.data()), "graphInput", "potato"); - } -}; - -struct Convolution2dNhwcSameFixture : Convolution2dFixture -{ - Convolution2dNhwcSameFixture() : Convolution2dFixture("NHWC", "SAME", 1){} -}; -BOOST_FIXTURE_TEST_CASE(ParseConv2dNhwcSame, Convolution2dNhwcSameFixture) -{ - RunTest<4>({1, 2, 3, 4, 5, 6}, {2, 4, 4, 6.5f, 10 , 8.5f}); -} - -struct Convolution2dNchwSameFixture : Convolution2dFixture -{ - Convolution2dNchwSameFixture() : Convolution2dFixture("NCHW", "SAME", 1){} -}; -BOOST_FIXTURE_TEST_CASE(ParseConv2dNchwSame, Convolution2dNchwSameFixture) -{ - RunTest<4>({1, 2, 3, 4, 5, 6}, {2, 4, 4, 6.5f, 10 , 8.5f}); -} - - -struct Convolution2dNhwcValidFixture : Convolution2dFixture -{ - Convolution2dNhwcValidFixture() : Convolution2dFixture("NHWC", "VALID", 1){} -}; -BOOST_FIXTURE_TEST_CASE(ParseConv2dNhwcValid, Convolution2dNhwcValidFixture) -{ - RunTest<4>({1, 2, 3, 4, 5, 6}, {4, 10}); -} - -struct Convolution2dNchwValidFixture : Convolution2dFixture -{ - Convolution2dNchwValidFixture() : Convolution2dFixture("NCHW", "VALID", 1){} -}; -BOOST_FIXTURE_TEST_CASE(ParseConv2dNchwValid, Convolution2dNchwValidFixture) -{ - RunTest<4>({1, 2, 3, 4, 5, 6}, {4, 10}); -} - - -struct Convolution2dStride2NhwcSameFixture : Convolution2dFixture -{ - Convolution2dStride2NhwcSameFixture() : Convolution2dFixture("NHWC", "SAME", 2){} -}; -BOOST_FIXTURE_TEST_CASE(ParseConv2dStride2NhwcSame, Convolution2dStride2NhwcSameFixture) -{ - RunTest<4>({1, 2, 3, 4, 5, 6, 7, 8, 9}, {2, 4, 6.5, 8.5, 11, 13}); -} - -struct Convolution2dStride2NchwSameFixture : Convolution2dFixture -{ - Convolution2dStride2NchwSameFixture() : Convolution2dFixture("NCHW", "SAME", 2){} -}; -BOOST_FIXTURE_TEST_CASE(ParseConv2dStride2NchwSame, Convolution2dStride2NchwSameFixture) -{ - RunTest<4>({1, 2, 3, 4, 5, 6, 7, 8, 9}, {2, 4, 6.5, 8.5, 11, 13}); -} - - -struct Convolution2dStride2NhwcValidFixture : Convolution2dFixture -{ - Convolution2dStride2NhwcValidFixture() : Convolution2dFixture("NHWC", "VALID", 2){} -}; -BOOST_FIXTURE_TEST_CASE(ParseConv2dStride2NhwcValid, Convolution2dStride2NhwcValidFixture) -{ - RunTest<4>({1, 2, 3, 4, 5, 6, 7, 8, 9}, {4, 10, 16}); -} - -struct Convolution2dStride2NchwValidFixture : Convolution2dFixture -{ - Convolution2dStride2NchwValidFixture() : Convolution2dFixture("NCHW", "VALID", 2){} -}; -BOOST_FIXTURE_TEST_CASE(ParseConv2dStride2NchwValid, Convolution2dStride2NchwValidFixture) -{ - RunTest<4>({1, 2, 3, 4, 5, 6, 7, 8, 9}, {4, 10, 16}); -} - - -struct Convolution2dDilation1NhwcFixture : Convolution2dFixture -{ - Convolution2dDilation1NhwcFixture() : Convolution2dFixture("NHWC", "SAME", 1, 1){} -}; -BOOST_FIXTURE_TEST_CASE(ParseConv2dDilation1Nhwc, Convolution2dDilation1NhwcFixture) -{ - RunTest<4>({1, 2, 3, 4, 5, 6}, {2, 4, 4, 6.5f, 10 , 8.5f}); -} - -struct Convolution2dDilation1NchwFixture : Convolution2dFixture -{ - Convolution2dDilation1NchwFixture() : Convolution2dFixture("NCHW", "SAME", 1, 1){} -}; -BOOST_FIXTURE_TEST_CASE(ParseConv2dDilation1Nchw, Convolution2dDilation1NchwFixture) -{ - RunTest<4>({1, 2, 3, 4, 5, 6}, {2, 4, 4, 6.5f, 10 , 8.5f}); -} - -struct Convolution2dDilationFixture : public armnnUtils::ParserPrototxtFixture<armnnTfParser::ITfParser> -{ - explicit Convolution2dDilationFixture(const std::string& dataLayout, const std::string& paddingType) - : Convolution2dDilationFixture(dataLayout, paddingType, 1) - {} - - explicit Convolution2dDilationFixture(const std::string& dataLayout, const std::string& paddingType, - int stride, int dilation = 0) - { - std::string strideString; - if (dataLayout == "NHWC") - { - strideString.append(" i: 1 \n" - " i: " + std::to_string(stride) + " \n" - " i: " + std::to_string(stride) + " \n" - " i: 1 \n"); - } - else // dataLayout == "NCHW" - { - strideString.append(" i: 1 \n" - " i: 1 \n" - " i: " + std::to_string(stride) + " \n" - " i: " + std::to_string(stride) + " \n"); - } - - std::string dilationString; - if (dataLayout == "NHWC") - { - dilationString.append(" i: 1 \n" - " i: " + std::to_string(dilation) + " \n" - " i: " + std::to_string(dilation) + " \n" - " i: 1 \n"); - } - else // dataLayout == "NCHW" - { - dilationString.append(" i: 1 \n" - " i: 1 \n" - " i: " + std::to_string(dilation) + " \n" - " i: " + std::to_string(dilation) + " \n"); - } - - m_Prototext = "node { \n" - " name: \"graphInput\" \n" - " op: \"Placeholder\" \n" - " attr { \n" - " key: \"dtype\" \n" - " value { \n" - " type: DT_FLOAT \n" - " } \n" - " } \n" - " attr { \n" - " key: \"shape\" \n" - " value { \n" - " shape { \n" - " } \n" - " } \n" - " } \n" - " } \n" - " node { \n" - " name: \"Const_1\" \n" - " op: \"Const\" \n" - " attr { \n" - " key: \"dtype\" \n" - " value { \n" - " type: DT_FLOAT \n" - " } \n" - " } \n" - " attr { \n" - " key: \"value\" \n" - " value { \n" - " tensor { \n" - " dtype: DT_FLOAT \n" - " tensor_shape { \n" - " dim { \n" - " size: 3 \n" - " } \n" - " dim { \n" - " size: 1 \n" - " } \n" - " dim { \n" - " size: 1 \n" - " } \n" - " dim { \n" - " size: 1 \n" - " } \n" - " } \n" - " tensor_content: \"\\001\\000\\000?\\000\\000\\000?\\001\\000\\000?\" \n" - " } \n" - " } \n" - " } \n" - "} \n" - "node { \n" - " name: \"potato\" \n" - " op: \"Conv2D\" \n" - " input: \"graphInput\" \n" - " input: \"Const_1\" \n" - " attr { \n" - " key: \"T\" \n" - " value { \n" - " type: DT_FLOAT \n" - " } \n" - " } \n" - " attr { \n" - " key: \"data_format\" \n" - " value { \n" - " s: \""; - m_Prototext.append(dataLayout); - m_Prototext.append("\"\n" - " } \n" - " } \n" - " attr { \n" - " key: \"padding\" \n" - " value { \n" - " s: \""); - m_Prototext.append(paddingType); - m_Prototext.append("\"\n" - " } \n" - " } \n" - " attr { \n" - " key: \"strides\" \n" - " value { \n" - " list { \n"); - m_Prototext.append(strideString); - - m_Prototext.append(" } \n" - " } \n" - " } \n"); - - if (dilation > 0) - { - m_Prototext.append(" attr { \n" - " key: \"dilations\" \n" - " value { \n" - " list { \n"); - m_Prototext.append(dilationString); - - m_Prototext.append(" } \n" - " } \n" - " } \n"); - } - m_Prototext.append(" attr { \n" - " key: \"use_cudnn_on_gpu\" \n" - " value { \n" - " b: false \n" - " } \n" - " } \n" - "} \n"); - - // Manual height computation based on stride parameter. - std::array<unsigned int, 4> dims = { 1u, 1u, 6u, 6u };; - - SetupSingleInputSingleOutput(armnn::TensorShape(4, dims.data()), "graphInput", "potato"); - } -}; - -struct Convolution2dDilation2NchwValidFixture : Convolution2dDilationFixture -{ - Convolution2dDilation2NchwValidFixture() : Convolution2dDilationFixture("NCHW", "VALID", 1, 2){} -}; -BOOST_FIXTURE_TEST_CASE(ParseConv2dDilation2NchwValid, Convolution2dDilation2NchwValidFixture) -{ - RunTest<4>({1.0, 2.0, 3.0, 4.0, 5.0, 6.0, - 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, - 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, - 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, - 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, - 7.0, 8.0, 9.0, 10.0, 11.0, 12.0}, - {1.5f, 3.0f, 4.5f, 6.0f, 7.5f, 9.0f, 10.5f, 12.f, 13.5f, 15.0f, 16.5f, 18.0f}); -} - - -BOOST_AUTO_TEST_SUITE_END() diff --git a/src/armnnTfParser/test/DepthwiseConvolution2d.cpp b/src/armnnTfParser/test/DepthwiseConvolution2d.cpp deleted file mode 100644 index 43a7ebc28e..0000000000 --- a/src/armnnTfParser/test/DepthwiseConvolution2d.cpp +++ /dev/null @@ -1,190 +0,0 @@ -// -// Copyright © 2017 Arm Ltd. All rights reserved. -// SPDX-License-Identifier: MIT -// - -#include "ParserPrototxtFixture.hpp" - -#include "armnnTfParser/ITfParser.hpp" - -#include <armnnUtils/Permute.hpp> - -#include <boost/test/unit_test.hpp> - -#include <string> -#include <iostream> - -using namespace armnnUtils; -using namespace armnn; - -BOOST_AUTO_TEST_SUITE(TensorflowParser) - -struct DepthwiseConvolution2dFixture : public armnnUtils::ParserPrototxtFixture<armnnTfParser::ITfParser> -{ - explicit DepthwiseConvolution2dFixture(const std::string& dataLayout, const char* paddingType) - { - m_Prototext = "node { \n" - " name: \"graphInput\" \n" - " op: \"Placeholder\" \n" - " attr { \n" - " key: \"dtype\" \n" - " value { \n" - " type: DT_FLOAT \n" - " } \n" - " } \n" - " attr { \n" - " key: \"shape\" \n" - " value { \n" - " shape { \n" - " } \n" - " } \n" - " } \n" - " } \n" - " node { \n" - " name: \"Const_1\" \n" - " op: \"Const\" \n" - " attr { \n" - " key: \"dtype\" \n" - " value { \n" - " type: DT_FLOAT \n" - " } \n" - " } \n" - " attr { \n" - " key: \"value\" \n" - " value { \n" - " tensor { \n" - " dtype: DT_FLOAT \n" - " tensor_shape { \n" - " dim { \n" - " size: 1 \n" - " } \n" - " dim { \n" - " size: 3 \n" - " } \n" - " dim { \n" - " size: 3 \n" - " } \n" - " dim { \n" - " size: 3 \n" - " } \n" - " } \n" - " tensor_content: \"\\000\\000\\000?\\000\\000\\200?\\000\\000\\000?" - "\\000\\000\\000?\\000\\000\\200?\\000\\000\\000?" - "\\000\\000\\000?\\000\\000\\200?\\000\\000\\000?" - "\\000\\000\\000?\\000\\000\\200?\\000\\000\\000?" - "\\000\\000\\000?\\000\\000\\200?\\000\\000\\000?" - "\\000\\000\\000?\\000\\000\\200?\\000\\000\\000?" - "\\000\\000\\000?\\000\\000\\200?\\000\\000\\000?" - "\\000\\000\\000?\\000\\000\\200?\\000\\000\\000?" - "\\000\\000\\000?\\000\\000\\200?\\000\\000\\000?\" \n" - " } \n" - " } \n" - " } \n" - "} \n" - "node { \n" - " name: \"potato\" \n" - " op: \"DepthwiseConv2dNative\" \n" - " input: \"graphInput\" \n" - " input: \"Const_1\" \n" - " attr { \n" - " key: \"T\" \n" - " value { \n" - " type: DT_FLOAT \n" - " } \n" - " } \n" - " attr { \n" - " key: \"data_format\" \n" - " value { \n" - " s: \""; - m_Prototext.append(dataLayout); - m_Prototext.append("\"\n" - " } \n" - " } \n" - " attr { \n" - " key: \"padding\" \n" - " value { \n" - " s: \""); - m_Prototext.append(paddingType); - m_Prototext.append("\"\n" - " } \n" - " } \n" - " attr { \n" - " key: \"strides\" \n" - " value { \n" - " list { \n" - " i: 1 \n" - " i: 1 \n" - " i: 1 \n" - " i: 1 \n" - " } \n" - " } \n" - " } \n" - " attr { \n" - " key: \"use_cudnn_on_gpu\" \n" - " value { \n" - " b: false \n" - " } \n" - " } \n" - "} \n"); - - if(dataLayout == "NHWC") - { - SetupSingleInputSingleOutput({ 1u, 1u, 3u, 3u }, "graphInput", "potato"); - } - else - { - SetupSingleInputSingleOutput({ 1u, 3u, 1u, 3u }, "graphInput", "potato"); - } - } -}; - -struct DepthwiseConvolution2dNhwcSameFixture : DepthwiseConvolution2dFixture -{ - DepthwiseConvolution2dNhwcSameFixture() : DepthwiseConvolution2dFixture("NHWC", "SAME") { } -}; - -BOOST_FIXTURE_TEST_CASE(ParseDepthwiseConv2DNhwcSame, DepthwiseConvolution2dNhwcSameFixture) -{ - RunTest<4>({ 1, 2, 3, 4, 5, 6, 7, 8, 9 }, - { 2.5f, 5.f, 2.5f, 3.5f, 7.f, 3.5f, 4.5f, 9.f, 4.5f, - 6.f, 12.f, 6.f, 7.5f, 15.f, 7.5f, 9.f, 18.f, 9.f, - 5.5f, 11.f, 5.5f, 6.5f, 13.f, 6.5f, 7.5f, 15.f, 7.5f }); -} - -struct DepthwiseConvolution2dNchwSameFixture : DepthwiseConvolution2dFixture -{ - DepthwiseConvolution2dNchwSameFixture() : DepthwiseConvolution2dFixture("NCHW", "SAME") { } -}; - -BOOST_FIXTURE_TEST_CASE(ParseDepthwiseConv2DNchwSame, DepthwiseConvolution2dNchwSameFixture) -{ - RunTest<4>({ 1, 4, 7, 2, 5, 8, 3, 6, 9 }, - { 2.5f, 6.f, 5.5f, 5.f, 12.f, 11.f, 2.5f, 6.f, 5.5f, - 3.5f, 7.5f, 6.5f, 7.f, 15.f, 13.f, 3.5f, 7.5f, 6.5f, - 4.5f, 9.f, 7.5f, 9.f, 18.f, 15.f, 4.5f, 9.f, 7.5f }); -} - -struct DepthwiseConvolution2dNhwcValidFixture : DepthwiseConvolution2dFixture -{ - DepthwiseConvolution2dNhwcValidFixture() : DepthwiseConvolution2dFixture("NHWC", "VALID") { } -}; - -BOOST_FIXTURE_TEST_CASE(ParseDepthwiseConv2DNhwcValid, DepthwiseConvolution2dNhwcValidFixture) -{ - RunTest<4>({ 1, 2, 3, 4, 5, 6, 7, 8, 9 }, // input data - { 6.f, 12.f, 6.f, 7.5f, 15.f, 7.5f, 9.f, 18.f, 9.f }); // output expected data -} - -struct DepthwiseConvolution2dNchwValidFixture : DepthwiseConvolution2dFixture -{ - DepthwiseConvolution2dNchwValidFixture() : DepthwiseConvolution2dFixture("NCHW", "VALID") { } -}; - -BOOST_FIXTURE_TEST_CASE(ParseDepthwiseConv2DNchwValid, DepthwiseConvolution2dNchwValidFixture) -{ - RunTest<4>({ 1, 4, 7, 2, 5, 8, 3, 6, 9 }, - { 6.f, 12.f, 6.f, 7.5f, 15.f, 7.5f, 9.f, 18.f, 9.f }); -} - - -BOOST_AUTO_TEST_SUITE_END() diff --git a/src/armnnTfParser/test/Equal.cpp b/src/armnnTfParser/test/Equal.cpp deleted file mode 100644 index 2dce822b0f..0000000000 --- a/src/armnnTfParser/test/Equal.cpp +++ /dev/null @@ -1,139 +0,0 @@ -// -// Copyright © 2017 Arm Ltd. All rights reserved. -// SPDX-License-Identifier: MIT -// - -#include <boost/test/unit_test.hpp> -#include "armnnTfParser/ITfParser.hpp" -#include "ParserPrototxtFixture.hpp" - -BOOST_AUTO_TEST_SUITE(TensorflowParser) - - struct EqualFixture : public armnnUtils::ParserPrototxtFixture<armnnTfParser::ITfParser> - { - EqualFixture() - { - m_Prototext = R"( -node { - name: "input0" - op: "Placeholder" - attr { - key: "dtype" - value { - type: DT_FLOAT - } - } - attr { - key: "shape" - value { - shape { - } - } - } -} -node { - name: "input1" - op: "Placeholder" - attr { - key: "dtype" - value { - type: DT_FLOAT - } - } - attr { - key: "shape" - value { - shape { - } - } - } -} -node { - name: "output" - op: "Equal" - input: "input0" - input: "input1" - attr { - key: "T" - value { - type: DT_FLOAT - } - } -} - )"; - } - }; - -BOOST_FIXTURE_TEST_CASE(ParseEqualUnsupportedBroadcast, EqualFixture) -{ - BOOST_REQUIRE_THROW(Setup({ { "input0", {2, 3} }, - { "input1", {1, 2, 2, 3} } }, - { "output" }), - armnn::ParseException); -} - -struct EqualFixtureAutoSetup : public EqualFixture -{ - EqualFixtureAutoSetup(const armnn::TensorShape& input0Shape, - const armnn::TensorShape& input1Shape) - : EqualFixture() - { - Setup({ { "input0", input0Shape }, - { "input1", input1Shape } }, - { "output" }); - } -}; - -struct EqualTwoByTwo : public EqualFixtureAutoSetup -{ - EqualTwoByTwo() : EqualFixtureAutoSetup({2,2}, {2,2}) {} -}; - -BOOST_FIXTURE_TEST_CASE(ParseEqualTwoByTwo, EqualTwoByTwo) -{ - RunComparisonTest<2>({ { "input0", { 1.0f, 2.0f, 3.0f, 2.0f } }, - { "input1", { 1.0f, 5.0f, 2.0f, 2.0f } } }, - { { "output", { 1, 0, 0, 1 } } }); -} - -struct EqualBroadcast1DAnd4D : public EqualFixtureAutoSetup -{ - EqualBroadcast1DAnd4D() : EqualFixtureAutoSetup({1}, {1,1,2,2}) {} -}; - -BOOST_FIXTURE_TEST_CASE(ParseEqualBroadcast1DToTwoByTwo, EqualBroadcast1DAnd4D) -{ - RunComparisonTest<4>({ { "input0", { 2.0f } }, - { "input1", { 1.0f, 2.0f, 3.0f, 2.0f } } }, - { { "output", { 0, 1, 0, 1 } } }); -} - -struct EqualBroadcast4DAnd1D : public EqualFixtureAutoSetup -{ - EqualBroadcast4DAnd1D() : EqualFixtureAutoSetup({1,1,2,2}, {1}) {} -}; - -BOOST_FIXTURE_TEST_CASE(ParseEqualBroadcast4DAnd1D, EqualBroadcast4DAnd1D) -{ - RunComparisonTest<4>({ { "input0", { 1.0f, 2.0f, 3.0f, 2.0f } }, - { "input1", { 3.0f } } }, - { { "output", { 0, 0, 1, 0 } } }); -} - -struct EqualMultiDimBroadcast : public EqualFixtureAutoSetup -{ - EqualMultiDimBroadcast() : EqualFixtureAutoSetup({1,1,2,1}, {1,2,1,3}) {} -}; - -BOOST_FIXTURE_TEST_CASE(ParseEqualMultiDimBroadcast, EqualMultiDimBroadcast) -{ - RunComparisonTest<4>({ { "input0", { 1.0f, 2.0f } }, - { "input1", { 1.0f, 2.0f, 3.0f, - 3.0f, 2.0f, 2.0f } } }, - { { "output", { 1, 0, 0, - 0, 1, 0, - 0, 0, 0, - 0, 1, 1 } } }); -} - -BOOST_AUTO_TEST_SUITE_END() diff --git a/src/armnnTfParser/test/ExpandDims.cpp b/src/armnnTfParser/test/ExpandDims.cpp deleted file mode 100644 index ad95641cd1..0000000000 --- a/src/armnnTfParser/test/ExpandDims.cpp +++ /dev/null @@ -1,313 +0,0 @@ -// -// Copyright © 2017 Arm Ltd. All rights reserved. -// SPDX-License-Identifier: MIT -// - -#include <boost/test/unit_test.hpp> -#include "armnnTfParser/ITfParser.hpp" -#include "ParserPrototxtFixture.hpp" - -BOOST_AUTO_TEST_SUITE(TensorflowParser) - -struct ExpandDimsFixture : public armnnUtils::ParserPrototxtFixture<armnnTfParser::ITfParser> -{ - ExpandDimsFixture(const std::string& expandDim) - { - m_Prototext = - "node { \n" - " name: \"graphInput\" \n" - " op: \"Placeholder\" \n" - " attr { \n" - " key: \"dtype\" \n" - " value { \n" - " type: DT_FLOAT \n" - " } \n" - " } \n" - " attr { \n" - " key: \"shape\" \n" - " value { \n" - " shape { \n" - " } \n" - " } \n" - " } \n" - " } \n" - "node { \n" - " name: \"ExpandDims\" \n" - " op: \"ExpandDims\" \n" - " input: \"graphInput\" \n" - " attr { \n" - " key: \"T\" \n" - " value { \n" - " type: DT_FLOAT \n" - " } \n" - " } \n" - " attr { \n" - " key: \"Tdim\" \n" - " value { \n"; - m_Prototext += "i:" + expandDim; - m_Prototext += - " } \n" - " } \n" - "} \n"; - - SetupSingleInputSingleOutput({ 2, 3, 5 }, "graphInput", "ExpandDims"); - } -}; - -struct ExpandZeroDim : ExpandDimsFixture -{ - ExpandZeroDim() : ExpandDimsFixture("0") {} -}; - -struct ExpandTwoDim : ExpandDimsFixture -{ - ExpandTwoDim() : ExpandDimsFixture("2") {} -}; - -struct ExpandThreeDim : ExpandDimsFixture -{ - ExpandThreeDim() : ExpandDimsFixture("3") {} -}; - -struct ExpandMinusOneDim : ExpandDimsFixture -{ - ExpandMinusOneDim() : ExpandDimsFixture("-1") {} -}; - -struct ExpandMinusThreeDim : ExpandDimsFixture -{ - ExpandMinusThreeDim() : ExpandDimsFixture("-3") {} -}; - -BOOST_FIXTURE_TEST_CASE(ParseExpandZeroDim, ExpandZeroDim) -{ - BOOST_TEST((m_Parser->GetNetworkOutputBindingInfo("ExpandDims").second.GetShape() == - armnn::TensorShape({1, 2, 3, 5}))); -} - -BOOST_FIXTURE_TEST_CASE(ParseExpandTwoDim, ExpandTwoDim) -{ - BOOST_TEST((m_Parser->GetNetworkOutputBindingInfo("ExpandDims").second.GetShape() == - armnn::TensorShape({2, 3, 1, 5}))); -} - -BOOST_FIXTURE_TEST_CASE(ParseExpandThreeDim, ExpandThreeDim) -{ - BOOST_TEST((m_Parser->GetNetworkOutputBindingInfo("ExpandDims").second.GetShape() == - armnn::TensorShape({2, 3, 5, 1}))); -} - -BOOST_FIXTURE_TEST_CASE(ParseExpandMinusOneDim, ExpandMinusOneDim) -{ - BOOST_TEST((m_Parser->GetNetworkOutputBindingInfo("ExpandDims").second.GetShape() == - armnn::TensorShape({2, 3, 5, 1}))); -} - -BOOST_FIXTURE_TEST_CASE(ParseExpandMinusThreeDim, ExpandMinusThreeDim) -{ - BOOST_TEST((m_Parser->GetNetworkOutputBindingInfo("ExpandDims").second.GetShape() == - armnn::TensorShape({2, 1, 3, 5}))); -} - -struct ExpandDimsAsInputFixture : public armnnUtils::ParserPrototxtFixture<armnnTfParser::ITfParser> -{ - ExpandDimsAsInputFixture(const std::string& expandDim, - const bool wrongDataType = false, - const std::string& numElements = "1") - { - std::string dataType = (wrongDataType) ? "DT_FLOAT" : "DT_INT32"; - std::string val = (wrongDataType) ? ("float_val: " + expandDim + ".0") : ("int_val: "+ expandDim); - - m_Prototext = R"( - node { - name: "a" - op: "Placeholder" - attr { - key: "dtype" - value { - type: DT_FLOAT - } - } - attr { - key: "shape" - value { - shape { - dim { - size: 1 - } - dim { - size: 4 - } - } - } - } - } - node { - name: "b" - op: "Const" - attr { - key: "dtype" - value { - type: )" + dataType + R"( - } - } - attr { - key: "value" - value { - tensor { - dtype: )" + dataType + R"( - tensor_shape { - dim { - size: )" + numElements + R"( - } - } - )" + val + R"( - } - } - } - } - node { - name: "ExpandDims" - op: "ExpandDims" - input: "a" - input: "b" - attr { - key: "T" - value { - type: DT_FLOAT - } - } - attr { - key: "Tdim" - value { - type: DT_INT32 - } - } - } - versions { - producer: 134 - })"; - } -}; - -struct ExpandDimAsInput : ExpandDimsAsInputFixture -{ - ExpandDimAsInput() : ExpandDimsAsInputFixture("0") - { - Setup({{"a", {1,4}} ,{"b",{1,1}}}, { "ExpandDims" }); - } -}; - - -BOOST_FIXTURE_TEST_CASE(ParseExpandDimAsInput, ExpandDimAsInput) -{ - // Axis parameter that describes which axis/dim should be expanded is passed as a second input - BOOST_TEST((m_Parser->GetNetworkOutputBindingInfo("ExpandDims").second.GetShape() == - armnn::TensorShape({1, 1, 4}))); -} - -struct ExpandDimAsInputWrongDataType : ExpandDimsAsInputFixture -{ - ExpandDimAsInputWrongDataType() : ExpandDimsAsInputFixture("0", true, "1") {} -}; - -BOOST_FIXTURE_TEST_CASE(ParseExpandDimAsInputWrongDataType, ExpandDimAsInputWrongDataType) -{ - // Axis parameter that describes which axis/dim should be expanded is passed as a second input - // Axis parameter is of wrong data type (float instead of int32) - BOOST_REQUIRE_THROW(Setup({{"a", {1,4}} ,{"b",{1,1}}}, { "ExpandDims" }), armnn::ParseException); -} - -struct ExpandDimAsInputWrongShape : ExpandDimsAsInputFixture -{ - ExpandDimAsInputWrongShape() : ExpandDimsAsInputFixture("0", false, "2") {} -}; - -BOOST_FIXTURE_TEST_CASE(ParseExpandDimAsInputWrongShape, ExpandDimAsInputWrongShape) -{ - // Axis parameter that describes which axis/dim should be expanded is passed as a second input - // Axis parameter is of wrong shape - BOOST_REQUIRE_THROW(Setup({{"a", {1,4}} ,{"b",{1,1}}}, { "ExpandDims" }), armnn::ParseException); -} - -struct ExpandDimsAsNotConstInputFixture : public armnnUtils::ParserPrototxtFixture<armnnTfParser::ITfParser> -{ - ExpandDimsAsNotConstInputFixture() - { - m_Prototext = R"( - node { - name: "a" - op: "Placeholder" - attr { - key: "dtype" - value { - type: DT_FLOAT - } - } - attr { - key: "shape" - value { - shape { - dim { - size: 1 - } - dim { - size: 4 - } - } - } - } - } - node { - name: "b" - op: "Placeholder" - attr { - key: "dtype" - value { - type: DT_INT32 - } - } - attr { - key: "shape" - value { - shape { - dim { - size: 1 - } - } - } - } - } - node { - name: "ExpandDims" - op: "ExpandDims" - input: "a" - input: "b" - attr { - key: "T" - value { - type: DT_FLOAT - } - } - attr { - key: "Tdim" - value { - type: DT_INT32 - } - } - } - versions { - producer: 134 - })"; - } -}; - -BOOST_FIXTURE_TEST_CASE(ParseExpandDimAsNotConstInput, ExpandDimsAsNotConstInputFixture) -{ - // Axis parameter that describes which axis/dim should be expanded is passed as a second input. - // But is not a constant tensor --> not supported - BOOST_REQUIRE_THROW(Setup({{"a", {1,4}} ,{"b",{1,1}}}, { "ExpandDims" }), - armnn::ParseException); -} - -BOOST_AUTO_TEST_SUITE_END() diff --git a/src/armnnTfParser/test/FullyConnected.cpp b/src/armnnTfParser/test/FullyConnected.cpp deleted file mode 100644 index 14561c43cd..0000000000 --- a/src/armnnTfParser/test/FullyConnected.cpp +++ /dev/null @@ -1,579 +0,0 @@ -// -// Copyright © 2017 Arm Ltd. All rights reserved. -// SPDX-License-Identifier: MIT -// - -#include <boost/test/unit_test.hpp> -#include "armnnTfParser/ITfParser.hpp" -#include "ParserPrototxtFixture.hpp" -#include "Runtime.hpp" -#include "Network.hpp" -#include "Graph.hpp" - -BOOST_AUTO_TEST_SUITE(TensorflowParser) - -// In Tensorflow fully connected layers are expressed as a MatMul followed by an Add. -// The TfParser must detect this case and convert them to a FullyConnected layer. -struct FullyConnectedFixture : public armnnUtils::ParserPrototxtFixture<armnnTfParser::ITfParser> -{ - FullyConnectedFixture() - { - // Input = tf.placeholder(tf.float32, [1, 1], "input") - // Weights = tf.constant([2], tf.float32, [1, 1]) - // Matmul = tf.matmul(input, weights) - // Bias = tf.constant([1], tf.float32) - // Output = tf.add(matmul, bias, name="output") - m_Prototext = R"( -node { - name: "input" - op: "Placeholder" - attr { - key: "dtype" - value { - type: DT_FLOAT - } - } - attr { - key: "shape" - value { - shape { - dim { - size: 1 - } - dim { - size: 1 - } - } - } - } -} -node { - name: "Const" - op: "Const" - attr { - key: "dtype" - value { - type: DT_FLOAT - } - } - attr { - key: "value" - value { - tensor { - dtype: DT_FLOAT - tensor_shape { - dim { - size: 1 - } - dim { - size: 1 - } - } - float_val: 2.0 - } - } - } -} -node { - name: "MatMul" - op: "MatMul" - input: "input" - input: "Const" - attr { - key: "T" - value { - type: DT_FLOAT - } - } - attr { - key: "transpose_a" - value { - b: false - } - } - attr { - key: "transpose_b" - value { - b: false - } - } -} -node { - name: "Const_1" - op: "Const" - attr { - key: "dtype" - value { - type: DT_FLOAT - } - } - attr { - key: "value" - value { - tensor { - dtype: DT_FLOAT - tensor_shape { - dim { - size: 1 - } - } - float_val: 1.0 - } - } - } -} -node { - name: "output" - op: "Add" - input: "MatMul" - input: "Const_1" - attr { - key: "T" - value { - type: DT_FLOAT - } - } -} - )"; - SetupSingleInputSingleOutput({ 1, 1 }, "input", "output"); - } -}; - -BOOST_FIXTURE_TEST_CASE(FullyConnected, FullyConnectedFixture) -{ - RunTest<1>({ 3 }, { 7 }); -} - -// Similar to FullyConnectedFixture, but this time the MatMul's output is used by two Adds. This should result -// in two FullyConnected layers being created. -// I -// | -// M -- C -// / \' -// C-- A A -- C -// \ / -// A -struct MatMulUsedInTwoFcFixture : public armnnUtils::ParserPrototxtFixture<armnnTfParser::ITfParser> -{ - MatMulUsedInTwoFcFixture() - { - m_Prototext = R"( -node { - name: "input" - op: "Placeholder" - attr { - key: "dtype" - value { - type: DT_FLOAT - } - } - attr { - key: "shape" - value { - shape { - dim { - size: 1 - } - dim { - size: 1 - } - } - } - } -} -node { - name: "Const" - op: "Const" - attr { - key: "dtype" - value { - type: DT_FLOAT - } - } - attr { - key: "value" - value { - tensor { - dtype: DT_FLOAT - tensor_shape { - dim { - size: 1 - } - dim { - size: 1 - } - } - float_val: 2.0 - } - } - } -} -node { - name: "MatMul" - op: "MatMul" - input: "input" - input: "Const" - attr { - key: "T" - value { - type: DT_FLOAT - } - } - attr { - key: "transpose_a" - value { - b: false - } - } - attr { - key: "transpose_b" - value { - b: false - } - } -} -node { - name: "Const_1" - op: "Const" - attr { - key: "dtype" - value { - type: DT_FLOAT - } - } - attr { - key: "value" - value { - tensor { - dtype: DT_FLOAT - tensor_shape { - dim { - size: 1 - } - } - float_val: 5.0 - } - } - } -} -node { - name: "Const_2" - op: "Const" - attr { - key: "dtype" - value { - type: DT_FLOAT - } - } - attr { - key: "value" - value { - tensor { - dtype: DT_FLOAT - tensor_shape { - dim { - size: 1 - } - } - float_val: 15.0 - } - } - } -} -node { - name: "Add" - op: "Add" - input: "MatMul" - input: "Const_1" - attr { - key: "T" - value { - type: DT_FLOAT - } - } -} -node { - name: "Add_1" - op: "Add" - input: "MatMul" - input: "Const_2" - attr { - key: "T" - value { - type: DT_FLOAT - } - } -} -node { - name: "output" - op: "Add" - input: "Add" - input: "Add_1" - attr { - key: "T" - value { - type: DT_FLOAT - } - } -} - )"; - SetupSingleInputSingleOutput({ 1, 1 }, "input", "output"); - } -}; - -BOOST_FIXTURE_TEST_CASE(MatMulUsedInTwoFc, MatMulUsedInTwoFcFixture) -{ - RunTest<1>({ 3 }, { 32 }); - // Ideally we would check here that the armnn network has 5 layers: - // Input, 2 x FullyConnected (biased), Add and Output. - // This would make sure the parser hasn't incorrectly added some unconnected layers corresponding to the MatMul. -} - -// Similar to MatMulUsedInTwoFc, but this time the Adds are 'staggered' (see diagram), which means that only one -// FullyConnected layer can be created (the other should just be an Add). -// I -// | -// M -- C1 -// / \' -// C2 -- A | -// \ / -// A -struct MatMulUsedInTwoFcStaggeredFixture : public armnnUtils::ParserPrototxtFixture<armnnTfParser::ITfParser> -{ - MatMulUsedInTwoFcStaggeredFixture() - { - // Input = tf.placeholder(tf.float32, shape=[1,1], name = "input") - // Const1 = tf.constant([17], tf.float32, [1,1]) - // Mul = tf.matmul(input, const1) - // Monst2 = tf.constant([7], tf.float32, [1]) - // Fc = tf.add(mul, const2) - // Output = tf.add(mul, fc, name="output") - m_Prototext = R"( -node { - name: "input" - op: "Placeholder" - attr { - key: "dtype" - value { - type: DT_FLOAT - } - } - attr { - key: "shape" - value { - shape { - dim { - size: 1 - } - dim { - size: 1 - } - } - } - } -} -node { - name: "Const" - op: "Const" - attr { - key: "dtype" - value { - type: DT_FLOAT - } - } - attr { - key: "value" - value { - tensor { - dtype: DT_FLOAT - tensor_shape { - dim { - size: 1 - } - dim { - size: 1 - } - } - float_val: 17.0 - } - } - } -} -node { - name: "MatMul" - op: "MatMul" - input: "input" - input: "Const" - attr { - key: "T" - value { - type: DT_FLOAT - } - } - attr { - key: "transpose_a" - value { - b: false - } - } - attr { - key: "transpose_b" - value { - b: false - } - } -} -node { - name: "Const_1" - op: "Const" - attr { - key: "dtype" - value { - type: DT_FLOAT - } - } - attr { - key: "value" - value { - tensor { - dtype: DT_FLOAT - tensor_shape { - dim { - size: 1 - } - } - float_val: 7.0 - } - } - } -} -node { - name: "Add" - op: "Add" - input: "MatMul" - input: "Const_1" - attr { - key: "T" - value { - type: DT_FLOAT - } - } -} -node { - name: "output" - op: "Add" - input: "MatMul" - input: "Add" - attr { - key: "T" - value { - type: DT_FLOAT - } - } -} - )"; - SetupSingleInputSingleOutput({ 1, 1 }, "input", "output"); - } -}; - -BOOST_FIXTURE_TEST_CASE(MatMulUsedInTwoFcStaggered, MatMulUsedInTwoFcStaggeredFixture) -{ - RunTest<1>({ 2 }, { 75 }); - // Ideally we would check here that the armnn network has 5 layers: - // Input, FullyConnected (biased), FullyConnected (non biased), Add and Output. -} - -// A MatMul in isolation, not connected to an add. Should result in a non-biased FullyConnected layer. -struct MatMulFixture : public armnnUtils::ParserPrototxtFixture<armnnTfParser::ITfParser> -{ - MatMulFixture() - { - // Input = tf.placeholder(tf.float32, shape = [1, 1], name = "input") - // Const = tf.constant([17], tf.float32, [1, 1]) - // Output = tf.matmul(input, const, name = "output") - m_Prototext = R"( -node { - name: "input" - op: "Placeholder" - attr { - key: "dtype" - value { - type: DT_FLOAT - } - } - attr { - key: "shape" - value { - shape { - dim { - size: 1 - } - dim { - size: 1 - } - } - } - } -} -node { - name: "Const" - op: "Const" - attr { - key: "dtype" - value { - type: DT_FLOAT - } - } - attr { - key: "value" - value { - tensor { - dtype: DT_FLOAT - tensor_shape { - dim { - size: 1 - } - dim { - size: 1 - } - } - float_val: 17.0 - } - } - } -} -node { - name: "output" - op: "MatMul" - input: "input" - input: "Const" - attr { - key: "T" - value { - type: DT_FLOAT - } - } - attr { - key: "transpose_a" - value { - b: false - } - } - attr { - key: "transpose_b" - value { - b: false - } - } -} - )"; - SetupSingleInputSingleOutput({ 1, 1 }, "input", "output"); - } -}; - -BOOST_FIXTURE_TEST_CASE(MatMul, MatMulFixture) -{ - RunTest<1>({ 2 }, { 34 }); -} - -BOOST_AUTO_TEST_SUITE_END() diff --git a/src/armnnTfParser/test/FusedBatchNorm.cpp b/src/armnnTfParser/test/FusedBatchNorm.cpp deleted file mode 100644 index b93a4728d0..0000000000 --- a/src/armnnTfParser/test/FusedBatchNorm.cpp +++ /dev/null @@ -1,212 +0,0 @@ -// -// Copyright © 2017 Arm Ltd. All rights reserved. -// SPDX-License-Identifier: MIT -// - -#include <boost/test/unit_test.hpp> -#include "armnnTfParser/ITfParser.hpp" -#include "ParserPrototxtFixture.hpp" - -#include <array> - -BOOST_AUTO_TEST_SUITE(TensorflowParser) - -struct FusedBatchNormFixture : public armnnUtils::ParserPrototxtFixture<armnnTfParser::ITfParser> -{ - explicit FusedBatchNormFixture(const std::string& dataLayout) - { - m_Prototext = "node { \n" - " name: \"graphInput\" \n" - " op: \"Placeholder\" \n" - " attr { \n" - " key: \"dtype\" \n" - " value { \n" - " type: DT_FLOAT \n" - " } \n" - " } \n" - " attr { \n" - " key: \"shape\" \n" - " value { \n" - " shape { \n" - " } \n" - " } \n" - " } \n" - "} \n" - "node { \n" - " name: \"Const_1\" \n" - " op: \"Const\" \n" - " attr { \n" - " key: \"dtype\" \n" - " value { \n" - " type: DT_FLOAT \n" - " } \n" - " } \n" - " attr { \n" - " key: \"value\" \n" - " value { \n" - " tensor { \n" - " dtype: DT_FLOAT \n" - " tensor_shape { \n" - " dim { \n" - " size: 1 \n" - " } \n" - " } \n" - " float_val: 1.0 \n" - " } \n" - " } \n" - " } \n" - "} \n" - "node { \n" - " name: \"Const_2\" \n" - " op: \"Const\" \n" - " attr { \n" - " key: \"dtype\" \n" - " value { \n" - " type: DT_FLOAT \n" - " } \n" - " } \n" - " attr { \n" - " key: \"value\" \n" - " value { \n" - " tensor { \n" - " dtype: DT_FLOAT \n" - " tensor_shape { \n" - " dim { \n" - " size: 1 \n" - " } \n" - " } \n" - " float_val: 0.0 \n" - " } \n" - " } \n" - " } \n" - "} \n" - "node { \n" - " name: \"FusedBatchNormLayer/mean\" \n" - " op: \"Const\" \n" - " attr { \n" - " key: \"dtype\" \n" - " value { \n" - " type: DT_FLOAT \n" - " } \n" - " } \n" - " attr { \n" - " key: \"value\" \n" - " value { \n" - " tensor { \n" - " dtype: DT_FLOAT \n" - " tensor_shape { \n" - " dim { \n" - " size: 1 \n" - " } \n" - " } \n" - " float_val: 5.0 \n" - " } \n" - " } \n" - " } \n" - "} \n" - "node { \n" - " name: \"FusedBatchNormLayer/variance\" \n" - " op: \"Const\" \n" - " attr { \n" - " key: \"dtype\" \n" - " value { \n" - " type: DT_FLOAT \n" - " } \n" - " } \n" - " attr { \n" - " key: \"value\" \n" - " value { \n" - " tensor { \n" - " dtype: DT_FLOAT \n" - " tensor_shape { \n" - " dim { \n" - " size: 1 \n" - " } \n" - " } \n" - " float_val: 2.0 \n" - " } \n" - " } \n" - " } \n" - "} \n" - "node { \n" - " name: \"output\" \n" - " op: \"FusedBatchNorm\" \n" - " input: \"graphInput\" \n" - " input: \"Const_1\" \n" - " input: \"Const_2\" \n" - " input: \"FusedBatchNormLayer/mean\" \n" - " input: \"FusedBatchNormLayer/variance\" \n" - " attr { \n" - " key: \"T\" \n" - " value { \n" - " type: DT_FLOAT \n" - " } \n" - " } \n"; - - // NOTE: we only explicitly set data_format when it is not the default NHWC - if (dataLayout != "NHWC") - { - m_Prototext.append(" attr { \n" - " key: \"data_format\" \n" - " value { \n" - " s: \""); - m_Prototext.append(dataLayout); - m_Prototext.append("\" \n" - " } \n" - " } \n"); - } - - m_Prototext.append(" attr { \n" - " key: \"epsilon\" \n" - " value { \n" - " f: 0.0010000000475 \n" - " } \n" - " } \n" - " attr { \n" - " key: \"is_training\" \n" - " value { \n" - " b: false \n" - " } \n" - " } \n" - "} \n"); - - // Set the input shape according to the data layout - std::array<unsigned int, 4> dims; - if (dataLayout == "NHWC") - { - dims = { 1u, 3u, 3u, 1u }; - } - else // dataLayout == "NCHW" - { - dims = { 1u, 1u, 3u, 3u }; - } - - SetupSingleInputSingleOutput(armnn::TensorShape(4, dims.data()), "graphInput", "output"); - } -}; - -struct FusedBatchNormNhwcFixture : FusedBatchNormFixture -{ - FusedBatchNormNhwcFixture() : FusedBatchNormFixture("NHWC"){} -}; -BOOST_FIXTURE_TEST_CASE(ParseFusedBatchNormNhwc, FusedBatchNormNhwcFixture) -{ - RunTest<4>({ 1, 2, 3, 4, 5, 6, 7, 8, 9 }, // Input data. - { -2.8277204f, -2.12079024f, -1.4138602f, - -0.7069301f, 0.0f, 0.7069301f, - 1.4138602f, 2.12079024f, 2.8277204f }); // Expected output data. -} - -struct FusedBatchNormNchwFixture : FusedBatchNormFixture -{ - FusedBatchNormNchwFixture() : FusedBatchNormFixture("NCHW"){} -}; -BOOST_FIXTURE_TEST_CASE(ParseFusedBatchNormNchw, FusedBatchNormNchwFixture) -{ - RunTest<4>({ 1, 2, 3, 4, 5, 6, 7, 8, 9 }, // Input data. - { -2.8277204f, -2.12079024f, -1.4138602f, - -0.7069301f, 0.0f, 0.7069301f, - 1.4138602f, 2.12079024f, 2.8277204f }); // Expected output data. -} - -BOOST_AUTO_TEST_SUITE_END() diff --git a/src/armnnTfParser/test/Gather.cpp b/src/armnnTfParser/test/Gather.cpp deleted file mode 100644 index ab5fb7104d..0000000000 --- a/src/armnnTfParser/test/Gather.cpp +++ /dev/null @@ -1,184 +0,0 @@ -// -// Copyright © 2017 Arm Ltd and Contributors. All rights reserved. -// SPDX-License-Identifier: MIT -// - -#include "armnnTfParser/ITfParser.hpp" - -#include "ParserPrototxtFixture.hpp" -#include <PrototxtConversions.hpp> - -#include <boost/test/unit_test.hpp> - -BOOST_AUTO_TEST_SUITE(TensorflowParser) - -namespace { -// helper for setting the dimensions in prototxt -void dimsHelper(const std::vector<int>& dims, std::string& text){ - for(unsigned int i = 0; i < dims.size(); ++i) { - text.append(R"(dim { - size: )"); - text.append(std::to_string(dims[i])); - text.append(R"( - })"); - } -} - -// helper for converting from integer to octal representation -void octalHelper(const std::vector<int>& indicesContent, std::string& text){ - for(unsigned int i = 0; i < indicesContent.size(); ++i) { - text.append(armnnUtils::ConvertInt32ToOctalString(static_cast<int>(indicesContent[i]))); - } -} -} // namespace - -struct GatherFixture : public armnnUtils::ParserPrototxtFixture<armnnTfParser::ITfParser> -{ - GatherFixture(const armnn::TensorShape& inputShape0, - const armnn::TensorShape& inputShape1, - const std::vector<int>& input1Content, - const std::vector<int>& input0Dims, - const std::vector<int>& input1Dims, - int axis = 0) - { - m_Prototext = R"( -node { - name: "input0" - op: "Placeholder" - attr { - key: "dtype" - value { - type: DT_FLOAT - } - } - attr { - key: "shape" - value { - shape { -)"; - dimsHelper(input0Dims, m_Prototext); - - m_Prototext.append(R"( - } - } - } -} -node { - name: "input1" - op: "Const" - attr { - key: "dtype" - value { - type: DT_INT32 - } - } - attr { - key: "value" - value { - tensor { - dtype: DT_INT32 - tensor_shape { -)"); - dimsHelper(input1Dims, m_Prototext); - - m_Prototext.append(R"( - } - tensor_content: ")"); - octalHelper(input1Content, m_Prototext); - m_Prototext.append(R"(" - } - } - } -} -node { - name: "output" - op: "Gather" - input: "input0" - input: "input1" - attr { - key: "Tindices" - value { - type: DT_INT32 - } - } - attr { - key: "Tparams" - value { - type: DT_FLOAT - } - } - attr { - key: "axis" - value { - i: )"); - m_Prototext += std::to_string(axis); - - m_Prototext.append(R"( - } - } -} - )"); - - Setup({ { "input0", inputShape0 }, - { "input1", inputShape1 } }, - { "output" }); - - } -}; - - -struct GatherFixture1DParams1DIndices : public GatherFixture -{ - GatherFixture1DParams1DIndices() : GatherFixture( - { 4, 1, 1, 1 }, - { 4, 0, 0, 0 }, - { 0, 2, 1, 3 }, - { 4 }, - { 4 }, - 0) {} -}; - -struct GatherFixture1DParamsMultiDimIndices : public GatherFixture -{ - GatherFixture1DParamsMultiDimIndices() : GatherFixture( - { 4, 1, 1 }, - { 2, 2, 1, 1 }, - { 0, 1, 1, 3 }, - { 4 }, - { 2, 2 }, - 0) {} -}; - -struct GatherFixtureMultiDimParamMultiDimIndices : public GatherFixture -{ - GatherFixtureMultiDimParamMultiDimIndices() : GatherFixture( - { 5, 2, 1 }, - { 2, 1, 4 }, - { 1, 3, 0, 2 }, - { 5, 2 }, - { 2, 2 }, - 0) {} -}; - -BOOST_FIXTURE_TEST_CASE(ParseGather1DParams1DIndices, GatherFixture1DParams1DIndices) -{ - RunTest<4>({ { "input0", { 1, 2, 3, 4 } } }, - - { { "output", { 1, 3, 2, 4 } } }); -} - -BOOST_FIXTURE_TEST_CASE(ParseGather1DParamsMultiDimIndices, GatherFixture1DParamsMultiDimIndices) -{ - RunTest<4>({ { "input0", { 1, 2, 3, 4 } } }, - - { { "output", { 1, 2, 2, 4 } } }); -} - -BOOST_FIXTURE_TEST_CASE(ParseGatherMultiDimParamMultiDimIndices, GatherFixtureMultiDimParamMultiDimIndices) -{ - RunTest<4>({ { "input0", { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 } } }, - - { { "output", { 3, 4, 7, 8, 1, 2, 5, 6} } }); -} - -BOOST_AUTO_TEST_SUITE_END() diff --git a/src/armnnTfParser/test/Greater.cpp b/src/armnnTfParser/test/Greater.cpp deleted file mode 100644 index d1e793987b..0000000000 --- a/src/armnnTfParser/test/Greater.cpp +++ /dev/null @@ -1,139 +0,0 @@ -// -// Copyright © 2017 Arm Ltd. All rights reserved. -// SPDX-License-Identifier: MIT -// - -#include <boost/test/unit_test.hpp> -#include "armnnTfParser/ITfParser.hpp" -#include "ParserPrototxtFixture.hpp" - -BOOST_AUTO_TEST_SUITE(TensorflowParser) - -struct GreaterFixture : public armnnUtils::ParserPrototxtFixture<armnnTfParser::ITfParser> -{ - GreaterFixture() - { - m_Prototext = R"( -node { - name: "input0" - op: "Placeholder" - attr { - key: "dtype" - value { - type: DT_FLOAT - } - } - attr { - key: "shape" - value { - shape { - } - } - } -} -node { - name: "input1" - op: "Placeholder" - attr { - key: "dtype" - value { - type: DT_FLOAT - } - } - attr { - key: "shape" - value { - shape { - } - } - } -} -node { - name: "output" - op: "Greater" - input: "input0" - input: "input1" - attr { - key: "T" - value { - type: DT_FLOAT - } - } -} - )"; - } -}; - -BOOST_FIXTURE_TEST_CASE(ParseGreaterUnsupportedBroadcast, GreaterFixture) -{ - BOOST_REQUIRE_THROW(Setup({ { "input0", {2, 3} }, - { "input1", {1, 2, 2, 3} } }, - { "output" }), - armnn::ParseException); -} - -struct GreaterFixtureAutoSetup : public GreaterFixture -{ - GreaterFixtureAutoSetup(const armnn::TensorShape& input0Shape, - const armnn::TensorShape& input1Shape) - : GreaterFixture() - { - Setup({ { "input0", input0Shape }, - { "input1", input1Shape } }, - { "output" }); - } -}; - -struct GreaterFixtureTwoByTwo : public GreaterFixtureAutoSetup -{ - GreaterFixtureTwoByTwo() : GreaterFixtureAutoSetup({2, 2}, {2, 2}) {} -}; - -BOOST_FIXTURE_TEST_CASE(ParseGreaterTwoByTwo, GreaterFixtureTwoByTwo) -{ - RunComparisonTest<2>({ { "input0", { 1.0f, 2.0f, 3.0f, 4.0f} }, - { "input1", { 1.0f, 5.0f, 2.0f, 2.0f} } }, - { { "output", { 0, 0, 1, 1} } }); -} - -struct GreaterBroadcast1DAnd4D : public GreaterFixtureAutoSetup -{ - GreaterBroadcast1DAnd4D() : GreaterFixtureAutoSetup({1}, {1,1,2,2}) {} -}; - -BOOST_FIXTURE_TEST_CASE(ParseGreaterBroadcast1DToTwoByTwo, GreaterBroadcast1DAnd4D) -{ - RunComparisonTest<4>({ { "input0", { 2.0f } }, - { "input1", { 1.0f, 2.0f, 3.0f, 2.0f } } }, - { { "output", { 1, 0, 0, 0 } } }); -} - -struct GreaterBroadcast4DAnd1D : public GreaterFixtureAutoSetup -{ - GreaterBroadcast4DAnd1D() : GreaterFixtureAutoSetup({1,1,2,2}, {1}) {} -}; - -BOOST_FIXTURE_TEST_CASE(ParseGreaterBroadcast4DAnd1D, GreaterBroadcast4DAnd1D) -{ - RunComparisonTest<4>({ { "input0", { 1.0f, 2.0f, 3.0f, 2.0f } }, - { "input1", { 3.0f } } }, - { { "output", { 0, 0, 0, 0 } } }); -} - -struct GreaterMultiDimBroadcast : public GreaterFixtureAutoSetup -{ - GreaterMultiDimBroadcast() : GreaterFixtureAutoSetup({1,1,2,1}, {1,2,1,3}) {} -}; - -BOOST_FIXTURE_TEST_CASE(ParseGreaterMultiDimBroadcast, GreaterMultiDimBroadcast) -{ - RunComparisonTest<4>({ { "input0", { 1.0f, 2.0f } }, - { "input1", { 1.0f, 2.0f, 3.0f, - 3.0f, 2.0f, 2.0f } } }, - { { "output", { 0, 0, 0, - 1, 0, 0, - 0, 0, 0, - 0, 0, 0 } } }); -} - -BOOST_AUTO_TEST_SUITE_END() diff --git a/src/armnnTfParser/test/Identity.cpp b/src/armnnTfParser/test/Identity.cpp deleted file mode 100644 index 5b04d42b67..0000000000 --- a/src/armnnTfParser/test/Identity.cpp +++ /dev/null @@ -1,161 +0,0 @@ -// -// Copyright © 2017 Arm Ltd. All rights reserved. -// SPDX-License-Identifier: MIT -// - -#include <boost/test/unit_test.hpp> -#include "armnnTfParser/ITfParser.hpp" -#include "ParserPrototxtFixture.hpp" - -BOOST_AUTO_TEST_SUITE(TensorflowParser) - -struct IdentitySimpleFixture : public armnnUtils::ParserPrototxtFixture<armnnTfParser::ITfParser> -{ - IdentitySimpleFixture() - { - m_Prototext = "node{ " - " name: \"Placeholder\"" - " op: \"Placeholder\"" - " attr {" - " key: \"dtype\"" - " value {" - " type: DT_FLOAT" - " }" - " }" - " attr {" - " key: \"shape\"" - " value {" - " shape {" - " unknown_rank: true" - " }" - " }" - " }" - "}" - "node {" - " name: \"Identity\"" - " op: \"Identity\"" - " input: \"Placeholder\"" - " attr {" - " key: \"T\"" - " value {" - " type: DT_FLOAT" - " }" - " }" - "}"; - SetupSingleInputSingleOutput({ 4 }, "Placeholder", "Identity"); - } -}; - -BOOST_FIXTURE_TEST_CASE(IdentitySimple, IdentitySimpleFixture) -{ - RunTest<1>({ 1.0f, 2.0f, 3.0f, 4.0f }, { 1.0f, 2.0f, 3.0f, 4.0f }); -} - -struct IdentityFixture : public armnnUtils::ParserPrototxtFixture<armnnTfParser::ITfParser> -{ - IdentityFixture() - { - m_Prototext = "node{ " - " name: \"Placeholder\"" - " op: \"Placeholder\"" - " attr {" - " key: \"dtype\"" - " value {" - " type: DT_FLOAT" - " }" - " }" - " attr {" - " key: \"shape\"" - " value {" - " shape {" - " unknown_rank: true" - " }" - " }" - " }" - "}" - "node {" - " name: \"Identity\"" - " op: \"Identity\"" - " input: \"Placeholder\"" - " attr {" - " key: \"T\"" - " value {" - " type: DT_FLOAT" - " }" - " }" - "}" - "node {" - " name: \"Add\"" - " op: \"Add\"" - " input: \"Identity\"" - " input: \"Identity\"" - " attr {" - " key: \"T\"" - " value {" - " type: DT_FLOAT" - " }" - " }" - "}"; - SetupSingleInputSingleOutput({ 4 }, "Placeholder", "Add"); - } -}; - -BOOST_FIXTURE_TEST_CASE(ParseIdentity, IdentityFixture) -{ - RunTest<1>({ 1.0f, 2.0f, 3.0f, 4.0f }, { 2.0f, 4.0f, 6.0f, 8.0f }); -} - -struct IdentityChainFixture : public armnnUtils::ParserPrototxtFixture<armnnTfParser::ITfParser> -{ - IdentityChainFixture() - { - m_Prototext = "node{ " - " name: \"Placeholder\"" - " op: \"Placeholder\"" - " attr {" - " key: \"dtype\"" - " value {" - " type: DT_FLOAT" - " }" - " }" - " attr {" - " key: \"shape\"" - " value {" - " shape {" - " unknown_rank: true" - " }" - " }" - " }" - "}" - "node {" - " name: \"Identity\"" - " op: \"Identity\"" - " input: \"Placeholder\"" - " attr {" - " key: \"T\"" - " value {" - " type: DT_FLOAT" - " }" - " }" - "}" - "node {" - " name: \"Identity2\"" - " op: \"Identity\"" - " input: \"Identity\"" - " attr {" - " key: \"T\"" - " value {" - " type: DT_FLOAT" - " }" - " }" - "}"; - SetupSingleInputSingleOutput({ 4 }, "Placeholder", "Identity2"); - } -}; - -BOOST_FIXTURE_TEST_CASE(IdentityChain, IdentityChainFixture) -{ - RunTest<1>({ 1.0f, 2.0f, 3.0f, 4.0f }, { 1.0f, 2.0f, 3.0f, 4.0f }); -} - -BOOST_AUTO_TEST_SUITE_END() diff --git a/src/armnnTfParser/test/LocalResponseNormalization.cpp b/src/armnnTfParser/test/LocalResponseNormalization.cpp deleted file mode 100644 index 7a364daac2..0000000000 --- a/src/armnnTfParser/test/LocalResponseNormalization.cpp +++ /dev/null @@ -1,120 +0,0 @@ -// -// Copyright © 2017 Arm Ltd. All rights reserved. -// SPDX-License-Identifier: MIT -// - -#include <boost/test/unit_test.hpp> -#include "armnnTfParser/ITfParser.hpp" -#include "ParserPrototxtFixture.hpp" - -BOOST_AUTO_TEST_SUITE(TensorflowParser) - -struct LocalResponseNormalizationBaseFixture : public armnnUtils::ParserPrototxtFixture<armnnTfParser::ITfParser> -{ - explicit LocalResponseNormalizationBaseFixture(float alpha, float beta, float bias) - { - std::string alphaString = std::to_string(alpha); - std::string betaString = std::to_string(beta); - std::string biasString = std::to_string(bias); - - m_Prototext = "node {" - " name: \"Placeholder\"" - " op: \"Placeholder\"" - " attr {" - " key: \"dtype\"" - " value {" - " type: DT_FLOAT" - " }" - " }" - " attr {" - " key: \"shape\"" - " value {" - " shape {" - " unknown_rank: true" - " }" - " }" - " }" - "}" - "node {" - " name: \"LRN\"" - " op: \"LRN\"" - " input: \"Placeholder\"" - " attr {" - " key: \"T\"" - " value {" - " type: DT_FLOAT" - " }" - " }" - " attr {" - " key: \"alpha\"" - " value {" - " f: "; - m_Prototext.append(alphaString); - m_Prototext.append("\n" - " }" - " }" - " attr {" - " key: \"beta\"" - " value {" - " f: "); - m_Prototext.append(betaString); - m_Prototext.append("\n" - " }" - " }" - " attr {" - " key: \"bias\"" - " value {" - " f: "); - m_Prototext.append(biasString); - m_Prototext.append("\n" - " }" - " }" - " attr {" - " key: \"depth_radius\"" - " value {" - " i: 1" - " }" - " }" - "}"); - } -}; - - -struct LocalResponseNormalizationFixtureSimple : public LocalResponseNormalizationBaseFixture -{ - explicit LocalResponseNormalizationFixtureSimple() - : LocalResponseNormalizationBaseFixture(1.0f, 1.0f, 1.0f) - { - SetupSingleInputSingleOutput({ 2, 2, 2, 1 }, "Placeholder", "LRN"); - } -}; -BOOST_FIXTURE_TEST_CASE(ParseSimpleLocalResponseNormalization, LocalResponseNormalizationFixtureSimple) -{ - RunTest<4>({ 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f }, - { 0.5f, 0.4f, 0.3f, 0.23529412f, 0.1923077f, 0.16216217f, 0.14f, 0.12307692f }); -} - - -struct LocalResponseNormalizationFixture : public LocalResponseNormalizationBaseFixture -{ - explicit LocalResponseNormalizationFixture() - : LocalResponseNormalizationBaseFixture(0.5f, 1.0f, 0.5f) - { - SetupSingleInputSingleOutput({1, 3, 3, 2}, "Placeholder", "LRN"); - } -}; -BOOST_FIXTURE_TEST_CASE(ParseLocalResponseNormalization, LocalResponseNormalizationFixture) -{ - RunTest<4>({ 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, - 7.0f, 8.0f, 9.0f, 10.0f, 11.0f, 12.0f, - 13.0f, 14.0f, 15.0f, 16.0f, 17.0f, 18.0f}, - - {0.333333340f, 0.66666670f, 0.230769250f, 0.307692320f, 0.161290320f, 0.19354838f, - 0.122807020f, 0.14035088f, 0.098901100f, 0.109890110f, 0.082706770f, 0.09022556f, - 0.071038246f, 0.07650273f, 0.062240668f, 0.066390045f, 0.055374593f, 0.05863192f}); -} - - - - -BOOST_AUTO_TEST_SUITE_END() diff --git a/src/armnnTfParser/test/Maximum.cpp b/src/armnnTfParser/test/Maximum.cpp deleted file mode 100644 index 8b87b76296..0000000000 --- a/src/armnnTfParser/test/Maximum.cpp +++ /dev/null @@ -1,144 +0,0 @@ -// -// Copyright © 2017 Arm Ltd. All rights reserved. -// SPDX-License-Identifier: MIT -// - -#include <boost/test/unit_test.hpp> -#include "armnnTfParser/ITfParser.hpp" -#include "ParserPrototxtFixture.hpp" - -BOOST_AUTO_TEST_SUITE(TensorflowParser) - -struct MaximumFixture : public armnnUtils::ParserPrototxtFixture<armnnTfParser::ITfParser> -{ - MaximumFixture(const armnn::TensorShape& inputShape0, const armnn::TensorShape& inputShape1) - { - m_Prototext = R"( -node { - name: "input0" - op: "Placeholder" - attr { - key: "dtype" - value { - type: DT_FLOAT - } - } - attr { - key: "shape" - value { - shape { - } - } - } -} -node { - name: "input1" - op: "Placeholder" - attr { - key: "dtype" - value { - type: DT_FLOAT - } - } - attr { - key: "shape" - value { - shape { - } - } - } -} -node { - name: "output" - op: "Maximum" - input: "input0" - input: "input1" - attr { - key: "T" - value { - type: DT_FLOAT - } - } -} - )"; - - Setup({ { "input0", inputShape0 }, - { "input1", inputShape1 } }, - { "output" }); - } -}; - -struct MaximumFixture4D4D : public MaximumFixture -{ - MaximumFixture4D4D() : MaximumFixture({ 1, 2, 2, 3 }, { 1, 2, 2, 3 }) {} -}; - -BOOST_FIXTURE_TEST_CASE(ParseMaximum4D4D, MaximumFixture4D4D) -{ - RunTest<4>({ { "input0", { 0.0f, 1.0f, 2.0f, - 3.0f, 4.0f, 5.0f, - 6.0f, 7.0f, 8.0f, - 9.0f, 10.0f, 11.0f } }, - { "input1", { 5.0f, 1.0f, 3.0f, - 4.0f, 5.5f, 1.0f, - 2.0f, 17.0f, 18.0f, - 19.0f, 1.0f, 3.0f } } }, - { { "output", { 5.0f, 1.0f, 3.0f, - 4.0f, 5.5f, 5.0f, - 6.0f, 17.0f, 18.0f, - 19.0f, 10.0f, 11.0f } } }); -} - -struct MaximumBroadcastFixture4D4D : public MaximumFixture -{ - MaximumBroadcastFixture4D4D() : MaximumFixture({ 1, 1, 2, 1 }, { 1, 2, 1, 3 }) {} -}; - -BOOST_FIXTURE_TEST_CASE(ParseMaximumBroadcast4D4D, MaximumBroadcastFixture4D4D) -{ - RunTest<4>({ { "input0", { 2.0f, 4.0f } }, - { "input1", { 1.0f, 2.0f, 3.0f, - 4.0f, 5.0f, 6.0f } } }, - { { "output", { 2.0f, 2.0f, 3.0f, - 4.0f, 4.0f, 4.0f, - 4.0f, 5.0f, 6.0f, - 4.0f, 5.0f, 6.0f } } }); -} - -struct MaximumBroadcastFixture4D1D : public MaximumFixture -{ - MaximumBroadcastFixture4D1D() : MaximumFixture({ 1, 2, 2, 3 }, { 1 }) {} -}; - -BOOST_FIXTURE_TEST_CASE(ParseMaximumBroadcast4D1D, MaximumBroadcastFixture4D1D) -{ - RunTest<4>({ { "input0", { 0.0f, 1.0f, 2.0f, - 3.0f, 4.0f, 5.0f, - 6.0f, 7.0f, 8.0f, - 9.0f, 10.0f, 11.0f } }, - { "input1", { 5.0f } } }, - { { "output", { 5.0f, 5.0f, 5.0f, - 5.0f, 5.0f, 5.0f, - 6.0f, 7.0f, 8.0f, - 9.0f, 10.0f, 11.0f } } }); -} - -struct MaximumBroadcastFixture1D4D : public MaximumFixture -{ - MaximumBroadcastFixture1D4D() : MaximumFixture({ 1 }, { 1, 2, 2, 3 }) {} -}; - -BOOST_FIXTURE_TEST_CASE(ParseMaximumBroadcast1D4D, MaximumBroadcastFixture1D4D) -{ - RunTest<4>({ { "input0", { 3.0f } }, - { "input1", { 0.0f, 1.0f, 2.0f, - 3.0f, 4.0f, 5.0f, - 6.0f, 7.0f, 8.0f, - 9.0f, 10.0f, 11.0f } } }, - { { "output", { 3.0f, 3.0f, 3.0f, - 3.0f, 4.0f, 5.0f, - 6.0f, 7.0f, 8.0f, - 9.0f, 10.0f, 11.0f } } }); -} - -BOOST_AUTO_TEST_SUITE_END() diff --git a/src/armnnTfParser/test/MaximumForLeakyRelu.cpp b/src/armnnTfParser/test/MaximumForLeakyRelu.cpp deleted file mode 100644 index 05c5003399..0000000000 --- a/src/armnnTfParser/test/MaximumForLeakyRelu.cpp +++ /dev/null @@ -1,168 +0,0 @@ -// -// Copyright © 2017 Arm Ltd. All rights reserved. -// SPDX-License-Identifier: MIT -// - -#include <boost/test/unit_test.hpp> -#include "armnnTfParser/ITfParser.hpp" -#include "ParserPrototxtFixture.hpp" - -BOOST_AUTO_TEST_SUITE(TensorflowParser) - -struct UnsupportedMaximumFixture - : public armnnUtils::ParserPrototxtFixture<armnnTfParser::ITfParser> -{ - UnsupportedMaximumFixture() - { - m_Prototext = R"( - node { - name: "graphInput" - op: "Placeholder" - attr { - key: "dtype" - value { - type: DT_FLOAT - } - } - attr { - key: "shape" - value { - shape { - } - } - } - } - node { - name: "Maximum" - op: "Maximum" - input: "graphInput" - attr { - key: "dtype" - value { - type: DT_FLOAT - } - } - } - )"; - } -}; - -BOOST_FIXTURE_TEST_CASE(UnsupportedMaximum, UnsupportedMaximumFixture) -{ - BOOST_CHECK_THROW( - SetupSingleInputSingleOutput({ 1, 1 }, "graphInput", "Maximum"), - armnn::ParseException); -} - -struct SupportedMaximumFixture - : public armnnUtils::ParserPrototxtFixture<armnnTfParser::ITfParser> -{ - SupportedMaximumFixture(const std::string & maxInput0, - const std::string & maxInput1, - const std::string & mulInput0, - const std::string & mulInput1) - { - m_Prototext = R"( - node { - name: "graphInput" - op: "Placeholder" - attr { - key: "dtype" - value { type: DT_FLOAT } - } - attr { - key: "shape" - value { shape { } } - } - } - node { - name: "Alpha" - op: "Const" - attr { - key: "dtype" - value { type: DT_FLOAT } - } - attr { - key: "value" - value { - tensor { - dtype: DT_FLOAT - tensor_shape { - dim { size: 1 } - } - float_val: 0.1 - } - } - } - } - node { - name: "Mul" - op: "Mul" - input: ")" + mulInput0 + R"(" - input: ")" + mulInput1 + R"(" - attr { - key: "T" - value { type: DT_FLOAT } - } - } - node { - name: "Maximum" - op: "Maximum" - input: ")" + maxInput0 + R"(" - input: ")" + maxInput1 + R"(" - attr { - key: "T" - value { type: DT_FLOAT } - } - } - )"; - SetupSingleInputSingleOutput({ 1, 2 }, "graphInput", "Maximum"); - } -}; - -struct LeakyRelu_Max_MulAT_T_Fixture : public SupportedMaximumFixture -{ - LeakyRelu_Max_MulAT_T_Fixture() - : SupportedMaximumFixture("Mul","graphInput","Alpha","graphInput") {} -}; - -BOOST_FIXTURE_TEST_CASE(LeakyRelu_Max_MulAT_T, LeakyRelu_Max_MulAT_T_Fixture) -{ - RunTest<2>(std::vector<float>({-5.0, 3.0}), {-0.5, 3.0}); -} - -struct LeakyRelu_Max_T_MulAT_Fixture : public SupportedMaximumFixture -{ - LeakyRelu_Max_T_MulAT_Fixture() - : SupportedMaximumFixture("graphInput","Mul","Alpha","graphInput") {} -}; - - -BOOST_FIXTURE_TEST_CASE(LeakyRelu_Max_T_MulAT, LeakyRelu_Max_T_MulAT_Fixture) -{ - RunTest<2>(std::vector<float>({-10.0, 3.0}), {-1.0, 3.0}); -} - -struct LeakyRelu_Max_MulTA_T_Fixture : public SupportedMaximumFixture -{ - LeakyRelu_Max_MulTA_T_Fixture() - : SupportedMaximumFixture("Mul", "graphInput","graphInput","Alpha") {} -}; - -BOOST_FIXTURE_TEST_CASE(LeakyRelu_Max_MulTA_T, LeakyRelu_Max_MulTA_T_Fixture) -{ - RunTest<2>(std::vector<float>({-5.0, 3.0}), {-0.5, 3.0}); -} - -struct LeakyRelu_Max_T_MulTA_Fixture : public SupportedMaximumFixture -{ - LeakyRelu_Max_T_MulTA_Fixture() - : SupportedMaximumFixture("graphInput", "Mul", "graphInput", "Alpha") {} -}; - -BOOST_FIXTURE_TEST_CASE(LeakyRelu_Max_T_MulTA, LeakyRelu_Max_T_MulTA_Fixture) -{ - RunTest<2>(std::vector<float>({-10.0, 13.0}), {-1.0, 13.0}); -} - -BOOST_AUTO_TEST_SUITE_END() diff --git a/src/armnnTfParser/test/Mean.cpp b/src/armnnTfParser/test/Mean.cpp deleted file mode 100644 index d73682961f..0000000000 --- a/src/armnnTfParser/test/Mean.cpp +++ /dev/null @@ -1,178 +0,0 @@ -// -// Copyright © 2017 Arm Ltd. All rights reserved. -// SPDX-License-Identifier: MIT -// - -#include "armnnTfParser/ITfParser.hpp" - -#include <ParserPrototxtFixture.hpp> -#include <PrototxtConversions.hpp> - -#include <boost/test/unit_test.hpp> - -BOOST_AUTO_TEST_SUITE(TensorflowParser) - -struct MeanFixture : public armnnUtils::ParserPrototxtFixture<armnnTfParser::ITfParser> -{ - explicit MeanFixture(const armnn::TensorShape& inputShape, const armnn::TensorShape& outputShape, - const std::vector<unsigned int>& axis, bool keepDims) - { - std::string protobufAxisString; - std::vector<unsigned int> protobufAxis(axis); - - // If no axis range is specified, the reduction is applied to - // all dimensions of the input tensor - if (protobufAxis.size() == 0) - { - for (unsigned int i = 0; i < inputShape.GetNumDimensions(); ++i) - { - protobufAxis.push_back(i); - } - } - - for (unsigned int i = 0; i < protobufAxis.size(); ++i) - { - protobufAxisString.append(armnnUtils::ConvertInt32ToOctalString(static_cast<int>(protobufAxis[i]))); - } - - m_Prototext = R"(node { - name: "input" - op: "Placeholder" - attr { - key: "dtype" - value { - type: DT_FLOAT - } - } - attr { - key: "shape" - value { - shape { - } - } - } - } - node { - name: "Const" - op: "Const" - attr { - key: "dtype" - value { - type: DT_INT32 - } - } - attr { - key: "value" - value { )"; - - if (axis.size() == 1) - { - m_Prototext.append(R"( tensor { - dtype: DT_INT32 - tensor_shape { - } - int_val: )").append(std::to_string(protobufAxis[0])).append(R"( - } )"); - } - else - { - m_Prototext.append(R"( tensor { - dtype: DT_INT32 - tensor_shape { - dim { - size: 2 - } - } - tensor_content: ")").append(protobufAxisString).append(R"(" - } )"); - } - - m_Prototext.append(R"( } - } - } - node { - name: "output" - op: "Mean" - input: "input" - input: "Const" - attr { - key: "T" - value { - type: DT_FLOAT - } - } - attr { - key: "Tidx" - value { - type: DT_INT32 - } - } - attr { - key: "keep_dims" - value { - b: )").append(keepDims ? "true" : "false").append(R"( - } - } - })"); - - SetupSingleInputSingleOutput(inputShape, outputShape, "input", "output"); - } -}; - -struct MeanNoAxisNoKeepDimsFixture: MeanFixture -{ - MeanNoAxisNoKeepDimsFixture() : MeanFixture({ 2, 3 }, { 1 }, {}, false) {} -}; - -struct MeanWithAxis0NoKeepDimsFixture: MeanFixture -{ - MeanWithAxis0NoKeepDimsFixture() : MeanFixture({ 2, 3 }, { 3 }, { 0 }, false) {} -}; - -struct MeanWithAxis1NoKeepDimsFixture: MeanFixture -{ - MeanWithAxis1NoKeepDimsFixture() : MeanFixture({ 2, 3 }, { 2 }, { 1 }, false) {} -}; - -struct MeanWithAxis0KeepDimsFixture: MeanFixture -{ - MeanWithAxis0KeepDimsFixture() : MeanFixture({ 2, 3 }, { 1, 3 }, { 0 }, true) {} -}; - -struct MeanWithAxis1KeepDimsFixture: MeanFixture -{ - MeanWithAxis1KeepDimsFixture() : MeanFixture({ 2, 3 }, { 2, 1 }, { 1 }, true) {} -}; - - -BOOST_FIXTURE_TEST_CASE(MeanNoAxisNoKeepDims, MeanNoAxisNoKeepDimsFixture) -{ - RunTest<1>({ { "input", { 1.0f, 1.0f, 1.0f, 2.0f, 2.0f, 2.0f } } }, - { { "output", { 1.5f } } }); -} - -BOOST_FIXTURE_TEST_CASE(MeanWithAxis0NoKeepDims, MeanWithAxis0NoKeepDimsFixture) -{ - RunTest<1>({ { "input", { 1.0f, 1.0f, 1.0f, 2.0f, 2.0f, 2.0f } } }, - { { "output", { 1.5f, 1.5f, 1.5f } } }); -} - -BOOST_FIXTURE_TEST_CASE(MeanWithAxis1NoKeepDims, MeanWithAxis1NoKeepDimsFixture) -{ - RunTest<1>({ { "input", { 1.0f, 1.0f, 1.0f, 2.0f, 2.0f, 2.0f } } }, - { { "output", { 1.f, 2.f } } }); -} - -BOOST_FIXTURE_TEST_CASE(MeanWithAxis0KeepDims, MeanWithAxis0KeepDimsFixture) -{ - RunTest<2>({ { "input", { 1.0f, 1.0f, 1.0f, 2.0f, 2.0f, 2.0f } } }, - { { "output", { 1.5f, 1.5f, 1.5f } } }); -} - -BOOST_FIXTURE_TEST_CASE(MeanWithAxis1KeepDims, MeanWithAxis1KeepDimsFixture) -{ - RunTest<2>({ { "input", { 1.0f, 1.0f, 1.0f, 2.0f, 2.0f, 2.0f } } }, - { { "output", { 1.f, 2.f } } }); -} - -BOOST_AUTO_TEST_SUITE_END() diff --git a/src/armnnTfParser/test/Minimum.cpp b/src/armnnTfParser/test/Minimum.cpp deleted file mode 100644 index feb86a17d6..0000000000 --- a/src/armnnTfParser/test/Minimum.cpp +++ /dev/null @@ -1,165 +0,0 @@ -// -// Copyright © 2017 Arm Ltd. All rights reserved. -// SPDX-License-Identifier: MIT -// - -#include <boost/test/unit_test.hpp> -#include "armnnTfParser/ITfParser.hpp" -#include "ParserPrototxtFixture.hpp" - -BOOST_AUTO_TEST_SUITE(TensorflowParser) - -struct MinimumFixture : public armnnUtils::ParserPrototxtFixture<armnnTfParser::ITfParser> -{ - MinimumFixture() - { - m_Prototext = R"( - node { - name: "input0" - op: "Placeholder" - attr { - key: "dtype" - value { - type: DT_FLOAT - } - } - attr { - key: "shape" - value { - shape { - } - } - } - } - node { - name: "input1" - op: "Placeholder" - attr { - key: "dtype" - value { - type: DT_FLOAT - } - } - attr { - key: "shape" - value { - shape { - } - } - } - } - node { - name: "output" - op: "Minimum" - input: "input0" - input: "input1" - attr { - key: "T" - value { - type: DT_FLOAT - } - } - } - )"; - } -}; - -BOOST_FIXTURE_TEST_CASE(ParseMininumUnsupportedBroadcast, MinimumFixture) -{ - BOOST_REQUIRE_THROW(Setup({ { "input0", {2, 3} }, - { "input1", {1, 2, 2, 3} } }, - { "output" }), - armnn::ParseException); -} - -struct MinimumFixtureAutoSetup : public MinimumFixture -{ - MinimumFixtureAutoSetup(const armnn::TensorShape& input0Shape, - const armnn::TensorShape& input1Shape) - : MinimumFixture() - { - Setup({ { "input0", input0Shape }, - { "input1", input1Shape } }, - { "output" }); - } -}; - -struct MinimumFixture4D : public MinimumFixtureAutoSetup -{ - MinimumFixture4D() - : MinimumFixtureAutoSetup({1, 2, 2, 3}, {1, 2, 2, 3}) {} -}; - -BOOST_FIXTURE_TEST_CASE(ParseMinimum4D, MinimumFixture4D) -{ - RunTest<4>({ { "input0", { 0.0f, 1.0f, 2.0f, - 3.0f, 4.0f, 5.0f, - 6.0f, 7.0f, 8.0f, - 9.0f, 10.0f, 11.0f } }, - { "input1", { 0.0f, 0.0f, 0.0f, - 5.0f, 5.0f, 5.0f, - 7.0f, 7.0f, 7.0f, - 9.0f, 9.0f, 9.0f } } }, - { { "output", { 0.0f, 0.0f, 0.0f, - 3.0f, 4.0f, 5.0f, - 6.0f, 7.0f, 7.0f, - 9.0f, 9.0f, 9.0f } } }); -} - -struct MinimumBroadcastFixture4D : public MinimumFixtureAutoSetup -{ - MinimumBroadcastFixture4D() - : MinimumFixtureAutoSetup({1, 1, 2, 1}, {1, 2, 1, 3}) {} -}; - -BOOST_FIXTURE_TEST_CASE(ParseMinimumBroadcast4D, MinimumBroadcastFixture4D) -{ - RunTest<4>({ { "input0", { 2.0f, - 4.0f } }, - { "input1", { 1.0f, 2.0f, 3.0f, - 4.0f, 5.0f, 6.0f } } }, - { { "output", { 1.0f, 2.0f, 2.0f, - 1.0f, 2.0f, 3.0f, - 2.0f, 2.0f, 2.0f, - 4.0f, 4.0f, 4.0f } } }); -} - -struct MinimumBroadcastFixture4D1D : public MinimumFixtureAutoSetup -{ - MinimumBroadcastFixture4D1D() - : MinimumFixtureAutoSetup({1, 2, 2, 3}, {1}) {} -}; - -BOOST_FIXTURE_TEST_CASE(ParseMinimumBroadcast4D1D, MinimumBroadcastFixture4D1D) -{ - RunTest<4>({ { "input0", { 0.0f, 1.0f, 2.0f, - 3.0f, 4.0f, 5.0f, - 6.0f, 7.0f, 8.0f, - 9.0f, 10.0f, 11.0f } }, - { "input1", { 5.0f } } }, - { { "output", { 0.0f, 1.0f, 2.0f, - 3.0f, 4.0f, 5.0f, - 5.0f, 5.0f, 5.0f, - 5.0f, 5.0f, 5.0f } } }); -} - -struct MinimumBroadcastFixture1D4D : public MinimumFixtureAutoSetup -{ - MinimumBroadcastFixture1D4D() - : MinimumFixtureAutoSetup({3}, {1, 2, 2, 3}) {} -}; - -BOOST_FIXTURE_TEST_CASE(ParseMinimumBroadcast1D4D, MinimumBroadcastFixture1D4D) -{ - RunTest<4>({ { "input0", { 5.0f, 6.0f, 7.0f } }, - { "input1", { 0.0f, 1.0f, 2.0f, - 3.0f, 4.0f, 5.0f, - 6.0f, 7.0f, 8.0f, - 9.0f, 10.0f, 11.0f } } }, - { { "output", { 0.0f, 1.0f, 2.0f, - 3.0f, 4.0f, 5.0f, - 5.0f, 6.0f, 7.0f, - 5.0f, 6.0f, 7.0f } } }); -} - -BOOST_AUTO_TEST_SUITE_END() diff --git a/src/armnnTfParser/test/MultiOutput.cpp b/src/armnnTfParser/test/MultiOutput.cpp deleted file mode 100644 index 15879c2bed..0000000000 --- a/src/armnnTfParser/test/MultiOutput.cpp +++ /dev/null @@ -1,144 +0,0 @@ -// -// Copyright © 2017 Arm Ltd. All rights reserved. -// SPDX-License-Identifier: MIT -// - -#include <boost/test/unit_test.hpp> -#include "armnnTfParser/ITfParser.hpp" -#include "ParserPrototxtFixture.hpp" - -BOOST_AUTO_TEST_SUITE(TensorflowParser) - -struct MultiOutMatchFixture : public armnnUtils::ParserPrototxtFixture<armnnTfParser::ITfParser> -{ - MultiOutMatchFixture() - { - m_Prototext = R"( -node { - name: "input" - op: "Placeholder" - attr { - key: "dtype" - value { - type: DT_FLOAT - } - } - attr { - key: "shape" - value { - shape { - } - } - } -} -node { - name: "softmax1" - op: "Softmax" - input: "input:0" - attr { - key: "T" - value { - type: DT_FLOAT - } - } -} - )"; - SetupSingleInputSingleOutput({ 1, 7 }, "input", "softmax1"); - } -}; - -BOOST_FIXTURE_TEST_CASE(MultiOutMatch, MultiOutMatchFixture) -{ - // Note that the point of this test is to verify the parsing went well. - // Here we make sure the softmax has really connected to the input layer. - RunTest<2>({ 0, 0, 10000, 0, 0, 0, 0 }, { 0, 0, 1, 0, 0, 0, 0 }); -} - -struct MultiOutFailFixture : public armnnUtils::ParserPrototxtFixture<armnnTfParser::ITfParser> -{ - MultiOutFailFixture() - { - m_Prototext = R"( -node { - name: "input" - op: "Placeholder" - attr { - key: "dtype" - value { - type: DT_FLOAT - } - } - attr { - key: "shape" - value { - shape { - } - } - } -} -node { - name: "softmax1" - op: "Softmax" - input: "input:1" - attr { - key: "T" - value { - type: DT_FLOAT - } - } -} - )"; - BOOST_CHECK_THROW(SetupSingleInputSingleOutput({ 1, 7 }, "input", "softmax1"), armnn::ParseException); - } -}; - -BOOST_FIXTURE_TEST_CASE(MultiOutFail, MultiOutFailFixture) -{ - // Not running the graph because this is expected to throw an exception during parsing. -} - -struct MultiOutInvalidFixture : public armnnUtils::ParserPrototxtFixture<armnnTfParser::ITfParser> -{ - MultiOutInvalidFixture() - { - m_Prototext = R"( -node { - name: "input" - op: "Placeholder" - attr { - key: "dtype" - value { - type: DT_FLOAT - } - } - attr { - key: "shape" - value { - shape { - } - } - } -} -node { - name: "softmax1" - op: "Softmax" - input: "input:-1" - attr { - key: "T" - value { - type: DT_FLOAT - } - } -} - )"; - BOOST_CHECK_THROW(SetupSingleInputSingleOutput({ 1, 7 }, "input", "softmax1"), armnn::ParseException); - } -}; - -BOOST_FIXTURE_TEST_CASE(MultiOutInvalid, MultiOutInvalidFixture) -{ - // Not running the graph because this is expected to throw an exception during parsing. -} - - -BOOST_AUTO_TEST_SUITE_END()
\ No newline at end of file diff --git a/src/armnnTfParser/test/Multiplication.cpp b/src/armnnTfParser/test/Multiplication.cpp deleted file mode 100644 index 01a7c79b6c..0000000000 --- a/src/armnnTfParser/test/Multiplication.cpp +++ /dev/null @@ -1,172 +0,0 @@ -// -// Copyright © 2017 Arm Ltd. All rights reserved. -// SPDX-License-Identifier: MIT -// - -#include <boost/test/unit_test.hpp> -#include "armnnTfParser/ITfParser.hpp" -#include "ParserPrototxtFixture.hpp" - -BOOST_AUTO_TEST_SUITE(TensorflowParser) - -struct MultiplicationFixture : public armnnUtils::ParserPrototxtFixture<armnnTfParser::ITfParser> -{ - MultiplicationFixture() - { - m_Prototext = "node { \n" - " name: \"graphInput\" \n" - " op: \"Placeholder\" \n" - " attr { \n" - " key: \"dtype\" \n" - " value { \n" - " type: DT_FLOAT \n" - " } \n" - " } \n" - " attr { \n" - " key: \"shape\" \n" - " value { \n" - " shape { \n" - " } \n" - " } \n" - " } \n" - " } \n" - " node { \n" - " name: \"softmax1\" \n" - " op: \"Softmax\" \n" - " input: \"graphInput\" \n" - " attr { \n" - " key: \"T\" \n" - " value { \n" - " type: DT_FLOAT \n" - " } \n" - " } \n" - " }\n" - " node {\n" - " name: \"softmax2\"\n" - " op : \"Softmax\"\n" - " input: \"graphInput\"\n" - " attr { \n" - " key: \"T\" \n" - " value { \n" - " type: DT_FLOAT \n" - " } \n" - " } \n" - " }\n" - " node {\n" - " name: \"multiplication\"\n" - " op : \"Mul\"\n" - " input: \"softmax1\"\n" - " input: \"softmax2\"\n" - " attr { \n" - " key: \"T\" \n" - " value { \n" - " type: DT_FLOAT \n" - " } \n" - " } \n" - " }\n"; - - SetupSingleInputSingleOutput({ 1, 7 }, "graphInput", "multiplication"); - } -}; - -BOOST_FIXTURE_TEST_CASE(ParseMultiplication, MultiplicationFixture) -{ - RunTest<2>({ 0, 0, 10000, 0, 0, 0, 0 }, { 0, 0, 1, 0, 0, 0, 0 }); -} - -struct MultiplicationBroadcastFixture : public armnnUtils::ParserPrototxtFixture<armnnTfParser::ITfParser> -{ - MultiplicationBroadcastFixture(const armnn::TensorShape& inputShape0, const armnn::TensorShape& inputShape1) - { - m_Prototext = R"( -node { - name: "input0" - op: "Placeholder" - attr { - key: "dtype" - value { - type: DT_FLOAT - } - } - attr { - key: "shape" - value { - shape { - } - } - } -} -node { - name: "input1" - op: "Placeholder" - attr { - key: "dtype" - value { - type: DT_FLOAT - } - } - attr { - key: "shape" - value { - shape { - } - } - } -} -node { - name: "output" - op: "Mul" - input: "input0" - input: "input1" - attr { - key: "T" - value { - type: DT_FLOAT - } - } -} - )"; - - Setup({ { "input0", inputShape0 }, - { "input1", inputShape1 } }, - { "output" }); - } -}; - -struct MultiplicationBroadcastFixture4D1D : public MultiplicationBroadcastFixture -{ - MultiplicationBroadcastFixture4D1D() : MultiplicationBroadcastFixture({ 1, 2, 2, 3 }, { 1 }) {} -}; - -BOOST_FIXTURE_TEST_CASE(ParseMultiplicationBroadcast4D1D, MultiplicationBroadcastFixture4D1D) -{ - RunTest<4>({ { "input0", { 0.0f, 1.0f, 2.0f, - 3.0f, 4.0f, 5.0f, - 6.0f, 7.0f, 8.0f, - 9.0f, 10.0f, 11.0f } }, - { "input1", { 5.0f } } }, - { { "output", { 0.0f, 5.0f, 10.0f, - 15.0f, 20.0f, 25.0f, - 30.0f, 35.0f, 40.0f, - 45.0f, 50.0f, 55.0f } } }); -} - -struct MultiplicationBroadcastFixture1D4D : public MultiplicationBroadcastFixture -{ - MultiplicationBroadcastFixture1D4D() : MultiplicationBroadcastFixture({ 1 }, { 1, 2, 2, 3 }) {} -}; - -BOOST_FIXTURE_TEST_CASE(ParseMultiplicationBroadcast1D4D, MultiplicationBroadcastFixture1D4D) -{ - RunTest<4>({ { "input0", { 3.0f } }, - { "input1", { 0.0f, 1.0f, 2.0f, - 3.0f, 4.0f, 5.0f, - 6.0f, 7.0f, 8.0f, - 9.0f, 10.0f, 11.0f } } }, - { { "output", { 0.0f, 3.0f, 6.0f, - 9.0f, 12.0f, 15.0f, - 18.0f, 21.0f, 24.0f, - 27.0f, 30.0f, 33.0f } } }); -} - -BOOST_AUTO_TEST_SUITE_END() diff --git a/src/armnnTfParser/test/Pad.cpp b/src/armnnTfParser/test/Pad.cpp deleted file mode 100644 index 8bfe970dfa..0000000000 --- a/src/armnnTfParser/test/Pad.cpp +++ /dev/null @@ -1,107 +0,0 @@ -// -// Copyright © 2017 Arm Ltd. All rights reserved. -// SPDX-License-Identifier: MIT -// - -#include <boost/test/unit_test.hpp> -#include "armnnTfParser/ITfParser.hpp" -#include "ParserPrototxtFixture.hpp" - -BOOST_AUTO_TEST_SUITE(TensorflowParser) - -struct PadFixture : public armnnUtils::ParserPrototxtFixture<armnnTfParser::ITfParser> -{ - PadFixture() { - m_Prototext = "node {\n" - " name: \"input\"\n" - " op: \"Placeholder\"\n" - " attr {\n" - " key: \"dtype\"\n" - " value {\n" - " type: DT_FLOAT\n" - " }\n" - " }\n" - " attr {\n" - " key: \"shape\"\n" - " value {\n" - " shape {\n" - " dim {\n" - " size: -1\n" - " }\n" - " dim {\n" - " size: 2\n" - " }\n" - " dim {\n" - " size: 2\n" - " }\n" - " dim {\n" - " size: 2\n" - " }\n" - " }\n" - " }\n" - " }\n" - "}\n" - "node {\n" - " name: \"Pad/paddings\"\n" - " op: \"Const\"\n" - " attr {\n" - " key: \"dtype\"\n" - " value {\n" - " type: DT_INT32\n" - " }\n" - " }\n" - " attr {\n" - " key: \"value\"\n" - " value {\n" - " tensor {\n" - " dtype: DT_INT32\n" - " tensor_shape {\n" - " dim {\n" - " size: 4\n" - " }\n" - " dim {\n" - " size: 2\n" - " }\n" - " }\n" - " tensor_content: \"\\000\\000\\000\\000\\000\\000\\000\\000" - "\\001\\000\\000\\000\\001\\000\\000\\000" - "\\001\\000\\000\\000\\001\\000\\000\\000" - "\\000\\000\\000\\000\\000\\000\\000\\000\"\n" - " }\n" - " }\n" - " }\n" - "}\n" - "node {\n" - " name: \"Pad\"\n" - " op: \"Pad\"\n" - " input: \"input\"\n" - " input: \"Pad/paddings\"\n" - " attr {\n" - " key: \"T\"\n" - " value {\n" - " type: DT_FLOAT\n" - " }\n" - " }\n" - " attr {\n" - " key: \"Tpaddings\"\n" - " value {\n" - " type: DT_INT32\n" - " }\n" - " }\n" - "}"; - - SetupSingleInputSingleOutput({1, 2, 2, 2}, "input", "Pad"); - } -}; - -BOOST_FIXTURE_TEST_CASE(ParsePad, PadFixture) -{ - RunTest<4>({ 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f }, - { 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, - 0.0f, 0.0f, 1.0f, 2.0f, 3.0f, 4.0f, 0.0f, 0.0f, - 0.0f, 0.0f, 5.0f, 6.0f, 7.0f, 8.0f, 0.0f, 0.0f, - 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, - }); -} - -BOOST_AUTO_TEST_SUITE_END()
\ No newline at end of file diff --git a/src/armnnTfParser/test/PassThru.cpp b/src/armnnTfParser/test/PassThru.cpp deleted file mode 100644 index 736e13c1ad..0000000000 --- a/src/armnnTfParser/test/PassThru.cpp +++ /dev/null @@ -1,52 +0,0 @@ -// -// Copyright © 2017 Arm Ltd. All rights reserved. -// SPDX-License-Identifier: MIT -// -#include <boost/test/unit_test.hpp> -#include "armnnTfParser/ITfParser.hpp" -#include "ParserPrototxtFixture.hpp" - -BOOST_AUTO_TEST_SUITE(TensorflowParser) - -struct PassThruFixture : public armnnUtils::ParserPrototxtFixture<armnnTfParser::ITfParser> -{ - PassThruFixture() - { - m_Prototext = "node {\n" - " name: \"Placeholder\"\n" - " op: \"Placeholder\"\n" - " attr {\n" - " key: \"dtype\"\n" - " value {\n" - " type: DT_FLOAT\n" - " }\n" - " }\n" - " attr {\n" - " key: \"shape\"\n" - " value {\n" - " shape {\n" - " }\n" - " }\n" - " }\n" - "}\n"; - SetupSingleInputSingleOutput({ 1, 7 }, "Placeholder", "Placeholder"); - } -}; - -BOOST_FIXTURE_TEST_CASE(ValidateOutput, PassThruFixture) -{ - BOOST_TEST(m_Parser->GetNetworkOutputBindingInfo("Placeholder").second.GetNumDimensions() == 2); - BOOST_TEST(m_Parser->GetNetworkOutputBindingInfo("Placeholder").second.GetShape()[0] == 1); - BOOST_TEST(m_Parser->GetNetworkOutputBindingInfo("Placeholder").second.GetShape()[1] == 7); -} - -BOOST_FIXTURE_TEST_CASE(RunGraph, PassThruFixture) -{ - armnn::TensorInfo inputTensorInfo = m_Parser->GetNetworkInputBindingInfo("Placeholder").second; - auto input = MakeRandomTensor<float, 2>(inputTensorInfo, 378346); - std::vector<float> inputVec; - inputVec.assign(input.data(), input.data() + input.num_elements()); - RunTest<2>(inputVec, inputVec); // The passthru network should output the same as the input. -} - -BOOST_AUTO_TEST_SUITE_END() diff --git a/src/armnnTfParser/test/Pooling.cpp b/src/armnnTfParser/test/Pooling.cpp deleted file mode 100644 index f6de44c95f..0000000000 --- a/src/armnnTfParser/test/Pooling.cpp +++ /dev/null @@ -1,186 +0,0 @@ -// -// Copyright © 2017 Arm Ltd. All rights reserved. -// SPDX-License-Identifier: MIT -// - -#include <boost/test/unit_test.hpp> -#include "armnnTfParser/ITfParser.hpp" -#include "ParserPrototxtFixture.hpp" - -BOOST_AUTO_TEST_SUITE(TensorflowParser) - -struct Pooling2dFixture : public armnnUtils::ParserPrototxtFixture<armnnTfParser::ITfParser> -{ - explicit Pooling2dFixture(const char* poolingtype, std::string dataLayout, std::string paddingOption) - { - m_Prototext = "node {\n" - " name: \"Placeholder\"\n" - " op: \"Placeholder\"\n" - " attr {\n" - " key: \"dtype\"\n" - " value {\n" - " type: DT_FLOAT\n" - " }\n" - " }\n" - " attr {\n" - " key: \"value\"\n" - " value {\n" - " tensor {\n" - " dtype: DT_FLOAT\n" - " tensor_shape {\n" - " }\n" - " }\n" - " }\n" - " }\n" - " }\n" - "node {\n" - " name: \""; - m_Prototext.append(poolingtype); - m_Prototext.append("\"\n" - " op: \""); - m_Prototext.append(poolingtype); - m_Prototext.append("\"\n" - " input: \"Placeholder\"\n" - " attr {\n" - " key: \"T\"\n" - " value {\n" - " type: DT_FLOAT\n" - " }\n" - " }\n" - " attr {\n" - " key: \"data_format\"\n" - " value {\n" - " s: \""); - m_Prototext.append(dataLayout); - m_Prototext.append("\"\n" - " }\n" - " }\n" - " attr {\n" - " key: \"ksize\"\n" - " value {\n" - " list {\n" - - " i: 1\n"); - if(dataLayout == "NHWC") - { - m_Prototext.append(" i: 2\n" - " i: 2\n" - " i: 1\n"); - } - else - { - m_Prototext.append(" i: 1\n" - " i: 2\n" - " i: 2\n"); - } - m_Prototext.append( - " }\n" - " }\n" - " }\n" - " attr {\n" - " key: \"padding\"\n" - " value {\n" - " s: \""); - m_Prototext.append(paddingOption); - m_Prototext.append( - "\"\n" - " }\n" - " }\n" - " attr {\n" - " key: \"strides\"\n" - " value {\n" - " list {\n" - " i: 1\n" - " i: 1\n" - " i: 1\n" - " i: 1\n" - " }\n" - " }\n" - " }\n" - "}\n"); - - if(dataLayout == "NHWC") - { - SetupSingleInputSingleOutput({ 1, 2, 2, 1 }, "Placeholder", poolingtype); - } - else - { - SetupSingleInputSingleOutput({ 1, 1, 2, 2 }, "Placeholder", poolingtype); - } - } -}; - - -struct MaxPoolFixtureNhwcValid : Pooling2dFixture -{ - MaxPoolFixtureNhwcValid() : Pooling2dFixture("MaxPool", "NHWC", "VALID") {} -}; -BOOST_FIXTURE_TEST_CASE(ParseMaxPoolNhwcValid, MaxPoolFixtureNhwcValid) -{ - RunTest<4>({1.0f, 2.0f, 3.0f, -4.0f}, {3.0f}); -} - -struct MaxPoolFixtureNchwValid : Pooling2dFixture -{ - MaxPoolFixtureNchwValid() : Pooling2dFixture("MaxPool", "NCHW", "VALID") {} -}; -BOOST_FIXTURE_TEST_CASE(ParseMaxPoolNchwValid, MaxPoolFixtureNchwValid) -{ - RunTest<4>({1.0f, 2.0f, 3.0f, -4.0f}, {3.0f}); -} - -struct MaxPoolFixtureNhwcSame : Pooling2dFixture -{ - MaxPoolFixtureNhwcSame() : Pooling2dFixture("MaxPool", "NHWC", "SAME") {} -}; -BOOST_FIXTURE_TEST_CASE(ParseMaxPoolNhwcSame, MaxPoolFixtureNhwcSame) -{ - RunTest<4>({1.0f, 2.0f, 3.0f, -4.0f}, {3.0f, 2.0f, 3.0f, -4.0f}); -} - -struct MaxPoolFixtureNchwSame : Pooling2dFixture -{ - MaxPoolFixtureNchwSame() : Pooling2dFixture("MaxPool", "NCHW", "SAME") {} -}; -BOOST_FIXTURE_TEST_CASE(ParseMaxPoolNchwSame, MaxPoolFixtureNchwSame) -{ - RunTest<4>({1.0f, 2.0f, 3.0f, -4.0f}, {3.0f, 2.0f, 3.0f, -4.0f}); -} - -struct AvgPoolFixtureNhwcValid : Pooling2dFixture -{ - AvgPoolFixtureNhwcValid() : Pooling2dFixture("AvgPool", "NHWC", "VALID") {} -}; -BOOST_FIXTURE_TEST_CASE(ParseAvgPoolNhwcValid, AvgPoolFixtureNhwcValid) -{ - RunTest<4>({1.0f, 2.0f, 3.0f, 4.0f}, {2.5f}); -} - -struct AvgPoolFixtureNchwValid : Pooling2dFixture -{ - AvgPoolFixtureNchwValid() : Pooling2dFixture("AvgPool", "NCHW", "VALID") {} -}; -BOOST_FIXTURE_TEST_CASE(ParseAvgPoolNchwValid, AvgPoolFixtureNchwValid) -{ - RunTest<4>({1.0f, 2.0f, 3.0f, 4.0f}, {2.5f}); -} - -struct AvgPoolFixtureNhwcSame : Pooling2dFixture -{ - AvgPoolFixtureNhwcSame() : Pooling2dFixture("AvgPool", "NHWC", "SAME") {} -}; -BOOST_FIXTURE_TEST_CASE(ParseAvgPoolNhwcSame, AvgPoolFixtureNhwcSame) -{ - RunTest<4>({1.0f, 2.0f, 3.0f, 4.0f}, {2.5f, 3.0f, 3.5f, 4.0f}); -} - -struct AvgPoolFixtureNchwSame : Pooling2dFixture -{ - AvgPoolFixtureNchwSame() : Pooling2dFixture("AvgPool", "NCHW", "SAME") {} -}; -BOOST_FIXTURE_TEST_CASE(ParseAvgPoolNchwSame, AvgPoolFixtureNchwSame) -{ - RunTest<4>({1.0f, 2.0f, 3.0f, 4.0f}, {2.5f, 3.0f, 3.5f, 4.0f}); -} - -BOOST_AUTO_TEST_SUITE_END() diff --git a/src/armnnTfParser/test/RealDiv.cpp b/src/armnnTfParser/test/RealDiv.cpp deleted file mode 100644 index 952590e001..0000000000 --- a/src/armnnTfParser/test/RealDiv.cpp +++ /dev/null @@ -1,169 +0,0 @@ -// -// Copyright © 2017 Arm Ltd. All rights reserved. -// SPDX-License-Identifier: MIT -// - -#include <boost/test/unit_test.hpp> -#include "armnnTfParser/ITfParser.hpp" -#include "ParserPrototxtFixture.hpp" - -BOOST_AUTO_TEST_SUITE(TensorflowParser) - -struct DivisionFixture : public armnnUtils::ParserPrototxtFixture<armnnTfParser::ITfParser> -{ - DivisionFixture() - { - m_Prototext = "node { \n" - " name: \"graphInput\" \n" - " op: \"Placeholder\" \n" - " attr { \n" - " key: \"dtype\" \n" - " value { \n" - " type: DT_FLOAT \n" - " } \n" - " } \n" - " attr { \n" - " key: \"shape\" \n" - " value { \n" - " shape { \n" - " } \n" - " } \n" - " } \n" - " } \n" - " node { \n" - " name: \"softmax1\" \n" - " op: \"Softmax\" \n" - " input: \"graphInput\" \n" - " attr { \n" - " key: \"T\" \n" - " value { \n" - " type: DT_FLOAT \n" - " } \n" - " } \n" - " }\n" - " node {\n" - " name: \"softmax2\"\n" - " op : \"Softmax\"\n" - " input: \"graphInput\"\n" - " attr { \n" - " key: \"T\" \n" - " value { \n" - " type: DT_FLOAT \n" - " } \n" - " } \n" - " }\n" - " node {\n" - " name: \"division\"\n" - " op : \"RealDiv\"\n" - " input: \"softmax1\"\n" - " input: \"softmax2\"\n" - " attr { \n" - " key: \"T\" \n" - " value { \n" - " type: DT_FLOAT \n" - " } \n" - " } \n" - " }\n"; - - SetupSingleInputSingleOutput({ 4, 1 }, "graphInput", "division"); - } -}; - -BOOST_FIXTURE_TEST_CASE(ParseDivision, DivisionFixture) -{ - RunTest<2>({ 2, 1.0f, 3, 1 }, { 1, 1.0f, 1, 1}); -} - -struct DivisionBroadcastFixture : public armnnUtils::ParserPrototxtFixture<armnnTfParser::ITfParser> -{ - DivisionBroadcastFixture(const armnn::TensorShape& inputShape0, const armnn::TensorShape& inputShape1) - { - m_Prototext = R"( - node { - name: "input0" - op: "Placeholder" - attr { - key: "dtype" - value { - type: DT_FLOAT - } - } - attr { - key: "shape" - value { - shape { - } - } - } - } - node { - name: "input1" - op: "Placeholder" - attr { - key: "dtype" - value { - type: DT_FLOAT - } - } - attr { - key: "shape" - value { - shape { - } - } - } - } - node { - name: "output" - op: "RealDiv" - input: "input0" - input: "input1" - attr { - key: "T" - value { - type: DT_FLOAT - } - } - } - )"; - - Setup({ { "input0", inputShape0 }, - { "input1", inputShape1 } }, - { "output" }); - } -}; -struct DivisionBroadcastFixture4D1D : public DivisionBroadcastFixture -{ - DivisionBroadcastFixture4D1D() : DivisionBroadcastFixture({ 1, 2, 2, 3 }, { 1 }) {} -}; - -BOOST_FIXTURE_TEST_CASE(ParseDivisionBroadcast4D1D, DivisionBroadcastFixture4D1D) -{ - RunTest<4>({ { "input0", { 0.0f, 100.0f, 2.0f, - 3.0f, 250.0f, 15.0f, - 33.0f, 60.0f, 5.0f, - 35.0f, 10.0f, 55.0f } }, - { "input1", { 5.0f } } }, - { { "output", { 0, 20.0f, 0.4f, - 0.6f, 50.0f, 3.0f, - 6.6f, 12.0f, 1.0f, - 7.0f, 2.0f, 11.0f } } }); -} - -BOOST_FIXTURE_TEST_CASE(ParseDivideByZeroBroadcast4D1D, DivisionBroadcastFixture4D1D) -{ - float Inf = std::numeric_limits<float>::infinity(); - float NaN = std::numeric_limits<float>::quiet_NaN(); - - RunTest<4>({ { "input0", { 0.0f, -100.0f, 2.0f, - 3.0f, -250.0f, 15.0f, - 33.0f, -0, 5.0f, - 35.0f, -10.0f, 55.0f } }, - { "input1", { 0 } } }, - { { "output", { NaN, -Inf, Inf, - Inf, -Inf, Inf, - Inf, NaN, Inf, - Inf, -Inf, Inf } } }); -} - -BOOST_AUTO_TEST_SUITE_END() diff --git a/src/armnnTfParser/test/Reshape.cpp b/src/armnnTfParser/test/Reshape.cpp deleted file mode 100644 index cbb3a75011..0000000000 --- a/src/armnnTfParser/test/Reshape.cpp +++ /dev/null @@ -1,85 +0,0 @@ -// -// Copyright © 2017 Arm Ltd. All rights reserved. -// SPDX-License-Identifier: MIT -// - -#include <boost/test/unit_test.hpp> -#include "armnnTfParser/ITfParser.hpp" -#include "ParserPrototxtFixture.hpp" - -BOOST_AUTO_TEST_SUITE(TensorflowParser) - -struct ReshapeFixture : public armnnUtils::ParserPrototxtFixture<armnnTfParser::ITfParser> -{ - ReshapeFixture() - { - m_Prototext = "node { \n" - " name: \"graphInput\" \n" - " op: \"Placeholder\" \n" - " attr { \n" - " key: \"dtype\" \n" - " value { \n" - " type: DT_FLOAT \n" - " } \n" - " } \n" - " attr { \n" - " key: \"shape\" \n" - " value { \n" - " shape { \n" - " } \n" - " } \n" - " } \n" - " } \n" - "node { \n" - " name: \"Reshape/shape\" \n" - " op: \"Const\" \n" - " attr { \n" - " key: \"dtype\" \n" - " value { \n" - " type: DT_INT32 \n" - " } \n" - " } \n" - " attr { \n" - " key: \"value\" \n" - " value { \n" - " tensor { \n" - " dtype: DT_INT32 \n" - " tensor_shape { \n" - " dim { \n" - " size: 2 \n" - " } \n" - " } \n" - " tensor_content: \"\\002\\000\\000\\000\\002\\000\\000\\000\" \n" - " } \n" - " } \n" - " } \n" - "} \n" - "node { \n" - " name: \"Reshape\" \n" - " op: \"Reshape\" \n" - " input: \"graphInput\" \n" - " input: \"Reshape/shape\" \n" - " attr { \n" - " key: \"T\" \n" - " value { \n" - " type: DT_FLOAT \n" - " } \n" - " } \n" - " attr { \n" - " key: \"Tshape\" \n" - " value { \n" - " type: DT_INT32 \n" - " } \n" - " } \n" - "} \n"; - - SetupSingleInputSingleOutput({1, 4}, "graphInput", "Reshape"); - } -}; - -BOOST_FIXTURE_TEST_CASE(ParseReshape, ReshapeFixture) -{ - RunTest<2>({ 0.0f, 1.0f, 2.0f, 3.0f }, { 0.0f, 1.0f, 2.0f, 3.0f }); -} - -BOOST_AUTO_TEST_SUITE_END() diff --git a/src/armnnTfParser/test/ResizeBilinear.cpp b/src/armnnTfParser/test/ResizeBilinear.cpp deleted file mode 100644 index d9741ee784..0000000000 --- a/src/armnnTfParser/test/ResizeBilinear.cpp +++ /dev/null @@ -1,114 +0,0 @@ -// -// Copyright © 2017 Arm Ltd. All rights reserved. -// SPDX-License-Identifier: MIT -// - -#include <boost/test/unit_test.hpp> -#include "armnnTfParser/ITfParser.hpp" -#include "ParserPrototxtFixture.hpp" - -BOOST_AUTO_TEST_SUITE(TensorflowParser) - -struct ResizeBilinearFixture : public armnnUtils::ParserPrototxtFixture<armnnTfParser::ITfParser> -{ - ResizeBilinearFixture() - { - m_Prototext = R"( -node { - name: "graphInput" - op: "Placeholder" - attr { - key: "dtype" - value { - type: DT_FLOAT - } - } - attr { - key: "value" - value { - tensor { - dtype: DT_FLOAT - tensor_shape { - dim { - size: 1 - } - dim { - size: 3 - } - dim { - size: 3 - } - dim { - size: 1 - } - } - tensor_content: -"\000\000\000\000\000\000\200?\000\000\000@\000\000@@\000\000\200@\000\000\240@\000\000\300@\000\000\340@\000\000\000A" - } - } - } -} -node { - name: "resizeBilinearLayer/size" - op: "Const" - attr { - key: "dtype" - value { - type: DT_INT32 - } - } - attr { - key: "value" - value { - tensor { - dtype: DT_INT32 - tensor_shape { - dim { - size: 2 - } - } - tensor_content: "\005\000\000\000\005\000\000\000" - } - } - } -} -node { - name: "resizeBilinearLayer" - op: "ResizeBilinear" - input: "graphInput" - input: "resizeBilinearLayer/size" - attr { - key: "T" - value { - type: DT_FLOAT - } - } - attr { - key: "align_corners" - value { - b: false - } - } -} - )"; - - SetupSingleInputSingleOutput({ 1, 3, 3, 1 }, "graphInput", "resizeBilinearLayer"); - } -}; - -BOOST_FIXTURE_TEST_CASE(ParseResizeBilinear, ResizeBilinearFixture) -{ - RunTest<4>(// Input data. - { 0.0f, 1.0f, 2.0f, - 3.0f, 4.0f, 5.0f, - 6.0f, 7.0f, 8.0f }, - // Expected output data. - { 0.0f, 0.6f, 1.2f, 1.8f, 2.0f, - 1.8f, 2.4f, 3.0f, 3.6f, 3.8f, - 3.6f, 4.2f, 4.8f, 5.4f, 5.6f, - 5.4f, 6.0f, 6.6f, 7.2f, 7.4f, - 6.0f, 6.6f, 7.2f, 7.8f, 8.0f }); - -} - -BOOST_AUTO_TEST_SUITE_END() diff --git a/src/armnnTfParser/test/Rsqrt.cpp b/src/armnnTfParser/test/Rsqrt.cpp deleted file mode 100644 index 6924c060a6..0000000000 --- a/src/armnnTfParser/test/Rsqrt.cpp +++ /dev/null @@ -1,59 +0,0 @@ -// -// Copyright © 2017 Arm Ltd. All rights reserved. -// SPDX-License-Identifier: MIT -// - -#include <boost/test/unit_test.hpp> -#include "armnnTfParser/ITfParser.hpp" -#include "ParserPrototxtFixture.hpp" - -BOOST_AUTO_TEST_SUITE(TensorflowParser) - -struct RsqrtFixture : public armnnUtils::ParserPrototxtFixture<armnnTfParser::ITfParser> -{ - RsqrtFixture() - { - m_Prototext = "node {\n" - " name: \"input\"\n" - " op: \"Placeholder\"\n" - " attr {\n" - " key: \"dtype\"\n" - " value {\n" - " type: DT_FLOAT\n" - " }\n" - " }\n" - " attr {\n" - " key: \"shape\"\n" - " value {\n" - " shape {\n" - " }\n" - " }\n" - " }\n" - "}\n" - "node {\n" - " name: \"Rsqrt\"\n" - " op: \"Rsqrt\"\n" - " input: \"input\"\n" - " attr {\n" - " key: \"T\"\n" - " value {\n" - " type: DT_FLOAT\n" - " }\n" - " }\n" - "}\n"; - - SetupSingleInputSingleOutput({ 2, 2 }, "input", "Rsqrt"); - } -}; - -BOOST_FIXTURE_TEST_CASE(ParseRsqrt, RsqrtFixture) -{ - RunTest<2>({ 1.f, 4.f, 16.f, 25.f }, { 1.f, 0.5f, 0.25f, 0.2f }); -} - -BOOST_FIXTURE_TEST_CASE(ParseRsqrtZeroNegative, RsqrtFixture) -{ - RunTest<2>({ 0.f, -0.f, -25.f, -16.f }, { INFINITY, -INFINITY, -NAN, -NAN }); -} - -BOOST_AUTO_TEST_SUITE_END()
\ No newline at end of file diff --git a/src/armnnTfParser/test/Shape.cpp b/src/armnnTfParser/test/Shape.cpp deleted file mode 100644 index 52fe9c8951..0000000000 --- a/src/armnnTfParser/test/Shape.cpp +++ /dev/null @@ -1,93 +0,0 @@ -// -// Copyright © 2017 Arm Ltd. All rights reserved. -// SPDX-License-Identifier: MIT -// - -#include <boost/test/unit_test.hpp> -#include "armnnTfParser/ITfParser.hpp" -#include "ParserPrototxtFixture.hpp" - -BOOST_AUTO_TEST_SUITE(TensorflowParser) - -struct ShapeFixture : public armnnUtils::ParserPrototxtFixture<armnnTfParser::ITfParser> -{ - ShapeFixture() - { - m_Prototext = - "node { \n" - " name: \"Placeholder\" \n" - " op: \"Placeholder\" \n" - " attr { \n" - " key: \"dtype\" \n" - " value { \n" - " type: DT_FLOAT \n" - " } \n" - " } \n" - " attr { \n" - " key: \"shape\" \n" - " value { \n" - " shape { \n" - " dim { \n" - " size: 1 \n" - " } \n" - " dim { \n" - " size: 1 \n" - " } \n" - " dim { \n" - " size: 1 \n" - " } \n" - " dim { \n" - " size: 4 \n" - " } \n" - " } \n" - " } \n" - " } \n" - "} \n" - "node { \n" - " name: \"shapeTest\" \n" - " op: \"Shape\" \n" - " input: \"Placeholder\" \n" - " attr { \n" - " key: \"T\" \n" - " value { \n" - " type: DT_FLOAT \n" - " } \n" - " } \n" - " attr { \n" - " key: \"out_type\" \n" - " value { \n" - " type: DT_INT32 \n" - " } \n" - " } \n" - "} \n" - "node { \n" - " name: \"Reshape\" \n" - " op: \"Reshape\" \n" - " input: \"Placeholder\" \n" - " input: \"shapeTest\" \n" - " attr { \n" - " key: \"T\" \n" - " value { \n" - " type: DT_FLOAT \n" - " } \n" - " } \n" - " attr { \n" - " key: \"Tshape\" \n" - " value { \n" - " type: DT_INT32 \n" - " } \n" - " } \n" - "} \n"; - - SetupSingleInputSingleOutput({1, 4}, "Placeholder", "Reshape"); - } -}; - -BOOST_FIXTURE_TEST_CASE(ParseShape, ShapeFixture) -{ - // Note: the test's output cannot be an int32 const layer, because ARMNN only supports u8 and float layers. - // For that reason I added a reshape layer which reshapes the input to its original dimensions. - RunTest<2>({ 0.0f, 1.0f, 2.0f, 3.0f }, { 0.0f, 1.0f, 2.0f, 3.0f }); -} - -BOOST_AUTO_TEST_SUITE_END() diff --git a/src/armnnTfParser/test/Softmax.cpp b/src/armnnTfParser/test/Softmax.cpp deleted file mode 100644 index df304b6880..0000000000 --- a/src/armnnTfParser/test/Softmax.cpp +++ /dev/null @@ -1,55 +0,0 @@ -// -// Copyright © 2017 Arm Ltd. All rights reserved. -// SPDX-License-Identifier: MIT -// - -#include <boost/test/unit_test.hpp> -#include "armnnTfParser/ITfParser.hpp" -#include "ParserPrototxtFixture.hpp" - -BOOST_AUTO_TEST_SUITE(TensorflowParser) - -struct SoftmaxFixture : public armnnUtils::ParserPrototxtFixture<armnnTfParser::ITfParser> -{ - SoftmaxFixture() - { - m_Prototext = "node {\n" - " name: \"blah\"\n" - " op: \"Placeholder\"\n" - " attr {\n" - " key: \"dtype\"\n" - " value {\n" - " type: DT_FLOAT\n" - " }\n" - " }\n" - " attr {\n" - " key: \"shape\"\n" - " value {\n" - " shape {\n" - " }\n" - " }\n" - " }\n" - "}\n" - "node {\n" - " name: \"blah2\"\n" - " op: \"Softmax\"\n" - " input: \"blah\"\n" - " attr {\n" - " key: \"T\"\n" - " value {\n" - " type: DT_FLOAT\n" - " }\n" - " }\n" - "}\n"; - - SetupSingleInputSingleOutput({ 1, 7 }, "blah", "blah2"); - } -}; - -BOOST_FIXTURE_TEST_CASE(ParseSoftmax, SoftmaxFixture) -{ - RunTest<2>({ 0, 0, 10000, 0, 0, 0, 0 }, { 0, 0, 1, 0, 0, 0, 0 }); -} - - -BOOST_AUTO_TEST_SUITE_END() diff --git a/src/armnnTfParser/test/Split.cpp b/src/armnnTfParser/test/Split.cpp deleted file mode 100644 index eeef90a625..0000000000 --- a/src/armnnTfParser/test/Split.cpp +++ /dev/null @@ -1,398 +0,0 @@ -// -// Copyright © 2017 Arm Ltd. All rights reserved. -// SPDX-License-Identifier: MIT -// - -#include "armnnTfParser/ITfParser.hpp" -#include "ParserPrototxtFixture.hpp" - -#include <armnn/utility/IgnoreUnused.hpp> - -#include <boost/test/unit_test.hpp> - -BOOST_AUTO_TEST_SUITE(TensorflowParser) - -struct SplitFixture : public armnnUtils::ParserPrototxtFixture<armnnTfParser::ITfParser> -{ - SplitFixture(bool withDimZero=false) { - m_Prototext = R"( - node { - name: "graphInput" - op: "Placeholder" - attr { - key: "dtype" - value { - type: DT_FLOAT - } - } - attr { - key: "shape" - value { - shape { - } - } - } - } - node { - name: "graphInput2" - op: "Placeholder" - attr { - key: "dtype" - value { - type: DT_FLOAT - } - } - attr { - key: "shape" - value { - shape { - } - } - } - } - node { - name: "multiplication" - op : "Mul" - input: "graphInput" - input: "graphInput2" - attr { - key: "T" - value { - type: DT_FLOAT - } - } - } - node { - name: "SplitInput" - op: "Const" - attr { - key: "dtype" - value { - type: DT_INT32 - } - } - attr { - key: "value" - value { - tensor { - dtype: DT_INT32 - tensor_shape { - } - int_val: )"; - - if(withDimZero) - { - m_Prototext += std::to_string(3); - } - else - { - m_Prototext += std::to_string(1); - } - - m_Prototext += R"( - } - } - } - } - node { - name: "Split" - op: "Split" )"; - if(withDimZero) - { - m_Prototext += "input: \"SplitInput\"\n"; - m_Prototext += "input: \"multiplication\"\n"; - } - else - { - m_Prototext += "input: \"graphInput\"\n"; - m_Prototext += "input: \"SplitInput\"\n"; - } - m_Prototext += R"( - attr { - key: "num_split" - value { - i: 2 - } - } - } - node { - name: "Relu_1" - op: "Relu" - input: "Split:0" - attr { - key: "T" - value { - type: DT_FLOAT - } - } - } - node { - name: "Relu_2" - op: "Relu" - input:"Split:1" - attr { - key: "T" - value { - type: DT_FLOAT - } - } - } )"; - - Setup( { { "graphInput", { 1, 2, 2 , 2} } , { "graphInput2", { 1, 2, 2 , 2} }}, - { "Relu_1", "Relu_2" }); - } -}; - -struct InputFirstSplitFixture : SplitFixture -{ - InputFirstSplitFixture() : SplitFixture(true) {} -}; - -BOOST_FIXTURE_TEST_CASE(ParseAxisOneSplitTwo, SplitFixture) -{ - BOOST_TEST( - (m_Parser->GetNetworkOutputBindingInfo("Relu_1").second.GetShape() == armnn::TensorShape({ 1, 1, 2, 2 }))); - - BOOST_TEST( - (m_Parser->GetNetworkOutputBindingInfo("Relu_2").second.GetShape() == armnn::TensorShape({ 1, 1, 2, 2 }))); - - RunTest<4>({ { "graphInput", { -1.0f, -0.5f, 1.25f, -3.0f, 0.0f, 0.5f, -0.75f, 1.75f } } }, - { { "Relu_1", { 0.0f, 0.0f, 1.25f, 0.0f } }, - { "Relu_2", { 0.0f, 0.5f, 0.0f, 1.75f } } }); -} - -BOOST_FIXTURE_TEST_CASE(ParseSplit, InputFirstSplitFixture) -{ - - BOOST_TEST( - (m_Parser->GetNetworkOutputBindingInfo("Relu_1").second.GetShape() == armnn::TensorShape({ 1, 2, 2, 1 }))); - - BOOST_TEST( - (m_Parser->GetNetworkOutputBindingInfo("Relu_2").second.GetShape() == armnn::TensorShape({ 1, 2, 2, 1 }))); - - RunTest<4>({ { "graphInput", { -1.0f, -0.5f, 1.25f, -3.0f, 0.0f, 0.5f, -0.75f , 1.75f } } , - { "graphInput2", { -1.0f, -0.5f, 1.25f, -3.0f, 0.0f, 0.5f, -0.75f , 1.75f } } }, - { { "Relu_1", { 1.0f, 1.5625f, 0, 0.5625f } }, - { "Relu_2", { 0.25, 9.0f, 0.25f, 3.0625f } } }); -} - -struct SplitLastDimFixture : public armnnUtils::ParserPrototxtFixture<armnnTfParser::ITfParser> -{ - SplitLastDimFixture(bool withDimZero=false) { - armnn::IgnoreUnused(withDimZero); - m_Prototext = R"( - node { - name: "Placeholder" - op: "Placeholder" - attr { - key: "dtype" - value { - type: DT_FLOAT - } - } - attr { - key: "shape" - value { - shape { - dim { - size: 1 - } - dim { - size: 2 - } - dim { - size: 2 - } - dim { - size: 3 - } - } - } - } - } - node { - name: "Const" - op: "Const" - attr { - key: "dtype" - value { - type: DT_INT32 - } - } - attr { - key: "value" - value { - tensor { - dtype: DT_INT32 - tensor_shape { - } - int_val: 3 - } - } - } - } - node { - name: "split/split_dim" - op: "Const" - attr { - key: "dtype" - value { - type: DT_INT32 - } - } - attr { - key: "value" - value { - tensor { - dtype: DT_INT32 - tensor_shape { - } - int_val: 3 - } - } - } - } - node { - name: "split" - op: "Split" - input: "split/split_dim" - input: "Placeholder" - attr { - key: "T" - value { - type: DT_FLOAT - } - } - attr { - key: "num_split" - value { - i: 3 - } - } - } - node { - name: "sub0/y" - op: "Const" - attr { - key: "dtype" - value { - type: DT_FLOAT - } - } - attr { - key: "value" - value { - tensor { - dtype: DT_FLOAT - tensor_shape { - } - float_val: 3.0 - } - } - } - } - node { - name: "sub0" - op: "Sub" - input: "split" - input: "sub0/y" - attr { - key: "T" - value { - type: DT_FLOAT - } - } - } - node { - name: "sub1/y" - op: "Const" - attr { - key: "dtype" - value { - type: DT_FLOAT - } - } - attr { - key: "value" - value { - tensor { - dtype: DT_FLOAT - tensor_shape { - } - float_val: 2.0 - } - } - } - } - node { - name: "sub1" - op: "Sub" - input: "split:1" - input: "sub1/y" - attr { - key: "T" - value { - type: DT_FLOAT - } - } - } - node { - name: "sub2/y" - op: "Const" - attr { - key: "dtype" - value { - type: DT_FLOAT - } - } - attr { - key: "value" - value { - tensor { - dtype: DT_FLOAT - tensor_shape { - } - float_val: 1.0 - } - } - } - } - node { - name: "sub2" - op: "Sub" - input: "split:2" - input: "sub2/y" - attr { - key: "T" - value { - type: DT_FLOAT - } - } - } - versions { - producer: 27 - } )"; - - Setup( { { "Placeholder", { 1, 2, 2 , 3} } }, - { "sub0", "sub1", "sub2" }); - } -}; - -BOOST_FIXTURE_TEST_CASE(SplitLastDimTest, SplitLastDimFixture) -{ - BOOST_TEST( - (m_Parser->GetNetworkOutputBindingInfo("sub0").second.GetShape() == armnn::TensorShape({ 1, 2, 2, 1 }))); - - BOOST_TEST( - (m_Parser->GetNetworkOutputBindingInfo("sub1").second.GetShape() == armnn::TensorShape({ 1, 2, 2, 1 }))); - - BOOST_TEST( - (m_Parser->GetNetworkOutputBindingInfo("sub2").second.GetShape() == armnn::TensorShape({ 1, 2, 2, 1 }))); - - RunTest<4>({ { "Placeholder", { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f, 9.0f, 10.0f, 11.0f, 12.0f } } }, - { { "sub0", { -2.0f, 1.0f, 4.0f, 7.0f } }, - { "sub1", { 0.0f, 3.0f, 6.0f, 9.0f } }, - { "sub2", { 2.0f, 5.0f, 8.0f, 11.0f } } }); -} - -BOOST_AUTO_TEST_SUITE_END() diff --git a/src/armnnTfParser/test/Squeeze.cpp b/src/armnnTfParser/test/Squeeze.cpp deleted file mode 100644 index e02a5947a5..0000000000 --- a/src/armnnTfParser/test/Squeeze.cpp +++ /dev/null @@ -1,107 +0,0 @@ -// -// Copyright © 2017 Arm Ltd. All rights reserved. -// SPDX-License-Identifier: MIT -// - -#include <boost/test/unit_test.hpp> -#include "armnnTfParser/ITfParser.hpp" -#include "ParserPrototxtFixture.hpp" - -BOOST_AUTO_TEST_SUITE(TensorflowParser) - -template <bool withDimZero, bool withDimOne> -struct SqueezeFixture : public armnnUtils::ParserPrototxtFixture<armnnTfParser::ITfParser> -{ - SqueezeFixture() - { - m_Prototext = - "node { \n" - " name: \"graphInput\" \n" - " op: \"Placeholder\" \n" - " attr { \n" - " key: \"dtype\" \n" - " value { \n" - " type: DT_FLOAT \n" - " } \n" - " } \n" - " attr { \n" - " key: \"shape\" \n" - " value { \n" - " shape { \n" - " } \n" - " } \n" - " } \n" - " } \n" - "node { \n" - " name: \"Squeeze\" \n" - " op: \"Squeeze\" \n" - " input: \"graphInput\" \n" - " attr { \n" - " key: \"T\" \n" - " value { \n" - " type: DT_FLOAT \n" - " } \n" - " } \n" - " attr { \n" - " key: \"squeeze_dims\" \n" - " value { \n" - " list {\n"; - - if (withDimZero) - { - m_Prototext += "i:0\n"; - } - - if (withDimOne) - { - m_Prototext += "i:1\n"; - } - - m_Prototext += - " } \n" - " } \n" - " } \n" - "} \n"; - - SetupSingleInputSingleOutput({ 1, 1, 2, 2 }, "graphInput", "Squeeze"); - } -}; - -typedef SqueezeFixture<false, false> ImpliedDimensionsSqueezeFixture; -typedef SqueezeFixture<true, false> ExplicitDimensionZeroSqueezeFixture; -typedef SqueezeFixture<false, true> ExplicitDimensionOneSqueezeFixture; -typedef SqueezeFixture<true, true> ExplicitDimensionsSqueezeFixture; - -BOOST_FIXTURE_TEST_CASE(ParseImplicitSqueeze, ImpliedDimensionsSqueezeFixture) -{ - BOOST_TEST((m_Parser->GetNetworkOutputBindingInfo("Squeeze").second.GetShape() == - armnn::TensorShape({2,2}))); - RunTest<2>({ 1.0f, 2.0f, 3.0f, 4.0f }, - { 1.0f, 2.0f, 3.0f, 4.0f }); -} - -BOOST_FIXTURE_TEST_CASE(ParseDimensionZeroSqueeze, ExplicitDimensionZeroSqueezeFixture) -{ - BOOST_TEST((m_Parser->GetNetworkOutputBindingInfo("Squeeze").second.GetShape() == - armnn::TensorShape({1,2,2}))); - RunTest<3>({ 1.0f, 2.0f, 3.0f, 4.0f }, - { 1.0f, 2.0f, 3.0f, 4.0f }); -} - -BOOST_FIXTURE_TEST_CASE(ParseDimensionOneSqueeze, ExplicitDimensionOneSqueezeFixture) -{ - BOOST_TEST((m_Parser->GetNetworkOutputBindingInfo("Squeeze").second.GetShape() == - armnn::TensorShape({1,2,2}))); - RunTest<3>({ 1.0f, 2.0f, 3.0f, 4.0f }, - { 1.0f, 2.0f, 3.0f, 4.0f }); -} - -BOOST_FIXTURE_TEST_CASE(ParseExplicitDimensionsSqueeze, ExplicitDimensionsSqueezeFixture) -{ - BOOST_TEST((m_Parser->GetNetworkOutputBindingInfo("Squeeze").second.GetShape() == - armnn::TensorShape({2,2}))); - RunTest<2>({ 1.0f, 2.0f, 3.0f, 4.0f }, - { 1.0f, 2.0f, 3.0f, 4.0f }); -} - -BOOST_AUTO_TEST_SUITE_END() diff --git a/src/armnnTfParser/test/Stack.cpp b/src/armnnTfParser/test/Stack.cpp deleted file mode 100644 index b28991713d..0000000000 --- a/src/armnnTfParser/test/Stack.cpp +++ /dev/null @@ -1,174 +0,0 @@ -// -// Copyright © 2020 Arm Ltd. All rights reserved. -// SPDX-License-Identifier: MIT -// - -#include "armnnTfParser/ITfParser.hpp" -#include "ParserPrototxtFixture.hpp" - -#include <PrototxtConversions.hpp> - -#include <boost/test/unit_test.hpp> - -BOOST_AUTO_TEST_SUITE(TensorflowParser) - -struct StackFixture : public armnnUtils::ParserPrototxtFixture<armnnTfParser::ITfParser> -{ - explicit StackFixture(const armnn::TensorShape& inputShape0, - const armnn::TensorShape& inputShape1, - int axis = 0) - { - m_Prototext = R"( - node { - name: "input0" - op: "Placeholder" - attr { - key: "dtype" - value { - type: DT_FLOAT - } - } - attr { - key: "shape" - value { - shape { - } - } - } - } - node { - name: "input1" - op: "Placeholder" - attr { - key: "dtype" - value { - type: DT_FLOAT - } - } - attr { - key: "shape" - value { - shape { - } - } - } - } - node { - name: "output" - op: "Stack" - input: "input0" - input: "input1" - attr { - key: "axis" - value { - i: )"; - m_Prototext += std::to_string(axis); - m_Prototext += R"( - } - } - })"; - - Setup({{"input0", inputShape0 }, - {"input1", inputShape1 }}, {"output"}); - } -}; - -struct Stack3DFixture : StackFixture -{ - Stack3DFixture() : StackFixture({ 3, 2, 3 }, { 3, 2, 3 }, 3 ) {} -}; - -BOOST_FIXTURE_TEST_CASE(Stack3D, Stack3DFixture) -{ - - RunTest<4>({ { "input0", { 1, 2, 3, - 4, 5, 6, - - 7, 8, 9, - 10, 11, 12, - - 13, 14, 15, - 16, 17, 18 } }, - { "input1", { 19, 20, 21, - 22, 23, 24, - - 25, 26, 27, - 28, 29, 30, - - 31, 32, 33, - 34, 35, 36 } } }, - { { "output", { 1, 19, - 2, 20, - 3, 21, - - 4, 22, - 5, 23, - 6, 24, - - 7, 25, - 8, 26, - 9, 27, - - 10, 28, - 11, 29, - 12, 30, - - 13, 31, - 14, 32, - 15, 33, - - 16, 34, - 17, 35, - 18, 36 } } }); -} - -struct Stack3DNegativeAxisFixture : StackFixture -{ - Stack3DNegativeAxisFixture() : StackFixture({ 3, 2, 3 }, { 3, 2, 3 }, -1 ) {} -}; - -BOOST_FIXTURE_TEST_CASE(Stack3DNegativeAxis, Stack3DNegativeAxisFixture) -{ - - RunTest<4>({ { "input0", { 1, 2, 3, - 4, 5, 6, - - 7, 8, 9, - 10, 11, 12, - - 13, 14, 15, - 16, 17, 18 } }, - { "input1", { 19, 20, 21, - 22, 23, 24, - - 25, 26, 27, - 28, 29, 30, - - 31, 32, 33, - 34, 35, 36 } } }, - { { "output", { 1, 19, - 2, 20, - 3, 21, - - 4, 22, - 5, 23, - 6, 24, - - 7, 25, - 8, 26, - 9, 27, - - 10, 28, - 11, 29, - 12, 30, - - 13, 31, - 14, 32, - 15, 33, - - 16, 34, - 17, 35, - 18, 36 } } }); -} - -BOOST_AUTO_TEST_SUITE_END() diff --git a/src/armnnTfParser/test/StridedSlice.cpp b/src/armnnTfParser/test/StridedSlice.cpp deleted file mode 100644 index 340f3a49ff..0000000000 --- a/src/armnnTfParser/test/StridedSlice.cpp +++ /dev/null @@ -1,283 +0,0 @@ -// -// Copyright © 2020 Arm Ltd. All rights reserved. -// SPDX-License-Identifier: MIT -// - -#include "armnnTfParser/ITfParser.hpp" - -#include "ParserPrototxtFixture.hpp" -#include <PrototxtConversions.hpp> - -#include <boost/test/unit_test.hpp> - -BOOST_AUTO_TEST_SUITE(TensorflowParser) - -namespace { -// helper for setting the dimensions in prototxt -void shapeHelper(const armnn::TensorShape& shape, std::string& text){ - for(unsigned int i = 0; i < shape.GetNumDimensions(); ++i) { - text.append(R"(dim { - size: )"); - text.append(std::to_string(shape[i])); - text.append(R"( - })"); - } -} - -// helper for converting from integer to octal representation -void octalHelper(const std::vector<int>& content, std::string& text){ - for (unsigned int i = 0; i < content.size(); ++i) - { - text.append(armnnUtils::ConvertInt32ToOctalString(static_cast<int>(content[i]))); - } -} -} // namespace - -struct StridedSliceFixture : public armnnUtils::ParserPrototxtFixture<armnnTfParser::ITfParser> -{ - StridedSliceFixture(const armnn::TensorShape& inputShape, - const std::vector<int>& beginData, - const std::vector<int>& endData, - const std::vector<int>& stridesData, - int beginMask = 0, - int endMask = 0, - int ellipsisMask = 0, - int newAxisMask = 0, - int shrinkAxisMask = 0) - { - m_Prototext = R"( - node { - name: "input" - op: "Placeholder" - attr { - key: "dtype" - value { - type: DT_FLOAT - } - } - attr { - key: "shape" - value { - shape {)"; - shapeHelper(inputShape, m_Prototext); - m_Prototext.append(R"( - } - } - } - } - node { - name: "begin" - op: "Const" - attr { - key: "dtype" - value { - type: DT_INT32 - } - } - attr { - key: "value" - value { - tensor { - dtype: DT_INT32 - tensor_shape { - dim { - size: )"); - m_Prototext += std::to_string(beginData.size()); - m_Prototext.append(R"( - } - } - tensor_content: ")"); - octalHelper(beginData, m_Prototext); - m_Prototext.append(R"(" - } - } - } - } - node { - name: "end" - op: "Const" - attr { - key: "dtype" - value { - type: DT_INT32 - } - } - attr { - key: "value" - value { - tensor { - dtype: DT_INT32 - tensor_shape { - dim { - size: )"); - m_Prototext += std::to_string(endData.size()); - m_Prototext.append(R"( - } - } - tensor_content: ")"); - octalHelper(endData, m_Prototext); - m_Prototext.append(R"(" - } - } - } - } - node { - name: "strides" - op: "Const" - attr { - key: "dtype" - value { - type: DT_INT32 - } - } - attr { - key: "value" - value { - tensor { - dtype: DT_INT32 - tensor_shape { - dim { - size: )"); - m_Prototext += std::to_string(stridesData.size()); - m_Prototext.append(R"( - } - } - tensor_content: ")"); - octalHelper(stridesData, m_Prototext); - m_Prototext.append(R"(" - } - } - } - } - node { - name: "output" - op: "StridedSlice" - input: "input" - input: "begin" - input: "end" - input: "strides" - attr { - key: "begin_mask" - value { - i: )"); - m_Prototext += std::to_string(beginMask); - m_Prototext.append(R"( - } - } - attr { - key: "end_mask" - value { - i: )"); - m_Prototext += std::to_string(endMask); - m_Prototext.append(R"( - } - } - attr { - key: "ellipsis_mask" - value { - i: )"); - m_Prototext += std::to_string(ellipsisMask); - m_Prototext.append(R"( - } - } - attr { - key: "new_axis_mask" - value { - i: )"); - m_Prototext += std::to_string(newAxisMask); - m_Prototext.append(R"( - } - } - attr { - key: "shrink_axis_mask" - value { - i: )"); - m_Prototext += std::to_string(shrinkAxisMask); - m_Prototext.append(R"( - } - } - })"); - - Setup({ { "input", inputShape } }, { "output" }); - } -}; - -struct StridedSlice4DFixture : StridedSliceFixture -{ - StridedSlice4DFixture() : StridedSliceFixture({ 3, 2, 3, 1 }, // inputShape - { 1, 0, 0, 0 }, // beginData - { 2, 2, 3, 1 }, // endData - { 1, 1, 1, 1 } // stridesData - ) {} -}; - -BOOST_FIXTURE_TEST_CASE(StridedSlice4D, StridedSlice4DFixture) -{ - RunTest<4>( - {{"input", { 1.0f, 1.0f, 1.0f, 2.0f, 2.0f, 2.0f, - 3.0f, 3.0f, 3.0f, 4.0f, 4.0f, 4.0f, - 5.0f, 5.0f, 5.0f, 6.0f, 6.0f, 6.0f }}}, - {{"output", { 3.0f, 3.0f, 3.0f, 4.0f, 4.0f, 4.0f }}}); -} - -struct StridedSlice4DReverseFixture : StridedSliceFixture -{ - - StridedSlice4DReverseFixture() : StridedSliceFixture({ 3, 2, 3, 1 }, // inputShape - { 1, -1, 0, 0 }, // beginData - { 2, -3, 3, 1 }, // endData - { 1, -1, 1, 1 } // stridesData - ) {} -}; - -BOOST_FIXTURE_TEST_CASE(StridedSlice4DReverse, StridedSlice4DReverseFixture) -{ - RunTest<4>( - {{"input", { 1.0f, 1.0f, 1.0f, 2.0f, 2.0f, 2.0f, - 3.0f, 3.0f, 3.0f, 4.0f, 4.0f, 4.0f, - 5.0f, 5.0f, 5.0f, 6.0f, 6.0f, 6.0f }}}, - {{"output", { 4.0f, 4.0f, 4.0f, 3.0f, 3.0f, 3.0f }}}); -} - -struct StridedSliceSimpleStrideFixture : StridedSliceFixture -{ - StridedSliceSimpleStrideFixture() : StridedSliceFixture({ 3, 2, 3, 1 }, // inputShape - { 0, 0, 0, 0 }, // beginData - { 3, 2, 3, 1 }, // endData - { 2, 2, 2, 1 } // stridesData - ) {} -}; - -BOOST_FIXTURE_TEST_CASE(StridedSliceSimpleStride, StridedSliceSimpleStrideFixture) -{ - RunTest<4>( - {{"input", { 1.0f, 1.0f, 1.0f, 2.0f, 2.0f, 2.0f, - 3.0f, 3.0f, 3.0f, 4.0f, 4.0f, 4.0f, - 5.0f, 5.0f, 5.0f, 6.0f, 6.0f, 6.0f }}}, - {{"output", { 1.0f, 1.0f, - 5.0f, 5.0f }}}); -} - -struct StridedSliceSimpleRangeMaskFixture : StridedSliceFixture -{ - StridedSliceSimpleRangeMaskFixture() : StridedSliceFixture({ 3, 2, 3, 1 }, // inputShape - { 1, 1, 1, 1 }, // beginData - { 1, 1, 1, 1 }, // endData - { 1, 1, 1, 1 }, // stridesData - (1 << 4) - 1, // beginMask - (1 << 4) - 1 // endMask - ) {} -}; - -BOOST_FIXTURE_TEST_CASE(StridedSliceSimpleRangeMask, StridedSliceSimpleRangeMaskFixture) -{ - RunTest<4>( - {{"input", { 1.0f, 1.0f, 1.0f, 2.0f, 2.0f, 2.0f, - 3.0f, 3.0f, 3.0f, 4.0f, 4.0f, 4.0f, - 5.0f, 5.0f, 5.0f, 6.0f, 6.0f, 6.0f }}}, - {{"output", { 1.0f, 1.0f, 1.0f, 2.0f, 2.0f, 2.0f, - 3.0f, 3.0f, 3.0f, 4.0f, 4.0f, 4.0f, - 5.0f, 5.0f, 5.0f, 6.0f, 6.0f, 6.0f }}}); -} - -BOOST_AUTO_TEST_SUITE_END() diff --git a/src/armnnTfParser/test/Sub.cpp b/src/armnnTfParser/test/Sub.cpp deleted file mode 100644 index 2b3cbe65d8..0000000000 --- a/src/armnnTfParser/test/Sub.cpp +++ /dev/null @@ -1,135 +0,0 @@ -// -// Copyright © 2017 Arm Ltd. All rights reserved. -// SPDX-License-Identifier: MIT -// - -#include <boost/test/unit_test.hpp> -#include "armnnTfParser/ITfParser.hpp" -#include "ParserPrototxtFixture.hpp" - -BOOST_AUTO_TEST_SUITE(TensorflowParser) - -struct SubFixture : public armnnUtils::ParserPrototxtFixture<armnnTfParser::ITfParser> -{ - SubFixture(const armnn::TensorShape& inputShape0, const armnn::TensorShape& inputShape1) - { - m_Prototext = R"( -node { - name: "input0" - op: "Placeholder" - attr { - key: "dtype" - value { - type: DT_FLOAT - } - } - attr { - key: "shape" - value { - shape { - } - } - } -} -node { - name: "input1" - op: "Placeholder" - attr { - key: "dtype" - value { - type: DT_FLOAT - } - } - attr { - key: "shape" - value { - shape { - } - } - } -} -node { - name: "output" - op: "Sub" - input: "input0" - input: "input1" - attr { - key: "T" - value { - type: DT_FLOAT - } - } -} - )"; - Setup({ { "input0", inputShape0 }, - { "input1", inputShape1 } }, - { "output" }); - - } -}; - -struct SubFixture4D4D : public SubFixture -{ - SubFixture4D4D() : SubFixture({ 1, 2, 2, 3 }, { 1, 2, 2, 3 }) {} -}; - -BOOST_FIXTURE_TEST_CASE(ParseSub, SubFixture4D4D) -{ - RunTest<4>({ { "input0", { 5.0f, 1.0f, 2.0f, - 3.0f, 4.0f, 5.0f, - 6.0f, 7.0f, 8.0f, - 29.0f, 10.0f, 11.0f } }, - - { "input1", { 0.0f, 1.0f, 3.0f, - 4.0f, 5.5f, 1.0f, - 2.0f, 17.0f, 18.0f, - 19.0f, 1.0f, 3.0f } } }, - - { { "output", { 5.0f, 0.0f, -1.0f, - -1.0f, -1.5f, 4.0f, - 4.0f, -10.0f, -10.0f, - 10.0f, 9.0f, 8.0f } } }); -} - -struct SubBroadcastFixture4D1D : public SubFixture -{ - SubBroadcastFixture4D1D() : SubFixture({ 1, 2, 2, 3 }, { 1 }) {} -}; - -BOOST_FIXTURE_TEST_CASE(ParseSubBroadcast4D1D, SubBroadcastFixture4D1D) -{ - RunTest<4>({ { "input0", { 0.0f, 1.0f, 2.0f, - 3.0f, 4.0f, 5.0f, - 6.0f, 7.0f, 8.0f, - 9.0f, 10.0f, 11.0f } }, - - { "input1", { 5.0f } } }, - - { { "output", { -5.0f, -4.0f, -3.0f, - -2.0f, -1.0f, 0.0f, - 1.0f, 2.0f, 3.0f, - 4.0f, 5.0f, 6.0f } } }); -} - -struct SubBroadcastFixture1D4D : public SubFixture -{ - SubBroadcastFixture1D4D() : SubFixture({ 1 }, { 1, 2, 2, 3 }) {} -}; - -BOOST_FIXTURE_TEST_CASE(ParseSubBroadcast1D4D, SubBroadcastFixture1D4D) -{ - RunTest<4>({ { "input0", { 3.0f } }, - - { "input1", { 0.0f, 1.0f, 2.0f, - 3.0f, 4.0f, 5.0f, - 6.0f, 7.0f, 8.0f, - 9.0f, 10.0f, 11.0f } } }, - - { { "output", { 3.0f, 2.0f, 1.0f, - 0.0f, -1.0f, -2.0f, - -3.0f, -4.0f, -5.0f, - -6.0f, -7.0f, -8.0f } } }); -} - - -BOOST_AUTO_TEST_SUITE_END()
\ No newline at end of file diff --git a/src/armnnTfParser/test/TestDependencies.cpp b/src/armnnTfParser/test/TestDependencies.cpp deleted file mode 100644 index f373e5669d..0000000000 --- a/src/armnnTfParser/test/TestDependencies.cpp +++ /dev/null @@ -1,296 +0,0 @@ -// -// Copyright © 2017 Arm Ltd. All rights reserved. -// SPDX-License-Identifier: MIT -// - -#include <boost/test/unit_test.hpp> -#include "armnnTfParser/ITfParser.hpp" -#include "ParserPrototxtFixture.hpp" - -BOOST_AUTO_TEST_SUITE(TensorflowParser) - -// Graph which tests that nodes are re-ordered in the queue when they are encountered a second time. -// In this case R0 will be encountered first via R1 and then via R2. At that time -// we need to make sure that R0 (and the I on which it is dependent) is moved to the front again -// so that it is before both R1 and R2. -// I -// | -// R0 -// / \' -// R1 R2 -// \ | -// \ R3 -// \| -// O -struct RediscoveredDependenciesFixture : public armnnUtils::ParserPrototxtFixture<armnnTfParser::ITfParser> -{ - RediscoveredDependenciesFixture() - { - // Input = tf.placeholder(tf.float32, 1, "input") - // Relu0 = tf.nn.relu(input, "relu0") - // Relu1 = tf.nn.relu(relu0, "relu1") - // Relu2 = tf.nn.relu(relu0, "relu2") - // Relu3 = tf.nn.relu(relu2, "relu3") - // Output = tf.add(relu1, relu3, "output") - m_Prototext = R"( - node { - name: "input" - op: "Placeholder" - attr { - key: "dtype" - value { - type: DT_FLOAT - } - } - attr { - key: "shape" - value { - shape { - dim { - size: 1 - } - } - } - } - } - node { - name: "relu0" - op: "Relu" - input: "input" - attr { - key: "T" - value { - type: DT_FLOAT - } - } - } - node { - name: "relu1" - op: "Relu" - input: "relu0" - attr { - key: "T" - value { - type: DT_FLOAT - } - } - } - node { - name: "relu2" - op: "Relu" - input: "relu0" - attr { - key: "T" - value { - type: DT_FLOAT - } - } - } - node { - name: "relu3" - op: "Relu" - input: "relu2" - attr { - key: "T" - value { - type: DT_FLOAT - } - } - } - node { - name: "output" - op: "Add" - input: "relu1" - input: "relu3" - attr { - key: "T" - value { - type: DT_FLOAT - } - } - } - )"; - SetupSingleInputSingleOutput({ 1 }, "input", "output"); - } -}; - -BOOST_FIXTURE_TEST_CASE(RediscoveredDependencies, RediscoveredDependenciesFixture) -{ - RunTest<1>({1}, {2}); -} - -// Tests that a simple cycle in the tensorflow graph will be detected and an exception thrown, rather than the TfParser -// getting stuck in an infinite loop. -BOOST_AUTO_TEST_CASE(SimpleCycle) -{ - const char* prototext = R"( -node { - name: "r1" - op: "Relu" - input: "r2" - attr { - key: "T" - value { - type: DT_FLOAT - } - } -} -node { - name: "r2" - op: "Relu" - input: "r1" - attr { - key: "T" - value { - type: DT_FLOAT - } - } -} - )"; - armnnTfParser::ITfParserPtr parser = armnnTfParser::ITfParser::Create(); - BOOST_CHECK_THROW(parser->CreateNetworkFromString(prototext, {}, { "r2" }), armnn::ParseException); -} - -// Similar to the above SimpleCycle test, but has a single node which connects to itself. -BOOST_AUTO_TEST_CASE(SingleNodeCycle) -{ - const char* prototext = R"( -node { - name: "r1" - op: "Relu" - input: "r1" - attr { - key: "T" - value { - type: DT_FLOAT - } - } -} - )"; - armnnTfParser::ITfParserPtr parser = armnnTfParser::ITfParser::Create(); - BOOST_CHECK_THROW(parser->CreateNetworkFromString(prototext, {}, { "r1" }), armnn::ParseException); -} - -// Similar to the above SimpleCycle test, but with a more complicated graph. -// I -// | -// A2---<---<- -// / \' | -// R1 R2 | -// \ | | -// \ R3 | -// \| | -// A1-->--->| -// -BOOST_AUTO_TEST_CASE(ComplexCycle) -{ - // Input = tf.placeholder(tf.float32, 1, "input") - // Add2 = tf.nn.relu(input, add1, "add2") // This line won't actually run in TF, because add1 is not yet defined - // Relu1 = tf.nn.relu(relu0, "relu1") - // Relu2 = tf.nn.relu(relu0, "relu2") - // Relu3 = tf.nn.relu(relu2, "relu3") - // Add1 = tf.add(relu1, relu3, "add1") - const char* prototext = R"( - node { - name: "input" - op: "Placeholder" - attr { - key: "dtype" - value { - type: DT_FLOAT - } - } - attr { - key: "shape" - value { - shape { - dim { - size: 1 - } - } - } - } - } - node { - name: "add2" - op: "Add" - input: "input" - input: "add1" - attr { - key: "T" - value { - type: DT_FLOAT - } - } - } - node { - name: "relu1" - op: "Relu" - input: "add2" - attr { - key: "T" - value { - type: DT_FLOAT - } - } - } - node { - name: "relu2" - op: "Relu" - input: "add2" - attr { - key: "T" - value { - type: DT_FLOAT - } - } - } - node { - name: "relu3" - op: "Relu" - input: "relu2" - attr { - key: "T" - value { - type: DT_FLOAT - } - } - } - node { - name: "add1" - op: "Add" - input: "relu1" - input: "relu3" - attr { - key: "T" - value { - type: DT_FLOAT - } - } - } - )"; - armnnTfParser::ITfParserPtr parser = armnnTfParser::ITfParser::Create(); - BOOST_CHECK_THROW(parser->CreateNetworkFromString(prototext, {}, { "add1" }), armnn::ParseException); -} - -// Tests that a graph with an input that is not present throws a ParseException. -BOOST_AUTO_TEST_CASE(InvalidInput) -{ - const char* prototext = R"( -node { - name: "r1" - op: "Relu" - input: "a-node-that-does-not-exist" - attr { - key: "T" - value { - type: DT_FLOAT - } - } -} - )"; - armnnTfParser::ITfParserPtr parser = armnnTfParser::ITfParser::Create(); - BOOST_CHECK_THROW(parser->CreateNetworkFromString(prototext, {}, { "r1" }), armnn::ParseException); -} - -BOOST_AUTO_TEST_SUITE_END() diff --git a/src/armnnTfParser/test/TestMultiInputsOutputs.cpp b/src/armnnTfParser/test/TestMultiInputsOutputs.cpp deleted file mode 100644 index a6d18b374c..0000000000 --- a/src/armnnTfParser/test/TestMultiInputsOutputs.cpp +++ /dev/null @@ -1,92 +0,0 @@ -// -// Copyright © 2017 Arm Ltd. All rights reserved. -// SPDX-License-Identifier: MIT -// - -#include <boost/test/unit_test.hpp> -#include "armnnTfParser/ITfParser.hpp" -#include "ParserPrototxtFixture.hpp" - -BOOST_AUTO_TEST_SUITE(TensorflowParser) - -struct MultiInputsOutputsFixture : public armnnUtils::ParserPrototxtFixture<armnnTfParser::ITfParser> -{ - MultiInputsOutputsFixture() - { - // Input1 = tf.placeholder(tf.float32, shape=[], name = "input1") - // Input2 = tf.placeholder(tf.float32, shape = [], name = "input2") - // Add1 = tf.add(input1, input2, name = "add1") - // Add2 = tf.add(input1, input2, name = "add2") - m_Prototext = R"( -node { - name: "input1" - op: "Placeholder" - attr { - key: "dtype" - value { - type: DT_FLOAT - } - } - attr { - key: "shape" - value { - shape { - } - } - } -} -node { - name: "input2" - op: "Placeholder" - attr { - key: "dtype" - value { - type: DT_FLOAT - } - } - attr { - key: "shape" - value { - shape { - } - } - } -} -node { - name: "add1" - op: "Add" - input: "input1" - input: "input2" - attr { - key: "T" - value { - type: DT_FLOAT - } - } -} -node { - name: "add2" - op: "Add" - input: "input1" - input: "input2" - attr { - key: "T" - value { - type: DT_FLOAT - } - } -} - )"; - Setup({ { "input1", { 1 } }, - { "input2", { 1 } } }, - { "add1", "add2" }); - } -}; - -BOOST_FIXTURE_TEST_CASE(MultiInputsOutputs, MultiInputsOutputsFixture) -{ - RunTest<1>({ { "input1", {12.0f} }, { "input2", { 13.0f } } }, - { { "add1", { 25.0f } }, { "add2", { 25.0f } } }); -} - -BOOST_AUTO_TEST_SUITE_END() diff --git a/src/armnnTfParser/test/Transpose.cpp b/src/armnnTfParser/test/Transpose.cpp deleted file mode 100644 index dd73bd90a2..0000000000 --- a/src/armnnTfParser/test/Transpose.cpp +++ /dev/null @@ -1,151 +0,0 @@ -// -// Copyright © 2020 Arm Ltd. All rights reserved. -// SPDX-License-Identifier: MIT -// - -#include "armnnTfParser/ITfParser.hpp" -#include "ParserPrototxtFixture.hpp" - -#include <boost/test/unit_test.hpp> -#include <PrototxtConversions.hpp> - -BOOST_AUTO_TEST_SUITE(TensorflowParser) - -namespace -{ - std::string ConvertInt32VectorToOctalString(const std::vector<unsigned int>& data) - { - std::stringstream ss; - ss << "\""; - std::for_each(data.begin(), data.end(), [&ss](unsigned int d) { - ss << armnnUtils::ConvertInt32ToOctalString(static_cast<int>(d)); - }); - ss << "\""; - return ss.str(); - } -} // namespace - -struct TransposeFixture : public armnnUtils::ParserPrototxtFixture<armnnTfParser::ITfParser> -{ - TransposeFixture(const armnn::TensorShape& inputShape, - const std::vector<unsigned int>& permuteVectorData) - { - using armnnUtils::ConvertTensorShapeToString; - armnn::TensorShape permuteVectorShape({static_cast<unsigned int>(permuteVectorData.size())}); - - m_Prototext = "node {\n" -" name: \"input\"\n" -" op: \"Placeholder\"\n" -" attr {\n" -" key: \"dtype\"\n" -" value {\n" -" type: DT_FLOAT\n" -" }\n" -" }\n" -" attr {\n" -" key: \"shape\"\n" -" value {\n" -" shape {\n"; - m_Prototext.append(ConvertTensorShapeToString(inputShape)); - m_Prototext.append( -" }\n" -" }\n" -" }\n" -"}\n" -"node {\n" -" name: \"transpose/perm\"\n" -" op: \"Const\"\n" -" attr {\n" -" key: \"dtype\"\n" -" value {\n" -" type: DT_INT32\n" -" }\n" -" }\n" -" attr {\n" -" key: \"value\"\n" -" value {\n" -" tensor {\n" -" dtype: DT_INT32\n" -" tensor_shape {\n" - ); - m_Prototext.append(ConvertTensorShapeToString(permuteVectorShape)); - m_Prototext.append( -" }\n" -" tensor_content: " - ); - m_Prototext.append(ConvertInt32VectorToOctalString(permuteVectorData) + "\n"); - m_Prototext.append( -" }\n" -" }\n" -" }\n" -"}\n" - ); - m_Prototext.append( -"node {\n" -" name: \"output\"\n" -" op: \"Transpose\"\n" -" input: \"input\"\n" -" input: \"transpose/perm\"\n" -" attr {\n" -" key: \"T\"\n" -" value {\n" -" type: DT_FLOAT\n" -" }\n" -" }\n" -" attr {\n" -" key: \"Tperm\"\n" -" value {\n" -" type: DT_INT32\n" -" }\n" -" }\n" -"}\n" - ); - Setup({{"input", inputShape}}, {"output"}); - } -}; - -struct TransposeFixtureWithPermuteData : TransposeFixture -{ - TransposeFixtureWithPermuteData() - : TransposeFixture({2, 2, 3, 4}, - std::vector<unsigned int>({1, 3, 2, 0})) {} -}; - -BOOST_FIXTURE_TEST_CASE(TransposeWithPermuteData, TransposeFixtureWithPermuteData) -{ - RunTest<4>( - {{"input", {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, - 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, - 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47}}}, - {{"output", {0, 24, 4, 28, 8, 32, 1, 25, 5, 29, 9, 33, 2, 26, 6, - 30, 10, 34, 3, 27, 7, 31, 11, 35, 12, 36, 16, 40, 20, 44, 13, 37, - 17, 41, 21, 45, 14, 38, 18, 42, 22, 46, 15, 39, 19, 43, 23, 47}}}); - - BOOST_TEST((m_Parser->GetNetworkOutputBindingInfo("output").second.GetShape() - == armnn::TensorShape({2, 4, 3, 2}))); -} - -struct TransposeFixtureWithoutPermuteData : TransposeFixture -{ - // In case permute data is not given, it assumes (n-1,...,0) is given - // where n is the rank of input tensor. - TransposeFixtureWithoutPermuteData() - : TransposeFixture({2, 2, 3, 4}, - std::vector<unsigned int>({3, 2, 1, 0})) {} -}; - -BOOST_FIXTURE_TEST_CASE(TransposeWithoutPermuteData, TransposeFixtureWithoutPermuteData) -{ - RunTest<4>( - {{"input", {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, - 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, - 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47}}}, - {{"output", {0, 24, 12, 36, 4, 28, 16, 40, 8, 32, 20, 44, 1, 25, - 13, 37, 5, 29, 17, 41, 9, 33, 21, 45, 2, 26, 14, 38, 6, 30, 18, - 42,10, 34, 22, 46, 3, 27, 15, 39, 7, 31, 19, 43, 11, 35, 23, 47}}}); - - BOOST_TEST((m_Parser->GetNetworkOutputBindingInfo("output").second.GetShape() - == armnn::TensorShape({4, 3, 2, 2}))); -} - -BOOST_AUTO_TEST_SUITE_END() |