Aron Virginas-Tar | 00d306e | 2019-08-28 18:08:46 +0100 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2017 Arm Ltd. All rights reserved. |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #include "ReshapeTestImpl.hpp" |
| 7 | |
| 8 | #include <armnn/ArmNN.hpp> |
| 9 | |
| 10 | #include <backendsCommon/test/DataTypeUtils.hpp> |
| 11 | #include <backendsCommon/test/TensorCopyUtils.hpp> |
| 12 | #include <backendsCommon/test/WorkloadTestUtils.hpp> |
| 13 | |
| 14 | #include <test/TensorHelpers.hpp> |
| 15 | |
| 16 | namespace |
| 17 | { |
| 18 | |
| 19 | template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>> |
| 20 | LayerTestResult<T, 2> Rsqrt2dTestCommon( |
| 21 | armnn::IWorkloadFactory& workloadFactory, |
| 22 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 23 | const armnn::TensorInfo inputTensorInfo, |
| 24 | const armnn::TensorInfo outputTensorInfo, |
| 25 | const std::vector<float>& inputValues, |
| 26 | const std::vector<float>& expectedOutputValues) |
| 27 | { |
Derek Lamberti | c374ff0 | 2019-12-10 21:57:35 +0000 | [diff] [blame] | 28 | boost::ignore_unused(memoryManager); |
Aron Virginas-Tar | 00d306e | 2019-08-28 18:08:46 +0100 | [diff] [blame] | 29 | auto inputTensor = MakeTensor<T, 2>(inputTensorInfo, ConvertToDataType<ArmnnType>(inputValues,inputTensorInfo)); |
| 30 | |
| 31 | LayerTestResult<T, 2> result(outputTensorInfo); |
| 32 | |
| 33 | result.outputExpected = MakeTensor<T, 2>(outputTensorInfo, |
| 34 | ConvertToDataType<ArmnnType>(expectedOutputValues,outputTensorInfo)); |
| 35 | |
| 36 | std::unique_ptr<armnn::ITensorHandle> inputHandle = workloadFactory.CreateTensorHandle(inputTensorInfo); |
| 37 | std::unique_ptr<armnn::ITensorHandle> outputHandle = workloadFactory.CreateTensorHandle(outputTensorInfo); |
| 38 | |
| 39 | armnn::RsqrtQueueDescriptor descriptor; |
| 40 | |
| 41 | armnn::WorkloadInfo info; |
| 42 | |
| 43 | AddInputToWorkload(descriptor, info, inputTensorInfo, inputHandle.get()); |
| 44 | AddOutputToWorkload(descriptor, info, outputTensorInfo, outputHandle.get()); |
| 45 | |
| 46 | std::unique_ptr<armnn::IWorkload> workload = workloadFactory.CreateRsqrt(descriptor, info); |
| 47 | |
| 48 | inputHandle->Allocate(); |
| 49 | outputHandle->Allocate(); |
| 50 | |
| 51 | CopyDataToITensorHandle(inputHandle.get(), &inputTensor[0][0]); |
| 52 | |
| 53 | workload->PostAllocationConfigure(); |
| 54 | workload->Execute(); |
| 55 | |
| 56 | CopyDataFromITensorHandle(&result.output[0][0], outputHandle.get()); |
| 57 | |
| 58 | return result; |
| 59 | } |
| 60 | |
| 61 | } // anonymous namespace |
| 62 | |
| 63 | template<armnn::DataType ArmnnType, typename T> |
| 64 | LayerTestResult<T, 2> Rsqrt2dTest( |
| 65 | armnn::IWorkloadFactory& workloadFactory, |
| 66 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager) |
| 67 | { |
| 68 | const armnn::TensorShape inputShape{ 2, 2 }; |
| 69 | const armnn::TensorShape outputShape{ 2, 2 }; |
| 70 | |
| 71 | armnn::TensorInfo inputTensorInfo(inputShape, ArmnnType); |
| 72 | inputTensorInfo.SetQuantizationScale(0.1f); |
| 73 | inputTensorInfo.SetQuantizationOffset(0); |
| 74 | |
| 75 | armnn::TensorInfo outputTensorInfo(outputShape, ArmnnType); |
| 76 | outputTensorInfo.SetQuantizationScale(0.1f); |
| 77 | outputTensorInfo.SetQuantizationOffset(0); |
| 78 | |
| 79 | std::vector<float> inputValues |
| 80 | { |
| 81 | 1.f, 4.f, |
| 82 | 16.f, 25.f |
| 83 | }; |
| 84 | |
| 85 | std::vector<float> expectedOutputValues |
| 86 | { |
| 87 | 1.f, 0.5f, |
| 88 | 0.25f, 0.2f |
| 89 | }; |
| 90 | |
| 91 | return Rsqrt2dTestCommon<ArmnnType>(workloadFactory, memoryManager, |
| 92 | inputTensorInfo, outputTensorInfo, |
| 93 | inputValues, expectedOutputValues); |
| 94 | } |
| 95 | |
| 96 | template<armnn::DataType ArmnnType, typename T> |
| 97 | LayerTestResult<T, 3> Rsqrt3dTest( |
| 98 | armnn::IWorkloadFactory& workloadFactory, |
| 99 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager) |
| 100 | { |
Derek Lamberti | c374ff0 | 2019-12-10 21:57:35 +0000 | [diff] [blame] | 101 | boost::ignore_unused(memoryManager); |
Aron Virginas-Tar | 00d306e | 2019-08-28 18:08:46 +0100 | [diff] [blame] | 102 | const armnn::TensorShape inputShape{ 3, 1, 2 }; |
| 103 | const armnn::TensorShape outputShape{ 3, 1, 2 }; |
| 104 | |
| 105 | armnn::TensorInfo inputTensorInfo(inputShape, ArmnnType); |
| 106 | inputTensorInfo.SetQuantizationScale(0.1f); |
| 107 | inputTensorInfo.SetQuantizationOffset(0); |
| 108 | |
| 109 | armnn::TensorInfo outputTensorInfo(outputShape, ArmnnType); |
| 110 | outputTensorInfo.SetQuantizationScale(0.1f); |
| 111 | outputTensorInfo.SetQuantizationOffset(0); |
| 112 | |
| 113 | std::vector<float> inputValues |
| 114 | { |
| 115 | 1.f, 4.f, 16.f, |
| 116 | 25.f, 64.f, 100.f |
| 117 | }; |
| 118 | |
| 119 | std::vector<float> expectedOutputValues |
| 120 | { |
| 121 | 1.f, 0.5f, 0.25f, |
| 122 | 0.2f, 0.125f, 0.1f |
| 123 | }; |
| 124 | |
| 125 | auto inputTensor = MakeTensor<T, 3>(inputTensorInfo, ConvertToDataType<ArmnnType>(inputValues,inputTensorInfo)); |
| 126 | |
| 127 | LayerTestResult<T, 3> result(outputTensorInfo); |
| 128 | result.outputExpected = MakeTensor<T, 3>(outputTensorInfo, |
| 129 | ConvertToDataType<ArmnnType>(expectedOutputValues,outputTensorInfo)); |
| 130 | |
| 131 | std::unique_ptr<armnn::ITensorHandle> inputHandle = workloadFactory.CreateTensorHandle(inputTensorInfo); |
| 132 | std::unique_ptr<armnn::ITensorHandle> outputHandle = workloadFactory.CreateTensorHandle(outputTensorInfo); |
| 133 | |
| 134 | armnn::RsqrtQueueDescriptor descriptor; |
| 135 | |
| 136 | armnn::WorkloadInfo info; |
| 137 | |
| 138 | AddInputToWorkload(descriptor, info, inputTensorInfo, inputHandle.get()); |
| 139 | AddOutputToWorkload(descriptor, info, outputTensorInfo, outputHandle.get()); |
| 140 | |
| 141 | std::unique_ptr<armnn::IWorkload> workload = workloadFactory.CreateRsqrt(descriptor, info); |
| 142 | |
| 143 | inputHandle->Allocate(); |
| 144 | outputHandle->Allocate(); |
| 145 | |
| 146 | CopyDataToITensorHandle(inputHandle.get(), &inputTensor[0][0][0]); |
| 147 | |
| 148 | workload->PostAllocationConfigure(); |
| 149 | workload->Execute(); |
| 150 | |
| 151 | CopyDataFromITensorHandle(&result.output[0][0][0], outputHandle.get()); |
| 152 | |
| 153 | return result; |
| 154 | } |
| 155 | |
| 156 | template<armnn::DataType ArmnnType, typename T> |
| 157 | LayerTestResult<T, 2> RsqrtZeroTest( |
| 158 | armnn::IWorkloadFactory& workloadFactory, |
| 159 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager) |
| 160 | { |
| 161 | const armnn::TensorShape inputShape{ 1, 2 }; |
| 162 | const armnn::TensorShape outputShape{ 1, 2 }; |
| 163 | |
| 164 | armnn::TensorInfo inputTensorInfo(inputShape, ArmnnType); |
| 165 | inputTensorInfo.SetQuantizationScale(0.1f); |
| 166 | |
| 167 | armnn::TensorInfo outputTensorInfo(outputShape, ArmnnType); |
| 168 | outputTensorInfo.SetQuantizationScale(0.1f); |
| 169 | |
| 170 | std::vector<float> inputValues |
| 171 | { |
| 172 | 0.f, -0.f |
| 173 | }; |
| 174 | |
| 175 | std::vector<float> expectedOutputValues |
| 176 | { |
| 177 | INFINITY, -INFINITY |
| 178 | }; |
| 179 | |
| 180 | return Rsqrt2dTestCommon<ArmnnType>(workloadFactory, memoryManager, |
| 181 | inputTensorInfo, outputTensorInfo, |
| 182 | inputValues, expectedOutputValues); |
| 183 | } |
| 184 | |
| 185 | template<armnn::DataType ArmnnType, typename T> |
| 186 | LayerTestResult<T, 2> RsqrtNegativeTest( |
| 187 | armnn::IWorkloadFactory& workloadFactory, |
| 188 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager) |
| 189 | { |
| 190 | const armnn::TensorShape inputShape{ 1, 2 }; |
| 191 | const armnn::TensorShape outputShape{ 1, 2 }; |
| 192 | |
| 193 | armnn::TensorInfo inputTensorInfo(inputShape, ArmnnType); |
| 194 | inputTensorInfo.SetQuantizationScale(0.1f); |
| 195 | inputTensorInfo.SetQuantizationOffset(0); |
| 196 | |
| 197 | armnn::TensorInfo outputTensorInfo(outputShape, ArmnnType); |
| 198 | outputTensorInfo.SetQuantizationScale(0.1f); |
| 199 | outputTensorInfo.SetQuantizationOffset(0); |
| 200 | |
| 201 | std::vector<float> inputValues |
| 202 | { |
| 203 | -25.f, -16.f |
| 204 | }; |
| 205 | |
| 206 | std::vector<float> expectedOutputValues |
| 207 | { |
| 208 | -NAN, -NAN |
| 209 | }; |
| 210 | |
| 211 | return Rsqrt2dTestCommon<ArmnnType>(workloadFactory, memoryManager, |
| 212 | inputTensorInfo, outputTensorInfo, |
| 213 | inputValues, expectedOutputValues); |
| 214 | } |
| 215 | |
| 216 | // |
| 217 | // Explicit template specializations |
| 218 | // |
| 219 | |
| 220 | template LayerTestResult<armnn::ResolveType<armnn::DataType::Float32>, 2> |
| 221 | Rsqrt2dTest<armnn::DataType::Float32>( |
| 222 | armnn::IWorkloadFactory& workloadFactory, |
| 223 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager); |
| 224 | |
Matthew Jackson | 9bff144 | 2019-09-12 09:08:23 +0100 | [diff] [blame] | 225 | template LayerTestResult<armnn::ResolveType<armnn::DataType::Float16>, 2> |
| 226 | Rsqrt2dTest<armnn::DataType::Float16>( |
| 227 | armnn::IWorkloadFactory& workloadFactory, |
| 228 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager); |
| 229 | |
Aron Virginas-Tar | 00d306e | 2019-08-28 18:08:46 +0100 | [diff] [blame] | 230 | template LayerTestResult<armnn::ResolveType<armnn::DataType::QuantisedAsymm8>, 2> |
| 231 | Rsqrt2dTest<armnn::DataType::QuantisedAsymm8>( |
| 232 | armnn::IWorkloadFactory& workloadFactory, |
| 233 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager); |
| 234 | |
| 235 | template LayerTestResult<armnn::ResolveType<armnn::DataType::QuantisedSymm16>, 2> |
| 236 | Rsqrt2dTest<armnn::DataType::QuantisedSymm16>( |
| 237 | armnn::IWorkloadFactory& workloadFactory, |
| 238 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager); |
| 239 | |
| 240 | template LayerTestResult<armnn::ResolveType<armnn::DataType::Float32>, 3> |
| 241 | Rsqrt3dTest<armnn::DataType::Float32>( |
| 242 | armnn::IWorkloadFactory& workloadFactory, |
| 243 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager); |
| 244 | |
Matthew Jackson | 9bff144 | 2019-09-12 09:08:23 +0100 | [diff] [blame] | 245 | template LayerTestResult<armnn::ResolveType<armnn::DataType::Float16>, 3> |
| 246 | Rsqrt3dTest<armnn::DataType::Float16>( |
| 247 | armnn::IWorkloadFactory& workloadFactory, |
| 248 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager); |
| 249 | |
Aron Virginas-Tar | 00d306e | 2019-08-28 18:08:46 +0100 | [diff] [blame] | 250 | template LayerTestResult<armnn::ResolveType<armnn::DataType::QuantisedAsymm8>, 3> |
| 251 | Rsqrt3dTest<armnn::DataType::QuantisedAsymm8>( |
| 252 | armnn::IWorkloadFactory& workloadFactory, |
| 253 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager); |
| 254 | |
| 255 | template LayerTestResult<armnn::ResolveType<armnn::DataType::QuantisedSymm16>, 3> |
| 256 | Rsqrt3dTest<armnn::DataType::QuantisedSymm16>( |
| 257 | armnn::IWorkloadFactory& workloadFactory, |
| 258 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager); |
| 259 | |
| 260 | template LayerTestResult<armnn::ResolveType<armnn::DataType::Float32>, 2> |
| 261 | RsqrtZeroTest<armnn::DataType::Float32>( |
| 262 | armnn::IWorkloadFactory& workloadFactory, |
| 263 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager); |
| 264 | |
| 265 | template LayerTestResult<armnn::ResolveType<armnn::DataType::Float32>, 2> |
| 266 | RsqrtNegativeTest<armnn::DataType::Float32>( |
| 267 | armnn::IWorkloadFactory& workloadFactory, |
| 268 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager); |