blob: 14e30ec5b10b182c1679c5298bf012d357da40d9 [file] [log] [blame]
giuros013175fcf2018-11-21 09:59:17 +00001/*
Manuel Bottini8481d832019-12-10 15:28:40 +00002 * Copyright (c) 2018-2020 ARM Limited.
giuros013175fcf2018-11-21 09:59:17 +00003 *
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/CL/kernels/CLTileKernel.h"
25
26#include "arm_compute/core/CL/CLHelpers.h"
27#include "arm_compute/core/CL/CLKernelLibrary.h"
28#include "arm_compute/core/CL/CLValidate.h"
29#include "arm_compute/core/CL/ICLTensor.h"
30#include "arm_compute/core/Helpers.h"
31#include "arm_compute/core/IAccessWindow.h"
32#include "arm_compute/core/TensorInfo.h"
33#include "arm_compute/core/Utils.h"
34#include "arm_compute/core/Validate.h"
35#include "arm_compute/core/Window.h"
36#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Matthew Bentham758b5ba2020-03-05 23:37:48 +000037#include "support/StringSupport.h"
giuros013175fcf2018-11-21 09:59:17 +000038
39namespace arm_compute
40{
41namespace
42{
43Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, const Multiples &multiples)
44{
45 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
Manuel Bottini8481d832019-12-10 15:28:40 +000046 ARM_COMPUTE_RETURN_ERROR_ON(input->data_type() == DataType::UNKNOWN);
giuros013175fcf2018-11-21 09:59:17 +000047 ARM_COMPUTE_RETURN_ERROR_ON(multiples.size() > 4);
48 ARM_COMPUTE_RETURN_ERROR_ON(multiples.empty());
49 ARM_COMPUTE_RETURN_ERROR_ON(std::any_of(multiples.begin(), multiples.end(), [](uint32_t e)
50 {
51 return e == 0;
52 }));
53
54 // Validate output if initialized
55 if(output->total_size() != 0)
56 {
57 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(misc::shape_calculator::compute_tiled_shape(input->tensor_shape(), multiples), output->tensor_shape());
58 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
59 }
60
61 return Status{};
62}
63} // namespace
64
65CLTileKernel::CLTileKernel()
66 : _input(nullptr), _output(nullptr)
67{
68}
69
70void CLTileKernel::configure(const ICLTensor *input, ICLTensor *output, const Multiples &multiples)
71{
Manuel Bottini4c6bd512020-04-08 10:15:51 +010072 configure(CLKernelLibrary::get().get_compile_context(), input, output, multiples);
73}
74
Manuel Bottini2803f702020-04-21 16:20:03 +010075void CLTileKernel::configure(const CLCompileContext &compile_context, const ICLTensor *input, ICLTensor *output, const Multiples &multiples)
Manuel Bottini4c6bd512020-04-08 10:15:51 +010076{
giuros013175fcf2018-11-21 09:59:17 +000077 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
78
79 // Auto initialize output
80 TensorShape tiled_shape = misc::shape_calculator::compute_tiled_shape(input->info()->tensor_shape(), multiples);
81 auto_init_if_empty(*output->info(), tiled_shape, 1, input->info()->data_type());
82
83 // Validate
84 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), multiples));
85
86 _input = input;
87 _output = output;
88
89 const DataType data_type = input->info()->data_type();
90 const int vec_size_x = 16 / input->info()->element_size();
91 const int input_width_x = input->info()->tensor_shape().x();
92 const unsigned int offset = ceil_to_multiple(input_width_x, vec_size_x) - input_width_x;
93 const bool multi_access_x = (input_width_x / vec_size_x > 0);
94
95 // Create kernel
96 CLBuildOptions build_opts;
97 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(data_type));
98 build_opts.add_option("-DSRC_WIDTH=" + support::cpp11::to_string(input_width_x));
99 build_opts.add_option("-DSRC_HEIGHT=" + support::cpp11::to_string(input->info()->dimension(1)));
100 build_opts.add_option("-DSRC_DEPTH=" + support::cpp11::to_string(input->info()->dimension(2)));
101 build_opts.add_option("-DSRC_BATCHES=" + support::cpp11::to_string(input->info()->dimension(3)));
102 build_opts.add_option("-DDST_DEPTH=" + support::cpp11::to_string(output->info()->dimension(2)));
103 build_opts.add_option_if(multi_access_x, "-DOFFSET=" + support::cpp11::to_string(offset));
104 build_opts.add_option_if(multi_access_x, "-DVEC_SIZE=" + support::cpp11::to_string(vec_size_x));
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100105 _kernel = create_kernel(compile_context, "tile", build_opts.options());
giuros013175fcf2018-11-21 09:59:17 +0000106
107 // Configure window without padding
108 Window win = calculate_max_window(*output->info());
109
110 if(multi_access_x)
111 {
112 // If multi-access is enabled, no thread should cross the tile boundaries. This means we need
113 // as many threads as those to cover a single tile times multiples[0]. Note that if threads
114 // do not cross the boundaries of the tiles, they won't cross the boundaries of the last tile, and
115 // we don't need to pad the output
116 const unsigned int size_win_x = ceil_to_multiple(input->info()->dimension(0), vec_size_x) * multiples[0];
117 win.set(Window::DimX,
118 Window::Dimension(win.x().start(), size_win_x, vec_size_x));
119 }
120
121 ICLKernel::configure_internal(win);
122
123 // Set config_id for enabling LWS tuning
124 _config_id = "tile";
125 _config_id += "_";
126 _config_id += lower_string(string_from_data_type(input->info()->data_type()));
127 for(unsigned int i = 0; i < multiples.size(); ++i)
128 {
129 _config_id += "_";
130 _config_id += support::cpp11::to_string(input->info()->dimension(i));
131 _config_id += "_";
132 _config_id += support::cpp11::to_string(multiples[i]);
133 }
134}
135
136Status CLTileKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const Multiples &multiples)
137{
138 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, multiples));
139 return Status{};
140}
141
142void CLTileKernel::run(const Window &window, cl::CommandQueue &queue)
143{
144 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
145 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
146
147 Window collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimZ);
148 Window slice = collapsed.first_slice_window_4D();
149
150 do
151 {
152 unsigned int idx = 0;
153 add_4D_tensor_argument(idx, _input, slice);
154 add_4D_tensor_argument(idx, _output, slice);
Georgios Pinitas275f99c2019-08-23 12:44:11 +0100155 enqueue(queue, *this, slice, lws_hint());
giuros013175fcf2018-11-21 09:59:17 +0000156 }
157 while(collapsed.slide_window_slice_4D(slice));
158}
159} // namespace arm_compute