blob: e2a0d669f315afd7fab27165d5884980a73bda95 [file] [log] [blame]
Aron Virginas-Tar77bfb5e2019-10-16 17:45:38 +01001//
Mike Kellya9c32672023-12-04 17:23:09 +00002// Copyright © 2019-2021,2023 Arm Ltd and Contributors. All rights reserved.
Aron Virginas-Tar77bfb5e2019-10-16 17:45:38 +01003// SPDX-License-Identifier: MIT
4//
5#pragma once
6
Sadik Armagana097d2a2021-11-24 15:47:28 +00007#include <CommonTestUtils.hpp>
Aron Virginas-Tar77bfb5e2019-10-16 17:45:38 +01008
9#include <ResolveType.hpp>
10
11#include <armnn/INetwork.hpp>
12
Matthew Sloyan171214c2020-09-09 09:07:37 +010013#include <armnn/utility/NumericCast.hpp>
14
Sadik Armagan1625efc2021-06-10 18:24:34 +010015#include <doctest/doctest.h>
Aron Virginas-Tar77bfb5e2019-10-16 17:45:38 +010016
17#include <vector>
18
19namespace
20{
21
22template<armnn::DataType ArmnnTypeInput>
23INetworkPtr 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 Corbett5b8093c2021-10-22 11:12:07 +010038 TensorInfo inputTensorInfo(inputShapes[i], ArmnnTypeInput, qScale, qOffset, true);
Matthew Sloyan171214c2020-09-09 09:07:37 +010039 IConnectableLayer* input = net->AddInputLayer(armnn::numeric_cast<LayerBindingId>(i));
Aron Virginas-Tar77bfb5e2019-10-16 17:45:38 +010040 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
50template<armnn::DataType ArmnnInType,
51 typename TInput = armnn::ResolveType<ArmnnInType>>
52void 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 Armagan1625efc2021-06-10 18:24:34 +010064 CHECK(net);
Aron Virginas-Tar77bfb5e2019-10-16 17:45:38 +010065
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
Mike Kellya9c32672023-12-04 17:23:09 +000075 EndToEndLayerTestImpl<ArmnnInType, DataType::Boolean>(std::move(net), inputTensorData, expectedOutputData,
76 backends);
Aron Virginas-Tar77bfb5e2019-10-16 17:45:38 +010077}
78
79template<armnn::DataType ArmnnInType,
80 typename TInput = armnn::ResolveType<ArmnnInType>>
81void ComparisonBroadcastEndToEnd(const std::vector<BackendId>& backends,
82 ComparisonOperation operation,
83 const std::vector<uint8_t> expectedOutput)
84{
85 using namespace armnn;
86
87 const std::vector<TensorShape> inputShapes{{ 1, 2, 2, 3 }, { 1, 1, 1, 3 }};
88 const TensorShape& outputShape = { 1, 2, 2, 3 };
89
90 // Builds up the structure of the network
91 INetworkPtr net = CreateComparisonNetwork<ArmnnInType>(inputShapes, outputShape, operation);
92
Aron Virginas-Tar77bfb5e2019-10-16 17:45:38 +010093 const std::vector<TInput> input0({ 1, 2, 3, 1, 0, 6,
94 7, 8, 9, 10, 11, 12 });
95
96 const std::vector<TInput> input1({ 1, 1, 3 });
97
98 std::map<int, std::vector<TInput>> inputTensorData = {{ 0, input0 }, { 1, input1 }};
99 std::map<int, std::vector<uint8_t>> expectedOutputData = {{ 0, expectedOutput }};
100
Mike Kellya9c32672023-12-04 17:23:09 +0000101 EndToEndLayerTestImpl<ArmnnInType, DataType::Boolean>(std::move(net), inputTensorData, expectedOutputData,
102 backends);
Aron Virginas-Tar77bfb5e2019-10-16 17:45:38 +0100103}
104
105} // anonymous namespace