Ramy Elgammal | 404462a | 2022-11-08 02:14:46 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (c) 2022 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 "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 | |
| 34 | namespace arm_compute |
| 35 | { |
| 36 | namespace experimental |
| 37 | { |
| 38 | namespace dynamic_fusion |
| 39 | { |
| 40 | constexpr unsigned int vector_size_byte_opencl = 16; |
| 41 | |
| 42 | ClTemplateElementwiseBinary::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 | |
| 57 | std::string ClTemplateElementwiseBinary::get_name() const |
| 58 | { |
| 59 | return "elementwise_binary"; |
| 60 | } |
| 61 | |
| 62 | std::string ClTemplateElementwiseBinary::get_component_code(const ComponentGroup &comp_group) const |
| 63 | { |
| 64 | ARM_COMPUTE_UNUSED(comp_group); |
| 65 | std::string code; |
| 66 | const bool is_broadcast = _lhs->tensor_shape() != _rhs->tensor_shape(); |
| 67 | const bool is_root = (comp_group.get_root_component()->id() == this->id()); |
| 68 | |
| 69 | if(is_root) |
| 70 | { |
| 71 | code = |
| 72 | R"_( |
| 73 | //------------------ START KERNEL {{meta_kernel_id}} ELTWISE_OP --------------------- |
| 74 | )_" |
| 75 | // IN_0(LHS) {{lhs}} |
| 76 | // IN_1(RHS) {{rhs}} |
| 77 | // OUT(dst, accum) {{dst}} |
| 78 | // dst = lhs + rhs (mix-precision, broadcast, boundary aware) |
| 79 | R"_( |
| 80 | TILE({{DATA_TYPE}}, M0, N0, {{dst}}); |
| 81 | TILE(uint, M0, 1, g_dst_indirect_y); |
| 82 | { |
| 83 | TILE({{DATA_TYPE}}, M0, N0, lhs_tile); |
| 84 | TILE({{DATA_TYPE}}, M0, N0, rhs_tile); |
| 85 | )_" |
| 86 | // Assuming un-collapsed window |
| 87 | R"_( |
| 88 | {{lhs}}_offset_first_element_in_bytes += g_ind_2 * {{lhs}}_stride_z; |
| 89 | {{rhs}}_offset_first_element_in_bytes += g_ind_2 * {{rhs}}_stride_z; |
| 90 | |
| 91 | T_LOAD({{DATA_TYPE}}, M0, N0, BUFFER, {{lhs}}, g_ind_0, g_ind_1, 1, {{lhs}}_stride_y, lhs_tile); |
| 92 | T_LOAD({{DATA_TYPE}}, {{rhs_m0}}, {{rhs_n0}}, BUFFER, {{rhs}}, {{rhs_start_ind_0}}, {{rhs_start_ind_1}}, 1, {{rhs}}_stride_y, rhs_tile); |
| 93 | )_"; |
| 94 | if(is_broadcast) |
| 95 | { |
| 96 | code += |
| 97 | R"_( |
| 98 | T_ELTWISE_BROADCAST_{{ELTWISE_OP}}_X({{DATA_TYPE}}, M0, N0, lhs_tile, rhs_tile, {{dst}}); |
| 99 | )_"; |
| 100 | } |
| 101 | else |
| 102 | { |
| 103 | code += |
| 104 | R"_( |
| 105 | T_ELTWISE_{{ELTWISE_OP}}({{DATA_TYPE}}, M0, N0, lhs_tile, rhs_tile, {{dst}}); |
| 106 | )_"; |
| 107 | } |
| 108 | code += |
| 109 | // Calculate the destination indirect Y |
| 110 | R"_( |
| 111 | LOOP_UNROLLING(int, i, 0, 1, M0, |
| 112 | { |
| 113 | g_dst_indirect_y[i].v = (uint)min(g_ind_1 + i, (int)({{dst}}_w * {{dst}}_h) - 1); |
| 114 | g_dst_indirect_y[i].v += g_ind_2 * (int)({{dst}}_w * {{dst}}_h); |
| 115 | }) |
| 116 | } |
| 117 | //------------------ END KERNEL {{meta_kernel_id}} ELTWISE_OP --------------------- |
| 118 | )_"; |
| 119 | } |
| 120 | |
| 121 | else // non-root |
| 122 | { |
| 123 | code = |
| 124 | R"_( |
| 125 | //------------------ START KERNEL {{meta_kernel_id}} ELTWISE_OP --------------------- |
| 126 | )_" |
| 127 | // IN_0/Out(Accumulator) {{acc}} |
| 128 | // IN_1(Operand) {{operand}} |
| 129 | // acc = operand + acc (mix-precision, broadcast, boundary aware) |
| 130 | R"_( |
| 131 | { |
| 132 | TILE(DATA_TYPE, M0, N0, operand_tile); |
| 133 | T_LOAD({{DATA_TYPE}}, {{rhs_m0}}, {{rhs_n0}}, BUFFER, {{operand}}, {{rhs_start_ind_0}}, {{rhs_start_ind_1}}, 1, {{operand}}_stride_y, operand_tile); |
| 134 | )_"; |
| 135 | |
| 136 | if(is_broadcast) |
| 137 | { |
| 138 | code += |
| 139 | R"_( |
| 140 | T_ELTWISE_BROADCAST_{{ELTWISE_OP}}_X({{DATA_TYPE}}, M0, N0, {{acc}}, operand_tile, {{acc}}); |
| 141 | )_"; |
| 142 | } |
| 143 | else |
| 144 | { |
| 145 | code += |
| 146 | R"_( |
| 147 | T_ELTWISE_{{ELTWISE_OP}}({{DATA_TYPE}}, M0, N0, {{acc}}, operand_tile, {{acc}}); |
| 148 | )_"; |
| 149 | } |
| 150 | code += |
| 151 | R"_( |
| 152 | } |
| 153 | //------------------ END KERNEL {{meta_kernel_id}} ELTWISE_OP --------------------- |
| 154 | )_"; |
| 155 | } |
| 156 | |
| 157 | return code; |
| 158 | } |
| 159 | |
| 160 | void ClTemplateElementwiseBinary::declare_variables(GpuKernelVariableTable &vtable, const ComponentGroup &comp_group) const |
| 161 | { |
| 162 | vtable.declare_variable( |
| 163 | _lhs, |
| 164 | GpuKernelArgumentInfo(common_tensor_type), |
| 165 | comp_group.is_intermediate_tensor(_lhs), |
| 166 | "lhs"); |
| 167 | |
| 168 | vtable.declare_variable( |
| 169 | _rhs, |
| 170 | GpuKernelArgumentInfo(common_tensor_type), |
| 171 | comp_group.is_intermediate_tensor(_rhs), |
| 172 | "rhs"); |
| 173 | |
| 174 | vtable.declare_variable( |
| 175 | _dst, |
| 176 | GpuKernelArgumentInfo(common_tensor_type), |
| 177 | comp_group.is_intermediate_tensor(_dst), |
| 178 | "dst"); |
| 179 | } |
| 180 | |
| 181 | TagLUT ClTemplateElementwiseBinary::get_tag_lut(const GpuKernelVariableTable &vtable, const ComponentGroup &comp_group) const |
| 182 | { |
| 183 | TagLUT lut{}; |
| 184 | const ITensorInfo *accumulator = _lhs; |
| 185 | const ITensorInfo *operand = _rhs; |
| 186 | |
| 187 | // Local build options |
| 188 | lut["meta_kernel_id"] = id(); |
| 189 | lut["DATA_TYPE"] = get_cl_type_from_data_type(_lhs->data_type()); |
| 190 | // Arguments and global shared variables |
| 191 | const bool is_root = (comp_group.get_root_component()->id() == this->id()); |
| 192 | if(is_root) |
| 193 | { |
| 194 | lut["lhs"] = vtable.get_variable(_lhs); |
| 195 | lut["rhs"] = vtable.get_variable(_rhs); |
| 196 | lut["dst"] = vtable.get_variable(_dst); |
| 197 | } |
| 198 | else |
| 199 | { |
| 200 | // Determine which tensor is the accumulator |
| 201 | if(comp_group.is_intermediate_tensor(_lhs)) |
| 202 | { |
| 203 | accumulator = _lhs; |
| 204 | operand = _rhs; |
| 205 | } |
| 206 | else if(comp_group.is_intermediate_tensor(_rhs)) |
| 207 | { |
| 208 | accumulator = _rhs; |
| 209 | operand = _lhs; |
| 210 | } |
| 211 | else |
| 212 | { |
| 213 | ARM_COMPUTE_ERROR("Invalid elementwise component linking"); |
| 214 | } |
| 215 | lut["acc"] = vtable.get_variable(accumulator); |
| 216 | lut["operand"] = vtable.get_variable(operand); |
| 217 | } |
| 218 | switch(_attributes.operation()) |
| 219 | { |
| 220 | case Attributes::ElementwiseOp::ADD: |
| 221 | lut["ELTWISE_OP"] = "ADD"; |
| 222 | break; |
| 223 | default: |
| 224 | ARM_COMPUTE_ERROR("Arithmetic Operation not supported"); |
| 225 | } |
| 226 | ARM_COMPUTE_ERROR_ON_MSG(detail::have_different_dimensions(accumulator->tensor_shape(), _dst->tensor_shape(), 0), "Only the operand can be broadcast to match the accumulator's shape"); |
| 227 | const bool is_broadcast = (operand->tensor_shape() != _dst->tensor_shape()); |
| 228 | |
| 229 | // Set broadcast parameters |
| 230 | // PRE: All tensors are broadcast-compatible |
| 231 | if(is_broadcast) |
| 232 | { |
| 233 | // Note that n0 maps to input tensor dimension 0, m0 maps to input dimensions 1 and 2 because of our collapse strategy |
| 234 | if(operand->dimension(0) == 1U && operand->dimension(1) == 1U && operand->dimension(2) == 1U) // Broadcast in X, Y, Z: collapsed rhs win [M0xN0] = [1x1] |
| 235 | { |
| 236 | lut["rhs_m0"] = "1"; |
| 237 | lut["rhs_n0"] = "1"; |
| 238 | lut["rhs_start_ind_1"] = "0"; |
| 239 | lut["rhs_start_ind_0"] = "0"; |
| 240 | } |
| 241 | else if(operand->dimension(1) == 1U && operand->dimension(2) == 1U) // Broadcast in Y and Z: collapsed rhs win [M0xN0] = [1xN] |
| 242 | { |
| 243 | lut["rhs_m0"] = "1"; |
| 244 | lut["rhs_n0"] = "N0"; |
| 245 | lut["rhs_start_ind_1"] = "0"; |
| 246 | lut["rhs_start_ind_0"] = "g_ind_0"; |
| 247 | } |
| 248 | else |
| 249 | { |
| 250 | ARM_COMPUTE_ERROR("Only support rhs broadcasting in all X, Y, Z dimensions, or just in Y and Z dimensions"); |
| 251 | } |
| 252 | } |
| 253 | else |
| 254 | { |
| 255 | lut["rhs_m0"] = "M0"; |
| 256 | lut["rhs_n0"] = "N0"; |
| 257 | lut["rhs_start_ind_1"] = "g_ind_1"; |
| 258 | lut["rhs_start_ind_0"] = "g_ind_0"; |
| 259 | } |
| 260 | return lut; |
| 261 | } |
| 262 | |
| 263 | CLBuildOptions ClTemplateElementwiseBinary::get_build_options(const ComponentGroup &comp_group) const |
| 264 | { |
| 265 | CLBuildOptions build_opts{}; |
| 266 | /// NOTE: For now tile sizes (n0, m0) are set by the execution window. This may change in the future |
| 267 | const auto root_window = comp_group.get_root_component()->template_writer()->get_window(); |
| 268 | const unsigned int n0 = root_window.x().step(); |
| 269 | const unsigned int m0 = root_window.y().step(); |
| 270 | const unsigned int partial_store_n0 = _dst->dimension(0) % n0; |
| 271 | |
| 272 | build_opts.add_option("-DM0=" + support::cpp11::to_string(m0)); |
| 273 | build_opts.add_option("-DN0=" + support::cpp11::to_string(n0)); |
| 274 | build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(_lhs->data_type())); |
| 275 | build_opts.add_option("-DPARTIAL_N0=" + support::cpp11::to_string(partial_store_n0)); |
| 276 | |
| 277 | return build_opts; |
| 278 | } |
| 279 | |
| 280 | std::string ClTemplateElementwiseBinary::get_config_id() const |
| 281 | { |
| 282 | std::string config_id{}; |
| 283 | config_id += lower_string(string_from_data_type(_dst->data_type())); |
| 284 | config_id += "_"; |
| 285 | config_id += support::cpp11::to_string(_dst->dimension(0)); |
| 286 | config_id += "_"; |
| 287 | config_id += support::cpp11::to_string(_dst->dimension(1)); |
| 288 | config_id += "_"; |
| 289 | config_id += lower_string(string_from_data_layout(_dst->data_layout())); |
| 290 | |
| 291 | return config_id; |
| 292 | } |
| 293 | |
| 294 | std::set<std::string> ClTemplateElementwiseBinary::get_headers_list() const |
| 295 | { |
| 296 | return std::set<std::string>{ "helpers.h", "tile_helpers.h" }; |
| 297 | } |
| 298 | |
| 299 | Window ClTemplateElementwiseBinary::get_window() const |
| 300 | { |
| 301 | ARM_COMPUTE_ERROR_ON_MSG(_dst->tensor_shape().total_size() == 0U, "Destination tensor is not initialized"); |
| 302 | |
| 303 | TensorShape output_shape = _dst->tensor_shape(); |
| 304 | // Collapse Dim 1 (W) and Dim 2 (H) together, leave Dim 0 (C) and upper dimensions unchanged |
| 305 | // This is in line with the collapsing convention used by operators like Conv2d |
| 306 | output_shape.collapse(2U, 1U); |
| 307 | const unsigned int num_elems_processed_per_iteration = adjust_vec_size(vector_size_byte_opencl / _dst->element_size(), _dst->dimension(0)); |
| 308 | Window win = calculate_max_window(output_shape, Steps(num_elems_processed_per_iteration)); |
| 309 | |
| 310 | return win; |
| 311 | } |
| 312 | |
| 313 | } // namespace dynamic_fusion |
| 314 | } // namespace experimental |
| 315 | } // namespace arm_compute |