Ryan OShea | 3c2795a | 2022-11-03 17:51:52 +0000 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2022 Arm Ltd and Contributors. All rights reserved. |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | #pragma once |
| 6 | |
Ryan OShea | 3c2795a | 2022-11-03 17:51:52 +0000 | [diff] [blame] | 7 | #include <armnn/INetwork.hpp> |
| 8 | |
Ryan OShea | 3c2795a | 2022-11-03 17:51:52 +0000 | [diff] [blame] | 9 | #include <CommonTestUtils.hpp> |
Matthew Sloyan | 2523b79 | 2022-11-14 10:18:01 +0000 | [diff] [blame] | 10 | #include <ResolveType.hpp> |
| 11 | |
| 12 | #include <doctest/doctest.h> |
Ryan OShea | 3c2795a | 2022-11-03 17:51:52 +0000 | [diff] [blame] | 13 | |
| 14 | namespace |
| 15 | { |
| 16 | |
| 17 | template<typename armnn::DataType DataType> |
| 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 | |
| 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 | |
| 46 | template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>> |
| 47 | void 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 Sloyan | 2523b79 | 2022-11-14 10:18:01 +0000 | [diff] [blame] | 81 | template<armnn::DataType ArmnnType> |
| 82 | void 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 | |
Cathal Corbett | bd18eab | 2022-11-15 12:56:16 +0000 | [diff] [blame] | 108 | } // anonymous namespace |