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