| 1 | // Tencent is pleased to support the open source community by making ncnn available. |
| 2 | // |
| 3 | // Copyright (C) 2017 THL A29 Limited, a Tencent company. All rights reserved. |
| 4 | // |
| 5 | // Licensed under the BSD 3-Clause License (the "License"); you may not use this file except |
| 6 | // in compliance with the License. You may obtain a copy of the License at |
| 7 | // |
| 8 | // https://opensource.org/licenses/BSD-3-Clause |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software distributed |
| 11 | // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR |
| 12 | // CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 13 | // specific language governing permissions and limitations under the License. |
| 14 | |
| 15 | #include "convolution_x86.h" |
| 16 | |
| 17 | #if __SSE2__ |
| 18 | #include <emmintrin.h> |
| 19 | #if __SSSE3__ |
| 20 | #include <tmmintrin.h> |
| 21 | #if __SSE4_1__ |
| 22 | #include <smmintrin.h> |
| 23 | #if __AVX__ |
| 24 | #include <immintrin.h> |
| 25 | #endif |
| 26 | #endif // __SSE4_1__ |
| 27 | #endif // __SSSE3__ |
| 28 | #endif // __SSE2__ |
| 29 | #include "x86_activation.h" |
| 30 | #include "x86_usability.h" |
| 31 | |
| 32 | #include "benchmark.h" |
| 33 | #include "cpu.h" |
| 34 | #include "layer_type.h" |
| 35 | |
| 36 | namespace ncnn { |
| 37 | |
| 38 | #include "convolution_3x3.h" |
| 39 | #include "convolution_5x5.h" |
| 40 | |
| 41 | #include "convolution_3x3_winograd.h" |
| 42 | #include "convolution_packed.h" |
| 43 | #include "convolution_im2col_gemm.h" |
| 44 | |
| 45 | #if NCNN_INT8 |
| 46 | #include "convolution_3x3_int8.h" |
| 47 | |
| 48 | #include "convolution_packed_int8.h" |
| 49 | #include "convolution_im2col_gemm_int8.h" |
| 50 | |
| 51 | #include "convolution_3x3_winograd_int8.h" |
| 52 | #endif // NCNN_INT8 |
| 53 | |
| 54 | #if __SSE2__ |
| 55 | #include "convolution_3x3_pack1to4.h" |
| 56 | |
| 57 | #if __AVX__ |
| 58 | #include "convolution_3x3_pack1to8.h" |
| 59 | #include "convolution_3x3_pack8to1.h" |
| 60 | #include "convolution_3x3_pack8.h" |
| 61 | #include "convolution_2x2_pack8.h" |
| 62 | |
| 63 | #if __AVX512F__ |
| 64 | #include "convolution_3x3_pack16to1.h" |
| 65 | #endif // __AVX512F__ |
| 66 | #endif // __AVX__ |
| 67 | #endif // __SSE2__ |
| 68 | |
| 69 | Convolution_x86::Convolution_x86() |
| 70 | { |
| 71 | #if __SSE2__ |
| 72 | support_packing = true; |
| 73 | #endif // __SSE2__ |
| 74 | |
| 75 | activation = 0; |
| 76 | nT = 0; |
| 77 | convolution_dilation1 = 0; |
| 78 | } |
| 79 | |
| 80 | static void convolution_transform_kernel_packed_sse(const Mat& weight_data, Mat& weight_data_tm, int num_input, int num_output, int kernel_w, int kernel_h, int elempack, int out_elempack) |
| 81 | { |
| 82 | const int maxk = kernel_w * kernel_h; |
| 83 | |
| 84 | // src = kw-kh-inch-outch |
| 85 | // dst = pb-pa-kw-kh-inch/pa-outch/pb |
| 86 | { |
| 87 | Mat weight_data_r2 = weight_data.reshape(maxk, num_input, num_output); |
| 88 | |
| 89 | weight_data_tm.create(maxk, num_input / elempack, num_output / out_elempack, (size_t)4u * elempack * out_elempack, elempack * out_elempack); |
| 90 | |
| 91 | for (int q = 0; q + (out_elempack - 1) < num_output; q += out_elempack) |
| 92 | { |
| 93 | float* g00 = weight_data_tm.channel(q / out_elempack); |
| 94 | |
| 95 | for (int p = 0; p + (elempack - 1) < num_input; p += elempack) |
| 96 | { |
| 97 | for (int k = 0; k < maxk; k++) |
| 98 | { |
| 99 | for (int i = 0; i < elempack; i++) |
| 100 | { |
| 101 | for (int j = 0; j < out_elempack; j++) |
| 102 | { |
| 103 | const float* k00 = weight_data_r2.channel(q + j).row(p + i); |
| 104 | |
| 105 | g00[0] = k00[k]; |
| 106 | |
| 107 | g00++; |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | static bool test_prefer_winograd63(int num_input, int num_output, int w, int h) |
| 117 | { |
| 118 | // winograd selection strategy (profiled on i7-7700 single thread) |
| 119 | |
| 120 | int minwh = std::min(w, h); |
| 121 | |
| 122 | if (num_input >= 64) |
| 123 | { |
| 124 | return false; |
| 125 | } |
| 126 | if (num_input >= 32) |
| 127 | { |
| 128 | if (num_output >= 64) return false; |
| 129 | if (num_output >= 32) return (minwh >= 11 && minwh <= 14) |
| 130 | || (minwh >= 19 && minwh <= 20) |
| 131 | || (minwh >= 23 && minwh <= 44) |
| 132 | || (minwh >= 47 && minwh <= 56) |
| 133 | || (minwh >= 63 && minwh <= 130); |
| 134 | if (num_output >= 16) return (minwh >= 13 && minwh <= 14) |
| 135 | || (minwh >= 19 && minwh <= 20) |
| 136 | || (minwh >= 23 && minwh <= 38) |
| 137 | || (minwh >= 43 && minwh <= 44) |
| 138 | || (minwh >= 47 && minwh <= 140); |
| 139 | if (num_output >= 8) return (minwh >= 11 && minwh <= 14) |
| 140 | || (minwh >= 19 && minwh <= 20) |
| 141 | || (minwh >= 31 && minwh <= 38) |
| 142 | || (minwh >= 43 && minwh <= 44) |
| 143 | || (minwh >= 55 && minwh <= 162); |
| 144 | return false; |
| 145 | } |
| 146 | if (num_input >= 16) |
| 147 | { |
| 148 | if (num_output >= 64) return false; |
| 149 | if (num_output >= 32) return (minwh >= 11 && minwh <= 14) |
| 150 | || (minwh >= 19 && minwh <= 20) |
| 151 | || (minwh >= 23 && minwh <= 44) |
| 152 | || (minwh >= 47 && minwh <= 92) |
| 153 | || (minwh >= 95 && minwh <= 188); |
| 154 | if (num_output >= 16) return (minwh >= 11 && minwh <= 14) |
| 155 | || (minwh >= 27 && minwh <= 38) |
| 156 | || (minwh >= 43 && minwh <= 44) |
| 157 | || (minwh >= 47 && minwh <= 74) |
| 158 | || (minwh >= 81 && minwh <= 110) |
| 159 | || (minwh >= 117 && minwh <= 170) |
| 160 | || (minwh >= 177 && minwh <= 182); |
| 161 | if (num_output >= 8) return (minwh >= 19 && minwh <= 20) |
| 162 | || (minwh >= 33 && minwh <= 38) |
| 163 | || (minwh >= 43 && minwh <= 44) |
| 164 | || (minwh >= 47 && minwh <= 128) |
| 165 | || (minwh >= 155 && minwh <= 210); |
| 166 | return false; |
| 167 | } |
| 168 | if (num_input >= 8) |
| 169 | { |
| 170 | if (num_output >= 64) return false; |
| 171 | if (num_output >= 32) return (minwh >= 7 && minwh <= 14) |
| 172 | || (minwh >= 17 && minwh <= 20) |
| 173 | || (minwh >= 23 && minwh <= 26) |
| 174 | || (minwh >= 31 && minwh <= 38) |
| 175 | || (minwh >= 43 && minwh <= 162); |
| 176 | if (num_output >= 16) return minwh == 31 || minwh == 32 |
| 177 | || (minwh >= 39 && minwh <= 44) |
| 178 | || (minwh >= 47 && minwh <= 212); |
| 179 | if (num_output >= 8) return false; |
| 180 | return false; |
| 181 | } |
| 182 | |
| 183 | return false; |
| 184 | } |
| 185 | |
| 186 | static bool test_prefer_winograd23(int num_input, int num_output, int w, int h) |
| 187 | { |
| 188 | int minwh = std::min(w, h); |
| 189 | |
| 190 | if (num_input >= 512) |
| 191 | { |
| 192 | if (num_output >= 512) return (minwh >= 3 && minwh <= 14); |
| 193 | if (num_output >= 256) return (minwh >= 3 && minwh <= 14); |
| 194 | if (num_output >= 128) return (minwh >= 3 && minwh <= 14); |
| 195 | if (num_output >= 64) return (minwh >= 3 && minwh <= 8) || (minwh >= 11 && minwh <= 12); |
| 196 | if (num_output >= 32) return (minwh >= 3 && minwh <= 8); |
| 197 | if (num_output >= 16) return (minwh >= 3 && minwh <= 8); |
| 198 | if (num_output >= 8) return (minwh >= 3 && minwh <= 6); |
| 199 | return false; |
| 200 | } |
| 201 | if (num_input >= 256) |
| 202 | { |
| 203 | if (num_output >= 512) return (minwh >= 3 && minwh <= 14); |
| 204 | if (num_output >= 256) return (minwh >= 3 && minwh <= 14); |
| 205 | if (num_output >= 128) return (minwh >= 3 && minwh <= 12); |
| 206 | if (num_output >= 64) return (minwh >= 3 && minwh <= 4); |
| 207 | if (num_output >= 32) return (minwh >= 3 && minwh <= 8); |
| 208 | if (num_output >= 16) return (minwh >= 3 && minwh <= 8); |
| 209 | if (num_output >= 8) return (minwh >= 3 && minwh <= 6); |
| 210 | return false; |
| 211 | } |
| 212 | if (num_input >= 128) |
| 213 | { |
| 214 | if (num_output >= 512) return (minwh >= 3 && minwh <= 14); |
| 215 | if (num_output >= 256) return (minwh >= 3 && minwh <= 8) || (minwh >= 11 && minwh <= 12); |
| 216 | if (num_output >= 128) return (minwh >= 3 && minwh <= 10); |
| 217 | if (num_output >= 64) return (minwh >= 3 && minwh <= 8); |
| 218 | if (num_output >= 32) return (minwh >= 3 && minwh <= 10); |
| 219 | if (num_output >= 16) return (minwh >= 3 && minwh <= 6); |
| 220 | if (num_output >= 8) return (minwh >= 3 && minwh <= 6); |
| 221 | return false; |
| 222 | } |
| 223 | if (num_input >= 64) |
| 224 | { |
| 225 | if (num_output >= 512) return (minwh >= 3 && minwh <= 8) || (minwh >= 11 && minwh <= 12) || (minwh >= 15 && minwh <= 20); |
| 226 | if (num_output >= 256) return (minwh >= 7 && minwh <= 8); |
| 227 | if (num_output >= 128) return (minwh >= 3 && minwh <= 8) || (minwh >= 19 && minwh <= 22); |
| 228 | if (num_output >= 64) return (minwh >= 3 && minwh <= 12); |
| 229 | if (num_output >= 32) return (minwh >= 3 && minwh <= 12); |
| 230 | if (num_output >= 16) return (minwh >= 3 && minwh <= 12); |
| 231 | if (num_output >= 8) return (minwh >= 3 && minwh <= 12); |
| 232 | return false; |
| 233 | } |
| 234 | if (num_input >= 32) |
| 235 | { |
| 236 | if (num_output >= 512) return (minwh >= 3 && minwh <= 6) || (minwh >= 11 && minwh <= 12); |
| 237 | if (num_output >= 256) return (minwh >= 3 && minwh <= 6) || (minwh >= 11 && minwh <= 12); |
| 238 | if (num_output >= 128) return (minwh >= 3 && minwh <= 4) || (minwh >= 7 && minwh <= 16); |
| 239 | if (num_output >= 64) return (minwh >= 3 && minwh <= 8); |
| 240 | if (num_output >= 32) return (minwh >= 7 && minwh <= 8); |
| 241 | if (num_output >= 16) return (minwh >= 7 && minwh <= 8); |
| 242 | if (num_output >= 8) return (minwh >= 3 && minwh <= 10); |
| 243 | return false; |
| 244 | } |
| 245 | if (num_input >= 16) |
| 246 | { |
| 247 | if (num_output >= 512) return (minwh >= 11 && minwh <= 12); |
| 248 | if (num_output >= 256) return (minwh >= 3 && minwh <= 12); |
| 249 | if (num_output >= 128) return (minwh >= 3 && minwh <= 6) |
| 250 | || (minwh >= 9 && minwh <= 18); |
| 251 | if (num_output >= 64) return (minwh >= 3 && minwh <= 4) |
| 252 | || (minwh >= 7 && minwh <= 8) |
| 253 | || (minwh >= 11 && minwh <= 12) |
| 254 | || (minwh >= 15 && minwh <= 18); |
| 255 | if (num_output >= 32) return (minwh >= 3 && minwh <= 4) |
| 256 | || (minwh >= 9 && minwh <= 10); |
| 257 | if (num_output >= 16) return (minwh >= 3 && minwh <= 10); |
| 258 | if (num_output >= 8) return (minwh >= 3 && minwh <= 8) |
| 259 | || (minwh >= 11 && minwh <= 12); |
| 260 | return false; |
| 261 | } |
| 262 | if (num_input >= 8) |
| 263 | { |
| 264 | if (num_output >= 128) return false; |
| 265 | if (num_output >= 64) return (minwh >= 3 && minwh <= 4) |
| 266 | || (minwh >= 7 && minwh <= 14) |
| 267 | || (minwh >= 47 && minwh <= 48); |
| 268 | if (num_output >= 32) return (minwh >= 3 && minwh <= 6) |
| 269 | || (minwh >= 15 && minwh <= 16); |
| 270 | if (num_output >= 16) return (minwh >= 3 && minwh <= 6) |
| 271 | || (minwh >= 9 && minwh <= 14) |
| 272 | || (minwh >= 47 && minwh <= 212); |
| 273 | if (num_output >= 8) return true; |
| 274 | return false; |
| 275 | } |
| 276 | |
| 277 | return false; |
| 278 | } |
| 279 | |
| 280 | int Convolution_x86::create_pipeline(const Option& opt) |
| 281 | { |
| 282 | if (dynamic_weight) |
| 283 | return 0; |
| 284 | |
| 285 | activation = create_activation_layer(activation_type, activation_params, opt); |
| 286 | nT = opt.num_threads; |
| 287 | |
| 288 | #if NCNN_INT8 |
| 289 | if (opt.use_int8_inference && weight_data.elemsize == (size_t)1u) |
| 290 | { |
| 291 | return create_pipeline_int8_x86(opt); |
| 292 | } |
| 293 | #endif |
| 294 | |
| 295 | int kernel_size = kernel_w * kernel_h; |
| 296 | int num_input = weight_data_size / kernel_size / num_output; |
| 297 | |
| 298 | if (!opt.use_packing_layout && kernel_w == kernel_h && dilation_w != 1 && dilation_h == dilation_w && stride_w == 1 && stride_h == 1) |
| 299 | { |
| 300 | convolution_dilation1 = ncnn::create_layer_cpu(ncnn::LayerType::Convolution); |
| 301 | |
| 302 | // set param |
| 303 | ncnn::ParamDict pd; |
| 304 | pd.set(0, num_output); // num_output |
| 305 | pd.set(1, kernel_w); |
| 306 | pd.set(11, kernel_h); |
| 307 | pd.set(2, 1); |
| 308 | pd.set(12, 1); |
| 309 | pd.set(3, 1); // stride_w |
| 310 | pd.set(13, 1); // stride_h |
| 311 | pd.set(4, 0); // pad_w |
| 312 | pd.set(14, 0); // pad_h |
| 313 | pd.set(5, bias_term); |
| 314 | pd.set(6, weight_data_size); |
| 315 | |
| 316 | convolution_dilation1->load_param(pd); |
| 317 | |
| 318 | // set weights |
| 319 | if (bias_term) |
| 320 | { |
| 321 | ncnn::Mat weights[2]; |
| 322 | weights[0] = weight_data; |
| 323 | weights[1] = bias_data; |
| 324 | |
| 325 | convolution_dilation1->load_model(ModelBinFromMatArray(weights)); |
| 326 | } |
| 327 | else |
| 328 | { |
| 329 | ncnn::Mat weights[1]; |
| 330 | weights[0] = weight_data; |
| 331 | |
| 332 | convolution_dilation1->load_model(ModelBinFromMatArray(weights)); |
| 333 | } |
| 334 | |
| 335 | convolution_dilation1->create_pipeline(opt); |
| 336 | |
| 337 | if (opt.lightmode) |
| 338 | weight_data.release(); |
| 339 | |
| 340 | return 0; |
| 341 | } |
| 342 | |
| 343 | int elempack = 1; |
| 344 | int out_elempack = 1; |
| 345 | |
| 346 | #if __SSE2__ |
| 347 | if (opt.use_packing_layout) |
| 348 | { |
| 349 | #if __AVX512F__ |
| 350 | elempack = num_input % 16 == 0 ? 16 : num_input % 8 == 0 ? 8 : num_input % 4 == 0 ? 4 : 1; |
| 351 | out_elempack = num_output % 16 == 0 ? 16 : num_output % 8 == 0 ? 8 : num_output % 4 == 0 ? 4 : 1; |
| 352 | #elif __AVX__ |
| 353 | elempack = num_input % 8 == 0 ? 8 : num_input % 4 == 0 ? 4 : 1; |
| 354 | out_elempack = num_output % 8 == 0 ? 8 : num_output % 4 == 0 ? 4 : 1; |
| 355 | #else |
| 356 | elempack = num_input % 4 == 0 ? 4 : 1; |
| 357 | out_elempack = num_output % 4 == 0 ? 4 : 1; |
| 358 | #endif |
| 359 | } |
| 360 | #endif // __SSE2__ |
| 361 | |
| 362 | bool prefer_winograd = (opt.use_winograd23_convolution || opt.use_winograd43_convolution || opt.use_winograd63_convolution) && (num_input > 8 || num_output > 8); |
| 363 | |
| 364 | if (opt.use_winograd_convolution && prefer_winograd && kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1) |
| 365 | { |
| 366 | if ((bottom_shapes.empty() || bottom_shapes[0].w == 0 || bottom_shapes[0].h == 0) && (top_shapes.empty() || top_shapes[0].w == 0 || top_shapes[0].h == 0)) |
| 367 | { |
| 368 | // dynamic shape |
| 369 | if ((opt.use_winograd63_convolution) && (num_input <= 32 && num_output <= 32)) |
| 370 | conv3x3s1_winograd63_transform_kernel(weight_data, weight_winograd63_data, num_input, num_output, opt); |
| 371 | else if (opt.use_winograd43_convolution) |
| 372 | conv3x3s1_winograd43_transform_kernel(weight_data, weight_winograd43_data, num_input, num_output, opt); |
| 373 | else |
| 374 | conv3x3s1_winograd23_transform_kernel(weight_data, weight_winograd23_data, num_input, num_output, opt); |
| 375 | } |
| 376 | else |
| 377 | { |
| 378 | int w; |
| 379 | int h; |
| 380 | if (top_shapes.empty() || top_shapes[0].w == 0 || top_shapes[0].h == 0) |
| 381 | { |
| 382 | w = bottom_shapes[0].w; |
| 383 | h = bottom_shapes[0].h; |
| 384 | |
| 385 | // make padding |
| 386 | if (pad_left > 0 || pad_right > 0 || pad_top > 0 || pad_bottom > 0) |
| 387 | { |
| 388 | w += pad_left + pad_right; |
| 389 | h += pad_top + pad_bottom; |
| 390 | } |
| 391 | else if ((pad_left == -233 && pad_right == -233 && pad_top == -233 && pad_bottom == -233) |
| 392 | || (pad_left == -234 && pad_right == -234 && pad_top == -234 && pad_bottom == -234)) |
| 393 | { |
| 394 | // tensorflow padding=SAME or onnx padding=SAME_UPPER/SAME_LOWER |
| 395 | w += 2; |
| 396 | h += 2; |
| 397 | } |
| 398 | } |
| 399 | else |
| 400 | { |
| 401 | w = top_shapes[0].w + 2; |
| 402 | h = top_shapes[0].h + 2; |
| 403 | } |
| 404 | |
| 405 | bool prefer_winograd63 = test_prefer_winograd63(num_input, num_output, w, h); |
| 406 | bool prefer_winograd23 = test_prefer_winograd23(num_input, num_output, w, h); |
| 407 | bool prefer_winograd43 = !prefer_winograd63 && !prefer_winograd23; |
| 408 | |
| 409 | if (prefer_winograd23 && !opt.use_winograd23_convolution) |
| 410 | { |
| 411 | // f23 fallback to f43 |
| 412 | prefer_winograd23 = false; |
| 413 | prefer_winograd43 = true; |
| 414 | } |
| 415 | |
| 416 | if (prefer_winograd63 && !opt.use_winograd63_convolution) |
| 417 | { |
| 418 | // f63 fallback to f43 |
| 419 | prefer_winograd63 = false; |
| 420 | prefer_winograd43 = true; |
| 421 | } |
| 422 | |
| 423 | if (prefer_winograd43 && !opt.use_winograd43_convolution) |
| 424 | { |
| 425 | // f43 fallback to f63 or f23 |
| 426 | prefer_winograd43 = false; |
| 427 | if (opt.use_winograd63_convolution) |
| 428 | { |
| 429 | prefer_winograd63 = true; |
| 430 | } |
| 431 | else |
| 432 | { |
| 433 | prefer_winograd23 = true; |
| 434 | } |
| 435 | } |
| 436 | |
| 437 | if (prefer_winograd23) |
| 438 | { |
| 439 | conv3x3s1_winograd23_transform_kernel(weight_data, weight_winograd23_data, num_input, num_output, opt); |
| 440 | } |
| 441 | else if (prefer_winograd43) |
| 442 | { |
| 443 | conv3x3s1_winograd43_transform_kernel(weight_data, weight_winograd43_data, num_input, num_output, opt); |
| 444 | } |
| 445 | else if (prefer_winograd63) |
| 446 | { |
| 447 | conv3x3s1_winograd63_transform_kernel(weight_data, weight_winograd63_data, num_input, num_output, opt); |
| 448 | } |
| 449 | else |
| 450 | { |
| 451 | // should never reach here |
| 452 | } |
| 453 | } |
| 454 | |
| 455 | if (opt.lightmode) |
| 456 | weight_data.release(); |
| 457 | |
| 458 | return 0; |
| 459 | } |
| 460 | |
| 461 | int l2_cache_size = get_cpu_level2_cache_size(); |
| 462 | bool prefer_sgemm = num_input * num_output * kernel_w * kernel_h * dilation_w * dilation_h * stride_w * stride_h * (int)sizeof(float) * 2 > l2_cache_size || (num_input > 16 || num_output > 16); |
| 463 | |
| 464 | if ((opt.use_sgemm_convolution && prefer_sgemm) || (kernel_w == 1 && kernel_h == 1)) |
| 465 | { |
| 466 | convolution_im2col_gemm_transform_kernel(weight_data, weight_sgemm_data, num_input, num_output, kernel_w, kernel_h, opt); |
| 467 | |
| 468 | if (opt.lightmode) |
| 469 | weight_data.release(); |
| 470 | |
| 471 | return 0; |
| 472 | } |
| 473 | |
| 474 | if ((elempack == 16 && out_elempack == 1 && kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1) |
| 475 | || (elempack == 8 && out_elempack == 8 && kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1) |
| 476 | || (elempack == 8 && out_elempack == 8 && kernel_w == 2 && kernel_h == 2 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1) |
| 477 | || (elempack == 1 && out_elempack == 8 && kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1) |
| 478 | || (elempack == 1 && out_elempack == 8 && kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2) |
| 479 | || (elempack == 8 && out_elempack == 1 && kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1) |
| 480 | || (elempack == 1 && out_elempack == 4 && kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1) |
| 481 | || (elempack == 1 && out_elempack == 4 && kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2)) |
| 482 | { |
| 483 | convolution_transform_kernel_packed_sse(weight_data, weight_data_tm, num_input, num_output, kernel_w, kernel_h, elempack, out_elempack); |
| 484 | } |
| 485 | else |
| 486 | { |
| 487 | convolution_transform_kernel_packed(weight_data, weight_data_tm, num_input, num_output, kernel_w, kernel_h); |
| 488 | } |
| 489 | |
| 490 | if (opt.lightmode) |
| 491 | weight_data.release(); |
| 492 | |
| 493 | return 0; |
| 494 | } |
| 495 | |
| 496 | int Convolution_x86::destroy_pipeline(const Option& opt) |
| 497 | { |
| 498 | if (activation) |
| 499 | { |
| 500 | activation->destroy_pipeline(opt); |
| 501 | delete activation; |
| 502 | activation = 0; |
| 503 | } |
| 504 | |
| 505 | if (convolution_dilation1) |
| 506 | { |
| 507 | convolution_dilation1->destroy_pipeline(opt); |
| 508 | delete convolution_dilation1; |
| 509 | convolution_dilation1 = 0; |
| 510 | } |
| 511 | |
| 512 | return 0; |
| 513 | } |
| 514 | |
| 515 | int Convolution_x86::forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const |
| 516 | { |
| 517 | #if NCNN_INT8 |
| 518 | if (opt.use_int8_inference && int8_scale_term) |
| 519 | { |
| 520 | return forward_int8_x86(bottom_blob, top_blob, opt); |
| 521 | } |
| 522 | #endif |
| 523 | |
| 524 | // flattened blob, implement as InnerProduct |
| 525 | if (bottom_blob.dims == 1 && kernel_w == 1 && kernel_h == 1) |
| 526 | { |
| 527 | Mat bottom_blob_3d; |
| 528 | if (bottom_blob.elemsize % 16 == 0) |
| 529 | { |
| 530 | bottom_blob_3d = bottom_blob; |
| 531 | bottom_blob_3d.dims = 3; |
| 532 | bottom_blob_3d.w = 1; |
| 533 | bottom_blob_3d.h = 1; |
| 534 | bottom_blob_3d.c = bottom_blob.w; |
| 535 | bottom_blob_3d.cstep = 1; |
| 536 | } |
| 537 | else |
| 538 | { |
| 539 | bottom_blob_3d = bottom_blob.reshape(1, 1, bottom_blob.w, opt.workspace_allocator); |
| 540 | if (bottom_blob_3d.empty()) |
| 541 | return -100; |
| 542 | } |
| 543 | |
| 544 | Mat top_blob_3d; |
| 545 | int ret = forward(bottom_blob_3d, top_blob_3d, opt); |
| 546 | if (ret != 0) |
| 547 | return ret; |
| 548 | |
| 549 | if (top_blob_3d.elemsize % 16 == 0) |
| 550 | { |
| 551 | top_blob = top_blob_3d; |
| 552 | top_blob.dims = 1; |
| 553 | top_blob.w = top_blob_3d.c; |
| 554 | top_blob.h = 1; |
| 555 | top_blob.c = 1; |
| 556 | bottom_blob_3d.cstep = top_blob_3d.c; |
| 557 | } |
| 558 | else |
| 559 | { |
| 560 | top_blob = top_blob_3d.reshape(top_blob_3d.c, opt.blob_allocator); |
| 561 | if (top_blob.empty()) |
| 562 | return -100; |
| 563 | } |
| 564 | |
| 565 | return 0; |
| 566 | } |
| 567 | |
| 568 | int w = bottom_blob.w; |
| 569 | int h = bottom_blob.h; |
| 570 | int channels = bottom_blob.c; |
| 571 | size_t elemsize = bottom_blob.elemsize; |
| 572 | int elempack = bottom_blob.elempack; |
| 573 | |
| 574 | const int kernel_extent_w = dilation_w * (kernel_w - 1) + 1; |
| 575 | const int kernel_extent_h = dilation_h * (kernel_h - 1) + 1; |
| 576 | |
| 577 | Mat bottom_blob_bordered; |
| 578 | make_padding(bottom_blob, bottom_blob_bordered, opt); |
| 579 | if (bottom_blob_bordered.empty()) |
| 580 | return -100; |
| 581 | |
| 582 | w = bottom_blob_bordered.w; |
| 583 | h = bottom_blob_bordered.h; |
| 584 | |
| 585 | int outw = (w - kernel_extent_w) / stride_w + 1; |
| 586 | int outh = (h - kernel_extent_h) / stride_h + 1; |
| 587 | int out_elempack = 1; |
| 588 | #if __SSE2__ |
| 589 | if (opt.use_packing_layout) |
| 590 | { |
| 591 | #if __AVX512F__ |
| 592 | out_elempack = num_output % 16 == 0 ? 16 : num_output % 8 == 0 ? 8 : num_output % 4 == 0 ? 4 : 1; |
| 593 | #elif __AVX__ |
| 594 | out_elempack = num_output % 8 == 0 ? 8 : num_output % 4 == 0 ? 4 : 1; |
| 595 | #else |
| 596 | out_elempack = num_output % 4 == 0 ? 4 : 1; |
| 597 | #endif |
| 598 | } |
| 599 | #endif // __SSE2__ |
| 600 | size_t out_elemsize = elemsize / elempack * out_elempack; |
| 601 | |
| 602 | top_blob.create(outw, outh, num_output / out_elempack, out_elemsize, out_elempack, opt.blob_allocator); |
| 603 | if (top_blob.empty()) |
| 604 | return -100; |
| 605 | |
| 606 | if (!opt.use_packing_layout && kernel_w == kernel_h && dilation_w != 1 && dilation_h == dilation_w && stride_w == 1 && stride_h == 1) |
| 607 | { |
| 608 | if (outw >= dilation_w && outh >= dilation_h) |
| 609 | { |
| 610 | return forwardDilation_x86(bottom_blob_bordered, top_blob, opt); |
| 611 | } |
| 612 | } |
| 613 | |
| 614 | const int num_input = channels * elempack; |
| 615 | |
| 616 | bool prefer_winograd = (opt.use_winograd23_convolution || opt.use_winograd43_convolution || opt.use_winograd63_convolution) && (num_input > 8 || num_output > 8); |
| 617 | |
| 618 | if (opt.use_winograd_convolution && prefer_winograd && kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1) |
| 619 | { |
| 620 | bool prefer_winograd63 = test_prefer_winograd63(num_input, num_output, w, h); |
| 621 | bool prefer_winograd23 = test_prefer_winograd23(num_input, num_output, w, h); |
| 622 | bool prefer_winograd43 = !prefer_winograd63 && !prefer_winograd23; |
| 623 | |
| 624 | if (prefer_winograd23 && (!opt.use_winograd23_convolution || weight_winograd23_data.empty())) |
| 625 | { |
| 626 | // f23 fallback to f43 |
| 627 | prefer_winograd23 = false; |
| 628 | prefer_winograd43 = true; |
| 629 | } |
| 630 | |
| 631 | if (prefer_winograd63 && (!opt.use_winograd63_convolution || weight_winograd63_data.empty())) |
| 632 | { |
| 633 | // f63 fallback to f43 |
| 634 | prefer_winograd63 = false; |
| 635 | prefer_winograd43 = true; |
| 636 | } |
| 637 | |
| 638 | if (prefer_winograd43 && (!opt.use_winograd43_convolution || weight_winograd43_data.empty())) |
| 639 | { |
| 640 | // f43 fallback to f63 or f23 |
| 641 | prefer_winograd43 = false; |
| 642 | if (opt.use_winograd63_convolution && !weight_winograd63_data.empty()) |
| 643 | { |
| 644 | prefer_winograd63 = true; |
| 645 | } |
| 646 | else |
| 647 | { |
| 648 | prefer_winograd23 = true; |
| 649 | } |
| 650 | } |
| 651 | |
| 652 | int _nT = nT ? nT : opt.num_threads; |
| 653 | if (nT != 0 && opt.num_threads != nT) |
| 654 | { |
| 655 | // force num_threads the same as in create_pipeline |
| 656 | // so we could use pre-packed A/B from the same tile config |
| 657 | NCNN_LOGE("opt.num_threads %d changed, convolution winograd will use load-time value %d", opt.num_threads, nT); |
| 658 | } |
| 659 | |
| 660 | int ret = 0; |
| 661 | if (prefer_winograd23) |
| 662 | { |
| 663 | ret = conv3x3s1_winograd23(bottom_blob_bordered, top_blob, weight_winograd23_data, bias_data, _nT, opt); |
| 664 | } |
| 665 | else if (prefer_winograd43) |
| 666 | { |
| 667 | ret = conv3x3s1_winograd43(bottom_blob_bordered, top_blob, weight_winograd43_data, bias_data, _nT, opt); |
| 668 | } |
| 669 | else if (prefer_winograd63) |
| 670 | { |
| 671 | ret = conv3x3s1_winograd63(bottom_blob_bordered, top_blob, weight_winograd63_data, bias_data, _nT, opt); |
| 672 | } |
| 673 | else |
| 674 | { |
| 675 | // should never reach here |
| 676 | } |
| 677 | if (ret != 0) |
| 678 | return ret; |
| 679 | |
| 680 | if (activation) |
| 681 | { |
| 682 | activation->forward_inplace(top_blob, opt); |
| 683 | } |
| 684 | return 0; |
| 685 | } |
| 686 | |
| 687 | int l2_cache_size = get_cpu_level2_cache_size(); |
| 688 | bool prefer_sgemm = num_input * num_output * kernel_w * kernel_h * dilation_w * dilation_h * stride_w * stride_h * (int)sizeof(float) * 2 > l2_cache_size || (num_input > 16 || num_output > 16); |
| 689 | |
| 690 | if ((opt.use_sgemm_convolution && prefer_sgemm) || (kernel_w == 1 && kernel_h == 1)) |
| 691 | { |
| 692 | int _nT = nT ? nT : opt.num_threads; |
| 693 | if (nT != 0 && opt.num_threads != nT) |
| 694 | { |
| 695 | // force num_threads the same as in create_pipeline |
| 696 | // so we could use pre-packed A/B from the same tile config |
| 697 | NCNN_LOGE("opt.num_threads %d changed, convolution gemm will use load-time value %d", opt.num_threads, nT); |
| 698 | } |
| 699 | |
| 700 | int ret = convolution_im2col_gemm(bottom_blob_bordered, top_blob, weight_sgemm_data, bias_data, kernel_w, kernel_h, dilation_w, dilation_h, stride_w, stride_h, _nT, opt); |
| 701 | if (ret != 0) |
| 702 | return ret; |
| 703 | |
| 704 | if (activation) |
| 705 | { |
| 706 | activation->forward_inplace(top_blob, opt); |
| 707 | } |
| 708 | return 0; |
| 709 | } |
| 710 | |
| 711 | #if __SSE2__ |
| 712 | #if __AVX__ |
| 713 | #if __AVX512F__ |
| 714 | if (elempack == 16 && out_elempack == 1) |
| 715 | { |
| 716 | if (kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1) |
| 717 | { |
| 718 | conv3x3s1_pack16to1_avx512(bottom_blob_bordered, top_blob, weight_data_tm, bias_data, opt); |
| 719 | |
| 720 | if (activation) |
| 721 | { |
| 722 | activation->forward_inplace(top_blob, opt); |
| 723 | } |
| 724 | return 0; |
| 725 | } |
| 726 | } |
| 727 | #endif // __AVX512F__ |
| 728 | |
| 729 | if (elempack == 8 && out_elempack == 8) |
| 730 | { |
| 731 | if (kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1) |
| 732 | { |
| 733 | conv3x3s1_pack8_avx(bottom_blob_bordered, top_blob, weight_data_tm, bias_data, opt); |
| 734 | |
| 735 | if (activation) |
| 736 | { |
| 737 | activation->forward_inplace(top_blob, opt); |
| 738 | } |
| 739 | return 0; |
| 740 | } |
| 741 | if (kernel_w == 2 && kernel_h == 2 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1) |
| 742 | { |
| 743 | conv2x2s1_pack8_avx(bottom_blob_bordered, top_blob, weight_data_tm, bias_data, opt); |
| 744 | |
| 745 | if (activation) |
| 746 | { |
| 747 | activation->forward_inplace(top_blob, opt); |
| 748 | } |
| 749 | return 0; |
| 750 | } |
| 751 | } |
| 752 | |
| 753 | if (elempack == 1 && out_elempack == 8) |
| 754 | { |
| 755 | if (kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1) |
| 756 | { |
| 757 | conv3x3s1_pack1to8_avx(bottom_blob_bordered, top_blob, weight_data_tm, bias_data, opt); |
| 758 | |
| 759 | if (activation) |
| 760 | { |
| 761 | activation->forward_inplace(top_blob, opt); |
| 762 | } |
| 763 | return 0; |
| 764 | } |
| 765 | if (kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2) |
| 766 | { |
| 767 | conv3x3s2_pack1to8_avx(bottom_blob_bordered, top_blob, weight_data_tm, bias_data, opt); |
| 768 | |
| 769 | if (activation) |
| 770 | { |
| 771 | activation->forward_inplace(top_blob, opt); |
| 772 | } |
| 773 | return 0; |
| 774 | } |
| 775 | } |
| 776 | |
| 777 | if (elempack == 8 && out_elempack == 1) |
| 778 | { |
| 779 | if (kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1) |
| 780 | { |
| 781 | conv3x3s1_pack8to1_avx(bottom_blob_bordered, top_blob, weight_data_tm, bias_data, opt); |
| 782 | |
| 783 | if (activation) |
| 784 | { |
| 785 | activation->forward_inplace(top_blob, opt); |
| 786 | } |
| 787 | return 0; |
| 788 | } |
| 789 | } |
| 790 | #endif // __AVX__ |
| 791 | |
| 792 | if (elempack == 1 && out_elempack == 4) |
| 793 | { |
| 794 | if (kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1) |
| 795 | { |
| 796 | conv3x3s1_pack1to4_sse(bottom_blob_bordered, top_blob, weight_data_tm, bias_data, opt); |
| 797 | |
| 798 | if (activation) |
| 799 | { |
| 800 | activation->forward_inplace(top_blob, opt); |
| 801 | } |
| 802 | return 0; |
| 803 | } |
| 804 | if (kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2) |
| 805 | { |
| 806 | conv3x3s2_pack1to4_sse(bottom_blob_bordered, top_blob, weight_data_tm, bias_data, opt); |
| 807 | |
| 808 | if (activation) |
| 809 | { |
| 810 | activation->forward_inplace(top_blob, opt); |
| 811 | } |
| 812 | return 0; |
| 813 | } |
| 814 | } |
| 815 | #endif // __SSE2__ |
| 816 | |
| 817 | convolution_packed(bottom_blob_bordered, top_blob, weight_data_tm, bias_data, kernel_w, kernel_h, dilation_w, dilation_h, stride_w, stride_h, activation_type, activation_params, opt); |
| 818 | |
| 819 | return 0; |
| 820 | } |
| 821 | |
| 822 | int Convolution_x86::forward(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& top_blobs, const Option& opt) const |
| 823 | { |
| 824 | const Mat& bottom_blob = bottom_blobs[0]; |
| 825 | const Mat& _weight_data = bottom_blobs[1]; |
| 826 | Mat& top_blob = top_blobs[0]; |
| 827 | |
| 828 | const int _kernel_w = _weight_data.w; |
| 829 | const int _kernel_h = _weight_data.h; |
| 830 | const int _num_output = _weight_data.c * _weight_data.elempack; |
| 831 | |
| 832 | Mat weight_data_flattened; |
| 833 | flatten(_weight_data, weight_data_flattened, opt); |
| 834 | if (weight_data_flattened.empty()) |
| 835 | return -100; |
| 836 | |
| 837 | // weight_data_flattened as pack1 |
| 838 | weight_data_flattened.w *= weight_data_flattened.elempack; |
| 839 | weight_data_flattened.elemsize /= weight_data_flattened.elempack; |
| 840 | weight_data_flattened.elempack = 1; |
| 841 | |
| 842 | Mat bias_data_flattened; |
| 843 | if (bias_term) |
| 844 | { |
| 845 | const Mat& _bias_data = bottom_blobs[2]; |
| 846 | flatten(_bias_data, bias_data_flattened, opt); |
| 847 | if (bias_data_flattened.empty()) |
| 848 | return -100; |
| 849 | |
| 850 | // bias_data_flattened as pack1 |
| 851 | bias_data_flattened.w *= bias_data_flattened.elempack; |
| 852 | bias_data_flattened.elemsize /= bias_data_flattened.elempack; |
| 853 | bias_data_flattened.elempack = 1; |
| 854 | } |
| 855 | |
| 856 | ncnn::Layer* op = ncnn::create_layer_cpu(ncnn::LayerType::Convolution); |
| 857 | |
| 858 | ncnn::ParamDict pd; |
| 859 | pd.set(0, _num_output); |
| 860 | pd.set(1, _kernel_w); |
| 861 | pd.set(11, _kernel_h); |
| 862 | pd.set(2, dilation_w); |
| 863 | pd.set(12, dilation_h); |
| 864 | pd.set(3, stride_w); |
| 865 | pd.set(13, stride_h); |
| 866 | pd.set(4, pad_left); |
| 867 | pd.set(15, pad_right); |
| 868 | pd.set(14, pad_top); |
| 869 | pd.set(16, pad_bottom); |
| 870 | pd.set(18, pad_value); |
| 871 | pd.set(5, bias_term); |
| 872 | pd.set(6, weight_data_flattened.w); |
| 873 | pd.set(8, int8_scale_term); |
| 874 | pd.set(9, activation_type); |
| 875 | pd.set(10, activation_params); |
| 876 | |
| 877 | op->load_param(pd); |
| 878 | |
| 879 | ncnn::Mat weights[2]; |
| 880 | weights[0] = weight_data_flattened; |
| 881 | weights[1] = bias_data_flattened; |
| 882 | |
| 883 | op->load_model(ncnn::ModelBinFromMatArray(weights)); |
| 884 | |
| 885 | op->create_pipeline(opt); |
| 886 | |
| 887 | op->forward(bottom_blob, top_blob, opt); |
| 888 | |
| 889 | op->destroy_pipeline(opt); |
| 890 | |
| 891 | delete op; |
| 892 | |
| 893 | return 0; |
| 894 | } |
| 895 | |
| 896 | #if NCNN_INT8 |
| 897 | int Convolution_x86::create_pipeline_int8_x86(const Option& opt) |
| 898 | { |
| 899 | const int maxk = kernel_w * kernel_h; |
| 900 | const int num_input = weight_data_size / maxk / num_output; |
| 901 | |
| 902 | bool prefer_winograd = (opt.use_winograd23_convolution || opt.use_winograd43_convolution) && (num_input > 8 || num_output > 8); |
| 903 | |
| 904 | if (opt.use_winograd_convolution && prefer_winograd && kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1) |
| 905 | { |
| 906 | if (opt.use_winograd43_convolution) |
| 907 | conv3x3s1_winograd43_transform_kernel_int8(weight_data, weight_winograd43_data, num_input, num_output, opt); |
| 908 | else |
| 909 | conv3x3s1_winograd23_transform_kernel_int8(weight_data, weight_winograd23_data, num_input, num_output, opt); |
| 910 | } |
| 911 | else if (opt.use_sgemm_convolution) |
| 912 | { |
| 913 | convolution_im2col_gemm_transform_kernel_int8(weight_data, weight_sgemm_data, num_input, num_output, kernel_w, kernel_h, opt); |
| 914 | } |
| 915 | else |
| 916 | { |
| 917 | convolution_transform_kernel_packed_int8(weight_data, weight_data_tm, num_input, num_output, kernel_w, kernel_h); |
| 918 | } |
| 919 | |
| 920 | scale_in_data.create(num_output); |
| 921 | for (int p = 0; p < num_output; p++) |
| 922 | { |
| 923 | // requantize and relu |
| 924 | float scale_in; |
| 925 | if (weight_data_int8_scales[p] == 0) |
| 926 | scale_in = 0; |
| 927 | else |
| 928 | scale_in = 1.f / (bottom_blob_int8_scales[0] * weight_data_int8_scales[p]); |
| 929 | |
| 930 | scale_in_data[p] = scale_in; |
| 931 | } |
| 932 | |
| 933 | if (opt.lightmode) |
| 934 | weight_data.release(); |
| 935 | |
| 936 | return 0; |
| 937 | } |
| 938 | |
| 939 | int Convolution_x86::forward_int8_x86(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const |
| 940 | { |
| 941 | int elembits = bottom_blob.elembits(); |
| 942 | |
| 943 | Mat bottom_blob_int8 = bottom_blob; |
| 944 | if (elembits != 8) |
| 945 | { |
| 946 | Option opt_q = opt; |
| 947 | opt_q.blob_allocator = opt.workspace_allocator; |
| 948 | quantize_to_int8(bottom_blob, bottom_blob_int8, bottom_blob_int8_scales, opt_q); |
| 949 | if (bottom_blob_int8.empty()) |
| 950 | return -100; |
| 951 | } |
| 952 | |
| 953 | // NCNN_LOGE("Convolution_x86 input %d x %d ksize=%d %d stride=%d %d", w, h, kernel_w, kernel_h, stride_w, stride_h); |
| 954 | |
| 955 | Mat bottom_blob_bordered; |
| 956 | make_padding(bottom_blob_int8, bottom_blob_bordered, opt); |
| 957 | if (bottom_blob_bordered.empty()) |
| 958 | return -100; |
| 959 | |
| 960 | int w = bottom_blob_bordered.w; |
| 961 | int h = bottom_blob_bordered.h; |
| 962 | int channels = bottom_blob_bordered.c; |
| 963 | int elempack = bottom_blob_bordered.elempack; |
| 964 | |
| 965 | const int kernel_extent_w = dilation_w * (kernel_w - 1) + 1; |
| 966 | const int kernel_extent_h = dilation_h * (kernel_h - 1) + 1; |
| 967 | |
| 968 | int outw = (w - kernel_extent_w) / stride_w + 1; |
| 969 | int outh = (h - kernel_extent_h) / stride_h + 1; |
| 970 | |
| 971 | bool use_int8_requantize = int8_scale_term > 100; |
| 972 | int out_elempack = 1; |
| 973 | #if __SSE2__ |
| 974 | if (opt.use_packing_layout) |
| 975 | { |
| 976 | if (use_int8_requantize) |
| 977 | out_elempack = num_output % 8 == 0 ? 8 : 1; |
| 978 | else |
| 979 | out_elempack = num_output % 4 == 0 ? 4 : 1; |
| 980 | } |
| 981 | #endif // __SSE2__ |
| 982 | size_t out_elemsize = use_int8_requantize ? 1u * out_elempack : 4u * out_elempack; |
| 983 | |
| 984 | // NCNN_LOGE("forward_int8_x86 %d %d %d %d %d", w, h, bottom_blob_bordered.c, elempack, out_elempack); |
| 985 | |
| 986 | top_blob.create(outw, outh, num_output / out_elempack, out_elemsize, out_elempack, opt.blob_allocator); |
| 987 | if (top_blob.empty()) |
| 988 | return -100; |
| 989 | |
| 990 | const int num_input = channels * elempack; |
| 991 | |
| 992 | int out_elempack_int32 = 1; |
| 993 | #if __SSE2__ |
| 994 | if (opt.use_packing_layout) |
| 995 | { |
| 996 | out_elempack_int32 = num_output % 4 == 0 ? 4 : 1; |
| 997 | } |
| 998 | #endif // __SSE2__ |
| 999 | |
| 1000 | Mat top_blob_int32; |
| 1001 | top_blob_int32.create(outw, outh, num_output / out_elempack_int32, (size_t)(4u * out_elempack_int32), out_elempack_int32, opt.workspace_allocator); |
| 1002 | if (top_blob_int32.empty()) |
| 1003 | return -100; |
| 1004 | |
| 1005 | bool prefer_winograd = (opt.use_winograd23_convolution || opt.use_winograd43_convolution) && (num_input > 8 || num_output > 8); |
| 1006 | |
| 1007 | int _nT = nT ? nT : opt.num_threads; |
| 1008 | if (nT != 0 && opt.num_threads != nT) |
| 1009 | { |
| 1010 | // force num_threads the same as in create_pipeline |
| 1011 | // so we could use pre-packed A/B from the same tile config |
| 1012 | NCNN_LOGE("opt.num_threads %d changed, convolution gemm will use load-time value %d", opt.num_threads, nT); |
| 1013 | } |
| 1014 | |
| 1015 | int ret = 0; |
| 1016 | if (opt.use_winograd_convolution && prefer_winograd && kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1) |
| 1017 | { |
| 1018 | if (opt.use_winograd43_convolution && !weight_winograd43_data.empty()) |
| 1019 | ret = conv3x3s1_winograd43_int8(bottom_blob_bordered, top_blob_int32, weight_winograd43_data, _nT, opt); |
| 1020 | else |
| 1021 | ret = conv3x3s1_winograd23_int8(bottom_blob_bordered, top_blob_int32, weight_winograd23_data, _nT, opt); |
| 1022 | } |
| 1023 | else if (opt.use_sgemm_convolution) |
| 1024 | { |
| 1025 | ret = convolution_im2col_gemm_int8(bottom_blob_bordered, top_blob_int32, weight_sgemm_data, kernel_w, kernel_h, dilation_w, dilation_h, stride_w, stride_h, _nT, opt); |
| 1026 | } |
| 1027 | else |
| 1028 | { |
| 1029 | convolution_packed_int8(bottom_blob_bordered, top_blob_int32, weight_data_tm, kernel_w, kernel_h, dilation_w, dilation_h, stride_w, stride_h, opt); |
| 1030 | } |
| 1031 | if (ret != 0) |
| 1032 | return ret; |
| 1033 | |
| 1034 | if (use_int8_requantize) |
| 1035 | { |
| 1036 | requantize_from_int32_to_int8(top_blob_int32, top_blob, scale_in_data, top_blob_int8_scales, bias_data, activation_type, activation_params, opt); |
| 1037 | } |
| 1038 | else |
| 1039 | { |
| 1040 | dequantize_from_int32(top_blob_int32, top_blob, scale_in_data, bias_data, opt); |
| 1041 | |
| 1042 | if (activation) |
| 1043 | { |
| 1044 | activation->forward_inplace(top_blob, opt); |
| 1045 | } |
| 1046 | } |
| 1047 | |
| 1048 | return 0; |
| 1049 | } |
| 1050 | #endif // NCNN_INT8 |
| 1051 | |
| 1052 | int Convolution_x86::forwardDilation_x86(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const |
| 1053 | { |
| 1054 | int w = bottom_blob.w; |
| 1055 | int h = bottom_blob.h; |
| 1056 | size_t elemsize = bottom_blob.elemsize; |
| 1057 | |
| 1058 | const int kernel_size = kernel_w; |
| 1059 | const int stride = stride_w; |
| 1060 | const int dilation = dilation_w; |
| 1061 | const int kernel_extent = dilation * (kernel_size - 1) + 1; |
| 1062 | |
| 1063 | int outw = (w - kernel_extent) / stride + 1; |
| 1064 | int outh = (h - kernel_extent) / stride + 1; |
| 1065 | |
| 1066 | top_blob.create(outw, outh, num_output, elemsize, opt.blob_allocator); |
| 1067 | if (top_blob.empty()) |
| 1068 | return -100; |
| 1069 | |
| 1070 | // Make (dilation * dilation) batches |
| 1071 | Mat inner_bottom_blob; |
| 1072 | Mat inner_top_blob; |
| 1073 | for (int x = 0; x < dilation; x++) |
| 1074 | { |
| 1075 | for (int y = 0; y < dilation; y++) |
| 1076 | { |
| 1077 | int inner_w = (w - y + dilation - 1) / dilation; |
| 1078 | int inner_h = (h - x + dilation - 1) / dilation; |
| 1079 | |
| 1080 | int inner_outw = (inner_w - kernel_size) / stride + 1; |
| 1081 | int inner_outh = (inner_h - kernel_size) / stride + 1; |
| 1082 | |
| 1083 | inner_bottom_blob.create(inner_w, inner_h, bottom_blob.c, elemsize, opt.workspace_allocator); |
| 1084 | if (inner_bottom_blob.empty()) |
| 1085 | return -100; |
| 1086 | |
| 1087 | inner_top_blob.create(inner_outw, inner_outh, num_output, elemsize, opt.workspace_allocator); |
| 1088 | if (inner_top_blob.empty()) |
| 1089 | return -100; |
| 1090 | |
| 1091 | #pragma omp parallel for num_threads(opt.num_threads) |
| 1092 | for (int c = 0; c < bottom_blob.c; c++) |
| 1093 | { |
| 1094 | float* outptr = inner_bottom_blob.channel(c); |
| 1095 | |
| 1096 | for (int i = 0; i < inner_h; i++) |
| 1097 | { |
| 1098 | const float* ptr = (const float*)bottom_blob.channel(c) + dilation * i * w + x * w + y; |
| 1099 | for (int j = 0; j < inner_w; j++) |
| 1100 | { |
| 1101 | outptr[j] = ptr[j * dilation]; |
| 1102 | } |
| 1103 | outptr += inner_w; |
| 1104 | } |
| 1105 | } |
| 1106 | |
| 1107 | Option opt_g = opt; |
| 1108 | opt_g.blob_allocator = inner_top_blob.allocator; |
| 1109 | convolution_dilation1->forward(inner_bottom_blob, inner_top_blob, opt_g); |
| 1110 | |
| 1111 | #pragma omp parallel for num_threads(opt.num_threads) |
| 1112 | for (int c = 0; c < num_output; c++) |
| 1113 | { |
| 1114 | float* outptr = (float*)top_blob.channel(c) + x * outw + y; |
| 1115 | for (int i = 0; i < inner_outh; i++) |
| 1116 | { |
| 1117 | const float* ptr = (const float*)inner_top_blob.channel(c) + i * inner_outw; |
| 1118 | for (int j = 0; j < inner_outw; j++) |
| 1119 | { |
| 1120 | outptr[j * dilation] = ptr[j]; |
| 1121 | } |
| 1122 | outptr += dilation * outw; |
| 1123 | } |
| 1124 | } |
| 1125 | } |
| 1126 | } |
| 1127 | |
| 1128 | if (activation) |
| 1129 | { |
| 1130 | activation->forward_inplace(top_blob, opt); |
| 1131 | } |
| 1132 | |
| 1133 | return 0; |
| 1134 | } |
| 1135 | |
| 1136 | } // namespace ncnn |