Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
SiCong Li | c0463a2 | 2023-05-17 13:46:13 +0100 | [diff] [blame] | 2 | * Copyright (c) 2017-2023 Arm Limited. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [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 | */ |
Georgios Pinitas | 7891a73 | 2021-08-20 21:39:25 +0100 | [diff] [blame] | 24 | #include "src/cpu/kernels/CpuActivationKernel.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 25 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 26 | #include "arm_compute/core/ITensor.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 27 | #include "arm_compute/core/TensorInfo.h" |
| 28 | #include "arm_compute/core/Utils.h" |
Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 29 | #include "src/core/CPP/Validate.h" |
Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 30 | #include "src/core/helpers/AutoConfiguration.h" |
| 31 | #include "src/core/helpers/WindowHelpers.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 32 | |
Michalis Spyrou | c4d4555 | 2020-10-19 12:41:30 +0100 | [diff] [blame] | 33 | #include "src/core/common/Registrars.h" |
Georgios Pinitas | 7891a73 | 2021-08-20 21:39:25 +0100 | [diff] [blame] | 34 | #include "src/cpu/kernels/activation/list.h" |
Michalis Spyrou | c4d4555 | 2020-10-19 12:41:30 +0100 | [diff] [blame] | 35 | |
Georgios Pinitas | f8f0442 | 2021-01-08 17:25:55 +0000 | [diff] [blame] | 36 | #include <array> |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 37 | |
Michele Di Giorgio | f9b595a | 2020-07-03 13:34:52 +0100 | [diff] [blame] | 38 | namespace arm_compute |
| 39 | { |
Georgios Pinitas | f8f0442 | 2021-01-08 17:25:55 +0000 | [diff] [blame] | 40 | namespace cpu |
| 41 | { |
| 42 | namespace kernels |
| 43 | { |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 44 | namespace |
| 45 | { |
Giorgio Arena | 5ae8d80 | 2021-11-18 18:02:13 +0000 | [diff] [blame] | 46 | static const std::vector<CpuActivationKernel::ActivationKernel> available_kernels = |
Michalis Spyrou | c4d4555 | 2020-10-19 12:41:30 +0100 | [diff] [blame] | 47 | { |
Pablo Marquez Tello | d158609 | 2022-06-10 14:37:10 +0100 | [diff] [blame] | 48 | #ifdef ARM_COMPUTE_ENABLE_SVE |
| 49 | { |
SiCong Li | c0463a2 | 2023-05-17 13:46:13 +0100 | [diff] [blame] | 50 | "sve2_q8_activation_lut", |
Pablo Marquez Tello | 48cfd5f | 2023-06-09 11:22:29 +0100 | [diff] [blame] | 51 | [](const ActivationDataTypeISASelectorData & data) { return (data.dt == DataType::QASYMM8 || data.dt == DataType::QASYMM8_SIGNED) && data.cpumodel == CPUModel::A510 && data.isa.sve2; }, |
SiCong Li | c0463a2 | 2023-05-17 13:46:13 +0100 | [diff] [blame] | 52 | REGISTER_QASYMM8_SVE2(arm_compute::cpu::sve2_q8_activation_lut) |
Pablo Marquez Tello | d158609 | 2022-06-10 14:37:10 +0100 | [diff] [blame] | 53 | }, |
| 54 | #endif // ARM_COMPUTE_ENABLE_SVE |
Viet-Hoa Do | b042e39 | 2022-06-21 15:56:15 +0100 | [diff] [blame] | 55 | #ifdef __aarch64__ |
Murray Kornelsen | 926f502 | 2022-07-13 21:22:39 -0400 | [diff] [blame] | 56 | { |
| 57 | // Neon LUT implementantion takes precedence |
Viet-Hoa Do | 29db3d2 | 2022-08-10 11:56:49 +0100 | [diff] [blame] | 58 | "neon_q8_activation_lut", |
Pablo Marquez Tello | 48cfd5f | 2023-06-09 11:22:29 +0100 | [diff] [blame] | 59 | [](const ActivationDataTypeISASelectorData & data) { return data.dt == DataType::QASYMM8 || data.dt == DataType::QASYMM8_SIGNED; }, |
Viet-Hoa Do | 29db3d2 | 2022-08-10 11:56:49 +0100 | [diff] [blame] | 60 | REGISTER_Q8_NEON(arm_compute::cpu::neon_q8_activation_lut) |
Pablo Marquez Tello | 700b913 | 2022-06-22 11:23:53 +0100 | [diff] [blame] | 61 | }, |
Viet-Hoa Do | b042e39 | 2022-06-21 15:56:15 +0100 | [diff] [blame] | 62 | #endif // __aarch64__ |
Michalis Spyrou | aa51a5b | 2020-11-22 00:49:42 +0000 | [diff] [blame] | 63 | { |
Dana Zlotnik | a538ae5 | 2022-02-21 13:12:41 +0200 | [diff] [blame] | 64 | "sve2_qu8_activation", |
Murray Kornelsen | 926f502 | 2022-07-13 21:22:39 -0400 | [diff] [blame] | 65 | [](const ActivationDataTypeISASelectorData & data) { return data.dt == DataType::QASYMM8 && data.isa.sve2 && data.f != ActivationLayerInfo::ActivationFunction::GELU; }, |
Dana Zlotnik | a538ae5 | 2022-02-21 13:12:41 +0200 | [diff] [blame] | 66 | REGISTER_QASYMM8_SVE2(arm_compute::cpu::sve2_qasymm8_activation) |
| 67 | }, |
| 68 | { |
| 69 | "sve2_qs8_activation", |
Murray Kornelsen | 926f502 | 2022-07-13 21:22:39 -0400 | [diff] [blame] | 70 | [](const ActivationDataTypeISASelectorData & data) { return data.dt == DataType::QASYMM8_SIGNED && data.isa.sve2 && data.f != ActivationLayerInfo::ActivationFunction::GELU; }, |
Dana Zlotnik | a538ae5 | 2022-02-21 13:12:41 +0200 | [diff] [blame] | 71 | REGISTER_QASYMM8_SIGNED_SVE2(arm_compute::cpu::sve2_qasymm8_signed_activation) |
| 72 | }, |
| 73 | { |
| 74 | "sve2_qs16_activation", |
Murray Kornelsen | 926f502 | 2022-07-13 21:22:39 -0400 | [diff] [blame] | 75 | [](const ActivationDataTypeISASelectorData & data) { return data.dt == DataType::QSYMM16 && data.isa.sve2 && data.f != ActivationLayerInfo::ActivationFunction::GELU; }, |
Dana Zlotnik | a538ae5 | 2022-02-21 13:12:41 +0200 | [diff] [blame] | 76 | REGISTER_QSYMM16_SVE2(arm_compute::cpu::sve2_qsymm16_activation) |
| 77 | }, |
| 78 | { |
Georgios Pinitas | 5fdde99 | 2021-06-25 05:42:57 +0100 | [diff] [blame] | 79 | "sve_fp16_activation", |
Murray Kornelsen | 926f502 | 2022-07-13 21:22:39 -0400 | [diff] [blame] | 80 | [](const ActivationDataTypeISASelectorData & data) { return data.dt == DataType::F16 && data.isa.sve && data.isa.fp16 && data.f != ActivationLayerInfo::ActivationFunction::GELU; }, |
Dana Zlotnik | 3229171 | 2021-11-25 09:58:27 +0200 | [diff] [blame] | 81 | REGISTER_FP16_SVE(arm_compute::cpu::sve_fp16_activation) |
Michalis Spyrou | aa51a5b | 2020-11-22 00:49:42 +0000 | [diff] [blame] | 82 | }, |
| 83 | { |
Georgios Pinitas | 5fdde99 | 2021-06-25 05:42:57 +0100 | [diff] [blame] | 84 | "sve_fp32_activation", |
Murray Kornelsen | 926f502 | 2022-07-13 21:22:39 -0400 | [diff] [blame] | 85 | [](const ActivationDataTypeISASelectorData & data) { return data.dt == DataType::F32 && data.isa.sve && data.f != ActivationLayerInfo::ActivationFunction::GELU; }, |
Dana Zlotnik | 3229171 | 2021-11-25 09:58:27 +0200 | [diff] [blame] | 86 | REGISTER_FP32_SVE(arm_compute::cpu::sve_fp32_activation) |
Michalis Spyrou | aa51a5b | 2020-11-22 00:49:42 +0000 | [diff] [blame] | 87 | }, |
Michalis Spyrou | c4d4555 | 2020-10-19 12:41:30 +0100 | [diff] [blame] | 88 | { |
Georgios Pinitas | 5fdde99 | 2021-06-25 05:42:57 +0100 | [diff] [blame] | 89 | "neon_fp16_activation", |
Pablo Marquez Tello | d75cd8a | 2022-05-26 14:19:39 +0100 | [diff] [blame] | 90 | [](const ActivationDataTypeISASelectorData & data) { return data.dt == DataType::F16 && data.isa.fp16; }, |
Dana Zlotnik | 3229171 | 2021-11-25 09:58:27 +0200 | [diff] [blame] | 91 | REGISTER_FP16_NEON(arm_compute::cpu::neon_fp16_activation) |
Michalis Spyrou | c4d4555 | 2020-10-19 12:41:30 +0100 | [diff] [blame] | 92 | }, |
| 93 | { |
Georgios Pinitas | 5fdde99 | 2021-06-25 05:42:57 +0100 | [diff] [blame] | 94 | "neon_fp32_activation", |
Pablo Marquez Tello | d75cd8a | 2022-05-26 14:19:39 +0100 | [diff] [blame] | 95 | [](const ActivationDataTypeISASelectorData & data) { return data.dt == DataType::F32; }, |
Dana Zlotnik | 3229171 | 2021-11-25 09:58:27 +0200 | [diff] [blame] | 96 | REGISTER_FP32_NEON(arm_compute::cpu::neon_fp32_activation) |
Michalis Spyrou | c4d4555 | 2020-10-19 12:41:30 +0100 | [diff] [blame] | 97 | }, |
Michalis Spyrou | aa51a5b | 2020-11-22 00:49:42 +0000 | [diff] [blame] | 98 | { |
Georgios Pinitas | 5fdde99 | 2021-06-25 05:42:57 +0100 | [diff] [blame] | 99 | "neon_qu8_activation", |
Viet-Hoa Do | b042e39 | 2022-06-21 15:56:15 +0100 | [diff] [blame] | 100 | [](const ActivationDataTypeISASelectorData & data) { return data.dt == DataType::QASYMM8; }, |
Dana Zlotnik | 3229171 | 2021-11-25 09:58:27 +0200 | [diff] [blame] | 101 | REGISTER_QASYMM8_NEON(arm_compute::cpu::neon_qasymm8_activation) |
Michalis Spyrou | c4d4555 | 2020-10-19 12:41:30 +0100 | [diff] [blame] | 102 | }, |
| 103 | { |
Georgios Pinitas | 5fdde99 | 2021-06-25 05:42:57 +0100 | [diff] [blame] | 104 | "neon_qs8_activation", |
Pablo Marquez Tello | d75cd8a | 2022-05-26 14:19:39 +0100 | [diff] [blame] | 105 | [](const ActivationDataTypeISASelectorData & data) { return data.dt == DataType::QASYMM8_SIGNED; }, |
Dana Zlotnik | 3229171 | 2021-11-25 09:58:27 +0200 | [diff] [blame] | 106 | REGISTER_QASYMM8_SIGNED_NEON(arm_compute::cpu::neon_qasymm8_signed_activation) |
Michalis Spyrou | c4d4555 | 2020-10-19 12:41:30 +0100 | [diff] [blame] | 107 | }, |
| 108 | { |
Georgios Pinitas | 5fdde99 | 2021-06-25 05:42:57 +0100 | [diff] [blame] | 109 | "neon_qs16_activation", |
Pablo Marquez Tello | d75cd8a | 2022-05-26 14:19:39 +0100 | [diff] [blame] | 110 | [](const ActivationDataTypeISASelectorData & data) { return data.dt == DataType::QSYMM16; }, |
Dana Zlotnik | 3229171 | 2021-11-25 09:58:27 +0200 | [diff] [blame] | 111 | REGISTER_QSYMM16_NEON(arm_compute::cpu::neon_qsymm16_activation) |
Michalis Spyrou | c4d4555 | 2020-10-19 12:41:30 +0100 | [diff] [blame] | 112 | }, |
| 113 | }; |
| 114 | |
Georgios Pinitas | f8f0442 | 2021-01-08 17:25:55 +0000 | [diff] [blame] | 115 | /* Supported activation in the 8-bit integer domain */ |
Murray Kornelsen | 926f502 | 2022-07-13 21:22:39 -0400 | [diff] [blame] | 116 | static const std::array<ActivationLayerInfo::ActivationFunction, 8> qasymm8_activations = |
Georgios Pinitas | f8f0442 | 2021-01-08 17:25:55 +0000 | [diff] [blame] | 117 | { |
| 118 | ActivationLayerInfo::ActivationFunction::RELU, |
| 119 | ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, |
| 120 | ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, |
| 121 | ActivationLayerInfo::ActivationFunction::LOGISTIC, |
| 122 | ActivationLayerInfo::ActivationFunction::TANH, |
| 123 | ActivationLayerInfo::ActivationFunction::HARD_SWISH, |
| 124 | ActivationLayerInfo::ActivationFunction::LEAKY_RELU, |
Murray Kornelsen | 926f502 | 2022-07-13 21:22:39 -0400 | [diff] [blame] | 125 | ActivationLayerInfo::ActivationFunction::GELU, |
Georgios Pinitas | f8f0442 | 2021-01-08 17:25:55 +0000 | [diff] [blame] | 126 | }; |
| 127 | /* Supported activation in the 16-bit integer domain */ |
Pablo Marquez Tello | f55cca5 | 2022-04-06 14:31:25 +0100 | [diff] [blame] | 128 | static const std::array<ActivationLayerInfo::ActivationFunction, 4> qsymm16_activations = |
Georgios Pinitas | f8f0442 | 2021-01-08 17:25:55 +0000 | [diff] [blame] | 129 | { |
| 130 | ActivationLayerInfo::ActivationFunction::LOGISTIC, |
| 131 | ActivationLayerInfo::ActivationFunction::TANH, |
Pablo Marquez Tello | f55cca5 | 2022-04-06 14:31:25 +0100 | [diff] [blame] | 132 | ActivationLayerInfo::ActivationFunction::HARD_SWISH, |
| 133 | ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU |
Georgios Pinitas | f8f0442 | 2021-01-08 17:25:55 +0000 | [diff] [blame] | 134 | }; |
| 135 | |
Michele Di Giorgio | bd2c8e1 | 2021-01-19 15:29:02 +0000 | [diff] [blame] | 136 | Status validate_arguments(const ITensorInfo *src, const ITensorInfo *dst, const ActivationLayerInfo &activation_info) |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 137 | { |
Michele Di Giorgio | bd2c8e1 | 2021-01-19 15:29:02 +0000 | [diff] [blame] | 138 | ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(src); |
| 139 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src, 1, DataType::QASYMM8_SIGNED, DataType::QASYMM8, DataType::QSYMM16, DataType::F16, DataType::F32); |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 140 | |
Pablo Marquez Tello | d158609 | 2022-06-10 14:37:10 +0100 | [diff] [blame] | 141 | const auto *uk = CpuActivationKernel::get_implementation(ActivationDataTypeISASelectorData{ src->data_type(), CPUInfo::get().get_cpu_model(), CPUInfo::get().get_isa(), activation_info.activation() }); |
Michalis Spyrou | c4d4555 | 2020-10-19 12:41:30 +0100 | [diff] [blame] | 142 | ARM_COMPUTE_RETURN_ERROR_ON(uk == nullptr || uk->ukernel == nullptr); |
| 143 | |
Michele Di Giorgio | bd2c8e1 | 2021-01-19 15:29:02 +0000 | [diff] [blame] | 144 | const DataType data_type = src->data_type(); |
| 145 | const QuantizationInfo &oq_info = (dst != nullptr) ? dst->quantization_info() : src->quantization_info(); |
Georgios Pinitas | 4b3fba1 | 2019-06-04 17:31:46 +0100 | [diff] [blame] | 146 | const ActivationLayerInfo::ActivationFunction f_act = activation_info.activation(); |
| 147 | |
Georgios Pinitas | f8f0442 | 2021-01-08 17:25:55 +0000 | [diff] [blame] | 148 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(is_data_type_quantized_asymmetric(data_type) && (std::find(std::begin(qasymm8_activations), std::end(qasymm8_activations), f_act) == std::end(qasymm8_activations)), |
Sang-Hoon Park | add8e81 | 2020-11-25 11:46:03 +0000 | [diff] [blame] | 149 | "For QASYMM8 only hard swish, leaky relu, tanh, logistic, relu and lower/upper bounded relu are supported"); |
giuros01 | c9573f3 | 2019-06-20 10:30:17 +0100 | [diff] [blame] | 150 | |
Georgios Pinitas | f8f0442 | 2021-01-08 17:25:55 +0000 | [diff] [blame] | 151 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(is_data_type_quantized_symmetric(data_type) && (std::find(std::begin(qsymm16_activations), std::end(qsymm16_activations), f_act) == std::end(qsymm16_activations)), |
giuros01 | c9573f3 | 2019-06-20 10:30:17 +0100 | [diff] [blame] | 152 | "For QSYMM16 only tanh and logistic are supported"); |
Michalis Spyrou | 8d4d1b8 | 2019-11-28 11:31:23 +0000 | [diff] [blame] | 153 | ARM_COMPUTE_RETURN_ERROR_ON((data_type == DataType::QASYMM8 || data_type == DataType::QASYMM16) && (f_act == ActivationLayerInfo::ActivationFunction::TANH) |
| 154 | && (oq_info != QuantizationInfo(1.f / 128.f, 128))); |
| 155 | ARM_COMPUTE_RETURN_ERROR_ON((data_type == DataType::QASYMM8 || data_type == DataType::QASYMM16) && (f_act == ActivationLayerInfo::ActivationFunction::LOGISTIC) |
| 156 | && (oq_info != QuantizationInfo(1.f / 256.f, 0))); |
| 157 | |
| 158 | ARM_COMPUTE_RETURN_ERROR_ON(data_type == DataType::QASYMM8_SIGNED && (f_act == ActivationLayerInfo::ActivationFunction::TANH) && (oq_info != QuantizationInfo(1.f / 128.f, 0))); |
| 159 | ARM_COMPUTE_RETURN_ERROR_ON(data_type == DataType::QASYMM8_SIGNED && (f_act == ActivationLayerInfo::ActivationFunction::LOGISTIC) && (oq_info != QuantizationInfo(1.f / 256.f, -128))); |
Georgios Pinitas | 4b3fba1 | 2019-06-04 17:31:46 +0100 | [diff] [blame] | 160 | |
giuros01 | c9573f3 | 2019-06-20 10:30:17 +0100 | [diff] [blame] | 161 | ARM_COMPUTE_RETURN_ERROR_ON(is_data_type_quantized_symmetric(data_type) && (f_act == ActivationLayerInfo::ActivationFunction::TANH) && (oq_info != QuantizationInfo(1.f / 32768.f, 0))); |
| 162 | ARM_COMPUTE_RETURN_ERROR_ON(is_data_type_quantized_symmetric(data_type) && (f_act == ActivationLayerInfo::ActivationFunction::LOGISTIC) && (oq_info != QuantizationInfo(1.f / 32768.f, 0))); |
| 163 | |
Michele Di Giorgio | bd2c8e1 | 2021-01-19 15:29:02 +0000 | [diff] [blame] | 164 | // Checks performed when dst is configured |
| 165 | if((dst != nullptr) && (dst->total_size() != 0)) |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 166 | { |
Michele Di Giorgio | bd2c8e1 | 2021-01-19 15:29:02 +0000 | [diff] [blame] | 167 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(src, dst); |
| 168 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src, dst); |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | return Status{}; |
| 172 | } |
| 173 | |
Michele Di Giorgio | bd2c8e1 | 2021-01-19 15:29:02 +0000 | [diff] [blame] | 174 | std::pair<Status, Window> validate_and_configure_window(const ITensorInfo *src, ITensorInfo *dst) |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 175 | { |
Georgios Pinitas | 5a59453 | 2018-12-03 14:30:05 +0000 | [diff] [blame] | 176 | // Configure kernel window |
Michele Di Giorgio | bd2c8e1 | 2021-01-19 15:29:02 +0000 | [diff] [blame] | 177 | Window win = calculate_max_window(*src, Steps()); |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 178 | |
Michele Di Giorgio | bd2c8e1 | 2021-01-19 15:29:02 +0000 | [diff] [blame] | 179 | if(dst != nullptr) |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 180 | { |
Michele Di Giorgio | bd2c8e1 | 2021-01-19 15:29:02 +0000 | [diff] [blame] | 181 | // dst auto inizialitation if not yet initialized |
| 182 | auto_init_if_empty(*dst, *src->clone()); |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 183 | } |
| 184 | |
Georgios Pinitas | 5a59453 | 2018-12-03 14:30:05 +0000 | [diff] [blame] | 185 | return std::make_pair(Status{}, win); |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 186 | } |
Pablo Marquez Tello | 48cfd5f | 2023-06-09 11:22:29 +0100 | [diff] [blame] | 187 | #ifdef __aarch64__ |
| 188 | void init_lut(ActivationLayerInfo::ActivationFunction act_func, DataType data_type, |
| 189 | const UniformQuantizationInfo &qi_in, const UniformQuantizationInfo &qi_out, |
| 190 | ActivationLayerInfo::LookupTable256 &lut, float a, float b) |
| 191 | { |
| 192 | for(size_t i = 0; i < lut.size(); ++i) |
| 193 | { |
| 194 | float tmp_f = (data_type == DataType::QASYMM8) ? dequantize_qasymm8(i, qi_in) : dequantize_qasymm8_signed(i, qi_in); |
| 195 | switch(act_func) |
| 196 | { |
| 197 | case ActivationLayerInfo::ActivationFunction::HARD_SWISH: |
| 198 | tmp_f = tmp_f * ((std::min(std::max((tmp_f + 3), 0.0f), 6.0f)) * 0.166666667f); |
| 199 | break; |
| 200 | case ActivationLayerInfo::ActivationFunction::LEAKY_RELU: |
| 201 | tmp_f = tmp_f > 0 ? tmp_f : tmp_f * a; |
| 202 | break; |
| 203 | case ActivationLayerInfo::ActivationFunction::LOGISTIC: |
| 204 | tmp_f = 1.f / (1.f + std::exp(-tmp_f)); |
| 205 | break; |
| 206 | case ActivationLayerInfo::ActivationFunction::ABS: |
| 207 | tmp_f = std::abs(tmp_f); |
| 208 | break; |
| 209 | case ActivationLayerInfo::ActivationFunction::LINEAR: |
| 210 | tmp_f = a * tmp_f + b; |
| 211 | break; |
| 212 | case ActivationLayerInfo::ActivationFunction::RELU: |
| 213 | tmp_f = std::max<>(0.f, tmp_f); |
| 214 | break; |
| 215 | case ActivationLayerInfo::ActivationFunction::BOUNDED_RELU: |
| 216 | tmp_f = std::min<>(a, std::max(0.f, tmp_f)); |
| 217 | break; |
| 218 | case ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU: |
| 219 | tmp_f = std::min<>(a, std::max<>(b, tmp_f)); |
| 220 | break; |
| 221 | case ActivationLayerInfo::ActivationFunction::SOFT_RELU: |
| 222 | tmp_f = (tmp_f > 12.f) ? tmp_f : std::log(1.f + std::exp(tmp_f)); |
| 223 | break; |
| 224 | case ActivationLayerInfo::ActivationFunction::ELU: |
| 225 | tmp_f = (tmp_f >= 0) ? tmp_f : a * (std::exp(tmp_f) - 1); |
| 226 | break; |
| 227 | case ActivationLayerInfo::ActivationFunction::SQRT: |
| 228 | tmp_f = std::sqrt(tmp_f); |
| 229 | break; |
| 230 | case ActivationLayerInfo::ActivationFunction::SQUARE: |
| 231 | tmp_f = tmp_f * tmp_f; |
| 232 | break; |
| 233 | case ActivationLayerInfo::ActivationFunction::TANH: |
| 234 | tmp_f = a * std::tanh(b * tmp_f); |
| 235 | break; |
| 236 | case ActivationLayerInfo::ActivationFunction::IDENTITY: |
| 237 | break; |
| 238 | case ActivationLayerInfo::ActivationFunction::SWISH: |
| 239 | tmp_f = tmp_f / (1.f + std::exp(-a * tmp_f)); |
| 240 | break; |
| 241 | case ActivationLayerInfo::ActivationFunction::GELU: |
| 242 | tmp_f = tmp_f * (0.5f * (1.0f + erff(tmp_f / 1.41421356237f))); |
| 243 | break; |
| 244 | default: |
| 245 | ARM_COMPUTE_ERROR("Not supported"); |
| 246 | tmp_f = 0; |
| 247 | break; |
| 248 | } |
| 249 | lut[i] = (data_type == DataType::QASYMM8) ? quantize_qasymm8(tmp_f, qi_out) : quantize_qasymm8_signed(tmp_f, qi_out); |
| 250 | } |
| 251 | } |
| 252 | #endif // __aarch64__ |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 253 | } // namespace |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 254 | |
Michele Di Giorgio | bd2c8e1 | 2021-01-19 15:29:02 +0000 | [diff] [blame] | 255 | void CpuActivationKernel::configure(const ITensorInfo *src, ITensorInfo *dst, ActivationLayerInfo activation_info) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 256 | { |
Mohammed Suhail Munshi | fa79fda | 2022-09-20 11:49:23 +0100 | [diff] [blame] | 257 | ARM_COMPUTE_UNUSED(dst); |
Michalis Spyrou | 60c3b0e | 2021-04-08 12:02:58 +0100 | [diff] [blame] | 258 | ARM_COMPUTE_ERROR_ON_NULLPTR(src); |
Michele Di Giorgio | bd2c8e1 | 2021-01-19 15:29:02 +0000 | [diff] [blame] | 259 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(src, dst, activation_info)); |
Michel Iwaniec | 5dfeae6 | 2017-11-29 10:48:23 +0000 | [diff] [blame] | 260 | |
Pablo Marquez Tello | d158609 | 2022-06-10 14:37:10 +0100 | [diff] [blame] | 261 | const auto uk = CpuActivationKernel::get_implementation(ActivationDataTypeISASelectorData{ src->data_type(), CPUInfo::get().get_cpu_model(), CPUInfo::get().get_isa(), activation_info.activation() }); |
Mohammed Suhail Munshi | fa79fda | 2022-09-20 11:49:23 +0100 | [diff] [blame] | 262 | if(dst != nullptr) |
| 263 | { |
| 264 | // dst auto inizialitation if not yet initialized |
| 265 | auto_init_if_empty(*dst, *src->clone()); |
| 266 | } |
Giorgio Arena | 5ae8d80 | 2021-11-18 18:02:13 +0000 | [diff] [blame] | 267 | |
Georgios Pinitas | 5fdde99 | 2021-06-25 05:42:57 +0100 | [diff] [blame] | 268 | ARM_COMPUTE_ERROR_ON_NULLPTR(uk); |
| 269 | |
Georgios Pinitas | 5fdde99 | 2021-06-25 05:42:57 +0100 | [diff] [blame] | 270 | _run_method = uk->ukernel; |
| 271 | _name = std::string("CpuActivationKernel").append("/").append(uk->name); |
| 272 | |
Viet-Hoa Do | b042e39 | 2022-06-21 15:56:15 +0100 | [diff] [blame] | 273 | #ifdef __aarch64__ |
Pablo Marquez Tello | 48cfd5f | 2023-06-09 11:22:29 +0100 | [diff] [blame] | 274 | if(src->data_type() == DataType::QASYMM8 || src->data_type() == DataType::QASYMM8_SIGNED) |
Pablo Marquez Tello | d75cd8a | 2022-05-26 14:19:39 +0100 | [diff] [blame] | 275 | { |
Pablo Marquez Tello | 48cfd5f | 2023-06-09 11:22:29 +0100 | [diff] [blame] | 276 | ActivationLayerInfo::LookupTable256 tmp_lut; |
| 277 | init_lut(activation_info.activation(), src->data_type(), src->quantization_info().uniform(), (dst) ? dst->quantization_info().uniform() : src->quantization_info().uniform(), |
| 278 | tmp_lut, activation_info.a(), activation_info.b()); |
| 279 | activation_info.setLookupTable256(tmp_lut); |
Pablo Marquez Tello | d75cd8a | 2022-05-26 14:19:39 +0100 | [diff] [blame] | 280 | } |
Viet-Hoa Do | b042e39 | 2022-06-21 15:56:15 +0100 | [diff] [blame] | 281 | #endif // __aarch64__ |
Pablo Marquez Tello | d75cd8a | 2022-05-26 14:19:39 +0100 | [diff] [blame] | 282 | _act_info = activation_info; |
| 283 | |
Mohammed Suhail Munshi | fa79fda | 2022-09-20 11:49:23 +0100 | [diff] [blame] | 284 | Window win; |
| 285 | |
Viet-Hoa Do | 0a36f58 | 2022-10-18 15:23:46 +0100 | [diff] [blame] | 286 | // Use squashed window |
| 287 | std::tie(win, _split_dimension) = calculate_squashed_or_max_window(*src); |
| 288 | ICPPKernel::configure(win); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 289 | } |
| 290 | |
Michele Di Giorgio | bd2c8e1 | 2021-01-19 15:29:02 +0000 | [diff] [blame] | 291 | Status CpuActivationKernel::validate(const ITensorInfo *src, const ITensorInfo *dst, const ActivationLayerInfo &act_info) |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 292 | { |
| 293 | ARM_COMPUTE_UNUSED(act_info); |
Michele Di Giorgio | bd2c8e1 | 2021-01-19 15:29:02 +0000 | [diff] [blame] | 294 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(src, dst, act_info)); |
| 295 | ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(src->clone().get(), (dst != nullptr) ? dst->clone().get() : nullptr).first); |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 296 | |
| 297 | return Status{}; |
| 298 | } |
| 299 | |
Dana Zlotnik | 4cdd6b8 | 2021-10-07 15:31:54 +0300 | [diff] [blame] | 300 | size_t CpuActivationKernel::get_mws(const CPUInfo &platform, size_t thread_count) const |
| 301 | { |
Dana Zlotnik | d7154db | 2021-11-10 11:50:58 +0200 | [diff] [blame] | 302 | ARM_COMPUTE_UNUSED(thread_count); |
Mohammed Suhail Munshi | 066607f | 2022-01-19 12:22:50 +0000 | [diff] [blame] | 303 | ARM_COMPUTE_UNUSED(platform); |
| 304 | |
Mohammed Suhail Munshi | 8307ecf | 2022-11-09 15:38:54 +0000 | [diff] [blame] | 305 | if(_split_dimension == Window::DimX) |
| 306 | { |
| 307 | // Don't split the work load too small if the tensor has been reinterpreted as 1D. |
| 308 | // This number is loosely chosen as threading overhead in each platform varies wildly. |
| 309 | return 1536; |
| 310 | } |
| 311 | return default_mws; |
Dana Zlotnik | 4cdd6b8 | 2021-10-07 15:31:54 +0300 | [diff] [blame] | 312 | } |
| 313 | |
Georgios Pinitas | f8f0442 | 2021-01-08 17:25:55 +0000 | [diff] [blame] | 314 | void CpuActivationKernel::run_op(ITensorPack &tensors, const Window &window, const ThreadInfo &info) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 315 | { |
Georgios Pinitas | 48b3ef8 | 2019-10-14 19:03:09 +0100 | [diff] [blame] | 316 | // Early exit on disabled activation |
| 317 | if(!_act_info.enabled()) |
| 318 | { |
| 319 | return; |
| 320 | } |
| 321 | |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 322 | ARM_COMPUTE_UNUSED(info); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 323 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
Gian Marco Iodice | b30dcc5 | 2017-06-20 09:07:21 +0100 | [diff] [blame] | 324 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 325 | |
Georgios Pinitas | 0499dff | 2020-07-31 22:21:38 +0100 | [diff] [blame] | 326 | ARM_COMPUTE_ERROR_ON(tensors.empty()); |
Georgios Pinitas | 5fdde99 | 2021-06-25 05:42:57 +0100 | [diff] [blame] | 327 | ARM_COMPUTE_ERROR_ON(_run_method == nullptr); |
Georgios Pinitas | 1fd2c80 | 2020-06-16 17:44:46 +0100 | [diff] [blame] | 328 | |
Michalis Spyrou | c4d4555 | 2020-10-19 12:41:30 +0100 | [diff] [blame] | 329 | const ITensor *src = tensors.get_const_tensor(TensorType::ACL_SRC); |
| 330 | ITensor *dst = tensors.get_tensor(TensorType::ACL_DST); |
| 331 | |
Georgios Pinitas | 5fdde99 | 2021-06-25 05:42:57 +0100 | [diff] [blame] | 332 | _run_method(src, dst, _act_info, window); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 333 | } |
Georgios Pinitas | f8f0442 | 2021-01-08 17:25:55 +0000 | [diff] [blame] | 334 | |
| 335 | const char *CpuActivationKernel::name() const |
| 336 | { |
Georgios Pinitas | 5fdde99 | 2021-06-25 05:42:57 +0100 | [diff] [blame] | 337 | return _name.c_str(); |
Georgios Pinitas | f8f0442 | 2021-01-08 17:25:55 +0000 | [diff] [blame] | 338 | } |
Giorgio Arena | 5ae8d80 | 2021-11-18 18:02:13 +0000 | [diff] [blame] | 339 | |
| 340 | const std::vector<CpuActivationKernel::ActivationKernel> &CpuActivationKernel::get_available_kernels() |
| 341 | { |
| 342 | return available_kernels; |
| 343 | } |
Georgios Pinitas | f8f0442 | 2021-01-08 17:25:55 +0000 | [diff] [blame] | 344 | } // namespace kernels |
| 345 | } // namespace cpu |
Michele Di Giorgio | f9b595a | 2020-07-03 13:34:52 +0100 | [diff] [blame] | 346 | } // namespace arm_compute |