cl_caller.cc 3.6 KB
Newer Older
Z
ZhenWang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/* Copyright (c) 2018 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 "paddle/fluid/lite/opencl/cl_caller.h"
#include <string>
#include "paddle/fluid/lite/core/compatible_tensor.h"
#include "paddle/fluid/lite/opencl/cl_engine.h"
#include "paddle/fluid/lite/opencl/cl_helper.h"
#include "paddle/fluid/lite/opencl/cl_image.h"
#include "paddle/fluid/lite/opencl/cl_tool.h"

namespace paddle {
namespace lite {
Z
ZhenWang 已提交
25 26
static void CopyImageData(CLHelper* helper, const CLImage& cl_image,
                          float* out) {
Z
ZhenWang 已提交
27 28 29
  int width = cl_image.image_dims()[0];
  int height = cl_image.image_dims()[1];

Z
ZhenWang 已提交
30
  float* image_data = new float[height * width * 4];
Z
ZhenWang 已提交
31 32 33 34
  cl::Image* image = cl_image.cl_image();
  const std::array<size_t, 3> origin{0, 0, 0};
  const std::array<size_t, 3> region{static_cast<size_t>(width),
                                     static_cast<size_t>(height), 1};
Z
ZhenWang 已提交
35
  cl_int err = helper->OpenCLCommandQueue().enqueueReadImage(
Z
ZhenWang 已提交
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
      *image, CL_TRUE, origin, region, 0, 0, image_data, nullptr, nullptr);
  CL_CHECK_ERRORS(err);

  auto* converter = cl_image.image_converter();
  converter->ImageToNCHW(image_data, out, cl_image.image_dims(),
                         cl_image.tensor_dims());

  delete[] image_data;
}

bool InitOpenCLEngine(std::string cl_path) {
  auto* engine = CLEngine::Global();
  engine->set_cl_path(cl_path);
  return engine->IsInitSuccess();
}

Z
ZhenWang 已提交
52
void elementwise_add(CLHelper* helper, const float* in, const DDim& in_dim,
C
Chunwei 已提交
53
                     const float* bias, const DDim& bias_dim, float* out,
Z
ZhenWang 已提交
54
                     const DDim& out_dim) {
Z
ZhenWang 已提交
55 56 57 58 59 60
  if (!(bias_dim.size() == 1 || bias_dim.size() == 4)) {
    LOG(FATAL) << "Error: bias dims is error";
    return;
  }
  auto kernel = bias_dim.size() == 1 ? helper->GetKernel("channel_add")
                                     : helper->GetKernel("elementwise_add");
Z
ZhenWang 已提交
61 62
  CLImage in_image;
  in_image.set_tensor_data(in, in_dim);
Z
ZhenWang 已提交
63
  in_image.InitNormalCLImage(helper->OpenCLContext());
Z
ZhenWang 已提交
64 65 66
  VLOG(3) << " --- Inpu image: " << in_image << " --- ";
  CLImage bias_image;
  bias_image.set_tensor_data(bias, bias_dim);
Z
ZhenWang 已提交
67
  bias_image.InitCLImage(helper->OpenCLContext());
Z
ZhenWang 已提交
68 69
  VLOG(3) << " --- Bias image: " << bias_image << " --- ";
  CLImage out_image;
Z
ZhenWang 已提交
70
  out_image.InitEmptyImage(helper->OpenCLContext(), out_dim);
Z
ZhenWang 已提交
71 72 73 74 75 76 77
  cl_int status;
  status = kernel.setArg(0, *in_image.cl_image());
  CL_CHECK_ERRORS(status);
  status = kernel.setArg(1, *bias_image.cl_image());
  CL_CHECK_ERRORS(status);
  status = kernel.setArg(2, *out_image.cl_image());
  CL_CHECK_ERRORS(status);
Z
ZhenWang 已提交
78 79 80 81 82 83

  if (bias_dim.size() == 1) {
    int tensor_w = in_dim[3];
    status = kernel.setArg(3, tensor_w);
    CL_CHECK_ERRORS(status);
  }
Z
ZhenWang 已提交
84 85 86
  size_t width = in_image.ImageWidth();
  size_t height = in_image.ImageHeight();
  auto global_work_size = cl::NDRange{width, height};
Z
ZhenWang 已提交
87
  status = helper->OpenCLCommandQueue().enqueueNDRangeKernel(
Z
ZhenWang 已提交
88 89 90
      kernel, cl::NullRange, global_work_size, cl::NullRange, nullptr, nullptr);
  CL_CHECK_ERRORS(status);

Z
ZhenWang 已提交
91 92
  status = helper->OpenCLCommandQueue().finish();
  CL_CHECK_ERRORS(status);
Z
ZhenWang 已提交
93
  VLOG(3) << " --- Out image: " << out_image << " --- ";
Z
ZhenWang 已提交
94
  CopyImageData(helper, out_image, out);
Z
ZhenWang 已提交
95 96 97 98
}

}  // namespace lite
}  // namespace paddle