xref: /aosp_15_r20/external/armnn/src/backends/backendsCommon/test/ReduceEndToEndTestImpl.hpp (revision 89c4ff92f2867872bb9e2354d150bf0c8c502810)
1 //
2 // Copyright © 2023 Arm Ltd and Contributors. All rights reserved.
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>
CreateReduceNetwork(const armnn::TensorShape & inputShape,const armnn::TensorShape & outputShape,const armnn::ReduceDescriptor & descriptor,const float qScale=1.0f,const int32_t qOffset=0)18 armnn::INetworkPtr CreateReduceNetwork(const armnn::TensorShape& inputShape,
19                                         const armnn::TensorShape& outputShape,
20                                         const armnn::ReduceDescriptor& descriptor,
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 inputTensorInfo(inputShape, DataType, qScale, qOffset, true);
29     TensorInfo outputTensorInfo(outputShape, DataType, qScale, qOffset);
30 
31 
32     IConnectableLayer* reduce = network->AddReduceLayer(descriptor, "reduce");
33     IConnectableLayer* input   = network->AddInputLayer(0, "input");
34     IConnectableLayer* output  = network->AddOutputLayer(0, "output");
35 
36     Connect(input, reduce, inputTensorInfo, 0, 0);
37     Connect(reduce, output, outputTensorInfo, 0, 0);
38 
39     return network;
40 }
41 
42 template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>>
ReduceEndToEnd(const std::vector<armnn::BackendId> & backends)43 void ReduceEndToEnd(const std::vector<armnn::BackendId>& backends)
44 {
45     using namespace armnn;
46 
47     const TensorShape& inputShape = { 1, 1, 1, 5 };
48     const TensorShape& outputShape = { 1, 1, 1 };
49 
50     ReduceDescriptor descriptor;
51     descriptor.m_KeepDims = false;
52     descriptor.m_vAxis = { 3 };
53     descriptor.m_ReduceOperation = ReduceOperation::Sum;
54 
55     INetworkPtr network = CreateReduceNetwork<ArmnnType>(inputShape, outputShape, descriptor);
56 
57     CHECK(network);
58 
59     std::vector<float> floatInputData({ 5.0f, 2.0f, 8.0f, 10.0f, 9.0f });
60     std::vector<float> floatOutputData({ 34.0f });
61 
62     std::vector<T> inputData = armnnUtils::QuantizedVector<T>(floatInputData);
63     std::vector<T> outputData = armnnUtils::QuantizedVector<T>(floatOutputData);
64 
65     std::map<int, std::vector<T>> inputTensorData = { { 0, inputData } };
66     std::map<int, std::vector<T>> expectedOutputData = { { 0, outputData } };
67 
68     EndToEndLayerTestImpl<ArmnnType, ArmnnType>(std::move(network), inputTensorData, expectedOutputData, backends);
69 }
70 } // anonymous namespace
71