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 | |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 231 | Window win = calculate_max_window(*src, Steps()); |
Michalis Spyrou | 2dc7e40 | 2020-02-28 14:41:35 +0000 | [diff] [blame] | 232 | Coordinates coord; |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 233 | coord.set_num_dimensions(dst->num_dimensions()); |
| 234 | dst->set_valid_region(ValidRegion(coord, dst->tensor_shape())); |
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 | ICpuKernel::configure(win); |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 237 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 238 | |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 239 | Status CpuLogits1DMaxKernel::validate(const ITensorInfo *src, const ITensorInfo *dst) |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 240 | { |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 241 | ARM_COMPUTE_ERROR_ON_NULLPTR(src, dst); |
| 242 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments_logits_1d_max(*src, *dst)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 243 | |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 244 | return Status{}; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 245 | } |
| 246 | |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 247 | void CpuLogits1DMaxKernel::run_op(ITensorPack &tensors, const Window &window, const ThreadInfo &info) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 248 | { |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 249 | ARM_COMPUTE_UNUSED(info); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 250 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 251 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICpuKernel::window(), window); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 252 | |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 253 | const auto src = tensors.get_const_tensor(TensorType::ACL_SRC); |
| 254 | auto dst = tensors.get_tensor(TensorType::ACL_DST); |
| 255 | |
| 256 | const auto *uk = get_implementation_logits_max(SoftmaxSelectorData{ src->info()->data_type() }); |
| 257 | uk->ukernel(src, dst, window); |
| 258 | } |
| 259 | |
| 260 | const char *CpuLogits1DMaxKernel::name() const |
| 261 | { |
| 262 | return "CpuLogits1DMaxKernel"; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | namespace |
| 266 | { |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 267 | Status validate_arguments_logits_softmax(const ITensorInfo &src, const ITensorInfo &max, |
| 268 | const ITensorInfo &dst, const float beta, const ITensorInfo &tmp, bool is_log) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 269 | { |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame] | 270 | ARM_COMPUTE_UNUSED(beta); |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 271 | // Check input |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 272 | ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(&src); |
| 273 | 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] | 274 | |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 275 | 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] | 276 | |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 277 | // Check max |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 278 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(&src, &max); |
| 279 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(TensorShape(src.tensor_shape()).set(0, 1), max.tensor_shape()); |
| 280 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(&src, &max); |
Georgios Pinitas | 9247c92 | 2017-06-28 18:29:47 +0100 | [diff] [blame] | 281 | |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 282 | // Check output if configured |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 283 | if(dst.total_size() != 0) |
Georgios Pinitas | 9247c92 | 2017-06-28 18:29:47 +0100 | [diff] [blame] | 284 | { |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 285 | const QuantizationInfo output_quantization = is_quantized_asymmetric ? arm_compute::get_softmax_output_quantization_info(src.data_type(), is_log) : dst.quantization_info(); |
| 286 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(&src, &dst); |
| 287 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(&src, &dst); |
| 288 | ARM_COMPUTE_RETURN_ERROR_ON(dst.quantization_info() != output_quantization); |
Georgios Pinitas | 9247c92 | 2017-06-28 18:29:47 +0100 | [diff] [blame] | 289 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 290 | |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 291 | // Check tmp if configured |
| 292 | if(tmp.total_size() != 0) |
| 293 | { |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 294 | 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] | 295 | ARM_COMPUTE_RETURN_ERROR_ON(tmp.data_type() != tmp_data_type); |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 296 | // We could potentially reduce tmp memory if we could predict or make an assumption |
| 297 | // on the maximum number of threads that will run in parallel. |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 298 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(&src, &tmp); |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | return Status{}; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 302 | } |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 303 | } // namespace |
| 304 | |
Sang-Hoon Park | d24affe | 2019-10-08 18:07:23 +0100 | [diff] [blame] | 305 | template <bool IS_LOG> |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 306 | CpuLogits1DSoftmaxKernel<IS_LOG>::CpuLogits1DSoftmaxKernel() |
| 307 | : _beta(1.0f) |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 308 | { |
| 309 | } |
| 310 | |
Sang-Hoon Park | d24affe | 2019-10-08 18:07:23 +0100 | [diff] [blame] | 311 | template <bool IS_LOG> |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 312 | 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] | 313 | { |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 314 | ARM_COMPUTE_ERROR_ON_NULLPTR(src, max, dst, tmp); |
| 315 | ARM_COMPUTE_ERROR_ON_NULLPTR(src, max, dst, tmp); |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 316 | // Perform validation step |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 317 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments_logits_softmax(*src, *max, *dst, beta, *tmp, IS_LOG)); |
| 318 | |
| 319 | _beta = beta; |
Michalis Spyrou | 2dc7e40 | 2020-02-28 14:41:35 +0000 | [diff] [blame] | 320 | |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 321 | // Configure kernel window |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 322 | 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] | 323 | |
| 324 | // Output auto initialization if not yet initialized |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 325 | const QuantizationInfo output_quantization = is_quantized_asymmetric ? arm_compute::get_softmax_output_quantization_info(src->data_type(), IS_LOG) : dst->quantization_info(); |
| 326 | 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] | 327 | |
| 328 | // Tmp auto initialization if not yet initialized |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 329 | const DataType tmp_data_type = is_quantized_asymmetric ? DataType::F32 : src->data_type(); |
| 330 | 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] | 331 | |
| 332 | // Configure kernel window |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 333 | Window win = calculate_max_window(*max, Steps()); |
Michalis Spyrou | 2dc7e40 | 2020-02-28 14:41:35 +0000 | [diff] [blame] | 334 | Coordinates coord; |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 335 | coord.set_num_dimensions(dst->num_dimensions()); |
| 336 | dst->set_valid_region(ValidRegion(coord, dst->tensor_shape())); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 337 | |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 338 | ICpuKernel::configure(win); |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 339 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 340 | |
Sang-Hoon Park | d24affe | 2019-10-08 18:07:23 +0100 | [diff] [blame] | 341 | template <bool IS_LOG> |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 342 | Status CpuLogits1DSoftmaxKernel<IS_LOG>::validate(const ITensorInfo *src, const ITensorInfo *max, |
| 343 | const ITensorInfo *dst, const float beta, const ITensorInfo *tmp) |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 344 | { |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 345 | ARM_COMPUTE_ERROR_ON_NULLPTR(src, max, dst, tmp); |
| 346 | 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] | 347 | |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 348 | return Status{}; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 349 | } |
| 350 | |
Sang-Hoon Park | d24affe | 2019-10-08 18:07:23 +0100 | [diff] [blame] | 351 | template <bool IS_LOG> |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 352 | 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] | 353 | { |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 354 | ARM_COMPUTE_UNUSED(info); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 355 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 356 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICpuKernel::window(), window); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 357 | |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 358 | const auto src = tensors.get_const_tensor(TensorType::ACL_SRC_0); |
| 359 | auto max = tensors.get_tensor(TensorType::ACL_SRC_1); |
| 360 | auto dst = tensors.get_tensor(TensorType::ACL_DST_0); |
| 361 | auto tmp = tensors.get_tensor(TensorType::ACL_DST_1); |
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 | const unsigned int num_elems_processed_per_iteration = src->info()->valid_region().shape.x(); |
| 364 | 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] | 365 | |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 366 | 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] | 367 | |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 368 | void *tmp_for_thread = tmp->buffer() + (info.thread_id * tmp_size_for_thread); |
| 369 | |
| 370 | const auto *uk = get_implementation_logits(SoftmaxSelectorData{ src->info()->data_type() }); |
| 371 | uk->ukernel(src, max, tmp_for_thread, dst, _beta, IS_LOG, window); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 372 | } |
| 373 | |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 374 | template <bool IS_LOG> |
| 375 | const char *CpuLogits1DSoftmaxKernel<IS_LOG>::name() const |
| 376 | { |
| 377 | if(IS_LOG) |
| 378 | { |
| 379 | return "CpuLogits1DSoftmaxKernel"; |
| 380 | } |
| 381 | else |
| 382 | { |
| 383 | return "CpuLogits1DLogSoftmaxKernel"; |
| 384 | } |
| 385 | } |
Sang-Hoon Park | d24affe | 2019-10-08 18:07:23 +0100 | [diff] [blame] | 386 | |
Michalis Spyrou | 373b407 | 2021-01-20 16:41:12 +0000 | [diff] [blame] | 387 | template class CpuLogits1DSoftmaxKernel<true>; |
| 388 | template class CpuLogits1DSoftmaxKernel<false>; |
| 389 | |
| 390 | } // namespace kernels |
| 391 | } // namespace cpu |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 392 | } // namespace arm_compute |