box_coder_image_compute.cc 6.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
// 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.

#include <memory>
#include <string>
#include "lite/backends/opencl/cl_half.h"
#include "lite/backends/opencl/cl_image_converter.h"
#include "lite/backends/opencl/cl_include.h"
#include "lite/core/kernel.h"
#include "lite/core/op_registry.h"
#include "lite/kernels/opencl/image_helper.h"
#include "lite/operators/op_params.h"
#include "lite/utils/logging.h"
#include "lite/utils/replace_stl/stream.h"

namespace paddle {
namespace lite {
namespace kernels {
namespace opencl {
C
chenjiaoAngel 已提交
31 32 33
class BoxCoderComputeImage : public KernelLite<TARGET(kOpenCL),
                                               PRECISION(kFP16),
                                               DATALAYOUT(kImageDefault)> {
34 35 36 37 38 39 40 41
 public:
  using param_t = operators::BoxCoderParam;

  void PrepareForRun() override {
    auto& context = ctx_->As<OpenCLContext>();
    boxcoder_param_ = param_.get_mutable<param_t>();
    if (boxcoder_param_->code_type == "decode_center_size" &&
        boxcoder_param_->box_normalized == true) {
C
chenjiaoAngel 已提交
42
      kernel_func_name_ = "decode_center_size";
43
    } else {
C
chenjiaoAngel 已提交
44 45
      printf("This code_type %s doesn't support \n",
             boxcoder_param_->code_type.c_str());
C
chenjiaoAngel 已提交
46
      return;
47 48 49 50 51 52 53 54 55 56 57 58
    }
    CHECK(context.cl_context() != nullptr);
    VLOG(1) << "kernel_func_name_:" << kernel_func_name_;
    context.cl_context()->AddKernel(
        kernel_func_name_, "image/box_coder_kernel.cl", build_options_);
  }

  void Run() override {
    boxcoder_param_ = param_.get_mutable<param_t>();
    const auto& out_dims = boxcoder_param_->proposals->dims();
    auto image_shape = InitImageDimInfoWith(out_dims);

C
chenjiaoAngel 已提交
59 60 61
    auto* out_buf =
        boxcoder_param_->proposals->mutable_data<half_t, cl::Image2D>(
            image_shape["width"], image_shape["height"]);
62 63 64 65 66 67 68 69 70 71

#ifndef LITE_SHUTDOWN_LOG
    VLOG(4) << "boxcoder input shape:  ";

#endif
    const auto* input_priorbox = boxcoder_param_->prior_box;
    const auto* input_priorboxvar = boxcoder_param_->prior_box_var;
    const auto* input_targetbox = boxcoder_param_->target_box;
    const auto& code_type = boxcoder_param_->code_type;
    if (code_type == "decode_center_size") {
C
chenjiaoAngel 已提交
72
      auto* prior_box_image = input_priorbox->data<half_t, cl::Image2D>();
C
chenjiaoAngel 已提交
73 74
      auto* prior_box_var_image =
          input_priorboxvar->data<half_t, cl::Image2D>();
C
chenjiaoAngel 已提交
75
      auto* target_box_image = input_targetbox->data<half_t, cl::Image2D>();
76

C
chenjiaoAngel 已提交
77 78 79 80 81 82 83 84 85
      int new_dims[4] = {1, 1, 1, 1};
      for (int i = 0; i < out_dims.size(); i++) {
        new_dims[4 - out_dims.size() + i] = out_dims[i];
      }
      auto& context = ctx_->As<OpenCLContext>();
      CHECK(context.cl_context() != nullptr);
      STL::stringstream kernel_key;
      kernel_key << kernel_func_name_ << build_options_;
      auto kernel = context.cl_context()->GetKernel(kernel_key.str());
86

C
chenjiaoAngel 已提交
87 88 89 90 91
      auto default_work_size = 
          DefaultWorkSize(out_dims,
                          DDim(std::vector<DDim::value_type>{
                               static_cast<int64_t>(image_shape["width"]),
                               static_cast<int64_t>(image_shape["height"])}));
92

C
chenjiaoAngel 已提交
93 94
      int out_C = new_dims[1];
      int out_H = new_dims[2];
95
#ifndef LITE_SHUTDOWN_LOG
C
chenjiaoAngel 已提交
96
      VLOG(4) << TargetToStr(boxcoder_param_->proposals->target());
C
chenjiaoAngel 已提交
97 98
      VLOG(4) << "output shape: " << out_dims[0] << ", " << out_dims[1] << ", "
              << out_dims[2] << ", " << out_dims[3];
C
chenjiaoAngel 已提交
99 100 101 102 103 104
      VLOG(4) << "image_shape(w,h):" << image_shape["width"] << " "
              << image_shape["height"];
      VLOG(4) << "out_C = " << out_C;
      VLOG(4) << "out_H = " << out_H;
      VLOG(4) << "default_work_size = " << default_work_size[0] << ", "
              << default_work_size[1] << ", " << default_work_size[2];
105
#endif
C
chenjiaoAngel 已提交
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
      int arg_idx = 0;
      cl_int status = kernel.setArg(arg_idx++, *prior_box_image);
      CL_CHECK_FATAL(status);
      status = kernel.setArg(arg_idx++, *prior_box_var_image);
      CL_CHECK_FATAL(status);
      status = kernel.setArg(arg_idx++, *target_box_image);
      CL_CHECK_FATAL(status);
      status = kernel.setArg(arg_idx++, *out_buf);
      CL_CHECK_FATAL(status);
      status = kernel.setArg(arg_idx++, out_C);
      CL_CHECK_FATAL(status);
      status = kernel.setArg(arg_idx++, out_H);
      CL_CHECK_FATAL(status);
      auto global_work_size =
          cl::NDRange{static_cast<cl::size_type>(default_work_size[0]),
                      static_cast<cl::size_type>(default_work_size[2])};
122

C
chenjiaoAngel 已提交
123 124 125 126 127 128 129 130 131
      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_buf, event_);
132 133

#ifndef LITE_SHUTDOWN_LOG
C
chenjiaoAngel 已提交
134 135
      VLOG(4) << "global_work_size:[2D]:" << global_work_size[0] << " "
              << global_work_size[1];
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
#endif
    }
  }
  std::string doc() { return "Boxcoder using cl::Image, kFP16"; }

  param_t* boxcoder_param_{nullptr};
  std::string kernel_func_name_{};
  std::string build_options_{" -DCL_DTYPE_half"};
  std::shared_ptr<cl::Event> event_{new cl::Event};
};

}  // namespace opencl
}  // namespace kernels
}  // namespace lite
}  // namespace paddle
typedef paddle::lite::kernels::opencl::BoxCoderComputeImage BoxCoder_image;

REGISTER_LITE_KERNEL(
    box_coder, kOpenCL, kFP16, kImageDefault, BoxCoder_image, ImageDefault)
    .BindInput("PriorBox",
               {LiteType::GetTensorTy(TARGET(kOpenCL),
                                      PRECISION(kFP16),
                                      DATALAYOUT(kImageDefault))})
    .BindInput("PriorBoxVar",
               {LiteType::GetTensorTy(TARGET(kOpenCL),
                                      PRECISION(kFP16),
                                      DATALAYOUT(kImageDefault))})
    .BindInput("TargetBox",
               {LiteType::GetTensorTy(TARGET(kOpenCL),
                                      PRECISION(kFP16),
                                      DATALAYOUT(kImageDefault))})
    .BindOutput("OutputBox",
                {LiteType::GetTensorTy(TARGET(kOpenCL),
                                       PRECISION(kFP16),
                                       DATALAYOUT(kImageDefault))})
    .Finalize();