Michalis Spyrou | aa51a5b | 2020-11-22 00:49:42 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (c) 2020 Arm Limited. |
| 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" |
| 28 | #include "src/core/common/StdTypes.h" |
| 29 | #include "src/core/common/Validate.h" |
| 30 | |
| 31 | #include <cmath> |
| 32 | #include <cstddef> |
| 33 | |
| 34 | #if defined(__ARM_FEATURE_SVE) |
| 35 | #include <arm_sve.h> |
| 36 | |
| 37 | namespace arm_compute |
| 38 | { |
| 39 | namespace cpu |
| 40 | { |
| 41 | void fp32_sve_activation(const ITensor *src, ITensor *dst, const ActivationLayerInfo &act_info, const Window &window) |
| 42 | { |
| 43 | const auto window_start_x = static_cast<int>(window.x().start()); |
| 44 | const auto window_end_x = static_cast<int>(window.x().end()); |
| 45 | const ActivationLayerInfo::ActivationFunction act = act_info.activation(); |
| 46 | |
| 47 | Window win_collapsed = window.collapse_if_possible(window, Window::DimZ); |
| 48 | win_collapsed.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 49 | |
| 50 | Iterator input(src, win_collapsed); |
| 51 | Iterator output(dst, win_collapsed); |
| 52 | |
| 53 | const auto const_1 = svdup_n_f32(1.f); |
| 54 | const auto const_0 = svdup_n_f32(0.f); |
| 55 | const auto const_6 = svdup_n_f32(6.f); |
| 56 | const auto const_3 = svdup_n_f32(3.f); |
| 57 | const auto const_inv_6 = svdup_n_f32(0.166666667f); |
| 58 | |
| 59 | const auto va = svdup_n_f32(act_info.a()); |
| 60 | const auto vb = svdup_n_f32(act_info.b()); |
| 61 | execute_window_loop(win_collapsed, [&](const Coordinates &) |
| 62 | { |
| 63 | const auto input_ptr = reinterpret_cast<const float *>(input.ptr()); |
| 64 | const auto output_ptr = reinterpret_cast<float *>(output.ptr()); |
| 65 | |
| 66 | svfloat32_t tmp; |
| 67 | |
| 68 | // Compute S elements per iteration |
| 69 | int x = window_start_x; |
| 70 | svbool_t pg = svwhilelt_b32(x, window_end_x); |
| 71 | do |
| 72 | { |
| 73 | const auto vin = svld1_f32(pg, input_ptr + x); |
| 74 | switch(act) |
| 75 | { |
| 76 | case ActivationLayerInfo::ActivationFunction::ABS: |
| 77 | tmp = svabs_f32_z(pg, vin); |
| 78 | break; |
| 79 | case ActivationLayerInfo::ActivationFunction::LINEAR: |
| 80 | tmp = svmla_f32_z(pg, vb, va, vin); |
| 81 | break; |
| 82 | case ActivationLayerInfo::ActivationFunction::LOGISTIC: |
| 83 | tmp = svinv_f32_z(pg, svadd_f32_z(pg, const_1, svexp_f32_z(pg, svneg_f32_z(pg, vin)))); |
| 84 | break; |
| 85 | case ActivationLayerInfo::ActivationFunction::RELU: |
| 86 | tmp = svmax_f32_z(pg, const_0, vin); |
| 87 | break; |
| 88 | case ActivationLayerInfo::ActivationFunction::BOUNDED_RELU: |
| 89 | tmp = svmin_f32_z(pg, va, svmax_f32_z(pg, const_0, vin)); |
| 90 | break; |
| 91 | case ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU: |
| 92 | tmp = svmin_f32_z(pg, va, svmax_f32_z(pg, vb, vin)); |
| 93 | break; |
| 94 | case ActivationLayerInfo::ActivationFunction::LEAKY_RELU: |
| 95 | tmp = svadd_f32_z(pg, svmul_f32_z(pg, svmin_f32_z(pg, vin, const_0), va), svmax_f32_z(pg, vin, const_0)); |
| 96 | break; |
| 97 | case ActivationLayerInfo::ActivationFunction::SOFT_RELU: |
| 98 | tmp = svlog_f32_z(pg, svadd_f32_z(pg, const_1, svexp_f32_z(pg, vin))); |
| 99 | break; |
| 100 | case ActivationLayerInfo::ActivationFunction::ELU: |
| 101 | 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))); |
| 102 | break; |
| 103 | case ActivationLayerInfo::ActivationFunction::SQRT: |
| 104 | tmp = svsqrt_f32_z(pg, vin); |
| 105 | break; |
| 106 | case ActivationLayerInfo::ActivationFunction::SQUARE: |
| 107 | tmp = svmul_f32_z(pg, vin, vin); |
| 108 | break; |
| 109 | case ActivationLayerInfo::ActivationFunction::TANH: |
| 110 | tmp = svmul_f32_z(pg, va, svtanh_f32_z(pg, svmul_f32_z(pg, vb, vin))); |
| 111 | break; |
| 112 | case ActivationLayerInfo::ActivationFunction::IDENTITY: |
| 113 | tmp = vin; |
| 114 | break; |
| 115 | case ActivationLayerInfo::ActivationFunction::HARD_SWISH: |
| 116 | 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))))); |
| 117 | break; |
| 118 | default: |
| 119 | ARM_COMPUTE_ERROR("Unsupported activation function"); |
| 120 | } |
| 121 | svst1_f32(pg, output_ptr + x, tmp); |
| 122 | |
| 123 | x += svcntw(); |
| 124 | pg = svwhilelt_b32(x, window_end_x); |
| 125 | |
| 126 | } |
| 127 | while(svptest_any(svptrue_b32(), pg)); |
| 128 | }, |
| 129 | input, output); |
| 130 | } |
| 131 | } // namespace cpu |
| 132 | } // namespace arm_compute |
| 133 | #endif // __ARM_FEATURE_SVE |