Michalis Spyrou | b7b3153 | 2017-11-23 12:10:21 +0000 | [diff] [blame] | 1 | /* |
Isabella Gottardi | 0a1090a | 2019-02-14 18:07:36 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2019 ARM Limited. |
Michalis Spyrou | b7b3153 | 2017-11-23 12:10:21 +0000 | [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/NEDepthwiseIm2ColKernel.h" |
| 25 | |
Michalis Spyrou | b7b3153 | 2017-11-23 12:10:21 +0000 | [diff] [blame] | 26 | #include "arm_compute/core/Coordinates.h" |
| 27 | #include "arm_compute/core/Error.h" |
| 28 | #include "arm_compute/core/Helpers.h" |
| 29 | #include "arm_compute/core/ITensor.h" |
| 30 | #include "arm_compute/core/NEON/INEKernel.h" |
| 31 | #include "arm_compute/core/TensorInfo.h" |
| 32 | #include "arm_compute/core/TensorShape.h" |
| 33 | #include "arm_compute/core/Types.h" |
| 34 | #include "arm_compute/core/Validate.h" |
| 35 | #include "arm_compute/core/Window.h" |
| 36 | |
| 37 | using namespace arm_compute; |
| 38 | |
Abe Mbise | 7784c83 | 2018-05-31 16:48:41 +0100 | [diff] [blame] | 39 | namespace |
| 40 | { |
Usama Arif | 881f2de | 2019-04-12 10:29:17 +0100 | [diff] [blame] | 41 | Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, |
| 42 | const Size2D &kernel_dims, const PadStrideInfo &conv_info, bool has_bias, unsigned int depth_multiplier, const Size2D &dilation) |
Abe Mbise | 7784c83 | 2018-05-31 16:48:41 +0100 | [diff] [blame] | 43 | { |
| 44 | ARM_COMPUTE_UNUSED(conv_info); |
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. |
Abe Mbise | 7784c83 | 2018-05-31 16:48:41 +0100 | [diff] [blame] | 46 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::F16, DataType::F32); |
| 47 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
| 48 | ARM_COMPUTE_RETURN_ERROR_ON(is_data_type_quantized_asymmetric(input->data_type()) && has_bias); |
| 49 | ARM_COMPUTE_RETURN_ERROR_ON((input->dimension(2) * depth_multiplier) != output->dimension(2)); |
| 50 | ARM_COMPUTE_RETURN_ERROR_ON(output->dimension(0) != (kernel_dims.width * kernel_dims.height + ((has_bias) ? 1 : 0))); |
Isabella Gottardi | 0a1090a | 2019-02-14 18:07:36 +0000 | [diff] [blame] | 51 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(input, output); |
Usama Arif | 881f2de | 2019-04-12 10:29:17 +0100 | [diff] [blame] | 52 | ARM_COMPUTE_RETURN_ERROR_ON((dilation.x() < 1) || dilation.y() < 1); |
Abe Mbise | 7784c83 | 2018-05-31 16:48:41 +0100 | [diff] [blame] | 53 | |
| 54 | return Status{}; |
| 55 | } |
| 56 | } // namespace |
| 57 | |
Georgios Pinitas | d05dce4 | 2018-01-22 16:29:17 +0000 | [diff] [blame] | 58 | template <typename T> |
| 59 | void NEDepthwiseIm2ColKernel::run_generic(const Window &window) |
Michalis Spyrou | b7b3153 | 2017-11-23 12:10:21 +0000 | [diff] [blame] | 60 | { |
Michalis Spyrou | b7b3153 | 2017-11-23 12:10:21 +0000 | [diff] [blame] | 61 | const int input_w = _input->info()->dimension(0); |
| 62 | const int input_h = _input->info()->dimension(1); |
| 63 | const int input_stride_x = _input->info()->strides_in_bytes().x(); |
| 64 | const int input_stride_y = _input->info()->strides_in_bytes().y(); |
| 65 | const int input_stride_z = _input->info()->strides_in_bytes().z(); |
| 66 | const int stride_x = _conv_info.stride().first; |
| 67 | const int stride_y = _conv_info.stride().second; |
| 68 | |
| 69 | const int pad_left = _conv_info.pad_left(); |
| 70 | const int pad_right = _conv_info.pad_right(); |
| 71 | const int pad_top = _conv_info.pad_top(); |
| 72 | |
| 73 | Window window_in(window); |
| 74 | // The first three dimensions of the input are increased by the inner loops |
| 75 | window_in.set(Window::DimX, Window::Dimension(0, 0, 0)); |
| 76 | window_in.set(Window::DimY, Window::Dimension(0, 0, 0)); |
| 77 | window_in.set(Window::DimZ, Window::Dimension(0, 0, 0)); |
| 78 | |
| 79 | // Setup output window |
| 80 | Window window_out(window); |
| 81 | window_out.set(Window::DimX, Window::Dimension(0, _output->info()->dimension(0), _output->info()->dimension(0))); |
| 82 | window_out.set(Window::DimY, Window::Dimension(0, _output->info()->dimension(1), 1)); |
| 83 | window_out.set(Window::DimZ, Window::Dimension(0, _output->info()->dimension(2), 1)); |
| 84 | |
| 85 | Iterator in(_input, window_in); |
| 86 | Iterator out(_output, window_out); |
| 87 | |
| 88 | const int full_length = input_w + pad_left + pad_right; |
Usama Arif | 881f2de | 2019-04-12 10:29:17 +0100 | [diff] [blame] | 89 | const int max_initial_x = stride_x * (((full_length - (_kernel_dims.width + (_kernel_dims.width - 1) * (_dilation.x() - 1))) / stride_x) + 1); |
Michalis Spyrou | b7b3153 | 2017-11-23 12:10:21 +0000 | [diff] [blame] | 90 | |
Georgios Pinitas | d05dce4 | 2018-01-22 16:29:17 +0000 | [diff] [blame] | 91 | // Define pad value |
| 92 | auto zero = static_cast<T>(0); |
| 93 | if(std::is_same<T, uint8_t>::value) |
| 94 | { |
| 95 | zero = _input->info()->quantization_info().offset; |
| 96 | } |
| 97 | |
Michalis Spyrou | b7b3153 | 2017-11-23 12:10:21 +0000 | [diff] [blame] | 98 | execute_window_loop(window_out, [&](const Coordinates & id) |
| 99 | { |
| 100 | const int src_pixel_linear = id.y() * stride_x; |
| 101 | |
| 102 | const int src_x = -pad_left + src_pixel_linear % max_initial_x; |
| 103 | const int src_y = -pad_top + src_pixel_linear / max_initial_x * stride_y; |
| 104 | |
| 105 | // Get pointers |
Giorgio Arena | 7657224 | 2018-04-04 17:44:26 +0100 | [diff] [blame] | 106 | const uint8_t *const input_ptr = in.ptr() + id.z() / _depth_multiplier * input_stride_z; |
Georgios Pinitas | d05dce4 | 2018-01-22 16:29:17 +0000 | [diff] [blame] | 107 | auto output_ptr = reinterpret_cast<T *>(out.ptr()); |
Usama Arif | 881f2de | 2019-04-12 10:29:17 +0100 | [diff] [blame] | 108 | const int height = src_y + (_kernel_dims.height + (_kernel_dims.height - 1) * (_dilation.y() - 1)); |
| 109 | const int width = src_x + (_kernel_dims.width + (_kernel_dims.width - 1) * (_dilation.x() - 1)); |
Michalis Spyrou | b7b3153 | 2017-11-23 12:10:21 +0000 | [diff] [blame] | 110 | |
Usama Arif | 881f2de | 2019-04-12 10:29:17 +0100 | [diff] [blame] | 111 | for(int y = src_y; y < height; y += _dilation.y()) |
Michalis Spyrou | b7b3153 | 2017-11-23 12:10:21 +0000 | [diff] [blame] | 112 | { |
Usama Arif | 881f2de | 2019-04-12 10:29:17 +0100 | [diff] [blame] | 113 | for(int x = src_x; x < width; x += _dilation.x(), ++output_ptr) |
Michalis Spyrou | b7b3153 | 2017-11-23 12:10:21 +0000 | [diff] [blame] | 114 | { |
| 115 | if(x < 0 || x >= input_w || y < 0 || y >= input_h) |
| 116 | { |
Georgios Pinitas | d05dce4 | 2018-01-22 16:29:17 +0000 | [diff] [blame] | 117 | *output_ptr = zero; |
Michalis Spyrou | b7b3153 | 2017-11-23 12:10:21 +0000 | [diff] [blame] | 118 | } |
| 119 | else |
| 120 | { |
Georgios Pinitas | d05dce4 | 2018-01-22 16:29:17 +0000 | [diff] [blame] | 121 | *output_ptr = *(reinterpret_cast<const T *>(input_ptr + x * input_stride_x + y * input_stride_y)); |
Michalis Spyrou | b7b3153 | 2017-11-23 12:10:21 +0000 | [diff] [blame] | 122 | } |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | if(_has_bias) |
| 127 | { |
Georgios Pinitas | d05dce4 | 2018-01-22 16:29:17 +0000 | [diff] [blame] | 128 | *output_ptr = static_cast<T>(1); |
Michalis Spyrou | b7b3153 | 2017-11-23 12:10:21 +0000 | [diff] [blame] | 129 | } |
| 130 | }, |
| 131 | in, out); |
| 132 | } |
Georgios Pinitas | d05dce4 | 2018-01-22 16:29:17 +0000 | [diff] [blame] | 133 | |
| 134 | NEDepthwiseIm2ColKernel::NEDepthwiseIm2ColKernel() |
Usama Arif | 881f2de | 2019-04-12 10:29:17 +0100 | [diff] [blame] | 135 | : _func(nullptr), _input(nullptr), _output(nullptr), _kernel_dims(), _conv_info(), _has_bias(), _depth_multiplier(1), _dilation() |
Georgios Pinitas | d05dce4 | 2018-01-22 16:29:17 +0000 | [diff] [blame] | 136 | { |
| 137 | } |
| 138 | |
Usama Arif | 881f2de | 2019-04-12 10:29:17 +0100 | [diff] [blame] | 139 | void NEDepthwiseIm2ColKernel::configure(const ITensor *input, ITensor *output, const Size2D &kernel_dims, const PadStrideInfo &conv_info, bool has_bias, unsigned int depth_multiplier, |
| 140 | const Size2D &dilation) |
Georgios Pinitas | d05dce4 | 2018-01-22 16:29:17 +0000 | [diff] [blame] | 141 | { |
Abe Mbise | 7784c83 | 2018-05-31 16:48:41 +0100 | [diff] [blame] | 142 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); |
| 143 | |
Usama Arif | 881f2de | 2019-04-12 10:29:17 +0100 | [diff] [blame] | 144 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), kernel_dims, conv_info, has_bias, depth_multiplier, dilation)); |
Georgios Pinitas | d05dce4 | 2018-01-22 16:29:17 +0000 | [diff] [blame] | 145 | |
Giorgio Arena | 7657224 | 2018-04-04 17:44:26 +0100 | [diff] [blame] | 146 | _input = input; |
| 147 | _output = output; |
| 148 | _kernel_dims = kernel_dims; |
| 149 | _conv_info = conv_info; |
| 150 | _has_bias = has_bias; |
| 151 | _depth_multiplier = depth_multiplier; |
Usama Arif | 881f2de | 2019-04-12 10:29:17 +0100 | [diff] [blame] | 152 | _dilation = dilation; |
Georgios Pinitas | d05dce4 | 2018-01-22 16:29:17 +0000 | [diff] [blame] | 153 | |
| 154 | // Configure kernel window |
| 155 | Window win = calculate_max_window(*input->info(), Steps()); |
| 156 | |
| 157 | // Set appropriate function to run |
| 158 | switch(input->info()->data_type()) |
| 159 | { |
| 160 | case DataType::QASYMM8: |
| 161 | _func = &NEDepthwiseIm2ColKernel::run_generic<uint8_t>; |
| 162 | break; |
| 163 | case DataType::F16: |
| 164 | _func = &NEDepthwiseIm2ColKernel::run_generic<half>; |
| 165 | break; |
| 166 | case DataType::F32: |
| 167 | _func = &NEDepthwiseIm2ColKernel::run_generic<float>; |
| 168 | break; |
| 169 | default: |
| 170 | ARM_COMPUTE_ERROR("Unsupported data type"); |
| 171 | } |
| 172 | |
| 173 | // The NEDepthwiseIm2ColKernel doesn't need padding so update_window_and_padding() can be skipped |
| 174 | output->info()->set_valid_region(ValidRegion(Coordinates(), output->info()->tensor_shape())); |
| 175 | |
| 176 | INEKernel::configure(win); |
| 177 | } |
| 178 | |
Usama Arif | 881f2de | 2019-04-12 10:29:17 +0100 | [diff] [blame] | 179 | Status NEDepthwiseIm2ColKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const Size2D &kernel_dims, const PadStrideInfo &conv_info, bool has_bias, unsigned int depth_multiplier, |
| 180 | const Size2D &dilation) |
Abe Mbise | 7784c83 | 2018-05-31 16:48:41 +0100 | [diff] [blame] | 181 | { |
| 182 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output); |
Usama Arif | 881f2de | 2019-04-12 10:29:17 +0100 | [diff] [blame] | 183 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, kernel_dims, conv_info, has_bias, depth_multiplier, dilation)); |
Abe Mbise | 7784c83 | 2018-05-31 16:48:41 +0100 | [diff] [blame] | 184 | return Status{}; |
| 185 | } |
| 186 | |
Georgios Pinitas | d05dce4 | 2018-01-22 16:29:17 +0000 | [diff] [blame] | 187 | void NEDepthwiseIm2ColKernel::run(const Window &window, const ThreadInfo &info) |
| 188 | { |
| 189 | ARM_COMPUTE_UNUSED(info); |
| 190 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 191 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window); |
| 192 | |
| 193 | if(_func != nullptr) |
| 194 | { |
| 195 | (this->*_func)(window); |
| 196 | } |
| 197 | } |