Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Michalis Spyrou | 702dc0c | 2021-03-19 15:06:07 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2021 Arm Limited. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [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 | */ |
Georgios Pinitas | 7891a73 | 2021-08-20 21:39:25 +0100 | [diff] [blame] | 24 | #include "src/gpu/cl/kernels/ClWeightsReshapeKernel.h" |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 25 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 26 | #include "arm_compute/core/CL/ICLTensor.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 27 | #include "arm_compute/core/Error.h" |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 28 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 29 | |
Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 30 | #include "src/core/helpers/AutoConfiguration.h" |
| 31 | #include "src/core/helpers/WindowHelpers.h" |
Manuel Bottini | d87aded | 2021-07-16 10:23:31 +0100 | [diff] [blame] | 32 | #include "support/Cast.h" |
Matthew Bentham | 758b5ba | 2020-03-05 23:37:48 +0000 | [diff] [blame] | 33 | #include "support/StringSupport.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 34 | |
Vidhya Sudhan Loganathan | 951b8a4 | 2019-11-04 14:42:08 +0000 | [diff] [blame] | 35 | namespace arm_compute |
| 36 | { |
Manuel Bottini | d87aded | 2021-07-16 10:23:31 +0100 | [diff] [blame] | 37 | using namespace misc::shape_calculator; |
| 38 | namespace opencl |
| 39 | { |
| 40 | namespace kernels |
| 41 | { |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 42 | namespace |
| 43 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 44 | Status validate_arguments(const ITensorInfo *input, |
| 45 | const ITensorInfo *biases, |
| 46 | const ITensorInfo *output, |
| 47 | unsigned int num_groups) |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 48 | { |
| 49 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output); |
Vidhya Sudhan Loganathan | 951b8a4 | 2019-11-04 14:42:08 +0000 | [diff] [blame] | 50 | ARM_COMPUTE_RETURN_ERROR_ON(input->data_type() == DataType::UNKNOWN); |
Giorgio Arena | c6aa49b | 2018-08-07 11:53:30 +0100 | [diff] [blame] | 51 | ARM_COMPUTE_RETURN_ERROR_ON(num_groups == 0); |
| 52 | ARM_COMPUTE_RETURN_ERROR_ON(input->data_layout() == DataLayout::NHWC && num_groups > 1); |
| 53 | ARM_COMPUTE_RETURN_ERROR_ON(input->num_dimensions() > 4 && num_groups > 1); |
| 54 | ARM_COMPUTE_RETURN_ERROR_ON((input->dimension(3) % num_groups) != 0); |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 55 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 56 | if (biases != nullptr) |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 57 | { |
Vidhya Sudhan Loganathan | 951b8a4 | 2019-11-04 14:42:08 +0000 | [diff] [blame] | 58 | ARM_COMPUTE_RETURN_ERROR_ON(!is_data_type_float(input->data_type())); |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 59 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, biases); |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 60 | ARM_COMPUTE_RETURN_ERROR_ON((input->num_dimensions() == 4) && (biases->num_dimensions() != 1)); |
| 61 | ARM_COMPUTE_RETURN_ERROR_ON((input->num_dimensions() == 5) && (biases->num_dimensions() != 2)); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 62 | ARM_COMPUTE_RETURN_ERROR_ON((input->num_dimensions() == 4) && |
| 63 | (biases->dimension(0) != input->tensor_shape()[3])); |
| 64 | ARM_COMPUTE_RETURN_ERROR_ON( |
| 65 | (input->num_dimensions() == 5) && |
| 66 | (biases->dimension(0) != input->tensor_shape()[3] || biases->dimension(1) != input->tensor_shape()[4])); |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | // Checks performed when output is configured |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 70 | if (output->total_size() != 0) |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 71 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 72 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS( |
| 73 | output->tensor_shape(), compute_weights_reshaped_shape(*input, biases != nullptr, num_groups)); |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 74 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 75 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(input, output); |
| 76 | } |
| 77 | |
| 78 | return Status{}; |
| 79 | } |
| 80 | } // namespace |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 81 | |
Manuel Bottini | d87aded | 2021-07-16 10:23:31 +0100 | [diff] [blame] | 82 | ClWeightsReshapeKernel::ClWeightsReshapeKernel() |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 83 | { |
Giorgio Arena | 4a95bba | 2021-06-28 11:00:27 +0100 | [diff] [blame] | 84 | _type = CLKernelType::ELEMENTWISE; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 85 | } |
| 86 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 87 | void ClWeightsReshapeKernel::configure(const ClCompileContext &compile_context, |
| 88 | const ITensorInfo *src, |
| 89 | const ITensorInfo *biases, |
| 90 | ITensorInfo *dst, |
| 91 | unsigned int num_groups) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 92 | { |
Manuel Bottini | d87aded | 2021-07-16 10:23:31 +0100 | [diff] [blame] | 93 | ARM_COMPUTE_ERROR_ON_NULLPTR(src, dst); |
Gian Marco Iodice | 5cb4c42 | 2017-06-23 10:38:25 +0100 | [diff] [blame] | 94 | |
| 95 | // Output tensor auto inizialitation if not yet initialized |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 96 | auto_init_if_empty( |
| 97 | *dst, src->clone()->set_tensor_shape(compute_weights_reshaped_shape(*src, (biases != nullptr), num_groups))); |
Gian Marco Iodice | 5cb4c42 | 2017-06-23 10:38:25 +0100 | [diff] [blame] | 98 | |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 99 | // Perform validation step |
Manuel Bottini | d87aded | 2021-07-16 10:23:31 +0100 | [diff] [blame] | 100 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(src, biases, dst, num_groups)); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 101 | auto padding_info = get_padding_info({src, biases, dst}); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 102 | |
Manuel Bottini | d87aded | 2021-07-16 10:23:31 +0100 | [diff] [blame] | 103 | const DataType data_type = src->data_type(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 104 | |
| 105 | // Create build options |
Chunosov | 5124be5 | 2017-11-22 20:42:13 +0700 | [diff] [blame] | 106 | CLBuildOptions build_opts; |
Sheri Zhang | 55c0c0c | 2020-04-14 21:58:37 +0100 | [diff] [blame] | 107 | build_opts.add_option("-DDATA_TYPE=" + get_cl_unsigned_type_from_element_size(data_size_from_type(data_type))); |
Giorgio Arena | c6aa49b | 2018-08-07 11:53:30 +0100 | [diff] [blame] | 108 | build_opts.add_option("-DNUM_GROUPS=" + support::cpp11::to_string(num_groups)); |
Chunosov | 5124be5 | 2017-11-22 20:42:13 +0700 | [diff] [blame] | 109 | build_opts.add_option_if(biases != nullptr, "-DHAS_BIAS"); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 110 | |
| 111 | // Create kernel |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 112 | _kernel = create_kernel(compile_context, "reshape_to_columns", build_opts.options()); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 113 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 114 | // Configure window |
Manuel Bottini | d87aded | 2021-07-16 10:23:31 +0100 | [diff] [blame] | 115 | Window win = calculate_max_window(*src, Steps()); |
Anthony Barbier | b6eb353 | 2018-08-08 13:20:04 +0100 | [diff] [blame] | 116 | ICLKernel::configure_internal(win); |
Sheri Zhang | 11d7327 | 2020-10-28 14:01:55 +0000 | [diff] [blame] | 117 | |
| 118 | ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 119 | } |
| 120 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 121 | Status ClWeightsReshapeKernel::validate(const ITensorInfo *src, |
| 122 | const ITensorInfo *biases, |
| 123 | const ITensorInfo *dst, |
| 124 | unsigned int num_groups) |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 125 | { |
Manuel Bottini | d87aded | 2021-07-16 10:23:31 +0100 | [diff] [blame] | 126 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(src, biases, dst, num_groups)); |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 127 | return Status{}; |
| 128 | } |
| 129 | |
Manuel Bottini | d87aded | 2021-07-16 10:23:31 +0100 | [diff] [blame] | 130 | void ClWeightsReshapeKernel::run_op(ITensorPack &tensors, const Window &window, cl::CommandQueue &queue) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 131 | { |
| 132 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 133 | ARM_COMPUTE_ERROR_ON_MISMATCHING_WINDOWS(ICLKernel::window(), window); |
| 134 | |
Manuel Bottini | d87aded | 2021-07-16 10:23:31 +0100 | [diff] [blame] | 135 | auto src = utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC)); |
| 136 | auto biases = utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_BIAS)); |
| 137 | auto dst = utils::cast::polymorphic_downcast<ICLTensor *>(tensors.get_tensor(TensorType::ACL_DST)); |
| 138 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 139 | Window out_window; |
Manuel Bottini | d87aded | 2021-07-16 10:23:31 +0100 | [diff] [blame] | 140 | out_window.use_tensor_dimensions(dst->info()->tensor_shape()); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 141 | |
| 142 | Window in_slice = window.first_slice_window_3D(); |
| 143 | Window out_slice = out_window.first_slice_window_2D(); |
| 144 | |
| 145 | Window biases_window; |
| 146 | Window biases_slice; |
| 147 | |
Gian Marco Iodice | 916d1bc | 2018-08-13 11:20:41 +0100 | [diff] [blame] | 148 | unsigned int idx = num_arguments_per_3D_tensor() + num_arguments_per_2D_tensor(); |
Manuel Bottini | d87aded | 2021-07-16 10:23:31 +0100 | [diff] [blame] | 149 | idx += (biases != nullptr) ? num_arguments_per_1D_tensor() : 0; |
| 150 | _kernel.setArg<cl_uint>(idx++, src->info()->dimension(0)); |
| 151 | _kernel.setArg<cl_uint>(idx++, src->info()->dimension(1)); |
| 152 | _kernel.setArg<cl_uint>(idx++, src->info()->dimension(2)); |
| 153 | _kernel.setArg<cl_uint>(idx++, src->info()->dimension(3)); |
| 154 | _kernel.setArg<cl_uint>(idx++, dst->info()->strides_in_bytes().z()); |
Gian Marco Iodice | 916d1bc | 2018-08-13 11:20:41 +0100 | [diff] [blame] | 155 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 156 | if (biases != nullptr) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 157 | { |
Manuel Bottini | d87aded | 2021-07-16 10:23:31 +0100 | [diff] [blame] | 158 | biases_window.use_tensor_dimensions(biases->info()->tensor_shape()); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 159 | biases_slice = biases_window.first_slice_window_1D(); |
| 160 | } |
| 161 | |
| 162 | do |
| 163 | { |
| 164 | // Set arguments |
| 165 | unsigned idx = 0; |
Manuel Bottini | d87aded | 2021-07-16 10:23:31 +0100 | [diff] [blame] | 166 | add_3D_tensor_argument(idx, src, in_slice); |
| 167 | add_2D_tensor_argument(idx, dst, out_slice); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 168 | if (biases != nullptr) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 169 | { |
Manuel Bottini | d87aded | 2021-07-16 10:23:31 +0100 | [diff] [blame] | 170 | add_1D_tensor_argument(idx, biases, biases_slice); |
Michalis Spyrou | ebdde65 | 2019-07-08 11:52:46 +0100 | [diff] [blame] | 171 | ARM_COMPUTE_UNUSED(biases_window.slide_window_slice_1D(biases_slice)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | // Run kernel |
Georgios Pinitas | 275f99c | 2019-08-23 12:44:11 +0100 | [diff] [blame] | 175 | enqueue(queue, *this, in_slice, lws_hint()); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 176 | } while (window.slide_window_slice_4D(in_slice) && out_window.slide_window_slice_2D(out_slice)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 177 | } |
Manuel Bottini | d87aded | 2021-07-16 10:23:31 +0100 | [diff] [blame] | 178 | } // namespace kernels |
| 179 | } // namespace opencl |
Vidhya Sudhan Loganathan | 951b8a4 | 2019-11-04 14:42:08 +0000 | [diff] [blame] | 180 | } // namespace arm_compute |