blob: 86bf366346b3720845e6491e2f4abe81bcb64594 [file] [log] [blame]
Vidhya Sudhan Loganathan338595b2019-06-28 14:09:53 +01001/*
2 * Copyright (c) 2019 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/CL/kernels/CLBatchConcatenateLayerKernel.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/CL/OpenCL.h"
31#include "arm_compute/core/Error.h"
32#include "arm_compute/core/Helpers.h"
33#include "arm_compute/core/IAccessWindow.h"
34#include "arm_compute/core/TensorInfo.h"
35#include "arm_compute/core/Utils.h"
36#include "arm_compute/core/Window.h"
37
38#include "support/ToolchainSupport.h"
39
40#include <map>
41
42using namespace arm_compute;
43
44namespace
45{
46std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, unsigned int batch_offset, ITensorInfo *output)
47{
48 ARM_COMPUTE_UNUSED(batch_offset);
49
50 const unsigned int num_elems_processed_per_iteration = 16 / input->element_size();
51
52 // The window needs to be based on output, except for the batch size
53 Window win = calculate_max_window(*output, Steps(num_elems_processed_per_iteration));
54 // The total batch size is the concatenation of the batch size of the inputs
55 win.set(3, Window::Dimension(0, input->tensor_shape()[3], 1));
56
57 AccessWindowHorizontal input_access(input, 0, num_elems_processed_per_iteration);
58 AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration);
59 bool window_changed = update_window_and_padding(win, input_access, output_access);
60 output_access.set_valid_region(win, ValidRegion(Coordinates(), output->tensor_shape()));
61
62 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
63 return std::make_pair(err, win);
64}
65Status validate_arguments(const ITensorInfo *input, unsigned int batch_offset, const ITensorInfo *output)
66{
67 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
68 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input);
69 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8, DataType::S8, DataType::QASYMM8,
70 DataType::U16, DataType::S16,
71 DataType::U32, DataType::S32,
72 DataType::F16, DataType::F32);
73 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
74
75 ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(Window::DimX) != output->dimension(Window::DimX));
76 ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(Window::DimY) != output->dimension(Window::DimY));
77 ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(Window::DimZ) != output->dimension(Window::DimZ));
78 ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(3) + batch_offset > output->dimension(3));
79 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(4, input, output);
80
81 return Status{};
82}
83} // namespace
84
85CLBatchConcatenateLayerKernel::CLBatchConcatenateLayerKernel()
86 : _input(nullptr), _output(nullptr), _batch_offset(0)
87{
88}
89
90void CLBatchConcatenateLayerKernel::configure(const ICLTensor *input, unsigned int batch_offset, ICLTensor *output)
91{
92 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
93 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), batch_offset, output->info()));
94
95 _input = input;
96 _output = output;
97 _batch_offset = batch_offset;
98
99 const unsigned int num_elems_processed_per_iteration = 16 / input->info()->element_size();
100
101 // Add build options
102 CLBuildOptions build_opts;
103 build_opts.add_option("-DDATA_TYPE=" + get_underlying_cl_type_from_data_type(input->info()->data_type()));
104 build_opts.add_option("-DVEC_SIZE=" + support::cpp11::to_string(num_elems_processed_per_iteration));
105 if(is_data_type_quantized_asymmetric(input->info()->data_type()) && input->info()->quantization_info() != output->info()->quantization_info())
106 {
107 const UniformQuantizationInfo iq_info = input->info()->quantization_info().uniform();
108 const UniformQuantizationInfo oq_info = output->info()->quantization_info().uniform();
109
110 build_opts.add_option("-DOFFSET_IN1=" + float_to_string_with_full_precision(iq_info.offset));
111 build_opts.add_option("-DOFFSET_OUT=" + float_to_string_with_full_precision(oq_info.offset));
112 build_opts.add_option("-DSCALE_IN1=" + float_to_string_with_full_precision(iq_info.scale));
113 build_opts.add_option("-DSCALE_OUT=" + float_to_string_with_full_precision(oq_info.scale));
114 }
115
116 // Create kernel
117 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel("concatenate", build_opts.options()));
118
119 // Configure kernel window
120 auto win_config = validate_and_configure_window(input->info(), batch_offset, output->info());
121 ARM_COMPUTE_ERROR_THROW_ON(std::get<0>(win_config));
122
123 ICLKernel::configure_internal(std::get<1>(win_config));
124 // Set config_id for enabling LWS tuning
125 _config_id = "concatenate_";
126 _config_id += support::cpp11::to_string(3);
127 _config_id += "_";
128 _config_id += support::cpp11::to_string(batch_offset);
129 _config_id += "_";
130 _config_id += support::cpp11::to_string(input->info()->dimension(0));
131 _config_id += "_";
132 _config_id += support::cpp11::to_string(input->info()->dimension(1));
133 _config_id += "_";
134 _config_id += support::cpp11::to_string(input->info()->dimension(2));
135 _config_id += "_";
136 _config_id += support::cpp11::to_string(input->info()->dimension(3));
137}
138
139Status CLBatchConcatenateLayerKernel::validate(const arm_compute::ITensorInfo *input,
140 unsigned int batch_offset,
141 const arm_compute::ITensorInfo *output)
142{
143 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, batch_offset, output));
144 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), batch_offset, output->clone().get()).first);
145 return Status{};
146}
147
148void CLBatchConcatenateLayerKernel::run(const Window &window, cl::CommandQueue &queue)
149{
150 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
151 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
152
153 Window slice = window.first_slice_window_3D();
154
155 const int offset_to_first_elements_in_bytes = _batch_offset * _output->info()->strides_in_bytes()[3];
156
157 unsigned int idx = 2 * num_arguments_per_3D_tensor(); // Skip the input and output parameters
158 _kernel.setArg<cl_int>(idx, offset_to_first_elements_in_bytes);
159
160 do
161 {
162 unsigned int idx = 0;
163 add_3D_tensor_argument(idx, _input, slice);
164 add_3D_tensor_argument(idx, _output, slice);
165 enqueue(queue, *this, slice, lws_hint());
166 }
167 while(window.slide_window_slice_3D(slice));
168}