conv_compute.cc 4.2 KB
Newer Older
T
tensor-tang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
// 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.

#include "paddle/fluid/lite/kernels/arm/conv_compute.h"
#include "paddle/fluid/lite/core/op_registry.h"
#include "paddle/fluid/lite/core/type_system.h"

namespace paddle {
namespace lite {
namespace kernels {
namespace arm {

24
void ConvCompute::PrepareForRun() {
T
tensor-tang 已提交
25 26 27 28 29 30
  auto& param = this->Param<param_t>();
  auto x_dims = param.x->dims();
  auto w_dims = param.filter->dims();
  auto o_dims = param.output->dims();

  auto& ctx = this->ctx_->template As<ARMContext>();
T
tensor-tang 已提交
31 32
  // TODO(xxx): make api and expose it
  ctx.SetRunMode(LITE_POWER_HIGH, 4);
T
tensor-tang 已提交
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

  int win = x_dims[3];  // nchw
  int hin = x_dims[2];
  int ic = x_dims[1];
  int bs = x_dims[0];
  int ow = o_dims[3];
  int oh = o_dims[2];
  int oc = o_dims[1];
  int kh = w_dims[2];  // oihw
  int kw = w_dims[3];
  int pad = param.paddings[0];
  int stride = param.strides[0];

  const auto* i_data = param.x->data<float>();
  const auto* w_data = param.filter->data<float>();
  const auto* b_data = param.bias ? param.bias->data<float>() : nullptr;
  auto* o_data = param.output->mutable_data<float>();

  bool kps_equal = (param.paddings[0] == param.paddings[1]) &&
                   (param.strides[0] == param.strides[1]) && (kw == kh);
  bool no_dilation = (param.dilations[0] == 1) && (param.dilations[1] == 1);
  bool flag_dw_3x3 =
      (kw == 3 && (pad == 0 || pad == 1) && (stride == 1 || stride == 2));
  bool flag_dw_5x5 =
      (kw == 5 && stride == 1) || (kw == 5 && stride == 2 && pad == 2);
  bool flag_dw = flag_dw_3x3 || flag_dw_5x5;

  // select conv impl
  if (param.groups == ic && ic == oc && kps_equal && no_dilation && flag_dw) {
    // dw conv impl
T
tensor-tang 已提交
63
    impl_ = new lite::arm::math::DepthwiseConv<PRECISION(kFloat)>;
64
    VLOG(3) << "invoking dw conv";
T
tensor-tang 已提交
65 66 67 68
  } else if (param.groups == 1 && kw == 3 && stride == 1 && kps_equal &&
             no_dilation) {
    if (ic >= 32 && oc >= 32 && oh > 16 && ow > 16) {
      // winograd conv impl
69
      impl_ = new lite::arm::math::WinogradConv<PRECISION(kFloat)>;
70
      VLOG(3) << "invoking winograd conv";
T
tensor-tang 已提交
71 72 73
    } else {
      // direct conv impl
      impl_ = new lite::arm::math::DirectConv<PRECISION(kFloat)>;
74
      VLOG(3) << "invoking direct conv";
T
tensor-tang 已提交
75 76 77 78 79
    }
  } else if (param.groups == 1 && kw == 3 && stride == 2 && kps_equal &&
             no_dilation) {
    // direct conv impl
    impl_ = new lite::arm::math::DirectConv<PRECISION(kFloat)>;
80
    VLOG(3) << "invoking direct conv";
T
tensor-tang 已提交
81
  } else {
T
tensor-tang 已提交
82
    impl_ = new lite::arm::math::GemmLikeConv<PRECISION(kFloat)>;
83
    VLOG(3) << "invoking gemm like conv";
T
tensor-tang 已提交
84
  }
85 86
  CHECK(this->impl_->create(param, &ctx));
}
T
tensor-tang 已提交
87

88 89
void ConvCompute::Run() {
  auto& param = this->Param<param_t>();
T
tensor-tang 已提交
90 91 92 93 94 95 96 97 98 99 100 101
  CHECK(impl_);
  impl_->run(param);
  // if (this->act_ != nullptr) {
  //   this->act_->run(outputs, outputs, param.activation_param);
  // }
}

}  // namespace arm
}  // namespace kernels
}  // namespace lite
}  // namespace paddle

T
tensor-tang 已提交
102 103 104
REGISTER_LITE_KERNEL(conv2d, kARM, kFloat, kNCHW,
                     paddle::lite::kernels::arm::ConvCompute, def)
    .BindInput("Input", {LiteType::GetTensorTy(TARGET(kARM))})
Z
zhupengyang 已提交
105
    //  .BindInput("Bias", {LiteType::GetTensorTy(TARGET(kARM))})
T
tensor-tang 已提交
106
    .BindInput("Filter", {LiteType::GetTensorTy(TARGET(kARM))})
Z
zhupengyang 已提交
107
    .BindOutput("Output", {LiteType::GetTensorTy(TARGET(kARM))})
T
tensor-tang 已提交
108 109 110
    .Finalize();

REGISTER_LITE_KERNEL(depthwise_conv2d, kARM, kFloat, kNCHW,
T
tensor-tang 已提交
111 112
                     paddle::lite::kernels::arm::ConvCompute, def)
    .BindInput("Input", {LiteType::GetTensorTy(TARGET(kARM))})
Z
zhupengyang 已提交
113
    //    .BindInput("Bias", {LiteType::GetTensorTy(TARGET(kARM))})
T
tensor-tang 已提交
114
    .BindInput("Filter", {LiteType::GetTensorTy(TARGET(kARM))})
Z
zhupengyang 已提交
115
    .BindOutput("Output", {LiteType::GetTensorTy(TARGET(kARM))})
T
tensor-tang 已提交
116
    .Finalize();