David Monahan | 0cf8442 | 2020-11-16 15:53:03 +0000 | [diff] [blame] | 1 | // |
Teresa Charlin | ad1b3d7 | 2023-03-14 12:10:28 +0000 | [diff] [blame] | 2 | // Copyright © 2020, 2023 Arm Ltd and Contributors. All rights reserved. |
David Monahan | 0cf8442 | 2020-11-16 15:53:03 +0000 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #include "ActivationTestHelper.hpp" |
| 7 | |
| 8 | #include <armnn_delegate.hpp> |
| 9 | |
| 10 | #include <flatbuffers/flatbuffers.h> |
| 11 | #include <tensorflow/lite/interpreter.h> |
Teresa Charlin | ad1b3d7 | 2023-03-14 12:10:28 +0000 | [diff] [blame] | 12 | #include <schema_generated.h> |
David Monahan | 0cf8442 | 2020-11-16 15:53:03 +0000 | [diff] [blame] | 13 | |
| 14 | #include <doctest/doctest.h> |
| 15 | |
| 16 | namespace armnnDelegate |
| 17 | { |
| 18 | |
| 19 | |
| 20 | void ActivationReLuTest(std::vector<armnn::BackendId>& backends) |
| 21 | { |
David Monahan | 0cf8442 | 2020-11-16 15:53:03 +0000 | [diff] [blame] | 22 | std::vector<float> inputData = { |
| 23 | -0.1f, -0.2f, -0.3f, -0.4f, |
| 24 | 0.1f, 0.2f, 0.3f, 0.4f, |
| 25 | -1.0f, -2.0f, -3.0f, -4.0f, |
| 26 | 1.0f, 2.0f, 3.0f, 4.0f |
| 27 | }; |
| 28 | |
| 29 | // Calculate output values for input. |
| 30 | auto f = [](float value) |
| 31 | { |
| 32 | return std::fmax(0.0f, value); |
| 33 | }; |
| 34 | std::vector<float> outputExpectedData(inputData.size()); |
| 35 | std::transform(inputData.begin(), inputData.end(), outputExpectedData.begin(), f); |
| 36 | |
| 37 | ActivationTest(tflite::BuiltinOperator_RELU, |
| 38 | backends, |
| 39 | inputData, |
| 40 | outputExpectedData); |
| 41 | } |
| 42 | |
| 43 | void ActivationBoundedReluTest(std::vector<armnn::BackendId>& backends) |
| 44 | { |
| 45 | std::vector<float> inputData = { |
| 46 | -0.1f, -0.2f, -0.3f, -0.4f, |
| 47 | 0.1f, 0.2f, 0.3f, 0.4f, |
| 48 | -1.0f, -2.0f, -3.0f, -4.0f, |
| 49 | 1.0f, 2.0f, 3.0f, 4.0f |
| 50 | }; |
| 51 | |
| 52 | const float a = 6.0f; |
| 53 | const float b = 0.0f; |
| 54 | // Calculate output values for input. |
| 55 | auto f = [a, b](float value) |
| 56 | { |
| 57 | return std::min(a, std::max(b, value)); |
| 58 | }; |
| 59 | std::vector<float> outputExpectedData(inputData.size()); |
| 60 | std::transform(inputData.begin(), inputData.end(), outputExpectedData.begin(), f); |
| 61 | |
| 62 | ActivationTest(tflite::BuiltinOperator_RELU6, |
| 63 | backends, |
| 64 | inputData, |
| 65 | outputExpectedData); |
| 66 | } |
| 67 | |
| 68 | void ActivationSigmoidTest(std::vector<armnn::BackendId>& backends) |
| 69 | { |
| 70 | std::vector<float> inputData = { |
| 71 | -0.1f, -0.2f, -0.3f, -0.4f, |
| 72 | 0.1f, 0.2f, 0.3f, 0.4f, |
| 73 | -1.0f, -2.0f, -3.0f, -4.0f, |
| 74 | 1.0f, 2.0f, 3.0f, 4.0f |
| 75 | }; |
| 76 | |
| 77 | // Calculate output values for input. |
| 78 | auto f = [](float value) |
| 79 | { |
| 80 | return 1.0f / (1.0f + std::exp(-value)); |
| 81 | }; |
| 82 | std::vector<float> outputExpectedData(inputData.size()); |
| 83 | std::transform(inputData.begin(), inputData.end(), outputExpectedData.begin(), f); |
| 84 | |
| 85 | ActivationTest(tflite::BuiltinOperator_LOGISTIC, |
| 86 | backends, |
| 87 | inputData, |
| 88 | outputExpectedData); |
| 89 | } |
| 90 | |
| 91 | |
| 92 | void ActivationTanHTest(std::vector<armnn::BackendId>& backends) |
| 93 | { |
| 94 | std::vector<float> inputData = { |
| 95 | -0.1f, -0.2f, -0.3f, -0.4f, |
| 96 | 0.1f, 0.2f, 0.3f, 0.4f, |
| 97 | -1.0f, -2.0f, -3.0f, -4.0f, |
| 98 | 1.0f, 2.0f, 3.0f, 4.0f |
| 99 | }; |
| 100 | |
| 101 | // Calculate output values for input. |
| 102 | auto f = [](float value) |
| 103 | { |
| 104 | return tanhf(value); |
| 105 | }; |
| 106 | std::vector<float> outputExpectedData(inputData.size()); |
| 107 | std::transform(inputData.begin(), inputData.end(), outputExpectedData.begin(), f); |
| 108 | |
| 109 | ActivationTest(tflite::BuiltinOperator_TANH, |
| 110 | backends, |
| 111 | inputData, |
| 112 | outputExpectedData); |
| 113 | } |
| 114 | |
Matthew Sloyan | 7515d07 | 2020-12-16 12:50:01 +0000 | [diff] [blame] | 115 | void ActivationEluTest(std::vector<armnn::BackendId>& backends) |
| 116 | { |
| 117 | std::vector<float> inputData = { |
| 118 | -0.1f, -0.2f, -0.3f, -0.4f, |
| 119 | 0.1f, 0.2f, 0.3f, 0.4f, |
| 120 | -1.0f, -2.0f, -3.0f, -4.0f, |
| 121 | 1.0f, 2.0f, 3.0f, 4.0f |
| 122 | }; |
| 123 | |
| 124 | // Calculate output values for input. |
| 125 | auto f = [](float value) |
| 126 | { |
| 127 | if (value < 0) |
| 128 | { |
| 129 | // alpha * (exp(x) - 1) |
| 130 | return 1 * (std::exp(value) - 1); |
| 131 | } |
| 132 | return value; |
| 133 | }; |
| 134 | std::vector<float> outputExpectedData(inputData.size()); |
| 135 | std::transform(inputData.begin(), inputData.end(), outputExpectedData.begin(), f); |
| 136 | |
| 137 | ActivationTest(tflite::BuiltinOperator_ELU, |
| 138 | backends, |
| 139 | inputData, |
| 140 | outputExpectedData); |
| 141 | } |
| 142 | |
| 143 | void ActivationHardSwishTest(std::vector<armnn::BackendId>& backends) |
| 144 | { |
| 145 | std::vector<float> inputData = { |
| 146 | -0.1f, -0.2f, -0.3f, -0.4f, |
| 147 | 0.1f, 0.2f, 0.3f, 0.4f, |
| 148 | -1.0f, -2.0f, -3.0f, -4.0f, |
| 149 | 1.0f, 2.0f, 3.0f, 4.0f |
| 150 | }; |
| 151 | |
| 152 | // Calculate output values for input. |
| 153 | auto f = [](float x) |
| 154 | { |
| 155 | // Break down the calculation to help with verification. |
| 156 | // hard_swish(x) = x * relu6(x+3) / 6 |
| 157 | // relu6(x) = min(max(x,0),6) |
| 158 | float reLu6_step1 = std::max((x + 3),0.0f); |
| 159 | float reLu6Complete = std::min(reLu6_step1, 6.0f); |
| 160 | float hardSwish_step1 = x * reLu6Complete; |
| 161 | float result = hardSwish_step1 / 6; |
| 162 | return result; |
| 163 | }; |
| 164 | std::vector<float> outputExpectedData(inputData.size()); |
| 165 | std::transform(inputData.begin(), inputData.end(), outputExpectedData.begin(), f); |
| 166 | |
| 167 | ActivationTest(tflite::BuiltinOperator_HARD_SWISH, |
| 168 | backends, |
| 169 | inputData, |
| 170 | outputExpectedData); |
| 171 | } |
| 172 | |
Tianle Cheng | ae93173 | 2023-07-28 11:53:04 +0100 | [diff] [blame] | 173 | void ActivationLeakyReLuTest(std::vector<armnn::BackendId>& backends) |
| 174 | { |
| 175 | std::vector<float> inputData = { |
| 176 | -0.1f, -0.2f, -0.3f, -0.4f, |
| 177 | 0.1f, 0.2f, 0.3f, 0.4f, |
| 178 | -1.0f, -2.0f, -3.0f, -4.0f, |
| 179 | 1.0f, 2.0f, 3.0f, 4.0f |
| 180 | }; |
| 181 | |
| 182 | float alpha = 0.3f; |
| 183 | |
| 184 | // Calculate output values for input. |
| 185 | auto f = [alpha](float value) |
| 186 | { |
| 187 | return value > 0 ? value : value * alpha; |
| 188 | }; |
| 189 | std::vector<float> outputExpectedData(inputData.size()); |
| 190 | std::transform(inputData.begin(), inputData.end(), outputExpectedData.begin(), f); |
| 191 | |
| 192 | ActivationTest(tflite::BuiltinOperator_LEAKY_RELU, |
| 193 | backends, |
| 194 | inputData, |
| 195 | outputExpectedData, |
| 196 | alpha); |
| 197 | } |
| 198 | |
Teresa Charlin | 077cddb | 2023-09-15 15:19:21 +0100 | [diff] [blame^] | 199 | void ActivationGeluTest(std::vector<armnn::BackendId>& backends) |
| 200 | { |
| 201 | std::vector<float> inputData = |
| 202 | { |
| 203 | -0.1f, -0.2f, -0.3f, -0.4f, |
| 204 | 0.1f, 0.2f, 0.3f, 0.4f, |
| 205 | -1.0f, -2.0f, -3.0f, -4.0f, |
| 206 | 1.0f, 2.0f, 3.0f, 4.0f |
| 207 | }; |
| 208 | |
| 209 | // Calculate output values for input. |
| 210 | auto f = [](float x) |
| 211 | { |
| 212 | // gelu(x) = x * 1/2 * (1 + erf(x / sqrt(2))), |
| 213 | // where erf is Gaussian error function |
| 214 | auto result = x * (0.5f * (1.0f + erff(static_cast<float>(x / std::sqrt(2))))); |
| 215 | return result; |
| 216 | }; |
| 217 | std::vector<float> outputExpectedData(inputData.size()); |
| 218 | std::transform(inputData.begin(), inputData.end(), outputExpectedData.begin(), f); |
| 219 | |
| 220 | ActivationTest(tflite::BuiltinOperator_GELU, |
| 221 | backends, |
| 222 | inputData, |
| 223 | outputExpectedData); |
| 224 | } |
| 225 | |
David Monahan | 0cf8442 | 2020-11-16 15:53:03 +0000 | [diff] [blame] | 226 | TEST_SUITE("Activation_CpuRefTests") |
| 227 | { |
| 228 | |
| 229 | TEST_CASE ("Activation_ReLu_CpuRef_Test") |
| 230 | { |
| 231 | std::vector<armnn::BackendId> backends = { armnn::Compute::CpuRef }; |
| 232 | ActivationReLuTest(backends); |
| 233 | } |
| 234 | |
| 235 | TEST_CASE ("Activation_Bounded_Relu6_CpuRef_Test") |
| 236 | { |
| 237 | std::vector<armnn::BackendId> backends = { armnn::Compute::CpuRef }; |
| 238 | ActivationBoundedReluTest(backends); |
| 239 | } |
| 240 | |
| 241 | TEST_CASE ("Activation_Sigmoid_CpuRef_Test") |
| 242 | { |
| 243 | std::vector<armnn::BackendId> backends = { armnn::Compute::CpuRef }; |
| 244 | ActivationSigmoidTest(backends); |
| 245 | } |
| 246 | |
David Monahan | 0cf8442 | 2020-11-16 15:53:03 +0000 | [diff] [blame] | 247 | TEST_CASE ("Activation_TanH_CpuRef_Test") |
| 248 | { |
| 249 | std::vector<armnn::BackendId> backends = { armnn::Compute::CpuRef }; |
| 250 | ActivationTanHTest(backends); |
| 251 | } |
| 252 | |
Matthew Sloyan | 7515d07 | 2020-12-16 12:50:01 +0000 | [diff] [blame] | 253 | TEST_CASE ("Activation_Elu_CpuRef_Test") |
| 254 | { |
| 255 | std::vector<armnn::BackendId> backends = { armnn::Compute::CpuRef }; |
| 256 | ActivationEluTest(backends); |
| 257 | } |
| 258 | |
| 259 | TEST_CASE ("Activation_HardSwish_CpuRef_Test") |
| 260 | { |
| 261 | std::vector<armnn::BackendId> backends = { armnn::Compute::CpuRef }; |
| 262 | ActivationHardSwishTest(backends); |
| 263 | } |
| 264 | |
Tianle Cheng | ae93173 | 2023-07-28 11:53:04 +0100 | [diff] [blame] | 265 | TEST_CASE ("Activation_LeakyRelu_CpuRef_Test") |
| 266 | { |
| 267 | std::vector<armnn::BackendId> backends = { armnn::Compute::CpuRef }; |
| 268 | ActivationLeakyReLuTest(backends); |
| 269 | } |
| 270 | |
Teresa Charlin | 077cddb | 2023-09-15 15:19:21 +0100 | [diff] [blame^] | 271 | TEST_CASE ("Activation_Gelu_CpuRef_Test") |
| 272 | { |
| 273 | std::vector<armnn::BackendId> backends = { armnn::Compute::CpuRef }; |
| 274 | ActivationGeluTest(backends); |
| 275 | } |
| 276 | |
David Monahan | 0cf8442 | 2020-11-16 15:53:03 +0000 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | TEST_SUITE("Activation_CpuAccTests") |
| 280 | { |
| 281 | |
| 282 | TEST_CASE ("Activation_ReLu_CpuAcc_Test") |
| 283 | { |
| 284 | std::vector<armnn::BackendId> backends = { armnn::Compute::CpuAcc }; |
| 285 | ActivationReLuTest(backends); |
| 286 | } |
| 287 | |
| 288 | TEST_CASE ("Activation_Bounded_Relu6_CpuAcc_Test") |
| 289 | { |
| 290 | std::vector<armnn::BackendId> backends = { armnn::Compute::CpuAcc }; |
| 291 | ActivationBoundedReluTest(backends); |
| 292 | } |
| 293 | |
| 294 | TEST_CASE ("Activation_Sigmoid_CpuAcc_Test") |
| 295 | { |
| 296 | std::vector<armnn::BackendId> backends = { armnn::Compute::CpuAcc }; |
| 297 | ActivationSigmoidTest(backends); |
| 298 | } |
| 299 | |
David Monahan | 0cf8442 | 2020-11-16 15:53:03 +0000 | [diff] [blame] | 300 | TEST_CASE ("Activation_TanH_CpuAcc_Test") |
| 301 | { |
| 302 | std::vector<armnn::BackendId> backends = { armnn::Compute::CpuAcc }; |
| 303 | ActivationTanHTest(backends); |
| 304 | } |
| 305 | |
Matthew Sloyan | 7515d07 | 2020-12-16 12:50:01 +0000 | [diff] [blame] | 306 | TEST_CASE ("Activation_Elu_CpuAcc_Test") |
| 307 | { |
| 308 | std::vector<armnn::BackendId> backends = { armnn::Compute::CpuAcc }; |
| 309 | ActivationEluTest(backends); |
| 310 | } |
| 311 | |
| 312 | TEST_CASE ("Activation_HardSwish_CpuAcc_Test") |
| 313 | { |
| 314 | std::vector<armnn::BackendId> backends = { armnn::Compute::CpuAcc }; |
| 315 | ActivationHardSwishTest(backends); |
| 316 | } |
| 317 | |
Tianle Cheng | ae93173 | 2023-07-28 11:53:04 +0100 | [diff] [blame] | 318 | TEST_CASE ("Activation_LeakyRelu_CpuAcc_Test") |
| 319 | { |
| 320 | std::vector<armnn::BackendId> backends = { armnn::Compute::CpuAcc }; |
| 321 | ActivationLeakyReLuTest(backends); |
| 322 | } |
| 323 | |
Teresa Charlin | 077cddb | 2023-09-15 15:19:21 +0100 | [diff] [blame^] | 324 | TEST_CASE ("Activation_Gelu_CpuAcc_Test") |
| 325 | { |
| 326 | std::vector<armnn::BackendId> backends = { armnn::Compute::CpuAcc }; |
| 327 | ActivationGeluTest(backends); |
| 328 | } |
| 329 | |
David Monahan | 0cf8442 | 2020-11-16 15:53:03 +0000 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | TEST_SUITE("Activation_GpuAccTests") |
| 333 | { |
| 334 | |
| 335 | TEST_CASE ("Activation_ReLu_GpuAcc_Test") |
| 336 | { |
| 337 | std::vector<armnn::BackendId> backends = { armnn::Compute::GpuAcc }; |
| 338 | ActivationReLuTest(backends); |
| 339 | } |
| 340 | |
| 341 | TEST_CASE ("Activation_Bounded_Relu6_GpuAcc_Test") |
| 342 | { |
| 343 | std::vector<armnn::BackendId> backends = { armnn::Compute::GpuAcc }; |
| 344 | ActivationBoundedReluTest(backends); |
| 345 | } |
| 346 | |
| 347 | TEST_CASE ("Activation_Sigmoid_GpuAcc_Test") |
| 348 | { |
| 349 | std::vector<armnn::BackendId> backends = { armnn::Compute::GpuAcc }; |
| 350 | ActivationSigmoidTest(backends); |
| 351 | } |
| 352 | |
David Monahan | 0cf8442 | 2020-11-16 15:53:03 +0000 | [diff] [blame] | 353 | TEST_CASE ("Activation_TanH_GpuAcc_Test") |
| 354 | { |
| 355 | std::vector<armnn::BackendId> backends = { armnn::Compute::GpuAcc }; |
| 356 | ActivationTanHTest(backends); |
| 357 | } |
| 358 | |
Matthew Sloyan | 7515d07 | 2020-12-16 12:50:01 +0000 | [diff] [blame] | 359 | TEST_CASE ("Activation_Elu_GpuAcc_Test") |
| 360 | { |
| 361 | std::vector<armnn::BackendId> backends = { armnn::Compute::GpuAcc }; |
| 362 | ActivationEluTest(backends); |
| 363 | } |
| 364 | |
| 365 | TEST_CASE ("Activation_HardSwish_GpuAcc_Test") |
| 366 | { |
| 367 | std::vector<armnn::BackendId> backends = { armnn::Compute::GpuAcc }; |
| 368 | ActivationHardSwishTest(backends); |
| 369 | } |
| 370 | |
Tianle Cheng | ae93173 | 2023-07-28 11:53:04 +0100 | [diff] [blame] | 371 | TEST_CASE ("Activation_LeakyRelu_GpuAcc_Test") |
| 372 | { |
| 373 | std::vector<armnn::BackendId> backends = { armnn::Compute::GpuAcc }; |
| 374 | ActivationLeakyReLuTest(backends); |
| 375 | } |
| 376 | |
Teresa Charlin | 077cddb | 2023-09-15 15:19:21 +0100 | [diff] [blame^] | 377 | TEST_CASE ("Activation_Gelu_GpuAcc_Test") |
| 378 | { |
| 379 | std::vector<armnn::BackendId> backends = { armnn::Compute::GpuAcc }; |
| 380 | ActivationGeluTest(backends); |
| 381 | } |
| 382 | |
David Monahan | 0cf8442 | 2020-11-16 15:53:03 +0000 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | } // namespace armnnDelegate |