Jakub Sujak | 3274172 | 2022-11-25 16:43:18 +0000 | [diff] [blame] | 1 | /* |
Ramy Elgammal | f800adf | 2022-12-14 15:39:29 +0000 | [diff] [blame] | 2 | * Copyright (c) 2022-2023 Arm Limited. |
Jakub Sujak | 3274172 | 2022-11-25 16:43:18 +0000 | [diff] [blame] | 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 "ClTemplateActivation.h" |
| 25 | |
| 26 | #include "arm_compute/core/Utils.h" |
| 27 | #include "src/core/helpers/WindowHelpers.h" |
| 28 | #include "src/dynamic_fusion/sketch/gpu/GpuKernelComponentGroup.h" |
| 29 | #include "support/StringSupport.h" |
| 30 | |
| 31 | namespace arm_compute |
| 32 | { |
| 33 | namespace experimental |
| 34 | { |
| 35 | namespace dynamic_fusion |
| 36 | { |
Jakub Sujak | 3274172 | 2022-11-25 16:43:18 +0000 | [diff] [blame] | 37 | ClTemplateActivation::ClTemplateActivation(ComponentId id, |
| 38 | const ArgumentPack<ITensorInfo> &tensors, |
| 39 | const Attributes &attributes) |
| 40 | : IGpuTemplateComponentWriter{ id, tensors }, |
| 41 | _src{}, |
| 42 | _dst{}, |
| 43 | _attributes{ attributes } |
| 44 | { |
| 45 | _src = this->tensors().get_const_tensor(TensorType::ACL_SRC); |
| 46 | _dst = this->tensors().get_const_tensor(TensorType::ACL_DST); |
| 47 | ARM_COMPUTE_ERROR_ON_NULLPTR(_src, _dst); |
| 48 | } |
| 49 | |
| 50 | std::string ClTemplateActivation::get_name() const |
| 51 | { |
| 52 | return "activation"; |
| 53 | } |
| 54 | |
| 55 | std::string ClTemplateActivation::get_component_code(const ComponentGroup &comp_group) const |
| 56 | { |
| 57 | std::string code; |
| 58 | const bool is_root = (comp_group.get_root_component()->id() == this->id()); |
| 59 | |
| 60 | code = R"_( |
| 61 | //------------------ START KERNEL {{meta_kernel_id}} --------------------- |
| 62 | )_"; |
| 63 | if(is_root) |
| 64 | { |
| 65 | code += R"_( |
| 66 | // IN(src) {{src}} |
| 67 | // OUT(dst, accum) {{dst}} |
| 68 | |
Viet-Hoa Do | 3558c58 | 2022-12-16 14:45:57 +0000 | [diff] [blame] | 69 | TILE({{DATA_TYPE}}, M0, N0, {{src}}); |
Jakub Sujak | 3274172 | 2022-11-25 16:43:18 +0000 | [diff] [blame] | 70 | TILE(uint, M0, 1, g_dst_indirect_y); |
| 71 | { |
| 72 | {{src}}_offset_first_element_in_bytes += g_ind_2 * {{src}}_stride_z; |
| 73 | |
Viet-Hoa Do | 3558c58 | 2022-12-16 14:45:57 +0000 | [diff] [blame] | 74 | T_LOAD({{DATA_TYPE}}, M0, N0, {{TENSOR_TYPE}}, {{src}}, g_ind_0, g_ind_1, 1, {{src}}_stride_y, {{src}}); |
Jakub Sujak | 3274172 | 2022-11-25 16:43:18 +0000 | [diff] [blame] | 75 | |
Viet-Hoa Do | 3558c58 | 2022-12-16 14:45:57 +0000 | [diff] [blame] | 76 | T_ACTIVATION({{DATA_TYPE}}, M0, N0, {{ACT}}, {{A_VAL}}, {{B_VAL}}, {{src}}, {{dst}}); |
Jakub Sujak | 3274172 | 2022-11-25 16:43:18 +0000 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | LOOP_UNROLLING(int, i, 0, 1, M0, |
| 80 | { |
| 81 | g_dst_indirect_y[i].v = (uint)min((int)(g_ind_1 + i), (int)({{arg_dst}}_w) - 1); |
| 82 | g_dst_indirect_y[i].v += (int)(g_ind_2 % {{arg_dst}}_h) * (int)({{arg_dst}}_w); |
| 83 | g_dst_indirect_y[i].v += (int)(g_ind_2 / {{arg_dst}}_h) * (int)({{arg_dst}}_w * {{arg_dst}}_h); |
| 84 | }) |
| 85 | )_"; |
| 86 | } |
| 87 | else |
| 88 | { |
| 89 | code += R"_( |
| 90 | // IN/OUT(src, accum) {{src}} |
| 91 | |
| 92 | { |
Viet-Hoa Do | 3558c58 | 2022-12-16 14:45:57 +0000 | [diff] [blame] | 93 | T_ACTIVATION({{DATA_TYPE}}, M0, N0, {{ACT}}, {{A_VAL}}, {{B_VAL}}, {{src}}, {{dst}}); |
Jakub Sujak | 3274172 | 2022-11-25 16:43:18 +0000 | [diff] [blame] | 94 | } |
| 95 | )_"; |
| 96 | } |
| 97 | code += R"_( |
| 98 | //------------------ END KERNEL {{meta_kernel_id}} --------------------- |
| 99 | )_"; |
| 100 | return code; |
| 101 | } |
| 102 | |
| 103 | void ClTemplateActivation::declare_variables(GpuKernelVariableTable &vtable, const ComponentGroup &comp_group) const |
| 104 | { |
| 105 | vtable.declare_variable( |
Viet-Hoa Do | 3558c58 | 2022-12-16 14:45:57 +0000 | [diff] [blame] | 106 | comp_group, |
Jakub Sujak | 3274172 | 2022-11-25 16:43:18 +0000 | [diff] [blame] | 107 | _src, |
| 108 | GpuKernelArgumentInfo(GpuKernelArgumentInfo::Type::Tensor_4D_t_Buffer), |
Jakub Sujak | 3274172 | 2022-11-25 16:43:18 +0000 | [diff] [blame] | 109 | "src"); |
| 110 | |
| 111 | vtable.declare_variable( |
Viet-Hoa Do | 3558c58 | 2022-12-16 14:45:57 +0000 | [diff] [blame] | 112 | comp_group, |
Jakub Sujak | 3274172 | 2022-11-25 16:43:18 +0000 | [diff] [blame] | 113 | _dst, |
| 114 | GpuKernelArgumentInfo(GpuKernelArgumentInfo::Type::Tensor_4D_t_Buffer), |
Jakub Sujak | 3274172 | 2022-11-25 16:43:18 +0000 | [diff] [blame] | 115 | "dst"); |
| 116 | } |
| 117 | |
| 118 | TagLUT ClTemplateActivation::get_tag_lut(const GpuKernelVariableTable &vtable, const ComponentGroup &comp_group) const |
| 119 | { |
| 120 | ARM_COMPUTE_UNUSED(comp_group); |
| 121 | |
| 122 | TagLUT lut{}; |
| 123 | // Arguments and global shared variables |
| 124 | lut["src"] = vtable.get_variable(_src); |
| 125 | lut["dst"] = vtable.get_variable(_dst); |
| 126 | |
Viet-Hoa Do | 04f4620 | 2022-12-14 14:49:56 +0000 | [diff] [blame] | 127 | const auto dst_argument = vtable.get_variable(comp_group.get_any_dst_tensor()); |
Jakub Sujak | 3274172 | 2022-11-25 16:43:18 +0000 | [diff] [blame] | 128 | lut["arg_dst"] = dst_argument.uniq_name; |
| 129 | |
| 130 | // Local build options |
| 131 | lut["meta_kernel_id"] = id(); |
| 132 | lut["DATA_TYPE"] = get_cl_type_from_data_type(_src->data_type()); |
| 133 | lut["TENSOR_TYPE"] = "BUFFER"; |
| 134 | |
| 135 | const auto f_act = lower_string(string_from_activation_func(_attributes.activation())); |
| 136 | |
| 137 | lut["ACT"] = f_act; |
| 138 | lut["A_VAL"] = float_to_string_with_full_precision(_attributes.a()); |
| 139 | lut["B_VAL"] = float_to_string_with_full_precision(_attributes.b()); |
| 140 | |
| 141 | return lut; |
| 142 | } |
| 143 | |
| 144 | CLBuildOptions ClTemplateActivation::get_build_options(const ComponentGroup &comp_group) const |
| 145 | { |
| 146 | /// NOTE: For now tile sizes (n0, m0) are set by the execution window. This may change in the future |
| 147 | const auto root_window = comp_group.get_root_component()->template_writer()->get_window(); |
| 148 | const unsigned int n0 = root_window.x().step(); |
| 149 | const unsigned int m0 = root_window.y().step(); |
| 150 | const unsigned int partial_store_n0 = _dst->dimension(0) % n0; |
| 151 | |
| 152 | CLBuildOptions build_opts; |
| 153 | build_opts.add_option("-DN0=" + support::cpp11::to_string(n0)); |
| 154 | build_opts.add_option("-DM0=" + support::cpp11::to_string(m0)); |
| 155 | build_opts.add_option("-DPARTIAL_N0=" + support::cpp11::to_string(partial_store_n0)); |
| 156 | |
| 157 | return build_opts; |
| 158 | } |
| 159 | |
| 160 | std::string ClTemplateActivation::get_config_id() const |
| 161 | { |
| 162 | std::string config_id{}; |
| 163 | config_id += "activation_"; |
| 164 | config_id += lower_string(string_from_data_type(_src->data_type())); |
| 165 | config_id += "_"; |
| 166 | config_id += support::cpp11::to_string(_src->dimension(0)); |
| 167 | config_id += "_"; |
| 168 | config_id += support::cpp11::to_string(_src->dimension(1)); |
| 169 | return config_id; |
| 170 | } |
| 171 | |
| 172 | std::set<std::string> ClTemplateActivation::get_headers_list() const |
| 173 | { |
| 174 | return std::set<std::string>{ "helpers.h", "tile_helpers.h", "activation_float_helpers.h" }; |
| 175 | } |
| 176 | |
| 177 | Window ClTemplateActivation::get_window() const |
| 178 | { |
| 179 | ARM_COMPUTE_ERROR_ON_MSG(_dst->tensor_shape().total_size() == 0U, "Destination tensor is not initialized"); |
Ramy Elgammal | f800adf | 2022-12-14 15:39:29 +0000 | [diff] [blame] | 180 | const unsigned int n0 = adjust_vec_size(16 / _dst->element_size(), _dst->dimension(0)); |
Jakub Sujak | 3274172 | 2022-11-25 16:43:18 +0000 | [diff] [blame] | 181 | Window win = calculate_max_window(*_dst, Steps(n0)); |
| 182 | return win.collapse(win, Window::DimZ); |
| 183 | } |
| 184 | |
| 185 | } // namespace dynamic_fusion |
| 186 | } // namespace experimental |
| 187 | } // namespace arm_compute |