Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 1 | /* |
Joel Liang | abd03cf | 2018-01-08 15:20:48 +0800 | [diff] [blame^] | 2 | * Copyright (c) 2017, 2018 ARM Limited. |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 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 | */ |
Giorgio Arena | 04a8f8c | 2017-11-23 11:45:24 +0000 | [diff] [blame] | 24 | #include "arm_compute/core/GLES_COMPUTE/kernels/GCDepthConcatenateLayerKernel.h" |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 25 | |
| 26 | #include "arm_compute/core/Error.h" |
| 27 | #include "arm_compute/core/GLES_COMPUTE/GCHelpers.h" |
| 28 | #include "arm_compute/core/GLES_COMPUTE/GCKernelLibrary.h" |
| 29 | #include "arm_compute/core/GLES_COMPUTE/IGCTensor.h" |
| 30 | #include "arm_compute/core/GLES_COMPUTE/OpenGLES.h" |
| 31 | #include "arm_compute/core/Helpers.h" |
| 32 | #include "arm_compute/core/Utils.h" |
| 33 | #include "arm_compute/core/Validate.h" |
| 34 | #include "arm_compute/core/Window.h" |
| 35 | |
| 36 | #include "support/ToolchainSupport.h" |
| 37 | |
| 38 | using namespace arm_compute; |
| 39 | |
Giorgio Arena | 04a8f8c | 2017-11-23 11:45:24 +0000 | [diff] [blame] | 40 | GCDepthConcatenateLayerKernel::GCDepthConcatenateLayerKernel() |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 41 | : _input(nullptr), _output(nullptr), _top_bottom(0), _left_right(0) |
| 42 | { |
| 43 | } |
| 44 | |
Giorgio Arena | 04a8f8c | 2017-11-23 11:45:24 +0000 | [diff] [blame] | 45 | BorderSize GCDepthConcatenateLayerKernel::border_size() const |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 46 | { |
| 47 | return BorderSize(_top_bottom, _left_right); |
| 48 | } |
| 49 | |
Giorgio Arena | 04a8f8c | 2017-11-23 11:45:24 +0000 | [diff] [blame] | 50 | void GCDepthConcatenateLayerKernel::configure(const IGCTensor *input, unsigned int depth_offset, IGCTensor *output) |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 51 | { |
| 52 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F16, DataType::F32); |
| 53 | ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
| 54 | ARM_COMPUTE_ERROR_ON(input->info()->dimension(2) + depth_offset > output->info()->dimension(2)); |
| 55 | ARM_COMPUTE_ERROR_ON(input->info()->dimension(0) > output->info()->dimension(0)); |
| 56 | ARM_COMPUTE_ERROR_ON(input->info()->dimension(1) > output->info()->dimension(1)); |
| 57 | ARM_COMPUTE_ERROR_ON_MISMATCHING_SHAPES(3, input, output); |
| 58 | |
| 59 | // The gaps between the two lowest dimensions of input and output need to be divisible by 2 |
| 60 | // Otherwise it is not clear how the padding should be added onto the input tensor |
| 61 | ARM_COMPUTE_ERROR_ON((output->info()->dimension(0) - input->info()->dimension(0)) % 2); |
| 62 | ARM_COMPUTE_ERROR_ON((output->info()->dimension(1) - input->info()->dimension(1)) % 2); |
| 63 | |
| 64 | _input = input; |
| 65 | _output = output; |
| 66 | |
| 67 | // Add build options |
| 68 | std::set<std::string> build_opts; |
| 69 | std::string dt_name = (input->info()->data_type() == DataType::F32) ? "DATA_TYPE_FP32" : "DATA_TYPE_FP16"; |
| 70 | build_opts.emplace(("#define " + dt_name)); |
| 71 | build_opts.emplace("#define LOCAL_SIZE_X " + support::cpp11::to_string(1)); |
| 72 | build_opts.emplace("#define LOCAL_SIZE_Y " + support::cpp11::to_string(1)); |
| 73 | build_opts.emplace("#define LOCAL_SIZE_Z " + support::cpp11::to_string(1)); |
| 74 | |
| 75 | // Configure kernel window |
| 76 | _left_right = (output->info()->dimension(0) - input->info()->dimension(0)) / 2; |
| 77 | _top_bottom = (output->info()->dimension(1) - input->info()->dimension(1)) / 2; |
| 78 | |
| 79 | const int offset_to_first_elements_in_bytes = depth_offset * output->info()->strides_in_bytes()[2]; |
| 80 | |
| 81 | build_opts.emplace("#define OFFSETS_X " + support::cpp11::to_string(_left_right)); |
| 82 | build_opts.emplace("#define OFFSETS_Y " + support::cpp11::to_string(_top_bottom)); |
| 83 | build_opts.emplace("#define OFFSETS_Z " + support::cpp11::to_string(offset_to_first_elements_in_bytes)); |
| 84 | |
| 85 | // Create kernel |
| 86 | _kernel = static_cast<GCKernel>(GCKernelLibrary::get().create_kernel("concatenate_depth", build_opts)); |
| 87 | |
| 88 | unsigned int num_elems_processed_per_iteration = 1; |
| 89 | unsigned int num_elems_read_per_iteration = 1; |
| 90 | if(input->info()->data_type() == DataType::F32) |
| 91 | { |
| 92 | num_elems_processed_per_iteration = 1; |
| 93 | num_elems_read_per_iteration = 1; |
| 94 | } |
| 95 | else if(input->info()->data_type() == DataType::F16) |
| 96 | { |
| 97 | num_elems_processed_per_iteration = 4; |
| 98 | num_elems_read_per_iteration = 4; |
| 99 | } |
| 100 | const unsigned int num_rows_read_per_iteration = 1; |
| 101 | |
| 102 | // The window needs to be based on input as we copy all the depths of input |
| 103 | Window win = calculate_max_window(*output->info(), Steps(num_elems_processed_per_iteration)); |
| 104 | win.set(Window::DimZ, Window::Dimension(0, input->info()->tensor_shape().z(), 1)); |
| 105 | |
| 106 | AccessWindowRectangle input_access(input->info(), -_left_right, -_top_bottom, num_elems_read_per_iteration, num_rows_read_per_iteration); |
| 107 | AccessWindowHorizontal output_access(output->info(), 0, num_elems_processed_per_iteration); |
| 108 | update_window_and_padding(win, input_access, output_access); |
| 109 | output_access.set_valid_region(win, ValidRegion(Coordinates(0, 0), output->info()->tensor_shape())); |
| 110 | |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 111 | IGCKernel::configure(win); |
| 112 | } |
| 113 | |
Giorgio Arena | 04a8f8c | 2017-11-23 11:45:24 +0000 | [diff] [blame] | 114 | void GCDepthConcatenateLayerKernel::run(const Window &window) |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 115 | { |
| 116 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 117 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IGCKernel::window(), window); |
| 118 | |
| 119 | _kernel.use(); |
| 120 | |
| 121 | Window slice = window.first_slice_window_3D(); |
| 122 | |
| 123 | do |
| 124 | { |
Joel Liang | abd03cf | 2018-01-08 15:20:48 +0800 | [diff] [blame^] | 125 | unsigned int idx = 0; |
| 126 | add_3D_tensor_argument(idx, _input, 1, slice); |
| 127 | add_3D_tensor_argument(idx, _output, 2, slice); |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 128 | |
| 129 | _kernel.update_shader_params(); |
| 130 | |
| 131 | enqueue(*this, slice); |
| 132 | } |
| 133 | while(window.slide_window_slice_3D(slice)); |
| 134 | } |