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

25 26
#undef LITE_WITH_LOG

Y
Yan Chunwei 已提交
27 28 29 30
namespace paddle {
namespace lite {
namespace kernels {
namespace opencl {
31 32 33 34 35 36 37 38 39 40
/* 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);
41
  const bool is_mali = context.cl_context()->IsArmMali();
42
  filter_gpu_image_ = std::unique_ptr<Tensor>(new Tensor);
X
xiebaiyuan 已提交
43 44
  tensor_hold_filter_image_ = std::unique_ptr<Tensor>(new Tensor);
  tensor_hold_bias_image_ = std::unique_ptr<Tensor>(new Tensor);
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
  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];

68
  VLOG(3) << "Is arm mali  / " << (is_mali ? "Yes" : "No");
69 70 71 72 73 74 75
  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];
76
  VLOG(3) << "dialtion:" << dilations[0] << " " << dilations[1];
77 78 79 80
  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];
81 82 83 84 85 86 87
  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);

88 89 90
  if (!is_mali) {
    use_turn_ = false;
  }
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
  // 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_)};

110 111
  if (kernel_h == 1 && kernel_w == 1) {
    // conv2d_1x1
X
xiebaiyuan 已提交
112 113 114 115 116 117
    // if (param.x->dims()[1] % 4 == 0) {
    //   kernel_func_names_.push_back("conv2d_1x1_simple");
    // } else {
    //   kernel_func_names_.push_back("conv2d_1x1_opt");
    // }

118 119 120
    if (param.x->dims()[1] % 4 == 0) {
      kernel_func_names_.push_back("conv2d_1x1_simple");
    } else {
121
      kernel_func_names_.push_back("conv2d_1x1_opt");
122
    }
123
    kernel_func_paths_.push_back("image/conv2d_1x1_opt_kernel.cl");
124 125 126

    CLImageConverterNWBlock converter;
    const DDim& filter_image_dims = converter.InitImageDimInfoWith(filter_dims);
X
xiebaiyuan 已提交
127 128 129 130 131 132 133 134 135 136
    // 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);
137
    filter_gpu_image_->mutable_data<half_t, cl::Image2D>(
X
xiebaiyuan 已提交
138
        filter_image_dims[0], filter_image_dims[1], filter_image_data);
139

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

193
    CLImageConverterNWBlock converter;
194
    const DDim& filter_image_dims = converter.InitImageDimInfoWith(filter_dims);
X
xiebaiyuan 已提交
195 196 197 198 199 200 201
    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);
202
    filter_gpu_image_->mutable_data<half_t, cl::Image2D>(
X
xiebaiyuan 已提交
203
        filter_image_dims[0], filter_image_dims[1], filter_image_data);
204 205 206 207 208 209 210

#endif
  } else if (filter_dims[1] == 1 && x_dims[1] == output_dims[1]
#ifdef DEPTH_CONV_USE_SPL
             &&
             kernel_h != 3
#endif
211
#undef DEPTH_CONV_USE_SPL
212
             ) {
213 214 215 216
    // depth_conv2d
    kernel_func_names_.push_back("depth_conv2d");
    kernel_func_paths_.push_back("image/depthwise_conv2d_basic_kernel.cl");

217
    CLImageConverterNWBlock converter;
218
    const DDim& filter_image_dims = converter.InitImageDimInfoWith(filter_dims);
X
xiebaiyuan 已提交
219 220 221 222 223 224 225
    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);
226
    filter_gpu_image_->mutable_data<half_t, cl::Image2D>(
X
xiebaiyuan 已提交
227
        filter_image_dims[0], filter_image_dims[1], filter_image_data);
228 229

    impl_ = &ConvImageCompute::DepthwiseConv2d;
230
  } else if (kernel_w == 3 && kernel_h == 3) {
X
xiebaiyuan 已提交
231 232
// #define CONV3x3OPT_FALL_BACK
#ifndef CONV3x3OPT_FALL_BACK
233
    // conv2d_3x3
234 235
    kernel_func_names_.push_back(bs > 1 ? "conv2d_3x3_multi_batch"
                                        : "conv2d_3x3_opt");
236
    kernel_func_paths_.push_back("image/conv2d_3x3_opt_kernel.cl");
237 238 239

    CLImageConverterFolder converter;
    const DDim& filter_image_dims = converter.InitImageDimInfoWith(filter_dims);
X
xiebaiyuan 已提交
240 241 242 243 244 245 246
    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);
247
    filter_gpu_image_->mutable_data<half_t, cl::Image2D>(
X
xiebaiyuan 已提交
248
        filter_image_dims[0], filter_image_dims[1], filter_image_data);
249

250
    impl_ = &ConvImageCompute::Conv2d3x3opt;
X
xiebaiyuan 已提交
251

252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
    {
      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 已提交
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
#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
287
  } else if (kernel_h == 5 && kernel_w == 5) {
288 289
#define CONV_5x5_OPT
#ifndef CONV_5x5_OPT
290 291 292 293 294 295
    // 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 已提交
296 297 298 299 300 301 302
    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);
303
    filter_gpu_image_->mutable_data<half_t, cl::Image2D>(
X
xiebaiyuan 已提交
304
        filter_image_dims[0], filter_image_dims[1], filter_image_data);
305 306

    impl_ = &ConvImageCompute::Conv2d5x5;
307 308
#else
    // conv2d_5x5_opt
309 310 311

    kernel_func_names_.push_back(bs > 1 ? "conv2d_5x5_multi_batch"
                                        : "conv2d_5x5_opt");
312 313 314 315
    kernel_func_paths_.push_back("image/conv2d_5x5_opt_kernel.cl");

    CLImageConverterFolder converter;
    const DDim& filter_image_dims = converter.InitImageDimInfoWith(filter_dims);
X
xiebaiyuan 已提交
316 317 318 319 320 321 322
    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);
323
    filter_gpu_image_->mutable_data<half_t, cl::Image2D>(
X
xiebaiyuan 已提交
324
        filter_image_dims[0], filter_image_dims[1], filter_image_data);
325 326

    impl_ = &ConvImageCompute::Conv2d5x5opt;
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
    {
      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_)};
    }
342 343
#endif
#undef CONV_5x5_OPT
344
  } else if (kernel_h == 7 && kernel_w == 7) {
345 346
#define CONV_7x7_OPT
#ifndef CONV_7x7_OPT
347 348 349 350 351 352
    // 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 已提交
353 354 355 356 357 358 359 360 361
    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);
362 363

    impl_ = &ConvImageCompute::Conv2d7x7;
364 365 366

#else
    // conv2d_7x7
367 368
    kernel_func_names_.push_back(bs > 1 ? "conv2d_7x7_multi_batch"
                                        : "conv2d_7x7_opt");
369 370 371 372
    kernel_func_paths_.push_back("image/conv2d_7x7_opt_kernel.cl");

    CLImageConverterFolder converter;
    const DDim& filter_image_dims = converter.InitImageDimInfoWith(filter_dims);
X
xiebaiyuan 已提交
373 374 375 376 377 378 379 380 381
    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);
382 383

    impl_ = &ConvImageCompute::Conv2d7x7opt;
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
    {
      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_)};
    }
399 400
#endif
#undef CONV_7x7_OPT
401 402 403
  } else {
    LOG(FATAL) << "conv image compute not support this condition yet! ";
  }
404 405
  VLOG(1) << "kernel_func_names_[0]:" << kernel_func_names_[0]
          << " kernel_func_paths_[0]:" << kernel_func_paths_[0];
406

407
  // build options
408
  std::string build_options_single(" -DCL_DTYPE_half");
409
  // relu options
410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
  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);
    }
427
  }
428

429 430 431 432 433
  // 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) {
434
    bias_gpu_image_ = std::unique_ptr<Tensor>(new Tensor);
435 436 437 438 439 440 441
    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 已提交
442 443 444 445 446 447

    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>();

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

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

  std::stringstream kernel_key;
469
  kernel_key << kernel_func_names_[0] << build_options_[0] << time_stamp_;
470 471 472 473 474 475 476 477 478 479
  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;

480 481 482 483
  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);
484 485
    VLOG(3) << "origin  :local_work_size_ : " << best_local_work_size[0] << " "
            << best_local_work_size[1] << " " << best_local_work_size[2];
486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503
    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;
        }
504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527
        auto turn_time = this->Turn(10);
        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_;
      }
      // reverse
      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()->LocalWorkSizeTurnReverse(
              global_work_size_, max_work_group_size, i);
        } else {
          local_work_size_ = context.cl_context()->LocalWorkSizeTurnReverse(
              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(10);
528 529 530 531 532 533 534 535
        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;
536 537
    VLOG(3) << "chossen :local_work_size_ : " << local_work_size_[0] << " "
            << local_work_size_[1] << " " << local_work_size_[2];
538 539 540
    VLOG(4) << "local_work_size_[3D]: {" << local_work_size_[0] << ","
            << local_work_size_[1] << "," << local_work_size_[2] << "}";
  }
541 542
}

543
void ConvImageCompute::Conv2d1x1opt(bool is_turn) {
544 545
  auto& context = ctx_->As<OpenCLContext>();
  CHECK(context.cl_context() != nullptr);
546 547 548 549
  const auto& param = *param_.get_mutable<param_t>();
  auto input_dims = param.x->dims();
  auto paddings = *param.paddings;
  auto strides = param.strides;
550
  auto* input_image = param.x->data<half_t, cl::Image2D>();
551
  auto* filter_image = filter_gpu_image_->data<half_t, cl::Image2D>();
552 553 554 555 556 557 558 559
  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);
560
  auto* out_image = param.output->mutable_data<half_t, cl::Image2D>(
561 562 563 564 565 566 567 568 569 570 571 572 573 574
      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;

575
#ifdef LITE_WITH_LOG
576 577 578 579
  //  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
580
#ifdef LITE_WITH_LOG
581 582 583 584 585
  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;
586
  //  VLOG(4) << "input_image: " << input_image;
587
  VLOG(4) << "filter_dims: " << filter_dims;
588
  //  VLOG(4) << "filter_image: " << filter_image;
589 590 591 592 593 594 595 596 597 598
  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];
599 600 601
// VLOG(4) << "default work size{c_block, w, nh}: "
//         << "{" << c_block << ", " << w << ", " << nh << ""
//         << "}";
602
#endif
603 604 605 606 607 608 609 610 611 612 613 614
  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) {
615
    bias_image = bias_gpu_image_->data<half_t, cl::Image2D>();
616 617
  }

618
  auto kernel = kernel_;
619 620
  cl_int status;
  int arg_idx = 0;
621
  status = kernel.setArg(arg_idx, c_blk_);
622
  CL_CHECK_FATAL(status);
623
  status = kernel.setArg(++arg_idx, w_blk_);
624
  CL_CHECK_FATAL(status);
625
  status = kernel.setArg(++arg_idx, nh_blk_);
626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655
  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);
656
  status = kernel.setArg(++arg_idx, default_w_blk_);
657 658
  CL_CHECK_FATAL(status);

659 660 661 662 663 664 665
  status = EnqueueNDRangeKernel(context,
                                kernel,
                                cl::NullRange,
                                global_work_size_,
                                local_work_size_,
                                nullptr,
                                event_);
666
  CL_CHECK_FATAL(status);
667
  if (is_turn) {
X
xiebaiyuan 已提交
668
    CLRuntime::Global()->command_queue().finish();
669
  }
670
}
671

672
void ConvImageCompute::Conv2d3x3(bool is_turn) {
X
xiebaiyuan 已提交
673
  auto kernel = kernel_;
674 675 676 677 678
  const auto& param = *param_.get_mutable<param_t>();
  auto input_dims = param.x->dims();
  auto paddings = *param.paddings;
  auto strides = param.strides;

679
  auto* input_image = param.x->data<half_t, cl::Image2D>();
680
  auto* filter_image = filter_gpu_image_->data<half_t, cl::Image2D>();
681 682 683 684 685 686 687 688 689 690 691 692 693
  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);
694
  auto* out_image = param.output->mutable_data<half_t, cl::Image2D>(
695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715
      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 已提交
716 717 718 719 720 721 722 723
  /* 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;
    }
  */
724

X
xiebaiyuan 已提交
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 755 756 757 758
  // 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 << ""
  //         << "}";
759 760 761 762 763 764 765 766 767 768 769

  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) {
770
    bias_image = bias_gpu_image_->data<half_t, cl::Image2D>();
771
  }
X
xiebaiyuan 已提交
772 773 774 775 776 777 778 779 780

  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;
781 782 783

  cl_int status;
  int arg_idx = 0;
784
  status = kernel.setArg(arg_idx, c_blk_);
785
  CL_CHECK_FATAL(status);
786
  status = kernel.setArg(++arg_idx, w_blk_);
787
  CL_CHECK_FATAL(status);
788
  status = kernel.setArg(++arg_idx, nh_blk_);
789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828
  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 已提交
829 830
  status = kernel.setArg(++arg_idx, static_cast<int>(input_dims[1]));
  CL_CHECK_FATAL(status);
831

X
xiebaiyuan 已提交
832 833 834 835 836 837 838 839
  // 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] << "}";
840

841 842 843 844 845 846 847
  status = EnqueueNDRangeKernel(context,
                                kernel,
                                cl::NullRange,
                                global_work_size_,
                                cl::NullRange,
                                nullptr,
                                event_);
848 849
  CL_CHECK_FATAL(status);
}
850
void ConvImageCompute::Conv2d3x3opt(bool is_turn) {
851 852
  auto& context = ctx_->As<OpenCLContext>();
  CHECK(context.cl_context() != nullptr);
853 854 855 856 857 858 859
  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>();
860
  auto* filter_image = filter_gpu_image_->data<half_t, cl::Image2D>();
861 862 863 864 865 866 867 868 869
  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];
870 871
  CHECK_EQ(input_dims[0], output_dims[0]);
  int batch = input_dims[0];
872 873 874 875 876 877 878 879
  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();

880
#ifdef LITE_WITH_LOG
881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896
  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];
897
#endif
898 899 900 901 902 903 904 905 906 907 908

  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) {
909
    bias_image = bias_gpu_image_->data<half_t, cl::Image2D>();
910 911
  }

912
  auto kernel = kernel_;
913 914 915

  cl_int status;
  int arg_idx = 0;
916
  status = kernel.setArg(arg_idx, c_blk_);
917
  CL_CHECK_FATAL(status);
918
  status = kernel.setArg(++arg_idx, w_blk_);
919
  CL_CHECK_FATAL(status);
920
  status = kernel.setArg(++arg_idx, nh_blk_);
921 922 923 924 925 926
  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) {
927
#ifdef LITE_WITH_LOG
928
    VLOG(4) << "set bias_image: ";
929
#endif
930 931 932 933 934 935 936 937 938 939 940 941 942
    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);
943 944
  status = kernel.setArg(++arg_idx, batch);
  CL_CHECK_FATAL(status);
945 946 947 948 949 950 951 952 953 954 955
  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);

956
#ifdef LITE_WITH_LOG
957
  //  VLOG(4) << "out_image: " << out_image;
958 959
  VLOG(4) << "global_work_size_[3D]: {" << global_work_size_[0] << ","
          << global_work_size_[1] << "," << global_work_size_[2] << "}";
960
#endif
961

962 963 964 965 966 967 968
  status = EnqueueNDRangeKernel(context,
                                kernel,
                                cl::NullRange,
                                global_work_size_,
                                local_work_size_,
                                nullptr,
                                event_);
969
  CL_CHECK_FATAL(status);
970
  if (is_turn) {
X
xiebaiyuan 已提交
971
    CLRuntime::Global()->command_queue().finish();
972
  }
973 974
}

975
void ConvImageCompute::Conv2d5x5(bool is_turn) {
976 977
  auto& context = ctx_->As<OpenCLContext>();
  CHECK(context.cl_context() != nullptr);
978 979 980 981
  const auto& param = *param_.get_mutable<param_t>();
  auto input_dims = param.x->dims();
  auto paddings = *param.paddings;
  auto strides = param.strides;
982
  auto* input_image = param.x->data<half_t, cl::Image2D>();
983
  auto* filter_image = filter_gpu_image_->data<half_t, cl::Image2D>();
984 985 986 987 988 989 990 991 992 993
  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);
994
  auto* out_image = param.output->mutable_data<half_t, cl::Image2D>(
995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008
      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;

1009
#ifdef LITE_WITH_LOG
1010 1011 1012 1013 1014
  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;
1015
  //  VLOG(4) << "input_image: " << input_image;
1016 1017
  VLOG(4) << "input_dims: " << input_dims;
  VLOG(4) << "filter_dims: " << filter_dims;
1018
  //  VLOG(4) << "filter_image: " << filter_image;
1019 1020 1021 1022 1023 1024 1025 1026 1027 1028
  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];
1029
#endif
1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040

  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) {
1041
    bias_image = bias_gpu_image_->data<half_t, cl::Image2D>();
1042 1043
  }

1044
  auto kernel = kernel_;
1045 1046 1047

  cl_int status;
  int arg_idx = 0;
1048
  status = kernel.setArg(arg_idx, c_blk_);
1049
  CL_CHECK_FATAL(status);
1050
  status = kernel.setArg(++arg_idx, w_blk_);
1051
  CL_CHECK_FATAL(status);
1052
  status = kernel.setArg(++arg_idx, nh_blk_);
1053 1054 1055 1056 1057 1058
  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) {
1059
#ifdef LITE_WITH_LOG
1060
    VLOG(4) << "set bias_image: ";
1061
#endif
1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085
    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);

1086
#ifdef LITE_WITH_LOG
1087
  //  VLOG(4) << "out_image: " << out_image;
1088 1089
  VLOG(4) << "global_work_size_[3D]: {" << global_work_size_[0] << ","
          << global_work_size_[1] << "," << global_work_size_[2] << "}";
1090
#endif
1091

1092 1093 1094 1095 1096 1097 1098
  status = EnqueueNDRangeKernel(context,
                                kernel,
                                cl::NullRange,
                                global_work_size_,
                                cl::NullRange,
                                nullptr,
                                event_);
1099
  CL_CHECK_FATAL(status);
1100
  if (is_turn) {
X
xiebaiyuan 已提交
1101
    CLRuntime::Global()->command_queue().finish();
1102
  }
1103
}
1104

1105
void ConvImageCompute::Conv2d5x5opt(bool is_turn) {
1106 1107
  auto& context = ctx_->As<OpenCLContext>();
  CHECK(context.cl_context() != nullptr);
1108 1109 1110 1111 1112 1113 1114
  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>();
1115
  auto* filter_image = filter_gpu_image_->data<half_t, cl::Image2D>();
1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136
  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;
1137
#ifdef LITE_WITH_LOG
1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164
  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) {
1165
    bias_image = bias_gpu_image_->data<half_t, cl::Image2D>();
1166 1167
  }

1168
  auto kernel = kernel_;
1169 1170
  cl_int status;
  int arg_idx = 0;
1171
  status = kernel.setArg(arg_idx, c_blk_);
1172
  CL_CHECK_FATAL(status);
1173
  status = kernel.setArg(++arg_idx, w_blk_);
1174
  CL_CHECK_FATAL(status);
1175
  status = kernel.setArg(++arg_idx, nh_blk_);
1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207
  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);

1208
  //  VLOG(4) << "out_image: " << out_image;
1209

1210 1211 1212 1213 1214 1215 1216
  status = EnqueueNDRangeKernel(context,
                                kernel,
                                cl::NullRange,
                                global_work_size_,
                                local_work_size_,
                                nullptr,
                                event_);
1217
  CL_CHECK_FATAL(status);
1218
  if (is_turn) {
X
xiebaiyuan 已提交
1219
    CLRuntime::Global()->command_queue().finish();
1220
  }
1221 1222
}

1223
void ConvImageCompute::Conv2d7x7(bool is_turn) {
1224 1225
  auto& context = ctx_->As<OpenCLContext>();
  CHECK(context.cl_context() != nullptr);
1226 1227 1228 1229
  const auto& param = *param_.get_mutable<param_t>();
  auto input_dims = param.x->dims();
  auto paddings = *param.paddings;
  auto strides = param.strides;
1230
  auto* input_image = param.x->data<half_t, cl::Image2D>();
1231
  auto* filter_image = filter_gpu_image_->data<half_t, cl::Image2D>();
1232 1233 1234 1235 1236 1237 1238 1239 1240 1241
  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);
1242
  auto* out_image = param.output->mutable_data<half_t, cl::Image2D>(
1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256
      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;

1257
#ifdef LITE_WITH_LOG
1258 1259 1260 1261 1262
  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;
1263
  //  VLOG(4) << "input_image: " << input_image;
1264 1265
  VLOG(4) << "input_dims: " << input_dims;
  VLOG(4) << "filter_dims: " << filter_dims;
1266
  //  VLOG(4) << "filter_image: " << filter_image;
1267 1268 1269 1270 1271 1272 1273 1274 1275 1276
  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];
1277
#endif
1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288

  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) {
1289
    bias_image = bias_gpu_image_->data<half_t, cl::Image2D>();
1290 1291
  }

1292
  auto kernel = kernel_;
1293 1294 1295

  cl_int status;
  int arg_idx = 0;
1296
  status = kernel.setArg(arg_idx, c_blk_);
1297
  CL_CHECK_FATAL(status);
1298
  status = kernel.setArg(++arg_idx, w_blk_);
1299
  CL_CHECK_FATAL(status);
1300
  status = kernel.setArg(++arg_idx, nh_blk_);
1301 1302 1303 1304 1305 1306
  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) {
1307
#ifdef LITE_WITH_LOG
1308
    VLOG(4) << "set bias_image: ";
1309
#endif
1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333
    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);

1334
#ifdef LITE_WITH_LOG
1335
  //  VLOG(4) << "out_image: " << out_image;
1336 1337
  VLOG(4) << "global_work_size_[3D]: {" << global_work_size_[0] << ","
          << global_work_size_[1] << "," << global_work_size_[2] << "}";
1338
#endif
1339

1340 1341 1342 1343 1344 1345 1346
  status = EnqueueNDRangeKernel(context,
                                kernel,
                                cl::NullRange,
                                global_work_size_,
                                cl::NullRange,
                                nullptr,
                                event_);
1347
  CL_CHECK_FATAL(status);
1348 1349

  if (is_turn) {
X
xiebaiyuan 已提交
1350
    CLRuntime::Global()->command_queue().finish();
1351
  }
1352
}
1353
void ConvImageCompute::Conv2d7x7opt(bool is_turn) {
1354 1355
  auto& context = ctx_->As<OpenCLContext>();
  CHECK(context.cl_context() != nullptr);
1356 1357 1358 1359 1360 1361 1362
  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>();
1363
  auto* filter_image = filter_gpu_image_->data<half_t, cl::Image2D>();
1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382
  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();

1383
#ifdef LITE_WITH_LOG
1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410
  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) {
1411
    bias_image = bias_gpu_image_->data<half_t, cl::Image2D>();
1412 1413
  }

1414
  auto kernel = kernel_;
1415

1416 1417
  cl_int status;
  int arg_idx = 0;
1418
  status = kernel.setArg(arg_idx, c_blk_);
1419
  CL_CHECK_FATAL(status);
1420
  status = kernel.setArg(++arg_idx, w_blk_);
1421
  CL_CHECK_FATAL(status);
1422
  status = kernel.setArg(++arg_idx, nh_blk_);
1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454
  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);

1455 1456 1457 1458 1459 1460 1461
  status = EnqueueNDRangeKernel(context,
                                kernel,
                                cl::NullRange,
                                global_work_size_,
                                local_work_size_,
                                nullptr,
                                event_);
1462
  CL_CHECK_FATAL(status);
1463 1464

  if (is_turn) {
X
xiebaiyuan 已提交
1465
    CLRuntime::Global()->command_queue().finish();
1466
  }
1467
}
1468
void ConvImageCompute::DepthwiseConv2d3x3s1(bool is_turn) {
1469 1470
  auto& context = ctx_->As<OpenCLContext>();
  CHECK(context.cl_context() != nullptr);
1471 1472 1473 1474 1475 1476 1477 1478
  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;

1479
  auto* input_img = param.x->data<half_t, cl::Image2D>();
1480
  auto* filter_img = filter_gpu_image_->data<half_t, cl::Image2D>();
1481 1482 1483

  const cl::Image2D* bias_img = nullptr;
  if (param.bias) {
1484
    bias_img = bias_gpu_image_->data<half_t, cl::Image2D>();
1485 1486 1487 1488
  }

  auto image_shape = InitImageDimInfoWith(output_dims);

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

1492
  auto kernel = kernel_;
1493 1494 1495

  cl_int status;
  int arg_idx = 0;
1496
  status = kernel.setArg(arg_idx, c_blk_);
1497
  CL_CHECK_FATAL(status);
1498
  status = kernel.setArg(++arg_idx, w_blk_);
1499
  CL_CHECK_FATAL(status);
1500
  status = kernel.setArg(++arg_idx, nh_blk_);
1501 1502 1503 1504 1505
  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);
1506 1507 1508 1509 1510 1511

  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) {
1512
    bias_image = bias_gpu_image_->data<half_t, cl::Image2D>();
1513
#ifdef LITE_WITH_LOG
1514
    VLOG(4) << "set bias_image: ";
1515
#endif
1516 1517 1518
    status = kernel.setArg(++arg_idx, *bias_image);
    CL_CHECK_FATAL(status);
  }
1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537
  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);

1538 1539 1540 1541 1542 1543 1544
  status = EnqueueNDRangeKernel(context,
                                kernel,
                                cl::NullRange,
                                global_work_size_,
                                local_work_size_,
                                nullptr,
                                event_);
1545
  CL_CHECK_FATAL(status);
1546 1547

  if (is_turn) {
X
xiebaiyuan 已提交
1548
    CLRuntime::Global()->command_queue().finish();
1549
  }
1550 1551
}

1552
void ConvImageCompute::DepthwiseConv2d3x3(bool is_turn) {
1553 1554
  auto& context = ctx_->As<OpenCLContext>();
  CHECK(context.cl_context() != nullptr);
1555 1556 1557 1558 1559 1560 1561 1562 1563 1564
  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;

1565
  auto* input_img = param.x->data<half_t, cl::Image2D>();
1566
  auto* filter_img = filter_gpu_image_->data<half_t, cl::Image2D>();
1567 1568 1569

  const cl::Image2D* bias_img = nullptr;
  if (param.bias) {
1570
    bias_img = bias_gpu_image_->data<half_t, cl::Image2D>();
1571 1572 1573 1574
  }

  auto image_shape = InitImageDimInfoWith(output_dims);

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

1578
  auto kernel = kernel_;
1579

1580
#ifdef LITE_WITH_LOG
1581 1582 1583 1584 1585 1586 1587 1588 1589
  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];
1590
#endif
1591 1592 1593

  cl_int status;
  int arg_idx = 0;
1594
  status = kernel.setArg(arg_idx, c_blk_);
1595
  CL_CHECK_FATAL(status);
1596
  status = kernel.setArg(++arg_idx, w_blk_);
1597
  CL_CHECK_FATAL(status);
1598
  status = kernel.setArg(++arg_idx, nh_blk_);
1599 1600 1601 1602 1603
  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);
1604 1605 1606 1607 1608
  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) {
1609
    bias_image = bias_gpu_image_->data<half_t, cl::Image2D>();
1610
#ifdef LITE_WITH_LOG
1611
    VLOG(4) << "set bias_image: ";
1612
#endif
1613 1614 1615
    status = kernel.setArg(++arg_idx, *bias_image);
    CL_CHECK_FATAL(status);
  }
1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634
  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);

1635 1636 1637 1638 1639 1640 1641
  status = EnqueueNDRangeKernel(context,
                                kernel,
                                cl::NullRange,
                                global_work_size_,
                                cl::NullRange,
                                nullptr,
                                event_);
1642
  CL_CHECK_FATAL(status);
1643 1644

  if (is_turn) {
X
xiebaiyuan 已提交
1645
    CLRuntime::Global()->command_queue().finish();
1646
  }
1647 1648
}

1649
void ConvImageCompute::DepthwiseConv2d(bool is_turn) {
1650 1651
  auto& context = ctx_->As<OpenCLContext>();
  CHECK(context.cl_context() != nullptr);
1652 1653 1654 1655
  const auto& param = *param_.get_mutable<param_t>();
  auto input_dims = param.x->dims();
  auto paddings = *param.paddings;
  auto strides = param.strides;
1656
  auto* input_image = param.x->data<half_t, cl::Image2D>();
1657
  auto* filter_image = filter_gpu_image_->data<half_t, cl::Image2D>();
1658 1659 1660 1661 1662 1663 1664 1665 1666 1667
  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);
1668
  auto* out_image = param.output->mutable_data<half_t, cl::Image2D>(
1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682
      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;

1683
#ifdef LITE_WITH_LOG
1684 1685 1686 1687 1688
  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;
1689
  //  VLOG(4) << "input_image: " << input_image;
1690
  VLOG(4) << "filter_dims: " << filter_dims;
1691
  //  VLOG(4) << "filter_image: " << filter_image;
1692 1693 1694 1695 1696 1697 1698 1699 1700 1701
  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];
1702
#endif
1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715

  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) {
1716
    bias_image = bias_gpu_image_->data<half_t, cl::Image2D>();
1717 1718
  }

1719
  auto kernel = kernel_;
1720 1721 1722

  cl_int status;
  int arg_idx = 0;
1723
  status = kernel.setArg(arg_idx, c_blk_);
1724
  CL_CHECK_FATAL(status);
1725
  status = kernel.setArg(++arg_idx, w_blk_);
1726
  CL_CHECK_FATAL(status);
1727
  status = kernel.setArg(++arg_idx, nh_blk_);
1728 1729 1730 1731 1732 1733
  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) {
1734
#ifdef LITE_WITH_LOG
1735
    VLOG(4) << "set bias_image: ";
1736
#endif
1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764
    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);

1765
#ifdef LITE_WITH_LOG
1766 1767
  VLOG(4) << "global_work_size_[3D]: {" << global_work_size_[0] << ","
          << global_work_size_[1] << "," << global_work_size_[2] << "}";
1768
#endif
1769

1770 1771 1772 1773 1774 1775 1776
  status = EnqueueNDRangeKernel(context,
                                kernel,
                                cl::NullRange,
                                global_work_size_,
                                cl::NullRange,
                                nullptr,
                                event_);
1777 1778 1779
  CL_CHECK_FATAL(status);
}

1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795
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 已提交
1796 1797 1798 1799 1800 1801 1802
}  // namespace opencl
}  // namespace kernels
}  // namespace lite
}  // namespace paddle

REGISTER_LITE_KERNEL(conv2d,
                     kOpenCL,
1803
                     kFP16,
1804 1805 1806 1807 1808
                     kImageDefault,
                     paddle::lite::kernels::opencl::ConvImageCompute,
                     image2d)
    .BindInput("Input",
               {LiteType::GetTensorTy(TARGET(kOpenCL),
1809
                                      PRECISION(kFP16),
1810 1811 1812 1813 1814
                                      DATALAYOUT(kImageDefault))})
    .BindInput("Bias", {LiteType::GetTensorTy(TARGET(kARM))})
    .BindInput("Filter", {LiteType::GetTensorTy(TARGET(kARM))})
    .BindOutput("Output",
                {LiteType::GetTensorTy(TARGET(kOpenCL),
1815
                                       PRECISION(kFP16),
1816
                                       DATALAYOUT(kImageDefault))})
Y
Yan Chunwei 已提交
1817
    .Finalize();
1818

1819
REGISTER_LITE_KERNEL(depthwise_conv2d,
1820
                     kOpenCL,
1821
                     kFP16,
1822 1823 1824 1825 1826
                     kImageDefault,
                     paddle::lite::kernels::opencl::ConvImageCompute,
                     image2d)
    .BindInput("Input",
               {LiteType::GetTensorTy(TARGET(kOpenCL),
1827
                                      PRECISION(kFP16),
1828 1829 1830 1831 1832
                                      DATALAYOUT(kImageDefault))})
    .BindInput("Bias", {LiteType::GetTensorTy(TARGET(kARM))})
    .BindInput("Filter", {LiteType::GetTensorTy(TARGET(kARM))})
    .BindOutput("Output",
                {LiteType::GetTensorTy(TARGET(kOpenCL),
1833
                                       PRECISION(kFP16),
1834 1835
                                       DATALAYOUT(kImageDefault))})
    .Finalize();
1836
#define LITE_WITH_LOG