Compute Library
 22.11
CpuSoftmaxKernel.cpp
Go to the documentation of this file.
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  */
25 
26 #include "arm_compute/core/Error.h"
32 #include "src/core/CPP/Validate.h"
35 
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",
52  [](const DataTypeISASelectorData & data) { return (data.dt == DataType::F32) && data.isa.sve; },
54  },
55  {
56  "sve_fp16_logits_1d_max",
57  [](const DataTypeISASelectorData & data) { return (data.dt == DataType::F16) && data.isa.sve && data.isa.fp16; },
59  },
60  {
61  "sve_qu8_logits_1d_max",
62  [](const DataTypeISASelectorData & data) { return (data.dt == DataType::QASYMM8) && data.isa.sve; },
64  },
65  {
66  "sve_qs8_logits_1d_max",
67  [](const DataTypeISASelectorData & data) { return (data.dt == DataType::QASYMM8_SIGNED) && data.isa.sve; },
69  },
70  {
71  "neon_fp32_logits_1d_max",
72  [](const DataTypeISASelectorData & data) { return (data.dt == DataType::F32); },
74  },
75  {
76  "neon_fp16_logits_1d_max",
77  [](const DataTypeISASelectorData & data) { return (data.dt == DataType::F16) && data.isa.fp16; },
79  },
80  {
81  "neon_qu8_logits_1d_max",
82  [](const DataTypeISASelectorData & data) { return (data.dt == DataType::QASYMM8); },
84  },
85  {
86  "neon_qs8_logits_1d_max",
87  [](const DataTypeISASelectorData & data) { return (data.dt == DataType::QASYMM8_SIGNED); },
89  },
90 };
91 
92 Status validate_arguments_logits_1d_max(const ITensorInfo &input, const ITensorInfo &output)
93 {
96 
97  // Validate in case of configured output
98  if(output.total_size() != 0)
99  {
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
108 const std::vector<CpuLogits1DMaxKernel::SoftmaxLogits1DMaxKernel> &CpuLogits1DMaxKernel::get_available_kernels()
109 {
110  return available_kernels_max_logits;
111 }
112 
114 {
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 
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 
134 {
136  ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments_logits_1d_max(*src, *dst));
137 
138  return Status{};
139 }
140 
142 {
143  ARM_COMPUTE_UNUSED(info);
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 
154 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",
165  [](const DataTypeISASelectorData & data) { return (data.dt == DataType::QASYMM8) && data.isa.sve2; },
167  },
168  {
169  "sve2_qs8_softmax_logits_1d",
170  [](const DataTypeISASelectorData & data) { return (data.dt == DataType::QASYMM8_SIGNED) && data.isa.sve2; },
172  },
173  {
174  "sve_fp32_softmax_logits_1d",
175  [](const DataTypeISASelectorData & data) { return (data.dt == DataType::F32) && data.isa.sve; },
177  },
178  {
179  "sve_fp16_softmax_logits_1d",
180  [](const DataTypeISASelectorData & data) { return (data.dt == DataType::F16) && data.isa.sve && data.isa.fp16; },
182  },
183 
184  {
185  "neon_fp32_softmax_logits_1d",
186  [](const DataTypeISASelectorData & data) { return (data.dt == DataType::F32); },
188  },
189  {
190  "neon_fp16_softmax_logits_1d",
191  [](const DataTypeISASelectorData & data) { return (data.dt == DataType::F16) && data.isa.fp16; },
193  },
194  {
195  "neon_qu8_softmax_logits_1d",
196  [](const DataTypeISASelectorData & data) { return (data.dt == DataType::QASYMM8); },
198  },
199  {
200  "neon_qs8_softmax_logits_1d",
201  [](const DataTypeISASelectorData & data) { return (data.dt == DataType::QASYMM8_SIGNED); },
203  },
204 };
205 namespace
206 {
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
214 
215  const bool is_quantized_asymmetric = is_data_type_quantized_asymmetric(src.data_type());
216 
217  // Check 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();
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.
239  }
240 
241  return Status{};
242 }
243 } // namespace
244 
245 template <bool IS_LOG>
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>
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 
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 
281 }
282 
283 template <bool IS_LOG>
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>
295 {
296  ARM_COMPUTE_UNUSED(info);
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>
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
const char * name() const override
Name of the kernel.
void neon_fp32_softmax(const ITensor *in, const ITensor *max, void *const tmp, ITensor *out, const float beta, bool is_log, const Window &window)
Definition: fp32.cpp:31
Window calculate_max_window(const ValidRegion &valid_region, const Steps &steps, bool skip_border, BorderSize border_size)
static Status validate(const ITensorInfo *src, const ITensorInfo *dst)
Static function to check if given info will lead to a valid configuration.
const Window & window() const
The maximum window the kernel can be executed on.
Definition: IKernel.cpp:28
Shape of a tensor.
Definition: TensorShape.h:39
void configure(const ITensorInfo *src, const ITensorInfo *max, ITensorInfo *dst, const float beta, ITensorInfo *tmp)
Set the input and output tensors.
void neon_qasymm8_softmax(const ITensor *in, const ITensor *max, void *const tmp, ITensor *out, const float beta, bool is_log, const Window &window)
Definition: qasymm8.cpp:31
void neon_qasymm8_signed_softmax(const ITensor *in, const ITensor *max, void *const tmp, ITensor *out, const float beta, bool is_log, const Window &window)
static const auto * get_implementation(const SelectorType &selector, KernelSelectionType selection_type=KernelSelectionType::Supported)
Micro-kernel selector.
Definition: ICpuKernel.h:53
static const std::vector< SoftmaxLogits1DKernel > & get_available_kernels()
void sve_qasymm8_signed_logits(const ITensor *in, ITensor *out, const Window &window)
#define ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(tensor)
Definition: Validate.h:115
#define REGISTER_FP16_NEON(func_name)
Definition: Registrars.h:48
void neon_qasymm8_logits(const ITensor *in, ITensor *out, const Window &window)
Definition: qasymm8.cpp:37
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(...)
Definition: Validate.h:606
void sve2_qasymm8_softmax(const ITensor *in, const ITensor *max, void *const tmp, ITensor *out, const float beta, bool is_log, const Window &window)
Definition: qasymm8.cpp:32
#define REGISTER_FP32_NEON(func_name)
Definition: Registrars.h:74
#define ARM_COMPUTE_RETURN_ON_ERROR(status)
Checks if a status contains an error and returns it.
Definition: Error.h:204
virtual DataType data_type() const =0
Data type used for each element of the tensor.
1 channel, 1 F32 per channel
#define REGISTER_FP32_SVE(func_name)
Definition: Registrars.h:75
ITensorInfo & set_data_type(DataType data_type) override
Set the data type to the specified value.
Definition: TensorInfo.cpp:307
#define REGISTER_QASYMM8_SVE(func_name)
Definition: Registrars.h:118
#define ARM_COMPUTE_ERROR_ON(cond)
If the condition is true then an error message is printed and an exception thrown.
Definition: Error.h:466
#define REGISTER_QASYMM8_SIGNED_NEON(func_name)
Definition: Registrars.h:96
Store the tensor&#39;s metadata.
Definition: ITensorInfo.h:40
QuantizationInfo get_softmax_output_quantization_info(DataType input_type, bool is_log)
Returns output quantization information for softmax layer.
Definition: Utils.cpp:537
#define ARM_COMPUTE_ERROR_THROW_ON(status)
Definition: Error.h:455
Interface for softmax computation for QASYMM8 with pre-computed max.
Status class.
Definition: Error.h:52
const char * name() const override
Name of the kernel.
virtual ITensorInfo & reset_padding()=0
Resets the padding settings of the tensor.
void sve_fp32_logits(const ITensor *in, ITensor *out, const Window &window)
Definition: fp32.cpp:38
#define ARM_COMPUTE_RETURN_ERROR_ON(cond)
If the condition is true, an error is returned.
Definition: Error.h:296
void sve_fp16_softmax(const ITensor *in, const ITensor *max, void *const tmp, ITensor *out, const float beta, bool is_log, const Window &window)
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(...)
Definition: Validate.h:284
SimpleTensor< float > src
Definition: DFT.cpp:155
Copyright (c) 2017-2022 Arm Limited.
1 channel, 1 F16 per channel
void neon_qasymm8_singed_logits(const ITensor *in, ITensor *out, const Window &window)
ITensorInfo & set_quantization_info(const QuantizationInfo &quantization_info) override
Set the quantization settings (scale and offset) of the tensor.
Definition: TensorInfo.cpp:366
#define REGISTER_QASYMM8_SIGNED_SVE(func_name)
Definition: Registrars.h:97
void sve_qasymm8_logits(const ITensor *in, ITensor *out, const Window &window)
Definition: qasymm8.cpp:32
const ITensor * get_const_tensor(int id) const
Get constant tensor of a given id.
Definition: ITensorPack.cpp:54
Quantization information.
#define ARM_COMPUTE_UNUSED(...)
To avoid unused variables warnings.
Definition: Error.h:152
#define REGISTER_QASYMM8_NEON(func_name)
Definition: Registrars.h:117
virtual const TensorShape & tensor_shape() const =0
Size for each dimension of the tensor.
quantized, asymmetric fixed-point 8-bit number unsigned
Class to describe a number of elements in each dimension.
Definition: Steps.h:40
void run_op(ITensorPack &tensors, const Window &window, const ThreadInfo &info) override
Execute the kernel on the passed window.
unsigned int num_elems_processed_per_iteration
bool auto_init_if_empty(ITensorInfo &info, const TensorShape &shape, int num_channels, DataType data_type, QuantizationInfo quantization_info=QuantizationInfo())
Auto initialize the tensor info (shape, number of channels and data type) if the current assignment i...
void configure(const ITensorInfo *src, ITensorInfo *dst)
Set the input and output tensors.
static const std::vector< SoftmaxLogits1DMaxKernel > & get_available_kernels()
#define REGISTER_QASYMM8_SIGNED_SVE2(func_name)
Definition: Registrars.h:98
void sve_fp16_logits(const ITensor *in, ITensor *out, const Window &window)
virtual QuantizationInfo quantization_info() const =0
Get the quantization settings (scale and offset) of the tensor.
#define ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(k)
Definition: Validate.h:915
bool is_data_type_quantized_asymmetric(DataType dt)
Check if a given data type is of asymmetric quantized type.
Definition: Utils.h:1052
ScaleKernelInfo info(interpolation_policy, default_border_mode, PixelValue(), sampling_policy, false)
ITensor * get_tensor(int id)
Get tensor of a given id from the pac.
Definition: ITensorPack.cpp:64
Information about executing thread and CPU.
Definition: CPPTypes.h:179
virtual size_t total_size() const =0
Returns the total size of the tensor in bytes.
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(...)
Definition: Validate.h:439
#define REGISTER_FP16_SVE(func_name)
Definition: Registrars.h:49
static Status validate(const ITensorInfo *src, const ITensorInfo *max, const ITensorInfo *dst, const float beta, const ITensorInfo *tmp)
Static function to check if given info will lead to a valid configuration.
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(...)
Definition: Validate.h:541
void sve_fp32_softmax(const ITensor *in, const ITensor *max, void *const tmp, ITensor *out, const float beta, bool is_log, const Window &window)
Definition: fp32.cpp:32
#define ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(t, c,...)
Definition: Validate.h:788
void neon_fp32_logits(const ITensor *in, ITensor *out, const Window &window)
Definition: fp32.cpp:37
void neon_fp16_softmax(const ITensor *in, const ITensor *max, void *const tmp, ITensor *out, const float beta, bool is_log, const Window &window)
Tensor packing service.
Definition: ITensorPack.h:39
#define ARM_COMPUTE_ERROR_ON_NULLPTR(...)
Definition: Validate.h:157
Store the tensor&#39;s metadata.
Definition: TensorInfo.h:43
quantized, asymmetric fixed-point 8-bit number signed
void neon_fp16_logits(const ITensor *in, ITensor *out, const Window &window)
static CPUInfo & get()
Access the KernelLibrary singleton.
Definition: CPPTypes.cpp:40
#define REGISTER_QASYMM8_SVE2(func_name)
Definition: Registrars.h:119
std::string kernel_name
DataType
Available data types.
Definition: Types.h:79
void sve2_qasymm8_signed_softmax(const ITensor *in, const ITensor *max, void *const tmp, ITensor *out, const float beta, bool is_log, const Window &window)
Describe a multidimensional execution window.
Definition: Window.h:39
#define ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(f, s)
Definition: Validate.h:201
cpuinfo::CpuIsaInfo get_isa() const
Gets the current cpu&#39;s ISA information.
Definition: CPPTypes.cpp:124
void run_op(ITensorPack &tensors, const Window &window, const ThreadInfo &info) override
Execute the kernel on the passed window.