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 
25 #include "src/dynamic_fusion/sketch/gpu/template_writer/cl/ClTemplateLogits1DNorm.h"
26 
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 {
ClTemplateLogits1DNorm(ComponentId id,const ArgumentPack<ITensorInfo> & tensors,const Attributes & attributes)37 ClTemplateLogits1DNorm::ClTemplateLogits1DNorm(ComponentId                      id,
38                                                const ArgumentPack<ITensorInfo> &tensors,
39                                                const Attributes                &attributes)
40     : IGpuTemplateComponentWriter{ id, tensors },
41       _src{},
42       _sum{},
43       _dst{},
44       _attributes{ attributes }
45 {
46     _src = this->tensors().get_const_tensor(TensorType::ACL_SRC_0);
47     _sum = this->tensors().get_const_tensor(TensorType::ACL_SRC_1);
48     _dst = this->tensors().get_const_tensor(TensorType::ACL_DST_0);
49     ARM_COMPUTE_ERROR_ON_NULLPTR(_src);
50     ARM_COMPUTE_ERROR_ON_NULLPTR(_sum);
51     ARM_COMPUTE_ERROR_ON_NULLPTR(_dst);
52 }
53 
get_name() const54 std::string ClTemplateLogits1DNorm::get_name() const
55 {
56     return "logits_1d_norm";
57 }
58 
get_component_code(const ComponentGroup & comp_group) const59 std::string ClTemplateLogits1DNorm::get_component_code(const ComponentGroup &comp_group) const
60 {
61     ARM_COMPUTE_UNUSED(comp_group);
62 
63     std::string code = R"_(
64 //------------------ START KERNEL {{meta_kernel_id}} ---------------------
65 {
66     const int x_offs = g_ind_0 * sizeof({{DATA_TYPE}});
67     __global uchar *src_addr = {{src}}_ptr + {{src}}_offset_first_element_in_bytes + x_offs + g_ind_1 * {{src}}_stride_y + g_ind_2 * {{src}}_stride_z;
68     __global uchar *dst_addr = {{dst}}_ptr + {{dst}}_offset_first_element_in_bytes + x_offs + g_ind_1 * {{dst}}_stride_y + g_ind_2 * {{dst}}_stride_z;
69     Image sum = CONVERT_TENSOR3D_TO_IMAGE_STRUCT_NO_STEP({{sum}});
70 )_";
71     // Load max value of 1D logits vector (row)
72     code += R"_(
73     {{DATA_TYPE}} sum_val = *((__global {{DATA_TYPE}} *)offset(&sum, 0, g_ind_1));
74     VEC_DATA_TYPE({{DATA_TYPE}}, N0)
75     data0 = VLOAD(N0)(0, (__global {{DATA_TYPE}} *)src_addr);
76 )_";
77 
78     if(_attributes.is_log_softmax())
79     {
80         code += R"_(
81     sum_val = log(sum_val);
82     data0 -= sum_val;
83 )_";
84     }
85     else
86     {
87         code += R"_(
88     data0 /= sum_val;
89 )_";
90     }
91 
92     code += R"_(
93     STORE_VECTOR_SELECT(data, {{DATA_TYPE}}, dst_addr, N0, PARTIAL_N0, PARTIAL_N0 != 0 && g_ind_0 == 0);
94 }
95 //------------------ END KERNEL {{meta_kernel_id}} ---------------------
96 )_";
97 
98     return code;
99 }
100 
declare_variables(GpuKernelVariableTable & vtable,const ComponentGroup & comp_group) const101 void ClTemplateLogits1DNorm::declare_variables(GpuKernelVariableTable &vtable, const ComponentGroup &comp_group) const
102 {
103     vtable.declare_variable(
104         comp_group,
105         _src,
106         GpuKernelArgumentInfo(GpuKernelArgumentInfo::Type::Tensor_3D),
107         "src");
108 
109     vtable.declare_variable(
110         comp_group,
111         _sum,
112         GpuKernelArgumentInfo(GpuKernelArgumentInfo::Type::Tensor_3D),
113         "sum");
114 
115     vtable.declare_variable(
116         comp_group,
117         _dst,
118         GpuKernelArgumentInfo(GpuKernelArgumentInfo::Type::Tensor_3D),
119         "dst");
120 }
121 
get_tag_lut(const GpuKernelVariableTable & vtable,const ComponentGroup & comp_group) const122 TagLUT ClTemplateLogits1DNorm::get_tag_lut(const GpuKernelVariableTable &vtable, const ComponentGroup &comp_group) const
123 {
124     ARM_COMPUTE_UNUSED(comp_group);
125 
126     TagLUT lut{};
127 
128     // Arguments and global shared variables
129     lut["src"] = vtable.get_variable(_src);
130     lut["sum"] = vtable.get_variable(_sum);
131     lut["dst"] = vtable.get_variable(_dst);
132 
133     // Local build options
134     lut["meta_kernel_id"] = id();
135 
136     const DataType data_type = _src->data_type();
137 
138     lut["DATA_TYPE"] = get_cl_type_from_data_type(data_type);
139 
140     return lut;
141 }
142 
get_build_options(const ComponentGroup & comp_group) const143 CLBuildOptions ClTemplateLogits1DNorm::get_build_options(const ComponentGroup &comp_group) const
144 {
145     ARM_COMPUTE_UNUSED(comp_group);
146     CLBuildOptions build_opts{};
147 
148     const auto         root_window = comp_group.get_root_component()->template_writer()->get_window();
149     const unsigned int n0          = root_window.x().step();
150     build_opts.add_option("-DN0=" + support::cpp11::to_string(n0));
151     build_opts.add_option("-DPARTIAL_N0=" + support::cpp11::to_string((_src->dimension(0) % n0)));
152 
153     return build_opts;
154 }
155 
get_config_id() const156 std::string ClTemplateLogits1DNorm::get_config_id() const
157 {
158     std::string config_id = get_name();
159 
160     config_id += "_";
161     config_id += support::cpp11::to_string(_src->dimension(0));
162     config_id += "_";
163     config_id += string_from_data_type(_src->data_type());
164 
165     return config_id;
166 }
167 
get_headers_list() const168 std::set<std::string> ClTemplateLogits1DNorm::get_headers_list() const
169 {
170     return std::set<std::string>{ "helpers.h", "tile_helpers.h" };
171 }
172 
get_window() const173 Window ClTemplateLogits1DNorm::get_window() const
174 {
175     ARM_COMPUTE_ERROR_ON_MSG(_dst->tensor_shape().total_size() == 0U, "Destination tensor is not initialized");
176     constexpr unsigned int serial_vector_size = 16;
177     const unsigned int vector_size = adjust_vec_size(serial_vector_size, _src->dimension(0));
178 
179     Window win = calculate_max_window(*_src, Steps(vector_size));
180     return win.collapse(win, Window::DimZ);
181 }
182 
183 } // namespace dynamic_fusion
184 } // namespace experimental
185 } // namespace arm_compute
186