Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
| 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 Pinitas | ac4e873 | 2017-07-05 17:02:25 +0100 | [diff] [blame] | 38 | #include "support/ToolchainSupport.h" |
| 39 | |
| 40 | #include <map> |
| 41 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 42 | using namespace arm_compute; |
| 43 | |
| 44 | CLDepthConcatenateKernel::CLDepthConcatenateKernel() |
Gian Marco Iodice | 906443f | 2017-09-06 15:09:54 +0100 | [diff] [blame] | 45 | : _input(nullptr), _output(nullptr), _top_bottom(0), _left_right(0), _depth_offset(0) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 46 | { |
| 47 | } |
| 48 | |
| 49 | BorderSize CLDepthConcatenateKernel::border_size() const |
| 50 | { |
| 51 | return BorderSize(_top_bottom, _left_right); |
| 52 | } |
| 53 | |
| 54 | void CLDepthConcatenateKernel::configure(const ICLTensor *input, unsigned int depth_offset, ICLTensor *output) |
| 55 | { |
Georgios Pinitas | ac4e873 | 2017-07-05 17:02:25 +0100 | [diff] [blame] | 56 | 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 Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 67 | 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 Pinitas | ac4e873 | 2017-07-05 17:02:25 +0100 | [diff] [blame] | 71 | ARM_COMPUTE_ERROR_ON(configs_map.find(input->info()->element_size()) == configs_map.end()); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 72 | |
| 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 Iodice | 906443f | 2017-09-06 15:09:54 +0100 | [diff] [blame] | 78 | _input = input; |
| 79 | _output = output; |
| 80 | _depth_offset = depth_offset; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 81 | |
Georgios Pinitas | ac4e873 | 2017-07-05 17:02:25 +0100 | [diff] [blame] | 82 | // 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 Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 88 | // Create kernel |
Georgios Pinitas | ac4e873 | 2017-07-05 17:02:25 +0100 | [diff] [blame] | 89 | _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel("concatenate_depth", build_opts)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 90 | |
| 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 Pinitas | ac4e873 | 2017-07-05 17:02:25 +0100 | [diff] [blame] | 95 | 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 Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 97 | 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 Pinitas | ac4e873 | 2017-07-05 17:02:25 +0100 | [diff] [blame] | 100 | 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 Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 102 | |
Georgios Pinitas | ac4e873 | 2017-07-05 17:02:25 +0100 | [diff] [blame] | 103 | AccessWindowRectangle input_access(input->info(), -_left_right, -_top_bottom, num_elems_read_per_iteration, num_rows_read_per_iteration); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 104 | AccessWindowHorizontal output_access(output->info(), 0, num_elems_processed_per_iteration); |
Georgios Pinitas | ac4e873 | 2017-07-05 17:02:25 +0100 | [diff] [blame] | 105 | update_window_and_padding(win, input_access, output_access); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 106 | output_access.set_valid_region(win, ValidRegion(Coordinates(0, 0), output->info()->tensor_shape())); |
| 107 | |
Gian Marco Iodice | 906443f | 2017-09-06 15:09:54 +0100 | [diff] [blame] | 108 | ICLKernel::configure(win); |
| 109 | } |
| 110 | |
| 111 | void 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 Pinitas | ac4e873 | 2017-07-05 17:02:25 +0100 | [diff] [blame] | 120 | 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 Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 130 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 131 | do |
| 132 | { |
| 133 | unsigned int idx = 0; |
Georgios Pinitas | ac4e873 | 2017-07-05 17:02:25 +0100 | [diff] [blame] | 134 | add_3D_tensor_argument(idx, _input, slice); |
| 135 | add_3D_tensor_argument(idx, _output, slice); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 136 | enqueue(queue, *this, slice); |
| 137 | } |
Georgios Pinitas | ac4e873 | 2017-07-05 17:02:25 +0100 | [diff] [blame] | 138 | while(window.slide_window_slice_3D(slice)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 139 | } |