IVGCVSW-7172 Add ElementwiseBinary (Subtraction & Multiplication) support to TOSA Reference Backend
* Removed AdditionOperator and moved to new ElementwiseBinaryOperator.
Signed-off-by: Nikhil Raj <nikhil.raj@arm.com>
Signed-off-by: Matthew Sloyan <matthew.sloyan@arm.com>
Change-Id: I8ce20f7575d68334aadcd176827bca3db53d0052
diff --git a/src/backends/backendsCommon/test/CMakeLists.txt b/src/backends/backendsCommon/test/CMakeLists.txt
index 5fcc8b5..d251bd2 100644
--- a/src/backends/backendsCommon/test/CMakeLists.txt
+++ b/src/backends/backendsCommon/test/CMakeLists.txt
@@ -1,5 +1,5 @@
#
-# Copyright © 2017,2022 Arm Ltd and Contributors. All rights reserved.
+# Copyright © 2017-2022 Arm Ltd and Contributors. All rights reserved.
# SPDX-License-Identifier: MIT
#
@@ -41,6 +41,7 @@
LogSoftmaxEndToEndTestImpl.hpp
MemoryManagerTests.cpp
MockBackendId.hpp
+ MultiplicationEndToEndTestImpl.hpp
OptimizeSubgraphViewTests.cpp
OptimizationViewsTests.cpp
PreluEndToEndTestImpl.hpp
@@ -57,6 +58,7 @@
SpaceToDepthEndToEndTestImpl.hpp
SplitterEndToEndTestImpl.hpp
StridedSliceAsyncEndToEndTest.hpp
+ SubtractionEndToEndTestImpl.hpp
TransposeEndToEndTestImpl.hpp
TensorCopyUtils.hpp
WorkloadFactoryHelper.hpp
diff --git a/src/backends/backendsCommon/test/MultiplicationEndToEndTestImpl.hpp b/src/backends/backendsCommon/test/MultiplicationEndToEndTestImpl.hpp
new file mode 100644
index 0000000..40442e2
--- /dev/null
+++ b/src/backends/backendsCommon/test/MultiplicationEndToEndTestImpl.hpp
@@ -0,0 +1,96 @@
+//
+// Copyright © 2022 Arm Ltd and Contributors. All rights reserved.
+// SPDX-License-Identifier: MIT
+//
+#pragma once
+
+#include <armnn/INetwork.hpp>
+
+#include <CommonTestUtils.hpp>
+#include <ResolveType.hpp>
+
+#include <doctest/doctest.h>
+
+namespace
+{
+
+template<typename armnn::DataType DataType>
+armnn::INetworkPtr CreateMultiplicationNetwork(const armnn::TensorShape& inputXShape,
+ const armnn::TensorShape& inputYShape,
+ const armnn::TensorShape& outputShape,
+ const float qScale = 1.0f,
+ const int32_t qOffset = 0)
+{
+ using namespace armnn;
+
+ INetworkPtr network(INetwork::Create());
+
+ TensorInfo inputXTensorInfo(inputXShape, DataType, qScale, qOffset, true);
+ TensorInfo inputYTensorInfo(inputYShape, DataType, qScale, qOffset, true);
+
+ TensorInfo outputTensorInfo(outputShape, DataType, qScale, qOffset);
+
+
+ IConnectableLayer* multiplication = network->AddMultiplicationLayer("multiplication");
+ IConnectableLayer* inputX = network->AddInputLayer(0, "inputX");
+ IConnectableLayer* inputY = network->AddInputLayer(1, "inputY");
+ IConnectableLayer* output = network->AddOutputLayer(0, "output");
+
+ Connect(inputX, multiplication, inputXTensorInfo, 0, 0);
+ Connect(inputY, multiplication, inputYTensorInfo, 0, 1);
+ Connect(multiplication, output, outputTensorInfo, 0, 0);
+
+ return network;
+}
+
+template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>>
+void MultiplicationEndToEnd(const std::vector<armnn::BackendId>& backends)
+{
+ using namespace armnn;
+
+ const TensorShape& inputXShape = { 2, 2 };
+ const TensorShape& inputYShape = { 2, 2 };
+ const TensorShape& outputShape = { 2, 2 };
+
+ INetworkPtr network = CreateMultiplicationNetwork<ArmnnType>(inputXShape, inputYShape, outputShape);
+
+ CHECK(network);
+
+ std::vector<T> inputXData{ 1, 2, 3, 4 };
+ std::vector<T> inputYData{ 5, 2, 6, 3 };
+ std::vector<T> expectedOutput{ 5, 4, 18, 12 };
+
+ std::map<int, std::vector<T>> inputTensorData = {{ 0, inputXData }, {1, inputYData}};
+ std::map<int, std::vector<T>> expectedOutputData = { { 0, expectedOutput } };
+
+ EndToEndLayerTestImpl<ArmnnType, ArmnnType>(std::move(network), inputTensorData, expectedOutputData, backends);
+}
+
+template<armnn::DataType ArmnnType>
+void MultiplicationEndToEndFloat16(const std::vector<armnn::BackendId>& backends)
+{
+ using namespace armnn;
+ using namespace half_float::literal;
+ using Half = half_float::half;
+
+ const TensorShape& inputXShape = { 2, 2 };
+ const TensorShape& inputYShape = { 2, 2 };
+ const TensorShape& outputShape = { 2, 2 };
+
+ INetworkPtr network = CreateMultiplicationNetwork<ArmnnType>(inputXShape, inputYShape, outputShape);
+ CHECK(network);
+
+ std::vector<Half> inputXData{ 1._h, 2._h,
+ 3._h, 4._h };
+ std::vector<Half> inputYData{ 1._h, 2._h,
+ 3._h, 4._h };
+ std::vector<Half> expectedOutput{ 1._h, 4._h,
+ 9._h, 16._h };
+
+ std::map<int, std::vector<Half>> inputTensorData = {{ 0, inputXData }, { 1, inputYData }};
+ std::map<int, std::vector<Half>> expectedOutputData = { { 0, expectedOutput } };
+
+ EndToEndLayerTestImpl<ArmnnType, ArmnnType>(std::move(network), inputTensorData, expectedOutputData, backends);
+}
+
+} // anonymous namespace
diff --git a/src/backends/backendsCommon/test/SubtractionEndToEndTestImpl.hpp b/src/backends/backendsCommon/test/SubtractionEndToEndTestImpl.hpp
new file mode 100644
index 0000000..747fe26
--- /dev/null
+++ b/src/backends/backendsCommon/test/SubtractionEndToEndTestImpl.hpp
@@ -0,0 +1,96 @@
+//
+// Copyright © 2022 Arm Ltd and Contributors. All rights reserved.
+// SPDX-License-Identifier: MIT
+//
+#pragma once
+
+#include <armnn/INetwork.hpp>
+
+#include <CommonTestUtils.hpp>
+#include <ResolveType.hpp>
+
+#include <doctest/doctest.h>
+
+namespace
+{
+
+template<typename armnn::DataType DataType>
+armnn::INetworkPtr CreateSubtractionNetwork(const armnn::TensorShape& inputXShape,
+ const armnn::TensorShape& inputYShape,
+ const armnn::TensorShape& outputShape,
+ const float qScale = 1.0f,
+ const int32_t qOffset = 0)
+{
+ using namespace armnn;
+
+ INetworkPtr network(INetwork::Create());
+
+ TensorInfo inputXTensorInfo(inputXShape, DataType, qScale, qOffset, true);
+ TensorInfo inputYTensorInfo(inputYShape, DataType, qScale, qOffset, true);
+
+ TensorInfo outputTensorInfo(outputShape, DataType, qScale, qOffset);
+
+
+ IConnectableLayer* subtraction = network->AddSubtractionLayer("subtraction");
+ IConnectableLayer* inputX = network->AddInputLayer(0, "inputX");
+ IConnectableLayer* inputY = network->AddInputLayer(1, "inputY");
+ IConnectableLayer* output = network->AddOutputLayer(0, "output");
+
+ Connect(inputX, subtraction, inputXTensorInfo, 0, 0);
+ Connect(inputY, subtraction, inputYTensorInfo, 0, 1);
+ Connect(subtraction, output, outputTensorInfo, 0, 0);
+
+ return network;
+}
+
+template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>>
+void SubtractionEndToEnd(const std::vector<armnn::BackendId>& backends)
+{
+ using namespace armnn;
+
+ const TensorShape& inputXShape = { 2, 2 };
+ const TensorShape& inputYShape = { 2, 2 };
+ const TensorShape& outputShape = { 2, 2 };
+
+ INetworkPtr network = CreateSubtractionNetwork<ArmnnType>(inputXShape, inputYShape, outputShape);
+
+ CHECK(network);
+
+ std::vector<T> inputXData{ 10, 11, 12, 13 };
+ std::vector<T> inputYData{ 5, 7, 6, 8 };
+ std::vector<T> expectedOutput{ 5, 4, 6, 5 };
+
+ std::map<int, std::vector<T>> inputTensorData = {{ 0, inputXData }, {1, inputYData}};
+ std::map<int, std::vector<T>> expectedOutputData = { { 0, expectedOutput } };
+
+ EndToEndLayerTestImpl<ArmnnType, ArmnnType>(std::move(network), inputTensorData, expectedOutputData, backends);
+}
+
+template<armnn::DataType ArmnnType>
+void SubtractionEndToEndFloat16(const std::vector<armnn::BackendId>& backends)
+{
+ using namespace armnn;
+ using namespace half_float::literal;
+ using Half = half_float::half;
+
+ const TensorShape& inputXShape = { 2, 2 };
+ const TensorShape& inputYShape = { 2, 2 };
+ const TensorShape& outputShape = { 2, 2 };
+
+ INetworkPtr network = CreateSubtractionNetwork<ArmnnType>(inputXShape, inputYShape, outputShape);
+ CHECK(network);
+
+ std::vector<Half> inputXData{ 11._h, 12._h,
+ 13._h, 14._h };
+ std::vector<Half> inputYData{ 5._h, 7._h,
+ 6._h, 8._h };
+ std::vector<Half> expectedOutput{ 6._h, 5._h,
+ 7._h, 6._h };
+
+ std::map<int, std::vector<Half>> inputTensorData = {{ 0, inputXData }, { 1, inputYData }};
+ std::map<int, std::vector<Half>> expectedOutputData = { { 0, expectedOutput } };
+
+ EndToEndLayerTestImpl<ArmnnType, ArmnnType>(std::move(network), inputTensorData, expectedOutputData, backends);
+}
+
+} // anonymous namespace