blob: 45dff96ea7a39adf1f92c8ac41275ff94519c731 [file] [log] [blame]
Matthew Jacksone69c3992019-09-09 14:31:21 +01001//
2// Copyright © 2019 Arm Ltd. All rights reserved.
3// SPDX-License-Identifier: MIT
4//
5
6#include "StackTestImpl.hpp"
7#include "LayerTestResult.hpp"
8
9#include <ResolveType.hpp>
10
Matthew Jacksone69c3992019-09-09 14:31:21 +010011
Matteo Martincighe5b8eb92019-11-28 15:45:42 +000012#include <armnn/backends/IBackendInternal.hpp>
Matthew Jacksone69c3992019-09-09 14:31:21 +010013#include <backendsCommon/WorkloadFactory.hpp>
14
15#include <backendsCommon/test/TensorCopyUtils.hpp>
16#include <backendsCommon/test/WorkloadTestUtils.hpp>
17
18#include <test/TensorHelpers.hpp>
19
20namespace
21{
22
23template<armnn::DataType ArmnnType, typename T, std::size_t outputDimLength>
24LayerTestResult<T, outputDimLength> StackTestHelper(
25 armnn::IWorkloadFactory& workloadFactory,
26 const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager,
27 const armnn::TensorInfo& inputTensorInfo,
28 const armnn::TensorInfo& outputTensorInfo,
29 unsigned int axis,
30 const std::vector<std::vector<T>>& inputData,
31 const std::vector<T>& outputExpectedData)
32{
Derek Lambertic374ff02019-12-10 21:57:35 +000033 boost::ignore_unused(memoryManager);
Matthew Jacksone69c3992019-09-09 14:31:21 +010034 unsigned int numInputs = static_cast<unsigned int>(inputData.size());
35 std::vector<boost::multi_array<T, outputDimLength-1>> inputs;
36 for (unsigned int i = 0; i < numInputs; ++i)
37 {
38 inputs.push_back(MakeTensor<T, outputDimLength-1>(inputTensorInfo, inputData[i]));
39 }
40
41 LayerTestResult<T, outputDimLength> result(outputTensorInfo);
42 result.outputExpected = MakeTensor<T, outputDimLength>(outputTensorInfo, outputExpectedData);
43
44 std::vector<std::unique_ptr<armnn::ITensorHandle>> inputHandles;
45 for (unsigned int i = 0; i < numInputs; ++i)
46 {
47 inputHandles.push_back(workloadFactory.CreateTensorHandle(inputTensorInfo));
48 }
49 std::unique_ptr<armnn::ITensorHandle> outputHandle = workloadFactory.CreateTensorHandle(outputTensorInfo);
50
51 armnn::StackQueueDescriptor descriptor;
52 descriptor.m_Parameters.m_Axis = axis;
53 descriptor.m_Parameters.m_InputShape = inputTensorInfo.GetShape();
54 descriptor.m_Parameters.m_NumInputs = numInputs;
55
56 armnn::WorkloadInfo info;
57 for (unsigned int i = 0; i < numInputs; ++i)
58 {
59 std::unique_ptr<armnn::ITensorHandle>& inputHandle = inputHandles[i];
60 AddInputToWorkload(descriptor, info, inputTensorInfo, inputHandle.get());
61 inputHandle->Allocate();
62 CopyDataToITensorHandle(inputHandle.get(), inputs[i].origin());
63 }
64
65 AddOutputToWorkload(descriptor, info, outputTensorInfo, outputHandle.get());
66 outputHandle->Allocate();
67
68 std::unique_ptr<armnn::IWorkload> workload = workloadFactory.CreateStack(descriptor, info);
69
70 workload->Execute();
71
72 CopyDataFromITensorHandle(result.output.origin(), outputHandle.get());
73
74 return result;
75}
76
77} // anonymous namespace
78
79//
80// Implementation templates
81//
82
83template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>>
84LayerTestResult<T, 4> StackAxis0TestImpl(
85 armnn::IWorkloadFactory& workloadFactory,
86 const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager)
87{
88 armnn::TensorInfo inputTensorInfo ({ 3, 2, 3 }, ArmnnType);
89 armnn::TensorInfo outputTensorInfo({ 2, 3, 2, 3 }, ArmnnType);
90
91 std::vector<std::vector<T>> inputData;
92
93 inputData.push_back(
94 {
95 1, 2, 3,
96 4, 5, 6,
97
98 7, 8, 9,
99 10, 11, 12,
100
101 13, 14, 15,
102 16, 17, 18
103 });
104
105 inputData.push_back(
106 {
107 19, 20, 21,
108 22, 23, 24,
109
110 25, 26, 27,
111 28, 29, 30,
112
113 31, 32, 33,
114 34, 35, 36
115 });
116
117 std::vector<T> outputExpectedData =
118 {
119 1, 2, 3,
120 4, 5, 6,
121
122 7, 8, 9,
123 10, 11, 12,
124
125 13, 14, 15,
126 16, 17, 18,
127
128
129 19, 20, 21,
130 22, 23, 24,
131
132 25, 26, 27,
133 28, 29, 30,
134
135 31, 32, 33,
136 34, 35, 36
137 };
138
139 return StackTestHelper<ArmnnType, T, 4>(
140 workloadFactory,
141 memoryManager,
142 inputTensorInfo,
143 outputTensorInfo,
144 0U,
145 inputData,
146 outputExpectedData
147 );
148}
149
150template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>>
151LayerTestResult<T, 4> StackOutput4DAxis1TestImpl(
152 armnn::IWorkloadFactory& workloadFactory,
153 const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager)
154{
155 armnn::TensorInfo inputTensorInfo ({ 3, 2, 3 }, ArmnnType);
156 armnn::TensorInfo outputTensorInfo({ 3, 2, 2, 3 }, ArmnnType);
157
158 std::vector<std::vector<T>> inputData;
159
160 inputData.push_back(
161 {
162 1, 2, 3,
163 4, 5, 6,
164
165 7, 8, 9,
166 10, 11, 12,
167
168 13, 14, 15,
169 16, 17, 18
170 });
171
172 inputData.push_back(
173 {
174 19, 20, 21,
175 22, 23, 24,
176
177 25, 26, 27,
178 28, 29, 30,
179
180 31, 32, 33,
181 34, 35, 36
182 });
183
184 std::vector<T> outputExpectedData =
185 {
186 1, 2, 3,
187 4, 5, 6,
188
189 19, 20, 21,
190 22, 23, 24,
191
192
193 7, 8, 9,
194 10, 11, 12,
195
196 25, 26, 27,
197 28, 29, 30,
198
199
200 13, 14, 15,
201 16, 17, 18,
202
203 31, 32, 33,
204 34, 35, 36
205 };
206
207 return StackTestHelper<ArmnnType, T, 4>(
208 workloadFactory,
209 memoryManager,
210 inputTensorInfo,
211 outputTensorInfo,
212 1U,
213 inputData,
214 outputExpectedData
215 );
216}
217
218template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>>
219LayerTestResult<T, 4> StackOutput4DAxis2TestImpl(
220 armnn::IWorkloadFactory& workloadFactory,
221 const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager)
222{
223 armnn::TensorInfo inputTensorInfo ({ 3, 2, 3 }, ArmnnType);
224 armnn::TensorInfo outputTensorInfo({ 3, 2, 2, 3 }, ArmnnType);
225
226 std::vector<std::vector<T>> inputData;
227
228 inputData.push_back(
229 {
230 1, 2, 3,
231 4, 5, 6,
232
233 7, 8, 9,
234 10, 11, 12,
235
236 13, 14, 15,
237 16, 17, 18
238 });
239
240 inputData.push_back(
241 {
242 19, 20, 21,
243 22, 23, 24,
244
245 25, 26, 27,
246 28, 29, 30,
247
248 31, 32, 33,
249 34, 35, 36
250 });
251
252 std::vector<T> outputExpectedData =
253 {
254 1, 2, 3,
255 19, 20, 21,
256
257 4, 5, 6,
258 22, 23, 24,
259
260 7, 8, 9,
261 25, 26, 27,
262
263 10, 11, 12,
264 28, 29, 30,
265
266 13, 14, 15,
267 31, 32, 33,
268
269 16, 17, 18,
270 34, 35, 36
271 };
272
273 return StackTestHelper<ArmnnType, T, 4>(
274 workloadFactory,
275 memoryManager,
276 inputTensorInfo,
277 outputTensorInfo,
278 2U,
279 inputData,
280 outputExpectedData
281 );
282}
283
284template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>>
285LayerTestResult<T, 4> StackOutput4DAxis3TestImpl(
286 armnn::IWorkloadFactory& workloadFactory,
287 const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager)
288{
289 armnn::TensorInfo inputTensorInfo ({ 3, 2, 3 }, ArmnnType);
290 armnn::TensorInfo outputTensorInfo({ 3, 2, 3, 2 }, ArmnnType);
291
292 std::vector<std::vector<T>> inputData;
293
294 inputData.push_back(
295 {
296 1, 2, 3,
297 4, 5, 6,
298
299 7, 8, 9,
300 10, 11, 12,
301
302 13, 14, 15,
303 16, 17, 18
304 });
305
306 inputData.push_back(
307 {
308 19, 20, 21,
309 22, 23, 24,
310
311 25, 26, 27,
312 28, 29, 30,
313
314 31, 32, 33,
315 34, 35, 36
316 });
317
318 std::vector<T> outputExpectedData =
319 {
320 1, 19,
321 2, 20,
322 3, 21,
323
324 4, 22,
325 5, 23,
326 6, 24,
327
328
329 7, 25,
330 8, 26,
331 9, 27,
332
333 10, 28,
334 11, 29,
335 12, 30,
336
337
338 13, 31,
339 14, 32,
340 15, 33,
341
342 16, 34,
343 17, 35,
344 18, 36
345 };
346
347 return StackTestHelper<ArmnnType, T, 4>(
348 workloadFactory,
349 memoryManager,
350 inputTensorInfo,
351 outputTensorInfo,
352 3U,
353 inputData,
354 outputExpectedData
355 );
356}
357
358template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>>
359LayerTestResult<T, 3> StackOutput3DInputs3TestImpl(
360 armnn::IWorkloadFactory& workloadFactory,
361 const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager)
362{
363 armnn::TensorInfo inputTensorInfo ({ 3, 3 }, ArmnnType);
364 armnn::TensorInfo outputTensorInfo({ 3, 3, 3 }, ArmnnType);
365
366 std::vector<std::vector<T>> inputData;
367
368 inputData.push_back(
369 {
370 1, 2, 3,
371 4, 5, 6,
372 7, 8, 9
373 });
374
375 inputData.push_back(
376 {
377 10, 11, 12,
378 13, 14, 15,
379 16, 17, 18
380 });
381
382 inputData.push_back(
383 {
384 19, 20, 21,
385 22, 23, 24,
386 25, 26, 27
387 });
388
389 std::vector<T> outputExpectedData =
390 {
391 1, 2, 3,
392 10, 11, 12,
393 19, 20, 21,
394
395 4, 5, 6,
396 13, 14, 15,
397 22, 23, 24,
398
399 7, 8, 9,
400 16, 17, 18,
401 25, 26, 27
402 };
403
404 return StackTestHelper<ArmnnType, T, 3>(
405 workloadFactory,
406 memoryManager,
407 inputTensorInfo,
408 outputTensorInfo,
409 1U,
410 inputData,
411 outputExpectedData
412 );
413}
414
415template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>>
416LayerTestResult<T, 5> StackOutput5DTestImpl(
417 armnn::IWorkloadFactory& workloadFactory,
418 const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager)
419{
420 armnn::TensorInfo inputTensorInfo ({ 2, 2, 2, 3 }, ArmnnType);
421 armnn::TensorInfo outputTensorInfo({ 2, 2, 2, 2, 3 }, ArmnnType);
422
423 std::vector<std::vector<T>> inputData;
424
425 inputData.push_back(
426 {
427 1, 2, 3,
428 4, 5, 6,
429
430 7, 8, 9,
431 10, 11, 12,
432
433
434 13, 14, 15,
435 16, 17, 18,
436
437 19, 20, 21,
438 22, 23, 24
439 });
440
441 inputData.push_back(
442 {
443 25, 26, 27,
444 28, 29, 30,
445
446 31, 32, 33,
447 34, 35, 36,
448
449
450 37, 38, 39,
451 40, 41, 42,
452
453 43, 44, 45,
454 46, 47, 48
455 });
456
457 std::vector<T> outputExpectedData =
458 {
459 1, 2, 3,
460 4, 5, 6,
461
462 7, 8, 9,
463 10, 11, 12,
464
465
466 25, 26, 27,
467 28, 29, 30,
468
469 31, 32, 33,
470 34, 35, 36,
471
472
473
474 13, 14, 15,
475 16, 17, 18,
476
477 19, 20, 21,
478 22, 23, 24,
479
480
481 37, 38, 39,
482 40, 41, 42,
483
484 43, 44, 45,
485 46, 47, 48
486
487 };
488
489 return StackTestHelper<ArmnnType, T, 5>(
490 workloadFactory,
491 memoryManager,
492 inputTensorInfo,
493 outputTensorInfo,
494 1U,
495 inputData,
496 outputExpectedData
497 );
498}
499
500//
501// Implementation functions
502//
503
504LayerTestResult<float, 4> StackAxis0Float32Test(
505 armnn::IWorkloadFactory& workloadFactory,
506 const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager)
507{
508 return StackAxis0TestImpl<armnn::DataType::Float32>(workloadFactory, memoryManager);
509}
510
511LayerTestResult<float, 4> StackOutput4DAxis1Float32Test(
512 armnn::IWorkloadFactory& workloadFactory,
513 const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager)
514{
515 return StackOutput4DAxis1TestImpl<armnn::DataType::Float32>(workloadFactory, memoryManager);
516}
517
518LayerTestResult<float, 4> StackOutput4DAxis2Float32Test(
519 armnn::IWorkloadFactory& workloadFactory,
520 const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager)
521{
522 return StackOutput4DAxis2TestImpl<armnn::DataType::Float32>(workloadFactory, memoryManager);
523}
524
525LayerTestResult<float, 4> StackOutput4DAxis3Float32Test(
526 armnn::IWorkloadFactory& workloadFactory,
527 const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager)
528{
529 return StackOutput4DAxis3TestImpl<armnn::DataType::Float32>(workloadFactory, memoryManager);
530}
531
532LayerTestResult<float, 3> StackOutput3DInputs3Float32Test(
533 armnn::IWorkloadFactory& workloadFactory,
534 const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager)
535{
536 return StackOutput3DInputs3TestImpl<armnn::DataType::Float32>(workloadFactory, memoryManager);
537}
538
539LayerTestResult<float, 5> StackOutput5DFloat32Test(
540 armnn::IWorkloadFactory& workloadFactory,
541 const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager)
542{
543 return StackOutput5DTestImpl<armnn::DataType::Float32>(workloadFactory, memoryManager);
544}
545
546LayerTestResult<armnn::Half, 4> StackFloat16Test(
547 armnn::IWorkloadFactory& workloadFactory,
548 const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager)
549{
550 using namespace half_float::literal;
551
552 armnn::TensorInfo inputTensorInfo ({ 3, 2, 3 }, armnn::DataType::Float16);
553 armnn::TensorInfo outputTensorInfo({ 3, 2, 2, 3 }, armnn::DataType::Float16);
554
555 std::vector<std::vector<armnn::Half>> inputData;
556
557 inputData.push_back(
558 {
559 1.0_h, 2.0_h, 3.0_h,
560 4.0_h, 5.0_h, 6.0_h,
561
562 7.0_h, 8.0_h, 9.0_h,
563 10.0_h, 11.0_h, 12.0_h,
564
565 13.0_h, 14.0_h, 15.0_h,
566 16.0_h, 17.0_h, 18.0_h
567 });
568
569 inputData.push_back(
570 {
571 19.0_h, 20.0_h, 21.0_h,
572 22.0_h, 23.0_h, 24.0_h,
573
574 25.0_h, 26.0_h, 27.0_h,
575 28.0_h, 29.0_h, 30.0_h,
576
577 31.0_h, 32.0_h, 33.0_h,
578 34.0_h, 35.0_h, 36.0_h
579 });
580
581 std::vector<armnn::Half> outputExpectedData =
582 {
583 1.0_h, 2.0_h, 3.0_h,
584 19.0_h, 20.0_h, 21.0_h,
585
586 4.0_h, 5.0_h, 6.0_h,
587 22.0_h, 23.0_h, 24.0_h,
588
589 7.0_h, 8.0_h, 9.0_h,
590 25.0_h, 26.0_h, 27.0_h,
591
592 10.0_h, 11.0_h, 12.0_h,
593 28.0_h, 29.0_h, 30.0_h,
594
595 13.0_h, 14.0_h, 15.0_h,
596 31.0_h, 32.0_h, 33.0_h,
597
598 16.0_h, 17.0_h, 18.0_h,
599 34.0_h, 35.0_h, 36.0_h
600 };
601
602 return StackTestHelper<armnn::DataType::Float16, armnn::Half, 4>(
603 workloadFactory,
604 memoryManager,
605 inputTensorInfo,
606 outputTensorInfo,
607 2U,
608 inputData,
609 outputExpectedData
610 );
611}