blob: d40e9a15bef257c1aac167960361fe3bafec1c70 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Michele Di Giorgiocbbed282019-12-20 13:26:08 +00002 * 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 */
24#include "arm_compute/core/CL/kernels/CLActivationLayerKernel.h"
25
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"
Vidhya Sudhan Loganathanf1f49062018-05-25 13:21:26 +010028#include "arm_compute/core/CL/CLValidate.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010029#include "arm_compute/core/CL/ICLTensor.h"
30#include "arm_compute/core/Helpers.h"
31#include "arm_compute/core/IAccessWindow.h"
32#include "arm_compute/core/TensorInfo.h"
Georgios Pinitas4b3fba12019-06-04 17:31:46 +010033#include "arm_compute/core/Types.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010034#include "arm_compute/core/Utils.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010035#include "arm_compute/core/Window.h"
Georgios Pinitas4b3fba12019-06-04 17:31:46 +010036#include "arm_compute/core/utils/helpers/float_ops.h"
Matthew Bentham758b5ba2020-03-05 23:37:48 +000037#include "support/StringSupport.h"
Georgios Pinitas00394ae2017-06-22 18:13:55 +010038
39#include <cmath>
Georgios Pinitas4b3fba12019-06-04 17:31:46 +010040#include <set>
Georgios Pinitas00394ae2017-06-22 18:13:55 +010041
Michele Di Giorgiocbbed282019-12-20 13:26:08 +000042namespace arm_compute
43{
Giorgio Arena2995f5b2017-11-29 17:33:59 +000044namespace
45{
Georgios Pinitas631c41a2017-12-06 11:53:03 +000046Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, const ActivationLayerInfo &act_info)
Giorgio Arena2995f5b2017-11-29 17:33:59 +000047{
Vidhya Sudhan Loganathanf1f49062018-05-25 13:21:26 +010048 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input);
Michele Di Giorgiocbbed282019-12-20 13:26:08 +000049 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8, DataType::QASYMM8, DataType::QASYMM8_SIGNED, DataType::QSYMM16, DataType::F16, DataType::F32);
Georgios Pinitas4b3fba12019-06-04 17:31:46 +010050
Manuel Bottini30dbeef2019-06-26 16:23:03 +010051 static std::set<ActivationLayerInfo::ActivationFunction> quantized_supported_activations =
Georgios Pinitas4b3fba12019-06-04 17:31:46 +010052 {
53 ActivationLayerInfo::ActivationFunction::RELU,
54 ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU,
55 ActivationLayerInfo::ActivationFunction::BOUNDED_RELU,
56 ActivationLayerInfo::ActivationFunction::LOGISTIC,
morgolock6b3865a2020-03-04 14:57:46 +000057 ActivationLayerInfo::ActivationFunction::TANH,
58 ActivationLayerInfo::ActivationFunction::HARD_SWISH
Georgios Pinitas4b3fba12019-06-04 17:31:46 +010059 };
60 const DataType data_type = input->data_type();
61 const QuantizationInfo &oq_info = (output != nullptr) ? output->quantization_info() : input->quantization_info();
62 const ActivationLayerInfo::ActivationFunction f_act = act_info.activation();
63
Manuel Bottini30dbeef2019-06-26 16:23:03 +010064 ARM_COMPUTE_RETURN_ERROR_ON_MSG(is_data_type_quantized(data_type) && (quantized_supported_activations.count(f_act) == 0),
65 "For Quantized data type only tanh, logistic, relu and lower/upper bounded relu are supported");
66
Michele Di Giorgiocbbed282019-12-20 13:26:08 +000067 ARM_COMPUTE_RETURN_ERROR_ON(data_type == DataType::QASYMM8 && (f_act == ActivationLayerInfo::ActivationFunction::TANH) && (oq_info != QuantizationInfo(1.f / 128.f, 128)));
68 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 +000069
Manuel Bottini30dbeef2019-06-26 16:23:03 +010070 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)));
71 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)));
72
Michele Di Giorgiocbbed282019-12-20 13:26:08 +000073 ARM_COMPUTE_RETURN_ERROR_ON(data_type == DataType::QASYMM8_SIGNED && (f_act == ActivationLayerInfo::ActivationFunction::TANH) && (oq_info != QuantizationInfo(1.f / 128.f, 0)));
74 ARM_COMPUTE_RETURN_ERROR_ON(data_type == DataType::QASYMM8_SIGNED && (f_act == ActivationLayerInfo::ActivationFunction::LOGISTIC) && (oq_info != QuantizationInfo(1.f / 256.f, -128)));
75
Giorgio Arena2995f5b2017-11-29 17:33:59 +000076 // Checks performed when output is configured
77 if((output != nullptr) && (output->total_size() != 0))
78 {
79 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, output);
80 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
Giorgio Arena2995f5b2017-11-29 17:33:59 +000081 }
82
Georgios Pinitas631c41a2017-12-06 11:53:03 +000083 return Status{};
Giorgio Arena2995f5b2017-11-29 17:33:59 +000084}
85
Georgios Pinitas631c41a2017-12-06 11:53:03 +000086std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output)
Giorgio Arena2995f5b2017-11-29 17:33:59 +000087{
88 if(output != nullptr)
89 {
90 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
91 // Output auto inizialitation if not yet initialized
Pablo Palmiera2b89ca2017-10-05 15:01:34 +010092 auto_init_if_empty(*output, *input);
Giorgio Arena2995f5b2017-11-29 17:33:59 +000093 }
94
95 const unsigned int num_elems_processed_per_iteration = 16 / input->element_size();
96
97 Window win = calculate_max_window(*input, Steps(num_elems_processed_per_iteration));
98 bool window_changed = false;
99
100 if(output != nullptr)
101 {
102 AccessWindowHorizontal input_access(input, 0, num_elems_processed_per_iteration);
103 AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration);
104 window_changed = update_window_and_padding(win, input_access, output_access);
105 output_access.set_valid_region(win, input->valid_region());
106 }
107 else
108 {
109 window_changed = update_window_and_padding(win,
110 AccessWindowHorizontal(input, 0, num_elems_processed_per_iteration));
111 }
112
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000113 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
Giorgio Arena2995f5b2017-11-29 17:33:59 +0000114 return std::make_pair(err, win);
115}
116} // namespace
117
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100118CLActivationLayerKernel::CLActivationLayerKernel()
119 : _input(nullptr), _output(nullptr), _run_in_place(false)
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +0100120{
121}
122
123void CLActivationLayerKernel::configure(ICLTensor *input, ICLTensor *output, ActivationLayerInfo act_info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100124{
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100125 configure(CLKernelLibrary::get().get_compile_context(), input, output, act_info);
126}
127
Manuel Bottini256c0b92020-04-21 13:29:30 +0100128void CLActivationLayerKernel::configure(const CLCompileContext &compile_context, ICLTensor *input, ICLTensor *output, ActivationLayerInfo act_info)
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100129{
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000130 ARM_COMPUTE_ERROR_ON_NULLPTR(input);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100131
Michele Di Giorgiodde9ec92018-02-13 15:24:04 +0000132 _run_in_place = (output == nullptr) || (output == input);
133
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +0100134 if(output != nullptr)
135 {
136 // Output auto inizialitation if not yet initialized
Georgios Pinitas05078ec2017-11-02 13:06:59 +0000137 auto_init_if_empty(*output->info(),
Giorgio Arena2995f5b2017-11-29 17:33:59 +0000138 *input->info()->clone());
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +0100139 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100140
Giorgio Arenaf6a43c52017-12-01 12:16:25 +0000141 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), (output != nullptr) ? output->info() : nullptr, act_info));
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000142
Georgios Pinitas00394ae2017-06-22 18:13:55 +0100143 const unsigned int num_elems_processed_per_iteration = 16 / input->info()->element_size();
Georgios Pinitas388d3ec2017-11-02 12:17:56 +0000144 const DataType dt = input->info()->data_type();
Georgios Pinitas00394ae2017-06-22 18:13:55 +0100145 float a_const = act_info.a();
146 float b_const = act_info.b();
Georgios Pinitas05078ec2017-11-02 13:06:59 +0000147
morgolock6b3865a2020-03-04 14:57:46 +0000148 const ActivationLayerInfo::ActivationFunction f_act = act_info.activation();
149 const bool is_quantized = is_data_type_quantized(dt);
150 const bool perform_activation_in_float =
151 (f_act == ActivationLayerInfo::ActivationFunction::LOGISTIC) || (f_act == ActivationLayerInfo::ActivationFunction::TANH) || (f_act == ActivationLayerInfo::ActivationFunction::HARD_SWISH);
Georgios Pinitas4b3fba12019-06-04 17:31:46 +0100152
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100153 // Set build options
Michele Di Giorgioe5bf4c52019-02-14 17:47:33 +0000154 CLBuildOptions build_opts;
Georgios Pinitas4b3fba12019-06-04 17:31:46 +0100155 build_opts.add_option_if(perform_activation_in_float, "-DFLOAT_DOMAIN");
156 build_opts.add_option_if(_run_in_place, "-DIN_PLACE");
157 build_opts.add_option(("-DACT=" + lower_string(string_from_activation_func(f_act))));
Michele Di Giorgioe5bf4c52019-02-14 17:47:33 +0000158 build_opts.add_option(("-DDATA_TYPE=" + get_cl_type_from_data_type(dt)));
Michele Di Giorgioe5bf4c52019-02-14 17:47:33 +0000159 build_opts.add_option(("-DVEC_SIZE=" + support::cpp11::to_string(num_elems_processed_per_iteration)));
Michel Iwaniec00633802017-10-12 14:14:15 +0100160
Michele Di Giorgiocbbed282019-12-20 13:26:08 +0000161 std::string kernel_name = std::string("activation_layer");
Michel Iwaniec00633802017-10-12 14:14:15 +0100162
Georgios Pinitas4b3fba12019-06-04 17:31:46 +0100163 // Set quantization info build options
Manuel Bottini30dbeef2019-06-26 16:23:03 +0100164 if(is_quantized)
Georgios Pinitas4b3fba12019-06-04 17:31:46 +0100165 {
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100166 const UniformQuantizationInfo iq_info = input->info()->quantization_info().uniform();
167
Michele Di Giorgiocbbed282019-12-20 13:26:08 +0000168 if(!perform_activation_in_float)
169 {
170 int a_const_int = 0;
171 int b_const_int = 0;
172
173 // Create quantized version of constants a, b if needed
174 switch(dt)
175 {
176 case DataType::QASYMM8:
177 {
178 a_const_int = quantize_qasymm8(a_const, iq_info);
179 b_const_int = quantize_qasymm8(b_const, iq_info);
180 }
181 break;
182 case DataType::QASYMM8_SIGNED:
183 {
184 a_const_int = quantize_qasymm8_signed(a_const, iq_info);
185 b_const_int = quantize_qasymm8_signed(b_const, iq_info);
186 }
187 break;
188 case DataType::QSYMM16:
189 {
190 a_const_int = quantize_qsymm16(a_const, iq_info);
191 b_const_int = quantize_qsymm16(b_const, iq_info);
192 }
193 break;
194 default:
195 break;
196 }
197 build_opts.add_option(("-DA_VAL=" + support::cpp11::to_string(a_const_int)));
198 build_opts.add_option(("-DB_VAL=" + support::cpp11::to_string(b_const_int)));
199 }
200 else
201 {
202 build_opts.add_option(("-DA_VAL=" + float_to_string_with_full_precision(a_const)));
203 build_opts.add_option(("-DB_VAL=" + float_to_string_with_full_precision(b_const)));
204 }
205
Michele Di Giorgiodde9ec92018-02-13 15:24:04 +0000206 // Quantized value of 0 corresponds to the offset o1
Manuel Bottini30dbeef2019-06-26 16:23:03 +0100207 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 +0100208 build_opts.add_option(("-DS1_VAL=" + float_to_string_with_full_precision(iq_info.scale)));
Manuel Bottini30dbeef2019-06-26 16:23:03 +0100209 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 +0000210
Michele Di Giorgiocbbed282019-12-20 13:26:08 +0000211 // Set correct kernel name
212 kernel_name += perform_activation_in_float ? std::string("_quant_f32") : std::string("_quant");
213
Giorgio Arenaa0d11832018-01-17 16:13:46 +0000214 // Set scale and offset of the input and output if they have different quantization info
Georgios Pinitas4b3fba12019-06-04 17:31:46 +0100215 if(output != nullptr)
Georgios Pinitas05078ec2017-11-02 13:06:59 +0000216 {
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100217 const UniformQuantizationInfo oq_info = output->info()->quantization_info().uniform();
Giorgio Arenaa0d11832018-01-17 16:13:46 +0000218
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100219 if(iq_info != oq_info)
Giorgio Arenaa0d11832018-01-17 16:13:46 +0000220 {
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100221 build_opts.add_option(("-DS2_VAL=" + float_to_string_with_full_precision(oq_info.scale)));
Manuel Bottini30dbeef2019-06-26 16:23:03 +0100222 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 +0000223 }
Georgios Pinitas05078ec2017-11-02 13:06:59 +0000224 }
Michel Iwaniec00633802017-10-12 14:14:15 +0100225 }
Michele Di Giorgiocbbed282019-12-20 13:26:08 +0000226 else
Michele Di Giorgioe5bf4c52019-02-14 17:47:33 +0000227 {
Michele Di Giorgiocbbed282019-12-20 13:26:08 +0000228 // Set A, B constants in build options for float types
229 build_opts.add_option(("-DA_VAL=" + float_to_string_with_full_precision(a_const)));
230 build_opts.add_option(("-DB_VAL=" + float_to_string_with_full_precision(b_const)));
Michele Di Giorgioe5bf4c52019-02-14 17:47:33 +0000231 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100232
Michele Di Giorgiocbbed282019-12-20 13:26:08 +0000233 // Create kernel
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100234 _kernel = create_kernel(compile_context, kernel_name, build_opts.options());
235
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100236 // Make sure _kernel is initialized before calling the parent's configure
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +0100237 _input = input;
238 _output = output;
239
240 // Configure kernel window
Michele Di Giorgiodde9ec92018-02-13 15:24:04 +0000241 auto win_config = validate_and_configure_window(input->info(), (_run_in_place) ? nullptr : output->info());
Giorgio Arena2995f5b2017-11-29 17:33:59 +0000242 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100243 ICLKernel::configure_internal(win_config.second);
Giorgio Arena63485ce2017-11-15 16:04:20 +0000244
245 // Set config_id for enabling LWS tuning
246 _config_id = "activation_layer_";
247 _config_id += lower_string(string_from_data_type(dt));
248 _config_id += "_";
249 _config_id += support::cpp11::to_string(input->info()->dimension(0));
250 _config_id += "_";
251 _config_id += support::cpp11::to_string(input->info()->dimension(1));
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +0100252}
253
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000254Status CLActivationLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const ActivationLayerInfo &act_info)
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000255{
Michele Di Giorgiodde9ec92018-02-13 15:24:04 +0000256 const bool run_in_place = (output == nullptr) || (output == input);
Giorgio Arena2995f5b2017-11-29 17:33:59 +0000257 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, act_info));
Michele Di Giorgiodde9ec92018-02-13 15:24:04 +0000258 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), (run_in_place) ? nullptr : output->clone().get()).first);
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000259
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000260 return Status{};
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000261}
262
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +0100263void CLActivationLayerKernel::run(const Window &window, cl::CommandQueue &queue)
264{
265 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
266 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
267
Anthony Barbierbe35b0e2017-07-11 18:13:08 +0100268 Window collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimZ);
269 Window slice = collapsed.first_slice_window_3D();
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +0100270
271 do
272 {
273 unsigned int idx = 0;
274 add_3D_tensor_argument(idx, _input, slice);
Michele Di Giorgiodde9ec92018-02-13 15:24:04 +0000275 if(!_run_in_place)
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +0100276 {
277 add_3D_tensor_argument(idx, _output, slice);
278 }
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100279 enqueue(queue, *this, slice, lws_hint());
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +0100280 }
Anthony Barbierbe35b0e2017-07-11 18:13:08 +0100281 while(collapsed.slide_window_slice_3D(slice));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100282}
Michele Di Giorgiocbbed282019-12-20 13:26:08 +0000283} // namespace arm_compute