• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright © 2022 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>
CreateAdditionNetwork(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 CreateAdditionNetwork(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* addition = network->AddAdditionLayer("addition");
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, addition, inputXTensorInfo, 0, 0);
41     Connect(inputY, addition, inputYTensorInfo, 0, 1);
42     Connect(addition, output, outputTensorInfo, 0, 0);
43 
44     return network;
45 }
46 
47 template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>>
AdditionEndToEnd(const std::vector<armnn::BackendId> & backends)48 void AdditionEndToEnd(const std::vector<armnn::BackendId>& backends)
49 {
50     using namespace armnn;
51 
52     const TensorShape& inputXShape = { 2, 2, 2 };
53     const TensorShape& inputYShape = { 2, 2, 2 };
54     const TensorShape& outputShape = { 2, 2, 2 };
55 
56     INetworkPtr network = CreateAdditionNetwork<ArmnnType>(inputXShape, inputYShape, outputShape);
57 
58     CHECK(network);
59 
60     std::vector<T> inputXData{ 1, 2,
61                                3, 4,
62 
63                                9, 10,
64                                11, 12 };
65     std::vector<T> inputYData{ 5, 7,
66                                6, 8,
67 
68                                13, 15,
69                                14, 16 };
70     std::vector<T> expectedOutput{ 6, 9,
71                                    9, 12,
72 
73                                    22, 25,
74                                    25, 28 };
75 
76     std::map<int, std::vector<T>> inputTensorData = {{ 0, inputXData }, {1, inputYData}};
77     std::map<int, std::vector<T>> expectedOutputData = { { 0, expectedOutput } };
78 
79     EndToEndLayerTestImpl<ArmnnType, ArmnnType>(std::move(network), inputTensorData, expectedOutputData, backends);
80 }
81 
82 template<armnn::DataType ArmnnType>
AdditionEndToEndFloat16(const std::vector<armnn::BackendId> & backends)83 void AdditionEndToEndFloat16(const std::vector<armnn::BackendId>& backends)
84 {
85     using namespace armnn;
86     using namespace half_float::literal;
87     using Half = half_float::half;
88 
89     const TensorShape& inputXShape = { 2, 2 };
90     const TensorShape& inputYShape = { 2, 2 };
91     const TensorShape& outputShape = { 2, 2 };
92 
93     INetworkPtr network = CreateAdditionNetwork<ArmnnType>(inputXShape, inputYShape, outputShape);
94     CHECK(network);
95 
96     std::vector<Half> inputXData{ 1._h, 2._h,
97                                   3._h, 4._h };
98     std::vector<Half> inputYData{ 5._h, 7._h,
99                                   6._h, 8._h };
100     std::vector<Half> expectedOutput{ 6._h, 9._h,
101                                       9._h, 12._h };
102 
103     std::map<int, std::vector<Half>> inputTensorData = {{ 0, inputXData }, { 1, inputYData }};
104     std::map<int, std::vector<Half>> expectedOutputData = { { 0, expectedOutput } };
105 
106     EndToEndLayerTestImpl<ArmnnType, ArmnnType>(std::move(network), inputTensorData, expectedOutputData, backends);
107 }
108 
109 } // anonymous namespace
110