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