Aron Virginas-Tar | 77bfb5e | 2019-10-16 17:45:38 +0100 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2017 Arm Ltd. All rights reserved. |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | #pragma once |
| 6 | |
Sadik Armagan | a097d2a | 2021-11-24 15:47:28 +0000 | [diff] [blame] | 7 | #include <CommonTestUtils.hpp> |
Aron Virginas-Tar | 77bfb5e | 2019-10-16 17:45:38 +0100 | [diff] [blame] | 8 | |
| 9 | #include <ResolveType.hpp> |
| 10 | |
| 11 | #include <armnn/INetwork.hpp> |
| 12 | |
Matthew Sloyan | 171214c | 2020-09-09 09:07:37 +0100 | [diff] [blame] | 13 | #include <armnn/utility/NumericCast.hpp> |
| 14 | |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 15 | #include <doctest/doctest.h> |
Aron Virginas-Tar | 77bfb5e | 2019-10-16 17:45:38 +0100 | [diff] [blame] | 16 | |
| 17 | #include <vector> |
| 18 | |
| 19 | namespace |
| 20 | { |
| 21 | |
| 22 | template<armnn::DataType ArmnnTypeInput> |
| 23 | INetworkPtr CreateComparisonNetwork(const std::vector<TensorShape>& inputShapes, |
| 24 | const TensorShape& outputShape, |
| 25 | ComparisonOperation operation, |
| 26 | const float qScale = 1.0f, |
| 27 | const int32_t qOffset = 0) |
| 28 | { |
| 29 | using namespace armnn; |
| 30 | |
| 31 | INetworkPtr net(INetwork::Create()); |
| 32 | |
| 33 | ComparisonDescriptor descriptor(operation); |
| 34 | IConnectableLayer* comparisonLayer = net->AddComparisonLayer(descriptor, "comparison"); |
| 35 | |
| 36 | for (unsigned int i = 0; i < inputShapes.size(); ++i) |
| 37 | { |
Cathal Corbett | 5b8093c | 2021-10-22 11:12:07 +0100 | [diff] [blame] | 38 | TensorInfo inputTensorInfo(inputShapes[i], ArmnnTypeInput, qScale, qOffset, true); |
Matthew Sloyan | 171214c | 2020-09-09 09:07:37 +0100 | [diff] [blame] | 39 | IConnectableLayer* input = net->AddInputLayer(armnn::numeric_cast<LayerBindingId>(i)); |
Aron Virginas-Tar | 77bfb5e | 2019-10-16 17:45:38 +0100 | [diff] [blame] | 40 | Connect(input, comparisonLayer, inputTensorInfo, 0, i); |
| 41 | } |
| 42 | |
| 43 | TensorInfo outputTensorInfo(outputShape, DataType::Boolean, qScale, qOffset); |
| 44 | IConnectableLayer* output = net->AddOutputLayer(0, "output"); |
| 45 | Connect(comparisonLayer, output, outputTensorInfo, 0, 0); |
| 46 | |
| 47 | return net; |
| 48 | } |
| 49 | |
| 50 | template<armnn::DataType ArmnnInType, |
| 51 | typename TInput = armnn::ResolveType<ArmnnInType>> |
| 52 | void ComparisonSimpleEndToEnd(const std::vector<BackendId>& backends, |
| 53 | ComparisonOperation operation, |
| 54 | const std::vector<uint8_t> expectedOutput) |
| 55 | { |
| 56 | using namespace armnn; |
| 57 | |
| 58 | const std::vector<TensorShape> inputShapes{{ 2, 2, 2, 2 }, { 2, 2, 2, 2 }}; |
| 59 | const TensorShape& outputShape = { 2, 2, 2, 2 }; |
| 60 | |
| 61 | // Builds up the structure of the network |
| 62 | INetworkPtr net = CreateComparisonNetwork<ArmnnInType>(inputShapes, outputShape, operation); |
| 63 | |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 64 | CHECK(net); |
Aron Virginas-Tar | 77bfb5e | 2019-10-16 17:45:38 +0100 | [diff] [blame] | 65 | |
| 66 | const std::vector<TInput> input0({ 1, 1, 1, 1, 5, 5, 5, 5, |
| 67 | 3, 3, 3, 3, 4, 4, 4, 4 }); |
| 68 | |
| 69 | const std::vector<TInput> input1({ 1, 1, 1, 1, 3, 3, 3, 3, |
| 70 | 5, 5, 5, 5, 4, 4, 4, 4 }); |
| 71 | |
| 72 | std::map<int, std::vector<TInput>> inputTensorData = {{ 0, input0 }, { 1, input1 }}; |
| 73 | std::map<int, std::vector<uint8_t>> expectedOutputData = {{ 0, expectedOutput }}; |
| 74 | |
| 75 | EndToEndLayerTestImpl<ArmnnInType, DataType::Boolean>(move(net), inputTensorData, expectedOutputData, backends); |
| 76 | } |
| 77 | |
| 78 | template<armnn::DataType ArmnnInType, |
| 79 | typename TInput = armnn::ResolveType<ArmnnInType>> |
| 80 | void ComparisonBroadcastEndToEnd(const std::vector<BackendId>& backends, |
| 81 | ComparisonOperation operation, |
| 82 | const std::vector<uint8_t> expectedOutput) |
| 83 | { |
| 84 | using namespace armnn; |
| 85 | |
| 86 | const std::vector<TensorShape> inputShapes{{ 1, 2, 2, 3 }, { 1, 1, 1, 3 }}; |
| 87 | const TensorShape& outputShape = { 1, 2, 2, 3 }; |
| 88 | |
| 89 | // Builds up the structure of the network |
| 90 | INetworkPtr net = CreateComparisonNetwork<ArmnnInType>(inputShapes, outputShape, operation); |
| 91 | |
Aron Virginas-Tar | 77bfb5e | 2019-10-16 17:45:38 +0100 | [diff] [blame] | 92 | const std::vector<TInput> input0({ 1, 2, 3, 1, 0, 6, |
| 93 | 7, 8, 9, 10, 11, 12 }); |
| 94 | |
| 95 | const std::vector<TInput> input1({ 1, 1, 3 }); |
| 96 | |
| 97 | std::map<int, std::vector<TInput>> inputTensorData = {{ 0, input0 }, { 1, input1 }}; |
| 98 | std::map<int, std::vector<uint8_t>> expectedOutputData = {{ 0, expectedOutput }}; |
| 99 | |
| 100 | EndToEndLayerTestImpl<ArmnnInType, DataType::Boolean>(move(net), inputTensorData, expectedOutputData, backends); |
| 101 | } |
| 102 | |
| 103 | } // anonymous namespace |