Michalis Spyrou | 7c9541c | 2018-09-20 17:40:04 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2018 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/NEUpsampleLayerKernel.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" |
| 32 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
| 33 | |
| 34 | #include <arm_neon.h> |
| 35 | |
| 36 | namespace arm_compute |
| 37 | { |
| 38 | namespace |
| 39 | { |
| 40 | std::pair<Status, Window> validate_and_configure_window_nchw(ITensorInfo *input, ITensorInfo *output, int num_elems_processed_per_iteration_x, const Size2D &info) |
| 41 | { |
| 42 | const int num_elems_processed_per_iteration_x_out = num_elems_processed_per_iteration_x * info.x(); |
| 43 | Window win = calculate_max_window(*output, Steps(num_elems_processed_per_iteration_x_out)); |
| 44 | AccessWindowRectangle input_access(input, 0, 0, num_elems_processed_per_iteration_x, 1, 0.5f, 0.5f); |
| 45 | AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration_x_out); |
| 46 | bool window_changed = update_window_and_padding(win, input_access, output_access); |
| 47 | output_access.set_valid_region(win, output->valid_region()); |
| 48 | |
| 49 | Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{}; |
| 50 | return std::make_pair(err, win); |
| 51 | } |
| 52 | |
| 53 | std::pair<Status, Window> validate_and_configure_window_nhwc(ITensorInfo *input, ITensorInfo *output, int num_elems_processed_per_iteration_x, const Size2D &info) |
| 54 | { |
| 55 | ARM_COMPUTE_UNUSED(info); |
| 56 | Window win = calculate_max_window(*output, Steps(num_elems_processed_per_iteration_x)); |
| 57 | AccessWindowHorizontal input_access(input, 0, num_elems_processed_per_iteration_x); |
| 58 | AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration_x); |
| 59 | bool window_changed = update_window_and_padding(win, input_access, output_access); |
| 60 | output_access.set_valid_region(win, output->valid_region()); |
| 61 | |
| 62 | Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{}; |
| 63 | return std::make_pair(err, win); |
| 64 | } |
| 65 | |
| 66 | std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output, int num_elems_processed_per_iteration_x, const Size2D &info) |
| 67 | { |
| 68 | std::pair<Status, Window> win_config; |
| 69 | switch(input->data_layout()) |
| 70 | { |
| 71 | case DataLayout::NCHW: |
| 72 | win_config = validate_and_configure_window_nchw(input, output, num_elems_processed_per_iteration_x, info); |
| 73 | break; |
| 74 | case DataLayout::NHWC: |
| 75 | win_config = validate_and_configure_window_nhwc(input, output, num_elems_processed_per_iteration_x, info); |
| 76 | break; |
| 77 | default: |
| 78 | win_config = std::make_pair(ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Unsupported data layout!"), Window{}); |
| 79 | } |
| 80 | |
| 81 | return win_config; |
| 82 | } |
| 83 | } // namespace |
| 84 | NEUpsampleLayerKernel::NEUpsampleLayerKernel() |
| 85 | : _func(nullptr), _input(nullptr), _output(nullptr), _info(), _num_elems_processed_per_iteration_x() |
| 86 | { |
| 87 | } |
| 88 | |
| 89 | Status NEUpsampleLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const Size2D &info, const InterpolationPolicy policy) |
| 90 | { |
| 91 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output); |
| 92 | ARM_COMPUTE_UNUSED(policy); |
| 93 | |
| 94 | const DataLayout data_layout = input->data_layout(); |
| 95 | const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH); |
| 96 | const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT); |
| 97 | |
| 98 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::F16, DataType::F32); |
| 99 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(info.x() != 2 || info.y() != 2, "Only stride 2 is supported"); |
| 100 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(policy != InterpolationPolicy::NEAREST_NEIGHBOR, "Only nearest neighbor policy supported"); |
| 101 | |
| 102 | // Check output if configured |
| 103 | if(output->total_size() != 0) |
| 104 | { |
| 105 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
| 106 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(input, output); |
| 107 | ARM_COMPUTE_RETURN_ERROR_ON(output->dimension(idx_width) != info.x() * input->dimension(idx_width)); |
| 108 | ARM_COMPUTE_RETURN_ERROR_ON(output->dimension(idx_height) != info.y() * input->dimension(idx_height)); |
| 109 | } |
| 110 | |
| 111 | const int num_elems_processed_per_iteration_x = 16 / input->element_size(); |
| 112 | ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), |
| 113 | output->clone().get(), num_elems_processed_per_iteration_x, info) |
| 114 | .first); |
| 115 | return Status{}; |
| 116 | } |
| 117 | |
| 118 | void NEUpsampleLayerKernel::upsample_f32_nchw(const arm_compute::Window &window) |
| 119 | { |
| 120 | Window window_in(window); |
| 121 | window_in.set(Window::DimX, Window::Dimension(0, _input->info()->dimension(0), _num_elems_processed_per_iteration_x)); |
| 122 | |
| 123 | Window window_out(window); |
| 124 | window_out.set(Window::DimY, Window::Dimension(0, _output->info()->dimension(1), _info.y())); |
| 125 | |
| 126 | Iterator input(_input, window_in); |
| 127 | Iterator output(_output, window_out); |
| 128 | const int offset_y_out = _output->info()->strides_in_bytes().y() / sizeof(float); |
| 129 | |
| 130 | execute_window_loop(window_out, [&](const Coordinates & id) |
| 131 | { |
| 132 | const float32x4_t data = vld1q_f32(reinterpret_cast<const float *>(input.ptr())); |
| 133 | const float32x4_t data_out1 = { vgetq_lane_f32(data, 0), vgetq_lane_f32(data, 0), vgetq_lane_f32(data, 1), vgetq_lane_f32(data, 1) }; |
| 134 | const float32x4_t data_out2 = { vgetq_lane_f32(data, 2), vgetq_lane_f32(data, 2), vgetq_lane_f32(data, 3), vgetq_lane_f32(data, 3) }; |
| 135 | auto out = reinterpret_cast<float *>(output.ptr()); |
| 136 | |
| 137 | vst1q_f32(out, data_out1); |
| 138 | vst1q_f32(out + 4, data_out2); |
| 139 | vst1q_f32(out + offset_y_out, data_out1); |
| 140 | vst1q_f32(out + offset_y_out + 4, data_out2); |
| 141 | }, |
| 142 | input, output); |
| 143 | } |
| 144 | |
| 145 | void NEUpsampleLayerKernel::upsample_f32_nhwc(const arm_compute::Window &window) |
| 146 | { |
| 147 | Window window_out(window); |
| 148 | window_out.set(Window::DimY, Window::Dimension(0, _output->info()->dimension(1), _info.x())); |
| 149 | window_out.set(Window::DimZ, Window::Dimension(0, _output->info()->dimension(2), _info.y())); |
| 150 | |
| 151 | Iterator input(_input, window); |
| 152 | Iterator output(_output, window_out); |
| 153 | |
| 154 | const int offset_y_out = _output->info()->strides_in_bytes().y() / sizeof(float); |
| 155 | const int offset_z_out = _output->info()->strides_in_bytes().z() / sizeof(float); |
| 156 | |
| 157 | execute_window_loop(window_out, [&](const Coordinates & id) |
| 158 | { |
| 159 | const float32x4_t data = vld1q_f32(reinterpret_cast<const float *>(input.ptr())); |
| 160 | auto out = reinterpret_cast<float *>(output.ptr()); |
| 161 | |
| 162 | vst1q_f32(out, data); |
| 163 | vst1q_f32(out + offset_y_out, data); |
| 164 | vst1q_f32(out + offset_z_out, data); |
| 165 | vst1q_f32(out + offset_y_out + offset_z_out, data); |
| 166 | }, |
| 167 | input, output); |
| 168 | } |
| 169 | |
| 170 | void NEUpsampleLayerKernel::upsample_qasymm8_nchw(const arm_compute::Window &window) |
| 171 | { |
| 172 | Window window_in(window); |
| 173 | window_in.set(Window::DimX, Window::Dimension(0, _input->info()->dimension(0), _num_elems_processed_per_iteration_x)); |
| 174 | |
| 175 | Window window_out(window); |
| 176 | window_out.set(Window::DimY, Window::Dimension(0, _output->info()->dimension(1), _info.y())); |
| 177 | |
| 178 | Iterator input(_input, window_in); |
| 179 | Iterator output(_output, window_out); |
| 180 | const int offset_y_out = _output->info()->strides_in_bytes().y() / sizeof(uint8_t); |
| 181 | |
| 182 | execute_window_loop(window_out, [&](const Coordinates & id) |
| 183 | { |
| 184 | const uint8x16_t data = vld1q_u8(reinterpret_cast<const uint8_t *>(input.ptr())); |
| 185 | const uint8x16_t data_out1 = { vgetq_lane_u8(data, 0), vgetq_lane_u8(data, 0), vgetq_lane_u8(data, 1), vgetq_lane_u8(data, 1), |
| 186 | vgetq_lane_u8(data, 2), vgetq_lane_u8(data, 2), vgetq_lane_u8(data, 3), vgetq_lane_u8(data, 3), |
| 187 | vgetq_lane_u8(data, 4), vgetq_lane_u8(data, 4), vgetq_lane_u8(data, 5), vgetq_lane_u8(data, 5), |
| 188 | vgetq_lane_u8(data, 6), vgetq_lane_u8(data, 6), vgetq_lane_u8(data, 7), vgetq_lane_u8(data, 7) |
| 189 | }; |
| 190 | const uint8x16_t data_out2 = |
| 191 | { |
| 192 | vgetq_lane_u8(data, 8), vgetq_lane_u8(data, 8), vgetq_lane_u8(data, 9), vgetq_lane_u8(data, 9), |
| 193 | vgetq_lane_u8(data, 10), vgetq_lane_u8(data, 10), vgetq_lane_u8(data, 11), vgetq_lane_u8(data, 11), |
| 194 | vgetq_lane_u8(data, 12), vgetq_lane_u8(data, 12), vgetq_lane_u8(data, 13), vgetq_lane_u8(data, 13), |
| 195 | vgetq_lane_u8(data, 14), vgetq_lane_u8(data, 14), vgetq_lane_u8(data, 15), vgetq_lane_u8(data, 15) |
| 196 | }; |
| 197 | auto out = reinterpret_cast<uint8_t *>(output.ptr()); |
| 198 | |
| 199 | vst1q_u8(out, data_out1); |
| 200 | vst1q_u8(out + 16, data_out2); |
| 201 | vst1q_u8(out + offset_y_out, data_out1); |
| 202 | vst1q_u8(out + offset_y_out + 16, data_out2); |
| 203 | }, |
| 204 | input, output); |
| 205 | } |
| 206 | |
| 207 | void NEUpsampleLayerKernel::upsample_qasymm8_nhwc(const arm_compute::Window &window) |
| 208 | { |
| 209 | Window window_out(window); |
| 210 | window_out.set(Window::DimY, Window::Dimension(0, _output->info()->dimension(1), _info.x())); |
| 211 | window_out.set(Window::DimZ, Window::Dimension(0, _output->info()->dimension(2), _info.y())); |
| 212 | |
| 213 | Iterator input(_input, window); |
| 214 | Iterator output(_output, window_out); |
| 215 | |
| 216 | const int offset_y_out = _output->info()->strides_in_bytes().y() / sizeof(uint8_t); |
| 217 | const int offset_z_out = _output->info()->strides_in_bytes().z() / sizeof(uint8_t); |
| 218 | execute_window_loop(window_out, [&](const Coordinates & id) |
| 219 | { |
| 220 | const uint8x16_t data = vld1q_u8(reinterpret_cast<const uint8_t *>(input.ptr())); |
| 221 | auto out = reinterpret_cast<uint8_t *>(output.ptr()); |
| 222 | |
| 223 | vst1q_u8(out, data); |
| 224 | vst1q_u8(out + offset_y_out, data); |
| 225 | vst1q_u8(out + offset_z_out, data); |
| 226 | vst1q_u8(out + offset_y_out + offset_z_out, data); |
| 227 | }, |
| 228 | input, output); |
| 229 | } |
| 230 | |
| 231 | void NEUpsampleLayerKernel::upsample_f16_nchw(const arm_compute::Window &window) |
| 232 | { |
| 233 | ARM_COMPUTE_UNUSED(window); |
| 234 | #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
| 235 | Window window_in(window); |
| 236 | window_in.set(Window::DimX, Window::Dimension(0, _input->info()->dimension(0), _num_elems_processed_per_iteration_x)); |
| 237 | |
| 238 | Window window_out(window); |
| 239 | window_out.set(Window::DimY, Window::Dimension(0, _output->info()->dimension(1), _info.y())); |
| 240 | |
| 241 | Iterator input(_input, window_in); |
| 242 | Iterator output(_output, window_out); |
| 243 | const int offset_y_out = _output->info()->strides_in_bytes().y() / sizeof(float16_t); |
| 244 | |
| 245 | execute_window_loop(window_out, [&](const Coordinates & id) |
| 246 | { |
| 247 | const float16x8_t data = vld1q_f16(reinterpret_cast<const float16_t *>(input.ptr())); |
| 248 | const float16x8_t data_out1 = { vgetq_lane_f16(data, 0), vgetq_lane_f16(data, 0), vgetq_lane_f16(data, 1), vgetq_lane_f16(data, 1), |
| 249 | vgetq_lane_f16(data, 2), vgetq_lane_f16(data, 2), vgetq_lane_f16(data, 3), vgetq_lane_f16(data, 3) |
| 250 | }; |
| 251 | const float16x8_t data_out2 = { vgetq_lane_f16(data, 4), vgetq_lane_f16(data, 4), vgetq_lane_f16(data, 5), vgetq_lane_f16(data, 5), |
| 252 | vgetq_lane_f16(data, 6), vgetq_lane_f16(data, 6), vgetq_lane_f16(data, 7), vgetq_lane_f16(data, 7) |
| 253 | }; |
| 254 | auto out = reinterpret_cast<float16_t *>(output.ptr()); |
| 255 | |
| 256 | vst1q_f16(out, data_out1); |
| 257 | vst1q_f16(out + 8, data_out2); |
| 258 | vst1q_f16(out + offset_y_out, data_out1); |
| 259 | vst1q_f16(out + offset_y_out + 8, data_out2); |
| 260 | }, |
| 261 | input, output); |
| 262 | #endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
| 263 | } |
| 264 | |
| 265 | void NEUpsampleLayerKernel::upsample_f16_nhwc(const arm_compute::Window &window) |
| 266 | { |
| 267 | ARM_COMPUTE_UNUSED(window); |
| 268 | #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
| 269 | Window window_out(window); |
| 270 | window_out.set(Window::DimY, Window::Dimension(0, _output->info()->dimension(1), _info.x())); |
| 271 | window_out.set(Window::DimZ, Window::Dimension(0, _output->info()->dimension(2), _info.y())); |
| 272 | |
| 273 | Iterator input(_input, window); |
| 274 | Iterator output(_output, window_out); |
| 275 | const int offset_y_out = _output->info()->strides_in_bytes().y() / sizeof(float16_t); |
| 276 | const int offset_z_out = _output->info()->strides_in_bytes().z() / sizeof(float16_t); |
| 277 | |
| 278 | execute_window_loop(window_out, [&](const Coordinates & id) |
| 279 | { |
| 280 | const float16x8_t data = vld1q_f16(reinterpret_cast<const float16_t *>(input.ptr())); |
| 281 | auto out = reinterpret_cast<float16_t *>(output.ptr()); |
| 282 | |
| 283 | vst1q_f16(out, data); |
| 284 | vst1q_f16(out + offset_y_out, data); |
| 285 | vst1q_f16(out + offset_z_out, data); |
| 286 | vst1q_f16(out + offset_y_out + offset_z_out, data); |
| 287 | }, |
| 288 | input, output); |
| 289 | #endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
| 290 | } |
| 291 | |
| 292 | void NEUpsampleLayerKernel::configure(const ITensor *input, ITensor *output, const Size2D &info, const InterpolationPolicy policy) |
| 293 | { |
| 294 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); |
| 295 | ARM_COMPUTE_UNUSED(policy); |
| 296 | |
| 297 | _input = input; |
| 298 | _output = output; |
| 299 | _info = info; |
| 300 | |
| 301 | const DataLayout data_layout = input->info()->data_layout(); |
| 302 | |
| 303 | TensorShape output_shape = misc::shape_calculator::compute_upsample_shape(*input->info(), info); |
| 304 | auto_init_if_empty(*output->info(), output_shape, 1, input->info()->data_type()); |
| 305 | output->info()->set_data_layout(data_layout); |
| 306 | |
| 307 | // Perform validation step |
| 308 | ARM_COMPUTE_ERROR_THROW_ON(NEUpsampleLayerKernel::validate(input->info(), output->info(), info, policy)); |
| 309 | |
| 310 | _num_elems_processed_per_iteration_x = 16 / output->info()->element_size(); |
| 311 | |
| 312 | switch(data_layout) |
| 313 | { |
| 314 | case DataLayout::NCHW: |
| 315 | { |
| 316 | switch(input->info()->data_type()) |
| 317 | { |
| 318 | case DataType::QASYMM8: |
| 319 | _func = &NEUpsampleLayerKernel::upsample_qasymm8_nchw; |
| 320 | break; |
| 321 | case DataType::F32: |
| 322 | _func = &NEUpsampleLayerKernel::upsample_f32_nchw; |
| 323 | break; |
| 324 | #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
| 325 | case DataType::F16: |
| 326 | _func = &NEUpsampleLayerKernel::upsample_f16_nchw; |
| 327 | break; |
| 328 | #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */ |
| 329 | default: |
| 330 | ARM_COMPUTE_ERROR("Not implemented"); |
| 331 | } |
| 332 | break; |
| 333 | } |
| 334 | case DataLayout::NHWC: |
| 335 | { |
| 336 | switch(input->info()->data_type()) |
| 337 | { |
| 338 | case DataType::QASYMM8: |
| 339 | _func = &NEUpsampleLayerKernel::upsample_qasymm8_nhwc; |
| 340 | break; |
| 341 | case DataType::F32: |
| 342 | _func = &NEUpsampleLayerKernel::upsample_f32_nhwc; |
| 343 | break; |
| 344 | #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
| 345 | case DataType::F16: |
| 346 | _func = &NEUpsampleLayerKernel::upsample_f16_nhwc; |
| 347 | break; |
| 348 | #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */ |
| 349 | default: |
| 350 | ARM_COMPUTE_ERROR("Not implemented"); |
| 351 | } |
| 352 | break; |
| 353 | } |
| 354 | default: |
| 355 | ARM_COMPUTE_ERROR("Not implemented"); |
| 356 | } |
| 357 | |
| 358 | // Configure window |
| 359 | std::pair<Status, Window> win_config = validate_and_configure_window(input->info(), |
| 360 | output->info(), |
| 361 | _num_elems_processed_per_iteration_x, |
| 362 | info); |
| 363 | ARM_COMPUTE_ERROR_THROW_ON(win_config.first); |
| 364 | INEKernel::configure(win_config.second); |
| 365 | } |
| 366 | |
| 367 | void NEUpsampleLayerKernel::run(const Window &window, const ThreadInfo &info) |
| 368 | { |
| 369 | ARM_COMPUTE_UNUSED(info); |
| 370 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 371 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window); |
| 372 | ARM_COMPUTE_ERROR_ON(_func == nullptr); |
| 373 | |
| 374 | (this->*_func)(window); |
| 375 | } |
| 376 | } // namespace arm_compute |