Sadik Armagan | 0534e03 | 2020-10-27 17:30:18 +0000 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2020 Arm Ltd and Contributors. All rights reserved. |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #include "ElementwiseUnaryTestHelper.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 | TEST_SUITE("ElementwiseUnaryTest") |
| 23 | { |
| 24 | |
| 25 | TEST_CASE ("Abs_Float32_GpuAcc_Test") |
| 26 | { |
| 27 | // Create the ArmNN Delegate |
| 28 | std::vector<armnn::BackendId> backends = { armnn::Compute::GpuAcc, |
| 29 | armnn::Compute::CpuRef }; |
| 30 | // Set input data |
| 31 | std::vector<float> inputValues |
| 32 | { |
| 33 | -0.1f, -0.2f, -0.3f, |
| 34 | 0.1f, 0.2f, 0.3f |
| 35 | }; |
| 36 | // Calculate output data |
| 37 | std::vector<float> expectedOutputValues(inputValues.size()); |
| 38 | for (unsigned int i = 0; i < inputValues.size(); ++i) |
| 39 | { |
| 40 | expectedOutputValues[i] = std::abs(inputValues[i]); |
| 41 | } |
| 42 | ElementwiseUnaryFP32Test(tflite::BuiltinOperator_ABS, backends, inputValues, expectedOutputValues); |
| 43 | } |
| 44 | |
| 45 | TEST_CASE ("Abs_Float32_CpuAcc_Test") |
| 46 | { |
| 47 | // Create the ArmNN Delegate |
| 48 | std::vector<armnn::BackendId> backends = { armnn::Compute::CpuAcc, |
| 49 | armnn::Compute::CpuRef }; |
| 50 | // Set input data |
| 51 | std::vector<float> inputValues |
| 52 | { |
| 53 | -0.1f, -0.2f, -0.3f, |
| 54 | 0.1f, 0.2f, 0.3f |
| 55 | }; |
| 56 | // Calculate output data |
| 57 | std::vector<float> expectedOutputValues(inputValues.size()); |
| 58 | for (unsigned int i = 0; i < inputValues.size(); ++i) |
| 59 | { |
| 60 | expectedOutputValues[i] = std::abs(inputValues[i]); |
| 61 | } |
| 62 | |
| 63 | ElementwiseUnaryFP32Test(tflite::BuiltinOperator_ABS, backends, inputValues, expectedOutputValues); |
| 64 | } |
| 65 | |
| 66 | TEST_CASE ("Exp_Float32_GpuAcc_Test") |
| 67 | { |
| 68 | // Create the ArmNN Delegate |
| 69 | std::vector<armnn::BackendId> backends = { armnn::Compute::GpuAcc, |
| 70 | armnn::Compute::CpuRef }; |
| 71 | // Set input data |
| 72 | std::vector<float> inputValues |
| 73 | { |
| 74 | 5.0f, 4.0f, |
| 75 | 3.0f, 2.0f, |
| 76 | 1.0f, 1.1f |
| 77 | }; |
| 78 | // Set output data |
| 79 | std::vector<float> expectedOutputValues |
| 80 | { |
| 81 | 148.413159102577f, 54.598150033144f, |
| 82 | 20.085536923188f, 7.389056098931f, |
| 83 | 2.718281828459f, 3.004166023946f |
| 84 | }; |
| 85 | |
| 86 | ElementwiseUnaryFP32Test(tflite::BuiltinOperator_EXP, backends, inputValues, expectedOutputValues); |
| 87 | } |
| 88 | |
| 89 | TEST_CASE ("Exp_Float32_CpuAcc_Test") |
| 90 | { |
| 91 | // Create the ArmNN Delegate |
| 92 | std::vector<armnn::BackendId> backends = { armnn::Compute::CpuAcc, |
| 93 | armnn::Compute::CpuRef }; |
| 94 | // Set input data |
| 95 | std::vector<float> inputValues |
| 96 | { |
| 97 | 5.0f, 4.0f, |
| 98 | 3.0f, 2.0f, |
| 99 | 1.0f, 1.1f |
| 100 | }; |
| 101 | // Set output data |
| 102 | std::vector<float> expectedOutputValues |
| 103 | { |
| 104 | 148.413159102577f, 54.598150033144f, |
| 105 | 20.085536923188f, 7.389056098931f, |
| 106 | 2.718281828459f, 3.004166023946f |
| 107 | }; |
| 108 | |
| 109 | ElementwiseUnaryFP32Test(tflite::BuiltinOperator_EXP, backends, inputValues, expectedOutputValues); |
| 110 | } |
| 111 | |
| 112 | TEST_CASE ("Neg_Float32_GpuAcc_Test") |
| 113 | { |
| 114 | // Create the ArmNN Delegate |
| 115 | std::vector<armnn::BackendId> backends = { armnn::Compute::GpuAcc, |
| 116 | armnn::Compute::CpuRef }; |
| 117 | // Set input data |
| 118 | std::vector<float> inputValues |
| 119 | { |
| 120 | 1.f, 0.f, 3.f, |
| 121 | 25.f, 64.f, 100.f |
| 122 | }; |
| 123 | // Set output data |
| 124 | std::vector<float> expectedOutputValues |
| 125 | { |
| 126 | -1.f, 0.f, -3.f, |
| 127 | -25.f, -64.f, -100.f |
| 128 | }; |
| 129 | |
| 130 | ElementwiseUnaryFP32Test(tflite::BuiltinOperator_NEG, backends, inputValues, expectedOutputValues); |
| 131 | } |
| 132 | |
| 133 | TEST_CASE ("Neg_Float32_CpuAcc_Test") |
| 134 | { |
| 135 | // Create the ArmNN Delegate |
| 136 | std::vector<armnn::BackendId> backends = { armnn::Compute::CpuAcc, |
| 137 | armnn::Compute::CpuRef }; |
| 138 | // Set input data |
| 139 | std::vector<float> inputValues |
| 140 | { |
| 141 | 1.f, 0.f, 3.f, |
| 142 | 25.f, 64.f, 100.f |
| 143 | }; |
| 144 | // Set output data |
| 145 | std::vector<float> expectedOutputValues |
| 146 | { |
| 147 | -1.f, 0.f, -3.f, |
| 148 | -25.f, -64.f, -100.f |
| 149 | }; |
| 150 | |
| 151 | ElementwiseUnaryFP32Test(tflite::BuiltinOperator_NEG, backends, inputValues, expectedOutputValues); |
| 152 | } |
| 153 | |
| 154 | TEST_CASE ("Rsqrt_Float32_GpuAcc_Test") |
| 155 | { |
| 156 | // Create the ArmNN Delegate |
| 157 | std::vector<armnn::BackendId> backends = { armnn::Compute::GpuAcc, |
| 158 | armnn::Compute::CpuRef }; |
| 159 | // Set input data |
| 160 | std::vector<float> inputValues |
| 161 | { |
| 162 | 1.f, 4.f, 16.f, |
| 163 | 25.f, 64.f, 100.f |
| 164 | }; |
| 165 | // Set output data |
| 166 | std::vector<float> expectedOutputValues |
| 167 | { |
| 168 | 1.f, 0.5f, 0.25f, |
| 169 | 0.2f, 0.125f, 0.1f |
| 170 | }; |
| 171 | |
| 172 | ElementwiseUnaryFP32Test(tflite::BuiltinOperator_RSQRT, backends, inputValues, expectedOutputValues); |
| 173 | } |
| 174 | |
| 175 | TEST_CASE ("Rsqrt_Float32_CpuAcc_Test") |
| 176 | { |
| 177 | // Create the ArmNN Delegate |
| 178 | std::vector<armnn::BackendId> backends = { armnn::Compute::CpuAcc, |
| 179 | armnn::Compute::CpuRef }; |
| 180 | // Set input data |
| 181 | std::vector<float> inputValues |
| 182 | { |
| 183 | 1.f, 4.f, 16.f, |
| 184 | 25.f, 64.f, 100.f |
| 185 | }; |
| 186 | // Set output data |
| 187 | std::vector<float> expectedOutputValues |
| 188 | { |
| 189 | 1.f, 0.5f, 0.25f, |
| 190 | 0.2f, 0.125f, 0.1f |
| 191 | }; |
| 192 | |
| 193 | ElementwiseUnaryFP32Test(tflite::BuiltinOperator_RSQRT, backends, inputValues, expectedOutputValues); |
| 194 | } |
| 195 | |
| 196 | TEST_CASE ("Sqrt_Float32_GpuAcc_Test") |
| 197 | { |
| 198 | // Create the ArmNN Delegate |
| 199 | std::vector<armnn::BackendId> backends = { armnn::Compute::GpuAcc, |
| 200 | armnn::Compute::CpuRef }; |
| 201 | // Set input data |
| 202 | std::vector<float> inputValues |
| 203 | { |
| 204 | 9.0f, 4.25f, 81.9f, |
| 205 | 0.1f, 0.9f, 169.0f |
| 206 | }; |
| 207 | // Calculate output data |
| 208 | std::vector<float> expectedOutputValues(inputValues.size()); |
| 209 | for (unsigned int i = 0; i < inputValues.size(); ++i) |
| 210 | { |
| 211 | expectedOutputValues[i] = std::sqrt(inputValues[i]); |
| 212 | } |
| 213 | |
| 214 | ElementwiseUnaryFP32Test(tflite::BuiltinOperator_SQRT, backends, inputValues, expectedOutputValues); |
| 215 | } |
| 216 | |
| 217 | TEST_CASE ("Sqrt_Float32_CpuAcc_Test") |
| 218 | { |
| 219 | std::vector<armnn::BackendId> backends = { armnn::Compute::CpuAcc, |
| 220 | armnn::Compute::CpuRef }; |
| 221 | // Set input data |
| 222 | std::vector<float> inputValues |
| 223 | { |
| 224 | 9.0f, 4.25f, 81.9f, |
| 225 | 0.1f, 0.9f, 169.0f |
| 226 | }; |
| 227 | // Calculate output data |
| 228 | std::vector<float> expectedOutputValues(inputValues.size()); |
| 229 | for (unsigned int i = 0; i < inputValues.size(); ++i) |
| 230 | { |
| 231 | expectedOutputValues[i] = std::sqrt(inputValues[i]); |
| 232 | } |
| 233 | |
| 234 | ElementwiseUnaryFP32Test(tflite::BuiltinOperator_SQRT, backends, inputValues, expectedOutputValues); |
| 235 | } |
| 236 | |
| 237 | } |
| 238 | |
| 239 | } // namespace armnnDelegate |