blob: 204f9aed6f43e6b117bebc0b7f0f93b2be2467c5 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Diego Lopez Recasbcbc9702017-12-18 11:28:27 +00002 * Copyright (c) 2017, 2018 ARM Limited.
Anthony Barbier6ff3b192017-09-04 18:44:23 +01003 *
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 */
Giorgio Arena04a8f8c2017-11-23 11:45:24 +000024#include "arm_compute/core/CL/kernels/CLDepthConcatenateLayerKernel.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010025
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"
Anthony Barbier6ff3b192017-09-04 18:44: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"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010036#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
Giorgio Arena04a8f8c2017-11-23 11:45:24 +000044CLDepthConcatenateLayerKernel::CLDepthConcatenateLayerKernel()
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
Giorgio Arena04a8f8c2017-11-23 11:45:24 +000049BorderSize CLDepthConcatenateLayerKernel::border_size() const
Anthony Barbier6ff3b192017-09-04 18:44:23 +010050{
51 return BorderSize(_top_bottom, _left_right);
52}
53
Giorgio Arena04a8f8c2017-11-23 11:45:24 +000054void CLDepthConcatenateLayerKernel::configure(const ICLTensor *input, unsigned int depth_offset, ICLTensor *output)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010055{
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
Vidhya Sudhan Loganathanf1f49062018-05-25 13:21:26 +010064 ARM_COMPUTE_ERROR_ON_F16_UNSUPPORTED(input);
Georgios Pinitasac4e8732017-07-05 17:02:25 +010065 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QS8, DataType::QS16, DataType::F16, DataType::F32);
66 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
67 ARM_COMPUTE_ERROR_ON_MISMATCHING_FIXED_POINT_POSITION(input, output);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010068 ARM_COMPUTE_ERROR_ON(input->info()->dimension(2) + depth_offset > output->info()->dimension(2));
69 ARM_COMPUTE_ERROR_ON(input->info()->dimension(0) > output->info()->dimension(0));
70 ARM_COMPUTE_ERROR_ON(input->info()->dimension(1) > output->info()->dimension(1));
71 ARM_COMPUTE_ERROR_ON_MISMATCHING_SHAPES(3, input, output);
Georgios Pinitasac4e8732017-07-05 17:02:25 +010072 ARM_COMPUTE_ERROR_ON(configs_map.find(input->info()->element_size()) == configs_map.end());
Anthony Barbier6ff3b192017-09-04 18:44:23 +010073
74 // The gaps between the two lowest dimensions of input and output need to be divisible by 2
75 // Otherwise it is not clear how the padding should be added onto the input tensor
76 ARM_COMPUTE_ERROR_ON((output->info()->dimension(0) - input->info()->dimension(0)) % 2);
77 ARM_COMPUTE_ERROR_ON((output->info()->dimension(1) - input->info()->dimension(1)) % 2);
78
Gian Marco Iodice906443f2017-09-06 15:09:54 +010079 _input = input;
80 _output = output;
81 _depth_offset = depth_offset;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010082
Georgios Pinitasac4e8732017-07-05 17:02:25 +010083 // Add build options
84 auto config = configs_map.find(static_cast<int>(input->info()->element_size()));
85 std::set<std::string> build_opts;
86 build_opts.emplace(("-DDATA_TYPE=" + config->second.first));
87 build_opts.emplace(("-DVEC_SIZE=" + support::cpp11::to_string(config->second.second)));
88
Anthony Barbier6ff3b192017-09-04 18:44:23 +010089 // Create kernel
Georgios Pinitasac4e8732017-07-05 17:02:25 +010090 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel("concatenate_depth", build_opts));
Anthony Barbier6ff3b192017-09-04 18:44:23 +010091
92 // Configure kernel window
93 _left_right = (output->info()->dimension(0) - input->info()->dimension(0)) / 2;
94 _top_bottom = (output->info()->dimension(1) - input->info()->dimension(1)) / 2;
95
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);
Diego Lopez Recasbcbc9702017-12-18 11:28:27 +0000107 output_access.set_valid_region(win, ValidRegion(Coordinates(), output->info()->tensor_shape()));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100108
Gian Marco Iodice906443f2017-09-06 15:09:54 +0100109 ICLKernel::configure(win);
110}
111
Giorgio Arena04a8f8c2017-11-23 11:45:24 +0000112void CLDepthConcatenateLayerKernel::run(const Window &window, cl::CommandQueue &queue)
Gian Marco Iodice906443f2017-09-06 15:09:54 +0100113{
114 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
115 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
116
117 Window slice = window.first_slice_window_3D();
118
119 const int offset_to_first_elements_in_bytes = _depth_offset * _output->info()->strides_in_bytes()[2];
120
Georgios Pinitasac4e8732017-07-05 17:02:25 +0100121 unsigned int idx = 2 * num_arguments_per_3D_tensor(); // Skip the input and output parameters
122 const cl_int3 offsets =
123 {
124 {
125 static_cast<cl_int>(_left_right),
126 static_cast<cl_int>(_top_bottom),
127 static_cast<cl_int>(offset_to_first_elements_in_bytes),
128 }
129 };
130 _kernel.setArg<cl_int3>(idx, offsets);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100131
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100132 do
133 {
134 unsigned int idx = 0;
Georgios Pinitasac4e8732017-07-05 17:02:25 +0100135 add_3D_tensor_argument(idx, _input, slice);
136 add_3D_tensor_argument(idx, _output, slice);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100137 enqueue(queue, *this, slice);
138 }
Georgios Pinitasac4e8732017-07-05 17:02:25 +0100139 while(window.slide_window_slice_3D(slice));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100140}