Manuel Bottini | 9032ee3 | 2019-08-07 17:04:11 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2019 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/NEPadLayerKernel.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/NEON/wrapper/wrapper.h" |
| 30 | #include "arm_compute/core/TensorInfo.h" |
| 31 | #include "arm_compute/core/Types.h" |
| 32 | #include "arm_compute/core/Validate.h" |
| 33 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
| 34 | |
| 35 | namespace arm_compute |
| 36 | { |
| 37 | namespace |
| 38 | { |
| 39 | Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, const PaddingList &paddings, const PaddingMode mode) |
| 40 | { |
Georgios Pinitas | 3384356 | 2019-12-10 13:33:18 +0000 | [diff] [blame] | 41 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input); |
| 42 | ARM_COMPUTE_RETURN_ERROR_ON(input->data_type() == DataType::UNKNOWN); |
Manuel Bottini | 9032ee3 | 2019-08-07 17:04:11 +0100 | [diff] [blame] | 43 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(mode != PaddingMode::CONSTANT, "Only constant padding mode is supported"); |
| 44 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(paddings.size() > 4, "Padding list bigger than 4 dimensions"); |
| 45 | if(output->total_size() != 0) |
| 46 | { |
| 47 | const TensorShape expected_output_shape = arm_compute::misc::shape_calculator::compute_padded_shape(input->tensor_shape(), paddings); |
| 48 | const TensorInfo expected_output_info = input->clone()->set_tensor_shape(expected_output_shape); |
| 49 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(output, &expected_output_info); |
| 50 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
| 51 | } |
| 52 | return Status{}; |
| 53 | } |
| 54 | } // namespace |
| 55 | |
| 56 | template <typename T> |
| 57 | void NEPadLayerKernel::run_pad_constant(const Window &window) |
| 58 | { |
| 59 | Window output_window{ window }; |
| 60 | output_window.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 61 | |
| 62 | const size_t element_size = _input->info()->element_size(); |
| 63 | Iterator output_it(_output, output_window); |
| 64 | execute_window_loop(output_window, [&](const Coordinates & id) |
| 65 | { |
| 66 | Coordinates idin{ id }; |
| 67 | for(size_t dim = _padding.size() - 1; dim > 0; --dim) |
| 68 | { |
| 69 | idin[dim] -= _padding[dim].first; |
| 70 | if(idin[dim] < 0 || static_cast<int>(_input->info()->dimension(dim)) - 1 < idin[dim]) |
| 71 | { |
| 72 | std::fill_n(reinterpret_cast<T *>(output_it.ptr()), _output->info()->dimension(0), _constant_value.get<T>()); |
| 73 | return; |
| 74 | } |
| 75 | } |
| 76 | T *input_it_ptr = reinterpret_cast<T *>(_input->ptr_to_element(idin)); |
| 77 | T *output_it_ptr = reinterpret_cast<T *>(output_it.ptr()); |
| 78 | std::fill_n(output_it_ptr, _padding[0].first, _constant_value.get<T>()); |
| 79 | memcpy(output_it_ptr + _padding[0].first, input_it_ptr, _input->info()->dimension(0) * element_size); |
| 80 | std::fill_n(output_it_ptr + _padding[0].first + _input->info()->dimension(0), _padding[0].second, _constant_value.get<T>()); |
| 81 | }, |
| 82 | output_it); |
| 83 | } |
| 84 | |
| 85 | void NEPadLayerKernel::run_pad_constant_uint8_3Dinput_3Dpad(const Window &window) |
| 86 | { |
| 87 | ARM_COMPUTE_UNUSED(window); |
| 88 | |
| 89 | const size_t start_plane = window.z().start(); |
| 90 | const size_t end_plane = window.z().end(); |
| 91 | |
Georgios Pinitas | 64e738f | 2019-12-18 15:09:00 +0000 | [diff] [blame] | 92 | size_t start_plane_input = start_plane; |
| 93 | if(_padding.size() > 2) |
| 94 | { |
| 95 | start_plane_input = (start_plane < _padding[2].first) ? 0 : start_plane - _padding[2].first; |
| 96 | } |
Manuel Bottini | 9032ee3 | 2019-08-07 17:04:11 +0100 | [diff] [blame] | 97 | const int output_plane_size = _output->info()->dimension(0) * _output->info()->dimension(1); |
Georgios Pinitas | 64e738f | 2019-12-18 15:09:00 +0000 | [diff] [blame] | 98 | const int input_plane_size = _input->info()->dimension(0) * _input->info()->dimension(1); |
Manuel Bottini | 9032ee3 | 2019-08-07 17:04:11 +0100 | [diff] [blame] | 99 | |
| 100 | const int pad_y_elems_top = (_padding.size() > 1 ? _padding[1].first : 0) * _output->info()->dimension(0); |
| 101 | const int pad_y_elems_bot = (_padding.size() > 1 ? _padding[1].second : 0) * _output->info()->dimension(0); |
| 102 | |
Georgios Pinitas | 64e738f | 2019-12-18 15:09:00 +0000 | [diff] [blame] | 103 | const size_t jump_to_next_row_input = _input->info()->dimension(0); |
| 104 | const size_t jump_to_next_row_output = _padding[0].first + _padding[0].second; |
Manuel Bottini | 9032ee3 | 2019-08-07 17:04:11 +0100 | [diff] [blame] | 105 | |
Georgios Pinitas | 64e738f | 2019-12-18 15:09:00 +0000 | [diff] [blame] | 106 | uint8_t *output_row_ptr = _output->buffer() + _output->info()->offset_first_element_in_bytes() + start_plane * output_plane_size; |
Manuel Bottini | 9032ee3 | 2019-08-07 17:04:11 +0100 | [diff] [blame] | 107 | const uint8_t *input_it_ptr = _input->buffer() + _input->info()->offset_first_element_in_bytes() + start_plane_input * input_plane_size; |
| 108 | const auto pad_value = _constant_value.get<uint8_t>(); |
| 109 | |
| 110 | for(size_t z_i = start_plane; z_i < end_plane; ++z_i) |
| 111 | { |
| 112 | if(_padding.size() > 2 && z_i < _padding[2].first) |
| 113 | { |
| 114 | memset(output_row_ptr, pad_value, output_plane_size); |
| 115 | output_row_ptr += output_plane_size; |
| 116 | } |
Georgios Pinitas | 64e738f | 2019-12-18 15:09:00 +0000 | [diff] [blame] | 117 | else if(_padding.size() > 2 && z_i > (_input->info()->dimension(2) + _padding[2].first - 1)) |
Manuel Bottini | 9032ee3 | 2019-08-07 17:04:11 +0100 | [diff] [blame] | 118 | { |
| 119 | memset(output_row_ptr, pad_value, output_plane_size); |
| 120 | output_row_ptr += output_plane_size; |
| 121 | } |
| 122 | else |
| 123 | { |
| 124 | memset(output_row_ptr, pad_value, pad_y_elems_top); |
| 125 | output_row_ptr += pad_y_elems_top; |
| 126 | size_t y_i = _input->info()->dimension(1); |
| 127 | // Basic loop unrolling |
| 128 | for(; y_i > 3; y_i -= 4) |
| 129 | { |
| 130 | memset(output_row_ptr, pad_value, _padding[0].first); |
| 131 | output_row_ptr += _padding[0].first; |
| 132 | |
| 133 | memcpy(output_row_ptr, input_it_ptr, _input->info()->dimension(0)); |
| 134 | output_row_ptr += _input->info()->dimension(0); |
| 135 | input_it_ptr += jump_to_next_row_input; |
| 136 | |
| 137 | memset(output_row_ptr, pad_value, _padding[0].second + _padding[0].first); |
| 138 | output_row_ptr += jump_to_next_row_output; |
| 139 | |
| 140 | memcpy(output_row_ptr, input_it_ptr, _input->info()->dimension(0)); |
| 141 | output_row_ptr += _input->info()->dimension(0); |
| 142 | input_it_ptr += jump_to_next_row_input; |
| 143 | |
| 144 | memset(output_row_ptr, pad_value, _padding[0].second + _padding[0].first); |
| 145 | output_row_ptr += jump_to_next_row_output; |
| 146 | |
| 147 | memcpy(output_row_ptr, input_it_ptr, _input->info()->dimension(0)); |
| 148 | output_row_ptr += _input->info()->dimension(0); |
| 149 | input_it_ptr += jump_to_next_row_input; |
| 150 | |
| 151 | memset(output_row_ptr, pad_value, _padding[0].second + _padding[0].first); |
| 152 | output_row_ptr += jump_to_next_row_output; |
| 153 | |
| 154 | memcpy(output_row_ptr, input_it_ptr, _input->info()->dimension(0)); |
| 155 | output_row_ptr += _input->info()->dimension(0); |
| 156 | input_it_ptr += jump_to_next_row_input; |
| 157 | |
| 158 | memset(output_row_ptr, pad_value, _padding[0].second); |
| 159 | output_row_ptr += _padding[0].second; |
| 160 | } |
| 161 | for(; y_i > 0; --y_i) |
| 162 | { |
| 163 | memset(output_row_ptr, pad_value, _padding[0].first); |
| 164 | output_row_ptr += _padding[0].first; |
| 165 | |
| 166 | memcpy(output_row_ptr, input_it_ptr, _input->info()->dimension(0)); |
| 167 | output_row_ptr += _input->info()->dimension(0); |
| 168 | input_it_ptr += _input->info()->dimension(0); |
| 169 | |
| 170 | memset(output_row_ptr, pad_value, _padding[0].second); |
| 171 | output_row_ptr += _padding[0].second; |
| 172 | } |
Manuel Bottini | 9032ee3 | 2019-08-07 17:04:11 +0100 | [diff] [blame] | 173 | memset(output_row_ptr, pad_value, pad_y_elems_bot); |
| 174 | output_row_ptr += pad_y_elems_bot; |
| 175 | } |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | NEPadLayerKernel::NEPadLayerKernel() |
| 180 | : _func(), _input(nullptr), _output(nullptr), _padding(), _constant_value(), _mode() |
| 181 | { |
| 182 | } |
| 183 | |
| 184 | void NEPadLayerKernel::configure(ITensor *input, ITensor *output, const PaddingList &padding, const PixelValue constant_value, const PaddingMode mode) |
| 185 | { |
| 186 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); |
| 187 | // Auto-init |
| 188 | const TensorShape expected_output_shape = arm_compute::misc::shape_calculator::compute_padded_shape(input->info()->tensor_shape(), padding); |
| 189 | const TensorInfo expected_output_info = input->info()->clone()->set_tensor_shape(expected_output_shape); |
| 190 | auto_init_if_empty(*output->info(), expected_output_info); |
| 191 | |
| 192 | // Perform validation step |
| 193 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), padding, mode)); |
| 194 | |
| 195 | _input = input; |
| 196 | _output = output; |
| 197 | _padding = padding; |
| 198 | _constant_value = constant_value; |
| 199 | _mode = mode; |
| 200 | |
| 201 | if(_mode == PaddingMode::CONSTANT) |
| 202 | { |
| 203 | switch(_input->info()->element_size()) |
| 204 | { |
| 205 | case 1: |
Georgios Pinitas | 64e738f | 2019-12-18 15:09:00 +0000 | [diff] [blame] | 206 | if(_input->info()->num_dimensions() == 3 && // Is 3D |
| 207 | padding.size() <= 3 && // Has 3D padding |
| 208 | !_input->info()->has_padding() && !_output->info()->has_padding()) // Input & Output have no padding |
Manuel Bottini | 9032ee3 | 2019-08-07 17:04:11 +0100 | [diff] [blame] | 209 | { |
| 210 | _func = &NEPadLayerKernel::run_pad_constant_uint8_3Dinput_3Dpad; |
| 211 | } |
| 212 | else |
| 213 | { |
| 214 | _func = &NEPadLayerKernel::run_pad_constant<uint8_t>; |
| 215 | } |
| 216 | break; |
| 217 | case 2: |
| 218 | _func = &NEPadLayerKernel::run_pad_constant<uint16_t>; |
| 219 | break; |
| 220 | case 4: |
| 221 | _func = &NEPadLayerKernel::run_pad_constant<uint32_t>; |
| 222 | break; |
| 223 | default: |
| 224 | ARM_COMPUTE_ERROR("Element size not supported"); |
| 225 | break; |
| 226 | } |
| 227 | } |
| 228 | else |
| 229 | { |
| 230 | ARM_COMPUTE_ERROR("Padding mode not supported"); |
| 231 | } |
| 232 | |
| 233 | // Configure kernel window |
| 234 | Window win = calculate_max_window(*output->info(), Steps()); |
| 235 | |
| 236 | // The NEPad doesn't need padding so update_window_and_padding() can be skipped |
| 237 | Coordinates coord; |
| 238 | coord.set_num_dimensions(output->info()->num_dimensions()); |
| 239 | output->info()->set_valid_region(ValidRegion(coord, output->info()->tensor_shape())); |
| 240 | |
| 241 | ICPPKernel::configure(win); |
| 242 | } |
| 243 | |
| 244 | Status NEPadLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const PaddingList &padding, const PixelValue constant_value, const PaddingMode mode) |
| 245 | { |
| 246 | ARM_COMPUTE_UNUSED(constant_value); |
| 247 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, padding, mode)); |
| 248 | return Status{}; |
| 249 | } |
| 250 | |
| 251 | void NEPadLayerKernel::run(const Window &window, const ThreadInfo &info) |
| 252 | { |
| 253 | ARM_COMPUTE_UNUSED(info); |
| 254 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 255 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window); |
| 256 | |
| 257 | if(_func != nullptr) |
| 258 | { |
| 259 | (this->*_func)(window); |
| 260 | } |
| 261 | } |
| 262 | } // namespace arm_compute |