blob: ab1543729f9f1d810a576d73d9ec94ef5a5c079e [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"
Matthew Bentham314d3e22023-06-23 10:53:52 +000031#include "arm_compute/core/utils/StringUtils.h"
SiCong Li91295492023-07-21 18:16:13 +010032#include "arm_compute/core/utils/helpers/AdjustVecSize.h"
33#include "arm_compute/function_info/ActivationLayerInfo.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010034#include "src/core/CL/CLValidate.h"
35#include "src/core/helpers/AutoConfiguration.h"
36#include "src/core/helpers/WindowHelpers.h"
37#include "support/Cast.h"
38
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);
54 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED, DataType::QSYMM16, DataType::F16, DataType::F32);
Georgios Pinitas4b3fba12019-06-04 17:31:46 +010055
Manuel Bottini30dbeef2019-06-26 16:23:03 +010056 static std::set<ActivationLayerInfo::ActivationFunction> quantized_supported_activations =
Georgios Pinitas4b3fba12019-06-04 17:31:46 +010057 {
58 ActivationLayerInfo::ActivationFunction::RELU,
59 ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU,
60 ActivationLayerInfo::ActivationFunction::BOUNDED_RELU,
61 ActivationLayerInfo::ActivationFunction::LOGISTIC,
morgolock6b3865a2020-03-04 14:57:46 +000062 ActivationLayerInfo::ActivationFunction::TANH,
Sang-Hoon Parkadd8e812020-11-25 11:46:03 +000063 ActivationLayerInfo::ActivationFunction::HARD_SWISH,
64 ActivationLayerInfo::ActivationFunction::LEAKY_RELU,
Georgios Pinitas4b3fba12019-06-04 17:31:46 +010065 };
Georgios Pinitasf47f7182021-01-15 09:29:50 +000066 const DataType data_type = src->data_type();
67 const QuantizationInfo &oq_info = (dst != nullptr) ? dst->quantization_info() : src->quantization_info();
Georgios Pinitas4b3fba12019-06-04 17:31:46 +010068 const ActivationLayerInfo::ActivationFunction f_act = act_info.activation();
69
Manuel Bottini30dbeef2019-06-26 16:23:03 +010070 ARM_COMPUTE_RETURN_ERROR_ON_MSG(is_data_type_quantized(data_type) && (quantized_supported_activations.count(f_act) == 0),
Sang-Hoon Parkadd8e812020-11-25 11:46:03 +000071 "For Quantized data type only hard swish, leaky relu, tanh, logistic, relu and lower/upper bounded relu are supported");
Manuel Bottini30dbeef2019-06-26 16:23:03 +010072
Michele Di Giorgiocbbed282019-12-20 13:26:08 +000073 ARM_COMPUTE_RETURN_ERROR_ON(data_type == DataType::QASYMM8 && (f_act == ActivationLayerInfo::ActivationFunction::TANH) && (oq_info != QuantizationInfo(1.f / 128.f, 128)));
74 ARM_COMPUTE_RETURN_ERROR_ON(data_type == DataType::QASYMM8 && (f_act == ActivationLayerInfo::ActivationFunction::LOGISTIC) && (oq_info != QuantizationInfo(1.f / 256.f, 0)));
Giorgio Arena2995f5b2017-11-29 17:33:59 +000075
Manuel Bottini30dbeef2019-06-26 16:23:03 +010076 ARM_COMPUTE_RETURN_ERROR_ON(is_data_type_quantized_symmetric(data_type) && (f_act == ActivationLayerInfo::ActivationFunction::TANH) && (oq_info != QuantizationInfo(1.f / 32768.f, 0)));
77 ARM_COMPUTE_RETURN_ERROR_ON(is_data_type_quantized_symmetric(data_type) && (f_act == ActivationLayerInfo::ActivationFunction::LOGISTIC) && (oq_info != QuantizationInfo(1.f / 32768.f, 0)));
78
Michele Di Giorgiocbbed282019-12-20 13:26:08 +000079 ARM_COMPUTE_RETURN_ERROR_ON(data_type == DataType::QASYMM8_SIGNED && (f_act == ActivationLayerInfo::ActivationFunction::TANH) && (oq_info != QuantizationInfo(1.f / 128.f, 0)));
80 ARM_COMPUTE_RETURN_ERROR_ON(data_type == DataType::QASYMM8_SIGNED && (f_act == ActivationLayerInfo::ActivationFunction::LOGISTIC) && (oq_info != QuantizationInfo(1.f / 256.f, -128)));
81
Georgios Pinitasf47f7182021-01-15 09:29:50 +000082 // Checks performed when destination is configured
83 if((dst != nullptr) && (dst->total_size() != 0))
Giorgio Arena2995f5b2017-11-29 17:33:59 +000084 {
Georgios Pinitasf47f7182021-01-15 09:29:50 +000085 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(src, dst);
86 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src, dst);
Giorgio Arena2995f5b2017-11-29 17:33:59 +000087 }
88
Georgios Pinitas631c41a2017-12-06 11:53:03 +000089 return Status{};
Giorgio Arena2995f5b2017-11-29 17:33:59 +000090}
Giorgio Arena2995f5b2017-11-29 17:33:59 +000091} // namespace
92
Georgios Pinitasf47f7182021-01-15 09:29:50 +000093ClActivationKernel::ClActivationKernel()
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +010094{
Giorgio Arena4a95bba2021-06-28 11:00:27 +010095 _type = CLKernelType::ELEMENTWISE;
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +010096}
97
Georgios Pinitasf47f7182021-01-15 09:29:50 +000098void ClActivationKernel::configure(const ClCompileContext &compile_context, ITensorInfo *src, ITensorInfo *dst, ActivationLayerInfo act_info)
Manuel Bottini4c6bd512020-04-08 10:15:51 +010099{
Georgios Pinitasf47f7182021-01-15 09:29:50 +0000100 ARM_COMPUTE_ERROR_ON_NULLPTR(src);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100101
Georgios Pinitasf47f7182021-01-15 09:29:50 +0000102 auto padding_info = get_padding_info({ src, dst });
SiCong Li04a07062020-11-17 14:09:01 +0000103
Georgios Pinitasf47f7182021-01-15 09:29:50 +0000104 _run_in_place = (dst == nullptr) || (dst == src);
Michele Di Giorgiodde9ec92018-02-13 15:24:04 +0000105
Georgios Pinitasf47f7182021-01-15 09:29:50 +0000106 if(dst != nullptr)
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +0100107 {
Georgios Pinitasf47f7182021-01-15 09:29:50 +0000108 // Destination auto inizialitation if not yet initialized
109 auto_init_if_empty(*dst, *src->clone());
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +0100110 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100111
Georgios Pinitasf47f7182021-01-15 09:29:50 +0000112 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(src, (dst != nullptr) ? dst : nullptr, act_info));
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000113
Georgios Pinitasf47f7182021-01-15 09:29:50 +0000114 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 +0100115
Georgios Pinitasf47f7182021-01-15 09:29:50 +0000116 const DataType dt = src->data_type();
Giorgio Arenad304adb2020-10-02 10:20:11 +0100117 float a_const = act_info.a();
118 float b_const = act_info.b();
Georgios Pinitas05078ec2017-11-02 13:06:59 +0000119
morgolock6b3865a2020-03-04 14:57:46 +0000120 const ActivationLayerInfo::ActivationFunction f_act = act_info.activation();
121 const bool is_quantized = is_data_type_quantized(dt);
122 const bool perform_activation_in_float =
Sang-Hoon Parkadd8e812020-11-25 11:46:03 +0000123 (f_act == ActivationLayerInfo::ActivationFunction::LOGISTIC)
124 || (f_act == ActivationLayerInfo::ActivationFunction::TANH)
125 || (f_act == ActivationLayerInfo::ActivationFunction::HARD_SWISH)
126 || (f_act == ActivationLayerInfo::ActivationFunction::LEAKY_RELU);
Georgios Pinitas4b3fba12019-06-04 17:31:46 +0100127
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100128 // Set build options
Michele Di Giorgioe5bf4c52019-02-14 17:47:33 +0000129 CLBuildOptions build_opts;
Georgios Pinitas4b3fba12019-06-04 17:31:46 +0100130 build_opts.add_option_if(perform_activation_in_float, "-DFLOAT_DOMAIN");
131 build_opts.add_option_if(_run_in_place, "-DIN_PLACE");
Giorgio Arenad304adb2020-10-02 10:20:11 +0100132 build_opts.add_option("-DACT=" + lower_string(string_from_activation_func(f_act)));
133 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(dt));
134 build_opts.add_option("-DVEC_SIZE=" + support::cpp11::to_string(num_elems_processed_per_iteration));
Georgios Pinitasf47f7182021-01-15 09:29:50 +0000135 build_opts.add_option("-DVEC_SIZE_LEFTOVER=" + support::cpp11::to_string(src->dimension(0) % num_elems_processed_per_iteration));
Michel Iwaniec00633802017-10-12 14:14:15 +0100136
Michele Di Giorgiocbbed282019-12-20 13:26:08 +0000137 std::string kernel_name = std::string("activation_layer");
Michel Iwaniec00633802017-10-12 14:14:15 +0100138
Georgios Pinitas4b3fba12019-06-04 17:31:46 +0100139 // Set quantization info build options
Manuel Bottini30dbeef2019-06-26 16:23:03 +0100140 if(is_quantized)
Georgios Pinitas4b3fba12019-06-04 17:31:46 +0100141 {
Georgios Pinitasf47f7182021-01-15 09:29:50 +0000142 const UniformQuantizationInfo iq_info = src->quantization_info().uniform();
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100143
Michele Di Giorgiocbbed282019-12-20 13:26:08 +0000144 if(!perform_activation_in_float)
145 {
146 int a_const_int = 0;
147 int b_const_int = 0;
148
149 // Create quantized version of constants a, b if needed
150 switch(dt)
151 {
152 case DataType::QASYMM8:
153 {
154 a_const_int = quantize_qasymm8(a_const, iq_info);
155 b_const_int = quantize_qasymm8(b_const, iq_info);
156 }
157 break;
158 case DataType::QASYMM8_SIGNED:
159 {
160 a_const_int = quantize_qasymm8_signed(a_const, iq_info);
161 b_const_int = quantize_qasymm8_signed(b_const, iq_info);
162 }
163 break;
164 case DataType::QSYMM16:
165 {
166 a_const_int = quantize_qsymm16(a_const, iq_info);
167 b_const_int = quantize_qsymm16(b_const, iq_info);
168 }
169 break;
170 default:
171 break;
172 }
173 build_opts.add_option(("-DA_VAL=" + support::cpp11::to_string(a_const_int)));
174 build_opts.add_option(("-DB_VAL=" + support::cpp11::to_string(b_const_int)));
175 }
176 else
177 {
178 build_opts.add_option(("-DA_VAL=" + float_to_string_with_full_precision(a_const)));
179 build_opts.add_option(("-DB_VAL=" + float_to_string_with_full_precision(b_const)));
180 }
181
Michele Di Giorgiodde9ec92018-02-13 15:24:04 +0000182 // Quantized value of 0 corresponds to the offset o1
Manuel Bottini30dbeef2019-06-26 16:23:03 +0100183 build_opts.add_option(("-DCONST_0=" + (is_data_type_quantized_asymmetric(dt) ? support::cpp11::to_string(iq_info.offset) : "0")));
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100184 build_opts.add_option(("-DS1_VAL=" + float_to_string_with_full_precision(iq_info.scale)));
Manuel Bottini30dbeef2019-06-26 16:23:03 +0100185 build_opts.add_option_if(is_data_type_quantized_asymmetric(dt), "-DO1_VAL=" + support::cpp11::to_string(iq_info.offset));
Michele Di Giorgiodde9ec92018-02-13 15:24:04 +0000186
Michele Di Giorgiocbbed282019-12-20 13:26:08 +0000187 // Set correct kernel name
188 kernel_name += perform_activation_in_float ? std::string("_quant_f32") : std::string("_quant");
189
Georgios Pinitasf47f7182021-01-15 09:29:50 +0000190 // Set scale and offset of the source and destination if they have different quantization info
191 if(dst != nullptr)
Georgios Pinitas05078ec2017-11-02 13:06:59 +0000192 {
Georgios Pinitasf47f7182021-01-15 09:29:50 +0000193 const UniformQuantizationInfo oq_info = dst->quantization_info().uniform();
Giorgio Arenaa0d11832018-01-17 16:13:46 +0000194
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100195 if(iq_info != oq_info)
Giorgio Arenaa0d11832018-01-17 16:13:46 +0000196 {
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100197 build_opts.add_option(("-DS2_VAL=" + float_to_string_with_full_precision(oq_info.scale)));
Manuel Bottini30dbeef2019-06-26 16:23:03 +0100198 build_opts.add_option_if(is_data_type_quantized_asymmetric(dt), "-DO2_VAL=" + support::cpp11::to_string(oq_info.offset));
Giorgio Arenaa0d11832018-01-17 16:13:46 +0000199 }
Georgios Pinitas05078ec2017-11-02 13:06:59 +0000200 }
Michel Iwaniec00633802017-10-12 14:14:15 +0100201 }
Michele Di Giorgiocbbed282019-12-20 13:26:08 +0000202 else
Michele Di Giorgioe5bf4c52019-02-14 17:47:33 +0000203 {
Michele Di Giorgiocbbed282019-12-20 13:26:08 +0000204 // Set A, B constants in build options for float types
205 build_opts.add_option(("-DA_VAL=" + float_to_string_with_full_precision(a_const)));
206 build_opts.add_option(("-DB_VAL=" + float_to_string_with_full_precision(b_const)));
Michele Di Giorgioe5bf4c52019-02-14 17:47:33 +0000207 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100208
Michele Di Giorgiocbbed282019-12-20 13:26:08 +0000209 // Create kernel
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100210 _kernel = create_kernel(compile_context, kernel_name, build_opts.options());
211
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +0100212 // Configure kernel window
Georgios Pinitasf47f7182021-01-15 09:29:50 +0000213 Window win = calculate_max_window(*src, Steps(num_elems_processed_per_iteration));
Giorgio Arenad304adb2020-10-02 10:20:11 +0100214 ICLKernel::configure_internal(win);
Giorgio Arena63485ce2017-11-15 16:04:20 +0000215
216 // Set config_id for enabling LWS tuning
217 _config_id = "activation_layer_";
218 _config_id += lower_string(string_from_data_type(dt));
219 _config_id += "_";
Georgios Pinitasf47f7182021-01-15 09:29:50 +0000220 _config_id += support::cpp11::to_string(src->dimension(0));
Giorgio Arena63485ce2017-11-15 16:04:20 +0000221 _config_id += "_";
Georgios Pinitasf47f7182021-01-15 09:29:50 +0000222 _config_id += support::cpp11::to_string(src->dimension(1));
SiCong Li04a07062020-11-17 14:09:01 +0000223
224 ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info));
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +0100225}
226
Georgios Pinitasf47f7182021-01-15 09:29:50 +0000227Status ClActivationKernel::validate(const ITensorInfo *src, const ITensorInfo *dst, const ActivationLayerInfo &act_info)
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000228{
Georgios Pinitasf47f7182021-01-15 09:29:50 +0000229 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(src, dst, act_info));
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000230 return Status{};
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000231}
232
Georgios Pinitasf47f7182021-01-15 09:29:50 +0000233void ClActivationKernel::run_op(ITensorPack &tensors, const Window &window, ::cl::CommandQueue &queue)
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +0100234{
235 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
236 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
237
Georgios Pinitas0499dff2020-07-31 22:21:38 +0100238 const auto src = utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC));
239 auto dst = utils::cast::polymorphic_downcast<ICLTensor *>(tensors.get_tensor(TensorType::ACL_DST));
Georgios Pinitasab23dd02020-07-06 14:57:36 +0100240 ARM_COMPUTE_ERROR_ON(_run_in_place && src != dst);
241
Anthony Barbierbe35b0e2017-07-11 18:13:08 +0100242 Window collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimZ);
243 Window slice = collapsed.first_slice_window_3D();
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +0100244
245 do
246 {
247 unsigned int idx = 0;
Georgios Pinitasab23dd02020-07-06 14:57:36 +0100248 add_3D_tensor_argument(idx, src, slice);
Michele Di Giorgiodde9ec92018-02-13 15:24:04 +0000249 if(!_run_in_place)
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +0100250 {
Georgios Pinitasab23dd02020-07-06 14:57:36 +0100251 add_3D_tensor_argument(idx, dst, slice);
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +0100252 }
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100253 enqueue(queue, *this, slice, lws_hint());
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +0100254 }
Anthony Barbierbe35b0e2017-07-11 18:13:08 +0100255 while(collapsed.slide_window_slice_3D(slice));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100256}
Georgios Pinitasf47f7182021-01-15 09:29:50 +0000257} // namespace kernels
258} // namespace opencl
Michele Di Giorgiocbbed282019-12-20 13:26:08 +0000259} // namespace arm_compute