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