Keith Davis | a57eccb | 2019-06-14 17:33:22 +0100 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2017 Arm Ltd. All rights reserved. |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
Keith Davis | a57eccb | 2019-06-14 17:33:22 +0100 | [diff] [blame] | 5 | |
Aron Virginas-Tar | 00d306e | 2019-08-28 18:08:46 +0100 | [diff] [blame] | 6 | #include "SpaceToDepthTestImpl.hpp" |
| 7 | |
Aron Virginas-Tar | 48623a0 | 2019-10-22 10:00:28 +0100 | [diff] [blame] | 8 | #include <QuantizeHelper.hpp> |
Aron Virginas-Tar | 00d306e | 2019-08-28 18:08:46 +0100 | [diff] [blame] | 9 | #include <ResolveType.hpp> |
Keith Davis | a57eccb | 2019-06-14 17:33:22 +0100 | [diff] [blame] | 10 | |
| 11 | #include <armnn/ArmNN.hpp> |
Keith Davis | a57eccb | 2019-06-14 17:33:22 +0100 | [diff] [blame] | 12 | |
Matteo Martincigh | e011d20 | 2019-11-28 11:35:47 +0000 | [diff] [blame] | 13 | #include <armnnUtils/Permute.hpp> |
| 14 | |
Aron Virginas-Tar | 00d306e | 2019-08-28 18:08:46 +0100 | [diff] [blame] | 15 | #include <backendsCommon/test/TensorCopyUtils.hpp> |
| 16 | #include <backendsCommon/test/WorkloadTestUtils.hpp> |
Keith Davis | a57eccb | 2019-06-14 17:33:22 +0100 | [diff] [blame] | 17 | |
| 18 | #include <test/TensorHelpers.hpp> |
| 19 | |
Aron Virginas-Tar | 00d306e | 2019-08-28 18:08:46 +0100 | [diff] [blame] | 20 | namespace |
| 21 | { |
| 22 | |
Keith Davis | a57eccb | 2019-06-14 17:33:22 +0100 | [diff] [blame] | 23 | template<typename T> |
| 24 | LayerTestResult<T, 4> SpaceToDepthTestImpl( |
| 25 | armnn::IWorkloadFactory& workloadFactory, |
| 26 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 27 | armnn::TensorInfo& inputTensorInfo, |
| 28 | armnn::TensorInfo& outputTensorInfo, |
| 29 | std::vector<float>& inputData, |
| 30 | std::vector<float>& outputExpectedData, |
| 31 | armnn::SpaceToDepthQueueDescriptor descriptor, |
| 32 | const float qScale = 1.0f, |
| 33 | const int32_t qOffset = 0) |
| 34 | { |
Derek Lamberti | c374ff0 | 2019-12-10 21:57:35 +0000 | [diff] [blame^] | 35 | boost::ignore_unused(memoryManager); |
Keith Davis | a57eccb | 2019-06-14 17:33:22 +0100 | [diff] [blame] | 36 | const armnn::PermutationVector NHWCToNCHW = {0, 2, 3, 1}; |
| 37 | |
| 38 | if (descriptor.m_Parameters.m_DataLayout == armnn::DataLayout::NCHW) |
| 39 | { |
| 40 | inputTensorInfo = armnnUtils::Permuted(inputTensorInfo, NHWCToNCHW); |
| 41 | outputTensorInfo = armnnUtils::Permuted(outputTensorInfo, NHWCToNCHW); |
| 42 | |
| 43 | std::vector<float> inputTmp(inputData.size()); |
| 44 | armnnUtils::Permute(inputTensorInfo.GetShape(), NHWCToNCHW, |
| 45 | inputData.data(), inputTmp.data(), sizeof(float)); |
| 46 | inputData = inputTmp; |
| 47 | |
| 48 | std::vector<float> outputTmp(outputExpectedData.size()); |
| 49 | armnnUtils::Permute(outputTensorInfo.GetShape(), NHWCToNCHW, |
| 50 | outputExpectedData.data(), outputTmp.data(), sizeof(float)); |
| 51 | outputExpectedData = outputTmp; |
| 52 | } |
| 53 | |
| 54 | if(armnn::IsQuantizedType<T>()) |
| 55 | { |
| 56 | inputTensorInfo.SetQuantizationScale(qScale); |
| 57 | inputTensorInfo.SetQuantizationOffset(qOffset); |
| 58 | outputTensorInfo.SetQuantizationScale(qScale); |
| 59 | outputTensorInfo.SetQuantizationOffset(qOffset); |
| 60 | } |
| 61 | |
Aron Virginas-Tar | 48623a0 | 2019-10-22 10:00:28 +0100 | [diff] [blame] | 62 | boost::multi_array<T, 4> input = MakeTensor<T, 4>(inputTensorInfo, |
| 63 | armnnUtils::QuantizedVector<T>(inputData, qScale, qOffset)); |
Keith Davis | a57eccb | 2019-06-14 17:33:22 +0100 | [diff] [blame] | 64 | |
| 65 | LayerTestResult<T, 4> ret(outputTensorInfo); |
Aron Virginas-Tar | 48623a0 | 2019-10-22 10:00:28 +0100 | [diff] [blame] | 66 | ret.outputExpected = MakeTensor<T, 4>(outputTensorInfo, |
| 67 | armnnUtils::QuantizedVector<T>(outputExpectedData, qScale, qOffset)); |
Keith Davis | a57eccb | 2019-06-14 17:33:22 +0100 | [diff] [blame] | 68 | |
| 69 | std::unique_ptr<armnn::ITensorHandle> inputHandle = workloadFactory.CreateTensorHandle(inputTensorInfo); |
| 70 | std::unique_ptr<armnn::ITensorHandle> outputHandle = workloadFactory.CreateTensorHandle(outputTensorInfo); |
| 71 | |
| 72 | armnn::WorkloadInfo info; |
| 73 | AddInputToWorkload(descriptor, info, inputTensorInfo, inputHandle.get()); |
| 74 | AddOutputToWorkload(descriptor, info, outputTensorInfo, outputHandle.get()); |
| 75 | |
| 76 | std::unique_ptr<armnn::IWorkload> workload = workloadFactory.CreateSpaceToDepth(descriptor, info); |
| 77 | |
| 78 | inputHandle->Allocate(); |
| 79 | outputHandle->Allocate(); |
| 80 | |
| 81 | CopyDataToITensorHandle(inputHandle.get(), &input[0][0][0][0]); |
| 82 | |
| 83 | workload->Execute(); |
| 84 | |
| 85 | CopyDataFromITensorHandle(&ret.output[0][0][0][0], outputHandle.get()); |
| 86 | |
| 87 | return ret; |
| 88 | } |
| 89 | |
| 90 | template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>> |
James Conroy | d2aa85e | 2019-07-01 17:12:40 +0100 | [diff] [blame] | 91 | LayerTestResult<T, 4> SpaceToDepthSimpleTest1( |
Keith Davis | a57eccb | 2019-06-14 17:33:22 +0100 | [diff] [blame] | 92 | armnn::IWorkloadFactory& workloadFactory, |
| 93 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 94 | armnn::DataLayout dataLayout = armnn::DataLayout::NHWC) |
| 95 | { |
| 96 | unsigned int inputShape[] = {1, 2, 2, 1}; |
| 97 | unsigned int outputShape[] = {1, 1, 1, 4}; |
| 98 | |
| 99 | std::vector<float> input = std::vector<float>( |
| 100 | { |
| 101 | 1.0f, 2.0f, 3.0f, 4.0f |
| 102 | }); |
| 103 | |
| 104 | std::vector<float> outputExpected = std::vector<float>( |
| 105 | { |
| 106 | 1.0f, 2.0f, 3.0f, 4.0f |
| 107 | }); |
| 108 | |
| 109 | armnn::TensorInfo inputTensorInfo; |
| 110 | armnn::TensorInfo outputTensorInfo; |
| 111 | |
| 112 | armnn::SpaceToDepthQueueDescriptor desc; |
| 113 | desc.m_Parameters.m_DataLayout = dataLayout; |
| 114 | desc.m_Parameters.m_BlockSize = 2; |
| 115 | |
| 116 | inputTensorInfo = armnn::TensorInfo(4, inputShape, ArmnnType); |
| 117 | outputTensorInfo = armnn::TensorInfo(4, outputShape, ArmnnType); |
| 118 | |
| 119 | return SpaceToDepthTestImpl<T>( |
| 120 | workloadFactory, memoryManager, inputTensorInfo, outputTensorInfo, input, outputExpected, desc); |
| 121 | } |
| 122 | |
| 123 | template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>> |
James Conroy | d2aa85e | 2019-07-01 17:12:40 +0100 | [diff] [blame] | 124 | LayerTestResult<T, 4> SpaceToDepthSimpleTest2( |
Keith Davis | a57eccb | 2019-06-14 17:33:22 +0100 | [diff] [blame] | 125 | armnn::IWorkloadFactory& workloadFactory, |
| 126 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 127 | armnn::DataLayout dataLayout = armnn::DataLayout::NHWC) |
| 128 | { |
Aron Virginas-Tar | 00d306e | 2019-08-28 18:08:46 +0100 | [diff] [blame] | 129 | unsigned int inputShape[] = {1, 2, 2, 2}; |
Keith Davis | a57eccb | 2019-06-14 17:33:22 +0100 | [diff] [blame] | 130 | unsigned int outputShape[] = {1, 1, 1, 8}; |
| 131 | |
| 132 | std::vector<float> input = std::vector<float>( |
| 133 | { |
| 134 | 1.4f, 2.3f, 3.2f, 4.1f, 5.4f, 6.3f, 7.2f, 8.1f |
| 135 | }); |
| 136 | |
| 137 | std::vector<float> outputExpected = std::vector<float>( |
| 138 | { |
| 139 | 1.4f, 2.3f, 3.2f, 4.1f, 5.4f, 6.3f, 7.2f, 8.1f |
| 140 | }); |
| 141 | |
| 142 | armnn::TensorInfo inputTensorInfo; |
| 143 | armnn::TensorInfo outputTensorInfo; |
| 144 | |
| 145 | armnn::SpaceToDepthQueueDescriptor desc; |
| 146 | desc.m_Parameters.m_DataLayout = dataLayout; |
| 147 | desc.m_Parameters.m_BlockSize = 2; |
| 148 | |
| 149 | inputTensorInfo = armnn::TensorInfo(4, inputShape, ArmnnType); |
| 150 | outputTensorInfo = armnn::TensorInfo(4, outputShape, ArmnnType); |
| 151 | |
| 152 | return SpaceToDepthTestImpl<T>( |
| 153 | workloadFactory, memoryManager, inputTensorInfo, outputTensorInfo, input, outputExpected, desc); |
| 154 | } |
Aron Virginas-Tar | 00d306e | 2019-08-28 18:08:46 +0100 | [diff] [blame] | 155 | |
| 156 | } // anonymous namespace |
| 157 | |
| 158 | LayerTestResult<uint8_t, 4> SpaceToDepthNhwcAsymmQ8Test( |
| 159 | armnn::IWorkloadFactory& workloadFactory, |
| 160 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager) |
| 161 | { |
| 162 | return SpaceToDepthSimpleTest1<armnn::DataType::QuantisedAsymm8>( |
| 163 | workloadFactory, |
| 164 | memoryManager); |
| 165 | } |
| 166 | |
| 167 | LayerTestResult<uint8_t, 4> SpaceToDepthNchwAsymmQ8Test( |
| 168 | armnn::IWorkloadFactory& workloadFactory, |
| 169 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager) |
| 170 | { |
| 171 | return SpaceToDepthSimpleTest1<armnn::DataType::QuantisedAsymm8>( |
| 172 | workloadFactory, |
| 173 | memoryManager, |
| 174 | armnn::DataLayout::NCHW); |
| 175 | } |
| 176 | |
Matthew Jackson | 9bff144 | 2019-09-12 09:08:23 +0100 | [diff] [blame] | 177 | LayerTestResult<armnn::Half, 4> SpaceToDepthNhwcFloat16Test( |
| 178 | armnn::IWorkloadFactory& workloadFactory, |
| 179 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager) |
| 180 | { |
| 181 | return SpaceToDepthSimpleTest1<armnn::DataType::Float16>( |
| 182 | workloadFactory, |
| 183 | memoryManager); |
| 184 | } |
| 185 | |
| 186 | LayerTestResult<armnn::Half, 4> SpaceToDepthNchwFloat16Test( |
| 187 | armnn::IWorkloadFactory& workloadFactory, |
| 188 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager) |
| 189 | { |
| 190 | return SpaceToDepthSimpleTest1<armnn::DataType::Float16>( |
| 191 | workloadFactory, |
| 192 | memoryManager, |
| 193 | armnn::DataLayout::NCHW); |
| 194 | } |
| 195 | |
Aron Virginas-Tar | 00d306e | 2019-08-28 18:08:46 +0100 | [diff] [blame] | 196 | LayerTestResult<float, 4> SpaceToDepthNhwcFloat32Test1( |
| 197 | armnn::IWorkloadFactory& workloadFactory, |
| 198 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager) |
| 199 | { |
| 200 | return SpaceToDepthSimpleTest1<armnn::DataType::Float32>( |
| 201 | workloadFactory, |
| 202 | memoryManager); |
| 203 | } |
| 204 | |
| 205 | LayerTestResult<float, 4> SpaceToDepthNchwFloat32Test1( |
| 206 | armnn::IWorkloadFactory& workloadFactory, |
| 207 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager) |
| 208 | { |
| 209 | return SpaceToDepthSimpleTest1<armnn::DataType::Float32>( |
| 210 | workloadFactory, |
| 211 | memoryManager, |
| 212 | armnn::DataLayout::NCHW); |
| 213 | } |
| 214 | |
| 215 | LayerTestResult<float, 4> SpaceToDepthNhwcFloat32Test2( |
| 216 | armnn::IWorkloadFactory& workloadFactory, |
| 217 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager) |
| 218 | { |
| 219 | return SpaceToDepthSimpleTest2<armnn::DataType::Float32>( |
| 220 | workloadFactory, |
| 221 | memoryManager); |
| 222 | } |
| 223 | |
| 224 | LayerTestResult<float, 4> SpaceToDepthNchwFloat32Test2( |
| 225 | armnn::IWorkloadFactory& workloadFactory, |
| 226 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager) |
| 227 | { |
| 228 | return SpaceToDepthSimpleTest2<armnn::DataType::Float32>( |
| 229 | workloadFactory, |
| 230 | memoryManager, |
| 231 | armnn::DataLayout::NCHW); |
| 232 | } |
| 233 | |
| 234 | LayerTestResult<int16_t, 4> SpaceToDepthNhwcQSymm16Test( |
| 235 | armnn::IWorkloadFactory& workloadFactory, |
| 236 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager) |
| 237 | { |
| 238 | return SpaceToDepthSimpleTest2<armnn::DataType::QuantisedSymm16>( |
| 239 | workloadFactory, |
| 240 | memoryManager); |
| 241 | } |
| 242 | |
| 243 | LayerTestResult<int16_t, 4> SpaceToDepthNchwQSymm16Test( |
| 244 | armnn::IWorkloadFactory& workloadFactory, |
| 245 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager) |
| 246 | { |
| 247 | return SpaceToDepthSimpleTest2<armnn::DataType::QuantisedSymm16>( |
| 248 | workloadFactory, |
| 249 | memoryManager, |
| 250 | armnn::DataLayout::NCHW); |
| 251 | } |