Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017 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 | */ |
| 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" |
| 28 | #include "arm_compute/core/CL/ICLTensor.h" |
| 29 | #include "arm_compute/core/CL/OpenCL.h" |
| 30 | #include "arm_compute/core/Error.h" |
| 31 | #include "arm_compute/core/Helpers.h" |
| 32 | #include "arm_compute/core/Types.h" |
| 33 | #include "arm_compute/core/Validate.h" |
| 34 | |
| 35 | using namespace arm_compute; |
| 36 | |
Gian Marco Iodice | 5cb4c42 | 2017-06-23 10:38:25 +0100 | [diff] [blame] | 37 | CLWeightsReshapeKernel::CLWeightsReshapeKernel() |
| 38 | : _input(nullptr), _biases(nullptr), _output(nullptr) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 39 | { |
| 40 | } |
| 41 | |
| 42 | void CLWeightsReshapeKernel::configure(const ICLTensor *input, const ICLTensor *biases, ICLTensor *output) |
| 43 | { |
Gian Marco | c827990 | 2017-11-27 16:35:28 +0000 | [diff] [blame] | 44 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QS8, DataType::QS16, DataType::QASYMM8, DataType::F16, DataType::F32); |
Gian Marco Iodice | 5cb4c42 | 2017-06-23 10:38:25 +0100 | [diff] [blame] | 45 | ARM_COMPUTE_ERROR_ON_NULLPTR(output); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 46 | |
Gian Marco Iodice | 5cb4c42 | 2017-06-23 10:38:25 +0100 | [diff] [blame] | 47 | const DataType dt = input->info()->data_type(); |
| 48 | const int fixed_point_position = input->info()->fixed_point_position(); |
| 49 | |
| 50 | TensorShape output_shape{ input->info()->tensor_shape() }; |
| 51 | output_shape.collapse(3); |
| 52 | const size_t tmp_dim = output_shape[0]; |
| 53 | output_shape.set(0, output_shape[1]); |
| 54 | output_shape.set(1, tmp_dim + (biases != nullptr ? 1 : 0)); |
| 55 | |
| 56 | // Output tensor auto inizialitation if not yet initialized |
Gian Marco | c827990 | 2017-11-27 16:35:28 +0000 | [diff] [blame] | 57 | auto_init_if_empty(*output->info(), output_shape, 1, dt, fixed_point_position, input->info()->quantization_info()); |
Gian Marco Iodice | 5cb4c42 | 2017-06-23 10:38:25 +0100 | [diff] [blame] | 58 | |
| 59 | ARM_COMPUTE_ERROR_ON_MISMATCHING_DIMENSIONS(output->info()->tensor_shape(), output_shape); |
| 60 | ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
| 61 | ARM_COMPUTE_ERROR_ON_MISMATCHING_FIXED_POINT(input, output); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 62 | |
Gian Marco Iodice | 13edbff | 2017-06-26 17:20:16 +0100 | [diff] [blame] | 63 | if(biases != nullptr) |
| 64 | { |
| 65 | ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, biases); |
| 66 | ARM_COMPUTE_ERROR_ON_MISMATCHING_FIXED_POINT(input, biases); |
| 67 | ARM_COMPUTE_ERROR_ON((input->info()->num_dimensions() == 4) && (biases->info()->num_dimensions() != 1)); |
| 68 | ARM_COMPUTE_ERROR_ON((input->info()->num_dimensions() == 5) && (biases->info()->num_dimensions() != 2)); |
| 69 | ARM_COMPUTE_ERROR_ON((input->info()->num_dimensions() == 4) && (biases->info()->dimension(0) != input->info()->tensor_shape()[3])); |
| 70 | ARM_COMPUTE_ERROR_ON((input->info()->num_dimensions() == 5) && (biases->info()->dimension(0) != input->info()->tensor_shape()[3] || biases->info()->dimension(1) != input->info()->tensor_shape()[4])); |
| 71 | } |
| 72 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 73 | _biases = biases; |
| 74 | _output = output; |
| 75 | _input = input; |
| 76 | |
| 77 | // Create build options |
| 78 | std::set<std::string> build_opts; |
| 79 | build_opts.emplace(("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type()))); |
| 80 | build_opts.emplace(((biases != nullptr) ? "-DHAS_BIAS" : "")); |
Gian Marco Iodice | 3a62324 | 2017-07-25 10:25:53 +0100 | [diff] [blame] | 81 | if(is_data_type_fixed_point(input->info()->data_type())) |
| 82 | { |
| 83 | build_opts.emplace("-DFIXED_POINT_POSITION=" + support::cpp11::to_string(input->info()->fixed_point_position())); |
| 84 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 85 | |
| 86 | // Create kernel |
| 87 | _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel("reshape_to_columns", build_opts)); |
| 88 | |
| 89 | // Set static arguments |
| 90 | unsigned int idx = num_arguments_per_3D_tensor() + num_arguments_per_2D_tensor(); |
| 91 | idx += (biases != nullptr) ? num_arguments_per_1D_tensor() : 0; |
| 92 | _kernel.setArg<cl_uint>(idx++, _input->info()->dimension(0)); |
| 93 | _kernel.setArg<cl_uint>(idx++, _input->info()->dimension(1)); |
| 94 | _kernel.setArg<cl_uint>(idx++, _input->info()->dimension(2)); |
| 95 | _kernel.setArg<cl_uint>(idx++, _input->info()->dimension(3)); |
| 96 | |
| 97 | // Configure window |
| 98 | Window win = calculate_max_window(*input->info(), Steps()); |
| 99 | // The CLWeightsReshapeKernel doesn't need padding so update_window_and_padding() can be skipped |
| 100 | output->info()->set_valid_region(ValidRegion(Coordinates(), output->info()->tensor_shape())); |
| 101 | ICLKernel::configure(win); |
| 102 | } |
| 103 | |
Gian Marco Iodice | 5cb4c42 | 2017-06-23 10:38:25 +0100 | [diff] [blame] | 104 | void CLWeightsReshapeKernel::run(const Window &window, cl::CommandQueue &queue) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 105 | { |
| 106 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 107 | ARM_COMPUTE_ERROR_ON_MISMATCHING_WINDOWS(ICLKernel::window(), window); |
| 108 | |
| 109 | Window out_window; |
SiCong Li | 86b5333 | 2017-08-23 11:02:43 +0100 | [diff] [blame] | 110 | out_window.use_tensor_dimensions(_output->info()->tensor_shape()); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 111 | |
| 112 | Window in_slice = window.first_slice_window_3D(); |
| 113 | Window out_slice = out_window.first_slice_window_2D(); |
| 114 | |
| 115 | Window biases_window; |
| 116 | Window biases_slice; |
| 117 | |
| 118 | if(_biases != nullptr) |
| 119 | { |
SiCong Li | 86b5333 | 2017-08-23 11:02:43 +0100 | [diff] [blame] | 120 | biases_window.use_tensor_dimensions(_biases->info()->tensor_shape()); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 121 | biases_slice = biases_window.first_slice_window_1D(); |
| 122 | } |
| 123 | |
| 124 | do |
| 125 | { |
| 126 | // Set arguments |
| 127 | unsigned idx = 0; |
| 128 | add_3D_tensor_argument(idx, _input, in_slice); |
| 129 | add_2D_tensor_argument(idx, _output, out_slice); |
| 130 | if(_biases != nullptr) |
| 131 | { |
| 132 | add_1D_tensor_argument(idx, _biases, biases_slice); |
| 133 | biases_window.slide_window_slice_1D(biases_slice); |
| 134 | } |
| 135 | |
| 136 | // Run kernel |
| 137 | enqueue(queue, *this, in_slice); |
| 138 | } |
| 139 | while(window.slide_window_slice_4D(in_slice) && out_window.slide_window_slice_2D(out_slice)); |
| 140 | } |