1 /*
2 * Copyright (c) 2016-2021 Arm Limited.
3 *
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 "src/gpu/cl/kernels/ClActivationKernel.h"
25
26 #include "arm_compute/core/CL/CLHelpers.h"
27 #include "arm_compute/core/CL/ICLTensor.h"
28 #include "arm_compute/core/TensorInfo.h"
29 #include "arm_compute/core/Utils.h"
30 #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
35 #include "support/StringSupport.h"
36
37 #include <set>
38
39 namespace arm_compute
40 {
41 namespace opencl
42 {
43 namespace kernels
44 {
45 namespace
46 {
validate_arguments(const ITensorInfo * src,const ITensorInfo * dst,const ActivationLayerInfo & act_info)47 Status validate_arguments(const ITensorInfo *src, const ITensorInfo *dst, const ActivationLayerInfo &act_info)
48 {
49 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);
51
52 static std::set<ActivationLayerInfo::ActivationFunction> quantized_supported_activations =
53 {
54 ActivationLayerInfo::ActivationFunction::RELU,
55 ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU,
56 ActivationLayerInfo::ActivationFunction::BOUNDED_RELU,
57 ActivationLayerInfo::ActivationFunction::LOGISTIC,
58 ActivationLayerInfo::ActivationFunction::TANH,
59 ActivationLayerInfo::ActivationFunction::HARD_SWISH,
60 ActivationLayerInfo::ActivationFunction::LEAKY_RELU,
61 };
62 const DataType data_type = src->data_type();
63 const QuantizationInfo &oq_info = (dst != nullptr) ? dst->quantization_info() : src->quantization_info();
64 const ActivationLayerInfo::ActivationFunction f_act = act_info.activation();
65
66 ARM_COMPUTE_RETURN_ERROR_ON_MSG(is_data_type_quantized(data_type) && (quantized_supported_activations.count(f_act) == 0),
67 "For Quantized data type only hard swish, leaky relu, tanh, logistic, relu and lower/upper bounded relu are supported");
68
69 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)));
71
72 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
75 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
78 // Checks performed when destination is configured
79 if((dst != nullptr) && (dst->total_size() != 0))
80 {
81 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(src, dst);
82 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src, dst);
83 }
84
85 return Status{};
86 }
87 } // namespace
88
ClActivationKernel()89 ClActivationKernel::ClActivationKernel()
90 {
91 _type = CLKernelType::ELEMENTWISE;
92 }
93
configure(const ClCompileContext & compile_context,ITensorInfo * src,ITensorInfo * dst,ActivationLayerInfo act_info)94 void ClActivationKernel::configure(const ClCompileContext &compile_context, ITensorInfo *src, ITensorInfo *dst, ActivationLayerInfo act_info)
95 {
96 ARM_COMPUTE_ERROR_ON_NULLPTR(src);
97
98 auto padding_info = get_padding_info({ src, dst });
99
100 _run_in_place = (dst == nullptr) || (dst == src);
101
102 if(dst != nullptr)
103 {
104 // Destination auto inizialitation if not yet initialized
105 auto_init_if_empty(*dst, *src->clone());
106 }
107
108 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(src, (dst != nullptr) ? dst : nullptr, act_info));
109
110 const unsigned int num_elems_processed_per_iteration = adjust_vec_size(16 / src->element_size(), src->dimension(0));
111
112 const DataType dt = src->data_type();
113 float a_const = act_info.a();
114 float b_const = act_info.b();
115
116 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 =
119 (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);
123
124 // Set build options
125 CLBuildOptions build_opts;
126 build_opts.add_option_if(perform_activation_in_float, "-DFLOAT_DOMAIN");
127 build_opts.add_option_if(_run_in_place, "-DIN_PLACE");
128 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));
131 build_opts.add_option("-DVEC_SIZE_LEFTOVER=" + support::cpp11::to_string(src->dimension(0) % num_elems_processed_per_iteration));
132
133 std::string kernel_name = std::string("activation_layer");
134
135 // Set quantization info build options
136 if(is_quantized)
137 {
138 const UniformQuantizationInfo iq_info = src->quantization_info().uniform();
139
140 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
178 // Quantized value of 0 corresponds to the offset o1
179 build_opts.add_option(("-DCONST_0=" + (is_data_type_quantized_asymmetric(dt) ? support::cpp11::to_string(iq_info.offset) : "0")));
180 build_opts.add_option(("-DS1_VAL=" + float_to_string_with_full_precision(iq_info.scale)));
181 build_opts.add_option_if(is_data_type_quantized_asymmetric(dt), "-DO1_VAL=" + support::cpp11::to_string(iq_info.offset));
182
183 // Set correct kernel name
184 kernel_name += perform_activation_in_float ? std::string("_quant_f32") : std::string("_quant");
185
186 // Set scale and offset of the source and destination if they have different quantization info
187 if(dst != nullptr)
188 {
189 const UniformQuantizationInfo oq_info = dst->quantization_info().uniform();
190
191 if(iq_info != oq_info)
192 {
193 build_opts.add_option(("-DS2_VAL=" + float_to_string_with_full_precision(oq_info.scale)));
194 build_opts.add_option_if(is_data_type_quantized_asymmetric(dt), "-DO2_VAL=" + support::cpp11::to_string(oq_info.offset));
195 }
196 }
197 }
198 else
199 {
200 // 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)));
203 }
204
205 // Create kernel
206 _kernel = create_kernel(compile_context, kernel_name, build_opts.options());
207
208 // Configure kernel window
209 Window win = calculate_max_window(*src, Steps(num_elems_processed_per_iteration));
210 ICLKernel::configure_internal(win);
211
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 += "_";
216 _config_id += support::cpp11::to_string(src->dimension(0));
217 _config_id += "_";
218 _config_id += support::cpp11::to_string(src->dimension(1));
219
220 ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info));
221 }
222
validate(const ITensorInfo * src,const ITensorInfo * dst,const ActivationLayerInfo & act_info)223 Status ClActivationKernel::validate(const ITensorInfo *src, const ITensorInfo *dst, const ActivationLayerInfo &act_info)
224 {
225 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(src, dst, act_info));
226 return Status{};
227 }
228
run_op(ITensorPack & tensors,const Window & window,::cl::CommandQueue & queue)229 void ClActivationKernel::run_op(ITensorPack &tensors, const Window &window, ::cl::CommandQueue &queue)
230 {
231 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
232 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
233
234 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));
236 ARM_COMPUTE_ERROR_ON(_run_in_place && src != dst);
237
238 Window collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimZ);
239 Window slice = collapsed.first_slice_window_3D();
240
241 do
242 {
243 unsigned int idx = 0;
244 add_3D_tensor_argument(idx, src, slice);
245 if(!_run_in_place)
246 {
247 add_3D_tensor_argument(idx, dst, slice);
248 }
249 enqueue(queue, *this, slice, lws_hint());
250 }
251 while(collapsed.slide_window_slice_3D(slice));
252 }
253 } // namespace kernels
254 } // namespace opencl
255 } // namespace arm_compute
256