blob: 56d6ec8f16fc38d6470dfccb9ce8d861dbc58fe2 [file] [log] [blame]
Michalis Spyrou55b3d122018-05-09 09:59:23 +01001/*
2 * Copyright (c) 2018 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/CLWidthConcatenateLayerKernel.h"
25
26#include "arm_compute/core/CL/CLHelpers.h"
27#include "arm_compute/core/CL/CLKernelLibrary.h"
Vidhya Sudhan Loganathanf1f49062018-05-25 13:21:26 +010028#include "arm_compute/core/CL/CLValidate.h"
Michalis Spyrou55b3d122018-05-09 09:59:23 +010029#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"
Michalis Spyrou55b3d122018-05-09 09:59:23 +010036#include "arm_compute/core/Window.h"
37#include "arm_compute/core/utils/misc/ShapeCalculator.h"
38
39#include "support/ToolchainSupport.h"
40
41#include <map>
42
43using namespace arm_compute;
44namespace
45{
46std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, unsigned int width_offset, ITensorInfo *output)
47{
48 const unsigned int num_elems_processed_per_iteration = 16;
49
50 // The window needs to be based on input as we copy all the widths of input
51 Window win = calculate_max_window(*input, Steps(num_elems_processed_per_iteration));
52 AccessWindowHorizontal input_access(input, 0, num_elems_processed_per_iteration);
53 AccessWindowHorizontal output_access(output, width_offset, num_elems_processed_per_iteration);
54 bool window_changed = update_window_and_padding(win, input_access, output_access);
55
56 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
57 return std::make_pair(err, win);
58}
59Status validate_arguments(const ITensorInfo *input, unsigned int width_offset, const ITensorInfo *output)
60{
61 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
Vidhya Sudhan Loganathanf1f49062018-05-25 13:21:26 +010062 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input);
Michalis Spyrou55b3d122018-05-09 09:59:23 +010063 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8, DataType::S8, DataType::QS8, DataType::QASYMM8, DataType::U16, DataType::S16, DataType::QS16, DataType::F16, DataType::U32,
64 DataType::F32);
65 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
66 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_FIXED_POINT_POSITION(input, output);
67 ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(0) + width_offset > output->dimension(0));
68
69 for(size_t i = 1; i < Coordinates::num_max_dimensions; ++i)
70 {
71 ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(i) != output->dimension(i));
72 }
73 ARM_COMPUTE_RETURN_ERROR_ON(input->num_dimensions() > 3);
74
75 return Status{};
76}
77} // namespace
78
79CLWidthConcatenateLayerKernel::CLWidthConcatenateLayerKernel()
80 : _input(nullptr), _output(nullptr), _width_offset(0)
81{
82}
83
84Status CLWidthConcatenateLayerKernel::validate(const ITensorInfo *input, unsigned int width_offset, const ITensorInfo *output)
85{
86 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, width_offset, output));
87 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), width_offset, output->clone().get()).first);
88 return Status{};
89}
90
91void CLWidthConcatenateLayerKernel::configure(const ICLTensor *input, unsigned int width_offset, ICLTensor *output)
92{
93 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
94 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), width_offset, output->info()));
95
96 _input = input;
97 _output = output;
98 _width_offset = width_offset;
99
100 const unsigned int num_elems_processed_per_iteration = 16;
101
102 // Add build options
103 CLBuildOptions build_opts;
104 build_opts.add_option("-DDATA_TYPE=" + get_underlying_cl_type_from_data_type(input->info()->data_type()));
105 build_opts.add_option("-DVEC_SIZE=" + support::cpp11::to_string(num_elems_processed_per_iteration));
106
107 // Create kernel
108 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel("concatenate_width", build_opts.options()));
109
110 const int offset_to_first_elements_in_bytes = _width_offset * _output->info()->strides_in_bytes()[0];
111
112 unsigned int idx = 2 * num_arguments_per_3D_tensor(); // Skip the input and output parameters
113 _kernel.setArg<cl_int>(idx, offset_to_first_elements_in_bytes);
114
115 // Configure kernel window
116 auto win_config = validate_and_configure_window(input->info(), width_offset, output->info());
117 ARM_COMPUTE_ERROR_THROW_ON(std::get<0>(win_config));
118
119 ICLKernel::configure(std::get<1>(win_config));
120}
121
122void CLWidthConcatenateLayerKernel::run(const Window &window, cl::CommandQueue &queue)
123{
124 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
125 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
126
127 Window slice = window.first_slice_window_3D();
128
129 do
130 {
131 unsigned int idx = 0;
132 add_3D_tensor_argument(idx, _input, slice);
133 add_3D_tensor_argument(idx, _output, slice);
134 enqueue(queue, *this, slice);
135 }
136 while(window.slide_window_slice_3D(slice));
137}