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