aboutsummaryrefslogtreecommitdiff
path: root/src/armnnTfLiteParser
diff options
context:
space:
mode:
authorIdriss Chaouch <idriss.chaouch@arm.com>2023-09-01 17:58:38 +0100
committeridriss.chaouch <idriss.chaouch@arm.com>2023-09-08 08:32:43 +0000
commit564c13dc098eb9353ac15e2609712ab8db9bf350 (patch)
tree6cb52e904e3cd001d650a6386b1105ee21b08847 /src/armnnTfLiteParser
parent04e3eb5d339c3778f26c69651bf1464c8ab5331d (diff)
downloadarmnn-564c13dc098eb9353ac15e2609712ab8db9bf350.tar.gz
IVGCVSW-7525 Add broadcast_to to TFLite Parser
* Changing the optimizer * Changing EndToEnd Tests Signed-off-by: Idriss Chaouch <idriss.chaouch@arm.com> Signed-off-by: Narumol Prangnawarat <narumol.prangnawarat@arm.com> Change-Id: Ib581794280322a39cfc5ea3c4e6a6398cf723d5e
Diffstat (limited to 'src/armnnTfLiteParser')
-rw-r--r--src/armnnTfLiteParser/TfLiteParser.cpp61
-rw-r--r--src/armnnTfLiteParser/TfLiteParser.hpp1
-rw-r--r--src/armnnTfLiteParser/test/BroadcastTo.cpp508
3 files changed, 570 insertions, 0 deletions
diff --git a/src/armnnTfLiteParser/TfLiteParser.cpp b/src/armnnTfLiteParser/TfLiteParser.cpp
index 052aac6101..3f4f0d811f 100644
--- a/src/armnnTfLiteParser/TfLiteParser.cpp
+++ b/src/armnnTfLiteParser/TfLiteParser.cpp
@@ -750,6 +750,7 @@ TfLiteParserImpl::TfLiteParserImpl(const Optional<ITfLiteParser::TfLiteParserOpt
m_ParserFunctions[tflite::BuiltinOperator_AVERAGE_POOL_2D] = &TfLiteParserImpl::ParseAveragePool2D;
m_ParserFunctions[tflite::BuiltinOperator_BATCH_TO_SPACE_ND] = &TfLiteParserImpl::ParseBatchToSpaceND;
m_ParserFunctions[tflite::BuiltinOperator_BATCH_MATMUL] = &TfLiteParserImpl::ParseBatchMatMul;
+ m_ParserFunctions[tflite::BuiltinOperator_BROADCAST_TO] = &TfLiteParserImpl::ParseBroadcastTo;
m_ParserFunctions[tflite::BuiltinOperator_CEIL] = &TfLiteParserImpl::ParseCeil;
m_ParserFunctions[tflite::BuiltinOperator_CAST] = &TfLiteParserImpl::ParseCast;
m_ParserFunctions[tflite::BuiltinOperator_CONCATENATION] = &TfLiteParserImpl::ParseConcatenation;
@@ -1894,6 +1895,66 @@ void TfLiteParserImpl::ParseBatchToSpaceND(size_t subgraphIndex, size_t operator
RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
}
+void TfLiteParserImpl::ParseBroadcastTo(size_t subgraphIndex, size_t operatorIndex)
+{
+ CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
+
+ auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
+ CHECK_VALID_SIZE(inputs.size(), 2);
+
+ auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
+ CHECK_VALID_SIZE(outputs.size(), 1);
+
+ TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
+ TensorInfo shapeTensorInfo = ToTensorInfo(inputs[1]);
+ TensorInfo outputTensorInfo = ToTensorInfo(outputs[0]);
+
+ auto layerName = fmt::format("Broadcast_to:{}:{}", subgraphIndex, operatorIndex);
+
+ BroadcastToDescriptor descriptor;
+
+ auto shapeBufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
+ if (shapeBufferPtr != nullptr)
+ {
+ std::vector<unsigned int> targetShape;
+ unsigned int numElement = shapeTensorInfo.GetNumElements();
+ auto shapeData = reinterpret_cast<const int32_t*>(shapeBufferPtr->data.data());
+ if (shapeData)
+ {
+ for (unsigned int i = 0; i < numElement; ++i)
+ {
+ targetShape.push_back(armnn::numeric_cast<unsigned int>(shapeData[i]));
+ }
+ descriptor.m_BroadcastToShape = TensorShape(numElement, targetShape.data());
+ }
+ /// get dataShape from outputShape if missing
+ else
+ {
+ if(outputTensorInfo.GetShape().GetNumElements() <= 1)
+ {
+ ARMNN_THROW_PARSE_EXCEPTION("For Broadcast_to layer, "
+ "data and output shape are not found in the buffer.");
+ }
+ descriptor.m_BroadcastToShape = outputTensorInfo.GetShape();
+ }
+ }
+ else
+ {
+ ARMNN_THROW_PARSE_EXCEPTION("For Broadcast_to layer, Shape data was not found in the buffer.");
+ }
+
+ IConnectableLayer* layer = m_Network->AddBroadcastToLayer(descriptor, layerName.c_str());
+ ARMNN_ASSERT(layer != nullptr);
+
+ layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
+
+ auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
+ RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
+
+ auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
+ RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
+}
+
void TfLiteParserImpl::ParseL2Normalization(size_t subgraphIndex, size_t operatorIndex)
{
CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
diff --git a/src/armnnTfLiteParser/TfLiteParser.hpp b/src/armnnTfLiteParser/TfLiteParser.hpp
index e7f265915a..f0c7ddefb9 100644
--- a/src/armnnTfLiteParser/TfLiteParser.hpp
+++ b/src/armnnTfLiteParser/TfLiteParser.hpp
@@ -117,6 +117,7 @@ private:
void ParseAveragePool2D(size_t subgraphIndex, size_t operatorIndex);
void ParseBatchMatMul(size_t subgraphIndex, size_t operatorIndex);
void ParseBatchToSpaceND(size_t subgraphIndex, size_t operatorIndex);
+ void ParseBroadcastTo(size_t subgraphIndex, size_t operatorIndex);
void ParseCast(size_t subgraphIndex, size_t operatorIndex);
void ParseCeil(size_t subgraphIndex, size_t operatorIndex);
void ParseComparison(size_t subgraphIndex, size_t operatorIndex, armnn::ComparisonOperation comparisonOperation);
diff --git a/src/armnnTfLiteParser/test/BroadcastTo.cpp b/src/armnnTfLiteParser/test/BroadcastTo.cpp
new file mode 100644
index 0000000000..9a5d6c0ba8
--- /dev/null
+++ b/src/armnnTfLiteParser/test/BroadcastTo.cpp
@@ -0,0 +1,508 @@
+//
+// Copyright © 2023 Arm Ltd and Contributors. All rights reserved.
+// SPDX-License-Identifier: MIT
+//
+
+#include "ParserFlatbuffersFixture.hpp"
+
+TEST_SUITE("TensorflowLiteParser_BroadcastTo")
+{
+struct BroadcastToFixture : public ParserFlatbuffersFixture
+{
+ explicit BroadcastToFixture(const std::string& inputShape1,
+ const std::string& inputShape2,
+ const std::string& shapeShape,
+ const std::string& outputBroadcastToShape,
+ const std::string& outputShape,
+ const std::string& shapeData,
+ const bool checkThrows,
+ const std::string& scale = "1.0",
+ const std::string& offset = "0")
+ {
+ m_JsonString = R"(
+ {
+ "version": 3,
+ "operator_codes": [
+ {
+ "deprecated_builtin_code": 127,
+ "version": 2,
+ "builtin_code": "BROADCAST_TO"
+ },
+ {
+ "version": 1,
+ "builtin_code": "MUL"
+ }
+ ],
+ "subgraphs": [
+ {
+ "tensors": [
+ {
+ "shape": )" + inputShape1 + R"(,
+ "type": "FLOAT32",
+ "buffer": 1,
+ "name": "inputTensor1",
+ "quantization":{
+ "min": [ 0.0 ],
+ "max": [ 255.0 ],
+ "scale": [ )" + scale + R"( ],
+ "zero_point": [ )" + offset + R"( ],
+ },
+ "is_variable": false,
+ "shape_signature": [
+ -1,
+ 3
+ ],
+ "has_rank": true
+ },
+ {
+ "shape": )" + inputShape2 + R"(,
+ "type": "FLOAT32",
+ "buffer": 2,
+ "name": "inputTensor2",
+ "quantization": {
+ "min": [ 0.0 ],
+ "max": [ 255.0 ],
+ "scale": [ )" + scale + R"( ],
+ "zero_point": [ )" + offset + R"( ],
+ },
+ "is_variable": false,
+ "shape_signature": [
+ -1,
+ 3
+ ],
+ "has_rank": true
+ },
+ {
+ "shape": )" + shapeShape + R"(,
+ "type": "INT32",
+ "buffer": 3,
+ "name": "shape",
+ "quantization": {
+ "details_type": "NONE",
+ "quantized_dimension": 0
+ },
+ "is_variable": false,
+ "shape_signature": [
+ -1
+ ],
+ "has_rank": true
+ },
+ {
+ "shape": )" + outputBroadcastToShape + R"(,
+ "type": "FLOAT32",
+ "buffer": 4,
+ "name": "model/tf.broadcast_to/BroadcastTo",
+ "quantization": {
+ "min": [ 0.0 ],
+ "max": [ 255.0 ],
+ "scale": [ )" + scale + R"( ],
+ "zero_point": [ )" + offset + R"( ],
+ },
+ "is_variable": false,
+ "has_rank": false
+ },
+ {
+ "shape": )" + outputShape + R"(,
+ "type": "FLOAT32",
+ "buffer": 5,
+ "name": "outputTensor",
+ "quantization": {
+ "min": [ 0.0 ],
+ "max": [ 255.0 ],
+ "scale": [ )" + scale + R"( ],
+ "zero_point": [ )" + offset + R"( ],
+ },
+ "is_variable": false,
+ "has_rank": false
+ }
+ ],
+ "inputs": [
+ 0,
+ 1,
+ 2
+ ],
+ "outputs": [
+ 4
+ ],
+ "operators": [
+ {
+ "opcode_index": 0,
+ "inputs": [
+ 0,
+ 2
+ ],
+ "outputs": [
+ 3
+ ],
+ "builtin_options_type": "NONE",
+ "custom_options_format": "FLEXBUFFERS"
+ },
+ {
+ "opcode_index": 1,
+ "inputs": [
+ 1,
+ 3
+ ],
+ "outputs": [
+ 4
+ ],
+ "builtin_options_type": "MulOptions",
+ "builtin_options": {
+ "fused_activation_function": "NONE"
+ },
+ "custom_options_format": "FLEXBUFFERS"
+ }
+ ],
+ "name": "main"
+ }
+ ],
+ "description": "MLIR Converted.",
+ "buffers": [
+ {
+ },
+ {
+ },
+ {
+ },
+ {
+ )" + shapeData + R"(
+ },
+ {
+ },
+ {
+ },
+ {
+ },
+ {
+ }
+ ],
+ "metadata": [
+ {
+ "name": "min_runtime_version",
+ "buffer": 6
+ },
+ {
+ "name": "CONVERSION_METADATA",
+ "buffer": 7
+ }
+ ],
+ "signature_defs": [
+
+ ]
+ }
+ )";
+ if(checkThrows)
+ {
+ CHECK_THROWS_AS(Setup(), armnn::ParseException);
+ }
+ else
+ {
+ Setup();
+ }
+ }
+};
+
+struct BroadcastToSimpleFixture : public ParserFlatbuffersFixture
+{
+ explicit BroadcastToSimpleFixture(const std::string& inputShape,
+ const std::string& shapeShape,
+ const std::string& outputShape,
+ const std::string& shapeData,
+ const std::string& dataType,
+ const std::string& scale = "1.0",
+ const std::string& offset = "0")
+ {
+ m_JsonString = R"(
+ {
+ "version": 3,
+ "operator_codes": [
+ {
+ "deprecated_builtin_code": 127,
+ "version": 2,
+ "builtin_code": "BROADCAST_TO"
+ }
+ ],
+ "subgraphs": [
+ {
+ "tensors": [
+ {
+ "shape": )" + inputShape + R"(,
+ "type": )" + dataType + R"(,
+ "buffer": 1,
+ "name": "input1",
+ "quantization": {
+ "min": [ 0.0 ],
+ "max": [ 255.0 ],
+ "scale": [ )" + scale + R"( ],
+ "zero_point": [ )" + offset + R"( ],
+ },
+ "is_variable": false,
+ "shape_signature": [
+ -1,
+ 3
+ ],
+ "has_rank": true
+ },
+ {
+ "shape": )" + shapeShape + R"(,
+ "type": "INT32",
+ "buffer": 2,
+ "name": "shape",
+ "quantization": {
+ "details_type": "NONE",
+ "quantized_dimension": 0
+ },
+ "is_variable": false,
+ "shape_signature": [
+ -1
+ ],
+ "has_rank": true
+ },
+ {
+ "shape": )" + outputShape + R"(,
+ "type": )" + dataType + R"(,
+ "buffer": 3,
+ "name": "Identity",
+ "quantization": {
+ "min": [ 0.0 ],
+ "max": [ 255.0 ],
+ "scale": [ )" + scale + R"( ],
+ "zero_point": [ )" + offset + R"( ],
+ },
+ "is_variable": false,
+ "has_rank": false
+ }
+ ],
+ "inputs": [
+ 0,
+ 1
+ ],
+ "outputs": [
+ 2
+ ],
+ "operators": [
+ {
+ "opcode_index": 0,
+ "inputs": [
+ 0,
+ 1
+ ],
+ "outputs": [
+ 2
+ ],
+ "builtin_options_type": "NONE",
+ "custom_options_format": "FLEXBUFFERS"
+ }
+ ],
+ "name": "main"
+ }
+ ],
+ "description": "MLIR Converted.",
+ "buffers": [
+ {
+ },
+ {
+ },
+ {
+ "data": )" + shapeData + R"(,
+ },
+ {
+ },
+ {
+ },
+ {
+ }
+ ],
+ "metadata": [
+ {
+ "name": "min_runtime_version",
+ "buffer": 4
+ },
+ {
+ "name": "CONVERSION_METADATA",
+ "buffer": 5
+ }
+ ],
+ "signature_defs": [
+
+ ]
+ }
+ )";
+ SetupSingleInputSingleOutput("inputTensor", "outputTensor");
+ }
+};
+
+struct SimpleBroadcastToSimpleFixtureFloat32 : public BroadcastToSimpleFixture
+{
+ SimpleBroadcastToSimpleFixtureFloat32() : BroadcastToSimpleFixture("[1, 4]",
+ "[2]",
+ "[3, 4]",
+ "[3, 0, 0, 0, 4, 0, 0, 0]",
+ "FLOAT32") {}
+};
+
+TEST_CASE_FIXTURE(SimpleBroadcastToSimpleFixtureFloat32, "SimpleParseBroadcastToFloat32")
+{
+ RunTest<2, armnn::DataType::Float32, armnn::DataType::Float32>
+ (0, {{ "input1", { 1.f, 2.f, 3.f, 4.f }}},
+ {{ "Identity", { 1.f, 2.f, 3.f, 4.f,
+ 1.f, 2.f, 3.f, 4.f,
+ 1.f, 2.f, 3.f, 4.f}}});
+
+}
+struct SimpleBroadcastToSimpleFixtureSigned32 : public BroadcastToSimpleFixture
+{
+ SimpleBroadcastToSimpleFixtureSigned32() : BroadcastToSimpleFixture("[1, 4]",
+ "[2]",
+ "[3, 4]",
+ "[3, 0, 0, 0, 4, 0, 0, 0]",
+ "INT32") {}
+};
+
+TEST_CASE_FIXTURE(SimpleBroadcastToSimpleFixtureSigned32, "SimpleParseBroadcastToSigned32")
+{
+ RunTest<2, armnn::DataType::Signed32, armnn::DataType::Signed32>
+ (0, {{ "input1", { 1, 2, 3, -4 }}},
+ {{ "Identity", { 1, 2, 3, -4,
+ 1, 2, 3, -4,
+ 1, 2, 3, -4}}});
+
+}
+
+struct SimpleBroadcastToSimpleFixtureQAsymmU8 : public BroadcastToSimpleFixture
+{
+ SimpleBroadcastToSimpleFixtureQAsymmU8() : BroadcastToSimpleFixture("[1, 4]",
+ "[2]",
+ "[3, 4]",
+ "[3, 0, 0, 0, 4, 0, 0, 0]",
+ "UINT8") {}
+};
+
+TEST_CASE_FIXTURE(SimpleBroadcastToSimpleFixtureQAsymmU8, "SimpleParseBroadcastToQAsymmU8")
+{
+ RunTest<2, armnn::DataType::QAsymmU8, armnn::DataType::QAsymmU8>
+ (0, {{ "input1", { 1, 2, 3, 4 }}},
+ {{ "Identity", { 1, 2, 3, 4,
+ 1, 2, 3, 4,
+ 1, 2, 3, 4}}});
+
+}
+
+struct SimpleBroadcastToFixture : public BroadcastToFixture
+{
+ SimpleBroadcastToFixture() : BroadcastToFixture("[1, 4]",
+ "[3, 4]",
+ "[2]",
+ "[3, 4]",
+ "[3, 4]",
+ "\"data\":[3, 0, 0, 0, 4, 0, 0, 0]",
+ false) {}
+};
+
+TEST_CASE_FIXTURE(SimpleBroadcastToFixture, "ParseBroadcastTo")
+{
+ RunTest<2, armnn::DataType::Float32, armnn::DataType::Float32, armnn::DataType::Float32>
+ (
+ 0, {{ "inputTensor1", { 1, 2, 3, 4 }}},
+ {{"inputTensor2", {1, 1, 1, 1,
+ 1, 1, 1, 1,
+ 1, 1, 1, 1}}},
+ {{ "outputTensor", { 1, 2, 3, 4,
+ 1, 2, 3, 4,
+ 1, 2, 3, 4}}}
+ );
+
+}
+
+struct DynamicBroadcastToFixture : public BroadcastToFixture
+{
+ DynamicBroadcastToFixture() : BroadcastToFixture("[1, 4]",
+ "[3, 4]",
+ "[2]",
+ "[3, 4]",
+ "[3, 4]",
+ "\"data\":[3, 0, 0, 0, 4, 0, 0, 0]",
+ false) {}
+};
+
+TEST_CASE_FIXTURE(DynamicBroadcastToFixture, "DynamicParseBroadcastTo")
+{
+ RunTest<2, armnn::DataType::Float32, armnn::DataType::Float32, armnn::DataType::Float32>
+ (
+ 0, {{ "inputTensor1", { 1, 2, 3, 4 }}},
+ {{"inputTensor2", {1, 1, 1, 1,
+ 1, 1, 1, 1,
+ 1, 1, 1, 1}}},
+ {{ "outputTensor", { 1, 2, 3, 4,
+ 1, 2, 3, 4,
+ 1, 2, 3, 4}}}
+ );
+
+}
+
+struct DynamicBroadcastToFixtureNoOutputShape : public BroadcastToFixture
+{
+ DynamicBroadcastToFixtureNoOutputShape() : BroadcastToFixture("[1, 4]",
+ "[3, 4]",
+ "[2]",
+ "[]",
+ "[3, 4]",
+ "\"data\":[3, 0, 0, 0, 4, 0, 0, 0]",
+ false) {}
+};
+
+TEST_CASE_FIXTURE(DynamicBroadcastToFixtureNoOutputShape, "DynamicParseBroadcastToNoOutputShape")
+{
+ RunTest<2, armnn::DataType::Float32, armnn::DataType::Float32, armnn::DataType::Float32>
+ (
+ 0, {{ "inputTensor1", { 1, 2, 3, 4 }}},
+ {{"inputTensor2", {1, 1, 1, 1,
+ 1, 1, 1, 1,
+ 1, 1, 1, 1}}},
+ {{ "outputTensor", { 1, 2, 3, 4,
+ 1, 2, 3, 4,
+ 1, 2, 3, 4}}}
+ );
+
+}
+
+struct DynamicBroadcastToFixtureNoData : public BroadcastToFixture
+{
+ DynamicBroadcastToFixtureNoData() : BroadcastToFixture("[1, 4]",
+ "[3, 4]",
+ "[2]",
+ "[3, 4]",
+ "[3, 4]",
+ "",
+ false) {}
+};
+
+TEST_CASE_FIXTURE(DynamicBroadcastToFixtureNoData, "DynamicParseBroadcastToNoData")
+{
+ RunTest<2, armnn::DataType::Float32, armnn::DataType::Float32, armnn::DataType::Float32>
+ (
+ 0, {{ "inputTensor1", { 1, 2, 3, 4 }}},
+ {{"inputTensor2", {1, 1, 1, 1,
+ 1, 1, 1, 1,
+ 1, 1, 1, 1}}},
+ {{ "outputTensor", { 1, 2, 3, 4,
+ 1, 2, 3, 4,
+ 1, 2, 3, 4}}}
+ );
+
+}
+
+struct DynamicBroadcastToFixtureNoDataNoOutputShape : public BroadcastToFixture
+{
+ DynamicBroadcastToFixtureNoDataNoOutputShape() : BroadcastToFixture("[1, 4]",
+ "[3, 4]",
+ "[2]",
+ "[]",
+ "[3, 4]",
+ "", true) { }
+};
+
+TEST_CASE_FIXTURE(DynamicBroadcastToFixtureNoDataNoOutputShape, "DynamicParseBroadcastToNoDataNoOutputShape")
+{
+}
+} \ No newline at end of file