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