2 * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * Copyright (c) 2016-2018 ARM Limited.
20 * SPDX-License-Identifier: MIT
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:
29 * The above copyright notice and this permission notice shall be included in all
30 * copies or substantial portions of the Software.
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
41 #include "arm_compute/core/CL/kernels/CLNegKernel.h"
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"
48 using namespace arm_compute;
52 Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output)
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);
66 CLNegKernel::CLNegKernel() : _input(nullptr), _output(nullptr) {}
68 void CLNegKernel::configure(const ICLTensor *input, ICLTensor *output)
71 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
72 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info()));
77 constexpr unsigned int num_elems_processed_per_iteration = 16;
80 std::set<std::string> build_opts;
81 build_opts.emplace(("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type())));
83 ("-DVEC_SIZE=" + support::cpp11::to_string(num_elems_processed_per_iteration)));
85 static_cast<cl::Kernel>(CLKernelLibraryEx::get().create_kernel("neg_tensor", build_opts));
88 Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration));
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());
95 ICLKernel::configure_internal(win);
98 void CLNegKernel::run(const Window &window, cl::CommandQueue &queue)
100 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
101 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
103 Window collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimZ);
104 Window slice = collapsed.first_slice_window_3D();
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));