IVGCVSW-2467 Remove GetDataType<T> function
[platform/upstream/armnn.git] / src / backends / backendsCommon / test / StridedSliceTestImpl.hpp
1 //
2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 #pragma once
6
7 #include "TypeUtils.hpp"
8 #include "WorkloadTestUtils.hpp"
9
10 #include <armnn/ArmNN.hpp>
11 #include <armnn/Tensor.hpp>
12 #include <armnn/TypesUtils.hpp>
13
14 #include <backendsCommon/CpuTensorHandle.hpp>
15 #include <backendsCommon/IBackendInternal.hpp>
16 #include <backendsCommon/WorkloadFactory.hpp>
17
18 #include <test/TensorHelpers.hpp>
19
20 namespace
21 {
22
23 template<typename T, std::size_t InDim, std::size_t OutDim>
24 LayerTestResult<T, OutDim> StridedSliceTestImpl(
25     armnn::IWorkloadFactory& workloadFactory,
26     const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager,
27     armnn::TensorInfo& inputTensorInfo,
28     armnn::TensorInfo& outputTensorInfo,
29     std::vector<float>& inputData,
30     std::vector<float>& outputExpectedData,
31     armnn::StridedSliceQueueDescriptor descriptor,
32     const float qScale = 1.0f,
33     const int32_t qOffset = 0)
34 {
35     if(armnn::IsQuantizedType<T>())
36     {
37         inputTensorInfo.SetQuantizationScale(qScale);
38         inputTensorInfo.SetQuantizationOffset(qOffset);
39
40         outputTensorInfo.SetQuantizationScale(qScale);
41         outputTensorInfo.SetQuantizationOffset(qOffset);
42     }
43
44     boost::multi_array<T, InDim> input =
45         MakeTensor<T, InDim>(inputTensorInfo, QuantizedVector<T>(qScale, qOffset, inputData));
46
47     LayerTestResult<T, OutDim> ret(outputTensorInfo);
48     ret.outputExpected =
49         MakeTensor<T, OutDim>(outputTensorInfo, QuantizedVector<T>(qScale, qOffset, outputExpectedData));
50
51     std::unique_ptr<armnn::ITensorHandle> inputHandle =
52         workloadFactory.CreateTensorHandle(inputTensorInfo);
53
54     std::unique_ptr<armnn::ITensorHandle> outputHandle =
55         workloadFactory.CreateTensorHandle(outputTensorInfo);
56
57     armnn::WorkloadInfo info;
58     AddInputToWorkload(descriptor, info, inputTensorInfo, inputHandle.get());
59     AddOutputToWorkload(descriptor, info, outputTensorInfo, outputHandle.get());
60
61     std::unique_ptr<armnn::IWorkload> workload = workloadFactory.CreateStridedSlice(descriptor, info);
62
63     inputHandle->Allocate();
64     outputHandle->Allocate();
65
66     CopyDataToITensorHandle(inputHandle.get(), input.data());
67
68     ExecuteWorkload(*workload, memoryManager);
69
70     CopyDataFromITensorHandle(ret.output.data(), outputHandle.get());
71
72     return ret;
73 }
74
75 template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>>
76 LayerTestResult<T, 4> StridedSlice4DTest(
77     armnn::IWorkloadFactory& workloadFactory,
78     const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager)
79 {
80     armnn::TensorInfo inputTensorInfo;
81     armnn::TensorInfo outputTensorInfo;
82
83     unsigned int inputShape[]  = {3, 2, 3, 1};
84     unsigned int outputShape[] = {1, 2, 3, 1};
85
86     armnn::StridedSliceQueueDescriptor desc;
87     desc.m_Parameters.m_Begin  = {1, 0, 0, 0};
88     desc.m_Parameters.m_End    = {2, 2, 3, 1};
89     desc.m_Parameters.m_Stride = {1, 1, 1, 1};
90
91     inputTensorInfo = armnn::TensorInfo(4, inputShape, ArmnnType);
92     outputTensorInfo = armnn::TensorInfo(4, outputShape, ArmnnType);
93
94     std::vector<float> input = std::vector<float>(
95     {
96         1.0f, 1.0f, 1.0f, 2.0f, 2.0f, 2.0f,
97
98         3.0f, 3.0f, 3.0f, 4.0f, 4.0f, 4.0f,
99
100         5.0f, 5.0f, 5.0f, 6.0f, 6.0f, 6.0f
101     });
102
103     std::vector<float> outputExpected = std::vector<float>(
104     {
105         3.0f, 3.0f, 3.0f, 4.0f, 4.0f, 4.0f
106     });
107
108     return StridedSliceTestImpl<T, 4, 4>(
109         workloadFactory, memoryManager, inputTensorInfo, outputTensorInfo, input, outputExpected, desc);
110 }
111
112 template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>>
113 LayerTestResult<T, 4> StridedSlice4DReverseTest(
114     armnn::IWorkloadFactory& workloadFactory,
115     const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager)
116 {
117     armnn::TensorInfo inputTensorInfo;
118     armnn::TensorInfo outputTensorInfo;
119
120     unsigned int inputShape[]  = {3, 2, 3, 1};
121     unsigned int outputShape[] = {1, 2, 3, 1};
122
123     armnn::StridedSliceQueueDescriptor desc;
124     desc.m_Parameters.m_Begin  = {1, -1, 0, 0};
125     desc.m_Parameters.m_End    = {2, -3, 3, 1};
126     desc.m_Parameters.m_Stride = {1, -1, 1, 1};
127
128     inputTensorInfo = armnn::TensorInfo(4, inputShape, ArmnnType);
129     outputTensorInfo = armnn::TensorInfo(4, outputShape, ArmnnType);
130
131     std::vector<float> input = std::vector<float>(
132     {
133         1.0f, 1.0f, 1.0f, 2.0f, 2.0f, 2.0f,
134
135         3.0f, 3.0f, 3.0f, 4.0f, 4.0f, 4.0f,
136
137         5.0f, 5.0f, 5.0f, 6.0f, 6.0f, 6.0f
138     });
139
140     std::vector<float> outputExpected = std::vector<float>(
141     {
142         4.0f, 4.0f, 4.0f, 3.0f, 3.0f, 3.0f
143     });
144
145     return StridedSliceTestImpl<T, 4, 4>(
146         workloadFactory, memoryManager, inputTensorInfo, outputTensorInfo, input, outputExpected, desc);
147 }
148
149 template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>>
150 LayerTestResult<T, 4> StridedSliceSimpleStrideTest(
151     armnn::IWorkloadFactory& workloadFactory,
152     const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager)
153 {
154     armnn::TensorInfo inputTensorInfo;
155     armnn::TensorInfo outputTensorInfo;
156
157     unsigned int inputShape[]  = {3, 2, 3, 1};
158     unsigned int outputShape[] = {2, 1, 2, 1};
159
160     armnn::StridedSliceQueueDescriptor desc;
161     desc.m_Parameters.m_Begin  = {0, 0, 0, 0};
162     desc.m_Parameters.m_End    = {3, 2, 3, 1};
163     desc.m_Parameters.m_Stride = {2, 2, 2, 1};
164
165     inputTensorInfo = armnn::TensorInfo(4, inputShape, ArmnnType);
166     outputTensorInfo = armnn::TensorInfo(4, outputShape, ArmnnType);
167
168     std::vector<float> input = std::vector<float>(
169     {
170         1.0f, 1.0f, 1.0f, 2.0f, 2.0f, 2.0f,
171
172         3.0f, 3.0f, 3.0f, 4.0f, 4.0f, 4.0f,
173
174         5.0f, 5.0f, 5.0f, 6.0f, 6.0f, 6.0f
175     });
176
177     std::vector<float> outputExpected = std::vector<float>(
178     {
179         1.0f, 1.0f,
180
181         5.0f, 5.0f
182     });
183
184     return StridedSliceTestImpl<T, 4, 4>(
185         workloadFactory, memoryManager, inputTensorInfo, outputTensorInfo, input, outputExpected, desc);
186 }
187
188 template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>>
189 LayerTestResult<T, 4> StridedSliceSimpleRangeMaskTest(
190     armnn::IWorkloadFactory& workloadFactory,
191     const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager)
192 {
193     armnn::TensorInfo inputTensorInfo;
194     armnn::TensorInfo outputTensorInfo;
195
196     unsigned int inputShape[]  = {3, 2, 3, 1};
197     unsigned int outputShape[] = {3, 2, 3, 1};
198
199     armnn::StridedSliceQueueDescriptor desc;
200     desc.m_Parameters.m_Begin     = {1, 1, 1, 1};
201     desc.m_Parameters.m_End       = {1, 1, 1, 1};
202     desc.m_Parameters.m_Stride    = {1, 1, 1, 1};
203     desc.m_Parameters.m_BeginMask = (1 << 4) - 1;
204     desc.m_Parameters.m_EndMask   = (1 << 4) - 1;
205
206     inputTensorInfo = armnn::TensorInfo(4, inputShape, ArmnnType);
207     outputTensorInfo = armnn::TensorInfo(4, outputShape, ArmnnType);
208
209     std::vector<float> input = std::vector<float>(
210     {
211         1.0f, 1.0f, 1.0f, 2.0f, 2.0f, 2.0f,
212
213         3.0f, 3.0f, 3.0f, 4.0f, 4.0f, 4.0f,
214
215         5.0f, 5.0f, 5.0f, 6.0f, 6.0f, 6.0f
216     });
217
218     std::vector<float> outputExpected = std::vector<float>(
219     {
220         1.0f, 1.0f, 1.0f, 2.0f, 2.0f, 2.0f,
221
222         3.0f, 3.0f, 3.0f, 4.0f, 4.0f, 4.0f,
223
224         5.0f, 5.0f, 5.0f, 6.0f, 6.0f, 6.0f
225     });
226
227     return StridedSliceTestImpl<T, 4, 4>(
228         workloadFactory, memoryManager, inputTensorInfo, outputTensorInfo, input, outputExpected, desc);
229 }
230
231 template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>>
232 LayerTestResult<T, 2> StridedSliceShrinkAxisMaskTest(
233     armnn::IWorkloadFactory& workloadFactory,
234     const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager)
235 {
236     armnn::TensorInfo inputTensorInfo;
237     armnn::TensorInfo outputTensorInfo;
238
239     unsigned int inputShape[]  = {3, 2, 3, 1};
240     unsigned int outputShape[] = {3, 1};
241
242     armnn::StridedSliceQueueDescriptor desc;
243     desc.m_Parameters.m_Begin          = {0, 0, 1, 0};
244     desc.m_Parameters.m_End            = {1, 1, 1, 1};
245     desc.m_Parameters.m_Stride         = {1, 1, 1, 1};
246     desc.m_Parameters.m_EndMask        = (1 << 4) - 1;
247     desc.m_Parameters.m_ShrinkAxisMask = (1 << 1) | (1 << 2);
248
249     inputTensorInfo = armnn::TensorInfo(4, inputShape, ArmnnType);
250     outputTensorInfo = armnn::TensorInfo(2, outputShape, ArmnnType);
251
252     std::vector<float> input = std::vector<float>(
253     {
254         1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f,
255
256         7.0f, 8.0f, 9.0f, 10.0f, 11.0f, 12.0f,
257
258         13.0f, 14.0f, 15.0f, 16.0f, 17.0f, 18.0f
259     });
260
261     std::vector<float> outputExpected = std::vector<float>(
262     {
263         2.0f, 8.0f, 14.0f
264     });
265
266     return StridedSliceTestImpl<T, 4, 2>(
267         workloadFactory, memoryManager, inputTensorInfo, outputTensorInfo, input, outputExpected, desc);
268 }
269
270 template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>>
271 LayerTestResult<T, 3> StridedSlice3DTest(
272     armnn::IWorkloadFactory& workloadFactory,
273     const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager)
274 {
275     armnn::TensorInfo inputTensorInfo;
276     armnn::TensorInfo outputTensorInfo;
277
278     unsigned int inputShape[]  = {3, 3, 3};
279     unsigned int outputShape[] = {2, 2, 2};
280
281     armnn::StridedSliceQueueDescriptor desc;
282     desc.m_Parameters.m_Begin   = {0, 0, 0};
283     desc.m_Parameters.m_End     = {1, 1, 1};
284     desc.m_Parameters.m_Stride  = {2, 2, 2};
285     desc.m_Parameters.m_EndMask = (1 << 3) - 1;
286
287     inputTensorInfo = armnn::TensorInfo(3, inputShape, ArmnnType);
288     outputTensorInfo = armnn::TensorInfo(3, outputShape, ArmnnType);
289
290     std::vector<float> input = std::vector<float>(
291     {
292         1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f, 9.0f,
293
294         10.0f, 11.0f, 12.0f, 13.0f, 14.0f, 15.0f, 16.0f, 17.0f, 18.0f,
295
296         19.0f, 20.0f, 21.0f, 22.0f, 23.0f, 24.0f, 25.0f, 26.0f, 27.0f
297     });
298
299     std::vector<float> outputExpected = std::vector<float>(
300     {
301         1.0f, 3.0f, 7.0f, 9.0f,
302
303         19.0f, 21.0f, 25.0f, 27.0f
304     });
305
306     return StridedSliceTestImpl<T, 3, 3>(
307         workloadFactory, memoryManager, inputTensorInfo, outputTensorInfo, input, outputExpected, desc);
308 }
309
310 template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>>
311 LayerTestResult<T, 3> StridedSlice3DReverseTest(
312     armnn::IWorkloadFactory& workloadFactory,
313     const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager)
314 {
315     armnn::TensorInfo inputTensorInfo;
316     armnn::TensorInfo outputTensorInfo;
317
318     unsigned int inputShape[]  = {3, 3, 3};
319     unsigned int outputShape[] = {2, 2, 2};
320
321     armnn::StridedSliceQueueDescriptor desc;
322     desc.m_Parameters.m_Begin  = {-1, -1, -1};
323     desc.m_Parameters.m_End    = {-4, -4, -4};
324     desc.m_Parameters.m_Stride = {-2, -2, -2};
325
326     inputTensorInfo = armnn::TensorInfo(3, inputShape, ArmnnType);
327     outputTensorInfo = armnn::TensorInfo(3, outputShape, ArmnnType);
328
329     std::vector<float> input = std::vector<float>(
330     {
331         1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f, 9.0f,
332
333         10.0f, 11.0f, 12.0f, 13.0f, 14.0f, 15.0f, 16.0f, 17.0f, 18.0f,
334
335         19.0f, 20.0f, 21.0f, 22.0f, 23.0f, 24.0f, 25.0f, 26.0f, 27.0f
336     });
337
338     std::vector<float> outputExpected = std::vector<float>(
339     {
340         27.0f, 25.0f, 21.0f, 19.0f,
341
342         9.0f, 7.0f, 3.0f, 1.0f
343     });
344
345     return StridedSliceTestImpl<T, 3, 3>(
346         workloadFactory, memoryManager, inputTensorInfo, outputTensorInfo, input, outputExpected, desc);
347 }
348
349 template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>>
350 LayerTestResult<T, 2> StridedSlice2DTest(
351     armnn::IWorkloadFactory& workloadFactory,
352     const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager)
353 {
354     armnn::TensorInfo inputTensorInfo;
355     armnn::TensorInfo outputTensorInfo;
356
357     unsigned int inputShape[]  = {3, 3};
358     unsigned int outputShape[] = {2, 2};
359
360     armnn::StridedSliceQueueDescriptor desc;
361     desc.m_Parameters.m_Begin   = {0, 0};
362     desc.m_Parameters.m_End     = {1, 1};
363     desc.m_Parameters.m_Stride  = {2, 2};
364     desc.m_Parameters.m_EndMask = (1 << 2) - 1;
365
366     inputTensorInfo = armnn::TensorInfo(2, inputShape, ArmnnType);
367     outputTensorInfo = armnn::TensorInfo(2, outputShape, ArmnnType);
368
369     std::vector<float> input = std::vector<float>(
370     {
371         1.0f, 2.0f, 3.0f,
372
373         4.0f, 5.0f, 6.0f,
374
375         7.0f, 8.0f, 9.0f
376     });
377
378     std::vector<float> outputExpected = std::vector<float>(
379     {
380         1.0f, 3.0f,
381
382         7.0f, 9.0f
383     });
384
385     return StridedSliceTestImpl<T, 2, 2>(
386         workloadFactory, memoryManager, inputTensorInfo, outputTensorInfo, input, outputExpected, desc);
387 }
388
389 template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>>
390 LayerTestResult<T, 2> StridedSlice2DReverseTest(
391     armnn::IWorkloadFactory& workloadFactory,
392     const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager)
393 {
394     armnn::TensorInfo inputTensorInfo;
395     armnn::TensorInfo outputTensorInfo;
396
397     unsigned int inputShape[]  = {3, 3};
398     unsigned int outputShape[] = {2, 2};
399
400     armnn::StridedSliceQueueDescriptor desc;
401     desc.m_Parameters.m_Begin     = {0, 0};
402     desc.m_Parameters.m_End       = {1, 1};
403     desc.m_Parameters.m_Stride    = {-2, -2};
404     desc.m_Parameters.m_BeginMask = (1 << 2) - 1;
405     desc.m_Parameters.m_EndMask   = (1 << 2) - 1;
406
407     inputTensorInfo = armnn::TensorInfo(2, inputShape, ArmnnType);
408     outputTensorInfo = armnn::TensorInfo(2, outputShape, ArmnnType);
409
410     std::vector<float> input = std::vector<float>(
411     {
412         1.0f, 2.0f, 3.0f,
413
414         4.0f, 5.0f, 6.0f,
415
416         7.0f, 8.0f, 9.0f
417     });
418
419     std::vector<float> outputExpected = std::vector<float>(
420     {
421         9.0f, 7.0f,
422
423         3.0f, 1.0f
424     });
425
426     return StridedSliceTestImpl<T, 2, 2>(
427         workloadFactory, memoryManager, inputTensorInfo, outputTensorInfo, input, outputExpected, desc);
428 }
429
430 } // anonymous namespace