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