blob: e33bc7afd799ea26d7e08b83b18d693053da589b [file] [log] [blame]
Manuel Bottini8529bd62018-11-21 11:53:04 +00001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2018-2020 Arm Limited.
Manuel Bottini8529bd62018-11-21 11:53:04 +00003 *
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 */
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010024#include "src/core/CL/kernels/CLGatherKernel.h"
Manuel Bottini8529bd62018-11-21 11:53:04 +000025#include "arm_compute/core/CL/ICLTensor.h"
Manuel Bottini8529bd62018-11-21 11:53:04 +000026#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010027#include "src/core/helpers/AutoConfiguration.h"
28#include "src/core/helpers/WindowHelpers.h"
Matthew Bentham758b5ba2020-03-05 23:37:48 +000029#include "support/StringSupport.h"
Manuel Bottini8529bd62018-11-21 11:53:04 +000030
31#include <string>
32
33namespace arm_compute
34{
35namespace
36{
37inline Status validate_arguments(const ITensorInfo *input, const ITensorInfo *indices, const ITensorInfo *output, int axis)
38{
Manuel Bottini8481d832019-12-10 15:28:40 +000039 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, indices, output);
Manuel Bottini8529bd62018-11-21 11:53:04 +000040 const uint32_t actual_axis = wrap_around(axis, static_cast<int>(input->num_dimensions()));
41 ARM_COMPUTE_RETURN_ERROR_ON(indices->num_dimensions() > 1);
42 ARM_COMPUTE_RETURN_ERROR_ON(input->num_dimensions() > 4);
43 ARM_COMPUTE_RETURN_ERROR_ON(actual_axis >= input->num_dimensions());
Manuel Bottini8481d832019-12-10 15:28:40 +000044 ARM_COMPUTE_RETURN_ERROR_ON(input->data_type() == DataType::UNKNOWN);
Manuel Bottini8529bd62018-11-21 11:53:04 +000045
46 if(output->total_size() != 0)
47 {
48 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
49 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(input, output);
50 TensorShape output_shape = arm_compute::misc::shape_calculator::compute_gather_shape(input->tensor_shape(), indices->tensor_shape(), actual_axis);
51 ARM_COMPUTE_RETURN_ERROR_ON(output_shape.total_size() != output->tensor_shape().total_size());
52 }
53
Manuel Bottini7ad62572019-01-16 11:18:15 +000054 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(indices, 1, DataType::U32, DataType::S32);
Manuel Bottini8529bd62018-11-21 11:53:04 +000055
56 return Status{};
57}
58
59std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *indices, ITensorInfo *output, int axis)
60{
61 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output, indices);
62 const uint32_t actual_axis = wrap_around(axis, static_cast<int>(input->num_dimensions()));
63 // Output auto initialization if not yet initialized
64 TensorShape output_shape = arm_compute::misc::shape_calculator::compute_gather_shape(input->tensor_shape(), indices->tensor_shape(), actual_axis);
65 auto_init_if_empty((*output), output_shape, 1, input->data_type());
66
67 // Create window
68 Window win = calculate_max_window(*output, Steps());
69 output->set_valid_region(ValidRegion(Coordinates(), output->tensor_shape()));
70
71 return std::make_pair(Status{}, win);
72}
73
74} // namespace
75
76CLGatherKernel::CLGatherKernel()
77 : _input(nullptr), _indices(nullptr), _output(nullptr), _axis(0)
78{
79}
80
81void CLGatherKernel::configure(const ICLTensor *input, const ICLTensor *indices, ICLTensor *output, int axis)
82{
Manuel Bottini4c6bd512020-04-08 10:15:51 +010083 configure(CLKernelLibrary::get().get_compile_context(), input, indices, output, axis);
84}
85
Manuel Bottini679fc962020-04-21 16:08:53 +010086void CLGatherKernel::configure(const CLCompileContext &compile_context, const ICLTensor *input, const ICLTensor *indices, ICLTensor *output, int axis)
Manuel Bottini4c6bd512020-04-08 10:15:51 +010087{
Manuel Bottini8529bd62018-11-21 11:53:04 +000088 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output, indices);
Manuel Bottinib6869dd2020-12-16 15:34:25 +000089 auto padding_info = get_padding_info({ input, output, indices });
Manuel Bottini8529bd62018-11-21 11:53:04 +000090 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), indices->info(), output->info(), axis));
91
92 // Configure kernel window
93 auto win_config = validate_and_configure_window(input->info(), indices->info(), output->info(), axis);
94 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
95
96 _input = input;
97 _output = output;
98 _indices = indices;
99 _axis = wrap_around(axis, static_cast<int>(input->info()->num_dimensions()));
100
101 // Set build options
102 CLBuildOptions build_opts;
Sheri Zhang0de45d02020-04-17 14:59:13 +0100103 build_opts.add_option("-DDATA_TYPE=" + get_cl_unsigned_type_from_element_size(data_size_from_type(input->info()->data_type())));
Manuel Bottini8529bd62018-11-21 11:53:04 +0000104 build_opts.add_option("-DOUTPUT_DIM_Z=" + support::cpp11::to_string(output->info()->dimension(2)));
105 build_opts.add_option("-DINPUT_DIM_Z=" + support::cpp11::to_string(input->info()->dimension(2)));
106 build_opts.add_option("-DAXIS=" + support::cpp11::to_string(_axis));
107
108 // Create kernel
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100109 _kernel = create_kernel(compile_context, "gather", build_opts.options());
Manuel Bottini8529bd62018-11-21 11:53:04 +0000110 ICLKernel::configure_internal(win_config.second);
Manuel Bottinib6869dd2020-12-16 15:34:25 +0000111 ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info));
Manuel Bottini8529bd62018-11-21 11:53:04 +0000112}
113
114Status CLGatherKernel::validate(const ITensorInfo *input, const ITensorInfo *indices, const ITensorInfo *output, int axis)
115{
116 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, indices, output, axis));
117 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), indices->clone().get(), output->clone().get(), axis).first);
118 return Status{};
119}
120
121void CLGatherKernel::run(const Window &window, cl::CommandQueue &queue)
122{
123 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
124 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
125
126 Window window_collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimZ);
127 unsigned int idx = 0;
128 add_4D_tensor_argument(idx, _input, window_collapsed);
129 add_1D_tensor_argument(idx, _indices, window_collapsed);
130 add_4D_tensor_argument(idx, _output, window_collapsed);
Georgios Pinitas275f99c2019-08-23 12:44:11 +0100131 enqueue(queue, *this, window_collapsed, lws_hint());
Manuel Bottini8529bd62018-11-21 11:53:04 +0000132}
133} // namespace arm_compute