conv_image_compute.cc 62.0 KB
Newer Older
Y
Yan Chunwei 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// 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.

15
#include "lite/kernels/opencl/conv_image_compute.h"
16

17
#include <iomanip>
Y
Yan Chunwei 已提交
18
#include <sstream>
19
#include "lite/backends/opencl/cl_image_converter.h"
20
#include "lite/backends/opencl/cl_include.h"
Y
Yan Chunwei 已提交
21
#include "lite/core/op_registry.h"
22
#include "lite/kernels/opencl/image_helper.h"
Y
Yan Chunwei 已提交
23 24 25 26 27 28 29
#include "lite/operators/op_params.h"

namespace paddle {
namespace lite {
namespace kernels {
namespace opencl {

30 31 32 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 63 64 65 66 67 68 69 70
/* image kernel*/
void ConvImageCompute::PrepareForRun() {
  const auto& param = this->Param<param_t>();
  auto x_dims = param.x->dims();
  auto filter_dims = param.filter->dims();
  auto output_dims = param.output->dims();

  float* filter_cpu = param.filter->mutable_data<float>();
  auto& context = ctx_->As<OpenCLContext>();
  CHECK(context.cl_context() != nullptr);

  int bs = x_dims[0];
  int c_in = x_dims[1];
  int h_out = output_dims[2];
  int w_out = output_dims[3];
  int kernel_h = filter_dims[2];  // oihw
  int kernel_w = filter_dims[3];
  auto paddings = *param.paddings;
  auto dilations = *param.dilations;
  int stride_h = param.strides[0];
  int stride_w = param.strides[1];
  int pad_h = paddings[0];
  int pad_w = paddings[2];
  int groups = param.groups;
  bool relu_fused = param.fuse_relu;
  bool no_dilation = (dilations[0] == 1) && (dilations[1] == 1);
  bool zero_pad = (pad_h == 0) && (pad_w == 0);

  bool pad_equal =
      ((paddings[0] == paddings[1]) && (paddings[1] == paddings[2]) &&
       (paddings[2] == paddings[3]));
  bool stride_equal = stride_h == stride_w;
  bool dilation_equal = dilations[0] == dilations[1];

  VLOG(3) << "Is relu fused? / " << (relu_fused ? "Yes" : "No");
  VLOG(3) << "groups:" << groups << " stride_h:" << stride_h
          << " stride_w:" << stride_w << " pad_h:" << pad_h
          << " pad_w:" << pad_w << " kernel_h:" << kernel_h
          << " kernel_h:" << kernel_h;
  VLOG(3) << "x_dims:" << x_dims[0] << " " << x_dims[1] << " " << x_dims[2]
          << " " << x_dims[3];
71
  VLOG(3) << "dialtion:" << dilations[0] << " " << dilations[1];
72 73 74 75
  VLOG(3) << "output_dims:" << output_dims[0] << " " << output_dims[1] << " "
          << output_dims[2] << " " << output_dims[3];
  VLOG(3) << "filter_dims:" << filter_dims[0] << " " << filter_dims[1] << " "
          << filter_dims[2] << " " << filter_dims[3];
76 77 78 79 80 81 82
  VLOG(3) << "pad_equal:" << pad_equal;
  VLOG(3) << "stride_equal:" << stride_equal;
  VLOG(3) << "dilation_equal:" << dilation_equal;
  VLOG(3) << "padding :" << paddings[0] << " " << paddings[1] << " "
          << paddings[2] << " " << paddings[3];
  CHECK(pad_equal && stride_equal && dilation_equal);

83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
  // general gws..
  auto out_image_shape = InitImageDimInfoWith(output_dims);

  const std::vector<size_t>& default_work_size =
      DefaultWorkSize(output_dims,
                      DDim(std::vector<DDim::value_type>{
                          static_cast<int64_t>(out_image_shape["width"]),
                          static_cast<int64_t>(out_image_shape["height"])}));

  default_c_blk_ = default_work_size[0];
  default_w_blk_ = default_work_size[1];
  default_nh_blk_ = default_work_size[2];
  c_blk_ = default_c_blk_;
  w_blk_ = default_w_blk_;
  nh_blk_ = default_nh_blk_;
  global_work_size_ = cl::NDRange{static_cast<size_t>(c_blk_),
                                  static_cast<size_t>(w_blk_),
                                  static_cast<size_t>(nh_blk_)};

102 103 104 105 106
  if (kernel_h == 1 && kernel_w == 1) {
    // conv2d_1x1
    if (param.x->dims()[1] % 4 == 0) {
      kernel_func_names_.push_back("conv2d_1x1_simple");
    } else {
107
      kernel_func_names_.push_back("conv2d_1x1_opt");
108
    }
109
    kernel_func_paths_.push_back("image/conv2d_1x1_opt_kernel.cl");
110 111 112

    CLImageConverterNWBlock converter;
    const DDim& filter_image_dims = converter.InitImageDimInfoWith(filter_dims);
113 114
    std::vector<half_t> filter_image_v(filter_image_dims[0] *
                                       filter_image_dims[1] * 4);  // 4 : RGBA
115
    converter.NCHWToImage(filter_cpu, filter_image_v.data(), filter_dims);
116
    filter_gpu_image_.mutable_data<half_t, cl::Image2D>(
117 118
        filter_image_dims[0], filter_image_dims[1], filter_image_v.data());

119
    impl_ = &ConvImageCompute::Conv2d1x1opt;
120 121 122 123 124 125 126 127 128
    {
      // calc 1x1 gws
      w_blk_ = maptofactor(default_w_blk_, 4);
      c_blk_ = default_c_blk_;
      nh_blk_ = default_nh_blk_;
      global_work_size_ = cl::NDRange{static_cast<size_t>(c_blk_),
                                      static_cast<size_t>(w_blk_),
                                      static_cast<size_t>(nh_blk_)};
    }
129
#define DEPTH_CONV_USE_SPL
130
#ifdef DEPTH_CONV_USE_SPL
131 132 133 134 135 136
  } else if (filter_dims[1] == 1 && x_dims[1] == output_dims[1] &&
             kernel_h == 3 && kernel_w == 3 && groups > 1) {
    // depth_conv2d_3x3s1, depth_conv2d_3x3
    if (stride_h == 1 && dilations[0] == 1) {
      kernel_func_names_.push_back("depth_conv2d_3x3s1");
      impl_ = &ConvImageCompute::DepthwiseConv2d3x3s1;
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
      {
        // depthwise spl gws s1
        int c_block = (output_dims[1] + 3) / 4;
        int w = output_dims[3];
        int nh = output_dims[0] * output_dims[2];
        int w_blk_size = 2;
        int w_blk = (w + w_blk_size - 1) / w_blk_size;

        c_blk_ = c_block;
        w_blk_ = w_blk;
        nh_blk_ = nh;
        global_work_size_ = cl::NDRange{static_cast<size_t>(c_blk_),
                                        static_cast<size_t>(w_blk_),
                                        static_cast<size_t>(nh_blk_)};
      }
152 153 154
    } else {
      kernel_func_names_.push_back("depth_conv2d_3x3");
      impl_ = &ConvImageCompute::DepthwiseConv2d3x3;
155 156 157 158 159 160 161 162 163 164 165 166 167 168
      {
        // depthwise spl gws
        int c_block = (output_dims[1] + 3) / 4;
        int w = output_dims[3];
        int nh = output_dims[0] * output_dims[2];

        c_blk_ = c_block;
        w_blk_ = w;
        nh_blk_ = nh;

        global_work_size_ = cl::NDRange{static_cast<size_t>(c_blk_),
                                        static_cast<size_t>(w_blk_),
                                        static_cast<size_t>(nh_blk_)};
      }
169 170 171
    }
    kernel_func_paths_.push_back("image/depthwise_conv2d_kernel.cl");

172
    CLImageConverterNWBlock converter;
173
    const DDim& filter_image_dims = converter.InitImageDimInfoWith(filter_dims);
174 175
    std::vector<half_t> filter_image_v(filter_image_dims[0] *
                                       filter_image_dims[1] * 4);  // 4 : RGBA
176
    converter.NCHWToImage(filter_cpu, filter_image_v.data(), filter_dims);
177
    filter_gpu_image_.mutable_data<half_t, cl::Image2D>(
178
        filter_image_dims[0], filter_image_dims[1], filter_image_v.data());
179 180 181 182 183 184 185

#endif
  } else if (filter_dims[1] == 1 && x_dims[1] == output_dims[1]
#ifdef DEPTH_CONV_USE_SPL
             &&
             kernel_h != 3
#endif
186
#undef DEPTH_CONV_USE_SPL
187
             ) {
188 189 190 191
    // depth_conv2d
    kernel_func_names_.push_back("depth_conv2d");
    kernel_func_paths_.push_back("image/depthwise_conv2d_basic_kernel.cl");

192
    CLImageConverterNWBlock converter;
193
    const DDim& filter_image_dims = converter.InitImageDimInfoWith(filter_dims);
194 195
    std::vector<half_t> filter_image_v(filter_image_dims[0] *
                                       filter_image_dims[1] * 4);  // 4 : RGBA
196
    converter.NCHWToImage(filter_cpu, filter_image_v.data(), filter_dims);
197
    filter_gpu_image_.mutable_data<half_t, cl::Image2D>(
198 199 200
        filter_image_dims[0], filter_image_dims[1], filter_image_v.data());

    impl_ = &ConvImageCompute::DepthwiseConv2d;
201
  } else if (kernel_w == 3 && kernel_h == 3) {
202
    // conv2d_3x3
203 204
    kernel_func_names_.push_back(bs > 1 ? "conv2d_3x3_multi_batch"
                                        : "conv2d_3x3_opt");
205
    kernel_func_paths_.push_back("image/conv2d_3x3_opt_kernel.cl");
206 207 208

    CLImageConverterFolder converter;
    const DDim& filter_image_dims = converter.InitImageDimInfoWith(filter_dims);
209 210
    std::vector<half_t> filter_image_v(filter_image_dims[0] *
                                       filter_image_dims[1] * 4);  // 4 : RGBA
211
    converter.NCHWToImage(filter_cpu, filter_image_v.data(), filter_dims);
212
    filter_gpu_image_.mutable_data<half_t, cl::Image2D>(
213 214
        filter_image_dims[0], filter_image_dims[1], filter_image_v.data());

215
    impl_ = &ConvImageCompute::Conv2d3x3opt;
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
    {
      int w_blk_size = 5;
      int w_blk = (default_w_blk_ + w_blk_size - 1) / w_blk_size;

      int h_blk_size = 1;
      int h_blk = (default_nh_blk_ + h_blk_size - 1) / h_blk_size;

      c_blk_ = default_c_blk_;
      w_blk_ = w_blk;
      nh_blk_ = h_blk;

      global_work_size_ = cl::NDRange{static_cast<size_t>(c_blk_),
                                      static_cast<size_t>(w_blk_),
                                      static_cast<size_t>(nh_blk_)};
    }

232
  } else if (kernel_h == 5 && kernel_w == 5) {
233 234
#define CONV_5x5_OPT
#ifndef CONV_5x5_OPT
235 236 237 238 239 240
    // conv2d_5x5
    kernel_func_names_.push_back("conv2d_5x5");
    kernel_func_paths_.push_back("image/conv2d_5x5_kernel.cl");

    CLImageConverterFolder converter;
    const DDim& filter_image_dims = converter.InitImageDimInfoWith(filter_dims);
241 242
    std::vector<half_t> filter_image_v(filter_image_dims[0] *
                                       filter_image_dims[1] * 4);  // 4 : RGBA
243
    converter.NCHWToImage(filter_cpu, filter_image_v.data(), filter_dims);
244
    filter_gpu_image_.mutable_data<half_t, cl::Image2D>(
245 246 247
        filter_image_dims[0], filter_image_dims[1], filter_image_v.data());

    impl_ = &ConvImageCompute::Conv2d5x5;
248 249
#else
    // conv2d_5x5_opt
250 251 252

    kernel_func_names_.push_back(bs > 1 ? "conv2d_5x5_multi_batch"
                                        : "conv2d_5x5_opt");
253 254 255 256 257 258 259 260 261 262 263
    kernel_func_paths_.push_back("image/conv2d_5x5_opt_kernel.cl");

    CLImageConverterFolder converter;
    const DDim& filter_image_dims = converter.InitImageDimInfoWith(filter_dims);
    std::vector<half_t> filter_image_v(filter_image_dims[0] *
                                       filter_image_dims[1] * 4);  // 4 : RGBA
    converter.NCHWToImage(filter_cpu, filter_image_v.data(), filter_dims);
    filter_gpu_image_.mutable_data<half_t, cl::Image2D>(
        filter_image_dims[0], filter_image_dims[1], filter_image_v.data());

    impl_ = &ConvImageCompute::Conv2d5x5opt;
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
    {
      int w_blk_size = 5;
      int w_blk = (default_w_blk_ + w_blk_size - 1) / w_blk_size;

      int h_blk_size = 1;
      int h_blk = (default_nh_blk_ + h_blk_size - 1) / h_blk_size;

      c_blk_ = default_c_blk_;
      w_blk_ = w_blk;
      nh_blk_ = h_blk;

      global_work_size_ = cl::NDRange{static_cast<size_t>(c_blk_),
                                      static_cast<size_t>(w_blk_),
                                      static_cast<size_t>(nh_blk_)};
    }
279 280
#endif
#undef CONV_5x5_OPT
281
  } else if (kernel_h == 7 && kernel_w == 7) {
282 283
#define CONV_7x7_OPT
#ifndef CONV_7x7_OPT
284 285 286 287 288 289
    // conv2d_7x7
    kernel_func_names_.push_back("conv2d_7x7");
    kernel_func_paths_.push_back("image/conv2d_7x7_kernel.cl");

    CLImageConverterFolder converter;
    const DDim& filter_image_dims = converter.InitImageDimInfoWith(filter_dims);
290 291
    std::vector<half_t> filter_image_v(filter_image_dims[0] *
                                       filter_image_dims[1] * 4);  // 4 : RGBA
292
    converter.NCHWToImage(filter_cpu, filter_image_v.data(), filter_dims);
293
    this->filter_gpu_image_.mutable_data<half_t, cl::Image2D>(
294 295 296
        filter_image_dims[0], filter_image_dims[1], filter_image_v.data());

    impl_ = &ConvImageCompute::Conv2d7x7;
297 298 299

#else
    // conv2d_7x7
300 301
    kernel_func_names_.push_back(bs > 1 ? "conv2d_7x7_multi_batch"
                                        : "conv2d_7x7_opt");
302 303 304 305 306 307 308 309 310 311 312
    kernel_func_paths_.push_back("image/conv2d_7x7_opt_kernel.cl");

    CLImageConverterFolder converter;
    const DDim& filter_image_dims = converter.InitImageDimInfoWith(filter_dims);
    std::vector<half_t> filter_image_v(filter_image_dims[0] *
                                       filter_image_dims[1] * 4);  // 4 : RGBA
    converter.NCHWToImage(filter_cpu, filter_image_v.data(), filter_dims);
    this->filter_gpu_image_.mutable_data<half_t, cl::Image2D>(
        filter_image_dims[0], filter_image_dims[1], filter_image_v.data());

    impl_ = &ConvImageCompute::Conv2d7x7opt;
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
    {
      int w_blk_size = 5;
      int w_blk = (default_w_blk_ + w_blk_size - 1) / w_blk_size;

      int h_blk_size = 1;
      int h_blk = (default_nh_blk_ + h_blk_size - 1) / h_blk_size;

      c_blk_ = default_c_blk_;
      w_blk_ = w_blk;
      nh_blk_ = h_blk;

      global_work_size_ = cl::NDRange{static_cast<size_t>(c_blk_),
                                      static_cast<size_t>(w_blk_),
                                      static_cast<size_t>(nh_blk_)};
    }
328 329 330
#endif
#undef CONV_7x7_OPT

331 332 333
  } else {
    LOG(FATAL) << "conv image compute not support this condition yet! ";
  }
334 335
  VLOG(1) << "kernel_func_names_[0]:" << kernel_func_names_[0]
          << " kernel_func_paths_[0]:" << kernel_func_paths_[0];
336

337
  std::string build_options_single(" -DCL_DTYPE_half");
338 339 340 341 342 343 344
  // relu options
  if (relu_fused) {
    build_options_single += " -DRELU";
  } else if (param.activation_param.active_type ==
             lite_api::ActivationType::kRelu6) {
    build_options_single += " -DRELU6";
  } else {
345
    // do nothing, may add more activation fuse
346 347 348 349 350 351 352 353 354 355 356 357 358
  }
  // bias options
  const bool has_bias = param.bias != nullptr;
  const bool is_element_wise_bias =
      has_bias && param.output->dims() == param.bias->dims();
  if (has_bias) {
    build_options_single +=
        is_element_wise_bias ? " -DBIASE_ELE" : " -DBIASE_CH";

    // convert cpu buffer bias --> gpu image
    CLImageConverterFolder bias_converter;
    const DDim& bias_image_dims =
        bias_converter.InitImageDimInfoWith(param.bias->dims());
359 360
    std::vector<half_t> bias_image_v(bias_image_dims[0] * bias_image_dims[1] *
                                     4);
361 362 363
    float* bias_cpu_data = param.bias->mutable_data<float>();
    bias_converter.NCHWToImage(
        bias_cpu_data, bias_image_v.data(), param.bias->dims());
364
    this->bias_gpu_image_.mutable_data<half_t, cl::Image2D>(
365 366 367 368 369 370 371 372 373 374
        bias_image_dims[0], bias_image_dims[1], bias_image_v.data());
    // convert cpu buffer bias --> gpu image --- end ----
  }

  build_options_.push_back(build_options_single);

  for (size_t i = 0; i < kernel_func_names_.size(); i++) {
    context.cl_context()->AddKernel(
        kernel_func_names_[i], kernel_func_paths_[i], build_options_[i]);
  }
375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399

  VLOG(4) << "global_work_size_[3D]: {" << global_work_size_[0] << ","
          << global_work_size_[1] << "," << global_work_size_[2] << "}";

  std::stringstream kernel_key;
  kernel_key << kernel_func_names_[0] << build_options_[0];
  kernel_ = context.cl_context()->GetKernel(kernel_key.str());
  VLOG(4) << "kernel_key: " << kernel_key.str();
  VLOG(4) << "kernel ready ... " << kernel_key.str();
  size_t max_work_group_size = 0;
  kernel_.getWorkGroupInfo<size_t>(CLRuntime::Global()->device(),
                                   CL_KERNEL_WORK_GROUP_SIZE,
                                   &max_work_group_size);

  VLOG(4) << "max_work_group_size: " << max_work_group_size;

  if (max_work_group_size > 0 && use_lws) {
    // local_work_size_ = context.cl_context()->LocalWorkSizeConv1x1(
    //     global_work_size_, max_work_group_size);
    local_work_size_ = context.cl_context()->LocalWorkSize(global_work_size_,
                                                           max_work_group_size);

    VLOG(4) << "local_work_size_[3D]: {" << local_work_size_[0] << ","
            << local_work_size_[1] << "," << local_work_size_[2] << "}";
  }
400 401
}

402
void ConvImageCompute::Conv2d1x1opt() {
403 404
  auto& context = ctx_->As<OpenCLContext>();
  CHECK(context.cl_context() != nullptr);
405 406 407 408
  const auto& param = *param_.get_mutable<param_t>();
  auto input_dims = param.x->dims();
  auto paddings = *param.paddings;
  auto strides = param.strides;
409 410
  auto* input_image = param.x->data<half_t, cl::Image2D>();
  auto* filter_image = filter_gpu_image_.data<half_t, cl::Image2D>();
411 412 413 414 415 416 417 418
  auto filter_dims = param.filter->dims();
  auto output_dims = param.output->dims();

  int input_width = input_dims[3];
  int input_height = input_dims[2];
  int output_width = output_dims[3];
  int output_height = output_dims[2];
  auto out_image_shape = InitImageDimInfoWith(output_dims);
419
  auto* out_image = param.output->mutable_data<half_t, cl::Image2D>(
420 421 422 423 424 425 426 427 428 429 430 431 432 433
      out_image_shape["width"], out_image_shape["height"]);

  const bool has_bias = param.bias != nullptr;
  const bool is_element_wise_bias =
      has_bias && param.output->dims() == param.bias->dims();
  int offset = static_cast<int>(param.filter->dims()[2]) / 2 -
               static_cast<int>(paddings[0]);

  // calc input_c_block
  auto input_image_shape = InitImageDimInfoWith(input_dims);
  int input_c_block = input_image_shape["width"] / input_dims[3];
  int input_c = input_dims[1];
  auto dilations = *param.dilations;

434 435 436 437 438 439 440 441 442 443 444
// const std::vector<size_t>& default_work_size =
//     DefaultWorkSize(output_dims,
//                     DDim(std::vector<DDim::value_type>{
//                         static_cast<int64_t>(out_image_shape["width"]),
//                         static_cast<int64_t>(out_image_shape["height"])}));

// int c_block = default_work_size[0];
// int w = default_work_size[1];
// int nh = default_work_size[2];

// int maped_w = maptofactor(w, 4);
445

446 447 448 449
// auto global_work_size_ =
//     cl::NDRange{static_cast<size_t>(default_work_size.data()[0]),
//                 static_cast<size_t>(maped_w),
//                 static_cast<size_t>(default_work_size.data()[2])};
450

451 452 453 454 455
#ifndef LITE_SHUTDOWN_LOG
  //  VLOG(4) << "out_image: " << out_image;
  VLOG(4) << "global_work_size_[3D]: {" << global_work_size_[0] << ","
          << global_work_size_[1] << "," << global_work_size_[2] << "}";
#endif
456
#ifndef LITE_SHUTDOWN_LOG
457 458 459 460 461
  VLOG(4) << "============ conv2d_1x1 params ============";
  VLOG(4) << "input_image_shape: " << input_image_shape["width"] << ","
          << input_image_shape["height"];
  VLOG(4) << "input_c_block: " << input_c_block;
  VLOG(4) << "input_c: " << input_c;
462
  //  VLOG(4) << "input_image: " << input_image;
463
  VLOG(4) << "filter_dims: " << filter_dims;
464
  //  VLOG(4) << "filter_image: " << filter_image;
465 466 467 468 469 470 471 472 473 474
  VLOG(4) << "output_dims: " << output_dims;
  VLOG(4) << "out_image_shape: " << out_image_shape["width"] << ", "
          << out_image_shape["height"];
  VLOG(4) << "paddings: " << paddings[0] << "," << paddings[1];
  VLOG(4) << "has bias: " << has_bias;
  VLOG(4) << "is_element_wise_bias : " << is_element_wise_bias;
  VLOG(4) << "strides: " << strides[0] << "," << strides[1];
  VLOG(4) << "offset: " << offset;
  VLOG(4) << "dilations.size : " << dilations.size();
  VLOG(4) << "dilations: " << dilations[0] << ", " << dilations[1];
475 476 477
// VLOG(4) << "default work size{c_block, w, nh}: "
//         << "{" << c_block << ", " << w << ", " << nh << ""
//         << "}";
478
#endif
479 480 481 482 483 484 485 486 487 488 489 490
  CHECK_GE(dilations.size(), 2);
  CHECK(dilations[0] == dilations[1]);
  CHECK_GE(input_dims.size(), 4);
  CHECK_GE(paddings.size(), 2);
  CHECK(paddings[0] == paddings[1]);
  CHECK_GE(strides.size(), 2);
  CHECK(strides[0] == strides[1]);

  // handle bias  use buffer for channel wise , use image for element wise
  const cl::Buffer* bias_buf = nullptr;
  const cl::Image2D* bias_image = nullptr;
  if (has_bias) {
491
    bias_image = bias_gpu_image_.data<half_t, cl::Image2D>();
492 493
  }

494
  auto kernel = kernel_;
495 496
  cl_int status;
  int arg_idx = 0;
497
  status = kernel.setArg(arg_idx, c_blk_);
498
  CL_CHECK_FATAL(status);
499
  status = kernel.setArg(++arg_idx, w_blk_);
500
  CL_CHECK_FATAL(status);
501
  status = kernel.setArg(++arg_idx, nh_blk_);
502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531
  CL_CHECK_FATAL(status);
  status = kernel.setArg(++arg_idx, *input_image);
  CL_CHECK_FATAL(status);
  status = kernel.setArg(++arg_idx, *filter_image);
  CL_CHECK_FATAL(status);
  if (has_bias) {
    status = kernel.setArg(++arg_idx, *bias_image);
    CL_CHECK_FATAL(status);
  }
  status = kernel.setArg(++arg_idx, *out_image);
  CL_CHECK_FATAL(status);
  status = kernel.setArg(++arg_idx, strides[0]);
  CL_CHECK_FATAL(status);

  status = kernel.setArg(++arg_idx, offset);
  CL_CHECK_FATAL(status);
  status = kernel.setArg(++arg_idx, input_c_block);
  CL_CHECK_FATAL(status);
  status = kernel.setArg(++arg_idx, input_c);
  CL_CHECK_FATAL(status);
  status = kernel.setArg(++arg_idx, dilations[0]);
  CL_CHECK_FATAL(status);
  status = kernel.setArg(++arg_idx, input_width);
  CL_CHECK_FATAL(status);
  status = kernel.setArg(++arg_idx, input_height);
  CL_CHECK_FATAL(status);
  status = kernel.setArg(++arg_idx, output_width);
  CL_CHECK_FATAL(status);
  status = kernel.setArg(++arg_idx, output_height);
  CL_CHECK_FATAL(status);
532
  status = kernel.setArg(++arg_idx, default_w_blk_);
533 534 535 536 537
  CL_CHECK_FATAL(status);

  status = context.cl_context()->GetCommandQueue().enqueueNDRangeKernel(
      kernel,
      cl::NullRange,
538 539
      global_work_size_,
      local_work_size_,
540 541 542 543
      nullptr,
      event_.get());
  CL_CHECK_FATAL(status);
  context.cl_wait_list()->emplace(out_image, event_);
544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607

#ifdef PROFILE_CONV_KERNEL
  bool use_profile = false;
  auto GetCurrentUS = []() -> double {
    struct timeval time;
    gettimeofday(&time, NULL);
    return 1e+6 * time.tv_sec + time.tv_usec;
  };
  double start = GetCurrentUS();

  if (use_profile) {
    status = context.cl_context()->GetCommandQueue().enqueueNDRangeKernel(
        kernel,
        cl::NullRange,
        global_work_size_,
        local_work_size_,
        nullptr,
        event_.get());
    CL_CHECK_FATAL(status);
    context.cl_wait_list()->emplace(out_image, event_);
  } else {
    int count = 50;
    double sumtime = 0;
    if (!use_profile) {
      count = 1;
    }
    for (size_t i = 0; i < count; i++) {
      start = GetCurrentUS();
      status = context.cl_context()->GetCommandQueue().enqueueNDRangeKernel(
          kernel,
          cl::NullRange,
          global_work_size_,
          local_work_size_,
          nullptr,
          event_.get());
      CL_CHECK_FATAL(status);
      context.cl_wait_list()->emplace(out_image, event_);
      if (use_profile) {
        event_->wait();
        double duration = GetCurrentUS() - start;
        sumtime += duration;
      }
    }

    auto dims_string = [](DDimLite dims) -> std::string {
      std::ostringstream stream;
      stream << "[" << dims[0] << "," << dims[1] << "," << dims[2] << ","
             << dims[3] << "]";
      return stream.str();
    };
    if (use_profile) {
      // LOG(INFO) << "input: " << input_dims;
      // LOG(INFO) << "filter: " << filter_dims;
      // LOG(INFO) << "output: " << output_dims;

      std::cout << std::setw(25) << std::left << dims_string(input_dims)
                << std::setw(25) << std::left << dims_string(filter_dims)
                << std::setw(25) << std::left << dims_string(output_dims)
                << std::setw(25) << std::left << sumtime / count << std::endl;
    } else {
      dims_string(input_dims);
    }
  }
#endif
608
}
609 610

void ConvImageCompute::Conv2d3x3() {
611 612
  auto& context = ctx_->As<OpenCLContext>();
  CHECK(context.cl_context() != nullptr);
613 614 615 616 617
  const auto& param = *param_.get_mutable<param_t>();
  auto input_dims = param.x->dims();
  auto paddings = *param.paddings;
  auto strides = param.strides;

618 619
  auto* input_image = param.x->data<half_t, cl::Image2D>();
  auto* filter_image = filter_gpu_image_.data<half_t, cl::Image2D>();
620 621 622 623 624 625 626 627 628 629 630 631 632
  auto filter_dims = param.filter->dims();
  auto output_dims = param.output->dims();

  int input_width = input_dims[3];
  int input_height = input_dims[2];
  int input_channel = input_dims[1];
  int output_width = output_dims[3];
  int output_height = output_dims[2];
  int output_channel = output_dims[1];
  int filter_width = filter_dims[3];
  int filter_height = filter_dims[2];
  int filter_channel = filter_dims[1];
  auto out_image_shape = InitImageDimInfoWith(output_dims);
633
  auto* out_image = param.output->mutable_data<half_t, cl::Image2D>(
634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654
      out_image_shape["width"], out_image_shape["height"]);

  const bool has_bias = param.bias != nullptr;
  const bool is_element_wise_bias =
      has_bias && param.output->dims() == param.bias->dims();
  int offset = static_cast<int>(param.filter->dims()[2]) / 2 -
               static_cast<int>(paddings[0]);

  // calc input_c_block
  auto input_image_shape = InitImageDimInfoWith(input_dims);
  int input_c_block = input_image_shape["width"] / input_dims[3];
  int input_c = input_dims[1];
  auto dilations = *param.dilations;

  // re-calc group
  int new_groups{param.groups};
  if (filter_dims[0] == output_dims[1] && filter_dims[1] == input_dims[1]) {
    new_groups = 1;
  } else if (!(filter_dims[0] == input_dims[1] && filter_dims[1] == 1)) {
    new_groups = input_channel / filter_channel;
  }
655 656 657 658 659 660 661 662
/* TODO(ysh329): mobile has no case below
   else {
    LOG(FATAL) << "Not support conv3x3 case with"
               << " input_dims:" << input_dims << " output_dims:" <<
  output_dims
               << " filter_dims:" << filter_dims;
  }
*/
663

664
#ifndef LITE_SHUTDOWN_LOG
665 666 667 668 669
  VLOG(4) << "============ conv2d params ============";
  VLOG(4) << "input_image_shape: " << input_image_shape["width"] << ","
          << input_image_shape["height"];
  VLOG(4) << "input_c_block: " << input_c_block;
  VLOG(4) << "input_c: " << input_c;
670
  //  VLOG(4) << "input_image: " << input_image;
671 672
  VLOG(4) << "input_dims: " << input_dims;
  VLOG(4) << "filter_dims: " << filter_dims;
673
  //  VLOG(4) << "filter_image: " << filter_image;
674 675 676 677 678 679 680 681 682 683 684 685
  VLOG(4) << "output_dims: " << output_dims;
  VLOG(4) << "out_image_shape: " << out_image_shape["width"] << ", "
          << out_image_shape["height"];
  VLOG(4) << "paddings: " << paddings[0] << "," << paddings[1];
  VLOG(4) << "has bias: " << has_bias;
  VLOG(4) << "is_element_wise_bias : " << is_element_wise_bias;
  VLOG(4) << "strides: " << strides[0] << "," << strides[1];
  VLOG(4) << "offset: " << offset;
  VLOG(4) << "dilations.size : " << dilations.size();
  VLOG(4) << "dilations: " << dilations[0] << ", " << dilations[1];
  VLOG(4) << "param.groups(groups):" << param.groups;
  VLOG(4) << "new_groups:" << new_groups;
686 687 688
// VLOG(4) << "default work size{c_block, w, nh}: "
//         << "{" << c_block << ", " << w << ", " << nh << ""
//         << "}";
689
#endif
690 691 692 693 694 695 696 697 698 699 700

  CHECK_GE(dilations.size(), 2);
  CHECK(dilations[0] == dilations[1]);
  CHECK_GE(input_dims.size(), 4);
  CHECK_GE(paddings.size(), 2);
  CHECK(paddings[0] == paddings[1]);
  CHECK_GE(strides.size(), 2);
  CHECK(strides[0] == strides[1]);

  const cl::Image2D* bias_image = nullptr;
  if (has_bias) {
701
    bias_image = bias_gpu_image_.data<half_t, cl::Image2D>();
702
  }
703
  auto kernel = kernel_;
704 705 706

  cl_int status;
  int arg_idx = 0;
707
  status = kernel.setArg(arg_idx, c_blk_);
708
  CL_CHECK_FATAL(status);
709
  status = kernel.setArg(++arg_idx, w_blk_);
710
  CL_CHECK_FATAL(status);
711
  status = kernel.setArg(++arg_idx, nh_blk_);
712 713 714 715 716 717
  CL_CHECK_FATAL(status);
  status = kernel.setArg(++arg_idx, *input_image);
  CL_CHECK_FATAL(status);
  status = kernel.setArg(++arg_idx, *filter_image);
  CL_CHECK_FATAL(status);
  if (has_bias) {
718
#ifndef LITE_SHUTDOWN_LOG
719
    VLOG(4) << "set bias_image: ";
720
#endif
721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754
    status = kernel.setArg(++arg_idx, *bias_image);
    CL_CHECK_FATAL(status);
  }
  status = kernel.setArg(++arg_idx, *out_image);
  CL_CHECK_FATAL(status);
  status = kernel.setArg(++arg_idx, strides[0]);
  CL_CHECK_FATAL(status);

  status = kernel.setArg(++arg_idx, offset);
  CL_CHECK_FATAL(status);
  status = kernel.setArg(++arg_idx, input_c_block);
  CL_CHECK_FATAL(status);

  status = kernel.setArg(++arg_idx, dilations[0]);
  CL_CHECK_FATAL(status);
  status = kernel.setArg(++arg_idx, input_width);
  CL_CHECK_FATAL(status);
  status = kernel.setArg(++arg_idx, input_height);
  CL_CHECK_FATAL(status);
  status = kernel.setArg(++arg_idx, output_width);
  CL_CHECK_FATAL(status);
  status = kernel.setArg(++arg_idx, output_height);
  CL_CHECK_FATAL(status);
  status = kernel.setArg(++arg_idx, output_channel);
  CL_CHECK_FATAL(status);
  status = kernel.setArg(++arg_idx, filter_channel);
  CL_CHECK_FATAL(status);
  status = kernel.setArg(++arg_idx, filter_width);
  CL_CHECK_FATAL(status);
  status = kernel.setArg(++arg_idx, filter_height);
  CL_CHECK_FATAL(status);
  status = kernel.setArg(++arg_idx, new_groups);
  CL_CHECK_FATAL(status);

755
#ifndef LITE_SHUTDOWN_LOG
756
  //  VLOG(4) << "out_image: " << out_image;
757 758
  VLOG(4) << "global_work_size_[3D]: {" << global_work_size_[0] << ","
          << global_work_size_[1] << "," << global_work_size_[2] << "}";
759
#endif
760

761 762 763
  status = context.cl_context()->GetCommandQueue().enqueueNDRangeKernel(
      kernel,
      cl::NullRange,
764
      global_work_size_,
765 766 767 768 769 770 771 772
      cl::NullRange,
      nullptr,
      event_.get());
  CL_CHECK_FATAL(status);
  context.cl_wait_list()->emplace(out_image, event_);
}

void ConvImageCompute::Conv2d3x3opt() {
773 774
  auto& context = ctx_->As<OpenCLContext>();
  CHECK(context.cl_context() != nullptr);
775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791
  const auto& param = *param_.get_mutable<param_t>();
  auto input_dims = param.x->dims();
  auto paddings = *param.paddings;
  auto strides = param.strides;
  auto dilations = *param.dilations;

  auto* input_image = param.x->data<half_t, cl::Image2D>();
  auto* filter_image = filter_gpu_image_.data<half_t, cl::Image2D>();
  auto filter_dims = param.filter->dims();
  auto output_dims = param.output->dims();

  int input_width = input_dims[3];
  int input_height = input_dims[2];
  int input_channel = input_dims[1];
  int output_width = output_dims[3];
  int output_height = output_dims[2];
  int output_channel = output_dims[1];
792 793
  CHECK_EQ(input_dims[0], output_dims[0]);
  int batch = input_dims[0];
794 795 796 797 798 799 800 801
  auto out_image_shape = InitImageDimInfoWith(output_dims);
  auto* out_image = param.output->mutable_data<half_t, cl::Image2D>(
      out_image_shape["width"], out_image_shape["height"]);

  const bool has_bias = param.bias != nullptr;
  const bool is_element_wise_bias =
      has_bias && param.output->dims() == param.bias->dims();

802
#ifndef LITE_SHUTDOWN_LOG
803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818
  VLOG(4) << "============ conv2d params ============";
  // VLOG(4) << "input_image_shape: " << input_image_shape["width"] << ","
  //         << input_image_shape["height"];
  //  VLOG(4) << "input_image: " << input_image;
  VLOG(4) << "input_dims: " << input_dims;
  VLOG(4) << "filter_dims: " << filter_dims;
  //  VLOG(4) << "filter_image: " << filter_image;
  VLOG(4) << "output_dims: " << output_dims;
  VLOG(4) << "out_image_shape: " << out_image_shape["width"] << ", "
          << out_image_shape["height"];
  VLOG(4) << "paddings: " << paddings[0] << "," << paddings[1];
  VLOG(4) << "has bias: " << has_bias;
  VLOG(4) << "is_element_wise_bias : " << is_element_wise_bias;
  VLOG(4) << "strides: " << strides[0] << "," << strides[1];
  VLOG(4) << "dilations.size : " << dilations.size();
  VLOG(4) << "dilations: " << dilations[0] << ", " << dilations[1];
819
#endif
820 821 822 823 824 825 826 827 828 829 830 831 832 833

  CHECK_GE(dilations.size(), 2);
  CHECK(dilations[0] == dilations[1]);
  CHECK_GE(input_dims.size(), 4);
  CHECK_GE(paddings.size(), 2);
  CHECK(paddings[0] == paddings[1]);
  CHECK_GE(strides.size(), 2);
  CHECK(strides[0] == strides[1]);

  const cl::Image2D* bias_image = nullptr;
  if (has_bias) {
    bias_image = bias_gpu_image_.data<half_t, cl::Image2D>();
  }

834
  auto kernel = kernel_;
835 836 837

  cl_int status;
  int arg_idx = 0;
838
  status = kernel.setArg(arg_idx, c_blk_);
839
  CL_CHECK_FATAL(status);
840
  status = kernel.setArg(++arg_idx, w_blk_);
841
  CL_CHECK_FATAL(status);
842
  status = kernel.setArg(++arg_idx, nh_blk_);
843 844 845 846 847 848
  CL_CHECK_FATAL(status);
  status = kernel.setArg(++arg_idx, *input_image);
  CL_CHECK_FATAL(status);
  status = kernel.setArg(++arg_idx, *filter_image);
  CL_CHECK_FATAL(status);
  if (has_bias) {
849
#ifndef LITE_SHUTDOWN_LOG
850
    VLOG(4) << "set bias_image: ";
851
#endif
852 853 854 855 856 857 858 859 860 861 862 863 864
    status = kernel.setArg(++arg_idx, *bias_image);
    CL_CHECK_FATAL(status);
  }
  status = kernel.setArg(++arg_idx, *out_image);
  CL_CHECK_FATAL(status);
  status = kernel.setArg(++arg_idx, strides[0]);
  CL_CHECK_FATAL(status);

  status = kernel.setArg(++arg_idx, paddings[0]);
  CL_CHECK_FATAL(status);

  status = kernel.setArg(++arg_idx, dilations[0]);
  CL_CHECK_FATAL(status);
865 866
  status = kernel.setArg(++arg_idx, batch);
  CL_CHECK_FATAL(status);
867 868 869 870 871 872 873 874 875 876 877
  status = kernel.setArg(++arg_idx, input_channel);
  CL_CHECK_FATAL(status);
  status = kernel.setArg(++arg_idx, input_width);
  CL_CHECK_FATAL(status);
  status = kernel.setArg(++arg_idx, input_height);
  CL_CHECK_FATAL(status);
  status = kernel.setArg(++arg_idx, output_width);
  CL_CHECK_FATAL(status);
  status = kernel.setArg(++arg_idx, output_height);
  CL_CHECK_FATAL(status);

878
#ifndef LITE_SHUTDOWN_LOG
879
  //  VLOG(4) << "out_image: " << out_image;
880 881
  VLOG(4) << "global_work_size_[3D]: {" << global_work_size_[0] << ","
          << global_work_size_[1] << "," << global_work_size_[2] << "}";
882
#endif
883

884 885 886
  status = context.cl_context()->GetCommandQueue().enqueueNDRangeKernel(
      kernel,
      cl::NullRange,
887 888
      global_work_size_,
      local_work_size_,
889 890 891 892 893 894
      nullptr,
      event_.get());
  CL_CHECK_FATAL(status);
  context.cl_wait_list()->emplace(out_image, event_);
}

895
void ConvImageCompute::Conv2d5x5() {
896 897
  auto& context = ctx_->As<OpenCLContext>();
  CHECK(context.cl_context() != nullptr);
898 899 900 901
  const auto& param = *param_.get_mutable<param_t>();
  auto input_dims = param.x->dims();
  auto paddings = *param.paddings;
  auto strides = param.strides;
902 903
  auto* input_image = param.x->data<half_t, cl::Image2D>();
  auto* filter_image = filter_gpu_image_.data<half_t, cl::Image2D>();
904 905 906 907 908 909 910 911 912 913
  auto filter_dims = param.filter->dims();
  auto output_dims = param.output->dims();

  int input_width = input_dims[3];
  int input_height = input_dims[2];
  int output_width = output_dims[3];
  int output_height = output_dims[2];
  int filter_width = filter_dims[3];
  int filter_height = filter_dims[2];
  auto out_image_shape = InitImageDimInfoWith(output_dims);
914
  auto* out_image = param.output->mutable_data<half_t, cl::Image2D>(
915 916 917 918 919 920 921 922 923 924 925 926 927 928
      out_image_shape["width"], out_image_shape["height"]);

  const bool has_bias = param.bias != nullptr;
  const bool is_element_wise_bias =
      has_bias && param.output->dims() == param.bias->dims();
  int offset = static_cast<int>(param.filter->dims()[2]) / 2 -
               static_cast<int>(paddings[0]);

  // calc input_c_block
  auto input_image_shape = InitImageDimInfoWith(input_dims);
  int input_c_block = input_image_shape["width"] / input_dims[3];
  int input_c = input_dims[1];
  auto dilations = *param.dilations;

929
#ifndef LITE_SHUTDOWN_LOG
930 931 932 933 934
  VLOG(4) << "============ conv2d params ============";
  VLOG(4) << "input_image_shape: " << input_image_shape["width"] << ","
          << input_image_shape["height"];
  VLOG(4) << "input_c_block: " << input_c_block;
  VLOG(4) << "input_c: " << input_c;
935
  //  VLOG(4) << "input_image: " << input_image;
936 937
  VLOG(4) << "input_dims: " << input_dims;
  VLOG(4) << "filter_dims: " << filter_dims;
938
  //  VLOG(4) << "filter_image: " << filter_image;
939 940 941 942 943 944 945 946 947 948
  VLOG(4) << "output_dims: " << output_dims;
  VLOG(4) << "out_image_shape: " << out_image_shape["width"] << ", "
          << out_image_shape["height"];
  VLOG(4) << "paddings: " << paddings[0] << "," << paddings[1];
  VLOG(4) << "has bias: " << has_bias;
  VLOG(4) << "is_element_wise_bias : " << is_element_wise_bias;
  VLOG(4) << "strides: " << strides[0] << "," << strides[1];
  VLOG(4) << "offset: " << offset;
  VLOG(4) << "dilations.size : " << dilations.size();
  VLOG(4) << "dilations: " << dilations[0] << ", " << dilations[1];
949
#endif
950 951 952 953 954 955 956 957 958 959 960

  CHECK_GE(dilations.size(), 2);
  CHECK(dilations[0] == dilations[1]);
  CHECK_GE(input_dims.size(), 4);
  CHECK_GE(paddings.size(), 2);
  CHECK(paddings[0] == paddings[1]);
  CHECK_GE(strides.size(), 2);
  CHECK(strides[0] == strides[1]);

  const cl::Image2D* bias_image = nullptr;
  if (has_bias) {
961
    bias_image = bias_gpu_image_.data<half_t, cl::Image2D>();
962 963
  }

964
  auto kernel = kernel_;
965 966 967

  cl_int status;
  int arg_idx = 0;
968
  status = kernel.setArg(arg_idx, c_blk_);
969
  CL_CHECK_FATAL(status);
970
  status = kernel.setArg(++arg_idx, w_blk_);
971
  CL_CHECK_FATAL(status);
972
  status = kernel.setArg(++arg_idx, nh_blk_);
973 974 975 976 977 978
  CL_CHECK_FATAL(status);
  status = kernel.setArg(++arg_idx, *input_image);
  CL_CHECK_FATAL(status);
  status = kernel.setArg(++arg_idx, *filter_image);
  CL_CHECK_FATAL(status);
  if (has_bias) {
979
#ifndef LITE_SHUTDOWN_LOG
980
    VLOG(4) << "set bias_image: ";
981
#endif
982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005
    status = kernel.setArg(++arg_idx, *bias_image);
    CL_CHECK_FATAL(status);
  }
  status = kernel.setArg(++arg_idx, *out_image);
  CL_CHECK_FATAL(status);
  status = kernel.setArg(++arg_idx, strides[0]);
  CL_CHECK_FATAL(status);

  status = kernel.setArg(++arg_idx, offset);
  CL_CHECK_FATAL(status);
  status = kernel.setArg(++arg_idx, input_c_block);
  CL_CHECK_FATAL(status);

  status = kernel.setArg(++arg_idx, dilations[0]);
  CL_CHECK_FATAL(status);
  status = kernel.setArg(++arg_idx, input_width);
  CL_CHECK_FATAL(status);
  status = kernel.setArg(++arg_idx, input_height);
  CL_CHECK_FATAL(status);
  status = kernel.setArg(++arg_idx, output_width);
  CL_CHECK_FATAL(status);
  status = kernel.setArg(++arg_idx, output_height);
  CL_CHECK_FATAL(status);

1006
#ifndef LITE_SHUTDOWN_LOG
1007
  //  VLOG(4) << "out_image: " << out_image;
1008 1009
  VLOG(4) << "global_work_size_[3D]: {" << global_work_size_[0] << ","
          << global_work_size_[1] << "," << global_work_size_[2] << "}";
1010
#endif
1011 1012 1013 1014

  status = context.cl_context()->GetCommandQueue().enqueueNDRangeKernel(
      kernel,
      cl::NullRange,
1015
      global_work_size_,
1016 1017 1018 1019 1020 1021
      cl::NullRange,
      nullptr,
      event_.get());
  CL_CHECK_FATAL(status);
  context.cl_wait_list()->emplace(out_image, event_);
}
1022

1023
void ConvImageCompute::Conv2d5x5opt() {
1024 1025
  auto& context = ctx_->As<OpenCLContext>();
  CHECK(context.cl_context() != nullptr);
1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085
  const auto& param = *param_.get_mutable<param_t>();
  auto input_dims = param.x->dims();
  auto paddings = *param.paddings;
  auto strides = param.strides;
  auto dilations = *param.dilations;

  auto* input_image = param.x->data<half_t, cl::Image2D>();
  auto* filter_image = filter_gpu_image_.data<half_t, cl::Image2D>();
  auto filter_dims = param.filter->dims();
  auto output_dims = param.output->dims();

  int input_width = input_dims[3];
  int input_height = input_dims[2];
  int input_channel = input_dims[1];
  int output_width = output_dims[3];
  int output_height = output_dims[2];
  int output_channel = output_dims[1];
  CHECK_EQ(input_dims[0], output_dims[0]);
  int batch = input_dims[0];

  auto out_image_shape = InitImageDimInfoWith(output_dims);
  auto* out_image = param.output->mutable_data<half_t, cl::Image2D>(
      out_image_shape["width"], out_image_shape["height"]);

  const bool has_bias = param.bias != nullptr;
  const bool is_element_wise_bias =
      has_bias && param.output->dims() == param.bias->dims();

// default_work_size[2] = h_blk;
#ifndef LITE_SHUTDOWN_LOG
  VLOG(4) << "============ conv2d params ============";
  // VLOG(4) << "input_image_shape: " << input_image_shape["width"] << ","
  //         << input_image_shape["height"];
  //  VLOG(4) << "input_image: " << input_image;
  VLOG(4) << "input_dims: " << input_dims;
  VLOG(4) << "filter_dims: " << filter_dims;
  //  VLOG(4) << "filter_image: " << filter_image;
  VLOG(4) << "output_dims: " << output_dims;
  VLOG(4) << "out_image_shape: " << out_image_shape["width"] << ", "
          << out_image_shape["height"];
  VLOG(4) << "paddings: " << paddings[0] << "," << paddings[1];
  VLOG(4) << "has bias: " << has_bias;
  VLOG(4) << "is_element_wise_bias : " << is_element_wise_bias;
  VLOG(4) << "strides: " << strides[0] << "," << strides[1];
  VLOG(4) << "dilations.size : " << dilations.size();
  VLOG(4) << "dilations: " << dilations[0] << ", " << dilations[1];
#endif
  CHECK_GE(dilations.size(), 2);
  CHECK(dilations[0] == dilations[1]);
  CHECK_GE(input_dims.size(), 4);
  CHECK_GE(paddings.size(), 2);
  CHECK(paddings[0] == paddings[1]);
  CHECK_GE(strides.size(), 2);
  CHECK(strides[0] == strides[1]);

  const cl::Image2D* bias_image = nullptr;
  if (has_bias) {
    bias_image = bias_gpu_image_.data<half_t, cl::Image2D>();
  }

1086
  auto kernel = kernel_;
1087 1088
  cl_int status;
  int arg_idx = 0;
1089
  status = kernel.setArg(arg_idx, c_blk_);
1090
  CL_CHECK_FATAL(status);
1091
  status = kernel.setArg(++arg_idx, w_blk_);
1092
  CL_CHECK_FATAL(status);
1093
  status = kernel.setArg(++arg_idx, nh_blk_);
1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125
  CL_CHECK_FATAL(status);
  status = kernel.setArg(++arg_idx, *input_image);
  CL_CHECK_FATAL(status);
  status = kernel.setArg(++arg_idx, *filter_image);
  CL_CHECK_FATAL(status);
  if (has_bias) {
    status = kernel.setArg(++arg_idx, *bias_image);
    CL_CHECK_FATAL(status);
  }
  status = kernel.setArg(++arg_idx, *out_image);
  CL_CHECK_FATAL(status);
  status = kernel.setArg(++arg_idx, strides[0]);
  CL_CHECK_FATAL(status);

  status = kernel.setArg(++arg_idx, paddings[0]);
  CL_CHECK_FATAL(status);

  status = kernel.setArg(++arg_idx, dilations[0]);
  CL_CHECK_FATAL(status);
  status = kernel.setArg(++arg_idx, batch);
  CL_CHECK_FATAL(status);
  status = kernel.setArg(++arg_idx, input_channel);
  CL_CHECK_FATAL(status);
  status = kernel.setArg(++arg_idx, input_width);
  CL_CHECK_FATAL(status);
  status = kernel.setArg(++arg_idx, input_height);
  CL_CHECK_FATAL(status);
  status = kernel.setArg(++arg_idx, output_width);
  CL_CHECK_FATAL(status);
  status = kernel.setArg(++arg_idx, output_height);
  CL_CHECK_FATAL(status);

1126
  //  VLOG(4) << "out_image: " << out_image;
1127 1128 1129 1130

  status = context.cl_context()->GetCommandQueue().enqueueNDRangeKernel(
      kernel,
      cl::NullRange,
1131 1132
      global_work_size_,
      local_work_size_,
1133 1134 1135 1136 1137 1138
      nullptr,
      event_.get());
  CL_CHECK_FATAL(status);
  context.cl_wait_list()->emplace(out_image, event_);
}

1139
void ConvImageCompute::Conv2d7x7() {
1140 1141
  auto& context = ctx_->As<OpenCLContext>();
  CHECK(context.cl_context() != nullptr);
1142 1143 1144 1145
  const auto& param = *param_.get_mutable<param_t>();
  auto input_dims = param.x->dims();
  auto paddings = *param.paddings;
  auto strides = param.strides;
1146 1147
  auto* input_image = param.x->data<half_t, cl::Image2D>();
  auto* filter_image = filter_gpu_image_.data<half_t, cl::Image2D>();
1148 1149 1150 1151 1152 1153 1154 1155 1156 1157
  auto filter_dims = param.filter->dims();
  auto output_dims = param.output->dims();

  int input_width = input_dims[3];
  int input_height = input_dims[2];
  int output_width = output_dims[3];
  int output_height = output_dims[2];
  int filter_width = filter_dims[3];
  int filter_height = filter_dims[2];
  auto out_image_shape = InitImageDimInfoWith(output_dims);
1158
  auto* out_image = param.output->mutable_data<half_t, cl::Image2D>(
1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172
      out_image_shape["width"], out_image_shape["height"]);

  const bool has_bias = param.bias != nullptr;
  const bool is_element_wise_bias =
      has_bias && param.output->dims() == param.bias->dims();
  int offset = static_cast<int>(param.filter->dims()[2]) / 2 -
               static_cast<int>(paddings[0]);

  // calc input_c_block
  auto input_image_shape = InitImageDimInfoWith(input_dims);
  int input_c_block = input_image_shape["width"] / input_dims[3];
  int input_c = input_dims[1];
  auto dilations = *param.dilations;

1173
#ifndef LITE_SHUTDOWN_LOG
1174 1175 1176 1177 1178
  VLOG(4) << "============ conv2d params ============";
  VLOG(4) << "input_image_shape: " << input_image_shape["width"] << ","
          << input_image_shape["height"];
  VLOG(4) << "input_c_block: " << input_c_block;
  VLOG(4) << "input_c: " << input_c;
1179
  //  VLOG(4) << "input_image: " << input_image;
1180 1181
  VLOG(4) << "input_dims: " << input_dims;
  VLOG(4) << "filter_dims: " << filter_dims;
1182
  //  VLOG(4) << "filter_image: " << filter_image;
1183 1184 1185 1186 1187 1188 1189 1190 1191 1192
  VLOG(4) << "output_dims: " << output_dims;
  VLOG(4) << "out_image_shape: " << out_image_shape["width"] << ", "
          << out_image_shape["height"];
  VLOG(4) << "paddings: " << paddings[0] << "," << paddings[1];
  VLOG(4) << "has bias: " << has_bias;
  VLOG(4) << "is_element_wise_bias : " << is_element_wise_bias;
  VLOG(4) << "strides: " << strides[0] << "," << strides[1];
  VLOG(4) << "offset: " << offset;
  VLOG(4) << "dilations.size : " << dilations.size();
  VLOG(4) << "dilations: " << dilations[0] << ", " << dilations[1];
1193
#endif
1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204

  CHECK_GE(dilations.size(), 2);
  CHECK(dilations[0] == dilations[1]);
  CHECK_GE(input_dims.size(), 4);
  CHECK_GE(paddings.size(), 2);
  CHECK(paddings[0] == paddings[1]);
  CHECK_GE(strides.size(), 2);
  CHECK(strides[0] == strides[1]);

  const cl::Image2D* bias_image = nullptr;
  if (has_bias) {
1205
    bias_image = bias_gpu_image_.data<half_t, cl::Image2D>();
1206 1207
  }

1208
  auto kernel = kernel_;
1209 1210 1211

  cl_int status;
  int arg_idx = 0;
1212
  status = kernel.setArg(arg_idx, c_blk_);
1213
  CL_CHECK_FATAL(status);
1214
  status = kernel.setArg(++arg_idx, w_blk_);
1215
  CL_CHECK_FATAL(status);
1216
  status = kernel.setArg(++arg_idx, nh_blk_);
1217 1218 1219 1220 1221 1222
  CL_CHECK_FATAL(status);
  status = kernel.setArg(++arg_idx, *input_image);
  CL_CHECK_FATAL(status);
  status = kernel.setArg(++arg_idx, *filter_image);
  CL_CHECK_FATAL(status);
  if (has_bias) {
1223
#ifndef LITE_SHUTDOWN_LOG
1224
    VLOG(4) << "set bias_image: ";
1225
#endif
1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249
    status = kernel.setArg(++arg_idx, *bias_image);
    CL_CHECK_FATAL(status);
  }
  status = kernel.setArg(++arg_idx, *out_image);
  CL_CHECK_FATAL(status);
  status = kernel.setArg(++arg_idx, strides[0]);
  CL_CHECK_FATAL(status);

  status = kernel.setArg(++arg_idx, offset);
  CL_CHECK_FATAL(status);
  status = kernel.setArg(++arg_idx, input_c_block);
  CL_CHECK_FATAL(status);

  status = kernel.setArg(++arg_idx, dilations[0]);
  CL_CHECK_FATAL(status);
  status = kernel.setArg(++arg_idx, input_width);
  CL_CHECK_FATAL(status);
  status = kernel.setArg(++arg_idx, input_height);
  CL_CHECK_FATAL(status);
  status = kernel.setArg(++arg_idx, output_width);
  CL_CHECK_FATAL(status);
  status = kernel.setArg(++arg_idx, output_height);
  CL_CHECK_FATAL(status);

1250
#ifndef LITE_SHUTDOWN_LOG
1251
  //  VLOG(4) << "out_image: " << out_image;
1252 1253
  VLOG(4) << "global_work_size_[3D]: {" << global_work_size_[0] << ","
          << global_work_size_[1] << "," << global_work_size_[2] << "}";
1254
#endif
1255 1256 1257 1258

  status = context.cl_context()->GetCommandQueue().enqueueNDRangeKernel(
      kernel,
      cl::NullRange,
1259
      global_work_size_,
1260 1261 1262 1263 1264 1265
      cl::NullRange,
      nullptr,
      event_.get());
  CL_CHECK_FATAL(status);
  context.cl_wait_list()->emplace(out_image, event_);
}
1266
void ConvImageCompute::Conv2d7x7opt() {
1267 1268
  auto& context = ctx_->As<OpenCLContext>();
  CHECK(context.cl_context() != nullptr);
1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326
  const auto& param = *param_.get_mutable<param_t>();
  auto input_dims = param.x->dims();
  auto paddings = *param.paddings;
  auto strides = param.strides;
  auto dilations = *param.dilations;

  auto* input_image = param.x->data<half_t, cl::Image2D>();
  auto* filter_image = filter_gpu_image_.data<half_t, cl::Image2D>();
  auto filter_dims = param.filter->dims();
  auto output_dims = param.output->dims();

  int input_width = input_dims[3];
  int input_height = input_dims[2];
  int input_channel = input_dims[1];
  int output_width = output_dims[3];
  int output_height = output_dims[2];
  int output_channel = output_dims[1];
  CHECK_EQ(input_dims[0], output_dims[0]);
  int batch = input_dims[0];
  auto out_image_shape = InitImageDimInfoWith(output_dims);
  auto* out_image = param.output->mutable_data<half_t, cl::Image2D>(
      out_image_shape["width"], out_image_shape["height"]);

  const bool has_bias = param.bias != nullptr;
  const bool is_element_wise_bias =
      has_bias && param.output->dims() == param.bias->dims();

#ifndef LITE_SHUTDOWN_LOG
  VLOG(4) << "============ conv2d 7x7 params ============";
  // VLOG(4) << "input_image_shape: " << input_image_shape["width"] << ","
  //         << input_image_shape["height"];
  //  VLOG(4) << "input_image: " << input_image;
  VLOG(4) << "input_dims: " << input_dims;
  VLOG(4) << "filter_dims: " << filter_dims;
  //  VLOG(4) << "filter_image: " << filter_image;
  VLOG(4) << "output_dims: " << output_dims;
  VLOG(4) << "out_image_shape: " << out_image_shape["width"] << ", "
          << out_image_shape["height"];
  VLOG(4) << "paddings: " << paddings[0] << "," << paddings[1];
  VLOG(4) << "has bias: " << has_bias;
  VLOG(4) << "is_element_wise_bias : " << is_element_wise_bias;
  VLOG(4) << "strides: " << strides[0] << "," << strides[1];
  VLOG(4) << "dilations.size : " << dilations.size();
  VLOG(4) << "dilations: " << dilations[0] << ", " << dilations[1];
#endif
  CHECK_GE(dilations.size(), 2);
  CHECK(dilations[0] == dilations[1]);
  CHECK_GE(input_dims.size(), 4);
  CHECK_GE(paddings.size(), 2);
  CHECK(paddings[0] == paddings[1]);
  CHECK_GE(strides.size(), 2);
  CHECK(strides[0] == strides[1]);

  const cl::Image2D* bias_image = nullptr;
  if (has_bias) {
    bias_image = bias_gpu_image_.data<half_t, cl::Image2D>();
  }

1327
  auto kernel = kernel_;
1328

1329 1330
  cl_int status;
  int arg_idx = 0;
1331
  status = kernel.setArg(arg_idx, c_blk_);
1332
  CL_CHECK_FATAL(status);
1333
  status = kernel.setArg(++arg_idx, w_blk_);
1334
  CL_CHECK_FATAL(status);
1335
  status = kernel.setArg(++arg_idx, nh_blk_);
1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370
  CL_CHECK_FATAL(status);
  status = kernel.setArg(++arg_idx, *input_image);
  CL_CHECK_FATAL(status);
  status = kernel.setArg(++arg_idx, *filter_image);
  CL_CHECK_FATAL(status);
  if (has_bias) {
    status = kernel.setArg(++arg_idx, *bias_image);
    CL_CHECK_FATAL(status);
  }
  status = kernel.setArg(++arg_idx, *out_image);
  CL_CHECK_FATAL(status);
  status = kernel.setArg(++arg_idx, strides[0]);
  CL_CHECK_FATAL(status);

  status = kernel.setArg(++arg_idx, paddings[0]);
  CL_CHECK_FATAL(status);

  status = kernel.setArg(++arg_idx, dilations[0]);
  CL_CHECK_FATAL(status);
  status = kernel.setArg(++arg_idx, batch);
  CL_CHECK_FATAL(status);
  status = kernel.setArg(++arg_idx, input_channel);
  CL_CHECK_FATAL(status);
  status = kernel.setArg(++arg_idx, input_width);
  CL_CHECK_FATAL(status);
  status = kernel.setArg(++arg_idx, input_height);
  CL_CHECK_FATAL(status);
  status = kernel.setArg(++arg_idx, output_width);
  CL_CHECK_FATAL(status);
  status = kernel.setArg(++arg_idx, output_height);
  CL_CHECK_FATAL(status);

  status = context.cl_context()->GetCommandQueue().enqueueNDRangeKernel(
      kernel,
      cl::NullRange,
1371 1372
      global_work_size_,
      local_work_size_,
1373 1374 1375 1376 1377
      nullptr,
      event_.get());
  CL_CHECK_FATAL(status);
  context.cl_wait_list()->emplace(out_image, event_);
}
1378
void ConvImageCompute::DepthwiseConv2d3x3s1() {
1379 1380
  auto& context = ctx_->As<OpenCLContext>();
  CHECK(context.cl_context() != nullptr);
1381 1382 1383 1384 1385 1386 1387 1388
  const auto& param = *param_.get_mutable<param_t>();
  auto x_dims = param.x->dims();
  auto filter_dims = param.filter->dims();
  auto output_dims = param.output->dims();
  auto paddings = *param.paddings;
  auto strides = param.strides;
  auto dilations = *param.dilations;

1389 1390
  auto* input_img = param.x->data<half_t, cl::Image2D>();
  auto* filter_img = filter_gpu_image_.data<half_t, cl::Image2D>();
1391 1392 1393

  const cl::Image2D* bias_img = nullptr;
  if (param.bias) {
1394
    bias_img = bias_gpu_image_.data<half_t, cl::Image2D>();
1395 1396 1397 1398
  }

  auto image_shape = InitImageDimInfoWith(output_dims);

1399
  auto* output_img = param.output->mutable_data<half_t, cl::Image2D>(
1400 1401
      image_shape["width"], image_shape["height"]);

1402
  auto kernel = kernel_;
1403 1404 1405

  cl_int status;
  int arg_idx = 0;
1406
  status = kernel.setArg(arg_idx, c_blk_);
1407
  CL_CHECK_FATAL(status);
1408
  status = kernel.setArg(++arg_idx, w_blk_);
1409
  CL_CHECK_FATAL(status);
1410
  status = kernel.setArg(++arg_idx, nh_blk_);
1411 1412 1413 1414 1415
  CL_CHECK_FATAL(status);
  status = kernel.setArg(++arg_idx, *input_img);
  CL_CHECK_FATAL(status);
  status = kernel.setArg(++arg_idx, *filter_img);
  CL_CHECK_FATAL(status);
1416 1417 1418 1419 1420 1421 1422

  const bool has_bias = param.bias != nullptr;
  const bool is_element_wise_bias =
      has_bias && param.output->dims() == param.bias->dims();
  const cl::Image2D* bias_image = nullptr;
  if (has_bias) {
    bias_image = bias_gpu_image_.data<half_t, cl::Image2D>();
1423
#ifndef LITE_SHUTDOWN_LOG
1424
    VLOG(4) << "set bias_image: ";
1425
#endif
1426 1427 1428
    status = kernel.setArg(++arg_idx, *bias_image);
    CL_CHECK_FATAL(status);
  }
1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450
  status = kernel.setArg(++arg_idx, *output_img);
  CL_CHECK_FATAL(status);
  status = kernel.setArg(++arg_idx, static_cast<const int>(strides[0]));
  CL_CHECK_FATAL(status);
  status = kernel.setArg(++arg_idx, static_cast<const int>(paddings[0]));
  CL_CHECK_FATAL(status);
  status = kernel.setArg(++arg_idx, static_cast<const int>(dilations[0]));
  CL_CHECK_FATAL(status);
  status = kernel.setArg(++arg_idx, static_cast<const int>(x_dims[1]));
  CL_CHECK_FATAL(status);
  status = kernel.setArg(++arg_idx, static_cast<const int>(x_dims[3]));
  CL_CHECK_FATAL(status);
  status = kernel.setArg(++arg_idx, static_cast<const int>(x_dims[2]));
  CL_CHECK_FATAL(status);
  status = kernel.setArg(++arg_idx, static_cast<const int>(output_dims[3]));
  CL_CHECK_FATAL(status);
  status = kernel.setArg(++arg_idx, static_cast<const int>(output_dims[2]));
  CL_CHECK_FATAL(status);

  status = context.cl_context()->GetCommandQueue().enqueueNDRangeKernel(
      kernel,
      cl::NullRange,
1451 1452
      global_work_size_,
      local_work_size_,
1453 1454 1455 1456 1457 1458 1459
      nullptr,
      event_.get());
  CL_CHECK_FATAL(status);
  context.cl_wait_list()->emplace(output_img, event_);
}

void ConvImageCompute::DepthwiseConv2d3x3() {
1460 1461
  auto& context = ctx_->As<OpenCLContext>();
  CHECK(context.cl_context() != nullptr);
1462 1463 1464 1465 1466 1467 1468 1469 1470 1471
  const auto& param = *param_.get_mutable<param_t>();
  auto x_dims = param.x->dims();
  auto filter_dims = param.filter->dims();
  auto output_dims = param.output->dims();
  auto paddings = *param.paddings;
  auto strides = param.strides;
  auto dilations = *param.dilations;
  int offset = filter_dims[2] / 2 - paddings[0];
  int input_c_block = (x_dims[1] + 3) / 4;

1472 1473
  auto* input_img = param.x->data<half_t, cl::Image2D>();
  auto* filter_img = filter_gpu_image_.data<half_t, cl::Image2D>();
1474 1475 1476

  const cl::Image2D* bias_img = nullptr;
  if (param.bias) {
1477
    bias_img = bias_gpu_image_.data<half_t, cl::Image2D>();
1478 1479 1480 1481
  }

  auto image_shape = InitImageDimInfoWith(output_dims);

1482
  auto* output_img = param.output->mutable_data<half_t, cl::Image2D>(
1483 1484
      image_shape["width"], image_shape["height"]);

1485
  auto kernel = kernel_;
1486

1487
#ifndef LITE_SHUTDOWN_LOG
1488 1489 1490 1491 1492 1493 1494 1495 1496
  VLOG(4) << "setArg";
  VLOG(4) << "strides = " << strides[0];
  VLOG(4) << "offset = " << offset;
  VLOG(4) << "dilations = " << dilations[0];
  VLOG(4) << "input_c_block = " << input_c_block;
  VLOG(4) << "x_dims[3] = " << x_dims[3];
  VLOG(4) << "x_dims[2] = " << x_dims[2];
  VLOG(4) << "output_dims[3] = " << output_dims[3];
  VLOG(4) << "output_dims[2] = " << output_dims[2];
1497
#endif
1498 1499 1500

  cl_int status;
  int arg_idx = 0;
1501
  status = kernel.setArg(arg_idx, c_blk_);
1502
  CL_CHECK_FATAL(status);
1503
  status = kernel.setArg(++arg_idx, w_blk_);
1504
  CL_CHECK_FATAL(status);
1505
  status = kernel.setArg(++arg_idx, nh_blk_);
1506 1507 1508 1509 1510
  CL_CHECK_FATAL(status);
  status = kernel.setArg(++arg_idx, *input_img);
  CL_CHECK_FATAL(status);
  status = kernel.setArg(++arg_idx, *filter_img);
  CL_CHECK_FATAL(status);
1511 1512 1513 1514 1515 1516
  const bool has_bias = param.bias != nullptr;
  const bool is_element_wise_bias =
      has_bias && param.output->dims() == param.bias->dims();
  const cl::Image2D* bias_image = nullptr;
  if (has_bias) {
    bias_image = bias_gpu_image_.data<half_t, cl::Image2D>();
1517
#ifndef LITE_SHUTDOWN_LOG
1518
    VLOG(4) << "set bias_image: ";
1519
#endif
1520 1521 1522
    status = kernel.setArg(++arg_idx, *bias_image);
    CL_CHECK_FATAL(status);
  }
1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544
  status = kernel.setArg(++arg_idx, *output_img);
  CL_CHECK_FATAL(status);
  status = kernel.setArg(++arg_idx, static_cast<const int>(strides[0]));
  CL_CHECK_FATAL(status);
  status = kernel.setArg(++arg_idx, static_cast<const int>(offset));
  CL_CHECK_FATAL(status);
  status = kernel.setArg(++arg_idx, static_cast<const int>(dilations[0]));
  CL_CHECK_FATAL(status);
  status = kernel.setArg(++arg_idx, static_cast<const int>(input_c_block));
  CL_CHECK_FATAL(status);
  status = kernel.setArg(++arg_idx, static_cast<const int>(x_dims[3]));
  CL_CHECK_FATAL(status);
  status = kernel.setArg(++arg_idx, static_cast<const int>(x_dims[2]));
  CL_CHECK_FATAL(status);
  status = kernel.setArg(++arg_idx, static_cast<const int>(output_dims[3]));
  CL_CHECK_FATAL(status);
  status = kernel.setArg(++arg_idx, static_cast<const int>(output_dims[2]));
  CL_CHECK_FATAL(status);

  status = context.cl_context()->GetCommandQueue().enqueueNDRangeKernel(
      kernel,
      cl::NullRange,
1545
      global_work_size_,
1546 1547 1548 1549 1550 1551 1552 1553
      cl::NullRange,
      nullptr,
      event_.get());
  CL_CHECK_FATAL(status);
  context.cl_wait_list()->emplace(output_img, event_);
}

void ConvImageCompute::DepthwiseConv2d() {
1554 1555
  auto& context = ctx_->As<OpenCLContext>();
  CHECK(context.cl_context() != nullptr);
1556 1557 1558 1559
  const auto& param = *param_.get_mutable<param_t>();
  auto input_dims = param.x->dims();
  auto paddings = *param.paddings;
  auto strides = param.strides;
1560 1561
  auto* input_image = param.x->data<half_t, cl::Image2D>();
  auto* filter_image = filter_gpu_image_.data<half_t, cl::Image2D>();
1562 1563 1564 1565 1566 1567 1568 1569 1570 1571
  auto filter_dims = param.filter->dims();
  auto output_dims = param.output->dims();

  int input_width = input_dims[3];
  int input_height = input_dims[2];
  int output_width = output_dims[3];
  int output_height = output_dims[2];
  int filter_width = filter_dims[3];
  int filter_height = filter_dims[2];
  auto out_image_shape = InitImageDimInfoWith(output_dims);
1572
  auto* out_image = param.output->mutable_data<half_t, cl::Image2D>(
1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586
      out_image_shape["width"], out_image_shape["height"]);

  const bool has_bias = param.bias != nullptr;
  const bool is_element_wise_bias =
      has_bias && param.output->dims() == param.bias->dims();
  int offset = static_cast<int>(param.filter->dims()[2]) / 2 -
               static_cast<int>(paddings[0]);

  // calc input_c_block
  auto input_image_shape = InitImageDimInfoWith(input_dims);
  int input_c_block = input_image_shape["width"] / input_dims[3];
  int input_c = input_dims[1];
  auto dilations = *param.dilations;

1587
#ifndef LITE_SHUTDOWN_LOG
1588 1589 1590 1591 1592
  VLOG(4) << "============ depthwise conv2d params ============";
  VLOG(4) << "input_image_shape: " << input_image_shape["width"] << ","
          << input_image_shape["height"];
  VLOG(4) << "input_c_block: " << input_c_block;
  VLOG(4) << "input_c: " << input_c;
1593
  //  VLOG(4) << "input_image: " << input_image;
1594
  VLOG(4) << "filter_dims: " << filter_dims;
1595
  //  VLOG(4) << "filter_image: " << filter_image;
1596 1597 1598 1599 1600 1601 1602 1603 1604 1605
  VLOG(4) << "output_dims: " << output_dims;
  VLOG(4) << "out_image_shape: " << out_image_shape["width"] << ", "
          << out_image_shape["height"];
  VLOG(4) << "paddings: " << paddings[0] << "," << paddings[1];
  VLOG(4) << "has bias: " << has_bias;
  VLOG(4) << "is_element_wise_bias : " << is_element_wise_bias;
  VLOG(4) << "strides: " << strides[0] << "," << strides[1];
  VLOG(4) << "offset: " << offset;
  VLOG(4) << "dilations.size : " << dilations.size();
  VLOG(4) << "dilations: " << dilations[0] << ", " << dilations[1];
1606
#endif
1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619

  CHECK_GE(dilations.size(), 2);
  CHECK(dilations[0] == dilations[1]);
  CHECK_GE(input_dims.size(), 4);
  CHECK_GE(paddings.size(), 2);
  CHECK(paddings[0] == paddings[1]);
  CHECK_GE(strides.size(), 2);
  CHECK(strides[0] == strides[1]);

  // handle bias  use buffer for channel wise , use image for element wise
  const cl::Buffer* bias_buf = nullptr;
  const cl::Image2D* bias_image = nullptr;
  if (has_bias) {
1620
    bias_image = bias_gpu_image_.data<half_t, cl::Image2D>();
1621 1622
  }

1623
  auto kernel = kernel_;
1624 1625 1626

  cl_int status;
  int arg_idx = 0;
1627
  status = kernel.setArg(arg_idx, c_blk_);
1628
  CL_CHECK_FATAL(status);
1629
  status = kernel.setArg(++arg_idx, w_blk_);
1630
  CL_CHECK_FATAL(status);
1631
  status = kernel.setArg(++arg_idx, nh_blk_);
1632 1633 1634 1635 1636 1637
  CL_CHECK_FATAL(status);
  status = kernel.setArg(++arg_idx, *input_image);
  CL_CHECK_FATAL(status);
  status = kernel.setArg(++arg_idx, *filter_image);
  CL_CHECK_FATAL(status);
  if (has_bias) {
1638
#ifndef LITE_SHUTDOWN_LOG
1639
    VLOG(4) << "set bias_image: ";
1640
#endif
1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668
    status = kernel.setArg(++arg_idx, *bias_image);
    CL_CHECK_FATAL(status);
  }
  status = kernel.setArg(++arg_idx, *out_image);
  CL_CHECK_FATAL(status);
  status = kernel.setArg(++arg_idx, strides[0]);
  CL_CHECK_FATAL(status);

  status = kernel.setArg(++arg_idx, offset);
  CL_CHECK_FATAL(status);
  status = kernel.setArg(++arg_idx, input_c_block);
  CL_CHECK_FATAL(status);

  status = kernel.setArg(++arg_idx, dilations[0]);
  CL_CHECK_FATAL(status);
  status = kernel.setArg(++arg_idx, input_width);
  CL_CHECK_FATAL(status);
  status = kernel.setArg(++arg_idx, input_height);
  CL_CHECK_FATAL(status);
  status = kernel.setArg(++arg_idx, output_width);
  CL_CHECK_FATAL(status);
  status = kernel.setArg(++arg_idx, output_height);
  CL_CHECK_FATAL(status);
  status = kernel.setArg(++arg_idx, filter_width);
  CL_CHECK_FATAL(status);
  status = kernel.setArg(++arg_idx, filter_height);
  CL_CHECK_FATAL(status);

1669
#ifndef LITE_SHUTDOWN_LOG
1670
  //  VLOG(4) << "out_image: " << out_image;
1671 1672
  VLOG(4) << "global_work_size_[3D]: {" << global_work_size_[0] << ","
          << global_work_size_[1] << "," << global_work_size_[2] << "}";
1673
#endif
1674 1675 1676 1677

  status = context.cl_context()->GetCommandQueue().enqueueNDRangeKernel(
      kernel,
      cl::NullRange,
1678
      global_work_size_,
1679 1680 1681 1682 1683 1684 1685
      cl::NullRange,
      nullptr,
      event_.get());
  CL_CHECK_FATAL(status);
  context.cl_wait_list()->emplace(out_image, event_);
}

1686
void ConvImageCompute::Run() { (this->*impl_)(); }
1687
#undef PROFILE_CONV_KERNEL
Y
Yan Chunwei 已提交
1688 1689 1690 1691 1692 1693 1694
}  // namespace opencl
}  // namespace kernels
}  // namespace lite
}  // namespace paddle

REGISTER_LITE_KERNEL(conv2d,
                     kOpenCL,
1695
                     kFP16,
1696 1697 1698 1699 1700
                     kImageDefault,
                     paddle::lite::kernels::opencl::ConvImageCompute,
                     image2d)
    .BindInput("Input",
               {LiteType::GetTensorTy(TARGET(kOpenCL),
1701
                                      PRECISION(kFP16),
1702 1703 1704 1705 1706
                                      DATALAYOUT(kImageDefault))})
    .BindInput("Bias", {LiteType::GetTensorTy(TARGET(kARM))})
    .BindInput("Filter", {LiteType::GetTensorTy(TARGET(kARM))})
    .BindOutput("Output",
                {LiteType::GetTensorTy(TARGET(kOpenCL),
1707
                                       PRECISION(kFP16),
1708
                                       DATALAYOUT(kImageDefault))})
Y
Yan Chunwei 已提交
1709
    .Finalize();
1710

1711
REGISTER_LITE_KERNEL(depthwise_conv2d,
1712
                     kOpenCL,
1713
                     kFP16,
1714 1715 1716 1717 1718
                     kImageDefault,
                     paddle::lite::kernels::opencl::ConvImageCompute,
                     image2d)
    .BindInput("Input",
               {LiteType::GetTensorTy(TARGET(kOpenCL),
1719
                                      PRECISION(kFP16),
1720 1721 1722 1723 1724
                                      DATALAYOUT(kImageDefault))})
    .BindInput("Bias", {LiteType::GetTensorTy(TARGET(kARM))})
    .BindInput("Filter", {LiteType::GetTensorTy(TARGET(kARM))})
    .BindOutput("Output",
                {LiteType::GetTensorTy(TARGET(kOpenCL),
1725
                                       PRECISION(kFP16),
1726 1727
                                       DATALAYOUT(kImageDefault))})
    .Finalize();