Nikhil Raj | 9a33946 | 2022-12-05 11:24:35 +0000 | [diff] [blame] | 1 | // |
Mike Kelly | 1ec5f85 | 2023-04-05 12:51:10 +0100 | [diff] [blame] | 2 | // Copyright © 2022-2023 Arm Ltd and Contributors. All rights reserved. |
Nikhil Raj | 9a33946 | 2022-12-05 11:24:35 +0000 | [diff] [blame] | 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> |
| 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 | |
Mike Kelly | 1ec5f85 | 2023-04-05 12:51:10 +0100 | [diff] [blame] | 33 | ARMNN_NO_DEPRECATE_WARN_BEGIN |
Nikhil Raj | 9a33946 | 2022-12-05 11:24:35 +0000 | [diff] [blame] | 34 | IConnectableLayer* multiplication = network->AddMultiplicationLayer("multiplication"); |
Mike Kelly | 1ec5f85 | 2023-04-05 12:51:10 +0100 | [diff] [blame] | 35 | ARMNN_NO_DEPRECATE_WARN_END |
Nikhil Raj | 9a33946 | 2022-12-05 11:24:35 +0000 | [diff] [blame] | 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>> |
| 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> |
| 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 |