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 | */ |
| 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" |
| 32 | |
| 33 | #include <arm_neon.h> |
| 34 | #include <cstddef> |
| 35 | #include <cstdint> |
| 36 | |
| 37 | using namespace arm_compute; |
| 38 | |
Georgios Pinitas | d912fd8 | 2017-11-27 21:00:13 +0000 | [diff] [blame] | 39 | namespace |
| 40 | { |
| 41 | TensorShape get_output_shape(const ITensorInfo *input, const Size2D &convolved_dims) |
| 42 | { |
| 43 | TensorShape output_shape = input->tensor_shape(); |
| 44 | output_shape.set(0, convolved_dims.width); |
| 45 | output_shape.set(1, convolved_dims.height); |
| 46 | output_shape.set(2, input->tensor_shape()[0]); |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 47 | output_shape.set(3, input->tensor_shape()[3]); // For NEON the batch size is on the fourth dimension of the input tensor |
Georgios Pinitas | d912fd8 | 2017-11-27 21:00:13 +0000 | [diff] [blame] | 48 | |
| 49 | return output_shape; |
| 50 | } |
| 51 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 52 | 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] | 53 | { |
Anthony Barbier | eaefd00 | 2018-07-20 17:49:35 +0100 | [diff] [blame] | 54 | //Note: ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(input) is not needed here as this kernel doesn't use NEON FP16 instructions. |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame] | 55 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8, DataType::S8, DataType::QASYMM8, |
| 56 | DataType::U16, DataType::S16, |
Georgios Pinitas | d912fd8 | 2017-11-27 21:00:13 +0000 | [diff] [blame] | 57 | DataType::U32, DataType::S32, |
| 58 | DataType::F16, DataType::F32); |
| 59 | |
| 60 | // Validate configured output |
| 61 | if(output->total_size() != 0) |
| 62 | { |
| 63 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(output->tensor_shape(), get_output_shape(input, convolved_dims)); |
| 64 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
Georgios Pinitas | d912fd8 | 2017-11-27 21:00:13 +0000 | [diff] [blame] | 65 | } |
| 66 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 67 | return Status{}; |
Georgios Pinitas | d912fd8 | 2017-11-27 21:00:13 +0000 | [diff] [blame] | 68 | } |
| 69 | } // namespace |
| 70 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 71 | template <typename T> |
| 72 | void NECol2ImKernel::run_col2im(const Window &window) |
| 73 | { |
| 74 | const int output_stride_x = _output->info()->strides_in_bytes().x(); |
| 75 | const int output_stride_y = _output->info()->strides_in_bytes().y(); |
| 76 | const int output_stride_z = _output->info()->strides_in_bytes().z(); |
| 77 | |
| 78 | Window window_out(window); |
| 79 | window_out.set(Window::DimX, Window::Dimension(0, 0, 0)); |
| 80 | window_out.set(Window::DimY, Window::Dimension(0, 0, 0)); |
| 81 | window_out.set(Window::DimZ, Window::Dimension(0, 0, 0)); |
| 82 | |
| 83 | // Create iterators |
| 84 | Iterator in(_input, window); |
| 85 | Iterator out(_output, window_out); |
| 86 | |
| 87 | execute_window_loop(window, [&](const Coordinates & id) |
| 88 | { |
| 89 | const int hidx = id.y(); |
Georgios Pinitas | d912fd8 | 2017-11-27 21:00:13 +0000 | [diff] [blame] | 90 | 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] | 91 | |
| 92 | *(reinterpret_cast<T *>(out.ptr() + idx)) = *(reinterpret_cast<const T *>(in.ptr())); |
| 93 | }, |
| 94 | in, out); |
| 95 | } |
| 96 | |
| 97 | NECol2ImKernel::NECol2ImKernel() |
| 98 | : _func(), _input(nullptr), _output(nullptr), _convolved_dims() |
| 99 | { |
| 100 | } |
| 101 | |
Georgios Pinitas | d912fd8 | 2017-11-27 21:00:13 +0000 | [diff] [blame] | 102 | void NECol2ImKernel::configure(const ITensor *input, ITensor *output, const Size2D &convolved_dims) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 103 | { |
Georgios Pinitas | d912fd8 | 2017-11-27 21:00:13 +0000 | [diff] [blame] | 104 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 105 | |
Gian Marco Iodice | ec8b45e | 2017-06-22 13:00:39 +0100 | [diff] [blame] | 106 | // Output auto inizialitation if not yet initialized |
Georgios Pinitas | d912fd8 | 2017-11-27 21:00:13 +0000 | [diff] [blame] | 107 | auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(get_output_shape(input->info(), 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 | // Perform validation step |
| 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 |
| 133 | Window win = calculate_max_window(*input->info(), Steps()); |
| 134 | |
| 135 | // The NECol2ImKernel doesn't need padding so update_window_and_padding() can be skipped |
| 136 | Coordinates coord; |
| 137 | coord.set_num_dimensions(output->info()->num_dimensions()); |
| 138 | output->info()->set_valid_region(ValidRegion(coord, output->info()->tensor_shape())); |
| 139 | |
| 140 | INEKernel::configure(win); |
| 141 | } |
| 142 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 143 | 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] | 144 | { |
| 145 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, convolved_dims)); |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 146 | return Status{}; |
Georgios Pinitas | d912fd8 | 2017-11-27 21:00:13 +0000 | [diff] [blame] | 147 | } |
| 148 | |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 149 | void NECol2ImKernel::run(const Window &window, const ThreadInfo &info) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 150 | { |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 151 | ARM_COMPUTE_UNUSED(info); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 152 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 153 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window); |
| 154 | |
| 155 | (this->*_func)(window); |
| 156 | } |