blob: cbf3773ddc8567898d681753e6335af43bb8a631 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Michalis Spyroub5a450a2021-01-06 17:40:30 +00002 * Copyright (c) 2017-2021 Arm Limited.
Anthony Barbier6ff3b192017-09-04 18:44:23 +01003 *
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 Pinitas7891a732021-08-20 21:39:25 +010024#include "src/cpu/kernels/CpuSoftmaxKernel.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010025
Anthony Barbier6ff3b192017-09-04 18:44:23 +010026#include "arm_compute/core/Error.h"
27#include "arm_compute/core/Helpers.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010028#include "arm_compute/core/ITensor.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010029#include "arm_compute/core/TensorInfo.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010030#include "arm_compute/core/Validate.h"
31#include "arm_compute/core/Window.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010032#include "src/core/CPP/Validate.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010033#include "src/core/helpers/AutoConfiguration.h"
34#include "src/core/helpers/WindowHelpers.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010035
Michalis Spyroub5a450a2021-01-06 17:40:30 +000036#include "src/core/common/Registrars.h"
Georgios Pinitas7891a732021-08-20 21:39:25 +010037#include "src/cpu/kernels/softmax/impl/neon/list.h"
38#include "src/cpu/kernels/softmax/impl/sve/list.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010039
Diego Lopez Recas35ceeb22017-12-04 18:56:10 +000040namespace arm_compute
41{
Michalis Spyrou373b4072021-01-20 16:41:12 +000042namespace cpu
43{
44namespace kernels
45{
Anthony Barbier6ff3b192017-09-04 18:44:23 +010046namespace
47{
Michalis Spyroub5a450a2021-01-06 17:40:30 +000048struct SoftmaxSelectorData
49{
Michalis Spyrou20fca522021-06-07 14:23:57 +010050 DataType dt;
51 const CPUInfo &ci;
Michalis Spyroub5a450a2021-01-06 17:40:30 +000052};
53using SoftmaxSelectorPtr = std::add_pointer<bool(const SoftmaxSelectorData &data)>::type;
54using SoftmaxLogits1DMaxKernelPtr = std::add_pointer<void(const ITensor *, ITensor *, const Window &)>::type;
55using SoftmaxLogits1DKernelPtr = std::add_pointer<void(const ITensor *, const ITensor *, void *const, ITensor *, float, bool, const Window &)>::type;
56
57struct SoftmaxLogits1DKernel
58{
59 const char *name;
60 const SoftmaxSelectorPtr is_selected;
61 SoftmaxLogits1DKernelPtr ukernel;
62};
63
64struct SoftmaxLogits1DMaxKernel
65{
66 const char *name;
67 const SoftmaxSelectorPtr is_selected;
68 SoftmaxLogits1DMaxKernelPtr ukernel;
69};
70
71static const SoftmaxLogits1DKernel available_logits_1d_kernels[] =
72{
Michalis Spyrou20fca522021-06-07 14:23:57 +010073#if defined(ARM_COMPUTE_ENABLE_SVE)
Michalis Spyroub5a450a2021-01-06 17:40:30 +000074 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +010075 "sve_fp32_softmax_logits_1d",
Michalis Spyrou20fca522021-06-07 14:23:57 +010076 [](const SoftmaxSelectorData & data) { return (data.dt == DataType::F32) && data.ci.has_sve(); },
Michalis Spyroub5a450a2021-01-06 17:40:30 +000077 REGISTER_FP32_SVE(arm_compute::cpu::sve_softmax_logits_1d_float<float>)
78 },
79 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +010080 "sve_fp16_softmax_logits_1d",
Michalis Spyrou20fca522021-06-07 14:23:57 +010081 [](const SoftmaxSelectorData & data) { return (data.dt == DataType::F16) && data.ci.has_sve(); },
Michalis Spyroub5a450a2021-01-06 17:40:30 +000082 REGISTER_FP16_SVE(arm_compute::cpu::sve_softmax_logits_1d_float<float16_t>)
83 },
Michalis Spyrou20fca522021-06-07 14:23:57 +010084#endif /* defined(ARM_COMPUTE_ENABLE_SVE) */
Georgios Pinitasbdcdc392021-04-22 16:42:03 +010085
Michalis Spyrou20fca522021-06-07 14:23:57 +010086#if defined(ARM_COMPUTE_ENABLE_NEON)
Michalis Spyroub5a450a2021-01-06 17:40:30 +000087 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +010088 "neon_fp32_softmax_logits_1d",
Michalis Spyroub5a450a2021-01-06 17:40:30 +000089 [](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 Pinitas5fdde992021-06-25 05:42:57 +010094 "neon_fp16_softmax_logits_1d",
Michalis Spyroub5a450a2021-01-06 17:40:30 +000095 [](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 Spyrou20fca522021-06-07 14:23:57 +010099#endif /* defined(ARM_COMPUTE_ENABLE_NEON) */
Michalis Spyroub5a450a2021-01-06 17:40:30 +0000100
Michalis Spyrou20fca522021-06-07 14:23:57 +0100101#if defined(ARM_COMPUTE_ENABLE_SVE2)
Michalis Spyroub5a450a2021-01-06 17:40:30 +0000102 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +0100103 "sve2_qu8_softmax_logits_1d",
Michalis Spyrou20fca522021-06-07 14:23:57 +0100104 [](const SoftmaxSelectorData & data) { return (data.dt == DataType::QASYMM8) && data.ci.has_sve2(); },
Michalis Spyroub5a450a2021-01-06 17:40:30 +0000105 REGISTER_QASYMM8_SVE(arm_compute::cpu::sve_softmax_logits_1d_quantized<qasymm8_t>)
106 },
107 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +0100108 "sve2_qs8_softmax_logits_1d",
Michalis Spyrou20fca522021-06-07 14:23:57 +0100109 [](const SoftmaxSelectorData & data) { return (data.dt == DataType::QASYMM8_SIGNED) && data.ci.has_sve2(); },
Michalis Spyroub5a450a2021-01-06 17:40:30 +0000110 REGISTER_QASYMM8_SIGNED_SVE(arm_compute::cpu::sve_softmax_logits_1d_quantized<qasymm8_signed_t>)
111 },
Michalis Spyrou20fca522021-06-07 14:23:57 +0100112#endif /* defined(ARM_COMPUTE_ENABLE_SVE2) */
Michalis Spyroub5a450a2021-01-06 17:40:30 +0000113 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +0100114 "neon_qu8_softmax_logits_1d",
Michalis Spyroub5a450a2021-01-06 17:40:30 +0000115 [](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 Pinitas5fdde992021-06-25 05:42:57 +0100119 "neon_qs8_softmax_logits_1d",
Michalis Spyroub5a450a2021-01-06 17:40:30 +0000120 [](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 Spyroub5a450a2021-01-06 17:40:30 +0000123};
124
125static const SoftmaxLogits1DMaxKernel available_logits_1d_max_kernels[] =
126{
Michalis Spyrou20fca522021-06-07 14:23:57 +0100127#if defined(ARM_COMPUTE_ENABLE_SVE)
Michalis Spyroub5a450a2021-01-06 17:40:30 +0000128 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +0100129 "sve_fp32_logits_1d_max",
Michalis Spyrou20fca522021-06-07 14:23:57 +0100130 [](const SoftmaxSelectorData & data) { return (data.dt == DataType::F32) && data.ci.has_sve(); },
Michalis Spyroub5a450a2021-01-06 17:40:30 +0000131 REGISTER_FP32_SVE(arm_compute::cpu::sve_logits_1d_max<float>)
132 },
133 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +0100134 "sve_fp16_logits_1d_max",
Michalis Spyrou20fca522021-06-07 14:23:57 +0100135 [](const SoftmaxSelectorData & data) { return (data.dt == DataType::F16) && data.ci.has_sve(); },
Michalis Spyroub5a450a2021-01-06 17:40:30 +0000136 REGISTER_FP16_SVE(arm_compute::cpu::sve_logits_1d_max<float16_t>)
137 },
138 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +0100139 "sve_qu8_logits_1d_max",
Michalis Spyrou20fca522021-06-07 14:23:57 +0100140 [](const SoftmaxSelectorData & data) { return (data.dt == DataType::QASYMM8) && data.ci.has_sve(); },
Michalis Spyroub5a450a2021-01-06 17:40:30 +0000141 REGISTER_QASYMM8_SVE(arm_compute::cpu::sve_logits_1d_max<qasymm8_t>)
142 },
143 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +0100144 "sve_qs8_logits_1d_max",
Michalis Spyrou20fca522021-06-07 14:23:57 +0100145 [](const SoftmaxSelectorData & data) { return (data.dt == DataType::QASYMM8_SIGNED) && data.ci.has_sve(); },
Michalis Spyroub5a450a2021-01-06 17:40:30 +0000146 REGISTER_QASYMM8_SIGNED_SVE(arm_compute::cpu::sve_logits_1d_max<qasymm8_signed_t>)
147 },
Michalis Spyrou20fca522021-06-07 14:23:57 +0100148#endif /* defined(ARM_COMPUTE_ENABLE_SVE) */
149#if defined(ARM_COMPUTE_ENABLE_NEON)
Michalis Spyroub5a450a2021-01-06 17:40:30 +0000150 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +0100151 "neon_fp32_logits_1d_max",
Michalis Spyroub5a450a2021-01-06 17:40:30 +0000152 [](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 Pinitas5fdde992021-06-25 05:42:57 +0100157 "neon_fp16_logits_1d_max",
Michalis Spyroub5a450a2021-01-06 17:40:30 +0000158 [](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 Pinitas5fdde992021-06-25 05:42:57 +0100163 "neon_qu8_logits_1d_max",
Michalis Spyroub5a450a2021-01-06 17:40:30 +0000164 [](const SoftmaxSelectorData & data) { return (data.dt == DataType::QASYMM8); },
165 REGISTER_QASYMM8_NEON(arm_compute::cpu::neon_logits_1d_max<qasymm8_t>)
166 },
167 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +0100168 "neon_qs8_logits_1d_max",
Michalis Spyroub5a450a2021-01-06 17:40:30 +0000169 [](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 Spyrou20fca522021-06-07 14:23:57 +0100172#endif /* defined(ARM_COMPUTE_ENABLE_NEON) */
Michalis Spyroub5a450a2021-01-06 17:40:30 +0000173};
174
175const SoftmaxLogits1DKernel *get_implementation_logits(const SoftmaxSelectorData &data)
176{
177 for(const auto &uk : available_logits_1d_kernels)
178 {
Michalis Spyrou20fca522021-06-07 14:23:57 +0100179 if(uk.is_selected({ data.dt, CPUInfo::get() }))
Michalis Spyroub5a450a2021-01-06 17:40:30 +0000180 {
181 return &uk;
182 }
183 }
184 return nullptr;
185}
186
187const SoftmaxLogits1DMaxKernel *get_implementation_logits_max(const SoftmaxSelectorData &data)
188{
189 for(const auto &uk : available_logits_1d_max_kernels)
190 {
Michalis Spyrou20fca522021-06-07 14:23:57 +0100191 if(uk.is_selected({ data.dt, CPUInfo::get() }))
Michalis Spyroub5a450a2021-01-06 17:40:30 +0000192 {
193 return &uk;
194 }
195 }
196 return nullptr;
197}
198
Diego Lopez Recas35ceeb22017-12-04 18:56:10 +0000199Status validate_arguments_logits_1d_max(const ITensorInfo &input, const ITensorInfo &output)
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000200{
Anthony Barbiereaefd002018-07-20 17:49:35 +0100201 ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(&input);
Sang-Hoon Parkc3a74202019-11-22 16:05:46 +0000202 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&input, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED, DataType::F16, DataType::F32);
Pablo Tellob49a7152017-07-11 16:31:35 +0100203
Diego Lopez Recas35ceeb22017-12-04 18:56:10 +0000204 // Validate in case of configured output
205 if(output.total_size() != 0)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100206 {
Diego Lopez Recas35ceeb22017-12-04 18:56:10 +0000207 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(&input, &output);
Diego Lopez Recas35ceeb22017-12-04 18:56:10 +0000208 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 Barbier6ff3b192017-09-04 18:44:23 +0100210 }
Diego Lopez Recas35ceeb22017-12-04 18:56:10 +0000211
212 return Status{};
213}
214
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100215} // namespace
216
Michalis Spyrou373b4072021-01-20 16:41:12 +0000217void CpuLogits1DMaxKernel::configure(const ITensorInfo *src, ITensorInfo *dst)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100218{
Michalis Spyrou373b4072021-01-20 16:41:12 +0000219 ARM_COMPUTE_ERROR_ON_NULLPTR(src, dst);
Michalis Spyrou373b4072021-01-20 16:41:12 +0000220 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments_logits_1d_max(*src, *dst));
Michalis Spyrou2dc7e402020-02-28 14:41:35 +0000221
222 // Softmax across the x dimension
Michalis Spyrou373b4072021-01-20 16:41:12 +0000223 const TensorShape output_shape = TensorShape(src->tensor_shape()).set(0, 1);
Michalis Spyrou2dc7e402020-02-28 14:41:35 +0000224 // Output auto initialization if not yet initialized
Michalis Spyrou373b4072021-01-20 16:41:12 +0000225 auto_init_if_empty(*dst, output_shape, 1, src->data_type(), src->quantization_info());
Michalis Spyrou2dc7e402020-02-28 14:41:35 +0000226
Georgios Pinitas5fdde992021-06-25 05:42:57 +0100227 const auto *uk = get_implementation_logits_max(SoftmaxSelectorData{ src->data_type(), CPUInfo::get() });
228 ARM_COMPUTE_ERROR_ON_NULLPTR(uk);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100229
Georgios Pinitas5fdde992021-06-25 05:42:57 +0100230 _run_method = uk->ukernel;
231 _name = std::string("CpuLogits1DMaxKernel").append("/").append(uk->name);
232
233 Window win = calculate_max_window(*src, Steps());
Michalis Spyrou373b4072021-01-20 16:41:12 +0000234 ICpuKernel::configure(win);
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000235}
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100236
Michalis Spyrou373b4072021-01-20 16:41:12 +0000237Status CpuLogits1DMaxKernel::validate(const ITensorInfo *src, const ITensorInfo *dst)
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000238{
Michalis Spyrou373b4072021-01-20 16:41:12 +0000239 ARM_COMPUTE_ERROR_ON_NULLPTR(src, dst);
240 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments_logits_1d_max(*src, *dst));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100241
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000242 return Status{};
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100243}
244
Michalis Spyrou373b4072021-01-20 16:41:12 +0000245void CpuLogits1DMaxKernel::run_op(ITensorPack &tensors, const Window &window, const ThreadInfo &info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100246{
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100247 ARM_COMPUTE_UNUSED(info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100248 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
Michalis Spyrou373b4072021-01-20 16:41:12 +0000249 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICpuKernel::window(), window);
Georgios Pinitas5fdde992021-06-25 05:42:57 +0100250 ARM_COMPUTE_ERROR_ON(_run_method == nullptr);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100251
Michalis Spyrou373b4072021-01-20 16:41:12 +0000252 const auto src = tensors.get_const_tensor(TensorType::ACL_SRC);
253 auto dst = tensors.get_tensor(TensorType::ACL_DST);
254
Georgios Pinitas5fdde992021-06-25 05:42:57 +0100255 _run_method(src, dst, window);
Michalis Spyrou373b4072021-01-20 16:41:12 +0000256}
257
258const char *CpuLogits1DMaxKernel::name() const
259{
Georgios Pinitas5fdde992021-06-25 05:42:57 +0100260 return _name.c_str();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100261}
262
263namespace
264{
Michalis Spyrou373b4072021-01-20 16:41:12 +0000265Status validate_arguments_logits_softmax(const ITensorInfo &src, const ITensorInfo &max,
266 const ITensorInfo &dst, const float beta, const ITensorInfo &tmp, bool is_log)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100267{
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +0100268 ARM_COMPUTE_UNUSED(beta);
Diego Lopez Recas35ceeb22017-12-04 18:56:10 +0000269 // Check input
Michalis Spyrou373b4072021-01-20 16:41:12 +0000270 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 Tellob49a7152017-07-11 16:31:35 +0100272
Michalis Spyrou373b4072021-01-20 16:41:12 +0000273 const bool is_quantized_asymmetric = is_data_type_quantized_asymmetric(src.data_type());
Georgios Pinitas9247c922017-06-28 18:29:47 +0100274
Diego Lopez Recas35ceeb22017-12-04 18:56:10 +0000275 // Check max
Michalis Spyrou373b4072021-01-20 16:41:12 +0000276 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 Pinitas9247c922017-06-28 18:29:47 +0100279
Diego Lopez Recas35ceeb22017-12-04 18:56:10 +0000280 // Check output if configured
Michalis Spyrou373b4072021-01-20 16:41:12 +0000281 if(dst.total_size() != 0)
Georgios Pinitas9247c922017-06-28 18:29:47 +0100282 {
Michalis Spyrou373b4072021-01-20 16:41:12 +0000283 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 Pinitas9247c922017-06-28 18:29:47 +0100287 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100288
Diego Lopez Recas35ceeb22017-12-04 18:56:10 +0000289 // Check tmp if configured
290 if(tmp.total_size() != 0)
291 {
Michalis Spyrou373b4072021-01-20 16:41:12 +0000292 const DataType tmp_data_type = is_quantized_asymmetric ? DataType::F32 : src.data_type();
Diego Lopez Recas35ceeb22017-12-04 18:56:10 +0000293 ARM_COMPUTE_RETURN_ERROR_ON(tmp.data_type() != tmp_data_type);
Diego Lopez Recas35ceeb22017-12-04 18:56:10 +0000294 // 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 Spyrou373b4072021-01-20 16:41:12 +0000296 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(&src, &tmp);
Diego Lopez Recas35ceeb22017-12-04 18:56:10 +0000297 }
298
299 return Status{};
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100300}
Diego Lopez Recas35ceeb22017-12-04 18:56:10 +0000301} // namespace
302
Sang-Hoon Parkd24affe2019-10-08 18:07:23 +0100303template <bool IS_LOG>
Michalis Spyrou373b4072021-01-20 16:41:12 +0000304void CpuLogits1DSoftmaxKernel<IS_LOG>::configure(const ITensorInfo *src, const ITensorInfo *max, ITensorInfo *dst, const float beta, ITensorInfo *tmp)
Diego Lopez Recas35ceeb22017-12-04 18:56:10 +0000305{
Michalis Spyrou373b4072021-01-20 16:41:12 +0000306 ARM_COMPUTE_ERROR_ON_NULLPTR(src, max, dst, tmp);
Michalis Spyrou373b4072021-01-20 16:41:12 +0000307 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments_logits_softmax(*src, *max, *dst, beta, *tmp, IS_LOG));
308
Diego Lopez Recas35ceeb22017-12-04 18:56:10 +0000309 // Configure kernel window
Michalis Spyrou373b4072021-01-20 16:41:12 +0000310 const bool is_quantized_asymmetric = is_data_type_quantized_asymmetric(src->data_type());
Michalis Spyrou2dc7e402020-02-28 14:41:35 +0000311
312 // Output auto initialization if not yet initialized
Michalis Spyrou373b4072021-01-20 16:41:12 +0000313 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 Spyrou2dc7e402020-02-28 14:41:35 +0000315
316 // Tmp auto initialization if not yet initialized
Michalis Spyrou373b4072021-01-20 16:41:12 +0000317 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 Spyrou2dc7e402020-02-28 14:41:35 +0000319
Georgios Pinitas5fdde992021-06-25 05:42:57 +0100320 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 Spyrou2dc7e402020-02-28 14:41:35 +0000329 // Configure kernel window
SiCongLib88272e2021-02-24 15:40:57 +0000330 Window win = calculate_max_window(*max, Steps());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100331
Michalis Spyrou373b4072021-01-20 16:41:12 +0000332 ICpuKernel::configure(win);
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000333}
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100334
Sang-Hoon Parkd24affe2019-10-08 18:07:23 +0100335template <bool IS_LOG>
Michalis Spyrou373b4072021-01-20 16:41:12 +0000336Status CpuLogits1DSoftmaxKernel<IS_LOG>::validate(const ITensorInfo *src, const ITensorInfo *max,
337 const ITensorInfo *dst, const float beta, const ITensorInfo *tmp)
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000338{
Michalis Spyrou373b4072021-01-20 16:41:12 +0000339 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 Barbier6ff3b192017-09-04 18:44:23 +0100341
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000342 return Status{};
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100343}
344
Sang-Hoon Parkd24affe2019-10-08 18:07:23 +0100345template <bool IS_LOG>
Michalis Spyrou373b4072021-01-20 16:41:12 +0000346void CpuLogits1DSoftmaxKernel<IS_LOG>::run_op(ITensorPack &tensors, const Window &window, const ThreadInfo &info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100347{
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100348 ARM_COMPUTE_UNUSED(info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100349 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
Michalis Spyrou373b4072021-01-20 16:41:12 +0000350 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICpuKernel::window(), window);
Georgios Pinitas5fdde992021-06-25 05:42:57 +0100351 ARM_COMPUTE_ERROR_ON(_run_method == nullptr);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100352
Michalis Spyrou373b4072021-01-20 16:41:12 +0000353 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 Recas35ceeb22017-12-04 18:56:10 +0000357
Michalis Spyrou373b4072021-01-20 16:41:12 +0000358 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 Recas35ceeb22017-12-04 18:56:10 +0000360
Michalis Spyrou373b4072021-01-20 16:41:12 +0000361 ARM_COMPUTE_ERROR_ON(tmp->info()->total_size() < (info.num_threads * tmp_size_for_thread));
Diego Lopez Recas35ceeb22017-12-04 18:56:10 +0000362
Michalis Spyrou373b4072021-01-20 16:41:12 +0000363 void *tmp_for_thread = tmp->buffer() + (info.thread_id * tmp_size_for_thread);
Georgios Pinitas5fdde992021-06-25 05:42:57 +0100364 _run_method(src, max, tmp_for_thread, dst, _beta, IS_LOG, window);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100365}
366
Michalis Spyrou373b4072021-01-20 16:41:12 +0000367template <bool IS_LOG>
368const char *CpuLogits1DSoftmaxKernel<IS_LOG>::name() const
369{
Georgios Pinitas5fdde992021-06-25 05:42:57 +0100370 return _name.c_str();
Michalis Spyrou373b4072021-01-20 16:41:12 +0000371}
Sang-Hoon Parkd24affe2019-10-08 18:07:23 +0100372
Michalis Spyrou373b4072021-01-20 16:41:12 +0000373template class CpuLogits1DSoftmaxKernel<true>;
374template class CpuLogits1DSoftmaxKernel<false>;
375
376} // namespace kernels
377} // namespace cpu
Diego Lopez Recas35ceeb22017-12-04 18:56:10 +0000378} // namespace arm_compute