blob: 6a699ae710b4bf04a4cf3d242f9b9f74771c0c85 [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()
45 : _input(nullptr), _output(nullptr), _top_bottom(0), _left_right(0)
46{
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
78 _input = input;
79 _output = output;
80
Georgios Pinitasac4e8732017-07-05 17:02:25 +010081 // Add build options
82 auto config = configs_map.find(static_cast<int>(input->info()->element_size()));
83 std::set<std::string> build_opts;
84 build_opts.emplace(("-DDATA_TYPE=" + config->second.first));
85 build_opts.emplace(("-DVEC_SIZE=" + support::cpp11::to_string(config->second.second)));
86
Anthony Barbier6ff3b192017-09-04 18:44:23 +010087 // Create kernel
Georgios Pinitasac4e8732017-07-05 17:02:25 +010088 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel("concatenate_depth", build_opts));
Anthony Barbier6ff3b192017-09-04 18:44:23 +010089
90 // Configure kernel window
91 _left_right = (output->info()->dimension(0) - input->info()->dimension(0)) / 2;
92 _top_bottom = (output->info()->dimension(1) - input->info()->dimension(1)) / 2;
93
Georgios Pinitasac4e8732017-07-05 17:02:25 +010094 const int offset_to_first_elements_in_bytes = depth_offset * output->info()->strides_in_bytes()[2];
Anthony Barbier6ff3b192017-09-04 18:44:23 +010095
Georgios Pinitasac4e8732017-07-05 17:02:25 +010096 const unsigned int num_elems_processed_per_iteration = 16 / input->info()->element_size();
97 const unsigned int num_elems_read_per_iteration = 16 / input->info()->element_size();
Anthony Barbier6ff3b192017-09-04 18:44:23 +010098 const unsigned int num_rows_read_per_iteration = 1;
99
100 // The window needs to be based on input as we copy all the depths of input
Georgios Pinitasac4e8732017-07-05 17:02:25 +0100101 Window win = calculate_max_window(*output->info(), Steps(num_elems_processed_per_iteration));
102 win.set(Window::DimZ, Window::Dimension(0, input->info()->tensor_shape().z(), 1));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100103
Georgios Pinitasac4e8732017-07-05 17:02:25 +0100104 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 +0100105 AccessWindowHorizontal output_access(output->info(), 0, num_elems_processed_per_iteration);
Georgios Pinitasac4e8732017-07-05 17:02:25 +0100106 update_window_and_padding(win, input_access, output_access);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100107 output_access.set_valid_region(win, ValidRegion(Coordinates(0, 0), output->info()->tensor_shape()));
108
Georgios Pinitasac4e8732017-07-05 17:02:25 +0100109 unsigned int idx = 2 * num_arguments_per_3D_tensor(); // Skip the input and output parameters
110 const cl_int3 offsets =
111 {
112 {
113 static_cast<cl_int>(_left_right),
114 static_cast<cl_int>(_top_bottom),
115 static_cast<cl_int>(offset_to_first_elements_in_bytes),
116 }
117 };
118 _kernel.setArg<cl_int3>(idx, offsets);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100119
120 ICLKernel::configure(win);
121}
122
123void CLDepthConcatenateKernel::run(const Window &window, cl::CommandQueue &queue)
124{
125 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
126 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
127
Georgios Pinitasac4e8732017-07-05 17:02:25 +0100128 Window slice = window.first_slice_window_3D();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100129
130 do
131 {
132 unsigned int idx = 0;
Georgios Pinitasac4e8732017-07-05 17:02:25 +0100133 add_3D_tensor_argument(idx, _input, slice);
134 add_3D_tensor_argument(idx, _output, slice);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100135 enqueue(queue, *this, slice);
136 }
Georgios Pinitasac4e8732017-07-05 17:02:25 +0100137 while(window.slide_window_slice_3D(slice));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100138}