box_coder_image_compute.cc 7.2 KB
Newer Older
H
HappyAngel 已提交
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
// 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"
26 27 28 29
#ifdef LITE_WITH_PROFILE
#include "lite/core/profile/profiler.h"
#endif
#include "lite/backends/opencl/cl_utility.h"
H
HappyAngel 已提交
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47

namespace paddle {
namespace lite {
namespace kernels {
namespace opencl {
class BoxCoderComputeImage : public KernelLite<TARGET(kOpenCL),
                                               PRECISION(kFP16),
                                               DATALAYOUT(kImageDefault)> {
 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) {
      kernel_func_name_ = "decode_center_size";
    } else {
48 49
      LOG(FATAL) << "This code_type " << boxcoder_param_->code_type
                 << " doesn't support";
H
HappyAngel 已提交
50 51 52
    }
    CHECK(context.cl_context() != nullptr);
    VLOG(1) << "kernel_func_name_:" << kernel_func_name_;
X
xiebaiyuan 已提交
53 54 55 56
    context.cl_context()->AddKernel(kernel_func_name_,
                                    "image/box_coder_kernel.cl",
                                    build_options_,
                                    time_stamp_);
H
HappyAngel 已提交
57 58 59 60 61 62 63 64 65 66 67
  }

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

    auto* out_buf =
        boxcoder_param_->proposals->mutable_data<half_t, cl::Image2D>(
            image_shape["width"], image_shape["height"]);

68
#ifdef LITE_WITH_LOG
H
HappyAngel 已提交
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
    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") {
      auto* prior_box_image = input_priorbox->data<half_t, cl::Image2D>();
      auto* prior_box_var_image =
          input_priorboxvar->data<half_t, cl::Image2D>();
      auto* target_box_image = input_targetbox->data<half_t, cl::Image2D>();

      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;
X
xiebaiyuan 已提交
89
      kernel_key << kernel_func_name_ << build_options_ << time_stamp_;
H
HappyAngel 已提交
90 91 92 93 94 95 96 97 98 99
      auto kernel = context.cl_context()->GetKernel(kernel_key.str());

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

      int out_C = new_dims[1];
      int out_H = new_dims[2];
100
#ifdef LITE_WITH_LOG
H
HappyAngel 已提交
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
      VLOG(4) << TargetToStr(boxcoder_param_->proposals->target());
      VLOG(4) << "output shape: " << out_dims[0] << ", " << out_dims[1] << ", "
              << out_dims[2] << ", " << out_dims[3];
      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];
#endif
      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])};

128 129 130 131 132 133 134
      status = EnqueueNDRangeKernel(context,
                                    kernel,
                                    cl::NullRange,
                                    global_work_size,
                                    cl::NullRange,
                                    nullptr,
                                    event_);
H
HappyAngel 已提交
135 136
      CL_CHECK_FATAL(status);

137
#ifdef LITE_WITH_LOG
H
HappyAngel 已提交
138 139 140 141 142 143 144
      VLOG(4) << "global_work_size:[2D]:" << global_work_size[0] << " "
              << global_work_size[1];
#endif
    }
  }
  std::string doc() { return "Boxcoder using cl::Image, kFP16"; }

145 146 147 148 149 150 151 152
#ifdef LITE_WITH_PROFILE
  void SetProfileRuntimeKernelInfo(paddle::lite::profile::OpCharacter* ch) {
    ch->kernel_func_name = kernel_func_name_;
    ch->cl_event =
        event_;  // `event_` defined in `kernel.h`, valid after kernel::Run
  }
#endif

H
HappyAngel 已提交
153 154 155
  param_t* boxcoder_param_{nullptr};
  std::string kernel_func_name_{};
  std::string build_options_{" -DCL_DTYPE_half"};
X
xiebaiyuan 已提交
156
  std::string time_stamp_{GetTimeStamp()};
H
HappyAngel 已提交
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
};

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