Michalis Spyrou | 1d38259 | 2018-08-13 17:28:04 +0100 | [diff] [blame] | 1 | /* |
Gian Marco Iodice | e214fcf | 2019-03-15 09:12:40 +0000 | [diff] [blame] | 2 | * Copyright (c) 2018-2019 ARM Limited. |
Michalis Spyrou | 1d38259 | 2018-08-13 17:28:04 +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 | */ |
| 24 | #include "arm_compute/core/NEON/kernels/NECopyKernel.h" |
| 25 | |
| 26 | #include "arm_compute/core/Error.h" |
| 27 | #include "arm_compute/core/Helpers.h" |
| 28 | #include "arm_compute/core/ITensor.h" |
| 29 | #include "arm_compute/core/TensorInfo.h" |
| 30 | #include "arm_compute/core/Validate.h" |
| 31 | #include "arm_compute/core/Window.h" |
Pablo Tello | c9564cb | 2019-09-13 10:20:25 +0100 | [diff] [blame] | 32 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
Michalis Spyrou | 1d38259 | 2018-08-13 17:28:04 +0100 | [diff] [blame] | 33 | |
Pablo Tello | c9564cb | 2019-09-13 10:20:25 +0100 | [diff] [blame] | 34 | namespace arm_compute |
| 35 | { |
| 36 | namespace |
| 37 | { |
| 38 | Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, const PaddingList &padding = PaddingList()) |
| 39 | { |
| 40 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output); |
| 41 | ARM_COMPUTE_RETURN_ERROR_ON(padding.size() > 4); |
| 42 | |
| 43 | // Validate output if initialized |
| 44 | if(output->total_size() != 0) |
| 45 | { |
| 46 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(misc::shape_calculator::compute_padded_shape(input->tensor_shape(), padding), output->tensor_shape()); |
| 47 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
| 48 | } |
| 49 | |
| 50 | return Status{}; |
| 51 | } |
| 52 | |
| 53 | std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output) |
| 54 | { |
| 55 | // Output auto inizialitation if not yet initialized |
| 56 | auto_init_if_empty(*output, *input); |
| 57 | return std::make_pair(Status{}, calculate_max_window(*output)); |
| 58 | } |
| 59 | |
| 60 | std::pair<Status, Window> validate_and_configure_window_with_padding(ITensorInfo *input, ITensorInfo *output, const PaddingList &padding) |
| 61 | { |
| 62 | const TensorShape input_shape = input->tensor_shape(); |
| 63 | const TensorShape padded_shape = misc::shape_calculator::compute_padded_shape(input_shape, padding); |
| 64 | auto_init_if_empty(*output, input->clone()->set_tensor_shape(padded_shape)); |
| 65 | // Configure window |
| 66 | const Window win = calculate_max_window(*output, output->dimension(0)); |
| 67 | return std::make_pair(Status{}, win); |
| 68 | } |
| 69 | |
| 70 | } // namespace |
Michalis Spyrou | 1d38259 | 2018-08-13 17:28:04 +0100 | [diff] [blame] | 71 | |
| 72 | NECopyKernel::NECopyKernel() |
Pablo Tello | c9564cb | 2019-09-13 10:20:25 +0100 | [diff] [blame] | 73 | : _input(nullptr), _output(nullptr), _padding() |
Michalis Spyrou | 1d38259 | 2018-08-13 17:28:04 +0100 | [diff] [blame] | 74 | { |
| 75 | } |
| 76 | |
Pablo Tello | c9564cb | 2019-09-13 10:20:25 +0100 | [diff] [blame] | 77 | void NECopyKernel::configure(const ITensor *input, ITensor *output, const PaddingList &padding) |
Michalis Spyrou | 1d38259 | 2018-08-13 17:28:04 +0100 | [diff] [blame] | 78 | { |
| 79 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); |
Pablo Tello | c9564cb | 2019-09-13 10:20:25 +0100 | [diff] [blame] | 80 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), padding)); |
Michalis Spyrou | 1d38259 | 2018-08-13 17:28:04 +0100 | [diff] [blame] | 81 | |
Pablo Tello | c9564cb | 2019-09-13 10:20:25 +0100 | [diff] [blame] | 82 | _input = input; |
| 83 | _output = output; |
| 84 | _padding = padding; |
Michalis Spyrou | 1d38259 | 2018-08-13 17:28:04 +0100 | [diff] [blame] | 85 | |
Pablo Tello | c9564cb | 2019-09-13 10:20:25 +0100 | [diff] [blame] | 86 | std::pair<Status, Window> win_config; |
| 87 | |
| 88 | if(padding.empty()) |
| 89 | { |
| 90 | win_config = validate_and_configure_window(input->info(), output->info()); |
| 91 | } |
| 92 | else |
| 93 | { |
| 94 | win_config = validate_and_configure_window_with_padding(input->info(), output->info(), padding); |
| 95 | } |
| 96 | |
| 97 | ARM_COMPUTE_ERROR_THROW_ON(win_config.first); |
| 98 | INEKernel::configure(win_config.second); |
Michalis Spyrou | 1d38259 | 2018-08-13 17:28:04 +0100 | [diff] [blame] | 99 | } |
| 100 | |
Pablo Tello | c9564cb | 2019-09-13 10:20:25 +0100 | [diff] [blame] | 101 | Status NECopyKernel::validate(const arm_compute::ITensorInfo *input, const arm_compute::ITensorInfo *output, const PaddingList &padding) |
Michalis Spyrou | 1d38259 | 2018-08-13 17:28:04 +0100 | [diff] [blame] | 102 | { |
Pablo Tello | c9564cb | 2019-09-13 10:20:25 +0100 | [diff] [blame] | 103 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, padding)); |
| 104 | |
| 105 | if(padding.empty()) |
| 106 | { |
| 107 | ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), output->clone().get()).first); |
| 108 | } |
| 109 | else |
| 110 | { |
| 111 | ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window_with_padding(input->clone().get(), output->clone().get(), padding).first); |
| 112 | } |
| 113 | |
Michalis Spyrou | 1d38259 | 2018-08-13 17:28:04 +0100 | [diff] [blame] | 114 | return Status{}; |
| 115 | } |
| 116 | |
| 117 | void NECopyKernel::run(const Window &window, const ThreadInfo &info) |
| 118 | { |
| 119 | ARM_COMPUTE_UNUSED(info); |
| 120 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 121 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window); |
| 122 | |
Pablo Tello | c9564cb | 2019-09-13 10:20:25 +0100 | [diff] [blame] | 123 | if(_padding.empty()) |
Michalis Spyrou | 1d38259 | 2018-08-13 17:28:04 +0100 | [diff] [blame] | 124 | { |
Pablo Tello | c9564cb | 2019-09-13 10:20:25 +0100 | [diff] [blame] | 125 | Window output_window{ window }; |
| 126 | output_window.set(Window::DimX, Window::Dimension(output_window.x().start(), output_window.x().end(), _input->info()->dimension(0))); |
| 127 | Window out_slice = output_window.first_slice_window_1D(); |
| 128 | do |
Michalis Spyrou | 1d38259 | 2018-08-13 17:28:04 +0100 | [diff] [blame] | 129 | { |
Pablo Tello | c9564cb | 2019-09-13 10:20:25 +0100 | [diff] [blame] | 130 | Iterator input_it(_input, out_slice); |
| 131 | Iterator output_it(_output, out_slice); |
| 132 | |
| 133 | execute_window_loop(out_slice, [&](const Coordinates &) |
| 134 | { |
| 135 | memcpy(output_it.ptr(), input_it.ptr(), _output->info()->dimension(0) * _output->info()->element_size()); |
| 136 | }, |
| 137 | input_it, output_it); |
| 138 | } |
| 139 | while(output_window.slide_window_slice_1D(out_slice)); |
| 140 | } |
| 141 | else |
| 142 | { |
| 143 | Window input_window{ window }; |
| 144 | input_window.set(Window::DimX, Window::Dimension(0, window.x().end() - _padding[0].first, _input->info()->dimension(0))); |
| 145 | |
| 146 | Iterator input_it(_input, input_window); |
| 147 | Iterator output_it(_output, window); |
| 148 | const size_t row_size_in_bytes = _input->info()->dimension(0) * _input->info()->element_size(); |
| 149 | execute_window_loop(window, [&](const Coordinates &) |
| 150 | { |
| 151 | auto dst_ptr = output_it.ptr() + _padding[0].first * _output->info()->element_size(); |
| 152 | std::memcpy(dst_ptr, input_it.ptr(), row_size_in_bytes); |
Michalis Spyrou | 1d38259 | 2018-08-13 17:28:04 +0100 | [diff] [blame] | 153 | }, |
| 154 | input_it, output_it); |
Michalis Spyrou | 1d38259 | 2018-08-13 17:28:04 +0100 | [diff] [blame] | 155 | } |
Michalis Spyrou | 1d38259 | 2018-08-13 17:28:04 +0100 | [diff] [blame] | 156 | } |
Pablo Tello | c9564cb | 2019-09-13 10:20:25 +0100 | [diff] [blame] | 157 | } // namespace arm_compute |