ArmNN
 20.08
PadTestImpl.cpp
Go to the documentation of this file.
1 //
2 // Copyright © 2017 Arm Ltd and Contributors. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
6 #include "PadTestImpl.hpp"
7 
8 #include <QuantizeHelper.hpp>
9 
12 
13 #include <test/TensorHelpers.hpp>
14 
15 //
16 // Implementation templates
17 //
18 
19 template<armnn::DataType ArmnnType, typename T>
21  armnn::IWorkloadFactory& workloadFactory,
23  float qScale,
24  int32_t qOffset,
25  const float customPaddingValue)
26 {
27  IgnoreUnused(memoryManager);
28  const armnn::TensorShape inputShape{ 3, 3 };
29  const armnn::TensorShape outputShape{ 7, 7 };
30 
31  const armnn::TensorInfo inputTensorInfo(inputShape, ArmnnType, qScale, qOffset);
32  const armnn::TensorInfo outputTensorInfo(outputShape, ArmnnType, qScale, qOffset);
33 
34  std::vector<T> inputValues = armnnUtils::QuantizedVector<T>(
35  {
36  // Height (3) x Width (3)
37  4, 8, 6,
38  7, 4, 4,
39  3, 2, 4
40  },
41  qScale, qOffset);
42 
43  auto p = customPaddingValue;
44  std::vector<T> expectedOutputValues = armnnUtils::QuantizedVector<T>(
45  {
46  p, p, p, p, p, p, p,
47  p, p, p, p, p, p, p,
48  p, p, 4, 8, 6, p, p,
49  p, p, 7, 4, 4, p, p,
50  p, p, 3, 2, 4, p, p,
51  p, p, p, p, p, p, p,
52  p, p, p, p, p, p, p
53  },
54  qScale, qOffset);
55 
56  auto inputTensor = MakeTensor<T, 2>(inputTensorInfo, std::vector<T>(inputValues));
57 
58  LayerTestResult<T, 2> result(outputTensorInfo);
59  result.outputExpected = MakeTensor<T, 2>(outputTensorInfo, std::vector<T>(expectedOutputValues));
60 
62  std::unique_ptr<armnn::ITensorHandle> inputHandle = workloadFactory.CreateTensorHandle(inputTensorInfo);
63  std::unique_ptr<armnn::ITensorHandle> outputHandle = workloadFactory.CreateTensorHandle(outputTensorInfo);
65 
66  armnn::PadQueueDescriptor descriptor;
67 
68  std::vector<std::pair<unsigned int, unsigned int>> padList;
69  padList.push_back(std::pair<unsigned int, unsigned int>(2,2));
70  padList.push_back(std::pair<unsigned int, unsigned int>(2,2));
71 
72  descriptor.m_Parameters.m_PadList = padList;
73  descriptor.m_Parameters.m_PadValue = customPaddingValue;
75 
76  AddInputToWorkload(descriptor, info, inputTensorInfo, inputHandle.get());
77  AddOutputToWorkload(descriptor, info, outputTensorInfo, outputHandle.get());
78 
79  std::unique_ptr<armnn::IWorkload> workload = workloadFactory.CreatePad(descriptor, info);
80 
81  inputHandle->Allocate();
82  outputHandle->Allocate();
83 
84  CopyDataToITensorHandle(inputHandle.get(), &inputTensor[0][0]);
85 
86  workload->PostAllocationConfigure();
87  workload->Execute();
88 
89  CopyDataFromITensorHandle(&result.output[0][0], outputHandle.get());
90 
91  return result;
92 }
93 
94 template<armnn::DataType ArmnnType, typename T>
96  armnn::IWorkloadFactory& workloadFactory,
98  float qScale,
99  int32_t qOffset)
100 {
101  IgnoreUnused(memoryManager);
102  const armnn::TensorShape inputShape{ 2, 2, 2 };
103  const armnn::TensorShape outputShape{ 3, 5, 6 };
104 
105  const armnn::TensorInfo inputTensorInfo(inputShape, ArmnnType, qScale, qOffset);
106  const armnn::TensorInfo outputTensorInfo(outputShape, ArmnnType, qScale, qOffset);
107 
108  std::vector<T> inputValues = armnnUtils::QuantizedVector<T>(
109  {
110  // Channel 0, Height (2) x Width (2)
111  0, 4,
112  2, 5,
113 
114  // Channel 1, Height (2) x Width (2)
115  6, 1,
116  5, 2
117  },
118  qScale, qOffset);
119 
120  std::vector<T> expectedOutputValues = armnnUtils::QuantizedVector<T>(
121  {
122  0, 0, 0, 0, 0, 0,
123  0, 0, 0, 0, 0, 0,
124  0, 0, 0, 4, 0, 0,
125  0, 0, 2, 5, 0, 0,
126  0, 0, 0, 0, 0, 0,
127 
128  0, 0, 0, 0, 0, 0,
129  0, 0, 0, 0, 0, 0,
130  0, 0, 6, 1, 0, 0,
131  0, 0, 5, 2, 0, 0,
132  0, 0, 0, 0, 0, 0,
133 
134  0, 0, 0, 0, 0, 0,
135  0, 0, 0, 0, 0, 0,
136  0, 0, 0, 0, 0, 0,
137  0, 0, 0, 0, 0, 0,
138  0, 0, 0, 0, 0, 0
139  },
140  qScale, qOffset);
141 
142  auto inputTensor = MakeTensor<T, 3>(inputTensorInfo, std::vector<T>(inputValues));
143 
144  LayerTestResult<T, 3> result(outputTensorInfo);
145  result.outputExpected = MakeTensor<T, 3>(outputTensorInfo, std::vector<T>(expectedOutputValues));
146 
148  std::unique_ptr<armnn::ITensorHandle> inputHandle = workloadFactory.CreateTensorHandle(inputTensorInfo);
149  std::unique_ptr<armnn::ITensorHandle> outputHandle = workloadFactory.CreateTensorHandle(outputTensorInfo);
151 
152  armnn::PadQueueDescriptor descriptor;
153 
154  std::vector<std::pair<unsigned int, unsigned int>> PadList;
155  PadList.push_back(std::pair<unsigned int, unsigned int>(0,1));
156  PadList.push_back(std::pair<unsigned int, unsigned int>(2,1));
157  PadList.push_back(std::pair<unsigned int, unsigned int>(2,2));
158 
159  descriptor.m_Parameters.m_PadList = PadList;
160  armnn::WorkloadInfo info;
161 
162  AddInputToWorkload(descriptor, info, inputTensorInfo, inputHandle.get());
163  AddOutputToWorkload(descriptor, info, outputTensorInfo, outputHandle.get());
164 
165  std::unique_ptr<armnn::IWorkload> workload = workloadFactory.CreatePad(descriptor, info);
166 
167  inputHandle->Allocate();
168  outputHandle->Allocate();
169 
170  CopyDataToITensorHandle(inputHandle.get(), &inputTensor[0][0][0]);
171 
172  workload->PostAllocationConfigure();
173  workload->Execute();
174 
175  CopyDataFromITensorHandle(&result.output[0][0][0], outputHandle.get());
176 
177  return result;
178 }
179 
180 template<armnn::DataType ArmnnType, typename T>
182  armnn::IWorkloadFactory& workloadFactory,
184  float qScale,
185  int32_t qOffset)
186 {
187  IgnoreUnused(memoryManager);
188  const armnn::TensorShape inputShape{ 2, 2, 3, 2 };
189  const armnn::TensorShape outputShape{ 4, 5, 7, 4 };
190 
191  const armnn::TensorInfo inputTensorInfo(inputShape, ArmnnType, qScale, qOffset);
192  const armnn::TensorInfo outputTensorInfo(outputShape, ArmnnType, qScale, qOffset);
193 
194  std::vector<T> inputValues = armnnUtils::QuantizedVector<T>(
195  {
196  // Batch 0, Channel 0, Height (3) x Width (2)
197  0, 1,
198  2, 3,
199  4, 5,
200 
201  // Batch 0, Channel 1, Height (3) x Width (2)
202  6, 7,
203  8, 9,
204  10, 11,
205 
206  // Batch 1, Channel 0, Height (3) x Width (2)
207  12, 13,
208  14, 15,
209  16, 17,
210 
211  // Batch 1, Channel 1, Height (3) x Width (2)
212  18, 19,
213  20, 21,
214  22, 23
215  },
216  qScale, qOffset);
217 
218  std::vector<T> expectedOutputValues = armnnUtils::QuantizedVector<T>(
219  {
220  0, 0, 0, 0,
221  0, 0, 0, 0,
222  0, 0, 0, 0,
223  0, 0, 0, 0,
224  0, 0, 0, 0,
225  0, 0, 0, 0,
226  0, 0, 0, 0,
227 
228  0, 0, 0, 0,
229  0, 0, 0, 0,
230  0, 0, 0, 0,
231  0, 0, 0, 0,
232  0, 0, 0, 0,
233  0, 0, 0, 0,
234  0, 0, 0, 0,
235 
236  0, 0, 0, 0,
237  0, 0, 0, 0,
238  0, 0, 0, 0,
239  0, 0, 0, 0,
240  0, 0, 0, 0,
241  0, 0, 0, 0,
242  0, 0, 0, 0,
243 
244  0, 0, 0, 0,
245  0, 0, 0, 0,
246  0, 0, 0, 0,
247  0, 0, 0, 0,
248  0, 0, 0, 0,
249  0, 0, 0, 0,
250  0, 0, 0, 0,
251 
252  0, 0, 0, 0,
253  0, 0, 0, 0,
254  0, 0, 0, 0,
255  0, 0, 0, 0,
256  0, 0, 0, 0,
257  0, 0, 0, 0,
258  0, 0, 0, 0,
259 
260  0, 0, 0, 0,
261  0, 0, 0, 0,
262  0, 0, 0, 0,
263  0, 0, 0, 0,
264  0, 0, 0, 0,
265  0, 0, 0, 0,
266  0, 0, 0, 0,
267 
268  0, 0, 0, 0,
269  0, 0, 0, 0,
270  0, 0, 0, 0,
271  0, 0, 0, 0,
272  0, 0, 0, 0,
273  0, 0, 0, 0,
274  0, 0, 0, 0,
275 
276  0, 0, 0, 0,
277  0, 0, 0, 0,
278  0, 0, 0, 0,
279  0, 0, 1, 0,
280  0, 2, 3, 0,
281  0, 4, 5, 0,
282  0, 0, 0, 0,
283 
284  0, 0, 0, 0,
285  0, 0, 0, 0,
286  0, 0, 0, 0,
287  0, 6, 7, 0,
288  0, 8, 9, 0,
289  0, 10, 11, 0,
290  0, 0, 0, 0,
291 
292  0, 0, 0, 0,
293  0, 0, 0, 0,
294  0, 0, 0, 0,
295  0, 0, 0, 0,
296  0, 0, 0, 0,
297  0, 0, 0, 0,
298  0, 0, 0, 0,
299 
300  0, 0, 0, 0,
301  0, 0, 0, 0,
302  0, 0, 0, 0,
303  0, 0, 0, 0,
304  0, 0, 0, 0,
305  0, 0, 0, 0,
306  0, 0, 0, 0,
307 
308  0, 0, 0, 0,
309  0, 0, 0, 0,
310  0, 0, 0, 0,
311  0, 0, 0, 0,
312  0, 0, 0, 0,
313  0, 0, 0, 0,
314  0, 0, 0, 0,
315 
316  0, 0, 0, 0,
317  0, 0, 0, 0,
318  0, 0, 0, 0,
319  0, 12, 13, 0,
320  0, 14, 15, 0,
321  0, 16, 17, 0,
322  0, 0, 0, 0,
323 
324  0, 0, 0, 0,
325  0, 0, 0, 0,
326  0, 0, 0, 0,
327  0, 18, 19, 0,
328  0, 20, 21, 0,
329  0, 22, 23, 0,
330  0, 0, 0, 0,
331 
332  0, 0, 0, 0,
333  0, 0, 0, 0,
334  0, 0, 0, 0,
335  0, 0, 0, 0,
336  0, 0, 0, 0,
337  0, 0, 0, 0,
338  0, 0, 0, 0,
339 
340  0, 0, 0, 0,
341  0, 0, 0, 0,
342  0, 0, 0, 0,
343  0, 0, 0, 0,
344  0, 0, 0, 0,
345  0, 0, 0, 0,
346  0, 0, 0, 0,
347 
348  0, 0, 0, 0,
349  0, 0, 0, 0,
350  0, 0, 0, 0,
351  0, 0, 0, 0,
352  0, 0, 0, 0,
353  0, 0, 0, 0,
354  0, 0, 0, 0,
355 
356  0, 0, 0, 0,
357  0, 0, 0, 0,
358  0, 0, 0, 0,
359  0, 0, 0, 0,
360  0, 0, 0, 0,
361  0, 0, 0, 0,
362  0, 0, 0, 0,
363 
364  0, 0, 0, 0,
365  0, 0, 0, 0,
366  0, 0, 0, 0,
367  0, 0, 0, 0,
368  0, 0, 0, 0,
369  0, 0, 0, 0,
370  0, 0, 0, 0,
371 
372  0, 0, 0, 0,
373  0, 0, 0, 0,
374  0, 0, 0, 0,
375  0, 0, 0, 0,
376  0, 0, 0, 0,
377  0, 0, 0, 0,
378  0, 0, 0, 0
379  },
380  qScale, qOffset);
381 
382  auto inputTensor = MakeTensor<T, 4>(inputTensorInfo, std::vector<T>(inputValues));
383 
384  LayerTestResult<T, 4> result(outputTensorInfo);
385  result.outputExpected = MakeTensor<T, 4>(outputTensorInfo, std::vector<T>(expectedOutputValues));
386 
388  std::unique_ptr<armnn::ITensorHandle> inputHandle = workloadFactory.CreateTensorHandle(inputTensorInfo);
389  std::unique_ptr<armnn::ITensorHandle> outputHandle = workloadFactory.CreateTensorHandle(outputTensorInfo);
391 
392  armnn::PadQueueDescriptor descriptor;
393 
394  std::vector<std::pair<unsigned int, unsigned int>> PadList;
395  PadList.push_back(std::pair<unsigned int, unsigned int>(1,1));
396  PadList.push_back(std::pair<unsigned int, unsigned int>(2,1));
397  PadList.push_back(std::pair<unsigned int, unsigned int>(3,1));
398  PadList.push_back(std::pair<unsigned int, unsigned int>(1,1));
399 
400  descriptor.m_Parameters.m_PadList = PadList;
401  armnn::WorkloadInfo info;
402 
403  AddInputToWorkload(descriptor, info, inputTensorInfo, inputHandle.get());
404  AddOutputToWorkload(descriptor, info, outputTensorInfo, outputHandle.get());
405 
406  std::unique_ptr<armnn::IWorkload> workload = workloadFactory.CreatePad(descriptor, info);
407 
408  inputHandle->Allocate();
409  outputHandle->Allocate();
410 
411  CopyDataToITensorHandle(inputHandle.get(), &inputTensor[0][0][0][0]);
412 
413  workload->PostAllocationConfigure();
414  workload->Execute();
415 
416  CopyDataFromITensorHandle(&result.output[0][0][0][0], outputHandle.get());
417 
418  return result;
419 }
420 
421 //
422 // Explicit template specializations
423 //
424 
426 Pad2dTestCommon<armnn::DataType::QSymmS16>(
427  armnn::IWorkloadFactory& workloadFactory,
429  float qScale,
430  int32_t qOffset,
431  const float customPaddingValue);
432 
434 Pad3dTestCommon<armnn::DataType::QSymmS16>(
435  armnn::IWorkloadFactory& workloadFactory,
437  float qScale,
438  int32_t qOffset);
439 
440 template LayerTestResult<armnn::ResolveType<armnn::DataType::QSymmS16>, 4>
441 Pad4dTestCommon<armnn::DataType::QSymmS16>(
442  armnn::IWorkloadFactory& workloadFactory,
444  float qScale,
445  int32_t qOffset);
446 
447 //
448 // Implementation functions
449 //
450 
452  armnn::IWorkloadFactory& workloadFactory,
454 {
455  return Pad2dTestCommon<armnn::DataType::QAsymmU8>(workloadFactory, memoryManager, 1.0f, 0);
456 }
457 
459  armnn::IWorkloadFactory& workloadFactory,
461 {
462  return Pad2dTestCommon<armnn::DataType::QAsymmU8>(workloadFactory, memoryManager, 1.0f, 0, 1.0f);
463 }
464 
466  armnn::IWorkloadFactory& workloadFactory,
468 {
469  return Pad3dTestCommon<armnn::DataType::QAsymmU8>(workloadFactory, memoryManager, 1.0f, 0);
470 }
471 
473  armnn::IWorkloadFactory& workloadFactory,
475 {
476  return Pad4dTestCommon<armnn::DataType::QAsymmU8>(workloadFactory, memoryManager, 1.0f, 0);
477 }
478 
480  armnn::IWorkloadFactory& workloadFactory,
482 {
483  return Pad2dTestCommon<armnn::DataType::Float32>(workloadFactory, memoryManager, 0.0f, 0);
484 }
485 
487  armnn::IWorkloadFactory& workloadFactory,
489 {
490  return Pad2dTestCommon<armnn::DataType::Float32>(workloadFactory, memoryManager, 0.0f, 0, 1.0f);
491 }
492 
494  armnn::IWorkloadFactory& workloadFactory,
496 {
497  return Pad3dTestCommon<armnn::DataType::Float32>(workloadFactory, memoryManager, 0.0f, 0);
498 }
499 
501  armnn::IWorkloadFactory& workloadFactory,
503 {
504  return Pad4dTestCommon<armnn::DataType::Float32>(workloadFactory, memoryManager, 0.0f, 0);
505 }
506 
508  armnn::IWorkloadFactory& workloadFactory,
510 {
511  return Pad2dTestCommon<armnn::DataType::BFloat16>(workloadFactory, memoryManager, 0.0f, 0);
512 }
513 
515  armnn::IWorkloadFactory& workloadFactory,
517 {
518  return Pad2dTestCommon<armnn::DataType::BFloat16>(workloadFactory, memoryManager, 0.0f, 0, 1.0f);
519 }
520 
522  armnn::IWorkloadFactory& workloadFactory,
524 {
525  return Pad3dTestCommon<armnn::DataType::BFloat16>(workloadFactory, memoryManager, 0.0f, 0);
526 }
527 
529  armnn::IWorkloadFactory& workloadFactory,
531 {
532  return Pad4dTestCommon<armnn::DataType::BFloat16>(workloadFactory, memoryManager, 0.0f, 0);
533 }
534 
536  armnn::IWorkloadFactory& workloadFactory,
538 {
539  return Pad2dTestCommon<armnn::DataType::QSymmS8>(workloadFactory, memoryManager, 1.0f, 0);
540 }
541 
543  armnn::IWorkloadFactory& workloadFactory,
545 {
546  return Pad2dTestCommon<armnn::DataType::QSymmS8>(workloadFactory, memoryManager, 1.0f, 0, 1.0f);
547 }
548 
550  armnn::IWorkloadFactory& workloadFactory,
552 {
553  return Pad3dTestCommon<armnn::DataType::QSymmS8>(workloadFactory, memoryManager, 1.0f, 0);
554 }
555 
557  armnn::IWorkloadFactory& workloadFactory,
559 {
560  return Pad4dTestCommon<armnn::DataType::QSymmS8>(workloadFactory, memoryManager, 1.0f, 0);
561 }
LayerTestResult< int8_t, 2 > PadInt82dCustomPaddingTest(armnn::IWorkloadFactory &workloadFactory, const armnn::IBackendInternal::IMemoryManagerSharedPtr &memoryManager)
#define ARMNN_NO_DEPRECATE_WARN_BEGIN
Definition: Deprecated.hpp:33
LayerTestResult< armnn::BFloat16, 2 > PadBFloat162dTest(armnn::IWorkloadFactory &workloadFactory, const armnn::IBackendInternal::IMemoryManagerSharedPtr &memoryManager)
boost::multi_array< T, n > outputExpected
LayerTestResult< int8_t, 2 > PadInt82dTest(armnn::IWorkloadFactory &workloadFactory, const armnn::IBackendInternal::IMemoryManagerSharedPtr &memoryManager)
std::vector< std::pair< unsigned int, unsigned int > > m_PadList
Specifies the padding for input dimension.
LayerTestResult< uint8_t, 3 > PadUint83dTest(armnn::IWorkloadFactory &workloadFactory, const armnn::IBackendInternal::IMemoryManagerSharedPtr &memoryManager)
LayerTestResult< float, 2 > PadFloat322dTest(armnn::IWorkloadFactory &workloadFactory, const armnn::IBackendInternal::IMemoryManagerSharedPtr &memoryManager)
void IgnoreUnused(Ts &&...)
LayerTestResult< T, 4 > Pad4dTestCommon(armnn::IWorkloadFactory &workloadFactory, const armnn::IBackendInternal::IMemoryManagerSharedPtr &memoryManager, float qScale, int32_t qOffset)
LayerTestResult< T, 3 > Pad3dTestCommon(armnn::IWorkloadFactory &workloadFactory, const armnn::IBackendInternal::IMemoryManagerSharedPtr &memoryManager, float qScale, int32_t qOffset)
Definition: PadTestImpl.cpp:95
LayerTestResult< uint8_t, 2 > PadUint82dTest(armnn::IWorkloadFactory &workloadFactory, const armnn::IBackendInternal::IMemoryManagerSharedPtr &memoryManager)
LayerTestResult< float, 4 > PadFloat324dTest(armnn::IWorkloadFactory &workloadFactory, const armnn::IBackendInternal::IMemoryManagerSharedPtr &memoryManager)
#define ARMNN_NO_DEPRECATE_WARN_END
Definition: Deprecated.hpp:34
LayerTestResult< float, 2 > PadFloat322dCustomPaddingTest(armnn::IWorkloadFactory &workloadFactory, const armnn::IBackendInternal::IMemoryManagerSharedPtr &memoryManager)
std::shared_ptr< IMemoryManager > IMemoryManagerSharedPtr
LayerTestResult< int8_t, 3 > PadInt83dTest(armnn::IWorkloadFactory &workloadFactory, const armnn::IBackendInternal::IMemoryManagerSharedPtr &memoryManager)
virtual std::unique_ptr< IWorkload > CreatePad(const PadQueueDescriptor &descriptor, const WorkloadInfo &Info) const
LayerTestResult< float, 3 > PadFloat323dTest(armnn::IWorkloadFactory &workloadFactory, const armnn::IBackendInternal::IMemoryManagerSharedPtr &memoryManager)
void CopyDataFromITensorHandle(void *memory, const armnn::ITensorHandle *tensorHandle)
virtual std::unique_ptr< ITensorHandle > CreateTensorHandle(const TensorInfo &tensorInfo, const bool IsMemoryManaged=true) const =0
LayerTestResult< armnn::BFloat16, 2 > PadBFloat162dCustomPaddingTest(armnn::IWorkloadFactory &workloadFactory, const armnn::IBackendInternal::IMemoryManagerSharedPtr &memoryManager)
boost::multi_array< T, n > output
LayerTestResult< uint8_t, 4 > PadUint84dTest(armnn::IWorkloadFactory &workloadFactory, const armnn::IBackendInternal::IMemoryManagerSharedPtr &memoryManager)
LayerTestResult< armnn::BFloat16, 3 > PadBFloat163dTest(armnn::IWorkloadFactory &workloadFactory, const armnn::IBackendInternal::IMemoryManagerSharedPtr &memoryManager)
LayerTestResult< armnn::BFloat16, 4 > PadBFloat164dTest(armnn::IWorkloadFactory &workloadFactory, const armnn::IBackendInternal::IMemoryManagerSharedPtr &memoryManager)
LayerTestResult< int8_t, 4 > PadInt84dTest(armnn::IWorkloadFactory &workloadFactory, const armnn::IBackendInternal::IMemoryManagerSharedPtr &memoryManager)
Contains information about inputs and outputs to a layer.
LayerTestResult< uint8_t, 2 > PadUint82dCustomPaddingTest(armnn::IWorkloadFactory &workloadFactory, const armnn::IBackendInternal::IMemoryManagerSharedPtr &memoryManager)
LayerTestResult< T, 2 > Pad2dTestCommon(armnn::IWorkloadFactory &workloadFactory, const armnn::IBackendInternal::IMemoryManagerSharedPtr &memoryManager, float qScale, int32_t qOffset, const float customPaddingValue)
Definition: PadTestImpl.cpp:20
void CopyDataToITensorHandle(armnn::ITensorHandle *tensorHandle, const void *memory)