Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame] | 2 | * Copyright (c) 2017-2018 ARM Limited. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +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/NEON/kernels/NEDepthConcatenateLayerKernel.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 25 | |
| 26 | #include "arm_compute/core/Error.h" |
| 27 | #include "arm_compute/core/Helpers.h" |
| 28 | #include "arm_compute/core/IAccessWindow.h" |
| 29 | #include "arm_compute/core/ITensor.h" |
Georgios Pinitas | ac4e873 | 2017-07-05 17:02:25 +0100 | [diff] [blame] | 30 | #include "arm_compute/core/NEON/NEFixedPoint.h" |
Georgios Pinitas | ae54e02 | 2018-07-16 15:41:27 +0100 | [diff] [blame] | 31 | #include "arm_compute/core/NEON/wrapper/wrapper.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 32 | #include "arm_compute/core/TensorInfo.h" |
| 33 | #include "arm_compute/core/Utils.h" |
| 34 | #include "arm_compute/core/Validate.h" |
| 35 | #include "arm_compute/core/Window.h" |
| 36 | |
Georgios Pinitas | ac4e873 | 2017-07-05 17:02:25 +0100 | [diff] [blame] | 37 | #include <cstdint> |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 38 | |
| 39 | using namespace arm_compute; |
| 40 | |
Georgios Pinitas | ac4e873 | 2017-07-05 17:02:25 +0100 | [diff] [blame] | 41 | namespace |
| 42 | { |
Georgios Pinitas | ac4e873 | 2017-07-05 17:02:25 +0100 | [diff] [blame] | 43 | template <typename T> |
| 44 | void depth_concat(const ITensor *in, ITensor *out, std::pair<int, int> start_xy, int depth_offset, const Window &window) |
| 45 | { |
| 46 | const int start_x = start_xy.first; |
| 47 | const int start_y = start_xy.second; |
| 48 | |
| 49 | // Offset input |
| 50 | const int input_offset_to_first_elements_in_bytes = in->info()->offset_first_element_in_bytes() - start_x * in->info()->strides_in_bytes()[0] - start_y * in->info()->strides_in_bytes()[1]; |
| 51 | uint8_t *input_ptr = in->buffer() + input_offset_to_first_elements_in_bytes; |
| 52 | |
| 53 | // Offset output |
| 54 | const unsigned int output_offset_to_first_elements_in_bytes = out->info()->offset_first_element_in_bytes() + depth_offset * out->info()->strides_in_bytes()[2]; |
| 55 | uint8_t *output_ptr = out->buffer() + output_offset_to_first_elements_in_bytes; |
| 56 | |
| 57 | Iterator input(in, window); |
| 58 | Iterator output(out, window); |
| 59 | |
| 60 | execute_window_loop(window, [&](const Coordinates & id) |
| 61 | { |
| 62 | const auto in_ptr = reinterpret_cast<const T *>(input_ptr + input.offset()); |
| 63 | const auto out_ptr = reinterpret_cast<T *>(output_ptr + output.offset()); |
| 64 | |
Georgios Pinitas | ae54e02 | 2018-07-16 15:41:27 +0100 | [diff] [blame] | 65 | wrapper::vstore(out_ptr, wrapper::vloadq(in_ptr)); |
Georgios Pinitas | ac4e873 | 2017-07-05 17:02:25 +0100 | [diff] [blame] | 66 | }, |
| 67 | input, output); |
| 68 | } |
Georgios Pinitas | ae54e02 | 2018-07-16 15:41:27 +0100 | [diff] [blame] | 69 | |
| 70 | std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, unsigned int depth_offset, ITensorInfo *output) |
| 71 | { |
| 72 | ARM_COMPUTE_UNUSED(depth_offset); |
| 73 | |
| 74 | // Configure kernel window |
| 75 | const int left_right = (output->dimension(0) - input->dimension(0)) / 2; |
| 76 | const int top_bottom = (output->dimension(1) - input->dimension(1)) / 2; |
| 77 | |
| 78 | const unsigned int num_elems_processed_per_iteration = 16 / input->element_size(); |
| 79 | const unsigned int num_elems_read_per_iteration = 16 / input->element_size(); |
| 80 | const unsigned int num_rows_read_per_iteration = 1; |
| 81 | |
| 82 | // The window needs to be based on input as we copy all the depths of input |
| 83 | Window win = calculate_max_window(*output, Steps(num_elems_processed_per_iteration)); |
| 84 | win.set(Window::DimZ, Window::Dimension(0, input->tensor_shape().z(), 1)); |
| 85 | |
| 86 | AccessWindowRectangle input_access(input, -left_right, -top_bottom, num_elems_read_per_iteration, num_rows_read_per_iteration); |
| 87 | AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration); |
| 88 | bool window_changed = update_window_and_padding(win, input_access, output_access); |
| 89 | output_access.set_valid_region(win, ValidRegion(Coordinates(), output->tensor_shape())); |
| 90 | |
| 91 | Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{}; |
| 92 | return std::make_pair(err, win); |
| 93 | } |
| 94 | |
| 95 | Status validate_arguments(const ITensorInfo *input, unsigned int depth_offset, const ITensorInfo *output) |
| 96 | { |
| 97 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output); |
Anthony Barbier | eaefd00 | 2018-07-20 17:49:35 +0100 | [diff] [blame] | 98 | //Note: ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(input) is not needed here as this kernel doesn't use NEON FP16 instructions. |
Georgios Pinitas | ae54e02 | 2018-07-16 15:41:27 +0100 | [diff] [blame] | 99 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::F16, DataType::F32); |
| 100 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
| 101 | |
| 102 | ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(2) + depth_offset > output->dimension(2)); |
| 103 | ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(0) > output->dimension(0)); |
| 104 | ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(1) > output->dimension(1)); |
| 105 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(3, input, output); |
| 106 | |
| 107 | // The gaps between the two lowest dimensions of input and output need to be divisible by 2 |
| 108 | // Otherwise it is not clear how the padding should be added onto the input tensor |
| 109 | ARM_COMPUTE_RETURN_ERROR_ON((output->dimension(0) - input->dimension(0)) % 2); |
| 110 | ARM_COMPUTE_RETURN_ERROR_ON((output->dimension(1) - input->dimension(1)) % 2); |
| 111 | |
| 112 | return Status{}; |
| 113 | } |
Georgios Pinitas | ac4e873 | 2017-07-05 17:02:25 +0100 | [diff] [blame] | 114 | } // namespace |
| 115 | |
Giorgio Arena | 04a8f8c | 2017-11-23 11:45:24 +0000 | [diff] [blame] | 116 | NEDepthConcatenateLayerKernel::NEDepthConcatenateLayerKernel() |
Georgios Pinitas | ac4e873 | 2017-07-05 17:02:25 +0100 | [diff] [blame] | 117 | : _func(nullptr), _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] | 118 | { |
| 119 | } |
| 120 | |
Giorgio Arena | 04a8f8c | 2017-11-23 11:45:24 +0000 | [diff] [blame] | 121 | BorderSize NEDepthConcatenateLayerKernel::border_size() const |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 122 | { |
| 123 | return BorderSize(_top_bottom, _left_right); |
| 124 | } |
| 125 | |
Giorgio Arena | 04a8f8c | 2017-11-23 11:45:24 +0000 | [diff] [blame] | 126 | void NEDepthConcatenateLayerKernel::configure(const ITensor *input, unsigned int depth_offset, ITensor *output) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 127 | { |
Georgios Pinitas | ae54e02 | 2018-07-16 15:41:27 +0100 | [diff] [blame] | 128 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); |
| 129 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), depth_offset, output->info())); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 130 | |
Georgios Pinitas | ac4e873 | 2017-07-05 17:02:25 +0100 | [diff] [blame] | 131 | _func = nullptr; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 132 | _input = input; |
| 133 | _output = output; |
| 134 | _depth_offset = depth_offset; |
| 135 | _left_right = (output->info()->dimension(0) - input->info()->dimension(0)) / 2; |
| 136 | _top_bottom = (output->info()->dimension(1) - input->info()->dimension(1)) / 2; |
| 137 | |
Georgios Pinitas | ac4e873 | 2017-07-05 17:02:25 +0100 | [diff] [blame] | 138 | switch(input->info()->data_type()) |
| 139 | { |
Georgios Pinitas | ae54e02 | 2018-07-16 15:41:27 +0100 | [diff] [blame] | 140 | case DataType::QASYMM8: |
| 141 | _func = &depth_concat<uint8_t>; |
| 142 | break; |
Georgios Pinitas | ac4e873 | 2017-07-05 17:02:25 +0100 | [diff] [blame] | 143 | case DataType::F16: |
| 144 | _func = &depth_concat<uint16_t>; |
| 145 | break; |
| 146 | case DataType::F32: |
| 147 | _func = &depth_concat<uint32_t>; |
| 148 | break; |
| 149 | default: |
| 150 | ARM_COMPUTE_ERROR("Unsupported data type."); |
| 151 | } |
| 152 | |
Georgios Pinitas | ae54e02 | 2018-07-16 15:41:27 +0100 | [diff] [blame] | 153 | // Configure kernel window |
| 154 | auto win_config = validate_and_configure_window(input->info(), depth_offset, output->info()); |
| 155 | ARM_COMPUTE_ERROR_THROW_ON(std::get<0>(win_config)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 156 | |
Georgios Pinitas | ae54e02 | 2018-07-16 15:41:27 +0100 | [diff] [blame] | 157 | INEKernel::configure(std::get<1>(win_config)); |
| 158 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 159 | |
Georgios Pinitas | ae54e02 | 2018-07-16 15:41:27 +0100 | [diff] [blame] | 160 | Status NEDepthConcatenateLayerKernel::validate(const arm_compute::ITensorInfo *input, |
| 161 | unsigned int depth_offset, |
| 162 | const arm_compute::ITensorInfo *output) |
| 163 | { |
| 164 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, depth_offset, output)); |
| 165 | ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), depth_offset, output->clone().get()).first); |
| 166 | return Status{}; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 167 | } |
| 168 | |
Giorgio Arena | 04a8f8c | 2017-11-23 11:45:24 +0000 | [diff] [blame] | 169 | void NEDepthConcatenateLayerKernel::run(const Window &window, const ThreadInfo &info) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 170 | { |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 171 | ARM_COMPUTE_UNUSED(info); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 172 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 173 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window); |
Georgios Pinitas | ac4e873 | 2017-07-05 17:02:25 +0100 | [diff] [blame] | 174 | ARM_COMPUTE_ERROR_ON(_func == nullptr); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 175 | |
Georgios Pinitas | ac4e873 | 2017-07-05 17:02:25 +0100 | [diff] [blame] | 176 | (*_func)(_input, _output, std::make_pair(_left_right, _top_bottom), _depth_offset, window); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 177 | } |