blob: a1a8bac0e7ced302519c7d3201119ccd92baa6e4 [file] [log] [blame]
Ryan OShea3c2795a2022-11-03 17:51:52 +00001//
2// Copyright © 2022 Arm Ltd and Contributors. All rights reserved.
3// SPDX-License-Identifier: MIT
4//
5#pragma once
6
Ryan OShea3c2795a2022-11-03 17:51:52 +00007#include <armnn/INetwork.hpp>
8
Ryan OShea3c2795a2022-11-03 17:51:52 +00009#include <CommonTestUtils.hpp>
Matthew Sloyan2523b792022-11-14 10:18:01 +000010#include <ResolveType.hpp>
11
12#include <doctest/doctest.h>
Ryan OShea3c2795a2022-11-03 17:51:52 +000013
14namespace
15{
16
17template<typename armnn::DataType DataType>
18armnn::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
34 IConnectableLayer* addition = network->AddAdditionLayer("addition");
35 IConnectableLayer* inputX = network->AddInputLayer(0, "inputX");
36 IConnectableLayer* inputY = network->AddInputLayer(1, "inputY");
37 IConnectableLayer* output = network->AddOutputLayer(0, "output");
38
39 Connect(inputX, addition, inputXTensorInfo, 0, 0);
40 Connect(inputY, addition, inputYTensorInfo, 0, 1);
41 Connect(addition, output, outputTensorInfo, 0, 0);
42
43 return network;
44}
45
46template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>>
47void AdditionEndToEnd(const std::vector<armnn::BackendId>& backends)
48{
49 using namespace armnn;
50
51 const TensorShape& inputXShape = { 2, 2, 2 };
52 const TensorShape& inputYShape = { 2, 2, 2 };
53 const TensorShape& outputShape = { 2, 2, 2 };
54
55 INetworkPtr network = CreateAdditionNetwork<ArmnnType>(inputXShape, inputYShape, outputShape);
56
57 CHECK(network);
58
59 std::vector<T> inputXData{ 1, 2,
60 3, 4,
61
62 9, 10,
63 11, 12 };
64 std::vector<T> inputYData{ 5, 7,
65 6, 8,
66
67 13, 15,
68 14, 16 };
69 std::vector<T> expectedOutput{ 6, 9,
70 9, 12,
71
72 22, 25,
73 25, 28 };
74
75 std::map<int, std::vector<T>> inputTensorData = {{ 0, inputXData }, {1, inputYData}};
76 std::map<int, std::vector<T>> expectedOutputData = { { 0, expectedOutput } };
77
78 EndToEndLayerTestImpl<ArmnnType, ArmnnType>(std::move(network), inputTensorData, expectedOutputData, backends);
79}
80
Matthew Sloyan2523b792022-11-14 10:18:01 +000081template<armnn::DataType ArmnnType>
82void AdditionEndToEndFloat16(const std::vector<armnn::BackendId>& backends)
83{
84 using namespace armnn;
85 using namespace half_float::literal;
86 using Half = half_float::half;
87
88 const TensorShape& inputXShape = { 2, 2 };
89 const TensorShape& inputYShape = { 2, 2 };
90 const TensorShape& outputShape = { 2, 2 };
91
92 INetworkPtr network = CreateAdditionNetwork<ArmnnType>(inputXShape, inputYShape, outputShape);
93 CHECK(network);
94
95 std::vector<Half> inputXData{ 1._h, 2._h,
96 3._h, 4._h };
97 std::vector<Half> inputYData{ 5._h, 7._h,
98 6._h, 8._h };
99 std::vector<Half> expectedOutput{ 6._h, 9._h,
100 9._h, 12._h };
101
102 std::map<int, std::vector<Half>> inputTensorData = {{ 0, inputXData }, { 1, inputYData }};
103 std::map<int, std::vector<Half>> expectedOutputData = { { 0, expectedOutput } };
104
105 EndToEndLayerTestImpl<ArmnnType, ArmnnType>(std::move(network), inputTensorData, expectedOutputData, backends);
106}
107
Ryan OShea3c2795a2022-11-03 17:51:52 +0000108} // anonymous namespaceS