ArmNN
 20.02
GatherTestImpl.cpp
Go to the documentation of this file.
1 //
2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
6 #include "GatherTestImpl.hpp"
7 
8 #include <ResolveType.hpp>
9 
10 
13 
14 #include <test/TensorHelpers.hpp>
15 
16 namespace
17 {
18 
19 template <armnn::DataType ArmnnType,
20  typename T = armnn::ResolveType<ArmnnType>,
21  size_t ParamsDim,
22  size_t IndicesDim,
23  size_t OutputDim>
24 LayerTestResult<T, OutputDim> GatherTestImpl(
25  armnn::IWorkloadFactory& workloadFactory,
27  const armnn::TensorInfo& paramsInfo,
28  const armnn::TensorInfo& indicesInfo,
29  const armnn::TensorInfo& outputInfo,
30  const std::vector<T>& paramsData,
31  const std::vector<int32_t>& indicesData,
32  const std::vector<T>& outputData)
33 {
34  IgnoreUnused(memoryManager);
35  auto params = MakeTensor<T, ParamsDim>(paramsInfo, paramsData);
36  auto indices = MakeTensor<int32_t, IndicesDim>(indicesInfo, indicesData);
37 
38  LayerTestResult<T, OutputDim> result(outputInfo);
39  result.outputExpected = MakeTensor<T, OutputDim>(outputInfo, outputData);
40 
41  std::unique_ptr<armnn::ITensorHandle> paramsHandle = workloadFactory.CreateTensorHandle(paramsInfo);
42  std::unique_ptr<armnn::ITensorHandle> indicesHandle = workloadFactory.CreateTensorHandle(indicesInfo);
43  std::unique_ptr<armnn::ITensorHandle> outputHandle = workloadFactory.CreateTensorHandle(outputInfo);
44 
47  AddInputToWorkload(data, info, paramsInfo, paramsHandle.get());
48  AddInputToWorkload(data, info, indicesInfo, indicesHandle.get());
49  AddOutputToWorkload(data, info, outputInfo, outputHandle.get());
50 
51  std::unique_ptr<armnn::IWorkload> workload = workloadFactory.CreateGather(data, info);
52 
53  paramsHandle->Allocate();
54  indicesHandle->Allocate();
55  outputHandle->Allocate();
56 
57  CopyDataToITensorHandle(paramsHandle.get(), params.origin());
58  CopyDataToITensorHandle(indicesHandle.get(), indices.origin());
59 
60  workload->Execute();
61 
62  CopyDataFromITensorHandle(result.output.origin(), outputHandle.get());
63 
64  return result;
65 }
66 
67 template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>>
68 struct GatherTestHelper
69 {
70  static LayerTestResult<T, 1> Gather1dParamsTestImpl(
71  armnn::IWorkloadFactory& workloadFactory,
73  {
74  armnn::TensorInfo paramsInfo({ 8 }, ArmnnType);
75  armnn::TensorInfo indicesInfo({ 4 }, armnn::DataType::Signed32);
76  armnn::TensorInfo outputInfo({ 4 }, ArmnnType);
77 
78  if (armnn::IsQuantizedType<T>())
79  {
80  paramsInfo.SetQuantizationScale(1.0f);
81  paramsInfo.SetQuantizationOffset(1);
82  outputInfo.SetQuantizationScale(1.0f);
83  outputInfo.SetQuantizationOffset(1);
84  }
85  const std::vector<T> params = std::vector<T>({ 1, 2, 3, 4, 5, 6, 7, 8 });
86  const std::vector<int32_t> indices = std::vector<int32_t>({ 0, 2, 1, 5 });
87  const std::vector<T> expectedOutput = std::vector<T>({ 1, 3, 2, 6 });
88 
89  return GatherTestImpl<ArmnnType, T, 1, 1, 1>(
90  workloadFactory,
91  memoryManager,
92  paramsInfo,
93  indicesInfo,
94  outputInfo,
95  params,
96  indices,
97  expectedOutput);
98  }
99 
100  static LayerTestResult<T, 2> GatherMultiDimParamsTestImpl(
101  armnn::IWorkloadFactory& workloadFactory,
103  {
104  armnn::TensorInfo paramsInfo({ 5, 2 }, ArmnnType);
105  armnn::TensorInfo indicesInfo({ 3 }, armnn::DataType::Signed32);
106  armnn::TensorInfo outputInfo({ 3, 2 }, ArmnnType);
107 
108  if (armnn::IsQuantizedType<T>())
109  {
110  paramsInfo.SetQuantizationScale(1.0f);
111  paramsInfo.SetQuantizationOffset(1);
112  outputInfo.SetQuantizationScale(1.0f);
113  outputInfo.SetQuantizationOffset(1);
114  }
115 
116  const std::vector<T> params = std::vector<T>({ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 });
117  const std::vector<int32_t> indices = std::vector<int32_t>({ 1, 3, 4 });
118  const std::vector<T> expectedOutput = std::vector<T>({ 3, 4, 7, 8, 9, 10 });
119 
120  return GatherTestImpl<ArmnnType, T, 2, 1, 2>(
121  workloadFactory,
122  memoryManager,
123  paramsInfo,
124  indicesInfo,
125  outputInfo,
126  params,
127  indices,
128  expectedOutput);
129  }
130 
131  static LayerTestResult<T, 4> GatherMultiDimParamsMultiDimIndicesTestImpl(
132  armnn::IWorkloadFactory& workloadFactory,
134  {
135  armnn::TensorInfo paramsInfo({ 3, 2, 3}, ArmnnType);
136  armnn::TensorInfo indicesInfo({ 2, 3 }, armnn::DataType::Signed32);
137  armnn::TensorInfo outputInfo({ 2, 3, 2, 3 }, ArmnnType);
138 
139  if (armnn::IsQuantizedType<T>())
140  {
141  paramsInfo.SetQuantizationScale(1.0f);
142  paramsInfo.SetQuantizationOffset(1);
143  outputInfo.SetQuantizationScale(1.0f);
144  outputInfo.SetQuantizationOffset(1);
145  }
146 
147  const std::vector<T> params =
148  {
149  1, 2, 3,
150  4, 5, 6,
151 
152  7, 8, 9,
153  10, 11, 12,
154 
155  13, 14, 15,
156  16, 17, 18
157  };
158 
159  const std::vector<int32_t> indices = { 1, 2, 1, 2, 1, 0 };
160 
161  const std::vector<T> expectedOutput =
162  {
163  7, 8, 9,
164  10, 11, 12,
165  13, 14, 15,
166  16, 17, 18,
167  7, 8, 9,
168  10, 11, 12,
169 
170  13, 14, 15,
171  16, 17, 18,
172  7, 8, 9,
173  10, 11, 12,
174  1, 2, 3,
175  4, 5, 6
176  };
177 
178  return GatherTestImpl<ArmnnType, T, 3, 2, 4>(
179  workloadFactory,
180  memoryManager,
181  paramsInfo,
182  indicesInfo,
183  outputInfo,
184  params,
185  indices,
186  expectedOutput);
187  }
188 };
189 
190 template<typename T>
191 struct GatherTestHelper<armnn::DataType::Float16, T>
192 {
193  static LayerTestResult<T, 1> Gather1dParamsTestImpl(
194  armnn::IWorkloadFactory& workloadFactory,
196  {
197  using namespace half_float::literal;
198 
199  armnn::TensorInfo paramsInfo({ 8 }, armnn::DataType::Float16);
200  armnn::TensorInfo indicesInfo({ 4 }, armnn::DataType::Signed32);
201  armnn::TensorInfo outputInfo({ 4 }, armnn::DataType::Float16);
202 
203  const std::vector<T> params = std::vector<T>({ 1._h, 2._h, 3._h, 4._h, 5._h, 6._h, 7._h, 8._h });
204  const std::vector<int32_t> indices = std::vector<int32_t>({ 0, 2, 1, 5 });
205  const std::vector<T> expectedOutput = std::vector<T>({ 1._h, 3._h, 2._h, 6._h });
206 
207  return GatherTestImpl<armnn::DataType::Float16, T, 1, 1, 1>(
208  workloadFactory,
209  memoryManager,
210  paramsInfo,
211  indicesInfo,
212  outputInfo,
213  params,
214  indices,
215  expectedOutput);
216  }
217 
218  static LayerTestResult<T, 2> GatherMultiDimParamsTestImpl(
219  armnn::IWorkloadFactory& workloadFactory,
221  {
222  using namespace half_float::literal;
223 
224  armnn::TensorInfo paramsInfo({ 5, 2 }, armnn::DataType::Float16);
225  armnn::TensorInfo indicesInfo({ 3 }, armnn::DataType::Signed32);
226  armnn::TensorInfo outputInfo({ 3, 2 }, armnn::DataType::Float16);
227 
228  const std::vector<T> params = std::vector<T>({ 1._h, 2._h, 3._h, 4._h, 5._h, 6._h, 7._h, 8._h, 9._h, 10._h });
229 
230  const std::vector<int32_t> indices = std::vector<int32_t>({ 1, 3, 4 });
231  const std::vector<T> expectedOutput = std::vector<T>({ 3._h, 4._h, 7._h, 8._h, 9._h, 10._h });
232 
233  return GatherTestImpl<armnn::DataType::Float16, T, 2, 1, 2>(
234  workloadFactory,
235  memoryManager,
236  paramsInfo,
237  indicesInfo,
238  outputInfo,
239  params,
240  indices,
241  expectedOutput);
242  }
243 
244  static LayerTestResult<T, 4> GatherMultiDimParamsMultiDimIndicesTestImpl(
245  armnn::IWorkloadFactory& workloadFactory,
247  {
248  using namespace half_float::literal;
249 
250  armnn::TensorInfo paramsInfo({ 3, 2, 3 }, armnn::DataType::Float16);
251  armnn::TensorInfo indicesInfo({ 2, 3 }, armnn::DataType::Signed32);
252  armnn::TensorInfo outputInfo({ 2, 3, 2, 3 }, armnn::DataType::Float16);
253 
254  const std::vector<T> params =
255  {
256  1._h, 2._h, 3._h,
257  4._h, 5._h, 6._h,
258 
259  7._h, 8._h, 9._h,
260  10._h, 11._h, 12._h,
261 
262  13._h, 14._h, 15._h,
263  16._h, 17._h, 18._h
264  };
265 
266  const std::vector<int32_t> indices = { 1, 2, 1, 2, 1, 0 };
267 
268  const std::vector<T> expectedOutput =
269  {
270  7._h, 8._h, 9._h,
271  10._h, 11._h, 12._h,
272  13._h, 14._h, 15._h,
273  16._h, 17._h, 18._h,
274  7._h, 8._h, 9._h,
275  10._h, 11._h, 12._h,
276 
277  13._h, 14._h, 15._h,
278  16._h, 17._h, 18._h,
279  7._h, 8._h, 9._h,
280  10._h, 11._h, 12._h,
281  1._h, 2._h, 3._h,
282  4._h, 5._h, 6._h
283  };
284 
285  return GatherTestImpl<armnn::DataType::Float16, T, 3, 2, 4>(
286  workloadFactory,
287  memoryManager,
288  paramsInfo,
289  indicesInfo,
290  outputInfo,
291  params,
292  indices,
293  expectedOutput);
294  }
295 };
296 
297 } // anonymous namespace
298 
300  armnn::IWorkloadFactory& workloadFactory,
302 {
303  return GatherTestHelper<armnn::DataType::Float32>::Gather1dParamsTestImpl(workloadFactory, memoryManager);
304 }
305 
307  armnn::IWorkloadFactory& workloadFactory,
309 {
310  return GatherTestHelper<armnn::DataType::Float16>::Gather1dParamsTestImpl(workloadFactory, memoryManager);
311 }
312 
314  armnn::IWorkloadFactory& workloadFactory,
316 {
317  return GatherTestHelper<armnn::DataType::QAsymmU8>::Gather1dParamsTestImpl(workloadFactory, memoryManager);
318 }
319 
321  armnn::IWorkloadFactory& workloadFactory,
323 {
324  return GatherTestHelper<armnn::DataType::QSymmS16>::Gather1dParamsTestImpl(workloadFactory, memoryManager);
325 }
326 
328  armnn::IWorkloadFactory& workloadFactory,
330 {
331  return GatherTestHelper<armnn::DataType::Float32>::GatherMultiDimParamsTestImpl(workloadFactory, memoryManager);
332 }
333 
335  armnn::IWorkloadFactory& workloadFactory,
337 {
338  return GatherTestHelper<armnn::DataType::Float16>::GatherMultiDimParamsTestImpl(workloadFactory, memoryManager);
339 }
340 
342  armnn::IWorkloadFactory& workloadFactory,
344 {
345  return GatherTestHelper<armnn::DataType::QAsymmU8>::GatherMultiDimParamsTestImpl(
346  workloadFactory, memoryManager);
347 }
348 
350  armnn::IWorkloadFactory& workloadFactory,
352 {
353  return GatherTestHelper<armnn::DataType::QSymmS16>::GatherMultiDimParamsTestImpl(
354  workloadFactory, memoryManager);
355 }
356 
358  armnn::IWorkloadFactory& workloadFactory,
360 {
361  return GatherTestHelper<armnn::DataType::Float32>::GatherMultiDimParamsMultiDimIndicesTestImpl(
362  workloadFactory, memoryManager);
363 }
364 
366  armnn::IWorkloadFactory& workloadFactory,
368 {
369  return GatherTestHelper<armnn::DataType::Float16>::GatherMultiDimParamsMultiDimIndicesTestImpl(
370  workloadFactory, memoryManager);
371 }
372 
374  armnn::IWorkloadFactory& workloadFactory,
376 {
377  return GatherTestHelper<armnn::DataType::QAsymmU8>::GatherMultiDimParamsMultiDimIndicesTestImpl(
378  workloadFactory, memoryManager);
379 }
380 
382  armnn::IWorkloadFactory& workloadFactory,
384 {
385  return GatherTestHelper<armnn::DataType::QSymmS16>::GatherMultiDimParamsMultiDimIndicesTestImpl(
386  workloadFactory, memoryManager);
387 }
LayerTestResult< armnn::Half, 4 > GatherMultiDimParamsMultiDimIndicesFloat16Test(armnn::IWorkloadFactory &workloadFactory, const armnn::IBackendInternal::IMemoryManagerSharedPtr &memoryManager)
LayerTestResult< uint8_t, 1 > Gather1dParamsUint8Test(armnn::IWorkloadFactory &workloadFactory, const armnn::IBackendInternal::IMemoryManagerSharedPtr &memoryManager)
LayerTestResult< int16_t, 1 > Gather1dParamsInt16Test(armnn::IWorkloadFactory &workloadFactory, const armnn::IBackendInternal::IMemoryManagerSharedPtr &memoryManager)
LayerTestResult< armnn::Half, 1 > Gather1dParamsFloat16Test(armnn::IWorkloadFactory &workloadFactory, const armnn::IBackendInternal::IMemoryManagerSharedPtr &memoryManager)
typename ResolveTypeImpl< DT >::Type ResolveType
Definition: ResolveType.hpp:73
Copyright (c) 2020 ARM Limited.
void IgnoreUnused(Ts &&...)
LayerTestResult< uint8_t, 2 > GatherMultiDimParamsUint8Test(armnn::IWorkloadFactory &workloadFactory, const armnn::IBackendInternal::IMemoryManagerSharedPtr &memoryManager)
LayerTestResult< int16_t, 2 > GatherMultiDimParamsInt16Test(armnn::IWorkloadFactory &workloadFactory, const armnn::IBackendInternal::IMemoryManagerSharedPtr &memoryManager)
LayerTestResult< float, 4 > GatherMultiDimParamsMultiDimIndicesFloat32Test(armnn::IWorkloadFactory &workloadFactory, const armnn::IBackendInternal::IMemoryManagerSharedPtr &memoryManager)
LayerTestResult< int16_t, 4 > GatherMultiDimParamsMultiDimIndicesInt16Test(armnn::IWorkloadFactory &workloadFactory, const armnn::IBackendInternal::IMemoryManagerSharedPtr &memoryManager)
DataType
Definition: Types.hpp:32
std::shared_ptr< IMemoryManager > IMemoryManagerSharedPtr
void SetQuantizationScale(float scale)
Definition: Tensor.cpp:259
void CopyDataFromITensorHandle(void *memory, const armnn::ITensorHandle *tensorHandle)
LayerTestResult< uint8_t, 4 > GatherMultiDimParamsMultiDimIndicesUint8Test(armnn::IWorkloadFactory &workloadFactory, const armnn::IBackendInternal::IMemoryManagerSharedPtr &memoryManager)
virtual std::unique_ptr< ITensorHandle > CreateTensorHandle(const TensorInfo &tensorInfo, const bool IsMemoryManaged=true) const =0
LayerTestResult< armnn::Half, 2 > GatherMultiDimParamsFloat16Test(armnn::IWorkloadFactory &workloadFactory, const armnn::IBackendInternal::IMemoryManagerSharedPtr &memoryManager)
virtual std::unique_ptr< IWorkload > CreateGather(const GatherQueueDescriptor &descriptor, const WorkloadInfo &info) const
LayerTestResult< float, 1 > Gather1dParamsFloat32Test(armnn::IWorkloadFactory &workloadFactory, const armnn::IBackendInternal::IMemoryManagerSharedPtr &memoryManager)
LayerTestResult< float, 2 > GatherMultiDimParamsFloat32Test(armnn::IWorkloadFactory &workloadFactory, const armnn::IBackendInternal::IMemoryManagerSharedPtr &memoryManager)
Contains information about inputs and outputs to a layer.
void CopyDataToITensorHandle(armnn::ITensorHandle *tensorHandle, const void *memory)