concat_kernel.cpp 7.8 KB
Newer Older
Y
yangfei 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/* 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 CONCAT_OP

#include "operators/kernel/concat_kernel.h"

namespace paddle_mobile {
Y
yangfei 已提交
20
namespace operators {
Y
yangfei 已提交
21

Y
yangfei 已提交
22 23
template <>
bool ConcatKernel<GPU_CL, float>::Init(ConcatParam<GPU_CL> *param) {
Y
yangfei 已提交
24
  if (param->Out()->dims().size() < 4) {
H
Huie 已提交
25 26 27 28 29
    if (param->Out()->dims().size() - param->axis_ == 1) {
      this->cl_helper_.AddKernel("concatByW", "concat_kernel.cl");
    } else {
      this->cl_helper_.AddKernel("concatByH", "concat_kernel.cl");
    }
30 31 32 33 34
  } else if (param->Out()->dims().size() >= 4) {
    if (param->Inputs().size() == 2) {
      this->cl_helper_.AddKernel("concatByCWith2Inputs", "concat_kernel.cl");
    } else if (param->Inputs().size() == 3) {
      this->cl_helper_.AddKernel("concatByCWith3Inputs", "concat_kernel.cl");
H
Huie 已提交
35 36
    } else if (param->Inputs().size() == 4) {
      this->cl_helper_.AddKernel("concatByCWith4Inputs", "concat_kernel.cl");
37 38 39
    } else {
      return false;
    }
Y
yangfei 已提交
40
  }
Y
yangfei 已提交
41 42
  return true;
}
Y
yangfei 已提交
43

Y
yangfei 已提交
44
template <>
Y
yangfei 已提交
45 46 47 48 49 50 51 52 53 54 55 56
void ConcatKernel<GPU_CL, float>::Compute(const ConcatParam<GPU_CL> &param) {
  if (param.Out()->dims().size() < 4) {
    auto kernel = this->cl_helper_.KernelAt(0);
    auto inputs = param.Inputs();
    auto *output_image = param.Out()->GetCLImage();
    int out_W = 0;
    if (param.Out()->dims().size() == 3) {
      out_W = param.Out()->dims()[2];
    } else if (param.Out()->dims().size() == 2) {
      out_W = param.Out()->dims()[1];
    }
    int out_H_Start = 0;
H
Huie 已提交
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
    if (param.Out()->dims().size() - param.axis_ == 1) {
      for (int i = 0; i < inputs.size(); i++) {
        int pre_Width = 0;
        for (int k = 0; k < i; ++k) {
          pre_Width += inputs[k]->dims()[inputs[k]->dims().size() - 1];
        }
        int in_w = inputs[i]->dims()[param.Out()->dims().size() - 2];
        auto input_image = inputs[i]->GetCLImage();
        auto default_work_size = this->cl_helper_.DefaultWorkSize(*inputs[i]);
        cl_int status;
        status = clSetKernelArg(kernel, 0, sizeof(cl_mem), &input_image);
        CL_CHECK_ERRORS(status);
        status = clSetKernelArg(kernel, 1, sizeof(cl_mem), &output_image);
        CL_CHECK_ERRORS(status);
        status = clSetKernelArg(kernel, 2, sizeof(int), &in_w);
        CL_CHECK_ERRORS(status);
        status = clSetKernelArg(kernel, 3, sizeof(int), &pre_Width);
        CL_CHECK_ERRORS(status);
        status = clSetKernelArg(kernel, 4, sizeof(int), &out_W);
        CL_CHECK_ERRORS(status);
        status = clEnqueueNDRangeKernel(
            this->cl_helper_.CLCommandQueue(), kernel, default_work_size.size(),
            NULL, default_work_size.data(), NULL, 0, NULL, NULL);
        CL_CHECK_ERRORS(status);
      }

    } else {
      for (int i = 0; i < inputs.size(); i++) {
        auto input_image = inputs[i]->GetCLImage();
        auto default_work_size = this->cl_helper_.DefaultWorkSize(*inputs[i]);
        cl_int status;
        status = clSetKernelArg(kernel, 0, sizeof(cl_mem), &input_image);
        CL_CHECK_ERRORS(status);
        status = clSetKernelArg(kernel, 1, sizeof(cl_mem), &output_image);
        CL_CHECK_ERRORS(status);
        status = clSetKernelArg(kernel, 2, sizeof(int), &out_W);
        CL_CHECK_ERRORS(status);
        status = clSetKernelArg(kernel, 3, sizeof(int), &out_H_Start);
        CL_CHECK_ERRORS(status);
        status = clEnqueueNDRangeKernel(
            this->cl_helper_.CLCommandQueue(), kernel, default_work_size.size(),
            NULL, default_work_size.data(), NULL, 0, NULL, NULL);
        CL_CHECK_ERRORS(status);
        if (param.Out()->dims().size() == 3) {
          out_H_Start += inputs[i]->dims()[1];
        } else if (param.Out()->dims().size() == 2) {
          out_H_Start += inputs[i]->dims()[0];
        }
Y
yangfei 已提交
105 106
      }
    }
H
Huie 已提交
107

Y
yangfei 已提交
108 109
  } else {
    auto kernel0 = this->cl_helper_.KernelAt(0);
110
    auto default_work_size = this->cl_helper_.DefaultWorkSize(*param.Out());
Y
yangfei 已提交
111
    auto inputs = param.Inputs();
112
    int arg_offset;
Y
yangfei 已提交
113
    cl_int status;
114 115 116
    if (inputs.size() == 2) {
      auto input_image_0 = inputs[0]->GetCLImage();
      status = clSetKernelArg(kernel0, 0, sizeof(cl_mem), &input_image_0);
Y
yangfei 已提交
117
      CL_CHECK_ERRORS(status);
118 119
      auto input_image_1 = inputs[1]->GetCLImage();
      status = clSetKernelArg(kernel0, 1, sizeof(cl_mem), &input_image_1);
Y
yangfei 已提交
120
      CL_CHECK_ERRORS(status);
121 122
      int C_0 = inputs[0]->dims()[1];
      status = clSetKernelArg(kernel0, 2, sizeof(int), &C_0);
Y
yangfei 已提交
123
      CL_CHECK_ERRORS(status);
124 125
      int C_1 = inputs[1]->dims()[1];
      status = clSetKernelArg(kernel0, 3, sizeof(int), &C_1);
Y
yangfei 已提交
126
      CL_CHECK_ERRORS(status);
127 128 129 130
      arg_offset = 4;
    } else if (inputs.size() == 3) {
      auto input_image_0 = inputs[0]->GetCLImage();
      status = clSetKernelArg(kernel0, 0, sizeof(cl_mem), &input_image_0);
Y
yangfei 已提交
131
      CL_CHECK_ERRORS(status);
132 133
      auto input_image_1 = inputs[1]->GetCLImage();
      status = clSetKernelArg(kernel0, 1, sizeof(cl_mem), &input_image_1);
Y
yangfei 已提交
134
      CL_CHECK_ERRORS(status);
135 136
      auto input_image_2 = inputs[2]->GetCLImage();
      status = clSetKernelArg(kernel0, 2, sizeof(cl_mem), &input_image_2);
Y
yangfei 已提交
137
      CL_CHECK_ERRORS(status);
138 139
      int C_0 = inputs[0]->dims()[1];
      status = clSetKernelArg(kernel0, 3, sizeof(int), &C_0);
Y
yangfei 已提交
140
      CL_CHECK_ERRORS(status);
141 142
      int C_1 = inputs[1]->dims()[1];
      status = clSetKernelArg(kernel0, 4, sizeof(int), &C_1);
Y
yangfei 已提交
143
      CL_CHECK_ERRORS(status);
144 145
      int C_2 = inputs[2]->dims()[1];
      status = clSetKernelArg(kernel0, 5, sizeof(int), &C_2);
Y
yangfei 已提交
146
      CL_CHECK_ERRORS(status);
147
      arg_offset = 6;
H
Huie 已提交
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
    } else if (inputs.size() == 4) {
      auto input_image_0 = inputs[0]->GetCLImage();
      status = clSetKernelArg(kernel0, 0, sizeof(cl_mem), &input_image_0);
      CL_CHECK_ERRORS(status);
      auto input_image_1 = inputs[1]->GetCLImage();
      status = clSetKernelArg(kernel0, 1, sizeof(cl_mem), &input_image_1);
      CL_CHECK_ERRORS(status);
      auto input_image_2 = inputs[2]->GetCLImage();
      status = clSetKernelArg(kernel0, 2, sizeof(cl_mem), &input_image_2);
      CL_CHECK_ERRORS(status);
      auto input_image_3 = inputs[3]->GetCLImage();
      status = clSetKernelArg(kernel0, 3, sizeof(cl_mem), &input_image_3);
      CL_CHECK_ERRORS(status);
      int C_0 = inputs[0]->dims()[1];
      status = clSetKernelArg(kernel0, 4, sizeof(int), &C_0);
      CL_CHECK_ERRORS(status);
      int C_1 = inputs[1]->dims()[1];
      status = clSetKernelArg(kernel0, 5, sizeof(int), &C_1);
      CL_CHECK_ERRORS(status);
      int C_2 = inputs[2]->dims()[1];
      status = clSetKernelArg(kernel0, 6, sizeof(int), &C_2);
      CL_CHECK_ERRORS(status);
      int C_3 = inputs[3]->dims()[1];
      status = clSetKernelArg(kernel0, 7, sizeof(int), &C_3);
      CL_CHECK_ERRORS(status);
      arg_offset = 8;
Y
yangfei 已提交
174
    }
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
    auto *output_image = param.Out()->GetCLImage();
    status =
        clSetKernelArg(kernel0, arg_offset + 0, sizeof(cl_mem), &output_image);
    CL_CHECK_ERRORS(status);
    int out_C = param.Out()->dims()[1];
    status = clSetKernelArg(kernel0, arg_offset + 1, sizeof(int), &out_C);
    CL_CHECK_ERRORS(status);
    int out_W = param.Out()->dims()[3];
    status = clSetKernelArg(kernel0, arg_offset + 2, sizeof(int), &out_W);
    CL_CHECK_ERRORS(status);

    status = clEnqueueNDRangeKernel(
        this->cl_helper_.CLCommandQueue(), kernel0, default_work_size.size(),
        NULL, default_work_size.data(), NULL, 0, NULL, NULL);
    CL_CHECK_ERRORS(status);
Y
yangfei 已提交
190 191
  }
}
Y
yangfei 已提交
192

Y
yangfei 已提交
193
}  // namespace operators
Y
yangfei 已提交
194 195 196
}  // namespace paddle_mobile

#endif