• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2017-2022 Arm Limited.
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  */
24 #include "src/cpu/kernels/CpuSoftmaxKernel.h"
25 
26 #include "arm_compute/core/Error.h"
27 #include "arm_compute/core/Helpers.h"
28 #include "arm_compute/core/ITensor.h"
29 #include "arm_compute/core/TensorInfo.h"
30 #include "arm_compute/core/Validate.h"
31 #include "arm_compute/core/Window.h"
32 #include "src/core/CPP/Validate.h"
33 #include "src/core/helpers/AutoConfiguration.h"
34 #include "src/core/helpers/WindowHelpers.h"
35 
36 #include "src/core/common/Registrars.h"
37 #include "src/cpu/kernels/softmax/list.h"
38 
39 namespace arm_compute
40 {
41 namespace cpu
42 {
43 namespace kernels
44 {
45 namespace
46 {
47 /* Softmax Logits 1D Max - identifying the max value of 1D Logits  */
48 static const std::vector<CpuLogits1DMaxKernel::SoftmaxLogits1DMaxKernel> available_kernels_max_logits =
49 {
50     {
51         "sve_fp32_logits_1d_max",
__anon0caa09040202() 52         [](const DataTypeISASelectorData & data) { return (data.dt == DataType::F32) && data.isa.sve; },
53         REGISTER_FP32_SVE(sve_fp32_logits)
54     },
55     {
56         "sve_fp16_logits_1d_max",
__anon0caa09040302() 57         [](const DataTypeISASelectorData & data) { return (data.dt == DataType::F16) && data.isa.sve && data.isa.fp16; },
58         REGISTER_FP16_SVE(sve_fp16_logits)
59     },
60     {
61         "sve_qu8_logits_1d_max",
__anon0caa09040402() 62         [](const DataTypeISASelectorData & data) { return (data.dt == DataType::QASYMM8) && data.isa.sve; },
63         REGISTER_QASYMM8_SVE(sve_qasymm8_logits)
64     },
65     {
66         "sve_qs8_logits_1d_max",
__anon0caa09040502() 67         [](const DataTypeISASelectorData & data) { return (data.dt == DataType::QASYMM8_SIGNED) && data.isa.sve; },
68         REGISTER_QASYMM8_SIGNED_SVE(sve_qasymm8_signed_logits)
69     },
70     {
71         "neon_fp32_logits_1d_max",
__anon0caa09040602() 72         [](const DataTypeISASelectorData & data) { return (data.dt == DataType::F32); },
73         REGISTER_FP32_NEON(neon_fp32_logits)
74     },
75     {
76         "neon_fp16_logits_1d_max",
__anon0caa09040702() 77         [](const DataTypeISASelectorData & data) { return (data.dt == DataType::F16) && data.isa.fp16; },
78         REGISTER_FP16_NEON(neon_fp16_logits)
79     },
80     {
81         "neon_qu8_logits_1d_max",
__anon0caa09040802() 82         [](const DataTypeISASelectorData & data) { return (data.dt == DataType::QASYMM8); },
83         REGISTER_QASYMM8_NEON(neon_qasymm8_logits)
84     },
85     {
86         "neon_qs8_logits_1d_max",
__anon0caa09040902() 87         [](const DataTypeISASelectorData & data) { return (data.dt == DataType::QASYMM8_SIGNED); },
88         REGISTER_QASYMM8_SIGNED_NEON(neon_qasymm8_singed_logits)
89     },
90 };
91 
validate_arguments_logits_1d_max(const ITensorInfo & input,const ITensorInfo & output)92 Status validate_arguments_logits_1d_max(const ITensorInfo &input, const ITensorInfo &output)
93 {
94     ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(&input);
95     ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&input, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED, DataType::F16, DataType::F32);
96 
97     // Validate in case of configured output
98     if(output.total_size() != 0)
99     {
100         ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(&input, &output);
101         ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(&input, &output);
102         ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(output.tensor_shape(), TensorShape(input.tensor_shape()).set(0, 1));
103     }
104 
105     return Status{};
106 }
107 } //namespace
get_available_kernels()108 const std::vector<CpuLogits1DMaxKernel::SoftmaxLogits1DMaxKernel> &CpuLogits1DMaxKernel::get_available_kernels()
109 {
110     return available_kernels_max_logits;
111 }
112 
configure(const ITensorInfo * src,ITensorInfo * dst)113 void CpuLogits1DMaxKernel::configure(const ITensorInfo *src, ITensorInfo *dst)
114 {
115     ARM_COMPUTE_ERROR_ON_NULLPTR(src, dst);
116     ARM_COMPUTE_ERROR_THROW_ON(validate_arguments_logits_1d_max(*src, *dst));
117 
118     // Softmax across the x dimension
119     const TensorShape output_shape = TensorShape(src->tensor_shape()).set(0, 1);
120     // Output auto initialization if not yet initialized
121     auto_init_if_empty(*dst, output_shape, 1, src->data_type(), src->quantization_info());
122 
123     const auto *uk = get_implementation(DataTypeISASelectorData{ src->data_type(), CPUInfo::get().get_isa() });
124     ARM_COMPUTE_ERROR_ON(uk == nullptr || uk->ukernel == nullptr);
125 
126     _run_method = uk->ukernel;
127     _name       = std::string("CpuLogits1DMaxKernel").append("/").append(uk->name);
128 
129     Window win = calculate_max_window(*src, Steps());
130     ICpuKernel::configure(win);
131 }
132 
validate(const ITensorInfo * src,const ITensorInfo * dst)133 Status CpuLogits1DMaxKernel::validate(const ITensorInfo *src, const ITensorInfo *dst)
134 {
135     ARM_COMPUTE_ERROR_ON_NULLPTR(src, dst);
136     ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments_logits_1d_max(*src, *dst));
137 
138     return Status{};
139 }
140 
run_op(ITensorPack & tensors,const Window & window,const ThreadInfo & info)141 void CpuLogits1DMaxKernel::run_op(ITensorPack &tensors, const Window &window, const ThreadInfo &info)
142 {
143     ARM_COMPUTE_UNUSED(info);
144     ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
145     ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICpuKernel::window(), window);
146     ARM_COMPUTE_ERROR_ON(_run_method == nullptr);
147 
148     const auto src = tensors.get_const_tensor(TensorType::ACL_SRC);
149     auto       dst = tensors.get_tensor(TensorType::ACL_DST);
150 
151     _run_method(src, dst, window);
152 }
153 
name() const154 const char *CpuLogits1DMaxKernel::name() const
155 {
156     return _name.c_str();
157 }
158 
159 /* Softmax Logits 1D  - computation for QASYMM8 with pre-computed max.  */
160 template <bool                                                                             IS_LOG>
161 static const std::vector<typename CpuLogits1DSoftmaxKernel<IS_LOG>::SoftmaxLogits1DKernel> available_kernels_logits =
162 {
163     {
164         "sve2_qu8_softmax_logits_1d",
__anon0caa09040a02() 165         [](const DataTypeISASelectorData & data) { return (data.dt == DataType::QASYMM8) && data.isa.sve2; },
166         REGISTER_QASYMM8_SVE2(sve2_qasymm8_softmax)
167     },
168     {
169         "sve2_qs8_softmax_logits_1d",
__anon0caa09040b02() 170         [](const DataTypeISASelectorData & data) { return (data.dt == DataType::QASYMM8_SIGNED) && data.isa.sve2; },
171         REGISTER_QASYMM8_SIGNED_SVE2(sve2_qasymm8_signed_softmax)
172     },
173     {
174         "sve_fp32_softmax_logits_1d",
__anon0caa09040c02() 175         [](const DataTypeISASelectorData & data) { return (data.dt == DataType::F32) && data.isa.sve; },
176         REGISTER_FP32_SVE(sve_fp32_softmax)
177     },
178     {
179         "sve_fp16_softmax_logits_1d",
__anon0caa09040d02() 180         [](const DataTypeISASelectorData & data) { return (data.dt == DataType::F16) && data.isa.sve && data.isa.fp16; },
181         REGISTER_FP16_SVE(sve_fp16_softmax)
182     },
183 
184     {
185         "neon_fp32_softmax_logits_1d",
__anon0caa09040e02() 186         [](const DataTypeISASelectorData & data) { return (data.dt == DataType::F32); },
187         REGISTER_FP32_NEON(neon_fp32_softmax)
188     },
189     {
190         "neon_fp16_softmax_logits_1d",
__anon0caa09040f02() 191         [](const DataTypeISASelectorData & data) { return (data.dt == DataType::F16) && data.isa.fp16; },
192         REGISTER_FP16_NEON(neon_fp16_softmax)
193     },
194     {
195         "neon_qu8_softmax_logits_1d",
__anon0caa09041002() 196         [](const DataTypeISASelectorData & data) { return (data.dt == DataType::QASYMM8); },
197         REGISTER_QASYMM8_NEON(arm_compute::cpu::neon_qasymm8_softmax)
198     },
199     {
200         "neon_qs8_softmax_logits_1d",
__anon0caa09041102() 201         [](const DataTypeISASelectorData & data) { return (data.dt == DataType::QASYMM8_SIGNED); },
202         REGISTER_QASYMM8_SIGNED_NEON(arm_compute::cpu::neon_qasymm8_signed_softmax)
203     },
204 };
205 namespace
206 {
validate_arguments_logits_softmax(const ITensorInfo & src,const ITensorInfo & max,const ITensorInfo & dst,const float beta,const ITensorInfo & tmp,bool is_log)207 Status validate_arguments_logits_softmax(const ITensorInfo &src, const ITensorInfo &max,
208                                          const ITensorInfo &dst, const float beta, const ITensorInfo &tmp, bool is_log)
209 {
210     ARM_COMPUTE_UNUSED(beta);
211     // Check input
212     ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(&src);
213     ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&src, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED, DataType::F16, DataType::F32);
214 
215     const bool is_quantized_asymmetric = is_data_type_quantized_asymmetric(src.data_type());
216 
217     // Check max
218     ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(&src, &max);
219     ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(TensorShape(src.tensor_shape()).set(0, 1), max.tensor_shape());
220     ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(&src, &max);
221 
222     // Check output if configured
223     if(dst.total_size() != 0)
224     {
225         const QuantizationInfo output_quantization = is_quantized_asymmetric ? arm_compute::get_softmax_output_quantization_info(src.data_type(), is_log) : dst.quantization_info();
226         ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(&src, &dst);
227         ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(&src, &dst);
228         ARM_COMPUTE_RETURN_ERROR_ON(dst.quantization_info() != output_quantization);
229     }
230 
231     // Check tmp if configured
232     if(tmp.total_size() != 0)
233     {
234         const DataType tmp_data_type = is_quantized_asymmetric ? DataType::F32 : src.data_type();
235         ARM_COMPUTE_RETURN_ERROR_ON(tmp.data_type() != tmp_data_type);
236         // We could potentially reduce tmp memory if we could predict or make an assumption
237         // on the maximum number of threads that will run in parallel.
238         ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(&src, &tmp);
239     }
240 
241     return Status{};
242 }
243 } // namespace
244 
245 template <bool                                                                       IS_LOG>
get_available_kernels()246 const std::vector<typename CpuLogits1DSoftmaxKernel<IS_LOG>::SoftmaxLogits1DKernel> &CpuLogits1DSoftmaxKernel<IS_LOG>::get_available_kernels()
247 {
248     return available_kernels_logits<IS_LOG>;
249 }
250 
251 template <bool IS_LOG>
configure(const ITensorInfo * src,const ITensorInfo * max,ITensorInfo * dst,const float beta,ITensorInfo * tmp)252 void CpuLogits1DSoftmaxKernel<IS_LOG>::configure(const ITensorInfo *src, const ITensorInfo *max, ITensorInfo *dst, const float beta, ITensorInfo *tmp)
253 {
254     ARM_COMPUTE_ERROR_ON_NULLPTR(src, max, dst, tmp);
255     ARM_COMPUTE_ERROR_THROW_ON(validate_arguments_logits_softmax(*src, *max, *dst, beta, *tmp, IS_LOG));
256 
257     // Configure kernel window
258     const bool is_quantized_asymmetric = is_data_type_quantized_asymmetric(src->data_type());
259 
260     // Output auto initialization if not yet initialized
261     const QuantizationInfo output_quantization = is_quantized_asymmetric ? arm_compute::get_softmax_output_quantization_info(src->data_type(), IS_LOG) : dst->quantization_info();
262     auto_init_if_empty(*dst, TensorInfo(*src).set_quantization_info(output_quantization).reset_padding());
263 
264     // Tmp auto initialization if not yet initialized
265     const DataType tmp_data_type = is_quantized_asymmetric ? DataType::F32 : src->data_type();
266     auto_init_if_empty(*tmp, TensorInfo(*src).set_data_type(tmp_data_type).reset_padding());
267 
268     const auto *uk = CpuLogits1DSoftmaxKernel<IS_LOG>::get_implementation(DataTypeISASelectorData{ src->data_type(), CPUInfo::get().get_isa() });
269     ARM_COMPUTE_ERROR_ON(uk == nullptr || uk->ukernel == nullptr);
270 
271     std::string kernel_name = IS_LOG ? std::string("CpuLogits1DLogSoftmaxKernel") : std::string("CpuLogits1DSoftmaxKernel");
272 
273     _beta       = beta;
274     _run_method = uk->ukernel;
275     _name       = kernel_name.append("/").append(uk->name);
276 
277     // Configure kernel window
278     Window win = calculate_max_window(*max, Steps());
279 
280     ICpuKernel<CpuLogits1DSoftmaxKernel<IS_LOG>>::configure(win);
281 }
282 
283 template <bool IS_LOG>
validate(const ITensorInfo * src,const ITensorInfo * max,const ITensorInfo * dst,const float beta,const ITensorInfo * tmp)284 Status CpuLogits1DSoftmaxKernel<IS_LOG>::validate(const ITensorInfo *src, const ITensorInfo *max,
285                                                   const ITensorInfo *dst, const float beta, const ITensorInfo *tmp)
286 {
287     ARM_COMPUTE_ERROR_ON_NULLPTR(src, max, dst, tmp);
288     ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments_logits_softmax(*src, *max, *dst, beta, *tmp, IS_LOG));
289 
290     return Status{};
291 }
292 
293 template <bool IS_LOG>
run_op(ITensorPack & tensors,const Window & window,const ThreadInfo & info)294 void CpuLogits1DSoftmaxKernel<IS_LOG>::run_op(ITensorPack &tensors, const Window &window, const ThreadInfo &info)
295 {
296     ARM_COMPUTE_UNUSED(info);
297     ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
298     ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICpuKernel<CpuLogits1DSoftmaxKernel<IS_LOG>>::window(), window);
299     ARM_COMPUTE_ERROR_ON(_run_method == nullptr);
300 
301     const auto src = tensors.get_const_tensor(TensorType::ACL_SRC_0);
302     auto       max = tensors.get_tensor(TensorType::ACL_SRC_1);
303     auto       dst = tensors.get_tensor(TensorType::ACL_DST_0);
304     auto       tmp = tensors.get_tensor(TensorType::ACL_DST_1);
305 
306     const unsigned int num_elems_processed_per_iteration = src->info()->valid_region().shape.x();
307     const unsigned int tmp_size_for_thread               = tmp->info()->element_size() * num_elems_processed_per_iteration;
308 
309     ARM_COMPUTE_ERROR_ON(tmp->info()->total_size() < (info.num_threads * tmp_size_for_thread));
310 
311     void *tmp_for_thread = tmp->buffer() + (info.thread_id * tmp_size_for_thread);
312     _run_method(src, max, tmp_for_thread, dst, _beta, IS_LOG, window);
313 }
314 
315 template <bool IS_LOG>
name() const316 const char    *CpuLogits1DSoftmaxKernel<IS_LOG>::name() const
317 {
318     return _name.c_str();
319 }
320 
321 template class CpuLogits1DSoftmaxKernel<true>;
322 template class CpuLogits1DSoftmaxKernel<false>;
323 
324 } // namespace kernels
325 } // namespace cpu
326 } // namespace arm_compute
327