Manuel Bottini | 8529bd6 | 2018-11-21 11:53:04 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2018-2019 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/CLGatherKernel.h" |
| 25 | |
| 26 | #include "arm_compute/core/AccessWindowStatic.h" |
| 27 | #include "arm_compute/core/CL/CLHelpers.h" |
| 28 | #include "arm_compute/core/CL/CLKernelLibrary.h" |
| 29 | #include "arm_compute/core/CL/CLValidate.h" |
| 30 | #include "arm_compute/core/CL/ICLTensor.h" |
| 31 | #include "arm_compute/core/CL/OpenCL.h" |
| 32 | #include "arm_compute/core/Helpers.h" |
| 33 | #include "arm_compute/core/IAccessWindow.h" |
| 34 | #include "arm_compute/core/TensorInfo.h" |
| 35 | #include "arm_compute/core/Window.h" |
| 36 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
| 37 | |
| 38 | #include <string> |
| 39 | |
| 40 | namespace arm_compute |
| 41 | { |
| 42 | namespace |
| 43 | { |
| 44 | inline Status validate_arguments(const ITensorInfo *input, const ITensorInfo *indices, const ITensorInfo *output, int axis) |
| 45 | { |
| 46 | const uint32_t actual_axis = wrap_around(axis, static_cast<int>(input->num_dimensions())); |
| 47 | ARM_COMPUTE_RETURN_ERROR_ON(indices->num_dimensions() > 1); |
| 48 | ARM_COMPUTE_RETURN_ERROR_ON(input->num_dimensions() > 4); |
| 49 | ARM_COMPUTE_RETURN_ERROR_ON(actual_axis >= input->num_dimensions()); |
| 50 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(output); |
| 51 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8, DataType::S8, DataType::QASYMM8, |
| 52 | DataType::U16, DataType::S16, |
| 53 | DataType::U32, DataType::S32, DataType::F16, DataType::F32); |
| 54 | |
| 55 | if(output->total_size() != 0) |
| 56 | { |
| 57 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
| 58 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(input, output); |
| 59 | TensorShape output_shape = arm_compute::misc::shape_calculator::compute_gather_shape(input->tensor_shape(), indices->tensor_shape(), actual_axis); |
| 60 | ARM_COMPUTE_RETURN_ERROR_ON(output_shape.total_size() != output->tensor_shape().total_size()); |
| 61 | } |
| 62 | |
| 63 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(indices, 1, DataType::U32); |
| 64 | |
| 65 | return Status{}; |
| 66 | } |
| 67 | |
| 68 | std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *indices, ITensorInfo *output, int axis) |
| 69 | { |
| 70 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output, indices); |
| 71 | const uint32_t actual_axis = wrap_around(axis, static_cast<int>(input->num_dimensions())); |
| 72 | // Output auto initialization if not yet initialized |
| 73 | TensorShape output_shape = arm_compute::misc::shape_calculator::compute_gather_shape(input->tensor_shape(), indices->tensor_shape(), actual_axis); |
| 74 | auto_init_if_empty((*output), output_shape, 1, input->data_type()); |
| 75 | |
| 76 | // Create window |
| 77 | Window win = calculate_max_window(*output, Steps()); |
| 78 | output->set_valid_region(ValidRegion(Coordinates(), output->tensor_shape())); |
| 79 | |
| 80 | return std::make_pair(Status{}, win); |
| 81 | } |
| 82 | |
| 83 | } // namespace |
| 84 | |
| 85 | CLGatherKernel::CLGatherKernel() |
| 86 | : _input(nullptr), _indices(nullptr), _output(nullptr), _axis(0) |
| 87 | { |
| 88 | } |
| 89 | |
| 90 | void CLGatherKernel::configure(const ICLTensor *input, const ICLTensor *indices, ICLTensor *output, int axis) |
| 91 | { |
| 92 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output, indices); |
| 93 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), indices->info(), output->info(), axis)); |
| 94 | |
| 95 | // Configure kernel window |
| 96 | auto win_config = validate_and_configure_window(input->info(), indices->info(), output->info(), axis); |
| 97 | ARM_COMPUTE_ERROR_THROW_ON(win_config.first); |
| 98 | |
| 99 | _input = input; |
| 100 | _output = output; |
| 101 | _indices = indices; |
| 102 | _axis = wrap_around(axis, static_cast<int>(input->info()->num_dimensions())); |
| 103 | |
| 104 | // Set build options |
| 105 | CLBuildOptions build_opts; |
| 106 | build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type())); |
| 107 | build_opts.add_option("-DOUTPUT_DIM_Z=" + support::cpp11::to_string(output->info()->dimension(2))); |
| 108 | build_opts.add_option("-DINPUT_DIM_Z=" + support::cpp11::to_string(input->info()->dimension(2))); |
| 109 | build_opts.add_option("-DAXIS=" + support::cpp11::to_string(_axis)); |
| 110 | |
| 111 | // Create kernel |
| 112 | _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel("gather", build_opts.options())); |
| 113 | ICLKernel::configure_internal(win_config.second); |
| 114 | } |
| 115 | |
| 116 | Status CLGatherKernel::validate(const ITensorInfo *input, const ITensorInfo *indices, const ITensorInfo *output, int axis) |
| 117 | { |
| 118 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, indices, output, axis)); |
| 119 | ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), indices->clone().get(), output->clone().get(), axis).first); |
| 120 | return Status{}; |
| 121 | } |
| 122 | |
| 123 | void CLGatherKernel::run(const Window &window, cl::CommandQueue &queue) |
| 124 | { |
| 125 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 126 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window); |
| 127 | |
| 128 | Window window_collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimZ); |
| 129 | unsigned int idx = 0; |
| 130 | add_4D_tensor_argument(idx, _input, window_collapsed); |
| 131 | add_1D_tensor_argument(idx, _indices, window_collapsed); |
| 132 | add_4D_tensor_argument(idx, _output, window_collapsed); |
| 133 | enqueue(queue, *this, window_collapsed); |
| 134 | } |
| 135 | } // namespace arm_compute |