Sadik Armagan | 9199e58 | 2019-09-05 17:35:31 +0100 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2017 Arm Ltd. All rights reserved. |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #include "AbsTestImpl.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> Abs2dTestCommon( |
| 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::AbsQueueDescriptor 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.CreateAbs(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> Abs2dTest( |
| 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 | float qScale = 0.0625f; |
| 71 | int32_t qOffset = 64; |
| 72 | |
| 73 | if (ArmnnType == armnn::DataType::QuantisedSymm16) |
| 74 | { |
| 75 | qScale = 0.1f; |
| 76 | qOffset = 0; |
| 77 | } |
| 78 | |
| 79 | armnn::TensorInfo inputTensorInfo(inputShape, ArmnnType); |
| 80 | inputTensorInfo.SetQuantizationScale(qScale); |
| 81 | inputTensorInfo.SetQuantizationOffset(qOffset); |
| 82 | |
| 83 | armnn::TensorInfo outputTensorInfo(outputShape, ArmnnType); |
| 84 | outputTensorInfo.SetQuantizationScale(qScale); |
| 85 | outputTensorInfo.SetQuantizationOffset(qOffset); |
| 86 | |
| 87 | std::vector<float> inputValues |
| 88 | { |
| 89 | -0.1f, 0.2f, |
| 90 | 0.3f, -0.4f |
| 91 | }; |
| 92 | |
| 93 | // Calculate output values for input. |
| 94 | auto f = [](float value) |
| 95 | { |
| 96 | return std::abs(value); |
| 97 | }; |
| 98 | std::vector<float> expectedOutputValues(inputValues.size()); |
| 99 | std::transform(inputValues.begin(), inputValues.end(), expectedOutputValues.begin(), f); |
| 100 | |
| 101 | return Abs2dTestCommon<ArmnnType>(workloadFactory, memoryManager, |
| 102 | inputTensorInfo, outputTensorInfo, |
| 103 | inputValues, expectedOutputValues); |
| 104 | } |
| 105 | |
| 106 | template<armnn::DataType ArmnnType, typename T> |
| 107 | LayerTestResult<T, 3> Abs3dTest( |
| 108 | armnn::IWorkloadFactory& workloadFactory, |
| 109 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager) |
| 110 | { |
| 111 | const armnn::TensorShape inputShape{ 3, 1, 2 }; |
| 112 | const armnn::TensorShape outputShape{ 3, 1, 2 }; |
| 113 | |
| 114 | float qScale = 0.0625f; |
| 115 | int32_t qOffset = 64; |
| 116 | |
| 117 | if (ArmnnType == armnn::DataType::QuantisedSymm16) |
| 118 | { |
| 119 | qScale = 0.1f; |
| 120 | qOffset = 0; |
| 121 | } |
| 122 | |
| 123 | armnn::TensorInfo inputTensorInfo(inputShape, ArmnnType); |
| 124 | inputTensorInfo.SetQuantizationScale(qScale); |
| 125 | inputTensorInfo.SetQuantizationOffset(qOffset); |
| 126 | |
| 127 | armnn::TensorInfo outputTensorInfo(outputShape, ArmnnType); |
| 128 | outputTensorInfo.SetQuantizationScale(qScale); |
| 129 | outputTensorInfo.SetQuantizationOffset(qOffset); |
| 130 | |
| 131 | std::vector<float> inputValues |
| 132 | { |
| 133 | -0.1f, -0.2f, -0.3f, |
| 134 | 0.1f, 0.2f, 0.3f |
| 135 | }; |
| 136 | |
| 137 | auto f = [](float value) |
| 138 | { |
| 139 | return std::abs(value); |
| 140 | }; |
| 141 | std::vector<float>expectedOutputValues(inputValues.size()); |
| 142 | std::transform(inputValues.begin(), inputValues.end(), expectedOutputValues.begin(), f); |
| 143 | |
| 144 | auto inputTensor = MakeTensor<T, 3>(inputTensorInfo, ConvertToDataType<ArmnnType>(inputValues,inputTensorInfo)); |
| 145 | |
| 146 | LayerTestResult<T, 3> result(outputTensorInfo); |
| 147 | result.outputExpected = MakeTensor<T, 3>(outputTensorInfo, |
| 148 | ConvertToDataType<ArmnnType>(expectedOutputValues,outputTensorInfo)); |
| 149 | |
| 150 | std::unique_ptr<armnn::ITensorHandle> inputHandle = workloadFactory.CreateTensorHandle(inputTensorInfo); |
| 151 | std::unique_ptr<armnn::ITensorHandle> outputHandle = workloadFactory.CreateTensorHandle(outputTensorInfo); |
| 152 | |
| 153 | armnn::AbsQueueDescriptor descriptor; |
| 154 | |
| 155 | armnn::WorkloadInfo info; |
| 156 | |
| 157 | AddInputToWorkload(descriptor, info, inputTensorInfo, inputHandle.get()); |
| 158 | AddOutputToWorkload(descriptor, info, outputTensorInfo, outputHandle.get()); |
| 159 | |
| 160 | std::unique_ptr<armnn::IWorkload> workload = workloadFactory.CreateAbs(descriptor, info); |
| 161 | |
| 162 | inputHandle->Allocate(); |
| 163 | outputHandle->Allocate(); |
| 164 | |
| 165 | CopyDataToITensorHandle(inputHandle.get(), &inputTensor[0][0][0]); |
| 166 | |
| 167 | workload->PostAllocationConfigure(); |
| 168 | workload->Execute(); |
| 169 | |
| 170 | CopyDataFromITensorHandle(&result.output[0][0][0], outputHandle.get()); |
| 171 | |
| 172 | return result; |
| 173 | } |
| 174 | |
| 175 | template<armnn::DataType ArmnnType, typename T> |
| 176 | LayerTestResult<T, 2> AbsZeroTest( |
| 177 | armnn::IWorkloadFactory& workloadFactory, |
| 178 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager) |
| 179 | { |
| 180 | const armnn::TensorShape inputShape{ 1, 2 }; |
| 181 | const armnn::TensorShape outputShape{ 1, 2 }; |
| 182 | |
| 183 | armnn::TensorInfo inputTensorInfo(inputShape, ArmnnType); |
| 184 | inputTensorInfo.SetQuantizationScale(0.1f); |
| 185 | |
| 186 | armnn::TensorInfo outputTensorInfo(outputShape, ArmnnType); |
| 187 | outputTensorInfo.SetQuantizationScale(0.1f); |
| 188 | |
| 189 | std::vector<float> inputValues |
| 190 | { |
| 191 | 0.f, -0.f |
| 192 | }; |
| 193 | |
| 194 | std::vector<float> expectedOutputValues |
| 195 | { |
| 196 | 0.f, 0.f |
| 197 | }; |
| 198 | |
| 199 | return Abs2dTestCommon<ArmnnType>(workloadFactory, memoryManager, |
| 200 | inputTensorInfo, outputTensorInfo, |
| 201 | inputValues, expectedOutputValues); |
| 202 | } |
| 203 | |
| 204 | // |
| 205 | // Explicit template specializations |
| 206 | // |
| 207 | |
| 208 | template LayerTestResult<armnn::ResolveType<armnn::DataType::Float32>, 2> |
| 209 | Abs2dTest<armnn::DataType::Float32>( |
| 210 | armnn::IWorkloadFactory& workloadFactory, |
| 211 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager); |
| 212 | |
Kevin May | b6482f3 | 2019-09-11 12:31:38 +0100 | [diff] [blame] | 213 | template LayerTestResult<armnn::ResolveType<armnn::DataType::Float16>, 2> |
| 214 | Abs2dTest<armnn::DataType::Float16>( |
| 215 | armnn::IWorkloadFactory& workloadFactory, |
| 216 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager); |
| 217 | |
Sadik Armagan | 9199e58 | 2019-09-05 17:35:31 +0100 | [diff] [blame] | 218 | template LayerTestResult<armnn::ResolveType<armnn::DataType::QuantisedAsymm8>, 2> |
| 219 | Abs2dTest<armnn::DataType::QuantisedAsymm8>( |
| 220 | armnn::IWorkloadFactory& workloadFactory, |
| 221 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager); |
| 222 | |
| 223 | template LayerTestResult<armnn::ResolveType<armnn::DataType::QuantisedSymm16>, 2> |
| 224 | Abs2dTest<armnn::DataType::QuantisedSymm16>( |
| 225 | armnn::IWorkloadFactory& workloadFactory, |
| 226 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager); |
| 227 | |
| 228 | template LayerTestResult<armnn::ResolveType<armnn::DataType::Float32>, 3> |
| 229 | Abs3dTest<armnn::DataType::Float32>( |
| 230 | armnn::IWorkloadFactory& workloadFactory, |
| 231 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager); |
| 232 | |
Kevin May | b6482f3 | 2019-09-11 12:31:38 +0100 | [diff] [blame] | 233 | template LayerTestResult<armnn::ResolveType<armnn::DataType::Float16>, 3> |
| 234 | Abs3dTest<armnn::DataType::Float16>( |
| 235 | armnn::IWorkloadFactory& workloadFactory, |
| 236 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager); |
| 237 | |
Sadik Armagan | 9199e58 | 2019-09-05 17:35:31 +0100 | [diff] [blame] | 238 | template LayerTestResult<armnn::ResolveType<armnn::DataType::QuantisedAsymm8>, 3> |
| 239 | Abs3dTest<armnn::DataType::QuantisedAsymm8>( |
| 240 | armnn::IWorkloadFactory& workloadFactory, |
| 241 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager); |
| 242 | |
| 243 | template LayerTestResult<armnn::ResolveType<armnn::DataType::QuantisedSymm16>, 3> |
| 244 | Abs3dTest<armnn::DataType::QuantisedSymm16>( |
| 245 | armnn::IWorkloadFactory& workloadFactory, |
| 246 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager); |
| 247 | |
| 248 | template LayerTestResult<armnn::ResolveType<armnn::DataType::Float32>, 2> |
| 249 | AbsZeroTest<armnn::DataType::Float32>( |
| 250 | armnn::IWorkloadFactory& workloadFactory, |
Kevin May | b6482f3 | 2019-09-11 12:31:38 +0100 | [diff] [blame] | 251 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager); |
| 252 | |
| 253 | template LayerTestResult<armnn::ResolveType<armnn::DataType::Float16>, 2> |
| 254 | AbsZeroTest<armnn::DataType::Float16>( |
| 255 | armnn::IWorkloadFactory& workloadFactory, |
Sadik Armagan | 9199e58 | 2019-09-05 17:35:31 +0100 | [diff] [blame] | 256 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager); |