conv_kernel.cpp 4.4 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

L
liuruilong 已提交
15 16
#ifdef CONV_OP

朔-望's avatar
朔-望 已提交
17
#include "operators/kernel/conv_kernel.h"
H
hjchen2 已提交
18
#include <iostream>
19
#include "operators/kernel/central-arm-func/conv_arm_func.h"
朔-望's avatar
朔-望 已提交
20 21

namespace paddle_mobile {
朔-望's avatar
朔-望 已提交
22 23
namespace operators {

L
liuruilong 已提交
24
template <>
N
nhzlx 已提交
25
bool ConvKernel<CPU, float>::Init(ConvParam<CPU> *param) {
H
hjchen2 已提交
26 27 28 29 30 31 32 33 34
  if (param->Filter()->type() == typeid(int8_t)) {
    if (param->Groups() == param->Input()->dims()[1] &&
        param->Input()->dims()[1] == param->Output()->dims()[1] &&
        param->Filter()->dims()[2] == param->Filter()->dims()[3] &&
        param->Filter()->dims()[2] == 3) {
      param->ExecMode() = ConvParam<CPU>::EXEC_DEPTHWISE3x3_INT8;
    } else {
      param->ExecMode() = ConvParam<CPU>::EXEC_GEMM_INT8;
    }
H
hjchen2 已提交
35 36 37 38 39 40 41 42 43 44 45
  } else {
    if (param->Groups() == param->Input()->dims()[1] &&
        param->Input()->dims()[1] == param->Output()->dims()[1] &&
        param->Filter()->dims()[2] == param->Filter()->dims()[3] &&
        param->Filter()->dims()[2] == 3 && param->Strides()[0] == 1) {
      param->ExecMode() = ConvParam<CPU>::EXEC_DEPTHWISE3x3S1P1_FLOAT;
    } else if (param->Groups() == param->Input()->dims()[1] &&
               param->Input()->dims()[1] == param->Output()->dims()[1] &&
               param->Filter()->dims()[2] == param->Filter()->dims()[3] &&
               param->Filter()->dims()[2] == 3) {
      param->ExecMode() = ConvParam<CPU>::EXEC_DEPTHWISE3x3_FLOAT;
H
hjchen2 已提交
46
#ifndef __aarch64__
H
hjchen2 已提交
47 48 49 50 51
    } else if (param->Filter()->dims()[2] == param->Filter()->dims()[3] &&
               param->Strides()[0] == param->Strides()[1] &&
               param->Dilations()[0] == param->Dilations()[1] &&
               param->Filter()->dims()[2] == 3 && param->Strides()[0] == 1 &&
               param->Dilations()[0] == 1 && param->Output()->dims()[1] >= 16 &&
52 53
               param->Input()->dims()[1] >= 16 &&
               param->Input()->dims()[2] <= 140 /* refered from ncnn */) {
H
hjchen2 已提交
54 55 56 57 58 59
      param->ExecMode() = ConvParam<CPU>::EXEC_WINOGRAD3X3_FLOAT;
      // transform weight
      framework::Tensor *transformed_weight = new framework::Tensor;
      operators::math::winograd_transform_weight<8, 3>(*param->Filter(),
                                                       transformed_weight);
      param->Filter() = transformed_weight;
H
hjchen2 已提交
60
#endif
H
hjchen2 已提交
61 62 63 64
    } else {
      param->ExecMode() = ConvParam<CPU>::EXEC_GEMM_FLOAT;
    }
  }
L
liuruilong 已提交
65 66 67
  return true;
}

朔-望's avatar
朔-望 已提交
68
template <>
L
liuruilong 已提交
69
void ConvKernel<CPU, float>::Compute(const ConvParam<CPU> &param) {
H
hjchen2 已提交
70 71 72
  switch (param.ExecMode()) {
    case ConvParam<CPU>::EXEC_GEMM_INT8:
      GemmConv<int8_t, int32_t>(param);
H
hjchen2 已提交
73 74 75 76 77
      std::cout << "EXEC_GEMM_INT8" << std::endl;
      break;
    case ConvParam<CPU>::EXEC_DEPTHWISE3x3_INT8:
      DepthwiseConv3x3<int8_t, int32_t>(param);
      std::cout << "EXEC_DEPTHWISE3x3_INT8" << std::endl;
H
hjchen2 已提交
78 79 80 81
      break;
    case ConvParam<CPU>::EXEC_DEPTHWISE3x3S1P1_FLOAT:
      math::DepthwiseConv3x3s1p1(param.Input(), param.Filter(), param.Output(),
                                 nullptr, false);
H
hjchen2 已提交
82
      std::cout << "EXEC_DEPTHWISE3x3S1P1_FLOAT" << std::endl;
H
hjchen2 已提交
83 84 85 86
      break;
    case ConvParam<CPU>::EXEC_DEPTHWISE3x3_FLOAT:
      math::DepthwiseConv3x3(param.Input(), param.Strides(), param.Paddings(),
                             param.Filter(), nullptr, param.Output(), false);
H
hjchen2 已提交
87 88
      std::cout << "EXEC_DEPTHWISE3x3_FLOAT=" << param.Strides()[0]
                << std::endl;
H
hjchen2 已提交
89 90 91
      break;
    case ConvParam<CPU>::EXEC_WINOGRAD3X3_FLOAT:
      WinogradConv3x3<8, 3>(param);
H
hjchen2 已提交
92
      std::cout << "EXEC_WINOGRAD3X3_FLOAT" << std::endl;
H
hjchen2 已提交
93 94 95
      break;
    case ConvParam<CPU>::EXEC_GEMM_FLOAT:
      GemmConv<float, float>(param);
H
hjchen2 已提交
96
      std::cout << "EXEC_GEMM_FLOAT" << std::endl;
H
hjchen2 已提交
97 98 99 100 101
      break;
    default:
      PADDLE_MOBILE_THROW_EXCEPTION("Invalid convolution execute mode %d",
                                    param.ExecMode());
  }
朔-望's avatar
朔-望 已提交
102 103
}

104
template class ConvKernel<CPU, float>;
朔-望's avatar
朔-望 已提交
105

朔-望's avatar
朔-望 已提交
106 107
}  // namespace operators
}  // namespace paddle_mobile
L
liuruilong 已提交
108 109

#endif