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