Gunes Bayir | 1dc6ff1 | 2022-12-06 20:48:31 +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 "ClTemplateCast.h" |
| 25 | |
| 26 | #include "src/core/helpers/WindowHelpers.h" |
| 27 | #include "src/dynamic_fusion/sketch/gpu/GpuKernelComponentGroup.h" |
| 28 | |
| 29 | namespace arm_compute |
| 30 | { |
| 31 | namespace experimental |
| 32 | { |
| 33 | namespace dynamic_fusion |
| 34 | { |
| 35 | ClTemplateCast::ClTemplateCast(ComponentId id, const ArgumentPack<ITensorInfo> &tensors, const Attributes &attributes) |
| 36 | : IGpuTemplateComponentWriter{ id, tensors }, _src{}, _dst{}, _attributes{ attributes } |
| 37 | { |
| 38 | _src = this->tensors().get_const_tensor(TensorType::ACL_SRC_0); |
| 39 | _dst = this->tensors().get_const_tensor(TensorType::ACL_DST_0); |
| 40 | |
| 41 | ARM_COMPUTE_ERROR_ON_NULLPTR(_src, _dst); |
| 42 | } |
| 43 | |
| 44 | std::string ClTemplateCast::get_name() const |
| 45 | { |
| 46 | const size_t src_size = data_size_from_type(_src->data_type()); |
| 47 | const size_t dst_size = data_size_from_type(_dst->data_type()); |
| 48 | |
| 49 | return (src_size >= dst_size) ? "cast_down" : "cast_up"; |
| 50 | } |
| 51 | |
| 52 | std::string ClTemplateCast::get_component_code(const ComponentGroup &comp_group) const |
| 53 | { |
| 54 | ARM_COMPUTE_UNUSED(comp_group); |
| 55 | |
| 56 | const std::string kernel_name = get_name(); |
| 57 | |
| 58 | std::string code = R"_( |
| 59 | //------------------ START KERNEL {{meta_kernel_id}} --------------------- |
| 60 | // IN_0(src) {{src}} |
| 61 | // OUT(dst, accum) {{dst}} |
| 62 | |
| 63 | TILE({{DATA_TYPE_OUT}}, M0, N0, {{dst}}); |
| 64 | TILE(uint, M0, 1, g_dst_indirect_y); |
| 65 | { |
| 66 | {{src}}_offset_first_element_in_bytes += get_global_id(2) * {{src}}_stride_z; |
| 67 | |
| 68 | TILE({{DATA_TYPE_IN}}, M0, N0, in_data); |
| 69 | T_LOAD({{DATA_TYPE_IN}}, M0, N0, BUFFER, {{src}}, g_ind_0, g_ind_1, 1, {{src}}_stride_y, in_data); |
| 70 | )_"; |
| 71 | |
| 72 | code += R"_( |
| 73 | LOOP_UNROLLING(int, m0, 0, 1, M0, |
| 74 | { |
| 75 | )_"; |
| 76 | |
| 77 | if(kernel_name == "cast_down" && is_data_type_quantized(_src->data_type())) |
| 78 | { |
| 79 | code += R"_( |
| 80 | in_data[m0].v ^= (VEC_DATA_TYPE({{DATA_TYPE_IN}}, N0))0x80; |
| 81 | )_"; |
| 82 | } |
| 83 | |
| 84 | if(kernel_name == "cast_down" && (is_data_type_float(_src->data_type()) || _attributes.convert_policy() == ConvertPolicy::SATURATE)) |
| 85 | { |
| 86 | code += R"_( |
| 87 | {{dst}}[m0].v = CONVERT_SAT(in_data[m0].v, VEC_DATA_TYPE({{DATA_TYPE_OUT}}, N0)); |
| 88 | )_"; |
| 89 | } |
| 90 | else |
| 91 | { |
| 92 | code += R"_( |
| 93 | {{dst}}[m0].v = CONVERT(in_data[m0].v, VEC_DATA_TYPE({{DATA_TYPE_OUT}}, N0)); |
| 94 | )_"; |
| 95 | } |
| 96 | |
| 97 | code += R"_( |
| 98 | }) |
| 99 | )_"; |
| 100 | |
| 101 | code += R"_( |
| 102 | LOOP_UNROLLING(int, i, 0, 1, M0, |
| 103 | { |
| 104 | g_dst_indirect_y[i].v = (uint)min((int)(g_ind_1 + i), (int)({{arg_dst}}_w) - 1); |
| 105 | g_dst_indirect_y[i].v += (int)(g_ind_2 % {{arg_dst}}_h) * (int)({{arg_dst}}_w); |
| 106 | g_dst_indirect_y[i].v += (int)(g_ind_2 / {{arg_dst}}_h) * (int)({{arg_dst}}_w * {{arg_dst}}_h); |
| 107 | }) |
| 108 | } |
| 109 | //------------------ END KERNEL {{meta_kernel_id}} --------------------- |
| 110 | )_"; |
| 111 | |
| 112 | return code; |
| 113 | } |
| 114 | |
| 115 | void ClTemplateCast::declare_variables(GpuKernelVariableTable &vtable, const ComponentGroup &comp_group) const |
| 116 | { |
| 117 | vtable.declare_variable( |
| 118 | _src, |
| 119 | GpuKernelArgumentInfo(GpuKernelArgumentInfo::Type::Tensor_4D_t_Buffer), |
| 120 | comp_group.is_intermediate_tensor(_src), |
| 121 | "src"); |
| 122 | |
| 123 | vtable.declare_variable( |
| 124 | _dst, |
| 125 | GpuKernelArgumentInfo(GpuKernelArgumentInfo::Type::Tensor_4D_t_Buffer), |
| 126 | comp_group.is_intermediate_tensor(_dst), |
| 127 | "dst"); |
| 128 | } |
| 129 | |
| 130 | TagLUT ClTemplateCast::get_tag_lut(const GpuKernelVariableTable &vtable, const ComponentGroup &comp_group) const |
| 131 | { |
| 132 | ARM_COMPUTE_UNUSED(comp_group); |
| 133 | |
| 134 | TagLUT lut{}; |
| 135 | |
| 136 | // Arguments and global shared variables |
| 137 | lut["src"] = vtable.get_variable(_src); |
| 138 | lut["dst"] = vtable.get_variable(_dst); |
| 139 | |
Viet-Hoa Do | 04f4620 | 2022-12-14 14:49:56 +0000 | [diff] [blame] | 140 | const auto dst_argument = vtable.get_variable(comp_group.get_any_dst_tensor()); |
Gunes Bayir | 1dc6ff1 | 2022-12-06 20:48:31 +0000 | [diff] [blame] | 141 | lut["arg_dst"] = dst_argument.uniq_name; |
| 142 | |
| 143 | // Local build options |
| 144 | lut["meta_kernel_id"] = id(); |
| 145 | |
| 146 | lut["DATA_TYPE_IN"] = get_cl_type_from_data_type(_src->data_type()); |
| 147 | lut["DATA_TYPE_OUT"] = get_cl_type_from_data_type(_dst->data_type()); |
| 148 | |
| 149 | return lut; |
| 150 | } |
| 151 | |
| 152 | CLBuildOptions ClTemplateCast::get_build_options(const ComponentGroup &comp_group) const |
| 153 | { |
| 154 | ARM_COMPUTE_UNUSED(comp_group); |
| 155 | |
| 156 | const auto root_window = comp_group.get_root_component()->template_writer()->get_window(); |
| 157 | const unsigned int n0 = root_window.x().step(); |
| 158 | const unsigned int m0 = root_window.y().step(); |
| 159 | |
| 160 | // Set build options |
| 161 | CLBuildOptions build_opts{}; |
| 162 | build_opts.add_option("-DN0=" + support::cpp11::to_string(n0)); |
| 163 | build_opts.add_option("-DPARTIAL_N0=" + support::cpp11::to_string(_src->dimension(0) % n0)); |
| 164 | build_opts.add_option("-DM0=" + support::cpp11::to_string(m0)); |
| 165 | |
| 166 | return build_opts; |
| 167 | } |
| 168 | |
| 169 | std::string ClTemplateCast::get_config_id() const |
| 170 | { |
| 171 | std::string config_id{}; |
| 172 | |
| 173 | config_id += "_"; |
| 174 | config_id += lower_string(string_from_data_type(_src->data_type())); |
| 175 | config_id += "_"; |
| 176 | config_id += lower_string(string_from_data_type(_dst->data_type())); |
| 177 | config_id += "_"; |
| 178 | config_id += support::cpp11::to_string(_src->dimension(0)); |
| 179 | config_id += "_"; |
| 180 | config_id += support::cpp11::to_string(_src->dimension(1)); |
| 181 | |
| 182 | return config_id; |
| 183 | } |
| 184 | |
| 185 | std::set<std::string> ClTemplateCast::get_headers_list() const |
| 186 | { |
| 187 | return std::set<std::string>{ "helpers.h", "tile_helpers.h" }; |
| 188 | } |
| 189 | |
| 190 | Window ClTemplateCast::get_window() const |
| 191 | { |
| 192 | ARM_COMPUTE_ERROR_ON_MSG(_dst->tensor_shape().total_size() == 0U, "Destination tensor is not initialized"); |
| 193 | |
| 194 | const unsigned int n0 = adjust_vec_size(16 / _src->element_size(), _src->dimension(0)); |
| 195 | Window win = calculate_max_window(*_dst, Steps(n0)); |
| 196 | return win.collapse(win, Window::DimZ); |
| 197 | } |
| 198 | |
| 199 | } // namespace dynamic_fusion |
| 200 | } // namespace experimental |
| 201 | } // namespace arm_compute |