telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1 | // |
Teresa Charlin | fbf0e5b | 2020-08-17 01:01:06 +0100 | [diff] [blame] | 2 | // Copyright © 2017 Arm Ltd and Contributors. All rights reserved. |
David Beck | ecb56cd | 2018-09-05 12:52:57 +0100 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 4 | // |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 5 | |
Aron Virginas-Tar | 00d306e | 2019-08-28 18:08:46 +0100 | [diff] [blame] | 6 | #include "ActivationTestImpl.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> |
Aron Virginas-Tar | c9cc804 | 2018-11-01 16:15:57 +0000 | [diff] [blame] | 10 | |
Aron Virginas-Tar | 00d306e | 2019-08-28 18:08:46 +0100 | [diff] [blame] | 11 | #include <backendsCommon/test/ActivationFixture.hpp> |
Aron Virginas-Tar | 00d306e | 2019-08-28 18:08:46 +0100 | [diff] [blame] | 12 | #include <backendsCommon/test/TensorCopyUtils.hpp> |
| 13 | #include <backendsCommon/test/WorkloadTestUtils.hpp> |
Keith Davis | 33a626f | 2020-08-27 15:38:12 +0100 | [diff] [blame] | 14 | #include <reference/test/RefWorkloadFactoryHelper.hpp> |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 15 | |
Aron Virginas-Tar | c9cc804 | 2018-11-01 16:15:57 +0000 | [diff] [blame] | 16 | #include <test/TensorHelpers.hpp> |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 17 | |
Aron Virginas-Tar | 00d306e | 2019-08-28 18:08:46 +0100 | [diff] [blame] | 18 | #include <boost/multi_array.hpp> |
| 19 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 20 | #include <algorithm> |
| 21 | |
Nattapat Chaimanowong | 649dd95 | 2019-01-22 16:10:44 +0000 | [diff] [blame] | 22 | template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>> |
Aron Virginas-Tar | 5caf907 | 2018-11-14 18:35:18 +0000 | [diff] [blame] | 23 | LayerTestResult<T, 4> BoundedReLuTestCommon( |
| 24 | armnn::IWorkloadFactory& workloadFactory, |
| 25 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
Keith Davis | 33a626f | 2020-08-27 15:38:12 +0100 | [diff] [blame] | 26 | const armnn::ITensorHandleFactory& tensorHandleFactory, |
Aron Virginas-Tar | 5caf907 | 2018-11-14 18:35:18 +0000 | [diff] [blame] | 27 | float upperBound, |
| 28 | float lowerBound, |
| 29 | float inputScale, |
| 30 | int32_t inputOffset, |
| 31 | float outputScale, |
| 32 | int32_t outputOffset, |
| 33 | const std::vector<T>& inputData, |
| 34 | const std::vector<T>& outputExpectedData, |
| 35 | unsigned int inputWidth, |
| 36 | unsigned int inputHeight, |
| 37 | unsigned int inputChannels, |
| 38 | unsigned int inputBatchSize) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 39 | { |
Jan Eilers | 8eb2560 | 2020-03-09 12:13:48 +0000 | [diff] [blame] | 40 | IgnoreUnused(memoryManager); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 41 | unsigned int outputWidth = inputWidth; |
| 42 | unsigned int outputHeight = inputHeight; |
| 43 | unsigned int outputChannels = inputChannels; |
| 44 | unsigned int outputBatchSize = inputBatchSize; |
| 45 | |
Nattapat Chaimanowong | 649dd95 | 2019-01-22 16:10:44 +0000 | [diff] [blame] | 46 | armnn::TensorInfo inputTensorInfo({ inputBatchSize, inputChannels, inputHeight, inputWidth }, ArmnnType); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 47 | |
Nattapat Chaimanowong | 649dd95 | 2019-01-22 16:10:44 +0000 | [diff] [blame] | 48 | armnn::TensorInfo outputTensorInfo({ outputBatchSize, outputChannels, outputHeight, outputWidth }, ArmnnType); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 49 | |
| 50 | if(armnn::IsQuantizedType<T>()) |
| 51 | { |
| 52 | inputTensorInfo.SetQuantizationScale(inputScale); |
| 53 | inputTensorInfo.SetQuantizationOffset(inputOffset); |
| 54 | |
| 55 | outputTensorInfo.SetQuantizationScale(outputScale); |
| 56 | outputTensorInfo.SetQuantizationOffset(outputOffset); |
| 57 | } |
| 58 | |
| 59 | LayerTestResult<T, 4> result(inputTensorInfo); |
| 60 | |
| 61 | auto input = MakeTensor<T, 4>(inputTensorInfo, inputData); |
| 62 | |
Keith Davis | 33a626f | 2020-08-27 15:38:12 +0100 | [diff] [blame] | 63 | std::unique_ptr<armnn::ITensorHandle> inputHandle = tensorHandleFactory.CreateTensorHandle(inputTensorInfo); |
| 64 | std::unique_ptr<armnn::ITensorHandle> outputHandle = tensorHandleFactory.CreateTensorHandle(outputTensorInfo); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 65 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 66 | // Setup bounded ReLu. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 67 | armnn::ActivationQueueDescriptor descriptor; |
| 68 | armnn::WorkloadInfo workloadInfo; |
| 69 | AddInputToWorkload(descriptor, workloadInfo, inputTensorInfo, inputHandle.get()); |
| 70 | AddOutputToWorkload(descriptor, workloadInfo, outputTensorInfo, outputHandle.get()); |
| 71 | |
| 72 | descriptor.m_Parameters.m_Function = armnn::ActivationFunction::BoundedReLu; |
| 73 | descriptor.m_Parameters.m_A = upperBound; |
| 74 | descriptor.m_Parameters.m_B = lowerBound; |
| 75 | |
| 76 | std::unique_ptr<armnn::IWorkload> workload = workloadFactory.CreateActivation(descriptor, workloadInfo); |
| 77 | |
| 78 | inputHandle->Allocate(); |
| 79 | outputHandle->Allocate(); |
| 80 | |
| 81 | CopyDataToITensorHandle(inputHandle.get(), &input[0][0][0][0]); |
| 82 | |
| 83 | workload->Execute(); |
| 84 | |
| 85 | CopyDataFromITensorHandle(&result.output[0][0][0][0], outputHandle.get()); |
| 86 | |
| 87 | result.outputExpected = MakeTensor<T, 4>(outputTensorInfo, outputExpectedData); |
| 88 | |
| 89 | return result; |
| 90 | } |
| 91 | |
Aron Virginas-Tar | 5caf907 | 2018-11-14 18:35:18 +0000 | [diff] [blame] | 92 | LayerTestResult<float, 4> BoundedReLuUpperAndLowerBoundTest( |
| 93 | armnn::IWorkloadFactory& workloadFactory, |
Keith Davis | 33a626f | 2020-08-27 15:38:12 +0100 | [diff] [blame] | 94 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 95 | const armnn::ITensorHandleFactory& tensorHandleFactory) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 96 | { |
| 97 | unsigned int inputWidth = 4u; |
| 98 | unsigned int inputHeight = 5u; |
| 99 | unsigned int inputChannels = 1u; |
| 100 | unsigned int inputBatchSize = 1; |
| 101 | |
| 102 | std::vector<float> input = std::vector<float>{ |
| 103 | -2.0f, 0.1f, 0.5f, 1.25f, |
| 104 | 0.786f, 0.9875f, -1.5f, 0.384f, |
| 105 | 1.0001f, 3.5f, 7.5f, 0.896f, |
| 106 | 2.126f, 2.0f, 0.3f, 0.15f, |
| 107 | 0.999f, 1.2f, 0.89f, 6.1f, |
| 108 | }; |
| 109 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 110 | // Calculated manually. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 111 | std::vector<float> output = std::vector<float>{ |
| 112 | -1.0f, 0.1f, 0.5f, 1.0f, |
| 113 | 0.786f, 0.9875f, -1.0f, 0.384f, |
| 114 | 1.0f, 1.0f, 1.0f, 0.896f, |
| 115 | 1.0f, 1.0f, 0.3f, 0.15f, |
| 116 | 0.999f, 1.0f, 0.89f, 1.0f, |
| 117 | }; |
| 118 | |
Nattapat Chaimanowong | 649dd95 | 2019-01-22 16:10:44 +0000 | [diff] [blame] | 119 | return BoundedReLuTestCommon<armnn::DataType::Float32>( |
Keith Davis | 33a626f | 2020-08-27 15:38:12 +0100 | [diff] [blame] | 120 | workloadFactory, memoryManager, tensorHandleFactory, 1.0f, -1.0f, 1.0f, 0, 1.0f, 0, input, output, |
Aron Virginas-Tar | 5caf907 | 2018-11-14 18:35:18 +0000 | [diff] [blame] | 121 | inputWidth, inputHeight, inputChannels, inputBatchSize); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 122 | } |
| 123 | |
Aron Virginas-Tar | 5caf907 | 2018-11-14 18:35:18 +0000 | [diff] [blame] | 124 | LayerTestResult<float, 4> BoundedReLuUpperBoundOnlyTest( |
| 125 | armnn::IWorkloadFactory& workloadFactory, |
Keith Davis | 33a626f | 2020-08-27 15:38:12 +0100 | [diff] [blame] | 126 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 127 | const armnn::ITensorHandleFactory& tensorHandleFactory) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 128 | { |
| 129 | unsigned int inputWidth = 4u; |
| 130 | unsigned int inputHeight = 5u; |
| 131 | unsigned int inputChannels = 1u; |
| 132 | unsigned int inputBatchSize = 1; |
| 133 | |
| 134 | std::vector<float> input = std::vector<float>{ |
| 135 | -1.0f, 0.1f, 0.5f, 6.25f, |
| 136 | 0.786f, 5.9875f, -0.5f, 0.384f, |
| 137 | 6.0001f, 3.5f, 7.5f, 0.896f, |
| 138 | 2.126f, 12.0f, 0.3f, 0.15f, |
| 139 | 0.999f, 1.2f, 0.89f, 6.1f, |
| 140 | }; |
| 141 | |
David Beck | ac42efd | 2018-09-26 17:41:13 +0100 | [diff] [blame] | 142 | // Calculated manually. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 143 | std::vector<float> output = std::vector<float>{ |
| 144 | 0.0f, 0.1f, 0.5f, 6.0f, |
| 145 | 0.786f, 5.9875f, 0.0f, 0.384f, |
| 146 | 6.0f, 3.5f, 6.0f, 0.896f, |
| 147 | 2.126f, 6.0f, 0.3f, 0.15f, |
| 148 | 0.999f, 1.2f, 0.89f, 6.0f, |
| 149 | }; |
| 150 | |
Nattapat Chaimanowong | 649dd95 | 2019-01-22 16:10:44 +0000 | [diff] [blame] | 151 | return BoundedReLuTestCommon<armnn::DataType::Float32>( |
Keith Davis | 33a626f | 2020-08-27 15:38:12 +0100 | [diff] [blame] | 152 | workloadFactory, memoryManager, tensorHandleFactory, 6.0f, 0.0f, 1.0f, 0, 1.0f, 0, input, output, |
Aron Virginas-Tar | 5caf907 | 2018-11-14 18:35:18 +0000 | [diff] [blame] | 153 | inputWidth, inputHeight, inputChannels, inputBatchSize); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 154 | } |
| 155 | |
Aron Virginas-Tar | 5caf907 | 2018-11-14 18:35:18 +0000 | [diff] [blame] | 156 | LayerTestResult<uint8_t, 4> BoundedReLuUint8UpperBoundOnlyTest( |
| 157 | armnn::IWorkloadFactory& workloadFactory, |
Keith Davis | 33a626f | 2020-08-27 15:38:12 +0100 | [diff] [blame] | 158 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 159 | const armnn::ITensorHandleFactory& tensorHandleFactory) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 160 | { |
| 161 | unsigned int inputWidth = 3u; |
| 162 | unsigned int inputHeight = 2u; |
| 163 | unsigned int inputChannels = 1u; |
| 164 | unsigned int inputBatchSize = 1; |
| 165 | |
| 166 | std::vector<uint8_t> input = std::vector<uint8_t>{ |
| 167 | 51, 124, 28, |
| 168 | 251, 8, 92 |
| 169 | }; |
| 170 | |
David Beck | ac42efd | 2018-09-26 17:41:13 +0100 | [diff] [blame] | 171 | // Calculated manually. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 172 | std::vector<uint8_t> output = std::vector<uint8_t>{ |
| 173 | 0, 122, 0, |
| 174 | 255, 0, 58 |
| 175 | }; |
| 176 | |
| 177 | float inputScale = 12.0f / 255.0f; |
| 178 | int32_t inputOffset = 63; |
| 179 | float outputScale = 6.0f / 255.0f; |
| 180 | int32_t outputOffset = 0; |
| 181 | |
Derek Lamberti | f90c56d | 2020-01-10 17:14:08 +0000 | [diff] [blame] | 182 | return BoundedReLuTestCommon<armnn::DataType::QAsymmU8>( |
Keith Davis | 33a626f | 2020-08-27 15:38:12 +0100 | [diff] [blame] | 183 | workloadFactory, memoryManager, tensorHandleFactory, 6.0f, 0.0f, |
Nattapat Chaimanowong | 649dd95 | 2019-01-22 16:10:44 +0000 | [diff] [blame] | 184 | inputScale, inputOffset, outputScale, outputOffset, |
| 185 | input, output, inputWidth, inputHeight, inputChannels, inputBatchSize); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 186 | } |
| 187 | |
Aron Virginas-Tar | 5caf907 | 2018-11-14 18:35:18 +0000 | [diff] [blame] | 188 | LayerTestResult<uint8_t, 4> BoundedReLuUint8UpperAndLowerBoundTest( |
| 189 | armnn::IWorkloadFactory& workloadFactory, |
Keith Davis | 33a626f | 2020-08-27 15:38:12 +0100 | [diff] [blame] | 190 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 191 | const armnn::ITensorHandleFactory& tensorHandleFactory) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 192 | { |
| 193 | unsigned int inputWidth = 3u; |
| 194 | unsigned int inputHeight = 2u; |
| 195 | unsigned int inputChannels = 1u; |
| 196 | unsigned int inputBatchSize = 1; |
| 197 | |
| 198 | std::vector<uint8_t> input = std::vector<uint8_t>{ |
| 199 | 51, 230, 28, |
| 200 | 251, 8, 92 |
| 201 | }; |
| 202 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 203 | // Calculated manually. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 204 | std::vector<uint8_t> output = std::vector<uint8_t>{ |
| 205 | 51, 192, 32, |
| 206 | 192, 32, 92 |
| 207 | }; |
| 208 | |
| 209 | int32_t inputOffset = 112; |
| 210 | float inputScale = 0.0125f; |
| 211 | |
Derek Lamberti | f90c56d | 2020-01-10 17:14:08 +0000 | [diff] [blame] | 212 | return BoundedReLuTestCommon<armnn::DataType::QAsymmU8>( |
Keith Davis | 33a626f | 2020-08-27 15:38:12 +0100 | [diff] [blame] | 213 | workloadFactory, memoryManager, tensorHandleFactory, 1.0f, -1.0f, |
Nattapat Chaimanowong | 649dd95 | 2019-01-22 16:10:44 +0000 | [diff] [blame] | 214 | inputScale, inputOffset, inputScale, inputOffset, // Input/output scale & offset same. |
| 215 | input, output, inputWidth, inputHeight, inputChannels, inputBatchSize); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | namespace |
| 219 | { |
| 220 | |
| 221 | struct BoundedReLuRandomInputTestTraits |
| 222 | { |
| 223 | constexpr static unsigned int inputHeight = 31u; |
| 224 | constexpr static unsigned int inputWidth = 19u; |
| 225 | constexpr static unsigned int inputChannels = 4u; |
| 226 | constexpr static unsigned int inputBatchSize = 2; |
| 227 | |
| 228 | constexpr static unsigned int outputHeight = inputHeight; |
| 229 | constexpr static unsigned int outputWidth = inputWidth; |
| 230 | constexpr static unsigned int outputChannels = inputChannels; |
| 231 | constexpr static unsigned int outputBatchSize = inputBatchSize; |
| 232 | |
| 233 | static armnn::TensorInfo GetInputTensorInfo() |
| 234 | { |
| 235 | return armnn::TensorInfo({ inputBatchSize, inputChannels, inputHeight, inputWidth }, |
| 236 | armnn::DataType::Float32); |
| 237 | } |
| 238 | |
| 239 | static armnn::TensorInfo GetOutputTensorInfo() |
| 240 | { |
| 241 | return armnn::TensorInfo({ outputBatchSize, outputChannels, outputHeight, outputWidth }, |
| 242 | armnn::DataType::Float32); |
| 243 | } |
| 244 | }; |
| 245 | |
Aron Virginas-Tar | 5caf907 | 2018-11-14 18:35:18 +0000 | [diff] [blame] | 246 | boost::multi_array<float, 4> BoundedReLuRandomInputTest( |
| 247 | armnn::IWorkloadFactory& workloadFactory, |
| 248 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
Keith Davis | 33a626f | 2020-08-27 15:38:12 +0100 | [diff] [blame] | 249 | const armnn::ITensorHandleFactory& tensorHandleFactory, |
Aron Virginas-Tar | 5caf907 | 2018-11-14 18:35:18 +0000 | [diff] [blame] | 250 | float lowerBound, |
| 251 | float upperBound, |
| 252 | const armnn::ActivationDescriptor& activationDescriptor) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 253 | { |
Jan Eilers | 8eb2560 | 2020-03-09 12:13:48 +0000 | [diff] [blame] | 254 | IgnoreUnused(memoryManager); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 255 | const armnn::TensorInfo inputTensorInfo = BoundedReLuRandomInputTestTraits::GetInputTensorInfo(); |
| 256 | const armnn::TensorInfo outputTensorInfo = BoundedReLuRandomInputTestTraits::GetOutputTensorInfo(); |
| 257 | |
| 258 | boost::multi_array<float, 4> output(GetTensorShapeAsArray<4>(outputTensorInfo)); |
| 259 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 260 | // Min/max random values passed to MakeRandomTensor are purposely outside of the ReLu |
| 261 | // range [lowerBound, upperBound]. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 262 | auto input = MakeRandomTensor<float, 4>(inputTensorInfo, 4605828, lowerBound - 5.0f, upperBound * 2.0f); |
| 263 | |
Keith Davis | 33a626f | 2020-08-27 15:38:12 +0100 | [diff] [blame] | 264 | std::unique_ptr<armnn::ITensorHandle> inputHandle = tensorHandleFactory.CreateTensorHandle(inputTensorInfo); |
| 265 | std::unique_ptr<armnn::ITensorHandle> outputHandle = tensorHandleFactory.CreateTensorHandle(outputTensorInfo); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 266 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 267 | // Set up bounded ReLu. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 268 | armnn::ActivationQueueDescriptor descriptor; |
| 269 | armnn::WorkloadInfo workloadInfo; |
| 270 | AddInputToWorkload(descriptor, workloadInfo, inputTensorInfo, inputHandle.get()); |
| 271 | AddOutputToWorkload(descriptor, workloadInfo, outputTensorInfo, outputHandle.get()); |
| 272 | descriptor.m_Parameters = activationDescriptor; |
| 273 | |
| 274 | std::unique_ptr<armnn::IWorkload> workload = workloadFactory.CreateActivation(descriptor, workloadInfo); |
| 275 | |
| 276 | inputHandle->Allocate(); |
| 277 | outputHandle->Allocate(); |
| 278 | |
| 279 | CopyDataToITensorHandle(inputHandle.get(), &input[0][0][0][0]); |
| 280 | |
| 281 | workload->Execute(); |
| 282 | |
| 283 | CopyDataFromITensorHandle(&output[0][0][0][0], outputHandle.get()); |
| 284 | |
| 285 | return output; |
| 286 | } |
| 287 | |
| 288 | } // namespace |
| 289 | |
Aron Virginas-Tar | 5caf907 | 2018-11-14 18:35:18 +0000 | [diff] [blame] | 290 | LayerTestResult<float, 4> CompareBoundedReLuTest( |
| 291 | armnn::IWorkloadFactory& workloadFactory, |
| 292 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 293 | armnn::IWorkloadFactory& refWorkloadFactory, |
Keith Davis | 33a626f | 2020-08-27 15:38:12 +0100 | [diff] [blame] | 294 | const armnn::ITensorHandleFactory& tensorHandleFactory, |
| 295 | const armnn::ITensorHandleFactory& refTensorHandleFactory, |
Aron Virginas-Tar | 5caf907 | 2018-11-14 18:35:18 +0000 | [diff] [blame] | 296 | float upperBound, |
| 297 | float lowerBound) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 298 | { |
| 299 | LayerTestResult<float, 4> result(BoundedReLuRandomInputTestTraits::GetOutputTensorInfo()); |
| 300 | |
| 301 | armnn::ActivationDescriptor activationDescriptor; |
| 302 | activationDescriptor.m_Function = armnn::ActivationFunction::BoundedReLu; |
| 303 | activationDescriptor.m_A = upperBound; |
| 304 | activationDescriptor.m_B = lowerBound; |
| 305 | |
Aron Virginas-Tar | 5caf907 | 2018-11-14 18:35:18 +0000 | [diff] [blame] | 306 | result.output = BoundedReLuRandomInputTest( |
Keith Davis | 33a626f | 2020-08-27 15:38:12 +0100 | [diff] [blame] | 307 | workloadFactory, memoryManager, tensorHandleFactory, 0.0f, upperBound, activationDescriptor); |
Aron Virginas-Tar | 5caf907 | 2018-11-14 18:35:18 +0000 | [diff] [blame] | 308 | result.outputExpected = BoundedReLuRandomInputTest( |
Keith Davis | 33a626f | 2020-08-27 15:38:12 +0100 | [diff] [blame] | 309 | refWorkloadFactory, nullptr, refTensorHandleFactory, 0.0f, upperBound, activationDescriptor); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 310 | |
| 311 | return result; |
| 312 | } |
| 313 | |
Nattapat Chaimanowong | 649dd95 | 2019-01-22 16:10:44 +0000 | [diff] [blame] | 314 | template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>> |
Aron Virginas-Tar | 5caf907 | 2018-11-14 18:35:18 +0000 | [diff] [blame] | 315 | LayerTestResult<T,4> ConstantLinearActivationTestCommon( |
| 316 | armnn::IWorkloadFactory& workloadFactory, |
| 317 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
Keith Davis | 33a626f | 2020-08-27 15:38:12 +0100 | [diff] [blame] | 318 | const armnn::ITensorHandleFactory& tensorHandleFactory, |
Aron Virginas-Tar | 5caf907 | 2018-11-14 18:35:18 +0000 | [diff] [blame] | 319 | float qScale = 0.0f, |
| 320 | int32_t qOffset = 0) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 321 | { |
Jan Eilers | 8eb2560 | 2020-03-09 12:13:48 +0000 | [diff] [blame] | 322 | IgnoreUnused(memoryManager); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 323 | unsigned int inputHeight = 20; |
| 324 | unsigned int inputWidth = 17; |
| 325 | unsigned int inputChannels = 3; |
| 326 | unsigned int batchSize = 5; |
| 327 | |
| 328 | armnn::TensorInfo inputTensorInfo; |
| 329 | armnn::TensorInfo outputTensorInfo; |
| 330 | |
| 331 | unsigned int shape[] = {batchSize, inputChannels, inputHeight, inputWidth}; |
| 332 | |
Nattapat Chaimanowong | 649dd95 | 2019-01-22 16:10:44 +0000 | [diff] [blame] | 333 | inputTensorInfo = armnn::TensorInfo(4, shape, ArmnnType); |
| 334 | outputTensorInfo = armnn::TensorInfo(4, shape, ArmnnType); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 335 | |
| 336 | // Set quantization parameters if the requested type is a quantized type. |
| 337 | if(armnn::IsQuantizedType<T>()) |
| 338 | { |
| 339 | inputTensorInfo.SetQuantizationScale(qScale); |
| 340 | inputTensorInfo.SetQuantizationOffset(qOffset); |
| 341 | outputTensorInfo.SetQuantizationScale(qScale); |
| 342 | outputTensorInfo.SetQuantizationOffset(qOffset); |
| 343 | } |
| 344 | |
| 345 | LayerTestResult<T, 4> ret(outputTensorInfo); |
Keith Davis | 33a626f | 2020-08-27 15:38:12 +0100 | [diff] [blame] | 346 | std::unique_ptr<armnn::ITensorHandle> inputHandle = tensorHandleFactory.CreateTensorHandle(inputTensorInfo); |
| 347 | std::unique_ptr<armnn::ITensorHandle> outputHandle = tensorHandleFactory.CreateTensorHandle(outputTensorInfo); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 348 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 349 | // Do linear activation that should leave the tensor unchanged. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 350 | armnn::ActivationQueueDescriptor data; |
| 351 | armnn::WorkloadInfo info; |
| 352 | AddInputToWorkload(data, info, inputTensorInfo, inputHandle.get()); |
| 353 | AddOutputToWorkload(data, info, outputTensorInfo, outputHandle.get()); |
| 354 | data.m_Parameters.m_A = 1.0f; |
| 355 | data.m_Parameters.m_B = 0.0f; |
| 356 | data.m_Parameters.m_Function = armnn::ActivationFunction::Linear; |
| 357 | |
| 358 | std::unique_ptr<armnn::IWorkload> workload = workloadFactory.CreateActivation(data, info); |
| 359 | |
| 360 | inputHandle->Allocate(); |
| 361 | outputHandle->Allocate(); |
| 362 | |
| 363 | boost::multi_array<T, 4> input = MakeRandomTensor<T, 4>(inputTensorInfo, 7123561); |
| 364 | CopyDataToITensorHandle(inputHandle.get(), &input[0][0][0][0]); |
| 365 | |
| 366 | workload->Execute(); |
| 367 | |
| 368 | CopyDataFromITensorHandle(&ret.output[0][0][0][0], outputHandle.get()); |
| 369 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 370 | // Ensure output equals input. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 371 | ret.outputExpected = input; |
| 372 | |
| 373 | return ret; |
| 374 | } |
| 375 | |
Aron Virginas-Tar | 5caf907 | 2018-11-14 18:35:18 +0000 | [diff] [blame] | 376 | LayerTestResult<float, 4> ConstantLinearActivationTest( |
| 377 | armnn::IWorkloadFactory& workloadFactory, |
Keith Davis | 33a626f | 2020-08-27 15:38:12 +0100 | [diff] [blame] | 378 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 379 | const armnn::ITensorHandleFactory& tensorHandleFactory) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 380 | { |
Keith Davis | 33a626f | 2020-08-27 15:38:12 +0100 | [diff] [blame] | 381 | return ConstantLinearActivationTestCommon<armnn::DataType::Float32>(workloadFactory, |
| 382 | memoryManager, |
| 383 | tensorHandleFactory); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 384 | } |
| 385 | |
Aron Virginas-Tar | 5caf907 | 2018-11-14 18:35:18 +0000 | [diff] [blame] | 386 | LayerTestResult<uint8_t, 4> ConstantLinearActivationUint8Test( |
| 387 | armnn::IWorkloadFactory& workloadFactory, |
Keith Davis | 33a626f | 2020-08-27 15:38:12 +0100 | [diff] [blame] | 388 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 389 | const armnn::ITensorHandleFactory& tensorHandleFactory) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 390 | { |
Derek Lamberti | f90c56d | 2020-01-10 17:14:08 +0000 | [diff] [blame] | 391 | return ConstantLinearActivationTestCommon<armnn::DataType::QAsymmU8>( |
Keith Davis | 33a626f | 2020-08-27 15:38:12 +0100 | [diff] [blame] | 392 | workloadFactory, memoryManager, tensorHandleFactory, 4.0f, 3); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 393 | } |
| 394 | |
Teresa Charlin | 18515e2 | 2019-04-24 10:17:46 +0100 | [diff] [blame] | 395 | LayerTestResult<int16_t, 4> ConstantLinearActivationInt16Test( |
| 396 | armnn::IWorkloadFactory& workloadFactory, |
Keith Davis | 33a626f | 2020-08-27 15:38:12 +0100 | [diff] [blame] | 397 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 398 | const armnn::ITensorHandleFactory& tensorHandleFactory) |
Teresa Charlin | 18515e2 | 2019-04-24 10:17:46 +0100 | [diff] [blame] | 399 | { |
Derek Lamberti | f90c56d | 2020-01-10 17:14:08 +0000 | [diff] [blame] | 400 | return ConstantLinearActivationTestCommon<armnn::DataType::QSymmS16>( |
Keith Davis | 33a626f | 2020-08-27 15:38:12 +0100 | [diff] [blame] | 401 | workloadFactory, memoryManager, tensorHandleFactory, 0.1f, 0); |
Teresa Charlin | 18515e2 | 2019-04-24 10:17:46 +0100 | [diff] [blame] | 402 | } |
| 403 | |
Nattapat Chaimanowong | 649dd95 | 2019-01-22 16:10:44 +0000 | [diff] [blame] | 404 | template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>> |
Aron Virginas-Tar | 5caf907 | 2018-11-14 18:35:18 +0000 | [diff] [blame] | 405 | LayerTestResult<T, 4> SimpleActivationTest( |
| 406 | armnn::IWorkloadFactory& workloadFactory, |
| 407 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
Keith Davis | 33a626f | 2020-08-27 15:38:12 +0100 | [diff] [blame] | 408 | const armnn::ITensorHandleFactory& tensorHandleFactory, |
Aron Virginas-Tar | 5caf907 | 2018-11-14 18:35:18 +0000 | [diff] [blame] | 409 | armnn::ActivationFunction activationFunction, |
| 410 | float activationParameterA, |
| 411 | float activationParameterB, |
Ferran Balaguer | b2b5a26 | 2019-06-24 12:43:38 +0100 | [diff] [blame] | 412 | float scale, |
| 413 | int32_t offset, |
Aron Virginas-Tar | 5caf907 | 2018-11-14 18:35:18 +0000 | [diff] [blame] | 414 | const std::vector<float>& inputData, |
Ferran Balaguer | b2b5a26 | 2019-06-24 12:43:38 +0100 | [diff] [blame] | 415 | float outScale, |
| 416 | int32_t outOffset, |
Aron Virginas-Tar | 5caf907 | 2018-11-14 18:35:18 +0000 | [diff] [blame] | 417 | const std::vector<float>& outputExpectedData) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 418 | { |
Jan Eilers | 8eb2560 | 2020-03-09 12:13:48 +0000 | [diff] [blame] | 419 | IgnoreUnused(memoryManager); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 420 | constexpr static unsigned int inputWidth = 16u; |
| 421 | constexpr static unsigned int inputHeight = 1u; |
| 422 | constexpr static unsigned int inputChannels = 1u; |
| 423 | constexpr static unsigned int inputBatchSize = 1u; |
| 424 | |
| 425 | constexpr static unsigned int outputWidth = inputWidth; |
| 426 | constexpr static unsigned int outputHeight = inputHeight; |
| 427 | constexpr static unsigned int outputChannels = inputChannels; |
| 428 | constexpr static unsigned int outputBatchSize = inputBatchSize; |
| 429 | |
Nattapat Chaimanowong | 649dd95 | 2019-01-22 16:10:44 +0000 | [diff] [blame] | 430 | armnn::TensorInfo inputTensorInfo({ inputBatchSize, inputChannels, inputHeight, inputWidth }, ArmnnType); |
| 431 | armnn::TensorInfo outputTensorInfo({ outputBatchSize, outputChannels, outputHeight, outputWidth }, ArmnnType); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 432 | |
| 433 | // Set quantization parameters if the requested type is a quantized type. |
| 434 | if(armnn::IsQuantizedType<T>()) |
| 435 | { |
Ferran Balaguer | b2b5a26 | 2019-06-24 12:43:38 +0100 | [diff] [blame] | 436 | inputTensorInfo.SetQuantizationScale(scale); |
| 437 | inputTensorInfo.SetQuantizationOffset(offset); |
| 438 | outputTensorInfo.SetQuantizationScale(outScale); |
| 439 | outputTensorInfo.SetQuantizationOffset(outOffset); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | LayerTestResult<T, 4> result(inputTensorInfo); |
| 443 | |
Aron Virginas-Tar | 48623a0 | 2019-10-22 10:00:28 +0100 | [diff] [blame] | 444 | auto input = MakeTensor<T, 4>(inputTensorInfo, armnnUtils::QuantizedVector<T>(inputData, scale, offset)); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 445 | |
Keith Davis | 33a626f | 2020-08-27 15:38:12 +0100 | [diff] [blame] | 446 | std::unique_ptr<armnn::ITensorHandle> inputHandle = tensorHandleFactory.CreateTensorHandle(inputTensorInfo); |
| 447 | std::unique_ptr<armnn::ITensorHandle> outputHandle = tensorHandleFactory.CreateTensorHandle(outputTensorInfo); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 448 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 449 | // Setup bounded ReLu. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 450 | armnn::ActivationQueueDescriptor descriptor; |
| 451 | armnn::WorkloadInfo workloadInfo; |
| 452 | AddInputToWorkload(descriptor, workloadInfo, inputTensorInfo, inputHandle.get()); |
| 453 | AddOutputToWorkload(descriptor, workloadInfo, outputTensorInfo, outputHandle.get()); |
| 454 | |
| 455 | descriptor.m_Parameters.m_Function = activationFunction; |
| 456 | descriptor.m_Parameters.m_A = activationParameterA; |
| 457 | descriptor.m_Parameters.m_B = activationParameterB; |
| 458 | |
| 459 | std::unique_ptr<armnn::IWorkload> workload = workloadFactory.CreateActivation(descriptor, workloadInfo); |
| 460 | |
| 461 | inputHandle->Allocate(); |
| 462 | outputHandle->Allocate(); |
| 463 | |
| 464 | CopyDataToITensorHandle(inputHandle.get(), &input[0][0][0][0]); |
| 465 | |
| 466 | workload->Execute(); |
| 467 | |
| 468 | CopyDataFromITensorHandle(&result.output[0][0][0][0], outputHandle.get()); |
| 469 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 470 | // Calculated manually. |
Aron Virginas-Tar | 48623a0 | 2019-10-22 10:00:28 +0100 | [diff] [blame] | 471 | result.outputExpected = |
| 472 | MakeTensor<T, 4>(outputTensorInfo, armnnUtils::QuantizedVector<T>(outputExpectedData, outScale, outOffset)); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 473 | |
| 474 | return result; |
| 475 | } |
| 476 | |
Nattapat Chaimanowong | 649dd95 | 2019-01-22 16:10:44 +0000 | [diff] [blame] | 477 | template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>> |
Aron Virginas-Tar | 5caf907 | 2018-11-14 18:35:18 +0000 | [diff] [blame] | 478 | LayerTestResult<T, 4> SimpleSigmoidTestCommon( |
| 479 | armnn::IWorkloadFactory& workloadFactory, |
| 480 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
Keith Davis | 33a626f | 2020-08-27 15:38:12 +0100 | [diff] [blame] | 481 | const armnn::ITensorHandleFactory& tensorHandleFactory, |
Aron Virginas-Tar | 5caf907 | 2018-11-14 18:35:18 +0000 | [diff] [blame] | 482 | float qScale, |
| 483 | int32_t qOffset) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 484 | { |
Aron Virginas-Tar | 00d306e | 2019-08-28 18:08:46 +0100 | [diff] [blame] | 485 | std::vector<float> inputData = |
| 486 | { |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 487 | -0.1f, -0.2f, -0.3f, -0.4f, |
| 488 | 0.1f, 0.2f, 0.3f, 0.4f, |
| 489 | -1.0f, -2.0f, -3.0f, -4.0f, |
| 490 | 1.0f, 2.0f, 3.0f, 4.0f |
| 491 | }; |
| 492 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 493 | // Calculate output values for input. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 494 | auto f = [](float value) |
| 495 | { |
| 496 | return 1.0f / (1.0f + std::exp(-value)); |
| 497 | }; |
| 498 | std::vector<float> outputExpectedData(inputData.size()); |
| 499 | std::transform(inputData.begin(), inputData.end(), outputExpectedData.begin(), f); |
| 500 | |
Nattapat Chaimanowong | 649dd95 | 2019-01-22 16:10:44 +0000 | [diff] [blame] | 501 | return SimpleActivationTest<ArmnnType>(workloadFactory, |
| 502 | memoryManager, |
Keith Davis | 33a626f | 2020-08-27 15:38:12 +0100 | [diff] [blame] | 503 | tensorHandleFactory, |
Nattapat Chaimanowong | 649dd95 | 2019-01-22 16:10:44 +0000 | [diff] [blame] | 504 | armnn::ActivationFunction::Sigmoid, |
| 505 | 0.f, |
| 506 | 0.f, |
| 507 | qScale, |
| 508 | qOffset, |
| 509 | inputData, |
Ferran Balaguer | b2b5a26 | 2019-06-24 12:43:38 +0100 | [diff] [blame] | 510 | 1.f / 256.f, |
| 511 | 0, |
Nattapat Chaimanowong | 649dd95 | 2019-01-22 16:10:44 +0000 | [diff] [blame] | 512 | outputExpectedData); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 513 | } |
| 514 | |
Aron Virginas-Tar | 5caf907 | 2018-11-14 18:35:18 +0000 | [diff] [blame] | 515 | LayerTestResult<float, 4> SimpleSigmoidTest( |
| 516 | armnn::IWorkloadFactory& workloadFactory, |
Keith Davis | 33a626f | 2020-08-27 15:38:12 +0100 | [diff] [blame] | 517 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 518 | const armnn::ITensorHandleFactory& tensorHandleFactory) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 519 | { |
Keith Davis | 33a626f | 2020-08-27 15:38:12 +0100 | [diff] [blame] | 520 | return SimpleSigmoidTestCommon<armnn::DataType::Float32>(workloadFactory, memoryManager, |
| 521 | tensorHandleFactory, 0.0f, 0); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 522 | } |
| 523 | |
Aron Virginas-Tar | 5caf907 | 2018-11-14 18:35:18 +0000 | [diff] [blame] | 524 | LayerTestResult<uint8_t, 4> SimpleSigmoidUint8Test( |
| 525 | armnn::IWorkloadFactory& workloadFactory, |
Keith Davis | 33a626f | 2020-08-27 15:38:12 +0100 | [diff] [blame] | 526 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 527 | const armnn::ITensorHandleFactory& tensorHandleFactory) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 528 | { |
Keith Davis | 33a626f | 2020-08-27 15:38:12 +0100 | [diff] [blame] | 529 | return SimpleSigmoidTestCommon<armnn::DataType::QAsymmU8>(workloadFactory, memoryManager, |
| 530 | tensorHandleFactory, 0.1f, 50); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 531 | } |
| 532 | |
Teresa Charlin | 18515e2 | 2019-04-24 10:17:46 +0100 | [diff] [blame] | 533 | LayerTestResult<int16_t, 4> SimpleSigmoidInt16Test( |
| 534 | armnn::IWorkloadFactory& workloadFactory, |
Keith Davis | 33a626f | 2020-08-27 15:38:12 +0100 | [diff] [blame] | 535 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 536 | const armnn::ITensorHandleFactory& tensorHandleFactory) |
Teresa Charlin | 18515e2 | 2019-04-24 10:17:46 +0100 | [diff] [blame] | 537 | { |
Keith Davis | 33a626f | 2020-08-27 15:38:12 +0100 | [diff] [blame] | 538 | return SimpleSigmoidTestCommon<armnn::DataType::QSymmS16>(workloadFactory, memoryManager, |
| 539 | tensorHandleFactory, 0.1f, 0); |
Teresa Charlin | 18515e2 | 2019-04-24 10:17:46 +0100 | [diff] [blame] | 540 | } |
| 541 | |
| 542 | template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>> |
| 543 | LayerTestResult<T, 4> ReLuTestCommon( |
| 544 | armnn::IWorkloadFactory& workloadFactory, |
| 545 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
Keith Davis | 33a626f | 2020-08-27 15:38:12 +0100 | [diff] [blame] | 546 | const armnn::ITensorHandleFactory& tensorHandleFactory, |
Teresa Charlin | 18515e2 | 2019-04-24 10:17:46 +0100 | [diff] [blame] | 547 | float qScale, |
| 548 | int32_t qOffset) |
| 549 | { |
| 550 | std::vector<float> inputData = { |
| 551 | -0.1f, -0.2f, -0.3f, -0.4f, |
| 552 | 0.1f, 0.2f, 0.3f, 0.4f, |
| 553 | -1.0f, -2.0f, -3.0f, -4.0f, |
| 554 | 1.0f, 2.0f, 3.0f, 4.0f |
| 555 | }; |
| 556 | |
| 557 | // Calculate output values for input. |
| 558 | auto f = [](float value) |
| 559 | { |
| 560 | return std::fmax(0.0f, value); |
| 561 | }; |
| 562 | std::vector<float> outputExpectedData(inputData.size()); |
| 563 | std::transform(inputData.begin(), inputData.end(), outputExpectedData.begin(), f); |
| 564 | |
| 565 | return SimpleActivationTest<ArmnnType>(workloadFactory, |
| 566 | memoryManager, |
Keith Davis | 33a626f | 2020-08-27 15:38:12 +0100 | [diff] [blame] | 567 | tensorHandleFactory, |
Teresa Charlin | 18515e2 | 2019-04-24 10:17:46 +0100 | [diff] [blame] | 568 | armnn::ActivationFunction::ReLu, |
| 569 | 0.f, |
| 570 | 0.f, |
| 571 | qScale, |
| 572 | qOffset, |
| 573 | inputData, |
Ferran Balaguer | b2b5a26 | 2019-06-24 12:43:38 +0100 | [diff] [blame] | 574 | qScale, |
| 575 | qOffset, |
Teresa Charlin | 18515e2 | 2019-04-24 10:17:46 +0100 | [diff] [blame] | 576 | outputExpectedData); |
| 577 | } |
| 578 | |
| 579 | LayerTestResult<int16_t, 4> ReLuInt16Test( |
| 580 | armnn::IWorkloadFactory& workloadFactory, |
Keith Davis | 33a626f | 2020-08-27 15:38:12 +0100 | [diff] [blame] | 581 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 582 | const armnn::ITensorHandleFactory& tensorHandleFactory) |
Teresa Charlin | 18515e2 | 2019-04-24 10:17:46 +0100 | [diff] [blame] | 583 | { |
Keith Davis | 33a626f | 2020-08-27 15:38:12 +0100 | [diff] [blame] | 584 | return ReLuTestCommon<armnn::DataType::QSymmS16>(workloadFactory, memoryManager, tensorHandleFactory, 0.1f, 0); |
Teresa Charlin | 18515e2 | 2019-04-24 10:17:46 +0100 | [diff] [blame] | 585 | } |
| 586 | |
| 587 | |
konsof01 | 7f6db40 | 2019-06-07 15:15:58 +0100 | [diff] [blame] | 588 | LayerTestResult<uint8_t, 4> ReLuUint8Test( |
| 589 | armnn::IWorkloadFactory& workloadFactory, |
Keith Davis | 33a626f | 2020-08-27 15:38:12 +0100 | [diff] [blame] | 590 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 591 | const armnn::ITensorHandleFactory& tensorHandleFactory) |
konsof01 | 7f6db40 | 2019-06-07 15:15:58 +0100 | [diff] [blame] | 592 | { |
Keith Davis | 33a626f | 2020-08-27 15:38:12 +0100 | [diff] [blame] | 593 | return ReLuTestCommon<armnn::DataType::QAsymmU8>(workloadFactory, memoryManager, tensorHandleFactory, 0.1f, 0); |
konsof01 | 7f6db40 | 2019-06-07 15:15:58 +0100 | [diff] [blame] | 594 | } |
| 595 | |
| 596 | LayerTestResult<float, 4> ReLuTest( |
| 597 | armnn::IWorkloadFactory& workloadFactory, |
Keith Davis | 33a626f | 2020-08-27 15:38:12 +0100 | [diff] [blame] | 598 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 599 | const armnn::ITensorHandleFactory& tensorHandleFactory) |
konsof01 | 7f6db40 | 2019-06-07 15:15:58 +0100 | [diff] [blame] | 600 | { |
Keith Davis | 33a626f | 2020-08-27 15:38:12 +0100 | [diff] [blame] | 601 | return ReLuTestCommon<armnn::DataType::Float32>(workloadFactory, memoryManager, tensorHandleFactory, 0.1f, 0); |
konsof01 | 7f6db40 | 2019-06-07 15:15:58 +0100 | [diff] [blame] | 602 | } |
| 603 | |
| 604 | |
Teresa Charlin | 18515e2 | 2019-04-24 10:17:46 +0100 | [diff] [blame] | 605 | template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>> |
| 606 | LayerTestResult<T, 4> BoundedReLuTestCommon( |
| 607 | armnn::IWorkloadFactory& workloadFactory, |
| 608 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
Keith Davis | 33a626f | 2020-08-27 15:38:12 +0100 | [diff] [blame] | 609 | const armnn::ITensorHandleFactory& tensorHandleFactory, |
Teresa Charlin | 18515e2 | 2019-04-24 10:17:46 +0100 | [diff] [blame] | 610 | float qScale, |
| 611 | int32_t qOffset) |
| 612 | { |
| 613 | std::vector<float> inputData = { |
| 614 | -0.1f, -0.2f, -0.3f, -0.4f, |
| 615 | 0.1f, 0.2f, 0.3f, 0.4f, |
| 616 | -1.0f, -2.0f, -3.0f, -4.0f, |
| 617 | 1.0f, 2.0f, 3.0f, 4.0f |
| 618 | }; |
| 619 | const float a = 1.0f; |
| 620 | const float b = -1.0f; |
| 621 | // Calculate output values for input. |
| 622 | auto f = [a, b](float value) |
| 623 | { |
| 624 | return std::min(a, std::max(b, value)); |
| 625 | }; |
| 626 | std::vector<float> outputExpectedData(inputData.size()); |
| 627 | std::transform(inputData.begin(), inputData.end(), outputExpectedData.begin(), f); |
| 628 | |
| 629 | return SimpleActivationTest<ArmnnType>(workloadFactory, |
| 630 | memoryManager, |
Keith Davis | 33a626f | 2020-08-27 15:38:12 +0100 | [diff] [blame] | 631 | tensorHandleFactory, |
Teresa Charlin | 18515e2 | 2019-04-24 10:17:46 +0100 | [diff] [blame] | 632 | armnn::ActivationFunction::BoundedReLu, |
| 633 | a, |
| 634 | b, |
| 635 | qScale, |
| 636 | qOffset, |
| 637 | inputData, |
Ferran Balaguer | b2b5a26 | 2019-06-24 12:43:38 +0100 | [diff] [blame] | 638 | qScale, |
| 639 | qOffset, |
Teresa Charlin | 18515e2 | 2019-04-24 10:17:46 +0100 | [diff] [blame] | 640 | outputExpectedData); |
| 641 | } |
| 642 | |
| 643 | LayerTestResult<int16_t, 4> BoundedReLuInt16Test( |
| 644 | armnn::IWorkloadFactory& workloadFactory, |
Keith Davis | 33a626f | 2020-08-27 15:38:12 +0100 | [diff] [blame] | 645 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 646 | const armnn::ITensorHandleFactory& tensorHandleFactory) |
Teresa Charlin | 18515e2 | 2019-04-24 10:17:46 +0100 | [diff] [blame] | 647 | { |
Keith Davis | 33a626f | 2020-08-27 15:38:12 +0100 | [diff] [blame] | 648 | return ReLuTestCommon<armnn::DataType::QSymmS16>(workloadFactory, memoryManager, tensorHandleFactory, 0.1f, 0); |
Teresa Charlin | 18515e2 | 2019-04-24 10:17:46 +0100 | [diff] [blame] | 649 | } |
| 650 | |
| 651 | |
| 652 | |
| 653 | template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>> |
| 654 | LayerTestResult<T, 4> SoftReLuTestCommon( |
| 655 | armnn::IWorkloadFactory& workloadFactory, |
| 656 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
Keith Davis | 33a626f | 2020-08-27 15:38:12 +0100 | [diff] [blame] | 657 | const armnn::ITensorHandleFactory& tensorHandleFactory, |
Teresa Charlin | 18515e2 | 2019-04-24 10:17:46 +0100 | [diff] [blame] | 658 | float qScale, |
| 659 | int32_t qOffset) |
| 660 | { |
| 661 | std::vector<float> inputData = { |
| 662 | -0.1f, -0.2f, -0.3f, -0.4f, |
| 663 | 0.1f, 0.2f, 0.3f, 0.4f, |
| 664 | -1.0f, -2.0f, -3.0f, -4.0f, |
| 665 | 1.0f, 2.0f, 3.0f, 4.0f |
| 666 | }; |
| 667 | |
| 668 | // Calculate output values for input. |
| 669 | auto f = [](float value) |
| 670 | { |
| 671 | return std::log(1.0f + std::exp(value)); |
| 672 | }; |
| 673 | std::vector<float> outputExpectedData(inputData.size()); |
| 674 | std::transform(inputData.begin(), inputData.end(), outputExpectedData.begin(), f); |
| 675 | |
| 676 | return SimpleActivationTest<ArmnnType>(workloadFactory, |
| 677 | memoryManager, |
Keith Davis | 33a626f | 2020-08-27 15:38:12 +0100 | [diff] [blame] | 678 | tensorHandleFactory, |
Teresa Charlin | 18515e2 | 2019-04-24 10:17:46 +0100 | [diff] [blame] | 679 | armnn::ActivationFunction::SoftReLu, |
| 680 | 0.f, |
| 681 | 0.f, |
| 682 | qScale, |
| 683 | qOffset, |
| 684 | inputData, |
Ferran Balaguer | b2b5a26 | 2019-06-24 12:43:38 +0100 | [diff] [blame] | 685 | qScale, |
| 686 | qOffset, |
Teresa Charlin | 18515e2 | 2019-04-24 10:17:46 +0100 | [diff] [blame] | 687 | outputExpectedData); |
| 688 | } |
| 689 | |
konsof01 | 7f6db40 | 2019-06-07 15:15:58 +0100 | [diff] [blame] | 690 | LayerTestResult<float, 4> SoftReLuTest( |
| 691 | armnn::IWorkloadFactory& workloadFactory, |
Keith Davis | 33a626f | 2020-08-27 15:38:12 +0100 | [diff] [blame] | 692 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 693 | const armnn::ITensorHandleFactory& tensorHandleFactory) |
konsof01 | 7f6db40 | 2019-06-07 15:15:58 +0100 | [diff] [blame] | 694 | { |
Keith Davis | 33a626f | 2020-08-27 15:38:12 +0100 | [diff] [blame] | 695 | return SoftReLuTestCommon<armnn::DataType::Float32>(workloadFactory, memoryManager, tensorHandleFactory, 0.1f, 0); |
konsof01 | 7f6db40 | 2019-06-07 15:15:58 +0100 | [diff] [blame] | 696 | } |
| 697 | |
| 698 | LayerTestResult<uint8_t, 4> SoftReLuUint8Test( |
| 699 | armnn::IWorkloadFactory& workloadFactory, |
Keith Davis | 33a626f | 2020-08-27 15:38:12 +0100 | [diff] [blame] | 700 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 701 | const armnn::ITensorHandleFactory& tensorHandleFactory) |
konsof01 | 7f6db40 | 2019-06-07 15:15:58 +0100 | [diff] [blame] | 702 | { |
Keith Davis | 33a626f | 2020-08-27 15:38:12 +0100 | [diff] [blame] | 703 | return SoftReLuTestCommon<armnn::DataType::QAsymmU8>(workloadFactory, memoryManager, |
| 704 | tensorHandleFactory, 0.0625f, 64); |
konsof01 | 7f6db40 | 2019-06-07 15:15:58 +0100 | [diff] [blame] | 705 | } |
| 706 | |
Teresa Charlin | 18515e2 | 2019-04-24 10:17:46 +0100 | [diff] [blame] | 707 | LayerTestResult<int16_t, 4> SoftReLuInt16Test( |
| 708 | armnn::IWorkloadFactory& workloadFactory, |
Keith Davis | 33a626f | 2020-08-27 15:38:12 +0100 | [diff] [blame] | 709 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 710 | const armnn::ITensorHandleFactory& tensorHandleFactory) |
Teresa Charlin | 18515e2 | 2019-04-24 10:17:46 +0100 | [diff] [blame] | 711 | { |
Keith Davis | 33a626f | 2020-08-27 15:38:12 +0100 | [diff] [blame] | 712 | return SoftReLuTestCommon<armnn::DataType::QSymmS16>(workloadFactory, memoryManager, tensorHandleFactory, 0.1f, 0); |
Teresa Charlin | 18515e2 | 2019-04-24 10:17:46 +0100 | [diff] [blame] | 713 | } |
| 714 | |
| 715 | template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>> |
| 716 | LayerTestResult<T, 4> LeakyReLuTestCommon( |
| 717 | armnn::IWorkloadFactory& workloadFactory, |
| 718 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
Keith Davis | 33a626f | 2020-08-27 15:38:12 +0100 | [diff] [blame] | 719 | const armnn::ITensorHandleFactory& tensorHandleFactory, |
Teresa Charlin | 18515e2 | 2019-04-24 10:17:46 +0100 | [diff] [blame] | 720 | float qScale, |
| 721 | int32_t qOffset) |
| 722 | { |
| 723 | std::vector<float> inputData = { |
| 724 | -0.1f, -0.2f, -0.3f, -0.4f, |
| 725 | 0.1f, 0.2f, 0.3f, 0.4f, |
| 726 | -1.0f, -2.0f, -3.0f, -4.0f, |
| 727 | 1.0f, 2.0f, 3.0f, 4.0f |
| 728 | }; |
| 729 | |
| 730 | const float a = 0.01f; |
| 731 | // Calculate output values for input. |
| 732 | auto f = [a](float value) |
| 733 | { |
| 734 | return value > 0.0f ? value : (value * a); |
| 735 | }; |
| 736 | std::vector<float> outputExpectedData(inputData.size()); |
| 737 | std::transform(inputData.begin(), inputData.end(), outputExpectedData.begin(), f); |
| 738 | |
| 739 | return SimpleActivationTest<ArmnnType>(workloadFactory, |
| 740 | memoryManager, |
Keith Davis | 33a626f | 2020-08-27 15:38:12 +0100 | [diff] [blame] | 741 | tensorHandleFactory, |
Teresa Charlin | 18515e2 | 2019-04-24 10:17:46 +0100 | [diff] [blame] | 742 | armnn::ActivationFunction::LeakyReLu, |
| 743 | a, |
| 744 | 0.f, |
| 745 | qScale, |
| 746 | qOffset, |
| 747 | inputData, |
Ferran Balaguer | b2b5a26 | 2019-06-24 12:43:38 +0100 | [diff] [blame] | 748 | qScale, |
| 749 | qOffset, |
Teresa Charlin | 18515e2 | 2019-04-24 10:17:46 +0100 | [diff] [blame] | 750 | outputExpectedData); |
| 751 | } |
| 752 | |
konsof01 | 7f6db40 | 2019-06-07 15:15:58 +0100 | [diff] [blame] | 753 | LayerTestResult<float, 4> LeakyReLuTest( |
| 754 | armnn::IWorkloadFactory& workloadFactory, |
Keith Davis | 33a626f | 2020-08-27 15:38:12 +0100 | [diff] [blame] | 755 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 756 | const armnn::ITensorHandleFactory& tensorHandleFactory) |
konsof01 | 7f6db40 | 2019-06-07 15:15:58 +0100 | [diff] [blame] | 757 | { |
Keith Davis | 33a626f | 2020-08-27 15:38:12 +0100 | [diff] [blame] | 758 | return LeakyReLuTestCommon<armnn::DataType::Float32>(workloadFactory, memoryManager, tensorHandleFactory, 0.1f, 0); |
konsof01 | 7f6db40 | 2019-06-07 15:15:58 +0100 | [diff] [blame] | 759 | } |
| 760 | |
| 761 | LayerTestResult<uint8_t, 4> LeakyReLuUint8Test( |
| 762 | armnn::IWorkloadFactory& workloadFactory, |
Keith Davis | 33a626f | 2020-08-27 15:38:12 +0100 | [diff] [blame] | 763 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 764 | const armnn::ITensorHandleFactory& tensorHandleFactory) |
konsof01 | 7f6db40 | 2019-06-07 15:15:58 +0100 | [diff] [blame] | 765 | { |
Keith Davis | 33a626f | 2020-08-27 15:38:12 +0100 | [diff] [blame] | 766 | return LeakyReLuTestCommon<armnn::DataType::QAsymmU8>(workloadFactory, memoryManager, |
| 767 | tensorHandleFactory, 0.0625f, 64); |
konsof01 | 7f6db40 | 2019-06-07 15:15:58 +0100 | [diff] [blame] | 768 | } |
| 769 | |
Teresa Charlin | 18515e2 | 2019-04-24 10:17:46 +0100 | [diff] [blame] | 770 | LayerTestResult<int16_t, 4> LeakyReLuInt16Test( |
| 771 | armnn::IWorkloadFactory& workloadFactory, |
Keith Davis | 33a626f | 2020-08-27 15:38:12 +0100 | [diff] [blame] | 772 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 773 | const armnn::ITensorHandleFactory& tensorHandleFactory) |
Teresa Charlin | 18515e2 | 2019-04-24 10:17:46 +0100 | [diff] [blame] | 774 | { |
Keith Davis | 33a626f | 2020-08-27 15:38:12 +0100 | [diff] [blame] | 775 | return LeakyReLuTestCommon<armnn::DataType::QSymmS16>(workloadFactory, memoryManager, tensorHandleFactory, 0.1f, 0); |
Teresa Charlin | 18515e2 | 2019-04-24 10:17:46 +0100 | [diff] [blame] | 776 | } |
| 777 | |
| 778 | template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>> |
| 779 | LayerTestResult<T, 4> AbsTestCommon( |
| 780 | armnn::IWorkloadFactory& workloadFactory, |
| 781 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
Keith Davis | 33a626f | 2020-08-27 15:38:12 +0100 | [diff] [blame] | 782 | const armnn::ITensorHandleFactory& tensorHandleFactory, |
Teresa Charlin | 18515e2 | 2019-04-24 10:17:46 +0100 | [diff] [blame] | 783 | float qScale, |
| 784 | int32_t qOffset) |
| 785 | { |
| 786 | std::vector<float> inputData = { |
| 787 | -0.1f, -0.2f, -0.3f, -0.4f, |
| 788 | 0.1f, 0.2f, 0.3f, 0.4f, |
| 789 | -1.0f, -2.0f, -3.0f, -4.0f, |
| 790 | 1.0f, 2.0f, 3.0f, 4.0f |
| 791 | }; |
| 792 | |
| 793 | // Calculate output values for input. |
| 794 | auto f = [](float value) |
| 795 | { |
| 796 | return std::abs(value); |
| 797 | }; |
| 798 | std::vector<float> outputExpectedData(inputData.size()); |
| 799 | std::transform(inputData.begin(), inputData.end(), outputExpectedData.begin(), f); |
| 800 | |
| 801 | return SimpleActivationTest<ArmnnType>(workloadFactory, |
| 802 | memoryManager, |
Keith Davis | 33a626f | 2020-08-27 15:38:12 +0100 | [diff] [blame] | 803 | tensorHandleFactory, |
Teresa Charlin | 18515e2 | 2019-04-24 10:17:46 +0100 | [diff] [blame] | 804 | armnn::ActivationFunction::Abs, |
| 805 | 0.f, |
| 806 | 0.f, |
| 807 | qScale, |
| 808 | qOffset, |
| 809 | inputData, |
Ferran Balaguer | b2b5a26 | 2019-06-24 12:43:38 +0100 | [diff] [blame] | 810 | qScale, |
| 811 | qOffset, |
Teresa Charlin | 18515e2 | 2019-04-24 10:17:46 +0100 | [diff] [blame] | 812 | outputExpectedData); |
| 813 | } |
| 814 | |
konsof01 | 7f6db40 | 2019-06-07 15:15:58 +0100 | [diff] [blame] | 815 | LayerTestResult<float, 4> AbsTest( |
| 816 | armnn::IWorkloadFactory& workloadFactory, |
Keith Davis | 33a626f | 2020-08-27 15:38:12 +0100 | [diff] [blame] | 817 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 818 | const armnn::ITensorHandleFactory& tensorHandleFactory) |
konsof01 | 7f6db40 | 2019-06-07 15:15:58 +0100 | [diff] [blame] | 819 | { |
Keith Davis | 33a626f | 2020-08-27 15:38:12 +0100 | [diff] [blame] | 820 | return AbsTestCommon<armnn::DataType::Float32>(workloadFactory, memoryManager, tensorHandleFactory, 0.1f, 0); |
konsof01 | 7f6db40 | 2019-06-07 15:15:58 +0100 | [diff] [blame] | 821 | } |
| 822 | |
| 823 | LayerTestResult<uint8_t, 4> AbsUint8Test( |
| 824 | armnn::IWorkloadFactory& workloadFactory, |
Keith Davis | 33a626f | 2020-08-27 15:38:12 +0100 | [diff] [blame] | 825 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 826 | const armnn::ITensorHandleFactory& tensorHandleFactory) |
konsof01 | 7f6db40 | 2019-06-07 15:15:58 +0100 | [diff] [blame] | 827 | { |
Keith Davis | 33a626f | 2020-08-27 15:38:12 +0100 | [diff] [blame] | 828 | return AbsTestCommon<armnn::DataType::QAsymmU8>(workloadFactory, memoryManager, tensorHandleFactory, 0.0625f, 64); |
konsof01 | 7f6db40 | 2019-06-07 15:15:58 +0100 | [diff] [blame] | 829 | } |
| 830 | |
Teresa Charlin | 18515e2 | 2019-04-24 10:17:46 +0100 | [diff] [blame] | 831 | LayerTestResult<int16_t, 4> AbsInt16Test( |
| 832 | armnn::IWorkloadFactory& workloadFactory, |
Keith Davis | 33a626f | 2020-08-27 15:38:12 +0100 | [diff] [blame] | 833 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 834 | const armnn::ITensorHandleFactory& tensorHandleFactory) |
Teresa Charlin | 18515e2 | 2019-04-24 10:17:46 +0100 | [diff] [blame] | 835 | { |
Keith Davis | 33a626f | 2020-08-27 15:38:12 +0100 | [diff] [blame] | 836 | return AbsTestCommon<armnn::DataType::QSymmS16>(workloadFactory, memoryManager, tensorHandleFactory, 0.1f, 0); |
Teresa Charlin | 18515e2 | 2019-04-24 10:17:46 +0100 | [diff] [blame] | 837 | } |
| 838 | |
Sadik Armagan | 6095ba5 | 2019-09-13 17:07:19 +0100 | [diff] [blame] | 839 | LayerTestResult<float, 5> SqrtNNTest( |
| 840 | armnn::IWorkloadFactory& workloadFactory, |
Keith Davis | 33a626f | 2020-08-27 15:38:12 +0100 | [diff] [blame] | 841 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 842 | const armnn::ITensorHandleFactory& tensorHandleFactory) |
Sadik Armagan | 6095ba5 | 2019-09-13 17:07:19 +0100 | [diff] [blame] | 843 | { |
Jan Eilers | 8eb2560 | 2020-03-09 12:13:48 +0000 | [diff] [blame] | 844 | IgnoreUnused(memoryManager); |
Sadik Armagan | 6095ba5 | 2019-09-13 17:07:19 +0100 | [diff] [blame] | 845 | const int inputDataSize = 120; |
| 846 | std::vector<float> inputData(inputDataSize); |
| 847 | |
| 848 | for (unsigned int i = 0u; i < inputDataSize; ++i) |
| 849 | { |
| 850 | inputData[i] = static_cast<float>(i) / 10; |
| 851 | } |
| 852 | |
| 853 | auto f = [](float value) |
| 854 | { |
| 855 | return std::sqrt(value); |
| 856 | }; |
| 857 | std::vector<float> outputExpectedData(inputDataSize); |
| 858 | std::transform(inputData.begin(), inputData.end(), outputExpectedData.begin(), f); |
| 859 | |
| 860 | armnn::TensorInfo inputTensorInfo( |
| 861 | { 1u, 2u, 3u, 4u, 5u }, armnn::DataType::Float32); |
| 862 | armnn::TensorInfo outputTensorInfo( |
| 863 | { 1u, 2u, 3u, 4u, 5u }, armnn::DataType::Float32); |
| 864 | |
| 865 | LayerTestResult<float, 5> result(inputTensorInfo); |
| 866 | |
Aron Virginas-Tar | 48623a0 | 2019-10-22 10:00:28 +0100 | [diff] [blame] | 867 | auto input = MakeTensor<float, 5>(inputTensorInfo, inputData); |
Sadik Armagan | 6095ba5 | 2019-09-13 17:07:19 +0100 | [diff] [blame] | 868 | |
Keith Davis | 33a626f | 2020-08-27 15:38:12 +0100 | [diff] [blame] | 869 | std::unique_ptr<armnn::ITensorHandle> inputHandle = tensorHandleFactory.CreateTensorHandle(inputTensorInfo); |
| 870 | std::unique_ptr<armnn::ITensorHandle> outputHandle = tensorHandleFactory.CreateTensorHandle(outputTensorInfo); |
Sadik Armagan | 6095ba5 | 2019-09-13 17:07:19 +0100 | [diff] [blame] | 871 | |
| 872 | armnn::ActivationQueueDescriptor descriptor; |
| 873 | armnn::WorkloadInfo workloadInfo; |
| 874 | AddInputToWorkload(descriptor, workloadInfo, inputTensorInfo, inputHandle.get()); |
| 875 | AddOutputToWorkload(descriptor, workloadInfo, outputTensorInfo, outputHandle.get()); |
| 876 | |
| 877 | descriptor.m_Parameters.m_Function = armnn::ActivationFunction::Sqrt; |
| 878 | |
| 879 | std::unique_ptr<armnn::IWorkload> workload = workloadFactory.CreateActivation(descriptor, workloadInfo); |
| 880 | |
| 881 | inputHandle->Allocate(); |
| 882 | outputHandle->Allocate(); |
| 883 | |
| 884 | CopyDataToITensorHandle(inputHandle.get(), &input[0][0][0][0][0]); |
| 885 | |
| 886 | workload->Execute(); |
| 887 | |
| 888 | CopyDataFromITensorHandle(&result.output[0][0][0][0][0], outputHandle.get()); |
| 889 | |
| 890 | // Calculated manually. |
Aron Virginas-Tar | 48623a0 | 2019-10-22 10:00:28 +0100 | [diff] [blame] | 891 | result.outputExpected = MakeTensor<float, 5>(outputTensorInfo, outputExpectedData); |
Sadik Armagan | 6095ba5 | 2019-09-13 17:07:19 +0100 | [diff] [blame] | 892 | |
| 893 | return result; |
| 894 | }; |
| 895 | |
Teresa Charlin | 18515e2 | 2019-04-24 10:17:46 +0100 | [diff] [blame] | 896 | template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>> |
| 897 | LayerTestResult<T, 4> SqrtTestCommon( |
| 898 | armnn::IWorkloadFactory& workloadFactory, |
| 899 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
Keith Davis | 33a626f | 2020-08-27 15:38:12 +0100 | [diff] [blame] | 900 | const armnn::ITensorHandleFactory& tensorHandleFactory, |
Teresa Charlin | 18515e2 | 2019-04-24 10:17:46 +0100 | [diff] [blame] | 901 | float qScale, |
| 902 | int32_t qOffset) |
| 903 | { |
| 904 | std::vector<float> inputData = { |
| 905 | 0.1f, 0.2f, 0.3f, 0.4f, |
| 906 | 0.1f, 0.2f, 0.3f, 0.4f, |
| 907 | 1.0f, 2.0f, 3.0f, 4.0f, |
| 908 | 1.0f, 2.0f, 3.0f, 4.0f |
| 909 | }; |
| 910 | |
| 911 | // Calculate output values for input. |
| 912 | auto f = [](float value) |
| 913 | { |
| 914 | return std::sqrt(value); |
| 915 | }; |
| 916 | std::vector<float> outputExpectedData(inputData.size()); |
| 917 | std::transform(inputData.begin(), inputData.end(), outputExpectedData.begin(), f); |
| 918 | |
| 919 | return SimpleActivationTest<ArmnnType>(workloadFactory, |
| 920 | memoryManager, |
Keith Davis | 33a626f | 2020-08-27 15:38:12 +0100 | [diff] [blame] | 921 | tensorHandleFactory, |
Teresa Charlin | 18515e2 | 2019-04-24 10:17:46 +0100 | [diff] [blame] | 922 | armnn::ActivationFunction::Sqrt, |
| 923 | 0.f, |
| 924 | 0.f, |
| 925 | qScale, |
| 926 | qOffset, |
| 927 | inputData, |
Ferran Balaguer | b2b5a26 | 2019-06-24 12:43:38 +0100 | [diff] [blame] | 928 | qScale, |
| 929 | qOffset, |
Teresa Charlin | 18515e2 | 2019-04-24 10:17:46 +0100 | [diff] [blame] | 930 | outputExpectedData); |
| 931 | } |
| 932 | |
konsof01 | 7f6db40 | 2019-06-07 15:15:58 +0100 | [diff] [blame] | 933 | LayerTestResult<float, 4> SqrtTest( |
| 934 | armnn::IWorkloadFactory& workloadFactory, |
Keith Davis | 33a626f | 2020-08-27 15:38:12 +0100 | [diff] [blame] | 935 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 936 | const armnn::ITensorHandleFactory& tensorHandleFactory) |
konsof01 | 7f6db40 | 2019-06-07 15:15:58 +0100 | [diff] [blame] | 937 | { |
Keith Davis | 33a626f | 2020-08-27 15:38:12 +0100 | [diff] [blame] | 938 | return SqrtTestCommon<armnn::DataType::Float32>(workloadFactory, memoryManager, tensorHandleFactory, 0.1f, 0); |
konsof01 | 7f6db40 | 2019-06-07 15:15:58 +0100 | [diff] [blame] | 939 | } |
| 940 | |
| 941 | LayerTestResult<uint8_t, 4> SqrtUint8Test( |
| 942 | armnn::IWorkloadFactory& workloadFactory, |
Keith Davis | 33a626f | 2020-08-27 15:38:12 +0100 | [diff] [blame] | 943 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 944 | const armnn::ITensorHandleFactory& tensorHandleFactory) |
konsof01 | 7f6db40 | 2019-06-07 15:15:58 +0100 | [diff] [blame] | 945 | { |
Keith Davis | 33a626f | 2020-08-27 15:38:12 +0100 | [diff] [blame] | 946 | return SqrtTestCommon<armnn::DataType::QAsymmU8>(workloadFactory, memoryManager, tensorHandleFactory, 0.0625f, 64); |
konsof01 | 7f6db40 | 2019-06-07 15:15:58 +0100 | [diff] [blame] | 947 | } |
| 948 | |
Teresa Charlin | 18515e2 | 2019-04-24 10:17:46 +0100 | [diff] [blame] | 949 | LayerTestResult<int16_t, 4> SqrtInt16Test( |
| 950 | armnn::IWorkloadFactory& workloadFactory, |
Keith Davis | 33a626f | 2020-08-27 15:38:12 +0100 | [diff] [blame] | 951 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 952 | const armnn::ITensorHandleFactory& tensorHandleFactory) |
Teresa Charlin | 18515e2 | 2019-04-24 10:17:46 +0100 | [diff] [blame] | 953 | { |
Keith Davis | 33a626f | 2020-08-27 15:38:12 +0100 | [diff] [blame] | 954 | return SqrtTestCommon<armnn::DataType::QSymmS16>(workloadFactory, memoryManager, tensorHandleFactory, 0.1f, 0); |
Teresa Charlin | 18515e2 | 2019-04-24 10:17:46 +0100 | [diff] [blame] | 955 | } |
| 956 | |
| 957 | template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>> |
| 958 | LayerTestResult<T, 4> SquareTestCommon( |
| 959 | armnn::IWorkloadFactory& workloadFactory, |
| 960 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
Keith Davis | 33a626f | 2020-08-27 15:38:12 +0100 | [diff] [blame] | 961 | const armnn::ITensorHandleFactory& tensorHandleFactory, |
Teresa Charlin | 18515e2 | 2019-04-24 10:17:46 +0100 | [diff] [blame] | 962 | float qScale, |
| 963 | int32_t qOffset) |
| 964 | { |
| 965 | std::vector<float> inputData = { |
| 966 | -0.1f, -0.2f, -0.3f, -0.4f, |
| 967 | 0.1f, 0.2f, 0.3f, 0.4f, |
| 968 | -1.0f, -2.0f, -3.0f, -4.0f, |
| 969 | 1.0f, 2.0f, 3.0f, 4.0f |
| 970 | }; |
| 971 | |
| 972 | // Calculate output values for input. |
| 973 | auto f = [](float value) |
| 974 | { |
| 975 | return std::pow(value,2); |
| 976 | }; |
| 977 | std::vector<float> outputExpectedData(inputData.size()); |
| 978 | std::transform(inputData.begin(), inputData.end(), outputExpectedData.begin(), f); |
| 979 | |
| 980 | return SimpleActivationTest<ArmnnType>(workloadFactory, |
| 981 | memoryManager, |
Keith Davis | 33a626f | 2020-08-27 15:38:12 +0100 | [diff] [blame] | 982 | tensorHandleFactory, |
Teresa Charlin | 18515e2 | 2019-04-24 10:17:46 +0100 | [diff] [blame] | 983 | armnn::ActivationFunction::Square, |
| 984 | 0.f, |
| 985 | 0.f, |
| 986 | qScale, |
| 987 | qOffset, |
| 988 | inputData, |
Ferran Balaguer | b2b5a26 | 2019-06-24 12:43:38 +0100 | [diff] [blame] | 989 | qScale, |
| 990 | qOffset, |
Teresa Charlin | 18515e2 | 2019-04-24 10:17:46 +0100 | [diff] [blame] | 991 | outputExpectedData); |
| 992 | } |
| 993 | |
konsof01 | 7f6db40 | 2019-06-07 15:15:58 +0100 | [diff] [blame] | 994 | LayerTestResult<float, 4> SquareTest( |
| 995 | armnn::IWorkloadFactory& workloadFactory, |
Keith Davis | 33a626f | 2020-08-27 15:38:12 +0100 | [diff] [blame] | 996 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 997 | const armnn::ITensorHandleFactory& tensorHandleFactory) |
konsof01 | 7f6db40 | 2019-06-07 15:15:58 +0100 | [diff] [blame] | 998 | { |
Keith Davis | 33a626f | 2020-08-27 15:38:12 +0100 | [diff] [blame] | 999 | return SquareTestCommon<armnn::DataType::Float32>(workloadFactory, memoryManager, tensorHandleFactory, 0.1f, 0); |
konsof01 | 7f6db40 | 2019-06-07 15:15:58 +0100 | [diff] [blame] | 1000 | } |
| 1001 | |
| 1002 | LayerTestResult<uint8_t, 4> SquareUint8Test( |
| 1003 | armnn::IWorkloadFactory& workloadFactory, |
Keith Davis | 33a626f | 2020-08-27 15:38:12 +0100 | [diff] [blame] | 1004 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 1005 | const armnn::ITensorHandleFactory& tensorHandleFactory) |
konsof01 | 7f6db40 | 2019-06-07 15:15:58 +0100 | [diff] [blame] | 1006 | { |
Keith Davis | 33a626f | 2020-08-27 15:38:12 +0100 | [diff] [blame] | 1007 | return SquareTestCommon<armnn::DataType::QAsymmU8>(workloadFactory, memoryManager, |
| 1008 | tensorHandleFactory, 0.0625f, 64); |
konsof01 | 7f6db40 | 2019-06-07 15:15:58 +0100 | [diff] [blame] | 1009 | } |
| 1010 | |
Teresa Charlin | 18515e2 | 2019-04-24 10:17:46 +0100 | [diff] [blame] | 1011 | LayerTestResult<int16_t, 4> SquareInt16Test( |
| 1012 | armnn::IWorkloadFactory& workloadFactory, |
Keith Davis | 33a626f | 2020-08-27 15:38:12 +0100 | [diff] [blame] | 1013 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 1014 | const armnn::ITensorHandleFactory& tensorHandleFactory) |
Teresa Charlin | 18515e2 | 2019-04-24 10:17:46 +0100 | [diff] [blame] | 1015 | { |
Keith Davis | 33a626f | 2020-08-27 15:38:12 +0100 | [diff] [blame] | 1016 | return SquareTestCommon<armnn::DataType::QSymmS16>(workloadFactory, memoryManager, tensorHandleFactory, 0.1f, 0); |
Teresa Charlin | 18515e2 | 2019-04-24 10:17:46 +0100 | [diff] [blame] | 1017 | } |
| 1018 | |
| 1019 | template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>> |
| 1020 | LayerTestResult<T, 4> TanhTestCommon( |
| 1021 | armnn::IWorkloadFactory& workloadFactory, |
| 1022 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
Keith Davis | 33a626f | 2020-08-27 15:38:12 +0100 | [diff] [blame] | 1023 | const armnn::ITensorHandleFactory& tensorHandleFactory, |
Teresa Charlin | 18515e2 | 2019-04-24 10:17:46 +0100 | [diff] [blame] | 1024 | float qScale, |
| 1025 | int32_t qOffset) |
| 1026 | { |
| 1027 | std::vector<float> inputData = { |
| 1028 | -0.1f, -0.2f, -0.3f, -0.4f, |
| 1029 | 0.1f, 0.2f, 0.3f, 0.4f, |
| 1030 | -1.0f, -2.0f, -3.0f, -4.0f, |
| 1031 | 1.0f, 2.0f, 3.0f, 4.0f |
| 1032 | }; |
| 1033 | |
| 1034 | const float a = 2.0f; |
| 1035 | const float b = 3.0f; |
| 1036 | // Calculate output values for input. |
| 1037 | auto f = [a, b](float value) |
| 1038 | { |
| 1039 | return a * tanhf(b * value); |
| 1040 | }; |
| 1041 | std::vector<float> outputExpectedData(inputData.size()); |
| 1042 | std::transform(inputData.begin(), inputData.end(), outputExpectedData.begin(), f); |
| 1043 | |
| 1044 | return SimpleActivationTest<ArmnnType>(workloadFactory, |
| 1045 | memoryManager, |
Keith Davis | 33a626f | 2020-08-27 15:38:12 +0100 | [diff] [blame] | 1046 | tensorHandleFactory, |
Teresa Charlin | 18515e2 | 2019-04-24 10:17:46 +0100 | [diff] [blame] | 1047 | armnn::ActivationFunction::TanH, |
| 1048 | a, |
| 1049 | b, |
| 1050 | qScale, |
| 1051 | qOffset, |
| 1052 | inputData, |
Ferran Balaguer | b2b5a26 | 2019-06-24 12:43:38 +0100 | [diff] [blame] | 1053 | qScale, |
| 1054 | qOffset, |
Teresa Charlin | 18515e2 | 2019-04-24 10:17:46 +0100 | [diff] [blame] | 1055 | outputExpectedData); |
| 1056 | } |
| 1057 | |
konsof01 | 7f6db40 | 2019-06-07 15:15:58 +0100 | [diff] [blame] | 1058 | LayerTestResult<float, 4> TanhTest( |
| 1059 | armnn::IWorkloadFactory& workloadFactory, |
Keith Davis | 33a626f | 2020-08-27 15:38:12 +0100 | [diff] [blame] | 1060 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 1061 | const armnn::ITensorHandleFactory& tensorHandleFactory) |
konsof01 | 7f6db40 | 2019-06-07 15:15:58 +0100 | [diff] [blame] | 1062 | { |
Keith Davis | 33a626f | 2020-08-27 15:38:12 +0100 | [diff] [blame] | 1063 | return TanhTestCommon<armnn::DataType::Float32>(workloadFactory, memoryManager, tensorHandleFactory, 0.1f, 0); |
konsof01 | 7f6db40 | 2019-06-07 15:15:58 +0100 | [diff] [blame] | 1064 | } |
| 1065 | |
| 1066 | LayerTestResult<uint8_t, 4> TanhUint8Test( |
| 1067 | armnn::IWorkloadFactory& workloadFactory, |
Keith Davis | 33a626f | 2020-08-27 15:38:12 +0100 | [diff] [blame] | 1068 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 1069 | const armnn::ITensorHandleFactory& tensorHandleFactory) |
konsof01 | 7f6db40 | 2019-06-07 15:15:58 +0100 | [diff] [blame] | 1070 | { |
Keith Davis | 33a626f | 2020-08-27 15:38:12 +0100 | [diff] [blame] | 1071 | return TanhTestCommon<armnn::DataType::QAsymmU8>(workloadFactory, memoryManager, tensorHandleFactory, 0.1f, 64); |
konsof01 | 7f6db40 | 2019-06-07 15:15:58 +0100 | [diff] [blame] | 1072 | } |
| 1073 | |
Teresa Charlin | 18515e2 | 2019-04-24 10:17:46 +0100 | [diff] [blame] | 1074 | LayerTestResult<int16_t, 4> TanhInt16Test( |
| 1075 | armnn::IWorkloadFactory& workloadFactory, |
Keith Davis | 33a626f | 2020-08-27 15:38:12 +0100 | [diff] [blame] | 1076 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 1077 | const armnn::ITensorHandleFactory& tensorHandleFactory) |
Teresa Charlin | 18515e2 | 2019-04-24 10:17:46 +0100 | [diff] [blame] | 1078 | { |
Keith Davis | 33a626f | 2020-08-27 15:38:12 +0100 | [diff] [blame] | 1079 | return TanhTestCommon<armnn::DataType::QSymmS16>(workloadFactory, memoryManager, tensorHandleFactory, 0.1f, 0); |
Teresa Charlin | 18515e2 | 2019-04-24 10:17:46 +0100 | [diff] [blame] | 1080 | } |
| 1081 | |
| 1082 | |
David Monahan | 3b3c381 | 2020-02-25 09:03:29 +0000 | [diff] [blame] | 1083 | template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>> |
| 1084 | LayerTestResult<T, 4> EluTestCommon( |
| 1085 | armnn::IWorkloadFactory& workloadFactory, |
| 1086 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
Keith Davis | 33a626f | 2020-08-27 15:38:12 +0100 | [diff] [blame] | 1087 | const armnn::ITensorHandleFactory& tensorHandleFactory, |
David Monahan | 3b3c381 | 2020-02-25 09:03:29 +0000 | [diff] [blame] | 1088 | float qScale, |
| 1089 | int32_t qOffset) |
| 1090 | { |
| 1091 | std::vector<float> inputData = { |
| 1092 | -0.1f, -0.2f, -0.3f, -0.4f, |
| 1093 | 0.1f, 0.2f, 0.3f, 0.4f, |
| 1094 | -1.0f, -2.0f, -3.0f, -4.0f, |
| 1095 | 1.0f, 2.0f, 3.0f, 4.0f |
| 1096 | }; |
| 1097 | |
| 1098 | |
| 1099 | const float a = 0.01f; |
| 1100 | // Calculate output values for input. |
| 1101 | auto f = [a](float value) |
| 1102 | { |
| 1103 | return (value >= 0) ? value : a * (expf(value) - 1); |
| 1104 | }; |
| 1105 | std::vector<float> outputExpectedData(inputData.size()); |
| 1106 | std::transform(inputData.begin(), inputData.end(), outputExpectedData.begin(), f); |
| 1107 | |
| 1108 | return SimpleActivationTest<ArmnnType>(workloadFactory, |
| 1109 | memoryManager, |
Keith Davis | 33a626f | 2020-08-27 15:38:12 +0100 | [diff] [blame] | 1110 | tensorHandleFactory, |
David Monahan | 3b3c381 | 2020-02-25 09:03:29 +0000 | [diff] [blame] | 1111 | armnn::ActivationFunction::Elu, |
| 1112 | a, |
| 1113 | 0.0f, |
| 1114 | qScale, |
| 1115 | qOffset, |
| 1116 | inputData, |
| 1117 | qScale, |
| 1118 | qOffset, |
| 1119 | outputExpectedData); |
| 1120 | } |
| 1121 | |
| 1122 | LayerTestResult<float, 4> EluTest( |
| 1123 | armnn::IWorkloadFactory& workloadFactory, |
Keith Davis | 33a626f | 2020-08-27 15:38:12 +0100 | [diff] [blame] | 1124 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 1125 | const armnn::ITensorHandleFactory& tensorHandleFactory) |
David Monahan | 3b3c381 | 2020-02-25 09:03:29 +0000 | [diff] [blame] | 1126 | { |
Keith Davis | 33a626f | 2020-08-27 15:38:12 +0100 | [diff] [blame] | 1127 | return EluTestCommon<armnn::DataType::Float32>(workloadFactory, memoryManager, tensorHandleFactory, 0.1f, 0); |
David Monahan | 3b3c381 | 2020-02-25 09:03:29 +0000 | [diff] [blame] | 1128 | } |
| 1129 | |
| 1130 | LayerTestResult<uint8_t, 4> EluUint8Test( |
| 1131 | armnn::IWorkloadFactory& workloadFactory, |
Keith Davis | 33a626f | 2020-08-27 15:38:12 +0100 | [diff] [blame] | 1132 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 1133 | const armnn::ITensorHandleFactory& tensorHandleFactory) |
David Monahan | 3b3c381 | 2020-02-25 09:03:29 +0000 | [diff] [blame] | 1134 | { |
Keith Davis | 33a626f | 2020-08-27 15:38:12 +0100 | [diff] [blame] | 1135 | return EluTestCommon<armnn::DataType::QAsymmU8>(workloadFactory, memoryManager, tensorHandleFactory, 0.1f, 64); |
David Monahan | 3b3c381 | 2020-02-25 09:03:29 +0000 | [diff] [blame] | 1136 | } |
| 1137 | |
| 1138 | LayerTestResult<int16_t, 4> EluInt16Test( |
| 1139 | armnn::IWorkloadFactory& workloadFactory, |
Keith Davis | 33a626f | 2020-08-27 15:38:12 +0100 | [diff] [blame] | 1140 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 1141 | const armnn::ITensorHandleFactory& tensorHandleFactory) |
David Monahan | 3b3c381 | 2020-02-25 09:03:29 +0000 | [diff] [blame] | 1142 | { |
Keith Davis | 33a626f | 2020-08-27 15:38:12 +0100 | [diff] [blame] | 1143 | return EluTestCommon<armnn::DataType::QSymmS16>(workloadFactory, memoryManager, tensorHandleFactory, 0.1f, 0); |
David Monahan | 3b3c381 | 2020-02-25 09:03:29 +0000 | [diff] [blame] | 1144 | } |
| 1145 | |
Teresa Charlin | 18515e2 | 2019-04-24 10:17:46 +0100 | [diff] [blame] | 1146 | |
Nattapat Chaimanowong | 649dd95 | 2019-01-22 16:10:44 +0000 | [diff] [blame] | 1147 | template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>> |
Colm Donelan | 03fbeaf | 2020-02-26 15:39:23 +0000 | [diff] [blame] | 1148 | LayerTestResult<T, 4> HardSwishTestCommon( |
| 1149 | armnn::IWorkloadFactory& workloadFactory, |
| 1150 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
Keith Davis | 33a626f | 2020-08-27 15:38:12 +0100 | [diff] [blame] | 1151 | const armnn::ITensorHandleFactory& tensorHandleFactory, |
Colm Donelan | 03fbeaf | 2020-02-26 15:39:23 +0000 | [diff] [blame] | 1152 | float qScale, |
| 1153 | int32_t qOffset) |
| 1154 | { |
| 1155 | std::vector<float> inputData = { |
| 1156 | -0.1f, -0.2f, -0.3f, -0.4f, |
| 1157 | 0.1f, 0.2f, 0.3f, 0.4f, |
| 1158 | -1.0f, -2.0f, -3.0f, -4.0f, |
| 1159 | 1.0f, 2.0f, 3.0f, 4.0f |
| 1160 | }; |
| 1161 | // Calculate output values for input. |
| 1162 | auto f = [](float x) |
| 1163 | { |
| 1164 | // Break down the calculation to help with verification. |
| 1165 | // hard_swish(x) = x * relu6(x+3) / 6 |
| 1166 | // relu6(x) = min(max(x,0),6) |
| 1167 | float reLu6_step1 = std::max((x + 3),0.0f); |
| 1168 | float reLu6Complete = std::min(reLu6_step1, 6.0f); |
| 1169 | float hardSwish_step1 = x * reLu6Complete; |
| 1170 | float result = hardSwish_step1 / 6; |
| 1171 | return result; |
| 1172 | }; |
| 1173 | std::vector<float> outputExpectedData(inputData.size()); |
| 1174 | std::transform(inputData.begin(), inputData.end(), outputExpectedData.begin(), f); |
| 1175 | |
| 1176 | return SimpleActivationTest<ArmnnType>(workloadFactory, |
| 1177 | memoryManager, |
Keith Davis | 33a626f | 2020-08-27 15:38:12 +0100 | [diff] [blame] | 1178 | tensorHandleFactory, |
Colm Donelan | 03fbeaf | 2020-02-26 15:39:23 +0000 | [diff] [blame] | 1179 | armnn::ActivationFunction::HardSwish, |
| 1180 | 0.f, |
| 1181 | 0.f, |
| 1182 | qScale, |
| 1183 | qOffset, |
| 1184 | inputData, |
| 1185 | qScale, |
| 1186 | qOffset, |
| 1187 | outputExpectedData); |
| 1188 | } |
| 1189 | |
| 1190 | LayerTestResult<float, 4> HardSwishTest( |
| 1191 | armnn::IWorkloadFactory& workloadFactory, |
Keith Davis | 33a626f | 2020-08-27 15:38:12 +0100 | [diff] [blame] | 1192 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 1193 | const armnn::ITensorHandleFactory& tensorHandleFactory) |
Colm Donelan | 03fbeaf | 2020-02-26 15:39:23 +0000 | [diff] [blame] | 1194 | { |
Keith Davis | 33a626f | 2020-08-27 15:38:12 +0100 | [diff] [blame] | 1195 | return HardSwishTestCommon<armnn::DataType::Float32>(workloadFactory, memoryManager, tensorHandleFactory, 0.1f, 0); |
Colm Donelan | 03fbeaf | 2020-02-26 15:39:23 +0000 | [diff] [blame] | 1196 | } |
| 1197 | |
| 1198 | LayerTestResult<uint8_t, 4> HardSwishUint8Test( |
| 1199 | armnn::IWorkloadFactory& workloadFactory, |
Keith Davis | 33a626f | 2020-08-27 15:38:12 +0100 | [diff] [blame] | 1200 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 1201 | const armnn::ITensorHandleFactory& tensorHandleFactory) |
Colm Donelan | 03fbeaf | 2020-02-26 15:39:23 +0000 | [diff] [blame] | 1202 | { |
Keith Davis | 33a626f | 2020-08-27 15:38:12 +0100 | [diff] [blame] | 1203 | return HardSwishTestCommon<armnn::DataType::QAsymmU8>(workloadFactory, memoryManager, |
| 1204 | tensorHandleFactory, 0.1f, 64); |
Colm Donelan | 03fbeaf | 2020-02-26 15:39:23 +0000 | [diff] [blame] | 1205 | } |
| 1206 | |
| 1207 | LayerTestResult<int16_t, 4> HardSwishInt16Test( |
| 1208 | armnn::IWorkloadFactory& workloadFactory, |
Keith Davis | 33a626f | 2020-08-27 15:38:12 +0100 | [diff] [blame] | 1209 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 1210 | const armnn::ITensorHandleFactory& tensorHandleFactory) |
Colm Donelan | 03fbeaf | 2020-02-26 15:39:23 +0000 | [diff] [blame] | 1211 | { |
Keith Davis | 33a626f | 2020-08-27 15:38:12 +0100 | [diff] [blame] | 1212 | return HardSwishTestCommon<armnn::DataType::QSymmS16>(workloadFactory, memoryManager, tensorHandleFactory, 0.1f, 0); |
Colm Donelan | 03fbeaf | 2020-02-26 15:39:23 +0000 | [diff] [blame] | 1213 | } |
| 1214 | |
| 1215 | |
| 1216 | template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>> |
Aron Virginas-Tar | 5caf907 | 2018-11-14 18:35:18 +0000 | [diff] [blame] | 1217 | LayerTestResult<T,4> CompareActivationTestImpl( |
| 1218 | armnn::IWorkloadFactory& workloadFactory, |
| 1219 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 1220 | armnn::IWorkloadFactory& refWorkloadFactory, |
Keith Davis | 33a626f | 2020-08-27 15:38:12 +0100 | [diff] [blame] | 1221 | const armnn::ITensorHandleFactory& tensorHandleFactory, |
| 1222 | const armnn::ITensorHandleFactory& refTensorHandleFactory, |
Aron Virginas-Tar | 5caf907 | 2018-11-14 18:35:18 +0000 | [diff] [blame] | 1223 | armnn::ActivationFunction f, |
| 1224 | unsigned int batchSize = 5, |
| 1225 | float qScale = 0.0f, |
| 1226 | int32_t qOffset = 0) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1227 | { |
Jan Eilers | 8eb2560 | 2020-03-09 12:13:48 +0000 | [diff] [blame] | 1228 | IgnoreUnused(memoryManager); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1229 | unsigned int width = 17; |
| 1230 | unsigned int height = 29; |
| 1231 | unsigned int channels = 2; |
| 1232 | |
| 1233 | float a = 0.234f; |
| 1234 | float b = -12.345f; |
| 1235 | |
| 1236 | armnn::TensorInfo inputTensorInfo; |
| 1237 | armnn::TensorInfo outputTensorInfo; |
| 1238 | |
| 1239 | unsigned int shape[] = {batchSize, channels, height, width}; |
| 1240 | |
Nattapat Chaimanowong | 649dd95 | 2019-01-22 16:10:44 +0000 | [diff] [blame] | 1241 | inputTensorInfo = armnn::TensorInfo(4, shape, ArmnnType); |
| 1242 | outputTensorInfo = armnn::TensorInfo(4, shape, ArmnnType); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1243 | |
| 1244 | // Set quantization parameters if the requested type is a quantized type. |
| 1245 | if(armnn::IsQuantizedType<T>()) |
| 1246 | { |
| 1247 | inputTensorInfo.SetQuantizationScale(qScale); |
| 1248 | inputTensorInfo.SetQuantizationOffset(qOffset); |
| 1249 | outputTensorInfo.SetQuantizationScale(qScale); |
| 1250 | outputTensorInfo.SetQuantizationOffset(qOffset); |
| 1251 | } |
| 1252 | |
| 1253 | float minVal = -10.f; |
| 1254 | if (f == armnn::ActivationFunction::Sqrt) |
| 1255 | { |
| 1256 | minVal = 0.f; |
| 1257 | } |
| 1258 | |
| 1259 | boost::multi_array<T, 4> input = MakeRandomTensor<T, 4>(inputTensorInfo, 21453, minVal, 10.f); |
| 1260 | |
| 1261 | |
| 1262 | LayerTestResult<T,4> ret(outputTensorInfo); |
| 1263 | auto boostArrayExtents = boost::extents |
| 1264 | [boost::numeric_cast<boost::multi_array_types::extent_gen::index>(batchSize)] |
| 1265 | [boost::numeric_cast<boost::multi_array_types::extent_gen::index>(channels)] |
| 1266 | [boost::numeric_cast<boost::multi_array_types::extent_gen::index>(height)] |
| 1267 | [boost::numeric_cast<boost::multi_array_types::extent_gen::index>(width)]; |
| 1268 | ret.output.resize(boostArrayExtents); |
| 1269 | ret.outputExpected.resize(boostArrayExtents); |
| 1270 | |
Keith Davis | 33a626f | 2020-08-27 15:38:12 +0100 | [diff] [blame] | 1271 | std::unique_ptr<armnn::ITensorHandle> inputHandle = tensorHandleFactory.CreateTensorHandle(inputTensorInfo); |
| 1272 | std::unique_ptr<armnn::ITensorHandle> outputHandle = tensorHandleFactory.CreateTensorHandle(outputTensorInfo); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1273 | |
Keith Davis | 33a626f | 2020-08-27 15:38:12 +0100 | [diff] [blame] | 1274 | std::unique_ptr<armnn::ITensorHandle> inputHandleRef = refTensorHandleFactory.CreateTensorHandle(inputTensorInfo); |
| 1275 | std::unique_ptr<armnn::ITensorHandle> outputHandleRef = refTensorHandleFactory.CreateTensorHandle(outputTensorInfo); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1276 | |
| 1277 | armnn::ActivationQueueDescriptor data; |
| 1278 | armnn::WorkloadInfo info; |
| 1279 | AddInputToWorkload(data, info, inputTensorInfo, inputHandle.get()); |
| 1280 | AddOutputToWorkload(data, info, outputTensorInfo, outputHandle.get()); |
| 1281 | data.m_Parameters.m_A = a; |
| 1282 | data.m_Parameters.m_B = b; |
| 1283 | data.m_Parameters.m_Function = f; |
| 1284 | |
| 1285 | armnn::ActivationQueueDescriptor refData = data; |
| 1286 | armnn::WorkloadInfo refInfo = info; |
| 1287 | SetWorkloadInput(refData, refInfo, 0, inputTensorInfo, inputHandleRef.get()); |
| 1288 | SetWorkloadOutput(refData, refInfo, 0, outputTensorInfo, outputHandleRef.get()); |
| 1289 | |
| 1290 | std::unique_ptr<armnn::IWorkload> workload = workloadFactory.CreateActivation(data, info); |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 1291 | ARMNN_ASSERT(workload != nullptr); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1292 | std::unique_ptr<armnn::IWorkload> workloadRef = refWorkloadFactory.CreateActivation(refData, refInfo); |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 1293 | ARMNN_ASSERT(workloadRef != nullptr); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1294 | |
| 1295 | inputHandle->Allocate(); |
| 1296 | outputHandle->Allocate(); |
| 1297 | inputHandleRef->Allocate(); |
| 1298 | outputHandleRef->Allocate(); |
| 1299 | |
| 1300 | CopyDataToITensorHandle(inputHandle.get(), &input[0][0][0][0]); |
| 1301 | CopyDataToITensorHandle(inputHandleRef.get(), &input[0][0][0][0]); |
| 1302 | |
| 1303 | workload->Execute(); |
| 1304 | workloadRef->Execute(); |
| 1305 | |
| 1306 | CopyDataFromITensorHandle(&ret.output[0][0][0][0], outputHandle.get()); |
| 1307 | CopyDataFromITensorHandle(&ret.outputExpected[0][0][0][0], outputHandleRef.get()); |
| 1308 | |
| 1309 | return ret; |
| 1310 | } |
| 1311 | |
Aron Virginas-Tar | 5caf907 | 2018-11-14 18:35:18 +0000 | [diff] [blame] | 1312 | LayerTestResult<float,4> CompareActivationTest( |
| 1313 | armnn::IWorkloadFactory& workloadFactory, |
| 1314 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 1315 | armnn::IWorkloadFactory& refWorkloadFactory, |
Keith Davis | 33a626f | 2020-08-27 15:38:12 +0100 | [diff] [blame] | 1316 | const armnn::ITensorHandleFactory& tensorHandleFactory, |
| 1317 | const armnn::ITensorHandleFactory& refTensorHandleFactory, |
Aron Virginas-Tar | 5caf907 | 2018-11-14 18:35:18 +0000 | [diff] [blame] | 1318 | armnn::ActivationFunction f, |
| 1319 | unsigned int batchSize) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1320 | { |
Nattapat Chaimanowong | 649dd95 | 2019-01-22 16:10:44 +0000 | [diff] [blame] | 1321 | return CompareActivationTestImpl<armnn::DataType::Float32>( |
Keith Davis | 33a626f | 2020-08-27 15:38:12 +0100 | [diff] [blame] | 1322 | workloadFactory, memoryManager, refWorkloadFactory, tensorHandleFactory, |
| 1323 | refTensorHandleFactory, f, batchSize); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1324 | } |
| 1325 | |
Aron Virginas-Tar | 5caf907 | 2018-11-14 18:35:18 +0000 | [diff] [blame] | 1326 | LayerTestResult<uint8_t,4> CompareActivationUint8Test( |
| 1327 | armnn::IWorkloadFactory& workloadFactory, |
| 1328 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 1329 | armnn::IWorkloadFactory& refWorkloadFactory, |
Keith Davis | 33a626f | 2020-08-27 15:38:12 +0100 | [diff] [blame] | 1330 | const armnn::ITensorHandleFactory& tensorHandleFactory, |
| 1331 | const armnn::ITensorHandleFactory& refTensorHandleFactory, |
Aron Virginas-Tar | 5caf907 | 2018-11-14 18:35:18 +0000 | [diff] [blame] | 1332 | armnn::ActivationFunction f) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1333 | { |
Derek Lamberti | f90c56d | 2020-01-10 17:14:08 +0000 | [diff] [blame] | 1334 | return CompareActivationTestImpl<armnn::DataType::QAsymmU8>( |
Keith Davis | 33a626f | 2020-08-27 15:38:12 +0100 | [diff] [blame] | 1335 | workloadFactory, memoryManager, refWorkloadFactory, |
| 1336 | tensorHandleFactory, refTensorHandleFactory, f, 5, 0.1f, 50); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1337 | } |
Teresa Charlin | 18515e2 | 2019-04-24 10:17:46 +0100 | [diff] [blame] | 1338 | |
| 1339 | LayerTestResult<int16_t,4> CompareActivationInt16Test( |
| 1340 | armnn::IWorkloadFactory& workloadFactory, |
| 1341 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 1342 | armnn::IWorkloadFactory& refWorkloadFactory, |
Keith Davis | 33a626f | 2020-08-27 15:38:12 +0100 | [diff] [blame] | 1343 | const armnn::ITensorHandleFactory& tensorHandleFactory, |
| 1344 | const armnn::ITensorHandleFactory& refTensorHandleFactory, |
Teresa Charlin | 18515e2 | 2019-04-24 10:17:46 +0100 | [diff] [blame] | 1345 | armnn::ActivationFunction f) |
| 1346 | { |
Derek Lamberti | f90c56d | 2020-01-10 17:14:08 +0000 | [diff] [blame] | 1347 | return CompareActivationTestImpl<armnn::DataType::QSymmS16>( |
Keith Davis | 33a626f | 2020-08-27 15:38:12 +0100 | [diff] [blame] | 1348 | workloadFactory, memoryManager, refWorkloadFactory, tensorHandleFactory, |
| 1349 | refTensorHandleFactory, f, 5, 0.1f, 0); |
Teresa Charlin | 18515e2 | 2019-04-24 10:17:46 +0100 | [diff] [blame] | 1350 | } |