Giorgio Arena | 44f5572 | 2019-07-12 14:49:49 +0100 | [diff] [blame] | 1 | /* |
Michele Di Giorgio | d9eaf61 | 2020-07-08 11:12:57 +0100 | [diff] [blame] | 2 | * Copyright (c) 2019-2020 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 | */ |
Michalis Spyrou | ebcebf1 | 2020-10-21 00:04:14 +0100 | [diff] [blame] | 24 | #include "src/core/NEON/kernels/NEDepthwiseConvolutionLayerNativeKernel.h" |
Giorgio Arena | 44f5572 | 2019-07-12 14:49:49 +0100 | [diff] [blame] | 25 | |
Giorgio Arena | 44f5572 | 2019-07-12 14:49:49 +0100 | [diff] [blame] | 26 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 27 | #include "arm_compute/core/utils/quantization/AsymmHelpers.h" |
Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 28 | #include "src/core/AccessWindowStatic.h" |
| 29 | #include "src/core/CPP/Validate.h" |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 30 | #include "src/core/NEON/kernels/convolution/depthwise/impl_qa8_qa8.hpp" |
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 | { |
| 39 | namespace |
| 40 | { |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 41 | constexpr auto data_layout = DataLayout::NHWC; |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 42 | const size_t width_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH); |
| 43 | const size_t height_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT); |
| 44 | const size_t channel_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL); |
| 45 | |
| 46 | constexpr auto dim_manual_loop = Window::Dimension(0, 0, 0); |
| 47 | constexpr auto dim_single_unit_step = Window::Dimension(0, 1, 1); |
| 48 | constexpr size_t vector_size = 8; |
| 49 | |
| 50 | struct DepthwiseConvolutionRunInfo |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 51 | { |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 52 | const size_t num_read_elements_per_iteration; |
| 53 | const uint32_t x_start; |
| 54 | const uint32_t x_end; |
| 55 | const uint32_t x_step; |
| 56 | const uint32_t x_leftover_start; |
| 57 | const size_t input_stride_y; |
| 58 | const size_t input_stride_z; |
| 59 | const size_t input_max_offset; |
| 60 | const size_t weights_width; |
| 61 | const size_t weights_height; |
| 62 | const size_t weights_stride_y; |
| 63 | const size_t weights_stride_z; |
| 64 | const size_t conv_stride_x; |
| 65 | const size_t conv_stride_y; |
| 66 | const size_t conv_pad_left; |
| 67 | const size_t conv_pad_top; |
| 68 | const size_t input_height; |
| 69 | const size_t input_width; |
| 70 | const size_t input_depth; |
| 71 | |
| 72 | DepthwiseConvolutionRunInfo(const ITensorInfo &input, const ITensorInfo &weights, const PadStrideInfo &conv_info, const Window &w, uint32_t depth_multiplier = 1) |
| 73 | : num_read_elements_per_iteration((depth_multiplier == 1 ? (vector_size / element_size_from_data_type(input.data_type())) : 1)), |
| 74 | x_start(w.x().start()), |
| 75 | x_end(w.x().end()), |
| 76 | x_step(static_cast<uint32_t>(num_read_elements_per_iteration * depth_multiplier)), |
| 77 | x_leftover_start(std::max(static_cast<int32_t>(w.x().end()) - static_cast<int32_t>(x_step) + 1, int32_t(0))), |
| 78 | input_stride_y(input.strides_in_bytes().y()), |
| 79 | input_stride_z(input.strides_in_bytes().z()), |
| 80 | input_max_offset(input.strides_in_bytes().z() * input.dimension(height_idx) - (input.padding().bottom + input.padding().top) * input.strides_in_bytes().y()), |
| 81 | weights_width(weights.dimension(width_idx)), |
| 82 | weights_height(weights.dimension(height_idx)), |
| 83 | weights_stride_y(weights.strides_in_bytes().y()), |
| 84 | weights_stride_z(weights.strides_in_bytes().z()), |
| 85 | conv_stride_x(conv_info.stride().first), |
| 86 | conv_stride_y(conv_info.stride().second), |
| 87 | conv_pad_left(conv_info.pad_left()), |
| 88 | conv_pad_top(conv_info.pad_top()), |
| 89 | input_height(input.dimension(height_idx)), |
| 90 | input_width(input.dimension(width_idx)), |
| 91 | input_depth(input.dimension(channel_idx)) |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 92 | { |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 93 | } |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 94 | }; |
| 95 | |
| 96 | 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) |
| 97 | { |
| 98 | const int32_t current_h = base_h + h * dilation.y(); |
| 99 | const bool is_valid_h = current_h >= 0 && current_h < static_cast<int32_t>(run_info.input_height); |
| 100 | |
| 101 | const int32_t current_w = base_w + w * dilation.x(); |
| 102 | const bool is_valid_w = current_w >= 0 && current_w < static_cast<int32_t>(run_info.input_width); |
| 103 | |
| 104 | return is_valid_h && is_valid_w; |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 105 | } |
| 106 | |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 107 | template <typename T> |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 108 | void depthwise_loop_multiplier1_fp(const ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output, const PadStrideInfo &conv_info, |
Michalis Spyrou | f401c74 | 2020-05-12 16:18:33 +0100 | [diff] [blame] | 109 | const Size2D &dilation, const Window &window, bool has_biases) |
Giorgio Arena | 44f5572 | 2019-07-12 14:49:49 +0100 | [diff] [blame] | 110 | { |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 111 | constexpr auto element_per_vector = vector_size / sizeof(T); |
| 112 | using VectorType = typename wrapper::traits::neon_vector<T, element_per_vector>::type; |
| 113 | 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] | 114 | |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 115 | const auto run_info = DepthwiseConvolutionRunInfo(*input->info(), *weights->info(), conv_info, window); |
| 116 | |
| 117 | const VectorType zero_vector = wrapper::vdup_n(static_cast<T>(0), TagType{}); |
| 118 | |
| 119 | Window execution_window = window; |
| 120 | execution_window.set(Window::DimX, dim_single_unit_step); |
Giorgio Arena | 44f5572 | 2019-07-12 14:49:49 +0100 | [diff] [blame] | 121 | |
| 122 | Window win_input = window; |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 123 | win_input.set(Window::DimX, dim_manual_loop); |
| 124 | win_input.set(Window::DimY, dim_manual_loop); |
| 125 | win_input.set(Window::DimZ, dim_manual_loop); |
Giorgio Arena | 44f5572 | 2019-07-12 14:49:49 +0100 | [diff] [blame] | 126 | |
| 127 | Window win_weights = win_input; |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 128 | win_weights.set(Window::DimW, dim_manual_loop); |
| 129 | |
| 130 | Window win_output = window; |
| 131 | win_output.set(Window::DimX, dim_manual_loop); |
Giorgio Arena | 44f5572 | 2019-07-12 14:49:49 +0100 | [diff] [blame] | 132 | |
| 133 | Iterator input_it(input, win_input); |
| 134 | Iterator weights_it(weights, win_weights); |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 135 | Iterator output_it(output, win_output); |
Giorgio Arena | 44f5572 | 2019-07-12 14:49:49 +0100 | [diff] [blame] | 136 | Iterator biases_it{}; |
| 137 | |
| 138 | if(has_biases) |
| 139 | { |
| 140 | biases_it = Iterator(biases, win_weights); |
| 141 | } |
| 142 | |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 143 | execute_window_loop(execution_window, [&](const Coordinates & id) |
Giorgio Arena | 44f5572 | 2019-07-12 14:49:49 +0100 | [diff] [blame] | 144 | { |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 145 | const int32_t input_y = id.y() * run_info.conv_stride_x - run_info.conv_pad_left; |
| 146 | const int32_t input_z = id.z() * run_info.conv_stride_y - run_info.conv_pad_top; |
| 147 | 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] | 148 | |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 149 | auto const base_weights_ptr = weights_it.ptr(); |
| 150 | uint32_t x = run_info.x_start; |
Giorgio Arena | 44f5572 | 2019-07-12 14:49:49 +0100 | [diff] [blame] | 151 | |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 152 | for(; x < run_info.x_leftover_start; x += run_info.x_step) |
Giorgio Arena | 44f5572 | 2019-07-12 14:49:49 +0100 | [diff] [blame] | 153 | { |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 154 | VectorType acc = zero_vector; |
| 155 | auto weights_ptr = base_weights_ptr; |
| 156 | int64_t input_offset = base_input_offset; |
Giorgio Arena | 44f5572 | 2019-07-12 14:49:49 +0100 | [diff] [blame] | 157 | |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 158 | for(uint32_t h = 0; h < run_info.weights_height; ++h) |
| 159 | { |
| 160 | int64_t offs = input_offset + x * sizeof(T); |
| 161 | for(uint32_t w = 0; w < run_info.weights_width; ++w) |
| 162 | { |
| 163 | const bool is_valid_region = is_valid_input_region(input_y, input_z, w, h, run_info, dilation); |
| 164 | const auto input_vals = is_valid_region ? |
| 165 | wrapper::vload(reinterpret_cast<T *>(input_it.ptr() + std::min(static_cast<size_t>(offs), run_info.input_max_offset))) : |
| 166 | zero_vector; |
| 167 | const auto weights_vals = wrapper::vload(reinterpret_cast<T *>(weights_ptr + w * run_info.weights_stride_y) + x); |
| 168 | acc = wrapper::vmla(acc, weights_vals, input_vals); |
| 169 | |
| 170 | offs += dilation.x() * run_info.input_stride_y; |
| 171 | } |
| 172 | |
| 173 | weights_ptr += run_info.weights_stride_z; |
| 174 | input_offset += dilation.y() * run_info.input_stride_z; |
Giorgio Arena | 44f5572 | 2019-07-12 14:49:49 +0100 | [diff] [blame] | 175 | } |
| 176 | |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 177 | if(has_biases) |
| 178 | { |
| 179 | const auto biases_vals = wrapper::vload(reinterpret_cast<T *>(biases_it.ptr()) + x); |
| 180 | acc = wrapper::vadd(acc, biases_vals); |
| 181 | } |
| 182 | |
| 183 | wrapper::vstore(reinterpret_cast<T *>(output_it.ptr()) + x, acc); |
Giorgio Arena | 44f5572 | 2019-07-12 14:49:49 +0100 | [diff] [blame] | 184 | } |
| 185 | |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 186 | for(; x < run_info.x_end; ++x) |
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 | auto acc_scalar = T{ 0 }; |
| 189 | auto weights_ptr = base_weights_ptr; |
| 190 | int64_t input_offset = base_input_offset; |
Giorgio Arena | 44f5572 | 2019-07-12 14:49:49 +0100 | [diff] [blame] | 191 | |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 192 | for(size_t h = 0; h < run_info.weights_height; ++h) |
| 193 | { |
| 194 | int64_t offs = input_offset + x * sizeof(T); |
| 195 | for(size_t w = 0; w < run_info.weights_width; ++w) |
| 196 | { |
| 197 | const bool is_valid_region = is_valid_input_region(input_y, input_z, w, h, run_info, dilation); |
| 198 | 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; |
| 199 | const auto weights_vals = *(reinterpret_cast<T *>(weights_ptr + w * run_info.weights_stride_y) + x); |
| 200 | |
| 201 | acc_scalar += (input_vals * weights_vals); |
| 202 | |
| 203 | offs += dilation.x() * run_info.input_stride_y; |
| 204 | } |
| 205 | |
| 206 | weights_ptr += run_info.weights_stride_z; |
| 207 | input_offset += dilation.y() * run_info.input_stride_z; |
| 208 | } |
| 209 | |
| 210 | if(has_biases) |
| 211 | { |
| 212 | const auto biases_vals = *(reinterpret_cast<T *>(biases_it.ptr()) + x); |
| 213 | acc_scalar += biases_vals; |
| 214 | } |
| 215 | *(reinterpret_cast<T *>(output_it.ptr()) + x) = acc_scalar; |
| 216 | } |
Giorgio Arena | 44f5572 | 2019-07-12 14:49:49 +0100 | [diff] [blame] | 217 | }, |
| 218 | input_it, weights_it, biases_it, output_it); |
| 219 | } |
| 220 | |
Michalis Spyrou | f401c74 | 2020-05-12 16:18:33 +0100 | [diff] [blame] | 221 | template <typename T> |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 222 | void depthwise_loop_generic_fp(const ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output, const PadStrideInfo &conv_info, |
Michalis Spyrou | f401c74 | 2020-05-12 16:18:33 +0100 | [diff] [blame] | 223 | 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] | 224 | { |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 225 | const auto run_info = DepthwiseConvolutionRunInfo(*input->info(), *weights->info(), conv_info, window, depth_multiplier); |
Giorgio Arena | 44f5572 | 2019-07-12 14:49:49 +0100 | [diff] [blame] | 226 | |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 227 | Window execution_window = window; |
| 228 | 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] | 229 | |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 230 | Window win_input = execution_window; |
| 231 | win_input.set(Window::DimX, Window::Dimension(0, run_info.input_depth, 1)); |
| 232 | win_input.set(Window::DimY, dim_manual_loop); |
| 233 | win_input.set(Window::DimZ, dim_manual_loop); |
Giorgio Arena | 44f5572 | 2019-07-12 14:49:49 +0100 | [diff] [blame] | 234 | |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 235 | Window win_weights = window; |
| 236 | win_weights.set_dimension_step(Window::DimX, run_info.x_step); |
| 237 | win_weights.set(Window::DimY, dim_manual_loop); |
| 238 | win_weights.set(Window::DimZ, dim_manual_loop); |
| 239 | win_weights.set(Window::DimW, dim_manual_loop); |
| 240 | |
| 241 | Window win_output = window; |
| 242 | win_output.set_dimension_step(Window::DimX, run_info.x_step); |
Giorgio Arena | 44f5572 | 2019-07-12 14:49:49 +0100 | [diff] [blame] | 243 | |
| 244 | Iterator input_it(input, win_input); |
| 245 | Iterator weights_it(weights, win_weights); |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 246 | Iterator output_it(output, win_output); |
Giorgio Arena | 44f5572 | 2019-07-12 14:49:49 +0100 | [diff] [blame] | 247 | Iterator biases_it{}; |
| 248 | |
| 249 | if(has_biases) |
| 250 | { |
| 251 | biases_it = Iterator(biases, win_weights); |
| 252 | } |
| 253 | |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 254 | execute_window_loop(execution_window, [&](const Coordinates & id) |
Giorgio Arena | 44f5572 | 2019-07-12 14:49:49 +0100 | [diff] [blame] | 255 | { |
| 256 | std::vector<T> acc(depth_multiplier, static_cast<T>(0)); |
| 257 | |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 258 | const int input_y = id.y() * run_info.conv_stride_x - run_info.conv_pad_left; |
| 259 | const int input_z = id.z() * run_info.conv_stride_y - run_info.conv_pad_top; |
| 260 | 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] | 261 | |
| 262 | auto weights_ptr = weights_it.ptr(); |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 263 | for(size_t h = 0; h < run_info.weights_height; ++h) |
Giorgio Arena | 44f5572 | 2019-07-12 14:49:49 +0100 | [diff] [blame] | 264 | { |
| 265 | int offs = input_offset; |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 266 | for(size_t w = 0; w < run_info.weights_width; ++w) |
Giorgio Arena | 44f5572 | 2019-07-12 14:49:49 +0100 | [diff] [blame] | 267 | { |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 268 | const bool is_valid_region = is_valid_input_region(input_y, input_z, w, h, run_info, dilation); |
| 269 | 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] | 270 | |
| 271 | for(size_t m = 0; m < depth_multiplier; ++m) |
| 272 | { |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 273 | 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] | 274 | 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] | 275 | } |
| 276 | |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 277 | offs += dilation.x() * run_info.input_stride_y; |
Giorgio Arena | 44f5572 | 2019-07-12 14:49:49 +0100 | [diff] [blame] | 278 | } |
| 279 | |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 280 | weights_ptr += run_info.weights_stride_z; |
| 281 | input_offset += dilation.y() * run_info.input_stride_z; |
Giorgio Arena | 44f5572 | 2019-07-12 14:49:49 +0100 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | if(has_biases) |
| 285 | { |
| 286 | for(size_t m = 0; m < depth_multiplier; ++m) |
| 287 | { |
| 288 | const auto biases_val = *(reinterpret_cast<T *>(biases_it.ptr() + m * sizeof(T))); |
| 289 | *(reinterpret_cast<T *>(output_it.ptr() + m * sizeof(T))) = acc.at(m) + biases_val; |
| 290 | } |
| 291 | } |
| 292 | else |
| 293 | { |
| 294 | for(size_t m = 0; m < depth_multiplier; ++m) |
| 295 | { |
| 296 | *(reinterpret_cast<T *>(output_it.ptr() + m * sizeof(T))) = acc.at(m); |
| 297 | } |
| 298 | } |
| 299 | }, |
| 300 | input_it, weights_it, biases_it, output_it); |
| 301 | } |
| 302 | |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 303 | template <typename T, typename TW> |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 304 | void depthwise_loop_multiplier1_quantized(const ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output, const PadStrideInfo &conv_info, |
Michalis Spyrou | f401c74 | 2020-05-12 16:18:33 +0100 | [diff] [blame] | 305 | const Size2D &dilation, std::vector<int> output_multiplier, std::vector<int> output_shift, const Window &window, bool has_biases) |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 306 | { |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 307 | constexpr auto element_per_vector = vector_size / sizeof(T); |
| 308 | using VectorType = typename wrapper::traits::neon_vector<T, element_per_vector>::type; |
| 309 | using TagType = typename wrapper::traits::neon_vector<T, element_per_vector>::tag_type; |
| 310 | using AccType = int32_t; |
| 311 | using AccArrayType = std::array<AccType, element_per_vector>; |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 312 | |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 313 | const auto out_of_bound_value = PixelValue(static_cast<uint64_t>(0), input->info()->data_type(), input->info()->quantization_info()).get<T>(); |
| 314 | const auto out_of_bound_vector = wrapper::vdup_n(static_cast<T>(out_of_bound_value), TagType{}); |
| 315 | |
| 316 | const auto run_info = DepthwiseConvolutionRunInfo(*input->info(), *weights->info(), conv_info, window); |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 317 | |
| 318 | const int32_t input_qoffset = input->info()->quantization_info().uniform().offset; |
| 319 | const int32_t weights_qoffset = weights->info()->quantization_info().uniform().offset; |
| 320 | const int32_t output_qoffset = output->info()->quantization_info().uniform().offset; |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 321 | const int32_t k_offset = run_info.weights_width * run_info.weights_height * input_qoffset * weights_qoffset; |
| 322 | |
| 323 | Window execution_window = window; |
| 324 | execution_window.set(Window::DimX, dim_single_unit_step); |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 325 | |
| 326 | Window win_input = window; |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 327 | win_input.set(Window::DimX, dim_manual_loop); |
| 328 | win_input.set(Window::DimY, dim_manual_loop); |
| 329 | win_input.set(Window::DimZ, dim_manual_loop); |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 330 | |
| 331 | Window win_weights = win_input; |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 332 | win_weights.set(Window::DimW, dim_manual_loop); |
| 333 | |
| 334 | Window win_output = window; |
| 335 | win_output.set(Window::DimX, dim_manual_loop); |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 336 | |
| 337 | Iterator input_it(input, win_input); |
| 338 | Iterator weights_it(weights, win_weights); |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 339 | Iterator output_it(output, win_output); |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 340 | Iterator biases_it{}; |
| 341 | |
| 342 | if(has_biases) |
| 343 | { |
| 344 | biases_it = Iterator(biases, win_weights); |
| 345 | } |
| 346 | |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 347 | execute_window_loop(execution_window, [&](const Coordinates & id) |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 348 | { |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 349 | const int32_t input_y = id.y() * run_info.conv_stride_x - run_info.conv_pad_left; |
| 350 | const int32_t input_z = id.z() * run_info.conv_stride_y - run_info.conv_pad_top; |
| 351 | const int64_t base_input_offset = input_y * run_info.input_stride_y + input_z * run_info.input_stride_z; |
| 352 | auto const base_weights_ptr = weights_it.ptr(); |
| 353 | size_t x = run_info.x_start; |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 354 | |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 355 | for(; x < run_info.x_leftover_start; x += run_info.x_step) |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 356 | { |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 357 | AccArrayType acc{}; |
| 358 | AccArrayType in_sum{}; |
| 359 | AccArrayType we_sum{}; |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 360 | |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 361 | auto weights_ptr = base_weights_ptr; |
| 362 | auto input_offset = base_input_offset; |
| 363 | |
| 364 | for(size_t h = 0; h < run_info.weights_height; ++h) |
| 365 | { |
| 366 | int64_t offs = input_offset + x * sizeof(T); |
| 367 | for(size_t w = 0; w < run_info.weights_width; ++w) |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 368 | { |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 369 | const bool is_valid_region = is_valid_input_region(input_y, input_z, w, h, run_info, dilation); |
| 370 | const auto input_vals = is_valid_region ? |
| 371 | wrapper::vload(reinterpret_cast<T *>(input_it.ptr() + std::min(static_cast<size_t>(offs), run_info.input_max_offset))) : |
| 372 | out_of_bound_vector; |
| 373 | const auto weights_vals = wrapper::vload(reinterpret_cast<TW *>(weights_ptr + w * run_info.weights_stride_y) + x); |
| 374 | |
Sang-Hoon Park | 1a0a4bc | 2020-11-12 17:41:32 +0000 | [diff] [blame^] | 375 | for(size_t i = 0; i < element_per_vector; ++i) |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 376 | { |
| 377 | acc.at(i) += input_vals[i] * weights_vals[i]; |
| 378 | in_sum.at(i) += input_vals[i]; |
| 379 | we_sum.at(i) += weights_vals[i]; |
| 380 | } |
| 381 | |
| 382 | offs += dilation.x() * run_info.input_stride_y; |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 383 | } |
| 384 | |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 385 | weights_ptr += run_info.weights_stride_z; |
| 386 | input_offset += dilation.y() * run_info.input_stride_z; |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 387 | } |
| 388 | |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 389 | VectorType out_vals = wrapper::vdup_n(static_cast<T>(0), TagType{}); |
Sang-Hoon Park | 1a0a4bc | 2020-11-12 17:41:32 +0000 | [diff] [blame^] | 390 | for(size_t i = 0; i < element_per_vector; ++i) |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 391 | { |
| 392 | acc.at(i) -= in_sum.at(i) * weights_qoffset; |
| 393 | acc.at(i) -= we_sum.at(i) * input_qoffset; |
| 394 | acc.at(i) += k_offset; |
| 395 | |
| 396 | if(has_biases) |
| 397 | { |
| 398 | acc.at(i) += *(reinterpret_cast<int32_t *>(biases_it.ptr() + i * sizeof(int32_t)) + x); |
| 399 | } |
| 400 | |
| 401 | const int32_t out_mul = output_multiplier.at(x + i); |
| 402 | const int32_t out_shift = output_shift.at(x + i); |
| 403 | if(out_shift < 0) |
| 404 | { |
| 405 | acc.at(i) = saturating_doubling_high_mul(acc.at(i) * (1 << (-out_shift)), out_mul) + output_qoffset; |
| 406 | } |
| 407 | else |
| 408 | { |
| 409 | acc.at(i) = rounding_divide_by_exp2(saturating_doubling_high_mul(acc.at(i), out_mul), out_shift) + output_qoffset; |
| 410 | } |
| 411 | out_vals[i] = static_cast<T>(utility::clamp<AccType, T>(acc.at(i))); |
| 412 | } |
| 413 | |
| 414 | wrapper::vstore(reinterpret_cast<T *>(output_it.ptr()) + x, out_vals); |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 415 | } |
| 416 | |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 417 | // left-over |
| 418 | for(; x < run_info.x_end; ++x) |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 419 | { |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 420 | AccType acc = 0; |
| 421 | AccType in_sum = 0; |
| 422 | AccType we_sum = 0; |
| 423 | |
| 424 | auto weights_ptr = base_weights_ptr; |
| 425 | auto input_offset = base_input_offset; |
| 426 | |
| 427 | for(size_t h = 0; h < run_info.weights_height; ++h) |
| 428 | { |
| 429 | int64_t offs = input_offset + x * sizeof(T); |
| 430 | for(size_t w = 0; w < run_info.weights_width; ++w) |
| 431 | { |
| 432 | const bool is_valid_region = is_valid_input_region(input_y, input_z, w, h, run_info, dilation); |
| 433 | const auto input_val = is_valid_region ? |
| 434 | *reinterpret_cast<T *>(input_it.ptr() + std::min(static_cast<size_t>(offs), run_info.input_max_offset)) : |
| 435 | out_of_bound_value; |
| 436 | const auto weights_val = *(reinterpret_cast<TW *>(weights_ptr + w * run_info.weights_stride_y) + x); |
| 437 | |
| 438 | acc += input_val * weights_val; |
| 439 | in_sum += input_val; |
| 440 | we_sum += weights_val; |
| 441 | |
| 442 | offs += dilation.x() * run_info.input_stride_y; |
| 443 | } |
| 444 | |
| 445 | weights_ptr += run_info.weights_stride_z; |
| 446 | input_offset += dilation.y() * run_info.input_stride_z; |
| 447 | } |
| 448 | |
| 449 | T out_vals{ 0 }; |
| 450 | |
| 451 | acc -= in_sum * weights_qoffset; |
| 452 | acc -= we_sum * input_qoffset; |
| 453 | acc += k_offset; |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 454 | |
| 455 | if(has_biases) |
| 456 | { |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 457 | acc += *(reinterpret_cast<int32_t *>(biases_it.ptr()) + x); |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 458 | } |
| 459 | |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 460 | const int32_t out_mul = output_multiplier.at(x); |
| 461 | const int32_t out_shift = output_shift.at(x); |
| 462 | |
Michele Di Giorgio | f29d1b7 | 2019-10-29 10:58:13 +0000 | [diff] [blame] | 463 | if(out_shift < 0) |
| 464 | { |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 465 | 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] | 466 | } |
| 467 | else |
| 468 | { |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 469 | 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] | 470 | } |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 471 | |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 472 | out_vals = static_cast<T>(utility::clamp<AccType, T>(acc)); |
| 473 | *(reinterpret_cast<T *>(output_it.ptr()) + x) = out_vals; |
| 474 | } |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 475 | }, |
| 476 | input_it, weights_it, biases_it, output_it); |
| 477 | } |
| 478 | |
Michalis Spyrou | f401c74 | 2020-05-12 16:18:33 +0100 | [diff] [blame] | 479 | template <typename T, typename TW> |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 480 | void depthwise_loop_generic_quantized(const ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output, const PadStrideInfo &conv_info, |
Michalis Spyrou | f401c74 | 2020-05-12 16:18:33 +0100 | [diff] [blame] | 481 | const Size2D &dilation, unsigned int depth_multiplier, std::vector<int> output_multiplier, std::vector<int> output_shift, const Window &window, bool has_biases) |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 482 | { |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 483 | using AccType = int32_t; |
| 484 | |
| 485 | const auto run_info = DepthwiseConvolutionRunInfo(*input->info(), *weights->info(), conv_info, window, depth_multiplier); |
| 486 | |
| 487 | const auto out_of_bound_value = PixelValue(static_cast<uint64_t>(0), input->info()->data_type(), input->info()->quantization_info()).get<T>(); |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 488 | |
| 489 | const int32_t input_qoffset = input->info()->quantization_info().uniform().offset; |
| 490 | const int32_t weights_qoffset = weights->info()->quantization_info().uniform().offset; |
| 491 | const int32_t output_qoffset = output->info()->quantization_info().uniform().offset; |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 492 | 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] | 493 | |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 494 | Window execution_window = window; |
| 495 | 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] | 496 | |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 497 | Window win_input = execution_window; |
| 498 | win_input.set(Window::DimY, dim_manual_loop); |
| 499 | win_input.set(Window::DimZ, dim_manual_loop); |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 500 | |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 501 | Window win_weights = window; |
| 502 | win_weights.set_dimension_step(Window::DimX, run_info.x_step); |
| 503 | win_weights.set(Window::DimY, dim_manual_loop); |
| 504 | win_weights.set(Window::DimZ, dim_manual_loop); |
| 505 | win_weights.set(Window::DimW, dim_manual_loop); |
| 506 | |
| 507 | Window win_output = window; |
| 508 | win_output.set_dimension_step(Window::DimX, run_info.x_step); |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 509 | |
| 510 | Iterator input_it(input, win_input); |
| 511 | Iterator weights_it(weights, win_weights); |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 512 | Iterator output_it(output, win_output); |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 513 | Iterator biases_it{}; |
| 514 | |
| 515 | if(has_biases) |
| 516 | { |
| 517 | biases_it = Iterator(biases, win_weights); |
| 518 | } |
| 519 | |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 520 | execute_window_loop(execution_window, [&](const Coordinates & id) |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 521 | { |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 522 | std::vector<AccType> acc(depth_multiplier, 0); |
| 523 | std::vector<AccType> we_sum(depth_multiplier, 0); |
| 524 | AccType in_sum = 0; |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 525 | |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 526 | const int32_t input_y = id.y() * run_info.conv_stride_x - run_info.conv_pad_left; |
| 527 | const int32_t input_z = id.z() * run_info.conv_stride_y - run_info.conv_pad_top; |
| 528 | 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] | 529 | |
| 530 | auto weights_ptr = weights_it.ptr(); |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 531 | for(size_t h = 0; h < run_info.weights_height; ++h) |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 532 | { |
| 533 | int offs = input_offset; |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 534 | for(size_t w = 0; w < run_info.weights_width; ++w) |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 535 | { |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 536 | const bool is_valid_region = is_valid_input_region(input_y, input_z, w, h, run_info, dilation); |
| 537 | 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] | 538 | |
| 539 | for(size_t m = 0; m < depth_multiplier; ++m) |
| 540 | { |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 541 | 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] | 542 | acc.at(m) += input_val * weights_val; |
| 543 | |
| 544 | we_sum.at(m) += weights_val; |
| 545 | } |
| 546 | |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 547 | offs += dilation.x() * run_info.input_stride_y; |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 548 | in_sum += input_val; |
| 549 | } |
| 550 | |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 551 | weights_ptr += run_info.weights_stride_z; |
| 552 | input_offset += dilation.y() * run_info.input_stride_z; |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 553 | } |
| 554 | |
| 555 | for(size_t m = 0; m < depth_multiplier; ++m) |
| 556 | { |
| 557 | acc.at(m) -= in_sum * weights_qoffset; |
| 558 | acc.at(m) -= we_sum.at(m) * input_qoffset; |
| 559 | acc.at(m) += k_offset; |
| 560 | |
| 561 | if(has_biases) |
| 562 | { |
Michele Di Giorgio | f29d1b7 | 2019-10-29 10:58:13 +0000 | [diff] [blame] | 563 | acc.at(m) += *(reinterpret_cast<int32_t *>(biases_it.ptr() + m * sizeof(int32_t))); |
| 564 | } |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 565 | |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 566 | const int32_t out_mul = output_multiplier.at(id.x() * depth_multiplier + m); |
| 567 | 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] | 568 | if(out_shift < 0) |
| 569 | { |
| 570 | 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] | 571 | } |
| 572 | else |
| 573 | { |
Michele Di Giorgio | f29d1b7 | 2019-10-29 10:58:13 +0000 | [diff] [blame] | 574 | 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] | 575 | } |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 576 | *(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] | 577 | } |
| 578 | }, |
| 579 | input_it, weights_it, biases_it, output_it); |
| 580 | } |
| 581 | |
Giorgio Arena | 44f5572 | 2019-07-12 14:49:49 +0100 | [diff] [blame] | 582 | Status validate_arguments(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info, unsigned int depth_multiplier, |
| 583 | const Size2D &dilation) |
| 584 | { |
| 585 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, weights, output); |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 586 | ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(input); |
| 587 | ARM_COMPUTE_RETURN_ERROR_ON(input->data_layout() == DataLayout::UNKNOWN); |
Michele Di Giorgio | 8c837ca | 2020-01-07 15:06:41 +0000 | [diff] [blame] | 588 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED, DataType::F16, DataType::F32); |
Giorgio Arena | 44f5572 | 2019-07-12 14:49:49 +0100 | [diff] [blame] | 589 | ARM_COMPUTE_RETURN_ERROR_ON(depth_multiplier == 0); |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 590 | ARM_COMPUTE_RETURN_ERROR_ON(weights->dimension(1) + (weights->dimension(1) - 1) * (dilation.x() - 1) > input->dimension(1) + conv_info.pad_left() + conv_info.pad_right()); |
| 591 | ARM_COMPUTE_RETURN_ERROR_ON(weights->dimension(2) + (weights->dimension(2) - 1) * (dilation.y() - 1) > input->dimension(2) + conv_info.pad_top() + conv_info.pad_bottom()); |
Giorgio Arena | 44f5572 | 2019-07-12 14:49:49 +0100 | [diff] [blame] | 592 | ARM_COMPUTE_RETURN_ERROR_ON((input->dimension(0) * depth_multiplier) != weights->dimension(0)); |
| 593 | ARM_COMPUTE_RETURN_ERROR_ON((dilation.x() < 1) || (dilation.y() < 1)); |
| 594 | ARM_COMPUTE_RETURN_ERROR_ON((conv_info.stride().first < 1) || (conv_info.stride().second < 1)); |
| 595 | |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 596 | if(is_data_type_quantized_per_channel(weights->data_type())) |
| 597 | { |
| 598 | 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] | 599 | ARM_COMPUTE_RETURN_ERROR_ON(weights->dimension(0) != weights->quantization_info().scale().size()); |
| 600 | } |
| 601 | else |
| 602 | { |
Michele Di Giorgio | f9b595a | 2020-07-03 13:34:52 +0100 | [diff] [blame] | 603 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights); |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 604 | } |
| 605 | |
Giorgio Arena | 44f5572 | 2019-07-12 14:49:49 +0100 | [diff] [blame] | 606 | if(biases != nullptr) |
| 607 | { |
| 608 | ARM_COMPUTE_RETURN_ERROR_ON(biases->num_dimensions() > 1); |
| 609 | ARM_COMPUTE_RETURN_ERROR_ON(biases->dimension(0) != weights->dimension(0)); |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 610 | |
| 611 | if(is_data_type_quantized_asymmetric(input->data_type())) |
| 612 | { |
| 613 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(biases, 1, DataType::S32); |
| 614 | } |
| 615 | else |
| 616 | { |
| 617 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(weights, biases); |
| 618 | } |
Giorgio Arena | 44f5572 | 2019-07-12 14:49:49 +0100 | [diff] [blame] | 619 | } |
| 620 | |
| 621 | if(output->total_size() != 0) |
| 622 | { |
| 623 | const TensorShape output_shape = misc::shape_calculator::compute_depthwise_convolution_shape(*input, *weights, conv_info, depth_multiplier, dilation); |
| 624 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(output->tensor_shape(), output_shape); |
Michele Di Giorgio | f9b595a | 2020-07-03 13:34:52 +0100 | [diff] [blame] | 625 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
Giorgio Arena | 44f5572 | 2019-07-12 14:49:49 +0100 | [diff] [blame] | 626 | } |
| 627 | |
| 628 | return Status{}; |
| 629 | } |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 630 | } // namespace |
Giorgio Arena | 44f5572 | 2019-07-12 14:49:49 +0100 | [diff] [blame] | 631 | |
Gian Marco Iodice | bd9097d | 2019-07-26 15:31:02 +0100 | [diff] [blame] | 632 | NEDepthwiseConvolutionLayerNativeKernel::NEDepthwiseConvolutionLayerNativeKernel() |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 633 | : _func(), _input(), _weights(), _biases(), _output(), _conv_info(), _depth_multiplier(1), _dilation(), _output_multiplier(), _output_shift(), _has_biases() |
Giorgio Arena | 44f5572 | 2019-07-12 14:49:49 +0100 | [diff] [blame] | 634 | { |
| 635 | } |
| 636 | |
Gian Marco Iodice | bd9097d | 2019-07-26 15:31:02 +0100 | [diff] [blame] | 637 | void NEDepthwiseConvolutionLayerNativeKernel::configure(const ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output, |
| 638 | const PadStrideInfo &conv_info, unsigned int depth_multiplier, const Size2D &dilation) |
Giorgio Arena | 44f5572 | 2019-07-12 14:49:49 +0100 | [diff] [blame] | 639 | { |
| 640 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights, output); |
| 641 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), weights->info(), (biases != nullptr) ? biases->info() : nullptr, output->info(), conv_info, depth_multiplier, dilation)); |
| 642 | |
| 643 | _input = input; |
| 644 | _weights = weights; |
| 645 | _biases = biases; |
| 646 | _output = output; |
| 647 | _conv_info = conv_info; |
| 648 | _depth_multiplier = depth_multiplier; |
Giorgio Arena | 44f5572 | 2019-07-12 14:49:49 +0100 | [diff] [blame] | 649 | _dilation = dilation; |
Michalis Spyrou | f401c74 | 2020-05-12 16:18:33 +0100 | [diff] [blame] | 650 | _has_biases = (biases != nullptr); |
Giorgio Arena | 44f5572 | 2019-07-12 14:49:49 +0100 | [diff] [blame] | 651 | |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 652 | if(is_data_type_quantized(_input->info()->data_type())) |
Giorgio Arena | 44f5572 | 2019-07-12 14:49:49 +0100 | [diff] [blame] | 653 | { |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 654 | const auto input_scale = input->info()->quantization_info().uniform().scale; |
| 655 | const auto output_scale = output->info()->quantization_info().uniform().scale; |
| 656 | |
| 657 | auto weights_scale = weights->info()->quantization_info().scale(); |
| 658 | if(!is_data_type_quantized_per_channel(_weights->info()->data_type())) |
| 659 | { |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 660 | for(size_t i = 1; i < _weights->info()->dimension(channel_idx); ++i) |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 661 | { |
| 662 | weights_scale.push_back(weights_scale.front()); |
| 663 | } |
| 664 | } |
| 665 | |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 666 | for(const auto &s : weights_scale) |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 667 | { |
Michalis Spyrou | e7be8a0 | 2019-12-12 16:16:09 +0000 | [diff] [blame] | 668 | int32_t out_mult = 0; |
| 669 | int32_t out_shift = 0; |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 670 | const float multiplier = input_scale * s / output_scale; |
Michele Di Giorgio | f29d1b7 | 2019-10-29 10:58:13 +0000 | [diff] [blame] | 671 | arm_compute::quantization::calculate_quantized_multiplier(multiplier, &out_mult, &out_shift); |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 672 | |
| 673 | _output_multiplier.push_back(out_mult); |
| 674 | _output_shift.push_back(out_shift); |
| 675 | } |
| 676 | } |
| 677 | |
| 678 | switch(_weights->info()->data_type()) |
| 679 | { |
| 680 | case DataType::QASYMM8: |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 681 | _func = &NEDepthwiseConvolutionLayerNativeKernel::run_depthwise<uint8_t, uint8_t>; |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 682 | break; |
Michele Di Giorgio | 8c837ca | 2020-01-07 15:06:41 +0000 | [diff] [blame] | 683 | case DataType::QASYMM8_SIGNED: |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 684 | _func = &NEDepthwiseConvolutionLayerNativeKernel::run_depthwise<int8_t, int8_t>; |
Michele Di Giorgio | 8c837ca | 2020-01-07 15:06:41 +0000 | [diff] [blame] | 685 | break; |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 686 | case DataType::QSYMM8_PER_CHANNEL: |
Michele Di Giorgio | 8c837ca | 2020-01-07 15:06:41 +0000 | [diff] [blame] | 687 | if(_input->info()->data_type() == DataType::QASYMM8) |
| 688 | { |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 689 | _func = &NEDepthwiseConvolutionLayerNativeKernel::run_depthwise<uint8_t, int8_t>; |
Michele Di Giorgio | 8c837ca | 2020-01-07 15:06:41 +0000 | [diff] [blame] | 690 | } |
| 691 | else |
| 692 | { |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 693 | _func = &NEDepthwiseConvolutionLayerNativeKernel::run_depthwise<int8_t, int8_t>; |
Michele Di Giorgio | 8c837ca | 2020-01-07 15:06:41 +0000 | [diff] [blame] | 694 | } |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 695 | break; |
| 696 | #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
| 697 | case DataType::F16: |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 698 | _func = &NEDepthwiseConvolutionLayerNativeKernel::run_depthwise<float16_t, float16_t>; |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 699 | break; |
| 700 | #endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
Giorgio Arena | 44f5572 | 2019-07-12 14:49:49 +0100 | [diff] [blame] | 701 | case DataType::F32: |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 702 | _func = &NEDepthwiseConvolutionLayerNativeKernel::run_depthwise<float, float>; |
Giorgio Arena | 44f5572 | 2019-07-12 14:49:49 +0100 | [diff] [blame] | 703 | break; |
| 704 | default: |
| 705 | ARM_COMPUTE_ERROR("Data type not supported"); |
| 706 | break; |
| 707 | } |
| 708 | |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 709 | const TensorShape output_shape = misc::shape_calculator::compute_depthwise_convolution_shape(*input->info(), *weights->info(), conv_info, depth_multiplier, dilation); |
| 710 | auto_init_if_empty(*output->info(), input->info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(output_shape).set_quantization_info(output->info()->quantization_info())); |
| 711 | |
| 712 | Window win = calculate_max_window(*output->info(), Steps()); |
| 713 | Coordinates coord; |
| 714 | coord.set_num_dimensions(output->info()->num_dimensions()); |
| 715 | output->info()->set_valid_region(ValidRegion(coord, output->info()->tensor_shape())); |
| 716 | INEKernel::configure(win); |
Giorgio Arena | 44f5572 | 2019-07-12 14:49:49 +0100 | [diff] [blame] | 717 | } |
| 718 | |
Gian Marco Iodice | bd9097d | 2019-07-26 15:31:02 +0100 | [diff] [blame] | 719 | Status NEDepthwiseConvolutionLayerNativeKernel::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info, |
| 720 | unsigned int depth_multiplier, |
| 721 | const Size2D &dilation) |
Giorgio Arena | 44f5572 | 2019-07-12 14:49:49 +0100 | [diff] [blame] | 722 | { |
| 723 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, weights, biases, output, conv_info, depth_multiplier, dilation)); |
Giorgio Arena | 44f5572 | 2019-07-12 14:49:49 +0100 | [diff] [blame] | 724 | return Status{}; |
| 725 | } |
| 726 | |
Gian Marco Iodice | bd9097d | 2019-07-26 15:31:02 +0100 | [diff] [blame] | 727 | void NEDepthwiseConvolutionLayerNativeKernel::run(const Window &window, const ThreadInfo &info) |
Giorgio Arena | 44f5572 | 2019-07-12 14:49:49 +0100 | [diff] [blame] | 728 | { |
| 729 | ARM_COMPUTE_UNUSED(info); |
| 730 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 731 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window); |
| 732 | |
Michalis Spyrou | f401c74 | 2020-05-12 16:18:33 +0100 | [diff] [blame] | 733 | (this->*_func)(window, _has_biases); |
Giorgio Arena | 44f5572 | 2019-07-12 14:49:49 +0100 | [diff] [blame] | 734 | } |
| 735 | |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 736 | template <typename T, typename TW, NEDepthwiseConvolutionLayerNativeKernel::FloatEnalber<T>> |
Michalis Spyrou | f401c74 | 2020-05-12 16:18:33 +0100 | [diff] [blame] | 737 | void NEDepthwiseConvolutionLayerNativeKernel::run_depthwise(const Window &window, bool has_biases) |
Giorgio Arena | 44f5572 | 2019-07-12 14:49:49 +0100 | [diff] [blame] | 738 | { |
| 739 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 740 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window); |
| 741 | |
| 742 | if(_depth_multiplier == 1) |
| 743 | { |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 744 | depthwise_loop_multiplier1_fp<T>(_input, _weights, _biases, _output, _conv_info, _dilation, window, has_biases); |
Giorgio Arena | 44f5572 | 2019-07-12 14:49:49 +0100 | [diff] [blame] | 745 | } |
| 746 | else |
| 747 | { |
Michalis Spyrou | f401c74 | 2020-05-12 16:18:33 +0100 | [diff] [blame] | 748 | depthwise_loop_generic_fp<T>(_input, _weights, _biases, _output, _conv_info, _dilation, _depth_multiplier, window, has_biases); |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 749 | } |
| 750 | } |
| 751 | |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 752 | template <typename T, typename TW, NEDepthwiseConvolutionLayerNativeKernel::Quantized8bitEnalber<T>> |
Michalis Spyrou | f401c74 | 2020-05-12 16:18:33 +0100 | [diff] [blame] | 753 | void NEDepthwiseConvolutionLayerNativeKernel::run_depthwise(const Window &window, bool has_biases) |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 754 | { |
| 755 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 756 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window); |
| 757 | |
| 758 | if(_depth_multiplier == 1) |
| 759 | { |
Sang-Hoon Park | e4558b5 | 2020-10-01 10:13:07 +0100 | [diff] [blame] | 760 | depthwise_loop_multiplier1_quantized<T, TW>(_input, _weights, _biases, _output, _conv_info, _dilation, _output_multiplier, _output_shift, window, has_biases); |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 761 | } |
| 762 | else |
| 763 | { |
Michalis Spyrou | f401c74 | 2020-05-12 16:18:33 +0100 | [diff] [blame] | 764 | depthwise_loop_generic_quantized<T, TW>(_input, _weights, _biases, _output, _conv_info, _dilation, _depth_multiplier, _output_multiplier, _output_shift, window, has_biases); |
Giorgio Arena | 44f5572 | 2019-07-12 14:49:49 +0100 | [diff] [blame] | 765 | } |
| 766 | } |
| 767 | } // namespace arm_compute |