Michalis Spyrou | 7362f0d | 2017-10-18 17:58:22 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017 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 | */ |
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 | |
Giorgio Arena | 04a8f8c | 2017-11-23 11:45:24 +0000 | [diff] [blame] | 45 | NEDepthwiseConvolutionLayer3x3Kernel::NEDepthwiseConvolutionLayer3x3Kernel() |
Michalis Spyrou | 7362f0d | 2017-10-18 17:58:22 +0100 | [diff] [blame] | 46 | : _border_size(0), _input(), _output(), _weights(), _conv_info() |
| 47 | { |
| 48 | } |
| 49 | |
Giorgio Arena | 04a8f8c | 2017-11-23 11:45:24 +0000 | [diff] [blame] | 50 | BorderSize NEDepthwiseConvolutionLayer3x3Kernel::border_size() const |
Michalis Spyrou | 7362f0d | 2017-10-18 17:58:22 +0100 | [diff] [blame] | 51 | { |
| 52 | return _border_size; |
| 53 | } |
| 54 | |
Giorgio Arena | 04a8f8c | 2017-11-23 11:45:24 +0000 | [diff] [blame] | 55 | void NEDepthwiseConvolutionLayer3x3Kernel::configure(const ITensor *input, const ITensor *weights, ITensor *output, const PadStrideInfo &conv_info) |
Michalis Spyrou | 7362f0d | 2017-10-18 17:58:22 +0100 | [diff] [blame] | 56 | { |
| 57 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F32); |
Georgios Pinitas | 1250a5a | 2018-01-02 13:27:37 +0000 | [diff] [blame] | 58 | ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights); |
Michalis Spyrou | 7362f0d | 2017-10-18 17:58:22 +0100 | [diff] [blame] | 59 | ARM_COMPUTE_ERROR_ON(weights->info()->dimension(0) != 3 || weights->info()->dimension(1) != 3); |
| 60 | |
Georgios Pinitas | 1250a5a | 2018-01-02 13:27:37 +0000 | [diff] [blame] | 61 | // Get convolved dimensions |
| 62 | const TensorShape output_shape = compute_depthwise_convolution_shape(*input->info(), *weights->info(), conv_info); |
Michalis Spyrou | 7362f0d | 2017-10-18 17:58:22 +0100 | [diff] [blame] | 63 | |
Georgios Pinitas | 1250a5a | 2018-01-02 13:27:37 +0000 | [diff] [blame] | 64 | // Output auto inizialitation if not yet initialized |
| 65 | auto_init_if_empty(*output->info(), |
| 66 | output_shape, |
| 67 | 1, |
| 68 | input->info()->data_type(), |
| 69 | input->info()->fixed_point_position(), |
| 70 | input->info()->quantization_info()); |
| 71 | |
| 72 | ARM_COMPUTE_ERROR_ON_MISMATCHING_DIMENSIONS(output->info()->tensor_shape(), output_shape); |
Michalis Spyrou | 7362f0d | 2017-10-18 17:58:22 +0100 | [diff] [blame] | 73 | |
| 74 | _input = input; |
| 75 | _output = output; |
| 76 | _weights = weights; |
| 77 | _conv_info = conv_info; |
| 78 | const unsigned int conv_stride_x = conv_info.stride().first; |
Anthony Barbier | 1568621 | 2017-12-12 17:17:50 +0000 | [diff] [blame] | 79 | const unsigned int conv_stride_y = conv_info.stride().second; |
Michalis Spyrou | 7362f0d | 2017-10-18 17:58:22 +0100 | [diff] [blame] | 80 | const unsigned int conv_pad_x = conv_info.pad().first; |
| 81 | const unsigned int conv_pad_y = conv_info.pad().second; |
| 82 | |
| 83 | ARM_COMPUTE_ERROR_ON(conv_stride_x < 1 || conv_stride_x > 3); |
| 84 | |
| 85 | const unsigned int num_elems_written_per_iteration = 16 >> conv_stride_x; |
| 86 | _border_size = BorderSize(conv_pad_y, conv_pad_x); |
| 87 | |
| 88 | // Configure kernel window |
| 89 | Window win = calculate_max_window(*output->info(), Steps(num_elems_written_per_iteration)); |
| 90 | |
Georgios Pinitas | 1250a5a | 2018-01-02 13:27:37 +0000 | [diff] [blame] | 91 | const unsigned int num_x_steps = (output_shape.x() + num_elems_written_per_iteration - 1) / num_elems_written_per_iteration; |
Anthony Barbier | 1568621 | 2017-12-12 17:17:50 +0000 | [diff] [blame] | 92 | const int input_num_elems_processed = get_input_num_elems_processed(num_elems_written_per_iteration, conv_stride_x); |
| 93 | |
Georgios Pinitas | 1250a5a | 2018-01-02 13:27:37 +0000 | [diff] [blame] | 94 | AccessWindowStatic input_access(input->info(), -conv_pad_x, -conv_pad_y, (num_x_steps - 1) * input_num_elems_processed + 12, conv_stride_y * (output_shape.y() - 1) + 2); |
Anthony Barbier | 1568621 | 2017-12-12 17:17:50 +0000 | [diff] [blame] | 95 | AccessWindowStatic weights_access(weights->info(), 0, 0, weights->info()->dimension(0), weights->info()->dimension(1)); |
Georgios Pinitas | 1250a5a | 2018-01-02 13:27:37 +0000 | [diff] [blame] | 96 | AccessWindowStatic output_access(output->info(), 0, 0, num_x_steps * num_elems_written_per_iteration, output_shape.y()); |
Michalis Spyrou | 7362f0d | 2017-10-18 17:58:22 +0100 | [diff] [blame] | 97 | |
| 98 | update_window_and_padding(win, input_access, weights_access, output_access); |
| 99 | output_access.set_valid_region(win, ValidRegion(Coordinates(), output->info()->tensor_shape())); |
| 100 | |
| 101 | INEKernel::configure(win); |
| 102 | } |
| 103 | |
| 104 | template <unsigned int stridex> |
| 105 | class convolver_3x3 |
| 106 | { |
| 107 | public: |
| 108 | static void convolve(const Window &window, unsigned int num_elems_written_per_iteration, |
| 109 | const ITensor *input, const ITensor *weights, ITensor *output, const PadStrideInfo &conv_info) |
| 110 | { |
| 111 | const int input_stride_x = input->info()->strides_in_bytes().x(); |
| 112 | const int input_stride_y = input->info()->strides_in_bytes().y(); |
| 113 | const int output_stride_y = output->info()->strides_in_bytes().y(); |
| 114 | const int kernel_stride_y = weights->info()->strides_in_bytes().y(); |
| 115 | const int kernel_stride_z = weights->info()->strides_in_bytes().z(); |
| 116 | const int output_w = output->info()->dimension(0); |
| 117 | const int output_h = output->info()->dimension(1); |
| 118 | const int delta_input = get_input_num_elems_processed<stridex>(num_elems_written_per_iteration); |
| 119 | const unsigned int conv_stride_y = std::get<1>(conv_info.stride()); |
| 120 | const unsigned int conv_pad_x = std::get<0>(conv_info.pad()); |
| 121 | const unsigned int conv_pad_y = std::get<1>(conv_info.pad()); |
| 122 | |
| 123 | // setup output window for the iterator |
| 124 | Window window_out = window; |
| 125 | window_out.set(Window::DimX, Window::Dimension(0, output->info()->dimension(Window::DimX), output->info()->dimension(Window::DimX))); |
| 126 | window_out.set(Window::DimY, Window::Dimension(0, output->info()->dimension(Window::DimY), output->info()->dimension(Window::DimY))); |
| 127 | |
| 128 | // setup input window for the iterator |
| 129 | Window window_in = window; |
| 130 | // we just want execute_window_loop to iterate over the dimensions > 2, so we set the first 2 dimensions to 0 |
| 131 | window_in.set(Window::DimX, Window::Dimension(0, 0, 0)); |
| 132 | window_in.set(Window::DimY, Window::Dimension(0, 0, 0)); |
| 133 | |
| 134 | Window window_k = calculate_max_window(*weights->info(), Steps(1u)); |
| 135 | |
| 136 | Iterator in(input, window_in); |
| 137 | Iterator out(output, window_out); |
| 138 | Iterator w(weights, window_k); |
| 139 | |
| 140 | const uint8_t *weights_ptr = w.ptr(); |
| 141 | |
| 142 | execute_window_loop(window_out, [&](const Coordinates & id) |
| 143 | { |
| 144 | const uint8_t *input_ptr = in.ptr() - conv_pad_x * input_stride_x - conv_pad_y * input_stride_y; |
| 145 | int ih = 0; |
| 146 | int oh = 0; |
| 147 | |
Anthony Barbier | 1568621 | 2017-12-12 17:17:50 +0000 | [diff] [blame] | 148 | const uint8_t *ptr_weights_base = weights_ptr + id.z() * kernel_stride_z; |
| 149 | const auto ptr_weights_r0 = reinterpret_cast<const float *>(ptr_weights_base); |
| 150 | const auto ptr_weights_r1 = reinterpret_cast<const float *>(ptr_weights_base + kernel_stride_y); |
| 151 | const auto ptr_weights_r2 = reinterpret_cast<const float *>(ptr_weights_base + kernel_stride_y * 2); |
| 152 | const float32x4x3_t vw_r0 = load_matrix_row(ptr_weights_r0); |
| 153 | const float32x4x3_t vw_r1 = load_matrix_row(ptr_weights_r1); |
| 154 | const float32x4x3_t vw_r2 = load_matrix_row(ptr_weights_r2); |
Michalis Spyrou | 7362f0d | 2017-10-18 17:58:22 +0100 | [diff] [blame] | 155 | |
| 156 | for(ih = 0, oh = 0; oh < output_h; ++oh, ih += conv_stride_y) |
| 157 | { |
| 158 | auto in_top = reinterpret_cast<const float *>(input_ptr + (ih + 0) * input_stride_y); |
| 159 | auto in_mid = reinterpret_cast<const float *>(input_ptr + (ih + 1) * input_stride_y); |
| 160 | auto in_low = reinterpret_cast<const float *>(input_ptr + (ih + 2) * input_stride_y); |
| 161 | auto p_out = reinterpret_cast<float *>(out.ptr() + oh * output_stride_y); |
| 162 | |
| 163 | for(int ow = 0; ow < output_w; ow += num_elems_written_per_iteration, |
| 164 | in_top += delta_input, in_mid += delta_input, in_low += delta_input, p_out += num_elems_written_per_iteration) |
| 165 | { |
| 166 | auto vres = convolve_3x3<stridex>(in_top, in_mid, in_low, vw_r0, vw_r1, vw_r2, 0); |
| 167 | store_results<stridex>(p_out, vres); |
| 168 | } |
| 169 | } |
| 170 | }, |
| 171 | in, out); |
| 172 | } |
| 173 | }; |
| 174 | |
Giorgio Arena | 04a8f8c | 2017-11-23 11:45:24 +0000 | [diff] [blame] | 175 | void NEDepthwiseConvolutionLayer3x3Kernel::run(const Window &window, const ThreadInfo &info) |
Michalis Spyrou | 7362f0d | 2017-10-18 17:58:22 +0100 | [diff] [blame] | 176 | { |
| 177 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 178 | ARM_COMPUTE_UNUSED(info); |
| 179 | |
| 180 | const unsigned int conv_stride_x = _conv_info.stride().first; |
| 181 | const unsigned int num_elems_written_per_iteration = 16 >> conv_stride_x; |
| 182 | |
| 183 | switch(conv_stride_x) |
| 184 | { |
| 185 | case 1: |
| 186 | convolver_3x3<1>::convolve(window, num_elems_written_per_iteration, _input, _weights, _output, _conv_info); |
| 187 | break; |
| 188 | case 2: |
| 189 | convolver_3x3<2>::convolve(window, num_elems_written_per_iteration, _input, _weights, _output, _conv_info); |
| 190 | break; |
| 191 | case 3: |
| 192 | convolver_3x3<3>::convolve(window, num_elems_written_per_iteration, _input, _weights, _output, _conv_info); |
| 193 | break; |
| 194 | default: |
| 195 | ARM_COMPUTE_ERROR("Not implemented"); |
| 196 | } |
| 197 | } |