blob: 79462d16e49b35365c097fee473d28f836f1b986 [file] [log] [blame]
Aron Virginas-Tar00d306e2019-08-28 18:08:46 +01001//
2// Copyright © 2017 Arm Ltd. All rights reserved.
3// SPDX-License-Identifier: MIT
4//
5
6#pragma once
7
8#include "LayerTestResult.hpp"
9
10#include <ResolveType.hpp>
11
12#include <armnn/ArmNN.hpp>
13
Matteo Martincighe5b8eb92019-11-28 15:45:42 +000014#include <armnn/backends/IBackendInternal.hpp>
Aron Virginas-Tar00d306e2019-08-28 18:08:46 +010015#include <backendsCommon/WorkloadFactory.hpp>
16
17#include <backendsCommon/test/DataTypeUtils.hpp>
18#include <backendsCommon/test/TensorCopyUtils.hpp>
19#include <backendsCommon/test/WorkloadTestUtils.hpp>
20
21#include <test/TensorHelpers.hpp>
22
23namespace
24{
25
26template<armnn::DataType ArmnnType,
27 std::size_t InputDim,
28 std::size_t OutputDim,
29 typename T = armnn::ResolveType<ArmnnType>>
30LayerTestResult<T, OutputDim> BatchToSpaceNdHelper(
31 armnn::IWorkloadFactory &workloadFactory,
32 const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager,
33 const armnn::DataLayout& dataLayout,
34 const unsigned int *inputShape,
35 const std::vector<float> &inputData,
36 const std::vector<unsigned int> &blockShape,
37 const std::vector<std::pair<unsigned int, unsigned int>> &crops,
38 const unsigned int *outputShape,
39 const std::vector<float> &outputData,
40 float scale = 1.0f,
41 int32_t offset = 0)
42{
43 armnn::TensorInfo inputTensorInfo(InputDim, inputShape, ArmnnType);
44 armnn::TensorInfo outputTensorInfo(OutputDim, outputShape, ArmnnType);
45
46 inputTensorInfo.SetQuantizationScale(scale);
47 inputTensorInfo.SetQuantizationOffset(offset);
48
49 outputTensorInfo.SetQuantizationScale(scale);
50 outputTensorInfo.SetQuantizationOffset(offset);
51
52 auto input = MakeTensor<T, InputDim>(inputTensorInfo, ConvertToDataType<ArmnnType>(inputData, inputTensorInfo));
53
54 LayerTestResult<T, OutputDim> result(outputTensorInfo);
55 result.outputExpected = MakeTensor<T, OutputDim>(outputTensorInfo,
56 ConvertToDataType<ArmnnType>(outputData, outputTensorInfo));
57
58 std::unique_ptr<armnn::ITensorHandle> inputHandle = workloadFactory.CreateTensorHandle(inputTensorInfo);
59 std::unique_ptr<armnn::ITensorHandle> outputHandle = workloadFactory.CreateTensorHandle(outputTensorInfo);
60
61 armnn::BatchToSpaceNdQueueDescriptor data;
62 data.m_Parameters.m_DataLayout = dataLayout;
63 data.m_Parameters.m_BlockShape = blockShape;
64 data.m_Parameters.m_Crops = crops;
65 armnn::WorkloadInfo info;
66 AddInputToWorkload(data, info, inputTensorInfo, inputHandle.get());
67 AddOutputToWorkload(data, info, outputTensorInfo, outputHandle.get());
68
69 std::unique_ptr<armnn::IWorkload> workload = workloadFactory.CreateBatchToSpaceNd(data, info);
70
71 inputHandle->Allocate();
72 outputHandle->Allocate();
73
74 CopyDataToITensorHandle(inputHandle.get(), input.origin());
75
76 workload->PostAllocationConfigure();
77 workload->Execute();
78
79 CopyDataFromITensorHandle(&result.output[0][0][0][0], outputHandle.get());
80
81 return result;
82}
83
84} // anonymous namespace
85
86template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>>
87LayerTestResult<T, 4> BatchToSpaceNdNhwcTest1(
88 armnn::IWorkloadFactory& workloadFactory,
89 const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager)
90{
91 const unsigned int inputShape[] = {4, 2, 2, 1};
92 const unsigned int outputShape[] = {1, 4, 4, 1};
93
94 std::vector<float> input({
95 // Batch 0, Height 0, Width (2) x Channel (1)
96 1.0f, 3.0f,
97 // Batch 0, Height 1, Width (2) x Channel (1)
98 9.0f, 11.0f,
99
100
101 // Batch 1, Height 0, Width (2) x Channel (1)
102 2.0f, 4.0f,
103 // Batch 1, Height 1, Width (2) x Channel (1)
104 10.0f, 12.0f,
105
106
107 // Batch 2, Height 0, Width (2) x Channel (1)
108 5.0f, 7.0f,
109 // Batch 2, Height 1, Width (2) x Channel (1)
110 13.0f, 15.0f,
111
112 // Batch 3, Height 0, Width (2) x Channel (3)
113 6.0f, 8.0f,
114 // Batch 3, Height 1, Width (2) x Channel (1)
115 14.0f, 16.0f
116 });
117
118 std::vector<float> expectedOutput({
119 1.0f, 2.0f, 3.0f, 4.0f,
120 5.0f, 6.0f, 7.0f, 8.0f,
121 9.0f, 10.0f, 11.0f, 12.0f,
122 13.0f, 14.0f, 15.0f, 16.0f
123 });
124
125 std::vector<unsigned int> blockShape {2, 2};
126 std::vector<std::pair<unsigned int, unsigned int>> crops = {{0, 0}, {0, 0}};
127
128 return BatchToSpaceNdHelper<ArmnnType, 4, 4>(workloadFactory, memoryManager,
129 armnn::DataLayout::NHWC, inputShape, input, blockShape,
130 crops, outputShape, expectedOutput);
131}
132
133template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>>
134LayerTestResult<T, 4> BatchToSpaceNdNhwcTest2(
135 armnn::IWorkloadFactory& workloadFactory,
136 const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager)
137{
138 const unsigned int inputShape[] = {4, 1, 1, 1};
139 const unsigned int outputShape[] = {1, 2, 2, 1};
140
141 std::vector<float> input({
142 // Batch 0, Height 0, Width (2) x Channel (1)
143 1.0f, 2.0f, 3.0f, 4.0f
144 });
145
146 std::vector<float> expectedOutput({1.0f, 2.0f, 3.0f, 4.0f});
147
148 std::vector<unsigned int> blockShape({2, 2});
149 std::vector<std::pair<unsigned int, unsigned int>> crops = {{0, 0}, {0, 0}};
150
151 return BatchToSpaceNdHelper<ArmnnType, 4, 4>(workloadFactory, memoryManager,
152 armnn::DataLayout::NHWC, inputShape, input, blockShape,
153 crops, outputShape, expectedOutput);
154}
155
156template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>>
157LayerTestResult<T, 4> BatchToSpaceNdNhwcTest3(
158 armnn::IWorkloadFactory& workloadFactory,
159 const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager)
160{
161 const unsigned int inputShape[] = {4, 1, 1, 3};
162 const unsigned int outputShape[] = {1, 2, 2, 3};
163
164 std::vector<float> input({1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f, 9.0f, 10.0f, 11.0f, 12.0f});
165
166 std::vector<float> expectedOutput({1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f, 9.0f, 10.0f, 11.0f, 12.0f});
167
168 std::vector<unsigned int> blockShape({2, 2});
169 std::vector<std::pair<unsigned int, unsigned int>> crops = {{0, 0}, {0, 0}};
170
171 return BatchToSpaceNdHelper<ArmnnType, 4, 4>(workloadFactory, memoryManager,
172 armnn::DataLayout::NHWC, inputShape, input, blockShape,
173 crops, outputShape, expectedOutput);
174}
175
176template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>>
177LayerTestResult<T, 4> BatchToSpaceNdNhwcTest4(
178 armnn::IWorkloadFactory& workloadFactory,
179 const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager)
180{
181 const unsigned int inputShape[] = {8, 1, 3, 1};
182 const unsigned int outputShape[] = {2, 2, 4, 1};
183
184 std::vector<float> input({
185 0.0f, 1.0f, 3.0f,
186 0.0f, 9.0f, 11.0f,
187 0.0f, 2.0f, 4.0f,
188 0.0f, 10.0f, 12.0f,
189 0.0f, 5.0f, 7.0f,
190 0.0f, 13.0f, 15.0f,
191 0.0f, 6.0f, 8.0f,
192 0.0f, 14.0f, 16.0f
193 });
194
195 std::vector<float> expectedOutput({
196 1.0f, 2.0f, 3.0f, 4.0f,
197 5.0f, 6.0f, 7.0f, 8.0f,
198 9.0f, 10.0f, 11.0f, 12.0f,
199 13.0f, 14.0f, 15.0f, 16.0f
200 });
201
202 std::vector<unsigned int> blockShape({2, 2});
203 std::vector<std::pair<unsigned int, unsigned int>> crops = {{0, 0}, {2, 0}};
204
205 return BatchToSpaceNdHelper<ArmnnType, 4, 4>(workloadFactory, memoryManager,
206 armnn::DataLayout::NHWC, inputShape, input, blockShape,
207 crops, outputShape, expectedOutput);
208}
209
210template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>>
211LayerTestResult<T, 4> BatchToSpaceNdNhwcTest5(
212 armnn::IWorkloadFactory& workloadFactory,
213 const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager)
214{
215 const unsigned int inputShape[] = {4, 2, 2, 1};
216 const unsigned int outputShape[] = {1, 4, 4, 1};
217
218 std::vector<float> input({1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16});
219 std::vector<float> expectedOutput({1, 5, 2, 6, 9, 13, 10, 14, 3, 7, 4, 8, 11, 15, 12, 16});
220
221 std::vector<unsigned int> blockShape({2, 2});
222 std::vector<std::pair<unsigned int, unsigned int>> crops = {{0, 0}, {0, 0}};
223
224 return BatchToSpaceNdHelper<ArmnnType, 4, 4>(workloadFactory, memoryManager, armnn::DataLayout::NHWC, inputShape,
225 input, blockShape, crops, outputShape, expectedOutput);
226}
227
228template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>>
229LayerTestResult<T, 4> BatchToSpaceNdNhwcTest6(
230 armnn::IWorkloadFactory& workloadFactory,
231 const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager)
232{
233 const unsigned int inputShape[] = {4, 1, 1, 1};
234 const unsigned int outputShape[] = {1, 2, 2, 1};
235
236 std::vector<float> input({
237 // Batch 0, Height 0, Width (2) x Channel (1)
238 1, 2, 3, 4
239 });
240
241 std::vector<float> expectedOutput({1, 2, 3, 4});
242
243 std::vector<unsigned int> blockShape({2, 2});
244 std::vector<std::pair<unsigned int, unsigned int>> crops = {{0, 0}, {0, 0}};
245
246 return BatchToSpaceNdHelper<ArmnnType, 4, 4>(workloadFactory, memoryManager,
247 armnn::DataLayout::NHWC, inputShape, input, blockShape,
248 crops, outputShape, expectedOutput);
249}
250
251template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>>
252LayerTestResult<T, 4> BatchToSpaceNdNhwcTest7(
253 armnn::IWorkloadFactory& workloadFactory,
254 const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager)
255{
256 const unsigned int inputShape[] = {4, 1, 1, 3};
257 const unsigned int outputShape[] = {1, 2, 2, 3};
258
259 std::vector<float> input({1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12});
260
261 std::vector<float> expectedOutput({1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12});
262
263 std::vector<unsigned int> blockShape({2, 2});
264 std::vector<std::pair<unsigned int, unsigned int>> crops = {{0, 0}, {0, 0}};
265
266 return BatchToSpaceNdHelper<ArmnnType, 4, 4>(workloadFactory, memoryManager,
267 armnn::DataLayout::NHWC, inputShape, input, blockShape,
268 crops, outputShape, expectedOutput);
269}
270
271template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>>
272LayerTestResult<T, 4> BatchToSpaceNdNchwTest1(
273 armnn::IWorkloadFactory &workloadFactory,
274 const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager)
275{
276 const unsigned int inputShape[] = {4, 3, 1, 1};
277 const unsigned int outputShape[] = {1, 3, 2, 2};
278
279 std::vector<float> input({1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f, 9.0f, 10.0f, 11.0f, 12.0f});
280
281 std::vector<float> expectedOutput({
282 // Batch 0, Channel 0, Height (2) x Width (2)
283 1.0f, 4.0f,
284 7.0f, 10.0f,
285
286 // Batch 0, Channel 1, Height (2) x Width (2)
287 2.0f, 5.0f,
288 8.0f, 11.0f,
289
290 // Batch 0, Channel 2, Height (2) x Width (2)
291 3.0f, 6.0f,
292 9.0f, 12.0f,
293 });
294
295 std::vector<unsigned int> blockShape({2, 2});
296 std::vector<std::pair<unsigned int, unsigned int>> crops = {{0, 0}, {0, 0}};
297
298 return BatchToSpaceNdHelper<ArmnnType, 4, 4>(workloadFactory, memoryManager,
299 armnn::DataLayout::NCHW, inputShape, input, blockShape,
300 crops, outputShape, expectedOutput);
301}
302
303template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>>
304LayerTestResult<T, 4> BatchToSpaceNdNchwTest2(
305 armnn::IWorkloadFactory& workloadFactory,
306 const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager)
307{
308 const unsigned int inputShape[] = {4, 1, 1, 1};
309 const unsigned int outputShape[] = {1, 1, 2, 2};
310
311 std::vector<float> input({
312 // Batch 0, Height 0, Width (2) x Channel (1)
313 1.0f, 2.0f, 3.0f, 4.0f
314 });
315
316 std::vector<float> expectedOutput({1.0f, 2.0f, 3.0f, 4.0f});
317
318 std::vector<unsigned int> blockShape({2, 2});
319 std::vector<std::pair<unsigned int, unsigned int>> crops = {{0, 0}, {0, 0}};
320
321 return BatchToSpaceNdHelper<ArmnnType, 4, 4>(workloadFactory, memoryManager,
322 armnn::DataLayout::NCHW, inputShape, input, blockShape,
323 crops, outputShape, expectedOutput);
324}
325
326template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>>
327LayerTestResult<T, 4> BatchToSpaceNdNchwTest3(
328 armnn::IWorkloadFactory& workloadFactory,
329 const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager)
330{
331 const unsigned int inputShape[] = {4, 3, 1, 1};
332 const unsigned int outputShape[] = {1, 3, 2, 2};
333
334 std::vector<float> input({1.0f, 3.0f, 5.0f, 7.0f, 9.0f, 11.0f, 2.0f, 4.0f, 6.0f, 8.0f, 10.0f, 12.0f});
335
336 std::vector<float> expectedOutput({
337 // Batch 0, Channel 0, Height (2) x Width (2)
338 1.0f, 7.0f,
339 2.0f, 8.0f,
340
341 // Batch 0, Channel 1, Height (2) x Width (2)
342 3.0f, 9.0f,
343 4.0f, 10.0f,
344
345 // Batch 0, Channel 2, Height (2) x Width (2)
346 5.0f, 11.0f,
347 6.0f, 12.0f,
348 });
349
350 std::vector<unsigned int> blockShape({2, 2});
351 std::vector<std::pair<unsigned int, unsigned int>> crops = {{0, 0}, {0, 0}};
352
353 return BatchToSpaceNdHelper<ArmnnType, 4, 4>(workloadFactory, memoryManager,
354 armnn::DataLayout::NCHW, inputShape, input, blockShape,
355 crops, outputShape, expectedOutput);
356}
357
358template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>>
359LayerTestResult<T, 4> BatchToSpaceNdNchwTest4(
360 armnn::IWorkloadFactory &workloadFactory,
361 const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager)
362{
363 const unsigned int inputShape[] = {4, 3, 1, 1};
364 const unsigned int outputShape[] = {1, 3, 2, 2};
365
366 std::vector<float> input({1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12});
367
368 std::vector<float> expectedOutput({
369 // Batch 0, Channel 0, Height (2) x Width (2)
370 1, 4,
371 7, 10,
372
373 // Batch 0, Channel 1, Height (2) x Width (2)
374 2, 5,
375 8, 11,
376
377 // Batch 0, Channel 2, Height (2) x Width (2)
378 3, 6,
379 9, 12,
380 });
381
382 std::vector<unsigned int> blockShape({2, 2});
383 std::vector<std::pair<unsigned int, unsigned int>> crops = {{0, 0}, {0, 0}};
384
385 return BatchToSpaceNdHelper<ArmnnType, 4, 4>(workloadFactory, memoryManager,
386 armnn::DataLayout::NCHW, inputShape, input, blockShape,
387 crops, outputShape, expectedOutput);
388}
389
390template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>>
391LayerTestResult<T, 4> BatchToSpaceNdNchwTest5(
392 armnn::IWorkloadFactory& workloadFactory,
393 const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager)
394{
395 const unsigned int inputShape[] = {4, 1, 1, 1};
396 const unsigned int outputShape[] = {1, 1, 2, 2};
397
398 std::vector<float> input({
399 // Batch 0, Height 0, Width (2) x Channel (1)
400 1, 2, 3, 4
401 });
402
403 std::vector<float> expectedOutput({1, 2, 3, 4});
404
405 std::vector<unsigned int> blockShape({2, 2});
406 std::vector<std::pair<unsigned int, unsigned int>> crops = {{0, 0}, {0, 0}};
407
408 return BatchToSpaceNdHelper<ArmnnType, 4, 4>(workloadFactory, memoryManager,
409 armnn::DataLayout::NCHW, inputShape, input, blockShape,
410 crops, outputShape, expectedOutput);
411}
412
413template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>>
414LayerTestResult<T, 4> BatchToSpaceNdNchwTest6(
415 armnn::IWorkloadFactory& workloadFactory,
416 const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager)
417{
418 const unsigned int inputShape[] = {4, 3, 1, 1};
419 const unsigned int outputShape[] = {1, 3, 2, 2};
420
421 std::vector<float> input({1, 3, 5, 7, 9, 11, 2, 4, 6, 8, 10, 12});
422
423 std::vector<float> expectedOutput({
424 // Batch 0, Channel 0, Height (2) x Width (2)
425 1, 7,
426 2, 8,
427
428 // Batch 0, Channel 1, Height (2) x Width (2)
429 3, 9,
430 4, 10,
431
432 // Batch 0, Channel 2, Height (2) x Width (2)
433 5, 11,
434 6, 12,
435 });
436
437 std::vector<unsigned int> blockShape({2, 2});
438 std::vector<std::pair<unsigned int, unsigned int>> crops = {{0, 0}, {0, 0}};
439
440 return BatchToSpaceNdHelper<ArmnnType, 4, 4>(workloadFactory, memoryManager,
441 armnn::DataLayout::NCHW, inputShape, input, blockShape,
442 crops, outputShape, expectedOutput);
443}
444
445template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>>
446LayerTestResult<T, 4> BatchToSpaceNdNchwTest7(
447 armnn::IWorkloadFactory& workloadFactory,
448 const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager)
449{
450 const unsigned int inputShape[] = {8, 1, 1, 3};
451 const unsigned int outputShape[] = {2, 1, 2, 4};
452
453 std::vector<float> input({
454 0, 1, 3, 0, 9, 11,
455 0, 2, 4, 0, 10, 12,
456 0, 5, 7, 0, 13, 15,
457 0, 6, 8, 0, 14, 16
458 });
459
460 std::vector<float> expectedOutput({
461 1, 2, 3, 4,
462 5, 6, 7, 8,
463 9, 10, 11, 12,
464 13, 14, 15, 16
465 });
466
467 std::vector<unsigned int> blockShape({2, 2});
468 std::vector<std::pair<unsigned int, unsigned int>> crops = {{0, 0}, {2, 0}};
469
470 return BatchToSpaceNdHelper<ArmnnType, 4, 4>(workloadFactory, memoryManager,
471 armnn::DataLayout::NCHW, inputShape, input, blockShape,
472 crops, outputShape, expectedOutput);
473}