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