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 | |
| 92 | const size_t start_plane_input = start_plane - (_padding.size() > 2 && start_plane >= _padding[2].first ? _padding[2].first : 0); |
| 93 | |
| 94 | const int output_plane_size = _output->info()->dimension(0) * _output->info()->dimension(1); |
| 95 | const int input_plane_size = (_input->info()->dimension(0) + _input->info()->padding().right + _input->info()->padding().left) * (_input->info()->dimension( |
| 96 | 1) |
| 97 | + _input->info()->padding().top + _input->info()->padding().bottom); |
| 98 | |
| 99 | const int pad_y_elems_top = (_padding.size() > 1 ? _padding[1].first : 0) * _output->info()->dimension(0); |
| 100 | const int pad_y_elems_bot = (_padding.size() > 1 ? _padding[1].second : 0) * _output->info()->dimension(0); |
| 101 | |
| 102 | const size_t jump_to_next_row_input = _input->info()->dimension(0) + _input->info()->padding().right + _input->info()->padding().left; |
| 103 | const size_t jump_to_next_row_output = _padding[0].first + _padding[0].second; |
| 104 | const size_t jump_to_next_plane_input = _input->info()->padding().empty() ? 0 : _input->info()->dimension(0) * (_input->info()->padding().right + _input->info()->padding().top); |
| 105 | |
| 106 | uint8_t *output_row_ptr = _output->buffer() + start_plane * output_plane_size; |
| 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 | } |
| 117 | else if(_padding.size() > 2 && z_i > _input->info()->dimension(2) + _padding[2].first - 1) |
| 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 | } |
| 173 | input_it_ptr += jump_to_next_plane_input; |
| 174 | memset(output_row_ptr, pad_value, pad_y_elems_bot); |
| 175 | output_row_ptr += pad_y_elems_bot; |
| 176 | } |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | NEPadLayerKernel::NEPadLayerKernel() |
| 181 | : _func(), _input(nullptr), _output(nullptr), _padding(), _constant_value(), _mode() |
| 182 | { |
| 183 | } |
| 184 | |
| 185 | void NEPadLayerKernel::configure(ITensor *input, ITensor *output, const PaddingList &padding, const PixelValue constant_value, const PaddingMode mode) |
| 186 | { |
| 187 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); |
| 188 | // Auto-init |
| 189 | const TensorShape expected_output_shape = arm_compute::misc::shape_calculator::compute_padded_shape(input->info()->tensor_shape(), padding); |
| 190 | const TensorInfo expected_output_info = input->info()->clone()->set_tensor_shape(expected_output_shape); |
| 191 | auto_init_if_empty(*output->info(), expected_output_info); |
| 192 | |
| 193 | // Perform validation step |
| 194 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), padding, mode)); |
| 195 | |
| 196 | _input = input; |
| 197 | _output = output; |
| 198 | _padding = padding; |
| 199 | _constant_value = constant_value; |
| 200 | _mode = mode; |
| 201 | |
| 202 | if(_mode == PaddingMode::CONSTANT) |
| 203 | { |
| 204 | switch(_input->info()->element_size()) |
| 205 | { |
| 206 | case 1: |
| 207 | if(_input->info()->num_dimensions() == 3 && padding.size() <= 3) |
| 208 | { |
| 209 | _func = &NEPadLayerKernel::run_pad_constant_uint8_3Dinput_3Dpad; |
| 210 | } |
| 211 | else |
| 212 | { |
| 213 | _func = &NEPadLayerKernel::run_pad_constant<uint8_t>; |
| 214 | } |
| 215 | break; |
| 216 | case 2: |
| 217 | _func = &NEPadLayerKernel::run_pad_constant<uint16_t>; |
| 218 | break; |
| 219 | case 4: |
| 220 | _func = &NEPadLayerKernel::run_pad_constant<uint32_t>; |
| 221 | break; |
| 222 | default: |
| 223 | ARM_COMPUTE_ERROR("Element size not supported"); |
| 224 | break; |
| 225 | } |
| 226 | } |
| 227 | else |
| 228 | { |
| 229 | ARM_COMPUTE_ERROR("Padding mode not supported"); |
| 230 | } |
| 231 | |
| 232 | // Configure kernel window |
| 233 | Window win = calculate_max_window(*output->info(), Steps()); |
| 234 | |
| 235 | // The NEPad doesn't need padding so update_window_and_padding() can be skipped |
| 236 | Coordinates coord; |
| 237 | coord.set_num_dimensions(output->info()->num_dimensions()); |
| 238 | output->info()->set_valid_region(ValidRegion(coord, output->info()->tensor_shape())); |
| 239 | |
| 240 | ICPPKernel::configure(win); |
| 241 | } |
| 242 | |
| 243 | Status NEPadLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const PaddingList &padding, const PixelValue constant_value, const PaddingMode mode) |
| 244 | { |
| 245 | ARM_COMPUTE_UNUSED(constant_value); |
| 246 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, padding, mode)); |
| 247 | return Status{}; |
| 248 | } |
| 249 | |
| 250 | void NEPadLayerKernel::run(const Window &window, const ThreadInfo &info) |
| 251 | { |
| 252 | ARM_COMPUTE_UNUSED(info); |
| 253 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 254 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window); |
| 255 | |
| 256 | if(_func != nullptr) |
| 257 | { |
| 258 | (this->*_func)(window); |
| 259 | } |
| 260 | } |
| 261 | } // namespace arm_compute |