blob: 00b6829e91b2231e64212d0c1b6a566aaa072d61 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2016-2020 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 */
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010024#include "src/core/CL/kernels/CLActivationLayerKernel.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010025
Pablo Tellodb8485a2019-09-24 11:03:47 +010026#include "arm_compute/core/CL/CLCoreRuntimeContext.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010027#include "arm_compute/core/CL/CLHelpers.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010028#include "arm_compute/core/CL/ICLTensor.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010029#include "arm_compute/core/TensorInfo.h"
30#include "arm_compute/core/Utils.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010031#include "src/core/CL/CLValidate.h"
32#include "src/core/helpers/AutoConfiguration.h"
33#include "src/core/helpers/WindowHelpers.h"
34#include "support/Cast.h"
35
Matthew Bentham758b5ba2020-03-05 23:37:48 +000036#include "support/StringSupport.h"
Georgios Pinitas00394ae2017-06-22 18:13:55 +010037
Georgios Pinitas4b3fba12019-06-04 17:31:46 +010038#include <set>
Georgios Pinitas00394ae2017-06-22 18:13:55 +010039
Michele Di Giorgiocbbed282019-12-20 13:26:08 +000040namespace arm_compute
41{
Giorgio Arena2995f5b2017-11-29 17:33:59 +000042namespace
43{
Georgios Pinitas631c41a2017-12-06 11:53:03 +000044Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, const ActivationLayerInfo &act_info)
Giorgio Arena2995f5b2017-11-29 17:33:59 +000045{
Vidhya Sudhan Loganathanf1f49062018-05-25 13:21:26 +010046 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input);
Michele Di Giorgiof6f78762020-07-06 11:27:21 +010047 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED, DataType::QSYMM16, DataType::F16, DataType::F32);
Georgios Pinitas4b3fba12019-06-04 17:31:46 +010048
Manuel Bottini30dbeef2019-06-26 16:23:03 +010049 static std::set<ActivationLayerInfo::ActivationFunction> quantized_supported_activations =
Georgios Pinitas4b3fba12019-06-04 17:31:46 +010050 {
51 ActivationLayerInfo::ActivationFunction::RELU,
52 ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU,
53 ActivationLayerInfo::ActivationFunction::BOUNDED_RELU,
54 ActivationLayerInfo::ActivationFunction::LOGISTIC,
morgolock6b3865a2020-03-04 14:57:46 +000055 ActivationLayerInfo::ActivationFunction::TANH,
Sang-Hoon Parkadd8e812020-11-25 11:46:03 +000056 ActivationLayerInfo::ActivationFunction::HARD_SWISH,
57 ActivationLayerInfo::ActivationFunction::LEAKY_RELU,
Georgios Pinitas4b3fba12019-06-04 17:31:46 +010058 };
59 const DataType data_type = input->data_type();
60 const QuantizationInfo &oq_info = (output != nullptr) ? output->quantization_info() : input->quantization_info();
61 const ActivationLayerInfo::ActivationFunction f_act = act_info.activation();
62
Manuel Bottini30dbeef2019-06-26 16:23:03 +010063 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 +000064 "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 +010065
Michele Di Giorgiocbbed282019-12-20 13:26:08 +000066 ARM_COMPUTE_RETURN_ERROR_ON(data_type == DataType::QASYMM8 && (f_act == ActivationLayerInfo::ActivationFunction::TANH) && (oq_info != QuantizationInfo(1.f / 128.f, 128)));
67 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 +000068
Manuel Bottini30dbeef2019-06-26 16:23:03 +010069 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)));
70 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)));
71
Michele Di Giorgiocbbed282019-12-20 13:26:08 +000072 ARM_COMPUTE_RETURN_ERROR_ON(data_type == DataType::QASYMM8_SIGNED && (f_act == ActivationLayerInfo::ActivationFunction::TANH) && (oq_info != QuantizationInfo(1.f / 128.f, 0)));
73 ARM_COMPUTE_RETURN_ERROR_ON(data_type == DataType::QASYMM8_SIGNED && (f_act == ActivationLayerInfo::ActivationFunction::LOGISTIC) && (oq_info != QuantizationInfo(1.f / 256.f, -128)));
74
Giorgio Arena2995f5b2017-11-29 17:33:59 +000075 // Checks performed when output is configured
76 if((output != nullptr) && (output->total_size() != 0))
77 {
78 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, output);
79 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
Giorgio Arena2995f5b2017-11-29 17:33:59 +000080 }
81
Georgios Pinitas631c41a2017-12-06 11:53:03 +000082 return Status{};
Giorgio Arena2995f5b2017-11-29 17:33:59 +000083}
Giorgio Arena2995f5b2017-11-29 17:33:59 +000084} // namespace
85
Manuel Bottini4c6bd512020-04-08 10:15:51 +010086CLActivationLayerKernel::CLActivationLayerKernel()
Georgios Pinitasab23dd02020-07-06 14:57:36 +010087 : _run_in_place(false)
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +010088{
89}
90
Georgios Pinitasab23dd02020-07-06 14:57:36 +010091void CLActivationLayerKernel::configure(const CLCompileContext &compile_context, ITensorInfo *input, ITensorInfo *output, ActivationLayerInfo act_info)
Manuel Bottini4c6bd512020-04-08 10:15:51 +010092{
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +000093 ARM_COMPUTE_ERROR_ON_NULLPTR(input);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010094
SiCong Li04a07062020-11-17 14:09:01 +000095 auto padding_info = get_padding_info({ input, output });
96
Michele Di Giorgiodde9ec92018-02-13 15:24:04 +000097 _run_in_place = (output == nullptr) || (output == input);
98
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +010099 if(output != nullptr)
100 {
101 // Output auto inizialitation if not yet initialized
Georgios Pinitasab23dd02020-07-06 14:57:36 +0100102 auto_init_if_empty(*output, *input->clone());
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +0100103 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100104
Georgios Pinitasab23dd02020-07-06 14:57:36 +0100105 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input, (output != nullptr) ? output : nullptr, act_info));
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000106
Giorgio Arenad304adb2020-10-02 10:20:11 +0100107 const unsigned int num_elems_processed_per_iteration = adjust_vec_size(16 / input->element_size(), input->dimension(0));
108
109 const DataType dt = input->data_type();
110 float a_const = act_info.a();
111 float b_const = act_info.b();
Georgios Pinitas05078ec2017-11-02 13:06:59 +0000112
morgolock6b3865a2020-03-04 14:57:46 +0000113 const ActivationLayerInfo::ActivationFunction f_act = act_info.activation();
114 const bool is_quantized = is_data_type_quantized(dt);
115 const bool perform_activation_in_float =
Sang-Hoon Parkadd8e812020-11-25 11:46:03 +0000116 (f_act == ActivationLayerInfo::ActivationFunction::LOGISTIC)
117 || (f_act == ActivationLayerInfo::ActivationFunction::TANH)
118 || (f_act == ActivationLayerInfo::ActivationFunction::HARD_SWISH)
119 || (f_act == ActivationLayerInfo::ActivationFunction::LEAKY_RELU);
Georgios Pinitas4b3fba12019-06-04 17:31:46 +0100120
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100121 // Set build options
Michele Di Giorgioe5bf4c52019-02-14 17:47:33 +0000122 CLBuildOptions build_opts;
Georgios Pinitas4b3fba12019-06-04 17:31:46 +0100123 build_opts.add_option_if(perform_activation_in_float, "-DFLOAT_DOMAIN");
124 build_opts.add_option_if(_run_in_place, "-DIN_PLACE");
Giorgio Arenad304adb2020-10-02 10:20:11 +0100125 build_opts.add_option("-DACT=" + lower_string(string_from_activation_func(f_act)));
126 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(dt));
127 build_opts.add_option("-DVEC_SIZE=" + support::cpp11::to_string(num_elems_processed_per_iteration));
128 build_opts.add_option("-DVEC_SIZE_LEFTOVER=" + support::cpp11::to_string(input->dimension(0) % num_elems_processed_per_iteration));
Michel Iwaniec00633802017-10-12 14:14:15 +0100129
Michele Di Giorgiocbbed282019-12-20 13:26:08 +0000130 std::string kernel_name = std::string("activation_layer");
Michel Iwaniec00633802017-10-12 14:14:15 +0100131
Georgios Pinitas4b3fba12019-06-04 17:31:46 +0100132 // Set quantization info build options
Manuel Bottini30dbeef2019-06-26 16:23:03 +0100133 if(is_quantized)
Georgios Pinitas4b3fba12019-06-04 17:31:46 +0100134 {
Georgios Pinitasab23dd02020-07-06 14:57:36 +0100135 const UniformQuantizationInfo iq_info = input->quantization_info().uniform();
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100136
Michele Di Giorgiocbbed282019-12-20 13:26:08 +0000137 if(!perform_activation_in_float)
138 {
139 int a_const_int = 0;
140 int b_const_int = 0;
141
142 // Create quantized version of constants a, b if needed
143 switch(dt)
144 {
145 case DataType::QASYMM8:
146 {
147 a_const_int = quantize_qasymm8(a_const, iq_info);
148 b_const_int = quantize_qasymm8(b_const, iq_info);
149 }
150 break;
151 case DataType::QASYMM8_SIGNED:
152 {
153 a_const_int = quantize_qasymm8_signed(a_const, iq_info);
154 b_const_int = quantize_qasymm8_signed(b_const, iq_info);
155 }
156 break;
157 case DataType::QSYMM16:
158 {
159 a_const_int = quantize_qsymm16(a_const, iq_info);
160 b_const_int = quantize_qsymm16(b_const, iq_info);
161 }
162 break;
163 default:
164 break;
165 }
166 build_opts.add_option(("-DA_VAL=" + support::cpp11::to_string(a_const_int)));
167 build_opts.add_option(("-DB_VAL=" + support::cpp11::to_string(b_const_int)));
168 }
169 else
170 {
171 build_opts.add_option(("-DA_VAL=" + float_to_string_with_full_precision(a_const)));
172 build_opts.add_option(("-DB_VAL=" + float_to_string_with_full_precision(b_const)));
173 }
174
Michele Di Giorgiodde9ec92018-02-13 15:24:04 +0000175 // Quantized value of 0 corresponds to the offset o1
Manuel Bottini30dbeef2019-06-26 16:23:03 +0100176 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 +0100177 build_opts.add_option(("-DS1_VAL=" + float_to_string_with_full_precision(iq_info.scale)));
Manuel Bottini30dbeef2019-06-26 16:23:03 +0100178 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 +0000179
Michele Di Giorgiocbbed282019-12-20 13:26:08 +0000180 // Set correct kernel name
181 kernel_name += perform_activation_in_float ? std::string("_quant_f32") : std::string("_quant");
182
Giorgio Arenaa0d11832018-01-17 16:13:46 +0000183 // Set scale and offset of the input and output if they have different quantization info
Georgios Pinitas4b3fba12019-06-04 17:31:46 +0100184 if(output != nullptr)
Georgios Pinitas05078ec2017-11-02 13:06:59 +0000185 {
Georgios Pinitasab23dd02020-07-06 14:57:36 +0100186 const UniformQuantizationInfo oq_info = output->quantization_info().uniform();
Giorgio Arenaa0d11832018-01-17 16:13:46 +0000187
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100188 if(iq_info != oq_info)
Giorgio Arenaa0d11832018-01-17 16:13:46 +0000189 {
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100190 build_opts.add_option(("-DS2_VAL=" + float_to_string_with_full_precision(oq_info.scale)));
Manuel Bottini30dbeef2019-06-26 16:23:03 +0100191 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 +0000192 }
Georgios Pinitas05078ec2017-11-02 13:06:59 +0000193 }
Michel Iwaniec00633802017-10-12 14:14:15 +0100194 }
Michele Di Giorgiocbbed282019-12-20 13:26:08 +0000195 else
Michele Di Giorgioe5bf4c52019-02-14 17:47:33 +0000196 {
Michele Di Giorgiocbbed282019-12-20 13:26:08 +0000197 // Set A, B constants in build options for float types
198 build_opts.add_option(("-DA_VAL=" + float_to_string_with_full_precision(a_const)));
199 build_opts.add_option(("-DB_VAL=" + float_to_string_with_full_precision(b_const)));
Michele Di Giorgioe5bf4c52019-02-14 17:47:33 +0000200 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100201
Michele Di Giorgiocbbed282019-12-20 13:26:08 +0000202 // Create kernel
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100203 _kernel = create_kernel(compile_context, kernel_name, build_opts.options());
204
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +0100205 // Configure kernel window
Giorgio Arenad304adb2020-10-02 10:20:11 +0100206 Window win = calculate_max_window(*input, Steps(num_elems_processed_per_iteration));
207 ICLKernel::configure_internal(win);
Giorgio Arena63485ce2017-11-15 16:04:20 +0000208
209 // Set config_id for enabling LWS tuning
210 _config_id = "activation_layer_";
211 _config_id += lower_string(string_from_data_type(dt));
212 _config_id += "_";
Georgios Pinitasab23dd02020-07-06 14:57:36 +0100213 _config_id += support::cpp11::to_string(input->dimension(0));
Giorgio Arena63485ce2017-11-15 16:04:20 +0000214 _config_id += "_";
Georgios Pinitasab23dd02020-07-06 14:57:36 +0100215 _config_id += support::cpp11::to_string(input->dimension(1));
SiCong Li04a07062020-11-17 14:09:01 +0000216
217 ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info));
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +0100218}
219
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000220Status CLActivationLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const ActivationLayerInfo &act_info)
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000221{
Giorgio Arena2995f5b2017-11-29 17:33:59 +0000222 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, act_info));
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000223 return Status{};
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000224}
225
Georgios Pinitas0499dff2020-07-31 22:21:38 +0100226void CLActivationLayerKernel::run_op(ITensorPack &tensors, const Window &window, cl::CommandQueue &queue)
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +0100227{
228 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
229 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
230
Georgios Pinitas0499dff2020-07-31 22:21:38 +0100231 const auto src = utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC));
232 auto dst = utils::cast::polymorphic_downcast<ICLTensor *>(tensors.get_tensor(TensorType::ACL_DST));
Georgios Pinitasab23dd02020-07-06 14:57:36 +0100233 ARM_COMPUTE_ERROR_ON(_run_in_place && src != dst);
234
Anthony Barbierbe35b0e2017-07-11 18:13:08 +0100235 Window collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimZ);
236 Window slice = collapsed.first_slice_window_3D();
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +0100237
238 do
239 {
240 unsigned int idx = 0;
Georgios Pinitasab23dd02020-07-06 14:57:36 +0100241 add_3D_tensor_argument(idx, src, slice);
Michele Di Giorgiodde9ec92018-02-13 15:24:04 +0000242 if(!_run_in_place)
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +0100243 {
Georgios Pinitasab23dd02020-07-06 14:57:36 +0100244 add_3D_tensor_argument(idx, dst, slice);
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +0100245 }
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100246 enqueue(queue, *this, slice, lws_hint());
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +0100247 }
Anthony Barbierbe35b0e2017-07-11 18:13:08 +0100248 while(collapsed.slide_window_slice_3D(slice));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100249}
Michele Di Giorgiocbbed282019-12-20 13:26:08 +0000250} // namespace arm_compute