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