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 | |
| 37 | CLWeightsReshapeKernel::CLWeightsReshapeKernel(bool is_shared) |
| 38 | : _is_shared(is_shared), _input(nullptr), _biases(nullptr), _output(nullptr) |
| 39 | { |
| 40 | } |
| 41 | |
| 42 | void CLWeightsReshapeKernel::configure(const ICLTensor *input, const ICLTensor *biases, ICLTensor *output) |
| 43 | { |
| 44 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F16, DataType::F32); |
| 45 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::F16, DataType::F32); |
| 46 | if(_is_shared) |
| 47 | { |
| 48 | ARM_COMPUTE_ERROR_ON(input->info()->dimension(4) != (output->info()->dimension(2))); |
| 49 | ARM_COMPUTE_ERROR_ON(input->info()->num_dimensions() > 5); |
| 50 | ARM_COMPUTE_ERROR_ON(output->info()->num_dimensions() > 3); |
| 51 | } |
| 52 | else |
| 53 | { |
| 54 | ARM_COMPUTE_ERROR_ON(input->info()->num_dimensions() > 4); |
| 55 | ARM_COMPUTE_ERROR_ON(output->info()->num_dimensions() > 2); |
| 56 | } |
| 57 | |
| 58 | // Check biases |
| 59 | if(biases != nullptr) |
| 60 | { |
| 61 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(biases, 1, DataType::F16, DataType::F32); |
| 62 | } |
| 63 | |
| 64 | _biases = biases; |
| 65 | _output = output; |
| 66 | _input = input; |
| 67 | |
| 68 | // Create build options |
| 69 | std::set<std::string> build_opts; |
| 70 | build_opts.emplace(("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type()))); |
| 71 | build_opts.emplace(((biases != nullptr) ? "-DHAS_BIAS" : "")); |
| 72 | |
| 73 | // Create kernel |
| 74 | _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel("reshape_to_columns", build_opts)); |
| 75 | |
| 76 | // Set static arguments |
| 77 | unsigned int idx = num_arguments_per_3D_tensor() + num_arguments_per_2D_tensor(); |
| 78 | idx += (biases != nullptr) ? num_arguments_per_1D_tensor() : 0; |
| 79 | _kernel.setArg<cl_uint>(idx++, _input->info()->dimension(0)); |
| 80 | _kernel.setArg<cl_uint>(idx++, _input->info()->dimension(1)); |
| 81 | _kernel.setArg<cl_uint>(idx++, _input->info()->dimension(2)); |
| 82 | _kernel.setArg<cl_uint>(idx++, _input->info()->dimension(3)); |
| 83 | |
| 84 | // Configure window |
| 85 | Window win = calculate_max_window(*input->info(), Steps()); |
| 86 | // The CLWeightsReshapeKernel doesn't need padding so update_window_and_padding() can be skipped |
| 87 | output->info()->set_valid_region(ValidRegion(Coordinates(), output->info()->tensor_shape())); |
| 88 | ICLKernel::configure(win); |
| 89 | } |
| 90 | |
| 91 | CLConvolutionLayerWeightsReshapeKernel::CLConvolutionLayerWeightsReshapeKernel() |
| 92 | : CLWeightsReshapeKernel(false) |
| 93 | { |
| 94 | } |
| 95 | |
| 96 | void CLConvolutionLayerWeightsReshapeKernel::run(const Window &window, cl::CommandQueue &queue) |
| 97 | { |
| 98 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 99 | ARM_COMPUTE_ERROR_ON_MISMATCHING_WINDOWS(ICLKernel::window(), window); |
| 100 | |
| 101 | Window out_window; |
| 102 | out_window.use_tensor_dimensions(_output->info()); |
| 103 | |
| 104 | Window in_slice = window.first_slice_window_3D(); |
| 105 | Window out_slice = out_window.first_slice_window_2D(); |
| 106 | |
| 107 | // Set arguments |
| 108 | unsigned idx = 0; |
| 109 | add_3D_tensor_argument(idx, _input, in_slice); |
| 110 | add_2D_tensor_argument(idx, _output, out_slice); |
| 111 | if(_biases != nullptr) |
| 112 | { |
| 113 | Window biases_slice; |
| 114 | biases_slice.set(Window::DimX, Window::Dimension(0, _biases->info()->tensor_shape().x(), 1)); |
| 115 | add_1D_tensor_argument(idx, _biases, biases_slice); |
| 116 | } |
| 117 | |
| 118 | // Run kernel |
| 119 | enqueue(queue, *this, in_slice); |
| 120 | } |
| 121 | |
| 122 | CLLocallyConnectedLayerWeightsReshapeKernel::CLLocallyConnectedLayerWeightsReshapeKernel() |
| 123 | : CLWeightsReshapeKernel(true) |
| 124 | { |
| 125 | } |
| 126 | |
| 127 | void CLLocallyConnectedLayerWeightsReshapeKernel::run(const Window &window, cl::CommandQueue &queue) |
| 128 | { |
| 129 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 130 | ARM_COMPUTE_ERROR_ON_MISMATCHING_WINDOWS(ICLKernel::window(), window); |
| 131 | |
| 132 | Window out_window; |
| 133 | out_window.use_tensor_dimensions(_output->info()); |
| 134 | |
| 135 | Window in_slice = window.first_slice_window_3D(); |
| 136 | Window out_slice = out_window.first_slice_window_2D(); |
| 137 | |
| 138 | Window biases_window; |
| 139 | Window biases_slice; |
| 140 | |
| 141 | if(_biases != nullptr) |
| 142 | { |
| 143 | biases_window.use_tensor_dimensions(_biases->info()); |
| 144 | biases_slice = biases_window.first_slice_window_1D(); |
| 145 | } |
| 146 | |
| 147 | do |
| 148 | { |
| 149 | // Set arguments |
| 150 | unsigned idx = 0; |
| 151 | add_3D_tensor_argument(idx, _input, in_slice); |
| 152 | add_2D_tensor_argument(idx, _output, out_slice); |
| 153 | if(_biases != nullptr) |
| 154 | { |
| 155 | add_1D_tensor_argument(idx, _biases, biases_slice); |
| 156 | biases_window.slide_window_slice_1D(biases_slice); |
| 157 | } |
| 158 | |
| 159 | // Run kernel |
| 160 | enqueue(queue, *this, in_slice); |
| 161 | } |
| 162 | while(window.slide_window_slice_4D(in_slice) && out_window.slide_window_slice_2D(out_slice)); |
| 163 | } |