Georgios Pinitas | ae54e02 | 2018-07-16 15:41:27 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2018 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/NEWidthConcatenateLayerKernel.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/NEON/wrapper/wrapper.h" |
| 31 | #include "arm_compute/core/TensorInfo.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 <cstdint> |
| 37 | |
| 38 | using namespace arm_compute; |
| 39 | |
| 40 | namespace |
| 41 | { |
| 42 | std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, unsigned int width_offset, ITensorInfo *output) |
| 43 | { |
| 44 | const unsigned int num_elems_processed_per_iteration = 16 / output->element_size(); |
| 45 | |
| 46 | // The window needs to be based on input as we copy all the widths of input |
| 47 | Window win = calculate_max_window(*input, Steps(num_elems_processed_per_iteration)); |
| 48 | AccessWindowHorizontal input_access(input, 0, num_elems_processed_per_iteration); |
| 49 | AccessWindowHorizontal output_access(output, width_offset, num_elems_processed_per_iteration); |
| 50 | bool window_changed = update_window_and_padding(win, input_access, output_access); |
| 51 | |
| 52 | Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{}; |
| 53 | return std::make_pair(err, win); |
| 54 | } |
| 55 | |
| 56 | Status validate_arguments(const ITensorInfo *input, unsigned int width_offset, const ITensorInfo *output) |
| 57 | { |
| 58 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output); |
| 59 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, |
| 60 | DataType::U8, DataType::S8, DataType::QASYMM8, |
| 61 | DataType::U16, DataType::S16, DataType::F16, |
| 62 | DataType::U32, DataType::F32); |
| 63 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
| 64 | ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(0) + width_offset > output->dimension(0)); |
| 65 | |
| 66 | for(size_t i = 1; i < Coordinates::num_max_dimensions; ++i) |
| 67 | { |
| 68 | ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(i) != output->dimension(i)); |
| 69 | } |
Georgios Pinitas | ae54e02 | 2018-07-16 15:41:27 +0100 | [diff] [blame] | 70 | |
| 71 | return Status{}; |
| 72 | } |
| 73 | } // namespace |
| 74 | |
| 75 | NEWidthConcatenateLayerKernel::NEWidthConcatenateLayerKernel() |
| 76 | : _input(nullptr), _output(nullptr), _width_offset(0) |
| 77 | { |
| 78 | } |
| 79 | |
| 80 | void NEWidthConcatenateLayerKernel::configure(const ITensor *input, unsigned int width_offset, ITensor *output) |
| 81 | { |
| 82 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); |
| 83 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), width_offset, output->info())); |
| 84 | |
| 85 | _input = input; |
| 86 | _output = output; |
| 87 | _width_offset = width_offset; |
| 88 | |
| 89 | // Configure kernel window |
| 90 | auto win_config = validate_and_configure_window(input->info(), width_offset, output->info()); |
| 91 | ARM_COMPUTE_ERROR_THROW_ON(std::get<0>(win_config)); |
| 92 | |
| 93 | INEKernel::configure(std::get<1>(win_config)); |
| 94 | } |
| 95 | |
| 96 | Status NEWidthConcatenateLayerKernel::validate(const ITensorInfo *input, unsigned int width_offset, const ITensorInfo *output) |
| 97 | { |
| 98 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, width_offset, output)); |
| 99 | ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), width_offset, output->clone().get()).first); |
| 100 | return Status{}; |
| 101 | } |
| 102 | |
| 103 | void NEWidthConcatenateLayerKernel::run(const Window &window, const ThreadInfo &info) |
| 104 | { |
| 105 | ARM_COMPUTE_UNUSED(info); |
| 106 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 107 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window); |
| 108 | |
| 109 | // Offset output pointer to the correct position |
| 110 | uint8_t *output_ptr = _output->buffer() + _output->info()->offset_first_element_in_bytes() + _width_offset * _output->info()->strides_in_bytes()[0]; |
| 111 | |
| 112 | // Create iterators |
| 113 | Iterator input(_input, window); |
| 114 | Iterator output(_output, window); |
| 115 | |
| 116 | execute_window_loop(window, [&](const Coordinates & id) |
| 117 | { |
| 118 | const auto in_ptr = input.ptr(); |
| 119 | const auto out_ptr = output_ptr + output.offset(); |
| 120 | |
| 121 | wrapper::vstore(out_ptr, wrapper::vloadq(in_ptr)); |
| 122 | }, |
| 123 | input, output); |
| 124 | } |