Gian Marco Iodice | 76335eb | 2022-11-17 11:03:39 +0000 | [diff] [blame] | 1 | /* |
Gian Marco Iodice | 3cce35d | 2022-12-30 16:07:45 +0000 | [diff] [blame] | 2 | * Copyright (c) 2022-2023 Arm Limited. |
Gian Marco Iodice | 76335eb | 2022-11-17 11:03:39 +0000 | [diff] [blame] | 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 "src/gpu/cl/kernels/ClIndirectConv2dKernel.h" |
| 25 | |
Matthew Bentham | 314d3e2 | 2023-06-23 10:53:52 +0000 | [diff] [blame] | 26 | #include "arm_compute/core/utils/ActivationFunctionUtils.h" |
Gian Marco Iodice | 76335eb | 2022-11-17 11:03:39 +0000 | [diff] [blame] | 27 | #include "arm_compute/core/CL/CLKernelLibrary.h" |
| 28 | #include "arm_compute/core/CL/ICLTensor.h" |
| 29 | #include "arm_compute/core/KernelDescriptors.h" |
Matthew Bentham | 314d3e2 | 2023-06-23 10:53:52 +0000 | [diff] [blame] | 30 | #include "arm_compute/core/utils/helpers/AdjustVecSize.h" |
Gian Marco Iodice | 76335eb | 2022-11-17 11:03:39 +0000 | [diff] [blame] | 31 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
Matthew Bentham | 314d3e2 | 2023-06-23 10:53:52 +0000 | [diff] [blame] | 32 | #include "arm_compute/core/utils/StringUtils.h" |
Gian Marco Iodice | 76335eb | 2022-11-17 11:03:39 +0000 | [diff] [blame] | 33 | #include "src/core/CL/CLUtils.h" |
| 34 | #include "src/core/CL/CLValidate.h" |
| 35 | #include "src/core/helpers/AutoConfiguration.h" |
| 36 | #include "src/core/helpers/WindowHelpers.h" |
| 37 | #include "src/gpu/cl/kernels/gemm/ClGemmHelpers.h" |
| 38 | #include "support/Cast.h" |
| 39 | #include "support/StringSupport.h" |
| 40 | |
| 41 | namespace arm_compute |
| 42 | { |
| 43 | namespace opencl |
| 44 | { |
| 45 | namespace kernels |
| 46 | { |
| 47 | namespace |
| 48 | { |
| 49 | Status validate_arguments(const ITensorInfo *src, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *indirect_buffer, const ITensorInfo *dst, |
| 50 | const PadStrideInfo &conv_info, const ActivationLayerInfo &act_info, const DirectConvComputeKernelInfo &desc) |
| 51 | { |
| 52 | ARM_COMPUTE_UNUSED(act_info); |
| 53 | ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(src); |
| 54 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src, 1, DataType::F16, DataType::F32); |
| 55 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(indirect_buffer, 1, DataType::S32); |
| 56 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_LAYOUT_NOT_IN(src, DataLayout::NHWC); |
| 57 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src, weights); |
| 58 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(indirect_buffer->tensor_shape(), |
| 59 | misc::shape_calculator::compute_indirect_buffer_shape(src->tensor_shape(), |
| 60 | src->data_layout(), |
| 61 | weights->tensor_shape(), |
| 62 | conv_info, |
| 63 | desc)); |
| 64 | |
| 65 | constexpr int channel_idx = 0; |
| 66 | constexpr int batch_idx = 3; |
| 67 | |
| 68 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(weights->dimension(channel_idx) != src->dimension(channel_idx), "Weights feature map dimension should match the respective src's one"); |
| 69 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(weights->num_dimensions() > 4, "Weights can be at most 4 dimensional"); |
| 70 | |
| 71 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(desc.m0 <= 0 || desc.m0 > 8, "M0 can only be greater than 0 and less than or equal to 8"); |
| 72 | |
| 73 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(desc.n0 != 1 && desc.n0 != 2 && desc.n0 != 3 && desc.n0 != 4 && desc.n0 != 8 && desc.n0 != 16, |
| 74 | "N0 can only be: 1, 2, 3, 4, 8, and 16"); |
| 75 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(desc.k0 != 1 && desc.k0 != 2 && desc.k0 != 3 && desc.k0 != 4 && desc.k0 != 8 && desc.k0 != 16, |
| 76 | "K0 can only be: 1, 2, 3, 4, 8, and 16"); |
| 77 | |
| 78 | if(desc.export_weights_to_cl_image) |
| 79 | { |
| 80 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(desc.k0 != 4 && desc.k0 != 8 && desc.k0 != 16, |
| 81 | "K0 can only be: 4, 8, and 16"); |
| 82 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(!export_to_cl_image(weights), |
| 83 | "Export to CLImage is not supported for this weight configuration"); |
| 84 | } |
| 85 | |
| 86 | if(biases != nullptr) |
| 87 | { |
| 88 | if(is_data_type_quantized_asymmetric(src->data_type())) |
| 89 | { |
| 90 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(biases, 1, DataType::S32); |
| 91 | } |
| 92 | else |
| 93 | { |
| 94 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(weights, biases); |
| 95 | } |
| 96 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(biases->dimension(channel_idx) != weights->dimension(batch_idx), |
| 97 | "Biases size and number of dst feature maps should match"); |
| 98 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(biases->num_dimensions() > 1, |
| 99 | "Biases should be one dimensional"); |
| 100 | } |
| 101 | |
| 102 | // Checks performed when dst is configured |
| 103 | if(dst->total_size() != 0) |
| 104 | { |
| 105 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(dst->tensor_shape(), |
| 106 | misc::shape_calculator::compute_deep_convolution_shape(*src, *weights, conv_info)); |
| 107 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src, dst); |
| 108 | } |
| 109 | |
| 110 | return Status{}; |
| 111 | } |
| 112 | } // namespace |
| 113 | |
| 114 | ClIndirectConv2dKernel::ClIndirectConv2dKernel() |
| 115 | { |
| 116 | _type = CLKernelType::DIRECT; |
| 117 | } |
| 118 | |
| 119 | void ClIndirectConv2dKernel::configure(const CLCompileContext &compile_context, ITensorInfo *src, ITensorInfo *weights, ITensorInfo *biases, ITensorInfo *indirect_buffer, ITensorInfo *dst, |
| 120 | const PadStrideInfo &conv_info, const ActivationLayerInfo &act_info, const DirectConvComputeKernelInfo &desc) |
| 121 | { |
| 122 | ARM_COMPUTE_ERROR_ON_NULLPTR(src, weights, indirect_buffer, dst); |
| 123 | |
| 124 | // Perform validation |
| 125 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(src, weights, biases, indirect_buffer, dst, conv_info, act_info, desc)); |
| 126 | |
| 127 | constexpr unsigned int channel_idx = 0; |
| 128 | constexpr unsigned int width_idx = 1; |
| 129 | constexpr unsigned int height_idx = 2; |
| 130 | const unsigned int kernel_width = weights->dimension(width_idx); |
| 131 | const unsigned int kernel_height = weights->dimension(height_idx); |
| 132 | const DataType data_type = src->data_type(); |
| 133 | |
| 134 | const GPUTarget gpu_target = get_target(); |
| 135 | |
| 136 | // Get dst shape |
| 137 | TensorShape output_shape = misc::shape_calculator::compute_deep_convolution_shape(*src, *weights, conv_info); |
| 138 | |
| 139 | // Output auto inizialitation if not yet initialized |
| 140 | auto_init_if_empty(*dst, output_shape, |
| 141 | 1, |
| 142 | src->data_type(), |
| 143 | src->quantization_info()); |
| 144 | |
| 145 | // Configure kernel window |
| 146 | Window win; |
| 147 | output_shape.collapse(2U, 1U); |
| 148 | const unsigned int n0 = adjust_vec_size(desc.n0, output_shape[0]); |
| 149 | const unsigned int m0 = adjust_vec_size(desc.m0, output_shape[1]); |
| 150 | const unsigned int k0 = adjust_vec_size(desc.k0, src->dimension(channel_idx)); |
| 151 | |
| 152 | const unsigned int partial_store_n0 = dst->dimension(channel_idx) % n0; |
| 153 | |
| 154 | // Create window and update padding |
| 155 | win = calculate_max_window(output_shape, Steps(n0, m0)); |
| 156 | |
| 157 | ICLKernel::configure_internal(win); |
| 158 | |
| 159 | std::stringstream kernel_name; |
| 160 | CLBuildOptions build_options; |
| 161 | |
| 162 | kernel_name << "indirect_convolution_nhwc"; |
| 163 | |
| 164 | _export_to_cl_image = desc.export_weights_to_cl_image; |
| 165 | |
| 166 | // Update the padding for the weights tensor if we can export to cl_image |
| 167 | if(_export_to_cl_image) |
| 168 | { |
| 169 | gemm::update_padding_for_cl_image(weights); |
| 170 | } |
| 171 | |
| 172 | // Add padding to indirect buffer to avoid out-of-bound reads |
| 173 | // When M0 is 5, 6, and 7, we use vload8 to fetch the data from the buffer |
| 174 | const unsigned int load_indirect_buf_size = m0 > 4 ? 8 : m0; |
| 175 | const unsigned int indirect_buf_width = indirect_buffer->tensor_shape()[0]; |
| 176 | const unsigned int round_up_width = ((indirect_buf_width + load_indirect_buf_size - 1) / load_indirect_buf_size) * load_indirect_buf_size; |
| 177 | const unsigned int padding = round_up_width - indirect_buf_width; |
| 178 | indirect_buffer->extend_padding(PaddingSize(0, indirect_buffer->padding().right + padding, 0, 0)); |
| 179 | |
| 180 | if(biases != nullptr) |
| 181 | { |
| 182 | build_options.add_option(std::string("-DHAS_BIAS")); |
| 183 | build_options.add_option(std::string("-DBIA_DATA_TYPE=" + get_cl_type_from_data_type(biases->data_type()))); |
| 184 | } |
| 185 | |
| 186 | // Conditions of -cl-fast-relaxed-math causing accuracy issues can be traced from COMPMID-5324 |
| 187 | const auto act_function = act_info.activation(); |
| 188 | |
| 189 | if((gpu_target != GPUTarget::G71 && (gpu_target & GPUTarget::GPU_ARCH_MASK) == GPUTarget::BIFROST) |
| 190 | && (act_function == ActivationLayerInfo::ActivationFunction::BOUNDED_RELU || act_function == ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU) |
| 191 | && (data_type == DataType::F32 || data_type == DataType::F16)) |
| 192 | { |
| 193 | // -cl-fast-relaxed-math also sets -cl-finite-math-only and -cl-unsafe-math-optimizations |
| 194 | // to disable -cl-finite-math-only, we only include -cl-unsafe-math-optimizations |
| 195 | build_options.add_option("-cl-unsafe-math-optimizations"); |
| 196 | } |
| 197 | else |
| 198 | { |
| 199 | build_options.add_option("-cl-fast-relaxed-math"); |
| 200 | } |
| 201 | |
| 202 | build_options.add_option("-DSRC_TENSOR_TYPE=BUFFER"); |
| 203 | build_options.add_option("-DSRC_DATA_TYPE=" + get_cl_type_from_data_type(data_type)); |
| 204 | build_options.add_option("-DSRC_CHANNELS=" + support::cpp11::to_string(src->dimension(channel_idx))); |
| 205 | build_options.add_option("-DOFF_TENSOR_TYPE=BUFFER"); |
| 206 | build_options.add_option("-DDST_WIDTH=" + support::cpp11::to_string(dst->dimension(width_idx))); |
| 207 | build_options.add_option("-DDST_HEIGHT=" + support::cpp11::to_string(dst->dimension(height_idx))); |
| 208 | build_options.add_option("-DDST_TENSOR_TYPE=BUFFER"); |
| 209 | build_options.add_option("-DDST_DATA_TYPE=" + get_cl_type_from_data_type(data_type)); |
| 210 | build_options.add_option_if_else(_export_to_cl_image, "-DWEI_TENSOR_TYPE=IMAGE", "-DWEI_TENSOR_TYPE=BUFFER"); |
| 211 | build_options.add_option("-DWEI_WIDTH=" + support::cpp11::to_string(kernel_width)); |
| 212 | build_options.add_option("-DWEI_HEIGHT=" + support::cpp11::to_string(kernel_height)); |
| 213 | build_options.add_option("-DWEI_DATA_TYPE=" + get_cl_type_from_data_type(data_type)); |
| 214 | build_options.add_option("-DN0=" + support::cpp11::to_string(n0)); |
| 215 | build_options.add_option("-DM0=" + support::cpp11::to_string(m0)); |
| 216 | build_options.add_option("-DK0=" + support::cpp11::to_string(k0)); |
| 217 | build_options.add_option("-DPARTIAL_N0=" + support::cpp11::to_string(partial_store_n0)); |
| 218 | build_options.add_option("-DIND_BUFF_VEC_SIZE=" + support::cpp11::to_string(load_indirect_buf_size)); |
| 219 | build_options.add_option_if((src->dimension(channel_idx) % k0) != 0, "-DLEFTOVER_LOOP"); |
| 220 | build_options.add_option("-DACTIVATION_TYPE=" + lower_string(string_from_activation_func(act_function))); |
| 221 | build_options.add_option_if(act_info.enabled(), "-DA_VAL=" + float_to_string_with_full_precision(act_info.a())); |
| 222 | build_options.add_option_if(act_info.enabled(), "-DB_VAL=" + float_to_string_with_full_precision(act_info.b())); |
| 223 | |
| 224 | // A macro guard to compile ONLY the kernel of interest |
| 225 | build_options.add_option("-D" + upper_string(kernel_name.str())); |
| 226 | |
| 227 | if(compile_context.get_ddk_version() >= 30) |
| 228 | { |
| 229 | build_options.add_option("-fregister-allocation=64"); |
| 230 | } |
| 231 | |
| 232 | _kernel = create_kernel(compile_context, kernel_name.str(), build_options.options()); |
| 233 | |
| 234 | // Set config_id for enabling LWS tuning |
| 235 | _config_id = kernel_name.str(); |
| 236 | _config_id += "_"; |
| 237 | _config_id += lower_string(string_from_data_type(data_type)); |
| 238 | _config_id += "_"; |
| 239 | _config_id += support::cpp11::to_string(kernel_width); |
| 240 | _config_id += "_"; |
| 241 | _config_id += support::cpp11::to_string(kernel_height); |
| 242 | _config_id += "_"; |
| 243 | _config_id += support::cpp11::to_string(src->dimension(width_idx)); |
| 244 | _config_id += "_"; |
| 245 | _config_id += support::cpp11::to_string(src->dimension(height_idx)); |
| 246 | _config_id += "_"; |
| 247 | _config_id += support::cpp11::to_string(src->dimension(channel_idx)); |
| 248 | _config_id += "_"; |
| 249 | _config_id += support::cpp11::to_string(dst->dimension(width_idx)); |
| 250 | _config_id += "_"; |
| 251 | _config_id += support::cpp11::to_string(dst->dimension(height_idx)); |
| 252 | _config_id += "_"; |
| 253 | _config_id += support::cpp11::to_string(dst->dimension(channel_idx)); |
| 254 | } |
| 255 | |
| 256 | Status ClIndirectConv2dKernel::validate(const ITensorInfo *src, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *indirect_buffer, const ITensorInfo *dst, |
| 257 | const PadStrideInfo &conv_info, const ActivationLayerInfo &act_info, const DirectConvComputeKernelInfo &desc) |
| 258 | { |
| 259 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(src, weights, biases, indirect_buffer, dst, conv_info, act_info, desc)); |
| 260 | return Status{}; |
| 261 | } |
| 262 | |
| 263 | void ClIndirectConv2dKernel::run_op(ITensorPack &tensors, const Window &window, cl::CommandQueue &queue) |
| 264 | { |
| 265 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 266 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window); |
| 267 | |
| 268 | // Get initial windows |
| 269 | Window slice = window.first_slice_window_3D(); |
| 270 | |
| 271 | const auto src = utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC_0)); |
| 272 | const auto weights = utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC_1)); |
| 273 | const auto biases = utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC_2)); |
| 274 | const auto indirect_buffer = utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC_3)); |
| 275 | auto dst = utils::cast::polymorphic_downcast<ICLTensor *>(tensors.get_tensor(TensorType::ACL_DST)); |
| 276 | |
| 277 | cl::Image2D weights_cl_image; |
| 278 | |
| 279 | if(_export_to_cl_image) |
| 280 | { |
| 281 | const size_t image_w = weights->info()->dimension(0) / 4; |
| 282 | const size_t image_h = weights->info()->dimension(1) * weights->info()->dimension(2) * weights->info()->dimension(3); |
| 283 | const TensorShape shape2d(image_w, image_h); |
| 284 | const size_t image_row_pitch = weights->info()->strides_in_bytes()[1]; |
| 285 | |
| 286 | // Export cl_buffer to cl_image |
Gian Marco Iodice | 3cce35d | 2022-12-30 16:07:45 +0000 | [diff] [blame] | 287 | weights_cl_image = create_image2d_from_buffer(CLKernelLibrary::get().context(), weights->cl_buffer(), shape2d, weights->info()->data_type(), image_row_pitch, CLImage2DType::ReadOnly); |
Gian Marco Iodice | 76335eb | 2022-11-17 11:03:39 +0000 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | unsigned int idx = 0; |
| 291 | add_4d_tensor_nhwc_argument(idx, src); |
| 292 | add_4d_tensor_nhwc_argument(idx, indirect_buffer); |
| 293 | add_4d_tensor_nhwc_argument(idx, dst); |
| 294 | if(_export_to_cl_image) |
| 295 | { |
| 296 | _kernel.setArg(idx++, weights_cl_image); |
| 297 | } |
| 298 | add_4d_tensor_nhwc_argument(idx, weights); |
| 299 | if(biases != nullptr) |
| 300 | { |
| 301 | add_1D_tensor_argument(idx, biases, slice); |
| 302 | } |
| 303 | enqueue(queue, *this, slice, lws_hint()); |
| 304 | } |
| 305 | } // namespace kernels |
| 306 | } // namespace opencl |
| 307 | } // namespace arm_compute |