elementwise_add_image_compute.cc 7.3 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/elementwise_add_image_compute.h"
Y
Yan Chunwei 已提交
16
#include <memory>
17
#include "lite/backends/opencl/cl_include.h"
Y
Yan Chunwei 已提交
18 19 20
#include "lite/core/op_registry.h"
#include "lite/utils/replace_stl/stream.h"

21 22
#undef LITE_WITH_LOG

Y
Yan Chunwei 已提交
23 24 25 26 27
namespace paddle {
namespace lite {
namespace kernels {
namespace opencl {

28
void ElementwiseAddImageCompute::PrepareForRun() {}
29

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
void ElementwiseAddImageCompute::ReInitWhenNeeded() {
  ele_param_ = param_.get_mutable<param_t>();
  auto x_dims = ele_param_->X->dims();
  if ((!first_epoch_for_reinit_ && x_dims != last_x_dims_) ||
      first_epoch_for_reinit_) {
    last_x_dims_ = x_dims;
    first_epoch_for_reinit_ = false;

    // choose kernel
    auto* x = ele_param_->X;
    auto* y = ele_param_->Y;
    auto* out = ele_param_->Out;
    auto axis = ele_param_->axis;

    if (y->dims().size() == 4) {
      kernel_func_name_ = "elementwise_add";  // y: ImageDefault
    } else if (y->dims().size() == 1) {
      if (axis == x->dims().size() - 1) {
        kernel_func_name_ = "width_add";  // y: ImageDefault
      } else if (axis == x->dims().size() - 3) {
        kernel_func_name_ = "channel_add";  // y: ImageFolder
      } else {
        LOG(FATAL) << "ElementwiseAddImage doesn't support axis:" << axis
                   << ", x->dims().size():" << x->dims().size()
                   << ", y->dims.size():" << y->dims().size();
      }
56 57 58 59 60
    } else {
      LOG(FATAL) << "ElementwiseAddImage doesn't support axis:" << axis
                 << ", x->dims().size():" << x->dims().size()
                 << ", y->dims.size():" << y->dims().size();
    }
61 62 63
    VLOG(1) << "kernel_func_name_:" << kernel_func_name_;

    auto& context = ctx_->As<OpenCLContext>();
64 65 66 67
    context.cl_context()->AddKernel(kernel_func_name_,
                                    "image/elementwise_add_kernel.cl",
                                    build_options_,
                                    time_stamp_);
68 69

    STL::stringstream kernel_key;
70
    kernel_key << kernel_func_name_ << build_options_ << time_stamp_;
71 72 73 74 75 76 77 78 79 80 81
    kernel_ = context.cl_context()->GetKernel(kernel_key.str());

    // compute image shape
    paddle::lite::CLImageConverterDefault default_convertor;
    x_img_shape_ = default_convertor.InitImageDimInfoWith(x->dims());  // w, h
    y_img_shape_ = default_convertor.InitImageDimInfoWith(y->dims());
    out_img_shape_ =
        default_convertor.InitImageDimInfoWith(out->dims());  // w, h

    // compute global work size
    GetGlobalWorkSize();
82
  }
83
}
84

85 86 87
void ElementwiseAddImageCompute::GetGlobalWorkSize() {
  global_work_size_ = cl::NDRange{static_cast<cl::size_type>(x_img_shape_[0]),
                                  static_cast<cl::size_type>(x_img_shape_[1])};
88
#ifdef LITE_WITH_LOG
89 90 91
  VLOG(4) << "global_work_size:[2D]:" << x_img_shape_[0] << " "
          << x_img_shape_[1];
#endif
92 93 94 95 96 97 98
}

void ElementwiseAddImageCompute::Run() {
  auto* x = ele_param_->X;
  auto* y = ele_param_->Y;
  auto* out = ele_param_->Out;
  auto axis = ele_param_->axis;
99 100 101 102 103 104 105
  auto x_dims = x->dims();
  auto y_dims = y->dims();

  auto* x_img = x->data<half_t, cl::Image2D>();
  auto* y_img = y->data<half_t, cl::Image2D>();
  auto* out_img = out->mutable_data<half_t, cl::Image2D>(out_img_shape_[0],
                                                         out_img_shape_[1]);
106

107
#ifdef LITE_WITH_LOG
108 109 110 111 112 113 114 115
  VLOG(4) << "x->target():" << TargetToStr(x->target());
  VLOG(4) << "y->target():" << TargetToStr(y->target());
  VLOG(4) << "out->target():" << TargetToStr(out->target());
  VLOG(4) << "x->dims():" << x->dims();
  VLOG(4) << "y->dims():" << y->dims();
  VLOG(4) << "out->dims():" << out->dims();
  VLOG(4) << "axis:" << axis;

116 117 118 119
  VLOG(4) << "x_img_shape_[w,h]:" << x_img_shape_[0] << " " << x_img_shape_[1];
  VLOG(4) << "y_img_shape_[w,h]:" << y_img_shape_[0] << " " << y_img_shape_[1];
  VLOG(4) << "out_img_shape_[w,h]:" << out_img_shape_[0] << " "
          << out_img_shape_[1];
120
#endif
121

122 123
  cl_int status;
  auto kernel = kernel_;
124
  if (y_dims.size() == 4) {
125
    status = kernel.setArg(0, *x_img);
126
    CL_CHECK_FATAL(status);
127
    status = kernel.setArg(1, *y_img);
128
    CL_CHECK_FATAL(status);
129
    status = kernel.setArg(2, *out_img);
130 131
    CL_CHECK_FATAL(status);
  } else if (y_dims.size() == 1) {
132 133
    if (axis == x_dims.size() - 1 || axis == x_dims.size() - 3) {
      const int tensor_w = x_dims[x_dims.size() - 1];
134
#ifdef LITE_WITH_LOG
135
      VLOG(4) << "tensor_w:" << tensor_w;
136
#endif
137
      status = kernel.setArg(0, *x_img);
138
      CL_CHECK_FATAL(status);
139
      status = kernel.setArg(1, *y_img);
140
      CL_CHECK_FATAL(status);
141
      status = kernel.setArg(2, *out_img);
142
      CL_CHECK_FATAL(status);
143
      status = kernel.setArg(3, tensor_w);
144 145 146
      CL_CHECK_FATAL(status);
    } else {
      LOG(FATAL) << "ElementwiseAddImage doesn't support axis:" << axis
147 148
                 << ", x->dims().size():" << x_dims.size()
                 << ", y->dims.size():" << y_dims.size();
149 150 151
    }
  } else {
    LOG(FATAL) << "ElementwiseAddImage doesn't support axis:" << axis
152 153
               << ", x->dims().size():" << x_dims.size()
               << ", y->dims.size():" << y_dims.size();
154 155
  }

156 157
  auto& context = ctx_->As<OpenCLContext>();
  CHECK(context.cl_context() != nullptr);
X
xiebaiyuan 已提交
158

159 160 161 162 163 164 165
  status = EnqueueNDRangeKernel(context,
                                kernel,
                                cl::NullRange,
                                global_work_size_,
                                cl::NullRange,
                                nullptr,
                                event_);
166 167
  CL_CHECK_FATAL(status);
}
Y
Yan Chunwei 已提交
168 169 170 171 172 173 174

}  // namespace opencl
}  // namespace kernels
}  // namespace lite
}  // namespace paddle

namespace ocl = paddle::lite::kernels::opencl;
175

176
// TODO(ysh329): May need fix.
177 178 179 180 181 182 183
// "Y" may from constant value like conv bias (kARM, need do cl_image_converter
// on CPU);
//     may from anther branch like "X" (kOpenCL, nothing to do).
// Consider 2 situations have different actions when pass running(pick kernel),
//     set target of "Y" as kOpenCL temporarily.
REGISTER_LITE_KERNEL(elementwise_add,
                     kOpenCL,
184
                     kFP16,
185 186 187 188 189
                     kImageDefault,
                     ocl::ElementwiseAddImageCompute,
                     def)
    .BindInput("X",
               {LiteType::GetTensorTy(TARGET(kOpenCL),
190
                                      PRECISION(kFP16),
191 192 193
                                      DATALAYOUT(kImageDefault))})
    .BindInput("Y",
               {LiteType::GetTensorTy(TARGET(kOpenCL),
194
                                      PRECISION(kFP16),
195 196 197
                                      DATALAYOUT(kImageDefault))})
    .BindOutput("Out",
                {LiteType::GetTensorTy(TARGET(kOpenCL),
198
                                       PRECISION(kFP16),
199
                                       DATALAYOUT(kImageDefault))})
Y
Yan Chunwei 已提交
200
    .Finalize();
201 202

#define LITE_WITH_LOG