Mike Kelly | c9ea45a | 2020-02-28 18:11:58 +0000 | [diff] [blame] | 1 | // |
| 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 Donelan | 0c47974 | 2021-12-10 12:43:54 +0000 | [diff] [blame] | 12 | #include <armnn/backends/WorkloadFactory.hpp> |
Mike Kelly | c9ea45a | 2020-02-28 18:11:58 +0000 | [diff] [blame] | 13 | |
Teresa Charlin | 9e132f5 | 2020-08-15 12:23:29 +0100 | [diff] [blame] | 14 | #include <backendsCommon/test/WorkloadFactoryHelper.hpp> |
Colm Donelan | 0c47974 | 2021-12-10 12:43:54 +0000 | [diff] [blame] | 15 | #include <armnnTestUtils/WorkloadTestUtils.hpp> |
Mike Kelly | c9ea45a | 2020-02-28 18:11:58 +0000 | [diff] [blame] | 16 | |
Colm Donelan | c42a987 | 2022-02-02 16:35:09 +0000 | [diff] [blame] | 17 | #include <armnnTestUtils/TensorHelpers.hpp> |
Mike Kelly | c9ea45a | 2020-02-28 18:11:58 +0000 | [diff] [blame] | 18 | |
Finn Williams | ec36d3e | 2020-08-28 13:17:05 +0100 | [diff] [blame] | 19 | template<typename T> |
Mike Kelly | c9ea45a | 2020-02-28 18:11:58 +0000 | [diff] [blame] | 20 | LayerTestResult<T, 4> SimpleTransposeTestImpl( |
| 21 | armnn::IWorkloadFactory& workloadFactory, |
| 22 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
Finn Williams | ec36d3e | 2020-08-28 13:17:05 +0100 | [diff] [blame] | 23 | const armnn::ITensorHandleFactory& tensorHandleFactory, |
Mike Kelly | c9ea45a | 2020-02-28 18:11:58 +0000 | [diff] [blame] | 24 | 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 Williams | ec36d3e | 2020-08-28 13:17:05 +0100 | [diff] [blame] | 30 | IgnoreUnused(memoryManager); |
Sadik Armagan | 483c811 | 2021-06-01 09:24:52 +0100 | [diff] [blame] | 31 | std::vector<T> actualOutput(outputTensorInfo.GetNumElements()); |
Mike Kelly | c9ea45a | 2020-02-28 18:11:58 +0000 | [diff] [blame] | 32 | |
Teresa Charlin | 9e132f5 | 2020-08-15 12:23:29 +0100 | [diff] [blame] | 33 | std::unique_ptr<armnn::ITensorHandle> inputHandle = tensorHandleFactory.CreateTensorHandle(inputTensorInfo); |
| 34 | std::unique_ptr<armnn::ITensorHandle> outputHandle = tensorHandleFactory.CreateTensorHandle(outputTensorInfo); |
Mike Kelly | c9ea45a | 2020-02-28 18:11:58 +0000 | [diff] [blame] | 35 | |
| 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 Charlin | 611c7fb | 2022-01-07 09:47:29 +0000 | [diff] [blame] | 42 | std::unique_ptr<armnn::IWorkload> workload = workloadFactory.CreateWorkload(armnn::LayerType::Transpose, |
| 43 | data, |
| 44 | info); |
Mike Kelly | c9ea45a | 2020-02-28 18:11:58 +0000 | [diff] [blame] | 45 | |
| 46 | inputHandle->Allocate(); |
| 47 | outputHandle->Allocate(); |
| 48 | |
Sadik Armagan | 483c811 | 2021-06-01 09:24:52 +0100 | [diff] [blame] | 49 | CopyDataToITensorHandle(inputHandle.get(), inputData.data()); |
Mike Kelly | c9ea45a | 2020-02-28 18:11:58 +0000 | [diff] [blame] | 50 | |
| 51 | workload->Execute(); |
| 52 | |
Sadik Armagan | 483c811 | 2021-06-01 09:24:52 +0100 | [diff] [blame] | 53 | CopyDataFromITensorHandle(actualOutput.data(), outputHandle.get()); |
Mike Kelly | c9ea45a | 2020-02-28 18:11:58 +0000 | [diff] [blame] | 54 | |
Sadik Armagan | 483c811 | 2021-06-01 09:24:52 +0100 | [diff] [blame] | 55 | return LayerTestResult<T, 4>(actualOutput, |
| 56 | outputExpectedData, |
| 57 | outputHandle->GetShape(), |
| 58 | outputTensorInfo.GetShape()); |
Mike Kelly | c9ea45a | 2020-02-28 18:11:58 +0000 | [diff] [blame] | 59 | } |
| 60 | |
Finn Williams | ec36d3e | 2020-08-28 13:17:05 +0100 | [diff] [blame] | 61 | template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>> |
Mike Kelly | c9ea45a | 2020-02-28 18:11:58 +0000 | [diff] [blame] | 62 | LayerTestResult<T, 4> SimpleTransposeTest( |
| 63 | armnn::IWorkloadFactory& workloadFactory, |
Finn Williams | ec36d3e | 2020-08-28 13:17:05 +0100 | [diff] [blame] | 64 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 65 | const armnn::ITensorHandleFactory& tensorHandleFactory) |
Mike Kelly | c9ea45a | 2020-02-28 18:11:58 +0000 | [diff] [blame] | 66 | { |
| 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 Prangnawarat | 44179c3 | 2020-03-11 14:51:27 +0000 | [diff] [blame] | 80 | float qScale = 0.5f; |
| 81 | int32_t qOffset = 5; |
Mike Kelly | c9ea45a | 2020-02-28 18:11:58 +0000 | [diff] [blame] | 82 | if(armnn::IsQuantizedType<T>()) |
| 83 | { |
Narumol Prangnawarat | 44179c3 | 2020-03-11 14:51:27 +0000 | [diff] [blame] | 84 | inputTensorInfo.SetQuantizationScale(qScale); |
| 85 | inputTensorInfo.SetQuantizationOffset(qOffset); |
| 86 | outputTensorInfo.SetQuantizationScale(qScale); |
| 87 | outputTensorInfo.SetQuantizationOffset(qOffset); |
Mike Kelly | c9ea45a | 2020-02-28 18:11:58 +0000 | [diff] [blame] | 88 | } |
| 89 | |
Narumol Prangnawarat | 44179c3 | 2020-03-11 14:51:27 +0000 | [diff] [blame] | 90 | std::vector<T> input = armnnUtils::QuantizedVector<T>( |
Mike Kelly | c9ea45a | 2020-02-28 18:11:58 +0000 | [diff] [blame] | 91 | { |
| 92 | 1, 2, |
| 93 | 3, 4, |
| 94 | 5, 6, |
| 95 | 7, 8 |
Narumol Prangnawarat | 44179c3 | 2020-03-11 14:51:27 +0000 | [diff] [blame] | 96 | }, |
| 97 | qScale, qOffset); |
Mike Kelly | c9ea45a | 2020-02-28 18:11:58 +0000 | [diff] [blame] | 98 | |
Narumol Prangnawarat | 44179c3 | 2020-03-11 14:51:27 +0000 | [diff] [blame] | 99 | std::vector<T> outputExpected = armnnUtils::QuantizedVector<T>( |
Mike Kelly | c9ea45a | 2020-02-28 18:11:58 +0000 | [diff] [blame] | 100 | { |
| 101 | 1, 5, 2, 6, |
| 102 | 3, 7, 4, 8 |
Narumol Prangnawarat | 44179c3 | 2020-03-11 14:51:27 +0000 | [diff] [blame] | 103 | }, |
| 104 | qScale, qOffset); |
Mike Kelly | c9ea45a | 2020-02-28 18:11:58 +0000 | [diff] [blame] | 105 | |
Finn Williams | ec36d3e | 2020-08-28 13:17:05 +0100 | [diff] [blame] | 106 | return SimpleTransposeTestImpl(workloadFactory, memoryManager, tensorHandleFactory, |
| 107 | descriptor, inputTensorInfo, |
| 108 | outputTensorInfo, input, outputExpected); |
Mike Kelly | c9ea45a | 2020-02-28 18:11:58 +0000 | [diff] [blame] | 109 | } |
| 110 | |
Finn Williams | ec36d3e | 2020-08-28 13:17:05 +0100 | [diff] [blame] | 111 | template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>> |
Mike Kelly | c9ea45a | 2020-02-28 18:11:58 +0000 | [diff] [blame] | 112 | LayerTestResult<T, 4> TransposeValueSet1Test( |
| 113 | armnn::IWorkloadFactory& workloadFactory, |
Finn Williams | ec36d3e | 2020-08-28 13:17:05 +0100 | [diff] [blame] | 114 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 115 | const armnn::ITensorHandleFactory& tensorHandleFactory) |
Mike Kelly | c9ea45a | 2020-02-28 18:11:58 +0000 | [diff] [blame] | 116 | { |
| 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 Prangnawarat | 44179c3 | 2020-03-11 14:51:27 +0000 | [diff] [blame] | 130 | float qScale = 0.5f; |
| 131 | int32_t qOffset = 5; |
Mike Kelly | c9ea45a | 2020-02-28 18:11:58 +0000 | [diff] [blame] | 132 | if(armnn::IsQuantizedType<T>()) |
| 133 | { |
Narumol Prangnawarat | 44179c3 | 2020-03-11 14:51:27 +0000 | [diff] [blame] | 134 | inputTensorInfo.SetQuantizationScale(qScale); |
| 135 | inputTensorInfo.SetQuantizationOffset(qOffset); |
| 136 | outputTensorInfo.SetQuantizationScale(qScale); |
| 137 | outputTensorInfo.SetQuantizationOffset(qOffset); |
Mike Kelly | c9ea45a | 2020-02-28 18:11:58 +0000 | [diff] [blame] | 138 | } |
| 139 | |
Narumol Prangnawarat | 44179c3 | 2020-03-11 14:51:27 +0000 | [diff] [blame] | 140 | std::vector<T> input = armnnUtils::QuantizedVector<T>( |
Mike Kelly | c9ea45a | 2020-02-28 18:11:58 +0000 | [diff] [blame] | 141 | { |
| 142 | 1, 2, 3, |
| 143 | 11, 12, 13, |
| 144 | 21, 22, 23, |
| 145 | 31, 32, 33 |
Narumol Prangnawarat | 44179c3 | 2020-03-11 14:51:27 +0000 | [diff] [blame] | 146 | }, |
| 147 | qScale, qOffset); |
Mike Kelly | c9ea45a | 2020-02-28 18:11:58 +0000 | [diff] [blame] | 148 | |
Narumol Prangnawarat | 44179c3 | 2020-03-11 14:51:27 +0000 | [diff] [blame] | 149 | std::vector<T> outputExpected = armnnUtils::QuantizedVector<T>( |
Mike Kelly | c9ea45a | 2020-02-28 18:11:58 +0000 | [diff] [blame] | 150 | { |
| 151 | 1, 11, 21, 31, |
| 152 | 2, 12, 22, 32, |
| 153 | 3, 13, 23, 33 |
Narumol Prangnawarat | 44179c3 | 2020-03-11 14:51:27 +0000 | [diff] [blame] | 154 | }, |
| 155 | qScale, qOffset); |
Mike Kelly | c9ea45a | 2020-02-28 18:11:58 +0000 | [diff] [blame] | 156 | |
Finn Williams | ec36d3e | 2020-08-28 13:17:05 +0100 | [diff] [blame] | 157 | return SimpleTransposeTestImpl<T>(workloadFactory, memoryManager, tensorHandleFactory, |
| 158 | descriptor, inputTensorInfo, |
| 159 | outputTensorInfo, input, outputExpected); |
Mike Kelly | c9ea45a | 2020-02-28 18:11:58 +0000 | [diff] [blame] | 160 | } |
| 161 | |
Finn Williams | ec36d3e | 2020-08-28 13:17:05 +0100 | [diff] [blame] | 162 | template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>> |
Mike Kelly | c9ea45a | 2020-02-28 18:11:58 +0000 | [diff] [blame] | 163 | LayerTestResult<T, 4> TransposeValueSet2Test( |
| 164 | armnn::IWorkloadFactory& workloadFactory, |
Finn Williams | ec36d3e | 2020-08-28 13:17:05 +0100 | [diff] [blame] | 165 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 166 | const armnn::ITensorHandleFactory& tensorHandleFactory) |
Mike Kelly | c9ea45a | 2020-02-28 18:11:58 +0000 | [diff] [blame] | 167 | { |
| 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 Prangnawarat | 44179c3 | 2020-03-11 14:51:27 +0000 | [diff] [blame] | 181 | float qScale = 0.5f; |
| 182 | int32_t qOffset = 5; |
Mike Kelly | c9ea45a | 2020-02-28 18:11:58 +0000 | [diff] [blame] | 183 | if(armnn::IsQuantizedType<T>()) |
| 184 | { |
Narumol Prangnawarat | 44179c3 | 2020-03-11 14:51:27 +0000 | [diff] [blame] | 185 | inputTensorInfo.SetQuantizationScale(qScale); |
| 186 | inputTensorInfo.SetQuantizationOffset(qOffset); |
| 187 | outputTensorInfo.SetQuantizationScale(qScale); |
| 188 | outputTensorInfo.SetQuantizationOffset(qOffset); |
Mike Kelly | c9ea45a | 2020-02-28 18:11:58 +0000 | [diff] [blame] | 189 | } |
| 190 | |
Narumol Prangnawarat | 44179c3 | 2020-03-11 14:51:27 +0000 | [diff] [blame] | 191 | std::vector<T> input = armnnUtils::QuantizedVector<T>( |
Mike Kelly | c9ea45a | 2020-02-28 18:11:58 +0000 | [diff] [blame] | 192 | { |
| 193 | 1, 11, 21, 31, |
| 194 | 2, 12, 22, 32, |
| 195 | 3, 13, 23, 33 |
Narumol Prangnawarat | 44179c3 | 2020-03-11 14:51:27 +0000 | [diff] [blame] | 196 | }, |
| 197 | qScale, qOffset); |
Mike Kelly | c9ea45a | 2020-02-28 18:11:58 +0000 | [diff] [blame] | 198 | |
Narumol Prangnawarat | 44179c3 | 2020-03-11 14:51:27 +0000 | [diff] [blame] | 199 | std::vector<T> outputExpected = armnnUtils::QuantizedVector<T>( |
Mike Kelly | c9ea45a | 2020-02-28 18:11:58 +0000 | [diff] [blame] | 200 | { |
| 201 | 1, 2, 3, |
| 202 | 11, 12, 13, |
| 203 | 21, 22, 23, |
| 204 | 31, 32, 33, |
Narumol Prangnawarat | 44179c3 | 2020-03-11 14:51:27 +0000 | [diff] [blame] | 205 | }, |
| 206 | qScale, qOffset); |
Mike Kelly | c9ea45a | 2020-02-28 18:11:58 +0000 | [diff] [blame] | 207 | |
Finn Williams | ec36d3e | 2020-08-28 13:17:05 +0100 | [diff] [blame] | 208 | return SimpleTransposeTestImpl<T>(workloadFactory, memoryManager, tensorHandleFactory, |
| 209 | descriptor, inputTensorInfo, |
| 210 | outputTensorInfo, input, outputExpected); |
Mike Kelly | c9ea45a | 2020-02-28 18:11:58 +0000 | [diff] [blame] | 211 | } |
| 212 | |
Finn Williams | ec36d3e | 2020-08-28 13:17:05 +0100 | [diff] [blame] | 213 | template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>> |
Mike Kelly | c9ea45a | 2020-02-28 18:11:58 +0000 | [diff] [blame] | 214 | LayerTestResult<T, 4> TransposeValueSet3Test( |
| 215 | armnn::IWorkloadFactory& workloadFactory, |
Finn Williams | ec36d3e | 2020-08-28 13:17:05 +0100 | [diff] [blame] | 216 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 217 | const armnn::ITensorHandleFactory& tensorHandleFactory) |
Mike Kelly | c9ea45a | 2020-02-28 18:11:58 +0000 | [diff] [blame] | 218 | { |
| 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 Prangnawarat | 44179c3 | 2020-03-11 14:51:27 +0000 | [diff] [blame] | 232 | float qScale = 0.5f; |
| 233 | int32_t qOffset = 5; |
Mike Kelly | c9ea45a | 2020-02-28 18:11:58 +0000 | [diff] [blame] | 234 | if(armnn::IsQuantizedType<T>()) |
| 235 | { |
Narumol Prangnawarat | 44179c3 | 2020-03-11 14:51:27 +0000 | [diff] [blame] | 236 | inputTensorInfo.SetQuantizationScale(qScale); |
| 237 | inputTensorInfo.SetQuantizationOffset(qOffset); |
| 238 | outputTensorInfo.SetQuantizationScale(qScale); |
| 239 | outputTensorInfo.SetQuantizationOffset(qOffset); |
Mike Kelly | c9ea45a | 2020-02-28 18:11:58 +0000 | [diff] [blame] | 240 | } |
| 241 | |
Narumol Prangnawarat | 44179c3 | 2020-03-11 14:51:27 +0000 | [diff] [blame] | 242 | std::vector<T> input = armnnUtils::QuantizedVector<T>( |
Mike Kelly | c9ea45a | 2020-02-28 18:11:58 +0000 | [diff] [blame] | 243 | { |
| 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 Prangnawarat | 44179c3 | 2020-03-11 14:51:27 +0000 | [diff] [blame] | 250 | }, |
| 251 | qScale, qOffset); |
Mike Kelly | c9ea45a | 2020-02-28 18:11:58 +0000 | [diff] [blame] | 252 | |
Narumol Prangnawarat | 44179c3 | 2020-03-11 14:51:27 +0000 | [diff] [blame] | 253 | std::vector<T> outputExpected = armnnUtils::QuantizedVector<T>( |
Mike Kelly | c9ea45a | 2020-02-28 18:11:58 +0000 | [diff] [blame] | 254 | { |
| 255 | 1, 11, 21, 31, 41, 51, |
| 256 | 2, 12, 22, 32, 42, 52, |
| 257 | 3, 13, 23, 33, 43, 53 |
Narumol Prangnawarat | 44179c3 | 2020-03-11 14:51:27 +0000 | [diff] [blame] | 258 | }, |
| 259 | qScale, qOffset); |
Mike Kelly | c9ea45a | 2020-02-28 18:11:58 +0000 | [diff] [blame] | 260 | |
Finn Williams | ec36d3e | 2020-08-28 13:17:05 +0100 | [diff] [blame] | 261 | return SimpleTransposeTestImpl<T>(workloadFactory, memoryManager, tensorHandleFactory, |
| 262 | descriptor, inputTensorInfo, |
| 263 | outputTensorInfo, input, outputExpected); |
Mike Kelly | c9ea45a | 2020-02-28 18:11:58 +0000 | [diff] [blame] | 264 | } |