conv_add_kernel.cpp 3.0 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 22 23 24

namespace paddle_mobile {
namespace operators {

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

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

L
liuruilong 已提交
36 37 38
  if (param->Filter()->dims()[2] == 1 && param->Filter()->dims()[3] == 1) {
    param->Filter()->InitNImage(cl_helper_.CLContext(),
                                cl_helper_.CLCommandQueue());
39
    this->cl_helper_.AddKernel("conv_1x1_spl", "conv_add_kernel.cl");
Y
yangfei 已提交
40 41 42 43
  } 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 已提交
44
                                 cl_helper_.CLCommandQueue());
Y
yangfei 已提交
45
    this->cl_helper_.AddKernel("depth_conv_3x3", "conv_add_kernel.cl");
L
liuruilong 已提交
46 47 48

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

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

Y
yangfei 已提交
54 55 56 57 58 59 60 61 62 63
  } 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 已提交
64 65
  }

L
liuruilong 已提交
66 67 68 69 70
  return true;
}

template <>
void ConvAddKernel<GPU_CL, float>::Compute(
L
liuruilong 已提交
71
    const FusionConvAddParam<GPU_CL> &param) {
Z
zhaojiaying01 已提交
72
  ConvAddBnRelu(this->cl_helper_, param, false, param.Bias());
L
liuruilong 已提交
73
}
L
liuruilong 已提交
74 75 76 77 78 79 80

template class ConvAddKernel<GPU_CL, float>;

}  // namespace operators
}  // namespace paddle_mobile

#endif