Ramy Elgammal | f800adf | 2022-12-14 15:39:29 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2023 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 "ClTemplateReshape.h" |
| 25 | #include "src/core/helpers/WindowHelpers.h" |
| 26 | #include "src/dynamic_fusion/sketch/gpu/GpuKernelComponentGroup.h" |
| 27 | |
| 28 | namespace arm_compute |
| 29 | { |
| 30 | namespace experimental |
| 31 | { |
| 32 | namespace dynamic_fusion |
| 33 | { |
| 34 | constexpr unsigned int vector_size_byte_opencl = 16; |
| 35 | |
| 36 | ClTemplateReshape::ClTemplateReshape(ComponentId id, |
| 37 | const ArgumentPack<ITensorInfo> &tensors) |
| 38 | : IGpuTemplateComponentWriter{ id, tensors }, |
| 39 | _src{}, |
| 40 | _dst{} |
| 41 | { |
| 42 | _src = this->tensors().get_const_tensor(TensorType::ACL_SRC_0); |
| 43 | _dst = this->tensors().get_const_tensor(TensorType::ACL_DST_0); |
| 44 | ARM_COMPUTE_ERROR_ON_NULLPTR(_src, _dst); |
| 45 | } |
| 46 | |
| 47 | std::string ClTemplateReshape::get_name() const |
| 48 | { |
| 49 | return "reshape"; |
| 50 | } |
| 51 | |
| 52 | std::string ClTemplateReshape::get_component_code(const ComponentGroup &comp_group) const |
| 53 | { |
| 54 | ARM_COMPUTE_UNUSED(comp_group); |
| 55 | std::string code; |
| 56 | |
| 57 | code = R"_( |
| 58 | //------------------ START KERNEL {{meta_kernel_id}} --------------------- |
| 59 | |
| 60 | // IN(src) {{src}} |
| 61 | // OUT(dst, accum) {{dst}} |
| 62 | |
| 63 | TILE(uint, M0, 1, g_dst_indirect_y); |
| 64 | { |
| 65 | __global uchar * base_src_ptr = {{src}}_ptr + {{src}}_offset_first_element_in_bytes; |
| 66 | const int tile_vertical_idx = g_ind_1 * {{arg_dst}}_c + g_ind_2 * {{arg_dst}}_c * {{arg_dst}}_w; |
| 67 | LOOP_UNROLLING(int, _m0, 0, 1, M0, |
| 68 | { |
| 69 | const int row_idx = _m0 * {{arg_dst}}_c + tile_vertical_idx; |
| 70 | const int tile_horizontal_idx = g_ind_0 + row_idx; |
| 71 | LOOP_UNROLLING(int, _n0, 0, 1, N0, |
| 72 | { |
| 73 | {{src}}_ptr = base_src_ptr; |
| 74 | const int linear_idx = tile_horizontal_idx + _n0; |
| 75 | const int in_id_x = linear_idx % {{src}}_c; |
| 76 | const int in_id_y = (linear_idx / {{src}}_c) % {{src}}_w; |
| 77 | const int in_id_z = linear_idx / ({{src}}_c * {{src}}_w); |
| 78 | {{src}}_ptr += in_id_x * sizeof({{DATA_TYPE}}) + in_id_y * {{src}}_stride_y + in_id_z * {{src}}_stride_z; |
| 79 | {{dst}}[_m0].s[_n0] = *((__global {{DATA_TYPE}} *){{src}}_ptr); |
| 80 | }) |
| 81 | }) |
| 82 | |
| 83 | LOOP_UNROLLING(int, i, 0, 1, M0, |
| 84 | { |
| 85 | g_dst_indirect_y[i].v = (uint)min((int)(g_ind_1 + i), (int)({{arg_dst}}_w) - 1); |
| 86 | g_dst_indirect_y[i].v += (int)(g_ind_2 % {{arg_dst}}_h) * (int)({{arg_dst}}_w); |
| 87 | g_dst_indirect_y[i].v += (int)(g_ind_2 / {{arg_dst}}_h) * (int)({{arg_dst}}_w * {{arg_dst}}_h); |
| 88 | }) |
| 89 | } |
| 90 | //------------------ END KERNEL {{meta_kernel_id}} --------------------- |
| 91 | )_"; |
| 92 | return code; |
| 93 | } |
| 94 | |
| 95 | void ClTemplateReshape::declare_variables(GpuKernelVariableTable &vtable, const ComponentGroup &comp_group) const |
| 96 | { |
| 97 | vtable.declare_variable( |
| 98 | comp_group, |
| 99 | _src, |
| 100 | GpuKernelArgumentInfo(common_tensor_type), // GpuKernelArgumentInfo::Type::Image_3D |
| 101 | "src"); |
| 102 | |
| 103 | vtable.declare_variable( |
| 104 | comp_group, |
| 105 | _dst, |
| 106 | GpuKernelArgumentInfo(common_tensor_type), |
| 107 | "dst"); |
| 108 | } |
| 109 | |
| 110 | TagLUT ClTemplateReshape::get_tag_lut(const GpuKernelVariableTable &vtable, const ComponentGroup &comp_group) const |
| 111 | { |
| 112 | ARM_COMPUTE_UNUSED(comp_group); |
| 113 | TagLUT lut{}; |
| 114 | |
| 115 | // Arguments and global shared variables |
| 116 | lut["src"] = vtable.get_variable(_src); |
| 117 | lut["dst"] = vtable.get_variable(_dst); |
| 118 | lut["arg_dst"] = vtable.get_variable(comp_group.get_any_dst_tensor()); |
| 119 | lut["meta_kernel_id"] = id(); |
| 120 | lut["DATA_TYPE"] = get_cl_type_from_data_type(_dst->data_type()); |
| 121 | |
| 122 | return lut; |
| 123 | } |
| 124 | |
| 125 | CLBuildOptions ClTemplateReshape::get_build_options(const ComponentGroup &comp_group) const |
| 126 | { |
| 127 | CLBuildOptions build_opts{}; |
| 128 | const auto root_window = comp_group.get_root_component()->template_writer()->get_window(); |
| 129 | const unsigned int n0 = root_window.x().step(); |
| 130 | const unsigned int m0 = root_window.y().step(); |
| 131 | const unsigned int partial_store_n0 = _dst->dimension(0) % n0; |
| 132 | build_opts.add_option("-DN0=" + support::cpp11::to_string(n0)); |
| 133 | build_opts.add_option("-DM0=" + support::cpp11::to_string(m0)); |
| 134 | build_opts.add_option("-DPARTIAL_N0=" + support::cpp11::to_string(partial_store_n0)); |
| 135 | |
| 136 | return build_opts; |
| 137 | } |
| 138 | |
| 139 | std::string ClTemplateReshape::get_config_id() const |
| 140 | { |
| 141 | std::string config_id{}; |
| 142 | config_id += lower_string(string_from_data_type(_dst->data_type())); |
| 143 | config_id += "_"; |
| 144 | config_id += support::cpp11::to_string(_dst->dimension(0)); |
| 145 | config_id += "_"; |
| 146 | config_id += support::cpp11::to_string(_dst->dimension(1)); |
| 147 | |
| 148 | return config_id; |
| 149 | } |
| 150 | |
| 151 | std::set<std::string> ClTemplateReshape::get_headers_list() const |
| 152 | { |
| 153 | return std::set<std::string>{ "helpers.h", "tile_helpers.h" }; |
| 154 | } |
| 155 | |
| 156 | Window ClTemplateReshape::get_window() const |
| 157 | { |
| 158 | ARM_COMPUTE_ERROR_ON_MSG(_dst->tensor_shape().total_size() == 0U, "Destination tensor is not initialized"); |
| 159 | const unsigned int n0 = adjust_vec_size(vector_size_byte_opencl / _dst->element_size(), _dst->dimension(0)); |
| 160 | Window win = calculate_max_window(*_dst, Steps(n0)); |
| 161 | return win.collapse(win, Window::DimZ); |
| 162 | } |
| 163 | |
| 164 | } // namespace dynamic_fusion |
| 165 | } // namespace experimental |
| 166 | } // namespace arm_compute |