conv_common.cpp 4.3 KB
Newer Older
Z
zhaojiaying01 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
/* Copyright (c) 2018 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. */
朔-望's avatar
朔-望 已提交
14

H
backup  
hjchen2 已提交
15
#include "operators/kernel/arm/convolution/conv_common.h"
16
#include "operators/math/slidingwindow_utils.h"
H
backup  
hjchen2 已提交
17
#include "operators/math/winograd/winograd_transform.h"
朔-望's avatar
朔-望 已提交
18 19

namespace paddle_mobile {
朔-望's avatar
朔-望 已提交
20 21
namespace operators {

H
backup  
hjchen2 已提交
22
void InitBaseConvKernel(ConvParam<CPU> *param) {
23 24
  bool conv3x3 = param->Filter()->dims()[2] == param->Filter()->dims()[3] &&
                 param->Filter()->dims()[2] == 3;
25 26
  bool conv5x5 = param->Filter()->dims()[2] == param->Filter()->dims()[3] &&
                 param->Filter()->dims()[2] == 5;
27 28
  bool depth3x3 = conv3x3 && param->Groups() == param->Input()->dims()[1] &&
                  param->Input()->dims()[1] == param->Output()->dims()[1];
H
backup  
hjchen2 已提交
29

30 31
  bool depth5x5 = conv5x5 && param->Groups() == param->Input()->dims()[1] &&
                  param->Input()->dims()[1] == param->Output()->dims()[1];
32

33
  if (param->Filter()->type() == type_id<int8_t>().hash_code()) {
34
#ifndef __aarch64__
35
    if (depth3x3 && param->Strides()[0] < 3 &&
36
        param->Strides()[0] == param->Strides()[1]) {
H
hjchen2 已提交
37
      param->ExecMode() = ConvParam<CPU>::EXEC_DEPTHWISE3x3_INT8;
38 39 40
    } else if (depth5x5 && param->Strides()[0] < 2 &&
               param->Strides()[0] == param->Strides()[1]) {
      param->ExecMode() = ConvParam<CPU>::EXEC_DEPTHWISE5x5_INT8;
H
hjchen2 已提交
41
    } else {
42
#endif  // __aarch64__
H
hjchen2 已提交
43
      param->ExecMode() = ConvParam<CPU>::EXEC_GEMM_INT8;
44
#ifndef __aarch64__
H
hjchen2 已提交
45
    }
46
#endif  // __aarch64__
H
hjchen2 已提交
47
  } else {
48
    if (depth3x3 && param->Strides()[0] == param->Strides()[1] &&
49 50
        param->Strides()[0] == 1) {
      param->ExecMode() = ConvParam<CPU>::EXEC_DEPTHWISE3x3S1_FLOAT;
51
    } else if (depth3x3 && param->Strides()[0] == param->Strides()[1] &&
52 53
               param->Strides()[0] == 2) {
      param->ExecMode() = ConvParam<CPU>::EXEC_DEPTHWISE3x3S2_FLOAT;
54 55
    } else if (depth5x5 && param->Strides()[0] == param->Strides()[1] &&
               param->Strides()[0] == 1) {
56
      param->ExecMode() = ConvParam<CPU>::EXEC_DEPTHWISE5x5_FLOAT;
H
hjchen2 已提交
57
    } else if (conv3x3 && param->Groups() == 1 &&
58
               param->Strides()[0] == param->Strides()[1] &&
H
hjchen2 已提交
59
               param->Dilations()[0] == param->Dilations()[1] &&
60
               param->Strides()[0] == 1 && param->Dilations()[0] == 1) {
H
hjchen2 已提交
61
      // transform weight
62 63 64
      Variable *transformed_var = param->GetScope()->Var();
      param->transformed_filter_ =
          transformed_var->GetMutable<framework::LoDTensor>();
65 66 67 68 69 70 71 72 73 74
      if (param->Input()->dims()[1] >= 32 && param->Output()->dims()[1] >= 32 &&
          param->Output()->dims()[2] > 16 && param->Output()->dims()[3] > 16) {
        math::winograd_transform_weight<8, 3>(*param->Filter(),
                                              param->transformed_filter_);
        param->ExecMode() = ConvParam<CPU>::EXEC_WINOGRAD3X3_FLOAT;
      } else {
        math::slidingwindow_transform_weight<float>(*param->Filter(),
                                                    param->transformed_filter_);
        param->ExecMode() = ConvParam<CPU>::EXEC_SLIDINGWINDOW3x3S1_FLOAT;
      }
H
update  
hjchen2 已提交
75
    } else if (conv3x3 && param->Groups() == 1 &&
S
StarryRain 已提交
76 77
               param->Strides()[0] == param->Strides()[1] &&
               param->Dilations()[0] == param->Dilations()[1] &&
78 79 80 81 82 83 84
               param->Strides()[0] == 2 && param->Dilations()[0] == 1) {
      // transform weight
      Variable *transformed_var = param->GetScope()->Var();
      param->transformed_filter_ =
          transformed_var->GetMutable<framework::LoDTensor>();
      math::slidingwindow_transform_weight<float>(*param->Filter(),
                                                  param->transformed_filter_);
S
StarryRain 已提交
85
      param->ExecMode() = ConvParam<CPU>::EXEC_SLIDINGWINDOW3x3S2_FLOAT;
H
hjchen2 已提交
86 87 88 89
    } else {
      param->ExecMode() = ConvParam<CPU>::EXEC_GEMM_FLOAT;
    }
  }
朔-望's avatar
朔-望 已提交
90 91
}

朔-望's avatar
朔-望 已提交
92 93
}  // namespace operators
}  // namespace paddle_mobile