blob: a85296f7cd78cdf28b79ea7de0f2db5af9851365 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Matthew Benthamf1aeab92023-05-30 13:35:34 +00002 * Copyright (c) 2016-2021, 2023 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/gpu/cl/kernels/ClActivationKernel.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010025
26#include "arm_compute/core/CL/CLHelpers.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010027#include "arm_compute/core/CL/ICLTensor.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010028#include "arm_compute/core/TensorInfo.h"
29#include "arm_compute/core/Utils.h"
SiCong Li91295492023-07-21 18:16:13 +010030#include "arm_compute/core/utils/ActivationFunctionUtils.h"
SiCong Li91295492023-07-21 18:16:13 +010031#include "arm_compute/core/utils/helpers/AdjustVecSize.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010032#include "arm_compute/core/utils/StringUtils.h"
SiCong Li91295492023-07-21 18:16:13 +010033#include "arm_compute/function_info/ActivationLayerInfo.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010034
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010035#include "src/core/CL/CLValidate.h"
36#include "src/core/helpers/AutoConfiguration.h"
37#include "src/core/helpers/WindowHelpers.h"
38#include "support/Cast.h"
Matthew Bentham758b5ba2020-03-05 23:37:48 +000039#include "support/StringSupport.h"
Georgios Pinitas00394ae2017-06-22 18:13:55 +010040
Georgios Pinitas4b3fba12019-06-04 17:31:46 +010041#include <set>
Georgios Pinitas00394ae2017-06-22 18:13:55 +010042
Michele Di Giorgiocbbed282019-12-20 13:26:08 +000043namespace arm_compute
44{
Georgios Pinitasf47f7182021-01-15 09:29:50 +000045namespace opencl
46{
47namespace kernels
48{
Giorgio Arena2995f5b2017-11-29 17:33:59 +000049namespace
50{
Georgios Pinitasf47f7182021-01-15 09:29:50 +000051Status validate_arguments(const ITensorInfo *src, const ITensorInfo *dst, const ActivationLayerInfo &act_info)
Giorgio Arena2995f5b2017-11-29 17:33:59 +000052{
Georgios Pinitasf47f7182021-01-15 09:29:50 +000053 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(src);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010054 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED,
55 DataType::QSYMM16, DataType::F16, DataType::F32);
Georgios Pinitas4b3fba12019-06-04 17:31:46 +010056
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010057 static std::set<ActivationLayerInfo::ActivationFunction> quantized_supported_activations = {
58 ActivationLayerInfo::ActivationFunction::RELU, ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU,
59 ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, ActivationLayerInfo::ActivationFunction::LOGISTIC,
60 ActivationLayerInfo::ActivationFunction::TANH, ActivationLayerInfo::ActivationFunction::HARD_SWISH,
Sang-Hoon Parkadd8e812020-11-25 11:46:03 +000061 ActivationLayerInfo::ActivationFunction::LEAKY_RELU,
Georgios Pinitas4b3fba12019-06-04 17:31:46 +010062 };
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010063 const DataType data_type = src->data_type();
64 const QuantizationInfo &oq_info = (dst != nullptr) ? dst->quantization_info() : src->quantization_info();
65 const ActivationLayerInfo::ActivationFunction f_act = act_info.activation();
Georgios Pinitas4b3fba12019-06-04 17:31:46 +010066
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010067 ARM_COMPUTE_RETURN_ERROR_ON_MSG(is_data_type_quantized(data_type) &&
68 (quantized_supported_activations.count(f_act) == 0),
69 "For Quantized data type only hard swish, leaky relu, tanh, logistic, relu and "
70 "lower/upper bounded relu are supported");
Manuel Bottini30dbeef2019-06-26 16:23:03 +010071
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010072 ARM_COMPUTE_RETURN_ERROR_ON(data_type == DataType::QASYMM8 &&
73 (f_act == ActivationLayerInfo::ActivationFunction::TANH) &&
74 (oq_info != QuantizationInfo(1.f / 128.f, 128)));
75 ARM_COMPUTE_RETURN_ERROR_ON(data_type == DataType::QASYMM8 &&
76 (f_act == ActivationLayerInfo::ActivationFunction::LOGISTIC) &&
77 (oq_info != QuantizationInfo(1.f / 256.f, 0)));
Giorgio Arena2995f5b2017-11-29 17:33:59 +000078
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010079 ARM_COMPUTE_RETURN_ERROR_ON(is_data_type_quantized_symmetric(data_type) &&
80 (f_act == ActivationLayerInfo::ActivationFunction::TANH) &&
81 (oq_info != QuantizationInfo(1.f / 32768.f, 0)));
82 ARM_COMPUTE_RETURN_ERROR_ON(is_data_type_quantized_symmetric(data_type) &&
83 (f_act == ActivationLayerInfo::ActivationFunction::LOGISTIC) &&
84 (oq_info != QuantizationInfo(1.f / 32768.f, 0)));
Manuel Bottini30dbeef2019-06-26 16:23:03 +010085
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010086 ARM_COMPUTE_RETURN_ERROR_ON(data_type == DataType::QASYMM8_SIGNED &&
87 (f_act == ActivationLayerInfo::ActivationFunction::TANH) &&
88 (oq_info != QuantizationInfo(1.f / 128.f, 0)));
89 ARM_COMPUTE_RETURN_ERROR_ON(data_type == DataType::QASYMM8_SIGNED &&
90 (f_act == ActivationLayerInfo::ActivationFunction::LOGISTIC) &&
91 (oq_info != QuantizationInfo(1.f / 256.f, -128)));
Michele Di Giorgiocbbed282019-12-20 13:26:08 +000092
Georgios Pinitasf47f7182021-01-15 09:29:50 +000093 // Checks performed when destination is configured
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010094 if ((dst != nullptr) && (dst->total_size() != 0))
Giorgio Arena2995f5b2017-11-29 17:33:59 +000095 {
Georgios Pinitasf47f7182021-01-15 09:29:50 +000096 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(src, dst);
97 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src, dst);
Giorgio Arena2995f5b2017-11-29 17:33:59 +000098 }
99
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000100 return Status{};
Giorgio Arena2995f5b2017-11-29 17:33:59 +0000101}
Giorgio Arena2995f5b2017-11-29 17:33:59 +0000102} // namespace
103
Georgios Pinitasf47f7182021-01-15 09:29:50 +0000104ClActivationKernel::ClActivationKernel()
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +0100105{
Giorgio Arena4a95bba2021-06-28 11:00:27 +0100106 _type = CLKernelType::ELEMENTWISE;
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +0100107}
108
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100109void ClActivationKernel::configure(const ClCompileContext &compile_context,
110 ITensorInfo *src,
111 ITensorInfo *dst,
112 ActivationLayerInfo act_info)
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100113{
Georgios Pinitasf47f7182021-01-15 09:29:50 +0000114 ARM_COMPUTE_ERROR_ON_NULLPTR(src);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100115
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100116 auto padding_info = get_padding_info({src, dst});
SiCong Li04a07062020-11-17 14:09:01 +0000117
Georgios Pinitasf47f7182021-01-15 09:29:50 +0000118 _run_in_place = (dst == nullptr) || (dst == src);
Michele Di Giorgiodde9ec92018-02-13 15:24:04 +0000119
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100120 if (dst != nullptr)
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +0100121 {
Georgios Pinitasf47f7182021-01-15 09:29:50 +0000122 // Destination auto inizialitation if not yet initialized
123 auto_init_if_empty(*dst, *src->clone());
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +0100124 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100125
Georgios Pinitasf47f7182021-01-15 09:29:50 +0000126 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(src, (dst != nullptr) ? dst : nullptr, act_info));
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000127
Georgios Pinitasf47f7182021-01-15 09:29:50 +0000128 const unsigned int num_elems_processed_per_iteration = adjust_vec_size(16 / src->element_size(), src->dimension(0));
Giorgio Arenad304adb2020-10-02 10:20:11 +0100129
Georgios Pinitasf47f7182021-01-15 09:29:50 +0000130 const DataType dt = src->data_type();
Giorgio Arenad304adb2020-10-02 10:20:11 +0100131 float a_const = act_info.a();
132 float b_const = act_info.b();
Georgios Pinitas05078ec2017-11-02 13:06:59 +0000133
morgolock6b3865a2020-03-04 14:57:46 +0000134 const ActivationLayerInfo::ActivationFunction f_act = act_info.activation();
135 const bool is_quantized = is_data_type_quantized(dt);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100136 const bool perform_activation_in_float = (f_act == ActivationLayerInfo::ActivationFunction::LOGISTIC) ||
137 (f_act == ActivationLayerInfo::ActivationFunction::TANH) ||
138 (f_act == ActivationLayerInfo::ActivationFunction::HARD_SWISH) ||
139 (f_act == ActivationLayerInfo::ActivationFunction::LEAKY_RELU);
Georgios Pinitas4b3fba12019-06-04 17:31:46 +0100140
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100141 // Set build options
Michele Di Giorgioe5bf4c52019-02-14 17:47:33 +0000142 CLBuildOptions build_opts;
Georgios Pinitas4b3fba12019-06-04 17:31:46 +0100143 build_opts.add_option_if(perform_activation_in_float, "-DFLOAT_DOMAIN");
144 build_opts.add_option_if(_run_in_place, "-DIN_PLACE");
Giorgio Arenad304adb2020-10-02 10:20:11 +0100145 build_opts.add_option("-DACT=" + lower_string(string_from_activation_func(f_act)));
146 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(dt));
147 build_opts.add_option("-DVEC_SIZE=" + support::cpp11::to_string(num_elems_processed_per_iteration));
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100148 build_opts.add_option("-DVEC_SIZE_LEFTOVER=" +
149 support::cpp11::to_string(src->dimension(0) % num_elems_processed_per_iteration));
Michel Iwaniec00633802017-10-12 14:14:15 +0100150
Michele Di Giorgiocbbed282019-12-20 13:26:08 +0000151 std::string kernel_name = std::string("activation_layer");
Michel Iwaniec00633802017-10-12 14:14:15 +0100152
Georgios Pinitas4b3fba12019-06-04 17:31:46 +0100153 // Set quantization info build options
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100154 if (is_quantized)
Georgios Pinitas4b3fba12019-06-04 17:31:46 +0100155 {
Georgios Pinitasf47f7182021-01-15 09:29:50 +0000156 const UniformQuantizationInfo iq_info = src->quantization_info().uniform();
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100157
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100158 if (!perform_activation_in_float)
Michele Di Giorgiocbbed282019-12-20 13:26:08 +0000159 {
160 int a_const_int = 0;
161 int b_const_int = 0;
162
163 // Create quantized version of constants a, b if needed
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100164 switch (dt)
Michele Di Giorgiocbbed282019-12-20 13:26:08 +0000165 {
166 case DataType::QASYMM8:
167 {
168 a_const_int = quantize_qasymm8(a_const, iq_info);
169 b_const_int = quantize_qasymm8(b_const, iq_info);
170 }
171 break;
172 case DataType::QASYMM8_SIGNED:
173 {
174 a_const_int = quantize_qasymm8_signed(a_const, iq_info);
175 b_const_int = quantize_qasymm8_signed(b_const, iq_info);
176 }
177 break;
178 case DataType::QSYMM16:
179 {
180 a_const_int = quantize_qsymm16(a_const, iq_info);
181 b_const_int = quantize_qsymm16(b_const, iq_info);
182 }
183 break;
184 default:
185 break;
186 }
187 build_opts.add_option(("-DA_VAL=" + support::cpp11::to_string(a_const_int)));
188 build_opts.add_option(("-DB_VAL=" + support::cpp11::to_string(b_const_int)));
189 }
190 else
191 {
192 build_opts.add_option(("-DA_VAL=" + float_to_string_with_full_precision(a_const)));
193 build_opts.add_option(("-DB_VAL=" + float_to_string_with_full_precision(b_const)));
194 }
195
Michele Di Giorgiodde9ec92018-02-13 15:24:04 +0000196 // Quantized value of 0 corresponds to the offset o1
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100197 build_opts.add_option(
198 ("-DCONST_0=" + (is_data_type_quantized_asymmetric(dt) ? support::cpp11::to_string(iq_info.offset) : "0")));
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100199 build_opts.add_option(("-DS1_VAL=" + float_to_string_with_full_precision(iq_info.scale)));
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100200 build_opts.add_option_if(is_data_type_quantized_asymmetric(dt),
201 "-DO1_VAL=" + support::cpp11::to_string(iq_info.offset));
Michele Di Giorgiodde9ec92018-02-13 15:24:04 +0000202
Michele Di Giorgiocbbed282019-12-20 13:26:08 +0000203 // Set correct kernel name
204 kernel_name += perform_activation_in_float ? std::string("_quant_f32") : std::string("_quant");
205
Georgios Pinitasf47f7182021-01-15 09:29:50 +0000206 // Set scale and offset of the source and destination if they have different quantization info
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100207 if (dst != nullptr)
Georgios Pinitas05078ec2017-11-02 13:06:59 +0000208 {
Georgios Pinitasf47f7182021-01-15 09:29:50 +0000209 const UniformQuantizationInfo oq_info = dst->quantization_info().uniform();
Giorgio Arenaa0d11832018-01-17 16:13:46 +0000210
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100211 if (iq_info != oq_info)
Giorgio Arenaa0d11832018-01-17 16:13:46 +0000212 {
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100213 build_opts.add_option(("-DS2_VAL=" + float_to_string_with_full_precision(oq_info.scale)));
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100214 build_opts.add_option_if(is_data_type_quantized_asymmetric(dt),
215 "-DO2_VAL=" + support::cpp11::to_string(oq_info.offset));
Giorgio Arenaa0d11832018-01-17 16:13:46 +0000216 }
Georgios Pinitas05078ec2017-11-02 13:06:59 +0000217 }
Michel Iwaniec00633802017-10-12 14:14:15 +0100218 }
Michele Di Giorgiocbbed282019-12-20 13:26:08 +0000219 else
Michele Di Giorgioe5bf4c52019-02-14 17:47:33 +0000220 {
Michele Di Giorgiocbbed282019-12-20 13:26:08 +0000221 // Set A, B constants in build options for float types
222 build_opts.add_option(("-DA_VAL=" + float_to_string_with_full_precision(a_const)));
223 build_opts.add_option(("-DB_VAL=" + float_to_string_with_full_precision(b_const)));
Michele Di Giorgioe5bf4c52019-02-14 17:47:33 +0000224 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100225
Michele Di Giorgiocbbed282019-12-20 13:26:08 +0000226 // Create kernel
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100227 _kernel = create_kernel(compile_context, kernel_name, build_opts.options());
228
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +0100229 // Configure kernel window
Georgios Pinitasf47f7182021-01-15 09:29:50 +0000230 Window win = calculate_max_window(*src, Steps(num_elems_processed_per_iteration));
Giorgio Arenad304adb2020-10-02 10:20:11 +0100231 ICLKernel::configure_internal(win);
Giorgio Arena63485ce2017-11-15 16:04:20 +0000232
233 // Set config_id for enabling LWS tuning
234 _config_id = "activation_layer_";
235 _config_id += lower_string(string_from_data_type(dt));
236 _config_id += "_";
Georgios Pinitasf47f7182021-01-15 09:29:50 +0000237 _config_id += support::cpp11::to_string(src->dimension(0));
Giorgio Arena63485ce2017-11-15 16:04:20 +0000238 _config_id += "_";
Georgios Pinitasf47f7182021-01-15 09:29:50 +0000239 _config_id += support::cpp11::to_string(src->dimension(1));
SiCong Li04a07062020-11-17 14:09:01 +0000240
241 ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info));
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +0100242}
243
Georgios Pinitasf47f7182021-01-15 09:29:50 +0000244Status ClActivationKernel::validate(const ITensorInfo *src, const ITensorInfo *dst, const ActivationLayerInfo &act_info)
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000245{
Georgios Pinitasf47f7182021-01-15 09:29:50 +0000246 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(src, dst, act_info));
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000247 return Status{};
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000248}
249
Georgios Pinitasf47f7182021-01-15 09:29:50 +0000250void ClActivationKernel::run_op(ITensorPack &tensors, const Window &window, ::cl::CommandQueue &queue)
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +0100251{
252 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
253 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
254
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100255 const auto src =
256 utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC));
257 auto dst = utils::cast::polymorphic_downcast<ICLTensor *>(tensors.get_tensor(TensorType::ACL_DST));
Georgios Pinitasab23dd02020-07-06 14:57:36 +0100258 ARM_COMPUTE_ERROR_ON(_run_in_place && src != dst);
259
Anthony Barbierbe35b0e2017-07-11 18:13:08 +0100260 Window collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimZ);
261 Window slice = collapsed.first_slice_window_3D();
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +0100262
263 do
264 {
265 unsigned int idx = 0;
Georgios Pinitasab23dd02020-07-06 14:57:36 +0100266 add_3D_tensor_argument(idx, src, slice);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100267 if (!_run_in_place)
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +0100268 {
Georgios Pinitasab23dd02020-07-06 14:57:36 +0100269 add_3D_tensor_argument(idx, dst, slice);
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +0100270 }
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100271 enqueue(queue, *this, slice, lws_hint());
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100272 } while (collapsed.slide_window_slice_3D(slice));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100273}
Georgios Pinitasf47f7182021-01-15 09:29:50 +0000274} // namespace kernels
275} // namespace opencl
Michele Di Giorgiocbbed282019-12-20 13:26:08 +0000276} // namespace arm_compute