blob: 8b50f1e209d8fc018c802de142b5ac51a47bc4de [file] [log] [blame]
Ramy Elgammalf800adf2022-12-14 15:39:29 +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#include "ClTemplateReshape.h"
Matthew Bentham314d3e22023-06-23 10:53:52 +000025
26#include "arm_compute/core/utils/helpers/AdjustVecSize.h"
27#include "arm_compute/core/utils/StringUtils.h"
Ramy Elgammalf800adf2022-12-14 15:39:29 +000028#include "src/core/helpers/WindowHelpers.h"
29#include "src/dynamic_fusion/sketch/gpu/GpuKernelComponentGroup.h"
30
31namespace arm_compute
32{
33namespace experimental
34{
35namespace dynamic_fusion
36{
37constexpr unsigned int vector_size_byte_opencl = 16;
38
39ClTemplateReshape::ClTemplateReshape(ComponentId id,
40 const ArgumentPack<ITensorInfo> &tensors)
41 : IGpuTemplateComponentWriter{ id, tensors },
42 _src{},
43 _dst{}
44{
45 _src = this->tensors().get_const_tensor(TensorType::ACL_SRC_0);
46 _dst = this->tensors().get_const_tensor(TensorType::ACL_DST_0);
47 ARM_COMPUTE_ERROR_ON_NULLPTR(_src, _dst);
48}
49
50std::string ClTemplateReshape::get_name() const
51{
52 return "reshape";
53}
54
55std::string ClTemplateReshape::get_component_code(const ComponentGroup &comp_group) const
56{
57 ARM_COMPUTE_UNUSED(comp_group);
58 std::string code;
59
60 code = R"_(
61//------------------ START KERNEL {{meta_kernel_id}} ---------------------
62
63// IN(src) {{src}}
64// OUT(dst, accum) {{dst}}
65
66TILE(uint, M0, 1, g_dst_indirect_y);
67{
68 __global uchar * base_src_ptr = {{src}}_ptr + {{src}}_offset_first_element_in_bytes;
69 const int tile_vertical_idx = g_ind_1 * {{arg_dst}}_c + g_ind_2 * {{arg_dst}}_c * {{arg_dst}}_w;
70 LOOP_UNROLLING(int, _m0, 0, 1, M0,
71 {
72 const int row_idx = _m0 * {{arg_dst}}_c + tile_vertical_idx;
73 const int tile_horizontal_idx = g_ind_0 + row_idx;
74 LOOP_UNROLLING(int, _n0, 0, 1, N0,
75 {
76 {{src}}_ptr = base_src_ptr;
77 const int linear_idx = tile_horizontal_idx + _n0;
78 const int in_id_x = linear_idx % {{src}}_c;
79 const int in_id_y = (linear_idx / {{src}}_c) % {{src}}_w;
80 const int in_id_z = linear_idx / ({{src}}_c * {{src}}_w);
81 {{src}}_ptr += in_id_x * sizeof({{DATA_TYPE}}) + in_id_y * {{src}}_stride_y + in_id_z * {{src}}_stride_z;
82 {{dst}}[_m0].s[_n0] = *((__global {{DATA_TYPE}} *){{src}}_ptr);
83 })
84 })
85
86 LOOP_UNROLLING(int, i, 0, 1, M0,
87 {
88 g_dst_indirect_y[i].v = (uint)min((int)(g_ind_1 + i), (int)({{arg_dst}}_w) - 1);
89 g_dst_indirect_y[i].v += (int)(g_ind_2 % {{arg_dst}}_h) * (int)({{arg_dst}}_w);
90 g_dst_indirect_y[i].v += (int)(g_ind_2 / {{arg_dst}}_h) * (int)({{arg_dst}}_w * {{arg_dst}}_h);
91 })
92}
93//------------------ END KERNEL {{meta_kernel_id}} ---------------------
94)_";
95 return code;
96}
97
98void ClTemplateReshape::declare_variables(GpuKernelVariableTable &vtable, const ComponentGroup &comp_group) const
99{
100 vtable.declare_variable(
101 comp_group,
102 _src,
103 GpuKernelArgumentInfo(common_tensor_type), // GpuKernelArgumentInfo::Type::Image_3D
104 "src");
105
106 vtable.declare_variable(
107 comp_group,
108 _dst,
109 GpuKernelArgumentInfo(common_tensor_type),
110 "dst");
111}
112
113TagLUT ClTemplateReshape::get_tag_lut(const GpuKernelVariableTable &vtable, const ComponentGroup &comp_group) const
114{
115 ARM_COMPUTE_UNUSED(comp_group);
116 TagLUT lut{};
117
118 // Arguments and global shared variables
119 lut["src"] = vtable.get_variable(_src);
120 lut["dst"] = vtable.get_variable(_dst);
121 lut["arg_dst"] = vtable.get_variable(comp_group.get_any_dst_tensor());
122 lut["meta_kernel_id"] = id();
123 lut["DATA_TYPE"] = get_cl_type_from_data_type(_dst->data_type());
124
125 return lut;
126}
127
128CLBuildOptions ClTemplateReshape::get_build_options(const ComponentGroup &comp_group) const
129{
130 CLBuildOptions build_opts{};
131 const auto root_window = comp_group.get_root_component()->template_writer()->get_window();
132 const unsigned int n0 = root_window.x().step();
133 const unsigned int m0 = root_window.y().step();
134 const unsigned int partial_store_n0 = _dst->dimension(0) % n0;
135 build_opts.add_option("-DN0=" + support::cpp11::to_string(n0));
136 build_opts.add_option("-DM0=" + support::cpp11::to_string(m0));
137 build_opts.add_option("-DPARTIAL_N0=" + support::cpp11::to_string(partial_store_n0));
138
139 return build_opts;
140}
141
142std::string ClTemplateReshape::get_config_id() const
143{
144 std::string config_id{};
145 config_id += lower_string(string_from_data_type(_dst->data_type()));
146 config_id += "_";
147 config_id += support::cpp11::to_string(_dst->dimension(0));
148 config_id += "_";
149 config_id += support::cpp11::to_string(_dst->dimension(1));
150
151 return config_id;
152}
153
154std::set<std::string> ClTemplateReshape::get_headers_list() const
155{
156 return std::set<std::string>{ "helpers.h", "tile_helpers.h" };
157}
158
159Window ClTemplateReshape::get_window() const
160{
161 ARM_COMPUTE_ERROR_ON_MSG(_dst->tensor_shape().total_size() == 0U, "Destination tensor is not initialized");
162 const unsigned int n0 = adjust_vec_size(vector_size_byte_opencl / _dst->element_size(), _dst->dimension(0));
163 Window win = calculate_max_window(*_dst, Steps(n0));
164 return win.collapse(win, Window::DimZ);
165}
166
167} // namespace dynamic_fusion
168} // namespace experimental
169} // namespace arm_compute