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