conv_kernel.cpp 2.7 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 CONV_OP

#include "operators/kernel/conv_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 ConvKernel<GPU_CL, float>::Init(ConvParam<GPU_CL> *param) {
L
liuruilong 已提交
25 26 27 28 29 30 31 32 33
  PADDLE_MOBILE_ENFORCE(
      param->Filter()->dims()[2] == param->Filter()->dims()[3] &&
          param->Paddings()[0] == param->Paddings()[1],
      "need equal");

  int offset = static_cast<int>(param->Filter()->dims()[2]) / 2 -
               static_cast<int>(param->Paddings()[1]);
  param->SetOffset(offset);

L
liuruilong 已提交
34 35
  DLOG << " init helper: " << &cl_helper_;
  DLOG << " conv kernel add kernel ~ ";
L
liuruilong 已提交
36 37
  DLOG << " width of one block: " << param->Filter()->dims()[3];
  DLOG << " height of one block: " << param->Filter()->dims()[2];
L
liuruilong 已提交
38 39
  DLOG << " filter dims: " << param->Filter()->dims();

L
liuruilong 已提交
40
  if (param->Filter()->dims()[2] == 1 && param->Filter()->dims()[3] == 1) {
Z
zhaojiaying01 已提交
41 42
    param->Filter()->InitNImage(cl_helper_.CLContext(),
                                cl_helper_.CLCommandQueue());
43
    this->cl_helper_.AddKernel("conv_1x1_spl", "conv_kernel.cl");
Z
zhaojiaying01 已提交
44
    DLOG << "conv 1x1";
L
liuruilong 已提交
45

Z
zhaojiaying01 已提交
46
  } else if (param->Filter()->dims()[1] == 1 &&
Y
yangfei 已提交
47 48
             param->Input()->dims()[1] == param->Output()->dims()[1] &&
             param->Filter()->dims()[2] == 3) {
Z
zhaojiaying01 已提交
49 50
    param->Filter()->InitDWImage(cl_helper_.CLContext(),
                                 cl_helper_.CLCommandQueue());
Y
yangfei 已提交
51
    this->cl_helper_.AddKernel("depth_conv_3x3", "depthwise_conv_kernel.cl");
Z
zhaojiaying01 已提交
52
    DLOG << "depth_conv 3x3";
L
liuruilong 已提交
53

L
liuruilong 已提交
54 55
  } else if (param->Filter()->dims()[2] == 3 &&
             param->Filter()->dims()[3] == 3) {
Z
zhaojiaying01 已提交
56 57 58 59
    param->Filter()->InitCLImage(cl_helper_.CLContext(),
                                 cl_helper_.CLCommandQueue());
    this->cl_helper_.AddKernel("conv_3x3", "conv_kernel.cl");
    DLOG << "conv 3x3";
L
liuruilong 已提交
60

L
liuruilong 已提交
61 62 63
  } else {
    PADDLE_MOBILE_THROW_EXCEPTION(" not support ");
  }
L
liuruilong 已提交
64

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

template <>
void ConvKernel<GPU_CL, float>::Compute(const ConvParam<GPU_CL> &param) {
Z
zhaojiaying01 已提交
70
  ConvAddBnRelu(this->cl_helper_, param);
L
liuruilong 已提交
71 72 73 74 75 76 77 78
}

template class ConvKernel<GPU_CL, float>;

}  // namespace operators
}  // namespace paddle_mobile

#endif