ArmNN  NotReleased
PermuteTestImpl.hpp
Go to the documentation of this file.
1 //
2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
6 #pragma once
7 
8 #include <ResolveType.hpp>
9 
10 
13 
15 
16 #include <test/TensorHelpers.hpp>
17 
18 template<typename T>
20  armnn::IWorkloadFactory& workloadFactory,
22  armnn::PermuteDescriptor descriptor,
23  armnn::TensorInfo inputTensorInfo,
24  armnn::TensorInfo outputTensorInfo,
25  const std::vector<T>& inputData,
26  const std::vector<T>& outputExpectedData)
27 {
28  boost::ignore_unused(memoryManager);
29  auto input = MakeTensor<T, 4>(inputTensorInfo, inputData);
30 
31  LayerTestResult<T, 4> ret(outputTensorInfo);
32  ret.outputExpected = MakeTensor<T, 4>(outputTensorInfo, outputExpectedData);
33 
34  std::unique_ptr<armnn::ITensorHandle> inputHandle = workloadFactory.CreateTensorHandle(inputTensorInfo);
35  std::unique_ptr<armnn::ITensorHandle> outputHandle = workloadFactory.CreateTensorHandle(outputTensorInfo);
36 
38  data.m_Parameters = descriptor;
40  AddInputToWorkload(data, info, inputTensorInfo, inputHandle.get());
41  AddOutputToWorkload(data, info, outputTensorInfo, outputHandle.get());
42 
43  std::unique_ptr<armnn::IWorkload> workload = workloadFactory.CreatePermute(data, info);
44 
45  inputHandle->Allocate();
46  outputHandle->Allocate();
47 
48  CopyDataToITensorHandle(inputHandle.get(), &input[0][0][0][0]);
49 
50  workload->Execute();
51 
52  CopyDataFromITensorHandle(&ret.output[0][0][0][0], outputHandle.get());
53 
54  return ret;
55 }
56 
57 template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>>
59  armnn::IWorkloadFactory& workloadFactory,
61 {
62  armnn::TensorInfo inputTensorInfo;
63  armnn::TensorInfo outputTensorInfo;
64 
65  unsigned int inputShape[] = { 1, 2, 2, 2 };
66  unsigned int outputShape[] = { 1, 2, 2, 2 };
67 
68  armnn::PermuteDescriptor descriptor;
69  descriptor.m_DimMappings = {0U, 3U, 1U, 2U};
70 
71  inputTensorInfo = armnn::TensorInfo(4, inputShape, ArmnnType);
72  outputTensorInfo = armnn::TensorInfo(4, outputShape, ArmnnType);
73 
74  // Set quantization parameters if the requested type is a quantized type.
75  if(armnn::IsQuantizedType<T>())
76  {
77  inputTensorInfo.SetQuantizationScale(0.5f);
78  inputTensorInfo.SetQuantizationOffset(5);
79  outputTensorInfo.SetQuantizationScale(0.5f);
80  outputTensorInfo.SetQuantizationOffset(5);
81  }
82 
83  std::vector<T> input = std::vector<T>(
84  {
85  1, 2,
86  3, 4,
87  5, 6,
88  7, 8
89  });
90 
91  std::vector<T> outputExpected = std::vector<T>(
92  {
93  1, 5, 2, 6,
94  3, 7, 4, 8
95  });
96 
97  return SimplePermuteTestImpl<T>(workloadFactory, memoryManager,
98  descriptor, inputTensorInfo,
99  outputTensorInfo, input, outputExpected);
100 }
101 
102 template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>>
104  armnn::IWorkloadFactory& workloadFactory,
106 {
107  armnn::TensorInfo inputTensorInfo;
108  armnn::TensorInfo outputTensorInfo;
109 
110  unsigned int inputShape[] = { 1, 2, 2, 3 };
111  unsigned int outputShape[] = { 1, 3, 2, 2 };
112 
113  armnn::PermuteDescriptor descriptor;
114  descriptor.m_DimMappings = {0U, 2U, 3U, 1U};
115 
116  inputTensorInfo = armnn::TensorInfo(4, inputShape, ArmnnType);
117  outputTensorInfo = armnn::TensorInfo(4, outputShape, ArmnnType);
118 
119  // Set quantization parameters if the requested type is a quantized type.
120  if(armnn::IsQuantizedType<T>())
121  {
122  inputTensorInfo.SetQuantizationScale(0.5f);
123  inputTensorInfo.SetQuantizationOffset(5);
124  outputTensorInfo.SetQuantizationScale(0.5f);
125  outputTensorInfo.SetQuantizationOffset(5);
126  }
127 
128  std::vector<T> input = std::vector<T>(
129  {
130  1, 2, 3,
131  11, 12, 13,
132  21, 22, 23,
133  31, 32, 33
134  });
135 
136  std::vector<T> outputExpected = std::vector<T>(
137  {
138  1, 11, 21, 31,
139  2, 12, 22, 32,
140  3, 13, 23, 33
141  });
142 
143  return SimplePermuteTestImpl<T>(workloadFactory, memoryManager,
144  descriptor, inputTensorInfo,
145  outputTensorInfo, input, outputExpected);
146 }
147 
148 template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>>
150  armnn::IWorkloadFactory& workloadFactory,
152 {
153  armnn::TensorInfo inputTensorInfo;
154  armnn::TensorInfo outputTensorInfo;
155 
156  unsigned int inputShape[] = { 1, 3, 2, 2 };
157  unsigned int outputShape[] = { 1, 2, 2, 3 };
158 
159  armnn::PermuteDescriptor descriptor;
160  descriptor.m_DimMappings = {0U, 3U, 1U, 2U};
161 
162  inputTensorInfo = armnn::TensorInfo(4, inputShape, ArmnnType);
163  outputTensorInfo = armnn::TensorInfo(4, outputShape, ArmnnType);
164 
165  // Set quantization parameters if the requested type is a quantized type.
166  if(armnn::IsQuantizedType<T>())
167  {
168  inputTensorInfo.SetQuantizationScale(0.5f);
169  inputTensorInfo.SetQuantizationOffset(5);
170  outputTensorInfo.SetQuantizationScale(0.5f);
171  outputTensorInfo.SetQuantizationOffset(5);
172  }
173 
174  std::vector<T> input = std::vector<T>(
175  {
176  1, 11, 21, 31,
177  2, 12, 22, 32,
178  3, 13, 23, 33
179  });
180 
181  std::vector<T> outputExpected = std::vector<T>(
182  {
183  1, 2, 3,
184  11, 12, 13,
185  21, 22, 23,
186  31, 32, 33,
187  });
188 
189  return SimplePermuteTestImpl<T>(workloadFactory, memoryManager,
190  descriptor, inputTensorInfo,
191  outputTensorInfo, input, outputExpected);
192 }
193 
194 template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>>
196  armnn::IWorkloadFactory& workloadFactory,
198 {
199  armnn::TensorInfo inputTensorInfo;
200  armnn::TensorInfo outputTensorInfo;
201 
202  unsigned int inputShape[] = { 1, 2, 3, 3 };
203  unsigned int outputShape[] = { 1, 3, 2, 3 };
204 
205  armnn::PermuteDescriptor descriptor;
206  descriptor.m_DimMappings = {0U, 2U, 3U, 1U};
207 
208  inputTensorInfo = armnn::TensorInfo(4, inputShape, ArmnnType);
209  outputTensorInfo = armnn::TensorInfo(4, outputShape, ArmnnType);
210 
211  // Set quantization parameters if the requested type is a quantized type.
212  if(armnn::IsQuantizedType<T>())
213  {
214  inputTensorInfo.SetQuantizationScale(0.5f);
215  inputTensorInfo.SetQuantizationOffset(5);
216  outputTensorInfo.SetQuantizationScale(0.5f);
217  outputTensorInfo.SetQuantizationOffset(5);
218  }
219 
220  std::vector<T> input = std::vector<T>(
221  {
222  1, 2, 3,
223  11, 12, 13,
224  21, 22, 23,
225  31, 32, 33,
226  41, 42, 43,
227  51, 52, 53
228  });
229 
230  std::vector<T> outputExpected = std::vector<T>(
231  {
232  1, 11, 21, 31, 41, 51,
233  2, 12, 22, 32, 42, 52,
234  3, 13, 23, 33, 43, 53
235  });
236 
237  return SimplePermuteTestImpl<T>(workloadFactory, memoryManager,
238  descriptor, inputTensorInfo,
239  outputTensorInfo, input, outputExpected);
240 }
LayerTestResult< T, 4 > SimplePermuteTestImpl(armnn::IWorkloadFactory &workloadFactory, const armnn::IBackendInternal::IMemoryManagerSharedPtr &memoryManager, armnn::PermuteDescriptor descriptor, armnn::TensorInfo inputTensorInfo, armnn::TensorInfo outputTensorInfo, const std::vector< T > &inputData, const std::vector< T > &outputExpectedData)
void CopyDataToITensorHandle(armnn::ITensorHandle *tensorHandle, const void *memory)
LayerTestResult< T, 4 > SimplePermuteTest(armnn::IWorkloadFactory &workloadFactory, const armnn::IBackendInternal::IMemoryManagerSharedPtr &memoryManager)
LayerTestResult< T, 4 > PermuteValueSet3Test(armnn::IWorkloadFactory &workloadFactory, const armnn::IBackendInternal::IMemoryManagerSharedPtr &memoryManager)
virtual std::unique_ptr< ITensorHandle > CreateTensorHandle(const TensorInfo &tensorInfo, const bool IsMemoryManaged=true) const =0
void CopyDataFromITensorHandle(void *memory, const armnn::ITensorHandle *tensorHandle)
std::shared_ptr< IMemoryManager > IMemoryManagerSharedPtr
LayerTestResult< T, 4 > PermuteValueSet1Test(armnn::IWorkloadFactory &workloadFactory, const armnn::IBackendInternal::IMemoryManagerSharedPtr &memoryManager)
void SetQuantizationScale(float scale)
Definition: Tensor.cpp:259
PermutationVector m_DimMappings
Indicates how to translate tensor elements from a given source into the target destination, when source and target potentially have different memory layouts e.g. {0U, 3U, 1U, 2U}.
LayerTestResult< T, 4 > PermuteValueSet2Test(armnn::IWorkloadFactory &workloadFactory, const armnn::IBackendInternal::IMemoryManagerSharedPtr &memoryManager)
virtual std::unique_ptr< IWorkload > CreatePermute(const PermuteQueueDescriptor &descriptor, const WorkloadInfo &info) const
A PermuteDescriptor for the PermuteLayer.
void SetQuantizationOffset(int32_t offset)
Definition: Tensor.cpp:275