Teresa Charlin | 91b7f74 | 2021-04-12 13:57:00 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2018-2021 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 | */ |
Georgios Pinitas | 7891a73 | 2021-08-20 21:39:25 +0100 | [diff] [blame] | 24 | #include "src/gpu/cl/kernels/ClConvertFullyConnectedWeightsKernel.h" |
Teresa Charlin | 91b7f74 | 2021-04-12 13:57:00 +0100 | [diff] [blame] | 25 | |
| 26 | #include "arm_compute/core/CL/CLHelpers.h" |
| 27 | #include "arm_compute/core/CL/CLKernelLibrary.h" |
| 28 | #include "arm_compute/core/CL/ICLTensor.h" |
| 29 | #include "arm_compute/core/Helpers.h" |
| 30 | #include "arm_compute/core/Utils.h" |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 31 | |
Teresa Charlin | 91b7f74 | 2021-04-12 13:57:00 +0100 | [diff] [blame] | 32 | #include "src/core/CL/CLValidate.h" |
| 33 | #include "src/core/helpers/AutoConfiguration.h" |
| 34 | #include "src/core/helpers/WindowHelpers.h" |
| 35 | #include "support/Cast.h" |
| 36 | #include "support/StringSupport.h" |
| 37 | |
| 38 | namespace arm_compute |
| 39 | { |
| 40 | namespace opencl |
| 41 | { |
| 42 | namespace kernels |
| 43 | { |
Giorgio Arena | 4a95bba | 2021-06-28 11:00:27 +0100 | [diff] [blame] | 44 | ClConvertFullyConnectedWeightsKernel::ClConvertFullyConnectedWeightsKernel() |
| 45 | { |
| 46 | _type = CLKernelType::ELEMENTWISE; |
| 47 | } |
| 48 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 49 | void ClConvertFullyConnectedWeightsKernel::configure(const CLCompileContext &compile_context, |
| 50 | const ITensorInfo *src, |
| 51 | ITensorInfo *dst, |
| 52 | const TensorShape &original_src_shape, |
| 53 | DataLayout data_layout) |
Teresa Charlin | 91b7f74 | 2021-04-12 13:57:00 +0100 | [diff] [blame] | 54 | { |
| 55 | ARM_COMPUTE_ERROR_ON_NULLPTR(src, dst); |
| 56 | |
| 57 | // Output tensor auto initialisation if not yet initialized |
| 58 | auto_init_if_empty(*dst, *src->clone()); |
| 59 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 60 | auto padding_info = get_padding_info({src, dst}); |
Teresa Charlin | 91b7f74 | 2021-04-12 13:57:00 +0100 | [diff] [blame] | 61 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 62 | ARM_COMPUTE_ERROR_THROW_ON( |
| 63 | ClConvertFullyConnectedWeightsKernel::validate(src, dst, original_src_shape, data_layout)); |
Teresa Charlin | 91b7f74 | 2021-04-12 13:57:00 +0100 | [diff] [blame] | 64 | |
| 65 | const DataLayout src_data_layout = (data_layout == DataLayout::NCHW) ? DataLayout::NHWC : DataLayout::NCHW; |
| 66 | |
| 67 | const int width_idx = get_data_layout_dimension_index(src_data_layout, DataLayoutDimension::WIDTH); |
| 68 | const int height_idx = get_data_layout_dimension_index(src_data_layout, DataLayoutDimension::HEIGHT); |
| 69 | const int channel_idx = get_data_layout_dimension_index(src_data_layout, DataLayoutDimension::CHANNEL); |
| 70 | |
| 71 | const unsigned int num_elems_per_src_plane = original_src_shape[width_idx] * original_src_shape[height_idx]; |
| 72 | const unsigned int num_channels = original_src_shape[channel_idx]; |
| 73 | |
| 74 | const unsigned int factor_1 = (data_layout == DataLayout::NCHW) ? num_elems_per_src_plane : num_channels; |
| 75 | const unsigned int factor_2 = (data_layout == DataLayout::NCHW) ? num_channels : num_elems_per_src_plane; |
| 76 | |
| 77 | // Set build options |
| 78 | CLBuildOptions build_opts; |
| 79 | build_opts.add_option("-DDATA_TYPE=" + get_cl_unsigned_type_from_element_size(src->element_size())); |
| 80 | build_opts.add_option("-DFACTOR_1=" + support::cpp11::to_string(factor_1)); |
| 81 | build_opts.add_option("-DFACTOR_2=" + support::cpp11::to_string(factor_2)); |
| 82 | |
| 83 | // Create kernel |
| 84 | _kernel = create_kernel(compile_context, "convert_fc_weights", build_opts.options()); |
| 85 | |
| 86 | // Configure kernel window |
| 87 | Window win = calculate_max_window(*src, Steps()); |
| 88 | ICLKernel::configure_internal(win); |
| 89 | |
| 90 | ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info)); |
| 91 | } |
| 92 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 93 | Status ClConvertFullyConnectedWeightsKernel::validate(const ITensorInfo *src, |
| 94 | const ITensorInfo *dst, |
| 95 | const TensorShape &original_src_shape, |
| 96 | DataLayout data_layout) |
Teresa Charlin | 91b7f74 | 2021-04-12 13:57:00 +0100 | [diff] [blame] | 97 | { |
| 98 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src, dst); |
| 99 | ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(src); |
| 100 | ARM_COMPUTE_RETURN_ERROR_ON(src->data_type() == DataType::UNKNOWN); |
| 101 | ARM_COMPUTE_RETURN_ERROR_ON(src->num_dimensions() != 2); |
| 102 | ARM_COMPUTE_RETURN_ERROR_ON(src->dimension(1) != original_src_shape.total_size_lower(3)); |
| 103 | ARM_COMPUTE_RETURN_ERROR_ON(data_layout == DataLayout::UNKNOWN); |
| 104 | |
| 105 | // Checks performed when dst is configured |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 106 | if (dst->total_size() != 0) |
Teresa Charlin | 91b7f74 | 2021-04-12 13:57:00 +0100 | [diff] [blame] | 107 | { |
| 108 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src, dst); |
| 109 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(src, dst); |
| 110 | } |
| 111 | |
| 112 | return Status{}; |
| 113 | } |
| 114 | |
| 115 | void ClConvertFullyConnectedWeightsKernel::run_op(ITensorPack &tensors, const Window &window, ::cl::CommandQueue &queue) |
| 116 | { |
| 117 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 118 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window); |
| 119 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 120 | const auto src = |
| 121 | utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC)); |
| 122 | auto dst = utils::cast::polymorphic_downcast<ICLTensor *>(tensors.get_tensor(TensorType::ACL_DST)); |
Teresa Charlin | 91b7f74 | 2021-04-12 13:57:00 +0100 | [diff] [blame] | 123 | ARM_COMPUTE_ERROR_ON_NULLPTR(src, dst); |
| 124 | |
| 125 | unsigned int idx = 0; |
| 126 | add_2D_tensor_argument(idx, src, window); |
| 127 | add_2D_tensor_argument(idx, dst, window); |
| 128 | enqueue(queue, *this, window, lws_hint()); |
| 129 | } |
| 130 | } // namespace kernels |
| 131 | } // namespace opencl |
| 132 | } // namespace arm_compute |