conv_add_kernel.cpp 3.1 KB
Newer Older
L
liuruilong 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/* 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. */

#ifdef FUSION_CONVADD_OP

#include "operators/kernel/conv_add_kernel.h"
Z
zhaojiaying01 已提交
18
#include "operators/kernel/cl/cl-kernel-func/conv_func.h"
L
liuruilong 已提交
19 20 21

namespace paddle_mobile {
namespace operators {
xiebaiyuan's avatar
xiebaiyuan 已提交
22
bool optimise_convadd = true;
L
liuruilong 已提交
23 24 25

template <>
bool ConvAddKernel<GPU_CL, float>::Init(FusionConvAddParam<GPU_CL> *param) {
L
liuruilong 已提交
26
  PADDLE_MOBILE_ENFORCE(
L
liuruilong 已提交
27
      param->Filter()->dims()[2] == param->Filter()->dims()[3] &&
L
liuruilong 已提交
28
          param->Paddings()[0] == param->Paddings()[1],
L
liuruilong 已提交
29
      "need equal");
L
liuruilong 已提交
30 31
  param->Bias()->InitCLImage(cl_helper_.CLContext(),
                             this->cl_helper_.CLCommandQueue());
L
liuruilong 已提交
32

L
liuruilong 已提交
33 34 35 36
  int offset = static_cast<int>(param->Filter()->dims()[2]) / 2 -
               static_cast<int>(param->Paddings()[1]);
  param->SetOffset(offset);

L
liuruilong 已提交
37 38 39
  if (param->Filter()->dims()[2] == 1 && param->Filter()->dims()[3] == 1) {
    param->Filter()->InitNImage(cl_helper_.CLContext(),
                                cl_helper_.CLCommandQueue());
xiebaiyuan's avatar
xiebaiyuan 已提交
40 41 42 43 44
    if (optimise_convadd) {
      this->cl_helper_.AddKernel("conv_1x1_spl", "conv_add_kernel.cl");
    } else {
      this->cl_helper_.AddKernel("conv_1x1", "conv_add_kernel.cl");
    }
Y
yangfei 已提交
45 46 47 48
  } else if (param->Filter()->dims()[1] == 1 &&
             param->Input()->dims()[1] == param->Output()->dims()[1] &&
             param->Filter()->dims()[2] == 3) {
    param->Filter()->InitDWImage(cl_helper_.CLContext(),
L
liuruilong 已提交
49
                                 cl_helper_.CLCommandQueue());
Y
yangfei 已提交
50
    this->cl_helper_.AddKernel("depth_conv_3x3", "conv_add_kernel.cl");
L
liuruilong 已提交
51 52 53

  } else if (param->Filter()->dims()[2] == 3 &&
             param->Filter()->dims()[3] == 3) {
Z
zhaojiaying01 已提交
54
    param->Filter()->InitCLImage(cl_helper_.CLContext(),
L
liuruilong 已提交
55 56
                                 cl_helper_.CLCommandQueue());

Y
yangfei 已提交
57
    this->cl_helper_.AddKernel("conv_3x3", "conv_add_kernel.cl");
L
liuruilong 已提交
58

Y
yangfei 已提交
59 60 61 62 63 64 65 66 67 68
  } else if (param->Filter()->dims()[2] == 7 &&
             param->Filter()->dims()[3] == 7) {
    param->Filter()->InitCLImage(cl_helper_.CLContext(),
                                 cl_helper_.CLCommandQueue());
    this->cl_helper_.AddKernel("conv_7x7", "conv_add_kernel.cl");
  } else if (param->Filter()->dims()[2] == 5 &&
             param->Filter()->dims()[3] == 5) {
    param->Filter()->InitCLImage(cl_helper_.CLContext(),
                                 cl_helper_.CLCommandQueue());
    this->cl_helper_.AddKernel("conv_5x5", "conv_add_kernel.cl");
L
liuruilong 已提交
69 70
  }

L
liuruilong 已提交
71 72 73 74 75
  return true;
}

template <>
void ConvAddKernel<GPU_CL, float>::Compute(
L
liuruilong 已提交
76
    const FusionConvAddParam<GPU_CL> &param) {
Z
zhaojiaying01 已提交
77
  ConvAddBnRelu(this->cl_helper_, param, false, param.Bias());
L
liuruilong 已提交
78
}
L
liuruilong 已提交
79 80 81 82 83 84 85

template class ConvAddKernel<GPU_CL, float>;

}  // namespace operators
}  // namespace paddle_mobile

#endif