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> |
| 12 | #include <backendsCommon/WorkloadFactory.hpp> |
| 13 | |
Teresa Charlin | 9e132f5 | 2020-08-15 12:23:29 +0100 | [diff] [blame] | 14 | #include <backendsCommon/test/WorkloadFactoryHelper.hpp> |
Mike Kelly | c9ea45a | 2020-02-28 18:11:58 +0000 | [diff] [blame] | 15 | #include <backendsCommon/test/WorkloadTestUtils.hpp> |
| 16 | |
| 17 | #include <test/TensorHelpers.hpp> |
| 18 | |
Teresa Charlin | 9e132f5 | 2020-08-15 12:23:29 +0100 | [diff] [blame] | 19 | template<typename FactoryType, 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, |
| 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 Kelly | c9ea45a | 2020-02-28 18:11:58 +0000 | [diff] [blame] | 29 | auto input = MakeTensor<T, 4>(inputTensorInfo, inputData); |
| 30 | |
| 31 | LayerTestResult<T, 4> ret(outputTensorInfo); |
| 32 | ret.outputExpected = MakeTensor<T, 4>(outputTensorInfo, outputExpectedData); |
| 33 | |
Teresa Charlin | 9e132f5 | 2020-08-15 12:23:29 +0100 | [diff] [blame] | 34 | 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 Kelly | c9ea45a | 2020-02-28 18:11:58 +0000 | [diff] [blame] | 37 | |
| 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 Charlin | 9e132f5 | 2020-08-15 12:23:29 +0100 | [diff] [blame] | 58 | template<typename FactoryType, armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>> |
Mike Kelly | c9ea45a | 2020-02-28 18:11:58 +0000 | [diff] [blame] | 59 | LayerTestResult<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 Prangnawarat | 44179c3 | 2020-03-11 14:51:27 +0000 | [diff] [blame] | 76 | float qScale = 0.5f; |
| 77 | int32_t qOffset = 5; |
Mike Kelly | c9ea45a | 2020-02-28 18:11:58 +0000 | [diff] [blame] | 78 | if(armnn::IsQuantizedType<T>()) |
| 79 | { |
Narumol Prangnawarat | 44179c3 | 2020-03-11 14:51:27 +0000 | [diff] [blame] | 80 | inputTensorInfo.SetQuantizationScale(qScale); |
| 81 | inputTensorInfo.SetQuantizationOffset(qOffset); |
| 82 | outputTensorInfo.SetQuantizationScale(qScale); |
| 83 | outputTensorInfo.SetQuantizationOffset(qOffset); |
Mike Kelly | c9ea45a | 2020-02-28 18:11:58 +0000 | [diff] [blame] | 84 | } |
| 85 | |
Narumol Prangnawarat | 44179c3 | 2020-03-11 14:51:27 +0000 | [diff] [blame] | 86 | std::vector<T> input = armnnUtils::QuantizedVector<T>( |
Mike Kelly | c9ea45a | 2020-02-28 18:11:58 +0000 | [diff] [blame] | 87 | { |
| 88 | 1, 2, |
| 89 | 3, 4, |
| 90 | 5, 6, |
| 91 | 7, 8 |
Narumol Prangnawarat | 44179c3 | 2020-03-11 14:51:27 +0000 | [diff] [blame] | 92 | }, |
| 93 | qScale, qOffset); |
Mike Kelly | c9ea45a | 2020-02-28 18:11:58 +0000 | [diff] [blame] | 94 | |
Narumol Prangnawarat | 44179c3 | 2020-03-11 14:51:27 +0000 | [diff] [blame] | 95 | std::vector<T> outputExpected = armnnUtils::QuantizedVector<T>( |
Mike Kelly | c9ea45a | 2020-02-28 18:11:58 +0000 | [diff] [blame] | 96 | { |
| 97 | 1, 5, 2, 6, |
| 98 | 3, 7, 4, 8 |
Narumol Prangnawarat | 44179c3 | 2020-03-11 14:51:27 +0000 | [diff] [blame] | 99 | }, |
| 100 | qScale, qOffset); |
Mike Kelly | c9ea45a | 2020-02-28 18:11:58 +0000 | [diff] [blame] | 101 | |
Teresa Charlin | 9e132f5 | 2020-08-15 12:23:29 +0100 | [diff] [blame] | 102 | return SimpleTransposeTestImpl<FactoryType, T>(workloadFactory, memoryManager, |
| 103 | descriptor, inputTensorInfo, |
| 104 | outputTensorInfo, input, outputExpected); |
Mike Kelly | c9ea45a | 2020-02-28 18:11:58 +0000 | [diff] [blame] | 105 | } |
| 106 | |
Teresa Charlin | 9e132f5 | 2020-08-15 12:23:29 +0100 | [diff] [blame] | 107 | template<typename FactoryType, armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>> |
Mike Kelly | c9ea45a | 2020-02-28 18:11:58 +0000 | [diff] [blame] | 108 | LayerTestResult<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 Prangnawarat | 44179c3 | 2020-03-11 14:51:27 +0000 | [diff] [blame] | 125 | float qScale = 0.5f; |
| 126 | int32_t qOffset = 5; |
Mike Kelly | c9ea45a | 2020-02-28 18:11:58 +0000 | [diff] [blame] | 127 | if(armnn::IsQuantizedType<T>()) |
| 128 | { |
Narumol Prangnawarat | 44179c3 | 2020-03-11 14:51:27 +0000 | [diff] [blame] | 129 | inputTensorInfo.SetQuantizationScale(qScale); |
| 130 | inputTensorInfo.SetQuantizationOffset(qOffset); |
| 131 | outputTensorInfo.SetQuantizationScale(qScale); |
| 132 | outputTensorInfo.SetQuantizationOffset(qOffset); |
Mike Kelly | c9ea45a | 2020-02-28 18:11:58 +0000 | [diff] [blame] | 133 | } |
| 134 | |
Narumol Prangnawarat | 44179c3 | 2020-03-11 14:51:27 +0000 | [diff] [blame] | 135 | std::vector<T> input = armnnUtils::QuantizedVector<T>( |
Mike Kelly | c9ea45a | 2020-02-28 18:11:58 +0000 | [diff] [blame] | 136 | { |
| 137 | 1, 2, 3, |
| 138 | 11, 12, 13, |
| 139 | 21, 22, 23, |
| 140 | 31, 32, 33 |
Narumol Prangnawarat | 44179c3 | 2020-03-11 14:51:27 +0000 | [diff] [blame] | 141 | }, |
| 142 | qScale, qOffset); |
Mike Kelly | c9ea45a | 2020-02-28 18:11:58 +0000 | [diff] [blame] | 143 | |
Narumol Prangnawarat | 44179c3 | 2020-03-11 14:51:27 +0000 | [diff] [blame] | 144 | std::vector<T> outputExpected = armnnUtils::QuantizedVector<T>( |
Mike Kelly | c9ea45a | 2020-02-28 18:11:58 +0000 | [diff] [blame] | 145 | { |
| 146 | 1, 11, 21, 31, |
| 147 | 2, 12, 22, 32, |
| 148 | 3, 13, 23, 33 |
Narumol Prangnawarat | 44179c3 | 2020-03-11 14:51:27 +0000 | [diff] [blame] | 149 | }, |
| 150 | qScale, qOffset); |
Mike Kelly | c9ea45a | 2020-02-28 18:11:58 +0000 | [diff] [blame] | 151 | |
Teresa Charlin | 9e132f5 | 2020-08-15 12:23:29 +0100 | [diff] [blame] | 152 | return SimpleTransposeTestImpl<FactoryType, T>(workloadFactory, memoryManager, |
| 153 | descriptor, inputTensorInfo, |
| 154 | outputTensorInfo, input, outputExpected); |
Mike Kelly | c9ea45a | 2020-02-28 18:11:58 +0000 | [diff] [blame] | 155 | } |
| 156 | |
Teresa Charlin | 9e132f5 | 2020-08-15 12:23:29 +0100 | [diff] [blame] | 157 | template<typename FactoryType, armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>> |
Mike Kelly | c9ea45a | 2020-02-28 18:11:58 +0000 | [diff] [blame] | 158 | LayerTestResult<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 Prangnawarat | 44179c3 | 2020-03-11 14:51:27 +0000 | [diff] [blame] | 175 | float qScale = 0.5f; |
| 176 | int32_t qOffset = 5; |
Mike Kelly | c9ea45a | 2020-02-28 18:11:58 +0000 | [diff] [blame] | 177 | if(armnn::IsQuantizedType<T>()) |
| 178 | { |
Narumol Prangnawarat | 44179c3 | 2020-03-11 14:51:27 +0000 | [diff] [blame] | 179 | inputTensorInfo.SetQuantizationScale(qScale); |
| 180 | inputTensorInfo.SetQuantizationOffset(qOffset); |
| 181 | outputTensorInfo.SetQuantizationScale(qScale); |
| 182 | outputTensorInfo.SetQuantizationOffset(qOffset); |
Mike Kelly | c9ea45a | 2020-02-28 18:11:58 +0000 | [diff] [blame] | 183 | } |
| 184 | |
Narumol Prangnawarat | 44179c3 | 2020-03-11 14:51:27 +0000 | [diff] [blame] | 185 | std::vector<T> input = armnnUtils::QuantizedVector<T>( |
Mike Kelly | c9ea45a | 2020-02-28 18:11:58 +0000 | [diff] [blame] | 186 | { |
| 187 | 1, 11, 21, 31, |
| 188 | 2, 12, 22, 32, |
| 189 | 3, 13, 23, 33 |
Narumol Prangnawarat | 44179c3 | 2020-03-11 14:51:27 +0000 | [diff] [blame] | 190 | }, |
| 191 | qScale, qOffset); |
Mike Kelly | c9ea45a | 2020-02-28 18:11:58 +0000 | [diff] [blame] | 192 | |
Narumol Prangnawarat | 44179c3 | 2020-03-11 14:51:27 +0000 | [diff] [blame] | 193 | std::vector<T> outputExpected = armnnUtils::QuantizedVector<T>( |
Mike Kelly | c9ea45a | 2020-02-28 18:11:58 +0000 | [diff] [blame] | 194 | { |
| 195 | 1, 2, 3, |
| 196 | 11, 12, 13, |
| 197 | 21, 22, 23, |
| 198 | 31, 32, 33, |
Narumol Prangnawarat | 44179c3 | 2020-03-11 14:51:27 +0000 | [diff] [blame] | 199 | }, |
| 200 | qScale, qOffset); |
Mike Kelly | c9ea45a | 2020-02-28 18:11:58 +0000 | [diff] [blame] | 201 | |
Teresa Charlin | 9e132f5 | 2020-08-15 12:23:29 +0100 | [diff] [blame] | 202 | return SimpleTransposeTestImpl<FactoryType, T>(workloadFactory, memoryManager, |
| 203 | descriptor, inputTensorInfo, |
| 204 | outputTensorInfo, input, outputExpected); |
Mike Kelly | c9ea45a | 2020-02-28 18:11:58 +0000 | [diff] [blame] | 205 | } |
| 206 | |
Teresa Charlin | 9e132f5 | 2020-08-15 12:23:29 +0100 | [diff] [blame] | 207 | template<typename FactoryType, armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>> |
Mike Kelly | c9ea45a | 2020-02-28 18:11:58 +0000 | [diff] [blame] | 208 | LayerTestResult<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 Prangnawarat | 44179c3 | 2020-03-11 14:51:27 +0000 | [diff] [blame] | 225 | float qScale = 0.5f; |
| 226 | int32_t qOffset = 5; |
Mike Kelly | c9ea45a | 2020-02-28 18:11:58 +0000 | [diff] [blame] | 227 | if(armnn::IsQuantizedType<T>()) |
| 228 | { |
Narumol Prangnawarat | 44179c3 | 2020-03-11 14:51:27 +0000 | [diff] [blame] | 229 | inputTensorInfo.SetQuantizationScale(qScale); |
| 230 | inputTensorInfo.SetQuantizationOffset(qOffset); |
| 231 | outputTensorInfo.SetQuantizationScale(qScale); |
| 232 | outputTensorInfo.SetQuantizationOffset(qOffset); |
Mike Kelly | c9ea45a | 2020-02-28 18:11:58 +0000 | [diff] [blame] | 233 | } |
| 234 | |
Narumol Prangnawarat | 44179c3 | 2020-03-11 14:51:27 +0000 | [diff] [blame] | 235 | std::vector<T> input = armnnUtils::QuantizedVector<T>( |
Mike Kelly | c9ea45a | 2020-02-28 18:11:58 +0000 | [diff] [blame] | 236 | { |
| 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 Prangnawarat | 44179c3 | 2020-03-11 14:51:27 +0000 | [diff] [blame] | 243 | }, |
| 244 | qScale, qOffset); |
Mike Kelly | c9ea45a | 2020-02-28 18:11:58 +0000 | [diff] [blame] | 245 | |
Narumol Prangnawarat | 44179c3 | 2020-03-11 14:51:27 +0000 | [diff] [blame] | 246 | std::vector<T> outputExpected = armnnUtils::QuantizedVector<T>( |
Mike Kelly | c9ea45a | 2020-02-28 18:11:58 +0000 | [diff] [blame] | 247 | { |
| 248 | 1, 11, 21, 31, 41, 51, |
| 249 | 2, 12, 22, 32, 42, 52, |
| 250 | 3, 13, 23, 33, 43, 53 |
Narumol Prangnawarat | 44179c3 | 2020-03-11 14:51:27 +0000 | [diff] [blame] | 251 | }, |
| 252 | qScale, qOffset); |
Mike Kelly | c9ea45a | 2020-02-28 18:11:58 +0000 | [diff] [blame] | 253 | |
Teresa Charlin | 9e132f5 | 2020-08-15 12:23:29 +0100 | [diff] [blame] | 254 | return SimpleTransposeTestImpl<FactoryType, T>(workloadFactory, memoryManager, |
| 255 | descriptor, inputTensorInfo, |
| 256 | outputTensorInfo, input, outputExpected); |
Mike Kelly | c9ea45a | 2020-02-28 18:11:58 +0000 | [diff] [blame] | 257 | } |