Michalis Spyrou | 7362f0d | 2017-10-18 17:58:22 +0100 | [diff] [blame] | 1 | /* |
Michalis Spyrou | 621965e | 2018-01-08 17:11:26 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2018 ARM Limited. |
Michalis Spyrou | 7362f0d | 2017-10-18 17:58:22 +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 | */ |
Giorgio Arena | 04a8f8c | 2017-11-23 11:45:24 +0000 | [diff] [blame] | 24 | #include "arm_compute/core/NEON/kernels/NEDepthwiseConvolutionLayer3x3Kernel.h" |
Michalis Spyrou | 7362f0d | 2017-10-18 17:58:22 +0100 | [diff] [blame] | 25 | #include "arm_compute/core/NEON/kernels/convolution/NEDirectConvolutionDetail.h" |
| 26 | |
| 27 | #include "arm_compute/core/AccessWindowStatic.h" |
| 28 | #include "arm_compute/core/AccessWindowTranspose.h" |
| 29 | #include "arm_compute/core/Coordinates.h" |
| 30 | #include "arm_compute/core/Error.h" |
| 31 | #include "arm_compute/core/Helpers.h" |
| 32 | #include "arm_compute/core/ITensor.h" |
| 33 | #include "arm_compute/core/NEON/INEKernel.h" |
| 34 | #include "arm_compute/core/TensorInfo.h" |
| 35 | #include "arm_compute/core/TensorShape.h" |
| 36 | #include "arm_compute/core/Types.h" |
| 37 | #include "arm_compute/core/Validate.h" |
| 38 | #include "arm_compute/core/Window.h" |
Georgios Pinitas | 1250a5a | 2018-01-02 13:27:37 +0000 | [diff] [blame] | 39 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
Michalis Spyrou | 7362f0d | 2017-10-18 17:58:22 +0100 | [diff] [blame] | 40 | |
| 41 | using namespace arm_compute; |
| 42 | using namespace arm_compute::detail; |
Georgios Pinitas | 1250a5a | 2018-01-02 13:27:37 +0000 | [diff] [blame] | 43 | using namespace arm_compute::misc::shape_calculator; |
Michalis Spyrou | 7362f0d | 2017-10-18 17:58:22 +0100 | [diff] [blame] | 44 | |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 45 | namespace |
Michalis Spyrou | 7362f0d | 2017-10-18 17:58:22 +0100 | [diff] [blame] | 46 | { |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 47 | template <typename T1, typename T2, unsigned int stridex> |
Michalis Spyrou | 7362f0d | 2017-10-18 17:58:22 +0100 | [diff] [blame] | 48 | class convolver_3x3 |
| 49 | { |
| 50 | public: |
| 51 | static void convolve(const Window &window, unsigned int num_elems_written_per_iteration, |
| 52 | const ITensor *input, const ITensor *weights, ITensor *output, const PadStrideInfo &conv_info) |
| 53 | { |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 54 | const int input_offset = -input->info()->quantization_info().offset; |
| 55 | const int weights_offset = -weights->info()->quantization_info().offset; |
| 56 | |
Michalis Spyrou | 7362f0d | 2017-10-18 17:58:22 +0100 | [diff] [blame] | 57 | const int input_stride_x = input->info()->strides_in_bytes().x(); |
| 58 | const int input_stride_y = input->info()->strides_in_bytes().y(); |
| 59 | const int output_stride_y = output->info()->strides_in_bytes().y(); |
| 60 | const int kernel_stride_y = weights->info()->strides_in_bytes().y(); |
| 61 | const int kernel_stride_z = weights->info()->strides_in_bytes().z(); |
| 62 | const int output_w = output->info()->dimension(0); |
| 63 | const int output_h = output->info()->dimension(1); |
| 64 | const int delta_input = get_input_num_elems_processed<stridex>(num_elems_written_per_iteration); |
| 65 | const unsigned int conv_stride_y = std::get<1>(conv_info.stride()); |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 66 | const unsigned int conv_pad_x = conv_info.pad_left(); |
| 67 | const unsigned int conv_pad_y = conv_info.pad_top(); |
Michalis Spyrou | 7362f0d | 2017-10-18 17:58:22 +0100 | [diff] [blame] | 68 | |
| 69 | // setup output window for the iterator |
| 70 | Window window_out = window; |
| 71 | window_out.set(Window::DimX, Window::Dimension(0, output->info()->dimension(Window::DimX), output->info()->dimension(Window::DimX))); |
| 72 | window_out.set(Window::DimY, Window::Dimension(0, output->info()->dimension(Window::DimY), output->info()->dimension(Window::DimY))); |
| 73 | |
| 74 | // setup input window for the iterator |
| 75 | Window window_in = window; |
| 76 | // we just want execute_window_loop to iterate over the dimensions > 2, so we set the first 2 dimensions to 0 |
| 77 | window_in.set(Window::DimX, Window::Dimension(0, 0, 0)); |
| 78 | window_in.set(Window::DimY, Window::Dimension(0, 0, 0)); |
| 79 | |
| 80 | Window window_k = calculate_max_window(*weights->info(), Steps(1u)); |
| 81 | |
| 82 | Iterator in(input, window_in); |
| 83 | Iterator out(output, window_out); |
| 84 | Iterator w(weights, window_k); |
| 85 | |
| 86 | const uint8_t *weights_ptr = w.ptr(); |
| 87 | |
| 88 | execute_window_loop(window_out, [&](const Coordinates & id) |
| 89 | { |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 90 | int ih = 0; |
| 91 | int oh = 0; |
Michalis Spyrou | 7362f0d | 2017-10-18 17:58:22 +0100 | [diff] [blame] | 92 | |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 93 | const uint8_t *input_ptr = in.ptr() - conv_pad_x * input_stride_x - conv_pad_y * input_stride_y; |
| 94 | const uint8_t *ptr_weights_base = weights_ptr + id.z() * kernel_stride_z; |
| 95 | |
| 96 | const auto ptr_weights_r0 = reinterpret_cast<const T1 *>(ptr_weights_base); |
| 97 | const auto ptr_weights_r1 = reinterpret_cast<const T1 *>(ptr_weights_base + kernel_stride_y); |
| 98 | const auto ptr_weights_r2 = reinterpret_cast<const T1 *>(ptr_weights_base + kernel_stride_y * 2); |
| 99 | const auto vw_r0 = load_matrix_row(ptr_weights_r0, weights_offset); |
| 100 | const auto vw_r1 = load_matrix_row(ptr_weights_r1, weights_offset); |
| 101 | const auto vw_r2 = load_matrix_row(ptr_weights_r2, weights_offset); |
Michalis Spyrou | 7362f0d | 2017-10-18 17:58:22 +0100 | [diff] [blame] | 102 | |
| 103 | for(ih = 0, oh = 0; oh < output_h; ++oh, ih += conv_stride_y) |
| 104 | { |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 105 | auto in_top = reinterpret_cast<const T1 *>(input_ptr + (ih + 0) * input_stride_y); |
| 106 | auto in_mid = reinterpret_cast<const T1 *>(input_ptr + (ih + 1) * input_stride_y); |
| 107 | auto in_low = reinterpret_cast<const T1 *>(input_ptr + (ih + 2) * input_stride_y); |
| 108 | auto p_out = reinterpret_cast<T2 *>(out.ptr() + oh * output_stride_y); |
Michalis Spyrou | 7362f0d | 2017-10-18 17:58:22 +0100 | [diff] [blame] | 109 | |
| 110 | for(int ow = 0; ow < output_w; ow += num_elems_written_per_iteration, |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 111 | in_top += delta_input, in_mid += delta_input, in_low += delta_input, |
| 112 | p_out += num_elems_written_per_iteration) |
Michalis Spyrou | 7362f0d | 2017-10-18 17:58:22 +0100 | [diff] [blame] | 113 | { |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 114 | auto vres = convolve_3x3<stridex>(in_top, in_mid, in_low, vw_r0, vw_r1, vw_r2, 0, input_offset); |
Michalis Spyrou | 7362f0d | 2017-10-18 17:58:22 +0100 | [diff] [blame] | 115 | store_results<stridex>(p_out, vres); |
| 116 | } |
| 117 | } |
| 118 | }, |
| 119 | in, out); |
| 120 | } |
| 121 | }; |
| 122 | |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 123 | template <typename T1, typename T2> |
| 124 | inline void convolve_3x3(const Window &window, unsigned int num_elems_written_per_iteration, |
| 125 | const ITensor *input, const ITensor *weights, ITensor *output, const PadStrideInfo &conv_info) |
| 126 | { |
| 127 | const unsigned int conv_stride_x = std::get<0>(conv_info.stride()); |
| 128 | switch(conv_stride_x) |
| 129 | { |
| 130 | case 1: |
| 131 | convolver_3x3<T1, T2, 1>::convolve(window, num_elems_written_per_iteration, input, weights, output, conv_info); |
| 132 | break; |
| 133 | case 2: |
| 134 | convolver_3x3<T1, T2, 2>::convolve(window, num_elems_written_per_iteration, input, weights, output, conv_info); |
| 135 | break; |
| 136 | case 3: |
| 137 | convolver_3x3<T1, T2, 3>::convolve(window, num_elems_written_per_iteration, input, weights, output, conv_info); |
| 138 | break; |
| 139 | default: |
| 140 | ARM_COMPUTE_ERROR("Not implemented"); |
| 141 | } |
| 142 | } |
| 143 | } // namespace |
| 144 | |
| 145 | NEDepthwiseConvolutionLayer3x3Kernel::NEDepthwiseConvolutionLayer3x3Kernel() |
| 146 | : _border_size(0), _input(), _output(), _weights(), _conv_info(), _num_elems_written_per_iteration(0) |
| 147 | { |
| 148 | } |
| 149 | |
| 150 | BorderSize NEDepthwiseConvolutionLayer3x3Kernel::border_size() const |
| 151 | { |
| 152 | return _border_size; |
| 153 | } |
| 154 | |
| 155 | void NEDepthwiseConvolutionLayer3x3Kernel::configure(const ITensor *input, const ITensor *weights, ITensor *output, const PadStrideInfo &conv_info) |
| 156 | { |
| 157 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::F32); |
| 158 | ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights); |
| 159 | ARM_COMPUTE_ERROR_ON(weights->info()->dimension(0) != 3 || weights->info()->dimension(1) != 3); |
| 160 | |
| 161 | // Get convolved dimensions |
| 162 | const TensorShape output_shape = compute_depthwise_convolution_shape(*input->info(), *weights->info(), conv_info); |
| 163 | const DataType output_dt = (input->info()->data_type() == DataType::QASYMM8) ? DataType::S32 : input->info()->data_type(); |
| 164 | |
| 165 | // Output auto inizialitation if not yet initialized |
| 166 | auto_init_if_empty(*output->info(), |
| 167 | input->info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(output_shape).set_data_type(output_dt)); |
| 168 | |
| 169 | ARM_COMPUTE_ERROR_ON_MISMATCHING_DIMENSIONS(output->info()->tensor_shape(), output_shape); |
| 170 | |
| 171 | _input = input; |
| 172 | _output = output; |
| 173 | _weights = weights; |
| 174 | _conv_info = conv_info; |
| 175 | const unsigned int conv_stride_x = conv_info.stride().first; |
| 176 | const unsigned int conv_stride_y = conv_info.stride().second; |
| 177 | const unsigned int conv_pad_left = conv_info.pad_left(); |
| 178 | const unsigned int conv_pad_top = conv_info.pad_top(); |
| 179 | |
| 180 | ARM_COMPUTE_ERROR_ON(conv_stride_x < 1 || conv_stride_x > 3); |
| 181 | |
| 182 | unsigned int num_elems_read_per_iteration = 0; |
| 183 | switch(input->info()->data_type()) |
| 184 | { |
| 185 | case DataType::QASYMM8: |
| 186 | num_elems_read_per_iteration = 16; |
| 187 | _num_elems_written_per_iteration = 16 >> conv_stride_x; |
| 188 | break; |
| 189 | case DataType::F32: |
| 190 | num_elems_read_per_iteration = 12; |
| 191 | _num_elems_written_per_iteration = 16 >> conv_stride_x; |
| 192 | break; |
| 193 | default: |
| 194 | ARM_COMPUTE_ERROR("Data type not supported."); |
| 195 | } |
| 196 | _border_size = BorderSize(conv_pad_top, conv_info.pad_right(), conv_info.pad_bottom(), conv_pad_left); |
| 197 | |
| 198 | // Configure kernel window |
| 199 | Window win = calculate_max_window(*output->info(), Steps(_num_elems_written_per_iteration)); |
| 200 | |
| 201 | const unsigned int num_x_steps = (output_shape.x() + _num_elems_written_per_iteration - 1) / _num_elems_written_per_iteration; |
| 202 | const int input_num_elems_processed = get_input_num_elems_processed(_num_elems_written_per_iteration, conv_stride_x); |
| 203 | |
| 204 | AccessWindowStatic input_access(input->info(), |
| 205 | -conv_pad_left, |
| 206 | -conv_pad_top, |
| 207 | (num_x_steps - 1) * input_num_elems_processed + num_elems_read_per_iteration, |
| 208 | conv_stride_y * (output_shape.y() - 1) + 2); |
| 209 | AccessWindowStatic weights_access(weights->info(), 0, 0, weights->info()->dimension(0), weights->info()->dimension(1)); |
| 210 | AccessWindowStatic output_access(output->info(), 0, 0, num_x_steps * _num_elems_written_per_iteration, output_shape.y()); |
| 211 | |
| 212 | update_window_and_padding(win, input_access, weights_access, output_access); |
| 213 | output_access.set_valid_region(win, ValidRegion(Coordinates(), output->info()->tensor_shape())); |
| 214 | |
| 215 | INEKernel::configure(win); |
| 216 | } |
| 217 | |
Giorgio Arena | 04a8f8c | 2017-11-23 11:45:24 +0000 | [diff] [blame] | 218 | void NEDepthwiseConvolutionLayer3x3Kernel::run(const Window &window, const ThreadInfo &info) |
Michalis Spyrou | 7362f0d | 2017-10-18 17:58:22 +0100 | [diff] [blame] | 219 | { |
| 220 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 221 | ARM_COMPUTE_UNUSED(info); |
| 222 | |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 223 | switch(_input->info()->data_type()) |
Michalis Spyrou | 7362f0d | 2017-10-18 17:58:22 +0100 | [diff] [blame] | 224 | { |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 225 | case DataType::F32: |
| 226 | convolve_3x3<float, float>(window, _num_elems_written_per_iteration, _input, _weights, _output, _conv_info); |
Michalis Spyrou | 7362f0d | 2017-10-18 17:58:22 +0100 | [diff] [blame] | 227 | break; |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 228 | case DataType::QASYMM8: |
| 229 | convolve_3x3<uint8_t, int32_t>(window, _num_elems_written_per_iteration, _input, _weights, _output, _conv_info); |
Michalis Spyrou | 7362f0d | 2017-10-18 17:58:22 +0100 | [diff] [blame] | 230 | break; |
| 231 | default: |
| 232 | ARM_COMPUTE_ERROR("Not implemented"); |
| 233 | } |
| 234 | } |