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 | |
| 7 | #include "CommonTestUtils.hpp" |
| 8 | |
| 9 | #include <ResolveType.hpp> |
| 10 | |
| 11 | #include <armnn/INetwork.hpp> |
| 12 | |
| 13 | #include <boost/test/unit_test.hpp> |
| 14 | |
| 15 | #include <vector> |
| 16 | |
| 17 | namespace |
| 18 | { |
| 19 | |
| 20 | template<armnn::DataType ArmnnTypeInput> |
| 21 | INetworkPtr CreateComparisonNetwork(const std::vector<TensorShape>& inputShapes, |
| 22 | const TensorShape& outputShape, |
| 23 | ComparisonOperation operation, |
| 24 | const float qScale = 1.0f, |
| 25 | const int32_t qOffset = 0) |
| 26 | { |
| 27 | using namespace armnn; |
| 28 | |
| 29 | INetworkPtr net(INetwork::Create()); |
| 30 | |
| 31 | ComparisonDescriptor descriptor(operation); |
| 32 | IConnectableLayer* comparisonLayer = net->AddComparisonLayer(descriptor, "comparison"); |
| 33 | |
| 34 | for (unsigned int i = 0; i < inputShapes.size(); ++i) |
| 35 | { |
| 36 | TensorInfo inputTensorInfo(inputShapes[i], ArmnnTypeInput, qScale, qOffset); |
| 37 | IConnectableLayer* input = net->AddInputLayer(boost::numeric_cast<LayerBindingId>(i)); |
| 38 | Connect(input, comparisonLayer, inputTensorInfo, 0, i); |
| 39 | } |
| 40 | |
| 41 | TensorInfo outputTensorInfo(outputShape, DataType::Boolean, qScale, qOffset); |
| 42 | IConnectableLayer* output = net->AddOutputLayer(0, "output"); |
| 43 | Connect(comparisonLayer, output, outputTensorInfo, 0, 0); |
| 44 | |
| 45 | return net; |
| 46 | } |
| 47 | |
| 48 | template<armnn::DataType ArmnnInType, |
| 49 | typename TInput = armnn::ResolveType<ArmnnInType>> |
| 50 | void ComparisonSimpleEndToEnd(const std::vector<BackendId>& backends, |
| 51 | ComparisonOperation operation, |
| 52 | const std::vector<uint8_t> expectedOutput) |
| 53 | { |
| 54 | using namespace armnn; |
| 55 | |
| 56 | const std::vector<TensorShape> inputShapes{{ 2, 2, 2, 2 }, { 2, 2, 2, 2 }}; |
| 57 | const TensorShape& outputShape = { 2, 2, 2, 2 }; |
| 58 | |
| 59 | // Builds up the structure of the network |
| 60 | INetworkPtr net = CreateComparisonNetwork<ArmnnInType>(inputShapes, outputShape, operation); |
| 61 | |
| 62 | BOOST_TEST_CHECKPOINT("create a network"); |
| 63 | |
| 64 | const std::vector<TInput> input0({ 1, 1, 1, 1, 5, 5, 5, 5, |
| 65 | 3, 3, 3, 3, 4, 4, 4, 4 }); |
| 66 | |
| 67 | const std::vector<TInput> input1({ 1, 1, 1, 1, 3, 3, 3, 3, |
| 68 | 5, 5, 5, 5, 4, 4, 4, 4 }); |
| 69 | |
| 70 | std::map<int, std::vector<TInput>> inputTensorData = {{ 0, input0 }, { 1, input1 }}; |
| 71 | std::map<int, std::vector<uint8_t>> expectedOutputData = {{ 0, expectedOutput }}; |
| 72 | |
| 73 | EndToEndLayerTestImpl<ArmnnInType, DataType::Boolean>(move(net), inputTensorData, expectedOutputData, backends); |
| 74 | } |
| 75 | |
| 76 | template<armnn::DataType ArmnnInType, |
| 77 | typename TInput = armnn::ResolveType<ArmnnInType>> |
| 78 | void ComparisonBroadcastEndToEnd(const std::vector<BackendId>& backends, |
| 79 | ComparisonOperation operation, |
| 80 | const std::vector<uint8_t> expectedOutput) |
| 81 | { |
| 82 | using namespace armnn; |
| 83 | |
| 84 | const std::vector<TensorShape> inputShapes{{ 1, 2, 2, 3 }, { 1, 1, 1, 3 }}; |
| 85 | const TensorShape& outputShape = { 1, 2, 2, 3 }; |
| 86 | |
| 87 | // Builds up the structure of the network |
| 88 | INetworkPtr net = CreateComparisonNetwork<ArmnnInType>(inputShapes, outputShape, operation); |
| 89 | |
| 90 | BOOST_TEST_CHECKPOINT("create a network"); |
| 91 | |
| 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 |