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" |
Georgios Pinitas | 4074c99 | 2018-01-30 18:13:46 +0000 | [diff] [blame] | 25 | #include "arm_compute/core/NEON/kernels/detail/NEDirectConvolutionDetail.h" |
Michalis Spyrou | 7362f0d | 2017-10-18 17:58:22 +0100 | [diff] [blame] | 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" |
Georgios Pinitas | 4074c99 | 2018-01-30 18:13:46 +0000 | [diff] [blame] | 37 | #include "arm_compute/core/Utils.h" |
Michalis Spyrou | 7362f0d | 2017-10-18 17:58:22 +0100 | [diff] [blame] | 38 | #include "arm_compute/core/Validate.h" |
| 39 | #include "arm_compute/core/Window.h" |
Georgios Pinitas | 1250a5a | 2018-01-02 13:27:37 +0000 | [diff] [blame] | 40 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
Georgios Pinitas | 4074c99 | 2018-01-30 18:13:46 +0000 | [diff] [blame] | 41 | #include "support/ToolchainSupport.h" |
Michalis Spyrou | 7362f0d | 2017-10-18 17:58:22 +0100 | [diff] [blame] | 42 | |
| 43 | using namespace arm_compute; |
| 44 | using namespace arm_compute::detail; |
Georgios Pinitas | 1250a5a | 2018-01-02 13:27:37 +0000 | [diff] [blame] | 45 | using namespace arm_compute::misc::shape_calculator; |
Georgios Pinitas | 4074c99 | 2018-01-30 18:13:46 +0000 | [diff] [blame] | 46 | using namespace depthwise; |
Michalis Spyrou | 7362f0d | 2017-10-18 17:58:22 +0100 | [diff] [blame] | 47 | |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 48 | namespace |
Michalis Spyrou | 7362f0d | 2017-10-18 17:58:22 +0100 | [diff] [blame] | 49 | { |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 50 | template <typename T1, typename T2, unsigned int stridex> |
Michalis Spyrou | 7362f0d | 2017-10-18 17:58:22 +0100 | [diff] [blame] | 51 | class convolver_3x3 |
| 52 | { |
| 53 | public: |
| 54 | static void convolve(const Window &window, unsigned int num_elems_written_per_iteration, |
| 55 | const ITensor *input, const ITensor *weights, ITensor *output, const PadStrideInfo &conv_info) |
| 56 | { |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 57 | const int input_offset = -input->info()->quantization_info().offset; |
| 58 | const int weights_offset = -weights->info()->quantization_info().offset; |
| 59 | |
Michalis Spyrou | 7362f0d | 2017-10-18 17:58:22 +0100 | [diff] [blame] | 60 | const int input_stride_x = input->info()->strides_in_bytes().x(); |
| 61 | const int input_stride_y = input->info()->strides_in_bytes().y(); |
| 62 | const int output_stride_y = output->info()->strides_in_bytes().y(); |
| 63 | const int kernel_stride_y = weights->info()->strides_in_bytes().y(); |
| 64 | const int kernel_stride_z = weights->info()->strides_in_bytes().z(); |
| 65 | const int output_w = output->info()->dimension(0); |
| 66 | const int output_h = output->info()->dimension(1); |
| 67 | const int delta_input = get_input_num_elems_processed<stridex>(num_elems_written_per_iteration); |
| 68 | const unsigned int conv_stride_y = std::get<1>(conv_info.stride()); |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 69 | const unsigned int conv_pad_x = conv_info.pad_left(); |
| 70 | const unsigned int conv_pad_y = conv_info.pad_top(); |
Michalis Spyrou | 7362f0d | 2017-10-18 17:58:22 +0100 | [diff] [blame] | 71 | |
| 72 | // setup output window for the iterator |
| 73 | Window window_out = window; |
| 74 | window_out.set(Window::DimX, Window::Dimension(0, output->info()->dimension(Window::DimX), output->info()->dimension(Window::DimX))); |
| 75 | window_out.set(Window::DimY, Window::Dimension(0, output->info()->dimension(Window::DimY), output->info()->dimension(Window::DimY))); |
| 76 | |
| 77 | // setup input window for the iterator |
| 78 | Window window_in = window; |
| 79 | // we just want execute_window_loop to iterate over the dimensions > 2, so we set the first 2 dimensions to 0 |
| 80 | window_in.set(Window::DimX, Window::Dimension(0, 0, 0)); |
| 81 | window_in.set(Window::DimY, Window::Dimension(0, 0, 0)); |
| 82 | |
| 83 | Window window_k = calculate_max_window(*weights->info(), Steps(1u)); |
| 84 | |
| 85 | Iterator in(input, window_in); |
| 86 | Iterator out(output, window_out); |
| 87 | Iterator w(weights, window_k); |
| 88 | |
| 89 | const uint8_t *weights_ptr = w.ptr(); |
| 90 | |
| 91 | execute_window_loop(window_out, [&](const Coordinates & id) |
| 92 | { |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 93 | int ih = 0; |
| 94 | int oh = 0; |
Michalis Spyrou | 7362f0d | 2017-10-18 17:58:22 +0100 | [diff] [blame] | 95 | |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 96 | const uint8_t *input_ptr = in.ptr() - conv_pad_x * input_stride_x - conv_pad_y * input_stride_y; |
| 97 | const uint8_t *ptr_weights_base = weights_ptr + id.z() * kernel_stride_z; |
| 98 | |
| 99 | const auto ptr_weights_r0 = reinterpret_cast<const T1 *>(ptr_weights_base); |
| 100 | const auto ptr_weights_r1 = reinterpret_cast<const T1 *>(ptr_weights_base + kernel_stride_y); |
| 101 | const auto ptr_weights_r2 = reinterpret_cast<const T1 *>(ptr_weights_base + kernel_stride_y * 2); |
| 102 | const auto vw_r0 = load_matrix_row(ptr_weights_r0, weights_offset); |
| 103 | const auto vw_r1 = load_matrix_row(ptr_weights_r1, weights_offset); |
| 104 | const auto vw_r2 = load_matrix_row(ptr_weights_r2, weights_offset); |
Michalis Spyrou | 7362f0d | 2017-10-18 17:58:22 +0100 | [diff] [blame] | 105 | |
| 106 | for(ih = 0, oh = 0; oh < output_h; ++oh, ih += conv_stride_y) |
| 107 | { |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 108 | auto in_top = reinterpret_cast<const T1 *>(input_ptr + (ih + 0) * input_stride_y); |
| 109 | auto in_mid = reinterpret_cast<const T1 *>(input_ptr + (ih + 1) * input_stride_y); |
| 110 | auto in_low = reinterpret_cast<const T1 *>(input_ptr + (ih + 2) * input_stride_y); |
| 111 | auto p_out = reinterpret_cast<T2 *>(out.ptr() + oh * output_stride_y); |
Michalis Spyrou | 7362f0d | 2017-10-18 17:58:22 +0100 | [diff] [blame] | 112 | |
| 113 | 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] | 114 | in_top += delta_input, in_mid += delta_input, in_low += delta_input, |
| 115 | p_out += num_elems_written_per_iteration) |
Michalis Spyrou | 7362f0d | 2017-10-18 17:58:22 +0100 | [diff] [blame] | 116 | { |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 117 | 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] | 118 | store_results<stridex>(p_out, vres); |
| 119 | } |
| 120 | } |
| 121 | }, |
| 122 | in, out); |
| 123 | } |
| 124 | }; |
| 125 | |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 126 | template <typename T1, typename T2> |
| 127 | inline void convolve_3x3(const Window &window, unsigned int num_elems_written_per_iteration, |
| 128 | const ITensor *input, const ITensor *weights, ITensor *output, const PadStrideInfo &conv_info) |
| 129 | { |
| 130 | const unsigned int conv_stride_x = std::get<0>(conv_info.stride()); |
| 131 | switch(conv_stride_x) |
| 132 | { |
| 133 | case 1: |
| 134 | convolver_3x3<T1, T2, 1>::convolve(window, num_elems_written_per_iteration, input, weights, output, conv_info); |
| 135 | break; |
| 136 | case 2: |
| 137 | convolver_3x3<T1, T2, 2>::convolve(window, num_elems_written_per_iteration, input, weights, output, conv_info); |
| 138 | break; |
| 139 | case 3: |
| 140 | convolver_3x3<T1, T2, 3>::convolve(window, num_elems_written_per_iteration, input, weights, output, conv_info); |
| 141 | break; |
| 142 | default: |
| 143 | ARM_COMPUTE_ERROR("Not implemented"); |
| 144 | } |
| 145 | } |
| 146 | } // namespace |
| 147 | |
| 148 | NEDepthwiseConvolutionLayer3x3Kernel::NEDepthwiseConvolutionLayer3x3Kernel() |
Georgios Pinitas | 4074c99 | 2018-01-30 18:13:46 +0000 | [diff] [blame] | 149 | : _border_size(0), _input(), _output(), _weights(), _conv_info(), _convolver(nullptr), _num_elems_written_per_iteration(0), _run_optimized(false) |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 150 | { |
| 151 | } |
| 152 | |
| 153 | BorderSize NEDepthwiseConvolutionLayer3x3Kernel::border_size() const |
| 154 | { |
| 155 | return _border_size; |
| 156 | } |
| 157 | |
Georgios Pinitas | 4074c99 | 2018-01-30 18:13:46 +0000 | [diff] [blame] | 158 | void NEDepthwiseConvolutionLayer3x3Kernel::configure(const ITensor *input, const ITensor *weights, ITensor *output, const PadStrideInfo &conv_info, DataLayout data_layout) |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 159 | { |
| 160 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::F32); |
| 161 | ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights); |
Georgios Pinitas | 4074c99 | 2018-01-30 18:13:46 +0000 | [diff] [blame] | 162 | |
| 163 | _input = input; |
| 164 | _output = output; |
| 165 | _weights = weights; |
| 166 | _conv_info = conv_info; |
| 167 | _convolver = nullptr; |
| 168 | |
| 169 | _run_optimized = NEDepthwiseConvolutionLayer3x3Kernel::is_optimized_execution_possible(input->info()->tensor_shape(), |
| 170 | conv_info, |
| 171 | input->info()->data_type(), |
| 172 | data_layout); |
| 173 | |
| 174 | (_run_optimized) ? configure_optimized() : configure_generic(); |
| 175 | } |
| 176 | |
| 177 | void NEDepthwiseConvolutionLayer3x3Kernel::run(const Window &window, const ThreadInfo &info) |
| 178 | { |
| 179 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 180 | ARM_COMPUTE_UNUSED(info); |
| 181 | |
| 182 | (_run_optimized) ? run_optimized(window, info) : run_generic(window, info); |
| 183 | } |
| 184 | |
| 185 | bool NEDepthwiseConvolutionLayer3x3Kernel::is_optimized_execution_possible(TensorShape input_shape, PadStrideInfo conv_info, DataType dt, DataLayout data_layout) |
| 186 | { |
| 187 | // Reshape input shape if in NHWC format |
| 188 | TensorShape in_shape{ input_shape }; |
| 189 | if(data_layout == DataLayout::NHWC) |
| 190 | { |
| 191 | in_shape.set(Window::DimX, input_shape.y()); |
| 192 | in_shape.set(Window::DimY, input_shape.z()); |
| 193 | in_shape.set(Window::DimZ, input_shape.x()); |
| 194 | } |
| 195 | |
| 196 | // Check supported data type |
| 197 | bool supported_datatype = (dt == DataType::F32); |
| 198 | |
| 199 | // Check for supported strides |
| 200 | const auto &strides = conv_info.stride(); |
| 201 | bool supported_strides = (strides.first == strides.second) && ((strides.first == 1) || (strides.first == 2)); |
| 202 | |
| 203 | // Check for supported padding |
| 204 | const auto pad_top = conv_info.pad_top(); |
| 205 | const auto pad_right = conv_info.pad_right(); |
| 206 | const auto pad_bottom = conv_info.pad_bottom(); |
| 207 | const auto pad_left = conv_info.pad_left(); |
| 208 | PadStrideInfo same_pad = calculate_same_pad(in_shape, TensorShape(3U, 3U), conv_info); |
| 209 | bool is_same_padding = (pad_top == same_pad.pad_top()) && (pad_right == same_pad.pad_right()) && (pad_bottom == same_pad.pad_bottom()) && (pad_left == same_pad.pad_left()); |
| 210 | bool is_valid_padding = (pad_top == 0) && (pad_right == 0) && (pad_bottom == 0) && (pad_left == 0); |
| 211 | bool supported_padding = is_same_padding || is_valid_padding; |
| 212 | |
| 213 | return supported_datatype && supported_strides && supported_padding; |
| 214 | } |
| 215 | |
| 216 | void NEDepthwiseConvolutionLayer3x3Kernel::generate_convolver() |
| 217 | { |
| 218 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(_input, 1, DataType::F32); |
| 219 | ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(_input, _weights); |
| 220 | ARM_COMPUTE_ERROR_ON(_weights->info()->dimension(1) != 3 || _weights->info()->dimension(2) != 3); |
| 221 | |
Georgios Pinitas | be0ae93 | 2018-03-13 13:08:12 +0000 | [diff] [blame] | 222 | _convolver = create_convolver_object(_conv_info, _weights, _input, _output, true); |
Georgios Pinitas | 4074c99 | 2018-01-30 18:13:46 +0000 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | void NEDepthwiseConvolutionLayer3x3Kernel::configure_generic() |
| 226 | { |
| 227 | ARM_COMPUTE_ERROR_ON(_weights->info()->dimension(0) != 3 || _weights->info()->dimension(1) != 3); |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 228 | |
| 229 | // Get convolved dimensions |
Georgios Pinitas | 4074c99 | 2018-01-30 18:13:46 +0000 | [diff] [blame] | 230 | const TensorShape output_shape = compute_depthwise_convolution_shape(*_input->info(), *_weights->info(), _conv_info); |
| 231 | const DataType output_dt = (_input->info()->data_type() == DataType::QASYMM8) ? DataType::S32 : _input->info()->data_type(); |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 232 | |
| 233 | // Output auto inizialitation if not yet initialized |
Georgios Pinitas | 4074c99 | 2018-01-30 18:13:46 +0000 | [diff] [blame] | 234 | auto_init_if_empty(*_output->info(), |
| 235 | _input->info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(output_shape).set_data_type(output_dt)); |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 236 | |
Georgios Pinitas | 4074c99 | 2018-01-30 18:13:46 +0000 | [diff] [blame] | 237 | ARM_COMPUTE_ERROR_ON_MISMATCHING_DIMENSIONS(_output->info()->tensor_shape(), output_shape); |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 238 | |
Georgios Pinitas | 4074c99 | 2018-01-30 18:13:46 +0000 | [diff] [blame] | 239 | const unsigned int conv_stride_x = _conv_info.stride().first; |
Georgios Pinitas | 1a03d76 | 2018-02-21 14:47:09 +0000 | [diff] [blame] | 240 | const unsigned int conv_stride_y = _conv_info.stride().second; |
Georgios Pinitas | 4074c99 | 2018-01-30 18:13:46 +0000 | [diff] [blame] | 241 | const unsigned int conv_pad_top = _conv_info.pad_top(); |
| 242 | const unsigned int conv_pad_right = _conv_info.pad_right(); |
| 243 | const unsigned int conv_pad_bottom = _conv_info.pad_bottom(); |
| 244 | const unsigned int conv_pad_left = _conv_info.pad_left(); |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 245 | |
| 246 | ARM_COMPUTE_ERROR_ON(conv_stride_x < 1 || conv_stride_x > 3); |
| 247 | |
| 248 | unsigned int num_elems_read_per_iteration = 0; |
Georgios Pinitas | 4074c99 | 2018-01-30 18:13:46 +0000 | [diff] [blame] | 249 | switch(_input->info()->data_type()) |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 250 | { |
| 251 | case DataType::QASYMM8: |
| 252 | num_elems_read_per_iteration = 16; |
| 253 | _num_elems_written_per_iteration = 16 >> conv_stride_x; |
| 254 | break; |
| 255 | case DataType::F32: |
| 256 | num_elems_read_per_iteration = 12; |
| 257 | _num_elems_written_per_iteration = 16 >> conv_stride_x; |
| 258 | break; |
| 259 | default: |
| 260 | ARM_COMPUTE_ERROR("Data type not supported."); |
| 261 | } |
Georgios Pinitas | 4074c99 | 2018-01-30 18:13:46 +0000 | [diff] [blame] | 262 | _border_size = BorderSize(conv_pad_top, conv_pad_right, conv_pad_bottom, conv_pad_left); |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 263 | |
| 264 | // Configure kernel window |
Georgios Pinitas | 4074c99 | 2018-01-30 18:13:46 +0000 | [diff] [blame] | 265 | Window win = calculate_max_window(*_output->info(), Steps(_num_elems_written_per_iteration)); |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 266 | |
Georgios Pinitas | 1a03d76 | 2018-02-21 14:47:09 +0000 | [diff] [blame] | 267 | AccessWindowRectangle input_access(_input->info(), -conv_pad_left, -conv_pad_top, |
| 268 | num_elems_read_per_iteration, 3, |
| 269 | conv_stride_x, conv_stride_y); |
| 270 | AccessWindowStatic weights_access(_weights->info(), 0, 0, 3, 3); |
Georgios Pinitas | 9be0c5a | 2018-02-19 12:46:29 +0000 | [diff] [blame] | 271 | AccessWindowHorizontal output_access(_output->info(), 0, _num_elems_written_per_iteration); |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 272 | |
| 273 | update_window_and_padding(win, input_access, weights_access, output_access); |
Georgios Pinitas | 4074c99 | 2018-01-30 18:13:46 +0000 | [diff] [blame] | 274 | output_access.set_valid_region(win, ValidRegion(Coordinates(), _output->info()->tensor_shape())); |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 275 | |
| 276 | INEKernel::configure(win); |
| 277 | } |
| 278 | |
Georgios Pinitas | 4074c99 | 2018-01-30 18:13:46 +0000 | [diff] [blame] | 279 | void NEDepthwiseConvolutionLayer3x3Kernel::configure_optimized() |
Michalis Spyrou | 7362f0d | 2017-10-18 17:58:22 +0100 | [diff] [blame] | 280 | { |
Georgios Pinitas | 4074c99 | 2018-01-30 18:13:46 +0000 | [diff] [blame] | 281 | ARM_COMPUTE_ERROR_ON(_weights->info()->dimension(1) != 3 || _weights->info()->dimension(2) != 3); |
| 282 | |
| 283 | _border_size = BorderSize(0, 0); |
Georgios Pinitas | be0ae93 | 2018-03-13 13:08:12 +0000 | [diff] [blame] | 284 | _convolver = create_convolver_object(_conv_info, _weights, _input, _output); |
Georgios Pinitas | 4074c99 | 2018-01-30 18:13:46 +0000 | [diff] [blame] | 285 | |
| 286 | // Auto-configure output |
| 287 | bool same_padding = _conv_info.has_padding(); |
| 288 | TensorShape output_shape{ _input->info()->tensor_shape() }; |
| 289 | |
| 290 | output_shape.set(1, _convolver->output_size(output_shape.y(), same_padding)); // Set width |
| 291 | output_shape.set(2, _convolver->output_size(output_shape.z(), same_padding)); // Set height |
| 292 | |
| 293 | // Output auto inizialitation if not yet initialized |
| 294 | auto_init_if_empty(*_output->info(), |
| 295 | _input->info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(output_shape)); |
| 296 | |
Georgios Pinitas | be0ae93 | 2018-03-13 13:08:12 +0000 | [diff] [blame] | 297 | // Set padding in channels |
| 298 | const int num_channels = _weights->info()->dimension(0); |
| 299 | if((num_channels >= 128) && (num_channels % 16 == 0)) |
| 300 | { |
| 301 | _input->info()->extend_padding(PaddingSize(0, 4, 0, 0)); |
| 302 | _weights->info()->extend_padding(PaddingSize(0, 4, 0, 0)); |
| 303 | _output->info()->extend_padding(PaddingSize(0, 4, 0, 0)); |
| 304 | } |
| 305 | |
Georgios Pinitas | 4074c99 | 2018-01-30 18:13:46 +0000 | [diff] [blame] | 306 | // Configure window |
| 307 | Window win; |
| 308 | auto win_last = _convolver->get_window(); |
| 309 | win.set(Window::DimX, Window::Dimension(0, win_last, 1)); |
| 310 | INEKernel::configure(win); |
| 311 | } |
| 312 | |
| 313 | void NEDepthwiseConvolutionLayer3x3Kernel::run_generic(const Window &window, const ThreadInfo &info) |
| 314 | { |
Michalis Spyrou | 7362f0d | 2017-10-18 17:58:22 +0100 | [diff] [blame] | 315 | ARM_COMPUTE_UNUSED(info); |
| 316 | |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 317 | switch(_input->info()->data_type()) |
Michalis Spyrou | 7362f0d | 2017-10-18 17:58:22 +0100 | [diff] [blame] | 318 | { |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 319 | case DataType::F32: |
| 320 | 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] | 321 | break; |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 322 | case DataType::QASYMM8: |
| 323 | 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] | 324 | break; |
| 325 | default: |
| 326 | ARM_COMPUTE_ERROR("Not implemented"); |
| 327 | } |
| 328 | } |
Georgios Pinitas | 4074c99 | 2018-01-30 18:13:46 +0000 | [diff] [blame] | 329 | |
| 330 | void NEDepthwiseConvolutionLayer3x3Kernel::run_optimized(const Window &window, const ThreadInfo &info) |
| 331 | { |
| 332 | ARM_COMPUTE_UNUSED(info); |
| 333 | ARM_COMPUTE_ERROR_ON(!_convolver); |
| 334 | |
| 335 | const size_t start = window.x().start(); |
| 336 | const size_t end = window.x().end(); |
| 337 | _convolver->run(start, end); |
| 338 | } |
| 339 | |
Georgios Pinitas | be0ae93 | 2018-03-13 13:08:12 +0000 | [diff] [blame] | 340 | std::unique_ptr<depthwise::IDepthwiseConvolution> NEDepthwiseConvolutionLayer3x3Kernel::create_convolver_object(PadStrideInfo conv_info, |
| 341 | const ITensor *w, |
| 342 | const ITensor *in, |
| 343 | ITensor *out, |
| 344 | bool setup_strides) |
Georgios Pinitas | 4074c99 | 2018-01-30 18:13:46 +0000 | [diff] [blame] | 345 | { |
Georgios Pinitas | be0ae93 | 2018-03-13 13:08:12 +0000 | [diff] [blame] | 346 | const TensorShape shape = in->info()->tensor_shape(); |
| 347 | const int in_rows = shape.z(); |
| 348 | const int in_cols = shape.y(); |
| 349 | const int n_batches = shape[3]; |
| 350 | const int n_channels = shape.x(); |
| 351 | const bool padding_same = conv_info.has_padding(); |
| 352 | const int weight_col_stride = (setup_strides) ? w->info()->strides_in_bytes().y() / w->info()->element_size() : 0; |
| 353 | const int weight_row_stride = (setup_strides) ? w->info()->strides_in_bytes().z() / w->info()->element_size() : 0; |
| 354 | const int input_col_stride = (setup_strides) ? in->info()->strides_in_bytes().y() / in->info()->element_size() : 0; |
| 355 | const int input_row_stride = (setup_strides) ? in->info()->strides_in_bytes().z() / in->info()->element_size() : 0; |
| 356 | const int input_batch_stride = (setup_strides) ? in->info()->strides_in_bytes()[3] / in->info()->element_size() : 0; |
| 357 | const int output_col_stride = (setup_strides) ? out->info()->strides_in_bytes().y() / out->info()->element_size() : 0; |
| 358 | const int output_row_stride = (setup_strides) ? out->info()->strides_in_bytes().z() / out->info()->element_size() : 0; |
| 359 | const int output_batch_stride = (setup_strides) ? out->info()->strides_in_bytes()[3] / out->info()->element_size() : 0; |
Georgios Pinitas | 4074c99 | 2018-01-30 18:13:46 +0000 | [diff] [blame] | 360 | |
| 361 | const auto stride_x = conv_info.stride().first; |
| 362 | switch(stride_x) |
| 363 | { |
| 364 | case 1: |
Georgios Pinitas | be0ae93 | 2018-03-13 13:08:12 +0000 | [diff] [blame] | 365 | return arm_compute::support::cpp14::make_unique<DepthwiseConvolution<4, 4, 3, 3, 1, 1, float, float>>( |
Georgios Pinitas | 4074c99 | 2018-01-30 18:13:46 +0000 | [diff] [blame] | 366 | n_batches, |
| 367 | in_rows, |
| 368 | in_cols, |
| 369 | n_channels, |
| 370 | padding_same, |
Georgios Pinitas | be0ae93 | 2018-03-13 13:08:12 +0000 | [diff] [blame] | 371 | reinterpret_cast<const float *>(w->ptr_to_element(Coordinates())), |
| 372 | reinterpret_cast<float *>(in->ptr_to_element(Coordinates())), |
| 373 | reinterpret_cast<float *>(out->ptr_to_element(Coordinates())), |
| 374 | weight_col_stride, weight_row_stride, |
| 375 | input_col_stride, input_row_stride, input_batch_stride, |
| 376 | output_col_stride, output_row_stride, output_batch_stride); |
Georgios Pinitas | 4074c99 | 2018-01-30 18:13:46 +0000 | [diff] [blame] | 377 | case 2: |
Georgios Pinitas | be0ae93 | 2018-03-13 13:08:12 +0000 | [diff] [blame] | 378 | return arm_compute::support::cpp14::make_unique<DepthwiseConvolution<3, 3, 3, 3, 2, 2, float, float>>( |
Georgios Pinitas | 4074c99 | 2018-01-30 18:13:46 +0000 | [diff] [blame] | 379 | n_batches, |
| 380 | in_rows, |
| 381 | in_cols, |
| 382 | n_channels, |
| 383 | padding_same, |
Georgios Pinitas | be0ae93 | 2018-03-13 13:08:12 +0000 | [diff] [blame] | 384 | reinterpret_cast<const float *>(w->ptr_to_element(Coordinates())), |
| 385 | reinterpret_cast<float *>(in->ptr_to_element(Coordinates())), |
| 386 | reinterpret_cast<float *>(out->ptr_to_element(Coordinates())), |
| 387 | weight_col_stride, weight_row_stride, |
| 388 | input_col_stride, input_row_stride, input_batch_stride, |
| 389 | output_col_stride, output_row_stride, output_batch_stride); |
Georgios Pinitas | 4074c99 | 2018-01-30 18:13:46 +0000 | [diff] [blame] | 390 | default: |
| 391 | return nullptr; |
| 392 | } |
| 393 | } |