conv_image_compute.cc 46.0 KB
Newer Older
Y
Yan Chunwei 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

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

Y
Yan Chunwei 已提交
17
#include <sstream>
18 19

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

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

30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
/* image kernel*/
void ConvImageCompute::PrepareForRun() {
  const auto& param = this->Param<param_t>();
  auto x_dims = param.x->dims();
  auto filter_dims = param.filter->dims();
  auto output_dims = param.output->dims();

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

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

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

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

83 84 85 86 87 88 89 90 91 92 93
  if (kernel_h == 1 && kernel_w == 1) {
    // conv2d_1x1
    if (param.x->dims()[1] % 4 == 0) {
      kernel_func_names_.push_back("conv2d_1x1_simple");
    } else {
      kernel_func_names_.push_back("conv2d_1x1");
    }
    kernel_func_paths_.push_back("image/conv2d_1x1_kernel.cl");

    CLImageConverterNWBlock converter;
    const DDim& filter_image_dims = converter.InitImageDimInfoWith(filter_dims);
94 95
    std::vector<half_t> filter_image_v(filter_image_dims[0] *
                                       filter_image_dims[1] * 4);  // 4 : RGBA
96
    converter.NCHWToImage(filter_cpu, filter_image_v.data(), filter_dims);
97
    filter_gpu_image_.mutable_data<half_t, cl::Image2D>(
98 99 100
        filter_image_dims[0], filter_image_dims[1], filter_image_v.data());

    impl_ = &ConvImageCompute::Conv2d1x1;
101 102 103 104 105 106 107 108 109 110 111 112
  } 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;
    } else {
      kernel_func_names_.push_back("depth_conv2d_3x3");
      impl_ = &ConvImageCompute::DepthwiseConv2d3x3;
    }
    kernel_func_paths_.push_back("image/depthwise_conv2d_kernel.cl");

113
    CLImageConverterNWBlock converter;
114
    const DDim& filter_image_dims = converter.InitImageDimInfoWith(filter_dims);
115 116
    std::vector<half_t> filter_image_v(filter_image_dims[0] *
                                       filter_image_dims[1] * 4);  // 4 : RGBA
117
    converter.NCHWToImage(filter_cpu, filter_image_v.data(), filter_dims);
118
    filter_gpu_image_.mutable_data<half_t, cl::Image2D>(
119 120
        filter_image_dims[0], filter_image_dims[1], filter_image_v.data());
  } else if (filter_dims[1] == 1 && x_dims[1] == output_dims[1] &&
121
             kernel_h != 3) {
122 123 124 125
    // depth_conv2d
    kernel_func_names_.push_back("depth_conv2d");
    kernel_func_paths_.push_back("image/depthwise_conv2d_basic_kernel.cl");

126
    CLImageConverterNWBlock converter;
127
    const DDim& filter_image_dims = converter.InitImageDimInfoWith(filter_dims);
128 129
    std::vector<half_t> filter_image_v(filter_image_dims[0] *
                                       filter_image_dims[1] * 4);  // 4 : RGBA
130
    converter.NCHWToImage(filter_cpu, filter_image_v.data(), filter_dims);
131
    filter_gpu_image_.mutable_data<half_t, cl::Image2D>(
132 133 134
        filter_image_dims[0], filter_image_dims[1], filter_image_v.data());

    impl_ = &ConvImageCompute::DepthwiseConv2d;
135 136 137 138 139 140 141
  } else if (kernel_h == 3 && kernel_h == 3) {
    // conv2d_3x3
    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);
142 143
    std::vector<half_t> filter_image_v(filter_image_dims[0] *
                                       filter_image_dims[1] * 4);  // 4 : RGBA
144
    converter.NCHWToImage(filter_cpu, filter_image_v.data(), filter_dims);
145
    filter_gpu_image_.mutable_data<half_t, cl::Image2D>(
146 147 148
        filter_image_dims[0], filter_image_dims[1], filter_image_v.data());

    impl_ = &ConvImageCompute::Conv2d3x3;
149 150 151 152 153 154 155
  } else if (kernel_h == 5 && kernel_w == 5) {
    // 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);
156 157
    std::vector<half_t> filter_image_v(filter_image_dims[0] *
                                       filter_image_dims[1] * 4);  // 4 : RGBA
158
    converter.NCHWToImage(filter_cpu, filter_image_v.data(), filter_dims);
159
    filter_gpu_image_.mutable_data<half_t, cl::Image2D>(
160 161 162 163 164 165 166 167 168 169
        filter_image_dims[0], filter_image_dims[1], filter_image_v.data());

    impl_ = &ConvImageCompute::Conv2d5x5;
  } else if (kernel_h == 7 && kernel_w == 7) {
    // 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);
170 171
    std::vector<half_t> filter_image_v(filter_image_dims[0] *
                                       filter_image_dims[1] * 4);  // 4 : RGBA
172
    converter.NCHWToImage(filter_cpu, filter_image_v.data(), filter_dims);
173
    this->filter_gpu_image_.mutable_data<half_t, cl::Image2D>(
174 175 176 177 178 179
        filter_image_dims[0], filter_image_dims[1], filter_image_v.data());

    impl_ = &ConvImageCompute::Conv2d7x7;
  } else {
    LOG(FATAL) << "conv image compute not support this condition yet! ";
  }
180 181
  VLOG(1) << "kernel_func_names_[0]:" << kernel_func_names_[0]
          << " kernel_func_paths_[0]:" << kernel_func_paths_[0];
182

183
  std::string build_options_single(" -DCL_DTYPE_half");
184 185 186 187 188 189 190
  // relu options
  if (relu_fused) {
    build_options_single += " -DRELU";
  } else if (param.activation_param.active_type ==
             lite_api::ActivationType::kRelu6) {
    build_options_single += " -DRELU6";
  } else {
191
    // do nothing, may add more activation fuse
192 193 194 195 196 197 198 199 200 201 202 203 204
  }
  // bias options
  const bool has_bias = param.bias != nullptr;
  const bool is_element_wise_bias =
      has_bias && param.output->dims() == param.bias->dims();
  if (has_bias) {
    build_options_single +=
        is_element_wise_bias ? " -DBIASE_ELE" : " -DBIASE_CH";

    // convert cpu buffer bias --> gpu image
    CLImageConverterFolder bias_converter;
    const DDim& bias_image_dims =
        bias_converter.InitImageDimInfoWith(param.bias->dims());
205 206
    std::vector<half_t> bias_image_v(bias_image_dims[0] * bias_image_dims[1] *
                                     4);
207 208 209
    float* bias_cpu_data = param.bias->mutable_data<float>();
    bias_converter.NCHWToImage(
        bias_cpu_data, bias_image_v.data(), param.bias->dims());
210
    this->bias_gpu_image_.mutable_data<half_t, cl::Image2D>(
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
        bias_image_dims[0], bias_image_dims[1], bias_image_v.data());
    // convert cpu buffer bias --> gpu image --- end ----
  }

  build_options_.push_back(build_options_single);

  for (size_t i = 0; i < kernel_func_names_.size(); i++) {
    context.cl_context()->AddKernel(
        kernel_func_names_[i], kernel_func_paths_[i], build_options_[i]);
  }
}

void ConvImageCompute::Conv2d1x1() {
  const auto& param = *param_.get_mutable<param_t>();
  auto input_dims = param.x->dims();
  auto paddings = *param.paddings;
  auto strides = param.strides;
228 229
  auto* input_image = param.x->data<half_t, cl::Image2D>();
  auto* filter_image = filter_gpu_image_.data<half_t, cl::Image2D>();
230 231 232 233 234 235 236 237
  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);
238
  auto* out_image = param.output->mutable_data<half_t, cl::Image2D>(
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
      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;

  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_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;
268
  //  VLOG(4) << "input_image: " << input_image;
269
  VLOG(4) << "filter_dims: " << filter_dims;
270
  //  VLOG(4) << "filter_image: " << filter_image;
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
  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) << "default work size{c_block, w, nh}: "
          << "{" << c_block << ", " << w << ", " << nh << ""
          << "}";

  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) {
297
    bias_image = bias_gpu_image_.data<half_t, cl::Image2D>();
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
  }

  auto& context = ctx_->As<OpenCLContext>();
  CHECK(context.cl_context() != nullptr);
  std::stringstream kernel_key;
  kernel_key << kernel_func_names_[0] << build_options_[0];
  auto kernel = context.cl_context()->GetKernel(kernel_key.str());
  int maped_w = maptofactor(w, 4);

  VLOG(4) << "kernel_key: " << kernel_key.str();
  VLOG(4) << "kernel ready ... " << kernel_key.str();
  VLOG(4) << "maped_w: " << maped_w;
  VLOG(4) << "hasbias: " << has_bias;

  cl_int status;
  int arg_idx = 0;
  status = kernel.setArg(arg_idx, c_block);
  CL_CHECK_FATAL(status);
  status = kernel.setArg(++arg_idx, maped_w);
  CL_CHECK_FATAL(status);
  status = kernel.setArg(++arg_idx, nh);
  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);
  status = kernel.setArg(++arg_idx, w);
  CL_CHECK_FATAL(status);

  auto global_work_size =
      cl::NDRange{static_cast<size_t>(default_work_size.data()[0]),
                  static_cast<size_t>(maped_w),
                  static_cast<size_t>(default_work_size.data()[2])};

357
  //  VLOG(4) << "out_image: " << out_image;
358 359 360 361 362 363 364 365 366 367 368 369 370
  VLOG(4) << "global_work_size[3D]: {" << global_work_size[0] << ","
          << global_work_size[1] << "," << global_work_size[2] << "}";

  status = context.cl_context()->GetCommandQueue().enqueueNDRangeKernel(
      kernel,
      cl::NullRange,
      global_work_size,
      cl::NullRange,
      nullptr,
      event_.get());
  CL_CHECK_FATAL(status);
  context.cl_wait_list()->emplace(out_image, event_);
}
371 372 373 374 375 376 377

void ConvImageCompute::Conv2d3x3() {
  const auto& param = *param_.get_mutable<param_t>();
  auto input_dims = param.x->dims();
  auto paddings = *param.paddings;
  auto strides = param.strides;

378 379
  auto* input_image = param.x->data<half_t, cl::Image2D>();
  auto* filter_image = filter_gpu_image_.data<half_t, cl::Image2D>();
380 381 382 383 384 385 386 387 388 389 390 391 392
  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);
393
  auto* out_image = param.output->mutable_data<half_t, cl::Image2D>(
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438
      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;
  }
  /* 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;
    }
  */

  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;
439
  //  VLOG(4) << "input_image: " << input_image;
440 441
  VLOG(4) << "input_dims: " << input_dims;
  VLOG(4) << "filter_dims: " << filter_dims;
442
  //  VLOG(4) << "filter_image: " << filter_image;
443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468
  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 << ""
          << "}";

  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) {
469
    bias_image = bias_gpu_image_.data<half_t, cl::Image2D>();
470 471 472 473 474 475 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 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533
  }

  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;

  cl_int status;
  int arg_idx = 0;
  status = kernel.setArg(arg_idx, c_block);
  CL_CHECK_FATAL(status);
  status = kernel.setArg(++arg_idx, w);
  CL_CHECK_FATAL(status);
  status = kernel.setArg(++arg_idx, nh);
  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);

  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])};

534
  //  VLOG(4) << "out_image: " << out_image;
535 536 537 538 539 540 541 542 543 544 545 546 547 548
  VLOG(4) << "global_work_size[3D]: {" << global_work_size[0] << ","
          << global_work_size[1] << "," << global_work_size[2] << "}";

  status = context.cl_context()->GetCommandQueue().enqueueNDRangeKernel(
      kernel,
      cl::NullRange,
      global_work_size,
      cl::NullRange,
      nullptr,
      event_.get());
  CL_CHECK_FATAL(status);
  context.cl_wait_list()->emplace(out_image, event_);
}

549 550 551 552 553
void ConvImageCompute::Conv2d5x5() {
  const auto& param = *param_.get_mutable<param_t>();
  auto input_dims = param.x->dims();
  auto paddings = *param.paddings;
  auto strides = param.strides;
554 555
  auto* input_image = param.x->data<half_t, cl::Image2D>();
  auto* filter_image = filter_gpu_image_.data<half_t, cl::Image2D>();
556 557 558 559 560 561 562 563 564 565
  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);
566
  auto* out_image = param.output->mutable_data<half_t, cl::Image2D>(
567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595
      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;

  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;
596
  //  VLOG(4) << "input_image: " << input_image;
597 598
  VLOG(4) << "input_dims: " << input_dims;
  VLOG(4) << "filter_dims: " << filter_dims;
599
  //  VLOG(4) << "filter_image: " << filter_image;
600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623
  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) << "default work size{c_block, w, nh}: "
          << "{" << c_block << ", " << w << ", " << nh << ""
          << "}";

  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) {
624
    bias_image = bias_gpu_image_.data<half_t, cl::Image2D>();
625 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 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678
  }

  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;

  cl_int status;
  int arg_idx = 0;
  status = kernel.setArg(arg_idx, c_block);
  CL_CHECK_FATAL(status);
  status = kernel.setArg(++arg_idx, w);
  CL_CHECK_FATAL(status);
  status = kernel.setArg(++arg_idx, nh);
  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);

  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])};

679
  //  VLOG(4) << "out_image: " << out_image;
680 681 682 683 684 685 686 687 688 689 690 691 692
  VLOG(4) << "global_work_size[3D]: {" << global_work_size[0] << ","
          << global_work_size[1] << "," << global_work_size[2] << "}";

  status = context.cl_context()->GetCommandQueue().enqueueNDRangeKernel(
      kernel,
      cl::NullRange,
      global_work_size,
      cl::NullRange,
      nullptr,
      event_.get());
  CL_CHECK_FATAL(status);
  context.cl_wait_list()->emplace(out_image, event_);
}
693

694 695 696 697 698
void ConvImageCompute::Conv2d7x7() {
  const auto& param = *param_.get_mutable<param_t>();
  auto input_dims = param.x->dims();
  auto paddings = *param.paddings;
  auto strides = param.strides;
699 700
  auto* input_image = param.x->data<half_t, cl::Image2D>();
  auto* filter_image = filter_gpu_image_.data<half_t, cl::Image2D>();
701 702 703 704 705 706 707 708 709 710
  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);
711
  auto* out_image = param.output->mutable_data<half_t, cl::Image2D>(
712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740
      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;

  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;
741
  //  VLOG(4) << "input_image: " << input_image;
742 743
  VLOG(4) << "input_dims: " << input_dims;
  VLOG(4) << "filter_dims: " << filter_dims;
744
  //  VLOG(4) << "filter_image: " << filter_image;
745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768
  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) << "default work size{c_block, w, nh}: "
          << "{" << c_block << ", " << w << ", " << nh << ""
          << "}";

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

  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;

  cl_int status;
  int arg_idx = 0;
  status = kernel.setArg(arg_idx, c_block);
  CL_CHECK_FATAL(status);
  status = kernel.setArg(++arg_idx, w);
  CL_CHECK_FATAL(status);
  status = kernel.setArg(++arg_idx, nh);
  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);

  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])};

824
  //  VLOG(4) << "out_image: " << out_image;
825 826 827 828 829 830 831 832 833 834 835 836 837 838
  VLOG(4) << "global_work_size[3D]: {" << global_work_size[0] << ","
          << global_work_size[1] << "," << global_work_size[2] << "}";

  status = context.cl_context()->GetCommandQueue().enqueueNDRangeKernel(
      kernel,
      cl::NullRange,
      global_work_size,
      cl::NullRange,
      nullptr,
      event_.get());
  CL_CHECK_FATAL(status);
  context.cl_wait_list()->emplace(out_image, event_);
}

839 840 841 842 843 844 845 846 847 848 849
void ConvImageCompute::DepthwiseConv2d3x3s1() {
  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;

  auto& context = ctx_->As<OpenCLContext>();
  CHECK(context.cl_context() != nullptr);
850 851
  auto* input_img = param.x->data<half_t, cl::Image2D>();
  auto* filter_img = filter_gpu_image_.data<half_t, cl::Image2D>();
852 853 854

  const cl::Image2D* bias_img = nullptr;
  if (param.bias) {
855
    bias_img = bias_gpu_image_.data<half_t, cl::Image2D>();
856 857 858 859
  }

  auto image_shape = InitImageDimInfoWith(output_dims);

860
  auto* output_img = param.output->mutable_data<half_t, cl::Image2D>(
861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887
      image_shape["width"], image_shape["height"]);

  STL::stringstream kernel_key;
  kernel_key << kernel_func_names_[0] << build_options_[0];
  auto kernel = context.cl_context()->GetKernel(kernel_key.str());

  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;

  auto global_work_size = cl::NDRange(c_block, w_blk, nh);

  cl_int status;
  int arg_idx = 0;
  status = kernel.setArg(arg_idx, static_cast<const int>(c_block));
  CL_CHECK_FATAL(status);
  status = kernel.setArg(++arg_idx, static_cast<const int>(w_blk));
  CL_CHECK_FATAL(status);
  status = kernel.setArg(++arg_idx, static_cast<const int>(nh));
  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);
888 889 890 891 892 893 894 895 896 897 898

  const bool has_bias = param.bias != nullptr;
  const bool is_element_wise_bias =
      has_bias && param.output->dims() == param.bias->dims();
  const cl::Image2D* bias_image = nullptr;
  if (has_bias) {
    bias_image = bias_gpu_image_.data<half_t, cl::Image2D>();
    VLOG(4) << "set bias_image: ";
    status = kernel.setArg(++arg_idx, *bias_image);
    CL_CHECK_FATAL(status);
  }
899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941
  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,
      global_work_size,
      cl::NullRange,
      nullptr,
      event_.get());
  CL_CHECK_FATAL(status);
  context.cl_wait_list()->emplace(output_img, event_);
}

void ConvImageCompute::DepthwiseConv2d3x3() {
  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;

  auto& context = ctx_->As<OpenCLContext>();
  CHECK(context.cl_context() != nullptr);
942 943
  auto* input_img = param.x->data<half_t, cl::Image2D>();
  auto* filter_img = filter_gpu_image_.data<half_t, cl::Image2D>();
944 945 946

  const cl::Image2D* bias_img = nullptr;
  if (param.bias) {
947
    bias_img = bias_gpu_image_.data<half_t, cl::Image2D>();
948 949 950 951
  }

  auto image_shape = InitImageDimInfoWith(output_dims);

952
  auto* output_img = param.output->mutable_data<half_t, cl::Image2D>(
953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989
      image_shape["width"], image_shape["height"]);

  STL::stringstream kernel_key;
  kernel_key << kernel_func_names_[0] << build_options_[0];
  auto kernel = context.cl_context()->GetKernel(kernel_key.str());

  int c_block = (output_dims[1] + 3) / 4;
  int w = output_dims[3];
  int nh = output_dims[0] * output_dims[2];
  auto global_work_size = cl::NDRange(c_block, w, nh);

  VLOG(4) << "setArg";
  VLOG(4) << "c_block = " << c_block;
  VLOG(4) << "w = " << w;
  VLOG(4) << "nh = " << nh;

  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];

  cl_int status;
  int arg_idx = 0;
  status = kernel.setArg(arg_idx, static_cast<const int>(c_block));
  CL_CHECK_FATAL(status);
  status = kernel.setArg(++arg_idx, static_cast<const int>(w));
  CL_CHECK_FATAL(status);
  status = kernel.setArg(++arg_idx, static_cast<const int>(nh));
  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);
990 991 992 993 994 995 996 997 998 999
  const bool has_bias = param.bias != nullptr;
  const bool is_element_wise_bias =
      has_bias && param.output->dims() == param.bias->dims();
  const cl::Image2D* bias_image = nullptr;
  if (has_bias) {
    bias_image = bias_gpu_image_.data<half_t, cl::Image2D>();
    VLOG(4) << "set bias_image: ";
    status = kernel.setArg(++arg_idx, *bias_image);
    CL_CHECK_FATAL(status);
  }
1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034
  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,
      global_work_size,
      cl::NullRange,
      nullptr,
      event_.get());
  CL_CHECK_FATAL(status);
  context.cl_wait_list()->emplace(output_img, event_);
}

void ConvImageCompute::DepthwiseConv2d() {
  const auto& param = *param_.get_mutable<param_t>();
  auto input_dims = param.x->dims();
  auto paddings = *param.paddings;
  auto strides = param.strides;
1035 1036
  auto* input_image = param.x->data<half_t, cl::Image2D>();
  auto* filter_image = filter_gpu_image_.data<half_t, cl::Image2D>();
1037 1038 1039 1040 1041 1042 1043 1044 1045 1046
  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);
1047
  auto* out_image = param.output->mutable_data<half_t, cl::Image2D>(
1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076
      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;

  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) << "============ 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;
1077
  //  VLOG(4) << "input_image: " << input_image;
1078
  VLOG(4) << "filter_dims: " << filter_dims;
1079
  //  VLOG(4) << "filter_image: " << filter_image;
1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105
  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) << "default work size{c_block, w, nh}: "
          << "{" << c_block << ", " << w << ", " << nh << ""
          << "}";

  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) {
1106
    bias_image = bias_gpu_image_.data<half_t, cl::Image2D>();
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 1134 1135 1136 1137 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
  }

  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;

  cl_int status;
  int arg_idx = 0;
  status = kernel.setArg(arg_idx, c_block);
  CL_CHECK_FATAL(status);
  status = kernel.setArg(++arg_idx, w);
  CL_CHECK_FATAL(status);
  status = kernel.setArg(++arg_idx, nh);
  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, filter_width);
  CL_CHECK_FATAL(status);
  status = kernel.setArg(++arg_idx, filter_height);
  CL_CHECK_FATAL(status);

  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])};

1165
  //  VLOG(4) << "out_image: " << out_image;
1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179
  VLOG(4) << "global_work_size[3D]: {" << global_work_size[0] << ","
          << global_work_size[1] << "," << global_work_size[2] << "}";

  status = context.cl_context()->GetCommandQueue().enqueueNDRangeKernel(
      kernel,
      cl::NullRange,
      global_work_size,
      cl::NullRange,
      nullptr,
      event_.get());
  CL_CHECK_FATAL(status);
  context.cl_wait_list()->emplace(out_image, event_);
}

1180 1181
void ConvImageCompute::Run() { (this->*impl_)(); }

Y
Yan Chunwei 已提交
1182 1183 1184 1185 1186 1187 1188
}  // namespace opencl
}  // namespace kernels
}  // namespace lite
}  // namespace paddle

REGISTER_LITE_KERNEL(conv2d,
                     kOpenCL,
1189
                     kFP16,
1190 1191 1192 1193 1194
                     kImageDefault,
                     paddle::lite::kernels::opencl::ConvImageCompute,
                     image2d)
    .BindInput("Input",
               {LiteType::GetTensorTy(TARGET(kOpenCL),
1195
                                      PRECISION(kFP16),
1196 1197 1198 1199 1200
                                      DATALAYOUT(kImageDefault))})
    .BindInput("Bias", {LiteType::GetTensorTy(TARGET(kARM))})
    .BindInput("Filter", {LiteType::GetTensorTy(TARGET(kARM))})
    .BindOutput("Output",
                {LiteType::GetTensorTy(TARGET(kOpenCL),
1201
                                       PRECISION(kFP16),
1202
                                       DATALAYOUT(kImageDefault))})
Y
Yan Chunwei 已提交
1203
    .Finalize();
1204

1205
REGISTER_LITE_KERNEL(depthwise_conv2d,
1206
                     kOpenCL,
1207
                     kFP16,
1208 1209 1210 1211 1212
                     kImageDefault,
                     paddle::lite::kernels::opencl::ConvImageCompute,
                     image2d)
    .BindInput("Input",
               {LiteType::GetTensorTy(TARGET(kOpenCL),
1213
                                      PRECISION(kFP16),
1214 1215 1216 1217 1218
                                      DATALAYOUT(kImageDefault))})
    .BindInput("Bias", {LiteType::GetTensorTy(TARGET(kARM))})
    .BindInput("Filter", {LiteType::GetTensorTy(TARGET(kARM))})
    .BindOutput("Output",
                {LiteType::GetTensorTy(TARGET(kOpenCL),
1219
                                       PRECISION(kFP16),
1220 1221
                                       DATALAYOUT(kImageDefault))})
    .Finalize();