blob: 589cc03cbc6818f8effd8e36de52b38e6d7ca0df [file] [log] [blame]
Sadik Armagana2747482021-02-09 10:28:54 +00001//
2// Copyright © 2021 Arm Ltd and Contributors. All rights reserved.
3// SPDX-License-Identifier: MIT
4//
5
6#include "ReductionTestImpl.hpp"
7
8#include <backendsCommon/test/DataTypeUtils.hpp>
9#include <backendsCommon/test/TensorCopyUtils.hpp>
10#include <backendsCommon/test/WorkloadTestUtils.hpp>
11
12#include <test/TensorHelpers.hpp>
13
14#include <iostream>
15
16namespace
17{
18
19template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>>
20LayerTestResult<float, 4> ReductionTestCommon(
21 armnn::IWorkloadFactory& workloadFactory,
22 const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager,
23 const armnn::ITensorHandleFactory& tensorHandleFactory,
24 const armnn::TensorInfo inputTensorInfo,
25 const armnn::TensorInfo outputTensorInfo,
26 const std::vector<float>& inputData,
27 const std::vector<float>& outputData,
28 const std::vector<int32_t> vAxis,
29 const armnn::ReduceOperation reduceOperation,
30 bool keepDims = false)
31{
32 IgnoreUnused(memoryManager);
33 auto inputTensor = MakeTensor<T, 4>(inputTensorInfo, ConvertToDataType<ArmnnType>(inputData, inputTensorInfo));
34
35 LayerTestResult<float, 4> result(outputTensorInfo);
36 result.outputExpected = MakeTensor<float, 4>(outputTensorInfo, outputData);
37
38 std::unique_ptr<armnn::ITensorHandle> inputHandle = tensorHandleFactory.CreateTensorHandle(inputTensorInfo);
39 std::unique_ptr<armnn::ITensorHandle> outputHandle = tensorHandleFactory.CreateTensorHandle(outputTensorInfo);
40
41 armnn::ReduceQueueDescriptor descriptor;
42 std::vector<uint32_t> updated_idx;
43 uint32_t resolvedAxis = 0;
44 for (uint32_t i = 0; i < vAxis.size(); ++i)
45 {
46 if (vAxis[i] < 0)
47 {
48 resolvedAxis = inputTensorInfo.GetNumDimensions() + static_cast<uint32_t>(vAxis[i]);
49 } else
50 {
51 resolvedAxis = static_cast<uint32_t>(vAxis[i]);
52 }
53
54 updated_idx.push_back(resolvedAxis);
55 }
56
57 descriptor.m_Parameters.m_vAxis = updated_idx;
58 descriptor.m_Parameters.m_ReduceOperation = reduceOperation;
59 descriptor.m_Parameters.m_KeepDims = keepDims;
60 armnn::WorkloadInfo info;
61
62 AddInputToWorkload(descriptor, info, inputTensorInfo, inputHandle.get());
63 AddOutputToWorkload(descriptor, info, outputTensorInfo, outputHandle.get());
64
65 std::unique_ptr<armnn::IWorkload> workload = workloadFactory.CreateReduce(descriptor, info);
66
67 inputHandle->Allocate();
68 outputHandle->Allocate();
69
70 CopyDataToITensorHandle(inputHandle.get(), inputTensor.origin());
71
72 workload->Execute();
73
74 CopyDataFromITensorHandle(result.output.origin(), outputHandle.get());
75
76 return result;
77}
78
79} // namespace
80
81template<armnn::DataType ArmnnType, typename T>
82LayerTestResult<float, 4> ReduceMaxSimpleTest(
83 armnn::IWorkloadFactory& workloadFactory,
84 const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager,
85 const armnn::ITensorHandleFactory& tensorHandleFactory)
86{
87 const armnn::TensorShape inputShape{ 1, 1, 2, 3 };
88 const armnn::TensorShape outputShape{ 1, 1, 1, 3};
89
90 armnn::TensorInfo inputTensorInfo(inputShape, ArmnnType);
91
92 if (armnn::IsQuantizedType<T>())
93 {
94 inputTensorInfo.SetQuantizationScale(1.0f);
95 inputTensorInfo.SetQuantizationOffset(0);
96 }
97
98 armnn::TensorInfo outputTensorInfo(outputShape, armnn::DataType::Float32);
99
100 std::vector<float> inputValues
101 ({
102 1001.0f, 11.0f, 1003.0f,
103 10.0f, 1002.0f, 12.0f
104 });
105 std::vector<float> outputValues
106 ({
107 1001.0f, 1002.0f, 1003.0f
108 });
109
110 return ReductionTestCommon<ArmnnType>(workloadFactory,
111 memoryManager,
112 tensorHandleFactory,
113 inputTensorInfo,
114 outputTensorInfo,
115 inputValues,
116 outputValues,
117 { 2 },
118 armnn::ReduceOperation::Max);
119}
120
121template<armnn::DataType ArmnnType, typename T>
122LayerTestResult<float, 4> ReduceMaxNegativeAxisTest(
123 armnn::IWorkloadFactory& workloadFactory,
124 const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager,
125 const armnn::ITensorHandleFactory& tensorHandleFactory)
126{
127 const armnn::TensorShape inputShape{ 1, 1, 2, 3 };
128 const armnn::TensorShape outputShape{ 1, 1, 2, 1};
129
130 armnn::TensorInfo inputTensorInfo(inputShape, ArmnnType);
131
132 if (armnn::IsQuantizedType<T>())
133 {
134 inputTensorInfo.SetQuantizationScale(1.0f);
135 inputTensorInfo.SetQuantizationOffset(0);
136 }
137
138 armnn::TensorInfo outputTensorInfo(outputShape, armnn::DataType::Float32);
139
140 std::vector<float> inputValues
141 ({
142 1001.0f, 11.0f, 1003.0f,
143 10.0f, 1002.0f, 12.0f
144 });
145 std::vector<float> outputValues
146 ({
147 1003.0f, 1002.0f
148 });
149
150 return ReductionTestCommon<ArmnnType>(workloadFactory,
151 memoryManager,
152 tensorHandleFactory,
153 inputTensorInfo,
154 outputTensorInfo,
155 inputValues,
156 outputValues,
157 { -1 },
158 armnn::ReduceOperation::Max,
159 true);
160}
161
162template<armnn::DataType ArmnnType, typename T>
163LayerTestResult<float, 4> ReduceMaxSimpleTest2(
164 armnn::IWorkloadFactory& workloadFactory,
165 const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager,
166 const armnn::ITensorHandleFactory& tensorHandleFactory)
167{
168 const armnn::TensorShape inputShape{ 1, 1, 2, 3 };
169 const armnn::TensorShape outputShape{ 1, 1, 2, 1 };
170
171 armnn::TensorInfo inputTensorInfo(inputShape, ArmnnType);
172
173 if (armnn::IsQuantizedType<T>())
174 {
175 inputTensorInfo.SetQuantizationScale(1.0f);
176 inputTensorInfo.SetQuantizationOffset(0);
177 }
178
179 armnn::TensorInfo outputTensorInfo(outputShape, armnn::DataType::Float32);
180
181 std::vector<float> inputValues
182 ({
183 1.0f, 3.0f, 2.0f,
184 6.0f, 4.0f, 5.0f
185 });
186
187 std::vector<float> outputValues
188 ({
189 3.0f, 6.0f
190 });
191
192 return ReductionTestCommon<ArmnnType>(workloadFactory,
193 memoryManager,
194 tensorHandleFactory,
195 inputTensorInfo,
196 outputTensorInfo,
197 inputValues,
198 outputValues,
199 { 3 },
200 armnn::ReduceOperation::Max,
201 true);
202}
203
204template<armnn::DataType ArmnnType, typename T>
205LayerTestResult<float, 4> ReduceMinSimpleTest(
206 armnn::IWorkloadFactory& workloadFactory,
207 const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager,
208 const armnn::ITensorHandleFactory& tensorHandleFactory)
209{
210 const armnn::TensorShape inputShape { 1, 1, 2, 3 };
211 const armnn::TensorShape outputShape { 1, 1, 1, 3};
212
213 armnn::TensorInfo inputTensorInfo(inputShape, ArmnnType);
214
215 if (armnn::IsQuantizedType<T>())
216 {
217 inputTensorInfo.SetQuantizationScale(1.0f);
218 inputTensorInfo.SetQuantizationOffset(0);
219 }
220
221 armnn::TensorInfo outputTensorInfo(outputShape, armnn::DataType::Float32);
222
223 std::vector<float> inputValues
224 ({
225 1001.0f, 11.0f, 1003.0f,
226 10.0f, 1002.0f, 12.0f
227 });
228 std::vector<float> outputValues
229 ({
230 10.0f, 11.0f, 12.0f
231 });
232
233 return ReductionTestCommon<ArmnnType>(workloadFactory,
234 memoryManager,
235 tensorHandleFactory,
236 inputTensorInfo,
237 outputTensorInfo,
238 inputValues,
239 outputValues,
240 { 2 },
241 armnn::ReduceOperation::Min);
242}
243
244template<armnn::DataType ArmnnType, typename T>
245LayerTestResult<float, 4> ReduceMinNegativeAxisTest(
246 armnn::IWorkloadFactory& workloadFactory,
247 const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager,
248 const armnn::ITensorHandleFactory& tensorHandleFactory)
249{
250 const armnn::TensorShape inputShape{ 1, 1, 2, 3 };
251 const armnn::TensorShape outputShape{ 1, 1, 2, 1};
252
253 armnn::TensorInfo inputTensorInfo(inputShape, ArmnnType);
254
255 if (armnn::IsQuantizedType<T>())
256 {
257 inputTensorInfo.SetQuantizationScale(1.0f);
258 inputTensorInfo.SetQuantizationOffset(0);
259 }
260
261 armnn::TensorInfo outputTensorInfo(outputShape, armnn::DataType::Float32);
262
263 std::vector<float> inputValues
264 ({
265 1001.0f, 11.0f, 1003.0f,
266 10.0f, 1002.0f, 12.0f
267 });
268 std::vector<float> outputValues
269 ({
270 11.0f, 10.0f
271 });
272
273 return ReductionTestCommon<ArmnnType>(workloadFactory,
274 memoryManager,
275 tensorHandleFactory,
276 inputTensorInfo,
277 outputTensorInfo,
278 inputValues,
279 outputValues,
280 { -1 },
281 armnn::ReduceOperation::Min,
282 true);
283}
284
285// Explicit template specializations
286template LayerTestResult<float, 4>
287ReduceMaxSimpleTest<armnn::DataType::Float32>(
288 armnn::IWorkloadFactory& workloadFactory,
289 const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager,
290 const armnn::ITensorHandleFactory& tensorHandleFactory);
291
292template LayerTestResult<float, 4>
293ReduceMaxNegativeAxisTest<armnn::DataType::Float32>(
294 armnn::IWorkloadFactory& workloadFactory,
295 const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager,
296 const armnn::ITensorHandleFactory& tensorHandleFactory);
297
298template LayerTestResult<float, 4>
299ReduceMaxSimpleTest2<armnn::DataType::Float32>(
300 armnn::IWorkloadFactory& workloadFactory,
301 const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager,
302 const armnn::ITensorHandleFactory& tensorHandleFactory);
303
304template LayerTestResult<float, 4>
305ReduceMinSimpleTest<armnn::DataType::Float32>(
306 armnn::IWorkloadFactory& workloadFactory,
307 const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager,
308 const armnn::ITensorHandleFactory& tensorHandleFactory);
309
310template LayerTestResult<float, 4>
311ReduceMinNegativeAxisTest<armnn::DataType::Float32>(
312 armnn::IWorkloadFactory& workloadFactory,
313 const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager,
314 const armnn::ITensorHandleFactory& tensorHandleFactory);
315