blob: 13d55b3f5ac88efeaeaef0fe3ba290d172fe7e8d [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Georgios Pinitasf47f7182021-01-15 09:29:50 +00002 * Copyright (c) 2016-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/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"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010030#include "src/core/CL/CLValidate.h"
31#include "src/core/helpers/AutoConfiguration.h"
32#include "src/core/helpers/WindowHelpers.h"
33#include "support/Cast.h"
34
Matthew Bentham758b5ba2020-03-05 23:37:48 +000035#include "support/StringSupport.h"
Georgios Pinitas00394ae2017-06-22 18:13:55 +010036
Georgios Pinitas4b3fba12019-06-04 17:31:46 +010037#include <set>
Georgios Pinitas00394ae2017-06-22 18:13:55 +010038
Michele Di Giorgiocbbed282019-12-20 13:26:08 +000039namespace arm_compute
40{
Georgios Pinitasf47f7182021-01-15 09:29:50 +000041namespace opencl
42{
43namespace kernels
44{
Giorgio Arena2995f5b2017-11-29 17:33:59 +000045namespace
46{
Georgios Pinitasf47f7182021-01-15 09:29:50 +000047Status validate_arguments(const ITensorInfo *src, const ITensorInfo *dst, const ActivationLayerInfo &act_info)
Giorgio Arena2995f5b2017-11-29 17:33:59 +000048{
Georgios Pinitasf47f7182021-01-15 09:29:50 +000049 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(src);
50 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 +010051
Manuel Bottini30dbeef2019-06-26 16:23:03 +010052 static std::set<ActivationLayerInfo::ActivationFunction> quantized_supported_activations =
Georgios Pinitas4b3fba12019-06-04 17:31:46 +010053 {
54 ActivationLayerInfo::ActivationFunction::RELU,
55 ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU,
56 ActivationLayerInfo::ActivationFunction::BOUNDED_RELU,
57 ActivationLayerInfo::ActivationFunction::LOGISTIC,
morgolock6b3865a2020-03-04 14:57:46 +000058 ActivationLayerInfo::ActivationFunction::TANH,
Sang-Hoon Parkadd8e812020-11-25 11:46:03 +000059 ActivationLayerInfo::ActivationFunction::HARD_SWISH,
60 ActivationLayerInfo::ActivationFunction::LEAKY_RELU,
Georgios Pinitas4b3fba12019-06-04 17:31:46 +010061 };
Georgios Pinitasf47f7182021-01-15 09:29:50 +000062 const DataType data_type = src->data_type();
63 const QuantizationInfo &oq_info = (dst != nullptr) ? dst->quantization_info() : src->quantization_info();
Georgios Pinitas4b3fba12019-06-04 17:31:46 +010064 const ActivationLayerInfo::ActivationFunction f_act = act_info.activation();
65
Manuel Bottini30dbeef2019-06-26 16:23:03 +010066 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 +000067 "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 +010068
Michele Di Giorgiocbbed282019-12-20 13:26:08 +000069 ARM_COMPUTE_RETURN_ERROR_ON(data_type == DataType::QASYMM8 && (f_act == ActivationLayerInfo::ActivationFunction::TANH) && (oq_info != QuantizationInfo(1.f / 128.f, 128)));
70 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 +000071
Manuel Bottini30dbeef2019-06-26 16:23:03 +010072 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)));
73 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)));
74
Michele Di Giorgiocbbed282019-12-20 13:26:08 +000075 ARM_COMPUTE_RETURN_ERROR_ON(data_type == DataType::QASYMM8_SIGNED && (f_act == ActivationLayerInfo::ActivationFunction::TANH) && (oq_info != QuantizationInfo(1.f / 128.f, 0)));
76 ARM_COMPUTE_RETURN_ERROR_ON(data_type == DataType::QASYMM8_SIGNED && (f_act == ActivationLayerInfo::ActivationFunction::LOGISTIC) && (oq_info != QuantizationInfo(1.f / 256.f, -128)));
77
Georgios Pinitasf47f7182021-01-15 09:29:50 +000078 // Checks performed when destination is configured
79 if((dst != nullptr) && (dst->total_size() != 0))
Giorgio Arena2995f5b2017-11-29 17:33:59 +000080 {
Georgios Pinitasf47f7182021-01-15 09:29:50 +000081 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(src, dst);
82 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src, dst);
Giorgio Arena2995f5b2017-11-29 17:33:59 +000083 }
84
Georgios Pinitas631c41a2017-12-06 11:53:03 +000085 return Status{};
Giorgio Arena2995f5b2017-11-29 17:33:59 +000086}
Giorgio Arena2995f5b2017-11-29 17:33:59 +000087} // namespace
88
Georgios Pinitasf47f7182021-01-15 09:29:50 +000089ClActivationKernel::ClActivationKernel()
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +010090{
Giorgio Arena4a95bba2021-06-28 11:00:27 +010091 _type = CLKernelType::ELEMENTWISE;
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +010092}
93
Georgios Pinitasf47f7182021-01-15 09:29:50 +000094void ClActivationKernel::configure(const ClCompileContext &compile_context, ITensorInfo *src, ITensorInfo *dst, ActivationLayerInfo act_info)
Manuel Bottini4c6bd512020-04-08 10:15:51 +010095{
Georgios Pinitasf47f7182021-01-15 09:29:50 +000096 ARM_COMPUTE_ERROR_ON_NULLPTR(src);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010097
Georgios Pinitasf47f7182021-01-15 09:29:50 +000098 auto padding_info = get_padding_info({ src, dst });
SiCong Li04a07062020-11-17 14:09:01 +000099
Georgios Pinitasf47f7182021-01-15 09:29:50 +0000100 _run_in_place = (dst == nullptr) || (dst == src);
Michele Di Giorgiodde9ec92018-02-13 15:24:04 +0000101
Georgios Pinitasf47f7182021-01-15 09:29:50 +0000102 if(dst != nullptr)
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +0100103 {
Georgios Pinitasf47f7182021-01-15 09:29:50 +0000104 // Destination auto inizialitation if not yet initialized
105 auto_init_if_empty(*dst, *src->clone());
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +0100106 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100107
Georgios Pinitasf47f7182021-01-15 09:29:50 +0000108 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(src, (dst != nullptr) ? dst : nullptr, act_info));
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000109
Georgios Pinitasf47f7182021-01-15 09:29:50 +0000110 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 +0100111
Georgios Pinitasf47f7182021-01-15 09:29:50 +0000112 const DataType dt = src->data_type();
Giorgio Arenad304adb2020-10-02 10:20:11 +0100113 float a_const = act_info.a();
114 float b_const = act_info.b();
Georgios Pinitas05078ec2017-11-02 13:06:59 +0000115
morgolock6b3865a2020-03-04 14:57:46 +0000116 const ActivationLayerInfo::ActivationFunction f_act = act_info.activation();
117 const bool is_quantized = is_data_type_quantized(dt);
118 const bool perform_activation_in_float =
Sang-Hoon Parkadd8e812020-11-25 11:46:03 +0000119 (f_act == ActivationLayerInfo::ActivationFunction::LOGISTIC)
120 || (f_act == ActivationLayerInfo::ActivationFunction::TANH)
121 || (f_act == ActivationLayerInfo::ActivationFunction::HARD_SWISH)
122 || (f_act == ActivationLayerInfo::ActivationFunction::LEAKY_RELU);
Georgios Pinitas4b3fba12019-06-04 17:31:46 +0100123
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100124 // Set build options
Michele Di Giorgioe5bf4c52019-02-14 17:47:33 +0000125 CLBuildOptions build_opts;
Georgios Pinitas4b3fba12019-06-04 17:31:46 +0100126 build_opts.add_option_if(perform_activation_in_float, "-DFLOAT_DOMAIN");
127 build_opts.add_option_if(_run_in_place, "-DIN_PLACE");
Giorgio Arenad304adb2020-10-02 10:20:11 +0100128 build_opts.add_option("-DACT=" + lower_string(string_from_activation_func(f_act)));
129 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(dt));
130 build_opts.add_option("-DVEC_SIZE=" + support::cpp11::to_string(num_elems_processed_per_iteration));
Georgios Pinitasf47f7182021-01-15 09:29:50 +0000131 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 +0100132
Michele Di Giorgiocbbed282019-12-20 13:26:08 +0000133 std::string kernel_name = std::string("activation_layer");
Michel Iwaniec00633802017-10-12 14:14:15 +0100134
Georgios Pinitas4b3fba12019-06-04 17:31:46 +0100135 // Set quantization info build options
Manuel Bottini30dbeef2019-06-26 16:23:03 +0100136 if(is_quantized)
Georgios Pinitas4b3fba12019-06-04 17:31:46 +0100137 {
Georgios Pinitasf47f7182021-01-15 09:29:50 +0000138 const UniformQuantizationInfo iq_info = src->quantization_info().uniform();
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100139
Michele Di Giorgiocbbed282019-12-20 13:26:08 +0000140 if(!perform_activation_in_float)
141 {
142 int a_const_int = 0;
143 int b_const_int = 0;
144
145 // Create quantized version of constants a, b if needed
146 switch(dt)
147 {
148 case DataType::QASYMM8:
149 {
150 a_const_int = quantize_qasymm8(a_const, iq_info);
151 b_const_int = quantize_qasymm8(b_const, iq_info);
152 }
153 break;
154 case DataType::QASYMM8_SIGNED:
155 {
156 a_const_int = quantize_qasymm8_signed(a_const, iq_info);
157 b_const_int = quantize_qasymm8_signed(b_const, iq_info);
158 }
159 break;
160 case DataType::QSYMM16:
161 {
162 a_const_int = quantize_qsymm16(a_const, iq_info);
163 b_const_int = quantize_qsymm16(b_const, iq_info);
164 }
165 break;
166 default:
167 break;
168 }
169 build_opts.add_option(("-DA_VAL=" + support::cpp11::to_string(a_const_int)));
170 build_opts.add_option(("-DB_VAL=" + support::cpp11::to_string(b_const_int)));
171 }
172 else
173 {
174 build_opts.add_option(("-DA_VAL=" + float_to_string_with_full_precision(a_const)));
175 build_opts.add_option(("-DB_VAL=" + float_to_string_with_full_precision(b_const)));
176 }
177
Michele Di Giorgiodde9ec92018-02-13 15:24:04 +0000178 // Quantized value of 0 corresponds to the offset o1
Manuel Bottini30dbeef2019-06-26 16:23:03 +0100179 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 +0100180 build_opts.add_option(("-DS1_VAL=" + float_to_string_with_full_precision(iq_info.scale)));
Manuel Bottini30dbeef2019-06-26 16:23:03 +0100181 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 +0000182
Michele Di Giorgiocbbed282019-12-20 13:26:08 +0000183 // Set correct kernel name
184 kernel_name += perform_activation_in_float ? std::string("_quant_f32") : std::string("_quant");
185
Georgios Pinitasf47f7182021-01-15 09:29:50 +0000186 // Set scale and offset of the source and destination if they have different quantization info
187 if(dst != nullptr)
Georgios Pinitas05078ec2017-11-02 13:06:59 +0000188 {
Georgios Pinitasf47f7182021-01-15 09:29:50 +0000189 const UniformQuantizationInfo oq_info = dst->quantization_info().uniform();
Giorgio Arenaa0d11832018-01-17 16:13:46 +0000190
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100191 if(iq_info != oq_info)
Giorgio Arenaa0d11832018-01-17 16:13:46 +0000192 {
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100193 build_opts.add_option(("-DS2_VAL=" + float_to_string_with_full_precision(oq_info.scale)));
Manuel Bottini30dbeef2019-06-26 16:23:03 +0100194 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 +0000195 }
Georgios Pinitas05078ec2017-11-02 13:06:59 +0000196 }
Michel Iwaniec00633802017-10-12 14:14:15 +0100197 }
Michele Di Giorgiocbbed282019-12-20 13:26:08 +0000198 else
Michele Di Giorgioe5bf4c52019-02-14 17:47:33 +0000199 {
Michele Di Giorgiocbbed282019-12-20 13:26:08 +0000200 // Set A, B constants in build options for float types
201 build_opts.add_option(("-DA_VAL=" + float_to_string_with_full_precision(a_const)));
202 build_opts.add_option(("-DB_VAL=" + float_to_string_with_full_precision(b_const)));
Michele Di Giorgioe5bf4c52019-02-14 17:47:33 +0000203 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100204
Michele Di Giorgiocbbed282019-12-20 13:26:08 +0000205 // Create kernel
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100206 _kernel = create_kernel(compile_context, kernel_name, build_opts.options());
207
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +0100208 // Configure kernel window
Georgios Pinitasf47f7182021-01-15 09:29:50 +0000209 Window win = calculate_max_window(*src, Steps(num_elems_processed_per_iteration));
Giorgio Arenad304adb2020-10-02 10:20:11 +0100210 ICLKernel::configure_internal(win);
Giorgio Arena63485ce2017-11-15 16:04:20 +0000211
212 // Set config_id for enabling LWS tuning
213 _config_id = "activation_layer_";
214 _config_id += lower_string(string_from_data_type(dt));
215 _config_id += "_";
Georgios Pinitasf47f7182021-01-15 09:29:50 +0000216 _config_id += support::cpp11::to_string(src->dimension(0));
Giorgio Arena63485ce2017-11-15 16:04:20 +0000217 _config_id += "_";
Georgios Pinitasf47f7182021-01-15 09:29:50 +0000218 _config_id += support::cpp11::to_string(src->dimension(1));
SiCong Li04a07062020-11-17 14:09:01 +0000219
220 ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info));
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +0100221}
222
Georgios Pinitasf47f7182021-01-15 09:29:50 +0000223Status ClActivationKernel::validate(const ITensorInfo *src, const ITensorInfo *dst, const ActivationLayerInfo &act_info)
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000224{
Georgios Pinitasf47f7182021-01-15 09:29:50 +0000225 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(src, dst, act_info));
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000226 return Status{};
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000227}
228
Georgios Pinitasf47f7182021-01-15 09:29:50 +0000229void ClActivationKernel::run_op(ITensorPack &tensors, const Window &window, ::cl::CommandQueue &queue)
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +0100230{
231 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
232 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
233
Georgios Pinitas0499dff2020-07-31 22:21:38 +0100234 const auto src = utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC));
235 auto dst = utils::cast::polymorphic_downcast<ICLTensor *>(tensors.get_tensor(TensorType::ACL_DST));
Georgios Pinitasab23dd02020-07-06 14:57:36 +0100236 ARM_COMPUTE_ERROR_ON(_run_in_place && src != dst);
237
Anthony Barbierbe35b0e2017-07-11 18:13:08 +0100238 Window collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimZ);
239 Window slice = collapsed.first_slice_window_3D();
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +0100240
241 do
242 {
243 unsigned int idx = 0;
Georgios Pinitasab23dd02020-07-06 14:57:36 +0100244 add_3D_tensor_argument(idx, src, slice);
Michele Di Giorgiodde9ec92018-02-13 15:24:04 +0000245 if(!_run_in_place)
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +0100246 {
Georgios Pinitasab23dd02020-07-06 14:57:36 +0100247 add_3D_tensor_argument(idx, dst, slice);
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +0100248 }
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100249 enqueue(queue, *this, slice, lws_hint());
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +0100250 }
Anthony Barbierbe35b0e2017-07-11 18:13:08 +0100251 while(collapsed.slide_window_slice_3D(slice));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100252}
Georgios Pinitasf47f7182021-01-15 09:29:50 +0000253} // namespace kernels
254} // namespace opencl
Michele Di Giorgiocbbed282019-12-20 13:26:08 +0000255} // namespace arm_compute