blob: ef271691b6fd608b7fedf600d6b275b6c6d7b8ff [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>
Colm Donelan0c479742021-12-10 12:43:54 +000012#include <armnn/backends/WorkloadFactory.hpp>
Mike Kellyc9ea45a2020-02-28 18:11:58 +000013
Teresa Charlin9e132f52020-08-15 12:23:29 +010014#include <backendsCommon/test/WorkloadFactoryHelper.hpp>
Colm Donelan0c479742021-12-10 12:43:54 +000015#include <armnnTestUtils/WorkloadTestUtils.hpp>
Mike Kellyc9ea45a2020-02-28 18:11:58 +000016
Colm Donelanc42a9872022-02-02 16:35:09 +000017#include <armnnTestUtils/TensorHelpers.hpp>
Mike Kellyc9ea45a2020-02-28 18:11:58 +000018
Finn Williamsec36d3e2020-08-28 13:17:05 +010019template<typename T>
Mike Kellyc9ea45a2020-02-28 18:11:58 +000020LayerTestResult<T, 4> SimpleTransposeTestImpl(
21 armnn::IWorkloadFactory& workloadFactory,
22 const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager,
Finn Williamsec36d3e2020-08-28 13:17:05 +010023 const armnn::ITensorHandleFactory& tensorHandleFactory,
Mike Kellyc9ea45a2020-02-28 18:11:58 +000024 armnn::TransposeDescriptor descriptor,
25 armnn::TensorInfo inputTensorInfo,
26 armnn::TensorInfo outputTensorInfo,
27 const std::vector<T>& inputData,
28 const std::vector<T>& outputExpectedData)
29{
Finn Williamsec36d3e2020-08-28 13:17:05 +010030 IgnoreUnused(memoryManager);
Sadik Armagan483c8112021-06-01 09:24:52 +010031 std::vector<T> actualOutput(outputTensorInfo.GetNumElements());
Mike Kellyc9ea45a2020-02-28 18:11:58 +000032
Teresa Charlin9e132f52020-08-15 12:23:29 +010033 std::unique_ptr<armnn::ITensorHandle> inputHandle = tensorHandleFactory.CreateTensorHandle(inputTensorInfo);
34 std::unique_ptr<armnn::ITensorHandle> outputHandle = tensorHandleFactory.CreateTensorHandle(outputTensorInfo);
Mike Kellyc9ea45a2020-02-28 18:11:58 +000035
36 armnn::TransposeQueueDescriptor data;
37 data.m_Parameters = descriptor;
38 armnn::WorkloadInfo info;
39 AddInputToWorkload(data, info, inputTensorInfo, inputHandle.get());
40 AddOutputToWorkload(data, info, outputTensorInfo, outputHandle.get());
41
Teresa Charlin611c7fb2022-01-07 09:47:29 +000042 std::unique_ptr<armnn::IWorkload> workload = workloadFactory.CreateWorkload(armnn::LayerType::Transpose,
43 data,
44 info);
Mike Kellyc9ea45a2020-02-28 18:11:58 +000045
46 inputHandle->Allocate();
47 outputHandle->Allocate();
48
Sadik Armagan483c8112021-06-01 09:24:52 +010049 CopyDataToITensorHandle(inputHandle.get(), inputData.data());
Mike Kellyc9ea45a2020-02-28 18:11:58 +000050
51 workload->Execute();
52
Sadik Armagan483c8112021-06-01 09:24:52 +010053 CopyDataFromITensorHandle(actualOutput.data(), outputHandle.get());
Mike Kellyc9ea45a2020-02-28 18:11:58 +000054
Sadik Armagan483c8112021-06-01 09:24:52 +010055 return LayerTestResult<T, 4>(actualOutput,
56 outputExpectedData,
57 outputHandle->GetShape(),
58 outputTensorInfo.GetShape());
Mike Kellyc9ea45a2020-02-28 18:11:58 +000059}
60
Finn Williamsec36d3e2020-08-28 13:17:05 +010061template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>>
Mike Kellyc9ea45a2020-02-28 18:11:58 +000062LayerTestResult<T, 4> SimpleTransposeTest(
63 armnn::IWorkloadFactory& workloadFactory,
Finn Williamsec36d3e2020-08-28 13:17:05 +010064 const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager,
65 const armnn::ITensorHandleFactory& tensorHandleFactory)
Mike Kellyc9ea45a2020-02-28 18:11:58 +000066{
67 armnn::TensorInfo inputTensorInfo;
68 armnn::TensorInfo outputTensorInfo;
69
70 unsigned int inputShape[] = { 1, 2, 2, 2 };
71 unsigned int outputShape[] = { 1, 2, 2, 2 };
72
73 armnn::TransposeDescriptor descriptor;
74 descriptor.m_DimMappings = {0U, 2U, 3U, 1U};
75
76 inputTensorInfo = armnn::TensorInfo(4, inputShape, ArmnnType);
77 outputTensorInfo = armnn::TensorInfo(4, outputShape, ArmnnType);
78
79 // Set quantization parameters if the requested type is a quantized type.
Narumol Prangnawarat44179c32020-03-11 14:51:27 +000080 float qScale = 0.5f;
81 int32_t qOffset = 5;
Mike Kellyc9ea45a2020-02-28 18:11:58 +000082 if(armnn::IsQuantizedType<T>())
83 {
Narumol Prangnawarat44179c32020-03-11 14:51:27 +000084 inputTensorInfo.SetQuantizationScale(qScale);
85 inputTensorInfo.SetQuantizationOffset(qOffset);
86 outputTensorInfo.SetQuantizationScale(qScale);
87 outputTensorInfo.SetQuantizationOffset(qOffset);
Mike Kellyc9ea45a2020-02-28 18:11:58 +000088 }
89
Narumol Prangnawarat44179c32020-03-11 14:51:27 +000090 std::vector<T> input = armnnUtils::QuantizedVector<T>(
Mike Kellyc9ea45a2020-02-28 18:11:58 +000091 {
92 1, 2,
93 3, 4,
94 5, 6,
95 7, 8
Narumol Prangnawarat44179c32020-03-11 14:51:27 +000096 },
97 qScale, qOffset);
Mike Kellyc9ea45a2020-02-28 18:11:58 +000098
Narumol Prangnawarat44179c32020-03-11 14:51:27 +000099 std::vector<T> outputExpected = armnnUtils::QuantizedVector<T>(
Mike Kellyc9ea45a2020-02-28 18:11:58 +0000100 {
101 1, 5, 2, 6,
102 3, 7, 4, 8
Narumol Prangnawarat44179c32020-03-11 14:51:27 +0000103 },
104 qScale, qOffset);
Mike Kellyc9ea45a2020-02-28 18:11:58 +0000105
Finn Williamsec36d3e2020-08-28 13:17:05 +0100106 return SimpleTransposeTestImpl(workloadFactory, memoryManager, tensorHandleFactory,
107 descriptor, inputTensorInfo,
108 outputTensorInfo, input, outputExpected);
Mike Kellyc9ea45a2020-02-28 18:11:58 +0000109}
110
Finn Williamsec36d3e2020-08-28 13:17:05 +0100111template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>>
Mike Kellyc9ea45a2020-02-28 18:11:58 +0000112LayerTestResult<T, 4> TransposeValueSet1Test(
113 armnn::IWorkloadFactory& workloadFactory,
Finn Williamsec36d3e2020-08-28 13:17:05 +0100114 const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager,
115 const armnn::ITensorHandleFactory& tensorHandleFactory)
Mike Kellyc9ea45a2020-02-28 18:11:58 +0000116{
117 armnn::TensorInfo inputTensorInfo;
118 armnn::TensorInfo outputTensorInfo;
119
120 unsigned int inputShape[] = { 1, 2, 2, 3 };
121 unsigned int outputShape[] = { 1, 3, 2, 2 };
122
123 armnn::TransposeDescriptor descriptor;
124 descriptor.m_DimMappings = {0U, 3U, 1U, 2U};
125
126 inputTensorInfo = armnn::TensorInfo(4, inputShape, ArmnnType);
127 outputTensorInfo = armnn::TensorInfo(4, outputShape, ArmnnType);
128
129 // Set quantization parameters if the requested type is a quantized type.
Narumol Prangnawarat44179c32020-03-11 14:51:27 +0000130 float qScale = 0.5f;
131 int32_t qOffset = 5;
Mike Kellyc9ea45a2020-02-28 18:11:58 +0000132 if(armnn::IsQuantizedType<T>())
133 {
Narumol Prangnawarat44179c32020-03-11 14:51:27 +0000134 inputTensorInfo.SetQuantizationScale(qScale);
135 inputTensorInfo.SetQuantizationOffset(qOffset);
136 outputTensorInfo.SetQuantizationScale(qScale);
137 outputTensorInfo.SetQuantizationOffset(qOffset);
Mike Kellyc9ea45a2020-02-28 18:11:58 +0000138 }
139
Narumol Prangnawarat44179c32020-03-11 14:51:27 +0000140 std::vector<T> input = armnnUtils::QuantizedVector<T>(
Mike Kellyc9ea45a2020-02-28 18:11:58 +0000141 {
142 1, 2, 3,
143 11, 12, 13,
144 21, 22, 23,
145 31, 32, 33
Narumol Prangnawarat44179c32020-03-11 14:51:27 +0000146 },
147 qScale, qOffset);
Mike Kellyc9ea45a2020-02-28 18:11:58 +0000148
Narumol Prangnawarat44179c32020-03-11 14:51:27 +0000149 std::vector<T> outputExpected = armnnUtils::QuantizedVector<T>(
Mike Kellyc9ea45a2020-02-28 18:11:58 +0000150 {
151 1, 11, 21, 31,
152 2, 12, 22, 32,
153 3, 13, 23, 33
Narumol Prangnawarat44179c32020-03-11 14:51:27 +0000154 },
155 qScale, qOffset);
Mike Kellyc9ea45a2020-02-28 18:11:58 +0000156
Finn Williamsec36d3e2020-08-28 13:17:05 +0100157 return SimpleTransposeTestImpl<T>(workloadFactory, memoryManager, tensorHandleFactory,
158 descriptor, inputTensorInfo,
159 outputTensorInfo, input, outputExpected);
Mike Kellyc9ea45a2020-02-28 18:11:58 +0000160}
161
Finn Williamsec36d3e2020-08-28 13:17:05 +0100162template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>>
Mike Kellyc9ea45a2020-02-28 18:11:58 +0000163LayerTestResult<T, 4> TransposeValueSet2Test(
164 armnn::IWorkloadFactory& workloadFactory,
Finn Williamsec36d3e2020-08-28 13:17:05 +0100165 const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager,
166 const armnn::ITensorHandleFactory& tensorHandleFactory)
Mike Kellyc9ea45a2020-02-28 18:11:58 +0000167{
168 armnn::TensorInfo inputTensorInfo;
169 armnn::TensorInfo outputTensorInfo;
170
171 unsigned int inputShape[] = { 1, 3, 2, 2 };
172 unsigned int outputShape[] = { 1, 2, 2, 3 };
173
174 armnn::TransposeDescriptor descriptor;
175 descriptor.m_DimMappings = {0U, 2U, 3U, 1U};
176
177 inputTensorInfo = armnn::TensorInfo(4, inputShape, ArmnnType);
178 outputTensorInfo = armnn::TensorInfo(4, outputShape, ArmnnType);
179
180 // Set quantization parameters if the requested type is a quantized type.
Narumol Prangnawarat44179c32020-03-11 14:51:27 +0000181 float qScale = 0.5f;
182 int32_t qOffset = 5;
Mike Kellyc9ea45a2020-02-28 18:11:58 +0000183 if(armnn::IsQuantizedType<T>())
184 {
Narumol Prangnawarat44179c32020-03-11 14:51:27 +0000185 inputTensorInfo.SetQuantizationScale(qScale);
186 inputTensorInfo.SetQuantizationOffset(qOffset);
187 outputTensorInfo.SetQuantizationScale(qScale);
188 outputTensorInfo.SetQuantizationOffset(qOffset);
Mike Kellyc9ea45a2020-02-28 18:11:58 +0000189 }
190
Narumol Prangnawarat44179c32020-03-11 14:51:27 +0000191 std::vector<T> input = armnnUtils::QuantizedVector<T>(
Mike Kellyc9ea45a2020-02-28 18:11:58 +0000192 {
193 1, 11, 21, 31,
194 2, 12, 22, 32,
195 3, 13, 23, 33
Narumol Prangnawarat44179c32020-03-11 14:51:27 +0000196 },
197 qScale, qOffset);
Mike Kellyc9ea45a2020-02-28 18:11:58 +0000198
Narumol Prangnawarat44179c32020-03-11 14:51:27 +0000199 std::vector<T> outputExpected = armnnUtils::QuantizedVector<T>(
Mike Kellyc9ea45a2020-02-28 18:11:58 +0000200 {
201 1, 2, 3,
202 11, 12, 13,
203 21, 22, 23,
204 31, 32, 33,
Narumol Prangnawarat44179c32020-03-11 14:51:27 +0000205 },
206 qScale, qOffset);
Mike Kellyc9ea45a2020-02-28 18:11:58 +0000207
Finn Williamsec36d3e2020-08-28 13:17:05 +0100208 return SimpleTransposeTestImpl<T>(workloadFactory, memoryManager, tensorHandleFactory,
209 descriptor, inputTensorInfo,
210 outputTensorInfo, input, outputExpected);
Mike Kellyc9ea45a2020-02-28 18:11:58 +0000211}
212
Finn Williamsec36d3e2020-08-28 13:17:05 +0100213template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>>
Mike Kellyc9ea45a2020-02-28 18:11:58 +0000214LayerTestResult<T, 4> TransposeValueSet3Test(
215 armnn::IWorkloadFactory& workloadFactory,
Finn Williamsec36d3e2020-08-28 13:17:05 +0100216 const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager,
217 const armnn::ITensorHandleFactory& tensorHandleFactory)
Mike Kellyc9ea45a2020-02-28 18:11:58 +0000218{
219 armnn::TensorInfo inputTensorInfo;
220 armnn::TensorInfo outputTensorInfo;
221
222 unsigned int inputShape[] = { 1, 2, 3, 3 };
223 unsigned int outputShape[] = { 1, 3, 2, 3 };
224
225 armnn::TransposeDescriptor descriptor;
226 descriptor.m_DimMappings = {0U, 3U, 1U, 2U};
227
228 inputTensorInfo = armnn::TensorInfo(4, inputShape, ArmnnType);
229 outputTensorInfo = armnn::TensorInfo(4, outputShape, ArmnnType);
230
231 // Set quantization parameters if the requested type is a quantized type.
Narumol Prangnawarat44179c32020-03-11 14:51:27 +0000232 float qScale = 0.5f;
233 int32_t qOffset = 5;
Mike Kellyc9ea45a2020-02-28 18:11:58 +0000234 if(armnn::IsQuantizedType<T>())
235 {
Narumol Prangnawarat44179c32020-03-11 14:51:27 +0000236 inputTensorInfo.SetQuantizationScale(qScale);
237 inputTensorInfo.SetQuantizationOffset(qOffset);
238 outputTensorInfo.SetQuantizationScale(qScale);
239 outputTensorInfo.SetQuantizationOffset(qOffset);
Mike Kellyc9ea45a2020-02-28 18:11:58 +0000240 }
241
Narumol Prangnawarat44179c32020-03-11 14:51:27 +0000242 std::vector<T> input = armnnUtils::QuantizedVector<T>(
Mike Kellyc9ea45a2020-02-28 18:11:58 +0000243 {
244 1, 2, 3,
245 11, 12, 13,
246 21, 22, 23,
247 31, 32, 33,
248 41, 42, 43,
249 51, 52, 53
Narumol Prangnawarat44179c32020-03-11 14:51:27 +0000250 },
251 qScale, qOffset);
Mike Kellyc9ea45a2020-02-28 18:11:58 +0000252
Narumol Prangnawarat44179c32020-03-11 14:51:27 +0000253 std::vector<T> outputExpected = armnnUtils::QuantizedVector<T>(
Mike Kellyc9ea45a2020-02-28 18:11:58 +0000254 {
255 1, 11, 21, 31, 41, 51,
256 2, 12, 22, 32, 42, 52,
257 3, 13, 23, 33, 43, 53
Narumol Prangnawarat44179c32020-03-11 14:51:27 +0000258 },
259 qScale, qOffset);
Mike Kellyc9ea45a2020-02-28 18:11:58 +0000260
Finn Williamsec36d3e2020-08-28 13:17:05 +0100261 return SimpleTransposeTestImpl<T>(workloadFactory, memoryManager, tensorHandleFactory,
262 descriptor, inputTensorInfo,
263 outputTensorInfo, input, outputExpected);
Mike Kellyc9ea45a2020-02-28 18:11:58 +0000264}