ArmNN
 20.08
GatherTestImpl.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 "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 
42  std::unique_ptr<armnn::ITensorHandle> paramsHandle = workloadFactory.CreateTensorHandle(paramsInfo);
43  std::unique_ptr<armnn::ITensorHandle> indicesHandle = workloadFactory.CreateTensorHandle(indicesInfo);
44  std::unique_ptr<armnn::ITensorHandle> outputHandle = workloadFactory.CreateTensorHandle(outputInfo);
46 
49  AddInputToWorkload(data, info, paramsInfo, paramsHandle.get());
50  AddInputToWorkload(data, info, indicesInfo, indicesHandle.get());
51  AddOutputToWorkload(data, info, outputInfo, outputHandle.get());
52 
53  std::unique_ptr<armnn::IWorkload> workload = workloadFactory.CreateGather(data, info);
54 
55  paramsHandle->Allocate();
56  indicesHandle->Allocate();
57  outputHandle->Allocate();
58 
59  CopyDataToITensorHandle(paramsHandle.get(), params.origin());
60  CopyDataToITensorHandle(indicesHandle.get(), indices.origin());
61 
62  workload->Execute();
63 
64  CopyDataFromITensorHandle(result.output.origin(), outputHandle.get());
65 
66  return result;
67 }
68 
69 template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>>
70 struct GatherTestHelper
71 {
72  static LayerTestResult<T, 1> Gather1dParamsTestImpl(
73  armnn::IWorkloadFactory& workloadFactory,
75  {
76  armnn::TensorInfo paramsInfo({ 8 }, ArmnnType);
77  armnn::TensorInfo indicesInfo({ 4 }, armnn::DataType::Signed32);
78  armnn::TensorInfo outputInfo({ 4 }, ArmnnType);
79 
80  if (armnn::IsQuantizedType<T>())
81  {
82  paramsInfo.SetQuantizationScale(1.0f);
83  paramsInfo.SetQuantizationOffset(1);
84  outputInfo.SetQuantizationScale(1.0f);
85  outputInfo.SetQuantizationOffset(1);
86  }
87  const std::vector<T> params = std::vector<T>({ 1, 2, 3, 4, 5, 6, 7, 8 });
88  const std::vector<int32_t> indices = std::vector<int32_t>({ 0, 2, 1, 5 });
89  const std::vector<T> expectedOutput = std::vector<T>({ 1, 3, 2, 6 });
90 
91  return GatherTestImpl<ArmnnType, T, 1, 1, 1>(
92  workloadFactory,
93  memoryManager,
94  paramsInfo,
95  indicesInfo,
96  outputInfo,
97  params,
98  indices,
99  expectedOutput);
100  }
101 
102  static LayerTestResult<T, 2> GatherMultiDimParamsTestImpl(
103  armnn::IWorkloadFactory& workloadFactory,
105  {
106  armnn::TensorInfo paramsInfo({ 5, 2 }, ArmnnType);
107  armnn::TensorInfo indicesInfo({ 3 }, armnn::DataType::Signed32);
108  armnn::TensorInfo outputInfo({ 3, 2 }, ArmnnType);
109 
110  if (armnn::IsQuantizedType<T>())
111  {
112  paramsInfo.SetQuantizationScale(1.0f);
113  paramsInfo.SetQuantizationOffset(1);
114  outputInfo.SetQuantizationScale(1.0f);
115  outputInfo.SetQuantizationOffset(1);
116  }
117 
118  const std::vector<T> params = std::vector<T>({ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 });
119  const std::vector<int32_t> indices = std::vector<int32_t>({ 1, 3, 4 });
120  const std::vector<T> expectedOutput = std::vector<T>({ 3, 4, 7, 8, 9, 10 });
121 
122  return GatherTestImpl<ArmnnType, T, 2, 1, 2>(
123  workloadFactory,
124  memoryManager,
125  paramsInfo,
126  indicesInfo,
127  outputInfo,
128  params,
129  indices,
130  expectedOutput);
131  }
132 
133  static LayerTestResult<T, 4> GatherMultiDimParamsMultiDimIndicesTestImpl(
134  armnn::IWorkloadFactory& workloadFactory,
136  {
137  armnn::TensorInfo paramsInfo({ 3, 2, 3}, ArmnnType);
138  armnn::TensorInfo indicesInfo({ 2, 3 }, armnn::DataType::Signed32);
139  armnn::TensorInfo outputInfo({ 2, 3, 2, 3 }, ArmnnType);
140 
141  if (armnn::IsQuantizedType<T>())
142  {
143  paramsInfo.SetQuantizationScale(1.0f);
144  paramsInfo.SetQuantizationOffset(1);
145  outputInfo.SetQuantizationScale(1.0f);
146  outputInfo.SetQuantizationOffset(1);
147  }
148 
149  const std::vector<T> params =
150  {
151  1, 2, 3,
152  4, 5, 6,
153 
154  7, 8, 9,
155  10, 11, 12,
156 
157  13, 14, 15,
158  16, 17, 18
159  };
160 
161  const std::vector<int32_t> indices = { 1, 2, 1, 2, 1, 0 };
162 
163  const std::vector<T> expectedOutput =
164  {
165  7, 8, 9,
166  10, 11, 12,
167  13, 14, 15,
168  16, 17, 18,
169  7, 8, 9,
170  10, 11, 12,
171 
172  13, 14, 15,
173  16, 17, 18,
174  7, 8, 9,
175  10, 11, 12,
176  1, 2, 3,
177  4, 5, 6
178  };
179 
180  return GatherTestImpl<ArmnnType, T, 3, 2, 4>(
181  workloadFactory,
182  memoryManager,
183  paramsInfo,
184  indicesInfo,
185  outputInfo,
186  params,
187  indices,
188  expectedOutput);
189  }
190 };
191 
192 template<typename T>
193 struct GatherTestHelper<armnn::DataType::Float16, T>
194 {
195  static LayerTestResult<T, 1> Gather1dParamsTestImpl(
196  armnn::IWorkloadFactory& workloadFactory,
198  {
199  using namespace half_float::literal;
200 
201  armnn::TensorInfo paramsInfo({ 8 }, armnn::DataType::Float16);
202  armnn::TensorInfo indicesInfo({ 4 }, armnn::DataType::Signed32);
203  armnn::TensorInfo outputInfo({ 4 }, armnn::DataType::Float16);
204 
205  const std::vector<T> params = std::vector<T>({ 1._h, 2._h, 3._h, 4._h, 5._h, 6._h, 7._h, 8._h });
206  const std::vector<int32_t> indices = std::vector<int32_t>({ 0, 2, 1, 5 });
207  const std::vector<T> expectedOutput = std::vector<T>({ 1._h, 3._h, 2._h, 6._h });
208 
209  return GatherTestImpl<armnn::DataType::Float16, T, 1, 1, 1>(
210  workloadFactory,
211  memoryManager,
212  paramsInfo,
213  indicesInfo,
214  outputInfo,
215  params,
216  indices,
217  expectedOutput);
218  }
219 
220  static LayerTestResult<T, 2> GatherMultiDimParamsTestImpl(
221  armnn::IWorkloadFactory& workloadFactory,
223  {
224  using namespace half_float::literal;
225 
226  armnn::TensorInfo paramsInfo({ 5, 2 }, armnn::DataType::Float16);
227  armnn::TensorInfo indicesInfo({ 3 }, armnn::DataType::Signed32);
228  armnn::TensorInfo outputInfo({ 3, 2 }, armnn::DataType::Float16);
229 
230  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 });
231 
232  const std::vector<int32_t> indices = std::vector<int32_t>({ 1, 3, 4 });
233  const std::vector<T> expectedOutput = std::vector<T>({ 3._h, 4._h, 7._h, 8._h, 9._h, 10._h });
234 
235  return GatherTestImpl<armnn::DataType::Float16, T, 2, 1, 2>(
236  workloadFactory,
237  memoryManager,
238  paramsInfo,
239  indicesInfo,
240  outputInfo,
241  params,
242  indices,
243  expectedOutput);
244  }
245 
246  static LayerTestResult<T, 4> GatherMultiDimParamsMultiDimIndicesTestImpl(
247  armnn::IWorkloadFactory& workloadFactory,
249  {
250  using namespace half_float::literal;
251 
252  armnn::TensorInfo paramsInfo({ 3, 2, 3 }, armnn::DataType::Float16);
253  armnn::TensorInfo indicesInfo({ 2, 3 }, armnn::DataType::Signed32);
254  armnn::TensorInfo outputInfo({ 2, 3, 2, 3 }, armnn::DataType::Float16);
255 
256  const std::vector<T> params =
257  {
258  1._h, 2._h, 3._h,
259  4._h, 5._h, 6._h,
260 
261  7._h, 8._h, 9._h,
262  10._h, 11._h, 12._h,
263 
264  13._h, 14._h, 15._h,
265  16._h, 17._h, 18._h
266  };
267 
268  const std::vector<int32_t> indices = { 1, 2, 1, 2, 1, 0 };
269 
270  const std::vector<T> expectedOutput =
271  {
272  7._h, 8._h, 9._h,
273  10._h, 11._h, 12._h,
274  13._h, 14._h, 15._h,
275  16._h, 17._h, 18._h,
276  7._h, 8._h, 9._h,
277  10._h, 11._h, 12._h,
278 
279  13._h, 14._h, 15._h,
280  16._h, 17._h, 18._h,
281  7._h, 8._h, 9._h,
282  10._h, 11._h, 12._h,
283  1._h, 2._h, 3._h,
284  4._h, 5._h, 6._h
285  };
286 
287  return GatherTestImpl<armnn::DataType::Float16, T, 3, 2, 4>(
288  workloadFactory,
289  memoryManager,
290  paramsInfo,
291  indicesInfo,
292  outputInfo,
293  params,
294  indices,
295  expectedOutput);
296  }
297 };
298 
299 } // anonymous namespace
300 
302  armnn::IWorkloadFactory& workloadFactory,
304 {
305  return GatherTestHelper<armnn::DataType::Float32>::Gather1dParamsTestImpl(workloadFactory, memoryManager);
306 }
307 
309  armnn::IWorkloadFactory& workloadFactory,
311 {
312  return GatherTestHelper<armnn::DataType::Float16>::Gather1dParamsTestImpl(workloadFactory, memoryManager);
313 }
314 
316  armnn::IWorkloadFactory& workloadFactory,
318 {
319  return GatherTestHelper<armnn::DataType::QAsymmU8>::Gather1dParamsTestImpl(workloadFactory, memoryManager);
320 }
321 
323  armnn::IWorkloadFactory& workloadFactory,
325 {
326  return GatherTestHelper<armnn::DataType::QSymmS16>::Gather1dParamsTestImpl(workloadFactory, memoryManager);
327 }
328 
330  armnn::IWorkloadFactory& workloadFactory,
332 {
333  return GatherTestHelper<armnn::DataType::Signed32>::Gather1dParamsTestImpl(workloadFactory, memoryManager);
334 }
335 
337  armnn::IWorkloadFactory& workloadFactory,
339 {
340  return GatherTestHelper<armnn::DataType::Float32>::GatherMultiDimParamsTestImpl(workloadFactory, memoryManager);
341 }
342 
344  armnn::IWorkloadFactory& workloadFactory,
346 {
347  return GatherTestHelper<armnn::DataType::Float16>::GatherMultiDimParamsTestImpl(workloadFactory, memoryManager);
348 }
349 
351  armnn::IWorkloadFactory& workloadFactory,
353 {
354  return GatherTestHelper<armnn::DataType::QAsymmU8>::GatherMultiDimParamsTestImpl(
355  workloadFactory, memoryManager);
356 }
357 
359  armnn::IWorkloadFactory& workloadFactory,
361 {
362  return GatherTestHelper<armnn::DataType::QSymmS16>::GatherMultiDimParamsTestImpl(
363  workloadFactory, memoryManager);
364 }
365 
367  armnn::IWorkloadFactory& workloadFactory,
369 {
370  return GatherTestHelper<armnn::DataType::Signed32>::GatherMultiDimParamsTestImpl(
371  workloadFactory, memoryManager);
372 }
373 
375  armnn::IWorkloadFactory& workloadFactory,
377 {
378  return GatherTestHelper<armnn::DataType::Float32>::GatherMultiDimParamsMultiDimIndicesTestImpl(
379  workloadFactory, memoryManager);
380 }
381 
383  armnn::IWorkloadFactory& workloadFactory,
385 {
386  return GatherTestHelper<armnn::DataType::Float16>::GatherMultiDimParamsMultiDimIndicesTestImpl(
387  workloadFactory, memoryManager);
388 }
389 
391  armnn::IWorkloadFactory& workloadFactory,
393 {
394  return GatherTestHelper<armnn::DataType::QAsymmU8>::GatherMultiDimParamsMultiDimIndicesTestImpl(
395  workloadFactory, memoryManager);
396 }
397 
399  armnn::IWorkloadFactory& workloadFactory,
401 {
402  return GatherTestHelper<armnn::DataType::QSymmS16>::GatherMultiDimParamsMultiDimIndicesTestImpl(
403  workloadFactory, memoryManager);
404 }
405 
407  armnn::IWorkloadFactory& workloadFactory,
409 {
410  return GatherTestHelper<armnn::DataType::Signed32>::GatherMultiDimParamsMultiDimIndicesTestImpl(
411  workloadFactory, memoryManager);
412 }
LayerTestResult< int32_t, 4 > GatherMultiDimParamsMultiDimIndicesInt32Test(armnn::IWorkloadFactory &workloadFactory, const armnn::IBackendInternal::IMemoryManagerSharedPtr &memoryManager)
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< int32_t, 2 > GatherMultiDimParamsInt32Test(armnn::IWorkloadFactory &workloadFactory, const armnn::IBackendInternal::IMemoryManagerSharedPtr &memoryManager)
#define ARMNN_NO_DEPRECATE_WARN_BEGIN
Definition: Deprecated.hpp:33
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
#define ARMNN_NO_DEPRECATE_WARN_END
Definition: Deprecated.hpp:34
std::shared_ptr< IMemoryManager > IMemoryManagerSharedPtr
LayerTestResult< int32_t, 1 > Gather1dParamsInt32Test(armnn::IWorkloadFactory &workloadFactory, const armnn::IBackendInternal::IMemoryManagerSharedPtr &memoryManager)
void SetQuantizationScale(float scale)
Definition: Tensor.cpp:465
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)