Imported Upstream version 1.12.0
[platform/core/ml/nnfw.git] / compute / ARMComputeEx / src / core / CL / kernels / CLNegKernel.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/CLNegKernel.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 "support/StringSupport.h"
47
48 using namespace arm_compute;
49
50 namespace
51 {
52 Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output)
53 {
54   ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::S16, DataType::S32,
55                                                 DataType::F16, DataType::F32);
56   ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::S16, DataType::S32,
57                                                 DataType::F16, DataType::F32);
58   ARM_COMPUTE_ERROR_ON_MISMATCHING_DIMENSIONS(input->tensor_shape(), output->tensor_shape());
59   ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
60
61   return Status{};
62 }
63
64 } // namespace
65
66 CLNegKernel::CLNegKernel() : _input(nullptr), _output(nullptr) {}
67
68 void CLNegKernel::configure(const ICLTensor *input, ICLTensor *output)
69 {
70
71   ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
72   ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info()));
73
74   _input = input;
75   _output = output;
76
77   constexpr unsigned int num_elems_processed_per_iteration = 16;
78
79   // Create kernel
80   std::set<std::string> build_opts;
81   build_opts.emplace(("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type())));
82   build_opts.emplace(
83     ("-DVEC_SIZE=" + support::cpp11::to_string(num_elems_processed_per_iteration)));
84   _kernel =
85     static_cast<cl::Kernel>(CLKernelLibraryEx::get().create_kernel("neg_tensor", build_opts));
86
87   // Configure window
88   Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration));
89
90   AccessWindowHorizontal input_access(input->info(), 0, num_elems_processed_per_iteration);
91   AccessWindowHorizontal output_access(output->info(), 0, num_elems_processed_per_iteration);
92   update_window_and_padding(win, input_access, output_access);
93   output_access.set_valid_region(win, input->info()->valid_region());
94
95   ICLKernel::configure_internal(win);
96 }
97
98 void CLNegKernel::run(const Window &window, cl::CommandQueue &queue)
99 {
100   ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
101   ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
102
103   Window collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimZ);
104   Window slice = collapsed.first_slice_window_3D();
105
106   do
107   {
108     unsigned int idx = 0;
109     add_3D_tensor_argument(idx, _input, slice);
110     add_3D_tensor_argument(idx, _output, slice);
111     enqueue(queue, *this, slice, lws_hint());
112   } while (collapsed.slide_window_slice_3D(slice));
113 }