Giorgio Arena | 44f5572 | 2019-07-12 14:49:49 +0100 | [diff] [blame] | 1 | /* |
SiCongLi | b88272e | 2021-02-24 15:40:57 +0000 | [diff] [blame] | 2 | * Copyright (c) 2019-2021 Arm Limited. |
Giorgio Arena | 44f5572 | 2019-07-12 14:49:49 +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 | */ |
Manuel Bottini | b4bb6a0 | 2021-05-24 16:01:32 +0100 | [diff] [blame] | 24 | #include "src/core/cpu/kernels/CpuDepthwiseConv2dNativeKernel.h" |
Giorgio Arena | 44f5572 | 2019-07-12 14:49:49 +0100 | [diff] [blame] | 25 | |
Michalis Spyrou | 60c3b0e | 2021-04-08 12:02:58 +0100 | [diff] [blame] | 26 | #include "arm_compute/core/ITensor.h" |
| 27 | #include "arm_compute/core/ITensorInfo.h" |
Giorgio Arena | 44f5572 | 2019-07-12 14:49:49 +0100 | [diff] [blame] | 28 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 29 | #include "arm_compute/core/utils/quantization/AsymmHelpers.h" |
Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 30 | #include "src/core/CPP/Validate.h" |
Georgios Pinitas | ddb93bb | 2020-10-02 16:38:59 +0100 | [diff] [blame] | 31 | #include "src/core/NEON/wrapper/traits.h" |
| 32 | #include "src/core/NEON/wrapper/wrapper.h" |
Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 33 | #include "src/core/helpers/AutoConfiguration.h" |
| 34 | #include "src/core/helpers/WindowHelpers.h" |
Matthew Bentham | 758b5ba | 2020-03-05 23:37:48 +0000 | [diff] [blame] | 35 | #include "support/ToolchainSupport.h" |
Georgios Pinitas | 1c29ffc | 2019-08-01 15:03:00 +0100 | [diff] [blame] | 36 | |
Giorgio Arena | 44f5572 | 2019-07-12 14:49:49 +0100 | [diff] [blame] | 37 | namespace arm_compute |
| 38 | { |
Michalis Spyrou | 60c3b0e | 2021-04-08 12:02:58 +0100 | [diff] [blame] | 39 | namespace cpu |
| 40 | { |
| 41 | namespace kernels |
| 42 | { |
Giorgio Arena | 44f5572 | 2019-07-12 14:49:49 +0100 | [diff] [blame] | 43 | namespace |
| 44 | { |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 45 | constexpr auto data_layout = DataLayout::NHWC; |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 46 | const size_t width_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH); |
| 47 | const size_t height_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT); |
| 48 | const size_t channel_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL); |
| 49 | |
| 50 | constexpr auto dim_manual_loop = Window::Dimension(0, 0, 0); |
| 51 | constexpr auto dim_single_unit_step = Window::Dimension(0, 1, 1); |
| 52 | constexpr size_t vector_size = 8; |
| 53 | |
| 54 | struct DepthwiseConvolutionRunInfo |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 55 | { |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 56 | const size_t num_read_elements_per_iteration; |
| 57 | const uint32_t x_start; |
| 58 | const uint32_t x_end; |
| 59 | const uint32_t x_step; |
| 60 | const uint32_t x_leftover_start; |
| 61 | const size_t input_stride_y; |
| 62 | const size_t input_stride_z; |
| 63 | const size_t input_max_offset; |
| 64 | const size_t weights_width; |
| 65 | const size_t weights_height; |
| 66 | const size_t weights_stride_y; |
| 67 | const size_t weights_stride_z; |
| 68 | const size_t conv_stride_x; |
| 69 | const size_t conv_stride_y; |
| 70 | const size_t conv_pad_left; |
| 71 | const size_t conv_pad_top; |
| 72 | const size_t input_height; |
| 73 | const size_t input_width; |
| 74 | const size_t input_depth; |
| 75 | |
Manuel Bottini | b4bb6a0 | 2021-05-24 16:01:32 +0100 | [diff] [blame] | 76 | DepthwiseConvolutionRunInfo(const ITensorInfo &input, const ITensorInfo &weights, const PadStrideInfo &conv_info, const Window &w, uint32_t depth_multiplier = 1) // NOLINT |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 77 | : num_read_elements_per_iteration((depth_multiplier == 1 ? (vector_size / element_size_from_data_type(input.data_type())) : 1)), |
| 78 | x_start(w.x().start()), |
| 79 | x_end(w.x().end()), |
| 80 | x_step(static_cast<uint32_t>(num_read_elements_per_iteration * depth_multiplier)), |
| 81 | x_leftover_start(std::max(static_cast<int32_t>(w.x().end()) - static_cast<int32_t>(x_step) + 1, int32_t(0))), |
| 82 | input_stride_y(input.strides_in_bytes().y()), |
| 83 | input_stride_z(input.strides_in_bytes().z()), |
| 84 | input_max_offset(input.strides_in_bytes().z() * input.dimension(height_idx) - (input.padding().bottom + input.padding().top) * input.strides_in_bytes().y()), |
| 85 | weights_width(weights.dimension(width_idx)), |
| 86 | weights_height(weights.dimension(height_idx)), |
| 87 | weights_stride_y(weights.strides_in_bytes().y()), |
| 88 | weights_stride_z(weights.strides_in_bytes().z()), |
| 89 | conv_stride_x(conv_info.stride().first), |
| 90 | conv_stride_y(conv_info.stride().second), |
| 91 | conv_pad_left(conv_info.pad_left()), |
| 92 | conv_pad_top(conv_info.pad_top()), |
| 93 | input_height(input.dimension(height_idx)), |
| 94 | input_width(input.dimension(width_idx)), |
| 95 | input_depth(input.dimension(channel_idx)) |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 96 | { |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 97 | } |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 98 | }; |
| 99 | |
Michele Di Giorgio | d02d5ed | 2021-01-22 09:47:04 +0000 | [diff] [blame] | 100 | inline int32x4_t saturating_doubling_high_mul(const int32x4_t &a, const int32_t &b) |
| 101 | { |
| 102 | return vqrdmulhq_n_s32(a, b); |
| 103 | } |
| 104 | |
| 105 | inline int32_t saturating_doubling_high_mul(const int32_t &a, const int32_t &b) |
| 106 | { |
| 107 | return vget_lane_s32(vqrdmulh_n_s32(vdup_n_s32(a), b), 0); |
| 108 | } |
| 109 | |
| 110 | inline int32x4_t rounding_divide_by_exp2(const int32x4_t &x, const int exponent) |
| 111 | { |
| 112 | const int32x4_t shift = vdupq_n_s32(-exponent); |
| 113 | const int32x4_t fixup = vshrq_n_s32(vandq_s32(x, shift), 31); |
| 114 | const int32x4_t fixed = vqaddq_s32(x, fixup); |
| 115 | return vrshlq_s32(fixed, shift); |
| 116 | } |
| 117 | |
| 118 | inline int32x2_t rounding_divide_by_exp2(const int32x2_t &x, const int exponent) |
| 119 | { |
| 120 | const int32x2_t shift = vdup_n_s32(-exponent); |
| 121 | const int32x2_t fixup = vshr_n_s32(vand_s32(x, shift), 31); |
| 122 | const int32x2_t fixed = vqadd_s32(x, fixup); |
| 123 | return vrshl_s32(fixed, shift); |
| 124 | } |
| 125 | |
| 126 | inline int32_t rounding_divide_by_exp2(const int32_t &x, const int exponent) |
| 127 | { |
| 128 | const int32x2_t xs = vdup_n_s32(x); |
| 129 | return vget_lane_s32(rounding_divide_by_exp2(xs, exponent), 0); |
| 130 | } |
| 131 | |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 132 | inline bool is_valid_input_region(int32_t base_w, uint32_t base_h, uint32_t w, uint32_t h, const DepthwiseConvolutionRunInfo &run_info, const Size2D &dilation) |
| 133 | { |
| 134 | const int32_t current_h = base_h + h * dilation.y(); |
| 135 | const bool is_valid_h = current_h >= 0 && current_h < static_cast<int32_t>(run_info.input_height); |
| 136 | |
| 137 | const int32_t current_w = base_w + w * dilation.x(); |
| 138 | const bool is_valid_w = current_w >= 0 && current_w < static_cast<int32_t>(run_info.input_width); |
| 139 | |
| 140 | return is_valid_h && is_valid_w; |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 141 | } |
| 142 | |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 143 | template <typename T> |
Manuel Bottini | b4bb6a0 | 2021-05-24 16:01:32 +0100 | [diff] [blame] | 144 | void depthwise_loop_multiplier1_fp(const ITensor *src, const ITensor *weights, const ITensor *biases, ITensor *dst, const PadStrideInfo &conv_info, |
Michalis Spyrou | f401c74 | 2020-05-12 16:18:33 +0100 | [diff] [blame] | 145 | const Size2D &dilation, const Window &window, bool has_biases) |
Giorgio Arena | 44f5572 | 2019-07-12 14:49:49 +0100 | [diff] [blame] | 146 | { |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 147 | constexpr auto element_per_vector = vector_size / sizeof(T); |
| 148 | using VectorType = typename wrapper::traits::neon_vector<T, element_per_vector>::type; |
| 149 | using TagType = typename wrapper::traits::neon_vector<T, element_per_vector>::tag_type; |
Giorgio Arena | 44f5572 | 2019-07-12 14:49:49 +0100 | [diff] [blame] | 150 | |
Manuel Bottini | b4bb6a0 | 2021-05-24 16:01:32 +0100 | [diff] [blame] | 151 | const auto run_info = DepthwiseConvolutionRunInfo(*src->info(), *weights->info(), conv_info, window); |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 152 | |
| 153 | const VectorType zero_vector = wrapper::vdup_n(static_cast<T>(0), TagType{}); |
| 154 | |
| 155 | Window execution_window = window; |
| 156 | execution_window.set(Window::DimX, dim_single_unit_step); |
Giorgio Arena | 44f5572 | 2019-07-12 14:49:49 +0100 | [diff] [blame] | 157 | |
| 158 | Window win_input = window; |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 159 | win_input.set(Window::DimX, dim_manual_loop); |
| 160 | win_input.set(Window::DimY, dim_manual_loop); |
| 161 | win_input.set(Window::DimZ, dim_manual_loop); |
Giorgio Arena | 44f5572 | 2019-07-12 14:49:49 +0100 | [diff] [blame] | 162 | |
| 163 | Window win_weights = win_input; |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 164 | win_weights.set(Window::DimW, dim_manual_loop); |
| 165 | |
| 166 | Window win_output = window; |
| 167 | win_output.set(Window::DimX, dim_manual_loop); |
Giorgio Arena | 44f5572 | 2019-07-12 14:49:49 +0100 | [diff] [blame] | 168 | |
Manuel Bottini | b4bb6a0 | 2021-05-24 16:01:32 +0100 | [diff] [blame] | 169 | Iterator input_it(src, win_input); |
Giorgio Arena | 44f5572 | 2019-07-12 14:49:49 +0100 | [diff] [blame] | 170 | Iterator weights_it(weights, win_weights); |
Manuel Bottini | b4bb6a0 | 2021-05-24 16:01:32 +0100 | [diff] [blame] | 171 | Iterator output_it(dst, win_output); |
Giorgio Arena | 44f5572 | 2019-07-12 14:49:49 +0100 | [diff] [blame] | 172 | Iterator biases_it{}; |
| 173 | |
| 174 | if(has_biases) |
| 175 | { |
| 176 | biases_it = Iterator(biases, win_weights); |
| 177 | } |
| 178 | |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 179 | execute_window_loop(execution_window, [&](const Coordinates & id) |
Giorgio Arena | 44f5572 | 2019-07-12 14:49:49 +0100 | [diff] [blame] | 180 | { |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 181 | const int32_t input_y = id.y() * run_info.conv_stride_x - run_info.conv_pad_left; |
| 182 | const int32_t input_z = id.z() * run_info.conv_stride_y - run_info.conv_pad_top; |
| 183 | const int64_t base_input_offset = input_y * run_info.input_stride_y + input_z * run_info.input_stride_z; |
Giorgio Arena | 44f5572 | 2019-07-12 14:49:49 +0100 | [diff] [blame] | 184 | |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 185 | auto const base_weights_ptr = weights_it.ptr(); |
| 186 | uint32_t x = run_info.x_start; |
Giorgio Arena | 44f5572 | 2019-07-12 14:49:49 +0100 | [diff] [blame] | 187 | |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 188 | for(; x < run_info.x_leftover_start; x += run_info.x_step) |
Giorgio Arena | 44f5572 | 2019-07-12 14:49:49 +0100 | [diff] [blame] | 189 | { |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 190 | VectorType acc = zero_vector; |
| 191 | auto weights_ptr = base_weights_ptr; |
| 192 | int64_t input_offset = base_input_offset; |
Giorgio Arena | 44f5572 | 2019-07-12 14:49:49 +0100 | [diff] [blame] | 193 | |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 194 | for(uint32_t h = 0; h < run_info.weights_height; ++h) |
| 195 | { |
| 196 | int64_t offs = input_offset + x * sizeof(T); |
| 197 | for(uint32_t w = 0; w < run_info.weights_width; ++w) |
| 198 | { |
| 199 | const bool is_valid_region = is_valid_input_region(input_y, input_z, w, h, run_info, dilation); |
| 200 | const auto input_vals = is_valid_region ? |
| 201 | wrapper::vload(reinterpret_cast<T *>(input_it.ptr() + std::min(static_cast<size_t>(offs), run_info.input_max_offset))) : |
| 202 | zero_vector; |
| 203 | const auto weights_vals = wrapper::vload(reinterpret_cast<T *>(weights_ptr + w * run_info.weights_stride_y) + x); |
| 204 | acc = wrapper::vmla(acc, weights_vals, input_vals); |
| 205 | |
| 206 | offs += dilation.x() * run_info.input_stride_y; |
| 207 | } |
| 208 | |
| 209 | weights_ptr += run_info.weights_stride_z; |
| 210 | input_offset += dilation.y() * run_info.input_stride_z; |
Giorgio Arena | 44f5572 | 2019-07-12 14:49:49 +0100 | [diff] [blame] | 211 | } |
| 212 | |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 213 | if(has_biases) |
| 214 | { |
| 215 | const auto biases_vals = wrapper::vload(reinterpret_cast<T *>(biases_it.ptr()) + x); |
| 216 | acc = wrapper::vadd(acc, biases_vals); |
| 217 | } |
| 218 | |
| 219 | wrapper::vstore(reinterpret_cast<T *>(output_it.ptr()) + x, acc); |
Giorgio Arena | 44f5572 | 2019-07-12 14:49:49 +0100 | [diff] [blame] | 220 | } |
| 221 | |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 222 | for(; x < run_info.x_end; ++x) |
Giorgio Arena | 44f5572 | 2019-07-12 14:49:49 +0100 | [diff] [blame] | 223 | { |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 224 | auto acc_scalar = T{ 0 }; |
| 225 | auto weights_ptr = base_weights_ptr; |
| 226 | int64_t input_offset = base_input_offset; |
Giorgio Arena | 44f5572 | 2019-07-12 14:49:49 +0100 | [diff] [blame] | 227 | |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 228 | for(size_t h = 0; h < run_info.weights_height; ++h) |
| 229 | { |
| 230 | int64_t offs = input_offset + x * sizeof(T); |
| 231 | for(size_t w = 0; w < run_info.weights_width; ++w) |
| 232 | { |
| 233 | const bool is_valid_region = is_valid_input_region(input_y, input_z, w, h, run_info, dilation); |
| 234 | const auto input_vals = is_valid_region ? *reinterpret_cast<T *>(input_it.ptr() + std::min(static_cast<size_t>(offs), run_info.input_max_offset)) : 0; |
| 235 | const auto weights_vals = *(reinterpret_cast<T *>(weights_ptr + w * run_info.weights_stride_y) + x); |
| 236 | |
| 237 | acc_scalar += (input_vals * weights_vals); |
| 238 | |
| 239 | offs += dilation.x() * run_info.input_stride_y; |
| 240 | } |
| 241 | |
| 242 | weights_ptr += run_info.weights_stride_z; |
| 243 | input_offset += dilation.y() * run_info.input_stride_z; |
| 244 | } |
| 245 | |
| 246 | if(has_biases) |
| 247 | { |
| 248 | const auto biases_vals = *(reinterpret_cast<T *>(biases_it.ptr()) + x); |
| 249 | acc_scalar += biases_vals; |
| 250 | } |
| 251 | *(reinterpret_cast<T *>(output_it.ptr()) + x) = acc_scalar; |
| 252 | } |
Giorgio Arena | 44f5572 | 2019-07-12 14:49:49 +0100 | [diff] [blame] | 253 | }, |
| 254 | input_it, weights_it, biases_it, output_it); |
| 255 | } |
| 256 | |
Michalis Spyrou | f401c74 | 2020-05-12 16:18:33 +0100 | [diff] [blame] | 257 | template <typename T> |
Manuel Bottini | b4bb6a0 | 2021-05-24 16:01:32 +0100 | [diff] [blame] | 258 | void depthwise_loop_generic_fp(const ITensor *src, const ITensor *weights, const ITensor *biases, ITensor *dst, const PadStrideInfo &conv_info, |
Michalis Spyrou | f401c74 | 2020-05-12 16:18:33 +0100 | [diff] [blame] | 259 | const Size2D &dilation, unsigned int depth_multiplier, const Window &window, bool has_biases) |
Giorgio Arena | 44f5572 | 2019-07-12 14:49:49 +0100 | [diff] [blame] | 260 | { |
Manuel Bottini | b4bb6a0 | 2021-05-24 16:01:32 +0100 | [diff] [blame] | 261 | const auto run_info = DepthwiseConvolutionRunInfo(*src->info(), *weights->info(), conv_info, window, depth_multiplier); |
Giorgio Arena | 44f5572 | 2019-07-12 14:49:49 +0100 | [diff] [blame] | 262 | |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 263 | Window execution_window = window; |
| 264 | execution_window.set(Window::DimX, Window::Dimension(0, run_info.input_depth, 1)); |
Giorgio Arena | 44f5572 | 2019-07-12 14:49:49 +0100 | [diff] [blame] | 265 | |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 266 | Window win_input = execution_window; |
| 267 | win_input.set(Window::DimX, Window::Dimension(0, run_info.input_depth, 1)); |
| 268 | win_input.set(Window::DimY, dim_manual_loop); |
| 269 | win_input.set(Window::DimZ, dim_manual_loop); |
Giorgio Arena | 44f5572 | 2019-07-12 14:49:49 +0100 | [diff] [blame] | 270 | |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 271 | Window win_weights = window; |
| 272 | win_weights.set_dimension_step(Window::DimX, run_info.x_step); |
| 273 | win_weights.set(Window::DimY, dim_manual_loop); |
| 274 | win_weights.set(Window::DimZ, dim_manual_loop); |
| 275 | win_weights.set(Window::DimW, dim_manual_loop); |
| 276 | |
| 277 | Window win_output = window; |
| 278 | win_output.set_dimension_step(Window::DimX, run_info.x_step); |
Giorgio Arena | 44f5572 | 2019-07-12 14:49:49 +0100 | [diff] [blame] | 279 | |
Manuel Bottini | b4bb6a0 | 2021-05-24 16:01:32 +0100 | [diff] [blame] | 280 | Iterator input_it(src, win_input); |
Giorgio Arena | 44f5572 | 2019-07-12 14:49:49 +0100 | [diff] [blame] | 281 | Iterator weights_it(weights, win_weights); |
Manuel Bottini | b4bb6a0 | 2021-05-24 16:01:32 +0100 | [diff] [blame] | 282 | Iterator output_it(dst, win_output); |
Giorgio Arena | 44f5572 | 2019-07-12 14:49:49 +0100 | [diff] [blame] | 283 | Iterator biases_it{}; |
| 284 | |
| 285 | if(has_biases) |
| 286 | { |
| 287 | biases_it = Iterator(biases, win_weights); |
| 288 | } |
| 289 | |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 290 | execute_window_loop(execution_window, [&](const Coordinates & id) |
Giorgio Arena | 44f5572 | 2019-07-12 14:49:49 +0100 | [diff] [blame] | 291 | { |
| 292 | std::vector<T> acc(depth_multiplier, static_cast<T>(0)); |
| 293 | |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 294 | const int input_y = id.y() * run_info.conv_stride_x - run_info.conv_pad_left; |
| 295 | const int input_z = id.z() * run_info.conv_stride_y - run_info.conv_pad_top; |
| 296 | int input_offset = input_y * run_info.input_stride_y + input_z * run_info.input_stride_z; |
Giorgio Arena | 44f5572 | 2019-07-12 14:49:49 +0100 | [diff] [blame] | 297 | |
| 298 | auto weights_ptr = weights_it.ptr(); |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 299 | for(size_t h = 0; h < run_info.weights_height; ++h) |
Giorgio Arena | 44f5572 | 2019-07-12 14:49:49 +0100 | [diff] [blame] | 300 | { |
| 301 | int offs = input_offset; |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 302 | for(size_t w = 0; w < run_info.weights_width; ++w) |
Giorgio Arena | 44f5572 | 2019-07-12 14:49:49 +0100 | [diff] [blame] | 303 | { |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 304 | const bool is_valid_region = is_valid_input_region(input_y, input_z, w, h, run_info, dilation); |
| 305 | const auto input_val = is_valid_region ? *(reinterpret_cast<T *>(input_it.ptr() + std::min(static_cast<size_t>(offs), run_info.input_max_offset))) : T(0); |
Giorgio Arena | 44f5572 | 2019-07-12 14:49:49 +0100 | [diff] [blame] | 306 | |
| 307 | for(size_t m = 0; m < depth_multiplier; ++m) |
| 308 | { |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 309 | const auto weights_val = *(reinterpret_cast<T *>(weights_ptr + m * sizeof(T) + w * run_info.weights_stride_y)); |
Georgios Pinitas | 1c29ffc | 2019-08-01 15:03:00 +0100 | [diff] [blame] | 310 | acc.at(m) = support::cpp11::fma(weights_val, input_val, acc.at(m)); |
Giorgio Arena | 44f5572 | 2019-07-12 14:49:49 +0100 | [diff] [blame] | 311 | } |
| 312 | |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 313 | offs += dilation.x() * run_info.input_stride_y; |
Giorgio Arena | 44f5572 | 2019-07-12 14:49:49 +0100 | [diff] [blame] | 314 | } |
| 315 | |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 316 | weights_ptr += run_info.weights_stride_z; |
| 317 | input_offset += dilation.y() * run_info.input_stride_z; |
Giorgio Arena | 44f5572 | 2019-07-12 14:49:49 +0100 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | if(has_biases) |
| 321 | { |
| 322 | for(size_t m = 0; m < depth_multiplier; ++m) |
| 323 | { |
| 324 | const auto biases_val = *(reinterpret_cast<T *>(biases_it.ptr() + m * sizeof(T))); |
| 325 | *(reinterpret_cast<T *>(output_it.ptr() + m * sizeof(T))) = acc.at(m) + biases_val; |
| 326 | } |
| 327 | } |
| 328 | else |
| 329 | { |
| 330 | for(size_t m = 0; m < depth_multiplier; ++m) |
| 331 | { |
| 332 | *(reinterpret_cast<T *>(output_it.ptr() + m * sizeof(T))) = acc.at(m); |
| 333 | } |
| 334 | } |
| 335 | }, |
| 336 | input_it, weights_it, biases_it, output_it); |
| 337 | } |
| 338 | |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 339 | template <typename T, typename TW> |
Manuel Bottini | b4bb6a0 | 2021-05-24 16:01:32 +0100 | [diff] [blame] | 340 | void depthwise_loop_multiplier1_quantized(const ITensor *src, const ITensor *weights, const ITensor *biases, ITensor *dst, const PadStrideInfo &conv_info, |
| 341 | const Size2D &dilation, std::vector<int> output_multiplier, std::vector<int> output_shift, const Window &window, bool has_biases) // NOLINT |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 342 | { |
Manuel Bottini | b4bb6a0 | 2021-05-24 16:01:32 +0100 | [diff] [blame] | 343 | ARM_COMPUTE_UNUSED(output_multiplier, output_shift); |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 344 | constexpr auto element_per_vector = vector_size / sizeof(T); |
| 345 | using VectorType = typename wrapper::traits::neon_vector<T, element_per_vector>::type; |
| 346 | using TagType = typename wrapper::traits::neon_vector<T, element_per_vector>::tag_type; |
| 347 | using AccType = int32_t; |
| 348 | using AccArrayType = std::array<AccType, element_per_vector>; |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 349 | |
Manuel Bottini | b4bb6a0 | 2021-05-24 16:01:32 +0100 | [diff] [blame] | 350 | const auto out_of_bound_value = PixelValue(static_cast<uint64_t>(0), src->info()->data_type(), src->info()->quantization_info()).get<T>(); |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 351 | const auto out_of_bound_vector = wrapper::vdup_n(static_cast<T>(out_of_bound_value), TagType{}); |
| 352 | |
Manuel Bottini | b4bb6a0 | 2021-05-24 16:01:32 +0100 | [diff] [blame] | 353 | const auto run_info = DepthwiseConvolutionRunInfo(*src->info(), *weights->info(), conv_info, window); |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 354 | |
Manuel Bottini | b4bb6a0 | 2021-05-24 16:01:32 +0100 | [diff] [blame] | 355 | const int32_t input_qoffset = src->info()->quantization_info().uniform().offset; |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 356 | const int32_t weights_qoffset = weights->info()->quantization_info().uniform().offset; |
Manuel Bottini | b4bb6a0 | 2021-05-24 16:01:32 +0100 | [diff] [blame] | 357 | const int32_t output_qoffset = dst->info()->quantization_info().uniform().offset; |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 358 | const int32_t k_offset = run_info.weights_width * run_info.weights_height * input_qoffset * weights_qoffset; |
| 359 | |
| 360 | Window execution_window = window; |
| 361 | execution_window.set(Window::DimX, dim_single_unit_step); |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 362 | |
| 363 | Window win_input = window; |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 364 | win_input.set(Window::DimX, dim_manual_loop); |
| 365 | win_input.set(Window::DimY, dim_manual_loop); |
| 366 | win_input.set(Window::DimZ, dim_manual_loop); |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 367 | |
| 368 | Window win_weights = win_input; |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 369 | win_weights.set(Window::DimW, dim_manual_loop); |
| 370 | |
| 371 | Window win_output = window; |
| 372 | win_output.set(Window::DimX, dim_manual_loop); |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 373 | |
Manuel Bottini | b4bb6a0 | 2021-05-24 16:01:32 +0100 | [diff] [blame] | 374 | Iterator input_it(src, win_input); |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 375 | Iterator weights_it(weights, win_weights); |
Manuel Bottini | b4bb6a0 | 2021-05-24 16:01:32 +0100 | [diff] [blame] | 376 | Iterator output_it(dst, win_output); |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 377 | Iterator biases_it{}; |
| 378 | |
| 379 | if(has_biases) |
| 380 | { |
| 381 | biases_it = Iterator(biases, win_weights); |
| 382 | } |
| 383 | |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 384 | execute_window_loop(execution_window, [&](const Coordinates & id) |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 385 | { |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 386 | const int32_t input_y = id.y() * run_info.conv_stride_x - run_info.conv_pad_left; |
| 387 | const int32_t input_z = id.z() * run_info.conv_stride_y - run_info.conv_pad_top; |
| 388 | const int64_t base_input_offset = input_y * run_info.input_stride_y + input_z * run_info.input_stride_z; |
| 389 | auto const base_weights_ptr = weights_it.ptr(); |
| 390 | size_t x = run_info.x_start; |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 391 | |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 392 | for(; x < run_info.x_leftover_start; x += run_info.x_step) |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 393 | { |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 394 | AccArrayType acc{}; |
| 395 | AccArrayType in_sum{}; |
| 396 | AccArrayType we_sum{}; |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 397 | |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 398 | auto weights_ptr = base_weights_ptr; |
| 399 | auto input_offset = base_input_offset; |
| 400 | |
| 401 | for(size_t h = 0; h < run_info.weights_height; ++h) |
| 402 | { |
| 403 | int64_t offs = input_offset + x * sizeof(T); |
| 404 | for(size_t w = 0; w < run_info.weights_width; ++w) |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 405 | { |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 406 | const bool is_valid_region = is_valid_input_region(input_y, input_z, w, h, run_info, dilation); |
| 407 | const auto input_vals = is_valid_region ? |
| 408 | wrapper::vload(reinterpret_cast<T *>(input_it.ptr() + std::min(static_cast<size_t>(offs), run_info.input_max_offset))) : |
| 409 | out_of_bound_vector; |
| 410 | const auto weights_vals = wrapper::vload(reinterpret_cast<TW *>(weights_ptr + w * run_info.weights_stride_y) + x); |
| 411 | |
Sang-Hoon Park | 1a0a4bc | 2020-11-12 17:41:32 +0000 | [diff] [blame] | 412 | for(size_t i = 0; i < element_per_vector; ++i) |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 413 | { |
| 414 | acc.at(i) += input_vals[i] * weights_vals[i]; |
| 415 | in_sum.at(i) += input_vals[i]; |
| 416 | we_sum.at(i) += weights_vals[i]; |
| 417 | } |
| 418 | |
| 419 | offs += dilation.x() * run_info.input_stride_y; |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 420 | } |
| 421 | |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 422 | weights_ptr += run_info.weights_stride_z; |
| 423 | input_offset += dilation.y() * run_info.input_stride_z; |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 424 | } |
| 425 | |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 426 | VectorType out_vals = wrapper::vdup_n(static_cast<T>(0), TagType{}); |
Sang-Hoon Park | 1a0a4bc | 2020-11-12 17:41:32 +0000 | [diff] [blame] | 427 | for(size_t i = 0; i < element_per_vector; ++i) |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 428 | { |
| 429 | acc.at(i) -= in_sum.at(i) * weights_qoffset; |
| 430 | acc.at(i) -= we_sum.at(i) * input_qoffset; |
| 431 | acc.at(i) += k_offset; |
| 432 | |
| 433 | if(has_biases) |
| 434 | { |
| 435 | acc.at(i) += *(reinterpret_cast<int32_t *>(biases_it.ptr() + i * sizeof(int32_t)) + x); |
| 436 | } |
| 437 | |
| 438 | const int32_t out_mul = output_multiplier.at(x + i); |
| 439 | const int32_t out_shift = output_shift.at(x + i); |
| 440 | if(out_shift < 0) |
| 441 | { |
| 442 | acc.at(i) = saturating_doubling_high_mul(acc.at(i) * (1 << (-out_shift)), out_mul) + output_qoffset; |
| 443 | } |
| 444 | else |
| 445 | { |
| 446 | acc.at(i) = rounding_divide_by_exp2(saturating_doubling_high_mul(acc.at(i), out_mul), out_shift) + output_qoffset; |
| 447 | } |
| 448 | out_vals[i] = static_cast<T>(utility::clamp<AccType, T>(acc.at(i))); |
| 449 | } |
| 450 | |
| 451 | wrapper::vstore(reinterpret_cast<T *>(output_it.ptr()) + x, out_vals); |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 452 | } |
| 453 | |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 454 | // left-over |
| 455 | for(; x < run_info.x_end; ++x) |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 456 | { |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 457 | AccType acc = 0; |
| 458 | AccType in_sum = 0; |
| 459 | AccType we_sum = 0; |
| 460 | |
| 461 | auto weights_ptr = base_weights_ptr; |
| 462 | auto input_offset = base_input_offset; |
| 463 | |
| 464 | for(size_t h = 0; h < run_info.weights_height; ++h) |
| 465 | { |
| 466 | int64_t offs = input_offset + x * sizeof(T); |
| 467 | for(size_t w = 0; w < run_info.weights_width; ++w) |
| 468 | { |
| 469 | const bool is_valid_region = is_valid_input_region(input_y, input_z, w, h, run_info, dilation); |
| 470 | const auto input_val = is_valid_region ? |
| 471 | *reinterpret_cast<T *>(input_it.ptr() + std::min(static_cast<size_t>(offs), run_info.input_max_offset)) : |
| 472 | out_of_bound_value; |
| 473 | const auto weights_val = *(reinterpret_cast<TW *>(weights_ptr + w * run_info.weights_stride_y) + x); |
| 474 | |
| 475 | acc += input_val * weights_val; |
| 476 | in_sum += input_val; |
| 477 | we_sum += weights_val; |
| 478 | |
| 479 | offs += dilation.x() * run_info.input_stride_y; |
| 480 | } |
| 481 | |
| 482 | weights_ptr += run_info.weights_stride_z; |
| 483 | input_offset += dilation.y() * run_info.input_stride_z; |
| 484 | } |
| 485 | |
| 486 | T out_vals{ 0 }; |
| 487 | |
| 488 | acc -= in_sum * weights_qoffset; |
| 489 | acc -= we_sum * input_qoffset; |
| 490 | acc += k_offset; |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 491 | |
| 492 | if(has_biases) |
| 493 | { |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 494 | acc += *(reinterpret_cast<int32_t *>(biases_it.ptr()) + x); |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 495 | } |
| 496 | |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 497 | const int32_t out_mul = output_multiplier.at(x); |
| 498 | const int32_t out_shift = output_shift.at(x); |
| 499 | |
Michele Di Giorgio | f29d1b7 | 2019-10-29 10:58:13 +0000 | [diff] [blame] | 500 | if(out_shift < 0) |
| 501 | { |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 502 | acc = saturating_doubling_high_mul(acc * (1 << (-out_shift)), out_mul) + output_qoffset; |
Michele Di Giorgio | f29d1b7 | 2019-10-29 10:58:13 +0000 | [diff] [blame] | 503 | } |
| 504 | else |
| 505 | { |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 506 | acc = rounding_divide_by_exp2(saturating_doubling_high_mul(acc, out_mul), out_shift) + output_qoffset; |
Michele Di Giorgio | f29d1b7 | 2019-10-29 10:58:13 +0000 | [diff] [blame] | 507 | } |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 508 | |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 509 | out_vals = static_cast<T>(utility::clamp<AccType, T>(acc)); |
| 510 | *(reinterpret_cast<T *>(output_it.ptr()) + x) = out_vals; |
| 511 | } |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 512 | }, |
| 513 | input_it, weights_it, biases_it, output_it); |
| 514 | } |
| 515 | |
Michalis Spyrou | f401c74 | 2020-05-12 16:18:33 +0100 | [diff] [blame] | 516 | template <typename T, typename TW> |
Manuel Bottini | b4bb6a0 | 2021-05-24 16:01:32 +0100 | [diff] [blame] | 517 | void depthwise_loop_generic_quantized(const ITensor *src, const ITensor *weights, const ITensor *biases, ITensor *dst, const PadStrideInfo &conv_info, |
| 518 | const Size2D &dilation, unsigned int depth_multiplier, std::vector<int> output_multiplier, std::vector<int> output_shift, const Window &window, bool has_biases) // NOLINT |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 519 | { |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 520 | using AccType = int32_t; |
| 521 | |
Manuel Bottini | b4bb6a0 | 2021-05-24 16:01:32 +0100 | [diff] [blame] | 522 | const auto run_info = DepthwiseConvolutionRunInfo(*src->info(), *weights->info(), conv_info, window, depth_multiplier); |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 523 | |
Manuel Bottini | b4bb6a0 | 2021-05-24 16:01:32 +0100 | [diff] [blame] | 524 | const auto out_of_bound_value = PixelValue(static_cast<uint64_t>(0), src->info()->data_type(), src->info()->quantization_info()).get<T>(); |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 525 | |
Manuel Bottini | b4bb6a0 | 2021-05-24 16:01:32 +0100 | [diff] [blame] | 526 | const int32_t input_qoffset = src->info()->quantization_info().uniform().offset; |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 527 | const int32_t weights_qoffset = weights->info()->quantization_info().uniform().offset; |
Manuel Bottini | b4bb6a0 | 2021-05-24 16:01:32 +0100 | [diff] [blame] | 528 | const int32_t output_qoffset = dst->info()->quantization_info().uniform().offset; |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 529 | const int32_t k_offset = run_info.weights_width * run_info.weights_height * input_qoffset * weights_qoffset; |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 530 | |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 531 | Window execution_window = window; |
| 532 | execution_window.set(Window::DimX, Window::Dimension(0, run_info.input_depth, 1)); |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 533 | |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 534 | Window win_input = execution_window; |
| 535 | win_input.set(Window::DimY, dim_manual_loop); |
| 536 | win_input.set(Window::DimZ, dim_manual_loop); |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 537 | |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 538 | Window win_weights = window; |
| 539 | win_weights.set_dimension_step(Window::DimX, run_info.x_step); |
| 540 | win_weights.set(Window::DimY, dim_manual_loop); |
| 541 | win_weights.set(Window::DimZ, dim_manual_loop); |
| 542 | win_weights.set(Window::DimW, dim_manual_loop); |
| 543 | |
| 544 | Window win_output = window; |
| 545 | win_output.set_dimension_step(Window::DimX, run_info.x_step); |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 546 | |
Manuel Bottini | b4bb6a0 | 2021-05-24 16:01:32 +0100 | [diff] [blame] | 547 | Iterator input_it(src, win_input); |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 548 | Iterator weights_it(weights, win_weights); |
Manuel Bottini | b4bb6a0 | 2021-05-24 16:01:32 +0100 | [diff] [blame] | 549 | Iterator output_it(dst, win_output); |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 550 | Iterator biases_it{}; |
| 551 | |
| 552 | if(has_biases) |
| 553 | { |
| 554 | biases_it = Iterator(biases, win_weights); |
| 555 | } |
| 556 | |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 557 | execute_window_loop(execution_window, [&](const Coordinates & id) |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 558 | { |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 559 | std::vector<AccType> acc(depth_multiplier, 0); |
| 560 | std::vector<AccType> we_sum(depth_multiplier, 0); |
| 561 | AccType in_sum = 0; |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 562 | |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 563 | const int32_t input_y = id.y() * run_info.conv_stride_x - run_info.conv_pad_left; |
| 564 | const int32_t input_z = id.z() * run_info.conv_stride_y - run_info.conv_pad_top; |
| 565 | int64_t input_offset = input_y * run_info.input_stride_y + input_z * run_info.input_stride_z; |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 566 | |
| 567 | auto weights_ptr = weights_it.ptr(); |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 568 | for(size_t h = 0; h < run_info.weights_height; ++h) |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 569 | { |
| 570 | int offs = input_offset; |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 571 | for(size_t w = 0; w < run_info.weights_width; ++w) |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 572 | { |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 573 | const bool is_valid_region = is_valid_input_region(input_y, input_z, w, h, run_info, dilation); |
| 574 | const auto input_val = is_valid_region ? *(reinterpret_cast<T *>(input_it.ptr() + std::min(static_cast<size_t>(offs), run_info.input_max_offset))) : out_of_bound_value; |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 575 | |
| 576 | for(size_t m = 0; m < depth_multiplier; ++m) |
| 577 | { |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 578 | const auto weights_val = *(reinterpret_cast<TW *>(weights_ptr + m * sizeof(T) + w * run_info.weights_stride_y)); |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 579 | acc.at(m) += input_val * weights_val; |
| 580 | |
| 581 | we_sum.at(m) += weights_val; |
| 582 | } |
| 583 | |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 584 | offs += dilation.x() * run_info.input_stride_y; |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 585 | in_sum += input_val; |
| 586 | } |
| 587 | |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 588 | weights_ptr += run_info.weights_stride_z; |
| 589 | input_offset += dilation.y() * run_info.input_stride_z; |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 590 | } |
| 591 | |
| 592 | for(size_t m = 0; m < depth_multiplier; ++m) |
| 593 | { |
| 594 | acc.at(m) -= in_sum * weights_qoffset; |
| 595 | acc.at(m) -= we_sum.at(m) * input_qoffset; |
| 596 | acc.at(m) += k_offset; |
| 597 | |
| 598 | if(has_biases) |
| 599 | { |
Michele Di Giorgio | f29d1b7 | 2019-10-29 10:58:13 +0000 | [diff] [blame] | 600 | acc.at(m) += *(reinterpret_cast<int32_t *>(biases_it.ptr() + m * sizeof(int32_t))); |
| 601 | } |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 602 | |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 603 | const int32_t out_mul = output_multiplier.at(id.x() * depth_multiplier + m); |
| 604 | const int32_t out_shift = output_shift.at(id.x() * depth_multiplier + m); |
Michele Di Giorgio | f29d1b7 | 2019-10-29 10:58:13 +0000 | [diff] [blame] | 605 | if(out_shift < 0) |
| 606 | { |
| 607 | acc.at(m) = saturating_doubling_high_mul(acc.at(m) * (1 << (-out_shift)), out_mul) + output_qoffset; |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 608 | } |
| 609 | else |
| 610 | { |
Michele Di Giorgio | f29d1b7 | 2019-10-29 10:58:13 +0000 | [diff] [blame] | 611 | acc.at(m) = rounding_divide_by_exp2(saturating_doubling_high_mul(acc.at(m), out_mul), out_shift) + output_qoffset; |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 612 | } |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 613 | *(reinterpret_cast<T *>(output_it.ptr() + m * sizeof(T))) = static_cast<T>(utility::clamp<AccType, T>(acc.at(m))); |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 614 | } |
| 615 | }, |
| 616 | input_it, weights_it, biases_it, output_it); |
| 617 | } |
| 618 | |
Giorgio Arena | 3737c79 | 2020-11-23 17:47:23 +0000 | [diff] [blame] | 619 | template <typename T, typename TW> |
Manuel Bottini | b4bb6a0 | 2021-05-24 16:01:32 +0100 | [diff] [blame] | 620 | void depthwise_loop_pow2_quantized_per_tensor(const ITensor *src, const ITensor *weights, const ITensor *biases, ITensor *dst, const PadStrideInfo &conv_info, |
| 621 | const Size2D &dilation, unsigned int depth_multiplier, std::vector<int> output_multiplier, std::vector<int> output_shift, const Window &window, bool has_biases) // NOLINT |
Giorgio Arena | 3737c79 | 2020-11-23 17:47:23 +0000 | [diff] [blame] | 622 | { |
| 623 | constexpr int half_vec = vector_size / 2; |
| 624 | |
| 625 | using AccType = int32_t; |
| 626 | using AccVectorType = typename wrapper::traits::neon_vector<AccType, half_vec>::type; |
| 627 | using AccVectorTagType = typename wrapper::traits::neon_vector<AccType, half_vec>::tag_type; |
| 628 | using TagType = typename wrapper::traits::neon_vector<T, vector_size>::tag_type; |
| 629 | |
Manuel Bottini | b4bb6a0 | 2021-05-24 16:01:32 +0100 | [diff] [blame] | 630 | const auto run_info = DepthwiseConvolutionRunInfo(*src->info(), *weights->info(), conv_info, window, depth_multiplier); |
Giorgio Arena | 3737c79 | 2020-11-23 17:47:23 +0000 | [diff] [blame] | 631 | |
Manuel Bottini | b4bb6a0 | 2021-05-24 16:01:32 +0100 | [diff] [blame] | 632 | const auto input_qoffset_vec = wrapper::vreinterpret(wrapper::vmovl(wrapper::vdup_n(static_cast<T>(src->info()->quantization_info().uniform().offset), TagType{}))); |
Giorgio Arena | 3737c79 | 2020-11-23 17:47:23 +0000 | [diff] [blame] | 633 | const auto weights_qoffset_vec = wrapper::vreinterpret(wrapper::vmovl(wrapper::vdup_n(static_cast<TW>(weights->info()->quantization_info().uniform().offset), TagType{}))); |
Manuel Bottini | b4bb6a0 | 2021-05-24 16:01:32 +0100 | [diff] [blame] | 634 | const auto output_qoffset_vec = wrapper::vdup_n(dst->info()->quantization_info().uniform().offset, arm_compute::wrapper::traits::vector_128_tag{}); |
Giorgio Arena | 3737c79 | 2020-11-23 17:47:23 +0000 | [diff] [blame] | 635 | |
| 636 | const auto lower = wrapper::vdup_n(static_cast<AccType>(std::numeric_limits<T>::lowest()), AccVectorTagType{}); |
| 637 | const auto upper = wrapper::vdup_n(static_cast<AccType>(std::numeric_limits<T>::max()), AccVectorTagType{}); |
| 638 | const auto zero = wrapper::vdup_n(static_cast<AccType>(0), AccVectorTagType{}); |
| 639 | |
| 640 | const auto out_mul = output_multiplier.at(0); |
| 641 | const auto out_shift = output_shift.at(0); |
| 642 | |
| 643 | Window execution_window = window; |
| 644 | execution_window.set(Window::DimX, Window::Dimension(0, run_info.input_depth, 1)); |
| 645 | |
| 646 | Window win_input = execution_window; |
| 647 | win_input.set(Window::DimY, dim_manual_loop); |
| 648 | win_input.set(Window::DimZ, dim_manual_loop); |
| 649 | |
| 650 | Window win_weights = window; |
| 651 | win_weights.set_dimension_step(Window::DimX, run_info.x_step); |
| 652 | win_weights.set(Window::DimY, dim_manual_loop); |
| 653 | win_weights.set(Window::DimZ, dim_manual_loop); |
| 654 | win_weights.set(Window::DimW, dim_manual_loop); |
| 655 | |
| 656 | Window win_output = window; |
| 657 | win_output.set_dimension_step(Window::DimX, run_info.x_step); |
| 658 | |
Manuel Bottini | b4bb6a0 | 2021-05-24 16:01:32 +0100 | [diff] [blame] | 659 | Iterator input_it(src, win_input); |
Giorgio Arena | 3737c79 | 2020-11-23 17:47:23 +0000 | [diff] [blame] | 660 | Iterator weights_it(weights, win_weights); |
Manuel Bottini | b4bb6a0 | 2021-05-24 16:01:32 +0100 | [diff] [blame] | 661 | Iterator output_it(dst, win_output); |
Giorgio Arena | 3737c79 | 2020-11-23 17:47:23 +0000 | [diff] [blame] | 662 | Iterator biases_it{}; |
| 663 | |
| 664 | if(has_biases) |
| 665 | { |
| 666 | biases_it = Iterator(biases, win_weights); |
| 667 | } |
| 668 | |
| 669 | std::vector<AccVectorType> acc0(depth_multiplier / vector_size); |
| 670 | std::vector<AccVectorType> acc1(depth_multiplier / vector_size); |
| 671 | |
| 672 | execute_window_loop(execution_window, [&](const Coordinates & id) |
| 673 | { |
| 674 | std::fill(begin(acc0), end(acc0), zero); |
| 675 | std::fill(begin(acc1), end(acc1), zero); |
| 676 | |
| 677 | const int32_t input_y = id.y() * run_info.conv_stride_x - run_info.conv_pad_left; |
| 678 | const int32_t input_z = id.z() * run_info.conv_stride_y - run_info.conv_pad_top; |
| 679 | int64_t input_offset = input_y * run_info.input_stride_y + input_z * run_info.input_stride_z; |
| 680 | |
| 681 | auto weights_ptr = weights_it.ptr(); |
| 682 | for(size_t h = 0; h < run_info.weights_height; ++h) |
| 683 | { |
| 684 | const int32_t current_h = input_z + h * dilation.y(); |
| 685 | if(current_h >= 0 && current_h < static_cast<int32_t>(run_info.input_height)) |
| 686 | { |
| 687 | int offs = input_offset; |
| 688 | for(size_t w = 0; w < run_info.weights_width; ++w) |
| 689 | { |
| 690 | const int32_t current_w = input_y + w * dilation.x(); |
| 691 | if(current_w >= 0 && current_w < static_cast<int32_t>(run_info.input_width)) |
| 692 | { |
| 693 | const auto input_8x8 = wrapper::vdup_n(*(reinterpret_cast<T *>(input_it.ptr() + std::min(static_cast<size_t>(offs), run_info.input_max_offset))), TagType{}); |
| 694 | const auto input_s16x8 = wrapper::vreinterpret(wrapper::vmovl(input_8x8)); |
| 695 | const auto input_no_offs = wrapper::vsub(input_s16x8, input_qoffset_vec); |
| 696 | |
| 697 | for(size_t m = 0, i = 0; m < depth_multiplier; m += vector_size, ++i) |
| 698 | { |
| 699 | const auto weights_8x8 = wrapper::vload(reinterpret_cast<TW *>(weights_ptr + m * sizeof(T) + w * run_info.weights_stride_y)); |
| 700 | const auto weights_s16x8 = wrapper::vreinterpret(wrapper::vmovl(weights_8x8)); |
| 701 | const auto weights_no_offs = wrapper::vsub(weights_s16x8, weights_qoffset_vec); |
| 702 | |
| 703 | acc0.at(i) = wrapper::vmlal(acc0.at(i), wrapper::vgetlow(input_no_offs), wrapper::vgetlow(weights_no_offs)); |
| 704 | acc1.at(i) = wrapper::vmlal(acc1.at(i), wrapper::vgethigh(input_no_offs), wrapper::vgethigh(weights_no_offs)); |
| 705 | } |
| 706 | } |
| 707 | |
| 708 | offs += dilation.x() * run_info.input_stride_y; |
| 709 | } |
| 710 | } |
| 711 | |
| 712 | weights_ptr += run_info.weights_stride_z; |
| 713 | input_offset += dilation.y() * run_info.input_stride_z; |
| 714 | } |
| 715 | |
| 716 | for(size_t m = 0, i = 0; m < depth_multiplier; m += vector_size, ++i) |
| 717 | { |
| 718 | if(has_biases) |
| 719 | { |
| 720 | const auto bias_val0 = wrapper::vloadq(reinterpret_cast<int32_t *>(biases_it.ptr() + m * sizeof(int32_t))); |
| 721 | const auto bias_val1 = wrapper::vloadq(reinterpret_cast<int32_t *>(biases_it.ptr() + (m + half_vec) * sizeof(int32_t))); |
| 722 | |
| 723 | acc0.at(i) = wrapper::vadd(acc0.at(i), bias_val0); |
| 724 | acc1.at(i) = wrapper::vadd(acc1.at(i), bias_val1); |
| 725 | } |
| 726 | |
| 727 | if(out_shift < 0) |
| 728 | { |
| 729 | acc0.at(i) = wrapper::vadd(saturating_doubling_high_mul(acc0.at(i) * (1 << (-out_shift)), out_mul), output_qoffset_vec); |
| 730 | acc1.at(i) = wrapper::vadd(saturating_doubling_high_mul(acc1.at(i) * (1 << (-out_shift)), out_mul), output_qoffset_vec); |
| 731 | } |
| 732 | else |
| 733 | { |
| 734 | acc0.at(i) = wrapper::vadd(rounding_divide_by_exp2(saturating_doubling_high_mul(acc0.at(i), out_mul), out_shift), output_qoffset_vec); |
| 735 | acc1.at(i) = wrapper::vadd(rounding_divide_by_exp2(saturating_doubling_high_mul(acc1.at(i), out_mul), out_shift), output_qoffset_vec); |
| 736 | } |
| 737 | |
| 738 | acc0.at(i) = wrapper::vmin(wrapper::vmax(acc0.at(i), lower), upper); |
| 739 | acc1.at(i) = wrapper::vmin(wrapper::vmax(acc1.at(i), lower), upper); |
| 740 | |
| 741 | const auto out_val = wrapper::vcombine(wrapper::vmovn(acc0.at(i)), |
| 742 | wrapper::vmovn(acc1.at(i))); |
| 743 | |
| 744 | if(std::is_same<T, uint8_t>::value) |
| 745 | { |
| 746 | wrapper::vstore(reinterpret_cast<uint8_t *>(output_it.ptr() + m * sizeof(uint8_t)), wrapper::vqmovn(vreinterpretq_u16_s16(out_val))); |
| 747 | } |
| 748 | else |
| 749 | { |
| 750 | wrapper::vstore(reinterpret_cast<int8_t *>(output_it.ptr() + m * sizeof(int8_t)), wrapper::vqmovn(out_val)); |
| 751 | } |
| 752 | } |
| 753 | }, |
| 754 | input_it, weights_it, biases_it, output_it); |
| 755 | } |
| 756 | |
Manuel Bottini | b4bb6a0 | 2021-05-24 16:01:32 +0100 | [diff] [blame] | 757 | Status validate_arguments(const ITensorInfo *src, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *dst, const ConvolutionInfo &info) |
Giorgio Arena | 44f5572 | 2019-07-12 14:49:49 +0100 | [diff] [blame] | 758 | { |
Manuel Bottini | b4bb6a0 | 2021-05-24 16:01:32 +0100 | [diff] [blame] | 759 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src, weights, dst); |
| 760 | ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(src); |
| 761 | ARM_COMPUTE_RETURN_ERROR_ON(src->data_layout() == DataLayout::UNKNOWN); |
| 762 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED, DataType::F16, DataType::F32); |
Michalis Spyrou | 60c3b0e | 2021-04-08 12:02:58 +0100 | [diff] [blame] | 763 | ARM_COMPUTE_RETURN_ERROR_ON(info.depth_multiplier == 0); |
Manuel Bottini | b4bb6a0 | 2021-05-24 16:01:32 +0100 | [diff] [blame] | 764 | ARM_COMPUTE_RETURN_ERROR_ON(weights->dimension(1) + (weights->dimension(1) - 1) * (info.dilation.x() - 1) > src->dimension(1) + info.pad_stride_info.pad_left() + info.pad_stride_info.pad_right()); |
| 765 | ARM_COMPUTE_RETURN_ERROR_ON(weights->dimension(2) + (weights->dimension(2) - 1) * (info.dilation.y() - 1) > src->dimension(2) + info.pad_stride_info.pad_top() + info.pad_stride_info.pad_bottom()); |
| 766 | ARM_COMPUTE_RETURN_ERROR_ON((src->dimension(0) * info.depth_multiplier) != weights->dimension(0)); |
Michalis Spyrou | 60c3b0e | 2021-04-08 12:02:58 +0100 | [diff] [blame] | 767 | ARM_COMPUTE_RETURN_ERROR_ON((info.dilation.x() < 1) || (info.dilation.y() < 1)); |
| 768 | ARM_COMPUTE_RETURN_ERROR_ON((info.pad_stride_info.stride().first < 1) || (info.pad_stride_info.stride().second < 1)); |
Giorgio Arena | 44f5572 | 2019-07-12 14:49:49 +0100 | [diff] [blame] | 769 | |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 770 | if(is_data_type_quantized_per_channel(weights->data_type())) |
| 771 | { |
| 772 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(weights, 1, DataType::QSYMM8_PER_CHANNEL); |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 773 | ARM_COMPUTE_RETURN_ERROR_ON(weights->dimension(0) != weights->quantization_info().scale().size()); |
| 774 | } |
| 775 | else |
| 776 | { |
Manuel Bottini | b4bb6a0 | 2021-05-24 16:01:32 +0100 | [diff] [blame] | 777 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src, weights); |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 778 | } |
| 779 | |
Giorgio Arena | 44f5572 | 2019-07-12 14:49:49 +0100 | [diff] [blame] | 780 | if(biases != nullptr) |
| 781 | { |
| 782 | ARM_COMPUTE_RETURN_ERROR_ON(biases->num_dimensions() > 1); |
| 783 | ARM_COMPUTE_RETURN_ERROR_ON(biases->dimension(0) != weights->dimension(0)); |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 784 | |
Manuel Bottini | b4bb6a0 | 2021-05-24 16:01:32 +0100 | [diff] [blame] | 785 | if(is_data_type_quantized_asymmetric(src->data_type())) |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 786 | { |
| 787 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(biases, 1, DataType::S32); |
| 788 | } |
| 789 | else |
| 790 | { |
| 791 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(weights, biases); |
| 792 | } |
Giorgio Arena | 44f5572 | 2019-07-12 14:49:49 +0100 | [diff] [blame] | 793 | } |
| 794 | |
Manuel Bottini | b4bb6a0 | 2021-05-24 16:01:32 +0100 | [diff] [blame] | 795 | if(dst->total_size() != 0) |
Giorgio Arena | 44f5572 | 2019-07-12 14:49:49 +0100 | [diff] [blame] | 796 | { |
Manuel Bottini | b4bb6a0 | 2021-05-24 16:01:32 +0100 | [diff] [blame] | 797 | const TensorShape output_shape = misc::shape_calculator::compute_depthwise_convolution_shape(*src, *weights, info); |
| 798 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(dst->tensor_shape(), output_shape); |
| 799 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src, dst); |
Giorgio Arena | 44f5572 | 2019-07-12 14:49:49 +0100 | [diff] [blame] | 800 | } |
| 801 | |
| 802 | return Status{}; |
| 803 | } |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 804 | } // namespace |
Giorgio Arena | 44f5572 | 2019-07-12 14:49:49 +0100 | [diff] [blame] | 805 | |
Manuel Bottini | b4bb6a0 | 2021-05-24 16:01:32 +0100 | [diff] [blame] | 806 | CpuDepthwiseConv2dNativeKernel::CpuDepthwiseConv2dNativeKernel() |
Michalis Spyrou | 60c3b0e | 2021-04-08 12:02:58 +0100 | [diff] [blame] | 807 | : _func(), _conv_info(), _depth_multiplier(1), _dilation(), _output_multiplier(), _output_shift(), _has_biases() |
Giorgio Arena | 44f5572 | 2019-07-12 14:49:49 +0100 | [diff] [blame] | 808 | { |
| 809 | } |
| 810 | |
Manuel Bottini | b4bb6a0 | 2021-05-24 16:01:32 +0100 | [diff] [blame] | 811 | void CpuDepthwiseConv2dNativeKernel::configure(const ITensorInfo *src, const ITensorInfo *weights, const ITensorInfo *biases, ITensorInfo *dst, const ConvolutionInfo &info) |
Giorgio Arena | 44f5572 | 2019-07-12 14:49:49 +0100 | [diff] [blame] | 812 | { |
Manuel Bottini | b4bb6a0 | 2021-05-24 16:01:32 +0100 | [diff] [blame] | 813 | ARM_COMPUTE_ERROR_ON_NULLPTR(src, weights, dst); |
| 814 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(src, weights, (biases != nullptr) ? biases : nullptr, dst, info)); |
Giorgio Arena | 44f5572 | 2019-07-12 14:49:49 +0100 | [diff] [blame] | 815 | |
Michalis Spyrou | 60c3b0e | 2021-04-08 12:02:58 +0100 | [diff] [blame] | 816 | _conv_info = info.pad_stride_info; |
| 817 | _depth_multiplier = info.depth_multiplier; |
| 818 | _dilation = info.dilation; |
Michalis Spyrou | f401c74 | 2020-05-12 16:18:33 +0100 | [diff] [blame] | 819 | _has_biases = (biases != nullptr); |
Giorgio Arena | 44f5572 | 2019-07-12 14:49:49 +0100 | [diff] [blame] | 820 | |
Manuel Bottini | b4bb6a0 | 2021-05-24 16:01:32 +0100 | [diff] [blame] | 821 | if(is_data_type_quantized(src->data_type())) |
Giorgio Arena | 44f5572 | 2019-07-12 14:49:49 +0100 | [diff] [blame] | 822 | { |
Manuel Bottini | b4bb6a0 | 2021-05-24 16:01:32 +0100 | [diff] [blame] | 823 | const auto input_scale = src->quantization_info().uniform().scale; |
| 824 | const auto output_scale = dst->quantization_info().uniform().scale; |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 825 | |
Michalis Spyrou | 60c3b0e | 2021-04-08 12:02:58 +0100 | [diff] [blame] | 826 | auto weights_scale = weights->quantization_info().scale(); |
| 827 | if(!is_data_type_quantized_per_channel(weights->data_type())) |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 828 | { |
Michalis Spyrou | 60c3b0e | 2021-04-08 12:02:58 +0100 | [diff] [blame] | 829 | for(size_t i = 1; i < weights->dimension(channel_idx); ++i) |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 830 | { |
| 831 | weights_scale.push_back(weights_scale.front()); |
| 832 | } |
| 833 | } |
| 834 | |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 835 | for(const auto &s : weights_scale) |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 836 | { |
Michalis Spyrou | e7be8a0 | 2019-12-12 16:16:09 +0000 | [diff] [blame] | 837 | int32_t out_mult = 0; |
| 838 | int32_t out_shift = 0; |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 839 | const float multiplier = input_scale * s / output_scale; |
Michele Di Giorgio | f29d1b7 | 2019-10-29 10:58:13 +0000 | [diff] [blame] | 840 | arm_compute::quantization::calculate_quantized_multiplier(multiplier, &out_mult, &out_shift); |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 841 | |
| 842 | _output_multiplier.push_back(out_mult); |
| 843 | _output_shift.push_back(out_shift); |
| 844 | } |
| 845 | } |
| 846 | |
Michalis Spyrou | 60c3b0e | 2021-04-08 12:02:58 +0100 | [diff] [blame] | 847 | switch(weights->data_type()) |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 848 | { |
| 849 | case DataType::QASYMM8: |
Manuel Bottini | b4bb6a0 | 2021-05-24 16:01:32 +0100 | [diff] [blame] | 850 | _func = &CpuDepthwiseConv2dNativeKernel::run_depthwise<uint8_t, uint8_t>; |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 851 | break; |
Michele Di Giorgio | 8c837ca | 2020-01-07 15:06:41 +0000 | [diff] [blame] | 852 | case DataType::QASYMM8_SIGNED: |
Manuel Bottini | b4bb6a0 | 2021-05-24 16:01:32 +0100 | [diff] [blame] | 853 | _func = &CpuDepthwiseConv2dNativeKernel::run_depthwise<int8_t, int8_t>; |
Michele Di Giorgio | 8c837ca | 2020-01-07 15:06:41 +0000 | [diff] [blame] | 854 | break; |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 855 | case DataType::QSYMM8_PER_CHANNEL: |
Manuel Bottini | b4bb6a0 | 2021-05-24 16:01:32 +0100 | [diff] [blame] | 856 | if(src->data_type() == DataType::QASYMM8) |
Michele Di Giorgio | 8c837ca | 2020-01-07 15:06:41 +0000 | [diff] [blame] | 857 | { |
Manuel Bottini | b4bb6a0 | 2021-05-24 16:01:32 +0100 | [diff] [blame] | 858 | _func = &CpuDepthwiseConv2dNativeKernel::run_depthwise<uint8_t, int8_t>; |
Michele Di Giorgio | 8c837ca | 2020-01-07 15:06:41 +0000 | [diff] [blame] | 859 | } |
| 860 | else |
| 861 | { |
Manuel Bottini | b4bb6a0 | 2021-05-24 16:01:32 +0100 | [diff] [blame] | 862 | _func = &CpuDepthwiseConv2dNativeKernel::run_depthwise<int8_t, int8_t>; |
Michele Di Giorgio | 8c837ca | 2020-01-07 15:06:41 +0000 | [diff] [blame] | 863 | } |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 864 | break; |
| 865 | #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
| 866 | case DataType::F16: |
Manuel Bottini | b4bb6a0 | 2021-05-24 16:01:32 +0100 | [diff] [blame] | 867 | _func = &CpuDepthwiseConv2dNativeKernel::run_depthwise<float16_t, float16_t>; |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 868 | break; |
| 869 | #endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
Giorgio Arena | 44f5572 | 2019-07-12 14:49:49 +0100 | [diff] [blame] | 870 | case DataType::F32: |
Manuel Bottini | b4bb6a0 | 2021-05-24 16:01:32 +0100 | [diff] [blame] | 871 | _func = &CpuDepthwiseConv2dNativeKernel::run_depthwise<float, float>; |
Giorgio Arena | 44f5572 | 2019-07-12 14:49:49 +0100 | [diff] [blame] | 872 | break; |
| 873 | default: |
| 874 | ARM_COMPUTE_ERROR("Data type not supported"); |
| 875 | break; |
| 876 | } |
| 877 | |
Manuel Bottini | b4bb6a0 | 2021-05-24 16:01:32 +0100 | [diff] [blame] | 878 | const TensorShape output_shape = misc::shape_calculator::compute_depthwise_convolution_shape(*src, *weights, info); |
| 879 | auto_init_if_empty(*dst, src->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(output_shape).set_quantization_info(dst->quantization_info())); |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 880 | |
Manuel Bottini | b4bb6a0 | 2021-05-24 16:01:32 +0100 | [diff] [blame] | 881 | Window win = calculate_max_window(*dst, Steps()); |
Michalis Spyrou | 60c3b0e | 2021-04-08 12:02:58 +0100 | [diff] [blame] | 882 | ICpuKernel::configure(win); |
Giorgio Arena | 44f5572 | 2019-07-12 14:49:49 +0100 | [diff] [blame] | 883 | } |
| 884 | |
Manuel Bottini | b4bb6a0 | 2021-05-24 16:01:32 +0100 | [diff] [blame] | 885 | Status CpuDepthwiseConv2dNativeKernel::validate(const ITensorInfo *src, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *dst, const ConvolutionInfo &info) |
Giorgio Arena | 44f5572 | 2019-07-12 14:49:49 +0100 | [diff] [blame] | 886 | { |
Manuel Bottini | b4bb6a0 | 2021-05-24 16:01:32 +0100 | [diff] [blame] | 887 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(src, weights, biases, dst, info)); |
Giorgio Arena | 44f5572 | 2019-07-12 14:49:49 +0100 | [diff] [blame] | 888 | return Status{}; |
| 889 | } |
| 890 | |
Manuel Bottini | b4bb6a0 | 2021-05-24 16:01:32 +0100 | [diff] [blame] | 891 | template <typename T, typename TW, CpuDepthwiseConv2dNativeKernel::FloatEnalber<T>> |
| 892 | void CpuDepthwiseConv2dNativeKernel::run_depthwise(const ITensor *src, const ITensor *weights, const ITensor *biases, |
| 893 | ITensor *dst, const Window &window, bool has_biases) |
Giorgio Arena | 44f5572 | 2019-07-12 14:49:49 +0100 | [diff] [blame] | 894 | { |
| 895 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
Michalis Spyrou | 60c3b0e | 2021-04-08 12:02:58 +0100 | [diff] [blame] | 896 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICpuKernel::window(), window); |
Giorgio Arena | 44f5572 | 2019-07-12 14:49:49 +0100 | [diff] [blame] | 897 | |
| 898 | if(_depth_multiplier == 1) |
| 899 | { |
Michalis Spyrou | 60c3b0e | 2021-04-08 12:02:58 +0100 | [diff] [blame] | 900 | depthwise_loop_multiplier1_fp<T>(src, weights, biases, dst, _conv_info, _dilation, window, has_biases); |
Giorgio Arena | 44f5572 | 2019-07-12 14:49:49 +0100 | [diff] [blame] | 901 | } |
| 902 | else |
| 903 | { |
Michalis Spyrou | 60c3b0e | 2021-04-08 12:02:58 +0100 | [diff] [blame] | 904 | depthwise_loop_generic_fp<T>(src, weights, biases, dst, _conv_info, _dilation, _depth_multiplier, window, has_biases); |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 905 | } |
| 906 | } |
| 907 | |
Manuel Bottini | b4bb6a0 | 2021-05-24 16:01:32 +0100 | [diff] [blame] | 908 | template <typename T, typename TW, CpuDepthwiseConv2dNativeKernel::Quantized8bitEnalber<T>> |
| 909 | void CpuDepthwiseConv2dNativeKernel::run_depthwise(const ITensor *src, const ITensor *weights, const ITensor *biases, |
| 910 | ITensor *dst, const Window &window, bool has_biases) |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 911 | { |
| 912 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
Michalis Spyrou | 60c3b0e | 2021-04-08 12:02:58 +0100 | [diff] [blame] | 913 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICpuKernel::window(), window); |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 914 | |
| 915 | if(_depth_multiplier == 1) |
| 916 | { |
Michalis Spyrou | 60c3b0e | 2021-04-08 12:02:58 +0100 | [diff] [blame] | 917 | depthwise_loop_multiplier1_quantized<T, TW>(src, weights, biases, dst, _conv_info, _dilation, _output_multiplier, _output_shift, window, has_biases); |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 918 | } |
| 919 | else |
| 920 | { |
Giorgio Arena | 3737c79 | 2020-11-23 17:47:23 +0000 | [diff] [blame] | 921 | const bool is_pow2 = ((_depth_multiplier & (_depth_multiplier - 1)) == 0); |
Michalis Spyrou | 60c3b0e | 2021-04-08 12:02:58 +0100 | [diff] [blame] | 922 | const bool is_quantized_per_tensor = !(is_data_type_quantized_per_channel(weights->info()->data_type())); |
Giorgio Arena | 3737c79 | 2020-11-23 17:47:23 +0000 | [diff] [blame] | 923 | |
| 924 | if(is_pow2 && is_quantized_per_tensor && _depth_multiplier >= 8) |
| 925 | { |
Michalis Spyrou | 60c3b0e | 2021-04-08 12:02:58 +0100 | [diff] [blame] | 926 | depthwise_loop_pow2_quantized_per_tensor<T, TW>(src, weights, biases, dst, _conv_info, _dilation, _depth_multiplier, _output_multiplier, _output_shift, window, has_biases); |
Giorgio Arena | 3737c79 | 2020-11-23 17:47:23 +0000 | [diff] [blame] | 927 | } |
| 928 | else |
| 929 | { |
Michalis Spyrou | 60c3b0e | 2021-04-08 12:02:58 +0100 | [diff] [blame] | 930 | depthwise_loop_generic_quantized<T, TW>(src, weights, biases, dst, _conv_info, _dilation, _depth_multiplier, _output_multiplier, _output_shift, window, has_biases); |
Giorgio Arena | 3737c79 | 2020-11-23 17:47:23 +0000 | [diff] [blame] | 931 | } |
Giorgio Arena | 44f5572 | 2019-07-12 14:49:49 +0100 | [diff] [blame] | 932 | } |
| 933 | } |
Michalis Spyrou | 60c3b0e | 2021-04-08 12:02:58 +0100 | [diff] [blame] | 934 | |
Manuel Bottini | b4bb6a0 | 2021-05-24 16:01:32 +0100 | [diff] [blame] | 935 | void CpuDepthwiseConv2dNativeKernel::run_op(ITensorPack &tensors, const Window &window, const ThreadInfo &info) |
Michalis Spyrou | 60c3b0e | 2021-04-08 12:02:58 +0100 | [diff] [blame] | 936 | { |
| 937 | ARM_COMPUTE_UNUSED(info); |
| 938 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 939 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICpuKernel::window(), window); |
| 940 | ARM_COMPUTE_ERROR_ON(_func == nullptr); |
| 941 | |
| 942 | const auto src = tensors.get_const_tensor(TensorType::ACL_SRC_0); |
| 943 | const auto weights = tensors.get_const_tensor(TensorType::ACL_SRC_1); |
| 944 | const auto biases = tensors.get_const_tensor(TensorType::ACL_SRC_2); |
| 945 | auto dst = tensors.get_tensor(TensorType::ACL_DST); |
| 946 | (this->*_func)(src, weights, biases, dst, window, _has_biases); |
| 947 | } |
| 948 | } // namespace kernels |
| 949 | } // namespace cpu |
Giorgio Arena | 44f5572 | 2019-07-12 14:49:49 +0100 | [diff] [blame] | 950 | } // namespace arm_compute |