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/NEON/kernels/NEDepthConcatenateKernel.h" |
| 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" |
| 30 | #include "arm_compute/core/TensorInfo.h" |
| 31 | #include "arm_compute/core/Utils.h" |
| 32 | #include "arm_compute/core/Validate.h" |
| 33 | #include "arm_compute/core/Window.h" |
| 34 | |
| 35 | #include <arm_neon.h> |
| 36 | |
| 37 | using namespace arm_compute; |
| 38 | |
| 39 | NEDepthConcatenateKernel::NEDepthConcatenateKernel() |
| 40 | : _input(nullptr), _output(nullptr), _top_bottom(0), _left_right(0), _depth_offset(0) |
| 41 | { |
| 42 | } |
| 43 | |
| 44 | BorderSize NEDepthConcatenateKernel::border_size() const |
| 45 | { |
| 46 | return BorderSize(_top_bottom, _left_right); |
| 47 | } |
| 48 | |
| 49 | void NEDepthConcatenateKernel::configure(const ITensor *input, unsigned int depth_offset, ITensor *output) |
| 50 | { |
| 51 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F32); |
| 52 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::F32); |
| 53 | ARM_COMPUTE_ERROR_ON(input->info()->dimension(2) + depth_offset > output->info()->dimension(2)); |
| 54 | ARM_COMPUTE_ERROR_ON(input->info()->dimension(0) > output->info()->dimension(0)); |
| 55 | ARM_COMPUTE_ERROR_ON(input->info()->dimension(1) > output->info()->dimension(1)); |
| 56 | ARM_COMPUTE_ERROR_ON_MISMATCHING_SHAPES(3, input, output); |
| 57 | |
| 58 | // The gaps between the two lowest dimensions of input and output need to be divisible by 2 |
| 59 | // Otherwise it is not clear how the padding should be added onto the input tensor |
| 60 | ARM_COMPUTE_ERROR_ON((output->info()->dimension(0) - input->info()->dimension(0)) % 2); |
| 61 | ARM_COMPUTE_ERROR_ON((output->info()->dimension(1) - input->info()->dimension(1)) % 2); |
| 62 | |
| 63 | _input = input; |
| 64 | _output = output; |
| 65 | _depth_offset = depth_offset; |
| 66 | _left_right = (output->info()->dimension(0) - input->info()->dimension(0)) / 2; |
| 67 | _top_bottom = (output->info()->dimension(1) - input->info()->dimension(1)) / 2; |
| 68 | |
| 69 | const unsigned int num_elems_processed_per_iteration = 4; |
| 70 | const unsigned int num_elems_read_per_iteration = 4; |
| 71 | const unsigned int num_rows_read_per_iteration = 1; |
| 72 | |
| 73 | // The window needs to be based on input as we copy all the depths of input |
| 74 | Window win = calculate_max_enlarged_window(*input->info(), Steps(num_elems_processed_per_iteration), border_size()); |
| 75 | |
| 76 | AccessWindowRectangle input_access(input->info(), -_left_right, -_top_bottom, num_elems_read_per_iteration, num_rows_read_per_iteration); |
| 77 | AccessWindowHorizontal output_access(output->info(), 0, num_elems_processed_per_iteration); |
| 78 | update_window_and_padding(win, input_access, output_access); |
| 79 | output_access.set_valid_region(win, ValidRegion(Coordinates(0, 0), output->info()->tensor_shape())); |
| 80 | |
| 81 | INEKernel::configure(win); |
| 82 | } |
| 83 | |
| 84 | void NEDepthConcatenateKernel::run(const Window &window) |
| 85 | { |
| 86 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 87 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window); |
| 88 | |
| 89 | // Offset output |
| 90 | const unsigned int offset_to_first_elements_in_bytes = _output->info()->offset_first_element_in_bytes() + _left_right * _output->info()->strides_in_bytes()[0] + _top_bottom * |
| 91 | _output->info()->strides_in_bytes()[1] + _depth_offset * _output->info()->strides_in_bytes()[2]; |
| 92 | uint8_t *output_ptr = _output->buffer() + offset_to_first_elements_in_bytes; |
| 93 | |
| 94 | Iterator input(_input, window); |
| 95 | Iterator output(_output, window); |
| 96 | |
| 97 | execute_window_loop(window, [&](const Coordinates & id) |
| 98 | { |
| 99 | const auto in_ptr = reinterpret_cast<const float *>(input.ptr()); |
| 100 | const auto out_ptr = reinterpret_cast<float *>(output_ptr + output.offset()); |
| 101 | |
| 102 | vst1q_f32(out_ptr, vld1q_f32(in_ptr)); |
| 103 | }, |
| 104 | input, output); |
| 105 | } |