Imported Upstream version 1.8.0
[platform/core/ml/nnfw.git] / compute / ARMComputeEx / src / core / CL / kernels / CLGatherExKernel.cpp
1 /*
2  * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 /*
18  * Copyright (c) 2016-2018 ARM Limited.
19  *
20  * SPDX-License-Identifier: MIT
21  *
22  * Permission is hereby granted, free of charge, to any person obtaining a copy
23  * of this software and associated documentation files (the "Software"), to
24  * deal in the Software without restriction, including without limitation the
25  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
26  * sell copies of the Software, and to permit persons to whom the Software is
27  * furnished to do so, subject to the following conditions:
28  *
29  * The above copyright notice and this permission notice shall be included in all
30  * copies or substantial portions of the Software.
31  *
32  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
33  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
34  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
35  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
36  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
37  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
38  * SOFTWARE.
39  */
40
41 #include "arm_compute/core/CL/kernels/CLGatherExKernel.h"
42
43 #include "arm_compute/core/CL/CLHelpers.h"
44 #include "arm_compute/core/CL/CLKernelLibraryEx.h"
45 #include "arm_compute/core/CL/ICLTensor.h"
46 #include "arm_compute/core/utils/misc/ShapeCalculatorEx.h"
47 #include "arm_compute/core/UtilsEx.h"
48 #include "support/StringSupport.h"
49
50 using namespace arm_compute;
51
52 namespace
53 {
54
55 inline Status validate_arguments(const ITensorInfo *input, const ITensorInfo *indices,
56                                  const ITensorInfo *output, int axis)
57 {
58   const uint32_t actual_axis = wrap_around(axis, static_cast<int>(input->num_dimensions()));
59   ARM_COMPUTE_RETURN_ERROR_ON(indices->num_dimensions() > 3);
60   ARM_COMPUTE_RETURN_ERROR_ON(input->num_dimensions() > 4);
61   ARM_COMPUTE_ERROR_ON(input->num_dimensions() + indices->num_dimensions() - 1 > 4);
62   ARM_COMPUTE_RETURN_ERROR_ON(actual_axis >= input->num_dimensions());
63   ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(output);
64   ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(
65       input, 1, DataType::U8, DataType::S8, DataType::QASYMM8, DataType::U16, DataType::S16,
66       DataType::U32, DataType::S32, DataType::F16, DataType::F32);
67
68   if (output->total_size() != 0)
69   {
70     ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
71     ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(input, output);
72     TensorShape output_shape = arm_compute::misc::shape_calculator::compute_gather_shape_ex(
73         input->tensor_shape(), indices->tensor_shape(), actual_axis);
74     ARM_COMPUTE_RETURN_ERROR_ON(output_shape.total_size() != output->tensor_shape().total_size());
75   }
76
77   ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(indices, 1, DataType::U32, DataType::S32);
78
79   return Status{};
80 }
81
82 std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *indices,
83                                                         ITensorInfo *output, int axis)
84 {
85   ARM_COMPUTE_ERROR_ON_NULLPTR(input, output, indices);
86   const uint32_t actual_axis = wrap_around(axis, static_cast<int>(input->num_dimensions()));
87   std::unique_ptr<ITensorInfo> output_info = input->clone();
88   output_info->set_tensor_shape(arm_compute::misc::shape_calculator::compute_gather_shape_ex(
89       input->tensor_shape(), indices->tensor_shape(), actual_axis));
90   // Output auto initialization if not yet initialized
91   auto_init_if_empty((*output), output_info->tensor_shape(), 1, input->data_type());
92
93   // Create window
94   Window win = calculate_max_window(*output, Steps());
95   output->set_valid_region(ValidRegion(Coordinates(), output->tensor_shape()));
96
97   return std::make_pair(Status{}, win);
98 }
99
100 } // namespace
101
102 CLGatherExKernel::CLGatherExKernel()
103     : _input(nullptr), _indices(nullptr), _output(nullptr), _axis(0)
104 {
105 }
106
107 void CLGatherExKernel::configure(const ICLTensor *input, const ICLTensor *indices,
108                                  ICLTensor *output, int axis)
109 {
110   ARM_COMPUTE_ERROR_ON_NULLPTR(input, output, indices);
111   ARM_COMPUTE_ERROR_THROW_ON(
112       validate_arguments(input->info(), indices->info(), output->info(), axis));
113
114   // Configure kernel window
115   auto win_config =
116       validate_and_configure_window(input->info(), indices->info(), output->info(), axis);
117   ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
118
119   _input = input;
120   _output = output;
121   _indices = indices;
122   _axis = wrap_around(axis, static_cast<int>(input->info()->num_dimensions()));
123
124   // Set build options
125   CLBuildOptions build_opts;
126   build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type()));
127   build_opts.add_option("-DOUTPUT_DIM_Z=" +
128                         support::cpp11::to_string(output->info()->dimension(2)));
129   build_opts.add_option("-DINPUT_DIM_Z=" + support::cpp11::to_string(input->info()->dimension(2)));
130   build_opts.add_option("-DAXIS=" + support::cpp11::to_string(_axis));
131   build_opts.add_option("-DINDICES_DIM=" +
132                         support::cpp11::to_string(indices->info()->num_dimensions()));
133
134   // Create kernel
135   _kernel = static_cast<cl::Kernel>(
136       CLKernelLibraryEx::get().create_kernel("gather_ex", build_opts.options()));
137   ICLKernel::configure_internal(win_config.second);
138 }
139
140 Status CLGatherExKernel::validate(const ITensorInfo *input, const ITensorInfo *indices,
141                                   const ITensorInfo *output, int axis)
142 {
143   ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, indices, output, axis));
144   ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(),
145                                                             indices->clone().get(),
146                                                             output->clone().get(), axis)
147                                   .first);
148   return Status{};
149 }
150
151 void CLGatherExKernel::run(const Window &window, cl::CommandQueue &queue)
152 {
153   ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
154   ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
155
156   Window window_collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimZ, 4);
157   unsigned int idx = 0;
158   add_4D_tensor_argument(idx, _input, window_collapsed);
159   add_3D_tensor_argument(idx, _indices, window_collapsed);
160   add_4D_tensor_argument(idx, _output, window_collapsed);
161   enqueue(queue, *this, window_collapsed, lws_hint());
162 }