1 /*
2 * Copyright (c) 2016-2020 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/core/CL/kernels/CLActivationLayerKernel.h"
25
26 #include "arm_compute/core/CL/CLCoreRuntimeContext.h"
27 #include "arm_compute/core/CL/CLHelpers.h"
28 #include "arm_compute/core/CL/ICLTensor.h"
29 #include "arm_compute/core/TensorInfo.h"
30 #include "arm_compute/core/Utils.h"
31 #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
36 #include "support/StringSupport.h"
37
38 #include <set>
39
40 namespace arm_compute
41 {
42 namespace
43 {
validate_arguments(const ITensorInfo * input,const ITensorInfo * output,const ActivationLayerInfo & act_info)44 Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, const ActivationLayerInfo &act_info)
45 {
46 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input);
47 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED, DataType::QSYMM16, DataType::F16, DataType::F32);
48
49 static std::set<ActivationLayerInfo::ActivationFunction> quantized_supported_activations =
50 {
51 ActivationLayerInfo::ActivationFunction::RELU,
52 ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU,
53 ActivationLayerInfo::ActivationFunction::BOUNDED_RELU,
54 ActivationLayerInfo::ActivationFunction::LOGISTIC,
55 ActivationLayerInfo::ActivationFunction::TANH,
56 ActivationLayerInfo::ActivationFunction::HARD_SWISH
57 };
58 const DataType data_type = input->data_type();
59 const QuantizationInfo &oq_info = (output != nullptr) ? output->quantization_info() : input->quantization_info();
60 const ActivationLayerInfo::ActivationFunction f_act = act_info.activation();
61
62 ARM_COMPUTE_RETURN_ERROR_ON_MSG(is_data_type_quantized(data_type) && (quantized_supported_activations.count(f_act) == 0),
63 "For Quantized data type only tanh, logistic, relu and lower/upper bounded relu are supported");
64
65 ARM_COMPUTE_RETURN_ERROR_ON(data_type == DataType::QASYMM8 && (f_act == ActivationLayerInfo::ActivationFunction::TANH) && (oq_info != QuantizationInfo(1.f / 128.f, 128)));
66 ARM_COMPUTE_RETURN_ERROR_ON(data_type == DataType::QASYMM8 && (f_act == ActivationLayerInfo::ActivationFunction::LOGISTIC) && (oq_info != QuantizationInfo(1.f / 256.f, 0)));
67
68 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)));
69 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)));
70
71 ARM_COMPUTE_RETURN_ERROR_ON(data_type == DataType::QASYMM8_SIGNED && (f_act == ActivationLayerInfo::ActivationFunction::TANH) && (oq_info != QuantizationInfo(1.f / 128.f, 0)));
72 ARM_COMPUTE_RETURN_ERROR_ON(data_type == DataType::QASYMM8_SIGNED && (f_act == ActivationLayerInfo::ActivationFunction::LOGISTIC) && (oq_info != QuantizationInfo(1.f / 256.f, -128)));
73
74 // Checks performed when output is configured
75 if((output != nullptr) && (output->total_size() != 0))
76 {
77 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, output);
78 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
79 }
80
81 return Status{};
82 }
83 } // namespace
84
CLActivationLayerKernel()85 CLActivationLayerKernel::CLActivationLayerKernel()
86 : _run_in_place(false)
87 {
88 }
89
configure(const CLCompileContext & compile_context,ITensorInfo * input,ITensorInfo * output,ActivationLayerInfo act_info)90 void CLActivationLayerKernel::configure(const CLCompileContext &compile_context, ITensorInfo *input, ITensorInfo *output, ActivationLayerInfo act_info)
91 {
92 ARM_COMPUTE_ERROR_ON_NULLPTR(input);
93
94 auto padding_info = get_padding_info({ input, output });
95
96 _run_in_place = (output == nullptr) || (output == input);
97
98 if(output != nullptr)
99 {
100 // Output auto inizialitation if not yet initialized
101 auto_init_if_empty(*output, *input->clone());
102 }
103
104 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input, (output != nullptr) ? output : nullptr, act_info));
105
106 const unsigned int num_elems_processed_per_iteration = adjust_vec_size(16 / input->element_size(), input->dimension(0));
107
108 const DataType dt = input->data_type();
109 float a_const = act_info.a();
110 float b_const = act_info.b();
111
112 const ActivationLayerInfo::ActivationFunction f_act = act_info.activation();
113 const bool is_quantized = is_data_type_quantized(dt);
114 const bool perform_activation_in_float =
115 (f_act == ActivationLayerInfo::ActivationFunction::LOGISTIC) || (f_act == ActivationLayerInfo::ActivationFunction::TANH) || (f_act == ActivationLayerInfo::ActivationFunction::HARD_SWISH);
116
117 // Set build options
118 CLBuildOptions build_opts;
119 build_opts.add_option_if(perform_activation_in_float, "-DFLOAT_DOMAIN");
120 build_opts.add_option_if(_run_in_place, "-DIN_PLACE");
121 build_opts.add_option("-DACT=" + lower_string(string_from_activation_func(f_act)));
122 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(dt));
123 build_opts.add_option("-DVEC_SIZE=" + support::cpp11::to_string(num_elems_processed_per_iteration));
124 build_opts.add_option("-DVEC_SIZE_LEFTOVER=" + support::cpp11::to_string(input->dimension(0) % num_elems_processed_per_iteration));
125
126 std::string kernel_name = std::string("activation_layer");
127
128 // Set quantization info build options
129 if(is_quantized)
130 {
131 const UniformQuantizationInfo iq_info = input->quantization_info().uniform();
132
133 if(!perform_activation_in_float)
134 {
135 int a_const_int = 0;
136 int b_const_int = 0;
137
138 // Create quantized version of constants a, b if needed
139 switch(dt)
140 {
141 case DataType::QASYMM8:
142 {
143 a_const_int = quantize_qasymm8(a_const, iq_info);
144 b_const_int = quantize_qasymm8(b_const, iq_info);
145 }
146 break;
147 case DataType::QASYMM8_SIGNED:
148 {
149 a_const_int = quantize_qasymm8_signed(a_const, iq_info);
150 b_const_int = quantize_qasymm8_signed(b_const, iq_info);
151 }
152 break;
153 case DataType::QSYMM16:
154 {
155 a_const_int = quantize_qsymm16(a_const, iq_info);
156 b_const_int = quantize_qsymm16(b_const, iq_info);
157 }
158 break;
159 default:
160 break;
161 }
162 build_opts.add_option(("-DA_VAL=" + support::cpp11::to_string(a_const_int)));
163 build_opts.add_option(("-DB_VAL=" + support::cpp11::to_string(b_const_int)));
164 }
165 else
166 {
167 build_opts.add_option(("-DA_VAL=" + float_to_string_with_full_precision(a_const)));
168 build_opts.add_option(("-DB_VAL=" + float_to_string_with_full_precision(b_const)));
169 }
170
171 // Quantized value of 0 corresponds to the offset o1
172 build_opts.add_option(("-DCONST_0=" + (is_data_type_quantized_asymmetric(dt) ? support::cpp11::to_string(iq_info.offset) : "0")));
173 build_opts.add_option(("-DS1_VAL=" + float_to_string_with_full_precision(iq_info.scale)));
174 build_opts.add_option_if(is_data_type_quantized_asymmetric(dt), "-DO1_VAL=" + support::cpp11::to_string(iq_info.offset));
175
176 // Set correct kernel name
177 kernel_name += perform_activation_in_float ? std::string("_quant_f32") : std::string("_quant");
178
179 // Set scale and offset of the input and output if they have different quantization info
180 if(output != nullptr)
181 {
182 const UniformQuantizationInfo oq_info = output->quantization_info().uniform();
183
184 if(iq_info != oq_info)
185 {
186 build_opts.add_option(("-DS2_VAL=" + float_to_string_with_full_precision(oq_info.scale)));
187 build_opts.add_option_if(is_data_type_quantized_asymmetric(dt), "-DO2_VAL=" + support::cpp11::to_string(oq_info.offset));
188 }
189 }
190 }
191 else
192 {
193 // Set A, B constants in build options for float types
194 build_opts.add_option(("-DA_VAL=" + float_to_string_with_full_precision(a_const)));
195 build_opts.add_option(("-DB_VAL=" + float_to_string_with_full_precision(b_const)));
196 }
197
198 // Create kernel
199 _kernel = create_kernel(compile_context, kernel_name, build_opts.options());
200
201 // Configure kernel window
202 Window win = calculate_max_window(*input, Steps(num_elems_processed_per_iteration));
203 ICLKernel::configure_internal(win);
204
205 // Set config_id for enabling LWS tuning
206 _config_id = "activation_layer_";
207 _config_id += lower_string(string_from_data_type(dt));
208 _config_id += "_";
209 _config_id += support::cpp11::to_string(input->dimension(0));
210 _config_id += "_";
211 _config_id += support::cpp11::to_string(input->dimension(1));
212
213 ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info));
214 }
215
validate(const ITensorInfo * input,const ITensorInfo * output,const ActivationLayerInfo & act_info)216 Status CLActivationLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const ActivationLayerInfo &act_info)
217 {
218 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, act_info));
219 return Status{};
220 }
221
run_op(ITensorPack & tensors,const Window & window,cl::CommandQueue & queue)222 void CLActivationLayerKernel::run_op(ITensorPack &tensors, const Window &window, cl::CommandQueue &queue)
223 {
224 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
225 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
226
227 const auto src = utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC));
228 auto dst = utils::cast::polymorphic_downcast<ICLTensor *>(tensors.get_tensor(TensorType::ACL_DST));
229 ARM_COMPUTE_ERROR_ON(_run_in_place && src != dst);
230
231 Window collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimZ);
232 Window slice = collapsed.first_slice_window_3D();
233
234 do
235 {
236 unsigned int idx = 0;
237 add_3D_tensor_argument(idx, src, slice);
238 if(!_run_in_place)
239 {
240 add_3D_tensor_argument(idx, dst, slice);
241 }
242 enqueue(queue, *this, slice, lws_hint());
243 }
244 while(collapsed.slide_window_slice_3D(slice));
245 }
246 } // namespace arm_compute
247