Giorgio Arena | 657bdb3 | 2018-04-26 18:52:01 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2018 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/CLConvertFullyConnectedWeightsKernel.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" |
Giorgio Arena | 657bdb3 | 2018-04-26 18:52:01 +0100 | [diff] [blame] | 29 | #include "arm_compute/core/CL/ICLTensor.h" |
| 30 | #include "arm_compute/core/Helpers.h" |
| 31 | #include "arm_compute/core/Types.h" |
| 32 | |
| 33 | namespace arm_compute |
| 34 | { |
| 35 | CLConvertFullyConnectedWeightsKernel::CLConvertFullyConnectedWeightsKernel() |
| 36 | : _input(nullptr), _output(nullptr) |
| 37 | { |
| 38 | } |
| 39 | |
| 40 | void CLConvertFullyConnectedWeightsKernel::configure(const ICLTensor *input, ICLTensor *output, const TensorShape &original_input_shape, |
| 41 | DataLayout data_layout) |
| 42 | { |
| 43 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); |
| 44 | ARM_COMPUTE_ERROR_THROW_ON(CLConvertFullyConnectedWeightsKernel::validate(input->info(), output->info(), original_input_shape, data_layout)); |
| 45 | |
| 46 | _input = input; |
| 47 | _output = output; |
| 48 | |
| 49 | const unsigned int num_elems_per_input_plane = original_input_shape.x() * original_input_shape.y(); |
| 50 | const unsigned int num_channels = original_input_shape.z(); |
| 51 | |
| 52 | // Set build options |
| 53 | CLBuildOptions build_opts; |
| 54 | build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type())); |
| 55 | if(data_layout == DataLayout::NCHW) |
| 56 | { |
| 57 | build_opts.add_option("-DFACTOR_1=" + support::cpp11::to_string(num_elems_per_input_plane)); |
| 58 | build_opts.add_option("-DFACTOR_2=" + support::cpp11::to_string(num_channels)); |
| 59 | } |
| 60 | else |
| 61 | { |
| 62 | build_opts.add_option("-DFACTOR_1=" + support::cpp11::to_string(num_channels)); |
| 63 | build_opts.add_option("-DFACTOR_2=" + support::cpp11::to_string(num_elems_per_input_plane)); |
| 64 | } |
| 65 | |
| 66 | // Create kernel |
| 67 | _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel("convert_fc_weights", build_opts.options())); |
| 68 | |
| 69 | // Configure kernel window |
| 70 | Window win = calculate_max_window(*input->info(), Steps()); |
| 71 | ICLKernel::configure(win); |
| 72 | } |
| 73 | |
| 74 | Status CLConvertFullyConnectedWeightsKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const TensorShape &original_input_shape, |
| 75 | DataLayout data_layout) |
| 76 | { |
Vidhya Sudhan Loganathan | f1f4906 | 2018-05-25 13:21:26 +0100 | [diff] [blame] | 77 | ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input); |
Giorgio Arena | 657bdb3 | 2018-04-26 18:52:01 +0100 | [diff] [blame] | 78 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8, DataType::S8, DataType::QS8, DataType::QASYMM8, DataType::U16, DataType::S16, DataType::QS16, DataType::U32, DataType::S32, |
| 79 | DataType::QS32, DataType::F16, DataType::F32); |
| 80 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
| 81 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, output); |
| 82 | ARM_COMPUTE_RETURN_ERROR_ON(input->num_dimensions() != 2); |
| 83 | ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(1) != original_input_shape.total_size_lower(3)); |
| 84 | ARM_COMPUTE_RETURN_ERROR_ON(data_layout == DataLayout::UNKNOWN); |
| 85 | |
| 86 | return Status{}; |
| 87 | } |
| 88 | |
| 89 | void CLConvertFullyConnectedWeightsKernel::run(const Window &window, cl::CommandQueue &queue) |
| 90 | { |
| 91 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 92 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window); |
| 93 | |
| 94 | unsigned int idx = 0; |
| 95 | add_2D_tensor_argument(idx, _input, window); |
| 96 | add_2D_tensor_argument(idx, _output, window); |
| 97 | enqueue(queue, *this, window); |
| 98 | } |
| 99 | } // namespace arm_compute |