blob: 935d8420ff083f344a6268a63efcff357a02b990 [file] [log] [blame]
Anthony Barbier7068f992017-10-26 15:23:08 +01001/*
2 * Copyright (c) 2017 ARM Limited.
3 *
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/GCIm2ColKernel.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/GLES_COMPUTE/OpenGLES.h"
32#include "arm_compute/core/Helpers.h"
33#include "arm_compute/core/Types.h"
34#include "arm_compute/core/Validate.h"
35#include "support/ToolchainSupport.h"
36
37#include <cmath>
38#include <tuple>
39
40using namespace arm_compute;
41
42GCIm2ColKernel::GCIm2ColKernel()
43 : _input(nullptr), _output(nullptr), _convolved_dims(), _num_elems_processed_per_iteration(1), _run_func(nullptr)
44{
45}
46
47void GCIm2ColKernel::configure(const IGCTensor *input, IGCTensor *output, std::pair<unsigned int, unsigned int> kernel_dims, const PadStrideInfo &conv_info, bool has_bias)
48{
49 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F16, DataType::F32);
50 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
51 ARM_COMPUTE_UNUSED(kernel_dims);
52
53 _input = input;
54 _output = output;
55 _kernel.clear_params();
56
57 std::set<std::string> build_opts;
58 std::string dt_name = (input->info()->data_type() == DataType::F32) ? "DATA_TYPE_FP32" : "DATA_TYPE_FP16";
59 build_opts.emplace("#define LOCAL_SIZE_X " + support::cpp11::to_string(1));
60 build_opts.emplace("#define LOCAL_SIZE_Y " + support::cpp11::to_string(1));
61 build_opts.emplace("#define LOCAL_SIZE_Z " + support::cpp11::to_string(1));
62 build_opts.insert("#define " + dt_name);
63
64 if(has_bias)
65 {
66 build_opts.emplace("#define HAS_BIAS");
67 }
68
69 int pad_x = 0;
70 int pad_y = 0;
71 int stride_x = 0;
72 int stride_y = 0;
73 std::tie(pad_x, pad_y) = conv_info.pad();
74 std::tie(stride_x, stride_y) = conv_info.stride();
75
76 const bool run_img2col_reduced = (output->info()->dimension(0) == (input->info()->dimension(0) * input->info()->dimension(1) * input->info()->dimension(2))) && (TensorShape::num_max_dimensions >= 4)
77 && (std::equal(input->info()->tensor_shape().cbegin() + 3,
78 input->info()->tensor_shape().cend(),
79 output->info()->tensor_shape().cbegin() + 1))
80 && ((stride_x == 1) && (stride_y == 1) && (pad_x == 0) && (pad_y == 0));
81
82 if(!run_img2col_reduced)
83 {
84 // this path is currently not used and not validated
85 build_opts.insert("#define IM2COL_GENERIC");
86 _convolved_dims = scaled_dimensions(input->info()->dimension(0), input->info()->dimension(1),
87 kernel_dims.first, kernel_dims.second,
88 conv_info);
89 _num_elems_processed_per_iteration = output->info()->dimension(0);
90
91 build_opts.emplace("#define KERNEL_WIDTH " + support::cpp11::to_string(kernel_dims.first));
92 build_opts.emplace("#define KERNEL_HEIGHT " + support::cpp11::to_string(kernel_dims.second));
93 build_opts.emplace("#define KERNEL_DEPTH " + support::cpp11::to_string(input->info()->dimension(2)));
94 build_opts.emplace("#define CONVOLVED_WIDTH " + support::cpp11::to_string(_convolved_dims.first));
95 build_opts.emplace("#define CONVOLVED_HEIGHT " + support::cpp11::to_string(_convolved_dims.second));
96 build_opts.emplace("#define STRIDE_X " + support::cpp11::to_string(conv_info.stride().first));
97 build_opts.emplace("#define STRIDE_Y " + support::cpp11::to_string(conv_info.stride().second));
98 build_opts.emplace("#define PAD_X " + support::cpp11::to_string(conv_info.pad().first));
99 build_opts.emplace("#define PAD_Y " + support::cpp11::to_string(conv_info.pad().second));
100 build_opts.emplace("#define SRC_WIDTH " + support::cpp11::to_string(input->info()->dimension(0)));
101 build_opts.emplace("#define SRC_HEIGHT " + support::cpp11::to_string(input->info()->dimension(1)));
102
103 // Create kernel
104 _kernel = static_cast<GCKernel>(GCKernelLibrary::get().create_kernel("im2col_generic", build_opts));
105
106 _run_func = &GCIm2ColKernel::run_generic;
107 }
108 else
109 {
110 build_opts.insert("#define IM2COL_REDUCED");
111 _num_elems_processed_per_iteration = 4 / input->info()->element_size();
112
113 // Create kernel
114 _kernel = static_cast<GCKernel>(GCKernelLibrary::get().create_kernel("im2col_reduced", build_opts));
115
116 _run_func = &GCIm2ColKernel::run_reduced;
117 }
118
119 // Configure kernel window
120 Window win = calculate_max_window(*input->info(), Steps(_num_elems_processed_per_iteration));
121
122 if(input->info()->data_type() == DataType::F16)
123 {
124 // Calculate input right and bottom border
125 AccessWindowHorizontal input_access(input->info(), 0, _num_elems_processed_per_iteration);
126
127 // Calculate output right and bottom border
128 const int output_width = output->info()->dimension(0);
129 const int output_height = output->info()->dimension(1);
130 const int output_padding_right = ceil_to_multiple(output_width, _num_elems_processed_per_iteration) - output_width;
131 AccessWindowStatic output_access(output->info(), 0, 0, output_width + output_padding_right, output_height);
132
133 update_window_and_padding(win, input_access, output_access);
134 }
135
136 output->info()->set_valid_region(ValidRegion(Coordinates(), output->info()->tensor_shape()));
137
138 if(!run_img2col_reduced)
139 {
140 // set the Z dimension's step same size as the whole dimension so that one can't split across the Z dimension
141 win.set_dimension_step(Window::DimZ, win[Window::DimZ].end() - win[Window::DimZ].start());
142 }
143
144 // set shader params binding point
145 _kernel.set_shader_params_binding_point(0);
146 IGCKernel::configure(win);
147}
148
149void GCIm2ColKernel::run(const Window &window)
150{
151 ARM_COMPUTE_ERROR_ON(_run_func == nullptr);
152 (this->*_run_func)(window);
153}
154
155void GCIm2ColKernel::run_generic(const Window &window)
156{
157 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
158 ARM_COMPUTE_ERROR_ON_MISMATCHING_WINDOWS(IGCKernel::window(), window);
159
160 // Get initial windows
161 Window window_collapsed = window.collapse_if_possible(IGCKernel::window(), Window::DimZ);
162 // Change the Z dimension's step back to 1
163 window_collapsed.set_dimension_step(Window::DimZ, 1);
164
165 Window slice = window_collapsed.first_slice_window_3D();
166 Window slice_in = window_collapsed.first_slice_window_3D();
167 Window slice_out = window_collapsed.first_slice_window_3D();
168
169 // Setup slice
170 slice.set(Window::DimX, Window::Dimension(0, static_cast<int>(_convolved_dims.first), 1));
171 slice.set(Window::DimY, Window::Dimension(0, static_cast<int>(_convolved_dims.second), 1));
172
173 // Setup input slice
174 // The first three dimensions of the input are increased by the inner loops
175 slice_in.set(Window::DimX, Window::Dimension(0, 0, 0));
176 slice_in.set(Window::DimY, Window::Dimension(0, 0, 0));
177 slice_in.set(Window::DimZ, Window::Dimension(0, 0, 0));
178
179 // Setup output slice
180 slice_out.set(Window::DimX, Window::Dimension(0, _output->info()->dimension(0), _num_elems_processed_per_iteration));
181 slice_out.set(Window::DimY, Window::Dimension(0, _output->info()->dimension(1), 1));
182 slice_out.set(Window::DimZ, Window::Dimension(0, 1, 1));
183
184 _kernel.use();
185
186 do
187 {
188 unsigned int idx = 0;
189 add_3D_tensor_argument(idx, _input, 1, slice_in);
190 add_2D_tensor_argument(idx, _output, 2, slice_out);
191
192 _kernel.set_params(idx++, static_cast<unsigned int>(_input->info()->dimension(2)));
193 _kernel.set_params(idx++, static_cast<unsigned int>(_input->info()->strides_in_bytes()[3]));
194 _kernel.set_params(idx++, static_cast<unsigned int>(_output->info()->strides_in_bytes()[3]));
195 _kernel.update_shader_params();
196
197 enqueue(*this, slice);
198 }
199 while(window_collapsed.slide_window_slice_3D(slice) && window_collapsed.slide_window_slice_3D(slice_out) && window_collapsed.slide_window_slice_3D(slice_in));
200}
201
202void GCIm2ColKernel::run_reduced(const Window &window)
203{
204 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
205 ARM_COMPUTE_ERROR_ON_MISMATCHING_WINDOWS(IGCKernel::window(), window);
206
207 Window out_window;
208 out_window.use_tensor_dimensions(_output->info()->tensor_shape());
209
210 Window out_slice = out_window.first_slice_window_1D();
211 Window in_slice = window.first_slice_window_3D();
212
213 _kernel.use();
214
215 // Run kernel
216 do
217 {
218 // Set arguments
219 unsigned int idx = 0;
220
221 add_3D_tensor_argument(idx, _input, 1, in_slice);
222 add_1D_tensor_argument(idx, _output, 2, out_slice);
223 _kernel.set_params(idx++, _input->info()->dimension(0));
224 _kernel.set_params(idx++, _input->info()->dimension(1));
225 _kernel.update_shader_params();
226
227 enqueue(*this, in_slice);
228 }
229 while(window.slide_window_slice_3D(in_slice) && out_window.slide_window_slice_1D(out_slice));
230}