blob: 7d7c3e6673584d3a51a8bc01fbb5535aad2c7520 [file] [log] [blame]
Ramy Elgammal002e6532023-01-11 18:48:04 +00001/*
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
Matthew Bentham314d3e22023-06-23 10:53:52 +000027#include "arm_compute/core/utils/helpers/AdjustVecSize.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010028
Ramy Elgammal002e6532023-01-11 18:48:04 +000029#include "src/core/helpers/WindowHelpers.h"
30#include "src/dynamic_fusion/sketch/gpu/GpuKernelComponentGroup.h"
31#include "support/StringSupport.h"
32
33namespace arm_compute
34{
35namespace experimental
36{
37namespace dynamic_fusion
38{
39ClTemplateLogits1DNorm::ClTemplateLogits1DNorm(ComponentId id,
40 const ArgumentPack<ITensorInfo> &tensors,
41 const Attributes &attributes)
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010042 : IGpuTemplateComponentWriter{id, tensors}, _src{}, _sum{}, _dst{}, _attributes{attributes}
Ramy Elgammal002e6532023-01-11 18:48:04 +000043{
44 _src = this->tensors().get_const_tensor(TensorType::ACL_SRC_0);
45 _sum = this->tensors().get_const_tensor(TensorType::ACL_SRC_1);
46 _dst = this->tensors().get_const_tensor(TensorType::ACL_DST_0);
47 ARM_COMPUTE_ERROR_ON_NULLPTR(_src);
48 ARM_COMPUTE_ERROR_ON_NULLPTR(_sum);
49 ARM_COMPUTE_ERROR_ON_NULLPTR(_dst);
50}
51
52std::string ClTemplateLogits1DNorm::get_name() const
53{
54 return "logits_1d_norm";
55}
56
57std::string ClTemplateLogits1DNorm::get_component_code(const ComponentGroup &comp_group) const
58{
59 ARM_COMPUTE_UNUSED(comp_group);
60
61 std::string code = R"_(
62//------------------ START KERNEL {{meta_kernel_id}} ---------------------
63{
64 const int x_offs = g_ind_0 * sizeof({{DATA_TYPE}});
65 __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;
66 __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;
67 Image sum = CONVERT_TENSOR3D_TO_IMAGE_STRUCT_NO_STEP({{sum}});
68)_";
69 // Load max value of 1D logits vector (row)
70 code += R"_(
71 {{DATA_TYPE}} sum_val = *((__global {{DATA_TYPE}} *)offset(&sum, 0, g_ind_1));
72 VEC_DATA_TYPE({{DATA_TYPE}}, N0)
73 data0 = VLOAD(N0)(0, (__global {{DATA_TYPE}} *)src_addr);
74)_";
75
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010076 if (_attributes.is_log_softmax())
Ramy Elgammal002e6532023-01-11 18:48:04 +000077 {
78 code += R"_(
79 sum_val = log(sum_val);
80 data0 -= sum_val;
81)_";
82 }
83 else
84 {
85 code += R"_(
86 data0 /= sum_val;
87)_";
88 }
89
90 code += R"_(
91 STORE_VECTOR_SELECT(data, {{DATA_TYPE}}, dst_addr, N0, PARTIAL_N0, PARTIAL_N0 != 0 && g_ind_0 == 0);
92}
93//------------------ END KERNEL {{meta_kernel_id}} ---------------------
94)_";
95
96 return code;
97}
98
99void ClTemplateLogits1DNorm::declare_variables(GpuKernelVariableTable &vtable, const ComponentGroup &comp_group) const
100{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100101 vtable.declare_variable(comp_group, _src, GpuKernelArgumentInfo(GpuKernelArgumentInfo::Type::Tensor_3D), "src");
Ramy Elgammal002e6532023-01-11 18:48:04 +0000102
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100103 vtable.declare_variable(comp_group, _sum, GpuKernelArgumentInfo(GpuKernelArgumentInfo::Type::Tensor_3D), "sum");
Ramy Elgammal002e6532023-01-11 18:48:04 +0000104
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100105 vtable.declare_variable(comp_group, _dst, GpuKernelArgumentInfo(GpuKernelArgumentInfo::Type::Tensor_3D), "dst");
Ramy Elgammal002e6532023-01-11 18:48:04 +0000106}
107
108TagLUT ClTemplateLogits1DNorm::get_tag_lut(const GpuKernelVariableTable &vtable, const ComponentGroup &comp_group) const
109{
110 ARM_COMPUTE_UNUSED(comp_group);
111
112 TagLUT lut{};
113
114 // Arguments and global shared variables
115 lut["src"] = vtable.get_variable(_src);
116 lut["sum"] = vtable.get_variable(_sum);
117 lut["dst"] = vtable.get_variable(_dst);
118
119 // Local build options
120 lut["meta_kernel_id"] = id();
121
122 const DataType data_type = _src->data_type();
123
124 lut["DATA_TYPE"] = get_cl_type_from_data_type(data_type);
125
126 return lut;
127}
128
129CLBuildOptions ClTemplateLogits1DNorm::get_build_options(const ComponentGroup &comp_group) const
130{
131 ARM_COMPUTE_UNUSED(comp_group);
132 CLBuildOptions build_opts{};
133
134 const auto root_window = comp_group.get_root_component()->template_writer()->get_window();
135 const unsigned int n0 = root_window.x().step();
136 build_opts.add_option("-DN0=" + support::cpp11::to_string(n0));
137 build_opts.add_option("-DPARTIAL_N0=" + support::cpp11::to_string((_src->dimension(0) % n0)));
138
139 return build_opts;
140}
141
142std::string ClTemplateLogits1DNorm::get_config_id() const
143{
144 std::string config_id = get_name();
145
146 config_id += "_";
147 config_id += support::cpp11::to_string(_src->dimension(0));
148 config_id += "_";
149 config_id += string_from_data_type(_src->data_type());
150
151 return config_id;
152}
153
154std::set<std::string> ClTemplateLogits1DNorm::get_headers_list() const
155{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100156 return std::set<std::string>{"helpers.h", "tile_helpers.h"};
Ramy Elgammal002e6532023-01-11 18:48:04 +0000157}
158
159Window ClTemplateLogits1DNorm::get_window() const
160{
161 ARM_COMPUTE_ERROR_ON_MSG(_dst->tensor_shape().total_size() == 0U, "Destination tensor is not initialized");
162 constexpr unsigned int serial_vector_size = 16;
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100163 const unsigned int vector_size = adjust_vec_size(serial_vector_size, _src->dimension(0));
Ramy Elgammal002e6532023-01-11 18:48:04 +0000164
165 Window win = calculate_max_window(*_src, Steps(vector_size));
166 return win.collapse(win, Window::DimZ);
167}
168
169} // namespace dynamic_fusion
170} // namespace experimental
171} // namespace arm_compute