blob: 03f78697bc9cab661e9b1559dc6341c975f5160e [file] [log] [blame]
Gian Marco Iodice781cba72020-06-19 16:56:57 +01001/*
Gian Marco Iodice3cce35d2022-12-30 16:07:45 +00002 * Copyright (c) 2020-2023 Arm Limited.
Gian Marco Iodice781cba72020-06-19 16:56:57 +01003 *
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 */
Matthew Bentham314d3e22023-06-23 10:53:52 +000024#include "src/core/CL/CLUtils.h"
25
26#include "arm_compute/core/utils/ActivationFunctionUtils.h"
SiCongLi1af54162021-10-06 15:25:57 +010027#include "arm_compute/core/CL/CLCompileContext.h"
Jakub Sujak8c49f162023-06-16 09:52:50 +010028#include "arm_compute/core/CL/CLKernelLibrary.h"
29#include "arm_compute/core/CL/ICLTensor.h"
SiCongLi1af54162021-10-06 15:25:57 +010030#include "arm_compute/core/Validate.h"
Matthew Bentham314d3e22023-06-23 10:53:52 +000031#include "arm_compute/core/utils/StringUtils.h"
SiCongLi1af54162021-10-06 15:25:57 +010032#include "support/StringSupport.h"
Gian Marco Iodice781cba72020-06-19 16:56:57 +010033
SiCongLi31778612021-11-12 17:33:45 +000034#include "src/core/experimental/PostOpUtils.h"
Gian Marco Iodice781cba72020-06-19 16:56:57 +010035
SiCongLi1af54162021-10-06 15:25:57 +010036namespace arm_compute
37{
Jakub Sujak8c49f162023-06-16 09:52:50 +010038cl::Image2D create_image2d_from_tensor(const ICLTensor *tensor, CLImage2DType image_type)
Gian Marco Iodice781cba72020-06-19 16:56:57 +010039{
Jakub Sujak8c49f162023-06-16 09:52:50 +010040 ARM_COMPUTE_ERROR_ON_NULLPTR(tensor);
41
42 const cl::Context &ctx = CLKernelLibrary::get().context();
43 const cl::Buffer &buffer = tensor->cl_buffer();
44 const ITensorInfo *info = tensor->info();
45 ARM_COMPUTE_ERROR_ON_MSG(info->lock_paddings(),
46 "Tensor paddings must not be locked to allow extending paddings to satisfy cl_image pitch alignment requirement");
47
48 const size_t image_w{ info->dimension(0) / 4 };
49 const size_t image_h{ info->tensor_shape().total_size() / info->dimension(0) };
50 const size_t max_image_w{ CLKernelLibrary::get().get_device().getInfo<CL_DEVICE_IMAGE2D_MAX_WIDTH>() };
51 const size_t max_image_h{ CLKernelLibrary::get().get_device().getInfo<CL_DEVICE_IMAGE2D_MAX_HEIGHT>() };
52
53 ARM_COMPUTE_UNUSED(max_image_w, max_image_h);
54 ARM_COMPUTE_ERROR_ON_MSG(image_w > max_image_w, "Image width exceeds maximum width for exporting to cl_image");
55 ARM_COMPUTE_ERROR_ON_MSG(image_h > max_image_h, "Image height exceeds maximum height for exporting to cl_image");
56
57 const TensorShape shape2d(image_w, image_h);
58 const size_t image_row_pitch = info->strides_in_bytes()[1];
59
60 return create_image2d_from_buffer(ctx, buffer, shape2d, info->data_type(), image_row_pitch, image_type);
61}
62
63cl::Image2D create_image2d_from_buffer(const cl::Context &ctx, const cl::Buffer &buffer, const TensorShape &shape2d, DataType data_type, size_t image_row_pitch, CLImage2DType image_type)
64{
65 ARM_COMPUTE_ERROR_ON_MSG(!image2d_from_buffer_supported(CLKernelLibrary::get().get_device()),
66 "The extension cl_khr_image2d_from_buffer is not supported on the target platform");
67 ARM_COMPUTE_ERROR_ON_MSG(get_cl_image_pitch_alignment(CLKernelLibrary::get().get_device()) == 0,
68 "Impossible to retrieve the cl_image pitch alignment");
69 ARM_COMPUTE_ERROR_ON_MSG(buffer.get() == nullptr,
70 "Cannot create cl_image from empty cl_buffer");
71
Gian Marco Iodice6f931342020-09-15 14:17:41 +010072 cl_channel_type cl_data_type;
73
74 switch(data_type)
75 {
76 case DataType::F32:
77 cl_data_type = CL_FLOAT;
78 break;
79 case DataType::F16:
80 cl_data_type = CL_HALF_FLOAT;
81 break;
82 default:
83 ARM_COMPUTE_ERROR("Data type not support with OpenCL image2d");
84 }
85
Gian Marco Iodice781cba72020-06-19 16:56:57 +010086 cl_mem cl_image;
87 cl_int err = CL_SUCCESS;
88
Gian Marco Iodice6f931342020-09-15 14:17:41 +010089 const cl_image_format format = { CL_RGBA, cl_data_type };
Gian Marco Iodice781cba72020-06-19 16:56:57 +010090
91 cl_image_desc desc;
92 memset(&desc, 0, sizeof(desc));
93 desc.image_type = CL_MEM_OBJECT_IMAGE2D;
94 desc.mem_object = buffer();
95 desc.image_row_pitch = image_row_pitch;
96 desc.image_width = shape2d[0];
97 desc.image_height = shape2d[1];
98
Jakub Sujak8c49f162023-06-16 09:52:50 +010099 switch(image_type)
Gian Marco Iodice3cce35d2022-12-30 16:07:45 +0000100 {
101 case CLImage2DType::ReadOnly:
102 cl_image = clCreateImage(ctx(), CL_MEM_READ_ONLY, &format, &desc, nullptr, &err);
103 break;
104 case CLImage2DType::WriteOnly:
105 cl_image = clCreateImage(ctx(), CL_MEM_WRITE_ONLY, &format, &desc, nullptr, &err);
106 break;
107 default:
108 ARM_COMPUTE_ERROR("Unsupported CLImage2DType");
109 }
Gian Marco Iodice781cba72020-06-19 16:56:57 +0100110
111 ARM_COMPUTE_UNUSED(err);
112 ARM_COMPUTE_ERROR_ON_MSG(err != CL_SUCCESS, "Error during the creation of CL image from buffer");
113
114 return cl::Image2D(cl_image);
115}
SiCongLi1af54162021-10-06 15:25:57 +0100116
117namespace experimental
118{
119PostOpCLKernelUtils::PostOpCLKernelUtils(const Config &supported_config)
120 : _supported_config(supported_config)
121{
122 ARM_COMPUTE_ERROR_ON_MSG(supported_config.empty(), "Empty PostOp CL kernel support configuration is not allowed");
123 for(auto it = _supported_config.begin(); it != _supported_config.end(); ++it)
124 {
125 auto post_op_sequence = it->first;
126 auto post_op_slots = std::get<1>(it->second);
127 ARM_COMPUTE_ERROR_ON_MSG(post_op_sequence.size() != post_op_slots.size(), "The number of PostOps must be the same as that of the assigned slots");
128 }
129}
130
131bool PostOpCLKernelUtils::are_post_op_shapes_compliant(const ITensorInfo *dst, const experimental::PostOpList<ITensorInfo *> &post_ops)
132{
SiCongLi1af54162021-10-06 15:25:57 +0100133 for(const auto &op : post_ops.get_list())
134 {
135 for(const auto &tensor : op->arguments())
136 {
137 const TensorShape &out_shape = TensorShape::broadcast_shape(dst->tensor_shape(), (*tensor)->tensor_shape());
SiCongLieb8bd812021-10-29 15:05:49 +0100138 // All post ops must be elementwise and must not alter the shape of the original dst tensor after broadcasting
SiCongLi1af54162021-10-06 15:25:57 +0100139 if(detail::have_different_dimensions(out_shape, dst->tensor_shape(), 0))
140 {
141 return false;
142 }
SiCongLieb8bd812021-10-29 15:05:49 +0100143 // NOTE: Kernel limitation: currently only the following broadcasting types are supported:
SiCongLid9287352021-11-03 19:01:22 +0000144 // 1. Post op arg is scalar, broadcast in both first and second dims
145 // 2. Post op arg is of shape: second dim=1, first dim=N, broadcast only in second dim
146 // This means this case: Post op arg is of shape: second dim=M, first dim=1, broadcast only in first dim, is NOT supported
SiCongLieb8bd812021-10-29 15:05:49 +0100147 if(dst->dimension(0) > 1 && dst->dimension(1) > 1 && (*tensor)->dimension(0) == 1 && (*tensor)->dimension(1) > 1)
148 {
149 return false;
150 }
SiCongLi1af54162021-10-06 15:25:57 +0100151 }
152 }
153 return true;
154}
155
156bool PostOpCLKernelUtils::is_post_op_sequence_supported(const PostOpList<ITensorInfo *> &post_ops) const
157{
158 if(post_ops.size() == 0)
159 {
160 return true; // Always support cases where no post op is specified
161 }
162 const auto post_op_sequence = get_post_op_sequence(post_ops);
163
164 return _supported_config.find(post_op_sequence) != _supported_config.end();
165}
166
167void PostOpCLKernelUtils::set_post_ops_cl_build_options(CLBuildOptions &build_opts, const PostOpList<ITensorInfo *> &post_ops) const
168{
169 const auto post_op_sequence = get_post_op_sequence(post_ops);
170 const auto slots = std::get<1>(_supported_config.at(post_op_sequence));
171 for(size_t post_op_id = 0; post_op_id < post_ops.size(); ++post_op_id)
172 {
173 const auto &post_op = post_ops.get_list().at(post_op_id);
174 const auto slot_prefix = "-DP" + support::cpp11::to_string(slots[post_op_id]);
175 if(post_op->type() == experimental::PostOpType::Activation)
176 {
177 const auto _post_op = utils::cast::polymorphic_downcast<const experimental::PostOpAct<ITensorInfo *> *>(post_op.get());
178 const auto act_type = slot_prefix + "_ACTIVATION_TYPE=" + lower_string(string_from_activation_func(_post_op->_act_info.activation()));
179 const auto act_a_val = slot_prefix + "_ACTIVATION_A_VAL=" + float_to_string_with_full_precision(_post_op->_act_info.a());
180 const auto act_b_val = slot_prefix + "_ACTIVATION_B_VAL=" + float_to_string_with_full_precision(_post_op->_act_info.b());
181 build_opts.add_option(act_type);
182 build_opts.add_option(act_a_val);
183 build_opts.add_option(act_b_val);
184 }
185 else if(post_op->type() == experimental::PostOpType::Eltwise_Add)
186 {
187 size_t arg_id = 1;
188 const auto eltwise_op = slot_prefix + "_ELTWISE_OP=ADD" + "_X_POS_" + support::cpp11::to_string(post_op->prev_dst_pos());
189 build_opts.add_option(eltwise_op);
190 for(const auto &tensor : post_op->arguments())
191 {
192 const auto height = slot_prefix + "_ELTWISE_ARG" + support::cpp11::to_string(arg_id) + "_HEIGHT=" + support::cpp11::to_string((*tensor)->dimension(1));
193 const auto width = slot_prefix + "_ELTWISE_ARG" + support::cpp11::to_string(arg_id) + "_WIDTH=" + support::cpp11::to_string((*tensor)->dimension(0));
194 build_opts.add_option(height);
195 build_opts.add_option(width);
196 ++arg_id;
197 }
198 }
ramelg016049eda2021-10-29 10:52:53 +0100199 else if(post_op->type() == experimental::PostOpType::Eltwise_PRelu)
200 {
201 size_t arg_id = 1;
202 const auto eltwise_op = slot_prefix + "_ELTWISE_OP=PRELU" + "_X_POS_" + support::cpp11::to_string(post_op->prev_dst_pos());
203 build_opts.add_option(eltwise_op);
204 for(const auto &tensor : post_op->arguments())
205 {
206 const auto height = slot_prefix + "_ELTWISE_ARG" + support::cpp11::to_string(arg_id) + "_HEIGHT=" + support::cpp11::to_string((*tensor)->dimension(1));
207 const auto width = slot_prefix + "_ELTWISE_ARG" + support::cpp11::to_string(arg_id) + "_WIDTH=" + support::cpp11::to_string((*tensor)->dimension(0));
208 build_opts.add_option(height);
209 build_opts.add_option(width);
210 ++arg_id;
211 }
212 }
SiCongLi1af54162021-10-06 15:25:57 +0100213 }
214}
215
216void PostOpCLKernelUtils::set_post_ops_cl_kernel_name(std::string &kernel_name, const PostOpList<ITensorInfo *> &post_ops) const
217{
218 const auto post_op_sequence = get_post_op_sequence(post_ops);
219 const auto postfix = std::get<0>(_supported_config.at(post_op_sequence));
220 kernel_name += postfix;
221}
222} // namespace experimental
223
Gian Marco Iodice3cce35d2022-12-30 16:07:45 +0000224} // namespace arm_compute