8d8853c8197b57b005b405f83c0bdd791d1e35b3
[platform/core/ml/nnfw.git] / compute / ARMComputeEx / src / core / CL / kernels / CLScaleFactorSymm8Kernel.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/CLScaleFactorSymm8Kernel.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/ICLTensor.h"
47 #include "arm_compute/core/Helpers.h"
48 #include "arm_compute/core/Validate.h"
49 #include "arm_compute/core/Window.h"
50 #include "arm_compute/core/utils/misc/ShapeCalculator.h"
51 #include "support/StringSupport.h"
52
53 #include <climits>
54
55 using namespace arm_compute;
56 using namespace arm_compute::misc::shape_calculator;
57
58 namespace
59 {
60 Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output)
61 {
62   ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
63   ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F32);
64   ARM_COMPUTE_RETURN_ERROR_ON(input->num_dimensions() > 2);
65
66   if (output->tensor_shape().total_size() > 0)
67   {
68     ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
69
70     TensorShape output_shape = TensorShape{input->dimension(1)};
71
72     ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(output->tensor_shape(), output_shape);
73   }
74
75   return Status{};
76 }
77
78 std::tuple<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output)
79 {
80   TensorShape output_shape = TensorShape{input->dimension(1)};
81
82   // Output auto initialization if not yet initialized
83   auto_init_if_empty(*output, output_shape, 1, input->data_type());
84
85   const unsigned int num_elems_processed_per_iteration = 1;
86
87   // Configure kernel window
88   Window win = calculate_max_window(*input, Steps(num_elems_processed_per_iteration));
89   AccessWindowHorizontal input_access(input, 0, num_elems_processed_per_iteration);
90   AccessWindowStatic output_access(output, 0, 0, output->dimension(0), 1);
91
92   bool window_changed = update_window_and_padding(win, input_access, output_access);
93
94   output_access.set_valid_region(win, ValidRegion(Coordinates(), output->tensor_shape()));
95
96   Status err = (window_changed)
97                    ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!")
98                    : Status{};
99   return std::make_tuple(err, win);
100 }
101 } // namespace
102
103 CLScaleFactorSymm8Kernel::CLScaleFactorSymm8Kernel() : _input(nullptr), _output(nullptr) {}
104
105 void CLScaleFactorSymm8Kernel::configure(const ICLTensor *input, ICLTensor *output)
106 {
107   ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
108   ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info()));
109
110   _input = input;
111   _output = output;
112
113   std::set<std::string> build_opts;
114   build_opts.emplace("-DWIDTH=" + support::cpp11::to_string(input->info()->dimension(0)));
115
116   // Create kernel
117   _kernel = static_cast<cl::Kernel>(
118       CLKernelLibraryEx::get().create_kernel("scale_factor_symm8", build_opts));
119
120   auto win_config = validate_and_configure_window(input->info(), output->info());
121
122   ARM_COMPUTE_ERROR_THROW_ON(std::get<0>(win_config));
123
124   ICLKernel::configure_internal(std::get<1>(win_config));
125 }
126
127 Status CLScaleFactorSymm8Kernel::validate(const ITensorInfo *input, const ITensorInfo *output)
128 {
129   ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output));
130   ARM_COMPUTE_RETURN_ON_ERROR(
131       std::get<0>(validate_and_configure_window(input->clone().get(), output->clone().get())));
132
133   return Status{};
134 }
135
136 void CLScaleFactorSymm8Kernel::run(const Window &window, cl::CommandQueue &queue)
137 {
138   ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
139   ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
140
141   Window window_collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimZ);
142   Window slice = window_collapsed.first_slice_window_2D();
143   slice.set(Window::DimX, Window::Dimension(0, 1, 1));
144
145   do
146   {
147     Window output_slice = slice.shift_dimensions(1);
148
149     unsigned int idx = 0;
150     // Set inputs
151     add_2D_tensor_argument(idx, _input, slice);
152     add_1D_tensor_argument(idx, _output, output_slice);
153     enqueue(queue, *this, slice, lws_hint());
154   } while (window_collapsed.slide_window_slice_2D(slice));
155 }