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