blob: edfbf829ed91e81dccf48ed2a3e307a0cab20ca0 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +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/CL/kernels/CLDepthConcatenateKernel.h"
25
26#include "arm_compute/core/CL/CLHelpers.h"
27#include "arm_compute/core/CL/CLKernelLibrary.h"
28#include "arm_compute/core/CL/ICLTensor.h"
29#include "arm_compute/core/CL/OpenCL.h"
30#include "arm_compute/core/Error.h"
31#include "arm_compute/core/Helpers.h"
32#include "arm_compute/core/IAccessWindow.h"
33#include "arm_compute/core/TensorInfo.h"
34#include "arm_compute/core/Utils.h"
35#include "arm_compute/core/Validate.h"
36#include "arm_compute/core/Window.h"
37
Georgios Pinitasac4e8732017-07-05 17:02:25 +010038#include "support/ToolchainSupport.h"
39
40#include <map>
41
Anthony Barbier6ff3b192017-09-04 18:44:23 +010042using namespace arm_compute;
43
44CLDepthConcatenateKernel::CLDepthConcatenateKernel()
Gian Marco Iodice906443f2017-09-06 15:09:54 +010045 : _input(nullptr), _output(nullptr), _top_bottom(0), _left_right(0), _depth_offset(0)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010046{
47}
48
49BorderSize CLDepthConcatenateKernel::border_size() const
50{
51 return BorderSize(_top_bottom, _left_right);
52}
53
54void CLDepthConcatenateKernel::configure(const ICLTensor *input, unsigned int depth_offset, ICLTensor *output)
55{
Georgios Pinitasac4e8732017-07-05 17:02:25 +010056 static std::map<int, std::pair<std::string, int>> configs_map =
57 {
58 { 1, { "uchar", 16 } },
59 { 2, { "ushort", 8 } },
60 { 4, { "uint", 4 } },
61 { 8, { "ulong", 2 } },
62 };
63
64 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QS8, DataType::QS16, DataType::F16, DataType::F32);
65 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
66 ARM_COMPUTE_ERROR_ON_MISMATCHING_FIXED_POINT_POSITION(input, output);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010067 ARM_COMPUTE_ERROR_ON(input->info()->dimension(2) + depth_offset > output->info()->dimension(2));
68 ARM_COMPUTE_ERROR_ON(input->info()->dimension(0) > output->info()->dimension(0));
69 ARM_COMPUTE_ERROR_ON(input->info()->dimension(1) > output->info()->dimension(1));
70 ARM_COMPUTE_ERROR_ON_MISMATCHING_SHAPES(3, input, output);
Georgios Pinitasac4e8732017-07-05 17:02:25 +010071 ARM_COMPUTE_ERROR_ON(configs_map.find(input->info()->element_size()) == configs_map.end());
Anthony Barbier6ff3b192017-09-04 18:44:23 +010072
73 // The gaps between the two lowest dimensions of input and output need to be divisible by 2
74 // Otherwise it is not clear how the padding should be added onto the input tensor
75 ARM_COMPUTE_ERROR_ON((output->info()->dimension(0) - input->info()->dimension(0)) % 2);
76 ARM_COMPUTE_ERROR_ON((output->info()->dimension(1) - input->info()->dimension(1)) % 2);
77
Gian Marco Iodice906443f2017-09-06 15:09:54 +010078 _input = input;
79 _output = output;
80 _depth_offset = depth_offset;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010081
Georgios Pinitasac4e8732017-07-05 17:02:25 +010082 // Add build options
83 auto config = configs_map.find(static_cast<int>(input->info()->element_size()));
84 std::set<std::string> build_opts;
85 build_opts.emplace(("-DDATA_TYPE=" + config->second.first));
86 build_opts.emplace(("-DVEC_SIZE=" + support::cpp11::to_string(config->second.second)));
87
Anthony Barbier6ff3b192017-09-04 18:44:23 +010088 // Create kernel
Georgios Pinitasac4e8732017-07-05 17:02:25 +010089 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel("concatenate_depth", build_opts));
Anthony Barbier6ff3b192017-09-04 18:44:23 +010090
91 // Configure kernel window
92 _left_right = (output->info()->dimension(0) - input->info()->dimension(0)) / 2;
93 _top_bottom = (output->info()->dimension(1) - input->info()->dimension(1)) / 2;
94
Georgios Pinitasac4e8732017-07-05 17:02:25 +010095 const unsigned int num_elems_processed_per_iteration = 16 / input->info()->element_size();
96 const unsigned int num_elems_read_per_iteration = 16 / input->info()->element_size();
Anthony Barbier6ff3b192017-09-04 18:44:23 +010097 const unsigned int num_rows_read_per_iteration = 1;
98
99 // The window needs to be based on input as we copy all the depths of input
Georgios Pinitasac4e8732017-07-05 17:02:25 +0100100 Window win = calculate_max_window(*output->info(), Steps(num_elems_processed_per_iteration));
101 win.set(Window::DimZ, Window::Dimension(0, input->info()->tensor_shape().z(), 1));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100102
Georgios Pinitasac4e8732017-07-05 17:02:25 +0100103 AccessWindowRectangle input_access(input->info(), -_left_right, -_top_bottom, num_elems_read_per_iteration, num_rows_read_per_iteration);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100104 AccessWindowHorizontal output_access(output->info(), 0, num_elems_processed_per_iteration);
Georgios Pinitasac4e8732017-07-05 17:02:25 +0100105 update_window_and_padding(win, input_access, output_access);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100106 output_access.set_valid_region(win, ValidRegion(Coordinates(0, 0), output->info()->tensor_shape()));
107
Gian Marco Iodice906443f2017-09-06 15:09:54 +0100108 ICLKernel::configure(win);
109}
110
111void CLDepthConcatenateKernel::run(const Window &window, cl::CommandQueue &queue)
112{
113 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
114 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
115
116 Window slice = window.first_slice_window_3D();
117
118 const int offset_to_first_elements_in_bytes = _depth_offset * _output->info()->strides_in_bytes()[2];
119
Georgios Pinitasac4e8732017-07-05 17:02:25 +0100120 unsigned int idx = 2 * num_arguments_per_3D_tensor(); // Skip the input and output parameters
121 const cl_int3 offsets =
122 {
123 {
124 static_cast<cl_int>(_left_right),
125 static_cast<cl_int>(_top_bottom),
126 static_cast<cl_int>(offset_to_first_elements_in_bytes),
127 }
128 };
129 _kernel.setArg<cl_int3>(idx, offsets);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100130
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100131 do
132 {
133 unsigned int idx = 0;
Georgios Pinitasac4e8732017-07-05 17:02:25 +0100134 add_3D_tensor_argument(idx, _input, slice);
135 add_3D_tensor_argument(idx, _output, slice);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100136 enqueue(queue, *this, slice);
137 }
Georgios Pinitasac4e8732017-07-05 17:02:25 +0100138 while(window.slide_window_slice_3D(slice));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100139}