2 * Copyright (c) 2020 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) 2017-2019 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/CLScaleFactorSymm8Kernel.h"
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"
55 using namespace arm_compute;
56 using namespace arm_compute::misc::shape_calculator;
60 Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output)
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);
66 if (output->tensor_shape().total_size() > 0)
68 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
70 TensorShape output_shape = TensorShape{input->dimension(1)};
72 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(output->tensor_shape(), output_shape);
78 std::tuple<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output)
80 TensorShape output_shape = TensorShape{input->dimension(1)};
82 // Output auto initialization if not yet initialized
83 auto_init_if_empty(*output, output_shape, 1, input->data_type());
85 const unsigned int num_elems_processed_per_iteration = 1;
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);
92 bool window_changed = update_window_and_padding(win, input_access, output_access);
94 output_access.set_valid_region(win, ValidRegion(Coordinates(), output->tensor_shape()));
96 Status err = (window_changed)
97 ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!")
99 return std::make_tuple(err, win);
103 CLScaleFactorSymm8Kernel::CLScaleFactorSymm8Kernel() : _input(nullptr), _output(nullptr) {}
105 void CLScaleFactorSymm8Kernel::configure(const ICLTensor *input, ICLTensor *output)
107 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
108 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info()));
113 std::set<std::string> build_opts;
114 build_opts.emplace("-DWIDTH=" + support::cpp11::to_string(input->info()->dimension(0)));
117 _kernel = static_cast<cl::Kernel>(
118 CLKernelLibraryEx::get().create_kernel("scale_factor_symm8", build_opts));
120 auto win_config = validate_and_configure_window(input->info(), output->info());
122 ARM_COMPUTE_ERROR_THROW_ON(std::get<0>(win_config));
124 ICLKernel::configure_internal(std::get<1>(win_config));
127 Status CLScaleFactorSymm8Kernel::validate(const ITensorInfo *input, const ITensorInfo *output)
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())));
136 void CLScaleFactorSymm8Kernel::run(const Window &window, cl::CommandQueue &queue)
138 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
139 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
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));
147 Window output_slice = slice.shift_dimensions(1);
149 unsigned int idx = 0;
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));