cl_image.cc 5.6 KB
Newer Older
Z
Zhen Wang 已提交
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
/* 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_image.h"
#include <glog/logging.h>
#include <array>
#include "paddle/fluid/lite/opencl/cl_engine.h"
#include "paddle/fluid/lite/opencl/cl_tool.h"

namespace paddle {
namespace lite {

std::ostream& operator<<(std::ostream& os, const CLImage& cl_image) {
  int width = cl_image.image_dims_[0];
  int height = cl_image.image_dims_[1];

Z
ZhenWang 已提交
28
  float* image_data = new float[height * width * 4];
Z
ZhenWang 已提交
29
  cl::Image* image = cl_image.cl_image();
Z
Zhen Wang 已提交
30 31 32 33
  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};
  cl_int err = CLEngine::Global()->command_queue().enqueueReadImage(
Z
ZhenWang 已提交
34
      *image, CL_TRUE, origin, region, 0, 0, image_data, nullptr, nullptr);
Z
Zhen Wang 已提交
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
  CL_CHECK_ERRORS(err);

  float* tensor_data = new float[cl_image.numel()];
  auto* converter = cl_image.image_converter();
  converter->ImageToNCHW(image_data, tensor_data, cl_image.image_dims_,
                         cl_image.tensor_dims_);
  int stride = cl_image.numel() / 20;
  stride = stride > 0 ? stride : 1;

  os << " dims: " << cl_image.tensor_dims_ << "\n";
  for (int i = 0; i < cl_image.numel(); i += stride) {
    os << tensor_data[i] << " ";
  }

  delete[] tensor_data;
  delete[] image_data;

  return os;
}

C
Chunwei 已提交
55
void CLImage::set_tensor_data(const float* tensor_data, const DDim& dim) {
Z
Zhen Wang 已提交
56 57 58 59 60 61 62 63 64 65 66
#ifdef LITE_WITH_LIGHT_WEIGHT_FRAMEWORK
  auto numel = dim.product();
#else
  auto numel = dim.production();
#endif
  tensor_data_.reset(new float[numel]);
  memcpy(tensor_data_.get(), tensor_data, numel * sizeof(float));
  tensor_dims_ = dim;
}

void CLImage::InitCLImage(const cl::Context& context) {
Z
ZhenWang 已提交
67 68 69
  CHECK(tensor_data_ != nullptr) << " Please call "
                                    "set_tensohelper->DefaultWorkSize(out_"
                                    "image)r_data first!";
Z
Zhen Wang 已提交
70 71 72 73 74
  image_converter_.reset(new CLImageConverterFolder);
  InitCLImage(context, image_converter_.get());
}

void CLImage::InitNormalCLImage(const cl::Context& context) {
Z
ZhenWang 已提交
75
  CHECK(tensor_data_ != nullptr) << " Please call set_tensor_data first!";
Z
Zhen Wang 已提交
76 77 78 79 80
  image_converter_.reset(new CLImageConverterNormal);
  InitCLImage(context, image_converter_.get());
}

void CLImage::InitNImage(const cl::Context& context) {
Z
ZhenWang 已提交
81
  CHECK(tensor_data_ != nullptr) << " Please call set_tensor_data first!";
Z
Zhen Wang 已提交
82
  CHECK(tensor_dims_.size() == 4) << " Tensor dim is not 4.";
Z
ZhenWang 已提交
83
  image_converter_.reset(new CLImageConverterNWBlock);
Z
Zhen Wang 已提交
84 85 86 87
  InitCLImage(context, image_converter_.get());
}

void CLImage::InitDWImage(const cl::Context& context) {
Z
ZhenWang 已提交
88
  CHECK(tensor_data_ != nullptr) << " Please call set_tensor_data first!";
Z
Zhen Wang 已提交
89
  CHECK(tensor_dims_.size() == 4) << " Tensor dim is not 4.";
Z
ZhenWang 已提交
90
  image_converter_.reset(new CLImageConverterDWBlock);
Z
Zhen Wang 已提交
91 92 93 94 95 96 97 98
  InitCLImage(context, image_converter_.get());
}

void CLImage::InitEmptyImage(const cl::Context& context, const DDim& dim) {
  CHECK(tensor_data_ == nullptr)
      << " Empty image tensor data shouldn't have value";

  tensor_dims_ = dim;
Z
ZhenWang 已提交
99
  image_converter_.reset(new CLImageConverterNormal);
Z
Zhen Wang 已提交
100 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

  VLOG(3) << " to get image dims ";
  image_dims_ = image_converter_->InitImageDimInfoWith(tensor_dims_);
  VLOG(3) << " end get image dims " << image_dims_;

  InitCLImage(context, image_dims_[0], image_dims_[1], nullptr);

  cl_event_ = CLEngine::Global()->CreateEvent(context);
  initialized_ = true;
  VLOG(3) << " end init cl image ";
}

void CLImage::InitEmptyWithImageDim(const cl::Context& context,
                                    const DDim& image_dims) {
  VLOG(3) << " to get image dims ";
  image_dims_ = image_dims;
  VLOG(3) << " end get image dims " << image_dims_;

  InitCLImage(context, image_dims_[0], image_dims_[1], nullptr);

  cl_event_ = CLEngine::Global()->CreateEvent(context);
  initialized_ = true;
  VLOG(3) << " end init cl image";
}

void CLImage::InitCLImage(const cl::Context& context,
                          CLImageConverterBase* converter) {
Z
ZhenWang 已提交
127
  CHECK(tensor_data_ != nullptr) << " Please call set_tensor_data first!";
Z
Zhen Wang 已提交
128 129 130 131 132

  VLOG(3) << " begin init cl image ";
  image_dims_ = converter->InitImageDimInfoWith(tensor_dims_);

#ifdef LITE_WITH_LIGHT_WEIGHT_FRAMEWORK
Z
ZhenWang 已提交
133
  float* image_data = new float[image_dims_.product() * 4];
Z
Zhen Wang 已提交
134
#else
Z
ZhenWang 已提交
135
  float* image_data = new float[image_dims_.production() * 4];
Z
Zhen Wang 已提交
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
#endif

  VLOG(3) << " convert to image ";
  converter->NCHWToImage(tensor_data_.get(), image_data, tensor_dims_);
  VLOG(3) << " end convert to image ";

  InitCLImage(context, image_dims_[0], image_dims_[1], image_data);

  delete[] image_data;
  tensor_data_ = nullptr;
  cl_event_ = CLEngine::Global()->CreateEvent(context);
  initialized_ = true;
  VLOG(3) << " end init cl image ";
}

void CLImage::InitCLImage(const cl::Context& context, int width, int height,
                          void* data) {
Z
ZhenWang 已提交
153
  cl::ImageFormat img_format(CL_RGBA, CL_FLOAT);
Z
Zhen Wang 已提交
154 155 156 157 158 159 160 161 162 163
  cl_int err;
  cl_image_.reset(new cl::Image2D(
      context, CL_MEM_READ_WRITE | (data ? CL_MEM_COPY_HOST_PTR : 0),
      img_format, width, height, 0, data, &err));
  CL_CHECK_ERRORS(err);
  CHECK(err == CL_SUCCESS) << " Create image 2d error.";
}

}  // namespace lite
}  // namespace paddle