blob: bbdf8df0a3ab84e8cc85905cb3b81c6c0b06685f [file] [log] [blame]
Giorgio Arena232c4522022-03-03 10:09:01 +00001/*
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#if defined(ENABLE_EXPERIMENTAL_DYNAMIC_FUSION)
25
26#include "src/core/experimental/dynamic_fusion/ClKernelBuildingImpl/components/ClElementwiseAddKernelComponent.h"
Gunes Bayir8a879832022-03-10 21:21:01 +000027#include "arm_compute/core/Validate.h"
28#include "src/core/helpers/AutoConfiguration.h"
29#include "src/core/helpers/WindowHelpers.h"
Giorgio Arena232c4522022-03-03 10:09:01 +000030
31namespace arm_compute
32{
33namespace experimental
34{
35namespace dynamic_fusion
36{
37ComponentType ClElementwiseAddKernelComponent::get_component_type() const
38{
39 return ComponentType::Simple;
40}
41
42std::set<std::string> ClElementwiseAddKernelComponent::get_headers_list() const
43{
44 return std::set<std::string> { "gemm_helpers.h", "repeat.h" };
45}
46
Gunes Bayir8a879832022-03-10 21:21:01 +000047Window ClElementwiseAddKernelComponent::get_window() const
48{
49 const ITensorInfo *lhs_info = _blueprint->impl().get_kernel_argument_info(_lhs.arg_id);
50 const ITensorInfo *rhs_info = _blueprint->impl().get_kernel_argument_info(_rhs.arg_id);
51 ITensorInfo *dst_info = _blueprint->impl().get_kernel_argument_info(_blueprint->impl().get_dst_id());
52
53 ARM_COMPUTE_ERROR_ON_NULLPTR(lhs_info, rhs_info, dst_info);
54
55 const std::pair<TensorShape, ValidRegion> broadcast_pair = ITensorInfo::broadcast_shape_and_valid_region(*lhs_info, *rhs_info);
56 const TensorShape &out_shape = broadcast_pair.first;
57
58 auto_init_if_empty(*dst_info, out_shape, 1, lhs_info->data_type());
59
60 const unsigned int vector_size_byte_opencl = 16;
61 const unsigned int num_elems_processed_per_iteration = adjust_vec_size(vector_size_byte_opencl / dst_info->element_size(), dst_info->dimension(0));
62 Window win = calculate_max_window(*dst_info, Steps(num_elems_processed_per_iteration));
63
64 return win;
65}
66
Giorgio Arena232c4522022-03-03 10:09:01 +000067std::string ClElementwiseAddKernelComponent::get_component_code() const
68{
69 std::string code;
70 return R"_(
71 //------------------ START KERNEL {{meta_kernel_id}} ELTWISE_ADD ---------------------
72 // IN_0(Accumulator) {{acc}}
73 // IN_1(Addend) {{addend}}
74
75 // c = addend + c (mix-precision, broadcast, boundary aware)
76 {
77 __global uchar *addend_addr = {{addend}}_ptr + {{addend}}_offset_first_element_in_bytes + (get_global_id(0) * (uint)N0 * sizeof(DATA_TYPE)) + (COMPUTE_M0_START_ROW(g_y, M0, PARTIAL_STORE_M0) * {{addend}}_stride_y) + get_global_id(2) * {{addend}}_stride_z; \
78 LOAD_BLOCK_BOUNDARY_AWARE(M0, N0, DATA_TYPE, addend, addend_addr, 0, {{addend}}_stride_y, g_zero, PARTIAL_LOAD_M0, PARTIAL_LOAD_N0, PARTIAL_COND_Y, PARTIAL_COND_X); \
79 MIXED_PRECISION_ELTWISE_OP_BLOCK(ADD_X_POS_0, M0, N0, {{acc}}, addend, DATA_TYPE_ACCUMULATOR, addend_hp);
80 }
81 //------------------ END KERNEL {{meta_kernel_id}} ELTWISE_ADD ---------------------
82
83)_";
84}
Giorgio Arenabd44caa2022-03-15 13:45:15 +000085
86CLBuildOptions ClElementwiseAddKernelComponent::generate_build_options() const
87{
88 auto t_dst_info = _blueprint->impl().get_kernel_argument_info(_blueprint->impl().get_dst_id());
89 auto tile_info = _blueprint->impl().get_tile_info();
90
91 CLBuildOptions build_opts{};
92
93 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(t_dst_info->data_type()));
94 build_opts.add_option("-DM0=" + support::cpp11::to_string(tile_info.tile_dims.y()));
95 build_opts.add_option("-DN0=" + support::cpp11::to_string(tile_info.tile_dims.x()));
96 build_opts.add_option("-DPARTIAL_STORE_M0=" + support::cpp11::to_string(tile_info.boundaries.y() % tile_info.tile_dims.y()));
97
98 return build_opts;
99}
100
Giorgio Arena232c4522022-03-03 10:09:01 +0000101ClElementwiseAddKernelComponent::TagLUT ClElementwiseAddKernelComponent::allocate_vars(SharedVarTable &vtable) const
102{
103 // Determine which argument is the accumulator
104 Link accumulator;
105 Link addend;
106 if(_lhs.group == SharedVarGroup::Automatic)
107 {
108 accumulator = _lhs;
109 addend = _rhs;
110 }
111 else if(_rhs.group == SharedVarGroup::Automatic)
112 {
113 accumulator = _rhs;
114 addend = _lhs;
115 }
116 else
117 {
118 ARM_COMPUTE_ERROR("Invalid elementwise component linking");
119 }
120 return {
121 { "meta_kernel_id", id() },
122 { "acc", vtable.add(accumulator, ClKernelArgRuntimeDescriptor(accumulator.arg_id, TensorArgType::Image_3D), "add_acc") },
123 { "addend", vtable.add(addend, ClKernelArgRuntimeDescriptor(addend.arg_id, TensorArgType::Image_3D), "add_addend") },
124 // {"dst", vtable.add(_dst, ClKernelArgRuntimeDescriptor(_dst.arg_id, TensorArgType::Image_3D), "dst")}, // dst is needed for the root version and/or non-inplace version should we need one
125 };
126}
127} // namespace dynamic_fusion
128} // namespace experimental
129} // namespace arm_compute
130
131#endif // defined(ENABLE_EXPERIMENTAL_DYNAMIC_FUSION)