Michalis Spyrou | aa51a5b | 2020-11-22 00:49:42 +0000 | [diff] [blame] | 1 | /* |
Georgios Pinitas | 70eb53b | 2021-01-06 19:42:21 +0000 | [diff] [blame^] | 2 | * Copyright (c) 2020-2021 Arm Limited. |
Michalis Spyrou | aa51a5b | 2020-11-22 00:49:42 +0000 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: MIT |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to |
| 8 | * deal in the Software without restriction, including without limitation the |
| 9 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 10 | * sell copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in all |
| 14 | * copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | * SOFTWARE. |
| 23 | */ |
| 24 | #include "arm_compute/core/Helpers.h" |
| 25 | #include "arm_compute/core/ITensorPack.h" |
| 26 | #include "arm_compute/core/Window.h" |
| 27 | #include "src/core/NEON/SVEMath.h" |
Michalis Spyrou | aa51a5b | 2020-11-22 00:49:42 +0000 | [diff] [blame] | 28 | #include "src/core/common/Validate.h" |
| 29 | |
| 30 | #include <cmath> |
| 31 | #include <cstddef> |
| 32 | |
| 33 | #if defined(__ARM_FEATURE_SVE) |
| 34 | #include <arm_sve.h> |
| 35 | |
| 36 | namespace arm_compute |
| 37 | { |
| 38 | namespace cpu |
| 39 | { |
| 40 | void fp32_sve_activation(const ITensor *src, ITensor *dst, const ActivationLayerInfo &act_info, const Window &window) |
| 41 | { |
| 42 | const auto window_start_x = static_cast<int>(window.x().start()); |
| 43 | const auto window_end_x = static_cast<int>(window.x().end()); |
| 44 | const ActivationLayerInfo::ActivationFunction act = act_info.activation(); |
| 45 | |
| 46 | Window win_collapsed = window.collapse_if_possible(window, Window::DimZ); |
| 47 | win_collapsed.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 48 | |
| 49 | Iterator input(src, win_collapsed); |
| 50 | Iterator output(dst, win_collapsed); |
| 51 | |
| 52 | const auto const_1 = svdup_n_f32(1.f); |
| 53 | const auto const_0 = svdup_n_f32(0.f); |
| 54 | const auto const_6 = svdup_n_f32(6.f); |
| 55 | const auto const_3 = svdup_n_f32(3.f); |
| 56 | const auto const_inv_6 = svdup_n_f32(0.166666667f); |
| 57 | |
| 58 | const auto va = svdup_n_f32(act_info.a()); |
| 59 | const auto vb = svdup_n_f32(act_info.b()); |
| 60 | execute_window_loop(win_collapsed, [&](const Coordinates &) |
| 61 | { |
| 62 | const auto input_ptr = reinterpret_cast<const float *>(input.ptr()); |
| 63 | const auto output_ptr = reinterpret_cast<float *>(output.ptr()); |
| 64 | |
| 65 | svfloat32_t tmp; |
| 66 | |
| 67 | // Compute S elements per iteration |
| 68 | int x = window_start_x; |
| 69 | svbool_t pg = svwhilelt_b32(x, window_end_x); |
| 70 | do |
| 71 | { |
| 72 | const auto vin = svld1_f32(pg, input_ptr + x); |
| 73 | switch(act) |
| 74 | { |
| 75 | case ActivationLayerInfo::ActivationFunction::ABS: |
| 76 | tmp = svabs_f32_z(pg, vin); |
| 77 | break; |
| 78 | case ActivationLayerInfo::ActivationFunction::LINEAR: |
| 79 | tmp = svmla_f32_z(pg, vb, va, vin); |
| 80 | break; |
| 81 | case ActivationLayerInfo::ActivationFunction::LOGISTIC: |
| 82 | tmp = svinv_f32_z(pg, svadd_f32_z(pg, const_1, svexp_f32_z(pg, svneg_f32_z(pg, vin)))); |
| 83 | break; |
| 84 | case ActivationLayerInfo::ActivationFunction::RELU: |
| 85 | tmp = svmax_f32_z(pg, const_0, vin); |
| 86 | break; |
| 87 | case ActivationLayerInfo::ActivationFunction::BOUNDED_RELU: |
| 88 | tmp = svmin_f32_z(pg, va, svmax_f32_z(pg, const_0, vin)); |
| 89 | break; |
| 90 | case ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU: |
| 91 | tmp = svmin_f32_z(pg, va, svmax_f32_z(pg, vb, vin)); |
| 92 | break; |
| 93 | case ActivationLayerInfo::ActivationFunction::LEAKY_RELU: |
| 94 | tmp = svadd_f32_z(pg, svmul_f32_z(pg, svmin_f32_z(pg, vin, const_0), va), svmax_f32_z(pg, vin, const_0)); |
| 95 | break; |
| 96 | case ActivationLayerInfo::ActivationFunction::SOFT_RELU: |
| 97 | tmp = svlog_f32_z(pg, svadd_f32_z(pg, const_1, svexp_f32_z(pg, vin))); |
| 98 | break; |
| 99 | case ActivationLayerInfo::ActivationFunction::ELU: |
| 100 | tmp = svsel_f32(svcmpgt_f32(pg, vin, const_0), vin, svmul_f32_z(pg, va, svsub_f32_z(pg, svexp_f32_z(pg, vin), const_1))); |
| 101 | break; |
| 102 | case ActivationLayerInfo::ActivationFunction::SQRT: |
| 103 | tmp = svsqrt_f32_z(pg, vin); |
| 104 | break; |
| 105 | case ActivationLayerInfo::ActivationFunction::SQUARE: |
| 106 | tmp = svmul_f32_z(pg, vin, vin); |
| 107 | break; |
| 108 | case ActivationLayerInfo::ActivationFunction::TANH: |
| 109 | tmp = svmul_f32_z(pg, va, svtanh_f32_z(pg, svmul_f32_z(pg, vb, vin))); |
| 110 | break; |
| 111 | case ActivationLayerInfo::ActivationFunction::IDENTITY: |
| 112 | tmp = vin; |
| 113 | break; |
| 114 | case ActivationLayerInfo::ActivationFunction::HARD_SWISH: |
| 115 | tmp = svmul_f32_z(pg, vin, svmul_f32_z(pg, const_inv_6, svmin_f32_z(pg, const_6, svmax_f32_z(pg, const_0, svadd_f32_z(pg, vin, const_3))))); |
| 116 | break; |
| 117 | default: |
| 118 | ARM_COMPUTE_ERROR("Unsupported activation function"); |
| 119 | } |
| 120 | svst1_f32(pg, output_ptr + x, tmp); |
| 121 | |
| 122 | x += svcntw(); |
| 123 | pg = svwhilelt_b32(x, window_end_x); |
| 124 | |
| 125 | } |
| 126 | while(svptest_any(svptrue_b32(), pg)); |
| 127 | }, |
| 128 | input, output); |
| 129 | } |
| 130 | } // namespace cpu |
| 131 | } // namespace arm_compute |
| 132 | #endif // __ARM_FEATURE_SVE |