1 //
2 // Copyright © 2022-2023 Arm Ltd and Contributors. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 #pragma once
6
7 #include <armnn/INetwork.hpp>
8
9 #include <CommonTestUtils.hpp>
10 #include <ResolveType.hpp>
11
12 #include <doctest/doctest.h>
13
14 namespace
15 {
16
17 template<typename armnn::DataType DataType>
CreateMultiplicationNetwork(const armnn::TensorShape & inputXShape,const armnn::TensorShape & inputYShape,const armnn::TensorShape & outputShape,const float qScale=1.0f,const int32_t qOffset=0)18 armnn::INetworkPtr CreateMultiplicationNetwork(const armnn::TensorShape& inputXShape,
19 const armnn::TensorShape& inputYShape,
20 const armnn::TensorShape& outputShape,
21 const float qScale = 1.0f,
22 const int32_t qOffset = 0)
23 {
24 using namespace armnn;
25
26 INetworkPtr network(INetwork::Create());
27
28 TensorInfo inputXTensorInfo(inputXShape, DataType, qScale, qOffset, true);
29 TensorInfo inputYTensorInfo(inputYShape, DataType, qScale, qOffset, true);
30
31 TensorInfo outputTensorInfo(outputShape, DataType, qScale, qOffset);
32
33 ARMNN_NO_DEPRECATE_WARN_BEGIN
34 IConnectableLayer* multiplication = network->AddMultiplicationLayer("multiplication");
35 ARMNN_NO_DEPRECATE_WARN_END
36 IConnectableLayer* inputX = network->AddInputLayer(0, "inputX");
37 IConnectableLayer* inputY = network->AddInputLayer(1, "inputY");
38 IConnectableLayer* output = network->AddOutputLayer(0, "output");
39
40 Connect(inputX, multiplication, inputXTensorInfo, 0, 0);
41 Connect(inputY, multiplication, inputYTensorInfo, 0, 1);
42 Connect(multiplication, output, outputTensorInfo, 0, 0);
43
44 return network;
45 }
46
47 template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>>
MultiplicationEndToEnd(const std::vector<armnn::BackendId> & backends)48 void MultiplicationEndToEnd(const std::vector<armnn::BackendId>& backends)
49 {
50 using namespace armnn;
51
52 const TensorShape& inputXShape = { 2, 2 };
53 const TensorShape& inputYShape = { 2, 2 };
54 const TensorShape& outputShape = { 2, 2 };
55
56 INetworkPtr network = CreateMultiplicationNetwork<ArmnnType>(inputXShape, inputYShape, outputShape);
57
58 CHECK(network);
59
60 std::vector<T> inputXData{ 1, 2, 3, 4 };
61 std::vector<T> inputYData{ 5, 2, 6, 3 };
62 std::vector<T> expectedOutput{ 5, 4, 18, 12 };
63
64 std::map<int, std::vector<T>> inputTensorData = {{ 0, inputXData }, {1, inputYData}};
65 std::map<int, std::vector<T>> expectedOutputData = { { 0, expectedOutput } };
66
67 EndToEndLayerTestImpl<ArmnnType, ArmnnType>(std::move(network), inputTensorData, expectedOutputData, backends);
68 }
69
70 template<armnn::DataType ArmnnType>
MultiplicationEndToEndFloat16(const std::vector<armnn::BackendId> & backends)71 void MultiplicationEndToEndFloat16(const std::vector<armnn::BackendId>& backends)
72 {
73 using namespace armnn;
74 using namespace half_float::literal;
75 using Half = half_float::half;
76
77 const TensorShape& inputXShape = { 2, 2 };
78 const TensorShape& inputYShape = { 2, 2 };
79 const TensorShape& outputShape = { 2, 2 };
80
81 INetworkPtr network = CreateMultiplicationNetwork<ArmnnType>(inputXShape, inputYShape, outputShape);
82 CHECK(network);
83
84 std::vector<Half> inputXData{ 1._h, 2._h,
85 3._h, 4._h };
86 std::vector<Half> inputYData{ 1._h, 2._h,
87 3._h, 4._h };
88 std::vector<Half> expectedOutput{ 1._h, 4._h,
89 9._h, 16._h };
90
91 std::map<int, std::vector<Half>> inputTensorData = {{ 0, inputXData }, { 1, inputYData }};
92 std::map<int, std::vector<Half>> expectedOutputData = { { 0, expectedOutput } };
93
94 EndToEndLayerTestImpl<ArmnnType, ArmnnType>(std::move(network), inputTensorData, expectedOutputData, backends);
95 }
96
97 } // anonymous namespace
98