Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Michalis Spyrou | b5a450a | 2021-01-06 17:40:30 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2021 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/CpuSoftmaxKernel.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/Error.h" |
| 27 | #include "arm_compute/core/Helpers.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 28 | #include "arm_compute/core/ITensor.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 29 | #include "arm_compute/core/TensorInfo.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 30 | #include "arm_compute/core/Validate.h" |
| 31 | #include "arm_compute/core/Window.h" |
Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 32 | #include "src/core/CPP/Validate.h" |
Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 33 | #include "src/core/helpers/AutoConfiguration.h" |
| 34 | #include "src/core/helpers/WindowHelpers.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 35 | |
Michalis Spyrou | b5a450a | 2021-01-06 17:40:30 +0000 | [diff] [blame] | 36 | #include "src/core/common/Registrars.h" |
Georgios Pinitas | 7891a73 | 2021-08-20 21:39:25 +0100 | [diff] [blame] | 37 | #include "src/cpu/kernels/softmax/impl/neon/list.h" |
| 38 | #include "src/cpu/kernels/softmax/impl/sve/list.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 39 | |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 40 | namespace arm_compute |
| 41 | { |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 42 | namespace cpu |
| 43 | { |
| 44 | namespace kernels |
| 45 | { |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 46 | namespace |
| 47 | { |
Michalis Spyrou | b5a450a | 2021-01-06 17:40:30 +0000 | [diff] [blame] | 48 | struct SoftmaxSelectorData |
| 49 | { |
Michalis Spyrou | 20fca52 | 2021-06-07 14:23:57 +0100 | [diff] [blame] | 50 | DataType dt; |
| 51 | const CPUInfo &ci; |
Michalis Spyrou | b5a450a | 2021-01-06 17:40:30 +0000 | [diff] [blame] | 52 | }; |
| 53 | using SoftmaxSelectorPtr = std::add_pointer<bool(const SoftmaxSelectorData &data)>::type; |
| 54 | using SoftmaxLogits1DMaxKernelPtr = std::add_pointer<void(const ITensor *, ITensor *, const Window &)>::type; |
| 55 | using SoftmaxLogits1DKernelPtr = std::add_pointer<void(const ITensor *, const ITensor *, void *const, ITensor *, float, bool, const Window &)>::type; |
| 56 | |
| 57 | struct SoftmaxLogits1DKernel |
| 58 | { |
| 59 | const char *name; |
| 60 | const SoftmaxSelectorPtr is_selected; |
| 61 | SoftmaxLogits1DKernelPtr ukernel; |
| 62 | }; |
| 63 | |
| 64 | struct SoftmaxLogits1DMaxKernel |
| 65 | { |
| 66 | const char *name; |
| 67 | const SoftmaxSelectorPtr is_selected; |
| 68 | SoftmaxLogits1DMaxKernelPtr ukernel; |
| 69 | }; |
| 70 | |
| 71 | static const SoftmaxLogits1DKernel available_logits_1d_kernels[] = |
| 72 | { |
Michalis Spyrou | 20fca52 | 2021-06-07 14:23:57 +0100 | [diff] [blame] | 73 | #if defined(ARM_COMPUTE_ENABLE_SVE) |
Michalis Spyrou | b5a450a | 2021-01-06 17:40:30 +0000 | [diff] [blame] | 74 | { |
Georgios Pinitas | 5fdde99 | 2021-06-25 05:42:57 +0100 | [diff] [blame] | 75 | "sve_fp32_softmax_logits_1d", |
Michalis Spyrou | 20fca52 | 2021-06-07 14:23:57 +0100 | [diff] [blame] | 76 | [](const SoftmaxSelectorData & data) { return (data.dt == DataType::F32) && data.ci.has_sve(); }, |
Michalis Spyrou | b5a450a | 2021-01-06 17:40:30 +0000 | [diff] [blame] | 77 | REGISTER_FP32_SVE(arm_compute::cpu::sve_softmax_logits_1d_float<float>) |
| 78 | }, |
| 79 | { |
Georgios Pinitas | 5fdde99 | 2021-06-25 05:42:57 +0100 | [diff] [blame] | 80 | "sve_fp16_softmax_logits_1d", |
Michalis Spyrou | 20fca52 | 2021-06-07 14:23:57 +0100 | [diff] [blame] | 81 | [](const SoftmaxSelectorData & data) { return (data.dt == DataType::F16) && data.ci.has_sve(); }, |
Michalis Spyrou | b5a450a | 2021-01-06 17:40:30 +0000 | [diff] [blame] | 82 | REGISTER_FP16_SVE(arm_compute::cpu::sve_softmax_logits_1d_float<float16_t>) |
| 83 | }, |
Michalis Spyrou | 20fca52 | 2021-06-07 14:23:57 +0100 | [diff] [blame] | 84 | #endif /* defined(ARM_COMPUTE_ENABLE_SVE) */ |
Georgios Pinitas | bdcdc39 | 2021-04-22 16:42:03 +0100 | [diff] [blame] | 85 | |
Michalis Spyrou | 20fca52 | 2021-06-07 14:23:57 +0100 | [diff] [blame] | 86 | #if defined(ARM_COMPUTE_ENABLE_NEON) |
Michalis Spyrou | b5a450a | 2021-01-06 17:40:30 +0000 | [diff] [blame] | 87 | { |
Georgios Pinitas | 5fdde99 | 2021-06-25 05:42:57 +0100 | [diff] [blame] | 88 | "neon_fp32_softmax_logits_1d", |
Michalis Spyrou | b5a450a | 2021-01-06 17:40:30 +0000 | [diff] [blame] | 89 | [](const SoftmaxSelectorData & data) { return (data.dt == DataType::F32); }, |
| 90 | REGISTER_FP32_NEON(arm_compute::cpu::neon_softmax_logits_1d_float<float>) |
| 91 | }, |
| 92 | #if defined(__ARM_FEATURE_FP16_VECTOR_ARITHMETIC) |
| 93 | { |
Georgios Pinitas | 5fdde99 | 2021-06-25 05:42:57 +0100 | [diff] [blame] | 94 | "neon_fp16_softmax_logits_1d", |
Michalis Spyrou | b5a450a | 2021-01-06 17:40:30 +0000 | [diff] [blame] | 95 | [](const SoftmaxSelectorData & data) { return (data.dt == DataType::F16); }, |
| 96 | REGISTER_FP16_NEON(arm_compute::cpu::neon_softmax_logits_1d_float<float16_t>) |
| 97 | }, |
| 98 | #endif /* defined(__ARM_FEATURE_FP16_VECTOR_ARITHMETIC) */ |
Michalis Spyrou | 20fca52 | 2021-06-07 14:23:57 +0100 | [diff] [blame] | 99 | #endif /* defined(ARM_COMPUTE_ENABLE_NEON) */ |
Michalis Spyrou | b5a450a | 2021-01-06 17:40:30 +0000 | [diff] [blame] | 100 | |
Michalis Spyrou | 20fca52 | 2021-06-07 14:23:57 +0100 | [diff] [blame] | 101 | #if defined(ARM_COMPUTE_ENABLE_SVE2) |
Michalis Spyrou | b5a450a | 2021-01-06 17:40:30 +0000 | [diff] [blame] | 102 | { |
Georgios Pinitas | 5fdde99 | 2021-06-25 05:42:57 +0100 | [diff] [blame] | 103 | "sve2_qu8_softmax_logits_1d", |
Michalis Spyrou | 20fca52 | 2021-06-07 14:23:57 +0100 | [diff] [blame] | 104 | [](const SoftmaxSelectorData & data) { return (data.dt == DataType::QASYMM8) && data.ci.has_sve2(); }, |
Michalis Spyrou | b5a450a | 2021-01-06 17:40:30 +0000 | [diff] [blame] | 105 | REGISTER_QASYMM8_SVE(arm_compute::cpu::sve_softmax_logits_1d_quantized<qasymm8_t>) |
| 106 | }, |
| 107 | { |
Georgios Pinitas | 5fdde99 | 2021-06-25 05:42:57 +0100 | [diff] [blame] | 108 | "sve2_qs8_softmax_logits_1d", |
Michalis Spyrou | 20fca52 | 2021-06-07 14:23:57 +0100 | [diff] [blame] | 109 | [](const SoftmaxSelectorData & data) { return (data.dt == DataType::QASYMM8_SIGNED) && data.ci.has_sve2(); }, |
Michalis Spyrou | b5a450a | 2021-01-06 17:40:30 +0000 | [diff] [blame] | 110 | REGISTER_QASYMM8_SIGNED_SVE(arm_compute::cpu::sve_softmax_logits_1d_quantized<qasymm8_signed_t>) |
| 111 | }, |
Michalis Spyrou | 20fca52 | 2021-06-07 14:23:57 +0100 | [diff] [blame] | 112 | #endif /* defined(ARM_COMPUTE_ENABLE_SVE2) */ |
Michalis Spyrou | b5a450a | 2021-01-06 17:40:30 +0000 | [diff] [blame] | 113 | { |
Georgios Pinitas | 5fdde99 | 2021-06-25 05:42:57 +0100 | [diff] [blame] | 114 | "neon_qu8_softmax_logits_1d", |
Michalis Spyrou | b5a450a | 2021-01-06 17:40:30 +0000 | [diff] [blame] | 115 | [](const SoftmaxSelectorData & data) { return (data.dt == DataType::QASYMM8); }, |
| 116 | REGISTER_QASYMM8_NEON(arm_compute::cpu::neon_softmax_logits_1d_quantized<qasymm8_t>) |
| 117 | }, |
| 118 | { |
Georgios Pinitas | 5fdde99 | 2021-06-25 05:42:57 +0100 | [diff] [blame] | 119 | "neon_qs8_softmax_logits_1d", |
Michalis Spyrou | b5a450a | 2021-01-06 17:40:30 +0000 | [diff] [blame] | 120 | [](const SoftmaxSelectorData & data) { return (data.dt == DataType::QASYMM8_SIGNED); }, |
| 121 | REGISTER_QASYMM8_SIGNED_NEON(arm_compute::cpu::neon_softmax_logits_1d_quantized<qasymm8_signed_t>) |
| 122 | }, |
Michalis Spyrou | b5a450a | 2021-01-06 17:40:30 +0000 | [diff] [blame] | 123 | }; |
| 124 | |
| 125 | static const SoftmaxLogits1DMaxKernel available_logits_1d_max_kernels[] = |
| 126 | { |
Michalis Spyrou | 20fca52 | 2021-06-07 14:23:57 +0100 | [diff] [blame] | 127 | #if defined(ARM_COMPUTE_ENABLE_SVE) |
Michalis Spyrou | b5a450a | 2021-01-06 17:40:30 +0000 | [diff] [blame] | 128 | { |
Georgios Pinitas | 5fdde99 | 2021-06-25 05:42:57 +0100 | [diff] [blame] | 129 | "sve_fp32_logits_1d_max", |
Michalis Spyrou | 20fca52 | 2021-06-07 14:23:57 +0100 | [diff] [blame] | 130 | [](const SoftmaxSelectorData & data) { return (data.dt == DataType::F32) && data.ci.has_sve(); }, |
Michalis Spyrou | b5a450a | 2021-01-06 17:40:30 +0000 | [diff] [blame] | 131 | REGISTER_FP32_SVE(arm_compute::cpu::sve_logits_1d_max<float>) |
| 132 | }, |
| 133 | { |
Georgios Pinitas | 5fdde99 | 2021-06-25 05:42:57 +0100 | [diff] [blame] | 134 | "sve_fp16_logits_1d_max", |
Michalis Spyrou | 20fca52 | 2021-06-07 14:23:57 +0100 | [diff] [blame] | 135 | [](const SoftmaxSelectorData & data) { return (data.dt == DataType::F16) && data.ci.has_sve(); }, |
Michalis Spyrou | b5a450a | 2021-01-06 17:40:30 +0000 | [diff] [blame] | 136 | REGISTER_FP16_SVE(arm_compute::cpu::sve_logits_1d_max<float16_t>) |
| 137 | }, |
| 138 | { |
Georgios Pinitas | 5fdde99 | 2021-06-25 05:42:57 +0100 | [diff] [blame] | 139 | "sve_qu8_logits_1d_max", |
Michalis Spyrou | 20fca52 | 2021-06-07 14:23:57 +0100 | [diff] [blame] | 140 | [](const SoftmaxSelectorData & data) { return (data.dt == DataType::QASYMM8) && data.ci.has_sve(); }, |
Michalis Spyrou | b5a450a | 2021-01-06 17:40:30 +0000 | [diff] [blame] | 141 | REGISTER_QASYMM8_SVE(arm_compute::cpu::sve_logits_1d_max<qasymm8_t>) |
| 142 | }, |
| 143 | { |
Georgios Pinitas | 5fdde99 | 2021-06-25 05:42:57 +0100 | [diff] [blame] | 144 | "sve_qs8_logits_1d_max", |
Michalis Spyrou | 20fca52 | 2021-06-07 14:23:57 +0100 | [diff] [blame] | 145 | [](const SoftmaxSelectorData & data) { return (data.dt == DataType::QASYMM8_SIGNED) && data.ci.has_sve(); }, |
Michalis Spyrou | b5a450a | 2021-01-06 17:40:30 +0000 | [diff] [blame] | 146 | REGISTER_QASYMM8_SIGNED_SVE(arm_compute::cpu::sve_logits_1d_max<qasymm8_signed_t>) |
| 147 | }, |
Michalis Spyrou | 20fca52 | 2021-06-07 14:23:57 +0100 | [diff] [blame] | 148 | #endif /* defined(ARM_COMPUTE_ENABLE_SVE) */ |
| 149 | #if defined(ARM_COMPUTE_ENABLE_NEON) |
Michalis Spyrou | b5a450a | 2021-01-06 17:40:30 +0000 | [diff] [blame] | 150 | { |
Georgios Pinitas | 5fdde99 | 2021-06-25 05:42:57 +0100 | [diff] [blame] | 151 | "neon_fp32_logits_1d_max", |
Michalis Spyrou | b5a450a | 2021-01-06 17:40:30 +0000 | [diff] [blame] | 152 | [](const SoftmaxSelectorData & data) { return (data.dt == DataType::F32); }, |
| 153 | REGISTER_FP32_NEON(arm_compute::cpu::neon_logits_1d_max<float>) |
| 154 | }, |
| 155 | #if defined(__ARM_FEATURE_FP16_VECTOR_ARITHMETIC) |
| 156 | { |
Georgios Pinitas | 5fdde99 | 2021-06-25 05:42:57 +0100 | [diff] [blame] | 157 | "neon_fp16_logits_1d_max", |
Michalis Spyrou | b5a450a | 2021-01-06 17:40:30 +0000 | [diff] [blame] | 158 | [](const SoftmaxSelectorData & data) { return (data.dt == DataType::F16); }, |
| 159 | REGISTER_FP16_NEON(arm_compute::cpu::neon_logits_1d_max<float16_t>) |
| 160 | }, |
| 161 | #endif /* defined(__ARM_FEATURE_FP16_VECTOR_ARITHMETIC) */ |
| 162 | { |
Georgios Pinitas | 5fdde99 | 2021-06-25 05:42:57 +0100 | [diff] [blame] | 163 | "neon_qu8_logits_1d_max", |
Michalis Spyrou | b5a450a | 2021-01-06 17:40:30 +0000 | [diff] [blame] | 164 | [](const SoftmaxSelectorData & data) { return (data.dt == DataType::QASYMM8); }, |
| 165 | REGISTER_QASYMM8_NEON(arm_compute::cpu::neon_logits_1d_max<qasymm8_t>) |
| 166 | }, |
| 167 | { |
Georgios Pinitas | 5fdde99 | 2021-06-25 05:42:57 +0100 | [diff] [blame] | 168 | "neon_qs8_logits_1d_max", |
Michalis Spyrou | b5a450a | 2021-01-06 17:40:30 +0000 | [diff] [blame] | 169 | [](const SoftmaxSelectorData & data) { return (data.dt == DataType::QASYMM8_SIGNED); }, |
| 170 | REGISTER_QASYMM8_SIGNED_NEON(arm_compute::cpu::neon_logits_1d_max<qasymm8_signed_t>) |
| 171 | }, |
Michalis Spyrou | 20fca52 | 2021-06-07 14:23:57 +0100 | [diff] [blame] | 172 | #endif /* defined(ARM_COMPUTE_ENABLE_NEON) */ |
Michalis Spyrou | b5a450a | 2021-01-06 17:40:30 +0000 | [diff] [blame] | 173 | }; |
| 174 | |
| 175 | const SoftmaxLogits1DKernel *get_implementation_logits(const SoftmaxSelectorData &data) |
| 176 | { |
| 177 | for(const auto &uk : available_logits_1d_kernels) |
| 178 | { |
Michalis Spyrou | 20fca52 | 2021-06-07 14:23:57 +0100 | [diff] [blame] | 179 | if(uk.is_selected({ data.dt, CPUInfo::get() })) |
Michalis Spyrou | b5a450a | 2021-01-06 17:40:30 +0000 | [diff] [blame] | 180 | { |
| 181 | return &uk; |
| 182 | } |
| 183 | } |
| 184 | return nullptr; |
| 185 | } |
| 186 | |
| 187 | const SoftmaxLogits1DMaxKernel *get_implementation_logits_max(const SoftmaxSelectorData &data) |
| 188 | { |
| 189 | for(const auto &uk : available_logits_1d_max_kernels) |
| 190 | { |
Michalis Spyrou | 20fca52 | 2021-06-07 14:23:57 +0100 | [diff] [blame] | 191 | if(uk.is_selected({ data.dt, CPUInfo::get() })) |
Michalis Spyrou | b5a450a | 2021-01-06 17:40:30 +0000 | [diff] [blame] | 192 | { |
| 193 | return &uk; |
| 194 | } |
| 195 | } |
| 196 | return nullptr; |
| 197 | } |
| 198 | |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 199 | Status validate_arguments_logits_1d_max(const ITensorInfo &input, const ITensorInfo &output) |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 200 | { |
Anthony Barbier | eaefd00 | 2018-07-20 17:49:35 +0100 | [diff] [blame] | 201 | ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(&input); |
Sang-Hoon Park | c3a7420 | 2019-11-22 16:05:46 +0000 | [diff] [blame] | 202 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&input, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED, DataType::F16, DataType::F32); |
Pablo Tello | b49a715 | 2017-07-11 16:31:35 +0100 | [diff] [blame] | 203 | |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 204 | // Validate in case of configured output |
| 205 | if(output.total_size() != 0) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 206 | { |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 207 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(&input, &output); |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 208 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(&input, &output); |
| 209 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(output.tensor_shape(), TensorShape(input.tensor_shape()).set(0, 1)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 210 | } |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 211 | |
| 212 | return Status{}; |
| 213 | } |
| 214 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 215 | } // namespace |
| 216 | |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 217 | void CpuLogits1DMaxKernel::configure(const ITensorInfo *src, ITensorInfo *dst) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 218 | { |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 219 | ARM_COMPUTE_ERROR_ON_NULLPTR(src, dst); |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 220 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments_logits_1d_max(*src, *dst)); |
Michalis Spyrou | 2dc7e40 | 2020-02-28 14:41:35 +0000 | [diff] [blame] | 221 | |
| 222 | // Softmax across the x dimension |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 223 | const TensorShape output_shape = TensorShape(src->tensor_shape()).set(0, 1); |
Michalis Spyrou | 2dc7e40 | 2020-02-28 14:41:35 +0000 | [diff] [blame] | 224 | // Output auto initialization if not yet initialized |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 225 | auto_init_if_empty(*dst, output_shape, 1, src->data_type(), src->quantization_info()); |
Michalis Spyrou | 2dc7e40 | 2020-02-28 14:41:35 +0000 | [diff] [blame] | 226 | |
Georgios Pinitas | 5fdde99 | 2021-06-25 05:42:57 +0100 | [diff] [blame] | 227 | const auto *uk = get_implementation_logits_max(SoftmaxSelectorData{ src->data_type(), CPUInfo::get() }); |
| 228 | ARM_COMPUTE_ERROR_ON_NULLPTR(uk); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 229 | |
Georgios Pinitas | 5fdde99 | 2021-06-25 05:42:57 +0100 | [diff] [blame] | 230 | _run_method = uk->ukernel; |
| 231 | _name = std::string("CpuLogits1DMaxKernel").append("/").append(uk->name); |
| 232 | |
| 233 | Window win = calculate_max_window(*src, Steps()); |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 234 | ICpuKernel::configure(win); |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 235 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 236 | |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 237 | Status CpuLogits1DMaxKernel::validate(const ITensorInfo *src, const ITensorInfo *dst) |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 238 | { |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 239 | ARM_COMPUTE_ERROR_ON_NULLPTR(src, dst); |
| 240 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments_logits_1d_max(*src, *dst)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 241 | |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 242 | return Status{}; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 243 | } |
| 244 | |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 245 | void CpuLogits1DMaxKernel::run_op(ITensorPack &tensors, const Window &window, const ThreadInfo &info) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 246 | { |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 247 | ARM_COMPUTE_UNUSED(info); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 248 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 249 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICpuKernel::window(), window); |
Georgios Pinitas | 5fdde99 | 2021-06-25 05:42:57 +0100 | [diff] [blame] | 250 | ARM_COMPUTE_ERROR_ON(_run_method == nullptr); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 251 | |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 252 | const auto src = tensors.get_const_tensor(TensorType::ACL_SRC); |
| 253 | auto dst = tensors.get_tensor(TensorType::ACL_DST); |
| 254 | |
Georgios Pinitas | 5fdde99 | 2021-06-25 05:42:57 +0100 | [diff] [blame] | 255 | _run_method(src, dst, window); |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | const char *CpuLogits1DMaxKernel::name() const |
| 259 | { |
Georgios Pinitas | 5fdde99 | 2021-06-25 05:42:57 +0100 | [diff] [blame] | 260 | return _name.c_str(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | namespace |
| 264 | { |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 265 | Status validate_arguments_logits_softmax(const ITensorInfo &src, const ITensorInfo &max, |
| 266 | const ITensorInfo &dst, const float beta, const ITensorInfo &tmp, bool is_log) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 267 | { |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame] | 268 | ARM_COMPUTE_UNUSED(beta); |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 269 | // Check input |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 270 | ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(&src); |
| 271 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&src, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED, DataType::F16, DataType::F32); |
Pablo Tello | b49a715 | 2017-07-11 16:31:35 +0100 | [diff] [blame] | 272 | |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 273 | const bool is_quantized_asymmetric = is_data_type_quantized_asymmetric(src.data_type()); |
Georgios Pinitas | 9247c92 | 2017-06-28 18:29:47 +0100 | [diff] [blame] | 274 | |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 275 | // Check max |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 276 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(&src, &max); |
| 277 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(TensorShape(src.tensor_shape()).set(0, 1), max.tensor_shape()); |
| 278 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(&src, &max); |
Georgios Pinitas | 9247c92 | 2017-06-28 18:29:47 +0100 | [diff] [blame] | 279 | |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 280 | // Check output if configured |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 281 | if(dst.total_size() != 0) |
Georgios Pinitas | 9247c92 | 2017-06-28 18:29:47 +0100 | [diff] [blame] | 282 | { |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 283 | const QuantizationInfo output_quantization = is_quantized_asymmetric ? arm_compute::get_softmax_output_quantization_info(src.data_type(), is_log) : dst.quantization_info(); |
| 284 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(&src, &dst); |
| 285 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(&src, &dst); |
| 286 | ARM_COMPUTE_RETURN_ERROR_ON(dst.quantization_info() != output_quantization); |
Georgios Pinitas | 9247c92 | 2017-06-28 18:29:47 +0100 | [diff] [blame] | 287 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 288 | |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 289 | // Check tmp if configured |
| 290 | if(tmp.total_size() != 0) |
| 291 | { |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 292 | const DataType tmp_data_type = is_quantized_asymmetric ? DataType::F32 : src.data_type(); |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 293 | ARM_COMPUTE_RETURN_ERROR_ON(tmp.data_type() != tmp_data_type); |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 294 | // We could potentially reduce tmp memory if we could predict or make an assumption |
| 295 | // on the maximum number of threads that will run in parallel. |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 296 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(&src, &tmp); |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | return Status{}; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 300 | } |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 301 | } // namespace |
| 302 | |
Sang-Hoon Park | d24affe | 2019-10-08 18:07:23 +0100 | [diff] [blame] | 303 | template <bool IS_LOG> |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 304 | void CpuLogits1DSoftmaxKernel<IS_LOG>::configure(const ITensorInfo *src, const ITensorInfo *max, ITensorInfo *dst, const float beta, ITensorInfo *tmp) |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 305 | { |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 306 | ARM_COMPUTE_ERROR_ON_NULLPTR(src, max, dst, tmp); |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 307 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments_logits_softmax(*src, *max, *dst, beta, *tmp, IS_LOG)); |
| 308 | |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 309 | // Configure kernel window |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 310 | const bool is_quantized_asymmetric = is_data_type_quantized_asymmetric(src->data_type()); |
Michalis Spyrou | 2dc7e40 | 2020-02-28 14:41:35 +0000 | [diff] [blame] | 311 | |
| 312 | // Output auto initialization if not yet initialized |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 313 | const QuantizationInfo output_quantization = is_quantized_asymmetric ? arm_compute::get_softmax_output_quantization_info(src->data_type(), IS_LOG) : dst->quantization_info(); |
| 314 | auto_init_if_empty(*dst, TensorInfo(*src).set_quantization_info(output_quantization).reset_padding()); |
Michalis Spyrou | 2dc7e40 | 2020-02-28 14:41:35 +0000 | [diff] [blame] | 315 | |
| 316 | // Tmp auto initialization if not yet initialized |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 317 | const DataType tmp_data_type = is_quantized_asymmetric ? DataType::F32 : src->data_type(); |
| 318 | auto_init_if_empty(*tmp, TensorInfo(*src).set_data_type(tmp_data_type).reset_padding()); |
Michalis Spyrou | 2dc7e40 | 2020-02-28 14:41:35 +0000 | [diff] [blame] | 319 | |
Georgios Pinitas | 5fdde99 | 2021-06-25 05:42:57 +0100 | [diff] [blame] | 320 | const auto *uk = get_implementation_logits(SoftmaxSelectorData{ src->data_type(), CPUInfo::get() }); |
| 321 | ARM_COMPUTE_ERROR_ON_NULLPTR(uk); |
| 322 | |
| 323 | std::string kernel_name = IS_LOG ? std::string("CpuLogits1DLogSoftmaxKernel") : std::string("CpuLogits1DSoftmaxKernel"); |
| 324 | |
| 325 | _beta = beta; |
| 326 | _run_method = uk->ukernel; |
| 327 | _name = kernel_name.append("/").append(uk->name); |
| 328 | |
Michalis Spyrou | 2dc7e40 | 2020-02-28 14:41:35 +0000 | [diff] [blame] | 329 | // Configure kernel window |
SiCongLi | b88272e | 2021-02-24 15:40:57 +0000 | [diff] [blame] | 330 | Window win = calculate_max_window(*max, Steps()); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 331 | |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 332 | ICpuKernel::configure(win); |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 333 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 334 | |
Sang-Hoon Park | d24affe | 2019-10-08 18:07:23 +0100 | [diff] [blame] | 335 | template <bool IS_LOG> |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 336 | Status CpuLogits1DSoftmaxKernel<IS_LOG>::validate(const ITensorInfo *src, const ITensorInfo *max, |
| 337 | const ITensorInfo *dst, const float beta, const ITensorInfo *tmp) |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 338 | { |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 339 | ARM_COMPUTE_ERROR_ON_NULLPTR(src, max, dst, tmp); |
| 340 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments_logits_softmax(*src, *max, *dst, beta, *tmp, IS_LOG)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 341 | |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 342 | return Status{}; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 343 | } |
| 344 | |
Sang-Hoon Park | d24affe | 2019-10-08 18:07:23 +0100 | [diff] [blame] | 345 | template <bool IS_LOG> |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 346 | void CpuLogits1DSoftmaxKernel<IS_LOG>::run_op(ITensorPack &tensors, const Window &window, const ThreadInfo &info) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 347 | { |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 348 | ARM_COMPUTE_UNUSED(info); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 349 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 350 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICpuKernel::window(), window); |
Georgios Pinitas | 5fdde99 | 2021-06-25 05:42:57 +0100 | [diff] [blame] | 351 | ARM_COMPUTE_ERROR_ON(_run_method == nullptr); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 352 | |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 353 | const auto src = tensors.get_const_tensor(TensorType::ACL_SRC_0); |
| 354 | auto max = tensors.get_tensor(TensorType::ACL_SRC_1); |
| 355 | auto dst = tensors.get_tensor(TensorType::ACL_DST_0); |
| 356 | auto tmp = tensors.get_tensor(TensorType::ACL_DST_1); |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 357 | |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 358 | const unsigned int num_elems_processed_per_iteration = src->info()->valid_region().shape.x(); |
| 359 | const unsigned int tmp_size_for_thread = tmp->info()->element_size() * num_elems_processed_per_iteration; |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 360 | |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 361 | ARM_COMPUTE_ERROR_ON(tmp->info()->total_size() < (info.num_threads * tmp_size_for_thread)); |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 362 | |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 363 | void *tmp_for_thread = tmp->buffer() + (info.thread_id * tmp_size_for_thread); |
Georgios Pinitas | 5fdde99 | 2021-06-25 05:42:57 +0100 | [diff] [blame] | 364 | _run_method(src, max, tmp_for_thread, dst, _beta, IS_LOG, window); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 365 | } |
| 366 | |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 367 | template <bool IS_LOG> |
| 368 | const char *CpuLogits1DSoftmaxKernel<IS_LOG>::name() const |
| 369 | { |
Georgios Pinitas | 5fdde99 | 2021-06-25 05:42:57 +0100 | [diff] [blame] | 370 | return _name.c_str(); |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 371 | } |
Sang-Hoon Park | d24affe | 2019-10-08 18:07:23 +0100 | [diff] [blame] | 372 | |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 373 | template class CpuLogits1DSoftmaxKernel<true>; |
| 374 | template class CpuLogits1DSoftmaxKernel<false>; |
| 375 | |
| 376 | } // namespace kernels |
| 377 | } // namespace cpu |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 378 | } // namespace arm_compute |