blob: c014078d1217f1b5a28c9d4ab70c71647ecfdd76 [file] [log] [blame]
Mike Kellyc9ea45a2020-02-28 18:11:58 +00001//
2// Copyright © 2020 Arm Ltd. All rights reserved.
3// SPDX-License-Identifier: MIT
4//
5
6#pragma once
7
8#include <ResolveType.hpp>
9
10
11#include <armnn/backends/IBackendInternal.hpp>
12#include <backendsCommon/WorkloadFactory.hpp>
13
Teresa Charlin9e132f52020-08-15 12:23:29 +010014#include <backendsCommon/test/WorkloadFactoryHelper.hpp>
Mike Kellyc9ea45a2020-02-28 18:11:58 +000015#include <backendsCommon/test/WorkloadTestUtils.hpp>
16
17#include <test/TensorHelpers.hpp>
18
Teresa Charlin9e132f52020-08-15 12:23:29 +010019template<typename FactoryType, typename T>
Mike Kellyc9ea45a2020-02-28 18:11:58 +000020LayerTestResult<T, 4> SimpleTransposeTestImpl(
21 armnn::IWorkloadFactory& workloadFactory,
22 const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager,
23 armnn::TransposeDescriptor descriptor,
24 armnn::TensorInfo inputTensorInfo,
25 armnn::TensorInfo outputTensorInfo,
26 const std::vector<T>& inputData,
27 const std::vector<T>& outputExpectedData)
28{
Mike Kellyc9ea45a2020-02-28 18:11:58 +000029 auto input = MakeTensor<T, 4>(inputTensorInfo, inputData);
30
31 LayerTestResult<T, 4> ret(outputTensorInfo);
32 ret.outputExpected = MakeTensor<T, 4>(outputTensorInfo, outputExpectedData);
33
Teresa Charlin9e132f52020-08-15 12:23:29 +010034 auto tensorHandleFactory = WorkloadFactoryHelper<FactoryType>::GetTensorHandleFactory(memoryManager);
35 std::unique_ptr<armnn::ITensorHandle> inputHandle = tensorHandleFactory.CreateTensorHandle(inputTensorInfo);
36 std::unique_ptr<armnn::ITensorHandle> outputHandle = tensorHandleFactory.CreateTensorHandle(outputTensorInfo);
Mike Kellyc9ea45a2020-02-28 18:11:58 +000037
38 armnn::TransposeQueueDescriptor data;
39 data.m_Parameters = descriptor;
40 armnn::WorkloadInfo info;
41 AddInputToWorkload(data, info, inputTensorInfo, inputHandle.get());
42 AddOutputToWorkload(data, info, outputTensorInfo, outputHandle.get());
43
44 std::unique_ptr<armnn::IWorkload> workload = workloadFactory.CreateTranspose(data, info);
45
46 inputHandle->Allocate();
47 outputHandle->Allocate();
48
49 CopyDataToITensorHandle(inputHandle.get(), &input[0][0][0][0]);
50
51 workload->Execute();
52
53 CopyDataFromITensorHandle(&ret.output[0][0][0][0], outputHandle.get());
54
55 return ret;
56}
57
Teresa Charlin9e132f52020-08-15 12:23:29 +010058template<typename FactoryType, armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>>
Mike Kellyc9ea45a2020-02-28 18:11:58 +000059LayerTestResult<T, 4> SimpleTransposeTest(
60 armnn::IWorkloadFactory& workloadFactory,
61 const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager)
62{
63 armnn::TensorInfo inputTensorInfo;
64 armnn::TensorInfo outputTensorInfo;
65
66 unsigned int inputShape[] = { 1, 2, 2, 2 };
67 unsigned int outputShape[] = { 1, 2, 2, 2 };
68
69 armnn::TransposeDescriptor descriptor;
70 descriptor.m_DimMappings = {0U, 2U, 3U, 1U};
71
72 inputTensorInfo = armnn::TensorInfo(4, inputShape, ArmnnType);
73 outputTensorInfo = armnn::TensorInfo(4, outputShape, ArmnnType);
74
75 // Set quantization parameters if the requested type is a quantized type.
Narumol Prangnawarat44179c32020-03-11 14:51:27 +000076 float qScale = 0.5f;
77 int32_t qOffset = 5;
Mike Kellyc9ea45a2020-02-28 18:11:58 +000078 if(armnn::IsQuantizedType<T>())
79 {
Narumol Prangnawarat44179c32020-03-11 14:51:27 +000080 inputTensorInfo.SetQuantizationScale(qScale);
81 inputTensorInfo.SetQuantizationOffset(qOffset);
82 outputTensorInfo.SetQuantizationScale(qScale);
83 outputTensorInfo.SetQuantizationOffset(qOffset);
Mike Kellyc9ea45a2020-02-28 18:11:58 +000084 }
85
Narumol Prangnawarat44179c32020-03-11 14:51:27 +000086 std::vector<T> input = armnnUtils::QuantizedVector<T>(
Mike Kellyc9ea45a2020-02-28 18:11:58 +000087 {
88 1, 2,
89 3, 4,
90 5, 6,
91 7, 8
Narumol Prangnawarat44179c32020-03-11 14:51:27 +000092 },
93 qScale, qOffset);
Mike Kellyc9ea45a2020-02-28 18:11:58 +000094
Narumol Prangnawarat44179c32020-03-11 14:51:27 +000095 std::vector<T> outputExpected = armnnUtils::QuantizedVector<T>(
Mike Kellyc9ea45a2020-02-28 18:11:58 +000096 {
97 1, 5, 2, 6,
98 3, 7, 4, 8
Narumol Prangnawarat44179c32020-03-11 14:51:27 +000099 },
100 qScale, qOffset);
Mike Kellyc9ea45a2020-02-28 18:11:58 +0000101
Teresa Charlin9e132f52020-08-15 12:23:29 +0100102 return SimpleTransposeTestImpl<FactoryType, T>(workloadFactory, memoryManager,
103 descriptor, inputTensorInfo,
104 outputTensorInfo, input, outputExpected);
Mike Kellyc9ea45a2020-02-28 18:11:58 +0000105}
106
Teresa Charlin9e132f52020-08-15 12:23:29 +0100107template<typename FactoryType, armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>>
Mike Kellyc9ea45a2020-02-28 18:11:58 +0000108LayerTestResult<T, 4> TransposeValueSet1Test(
109 armnn::IWorkloadFactory& workloadFactory,
110 const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager)
111{
112 armnn::TensorInfo inputTensorInfo;
113 armnn::TensorInfo outputTensorInfo;
114
115 unsigned int inputShape[] = { 1, 2, 2, 3 };
116 unsigned int outputShape[] = { 1, 3, 2, 2 };
117
118 armnn::TransposeDescriptor descriptor;
119 descriptor.m_DimMappings = {0U, 3U, 1U, 2U};
120
121 inputTensorInfo = armnn::TensorInfo(4, inputShape, ArmnnType);
122 outputTensorInfo = armnn::TensorInfo(4, outputShape, ArmnnType);
123
124 // Set quantization parameters if the requested type is a quantized type.
Narumol Prangnawarat44179c32020-03-11 14:51:27 +0000125 float qScale = 0.5f;
126 int32_t qOffset = 5;
Mike Kellyc9ea45a2020-02-28 18:11:58 +0000127 if(armnn::IsQuantizedType<T>())
128 {
Narumol Prangnawarat44179c32020-03-11 14:51:27 +0000129 inputTensorInfo.SetQuantizationScale(qScale);
130 inputTensorInfo.SetQuantizationOffset(qOffset);
131 outputTensorInfo.SetQuantizationScale(qScale);
132 outputTensorInfo.SetQuantizationOffset(qOffset);
Mike Kellyc9ea45a2020-02-28 18:11:58 +0000133 }
134
Narumol Prangnawarat44179c32020-03-11 14:51:27 +0000135 std::vector<T> input = armnnUtils::QuantizedVector<T>(
Mike Kellyc9ea45a2020-02-28 18:11:58 +0000136 {
137 1, 2, 3,
138 11, 12, 13,
139 21, 22, 23,
140 31, 32, 33
Narumol Prangnawarat44179c32020-03-11 14:51:27 +0000141 },
142 qScale, qOffset);
Mike Kellyc9ea45a2020-02-28 18:11:58 +0000143
Narumol Prangnawarat44179c32020-03-11 14:51:27 +0000144 std::vector<T> outputExpected = armnnUtils::QuantizedVector<T>(
Mike Kellyc9ea45a2020-02-28 18:11:58 +0000145 {
146 1, 11, 21, 31,
147 2, 12, 22, 32,
148 3, 13, 23, 33
Narumol Prangnawarat44179c32020-03-11 14:51:27 +0000149 },
150 qScale, qOffset);
Mike Kellyc9ea45a2020-02-28 18:11:58 +0000151
Teresa Charlin9e132f52020-08-15 12:23:29 +0100152 return SimpleTransposeTestImpl<FactoryType, T>(workloadFactory, memoryManager,
153 descriptor, inputTensorInfo,
154 outputTensorInfo, input, outputExpected);
Mike Kellyc9ea45a2020-02-28 18:11:58 +0000155}
156
Teresa Charlin9e132f52020-08-15 12:23:29 +0100157template<typename FactoryType, armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>>
Mike Kellyc9ea45a2020-02-28 18:11:58 +0000158LayerTestResult<T, 4> TransposeValueSet2Test(
159 armnn::IWorkloadFactory& workloadFactory,
160 const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager)
161{
162 armnn::TensorInfo inputTensorInfo;
163 armnn::TensorInfo outputTensorInfo;
164
165 unsigned int inputShape[] = { 1, 3, 2, 2 };
166 unsigned int outputShape[] = { 1, 2, 2, 3 };
167
168 armnn::TransposeDescriptor descriptor;
169 descriptor.m_DimMappings = {0U, 2U, 3U, 1U};
170
171 inputTensorInfo = armnn::TensorInfo(4, inputShape, ArmnnType);
172 outputTensorInfo = armnn::TensorInfo(4, outputShape, ArmnnType);
173
174 // Set quantization parameters if the requested type is a quantized type.
Narumol Prangnawarat44179c32020-03-11 14:51:27 +0000175 float qScale = 0.5f;
176 int32_t qOffset = 5;
Mike Kellyc9ea45a2020-02-28 18:11:58 +0000177 if(armnn::IsQuantizedType<T>())
178 {
Narumol Prangnawarat44179c32020-03-11 14:51:27 +0000179 inputTensorInfo.SetQuantizationScale(qScale);
180 inputTensorInfo.SetQuantizationOffset(qOffset);
181 outputTensorInfo.SetQuantizationScale(qScale);
182 outputTensorInfo.SetQuantizationOffset(qOffset);
Mike Kellyc9ea45a2020-02-28 18:11:58 +0000183 }
184
Narumol Prangnawarat44179c32020-03-11 14:51:27 +0000185 std::vector<T> input = armnnUtils::QuantizedVector<T>(
Mike Kellyc9ea45a2020-02-28 18:11:58 +0000186 {
187 1, 11, 21, 31,
188 2, 12, 22, 32,
189 3, 13, 23, 33
Narumol Prangnawarat44179c32020-03-11 14:51:27 +0000190 },
191 qScale, qOffset);
Mike Kellyc9ea45a2020-02-28 18:11:58 +0000192
Narumol Prangnawarat44179c32020-03-11 14:51:27 +0000193 std::vector<T> outputExpected = armnnUtils::QuantizedVector<T>(
Mike Kellyc9ea45a2020-02-28 18:11:58 +0000194 {
195 1, 2, 3,
196 11, 12, 13,
197 21, 22, 23,
198 31, 32, 33,
Narumol Prangnawarat44179c32020-03-11 14:51:27 +0000199 },
200 qScale, qOffset);
Mike Kellyc9ea45a2020-02-28 18:11:58 +0000201
Teresa Charlin9e132f52020-08-15 12:23:29 +0100202 return SimpleTransposeTestImpl<FactoryType, T>(workloadFactory, memoryManager,
203 descriptor, inputTensorInfo,
204 outputTensorInfo, input, outputExpected);
Mike Kellyc9ea45a2020-02-28 18:11:58 +0000205}
206
Teresa Charlin9e132f52020-08-15 12:23:29 +0100207template<typename FactoryType, armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>>
Mike Kellyc9ea45a2020-02-28 18:11:58 +0000208LayerTestResult<T, 4> TransposeValueSet3Test(
209 armnn::IWorkloadFactory& workloadFactory,
210 const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager)
211{
212 armnn::TensorInfo inputTensorInfo;
213 armnn::TensorInfo outputTensorInfo;
214
215 unsigned int inputShape[] = { 1, 2, 3, 3 };
216 unsigned int outputShape[] = { 1, 3, 2, 3 };
217
218 armnn::TransposeDescriptor descriptor;
219 descriptor.m_DimMappings = {0U, 3U, 1U, 2U};
220
221 inputTensorInfo = armnn::TensorInfo(4, inputShape, ArmnnType);
222 outputTensorInfo = armnn::TensorInfo(4, outputShape, ArmnnType);
223
224 // Set quantization parameters if the requested type is a quantized type.
Narumol Prangnawarat44179c32020-03-11 14:51:27 +0000225 float qScale = 0.5f;
226 int32_t qOffset = 5;
Mike Kellyc9ea45a2020-02-28 18:11:58 +0000227 if(armnn::IsQuantizedType<T>())
228 {
Narumol Prangnawarat44179c32020-03-11 14:51:27 +0000229 inputTensorInfo.SetQuantizationScale(qScale);
230 inputTensorInfo.SetQuantizationOffset(qOffset);
231 outputTensorInfo.SetQuantizationScale(qScale);
232 outputTensorInfo.SetQuantizationOffset(qOffset);
Mike Kellyc9ea45a2020-02-28 18:11:58 +0000233 }
234
Narumol Prangnawarat44179c32020-03-11 14:51:27 +0000235 std::vector<T> input = armnnUtils::QuantizedVector<T>(
Mike Kellyc9ea45a2020-02-28 18:11:58 +0000236 {
237 1, 2, 3,
238 11, 12, 13,
239 21, 22, 23,
240 31, 32, 33,
241 41, 42, 43,
242 51, 52, 53
Narumol Prangnawarat44179c32020-03-11 14:51:27 +0000243 },
244 qScale, qOffset);
Mike Kellyc9ea45a2020-02-28 18:11:58 +0000245
Narumol Prangnawarat44179c32020-03-11 14:51:27 +0000246 std::vector<T> outputExpected = armnnUtils::QuantizedVector<T>(
Mike Kellyc9ea45a2020-02-28 18:11:58 +0000247 {
248 1, 11, 21, 31, 41, 51,
249 2, 12, 22, 32, 42, 52,
250 3, 13, 23, 33, 43, 53
Narumol Prangnawarat44179c32020-03-11 14:51:27 +0000251 },
252 qScale, qOffset);
Mike Kellyc9ea45a2020-02-28 18:11:58 +0000253
Teresa Charlin9e132f52020-08-15 12:23:29 +0100254 return SimpleTransposeTestImpl<FactoryType, T>(workloadFactory, memoryManager,
255 descriptor, inputTensorInfo,
256 outputTensorInfo, input, outputExpected);
Mike Kellyc9ea45a2020-02-28 18:11:58 +0000257}