David Monahan | 0cf8442 | 2020-11-16 15:53:03 +0000 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2020 Arm Ltd and Contributors. All rights reserved. |
| 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> |
| 12 | #include <tensorflow/lite/kernels/register.h> |
| 13 | #include <tensorflow/lite/model.h> |
| 14 | #include <tensorflow/lite/schema/schema_generated.h> |
| 15 | #include <tensorflow/lite/version.h> |
| 16 | |
| 17 | #include <doctest/doctest.h> |
| 18 | |
| 19 | namespace armnnDelegate |
| 20 | { |
| 21 | |
| 22 | |
| 23 | void ActivationReLuTest(std::vector<armnn::BackendId>& backends) |
| 24 | { |
David Monahan | 0cf8442 | 2020-11-16 15:53:03 +0000 | [diff] [blame] | 25 | std::vector<float> inputData = { |
| 26 | -0.1f, -0.2f, -0.3f, -0.4f, |
| 27 | 0.1f, 0.2f, 0.3f, 0.4f, |
| 28 | -1.0f, -2.0f, -3.0f, -4.0f, |
| 29 | 1.0f, 2.0f, 3.0f, 4.0f |
| 30 | }; |
| 31 | |
| 32 | // Calculate output values for input. |
| 33 | auto f = [](float value) |
| 34 | { |
| 35 | return std::fmax(0.0f, value); |
| 36 | }; |
| 37 | std::vector<float> outputExpectedData(inputData.size()); |
| 38 | std::transform(inputData.begin(), inputData.end(), outputExpectedData.begin(), f); |
| 39 | |
| 40 | ActivationTest(tflite::BuiltinOperator_RELU, |
| 41 | backends, |
| 42 | inputData, |
| 43 | outputExpectedData); |
| 44 | } |
| 45 | |
| 46 | void ActivationBoundedReluTest(std::vector<armnn::BackendId>& backends) |
| 47 | { |
| 48 | std::vector<float> inputData = { |
| 49 | -0.1f, -0.2f, -0.3f, -0.4f, |
| 50 | 0.1f, 0.2f, 0.3f, 0.4f, |
| 51 | -1.0f, -2.0f, -3.0f, -4.0f, |
| 52 | 1.0f, 2.0f, 3.0f, 4.0f |
| 53 | }; |
| 54 | |
| 55 | const float a = 6.0f; |
| 56 | const float b = 0.0f; |
| 57 | // Calculate output values for input. |
| 58 | auto f = [a, b](float value) |
| 59 | { |
| 60 | return std::min(a, std::max(b, value)); |
| 61 | }; |
| 62 | std::vector<float> outputExpectedData(inputData.size()); |
| 63 | std::transform(inputData.begin(), inputData.end(), outputExpectedData.begin(), f); |
| 64 | |
| 65 | ActivationTest(tflite::BuiltinOperator_RELU6, |
| 66 | backends, |
| 67 | inputData, |
| 68 | outputExpectedData); |
| 69 | } |
| 70 | |
| 71 | void ActivationSigmoidTest(std::vector<armnn::BackendId>& backends) |
| 72 | { |
| 73 | std::vector<float> inputData = { |
| 74 | -0.1f, -0.2f, -0.3f, -0.4f, |
| 75 | 0.1f, 0.2f, 0.3f, 0.4f, |
| 76 | -1.0f, -2.0f, -3.0f, -4.0f, |
| 77 | 1.0f, 2.0f, 3.0f, 4.0f |
| 78 | }; |
| 79 | |
| 80 | // Calculate output values for input. |
| 81 | auto f = [](float value) |
| 82 | { |
| 83 | return 1.0f / (1.0f + std::exp(-value)); |
| 84 | }; |
| 85 | std::vector<float> outputExpectedData(inputData.size()); |
| 86 | std::transform(inputData.begin(), inputData.end(), outputExpectedData.begin(), f); |
| 87 | |
| 88 | ActivationTest(tflite::BuiltinOperator_LOGISTIC, |
| 89 | backends, |
| 90 | inputData, |
| 91 | outputExpectedData); |
| 92 | } |
| 93 | |
| 94 | |
| 95 | void ActivationTanHTest(std::vector<armnn::BackendId>& backends) |
| 96 | { |
| 97 | std::vector<float> inputData = { |
| 98 | -0.1f, -0.2f, -0.3f, -0.4f, |
| 99 | 0.1f, 0.2f, 0.3f, 0.4f, |
| 100 | -1.0f, -2.0f, -3.0f, -4.0f, |
| 101 | 1.0f, 2.0f, 3.0f, 4.0f |
| 102 | }; |
| 103 | |
| 104 | // Calculate output values for input. |
| 105 | auto f = [](float value) |
| 106 | { |
| 107 | return tanhf(value); |
| 108 | }; |
| 109 | std::vector<float> outputExpectedData(inputData.size()); |
| 110 | std::transform(inputData.begin(), inputData.end(), outputExpectedData.begin(), f); |
| 111 | |
| 112 | ActivationTest(tflite::BuiltinOperator_TANH, |
| 113 | backends, |
| 114 | inputData, |
| 115 | outputExpectedData); |
| 116 | } |
| 117 | |
Matthew Sloyan | 7515d07 | 2020-12-16 12:50:01 +0000 | [diff] [blame] | 118 | void ActivationEluTest(std::vector<armnn::BackendId>& backends) |
| 119 | { |
| 120 | std::vector<float> inputData = { |
| 121 | -0.1f, -0.2f, -0.3f, -0.4f, |
| 122 | 0.1f, 0.2f, 0.3f, 0.4f, |
| 123 | -1.0f, -2.0f, -3.0f, -4.0f, |
| 124 | 1.0f, 2.0f, 3.0f, 4.0f |
| 125 | }; |
| 126 | |
| 127 | // Calculate output values for input. |
| 128 | auto f = [](float value) |
| 129 | { |
| 130 | if (value < 0) |
| 131 | { |
| 132 | // alpha * (exp(x) - 1) |
| 133 | return 1 * (std::exp(value) - 1); |
| 134 | } |
| 135 | return value; |
| 136 | }; |
| 137 | std::vector<float> outputExpectedData(inputData.size()); |
| 138 | std::transform(inputData.begin(), inputData.end(), outputExpectedData.begin(), f); |
| 139 | |
| 140 | ActivationTest(tflite::BuiltinOperator_ELU, |
| 141 | backends, |
| 142 | inputData, |
| 143 | outputExpectedData); |
| 144 | } |
| 145 | |
| 146 | void ActivationHardSwishTest(std::vector<armnn::BackendId>& backends) |
| 147 | { |
| 148 | std::vector<float> inputData = { |
| 149 | -0.1f, -0.2f, -0.3f, -0.4f, |
| 150 | 0.1f, 0.2f, 0.3f, 0.4f, |
| 151 | -1.0f, -2.0f, -3.0f, -4.0f, |
| 152 | 1.0f, 2.0f, 3.0f, 4.0f |
| 153 | }; |
| 154 | |
| 155 | // Calculate output values for input. |
| 156 | auto f = [](float x) |
| 157 | { |
| 158 | // Break down the calculation to help with verification. |
| 159 | // hard_swish(x) = x * relu6(x+3) / 6 |
| 160 | // relu6(x) = min(max(x,0),6) |
| 161 | float reLu6_step1 = std::max((x + 3),0.0f); |
| 162 | float reLu6Complete = std::min(reLu6_step1, 6.0f); |
| 163 | float hardSwish_step1 = x * reLu6Complete; |
| 164 | float result = hardSwish_step1 / 6; |
| 165 | return result; |
| 166 | }; |
| 167 | std::vector<float> outputExpectedData(inputData.size()); |
| 168 | std::transform(inputData.begin(), inputData.end(), outputExpectedData.begin(), f); |
| 169 | |
| 170 | ActivationTest(tflite::BuiltinOperator_HARD_SWISH, |
| 171 | backends, |
| 172 | inputData, |
| 173 | outputExpectedData); |
| 174 | } |
| 175 | |
David Monahan | 0cf8442 | 2020-11-16 15:53:03 +0000 | [diff] [blame] | 176 | TEST_SUITE("Activation_CpuRefTests") |
| 177 | { |
| 178 | |
| 179 | TEST_CASE ("Activation_ReLu_CpuRef_Test") |
| 180 | { |
| 181 | std::vector<armnn::BackendId> backends = { armnn::Compute::CpuRef }; |
| 182 | ActivationReLuTest(backends); |
| 183 | } |
| 184 | |
| 185 | TEST_CASE ("Activation_Bounded_Relu6_CpuRef_Test") |
| 186 | { |
| 187 | std::vector<armnn::BackendId> backends = { armnn::Compute::CpuRef }; |
| 188 | ActivationBoundedReluTest(backends); |
| 189 | } |
| 190 | |
| 191 | TEST_CASE ("Activation_Sigmoid_CpuRef_Test") |
| 192 | { |
| 193 | std::vector<armnn::BackendId> backends = { armnn::Compute::CpuRef }; |
| 194 | ActivationSigmoidTest(backends); |
| 195 | } |
| 196 | |
David Monahan | 0cf8442 | 2020-11-16 15:53:03 +0000 | [diff] [blame] | 197 | TEST_CASE ("Activation_TanH_CpuRef_Test") |
| 198 | { |
| 199 | std::vector<armnn::BackendId> backends = { armnn::Compute::CpuRef }; |
| 200 | ActivationTanHTest(backends); |
| 201 | } |
| 202 | |
Matthew Sloyan | 7515d07 | 2020-12-16 12:50:01 +0000 | [diff] [blame] | 203 | TEST_CASE ("Activation_Elu_CpuRef_Test") |
| 204 | { |
| 205 | std::vector<armnn::BackendId> backends = { armnn::Compute::CpuRef }; |
| 206 | ActivationEluTest(backends); |
| 207 | } |
| 208 | |
| 209 | TEST_CASE ("Activation_HardSwish_CpuRef_Test") |
| 210 | { |
| 211 | std::vector<armnn::BackendId> backends = { armnn::Compute::CpuRef }; |
| 212 | ActivationHardSwishTest(backends); |
| 213 | } |
| 214 | |
David Monahan | 0cf8442 | 2020-11-16 15:53:03 +0000 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | TEST_SUITE("Activation_CpuAccTests") |
| 218 | { |
| 219 | |
| 220 | TEST_CASE ("Activation_ReLu_CpuAcc_Test") |
| 221 | { |
| 222 | std::vector<armnn::BackendId> backends = { armnn::Compute::CpuAcc }; |
| 223 | ActivationReLuTest(backends); |
| 224 | } |
| 225 | |
| 226 | TEST_CASE ("Activation_Bounded_Relu6_CpuAcc_Test") |
| 227 | { |
| 228 | std::vector<armnn::BackendId> backends = { armnn::Compute::CpuAcc }; |
| 229 | ActivationBoundedReluTest(backends); |
| 230 | } |
| 231 | |
| 232 | TEST_CASE ("Activation_Sigmoid_CpuAcc_Test") |
| 233 | { |
| 234 | std::vector<armnn::BackendId> backends = { armnn::Compute::CpuAcc }; |
| 235 | ActivationSigmoidTest(backends); |
| 236 | } |
| 237 | |
David Monahan | 0cf8442 | 2020-11-16 15:53:03 +0000 | [diff] [blame] | 238 | TEST_CASE ("Activation_TanH_CpuAcc_Test") |
| 239 | { |
| 240 | std::vector<armnn::BackendId> backends = { armnn::Compute::CpuAcc }; |
| 241 | ActivationTanHTest(backends); |
| 242 | } |
| 243 | |
Matthew Sloyan | 7515d07 | 2020-12-16 12:50:01 +0000 | [diff] [blame] | 244 | TEST_CASE ("Activation_Elu_CpuAcc_Test") |
| 245 | { |
| 246 | std::vector<armnn::BackendId> backends = { armnn::Compute::CpuAcc }; |
| 247 | ActivationEluTest(backends); |
| 248 | } |
| 249 | |
| 250 | TEST_CASE ("Activation_HardSwish_CpuAcc_Test") |
| 251 | { |
| 252 | std::vector<armnn::BackendId> backends = { armnn::Compute::CpuAcc }; |
| 253 | ActivationHardSwishTest(backends); |
| 254 | } |
| 255 | |
David Monahan | 0cf8442 | 2020-11-16 15:53:03 +0000 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | TEST_SUITE("Activation_GpuAccTests") |
| 259 | { |
| 260 | |
| 261 | TEST_CASE ("Activation_ReLu_GpuAcc_Test") |
| 262 | { |
| 263 | std::vector<armnn::BackendId> backends = { armnn::Compute::GpuAcc }; |
| 264 | ActivationReLuTest(backends); |
| 265 | } |
| 266 | |
| 267 | TEST_CASE ("Activation_Bounded_Relu6_GpuAcc_Test") |
| 268 | { |
| 269 | std::vector<armnn::BackendId> backends = { armnn::Compute::GpuAcc }; |
| 270 | ActivationBoundedReluTest(backends); |
| 271 | } |
| 272 | |
| 273 | TEST_CASE ("Activation_Sigmoid_GpuAcc_Test") |
| 274 | { |
| 275 | std::vector<armnn::BackendId> backends = { armnn::Compute::GpuAcc }; |
| 276 | ActivationSigmoidTest(backends); |
| 277 | } |
| 278 | |
David Monahan | 0cf8442 | 2020-11-16 15:53:03 +0000 | [diff] [blame] | 279 | TEST_CASE ("Activation_TanH_GpuAcc_Test") |
| 280 | { |
| 281 | std::vector<armnn::BackendId> backends = { armnn::Compute::GpuAcc }; |
| 282 | ActivationTanHTest(backends); |
| 283 | } |
| 284 | |
Matthew Sloyan | 7515d07 | 2020-12-16 12:50:01 +0000 | [diff] [blame] | 285 | TEST_CASE ("Activation_Elu_GpuAcc_Test") |
| 286 | { |
| 287 | std::vector<armnn::BackendId> backends = { armnn::Compute::GpuAcc }; |
| 288 | ActivationEluTest(backends); |
| 289 | } |
| 290 | |
| 291 | TEST_CASE ("Activation_HardSwish_GpuAcc_Test") |
| 292 | { |
| 293 | std::vector<armnn::BackendId> backends = { armnn::Compute::GpuAcc }; |
| 294 | ActivationHardSwishTest(backends); |
| 295 | } |
| 296 | |
David Monahan | 0cf8442 | 2020-11-16 15:53:03 +0000 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | } // namespace armnnDelegate |