blob: 16dafaf543244cadb1562bd17a5ce526aecf2e9f [file] [log] [blame]
Xinghang Zhou33ff9ef2018-01-17 11:23:39 +08001/*
Matthew Bentham758b5ba2020-03-05 23:37:48 +00002 * Copyright (c) 2017-2020 ARM Limited.
Xinghang Zhou33ff9ef2018-01-17 11:23:39 +08003 *
4 * SPDX-License-Identifier: MIT
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24#include "arm_compute/core/GLES_COMPUTE/kernels/GCTensorShiftKernel.h"
25
26#include "arm_compute/core/AccessWindowStatic.h"
27#include "arm_compute/core/Error.h"
28#include "arm_compute/core/GLES_COMPUTE/GCHelpers.h"
29#include "arm_compute/core/GLES_COMPUTE/GCKernelLibrary.h"
30#include "arm_compute/core/GLES_COMPUTE/IGCTensor.h"
31#include "arm_compute/core/Helpers.h"
32#include "arm_compute/core/IAccessWindow.h"
33#include "arm_compute/core/ITensor.h"
34#include "arm_compute/core/Types.h"
35#include "arm_compute/core/Validate.h"
Matthew Bentham758b5ba2020-03-05 23:37:48 +000036#include "support/StringSupport.h"
Xinghang Zhou33ff9ef2018-01-17 11:23:39 +080037
38using namespace arm_compute;
Xinghang Zhou33ff9ef2018-01-17 11:23:39 +080039
40GCTensorShiftKernel::GCTensorShiftKernel()
Frank Lei4406fd62018-02-01 14:47:14 +080041 : _input(nullptr), _lws(gles::NDRange(1U, 1U, 1U)), _left_padding(0)
Xinghang Zhou33ff9ef2018-01-17 11:23:39 +080042{
43}
44
45void GCTensorShiftKernel::configure(IGCTensor *input)
46{
47 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F16, DataType::F32);
48
49 _input = input;
50
51 std::set<std::string> options;
52 options.emplace("#define LOCAL_SIZE_X " + support::cpp11::to_string(_lws[0]));
53 options.emplace("#define LOCAL_SIZE_Y " + support::cpp11::to_string(_lws[1]));
54 options.emplace("#define LOCAL_SIZE_Z " + support::cpp11::to_string(_lws[2]));
55 options.emplace("#define WIDTH " + support::cpp11::to_string(input->info()->dimension(0)));
56
57 std::string dt_name = (input->info()->data_type() == DataType::F32) ? "DATA_TYPE_FP32" : "DATA_TYPE_FP16";
58 options.emplace(("#define " + dt_name));
59
60 unsigned int num_elems_written_per_iteration_x = input->info()->dimension(0) + input->info()->padding().left + input->info()->padding().right;
Xinghang Zhou33ff9ef2018-01-17 11:23:39 +080061
62 std::stringstream kernel_name;
63 kernel_name << "tensorshift";
64
65 _kernel = static_cast<GCKernel>(GCKernelLibrary::get().create_kernel(kernel_name.str(), options));
66
Frank Lei4406fd62018-02-01 14:47:14 +080067 Window win;
68 win.set(Window::DimX, Window::Dimension(0, num_elems_written_per_iteration_x, num_elems_written_per_iteration_x));
69 win.use_tensor_dimensions(input->info()->tensor_shape(), Window::DimY);
70 win.use_tensor_dimensions(input->info()->tensor_shape(), Window::DimZ);
Xinghang Zhou33ff9ef2018-01-17 11:23:39 +080071
Frank Lei4406fd62018-02-01 14:47:14 +080072 _left_padding = _input->info()->padding().left;
Xinghang Zhou33ff9ef2018-01-17 11:23:39 +080073
74 IGCKernel::configure(win);
75}
76
77void GCTensorShiftKernel::run(const Window &window)
78{
79 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
80 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
81
Frank Lei4406fd62018-02-01 14:47:14 +080082 if(int(_left_padding) == 0 || !_input->needs_shifting())
83 {
84 return;
85 }
86
Xinghang Zhou33ff9ef2018-01-17 11:23:39 +080087 _kernel.use();
88
89 // Get initial windows
90 Window slice = window.first_slice_window_3D();
91 slice.shift(Window::DimX, -(_input->info()->padding()).left);
92
93 do
94 {
95 unsigned int idx = 0;
96
97 add_3D_tensor_argument(idx, _input, 1, slice);
98
Frank Lei4406fd62018-02-01 14:47:14 +080099 _kernel.set_argument(idx++, static_cast<unsigned int>(_left_padding));
Xinghang Zhou33ff9ef2018-01-17 11:23:39 +0800100
101 _kernel.update_shader_params();
102 enqueue(*this, slice, _lws);
103 }
104 while(window.slide_window_slice_3D(slice));
105}