conv_impl.cc 40.0 KB
Newer Older
Y
Yan Chunwei 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

15
#include "lite/backends/arm/math/conv_impl.h"
Y
Yan Chunwei 已提交
16
#include <arm_neon.h>
17
#include "lite/backends/arm/math/conv_depthwise.h"
18 19 20 21
#include "lite/backends/arm/math/gemm_prepacked_int8.h"
#include "lite/backends/arm/math/gemv_arm_int8.h"
#include "lite/backends/arm/math/packed_sgemm.h"
#include "lite/backends/arm/math/sgemv.h"
Y
Yan Chunwei 已提交
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
#include "lite/core/context.h"
#include "lite/core/target_wrapper.h"
#include "lite/operators/op_params.h"

namespace paddle {
namespace lite {
namespace arm {
namespace math {

/**
 * \brief neon implementation to add bias
 * @param tensor
 * @param bias
 * @param channel
 * @param channel_size
 */
void fill_bias(float* tensor,
               const float* bias,
               int channel,
               int channel_size) {
  if (tensor == nullptr) {
    return;
  }
  float* data = tensor;

  for (int j = 0; j < channel; ++j) {
    float32x4_t vdata = vdupq_n_f32(bias[j]);
    int i = 0;
    for (; i < channel_size - 3; i += 4) {
      vst1q_f32(data + i, vdata);
    }
    for (; i < channel_size; i++) {
      data[i] = bias[j];
    }
    data += channel_size;
  }
}

void fill_bias_int8(int* tensor,
                    const int* bias,
                    int channel,
                    int channel_size) {
  if (tensor == nullptr) {
    return;
  }
  int* data = tensor;
  for (int j = 0; j < channel; ++j) {
    int32x4_t vdata = vdupq_n_s32(bias[j]);
    int i = 0;
    for (; i < channel_size - 3; i += 4) {
      vst1q_s32(data + i, vdata);
    }
    for (; i < channel_size; i++) {
      data[i] = bias[j];
    }
    data += channel_size;
  }
}

/**
 * \brief inline funcs used in im2col
 * @param a
 * @param b
 * @return
 */
inline bool is_a_ge_zero_and_a_lt_b(int a, int b) {
  return static_cast<unsigned>(a) < static_cast<unsigned>(b);
}

/**
 * \brief normal im2col function for gemm conv
 * @tparam dtype
 * @param data_im
 * @param channels
 * @param height
 * @param width
 * @param kernel_size
 * @param pad
 * @param stride
 * @param data_col
 */
template <typename Dtype>
void im2col(const Dtype* data_im,
105 106 107 108 109
            int channels,
            int height,
            int width,
            int kernel_h,
            int kernel_w,
H
HappyAngel 已提交
110 111 112 113
            int pad_top,
            int pad_bottom,
            int pad_left,
            int pad_right,
114 115 116 117
            int stride_h,
            int stride_w,
            int dilation_h,
            int dilation_w,
Y
Yan Chunwei 已提交
118 119
            Dtype* data_col) {
  const int output_h =
H
HappyAngel 已提交
120 121 122
      (height + pad_top + pad_bottom - (dilation_h * (kernel_h - 1) + 1)) /
          stride_h +
      1;
Y
Yan Chunwei 已提交
123
  const int output_w =
H
HappyAngel 已提交
124 125 126
      (width + pad_left + pad_right - (dilation_w * (kernel_w - 1) + 1)) /
          stride_w +
      1;
Y
Yan Chunwei 已提交
127 128 129 130
  const int channel_size = height * width;
  for (int channel = channels; channel--; data_im += channel_size) {
    for (int kernel_row = 0; kernel_row < kernel_h; kernel_row++) {
      for (int kernel_col = 0; kernel_col < kernel_w; kernel_col++) {
H
HappyAngel 已提交
131
        int input_row = -pad_top + kernel_row * dilation_h;
Y
Yan Chunwei 已提交
132 133 134 135 136 137
        for (int output_rows = output_h; output_rows; output_rows--) {
          if (!is_a_ge_zero_and_a_lt_b(input_row, height)) {
            for (int output_cols = output_w; output_cols; output_cols--) {
              *(data_col++) = 0;
            }
          } else {
H
HappyAngel 已提交
138
            int input_col = -pad_left + kernel_col * dilation_w;
Y
Yan Chunwei 已提交
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
            for (int output_col = output_w; output_col; output_col--) {
              if (is_a_ge_zero_and_a_lt_b(input_col, width)) {
                *(data_col++) = data_im[input_row * width + input_col];
              } else {
                *(data_col++) = 0;
              }
              input_col += stride_w;
            }
          }
          input_row += stride_h;
        }
      }
    }
  }
}

/**
 * \brief convolution function for kernel size 1x1, stride size 1, gemm
 * implementation
 */
void conv1x1s1_gemm(const float* i_data,
                    float* o_data,
                    int num,
                    int oc,
                    int oh,
                    int ow,
                    int ic,
                    int ih,
                    int win,
                    const float* weights,
                    const float* bias,
                    const operators::ConvParam& param,
171
                    ARMContext* ctx) {
Y
Yan Chunwei 已提交
172 173 174 175 176 177 178 179 180 181
  int channel_size_out = ow * oh;
  int channel_size_in = win * ih;

  const int group = param.groups;
  const int m = oc / group;
  const int n = oh * ow;
  const int k = ic / group;

  bool flag_relu = param.fuse_relu;
  bool flag_bias = param.bias != nullptr;
182

183 184
  auto act_param = param.activation_param;

185
  int hblock = get_hblock(ctx);
Y
Yan Chunwei 已提交
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
  int m_roundup = hblock * ((m + hblock - 1) / hblock);
  int weights_size_per_group = m * k;
  if (n > 1) {
    weights_size_per_group = ((m_roundup * k + 15) / 16) * 16;
  }
  //! use gemv when the output channel size = 1
  for (int b = 0; b < num; ++b) {
    // dC
    for (int g = 0; g < group; ++g) {
      float* dout_group =
          static_cast<float*>(o_data) + (b * oc + g * m) * channel_size_out;
      const float* din_group = static_cast<const float*>(i_data) +
                               (b * ic + g * k) * channel_size_in;
      const float* weights_group =
          static_cast<const float*>(weights) + g * weights_size_per_group;
      const float* bias_group = static_cast<const float*>(bias) + g * m;

      if (n == 1) {
        sgemv(weights_group,
              din_group,
              dout_group,
              false,
              m,
              k,
              flag_bias,
              bias_group,
212 213 214 215 216
              act_param.has_active,
              act_param.active_type,
              ctx,
              act_param.Relu_clipped_coef,
              act_param.Leaky_relu_alpha);
Y
Yan Chunwei 已提交
217 218 219 220 221 222 223 224 225 226 227 228 229
      } else {
        sgemm_prepack(false,
                      m,
                      n,
                      k,
                      weights_group,
                      din_group,
                      n,
                      0.f,
                      dout_group,
                      n,
                      bias_group,
                      flag_bias,
230
                      act_param,
Y
Yan Chunwei 已提交
231 232 233 234 235 236
                      ctx);
      }
    }
  }
}

237
template <typename Dtype>
Y
Yan Chunwei 已提交
238
void conv1x1s1_gemm_int8(const int8_t* i_data,
239
                         Dtype* o_data,
Y
Yan Chunwei 已提交
240 241 242 243 244 245 246 247
                         int num,
                         int oc,
                         int oh,
                         int ow,
                         int ic,
                         int ih,
                         int win,
                         const int8_t* weights,
248
                         const float* bias,
Y
Yan Chunwei 已提交
249 250
                         const operators::ConvParam& param,
                         ARMContext* ctx,
251
                         const float* scale) {
Y
Yan Chunwei 已提交
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
  int group = param.groups;
  int channel_size_out = ow * oh;
  int channel_size_in = win * ih;
  const int m = oc / group;
  const int n = oh * ow;
  const int k = ic / group;
  int hblock = get_hblock_int8(ctx);
  int k_roundup = ROUNDUP(k, KBLOCK_INT8);
  int m_roundup = ROUNDUP(m, hblock);
  int weights_size_per_group = m * k;
  if (n > 1) {
    weights_size_per_group = ((m_roundup * k_roundup + 15) / 16) * 16;
  }
  bool flag_relu = param.fuse_relu;
  bool flag_bias = param.bias != nullptr;
267
  auto act_param = param.activation_param;
Y
Yan Chunwei 已提交
268 269 270 271
  //! use gemv when the output channel size = 1
  for (int b = 0; b < num; ++b) {
    // dC
    for (int g = 0; g < group; ++g) {
272
      Dtype* dout_group = o_data + (b * oc + g * m) * channel_size_out;
Y
Yan Chunwei 已提交
273 274
      const int8_t* din_group = i_data + (b * ic + g * k) * channel_size_in;
      const int8_t* weights_group = weights + g * weights_size_per_group;
275
      const float* bias_group = bias + g * m;
Y
Yan Chunwei 已提交
276 277
      const float* scale_group = scale + g * m;
      if (n == 1) {
278 279 280 281 282 283 284 285 286
        gemv_int8(weights_group,
                  din_group,
                  dout_group,
                  false,
                  m,
                  k,
                  scale_group,
                  flag_bias,
                  bias_group,
287 288 289 290 291
                  act_param.has_active,
                  act_param.active_type,
                  ctx,
                  act_param.Relu_clipped_coef,
                  act_param.Leaky_relu_alpha);
Y
Yan Chunwei 已提交
292
      } else {
293 294 295 296 297 298 299 300 301 302
        gemm_prepack_int8(weights_group,
                          din_group,
                          bias_group,
                          dout_group,
                          m,
                          n,
                          k,
                          flag_bias,
                          false,
                          scale_group,
303
                          act_param,
304
                          ctx);
Y
Yan Chunwei 已提交
305 306 307 308 309
      }
    }
  }
}

310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
template void conv1x1s1_gemm_int8<int8_t>(const int8_t* i_data,
                                          int8_t* o_data,
                                          int num,
                                          int oc,
                                          int oh,
                                          int ow,
                                          int ic,
                                          int ih,
                                          int win,
                                          const int8_t* weights,
                                          const float* bias,
                                          const operators::ConvParam& param,
                                          ARMContext* ctx,
                                          const float* scale);

template void conv1x1s1_gemm_int8<float>(const int8_t* i_data,
                                         float* o_data,
                                         int num,
                                         int oc,
                                         int oh,
                                         int ow,
                                         int ic,
                                         int ih,
                                         int win,
                                         const int8_t* weights,
                                         const float* bias,
                                         const operators::ConvParam& param,
                                         ARMContext* ctx,
                                         const float* scale);

Y
Yan Chunwei 已提交
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
/**
 * \brief convolution function for kernel size 3x3, stride size 2, gemm
 * implementation
 */
void conv_im2col_gemm(const float* i_data,
                      float* o_data,
                      int num,
                      int oc,
                      int oh,
                      int ow,
                      int ic,
                      int ih,
                      int win,
                      const float* weights,
                      const float* bias,
                      const operators::ConvParam& param,
356
                      ARMContext* ctx) {
Y
Yan Chunwei 已提交
357 358 359 360 361 362 363 364 365 366 367 368
  const int group = param.groups;
  auto filter_dims = param.filter->dims();
  const int kernel_h = filter_dims[2];
  const int kernel_w = filter_dims[3];  // nchw
  const int m = oc / group;
  const int n = oh * ow;
  const int k = ic * kernel_h * kernel_w / group;
  const int chin_per_group = ic / group;
  int channel_size_out = ow * oh;
  int channel_size_in = win * ih;
  bool flag_relu = param.fuse_relu;
  bool flag_bias = param.bias != nullptr;
369
  int hblock = get_hblock(ctx);
Y
Yan Chunwei 已提交
370 371
  int m_roundup = hblock * ((m + hblock - 1) / hblock);
  int weights_size_per_group = m * k;
372 373

  auto act_param = param.activation_param;
Y
Yan Chunwei 已提交
374 375 376 377 378 379
  if (n > 1) {
    weights_size_per_group = ((m_roundup * k + 15) / 16) * 16;
  }

  float* tmp_work_space =
      ctx->workspace_data<float>() + ctx->llc_size() / sizeof(float);
H
HappyAngel 已提交
380 381 382

  auto paddings = *param.paddings;
  auto dilations = *param.dilations;
Y
Yan Chunwei 已提交
383 384 385 386 387 388 389 390 391 392 393
  //! use gemv when the output channel size = 1
  for (int b = 0; b < num; ++b) {
    // dC
    for (int g = 0; g < group; ++g) {
      float* dout_group = o_data + (b * oc + g * m) * channel_size_out;
      const float* din_group =
          i_data + (b * ic + g * chin_per_group) * channel_size_in;
      const float* weights_group = weights + g * weights_size_per_group;
      const float* bias_group = bias + g * m;
      float* dB = tmp_work_space;

394 395 396 397 398 399
      im2col(din_group,
             chin_per_group,
             ih,
             win,
             kernel_h,
             kernel_w,
H
HappyAngel 已提交
400 401 402 403
             paddings[0],
             paddings[1],
             paddings[2],
             paddings[3],
404 405
             param.strides[0],
             param.strides[1],
H
HappyAngel 已提交
406 407
             dilations[0],
             dilations[1],
408 409
             dB);

Y
Yan Chunwei 已提交
410 411 412 413 414 415 416 417 418
      if (n == 1) {
        sgemv(weights_group,
              dB,
              dout_group,
              false,
              m,
              k,
              flag_bias,
              bias_group,
419 420 421 422 423
              act_param.has_active,
              act_param.active_type,
              ctx,
              act_param.Relu_clipped_coef,
              act_param.Leaky_relu_alpha);
Y
Yan Chunwei 已提交
424 425
      } else {
        int ldb = n;
426
        sgemm_prepack(false,
Y
Yan Chunwei 已提交
427 428 429 430 431 432 433 434 435 436 437
                      m,
                      n,
                      k,
                      weights_group,
                      dB,
                      ldb,
                      0.f,
                      dout_group,
                      n,
                      bias_group,
                      flag_bias,
438
                      act_param,
Y
Yan Chunwei 已提交
439 440 441 442 443 444
                      ctx);
      }
    }
  }
}

445
template <typename Dtype>
Y
Yan Chunwei 已提交
446
void conv_im2col_gemm_int8(const int8_t* i_data,
447
                           Dtype* o_data,
Y
Yan Chunwei 已提交
448 449 450 451 452 453 454 455
                           int num,
                           int oc,
                           int oh,
                           int ow,
                           int ic,
                           int ih,
                           int win,
                           const int8_t* weights,
456
                           const float* bias,
Y
Yan Chunwei 已提交
457 458
                           const operators::ConvParam& param,
                           ARMContext* ctx,
459
                           const float* scale) {
Y
Yan Chunwei 已提交
460 461
  int group = param.groups;
  auto filter_dims = param.filter->dims();
H
HappyAngel 已提交
462 463
  auto paddings = *param.paddings;
  auto dilations = *param.dilations;
Y
Yan Chunwei 已提交
464 465 466 467
  int kernel_h = filter_dims[2];
  int kernel_w = filter_dims[3];
  int stride_h = param.strides[0];
  int stride_w = param.strides[1];
H
HappyAngel 已提交
468 469 470 471
  int dila_h = dilations[0];
  int dila_w = dilations[1];
  int pad_h = paddings[0];
  int pad_w = paddings[2];
Y
Yan Chunwei 已提交
472 473 474 475 476 477 478 479 480
  const int m = oc / group;
  const int n = oh * ow;
  const int k = ic * kernel_h * kernel_w / group;
  const int chin_per_group = ic / group;
  int channel_size_out = ow * oh;
  int channel_size_in = win * ih;
  bool flag_relu = param.fuse_relu;
  bool flag_bias = param.bias != nullptr;

481 482
  auto act_param = param.activation_param;

Y
Yan Chunwei 已提交
483 484 485 486 487 488 489 490 491 492 493 494 495 496 497
  int hblock = get_hblock_int8(ctx);
  int k_roundup = ROUNDUP(k, KBLOCK_INT8);
  int m_roundup = ROUNDUP(m, hblock);
  int weights_size_per_group = m * k;
  if (n > 1) {
    weights_size_per_group = ((m_roundup * k_roundup + 15) / 16) * 16;
  }

  int8_t* tmp_work_space =
      ctx->workspace_data<int8_t>() + ctx->llc_size() / sizeof(int8_t);

  //! use gemv when the output channel size = 1
  for (int b = 0; b < num; ++b) {
    // dC
    for (int g = 0; g < group; ++g) {
498
      Dtype* dout_group = o_data + (b * oc + g * m) * channel_size_out;
Y
Yan Chunwei 已提交
499 500 501 502
      const int8_t* din_group = static_cast<const int8_t*>(i_data) +
                                (b * ic + g * chin_per_group) * channel_size_in;
      const int8_t* weights_group =
          static_cast<const int8_t*>(weights) + g * weights_size_per_group;
503
      const float* bias_group = bias + g * m;
Y
Yan Chunwei 已提交
504 505 506
      int8_t* dB = tmp_work_space;
      const float* scale_group = scale + g * m;

507 508 509 510 511 512 513
      im2col(din_group,
             chin_per_group,
             ih,
             win,
             kernel_h,
             kernel_w,
             pad_h,
H
HappyAngel 已提交
514
             paddings[1],
515
             pad_w,
H
HappyAngel 已提交
516
             paddings[3],
517 518 519 520 521
             stride_h,
             stride_w,
             dila_h,
             dila_w,
             dB);
Y
Yan Chunwei 已提交
522
      if (n == 1) {
523 524 525 526 527 528 529 530 531
        gemv_int8(weights_group,
                  dB,
                  dout_group,
                  false,
                  m,
                  k,
                  scale_group,
                  flag_bias,
                  bias_group,
532 533 534 535 536
                  act_param.has_active,
                  act_param.active_type,
                  ctx,
                  act_param.Relu_clipped_coef,
                  act_param.Leaky_relu_alpha);
Y
Yan Chunwei 已提交
537
      } else {
538 539 540 541 542 543 544 545 546 547
        gemm_prepack_int8(weights_group,
                          dB,
                          bias_group,
                          dout_group,
                          m,
                          n,
                          k,
                          flag_bias,
                          false,
                          scale_group,
548
                          act_param,
549
                          ctx);
Y
Yan Chunwei 已提交
550 551 552 553 554
      }
    }
  }
}

555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584
template void conv_im2col_gemm_int8<int8_t>(const int8_t* i_data,
                                            int8_t* o_data,
                                            int num,
                                            int oc,
                                            int oh,
                                            int ow,
                                            int ic,
                                            int ih,
                                            int win,
                                            const int8_t* weights,
                                            const float* bias,
                                            const operators::ConvParam& param,
                                            ARMContext* ctx,
                                            const float* scale);

template void conv_im2col_gemm_int8<float>(const int8_t* i_data,
                                           float* o_data,
                                           int num,
                                           int oc,
                                           int oh,
                                           int ow,
                                           int ic,
                                           int ih,
                                           int win,
                                           const int8_t* weights,
                                           const float* bias,
                                           const operators::ConvParam& param,
                                           ARMContext* ctx,
                                           const float* scale);

585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600
template void im2col<float>(const float* data_im,
                            int channels,
                            int height,
                            int width,
                            int kernel_h,
                            int kernel_w,
                            int pad_top,
                            int pad_bottom,
                            int pad_left,
                            int pad_right,
                            int stride_h,
                            int stride_w,
                            int dilation_h,
                            int dilation_w,
                            float* data_col);

601 602 603 604 605 606 607 608 609 610 611 612 613 614
void conv_depthwise_3x3_fp32(const void* din,
                             void* dout,
                             int num,
                             int ch_out,
                             int h_out,
                             int w_out,
                             int ch_in,
                             int h_in,
                             int w_in,
                             const void* weights,
                             const float* bias,
                             const operators::ConvParam& param,
                             ARMContext* ctx,
                             const float* scale) {
H
HappyAngel 已提交
615
  auto paddings = *param.paddings;
616
  auto act_param = param.activation_param;
H
HappyAngel 已提交
617 618
  const int pad_h = paddings[0];
  const int pad_w = paddings[2];
Y
Yan Chunwei 已提交
619
  int stride = param.strides[1];
H
HappyAngel 已提交
620 621
  int pad = pad_w;
  bool flag_bias = param.bias != nullptr;
622
  bool pads_less = ((paddings[1] < 2) && (paddings[3] < 2));
H
HappyAngel 已提交
623
  if (stride == 1) {
624
    if (pads_less && (pad_h == pad_w) && (pad < 2)) {  // support pad = [0, 1]
H
HappyAngel 已提交
625 626 627 628 629 630 631 632 633 634 635 636 637
      conv_depthwise_3x3s1_fp32(reinterpret_cast<const float*>(din),
                                reinterpret_cast<float*>(dout),
                                num,
                                ch_out,
                                h_out,
                                w_out,
                                ch_in,
                                h_in,
                                w_in,
                                reinterpret_cast<const float*>(weights),
                                bias,
                                pad,
                                flag_bias,
638
                                act_param,
H
HappyAngel 已提交
639 640 641 642 643 644 645 646 647 648 649 650 651 652
                                ctx);
    } else {
      conv_3x3s1_depthwise_fp32(reinterpret_cast<const float*>(din),
                                reinterpret_cast<float*>(dout),
                                num,
                                ch_out,
                                h_out,
                                w_out,
                                ch_in,
                                h_in,
                                w_in,
                                reinterpret_cast<const float*>(weights),
                                bias,
                                param,
653
                                act_param,
H
HappyAngel 已提交
654 655 656
                                ctx);
    }
  } else if (stride == 2) {
657
    if (pads_less && pad_h == pad_w && (pad < 2)) {  // support pad = [0, 1]
H
HappyAngel 已提交
658 659 660 661 662 663 664 665 666 667 668 669 670
      conv_depthwise_3x3s2_fp32(reinterpret_cast<const float*>(din),
                                reinterpret_cast<float*>(dout),
                                num,
                                ch_out,
                                h_out,
                                w_out,
                                ch_in,
                                h_in,
                                w_in,
                                reinterpret_cast<const float*>(weights),
                                bias,
                                pad,
                                flag_bias,
671
                                act_param,
H
HappyAngel 已提交
672 673 674 675 676 677 678 679 680 681 682 683 684 685
                                ctx);
    } else {
      conv_3x3s2_depthwise_fp32(reinterpret_cast<const float*>(din),
                                reinterpret_cast<float*>(dout),
                                num,
                                ch_out,
                                h_out,
                                w_out,
                                ch_in,
                                h_in,
                                w_in,
                                reinterpret_cast<const float*>(weights),
                                bias,
                                param,
686
                                act_param,
H
HappyAngel 已提交
687 688
                                ctx);
    }
Y
Yan Chunwei 已提交
689
  } else {
H
HappyAngel 已提交
690
    LOG(FATAL) << "fp32 depthwise conv3x3 stride: " << stride << " unsupported";
Y
Yan Chunwei 已提交
691 692 693
  }
}

694 695 696 697 698 699 700 701 702 703 704 705 706 707
void conv_depthwise_5x5_fp32(const void* din,
                             void* dout,
                             int num,
                             int ch_out,
                             int h_out,
                             int w_out,
                             int ch_in,
                             int h_in,
                             int w_in,
                             const void* weights,
                             const float* bias,
                             const operators::ConvParam& param,
                             ARMContext* ctx,
                             const float* scale) {
H
HappyAngel 已提交
708
  auto paddings = *param.paddings;
709
  auto act_param = param.activation_param;
710 711
  int pad_h = paddings[0];
  int pad_w = paddings[2];
Y
Yan Chunwei 已提交
712 713 714
  int stride = param.strides[1];
  bool flag_relu = param.fuse_relu;
  bool flag_bias = param.bias != nullptr;
715
  ctx->ExtendWorkspace((w_in + w_out) * sizeof(float));
716
  if (stride == 2) {
717 718 719 720 721 722 723 724 725 726 727
    conv_depthwise_5x5s2_fp32(reinterpret_cast<const float*>(din),
                              reinterpret_cast<float*>(dout),
                              num,
                              ch_out,
                              h_out,
                              w_out,
                              ch_in,
                              h_in,
                              w_in,
                              reinterpret_cast<const float*>(weights),
                              bias,
728 729
                              param,
                              act_param,
730
                              ctx);
Y
Yan Chunwei 已提交
731
  } else if (stride == 1) {
732 733
    conv_depthwise_5x5s1_fp32(reinterpret_cast<float*>(dout),
                              reinterpret_cast<const float*>(din),
734 735 736 737
                              reinterpret_cast<const float*>(weights),
                              bias,
                              flag_bias,
                              flag_relu,
738 739 740 741 742 743 744 745 746
                              num,
                              ch_in,
                              h_in,
                              w_in,
                              h_out,
                              w_out,
                              pad_w,
                              pad_h,
                              param,
747
                              ctx);
Y
Yan Chunwei 已提交
748 749 750 751 752
  } else {
    LOG(FATAL) << "unsupport this type 5x5 dw conv";
  }
}

753 754 755 756 757 758 759 760 761 762 763 764 765 766
void conv_depthwise_3x3_int8_fp32(const void* din,
                                  void* dout,
                                  int num,
                                  int ch_out,
                                  int h_out,
                                  int w_out,
                                  int ch_in,
                                  int h_in,
                                  int w_in,
                                  const void* weights,
                                  const float* bias,
                                  const operators::ConvParam& param,
                                  ARMContext* ctx,
                                  const float* scale) {
H
HappyAngel 已提交
767 768 769
  auto paddings = *param.paddings;
  int pad_h = paddings[0];
  int pad_w = paddings[2];
770 771
  int stride = param.strides[1];
  bool flag_bias = param.bias != nullptr;
772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794
  auto act_param = param.activation_param;
  auto act_type = act_param.active_type;
  int flag_act = 0;  // relu: 1, relu6: 2, leakey: 3
  float alpha[4] = {0.f, 0.f, 0.f, 0.f};
  if (act_param.has_active) {
    if (act_type == lite_api::ActivationType::kRelu) {
      flag_act = 1;
    } else if (act_type == lite_api::ActivationType::kRelu6) {
      flag_act = 2;
      float local_alpha = act_param.Relu_clipped_coef;
      alpha[0] = local_alpha;
      alpha[1] = local_alpha;
      alpha[2] = local_alpha;
      alpha[3] = local_alpha;
    } else if (act_type == lite_api::ActivationType::kLeakyRelu) {
      flag_act = 3;
      float local_alpha = act_param.Leaky_relu_alpha;
      alpha[0] = local_alpha;
      alpha[1] = local_alpha;
      alpha[2] = local_alpha;
      alpha[3] = local_alpha;
    }
  }
795 796 797 798 799 800
  bool support_act_type = flag_act <= 1;
  bool support_pad_type =
      (paddings[0] == paddings[1]) && (paddings[2] == paddings[3]) &&
      (paddings[0] == paddings[2]) && (paddings[0] == 0 || paddings[0] == 1);
  bool support_stride_type = (param.strides[0] == 1 && param.strides[1] == 1);
  bool support_width_type = w_in > 9 ? true : false;
801
  if (stride == 1) {
802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840
    if (!support_act_type || !support_pad_type || !support_stride_type ||
        !support_width_type) {
      conv_depthwise_3x3s1_int8(reinterpret_cast<float*>(dout),
                                reinterpret_cast<const int8_t*>(din),
                                reinterpret_cast<const int8_t*>(weights),
                                scale,
                                bias,
                                flag_bias,
                                flag_act,
                                alpha,
                                num,
                                ch_in,
                                h_in,
                                w_in,
                                h_out,
                                w_out,
                                pad_w,
                                pad_h,
                                ctx);
    } else {
      conv_depthwise_3x3s1_int8_float_impl(
          reinterpret_cast<float*>(dout),
          reinterpret_cast<const int8_t*>(din),
          reinterpret_cast<const int8_t*>(weights),
          scale,
          bias,
          flag_bias,
          flag_act,
          alpha,
          num,
          ch_in,
          h_in,
          w_in,
          h_out,
          w_out,
          pad_w,
          pad_h,
          ctx);
    }
841 842 843 844 845 846 847
  } else if (stride == 2) {
    conv_depthwise_3x3s2_int8(reinterpret_cast<float*>(dout),
                              reinterpret_cast<const int8_t*>(din),
                              reinterpret_cast<const int8_t*>(weights),
                              scale,
                              bias,
                              flag_bias,
848 849
                              flag_act,
                              alpha,
850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877
                              num,
                              ch_in,
                              h_in,
                              w_in,
                              h_out,
                              w_out,
                              pad_w,
                              pad_h,
                              ctx);
  } else {
    LOG(FATAL) << "unsupport this type 3x3 dw conv int8";
  }
}

void conv_depthwise_3x3_int8_int8(const void* din,
                                  void* dout,
                                  int num,
                                  int ch_out,
                                  int h_out,
                                  int w_out,
                                  int ch_in,
                                  int h_in,
                                  int w_in,
                                  const void* weights,
                                  const float* bias,
                                  const operators::ConvParam& param,
                                  ARMContext* ctx,
                                  const float* scale) {
H
HappyAngel 已提交
878 879 880
  auto paddings = *param.paddings;
  int pad_h = paddings[0];
  int pad_w = paddings[2];
881 882
  int stride = param.strides[1];
  bool flag_bias = param.bias != nullptr;
883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905
  auto act_param = param.activation_param;
  auto act_type = act_param.active_type;
  int flag_act = 0;  // relu: 1, relu6: 2, leakey: 3
  float alpha[4] = {0.f, 0.f, 0.f, 0.f};
  if (act_param.has_active) {
    if (act_type == lite_api::ActivationType::kRelu) {
      flag_act = 1;
    } else if (act_type == lite_api::ActivationType::kRelu6) {
      flag_act = 2;
      float local_alpha = act_param.Relu_clipped_coef;
      alpha[0] = local_alpha;
      alpha[1] = local_alpha;
      alpha[2] = local_alpha;
      alpha[3] = local_alpha;
    } else if (act_type == lite_api::ActivationType::kLeakyRelu) {
      flag_act = 3;
      float local_alpha = act_param.Leaky_relu_alpha;
      alpha[0] = local_alpha;
      alpha[1] = local_alpha;
      alpha[2] = local_alpha;
      alpha[3] = local_alpha;
    }
  }
906 907 908 909 910 911
  bool support_act_type = flag_act <= 1;
  bool support_pad_type =
      (paddings[0] == paddings[1]) && (paddings[2] == paddings[3]) &&
      (paddings[0] == paddings[2]) && (paddings[0] == 0 || paddings[0] == 1);
  bool support_stride_type = (param.strides[0] == 1 && param.strides[1] == 1);
  bool support_width_type = w_in > 9 ? true : false;
912
  if (stride == 1) {
913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951
    if (!support_act_type || !support_pad_type || !support_stride_type ||
        !support_width_type) {
      conv_depthwise_3x3s1_int8(reinterpret_cast<int8_t*>(dout),
                                reinterpret_cast<const int8_t*>(din),
                                reinterpret_cast<const int8_t*>(weights),
                                scale,
                                bias,
                                flag_bias,
                                flag_act,
                                alpha,
                                num,
                                ch_in,
                                h_in,
                                w_in,
                                h_out,
                                w_out,
                                pad_w,
                                pad_h,
                                ctx);
    } else {
      conv_depthwise_3x3s1_int8_int8_impl(
          reinterpret_cast<int8_t*>(dout),
          reinterpret_cast<const int8_t*>(din),
          reinterpret_cast<const int8_t*>(weights),
          scale,
          bias,
          flag_bias,
          flag_act,
          alpha,
          num,
          ch_in,
          h_in,
          w_in,
          h_out,
          w_out,
          pad_w,
          pad_h,
          ctx);
    }
952 953 954 955 956 957 958
  } else if (stride == 2) {
    conv_depthwise_3x3s2_int8(reinterpret_cast<int8_t*>(dout),
                              reinterpret_cast<const int8_t*>(din),
                              reinterpret_cast<const int8_t*>(weights),
                              scale,
                              bias,
                              flag_bias,
959 960
                              flag_act,
                              alpha,
961 962 963 964 965 966 967 968 969 970 971 972 973 974
                              num,
                              ch_in,
                              h_in,
                              w_in,
                              h_out,
                              w_out,
                              pad_w,
                              pad_h,
                              ctx);
  } else {
    LOG(FATAL) << "unsupport this type 3x3 dw conv int8";
  }
}

Y
yiicy 已提交
975 976 977 978 979 980 981 982 983 984 985 986 987 988
void conv_depthwise_5x5_int8_fp32(const void* din,
                                  void* dout,
                                  int num,
                                  int ch_out,
                                  int h_out,
                                  int w_out,
                                  int ch_in,
                                  int h_in,
                                  int w_in,
                                  const void* weights,
                                  const float* bias,
                                  const operators::ConvParam& param,
                                  ARMContext* ctx,
                                  const float* scale) {
H
HappyAngel 已提交
989 990 991
  auto paddings = *param.paddings;
  int pad_h = paddings[0];
  int pad_w = paddings[2];
Y
yiicy 已提交
992 993
  int stride = param.strides[1];
  bool flag_bias = param.bias != nullptr;
994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016
  auto act_param = param.activation_param;
  auto act_type = act_param.active_type;
  int flag_act = 0;  // relu: 1, relu6: 2, leakey: 3
  float alpha[4] = {0.f, 0.f, 0.f, 0.f};
  if (act_param.has_active) {
    if (act_type == lite_api::ActivationType::kRelu) {
      flag_act = 1;
    } else if (act_type == lite_api::ActivationType::kRelu6) {
      flag_act = 2;
      float local_alpha = act_param.Relu_clipped_coef;
      alpha[0] = local_alpha;
      alpha[1] = local_alpha;
      alpha[2] = local_alpha;
      alpha[3] = local_alpha;
    } else if (act_type == lite_api::ActivationType::kLeakyRelu) {
      flag_act = 3;
      float local_alpha = act_param.Leaky_relu_alpha;
      alpha[0] = local_alpha;
      alpha[1] = local_alpha;
      alpha[2] = local_alpha;
      alpha[3] = local_alpha;
    }
  }
Y
yiicy 已提交
1017 1018 1019 1020 1021 1022 1023
  if (stride == 1) {
    conv_depthwise_5x5s1_int8(reinterpret_cast<float*>(dout),
                              reinterpret_cast<const int8_t*>(din),
                              reinterpret_cast<const int8_t*>(weights),
                              scale,
                              bias,
                              flag_bias,
1024 1025
                              flag_act,
                              alpha,
Y
yiicy 已提交
1026 1027 1028 1029 1030 1031 1032 1033 1034
                              num,
                              ch_in,
                              h_in,
                              w_in,
                              h_out,
                              w_out,
                              pad_w,
                              pad_h,
                              ctx);
1035 1036 1037 1038 1039 1040 1041
  } else if (stride == 2) {
    conv_depthwise_5x5s2_int8(reinterpret_cast<float*>(dout),
                              reinterpret_cast<const int8_t*>(din),
                              reinterpret_cast<const int8_t*>(weights),
                              scale,
                              bias,
                              flag_bias,
1042 1043
                              flag_act,
                              alpha,
1044 1045 1046 1047 1048 1049 1050 1051 1052
                              num,
                              ch_in,
                              h_in,
                              w_in,
                              h_out,
                              w_out,
                              pad_w,
                              pad_h,
                              ctx);
Y
yiicy 已提交
1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071
  } else {
    LOG(FATAL) << "unsupport this type 5x5 dw conv int8";
  }
}

void conv_depthwise_5x5_int8_int8(const void* din,
                                  void* dout,
                                  int num,
                                  int ch_out,
                                  int h_out,
                                  int w_out,
                                  int ch_in,
                                  int h_in,
                                  int w_in,
                                  const void* weights,
                                  const float* bias,
                                  const operators::ConvParam& param,
                                  ARMContext* ctx,
                                  const float* scale) {
H
HappyAngel 已提交
1072 1073 1074
  auto paddings = *param.paddings;
  int pad_h = paddings[0];
  int pad_w = paddings[2];
Y
yiicy 已提交
1075 1076
  int stride = param.strides[1];
  bool flag_bias = param.bias != nullptr;
1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099
  auto act_param = param.activation_param;
  auto act_type = act_param.active_type;
  int flag_act = 0;  // relu: 1, relu6: 2, leakey: 3
  float alpha[4] = {0.f, 0.f, 0.f, 0.f};
  if (act_param.has_active) {
    if (act_type == lite_api::ActivationType::kRelu) {
      flag_act = 1;
    } else if (act_type == lite_api::ActivationType::kRelu6) {
      flag_act = 2;
      float local_alpha = act_param.Relu_clipped_coef;
      alpha[0] = local_alpha;
      alpha[1] = local_alpha;
      alpha[2] = local_alpha;
      alpha[3] = local_alpha;
    } else if (act_type == lite_api::ActivationType::kLeakyRelu) {
      flag_act = 3;
      float local_alpha = act_param.Leaky_relu_alpha;
      alpha[0] = local_alpha;
      alpha[1] = local_alpha;
      alpha[2] = local_alpha;
      alpha[3] = local_alpha;
    }
  }
Y
yiicy 已提交
1100 1101 1102 1103 1104 1105 1106
  if (stride == 1) {
    conv_depthwise_5x5s1_int8(reinterpret_cast<int8_t*>(dout),
                              reinterpret_cast<const int8_t*>(din),
                              reinterpret_cast<const int8_t*>(weights),
                              scale,
                              bias,
                              flag_bias,
1107 1108
                              flag_act,
                              alpha,
Y
yiicy 已提交
1109 1110 1111 1112 1113 1114 1115 1116 1117
                              num,
                              ch_in,
                              h_in,
                              w_in,
                              h_out,
                              w_out,
                              pad_w,
                              pad_h,
                              ctx);
1118 1119 1120 1121 1122 1123 1124
  } else if (stride == 2) {
    conv_depthwise_5x5s2_int8(reinterpret_cast<int8_t*>(dout),
                              reinterpret_cast<const int8_t*>(din),
                              reinterpret_cast<const int8_t*>(weights),
                              scale,
                              bias,
                              flag_bias,
1125 1126
                              flag_act,
                              alpha,
1127 1128 1129 1130 1131 1132 1133 1134 1135
                              num,
                              ch_in,
                              h_in,
                              w_in,
                              h_out,
                              w_out,
                              pad_w,
                              pad_h,
                              ctx);
Y
yiicy 已提交
1136 1137 1138 1139 1140
  } else {
    LOG(FATAL) << "unsupport this type 5x5 dw conv int8";
  }
}

Y
Yan Chunwei 已提交
1141 1142 1143 1144
}  // namespace math
}  // namespace arm
}  // namespace lite
}  // namespace paddle