Michalis Spyrou | aa51a5b | 2020-11-22 00:49:42 +0000 | [diff] [blame] | 1 | /* |
Matthew Bentham | f1aeab9 | 2023-05-30 13:35:34 +0000 | [diff] [blame] | 2 | * Copyright (c) 2020-2023 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 "arm_compute/core/experimental/Types.h" |
SiCong Li | 9129549 | 2023-07-21 18:16:13 +0100 | [diff] [blame] | 28 | #include "arm_compute/function_info/ActivationLayerInfo.h" |
Michalis Spyrou | aa51a5b | 2020-11-22 00:49:42 +0000 | [diff] [blame] | 29 | |
| 30 | #include <cmath> |
| 31 | #include <cstddef> |
| 32 | |
Michalis Spyrou | aa51a5b | 2020-11-22 00:49:42 +0000 | [diff] [blame] | 33 | #include "src/core/NEON/SVEMath.h" |
| 34 | #include "src/core/NEON/SVESymm.h" |
| 35 | #include <arm_sve.h> |
| 36 | |
| 37 | namespace arm_compute |
| 38 | { |
| 39 | namespace cpu |
| 40 | { |
Dana Zlotnik | 3229171 | 2021-11-25 09:58:27 +0200 | [diff] [blame] | 41 | void sve2_qsymm16_activation(const ITensor *src, ITensor *dst, const ActivationLayerInfo &act_info, const Window &window) |
Michalis Spyrou | aa51a5b | 2020-11-22 00:49:42 +0000 | [diff] [blame] | 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 UniformQuantizationInfo qi_in = src->info()->quantization_info().uniform(); |
| 54 | const UniformQuantizationInfo qi_out = dst->info()->quantization_info().uniform(); |
| 55 | const auto vconst_1 = svdup_n_f32(1.f); |
| 56 | const auto va_f32 = svdup_n_f32(act_info.a()); |
| 57 | const auto vb_f32 = svdup_n_f32(act_info.b()); |
| 58 | |
| 59 | execute_window_loop(win_collapsed, [&](const Coordinates &) |
| 60 | { |
| 61 | const auto input_ptr = reinterpret_cast<const int16_t *>(input.ptr()); |
| 62 | const auto output_ptr = reinterpret_cast<int16_t *>(output.ptr()); |
| 63 | |
| 64 | svint16_t tmp; |
| 65 | |
| 66 | int x = window_start_x; |
| 67 | svbool_t pg = svwhilelt_b16(x, window_end_x); |
| 68 | do |
| 69 | { |
| 70 | const auto vin = svld1_s16(pg, input_ptr + x); |
| 71 | if(act == ActivationLayerInfo::ActivationFunction::LOGISTIC) |
| 72 | { |
| 73 | // De-quantize |
| 74 | auto vin_deq = svdequantize_qsymm16_z(pg, vin, qi_in.scale); |
| 75 | // Perform activation |
Michalis Spyrou | 168d6a8 | 2022-05-03 17:15:42 +0100 | [diff] [blame] | 76 | const svfloat32x2_t tmp_dep = svcreate2_f32(svdiv_f32_z(pg, vconst_1, svadd_f32_z(pg, vconst_1, svexp_f32_z(pg, svneg_f32_z(pg, svget2_f32(vin_deq, 0))))), |
| 77 | svdiv_f32_z(pg, vconst_1, svadd_f32_z(pg, vconst_1, svexp_f32_z(pg, svneg_f32_z(pg, svget2_f32(vin_deq, 1)))))); |
Michalis Spyrou | aa51a5b | 2020-11-22 00:49:42 +0000 | [diff] [blame] | 78 | // Re-quantize to new output space |
| 79 | tmp = svquantize_qsymm16_z(pg, tmp_dep, qi_out.scale); |
| 80 | } |
| 81 | else if(act == ActivationLayerInfo::ActivationFunction::TANH) |
| 82 | { |
| 83 | // De-quantize |
| 84 | auto vin_deq = svdequantize_qsymm16_z(pg, vin, qi_in.scale); |
| 85 | // Perform activation |
Michalis Spyrou | 168d6a8 | 2022-05-03 17:15:42 +0100 | [diff] [blame] | 86 | const svfloat32x2_t tmp_dep = svcreate2_f32(svmul_f32_z(pg, va_f32, svtanh_f32_z(pg, svmul_f32_z(pg, svget2_f32(vin_deq, 0), vb_f32))), |
| 87 | svmul_f32_z(pg, va_f32, svtanh_f32_z(pg, svmul_f32_z(pg, svget2_f32(vin_deq, 1), vb_f32)))); |
Michalis Spyrou | aa51a5b | 2020-11-22 00:49:42 +0000 | [diff] [blame] | 88 | // Re-quantize to new output space |
| 89 | tmp = svquantize_qsymm16_z(pg, tmp_dep, qi_out.scale); |
| 90 | } |
Pablo Marquez Tello | f55cca5 | 2022-04-06 14:31:25 +0100 | [diff] [blame] | 91 | else if(act == ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU) |
| 92 | { |
| 93 | // De-quantize |
| 94 | auto vin_deq = svdequantize_qsymm16_z(pg, vin, qi_in.scale); |
| 95 | // Perform activation |
Michalis Spyrou | 168d6a8 | 2022-05-03 17:15:42 +0100 | [diff] [blame] | 96 | const svfloat32x2_t tmp_dep = svcreate2_f32(svmin_f32_z(pg, va_f32, svmax_f32_z(pg, vb_f32, svget2_f32(vin_deq, 0))), |
| 97 | svmin_f32_z(pg, va_f32, svmax_f32_z(pg, vb_f32, svget2_f32(vin_deq, 1)))); |
Pablo Marquez Tello | f55cca5 | 2022-04-06 14:31:25 +0100 | [diff] [blame] | 98 | // Re-quantize to new output space |
| 99 | tmp = svquantize_qsymm16_z(pg, tmp_dep, qi_out.scale); |
| 100 | } |
Michalis Spyrou | aa51a5b | 2020-11-22 00:49:42 +0000 | [diff] [blame] | 101 | else |
| 102 | { |
| 103 | ARM_COMPUTE_ERROR("Unsupported activation function"); |
| 104 | } |
| 105 | |
| 106 | svst1_s16(pg, output_ptr + x, tmp); |
| 107 | |
| 108 | x += svcnth(); |
| 109 | pg = svwhilelt_b16(x, window_end_x); |
| 110 | |
| 111 | } |
| 112 | while(svptest_any(svptrue_b16(), pg)); |
| 113 | }, |
| 114 | input, output); |
| 115 | } |
| 116 | } // namespace cpu |
| 117 | } // namespace arm_compute |