blob: 01017ed90981ff6026e40623ac481be3fbdb697c [file] [log] [blame]
Ramy Elgammal404462a2022-11-08 02:14:46 +00001/*
Viet-Hoa Dob3077fb2023-01-03 17:59:14 +00002 * Copyright (c) 2022-2023 Arm Limited.
Ramy Elgammal404462a2022-11-08 02:14:46 +00003 *
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 "ClTemplateElementwiseBinary.h"
25
26#include "src/dynamic_fusion/sketch/gpu/GpuKernelComponentGroup.h"
27#include "src/dynamic_fusion/sketch/gpu/components/cl/ClComponentElementwiseBinary.h"
28
29#include "arm_compute/core/utils/misc/ShapeCalculator.h"
30#include "src/core/helpers/WindowHelpers.h"
31
32#include "support/StringSupport.h"
33
34namespace arm_compute
35{
36namespace experimental
37{
38namespace dynamic_fusion
39{
40constexpr unsigned int vector_size_byte_opencl = 16;
41
42ClTemplateElementwiseBinary::ClTemplateElementwiseBinary(ComponentId id,
43 const ArgumentPack<ITensorInfo> &tensors,
44 const Attributes &attributes)
45 : IGpuTemplateComponentWriter{ id, tensors },
46 _lhs{},
47 _rhs{},
48 _dst{},
49 _attributes{ attributes }
50{
51 _lhs = this->tensors().get_const_tensor(TensorType::ACL_SRC_0);
52 _rhs = this->tensors().get_const_tensor(TensorType::ACL_SRC_1);
53 _dst = this->tensors().get_const_tensor(TensorType::ACL_DST_0);
54 ARM_COMPUTE_ERROR_ON_NULLPTR(_lhs, _rhs, _dst);
55}
56
57std::string ClTemplateElementwiseBinary::get_name() const
58{
59 return "elementwise_binary";
60}
61
62std::string ClTemplateElementwiseBinary::get_component_code(const ComponentGroup &comp_group) const
63{
Ramy Elgammal404462a2022-11-08 02:14:46 +000064 std::string code;
Ramy Elgammal404462a2022-11-08 02:14:46 +000065 const bool is_root = (comp_group.get_root_component()->id() == this->id());
Viet-Hoa Do3558c582022-12-16 14:45:57 +000066 const bool is_lhs_input = comp_group.is_input_tensor(_lhs);
67 const bool is_rhs_input = comp_group.is_input_tensor(_rhs);
68
69 code =
70R"_(
71 //------------------ START KERNEL {{meta_kernel_id}} ELTWISE_OP ---------------------
72)_";
Ramy Elgammal404462a2022-11-08 02:14:46 +000073
74 if(is_root)
75 {
Viet-Hoa Do3558c582022-12-16 14:45:57 +000076 code +=
Ramy Elgammal404462a2022-11-08 02:14:46 +000077R"_(
Ramy Elgammal404462a2022-11-08 02:14:46 +000078 TILE(uint, M0, 1, g_dst_indirect_y);
Viet-Hoa Do3558c582022-12-16 14:45:57 +000079)_";
80 }
81
82 if(is_lhs_input)
Ramy Elgammal404462a2022-11-08 02:14:46 +000083 {
Viet-Hoa Do3558c582022-12-16 14:45:57 +000084 code +=
85R"_(
Viet-Hoa Dob3077fb2023-01-03 17:59:14 +000086 TILE({{DATA_TYPE}}, {{lhs_m0}}, N0, {{lhs}});
Viet-Hoa Do3558c582022-12-16 14:45:57 +000087)_";
88 }
89
90 if(is_rhs_input)
91 {
92 code +=
93R"_(
Viet-Hoa Dob3077fb2023-01-03 17:59:14 +000094 TILE({{DATA_TYPE}}, {{rhs_m0}}, N0, {{rhs}});
Viet-Hoa Do3558c582022-12-16 14:45:57 +000095)_";
96 }
97
98 code +=
99R"_(
100 {
101)_";
102
103 if(is_lhs_input)
104 {
105 code +=
Ramy Elgammal404462a2022-11-08 02:14:46 +0000106R"_(
Viet-Hoa Dob3077fb2023-01-03 17:59:14 +0000107 {{lhs}}_offset_first_element_in_bytes += g_ind_2 * {{lhs}}_stride_w;
Viet-Hoa Do3558c582022-12-16 14:45:57 +0000108 T_LOAD({{DATA_TYPE}}, {{lhs_m0}}, {{lhs_n0}}, BUFFER, {{lhs}}, {{lhs_start_ind_0}}, {{lhs_start_ind_1}}, 1, {{lhs}}_stride_y, {{lhs}});
109)_";
110 }
111
112 if(is_rhs_input)
113 {
114 code +=
115R"_(
Viet-Hoa Dob3077fb2023-01-03 17:59:14 +0000116 {{rhs}}_offset_first_element_in_bytes += g_ind_2 * {{rhs}}_stride_w;
Viet-Hoa Do3558c582022-12-16 14:45:57 +0000117 T_LOAD({{DATA_TYPE}}, {{rhs_m0}}, {{rhs_n0}}, BUFFER, {{rhs}}, {{rhs_start_ind_0}}, {{rhs_start_ind_1}}, 1, {{rhs}}_stride_y, {{rhs}});
Ramy Elgammal404462a2022-11-08 02:14:46 +0000118)_";
119 }
120
Viet-Hoa Dob3077fb2023-01-03 17:59:14 +0000121 code +=
122R"_(
123 T_ELTWISE_{{BROADCAST_OP}}{{ELTWISE_OP}}({{DATA_TYPE}}, M0, N0, {{lhs}}, {{rhs}}, {{dst}});
Ramy Elgammal404462a2022-11-08 02:14:46 +0000124)_";
125
Viet-Hoa Do3558c582022-12-16 14:45:57 +0000126 if(is_root)
127 {
128 // Calculate the destination indirect Y
129 code +=
Ramy Elgammal404462a2022-11-08 02:14:46 +0000130R"_(
Viet-Hoa Do3558c582022-12-16 14:45:57 +0000131 LOOP_UNROLLING(int, i, 0, 1, M0,
Ramy Elgammal404462a2022-11-08 02:14:46 +0000132 {
Viet-Hoa Do3558c582022-12-16 14:45:57 +0000133 g_dst_indirect_y[i].v = (uint)min(g_ind_1 + i, (int)({{arg_dst}}_w * {{arg_dst}}_h) - 1);
134 g_dst_indirect_y[i].v += g_ind_2 * (int)({{arg_dst}}_w * {{arg_dst}}_h);
135 })
Ramy Elgammal404462a2022-11-08 02:14:46 +0000136)_";
Viet-Hoa Do3558c582022-12-16 14:45:57 +0000137 }
138
Ramy Elgammal404462a2022-11-08 02:14:46 +0000139 code +=
140R"_(
141 }
142 //------------------ END KERNEL {{meta_kernel_id}} ELTWISE_OP ---------------------
143)_";
Ramy Elgammal404462a2022-11-08 02:14:46 +0000144
145 return code;
146}
147
148void ClTemplateElementwiseBinary::declare_variables(GpuKernelVariableTable &vtable, const ComponentGroup &comp_group) const
149{
150 vtable.declare_variable(
Viet-Hoa Do3558c582022-12-16 14:45:57 +0000151 comp_group,
Ramy Elgammal404462a2022-11-08 02:14:46 +0000152 _lhs,
153 GpuKernelArgumentInfo(common_tensor_type),
Ramy Elgammal404462a2022-11-08 02:14:46 +0000154 "lhs");
155
156 vtable.declare_variable(
Viet-Hoa Do3558c582022-12-16 14:45:57 +0000157 comp_group,
Ramy Elgammal404462a2022-11-08 02:14:46 +0000158 _rhs,
159 GpuKernelArgumentInfo(common_tensor_type),
Ramy Elgammal404462a2022-11-08 02:14:46 +0000160 "rhs");
161
162 vtable.declare_variable(
Viet-Hoa Do3558c582022-12-16 14:45:57 +0000163 comp_group,
Ramy Elgammal404462a2022-11-08 02:14:46 +0000164 _dst,
165 GpuKernelArgumentInfo(common_tensor_type),
Ramy Elgammal404462a2022-11-08 02:14:46 +0000166 "dst");
167}
168
169TagLUT ClTemplateElementwiseBinary::get_tag_lut(const GpuKernelVariableTable &vtable, const ComponentGroup &comp_group) const
170{
171 TagLUT lut{};
Ramy Elgammal404462a2022-11-08 02:14:46 +0000172
173 // Local build options
174 lut["meta_kernel_id"] = id();
175 lut["DATA_TYPE"] = get_cl_type_from_data_type(_lhs->data_type());
176 // Arguments and global shared variables
Viet-Hoa Do3558c582022-12-16 14:45:57 +0000177
178 lut["lhs"] = vtable.get_variable(_lhs);
179 lut["rhs"] = vtable.get_variable(_rhs);
180 lut["dst"] = vtable.get_variable(_dst);
181 lut["arg_dst"] = vtable.get_variable(comp_group.get_any_dst_tensor());
182
Ramy Elgammal404462a2022-11-08 02:14:46 +0000183 switch(_attributes.operation())
184 {
185 case Attributes::ElementwiseOp::ADD:
186 lut["ELTWISE_OP"] = "ADD";
187 break;
188 default:
189 ARM_COMPUTE_ERROR("Arithmetic Operation not supported");
190 }
Viet-Hoa Do3558c582022-12-16 14:45:57 +0000191
192 ARM_COMPUTE_ERROR_ON(
193 comp_group.is_intermediate_tensor(_lhs) &&
194 detail::have_different_dimensions(_lhs->tensor_shape(), _dst->tensor_shape(), 0));
195 ARM_COMPUTE_ERROR_ON(
196 comp_group.is_intermediate_tensor(_rhs) &&
197 detail::have_different_dimensions(_rhs->tensor_shape(), _dst->tensor_shape(), 0));
Ramy Elgammal404462a2022-11-08 02:14:46 +0000198
199 // Set broadcast parameters
200 // PRE: All tensors are broadcast-compatible
Viet-Hoa Dob3077fb2023-01-03 17:59:14 +0000201 const auto &lhs_dims = _lhs->tensor_shape();
202 const auto &rhs_dims = _rhs->tensor_shape();
203 const auto &dst_dims = _dst->tensor_shape();
Viet-Hoa Do3558c582022-12-16 14:45:57 +0000204
Viet-Hoa Dob3077fb2023-01-03 17:59:14 +0000205 const auto lhs_broadcast_x = dst_dims[0] != 1 && lhs_dims[0] == 1;
206 const auto rhs_broadcast_x = dst_dims[0] != 1 && rhs_dims[0] == 1;
207 const auto lhs_broadcast_y = dst_dims[1] != 1 && lhs_dims[1] == 1;
208 const auto rhs_broadcast_y = dst_dims[1] != 1 && rhs_dims[1] == 1;
209 const auto lhs_broadcast_z = dst_dims[2] != 1 && lhs_dims[2] == 1;
210 const auto rhs_broadcast_z = dst_dims[2] != 1 && rhs_dims[2] == 1;
Viet-Hoa Do3558c582022-12-16 14:45:57 +0000211
Viet-Hoa Dob3077fb2023-01-03 17:59:14 +0000212 const auto lhs_broadcast_yz = lhs_broadcast_y && lhs_broadcast_z;
213 const auto rhs_broadcast_yz = rhs_broadcast_y && rhs_broadcast_z;
Viet-Hoa Do3558c582022-12-16 14:45:57 +0000214
Viet-Hoa Dob3077fb2023-01-03 17:59:14 +0000215 lut["lhs_n0"] = (lhs_broadcast_x) ? "1" : "N0";
216 lut["lhs_start_ind_0"] = (lhs_broadcast_x) ? "0" : "g_ind_0";
217 lut["rhs_n0"] = (rhs_broadcast_x) ? "1" : "N0";
218 lut["rhs_start_ind_0"] = (rhs_broadcast_x) ? "0" : "g_ind_0";
219
220 lut["lhs_m0"] = (lhs_broadcast_yz) ? "1" : "M0";
221 lut["lhs_start_ind_1"] = (lhs_broadcast_yz) ? "0" : "g_ind_1";
222 lut["rhs_m0"] = (rhs_broadcast_yz) ? "1" : "M0";
223 lut["rhs_start_ind_1"] = (rhs_broadcast_yz) ? "0" : "g_ind_1";
224
225 lut["BROADCAST_OP"] = (lhs_broadcast_yz) ? "BROADCAST_LHS_X_" :
226 (rhs_broadcast_yz) ? "BROADCAST_RHS_X_" :
227 "";
Viet-Hoa Do3558c582022-12-16 14:45:57 +0000228
Ramy Elgammal404462a2022-11-08 02:14:46 +0000229 return lut;
230}
231
232CLBuildOptions ClTemplateElementwiseBinary::get_build_options(const ComponentGroup &comp_group) const
233{
234 CLBuildOptions build_opts{};
235 /// NOTE: For now tile sizes (n0, m0) are set by the execution window. This may change in the future
236 const auto root_window = comp_group.get_root_component()->template_writer()->get_window();
237 const unsigned int n0 = root_window.x().step();
238 const unsigned int m0 = root_window.y().step();
239 const unsigned int partial_store_n0 = _dst->dimension(0) % n0;
240
241 build_opts.add_option("-DM0=" + support::cpp11::to_string(m0));
242 build_opts.add_option("-DN0=" + support::cpp11::to_string(n0));
243 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(_lhs->data_type()));
244 build_opts.add_option("-DPARTIAL_N0=" + support::cpp11::to_string(partial_store_n0));
245
246 return build_opts;
247}
248
249std::string ClTemplateElementwiseBinary::get_config_id() const
250{
251 std::string config_id{};
252 config_id += lower_string(string_from_data_type(_dst->data_type()));
253 config_id += "_";
254 config_id += support::cpp11::to_string(_dst->dimension(0));
255 config_id += "_";
256 config_id += support::cpp11::to_string(_dst->dimension(1));
257 config_id += "_";
258 config_id += lower_string(string_from_data_layout(_dst->data_layout()));
259
260 return config_id;
261}
262
263std::set<std::string> ClTemplateElementwiseBinary::get_headers_list() const
264{
265 return std::set<std::string>{ "helpers.h", "tile_helpers.h" };
266}
267
268Window ClTemplateElementwiseBinary::get_window() const
269{
270 ARM_COMPUTE_ERROR_ON_MSG(_dst->tensor_shape().total_size() == 0U, "Destination tensor is not initialized");
271
272 TensorShape output_shape = _dst->tensor_shape();
273 // Collapse Dim 1 (W) and Dim 2 (H) together, leave Dim 0 (C) and upper dimensions unchanged
274 // This is in line with the collapsing convention used by operators like Conv2d
275 output_shape.collapse(2U, 1U);
276 const unsigned int num_elems_processed_per_iteration = adjust_vec_size(vector_size_byte_opencl / _dst->element_size(), _dst->dimension(0));
277 Window win = calculate_max_window(output_shape, Steps(num_elems_processed_per_iteration));
278
279 return win;
280}
281
282} // namespace dynamic_fusion
283} // namespace experimental
284} // namespace arm_compute