Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Isabella Gottardi | 0a1090a | 2019-02-14 18:07:36 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2019 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 | */ |
| 24 | #include "arm_compute/core/NEON/kernels/NECol2ImKernel.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/Types.h" |
| 31 | #include "arm_compute/core/Validate.h" |
Giorgio Arena | 226e4b9 | 2018-08-23 12:00:02 +0100 | [diff] [blame] | 32 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 33 | |
| 34 | #include <arm_neon.h> |
| 35 | #include <cstddef> |
| 36 | #include <cstdint> |
| 37 | |
| 38 | using namespace arm_compute; |
Giorgio Arena | 226e4b9 | 2018-08-23 12:00:02 +0100 | [diff] [blame] | 39 | using namespace misc::shape_calculator; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 40 | |
Georgios Pinitas | d912fd8 | 2017-11-27 21:00:13 +0000 | [diff] [blame] | 41 | namespace |
| 42 | { |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 43 | Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, const Size2D &convolved_dims) |
Georgios Pinitas | d912fd8 | 2017-11-27 21:00:13 +0000 | [diff] [blame] | 44 | { |
Anthony Barbier | eaefd00 | 2018-07-20 17:49:35 +0100 | [diff] [blame] | 45 | //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 | 3384356 | 2019-12-10 13:33:18 +0000 | [diff] [blame] | 46 | ARM_COMPUTE_RETURN_ERROR_ON(input->data_type() == DataType::UNKNOWN); |
Georgios Pinitas | d912fd8 | 2017-11-27 21:00:13 +0000 | [diff] [blame] | 47 | |
| 48 | // Validate configured output |
| 49 | if(output->total_size() != 0) |
| 50 | { |
Giorgio Arena | 226e4b9 | 2018-08-23 12:00:02 +0100 | [diff] [blame] | 51 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(output->tensor_shape(), compute_col2im_shape(*input, convolved_dims, false)); |
Georgios Pinitas | d912fd8 | 2017-11-27 21:00:13 +0000 | [diff] [blame] | 52 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
Isabella Gottardi | 0a1090a | 2019-02-14 18:07:36 +0000 | [diff] [blame] | 53 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(input, output); |
Georgios Pinitas | d912fd8 | 2017-11-27 21:00:13 +0000 | [diff] [blame] | 54 | } |
| 55 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 56 | return Status{}; |
Georgios Pinitas | d912fd8 | 2017-11-27 21:00:13 +0000 | [diff] [blame] | 57 | } |
Giorgio Arena | 226e4b9 | 2018-08-23 12:00:02 +0100 | [diff] [blame] | 58 | |
| 59 | std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output, const Size2D &convolved_dims) |
| 60 | { |
| 61 | // Output auto inizialitation if not yet initialized |
| 62 | auto_init_if_empty(*output, input->clone()->set_tensor_shape(compute_col2im_shape(*input, convolved_dims, false))); |
| 63 | |
| 64 | // Configure kernel window |
| 65 | Window win = calculate_max_window(*input, Steps()); |
| 66 | |
| 67 | // The NECol2ImKernel doesn't need padding so update_window_and_padding() can be skipped |
| 68 | Coordinates coord; |
| 69 | coord.set_num_dimensions(output->num_dimensions()); |
| 70 | output->set_valid_region(ValidRegion(coord, output->tensor_shape())); |
| 71 | |
| 72 | return std::make_pair(Status{}, win); |
| 73 | } |
Georgios Pinitas | d912fd8 | 2017-11-27 21:00:13 +0000 | [diff] [blame] | 74 | } // namespace |
| 75 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 76 | template <typename T> |
| 77 | void NECol2ImKernel::run_col2im(const Window &window) |
| 78 | { |
| 79 | const int output_stride_x = _output->info()->strides_in_bytes().x(); |
| 80 | const int output_stride_y = _output->info()->strides_in_bytes().y(); |
| 81 | const int output_stride_z = _output->info()->strides_in_bytes().z(); |
| 82 | |
| 83 | Window window_out(window); |
| 84 | window_out.set(Window::DimX, Window::Dimension(0, 0, 0)); |
| 85 | window_out.set(Window::DimY, Window::Dimension(0, 0, 0)); |
| 86 | window_out.set(Window::DimZ, Window::Dimension(0, 0, 0)); |
| 87 | |
| 88 | // Create iterators |
| 89 | Iterator in(_input, window); |
| 90 | Iterator out(_output, window_out); |
| 91 | |
| 92 | execute_window_loop(window, [&](const Coordinates & id) |
| 93 | { |
| 94 | const int hidx = id.y(); |
Georgios Pinitas | d912fd8 | 2017-11-27 21:00:13 +0000 | [diff] [blame] | 95 | const int idx = id.x() * output_stride_z + (hidx / _convolved_dims.width) * output_stride_y + (hidx % _convolved_dims.width) * output_stride_x; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 96 | |
| 97 | *(reinterpret_cast<T *>(out.ptr() + idx)) = *(reinterpret_cast<const T *>(in.ptr())); |
| 98 | }, |
| 99 | in, out); |
| 100 | } |
| 101 | |
| 102 | NECol2ImKernel::NECol2ImKernel() |
| 103 | : _func(), _input(nullptr), _output(nullptr), _convolved_dims() |
| 104 | { |
| 105 | } |
| 106 | |
Georgios Pinitas | d912fd8 | 2017-11-27 21:00:13 +0000 | [diff] [blame] | 107 | void NECol2ImKernel::configure(const ITensor *input, ITensor *output, const Size2D &convolved_dims) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 108 | { |
Georgios Pinitas | d912fd8 | 2017-11-27 21:00:13 +0000 | [diff] [blame] | 109 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); |
Georgios Pinitas | d912fd8 | 2017-11-27 21:00:13 +0000 | [diff] [blame] | 110 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), convolved_dims)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 111 | |
| 112 | _input = input; |
| 113 | _output = output; |
| 114 | _convolved_dims = convolved_dims; |
| 115 | |
| 116 | switch(input->info()->element_size()) |
| 117 | { |
| 118 | case 1: |
| 119 | _func = &NECol2ImKernel::run_col2im<uint8_t>; |
| 120 | break; |
| 121 | case 2: |
| 122 | _func = &NECol2ImKernel::run_col2im<uint16_t>; |
| 123 | break; |
| 124 | case 4: |
| 125 | _func = &NECol2ImKernel::run_col2im<uint32_t>; |
| 126 | break; |
| 127 | default: |
| 128 | ARM_COMPUTE_ERROR("Element size not supported"); |
| 129 | break; |
| 130 | } |
| 131 | |
| 132 | // Configure kernel window |
Giorgio Arena | 226e4b9 | 2018-08-23 12:00:02 +0100 | [diff] [blame] | 133 | auto win_config = validate_and_configure_window(input->info(), output->info(), convolved_dims); |
| 134 | ARM_COMPUTE_ERROR_THROW_ON(win_config.first); |
| 135 | INEKernel::configure(win_config.second); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 136 | } |
| 137 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 138 | Status NECol2ImKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const Size2D &convolved_dims) |
Georgios Pinitas | d912fd8 | 2017-11-27 21:00:13 +0000 | [diff] [blame] | 139 | { |
| 140 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, convolved_dims)); |
Giorgio Arena | 226e4b9 | 2018-08-23 12:00:02 +0100 | [diff] [blame] | 141 | ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), output->clone().get(), convolved_dims).first); |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 142 | return Status{}; |
Georgios Pinitas | d912fd8 | 2017-11-27 21:00:13 +0000 | [diff] [blame] | 143 | } |
| 144 | |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 145 | void NECol2ImKernel::run(const Window &window, const ThreadInfo &info) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 146 | { |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 147 | ARM_COMPUTE_UNUSED(info); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 148 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 149 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window); |
| 150 | |
| 151 | (this->*_func)(window); |
| 152 | } |