conv_image_compute.cc 64.2 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
#include "lite/operators/op_params.h"

namespace paddle {
namespace lite {
namespace kernels {
namespace opencl {
29 30 31 32 33 34 35 36 37 38 39
/* 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);

40
  filter_gpu_image_ = std::unique_ptr<Tensor>(new Tensor);
X
xiebaiyuan 已提交
41 42
  tensor_hold_filter_image_ = std::unique_ptr<Tensor>(new Tensor);
  tensor_hold_bias_image_ = std::unique_ptr<Tensor>(new Tensor);
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 71 72
  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];
73
  VLOG(3) << "dialtion:" << dilations[0] << " " << dilations[1];
74 75 76 77
  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];
78 79 80 81 82 83 84
  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);

85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
  // 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_)};

104 105
  if (kernel_h == 1 && kernel_w == 1) {
    // conv2d_1x1
X
xiebaiyuan 已提交
106 107 108 109 110 111
    // if (param.x->dims()[1] % 4 == 0) {
    //   kernel_func_names_.push_back("conv2d_1x1_simple");
    // } else {
    //   kernel_func_names_.push_back("conv2d_1x1_opt");
    // }

112 113 114
    if (param.x->dims()[1] % 4 == 0) {
      kernel_func_names_.push_back("conv2d_1x1_simple");
    } else {
115
      kernel_func_names_.push_back("conv2d_1x1_opt");
116
    }
117
    kernel_func_paths_.push_back("image/conv2d_1x1_opt_kernel.cl");
118 119 120

    CLImageConverterNWBlock converter;
    const DDim& filter_image_dims = converter.InitImageDimInfoWith(filter_dims);
X
xiebaiyuan 已提交
121 122 123 124 125 126 127 128 129 130
    // std::vector<half_t> filter_image_v(filter_image_dims[0] *
    //                                    filter_image_dims[1] * 4);  // 4 :
    //                                    RGBA
    tensor_hold_filter_image_->Resize(
        {1, filter_image_dims[0], filter_image_dims[1], 4});

    half_t* filter_image_data =
        tensor_hold_filter_image_->mutable_data<half_t>();

    converter.NCHWToImage(filter_cpu, filter_image_data, filter_dims);
131
    filter_gpu_image_->mutable_data<half_t, cl::Image2D>(
X
xiebaiyuan 已提交
132
        filter_image_dims[0], filter_image_dims[1], filter_image_data);
133

134
    impl_ = &ConvImageCompute::Conv2d1x1opt;
135 136 137 138 139 140 141 142 143
    {
      // 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_)};
    }
144
#define DEPTH_CONV_USE_SPL
145
#ifdef DEPTH_CONV_USE_SPL
146 147 148 149 150 151
  } 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;
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
      {
        // 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_)};
      }
167 168 169
    } else {
      kernel_func_names_.push_back("depth_conv2d_3x3");
      impl_ = &ConvImageCompute::DepthwiseConv2d3x3;
170 171 172 173 174 175 176 177 178 179 180 181 182 183
      {
        // 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_)};
      }
184 185 186
    }
    kernel_func_paths_.push_back("image/depthwise_conv2d_kernel.cl");

187
    CLImageConverterNWBlock converter;
188
    const DDim& filter_image_dims = converter.InitImageDimInfoWith(filter_dims);
X
xiebaiyuan 已提交
189 190 191 192 193 194 195
    tensor_hold_filter_image_->Resize(
        {1, filter_image_dims[0], filter_image_dims[1], 4});

    half_t* filter_image_data =
        tensor_hold_filter_image_->mutable_data<half_t>();

    converter.NCHWToImage(filter_cpu, filter_image_data, filter_dims);
196
    filter_gpu_image_->mutable_data<half_t, cl::Image2D>(
X
xiebaiyuan 已提交
197
        filter_image_dims[0], filter_image_dims[1], filter_image_data);
198 199 200 201 202 203 204

#endif
  } else if (filter_dims[1] == 1 && x_dims[1] == output_dims[1]
#ifdef DEPTH_CONV_USE_SPL
             &&
             kernel_h != 3
#endif
205
#undef DEPTH_CONV_USE_SPL
206
             ) {
207 208 209 210
    // depth_conv2d
    kernel_func_names_.push_back("depth_conv2d");
    kernel_func_paths_.push_back("image/depthwise_conv2d_basic_kernel.cl");

211
    CLImageConverterNWBlock converter;
212
    const DDim& filter_image_dims = converter.InitImageDimInfoWith(filter_dims);
X
xiebaiyuan 已提交
213 214 215 216 217 218 219
    tensor_hold_filter_image_->Resize(
        {1, filter_image_dims[0], filter_image_dims[1], 4});

    half_t* filter_image_data =
        tensor_hold_filter_image_->mutable_data<half_t>();

    converter.NCHWToImage(filter_cpu, filter_image_data, filter_dims);
220
    filter_gpu_image_->mutable_data<half_t, cl::Image2D>(
X
xiebaiyuan 已提交
221
        filter_image_dims[0], filter_image_dims[1], filter_image_data);
222 223

    impl_ = &ConvImageCompute::DepthwiseConv2d;
224
  } else if (kernel_w == 3 && kernel_h == 3) {
X
xiebaiyuan 已提交
225 226
// #define CONV3x3OPT_FALL_BACK
#ifndef CONV3x3OPT_FALL_BACK
227
    // conv2d_3x3
228 229
    kernel_func_names_.push_back(bs > 1 ? "conv2d_3x3_multi_batch"
                                        : "conv2d_3x3_opt");
230
    kernel_func_paths_.push_back("image/conv2d_3x3_opt_kernel.cl");
231 232 233

    CLImageConverterFolder converter;
    const DDim& filter_image_dims = converter.InitImageDimInfoWith(filter_dims);
X
xiebaiyuan 已提交
234 235 236 237 238 239 240
    tensor_hold_filter_image_->Resize(
        {1, filter_image_dims[0], filter_image_dims[1], 4});

    half_t* filter_image_data =
        tensor_hold_filter_image_->mutable_data<half_t>();

    converter.NCHWToImage(filter_cpu, filter_image_data, filter_dims);
241
    filter_gpu_image_->mutable_data<half_t, cl::Image2D>(
X
xiebaiyuan 已提交
242
        filter_image_dims[0], filter_image_dims[1], filter_image_data);
243

244
    impl_ = &ConvImageCompute::Conv2d3x3opt;
X
xiebaiyuan 已提交
245

246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
    {
      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_)};
    }
X
xiebaiyuan 已提交
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
#else
    kernel_func_names_.push_back("conv2d_3x3");
    kernel_func_paths_.push_back("image/conv2d_3x3_kernel.cl");

    CLImageConverterFolder converter;
    const DDim& filter_image_dims = converter.InitImageDimInfoWith(filter_dims);
    tensor_hold_filter_image_->Resize(
        {1, filter_image_dims[0], filter_image_dims[1], 4});

    half_t* filter_image_data =
        tensor_hold_filter_image_->mutable_data<half_t>();

    converter.NCHWToImage(filter_cpu, filter_image_data, filter_dims);
    filter_gpu_image_->mutable_data<half_t, cl::Image2D>(
        filter_image_dims[0], filter_image_dims[1], filter_image_data);

    impl_ = &ConvImageCompute::Conv2d3x3;

#endif
#undef CONV3x3OPT_FALL_BACK
281

282
  } else if (kernel_h == 5 && kernel_w == 5) {
283 284
#define CONV_5x5_OPT
#ifndef CONV_5x5_OPT
285 286 287 288 289 290
    // 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);
X
xiebaiyuan 已提交
291 292 293 294 295 296 297
    tensor_hold_filter_image_->Resize(
        {1, filter_image_dims[0], filter_image_dims[1], 4});

    half_t* filter_image_data =
        tensor_hold_filter_image_->mutable_data<half_t>();

    converter.NCHWToImage(filter_cpu, filter_image_data, filter_dims);
298
    filter_gpu_image_->mutable_data<half_t, cl::Image2D>(
X
xiebaiyuan 已提交
299
        filter_image_dims[0], filter_image_dims[1], filter_image_data);
300 301

    impl_ = &ConvImageCompute::Conv2d5x5;
302 303
#else
    // conv2d_5x5_opt
304 305 306

    kernel_func_names_.push_back(bs > 1 ? "conv2d_5x5_multi_batch"
                                        : "conv2d_5x5_opt");
307 308 309 310
    kernel_func_paths_.push_back("image/conv2d_5x5_opt_kernel.cl");

    CLImageConverterFolder converter;
    const DDim& filter_image_dims = converter.InitImageDimInfoWith(filter_dims);
X
xiebaiyuan 已提交
311 312 313 314 315 316 317
    tensor_hold_filter_image_->Resize(
        {1, filter_image_dims[0], filter_image_dims[1], 4});

    half_t* filter_image_data =
        tensor_hold_filter_image_->mutable_data<half_t>();

    converter.NCHWToImage(filter_cpu, filter_image_data, filter_dims);
318
    filter_gpu_image_->mutable_data<half_t, cl::Image2D>(
X
xiebaiyuan 已提交
319
        filter_image_dims[0], filter_image_dims[1], filter_image_data);
320 321

    impl_ = &ConvImageCompute::Conv2d5x5opt;
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
    {
      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_)};
    }
337 338
#endif
#undef CONV_5x5_OPT
339
  } else if (kernel_h == 7 && kernel_w == 7) {
340 341
#define CONV_7x7_OPT
#ifndef CONV_7x7_OPT
342 343 344 345 346 347
    // 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);
X
xiebaiyuan 已提交
348 349 350 351 352 353 354 355 356
    tensor_hold_filter_image_->Resize(
        {1, filter_image_dims[0], filter_image_dims[1], 4});

    half_t* filter_image_data =
        tensor_hold_filter_image_->mutable_data<half_t>();

    converter.NCHWToImage(filter_cpu, filter_image_data, filter_dims);
    filter_gpu_image_->mutable_data<half_t, cl::Image2D>(
        filter_image_dims[0], filter_image_dims[1], filter_image_data);
357 358

    impl_ = &ConvImageCompute::Conv2d7x7;
359 360 361

#else
    // conv2d_7x7
362 363
    kernel_func_names_.push_back(bs > 1 ? "conv2d_7x7_multi_batch"
                                        : "conv2d_7x7_opt");
364 365 366 367
    kernel_func_paths_.push_back("image/conv2d_7x7_opt_kernel.cl");

    CLImageConverterFolder converter;
    const DDim& filter_image_dims = converter.InitImageDimInfoWith(filter_dims);
X
xiebaiyuan 已提交
368 369 370 371 372 373 374 375 376
    tensor_hold_filter_image_->Resize(
        {1, filter_image_dims[0], filter_image_dims[1], 4});

    half_t* filter_image_data =
        tensor_hold_filter_image_->mutable_data<half_t>();

    converter.NCHWToImage(filter_cpu, filter_image_data, filter_dims);
    filter_gpu_image_->mutable_data<half_t, cl::Image2D>(
        filter_image_dims[0], filter_image_dims[1], filter_image_data);
377 378

    impl_ = &ConvImageCompute::Conv2d7x7opt;
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
    {
      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_)};
    }
394 395 396
#endif
#undef CONV_7x7_OPT

397 398 399
  } else {
    LOG(FATAL) << "conv image compute not support this condition yet! ";
  }
400 401
  VLOG(1) << "kernel_func_names_[0]:" << kernel_func_names_[0]
          << " kernel_func_paths_[0]:" << kernel_func_paths_[0];
402

403
  // build options
404
  std::string build_options_single(" -DCL_DTYPE_half");
405
  // relu options
406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
  VLOG(3) << "relu_fused:" << relu_fused
          << " param.activation_param.active_type:"
          << static_cast<int>(param.activation_param.active_type)
          << " param.activation_param.has_active:"
          << param.activation_param.has_active;
  if (param.activation_param.has_active) {
    if (param.activation_param.active_type ==
        lite_api::ActivationType::kRelu) {  // Note: judge using `relu_fused`
                                            // also is ok
      build_options_single += " -DRELU";
    } else if (param.activation_param.active_type ==
               lite_api::ActivationType::kRelu6) {
      build_options_single += " -DRELU6";
    } else {
      LOG(FATAL) << "Unsupported activation type:"
                 << static_cast<int>(param.activation_param.active_type);
    }
423
  }
424

425 426 427 428 429
  // 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) {
430
    bias_gpu_image_ = std::unique_ptr<Tensor>(new Tensor);
431 432 433 434 435 436 437
    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());
X
xiebaiyuan 已提交
438 439 440 441 442 443

    tensor_hold_bias_image_->Resize(
        {1, bias_image_dims[0], bias_image_dims[1], 4});

    half_t* bias_image_data = tensor_hold_bias_image_->mutable_data<half_t>();

444 445
    float* bias_cpu_data = param.bias->mutable_data<float>();
    bias_converter.NCHWToImage(
X
xiebaiyuan 已提交
446
        bias_cpu_data, bias_image_data, param.bias->dims());
447
    this->bias_gpu_image_->mutable_data<half_t, cl::Image2D>(
X
xiebaiyuan 已提交
448
        bias_image_dims[0], bias_image_dims[1], bias_image_data);
449 450 451 452 453 454
    // 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++) {
455 456 457 458
    context.cl_context()->AddKernel(kernel_func_names_[i],
                                    kernel_func_paths_[i],
                                    build_options_[i],
                                    time_stamp_);
459
  }
460 461 462 463 464

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

  std::stringstream kernel_key;
465
  kernel_key << kernel_func_names_[0] << build_options_[0] << time_stamp_;
466 467 468 469 470 471 472 473 474 475
  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;

476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506
  if (max_work_group_size > 0 && use_lws_) {
    double min_turn_time = DBL_MAX;
    cl::NDRange best_local_work_size = context.cl_context()->LocalWorkSize(
        global_work_size_, max_work_group_size);
    cl::NDRange last_local_work_size = cl::NDRange{
        static_cast<size_t>(0), static_cast<size_t>(0), static_cast<size_t>(0)};
    if (use_turn_) {
      for (size_t i = 1; i < 15; i++) {
        if (kernel_h == 1 && kernel_w == 1) {
          // todo use diff logics
          local_work_size_ = context.cl_context()->LocalWorkSizeTurn(
              global_work_size_, max_work_group_size, i);
        } else {
          local_work_size_ = context.cl_context()->LocalWorkSizeTurn(
              global_work_size_, max_work_group_size, i);
        }
        if (last_local_work_size[0] == local_work_size_[0] &&
            last_local_work_size[1] == local_work_size_[1] &&
            last_local_work_size[2] == local_work_size_[2]) {
          // skiped turned lws
          continue;
        }
        auto turn_time = this->Turn(5);
        if (min_turn_time > turn_time) {
          min_turn_time = turn_time;
          best_local_work_size = local_work_size_;
        }
        last_local_work_size = local_work_size_;
      }
    }
    local_work_size_ = best_local_work_size;
507 508 509
    VLOG(4) << "local_work_size_[3D]: {" << local_work_size_[0] << ","
            << local_work_size_[1] << "," << local_work_size_[2] << "}";
  }
510 511
}

512
void ConvImageCompute::Conv2d1x1opt(bool is_turn) {
513 514
  auto& context = ctx_->As<OpenCLContext>();
  CHECK(context.cl_context() != nullptr);
515 516 517 518
  const auto& param = *param_.get_mutable<param_t>();
  auto input_dims = param.x->dims();
  auto paddings = *param.paddings;
  auto strides = param.strides;
519
  auto* input_image = param.x->data<half_t, cl::Image2D>();
520
  auto* filter_image = filter_gpu_image_->data<half_t, cl::Image2D>();
521 522 523 524 525 526 527 528
  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);
529
  auto* out_image = param.output->mutable_data<half_t, cl::Image2D>(
530 531 532 533 534 535 536 537 538 539 540 541 542 543
      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;

544
#ifdef LITE_WITH_LOG
545 546 547 548
  //  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
549
#ifdef LITE_WITH_LOG
550 551 552 553 554
  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;
555
  //  VLOG(4) << "input_image: " << input_image;
556
  VLOG(4) << "filter_dims: " << filter_dims;
557
  //  VLOG(4) << "filter_image: " << filter_image;
558 559 560 561 562 563 564 565 566 567
  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];
568 569 570
// VLOG(4) << "default work size{c_block, w, nh}: "
//         << "{" << c_block << ", " << w << ", " << nh << ""
//         << "}";
571
#endif
572 573 574 575 576 577 578 579 580 581 582 583
  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) {
584
    bias_image = bias_gpu_image_->data<half_t, cl::Image2D>();
585 586
  }

587
  auto kernel = kernel_;
588 589
  cl_int status;
  int arg_idx = 0;
590
  status = kernel.setArg(arg_idx, c_blk_);
591
  CL_CHECK_FATAL(status);
592
  status = kernel.setArg(++arg_idx, w_blk_);
593
  CL_CHECK_FATAL(status);
594
  status = kernel.setArg(++arg_idx, nh_blk_);
595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624
  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);
625
  status = kernel.setArg(++arg_idx, default_w_blk_);
626 627 628 629 630
  CL_CHECK_FATAL(status);

  status = context.cl_context()->GetCommandQueue().enqueueNDRangeKernel(
      kernel,
      cl::NullRange,
631 632
      global_work_size_,
      local_work_size_,
633
      nullptr,
X
xiebaiyuan 已提交
634
      nullptr);
635
  CL_CHECK_FATAL(status);
636
  if (is_turn) {
X
xiebaiyuan 已提交
637
    CLRuntime::Global()->command_queue().finish();
638
  }
639
}
640

641
void ConvImageCompute::Conv2d3x3(bool is_turn) {
X
xiebaiyuan 已提交
642
  auto kernel = kernel_;
643 644 645 646 647
  const auto& param = *param_.get_mutable<param_t>();
  auto input_dims = param.x->dims();
  auto paddings = *param.paddings;
  auto strides = param.strides;

648
  auto* input_image = param.x->data<half_t, cl::Image2D>();
649
  auto* filter_image = filter_gpu_image_->data<half_t, cl::Image2D>();
650 651 652 653 654 655 656 657 658 659 660 661 662
  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);
663
  auto* out_image = param.output->mutable_data<half_t, cl::Image2D>(
664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684
      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;
  }
X
xiebaiyuan 已提交
685 686 687 688 689 690 691 692
  /* 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;
    }
  */
693

X
xiebaiyuan 已提交
694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727
  // 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];

  // 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;
  // 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) << "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;
  // VLOG(4) << "default work size{c_block, w, nh}: "
  //         << "{" << c_block << ", " << w << ", " << nh << ""
  //         << "}";
728 729 730 731 732 733 734 735 736 737 738

  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) {
739
    bias_image = bias_gpu_image_->data<half_t, cl::Image2D>();
740
  }
X
xiebaiyuan 已提交
741 742 743 744 745 746 747 748 749

  auto& context = ctx_->As<OpenCLContext>();
  CHECK(context.cl_context() != nullptr);
  // STL::stringstream kernel_key;
  // kernel_key << kernel_func_names_[0] << build_options_[0];
  // auto kernel = context.cl_context()->GetKernel(kernel_key.str());
  // VLOG(4) << "kernel_key: " << kernel_key.str();
  // VLOG(4) << "kernel ready ... " << kernel_key.str();
  // VLOG(4) << "w: " << w;
750 751 752

  cl_int status;
  int arg_idx = 0;
753
  status = kernel.setArg(arg_idx, c_blk_);
754
  CL_CHECK_FATAL(status);
755
  status = kernel.setArg(++arg_idx, w_blk_);
756
  CL_CHECK_FATAL(status);
757
  status = kernel.setArg(++arg_idx, nh_blk_);
758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797
  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) {
    VLOG(4) << "set bias_image: ";
    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);
X
xiebaiyuan 已提交
798 799
  status = kernel.setArg(++arg_idx, static_cast<int>(input_dims[1]));
  CL_CHECK_FATAL(status);
800

X
xiebaiyuan 已提交
801 802 803 804 805 806 807 808
  // auto global_work_size =
  //     cl::NDRange{static_cast<size_t>(default_work_size.data()[0]),
  //                 static_cast<size_t>(default_work_size.data()[1]),
  //                 static_cast<size_t>(default_work_size.data()[2])};

  // VLOG(4) << "out_image: " << out_image;
  // VLOG(4) << "global_work_size[3D]: {" << global_work_size[0] << ","
  //         << global_work_size[1] << "," << global_work_size[2] << "}";
809

810 811 812
  status = context.cl_context()->GetCommandQueue().enqueueNDRangeKernel(
      kernel,
      cl::NullRange,
813
      global_work_size_,
814 815
      cl::NullRange,
      nullptr,
X
xiebaiyuan 已提交
816
      nullptr);
817 818
  CL_CHECK_FATAL(status);
}
819
void ConvImageCompute::Conv2d3x3opt(bool is_turn) {
820 821
  auto& context = ctx_->As<OpenCLContext>();
  CHECK(context.cl_context() != nullptr);
822 823 824 825 826 827 828
  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>();
829
  auto* filter_image = filter_gpu_image_->data<half_t, cl::Image2D>();
830 831 832 833 834 835 836 837 838
  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];
839 840
  CHECK_EQ(input_dims[0], output_dims[0]);
  int batch = input_dims[0];
841 842 843 844 845 846 847 848
  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();

849
#ifdef LITE_WITH_LOG
850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865
  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];
866
#endif
867 868 869 870 871 872 873 874 875 876 877

  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) {
878
    bias_image = bias_gpu_image_->data<half_t, cl::Image2D>();
879 880
  }

881
  auto kernel = kernel_;
882 883 884

  cl_int status;
  int arg_idx = 0;
885
  status = kernel.setArg(arg_idx, c_blk_);
886
  CL_CHECK_FATAL(status);
887
  status = kernel.setArg(++arg_idx, w_blk_);
888
  CL_CHECK_FATAL(status);
889
  status = kernel.setArg(++arg_idx, nh_blk_);
890 891 892 893 894 895
  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) {
896
#ifdef LITE_WITH_LOG
897
    VLOG(4) << "set bias_image: ";
898
#endif
899 900 901 902 903 904 905 906 907 908 909 910 911
    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);
912 913
  status = kernel.setArg(++arg_idx, batch);
  CL_CHECK_FATAL(status);
914 915 916 917 918 919 920 921 922 923 924
  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);

925
#ifdef LITE_WITH_LOG
926
  //  VLOG(4) << "out_image: " << out_image;
927 928
  VLOG(4) << "global_work_size_[3D]: {" << global_work_size_[0] << ","
          << global_work_size_[1] << "," << global_work_size_[2] << "}";
929
#endif
930

931 932 933
  status = context.cl_context()->GetCommandQueue().enqueueNDRangeKernel(
      kernel,
      cl::NullRange,
934 935
      global_work_size_,
      local_work_size_,
936
      nullptr,
X
xiebaiyuan 已提交
937
      nullptr);
938
  CL_CHECK_FATAL(status);
939
  if (is_turn) {
X
xiebaiyuan 已提交
940
    CLRuntime::Global()->command_queue().finish();
941
  }
942 943
}

944
void ConvImageCompute::Conv2d5x5(bool is_turn) {
945 946
  auto& context = ctx_->As<OpenCLContext>();
  CHECK(context.cl_context() != nullptr);
947 948 949 950
  const auto& param = *param_.get_mutable<param_t>();
  auto input_dims = param.x->dims();
  auto paddings = *param.paddings;
  auto strides = param.strides;
951
  auto* input_image = param.x->data<half_t, cl::Image2D>();
952
  auto* filter_image = filter_gpu_image_->data<half_t, cl::Image2D>();
953 954 955 956 957 958 959 960 961 962
  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);
963
  auto* out_image = param.output->mutable_data<half_t, cl::Image2D>(
964 965 966 967 968 969 970 971 972 973 974 975 976 977
      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;

978
#ifdef LITE_WITH_LOG
979 980 981 982 983
  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;
984
  //  VLOG(4) << "input_image: " << input_image;
985 986
  VLOG(4) << "input_dims: " << input_dims;
  VLOG(4) << "filter_dims: " << filter_dims;
987
  //  VLOG(4) << "filter_image: " << filter_image;
988 989 990 991 992 993 994 995 996 997
  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];
998
#endif
999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009

  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) {
1010
    bias_image = bias_gpu_image_->data<half_t, cl::Image2D>();
1011 1012
  }

1013
  auto kernel = kernel_;
1014 1015 1016

  cl_int status;
  int arg_idx = 0;
1017
  status = kernel.setArg(arg_idx, c_blk_);
1018
  CL_CHECK_FATAL(status);
1019
  status = kernel.setArg(++arg_idx, w_blk_);
1020
  CL_CHECK_FATAL(status);
1021
  status = kernel.setArg(++arg_idx, nh_blk_);
1022 1023 1024 1025 1026 1027
  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) {
1028
#ifdef LITE_WITH_LOG
1029
    VLOG(4) << "set bias_image: ";
1030
#endif
1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054
    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);

1055
#ifdef LITE_WITH_LOG
1056
  //  VLOG(4) << "out_image: " << out_image;
1057 1058
  VLOG(4) << "global_work_size_[3D]: {" << global_work_size_[0] << ","
          << global_work_size_[1] << "," << global_work_size_[2] << "}";
1059
#endif
1060 1061 1062 1063

  status = context.cl_context()->GetCommandQueue().enqueueNDRangeKernel(
      kernel,
      cl::NullRange,
1064
      global_work_size_,
1065 1066
      cl::NullRange,
      nullptr,
X
xiebaiyuan 已提交
1067
      nullptr);
1068
  CL_CHECK_FATAL(status);
1069
  if (is_turn) {
X
xiebaiyuan 已提交
1070
    CLRuntime::Global()->command_queue().finish();
1071
  }
1072
}
1073

1074
void ConvImageCompute::Conv2d5x5opt(bool is_turn) {
1075 1076
  auto& context = ctx_->As<OpenCLContext>();
  CHECK(context.cl_context() != nullptr);
1077 1078 1079 1080 1081 1082 1083
  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>();
1084
  auto* filter_image = filter_gpu_image_->data<half_t, cl::Image2D>();
1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105
  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;
1106
#ifdef LITE_WITH_LOG
1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133
  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) {
1134
    bias_image = bias_gpu_image_->data<half_t, cl::Image2D>();
1135 1136
  }

1137
  auto kernel = kernel_;
1138 1139
  cl_int status;
  int arg_idx = 0;
1140
  status = kernel.setArg(arg_idx, c_blk_);
1141
  CL_CHECK_FATAL(status);
1142
  status = kernel.setArg(++arg_idx, w_blk_);
1143
  CL_CHECK_FATAL(status);
1144
  status = kernel.setArg(++arg_idx, nh_blk_);
1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176
  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);

1177
  //  VLOG(4) << "out_image: " << out_image;
1178 1179 1180 1181

  status = context.cl_context()->GetCommandQueue().enqueueNDRangeKernel(
      kernel,
      cl::NullRange,
1182 1183
      global_work_size_,
      local_work_size_,
1184
      nullptr,
X
xiebaiyuan 已提交
1185
      nullptr);
1186
  CL_CHECK_FATAL(status);
1187
  if (is_turn) {
X
xiebaiyuan 已提交
1188
    CLRuntime::Global()->command_queue().finish();
1189
  }
1190 1191
}

1192
void ConvImageCompute::Conv2d7x7(bool is_turn) {
1193 1194
  auto& context = ctx_->As<OpenCLContext>();
  CHECK(context.cl_context() != nullptr);
1195 1196 1197 1198
  const auto& param = *param_.get_mutable<param_t>();
  auto input_dims = param.x->dims();
  auto paddings = *param.paddings;
  auto strides = param.strides;
1199
  auto* input_image = param.x->data<half_t, cl::Image2D>();
1200
  auto* filter_image = filter_gpu_image_->data<half_t, cl::Image2D>();
1201 1202 1203 1204 1205 1206 1207 1208 1209 1210
  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);
1211
  auto* out_image = param.output->mutable_data<half_t, cl::Image2D>(
1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225
      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;

1226
#ifdef LITE_WITH_LOG
1227 1228 1229 1230 1231
  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;
1232
  //  VLOG(4) << "input_image: " << input_image;
1233 1234
  VLOG(4) << "input_dims: " << input_dims;
  VLOG(4) << "filter_dims: " << filter_dims;
1235
  //  VLOG(4) << "filter_image: " << filter_image;
1236 1237 1238 1239 1240 1241 1242 1243 1244 1245
  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];
1246
#endif
1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257

  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) {
1258
    bias_image = bias_gpu_image_->data<half_t, cl::Image2D>();
1259 1260
  }

1261
  auto kernel = kernel_;
1262 1263 1264

  cl_int status;
  int arg_idx = 0;
1265
  status = kernel.setArg(arg_idx, c_blk_);
1266
  CL_CHECK_FATAL(status);
1267
  status = kernel.setArg(++arg_idx, w_blk_);
1268
  CL_CHECK_FATAL(status);
1269
  status = kernel.setArg(++arg_idx, nh_blk_);
1270 1271 1272 1273 1274 1275
  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) {
1276
#ifdef LITE_WITH_LOG
1277
    VLOG(4) << "set bias_image: ";
1278
#endif
1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302
    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);

1303
#ifdef LITE_WITH_LOG
1304
  //  VLOG(4) << "out_image: " << out_image;
1305 1306
  VLOG(4) << "global_work_size_[3D]: {" << global_work_size_[0] << ","
          << global_work_size_[1] << "," << global_work_size_[2] << "}";
1307
#endif
1308 1309 1310 1311

  status = context.cl_context()->GetCommandQueue().enqueueNDRangeKernel(
      kernel,
      cl::NullRange,
1312
      global_work_size_,
1313 1314
      cl::NullRange,
      nullptr,
X
xiebaiyuan 已提交
1315
      nullptr);
1316
  CL_CHECK_FATAL(status);
1317 1318

  if (is_turn) {
X
xiebaiyuan 已提交
1319
    CLRuntime::Global()->command_queue().finish();
1320
  }
1321
}
1322
void ConvImageCompute::Conv2d7x7opt(bool is_turn) {
1323 1324
  auto& context = ctx_->As<OpenCLContext>();
  CHECK(context.cl_context() != nullptr);
1325 1326 1327 1328 1329 1330 1331
  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>();
1332
  auto* filter_image = filter_gpu_image_->data<half_t, cl::Image2D>();
1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351
  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();

1352
#ifdef LITE_WITH_LOG
1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379
  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) {
1380
    bias_image = bias_gpu_image_->data<half_t, cl::Image2D>();
1381 1382
  }

1383
  auto kernel = kernel_;
1384

1385 1386
  cl_int status;
  int arg_idx = 0;
1387
  status = kernel.setArg(arg_idx, c_blk_);
1388
  CL_CHECK_FATAL(status);
1389
  status = kernel.setArg(++arg_idx, w_blk_);
1390
  CL_CHECK_FATAL(status);
1391
  status = kernel.setArg(++arg_idx, nh_blk_);
1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426
  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,
1427 1428
      global_work_size_,
      local_work_size_,
1429
      nullptr,
X
xiebaiyuan 已提交
1430
      nullptr);
1431
  CL_CHECK_FATAL(status);
1432 1433

  if (is_turn) {
X
xiebaiyuan 已提交
1434
    CLRuntime::Global()->command_queue().finish();
1435
  }
1436
}
1437
void ConvImageCompute::DepthwiseConv2d3x3s1(bool is_turn) {
1438 1439
  auto& context = ctx_->As<OpenCLContext>();
  CHECK(context.cl_context() != nullptr);
1440 1441 1442 1443 1444 1445 1446 1447
  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;

1448
  auto* input_img = param.x->data<half_t, cl::Image2D>();
1449
  auto* filter_img = filter_gpu_image_->data<half_t, cl::Image2D>();
1450 1451 1452

  const cl::Image2D* bias_img = nullptr;
  if (param.bias) {
1453
    bias_img = bias_gpu_image_->data<half_t, cl::Image2D>();
1454 1455 1456 1457
  }

  auto image_shape = InitImageDimInfoWith(output_dims);

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

1461
  auto kernel = kernel_;
1462 1463 1464

  cl_int status;
  int arg_idx = 0;
1465
  status = kernel.setArg(arg_idx, c_blk_);
1466
  CL_CHECK_FATAL(status);
1467
  status = kernel.setArg(++arg_idx, w_blk_);
1468
  CL_CHECK_FATAL(status);
1469
  status = kernel.setArg(++arg_idx, nh_blk_);
1470 1471 1472 1473 1474
  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);
1475 1476 1477 1478 1479 1480

  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) {
1481
    bias_image = bias_gpu_image_->data<half_t, cl::Image2D>();
1482
#ifdef LITE_WITH_LOG
1483
    VLOG(4) << "set bias_image: ";
1484
#endif
1485 1486 1487
    status = kernel.setArg(++arg_idx, *bias_image);
    CL_CHECK_FATAL(status);
  }
1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509
  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,
1510 1511
      global_work_size_,
      local_work_size_,
1512
      nullptr,
X
xiebaiyuan 已提交
1513
      nullptr);
1514
  CL_CHECK_FATAL(status);
1515 1516

  if (is_turn) {
X
xiebaiyuan 已提交
1517
    CLRuntime::Global()->command_queue().finish();
1518
  }
1519 1520
}

1521
void ConvImageCompute::DepthwiseConv2d3x3(bool is_turn) {
1522 1523
  auto& context = ctx_->As<OpenCLContext>();
  CHECK(context.cl_context() != nullptr);
1524 1525 1526 1527 1528 1529 1530 1531 1532 1533
  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;

1534
  auto* input_img = param.x->data<half_t, cl::Image2D>();
1535
  auto* filter_img = filter_gpu_image_->data<half_t, cl::Image2D>();
1536 1537 1538

  const cl::Image2D* bias_img = nullptr;
  if (param.bias) {
1539
    bias_img = bias_gpu_image_->data<half_t, cl::Image2D>();
1540 1541 1542 1543
  }

  auto image_shape = InitImageDimInfoWith(output_dims);

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

1547
  auto kernel = kernel_;
1548

1549
#ifdef LITE_WITH_LOG
1550 1551 1552 1553 1554 1555 1556 1557 1558
  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];
1559
#endif
1560 1561 1562

  cl_int status;
  int arg_idx = 0;
1563
  status = kernel.setArg(arg_idx, c_blk_);
1564
  CL_CHECK_FATAL(status);
1565
  status = kernel.setArg(++arg_idx, w_blk_);
1566
  CL_CHECK_FATAL(status);
1567
  status = kernel.setArg(++arg_idx, nh_blk_);
1568 1569 1570 1571 1572
  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);
1573 1574 1575 1576 1577
  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) {
1578
    bias_image = bias_gpu_image_->data<half_t, cl::Image2D>();
1579
#ifdef LITE_WITH_LOG
1580
    VLOG(4) << "set bias_image: ";
1581
#endif
1582 1583 1584
    status = kernel.setArg(++arg_idx, *bias_image);
    CL_CHECK_FATAL(status);
  }
1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606
  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,
1607
      global_work_size_,
1608 1609
      cl::NullRange,
      nullptr,
X
xiebaiyuan 已提交
1610
      nullptr);
1611
  CL_CHECK_FATAL(status);
1612 1613

  if (is_turn) {
X
xiebaiyuan 已提交
1614
    CLRuntime::Global()->command_queue().finish();
1615
  }
1616 1617
}

1618
void ConvImageCompute::DepthwiseConv2d(bool is_turn) {
1619 1620
  auto& context = ctx_->As<OpenCLContext>();
  CHECK(context.cl_context() != nullptr);
1621 1622 1623 1624
  const auto& param = *param_.get_mutable<param_t>();
  auto input_dims = param.x->dims();
  auto paddings = *param.paddings;
  auto strides = param.strides;
1625
  auto* input_image = param.x->data<half_t, cl::Image2D>();
1626
  auto* filter_image = filter_gpu_image_->data<half_t, cl::Image2D>();
1627 1628 1629 1630 1631 1632 1633 1634 1635 1636
  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);
1637
  auto* out_image = param.output->mutable_data<half_t, cl::Image2D>(
1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651
      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;

1652
#ifdef LITE_WITH_LOG
1653 1654 1655 1656 1657
  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;
1658
  //  VLOG(4) << "input_image: " << input_image;
1659
  VLOG(4) << "filter_dims: " << filter_dims;
1660
  //  VLOG(4) << "filter_image: " << filter_image;
1661 1662 1663 1664 1665 1666 1667 1668 1669 1670
  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];
1671
#endif
1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684

  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) {
1685
    bias_image = bias_gpu_image_->data<half_t, cl::Image2D>();
1686 1687
  }

1688
  auto kernel = kernel_;
1689 1690 1691

  cl_int status;
  int arg_idx = 0;
1692
  status = kernel.setArg(arg_idx, c_blk_);
1693
  CL_CHECK_FATAL(status);
1694
  status = kernel.setArg(++arg_idx, w_blk_);
1695
  CL_CHECK_FATAL(status);
1696
  status = kernel.setArg(++arg_idx, nh_blk_);
1697 1698 1699 1700 1701 1702
  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) {
1703
#ifdef LITE_WITH_LOG
1704
    VLOG(4) << "set bias_image: ";
1705
#endif
1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733
    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);

1734
#ifdef LITE_WITH_LOG
1735 1736
  VLOG(4) << "global_work_size_[3D]: {" << global_work_size_[0] << ","
          << global_work_size_[1] << "," << global_work_size_[2] << "}";
1737
#endif
1738 1739 1740 1741

  status = context.cl_context()->GetCommandQueue().enqueueNDRangeKernel(
      kernel,
      cl::NullRange,
1742
      global_work_size_,
1743 1744
      cl::NullRange,
      nullptr,
X
xiebaiyuan 已提交
1745
      nullptr);
1746 1747 1748
  CL_CHECK_FATAL(status);
}

1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764
void ConvImageCompute::Run() { (this->*impl_)(false); }

double ConvImageCompute::Turn(int times) {
  auto GetCurrentUS = []() -> double {
    struct timeval time;
    gettimeofday(&time, NULL);
    return 1e+6 * time.tv_sec + time.tv_usec;
  };
  auto start = GetCurrentUS();
  for (size_t i = 0; i < times; i++) {
    (this->*impl_)(true);
  }
  auto time_diff = (GetCurrentUS() - start) / times;
  return time_diff;
}

Y
Yan Chunwei 已提交
1765 1766 1767 1768 1769 1770 1771
}  // namespace opencl
}  // namespace kernels
}  // namespace lite
}  // namespace paddle

REGISTER_LITE_KERNEL(conv2d,
                     kOpenCL,
1772
                     kFP16,
1773 1774 1775 1776 1777
                     kImageDefault,
                     paddle::lite::kernels::opencl::ConvImageCompute,
                     image2d)
    .BindInput("Input",
               {LiteType::GetTensorTy(TARGET(kOpenCL),
1778
                                      PRECISION(kFP16),
1779 1780 1781 1782 1783
                                      DATALAYOUT(kImageDefault))})
    .BindInput("Bias", {LiteType::GetTensorTy(TARGET(kARM))})
    .BindInput("Filter", {LiteType::GetTensorTy(TARGET(kARM))})
    .BindOutput("Output",
                {LiteType::GetTensorTy(TARGET(kOpenCL),
1784
                                       PRECISION(kFP16),
1785
                                       DATALAYOUT(kImageDefault))})
Y
Yan Chunwei 已提交
1786
    .Finalize();
1787

1788
REGISTER_LITE_KERNEL(depthwise_conv2d,
1789
                     kOpenCL,
1790
                     kFP16,
1791 1792 1793 1794 1795
                     kImageDefault,
                     paddle::lite::kernels::opencl::ConvImageCompute,
                     image2d)
    .BindInput("Input",
               {LiteType::GetTensorTy(TARGET(kOpenCL),
1796
                                      PRECISION(kFP16),
1797 1798 1799 1800 1801
                                      DATALAYOUT(kImageDefault))})
    .BindInput("Bias", {LiteType::GetTensorTy(TARGET(kARM))})
    .BindInput("Filter", {LiteType::GetTensorTy(TARGET(kARM))})
    .BindOutput("Output",
                {LiteType::GetTensorTy(TARGET(kOpenCL),
1802
                                       PRECISION(kFP16),
1803 1804
                                       DATALAYOUT(kImageDefault))})
    .Finalize();