1*c217d954SCole Faust /*
2*c217d954SCole Faust * Copyright (c) 2016-2021 Arm Limited.
3*c217d954SCole Faust *
4*c217d954SCole Faust * SPDX-License-Identifier: MIT
5*c217d954SCole Faust *
6*c217d954SCole Faust * Permission is hereby granted, free of charge, to any person obtaining a copy
7*c217d954SCole Faust * of this software and associated documentation files (the "Software"), to
8*c217d954SCole Faust * deal in the Software without restriction, including without limitation the
9*c217d954SCole Faust * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10*c217d954SCole Faust * sell copies of the Software, and to permit persons to whom the Software is
11*c217d954SCole Faust * furnished to do so, subject to the following conditions:
12*c217d954SCole Faust *
13*c217d954SCole Faust * The above copyright notice and this permission notice shall be included in all
14*c217d954SCole Faust * copies or substantial portions of the Software.
15*c217d954SCole Faust *
16*c217d954SCole Faust * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17*c217d954SCole Faust * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18*c217d954SCole Faust * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19*c217d954SCole Faust * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20*c217d954SCole Faust * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21*c217d954SCole Faust * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22*c217d954SCole Faust * SOFTWARE.
23*c217d954SCole Faust */
24*c217d954SCole Faust #include "src/gpu/cl/kernels/ClActivationKernel.h"
25*c217d954SCole Faust
26*c217d954SCole Faust #include "arm_compute/core/CL/CLHelpers.h"
27*c217d954SCole Faust #include "arm_compute/core/CL/ICLTensor.h"
28*c217d954SCole Faust #include "arm_compute/core/TensorInfo.h"
29*c217d954SCole Faust #include "arm_compute/core/Utils.h"
30*c217d954SCole Faust #include "src/core/CL/CLValidate.h"
31*c217d954SCole Faust #include "src/core/helpers/AutoConfiguration.h"
32*c217d954SCole Faust #include "src/core/helpers/WindowHelpers.h"
33*c217d954SCole Faust #include "support/Cast.h"
34*c217d954SCole Faust
35*c217d954SCole Faust #include "support/StringSupport.h"
36*c217d954SCole Faust
37*c217d954SCole Faust #include <set>
38*c217d954SCole Faust
39*c217d954SCole Faust namespace arm_compute
40*c217d954SCole Faust {
41*c217d954SCole Faust namespace opencl
42*c217d954SCole Faust {
43*c217d954SCole Faust namespace kernels
44*c217d954SCole Faust {
45*c217d954SCole Faust namespace
46*c217d954SCole Faust {
validate_arguments(const ITensorInfo * src,const ITensorInfo * dst,const ActivationLayerInfo & act_info)47*c217d954SCole Faust Status validate_arguments(const ITensorInfo *src, const ITensorInfo *dst, const ActivationLayerInfo &act_info)
48*c217d954SCole Faust {
49*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(src);
50*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED, DataType::QSYMM16, DataType::F16, DataType::F32);
51*c217d954SCole Faust
52*c217d954SCole Faust static std::set<ActivationLayerInfo::ActivationFunction> quantized_supported_activations =
53*c217d954SCole Faust {
54*c217d954SCole Faust ActivationLayerInfo::ActivationFunction::RELU,
55*c217d954SCole Faust ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU,
56*c217d954SCole Faust ActivationLayerInfo::ActivationFunction::BOUNDED_RELU,
57*c217d954SCole Faust ActivationLayerInfo::ActivationFunction::LOGISTIC,
58*c217d954SCole Faust ActivationLayerInfo::ActivationFunction::TANH,
59*c217d954SCole Faust ActivationLayerInfo::ActivationFunction::HARD_SWISH,
60*c217d954SCole Faust ActivationLayerInfo::ActivationFunction::LEAKY_RELU,
61*c217d954SCole Faust };
62*c217d954SCole Faust const DataType data_type = src->data_type();
63*c217d954SCole Faust const QuantizationInfo &oq_info = (dst != nullptr) ? dst->quantization_info() : src->quantization_info();
64*c217d954SCole Faust const ActivationLayerInfo::ActivationFunction f_act = act_info.activation();
65*c217d954SCole Faust
66*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_MSG(is_data_type_quantized(data_type) && (quantized_supported_activations.count(f_act) == 0),
67*c217d954SCole Faust "For Quantized data type only hard swish, leaky relu, tanh, logistic, relu and lower/upper bounded relu are supported");
68*c217d954SCole Faust
69*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON(data_type == DataType::QASYMM8 && (f_act == ActivationLayerInfo::ActivationFunction::TANH) && (oq_info != QuantizationInfo(1.f / 128.f, 128)));
70*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON(data_type == DataType::QASYMM8 && (f_act == ActivationLayerInfo::ActivationFunction::LOGISTIC) && (oq_info != QuantizationInfo(1.f / 256.f, 0)));
71*c217d954SCole Faust
72*c217d954SCole Faust 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*c217d954SCole Faust 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*c217d954SCole Faust
75*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON(data_type == DataType::QASYMM8_SIGNED && (f_act == ActivationLayerInfo::ActivationFunction::TANH) && (oq_info != QuantizationInfo(1.f / 128.f, 0)));
76*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON(data_type == DataType::QASYMM8_SIGNED && (f_act == ActivationLayerInfo::ActivationFunction::LOGISTIC) && (oq_info != QuantizationInfo(1.f / 256.f, -128)));
77*c217d954SCole Faust
78*c217d954SCole Faust // Checks performed when destination is configured
79*c217d954SCole Faust if((dst != nullptr) && (dst->total_size() != 0))
80*c217d954SCole Faust {
81*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(src, dst);
82*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src, dst);
83*c217d954SCole Faust }
84*c217d954SCole Faust
85*c217d954SCole Faust return Status{};
86*c217d954SCole Faust }
87*c217d954SCole Faust } // namespace
88*c217d954SCole Faust
ClActivationKernel()89*c217d954SCole Faust ClActivationKernel::ClActivationKernel()
90*c217d954SCole Faust {
91*c217d954SCole Faust _type = CLKernelType::ELEMENTWISE;
92*c217d954SCole Faust }
93*c217d954SCole Faust
configure(const ClCompileContext & compile_context,ITensorInfo * src,ITensorInfo * dst,ActivationLayerInfo act_info)94*c217d954SCole Faust void ClActivationKernel::configure(const ClCompileContext &compile_context, ITensorInfo *src, ITensorInfo *dst, ActivationLayerInfo act_info)
95*c217d954SCole Faust {
96*c217d954SCole Faust ARM_COMPUTE_ERROR_ON_NULLPTR(src);
97*c217d954SCole Faust
98*c217d954SCole Faust auto padding_info = get_padding_info({ src, dst });
99*c217d954SCole Faust
100*c217d954SCole Faust _run_in_place = (dst == nullptr) || (dst == src);
101*c217d954SCole Faust
102*c217d954SCole Faust if(dst != nullptr)
103*c217d954SCole Faust {
104*c217d954SCole Faust // Destination auto inizialitation if not yet initialized
105*c217d954SCole Faust auto_init_if_empty(*dst, *src->clone());
106*c217d954SCole Faust }
107*c217d954SCole Faust
108*c217d954SCole Faust ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(src, (dst != nullptr) ? dst : nullptr, act_info));
109*c217d954SCole Faust
110*c217d954SCole Faust const unsigned int num_elems_processed_per_iteration = adjust_vec_size(16 / src->element_size(), src->dimension(0));
111*c217d954SCole Faust
112*c217d954SCole Faust const DataType dt = src->data_type();
113*c217d954SCole Faust float a_const = act_info.a();
114*c217d954SCole Faust float b_const = act_info.b();
115*c217d954SCole Faust
116*c217d954SCole Faust const ActivationLayerInfo::ActivationFunction f_act = act_info.activation();
117*c217d954SCole Faust const bool is_quantized = is_data_type_quantized(dt);
118*c217d954SCole Faust const bool perform_activation_in_float =
119*c217d954SCole Faust (f_act == ActivationLayerInfo::ActivationFunction::LOGISTIC)
120*c217d954SCole Faust || (f_act == ActivationLayerInfo::ActivationFunction::TANH)
121*c217d954SCole Faust || (f_act == ActivationLayerInfo::ActivationFunction::HARD_SWISH)
122*c217d954SCole Faust || (f_act == ActivationLayerInfo::ActivationFunction::LEAKY_RELU);
123*c217d954SCole Faust
124*c217d954SCole Faust // Set build options
125*c217d954SCole Faust CLBuildOptions build_opts;
126*c217d954SCole Faust build_opts.add_option_if(perform_activation_in_float, "-DFLOAT_DOMAIN");
127*c217d954SCole Faust build_opts.add_option_if(_run_in_place, "-DIN_PLACE");
128*c217d954SCole Faust build_opts.add_option("-DACT=" + lower_string(string_from_activation_func(f_act)));
129*c217d954SCole Faust build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(dt));
130*c217d954SCole Faust build_opts.add_option("-DVEC_SIZE=" + support::cpp11::to_string(num_elems_processed_per_iteration));
131*c217d954SCole Faust build_opts.add_option("-DVEC_SIZE_LEFTOVER=" + support::cpp11::to_string(src->dimension(0) % num_elems_processed_per_iteration));
132*c217d954SCole Faust
133*c217d954SCole Faust std::string kernel_name = std::string("activation_layer");
134*c217d954SCole Faust
135*c217d954SCole Faust // Set quantization info build options
136*c217d954SCole Faust if(is_quantized)
137*c217d954SCole Faust {
138*c217d954SCole Faust const UniformQuantizationInfo iq_info = src->quantization_info().uniform();
139*c217d954SCole Faust
140*c217d954SCole Faust if(!perform_activation_in_float)
141*c217d954SCole Faust {
142*c217d954SCole Faust int a_const_int = 0;
143*c217d954SCole Faust int b_const_int = 0;
144*c217d954SCole Faust
145*c217d954SCole Faust // Create quantized version of constants a, b if needed
146*c217d954SCole Faust switch(dt)
147*c217d954SCole Faust {
148*c217d954SCole Faust case DataType::QASYMM8:
149*c217d954SCole Faust {
150*c217d954SCole Faust a_const_int = quantize_qasymm8(a_const, iq_info);
151*c217d954SCole Faust b_const_int = quantize_qasymm8(b_const, iq_info);
152*c217d954SCole Faust }
153*c217d954SCole Faust break;
154*c217d954SCole Faust case DataType::QASYMM8_SIGNED:
155*c217d954SCole Faust {
156*c217d954SCole Faust a_const_int = quantize_qasymm8_signed(a_const, iq_info);
157*c217d954SCole Faust b_const_int = quantize_qasymm8_signed(b_const, iq_info);
158*c217d954SCole Faust }
159*c217d954SCole Faust break;
160*c217d954SCole Faust case DataType::QSYMM16:
161*c217d954SCole Faust {
162*c217d954SCole Faust a_const_int = quantize_qsymm16(a_const, iq_info);
163*c217d954SCole Faust b_const_int = quantize_qsymm16(b_const, iq_info);
164*c217d954SCole Faust }
165*c217d954SCole Faust break;
166*c217d954SCole Faust default:
167*c217d954SCole Faust break;
168*c217d954SCole Faust }
169*c217d954SCole Faust build_opts.add_option(("-DA_VAL=" + support::cpp11::to_string(a_const_int)));
170*c217d954SCole Faust build_opts.add_option(("-DB_VAL=" + support::cpp11::to_string(b_const_int)));
171*c217d954SCole Faust }
172*c217d954SCole Faust else
173*c217d954SCole Faust {
174*c217d954SCole Faust build_opts.add_option(("-DA_VAL=" + float_to_string_with_full_precision(a_const)));
175*c217d954SCole Faust build_opts.add_option(("-DB_VAL=" + float_to_string_with_full_precision(b_const)));
176*c217d954SCole Faust }
177*c217d954SCole Faust
178*c217d954SCole Faust // Quantized value of 0 corresponds to the offset o1
179*c217d954SCole Faust build_opts.add_option(("-DCONST_0=" + (is_data_type_quantized_asymmetric(dt) ? support::cpp11::to_string(iq_info.offset) : "0")));
180*c217d954SCole Faust build_opts.add_option(("-DS1_VAL=" + float_to_string_with_full_precision(iq_info.scale)));
181*c217d954SCole Faust build_opts.add_option_if(is_data_type_quantized_asymmetric(dt), "-DO1_VAL=" + support::cpp11::to_string(iq_info.offset));
182*c217d954SCole Faust
183*c217d954SCole Faust // Set correct kernel name
184*c217d954SCole Faust kernel_name += perform_activation_in_float ? std::string("_quant_f32") : std::string("_quant");
185*c217d954SCole Faust
186*c217d954SCole Faust // Set scale and offset of the source and destination if they have different quantization info
187*c217d954SCole Faust if(dst != nullptr)
188*c217d954SCole Faust {
189*c217d954SCole Faust const UniformQuantizationInfo oq_info = dst->quantization_info().uniform();
190*c217d954SCole Faust
191*c217d954SCole Faust if(iq_info != oq_info)
192*c217d954SCole Faust {
193*c217d954SCole Faust build_opts.add_option(("-DS2_VAL=" + float_to_string_with_full_precision(oq_info.scale)));
194*c217d954SCole Faust build_opts.add_option_if(is_data_type_quantized_asymmetric(dt), "-DO2_VAL=" + support::cpp11::to_string(oq_info.offset));
195*c217d954SCole Faust }
196*c217d954SCole Faust }
197*c217d954SCole Faust }
198*c217d954SCole Faust else
199*c217d954SCole Faust {
200*c217d954SCole Faust // Set A, B constants in build options for float types
201*c217d954SCole Faust build_opts.add_option(("-DA_VAL=" + float_to_string_with_full_precision(a_const)));
202*c217d954SCole Faust build_opts.add_option(("-DB_VAL=" + float_to_string_with_full_precision(b_const)));
203*c217d954SCole Faust }
204*c217d954SCole Faust
205*c217d954SCole Faust // Create kernel
206*c217d954SCole Faust _kernel = create_kernel(compile_context, kernel_name, build_opts.options());
207*c217d954SCole Faust
208*c217d954SCole Faust // Configure kernel window
209*c217d954SCole Faust Window win = calculate_max_window(*src, Steps(num_elems_processed_per_iteration));
210*c217d954SCole Faust ICLKernel::configure_internal(win);
211*c217d954SCole Faust
212*c217d954SCole Faust // Set config_id for enabling LWS tuning
213*c217d954SCole Faust _config_id = "activation_layer_";
214*c217d954SCole Faust _config_id += lower_string(string_from_data_type(dt));
215*c217d954SCole Faust _config_id += "_";
216*c217d954SCole Faust _config_id += support::cpp11::to_string(src->dimension(0));
217*c217d954SCole Faust _config_id += "_";
218*c217d954SCole Faust _config_id += support::cpp11::to_string(src->dimension(1));
219*c217d954SCole Faust
220*c217d954SCole Faust ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info));
221*c217d954SCole Faust }
222*c217d954SCole Faust
validate(const ITensorInfo * src,const ITensorInfo * dst,const ActivationLayerInfo & act_info)223*c217d954SCole Faust Status ClActivationKernel::validate(const ITensorInfo *src, const ITensorInfo *dst, const ActivationLayerInfo &act_info)
224*c217d954SCole Faust {
225*c217d954SCole Faust ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(src, dst, act_info));
226*c217d954SCole Faust return Status{};
227*c217d954SCole Faust }
228*c217d954SCole Faust
run_op(ITensorPack & tensors,const Window & window,::cl::CommandQueue & queue)229*c217d954SCole Faust void ClActivationKernel::run_op(ITensorPack &tensors, const Window &window, ::cl::CommandQueue &queue)
230*c217d954SCole Faust {
231*c217d954SCole Faust ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
232*c217d954SCole Faust ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
233*c217d954SCole Faust
234*c217d954SCole Faust const auto src = utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC));
235*c217d954SCole Faust auto dst = utils::cast::polymorphic_downcast<ICLTensor *>(tensors.get_tensor(TensorType::ACL_DST));
236*c217d954SCole Faust ARM_COMPUTE_ERROR_ON(_run_in_place && src != dst);
237*c217d954SCole Faust
238*c217d954SCole Faust Window collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimZ);
239*c217d954SCole Faust Window slice = collapsed.first_slice_window_3D();
240*c217d954SCole Faust
241*c217d954SCole Faust do
242*c217d954SCole Faust {
243*c217d954SCole Faust unsigned int idx = 0;
244*c217d954SCole Faust add_3D_tensor_argument(idx, src, slice);
245*c217d954SCole Faust if(!_run_in_place)
246*c217d954SCole Faust {
247*c217d954SCole Faust add_3D_tensor_argument(idx, dst, slice);
248*c217d954SCole Faust }
249*c217d954SCole Faust enqueue(queue, *this, slice, lws_hint());
250*c217d954SCole Faust }
251*c217d954SCole Faust while(collapsed.slide_window_slice_3D(slice));
252*c217d954SCole Faust }
253*c217d954SCole Faust } // namespace kernels
254*c217d954SCole Faust } // namespace opencl
255*c217d954SCole Faust } // namespace arm_compute
256