ArmNN
 21.05
ReductionTestImpl.cpp
Go to the documentation of this file.
1 //
2 // Copyright © 2021 Arm Ltd and Contributors. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
6 #include "ReductionTestImpl.hpp"
7 
11 
12 #include <test/TensorHelpers.hpp>
13 
14 #include <iostream>
15 
16 namespace
17 {
18 
19 template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>>
20 LayerTestResult<float, 4> ReductionTestCommon(
21  armnn::IWorkloadFactory& workloadFactory,
23  const armnn::ITensorHandleFactory& tensorHandleFactory,
24  const armnn::TensorInfo inputTensorInfo,
25  const armnn::TensorInfo outputTensorInfo,
26  const std::vector<float>& inputData,
27  const std::vector<float>& outputData,
28  const std::vector<int32_t> vAxis,
29  const armnn::ReduceOperation reduceOperation,
30  bool keepDims = false)
31 {
32  IgnoreUnused(memoryManager);
33  auto inputTensor = MakeTensor<T, 4>(inputTensorInfo, ConvertToDataType<ArmnnType>(inputData, inputTensorInfo));
34 
35  LayerTestResult<float, 4> result(outputTensorInfo);
36  result.outputExpected = MakeTensor<float, 4>(outputTensorInfo, outputData);
37 
38  std::unique_ptr<armnn::ITensorHandle> inputHandle = tensorHandleFactory.CreateTensorHandle(inputTensorInfo);
39  std::unique_ptr<armnn::ITensorHandle> outputHandle = tensorHandleFactory.CreateTensorHandle(outputTensorInfo);
40 
42  std::vector<uint32_t> updated_idx;
43  uint32_t resolvedAxis = 0;
44  for (uint32_t i = 0; i < vAxis.size(); ++i)
45  {
46  if (vAxis[i] < 0)
47  {
48  resolvedAxis = inputTensorInfo.GetNumDimensions() + static_cast<uint32_t>(vAxis[i]);
49  } else
50  {
51  resolvedAxis = static_cast<uint32_t>(vAxis[i]);
52  }
53 
54  updated_idx.push_back(resolvedAxis);
55  }
56 
57  descriptor.m_Parameters.m_vAxis = updated_idx;
58  descriptor.m_Parameters.m_ReduceOperation = reduceOperation;
59  descriptor.m_Parameters.m_KeepDims = keepDims;
61 
62  AddInputToWorkload(descriptor, info, inputTensorInfo, inputHandle.get());
63  AddOutputToWorkload(descriptor, info, outputTensorInfo, outputHandle.get());
64 
65  std::unique_ptr<armnn::IWorkload> workload = workloadFactory.CreateReduce(descriptor, info);
66 
67  inputHandle->Allocate();
68  outputHandle->Allocate();
69 
70  CopyDataToITensorHandle(inputHandle.get(), inputTensor.origin());
71 
72  workload->Execute();
73 
74  CopyDataFromITensorHandle(result.output.origin(), outputHandle.get());
75 
76  return result;
77 }
78 
79 } // namespace
80 
81 template<armnn::DataType ArmnnType, typename T>
83  armnn::IWorkloadFactory& workloadFactory,
85  const armnn::ITensorHandleFactory& tensorHandleFactory)
86 {
87  const armnn::TensorShape inputShape{ 1, 1, 2, 3 };
88  const armnn::TensorShape outputShape{ 1, 1, 1, 3};
89 
90  armnn::TensorInfo inputTensorInfo(inputShape, ArmnnType);
91 
92  if (armnn::IsQuantizedType<T>())
93  {
94  inputTensorInfo.SetQuantizationScale(1.0f);
95  inputTensorInfo.SetQuantizationOffset(0);
96  }
97 
98  armnn::TensorInfo outputTensorInfo(outputShape, armnn::DataType::Float32);
99 
100  std::vector<float> inputValues
101  ({
102  1001.0f, 11.0f, 1003.0f,
103  10.0f, 1002.0f, 12.0f
104  });
105  std::vector<float> outputValues
106  ({
107  1001.0f, 1002.0f, 1003.0f
108  });
109 
110  return ReductionTestCommon<ArmnnType>(workloadFactory,
111  memoryManager,
112  tensorHandleFactory,
113  inputTensorInfo,
114  outputTensorInfo,
115  inputValues,
116  outputValues,
117  { 2 },
119 }
120 
121 template<armnn::DataType ArmnnType, typename T>
123  armnn::IWorkloadFactory& workloadFactory,
125  const armnn::ITensorHandleFactory& tensorHandleFactory)
126 {
127  const armnn::TensorShape inputShape{ 1, 1, 2, 3 };
128  const armnn::TensorShape outputShape{ 1, 1, 2, 1};
129 
130  armnn::TensorInfo inputTensorInfo(inputShape, ArmnnType);
131 
132  if (armnn::IsQuantizedType<T>())
133  {
134  inputTensorInfo.SetQuantizationScale(1.0f);
135  inputTensorInfo.SetQuantizationOffset(0);
136  }
137 
138  armnn::TensorInfo outputTensorInfo(outputShape, armnn::DataType::Float32);
139 
140  std::vector<float> inputValues
141  ({
142  1001.0f, 11.0f, 1003.0f,
143  10.0f, 1002.0f, 12.0f
144  });
145  std::vector<float> outputValues
146  ({
147  1003.0f, 1002.0f
148  });
149 
150  return ReductionTestCommon<ArmnnType>(workloadFactory,
151  memoryManager,
152  tensorHandleFactory,
153  inputTensorInfo,
154  outputTensorInfo,
155  inputValues,
156  outputValues,
157  { -1 },
159  true);
160 }
161 
162 template<armnn::DataType ArmnnType, typename T>
164  armnn::IWorkloadFactory& workloadFactory,
166  const armnn::ITensorHandleFactory& tensorHandleFactory)
167 {
168  const armnn::TensorShape inputShape{ 1, 1, 2, 3 };
169  const armnn::TensorShape outputShape{ 1, 1, 2, 1 };
170 
171  armnn::TensorInfo inputTensorInfo(inputShape, ArmnnType);
172 
173  if (armnn::IsQuantizedType<T>())
174  {
175  inputTensorInfo.SetQuantizationScale(1.0f);
176  inputTensorInfo.SetQuantizationOffset(0);
177  }
178 
179  armnn::TensorInfo outputTensorInfo(outputShape, armnn::DataType::Float32);
180 
181  std::vector<float> inputValues
182  ({
183  1.0f, 3.0f, 2.0f,
184  6.0f, 4.0f, 5.0f
185  });
186 
187  std::vector<float> outputValues
188  ({
189  3.0f, 6.0f
190  });
191 
192  return ReductionTestCommon<ArmnnType>(workloadFactory,
193  memoryManager,
194  tensorHandleFactory,
195  inputTensorInfo,
196  outputTensorInfo,
197  inputValues,
198  outputValues,
199  { 3 },
201  true);
202 }
203 
204 template<armnn::DataType ArmnnType, typename T>
206  armnn::IWorkloadFactory& workloadFactory,
208  const armnn::ITensorHandleFactory& tensorHandleFactory)
209 {
210  const armnn::TensorShape inputShape { 1, 1, 2, 3 };
211  const armnn::TensorShape outputShape { 1, 1, 1, 3};
212 
213  armnn::TensorInfo inputTensorInfo(inputShape, ArmnnType);
214 
215  if (armnn::IsQuantizedType<T>())
216  {
217  inputTensorInfo.SetQuantizationScale(1.0f);
218  inputTensorInfo.SetQuantizationOffset(0);
219  }
220 
221  armnn::TensorInfo outputTensorInfo(outputShape, armnn::DataType::Float32);
222 
223  std::vector<float> inputValues
224  ({
225  1001.0f, 11.0f, 1003.0f,
226  10.0f, 1002.0f, 12.0f
227  });
228  std::vector<float> outputValues
229  ({
230  10.0f, 11.0f, 12.0f
231  });
232 
233  return ReductionTestCommon<ArmnnType>(workloadFactory,
234  memoryManager,
235  tensorHandleFactory,
236  inputTensorInfo,
237  outputTensorInfo,
238  inputValues,
239  outputValues,
240  { 2 },
242 }
243 
244 template<armnn::DataType ArmnnType, typename T>
246  armnn::IWorkloadFactory& workloadFactory,
248  const armnn::ITensorHandleFactory& tensorHandleFactory)
249 {
250  const armnn::TensorShape inputShape{ 1, 1, 2, 3 };
251  const armnn::TensorShape outputShape{ 1, 1, 2, 1};
252 
253  armnn::TensorInfo inputTensorInfo(inputShape, ArmnnType);
254 
255  if (armnn::IsQuantizedType<T>())
256  {
257  inputTensorInfo.SetQuantizationScale(1.0f);
258  inputTensorInfo.SetQuantizationOffset(0);
259  }
260 
261  armnn::TensorInfo outputTensorInfo(outputShape, armnn::DataType::Float32);
262 
263  std::vector<float> inputValues
264  ({
265  1001.0f, 11.0f, 1003.0f,
266  10.0f, 1002.0f, 12.0f
267  });
268  std::vector<float> outputValues
269  ({
270  11.0f, 10.0f
271  });
272 
273  return ReductionTestCommon<ArmnnType>(workloadFactory,
274  memoryManager,
275  tensorHandleFactory,
276  inputTensorInfo,
277  outputTensorInfo,
278  inputValues,
279  outputValues,
280  { -1 },
282  true);
283 }
284 
285 // Explicit template specializations
287 ReduceMaxSimpleTest<armnn::DataType::Float32>(
288  armnn::IWorkloadFactory& workloadFactory,
290  const armnn::ITensorHandleFactory& tensorHandleFactory);
291 
293 ReduceMaxNegativeAxisTest<armnn::DataType::Float32>(
294  armnn::IWorkloadFactory& workloadFactory,
296  const armnn::ITensorHandleFactory& tensorHandleFactory);
297 
299 ReduceMaxSimpleTest2<armnn::DataType::Float32>(
300  armnn::IWorkloadFactory& workloadFactory,
302  const armnn::ITensorHandleFactory& tensorHandleFactory);
303 
305 ReduceMinSimpleTest<armnn::DataType::Float32>(
306  armnn::IWorkloadFactory& workloadFactory,
308  const armnn::ITensorHandleFactory& tensorHandleFactory);
309 
311 ReduceMinNegativeAxisTest<armnn::DataType::Float32>(
312  armnn::IWorkloadFactory& workloadFactory,
314  const armnn::ITensorHandleFactory& tensorHandleFactory);
315 
bool m_KeepDims
if true then output shape has no change.
LayerTestResult< float, 4 > ReduceMaxSimpleTest(armnn::IWorkloadFactory &workloadFactory, const armnn::IBackendInternal::IMemoryManagerSharedPtr &memoryManager, const armnn::ITensorHandleFactory &tensorHandleFactory)
ReduceOperation m_ReduceOperation
Specifies the reduction operation to execute.
void IgnoreUnused(Ts &&...)
ReduceOperation
Definition: Types.hpp:122
LayerTestResult< float, 4 > ReduceMinNegativeAxisTest(armnn::IWorkloadFactory &workloadFactory, const armnn::IBackendInternal::IMemoryManagerSharedPtr &memoryManager, const armnn::ITensorHandleFactory &tensorHandleFactory)
std::shared_ptr< IMemoryManager > IMemoryManagerSharedPtr
LayerTestResult< float, 4 > ReduceMinSimpleTest(armnn::IWorkloadFactory &workloadFactory, const armnn::IBackendInternal::IMemoryManagerSharedPtr &memoryManager, const armnn::ITensorHandleFactory &tensorHandleFactory)
void SetQuantizationScale(float scale)
Definition: Tensor.cpp:464
void CopyDataFromITensorHandle(void *memory, const armnn::ITensorHandle *tensorHandle)
LayerTestResult< float, 4 > ReduceMaxNegativeAxisTest(armnn::IWorkloadFactory &workloadFactory, const armnn::IBackendInternal::IMemoryManagerSharedPtr &memoryManager, const armnn::ITensorHandleFactory &tensorHandleFactory)
std::vector< uint32_t > m_vAxis
The indices of the dimensions to reduce.
LayerTestResult< float, 4 > ReduceMaxSimpleTest2(armnn::IWorkloadFactory &workloadFactory, const armnn::IBackendInternal::IMemoryManagerSharedPtr &memoryManager, const armnn::ITensorHandleFactory &tensorHandleFactory)
Contains information about inputs and outputs to a layer.
void SetQuantizationOffset(int32_t offset)
Definition: Tensor.cpp:480
void CopyDataToITensorHandle(armnn::ITensorHandle *tensorHandle, const void *memory)