blob: 82bd487d3c5a0ecc21a14dc25d66cd8ed733a010 [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
Sadik Armagana097d2a2021-11-24 15:47:28 +000017#include <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
42 std::unique_ptr<armnn::IWorkload> workload = workloadFactory.CreateTranspose(data, info);
43
44 inputHandle->Allocate();
45 outputHandle->Allocate();
46
Sadik Armagan483c8112021-06-01 09:24:52 +010047 CopyDataToITensorHandle(inputHandle.get(), inputData.data());
Mike Kellyc9ea45a2020-02-28 18:11:58 +000048
49 workload->Execute();
50
Sadik Armagan483c8112021-06-01 09:24:52 +010051 CopyDataFromITensorHandle(actualOutput.data(), outputHandle.get());
Mike Kellyc9ea45a2020-02-28 18:11:58 +000052
Sadik Armagan483c8112021-06-01 09:24:52 +010053 return LayerTestResult<T, 4>(actualOutput,
54 outputExpectedData,
55 outputHandle->GetShape(),
56 outputTensorInfo.GetShape());
Mike Kellyc9ea45a2020-02-28 18:11:58 +000057}
58
Finn Williamsec36d3e2020-08-28 13:17:05 +010059template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>>
Mike Kellyc9ea45a2020-02-28 18:11:58 +000060LayerTestResult<T, 4> SimpleTransposeTest(
61 armnn::IWorkloadFactory& workloadFactory,
Finn Williamsec36d3e2020-08-28 13:17:05 +010062 const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager,
63 const armnn::ITensorHandleFactory& tensorHandleFactory)
Mike Kellyc9ea45a2020-02-28 18:11:58 +000064{
65 armnn::TensorInfo inputTensorInfo;
66 armnn::TensorInfo outputTensorInfo;
67
68 unsigned int inputShape[] = { 1, 2, 2, 2 };
69 unsigned int outputShape[] = { 1, 2, 2, 2 };
70
71 armnn::TransposeDescriptor descriptor;
72 descriptor.m_DimMappings = {0U, 2U, 3U, 1U};
73
74 inputTensorInfo = armnn::TensorInfo(4, inputShape, ArmnnType);
75 outputTensorInfo = armnn::TensorInfo(4, outputShape, ArmnnType);
76
77 // Set quantization parameters if the requested type is a quantized type.
Narumol Prangnawarat44179c32020-03-11 14:51:27 +000078 float qScale = 0.5f;
79 int32_t qOffset = 5;
Mike Kellyc9ea45a2020-02-28 18:11:58 +000080 if(armnn::IsQuantizedType<T>())
81 {
Narumol Prangnawarat44179c32020-03-11 14:51:27 +000082 inputTensorInfo.SetQuantizationScale(qScale);
83 inputTensorInfo.SetQuantizationOffset(qOffset);
84 outputTensorInfo.SetQuantizationScale(qScale);
85 outputTensorInfo.SetQuantizationOffset(qOffset);
Mike Kellyc9ea45a2020-02-28 18:11:58 +000086 }
87
Narumol Prangnawarat44179c32020-03-11 14:51:27 +000088 std::vector<T> input = armnnUtils::QuantizedVector<T>(
Mike Kellyc9ea45a2020-02-28 18:11:58 +000089 {
90 1, 2,
91 3, 4,
92 5, 6,
93 7, 8
Narumol Prangnawarat44179c32020-03-11 14:51:27 +000094 },
95 qScale, qOffset);
Mike Kellyc9ea45a2020-02-28 18:11:58 +000096
Narumol Prangnawarat44179c32020-03-11 14:51:27 +000097 std::vector<T> outputExpected = armnnUtils::QuantizedVector<T>(
Mike Kellyc9ea45a2020-02-28 18:11:58 +000098 {
99 1, 5, 2, 6,
100 3, 7, 4, 8
Narumol Prangnawarat44179c32020-03-11 14:51:27 +0000101 },
102 qScale, qOffset);
Mike Kellyc9ea45a2020-02-28 18:11:58 +0000103
Finn Williamsec36d3e2020-08-28 13:17:05 +0100104 return SimpleTransposeTestImpl(workloadFactory, memoryManager, tensorHandleFactory,
105 descriptor, inputTensorInfo,
106 outputTensorInfo, input, outputExpected);
Mike Kellyc9ea45a2020-02-28 18:11:58 +0000107}
108
Finn Williamsec36d3e2020-08-28 13:17:05 +0100109template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>>
Mike Kellyc9ea45a2020-02-28 18:11:58 +0000110LayerTestResult<T, 4> TransposeValueSet1Test(
111 armnn::IWorkloadFactory& workloadFactory,
Finn Williamsec36d3e2020-08-28 13:17:05 +0100112 const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager,
113 const armnn::ITensorHandleFactory& tensorHandleFactory)
Mike Kellyc9ea45a2020-02-28 18:11:58 +0000114{
115 armnn::TensorInfo inputTensorInfo;
116 armnn::TensorInfo outputTensorInfo;
117
118 unsigned int inputShape[] = { 1, 2, 2, 3 };
119 unsigned int outputShape[] = { 1, 3, 2, 2 };
120
121 armnn::TransposeDescriptor descriptor;
122 descriptor.m_DimMappings = {0U, 3U, 1U, 2U};
123
124 inputTensorInfo = armnn::TensorInfo(4, inputShape, ArmnnType);
125 outputTensorInfo = armnn::TensorInfo(4, outputShape, ArmnnType);
126
127 // Set quantization parameters if the requested type is a quantized type.
Narumol Prangnawarat44179c32020-03-11 14:51:27 +0000128 float qScale = 0.5f;
129 int32_t qOffset = 5;
Mike Kellyc9ea45a2020-02-28 18:11:58 +0000130 if(armnn::IsQuantizedType<T>())
131 {
Narumol Prangnawarat44179c32020-03-11 14:51:27 +0000132 inputTensorInfo.SetQuantizationScale(qScale);
133 inputTensorInfo.SetQuantizationOffset(qOffset);
134 outputTensorInfo.SetQuantizationScale(qScale);
135 outputTensorInfo.SetQuantizationOffset(qOffset);
Mike Kellyc9ea45a2020-02-28 18:11:58 +0000136 }
137
Narumol Prangnawarat44179c32020-03-11 14:51:27 +0000138 std::vector<T> input = armnnUtils::QuantizedVector<T>(
Mike Kellyc9ea45a2020-02-28 18:11:58 +0000139 {
140 1, 2, 3,
141 11, 12, 13,
142 21, 22, 23,
143 31, 32, 33
Narumol Prangnawarat44179c32020-03-11 14:51:27 +0000144 },
145 qScale, qOffset);
Mike Kellyc9ea45a2020-02-28 18:11:58 +0000146
Narumol Prangnawarat44179c32020-03-11 14:51:27 +0000147 std::vector<T> outputExpected = armnnUtils::QuantizedVector<T>(
Mike Kellyc9ea45a2020-02-28 18:11:58 +0000148 {
149 1, 11, 21, 31,
150 2, 12, 22, 32,
151 3, 13, 23, 33
Narumol Prangnawarat44179c32020-03-11 14:51:27 +0000152 },
153 qScale, qOffset);
Mike Kellyc9ea45a2020-02-28 18:11:58 +0000154
Finn Williamsec36d3e2020-08-28 13:17:05 +0100155 return SimpleTransposeTestImpl<T>(workloadFactory, memoryManager, tensorHandleFactory,
156 descriptor, inputTensorInfo,
157 outputTensorInfo, input, outputExpected);
Mike Kellyc9ea45a2020-02-28 18:11:58 +0000158}
159
Finn Williamsec36d3e2020-08-28 13:17:05 +0100160template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>>
Mike Kellyc9ea45a2020-02-28 18:11:58 +0000161LayerTestResult<T, 4> TransposeValueSet2Test(
162 armnn::IWorkloadFactory& workloadFactory,
Finn Williamsec36d3e2020-08-28 13:17:05 +0100163 const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager,
164 const armnn::ITensorHandleFactory& tensorHandleFactory)
Mike Kellyc9ea45a2020-02-28 18:11:58 +0000165{
166 armnn::TensorInfo inputTensorInfo;
167 armnn::TensorInfo outputTensorInfo;
168
169 unsigned int inputShape[] = { 1, 3, 2, 2 };
170 unsigned int outputShape[] = { 1, 2, 2, 3 };
171
172 armnn::TransposeDescriptor descriptor;
173 descriptor.m_DimMappings = {0U, 2U, 3U, 1U};
174
175 inputTensorInfo = armnn::TensorInfo(4, inputShape, ArmnnType);
176 outputTensorInfo = armnn::TensorInfo(4, outputShape, ArmnnType);
177
178 // Set quantization parameters if the requested type is a quantized type.
Narumol Prangnawarat44179c32020-03-11 14:51:27 +0000179 float qScale = 0.5f;
180 int32_t qOffset = 5;
Mike Kellyc9ea45a2020-02-28 18:11:58 +0000181 if(armnn::IsQuantizedType<T>())
182 {
Narumol Prangnawarat44179c32020-03-11 14:51:27 +0000183 inputTensorInfo.SetQuantizationScale(qScale);
184 inputTensorInfo.SetQuantizationOffset(qOffset);
185 outputTensorInfo.SetQuantizationScale(qScale);
186 outputTensorInfo.SetQuantizationOffset(qOffset);
Mike Kellyc9ea45a2020-02-28 18:11:58 +0000187 }
188
Narumol Prangnawarat44179c32020-03-11 14:51:27 +0000189 std::vector<T> input = armnnUtils::QuantizedVector<T>(
Mike Kellyc9ea45a2020-02-28 18:11:58 +0000190 {
191 1, 11, 21, 31,
192 2, 12, 22, 32,
193 3, 13, 23, 33
Narumol Prangnawarat44179c32020-03-11 14:51:27 +0000194 },
195 qScale, qOffset);
Mike Kellyc9ea45a2020-02-28 18:11:58 +0000196
Narumol Prangnawarat44179c32020-03-11 14:51:27 +0000197 std::vector<T> outputExpected = armnnUtils::QuantizedVector<T>(
Mike Kellyc9ea45a2020-02-28 18:11:58 +0000198 {
199 1, 2, 3,
200 11, 12, 13,
201 21, 22, 23,
202 31, 32, 33,
Narumol Prangnawarat44179c32020-03-11 14:51:27 +0000203 },
204 qScale, qOffset);
Mike Kellyc9ea45a2020-02-28 18:11:58 +0000205
Finn Williamsec36d3e2020-08-28 13:17:05 +0100206 return SimpleTransposeTestImpl<T>(workloadFactory, memoryManager, tensorHandleFactory,
207 descriptor, inputTensorInfo,
208 outputTensorInfo, input, outputExpected);
Mike Kellyc9ea45a2020-02-28 18:11:58 +0000209}
210
Finn Williamsec36d3e2020-08-28 13:17:05 +0100211template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>>
Mike Kellyc9ea45a2020-02-28 18:11:58 +0000212LayerTestResult<T, 4> TransposeValueSet3Test(
213 armnn::IWorkloadFactory& workloadFactory,
Finn Williamsec36d3e2020-08-28 13:17:05 +0100214 const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager,
215 const armnn::ITensorHandleFactory& tensorHandleFactory)
Mike Kellyc9ea45a2020-02-28 18:11:58 +0000216{
217 armnn::TensorInfo inputTensorInfo;
218 armnn::TensorInfo outputTensorInfo;
219
220 unsigned int inputShape[] = { 1, 2, 3, 3 };
221 unsigned int outputShape[] = { 1, 3, 2, 3 };
222
223 armnn::TransposeDescriptor descriptor;
224 descriptor.m_DimMappings = {0U, 3U, 1U, 2U};
225
226 inputTensorInfo = armnn::TensorInfo(4, inputShape, ArmnnType);
227 outputTensorInfo = armnn::TensorInfo(4, outputShape, ArmnnType);
228
229 // Set quantization parameters if the requested type is a quantized type.
Narumol Prangnawarat44179c32020-03-11 14:51:27 +0000230 float qScale = 0.5f;
231 int32_t qOffset = 5;
Mike Kellyc9ea45a2020-02-28 18:11:58 +0000232 if(armnn::IsQuantizedType<T>())
233 {
Narumol Prangnawarat44179c32020-03-11 14:51:27 +0000234 inputTensorInfo.SetQuantizationScale(qScale);
235 inputTensorInfo.SetQuantizationOffset(qOffset);
236 outputTensorInfo.SetQuantizationScale(qScale);
237 outputTensorInfo.SetQuantizationOffset(qOffset);
Mike Kellyc9ea45a2020-02-28 18:11:58 +0000238 }
239
Narumol Prangnawarat44179c32020-03-11 14:51:27 +0000240 std::vector<T> input = armnnUtils::QuantizedVector<T>(
Mike Kellyc9ea45a2020-02-28 18:11:58 +0000241 {
242 1, 2, 3,
243 11, 12, 13,
244 21, 22, 23,
245 31, 32, 33,
246 41, 42, 43,
247 51, 52, 53
Narumol Prangnawarat44179c32020-03-11 14:51:27 +0000248 },
249 qScale, qOffset);
Mike Kellyc9ea45a2020-02-28 18:11:58 +0000250
Narumol Prangnawarat44179c32020-03-11 14:51:27 +0000251 std::vector<T> outputExpected = armnnUtils::QuantizedVector<T>(
Mike Kellyc9ea45a2020-02-28 18:11:58 +0000252 {
253 1, 11, 21, 31, 41, 51,
254 2, 12, 22, 32, 42, 52,
255 3, 13, 23, 33, 43, 53
Narumol Prangnawarat44179c32020-03-11 14:51:27 +0000256 },
257 qScale, qOffset);
Mike Kellyc9ea45a2020-02-28 18:11:58 +0000258
Finn Williamsec36d3e2020-08-28 13:17:05 +0100259 return SimpleTransposeTestImpl<T>(workloadFactory, memoryManager, tensorHandleFactory,
260 descriptor, inputTensorInfo,
261 outputTensorInfo, input, outputExpected);
Mike Kellyc9ea45a2020-02-28 18:11:58 +0000262}