Imported Upstream version 1.8.0
[platform/core/ml/nnfw.git] / compute / ARMComputeEx / src / core / CL / kernels / CLMultiplyScaleFactorKernel.cpp
1 /*
2  * Copyright (c) 2020 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) 2017-2019 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/CLMultiplyScaleFactorKernel.h"
42
43 #include "arm_compute/core/AccessWindowStatic.h"
44 #include "arm_compute/core/CL/CLHelpers.h"
45 #include "arm_compute/core/CL/CLKernelLibraryEx.h"
46 #include "arm_compute/core/CL/CLValidate.h"
47 #include "arm_compute/core/CL/ICLTensor.h"
48 #include "arm_compute/core/TensorInfo.h"
49 #include "arm_compute/core/Utils.h"
50 #include "arm_compute/core/Validate.h"
51 #include "arm_compute/core/Window.h"
52 #include "support/StringSupport.h"
53
54 using namespace arm_compute;
55
56 namespace
57 {
58 Status validate_arguments(const ITensorInfo *input, const ITensorInfo *scale_factor,
59                           const ITensorInfo *output)
60 {
61   ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
62   ARM_COMPUTE_RETURN_ERROR_ON(input->num_dimensions() > 2);
63   ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::S32);
64   ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(scale_factor, 1, DataType::F16,
65                                                        DataType::F32);
66   ARM_COMPUTE_RETURN_ERROR_ON(scale_factor->tensor_shape().total_size() == 0);
67   ARM_COMPUTE_RETURN_ERROR_ON(scale_factor->num_dimensions() > 1);
68   ARM_COMPUTE_RETURN_ERROR_ON(scale_factor->dimension(0) != input->dimension(1));
69   ARM_COMPUTE_RETURN_ERROR_ON(output->tensor_shape().total_size() == 0);
70   ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::F16, DataType::F32);
71   ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, output);
72
73   // Checks performed when output is configured
74   if ((output->total_size() != 0))
75   {
76     ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, output);
77   }
78
79   return Status{};
80 }
81
82 std::tuple<Status, Window> validate_and_configure_window(const ITensorInfo *input,
83                                                          ITensorInfo *output)
84 {
85   // Configure kernel window
86   Window win = calculate_max_window(*input, Steps());
87
88   // Output tensor auto initialization if not yet initialized
89   auto_init_if_empty(*output, input->tensor_shape(), 1, DataType::F32);
90
91   // CLMultiplyScaleFactorKernel doesn't need padding so update_window_and_padding() can be
92   // skipped
93   Coordinates coord;
94   coord.set_num_dimensions(output->num_dimensions());
95   output->set_valid_region(ValidRegion(coord, output->tensor_shape()));
96
97   return std::make_tuple(Status{}, win);
98 }
99 } // namespace
100
101 CLMultiplyScaleFactorKernel::CLMultiplyScaleFactorKernel()
102     : _input(nullptr), _scale_factor(nullptr), _output(nullptr), _multiplier(1.f)
103 {
104 }
105
106 void CLMultiplyScaleFactorKernel::configure(const ICLTensor *input, const ICLTensor *scale_factor,
107                                             ICLTensor *output, float multiplier)
108 {
109   ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
110   ARM_COMPUTE_ERROR_THROW_ON(
111       validate_arguments(input->info(), scale_factor->info(), output->info()));
112
113   _input = input;
114   _scale_factor = scale_factor;
115   _output = output;
116   _multiplier = multiplier;
117
118   const int vec_size_x = 16 / output->info()->element_size();
119   const int output_width_x = output->info()->tensor_shape().x();
120   const bool multi_access_x = (output_width_x / vec_size_x > 0);
121
122   // Create and update the window (if needed)
123   Window win = calculate_max_window(*output->info());
124   if (multi_access_x)
125   {
126     win.set(Window::DimX,
127             Window::Dimension(win.x().start(), ceil_to_multiple(win.x().end(), vec_size_x),
128                               vec_size_x));
129   }
130   ICLKernel::configure_internal(win);
131
132   // Create kernel
133   CLBuildOptions build_opts;
134   build_opts.add_option("-DVEC_SIZE=" + support::cpp11::to_string(vec_size_x));
135   build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(output->info()->data_type()));
136   build_opts.add_option_if(
137       multi_access_x, "-DLAST_ACCESSED_X=" +
138                           support::cpp11::to_string(std::max<int>(output_width_x - vec_size_x, 0)));
139
140   _kernel = static_cast<cl::Kernel>(
141       CLKernelLibraryEx::get().create_kernel("multiply_scale_factor", build_opts.options()));
142 }
143
144 Status CLMultiplyScaleFactorKernel::validate(const ITensorInfo *input,
145                                              const ITensorInfo *scale_factor,
146                                              const ITensorInfo *output)
147 {
148   ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, scale_factor, output));
149   ARM_COMPUTE_RETURN_ON_ERROR(
150       std::get<0>(validate_and_configure_window(input->clone().get(), output->clone().get())));
151   return Status{};
152 }
153
154 void CLMultiplyScaleFactorKernel::run(const Window &window, cl::CommandQueue &queue)
155 {
156   ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
157   ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
158
159   Window window_collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimZ);
160   Window slice = window_collapsed.first_slice_window_2D();
161
162   // Set scale_factor window
163   Window win_scale = calculate_max_window(*_scale_factor->info(), Steps());
164
165   do
166   {
167     unsigned int idx = 0;
168     add_2D_tensor_argument(idx, _input, slice);
169     add_1D_tensor_argument(idx, _scale_factor, win_scale);
170     add_2D_tensor_argument(idx, _output, slice);
171     _kernel.setArg<float>(idx++, _multiplier);
172     enqueue(queue, *this, slice, lws_hint());
173   } while (window_collapsed.slide_window_slice_2D(slice));
174 }