concat_image_compute.cc 10.3 KB
Newer Older
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/backends/opencl/cl_half.h"
16 17 18 19 20 21
#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/replace_stl/stream.h"
22 23 24 25
#ifdef LITE_WITH_PROFILE
#include "lite/core/profile/profiler.h"
#endif
#include "lite/backends/opencl/cl_utility.h"
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45

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

class ConcatComputeImage : public KernelLite<TARGET(kOpenCL),
                                             PRECISION(kFP16),
                                             DATALAYOUT(kImageDefault)> {
 public:
  using param_t = operators::ConcatParam;

  void PrepareForRun() override {
    auto& context = ctx_->As<OpenCLContext>();
    concat_param_ = param_.get_mutable<param_t>();
    if (concat_param_->x.size() == 2) {
      kernel_func_name_ = "concat2";
    } else {
      kernel_func_name_ = "concat_mul";
    }
46
    VLOG(1) << "kernel_func_name_:" << kernel_func_name_;
47 48 49 50
    context.cl_context()->AddKernel(kernel_func_name_,
                                    "image/concat_kernel.cl",
                                    build_options_,
                                    time_stamp_);
51 52 53 54 55 56 57 58 59 60 61 62

    auto axis = concat_param_->axis;
    auto inputs = concat_param_->x;
    auto out_dims = concat_param_->output->dims();
    auto* axis_tensor = concat_param_->axis_tensor;
    if (axis_tensor != nullptr) {
      // auto* axis_tensor_data = axis_tensor->data<int>(TARGET(kARM));
      // axis = axis_tensor_data[0];
    }
    auto in_dims = inputs[0]->dims();
    axis_size_ = out_dims[axis];
    axis_ = axis;
63 64 65
    if (out_dims.size() < 4) {
      if (out_dims.size() - axis == 1) {
        // width
66 67
        width_ = out_dims[1];  // c
        flag_ = 3;
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
      } else {
        // height
        width_ = out_dims[0];  // n
        flag_ = 2;
      }
    } else {
      switch (axis_) {
        case 0:
          width_ = out_dims[2];  // h
          flag_ = 0;
          break;
        case 1:                  // channel
          width_ = out_dims[3];  // w
          flag_ = 1;
          break;
        case 2:                  // height
          width_ = out_dims[0];  // n
          flag_ = 2;
          break;
        case 3:
        case -1:                 // width
          width_ = out_dims[1];  // c
          flag_ = 3;
          break;
        default:
          printf("this axis: %d does not support \n", axis_);
      }
95
    }
96

97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
    for (int i = 1; i < inputs.size(); i++) {
      auto dims = inputs[i]->dims();
      // auto flag = CHECK_EQ_OR_FALSE(in_dims.size(), dims.size());
      if (in_dims.size() != dims.size()) {
        printf("input shape must be same \n");
        return;
      }
      for (int i = 0; i < dims.size(); i++) {
        if (i != axis) {
          if (in_dims[i] != dims[i]) {
            printf("input shape must be same \n");
            return;
          }
        }
      }
    }
  }

  void Run() override {
    auto& param = *param_.get_mutable<param_t>();
    const auto& x_dims = param.output->dims();
    auto image_shape = InitImageDimInfoWith(x_dims);
119
    auto* out_buf = param.output->mutable_data<half_t, cl::Image2D>(
120 121 122 123 124 125
        image_shape["width"], image_shape["height"]);
    const auto& y_dims = param.output->dims();  // useless: check dim only

    auto& context = ctx_->As<OpenCLContext>();
    CHECK(context.cl_context() != nullptr);
    STL::stringstream kernel_key;
126
    kernel_key << kernel_func_name_ << build_options_ << time_stamp_;
127 128 129

    auto inputs = param.x;
    int arg_idx = 0;
130 131
    int width = inputs[0]->dims()[inputs[0]->dims().size() - 1];

132
#ifdef LITE_WITH_LOG
133
    VLOG(4) << "concat input shape:  ";
134
    for (size_t i = 0; i < inputs.size(); i++) {
135 136 137 138 139
      VLOG(4) << "inputs [" << i << "]"
              << "[" << inputs[i]->dims().size() << "D]:"
              << "   dims:" << inputs[i]->dims()[0] << " "
              << inputs[i]->dims()[1] << " " << inputs[i]->dims()[2] << " "
              << inputs[i]->dims()[3];
140
    }
141

142
    VLOG(4) << "concat output shape:  ";
143 144 145 146 147
    VLOG(4) << " out  dims:  "
            << "[" << x_dims.size() << "D]:" << x_dims[0] << " " << x_dims[1]
            << " " << x_dims[2] << " " << x_dims[3];
    VLOG(4) << "axis_: " << axis_;
    VLOG(4) << "flag_: " << flag_;
148
#endif
149

150 151 152 153 154
    auto global_work_size =
        cl::NDRange{static_cast<cl::size_type>(x_dims[x_dims.size() - 1]),
                    static_cast<cl::size_type>(image_shape["width"] /
                                               x_dims[x_dims.size() - 1]),
                    static_cast<cl::size_type>(image_shape["height"])};
155

156
#ifdef LITE_WITH_LOG
157 158 159 160
    VLOG(4) << TargetToStr(param.output->target());
    VLOG(4) << "image_shape(w,h):" << image_shape["width"] << " "
            << image_shape["height"];
    VLOG(4) << "x_dims[" << x_dims.size() << "D]:" << x_dims[0] << " "
161 162
            << x_dims[1] << " " << x_dims[2] << " " << x_dims[3]
            << "x_dims[x_dims.size() - 1]" << x_dims[x_dims.size() - 1];
163 164
    VLOG(4) << "y_dims[" << y_dims.size() << "D]:" << y_dims[0] << " "
            << y_dims[1] << " " << y_dims[2] << " " << y_dims[3];
165
    VLOG(4) << "width_: " << width_ << ", flag_: " << flag_;
166 167 168
    VLOG(4) << "global_work_size: " << x_dims[x_dims.size() - 1] << "  "
            << (image_shape["width"] / x_dims[x_dims.size() - 1]) << "  "
            << (image_shape["height"]);
169
#endif
170

171
    auto kernel = context.cl_context()->GetKernel(kernel_key.str());
172
    int out_w = x_dims[x_dims.size() - 1];
173
    int out_c = x_dims[1];
174
    if (inputs.size() == 2) {
175 176
      auto* x_buf0 = inputs[0]->data<half_t, cl::Image2D>();
      auto* x_buf1 = inputs[1]->data<half_t, cl::Image2D>();
177 178 179 180 181 182
      cl_int status = kernel.setArg(arg_idx, *x_buf0);
      CL_CHECK_FATAL(status);
      status = kernel.setArg(++arg_idx, *x_buf1);
      CL_CHECK_FATAL(status);
      status = kernel.setArg(++arg_idx, *out_buf);
      CL_CHECK_FATAL(status);
183 184
      status = kernel.setArg(++arg_idx, flag_);
      CL_CHECK_FATAL(status);
185 186 187
      status =
          kernel.setArg(++arg_idx, static_cast<int>(inputs[0]->dims()[axis_]));
      CL_CHECK_FATAL(status);
188
      status = kernel.setArg(++arg_idx, out_c);
189
      CL_CHECK_FATAL(status);
190 191 192
      status = kernel.setArg(++arg_idx, out_w);
      CL_CHECK_FATAL(status);
      status = kernel.setArg(++arg_idx, width_);
193
      CL_CHECK_FATAL(status);
X
xiebaiyuan 已提交
194

195 196 197 198 199 200
      status = context.cl_context()->GetCommandQueue().enqueueNDRangeKernel(
          kernel,
          cl::NullRange,
          global_work_size,
          cl::NullRange,
          nullptr,
X
xiebaiyuan 已提交
201
          nullptr);
202 203 204 205 206
      CL_CHECK_FATAL(status);
    } else {
      auto start = 0;
      for (int i = 0; i < inputs.size(); i++) {
        arg_idx = 0;
207 208
        auto in_dims = inputs[i]->dims();
        image_shape = InitImageDimInfoWith(in_dims);
209
        auto* x_buf = inputs[i]->data<half_t, cl::Image2D>();
210
        int in_w = in_dims[in_dims.size() - 1];
211
#ifdef LITE_WITH_LOG
212 213
        VLOG(4) << "image_shape(w,h):" << image_shape["width"] << " "
                << image_shape["height"];
214
#endif
215 216 217 218 219
        global_work_size =
            cl::NDRange{static_cast<cl::size_type>(in_dims[in_dims.size() - 1]),
                        static_cast<cl::size_type>(image_shape["width"] /
                                                   in_dims[in_dims.size() - 1]),
                        static_cast<cl::size_type>(image_shape["height"])};
220 221 222 223
        cl_int status = kernel.setArg(arg_idx, *x_buf);
        CL_CHECK_FATAL(status);
        status = kernel.setArg(++arg_idx, *out_buf);
        CL_CHECK_FATAL(status);
224
        status = kernel.setArg(++arg_idx, flag_);
225 226 227
        CL_CHECK_FATAL(status);
        status = kernel.setArg(++arg_idx, start);
        CL_CHECK_FATAL(status);
228 229 230 231 232
        status = kernel.setArg(++arg_idx, out_c);
        CL_CHECK_FATAL(status);
        status = kernel.setArg(++arg_idx, out_w);
        CL_CHECK_FATAL(status);
        status = kernel.setArg(++arg_idx, in_w);
233
        CL_CHECK_FATAL(status);
234
        status = kernel.setArg(++arg_idx, width_);
235 236
        CL_CHECK_FATAL(status);
        CL_CHECK_FATAL(status);
X
xiebaiyuan 已提交
237

238 239 240 241 242 243
        status = context.cl_context()->GetCommandQueue().enqueueNDRangeKernel(
            kernel,
            cl::NullRange,
            global_work_size,
            cl::NullRange,
            nullptr,
X
xiebaiyuan 已提交
244
            nullptr);
245 246 247 248 249 250 251 252
        CL_CHECK_FATAL(status);
        start += inputs[i]->dims()[axis_];
      }
    }
  }

  std::string doc() { return "Concat using cl::Image, kFP16"; }

253 254 255 256 257 258 259 260
#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

261 262
  int axis_size_ = 1;
  int axis_ = 1;
263 264
  int flag_ = 1;
  int width_ = 1;
265 266
  param_t* concat_param_{nullptr};
  std::string kernel_func_name_{};
267
  std::string build_options_{" -DCL_DTYPE_half"};
268
  std::string time_stamp_{GetTimeStamp()};
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
};

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

typedef paddle::lite::kernels::opencl::ConcatComputeImage Concat_image;

REGISTER_LITE_KERNEL(
    concat, kOpenCL, kFP16, kImageDefault, Concat_image, ImageDefault)
    .BindInput("X",
               {LiteType::GetTensorTy(TARGET(kOpenCL),
                                      PRECISION(kFP16),
                                      DATALAYOUT(kImageDefault))})
    .BindInput("AxisTensor",
               {LiteType::GetTensorTy(TARGET(kOpenCL),
                                      PRECISION(kInt32),
                                      DATALAYOUT(kImageDefault))})
    .BindOutput("Out",
                {LiteType::GetTensorTy(TARGET(kOpenCL),
                                       PRECISION(kFP16),
                                       DATALAYOUT(kImageDefault))})
    .Finalize();