1*c217d954SCole Faust /*
2*c217d954SCole Faust * Copyright (c) 2018-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/ClElementwiseKernel.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 "src/common/utils/Validate.h"
29*c217d954SCole Faust #include "src/core/CL/CLValidate.h"
30*c217d954SCole Faust #include "src/core/helpers/AutoConfiguration.h"
31*c217d954SCole Faust #include "src/core/helpers/WindowHelpers.h"
32*c217d954SCole Faust #include "support/Cast.h"
33*c217d954SCole Faust #include "support/StringSupport.h"
34*c217d954SCole Faust #include <map>
35*c217d954SCole Faust
36*c217d954SCole Faust namespace arm_compute
37*c217d954SCole Faust {
38*c217d954SCole Faust namespace opencl
39*c217d954SCole Faust {
40*c217d954SCole Faust namespace kernels
41*c217d954SCole Faust {
42*c217d954SCole Faust namespace
43*c217d954SCole Faust {
44*c217d954SCole Faust constexpr unsigned int vector_size_byte_opencl = 16;
45*c217d954SCole Faust
46*c217d954SCole Faust std::map<ArithmeticOperation, std::string> supported_arithmetic_ops =
47*c217d954SCole Faust {
48*c217d954SCole Faust { ArithmeticOperation::ADD, "ADD" },
49*c217d954SCole Faust { ArithmeticOperation::SUB, "SUB" },
50*c217d954SCole Faust { ArithmeticOperation::DIV, "DIV" },
51*c217d954SCole Faust { ArithmeticOperation::SQUARED_DIFF, "SQUARED_DIFF" },
52*c217d954SCole Faust { ArithmeticOperation::MIN, "MIN" },
53*c217d954SCole Faust { ArithmeticOperation::MAX, "MAX" },
54*c217d954SCole Faust { ArithmeticOperation::POWER, "POWER" },
55*c217d954SCole Faust { ArithmeticOperation::PRELU, "PRELU" },
56*c217d954SCole Faust };
57*c217d954SCole Faust
58*c217d954SCole Faust std::map<ArithmeticOperation, std::string> supported_sat_arithmetic_ops =
59*c217d954SCole Faust {
60*c217d954SCole Faust { ArithmeticOperation::ADD, "ADD" },
61*c217d954SCole Faust { ArithmeticOperation::SUB, "SUB" },
62*c217d954SCole Faust };
63*c217d954SCole Faust
generate_id_for_tuning_common(const std::string & kernel_name,const ITensorInfo & src1,const ITensorInfo & dst)64*c217d954SCole Faust std::string generate_id_for_tuning_common(const std::string &kernel_name, const ITensorInfo &src1, const ITensorInfo &dst)
65*c217d954SCole Faust {
66*c217d954SCole Faust std::string config_id;
67*c217d954SCole Faust // Set config_id for enabling LWS tuning
68*c217d954SCole Faust config_id = kernel_name;
69*c217d954SCole Faust config_id += "_";
70*c217d954SCole Faust config_id += lower_string(string_from_data_type(src1.data_type()));
71*c217d954SCole Faust config_id += "_";
72*c217d954SCole Faust config_id += support::cpp11::to_string(dst.dimension(0));
73*c217d954SCole Faust config_id += "_";
74*c217d954SCole Faust config_id += support::cpp11::to_string(dst.dimension(1));
75*c217d954SCole Faust return config_id;
76*c217d954SCole Faust }
77*c217d954SCole Faust
validate_in_place_output_shape(const bool in_place,const bool src1_in_place,const ITensorInfo & src1,const ITensorInfo & src2,const ITensorInfo & dst,const TensorShape & out_shape)78*c217d954SCole Faust Status validate_in_place_output_shape(const bool in_place, const bool src1_in_place, const ITensorInfo &src1, const ITensorInfo &src2, const ITensorInfo &dst, const TensorShape &out_shape)
79*c217d954SCole Faust {
80*c217d954SCole Faust if(in_place)
81*c217d954SCole Faust {
82*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_MSG(detail::have_different_dimensions(out_shape, src1_in_place ? src1.tensor_shape() : src2.tensor_shape(), 0),
83*c217d954SCole Faust "Wrong shape for dst, cannot do in_place calculation");
84*c217d954SCole Faust }
85*c217d954SCole Faust else
86*c217d954SCole Faust {
87*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_MSG(detail::have_different_dimensions(out_shape, dst.tensor_shape(), 0),
88*c217d954SCole Faust "Wrong shape for dst");
89*c217d954SCole Faust }
90*c217d954SCole Faust return Status{};
91*c217d954SCole Faust }
92*c217d954SCole Faust
validate_arguments_with_float_only_supported_rules(const ITensorInfo & src1,const ITensorInfo & src2,const ITensorInfo & dst)93*c217d954SCole Faust Status validate_arguments_with_float_only_supported_rules(const ITensorInfo &src1, const ITensorInfo &src2, const ITensorInfo &dst)
94*c217d954SCole Faust {
95*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(&src1, &src2, &dst);
96*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(&src1);
97*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&src1, 1, DataType::F16, DataType::F32);
98*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(&src1, &src2);
99*c217d954SCole Faust
100*c217d954SCole Faust // Check whether it is in_place calculation
101*c217d954SCole Faust const bool in_place = (&src1 == &dst) || (&src2 == &dst);
102*c217d954SCole Faust const bool src1_in_place = in_place && (&src1 == &dst);
103*c217d954SCole Faust
104*c217d954SCole Faust const TensorShape out_shape = TensorShape::broadcast_shape(src1.tensor_shape(), src2.tensor_shape());
105*c217d954SCole Faust
106*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_MSG(out_shape.total_size() == 0, "Inputs are not broadcast compatible");
107*c217d954SCole Faust
108*c217d954SCole Faust // Validate in case of configured dst
109*c217d954SCole Faust if(dst.total_size() > 0)
110*c217d954SCole Faust {
111*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&dst, 1, DataType::F16, DataType::F32);
112*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(&src1, &dst);
113*c217d954SCole Faust ARM_COMPUTE_RETURN_ON_ERROR(validate_in_place_output_shape(in_place, src1_in_place, src1, src2, dst, out_shape));
114*c217d954SCole Faust }
115*c217d954SCole Faust
116*c217d954SCole Faust return Status{};
117*c217d954SCole Faust }
118*c217d954SCole Faust
validate_arguments_divide_operation(const ITensorInfo * src1,const ITensorInfo * src2,const ITensorInfo * dst)119*c217d954SCole Faust Status validate_arguments_divide_operation(const ITensorInfo *src1, const ITensorInfo *src2, const ITensorInfo *dst)
120*c217d954SCole Faust {
121*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src1, src2, dst);
122*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(src1);
123*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src1, 1, DataType::F16, DataType::F32, DataType::S32);
124*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src1, src2);
125*c217d954SCole Faust
126*c217d954SCole Faust // Check whether it is in_place calculation
127*c217d954SCole Faust const bool in_place = (src1 == dst) || (src2 == dst);
128*c217d954SCole Faust const bool src1_in_place = in_place && (src1 == dst);
129*c217d954SCole Faust
130*c217d954SCole Faust const TensorShape out_shape = TensorShape::broadcast_shape(src1->tensor_shape(), src2->tensor_shape());
131*c217d954SCole Faust
132*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_MSG(out_shape.total_size() == 0, "Inputs are not broadcast compatible");
133*c217d954SCole Faust
134*c217d954SCole Faust // Validate in case of configured dst
135*c217d954SCole Faust if(dst->total_size() > 0)
136*c217d954SCole Faust {
137*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(dst, 1, DataType::F16, DataType::F32, DataType::S32);
138*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src1, dst);
139*c217d954SCole Faust ARM_COMPUTE_RETURN_ON_ERROR(validate_in_place_output_shape(in_place, src1_in_place, *src1, *src2, *dst, out_shape));
140*c217d954SCole Faust }
141*c217d954SCole Faust
142*c217d954SCole Faust return Status{};
143*c217d954SCole Faust }
144*c217d954SCole Faust
validate_arguments_with_arithmetic_rules(const ITensorInfo & src1,const ITensorInfo & src2,const ITensorInfo & dst)145*c217d954SCole Faust Status validate_arguments_with_arithmetic_rules(const ITensorInfo &src1, const ITensorInfo &src2, const ITensorInfo &dst)
146*c217d954SCole Faust {
147*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(&src1);
148*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&src1, 1, DataType::U8, DataType::QASYMM8, DataType::QASYMM8_SIGNED,
149*c217d954SCole Faust DataType::S16, DataType::QSYMM16, DataType::F16,
150*c217d954SCole Faust DataType::S32, DataType::F32);
151*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(&src1, &src2);
152*c217d954SCole Faust
153*c217d954SCole Faust if(is_data_type_quantized_symmetric(src1.data_type()))
154*c217d954SCole Faust {
155*c217d954SCole Faust const int32_t in1_offset = src1.quantization_info().uniform().offset;
156*c217d954SCole Faust const int32_t in2_offset = src2.quantization_info().uniform().offset;
157*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_MSG(in1_offset != 0, "For quantized symmetric, offset must be zero");
158*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_MSG(in2_offset != 0, "For quantized symmetric, offset must be zero");
159*c217d954SCole Faust }
160*c217d954SCole Faust
161*c217d954SCole Faust // Check whether it is in_place calculation
162*c217d954SCole Faust const bool in_place = (&src1 == &dst) || (&src2 == &dst);
163*c217d954SCole Faust const bool src1_in_place = in_place && (&src1 == &dst);
164*c217d954SCole Faust
165*c217d954SCole Faust const TensorShape out_shape = TensorShape::broadcast_shape(src1.tensor_shape(), src2.tensor_shape());
166*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_MSG(out_shape.total_size() == 0, "Inputs are not broadcast compatible");
167*c217d954SCole Faust
168*c217d954SCole Faust // Validate in case of configured dst
169*c217d954SCole Faust if(dst.total_size() > 0)
170*c217d954SCole Faust {
171*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(&src1, &dst);
172*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_MSG(detail::have_different_dimensions(out_shape, dst.tensor_shape(), 0), "Wrong shape for dst");
173*c217d954SCole Faust ARM_COMPUTE_RETURN_ON_ERROR(validate_in_place_output_shape(in_place, src1_in_place, src1, src2, dst, out_shape));
174*c217d954SCole Faust
175*c217d954SCole Faust if(is_data_type_quantized_symmetric(dst.data_type()))
176*c217d954SCole Faust {
177*c217d954SCole Faust const int32_t offset = dst.quantization_info().uniform().offset;
178*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_MSG(offset != 0, "For quantized symmetric, offset must be zero");
179*c217d954SCole Faust }
180*c217d954SCole Faust }
181*c217d954SCole Faust return Status{};
182*c217d954SCole Faust }
183*c217d954SCole Faust
generate_build_options_with_arithmetic_rules(const ITensorInfo & src1,const ITensorInfo & src2,const ITensorInfo & dst,const std::string & operation_string)184*c217d954SCole Faust CLBuildOptions generate_build_options_with_arithmetic_rules(const ITensorInfo &src1, const ITensorInfo &src2, const ITensorInfo &dst, const std::string &operation_string)
185*c217d954SCole Faust {
186*c217d954SCole Faust CLBuildOptions build_opts;
187*c217d954SCole Faust
188*c217d954SCole Faust const unsigned int num_elems_processed_per_iteration = adjust_vec_size(vector_size_byte_opencl / dst.element_size(), dst.dimension(0));
189*c217d954SCole Faust
190*c217d954SCole Faust build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(src1.data_type()));
191*c217d954SCole Faust build_opts.add_option("-DVEC_SIZE_IN1=" + support::cpp11::to_string(src1.dimension(0) == 1 ? 1 : num_elems_processed_per_iteration));
192*c217d954SCole Faust build_opts.add_option("-DVEC_SIZE_IN2=" + support::cpp11::to_string(src2.dimension(0) == 1 ? 1 : num_elems_processed_per_iteration));
193*c217d954SCole Faust build_opts.add_option("-DVEC_SIZE_OUT=" + support::cpp11::to_string(num_elems_processed_per_iteration));
194*c217d954SCole Faust build_opts.add_option("-DVEC_SIZE_LEFTOVER=" + support::cpp11::to_string(dst.dimension(0) % num_elems_processed_per_iteration));
195*c217d954SCole Faust build_opts.add_option("-DOP=" + operation_string);
196*c217d954SCole Faust if(is_data_type_quantized(src1.data_type()))
197*c217d954SCole Faust {
198*c217d954SCole Faust const UniformQuantizationInfo iq1info = src1.quantization_info().uniform();
199*c217d954SCole Faust const UniformQuantizationInfo iq2info = src2.quantization_info().uniform();
200*c217d954SCole Faust const UniformQuantizationInfo oqinfo = dst.quantization_info().uniform();
201*c217d954SCole Faust
202*c217d954SCole Faust build_opts.add_option("-DOFFSET_IN1=" + support::cpp11::to_string(iq1info.offset));
203*c217d954SCole Faust build_opts.add_option("-DOFFSET_IN2=" + support::cpp11::to_string(iq2info.offset));
204*c217d954SCole Faust build_opts.add_option("-DOFFSET_OUT=" + support::cpp11::to_string(oqinfo.offset));
205*c217d954SCole Faust build_opts.add_option("-DSCALE_IN1=" + float_to_string_with_full_precision(iq1info.scale));
206*c217d954SCole Faust build_opts.add_option("-DSCALE_IN2=" + float_to_string_with_full_precision(iq2info.scale));
207*c217d954SCole Faust build_opts.add_option("-DSCALE_OUT=" + float_to_string_with_full_precision(oqinfo.scale));
208*c217d954SCole Faust }
209*c217d954SCole Faust build_opts.add_option_if(src1.data_type() == DataType::S32, "-DS32");
210*c217d954SCole Faust
211*c217d954SCole Faust // Check whether it is in_place calculation
212*c217d954SCole Faust const bool in_place = (&src1 == &dst) || (&src2 == &dst);
213*c217d954SCole Faust const bool src1_in_place = in_place && (&src1 == &dst);
214*c217d954SCole Faust build_opts.add_option_if(in_place, "-DIN_PLACE");
215*c217d954SCole Faust build_opts.add_option_if(src1_in_place, "-DSRC1_IN_PLACE");
216*c217d954SCole Faust
217*c217d954SCole Faust return build_opts;
218*c217d954SCole Faust }
219*c217d954SCole Faust
configure_window_arithmetic_common(ITensorInfo & dst)220*c217d954SCole Faust std::pair<Status, Window> configure_window_arithmetic_common(ITensorInfo &dst)
221*c217d954SCole Faust {
222*c217d954SCole Faust const unsigned int num_elems_processed_per_iteration = adjust_vec_size(vector_size_byte_opencl / dst.element_size(), dst.dimension(0));
223*c217d954SCole Faust Window win = calculate_max_window(dst, Steps(num_elems_processed_per_iteration));
224*c217d954SCole Faust return std::make_pair(Status{}, win);
225*c217d954SCole Faust }
226*c217d954SCole Faust
validate_and_configure_window_for_arithmetic_operators(ITensorInfo & src1,ITensorInfo & src2,ITensorInfo & dst)227*c217d954SCole Faust std::pair<Status, Window> validate_and_configure_window_for_arithmetic_operators(ITensorInfo &src1, ITensorInfo &src2, ITensorInfo &dst)
228*c217d954SCole Faust {
229*c217d954SCole Faust const std::pair<TensorShape, ValidRegion> broadcast_pair = ITensorInfo::broadcast_shape_and_valid_region(src1, src2);
230*c217d954SCole Faust const TensorShape &out_shape = broadcast_pair.first;
231*c217d954SCole Faust
232*c217d954SCole Faust auto_init_if_empty(dst, out_shape, 1, src1.data_type());
233*c217d954SCole Faust
234*c217d954SCole Faust return configure_window_arithmetic_common(dst);
235*c217d954SCole Faust }
236*c217d954SCole Faust
validate_and_configure_window_for_logical_binary_operators(ITensorInfo & src1,ITensorInfo & src2,ITensorInfo & dst)237*c217d954SCole Faust std::pair<Status, Window> validate_and_configure_window_for_logical_binary_operators(ITensorInfo &src1, ITensorInfo &src2, ITensorInfo &dst)
238*c217d954SCole Faust {
239*c217d954SCole Faust const std::pair<TensorShape, ValidRegion> broadcast_pair = ITensorInfo::broadcast_shape_and_valid_region(src1, src2);
240*c217d954SCole Faust const TensorShape &out_shape = broadcast_pair.first;
241*c217d954SCole Faust
242*c217d954SCole Faust set_shape_if_empty(dst, out_shape);
243*c217d954SCole Faust set_data_type_if_unknown(dst, DataType::U8);
244*c217d954SCole Faust
245*c217d954SCole Faust return configure_window_arithmetic_common(dst);
246*c217d954SCole Faust }
247*c217d954SCole Faust
validate_and_configure_window_for_division(ITensorInfo & src1,ITensorInfo & src2,ITensorInfo & dst)248*c217d954SCole Faust std::pair<Status, Window> validate_and_configure_window_for_division(ITensorInfo &src1, ITensorInfo &src2, ITensorInfo &dst)
249*c217d954SCole Faust {
250*c217d954SCole Faust const std::pair<TensorShape, ValidRegion> broadcast_pair = ITensorInfo::broadcast_shape_and_valid_region(src1, src2);
251*c217d954SCole Faust const TensorShape &out_shape = broadcast_pair.first;
252*c217d954SCole Faust
253*c217d954SCole Faust auto_init_if_empty(dst, out_shape, 1, src1.data_type());
254*c217d954SCole Faust
255*c217d954SCole Faust return configure_window_arithmetic_common(dst);
256*c217d954SCole Faust }
257*c217d954SCole Faust } // namespace
258*c217d954SCole Faust
ClElementwiseKernel()259*c217d954SCole Faust ClElementwiseKernel::ClElementwiseKernel()
260*c217d954SCole Faust {
261*c217d954SCole Faust _type = CLKernelType::ELEMENTWISE;
262*c217d954SCole Faust }
263*c217d954SCole Faust
configure_common(const ClCompileContext & compile_context,ITensorInfo * src1,ITensorInfo * src2,ITensorInfo * dst)264*c217d954SCole Faust void ClElementwiseKernel::configure_common(const ClCompileContext &compile_context, ITensorInfo *src1, ITensorInfo *src2, ITensorInfo *dst)
265*c217d954SCole Faust {
266*c217d954SCole Faust // Configure kernel window
267*c217d954SCole Faust auto win_config = validate_and_configure_window(*src1, *src2, *dst);
268*c217d954SCole Faust ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
269*c217d954SCole Faust
270*c217d954SCole Faust std::string kernel_name = "elementwise_operation_" + name();
271*c217d954SCole Faust if(is_data_type_quantized(src1->data_type()))
272*c217d954SCole Faust {
273*c217d954SCole Faust kernel_name += "_quantized";
274*c217d954SCole Faust }
275*c217d954SCole Faust
276*c217d954SCole Faust // Set kernel build options
277*c217d954SCole Faust CLBuildOptions build_opts = generate_build_options(*src1, *src2, *dst);
278*c217d954SCole Faust if(_act_info.enabled())
279*c217d954SCole Faust {
280*c217d954SCole Faust build_opts.add_option("-DACTIVATION_TYPE=" + lower_string(string_from_activation_func(_act_info.activation())));
281*c217d954SCole Faust build_opts.add_option("-DA_VAL=" + float_to_string_with_full_precision(_act_info.a()));
282*c217d954SCole Faust build_opts.add_option("-DB_VAL=" + float_to_string_with_full_precision(_act_info.b()));
283*c217d954SCole Faust }
284*c217d954SCole Faust
285*c217d954SCole Faust // Create kernel
286*c217d954SCole Faust _kernel = create_kernel(compile_context, kernel_name, build_opts.options());
287*c217d954SCole Faust
288*c217d954SCole Faust ICLKernel::configure_internal(win_config.second);
289*c217d954SCole Faust
290*c217d954SCole Faust _config_id = generate_id_for_tuning(kernel_name, *src1, *dst);
291*c217d954SCole Faust }
292*c217d954SCole Faust
run_op(ITensorPack & tensors,const Window & window,::cl::CommandQueue & queue)293*c217d954SCole Faust void ClElementwiseKernel::run_op(ITensorPack &tensors, const Window &window, ::cl::CommandQueue &queue)
294*c217d954SCole Faust {
295*c217d954SCole Faust ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
296*c217d954SCole Faust ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
297*c217d954SCole Faust
298*c217d954SCole Faust const auto src_0 = utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC_0));
299*c217d954SCole Faust const auto src_1 = utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC_1));
300*c217d954SCole Faust auto dst = utils::cast::polymorphic_downcast<ICLTensor *>(tensors.get_tensor(TensorType::ACL_DST));
301*c217d954SCole Faust
302*c217d954SCole Faust ARM_COMPUTE_ERROR_ON_NULLPTR(src_0, src_1, dst);
303*c217d954SCole Faust
304*c217d954SCole Faust const TensorShape &in_shape1 = src_0->info()->tensor_shape();
305*c217d954SCole Faust const TensorShape &in_shape2 = src_1->info()->tensor_shape();
306*c217d954SCole Faust const TensorShape &out_shape = dst->info()->tensor_shape();
307*c217d954SCole Faust
308*c217d954SCole Faust bool can_collapse = true;
309*c217d954SCole Faust const bool is_vector = in_shape1.num_dimensions() == 1 || in_shape2.num_dimensions() == 1;
310*c217d954SCole Faust if(std::min(in_shape1.total_size(), in_shape2.total_size()) > 1 && !is_vector)
311*c217d954SCole Faust {
312*c217d954SCole Faust can_collapse = (std::min(in_shape1.num_dimensions(), in_shape2.num_dimensions()) > Window::DimZ);
313*c217d954SCole Faust for(size_t d = Window::DimZ; can_collapse && (d < out_shape.num_dimensions()); d++)
314*c217d954SCole Faust {
315*c217d954SCole Faust can_collapse = (in_shape1[d] == in_shape2[d]);
316*c217d954SCole Faust }
317*c217d954SCole Faust }
318*c217d954SCole Faust
319*c217d954SCole Faust bool has_collapsed = false;
320*c217d954SCole Faust Window collapsed = can_collapse ? window.collapse_if_possible(ICLKernel::window(), Window::DimZ, &has_collapsed) : window;
321*c217d954SCole Faust
322*c217d954SCole Faust const TensorShape &in_shape1_collapsed = has_collapsed ? in_shape1.collapsed_from(Window::DimZ) : in_shape1;
323*c217d954SCole Faust const TensorShape &in_shape2_collapsed = has_collapsed ? in_shape2.collapsed_from(Window::DimZ) : in_shape2;
324*c217d954SCole Faust
325*c217d954SCole Faust Window slice = collapsed.first_slice_window_3D();
326*c217d954SCole Faust Window slice_src1 = slice.broadcast_if_dimension_le_one(in_shape1_collapsed);
327*c217d954SCole Faust Window slice_src2 = slice.broadcast_if_dimension_le_one(in_shape2_collapsed);
328*c217d954SCole Faust
329*c217d954SCole Faust // Check whether it is in_place calculation
330*c217d954SCole Faust const bool in_place = (src_0 == dst) || (src_1 == dst);
331*c217d954SCole Faust do
332*c217d954SCole Faust {
333*c217d954SCole Faust unsigned int idx = 0;
334*c217d954SCole Faust add_3D_tensor_argument(idx, src_0, slice_src1);
335*c217d954SCole Faust add_3D_tensor_argument(idx, src_1, slice_src2);
336*c217d954SCole Faust if(!in_place)
337*c217d954SCole Faust {
338*c217d954SCole Faust add_3D_tensor_argument(idx, dst, slice);
339*c217d954SCole Faust }
340*c217d954SCole Faust
341*c217d954SCole Faust enqueue(queue, *this, slice, lws_hint());
342*c217d954SCole Faust ARM_COMPUTE_UNUSED(collapsed.slide_window_slice_3D(slice_src1));
343*c217d954SCole Faust ARM_COMPUTE_UNUSED(collapsed.slide_window_slice_3D(slice_src2));
344*c217d954SCole Faust }
345*c217d954SCole Faust while(collapsed.slide_window_slice_3D(slice));
346*c217d954SCole Faust }
347*c217d954SCole Faust
348*c217d954SCole Faust /** Logical binary */
349*c217d954SCole Faust
configure(const ClCompileContext & compile_context,LogicalOperation op,ITensorInfo * src1,ITensorInfo * src2,ITensorInfo * dst)350*c217d954SCole Faust void ClLogicalBinaryKernel::configure(const ClCompileContext &compile_context, LogicalOperation op, ITensorInfo *src1, ITensorInfo *src2, ITensorInfo *dst)
351*c217d954SCole Faust {
352*c217d954SCole Faust ARM_COMPUTE_ERROR_ON_NULLPTR(src1, src2, dst);
353*c217d954SCole Faust ARM_COMPUTE_ERROR_THROW_ON(ClLogicalBinaryKernel::validate(op, src1, src2, dst));
354*c217d954SCole Faust _op = op;
355*c217d954SCole Faust configure_common(compile_context, src1, src2, dst);
356*c217d954SCole Faust }
357*c217d954SCole Faust
validate(LogicalOperation op,const ITensorInfo * src1,const ITensorInfo * src2,const ITensorInfo * dst)358*c217d954SCole Faust Status ClLogicalBinaryKernel::validate(LogicalOperation op, const ITensorInfo *src1, const ITensorInfo *src2, const ITensorInfo *dst)
359*c217d954SCole Faust {
360*c217d954SCole Faust ARM_COMPUTE_UNUSED(op);
361*c217d954SCole Faust ARM_COMPUTE_ASSERT(op != LogicalOperation::Unknown && op != LogicalOperation::Not);
362*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src1, src2, dst);
363*c217d954SCole Faust
364*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src1, 1, DataType::U8);
365*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src1, src2);
366*c217d954SCole Faust
367*c217d954SCole Faust ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments_with_arithmetic_rules(*src1, *src2, *dst));
368*c217d954SCole Faust ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window_for_logical_binary_operators(*src1->clone(), *src2->clone(), *dst->clone()).first);
369*c217d954SCole Faust
370*c217d954SCole Faust return Status{};
371*c217d954SCole Faust }
372*c217d954SCole Faust
name()373*c217d954SCole Faust std::string ClLogicalBinaryKernel::name()
374*c217d954SCole Faust {
375*c217d954SCole Faust switch(_op)
376*c217d954SCole Faust {
377*c217d954SCole Faust case LogicalOperation::And:
378*c217d954SCole Faust return "AND";
379*c217d954SCole Faust case LogicalOperation::Or:
380*c217d954SCole Faust return "OR";
381*c217d954SCole Faust case LogicalOperation::Not:
382*c217d954SCole Faust /* fall through */
383*c217d954SCole Faust default:
384*c217d954SCole Faust ARM_COMPUTE_ASSERT(true);
385*c217d954SCole Faust }
386*c217d954SCole Faust return "";
387*c217d954SCole Faust }
388*c217d954SCole Faust
validate_and_configure_window(ITensorInfo & src1,ITensorInfo & src2,ITensorInfo & dst)389*c217d954SCole Faust std::pair<Status, Window> ClLogicalBinaryKernel::validate_and_configure_window(ITensorInfo &src1, ITensorInfo &src2, ITensorInfo &dst)
390*c217d954SCole Faust {
391*c217d954SCole Faust return validate_and_configure_window_for_logical_binary_operators(src1, src2, dst);
392*c217d954SCole Faust }
393*c217d954SCole Faust
generate_build_options(const ITensorInfo & src1,const ITensorInfo & src2,const ITensorInfo & dst)394*c217d954SCole Faust CLBuildOptions ClLogicalBinaryKernel::generate_build_options(const ITensorInfo &src1, const ITensorInfo &src2, const ITensorInfo &dst)
395*c217d954SCole Faust {
396*c217d954SCole Faust // The arithmetic utility functions can be share
397*c217d954SCole Faust return generate_build_options_with_arithmetic_rules(src1, src2, dst, name());
398*c217d954SCole Faust }
399*c217d954SCole Faust
generate_id_for_tuning(const std::string & kernel_name,const ITensorInfo & src1,const ITensorInfo & dst)400*c217d954SCole Faust std::string ClLogicalBinaryKernel::generate_id_for_tuning(const std::string &kernel_name, const ITensorInfo &src1, const ITensorInfo &dst)
401*c217d954SCole Faust {
402*c217d954SCole Faust return generate_id_for_tuning_common(kernel_name, src1, dst);
403*c217d954SCole Faust }
404*c217d954SCole Faust
405*c217d954SCole Faust /** Arithmetic operations with saturation*/
configure(const ClCompileContext & compile_context,ArithmeticOperation op,ITensorInfo * input1,ITensorInfo * input2,ITensorInfo * output,const ConvertPolicy & policy,const ActivationLayerInfo & act_info)406*c217d954SCole Faust void ClSaturatedArithmeticKernel::configure(const ClCompileContext &compile_context, ArithmeticOperation op, ITensorInfo *input1, ITensorInfo *input2, ITensorInfo *output,
407*c217d954SCole Faust const ConvertPolicy &policy,
408*c217d954SCole Faust const ActivationLayerInfo &act_info)
409*c217d954SCole Faust {
410*c217d954SCole Faust ARM_COMPUTE_ERROR_ON_NULLPTR(input1, input2, output);
411*c217d954SCole Faust ARM_COMPUTE_ERROR_THROW_ON(ClSaturatedArithmeticKernel::validate(op, input1, input2, output, policy, act_info));
412*c217d954SCole Faust auto padding_info = get_padding_info({ input1, input2, output });
413*c217d954SCole Faust
414*c217d954SCole Faust _policy = policy;
415*c217d954SCole Faust _op = op;
416*c217d954SCole Faust _act_info = act_info;
417*c217d954SCole Faust configure_common(compile_context, input1, input2, output);
418*c217d954SCole Faust ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info));
419*c217d954SCole Faust }
420*c217d954SCole Faust
validate(ArithmeticOperation op,const ITensorInfo * input1,const ITensorInfo * input2,const ITensorInfo * output,const ConvertPolicy & policy,const ActivationLayerInfo & act_info)421*c217d954SCole Faust Status ClSaturatedArithmeticKernel::validate(ArithmeticOperation op, const ITensorInfo *input1, const ITensorInfo *input2, const ITensorInfo *output, const ConvertPolicy &policy,
422*c217d954SCole Faust const ActivationLayerInfo &act_info)
423*c217d954SCole Faust {
424*c217d954SCole Faust ARM_COMPUTE_UNUSED(op, policy);
425*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input1, input2, output);
426*c217d954SCole Faust ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments_with_arithmetic_rules(*input1, *input2, *output));
427*c217d954SCole Faust ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window_for_arithmetic_operators(*input1->clone(), *input2->clone(), *output->clone()).first);
428*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON(act_info.enabled() && !is_data_type_float(output->data_type()));
429*c217d954SCole Faust
430*c217d954SCole Faust return Status{};
431*c217d954SCole Faust }
432*c217d954SCole Faust
validate_and_configure_window(ITensorInfo & input1,ITensorInfo & input2,ITensorInfo & output)433*c217d954SCole Faust std::pair<Status, Window> ClSaturatedArithmeticKernel::validate_and_configure_window(ITensorInfo &input1, ITensorInfo &input2, ITensorInfo &output)
434*c217d954SCole Faust {
435*c217d954SCole Faust return validate_and_configure_window_for_arithmetic_operators(input1, input2, output);
436*c217d954SCole Faust }
437*c217d954SCole Faust
generate_build_options(const ITensorInfo & input1,const ITensorInfo & input2,const ITensorInfo & output)438*c217d954SCole Faust CLBuildOptions ClSaturatedArithmeticKernel::generate_build_options(const ITensorInfo &input1, const ITensorInfo &input2, const ITensorInfo &output)
439*c217d954SCole Faust {
440*c217d954SCole Faust const bool has_float_out = is_data_type_float(output.data_type());
441*c217d954SCole Faust auto build_options = generate_build_options_with_arithmetic_rules(input1, input2, output, name());
442*c217d954SCole Faust build_options.add_option((_policy == ConvertPolicy::WRAP || has_float_out) ? "-DWRAP" : "-DSATURATE");
443*c217d954SCole Faust return build_options;
444*c217d954SCole Faust }
445*c217d954SCole Faust
generate_id_for_tuning(const std::string & kernel_name,const ITensorInfo & input1,const ITensorInfo & output)446*c217d954SCole Faust std::string ClSaturatedArithmeticKernel::generate_id_for_tuning(const std::string &kernel_name, const ITensorInfo &input1, const ITensorInfo &output)
447*c217d954SCole Faust {
448*c217d954SCole Faust auto config_id = generate_id_for_tuning_common(kernel_name, input1, output);
449*c217d954SCole Faust config_id += (_policy == ConvertPolicy::WRAP) ? "_wrap_" : "_saturate_";
450*c217d954SCole Faust config_id += lower_string(string_from_data_layout(input1.data_layout()));
451*c217d954SCole Faust return config_id;
452*c217d954SCole Faust }
453*c217d954SCole Faust
name()454*c217d954SCole Faust std::string ClSaturatedArithmeticKernel::name()
455*c217d954SCole Faust {
456*c217d954SCole Faust return supported_sat_arithmetic_ops[_op];
457*c217d954SCole Faust }
458*c217d954SCole Faust
459*c217d954SCole Faust /** Arithmetic operations*/
configure(const ClCompileContext & compile_context,ArithmeticOperation op,ITensorInfo * src1,ITensorInfo * src2,ITensorInfo * dst,const ActivationLayerInfo & act_info)460*c217d954SCole Faust void ClArithmeticKernel::configure(const ClCompileContext &compile_context, ArithmeticOperation op, ITensorInfo *src1, ITensorInfo *src2, ITensorInfo *dst,
461*c217d954SCole Faust const ActivationLayerInfo &act_info)
462*c217d954SCole Faust {
463*c217d954SCole Faust ARM_COMPUTE_ERROR_ON_NULLPTR(src1, src2, dst);
464*c217d954SCole Faust ARM_COMPUTE_ERROR_THROW_ON(ClArithmeticKernel::validate(op, src1, src2, dst, act_info));
465*c217d954SCole Faust auto padding_info = get_padding_info({ src1, src2, dst });
466*c217d954SCole Faust
467*c217d954SCole Faust _op = op;
468*c217d954SCole Faust _act_info = act_info;
469*c217d954SCole Faust configure_common(compile_context, src1, src2, dst);
470*c217d954SCole Faust ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info));
471*c217d954SCole Faust }
472*c217d954SCole Faust
validate(ArithmeticOperation op,const ITensorInfo * src1,const ITensorInfo * src2,const ITensorInfo * dst,const ActivationLayerInfo & act_info)473*c217d954SCole Faust Status ClArithmeticKernel::validate(ArithmeticOperation op, const ITensorInfo *src1, const ITensorInfo *src2, const ITensorInfo *dst, const ActivationLayerInfo &act_info)
474*c217d954SCole Faust {
475*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src1, src2, dst);
476*c217d954SCole Faust if(op == ArithmeticOperation::DIV)
477*c217d954SCole Faust {
478*c217d954SCole Faust // Partial integer support S32/F32/F16
479*c217d954SCole Faust ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments_divide_operation(src1, src2, dst));
480*c217d954SCole Faust ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window_for_division(*src1->clone(), *src2->clone(), *dst->clone()).first);
481*c217d954SCole Faust }
482*c217d954SCole Faust else if(op == ArithmeticOperation::POWER)
483*c217d954SCole Faust {
484*c217d954SCole Faust // Power operators doesn't support integer arithmetic
485*c217d954SCole Faust ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments_with_float_only_supported_rules(*src1, *src2, *dst));
486*c217d954SCole Faust ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window_for_division(*src1->clone(), *src2->clone(), *dst->clone()).first);
487*c217d954SCole Faust }
488*c217d954SCole Faust else
489*c217d954SCole Faust {
490*c217d954SCole Faust ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments_with_arithmetic_rules(*src1, *src2, *dst));
491*c217d954SCole Faust ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window_for_arithmetic_operators(*src1->clone(), *src2->clone(), *dst->clone()).first);
492*c217d954SCole Faust }
493*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON(act_info.enabled() && !is_data_type_float(dst->data_type()));
494*c217d954SCole Faust
495*c217d954SCole Faust return Status{};
496*c217d954SCole Faust }
validate_and_configure_window(ITensorInfo & src1,ITensorInfo & src2,ITensorInfo & dst)497*c217d954SCole Faust std::pair<Status, Window> ClArithmeticKernel::validate_and_configure_window(ITensorInfo &src1, ITensorInfo &src2, ITensorInfo &dst)
498*c217d954SCole Faust {
499*c217d954SCole Faust if(_op == ArithmeticOperation::DIV || _op == ArithmeticOperation::POWER)
500*c217d954SCole Faust {
501*c217d954SCole Faust // Division and Power operators don't support integer arithmetic
502*c217d954SCole Faust return validate_and_configure_window_for_division(src1, src2, dst);
503*c217d954SCole Faust }
504*c217d954SCole Faust else
505*c217d954SCole Faust {
506*c217d954SCole Faust return validate_and_configure_window_for_arithmetic_operators(src1, src2, dst);
507*c217d954SCole Faust }
508*c217d954SCole Faust }
509*c217d954SCole Faust
generate_build_options(const ITensorInfo & src1,const ITensorInfo & src2,const ITensorInfo & dst)510*c217d954SCole Faust CLBuildOptions ClArithmeticKernel::generate_build_options(const ITensorInfo &src1, const ITensorInfo &src2, const ITensorInfo &dst)
511*c217d954SCole Faust {
512*c217d954SCole Faust return generate_build_options_with_arithmetic_rules(src1, src2, dst, name());
513*c217d954SCole Faust }
generate_id_for_tuning(const std::string & kernel_name,const ITensorInfo & src1,const ITensorInfo & dst)514*c217d954SCole Faust std::string ClArithmeticKernel::generate_id_for_tuning(const std::string &kernel_name, const ITensorInfo &src1, const ITensorInfo &dst)
515*c217d954SCole Faust {
516*c217d954SCole Faust return generate_id_for_tuning_common(kernel_name, src1, dst);
517*c217d954SCole Faust }
518*c217d954SCole Faust
name()519*c217d954SCole Faust std::string ClArithmeticKernel::name()
520*c217d954SCole Faust {
521*c217d954SCole Faust return supported_arithmetic_ops[_op];
522*c217d954SCole Faust }
523*c217d954SCole Faust } // namespace kernels
524*c217d954SCole Faust } // namespace opencl
525*c217d954SCole Faust } // namespace arm_compute
526