cl_image.h 8.9 KB
Newer Older
L
liuruilong 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/* 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. */

#pragma once

17 18 19
#include <vector>

#include "CL/cl.h"
L
liuruilong 已提交
20

21
#include "framework/cl/cl_half.h"
L
liuruilong 已提交
22
#include "framework/cl/cl_tool.h"
L
liuruilong 已提交
23
#include "framework/cl/cl_deleter.h"
L
liuruilong 已提交
24 25 26 27 28 29
#include "framework/ddim.h"
#include "framework/tensor.h"

namespace paddle_mobile {
namespace framework {

L
liuruilong 已提交
30
enum ImageType { Invalid = -1, Normal = 0, Folder = 1 };
Y
yangfei 已提交
31

L
liuruilong 已提交
32 33
class CLImage {
 public:
L
liuruilong 已提交
34 35
  CLImage() = default;

L
liuruilong 已提交
36 37 38 39 40 41 42
  /*
   * will not hold input tensor data, memcpy in this method
   * */
  void SetTensorData(float *tensorData, const DDim &dim) {
    int numel = product(dim);
    if (tensor_data_ != nullptr) {
      delete[](tensor_data_);
L
liuruilong 已提交
43
      tensor_data_ = nullptr;
L
liuruilong 已提交
44 45
    }
    tensor_data_ = new float[numel];
L
liuruilong 已提交
46
    memcpy(tensor_data_, tensorData, numel * sizeof(float));
L
liuruilong 已提交
47 48 49 50 51
    tensor_dims_ = dim;
  }

  /*
   * need call SetTensorData first
L
liuruilong 已提交
52 53
   *
   * folder when one dim or two dim
L
liuruilong 已提交
54
   * */
L
liuruilong 已提交
55
  void InitCLImage(cl_context context, cl_command_queue command_queue) {
L
liuruilong 已提交
56 57 58
    if (tensor_data_ == nullptr) {
      PADDLE_MOBILE_THROW_EXCEPTION(" need call SetTensorData first");
    }
L
liuruilong 已提交
59
    DLOG << tensor_dims_;
D
dolphin8 已提交
60
    if (tensor_dims_.size() <= 2) {
L
liuruilong 已提交
61
      DLOG << " dim <= 2 folder ~~~~~ ";
L
liuruilong 已提交
62
      InitCLImage2C(context, command_queue, tensor_data_, tensor_dims_);
D
dolphin8 已提交
63
    } else {
L
liuruilong 已提交
64
      DLOG << " dim >  2 norm ~~~~~ ";
L
liuruilong 已提交
65
      InitCLImage(context, command_queue, tensor_data_, tensor_dims_);
D
dolphin8 已提交
66
    }
L
liuruilong 已提交
67 68 69 70 71
    delete[](tensor_data_);
    tensor_data_ = nullptr;
    initialized_ = true;
  }

Y
yangfei 已提交
72 73 74 75 76 77 78 79 80 81 82 83 84
  /*
   * need call SetTensorData first
   * */
  void InitCLImageNormal(cl_context context, cl_command_queue command_queue) {
    if (tensor_data_ == nullptr) {
      PADDLE_MOBILE_THROW_EXCEPTION(" need call SetTensorData first");
    }
    InitCLImage(context, command_queue, tensor_data_, tensor_dims_);
    delete[](tensor_data_);
    tensor_data_ = nullptr;
    initialized_ = true;
  }

L
liuruilong 已提交
85 86
  void InitEmptyImage(cl_context context, cl_command_queue command_queue,
                      const DDim &dim) {
L
liuruilong 已提交
87 88 89 90
    if (tensor_data_ != nullptr) {
      PADDLE_MOBILE_THROW_EXCEPTION(
          " empty image tensor data shouldn't have value");
    }
L
liuruilong 已提交
91
    DLOG << " init empty image ";
L
liuruilong 已提交
92 93 94 95 96 97 98 99 100 101
    if (tensor_dims_.size() <= 2) {
      DLOG << " dim <= 2 folder ~~~~~ ";
      InitCLImage2C(context, command_queue, tensor_data_, tensor_dims_);
    } else {
      DLOG << " dim >  2 norm ~~~~~ ";
      InitCLImage(context, command_queue, tensor_data_, tensor_dims_);
    }


//    InitCLImage(context, command_queue, nullptr, dim);
L
liuruilong 已提交
102 103 104
    initialized_ = true;
  }

L
liuruilong 已提交
105
  cl_mem GetCLImage() const { return cl_image_.get(); }
L
liuruilong 已提交
106

Y
yangfei 已提交
107
  const DDim &ImageDims() const { return image_dims_; }
L
liuruilong 已提交
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127

  inline size_t ImageWidth() const { return image_width_; }

  inline size_t ImageHeight() const { return image_height_; }

  /*
   * block of channels, 4 channel one block
   * */
  inline size_t CBlock() const { return c_block_; }

  /*
   *  width of original tensor
   * */
  inline size_t WidthOfOneBlock() const { return width_of_one_block_; }

  /*
   *  height of original tensor
   * */
  inline size_t HeightOfOneBlock() const { return height_of_one_block_; }

L
liuruilong 已提交
128
  inline cl_command_queue CommandQueue() const { return command_queue_; }
Y
yangfei 已提交
129

L
liuruilong 已提交
130 131 132 133 134 135 136 137 138 139 140 141
  /*
   *  resize original tensor dim
   * */
  inline CLImage &Resize(const DDim &dims) {
    tensor_dims_ = dims;
    return *this;
  }

  template <typename T>
  T *data() const {
    if (initialized_) {
      PADDLE_MOBILE_THROW_EXCEPTION(
L
liuruilong 已提交
142 143
          " cl image has initialized, tensor data has been deleted, can't use "
          "tensor data");
L
liuruilong 已提交
144 145 146 147 148 149 150 151 152 153 154 155 156 157
    }
    return reinterpret_cast<T *>(tensor_data_);
  }

  /*
   *  numel of tensor dim
   * */
  inline int64_t numel() const { return product(tensor_dims_); }

  /*
   *  original tensor dim
   * */
  const DDim &dims() const { return tensor_dims_; }

L
liuruilong 已提交
158
  const ImageType GetImageType() const { return image_type_; }
Y
yangfei 已提交
159

L
liuruilong 已提交
160
 private:
L
liuruilong 已提交
161
  ImageType image_type_ = Invalid;
L
liuruilong 已提交
162 163
  void InitCLImage2C(cl_context context, cl_command_queue command_queue,
                     float *tensor_data, const DDim &dim) {
L
liuruilong 已提交
164
    image_type_ = Folder;
Y
yangfei 已提交
165
    command_queue_ = command_queue;
D
dolphin8 已提交
166 167 168 169 170 171 172 173
    assert(dim.size() <= 2);
    int tdim[2] = {1, 1};
    if (dim.size() == 1) {
      tdim[1] = dim[0];
    } else {
      tdim[0] = dim[0];
      tdim[1] = dim[1];
    }
Y
yangfei 已提交
174
    int width = (tdim[1] + 3) / 4;
D
dolphin8 已提交
175
    int height = tdim[0];
Y
yangfei 已提交
176 177 178

    image_width_ = width;
    image_height_ = height;
L
liuruilong 已提交
179 180 181 182 183
    image_dims_ = make_ddim({width, height});
    width_of_one_block_ = width;
    height_of_one_block_ = height;
    c_block_ = 1;

D
dolphin8 已提交
184 185 186 187 188
    std::unique_ptr<half_t[]> imageData{};
    if (tensor_data) {
      imageData.reset(new half_t[width * height * 4]);
      for (int h = 0; h < tdim[0]; h++) {
        for (int w = 0; w < tdim[1]; w++) {
L
liuruilong 已提交
189 190
          imageData[(h * width + w / 4) * 4 + (w % 4)] =
              Float2Half(tensor_data[h * tdim[1] + w]);
D
dolphin8 已提交
191 192 193 194 195 196
        }
      }
    }
    InitCLImage(context, width, height, imageData.get());
  }

L
liuruilong 已提交
197
  void InitCLImage(cl_context context, int width, int height, void *data) {
D
dolphin8 已提交
198 199 200
    cl_image_format cf = {.image_channel_order = CL_RGBA,
                          .image_channel_data_type = CL_HALF_FLOAT};
    cl_image_desc cid = {
L
liuruilong 已提交
201 202 203 204 205 206 207 208 209 210
        .image_type = CL_MEM_OBJECT_IMAGE2D,
        .image_width = width,
        .image_height = height,
        .image_depth = 1,
        .image_array_size = 1,
        .image_row_pitch = 0,
        .image_slice_pitch = 0,
        .num_mip_levels = 0,
        .num_samples = 0,
        // .buffer = nullptr
D
dolphin8 已提交
211 212 213
    };
    cid.buffer = nullptr;
    cl_int err;
L
liuruilong 已提交
214
    cl_mem cl_image = clCreateImage(
L
liuruilong 已提交
215 216 217 218 219
        context, CL_MEM_READ_WRITE | (data ? CL_MEM_COPY_HOST_PTR : 0),
        &cf,   // const cl_image_format *image_format
        &cid,  // const cl_image_desc *image_desc
        data,  // void *host_ptr
        &err);
L
liuruilong 已提交
220
    cl_image_.reset(cl_image);
D
dolphin8 已提交
221 222 223 224 225
    if (err != CL_SUCCESS) {
      CL_CHECK_ERRORS(err);
      PADDLE_MOBILE_THROW_EXCEPTION(" create image 2d error ");
    }
  }
L
liuruilong 已提交
226 227
  void InitCLImage(cl_context context, cl_command_queue command_queue,
                   float *tensor_data, const DDim &dim) {
L
liuruilong 已提交
228
    image_type_ = Normal;
L
liuruilong 已提交
229
    DLOG << " tensor dim: " << dim;
D
dolphin8 已提交
230
    // NCHW -> [W * (C+3)/4, H * N]
Y
yangfei 已提交
231
    tensor_dims_ = dim;
Y
yangfei 已提交
232
    command_queue_ = command_queue;
Y
yangfei 已提交
233 234 235
    if (tensor_data) {
      tensor_data_ = tensor_data;
    }
L
liuruilong 已提交
236
    size_t new_dims[] = {1, 1, 1, 1};
L
liuruilong 已提交
237

L
liuruilong 已提交
238 239
    for (int j = 0; j < dim.size(); ++j) {
      new_dims[4 - dim.size() + j] = dim[j];
Y
yangfei 已提交
240 241
    }

L
liuruilong 已提交
242 243 244 245 246 247 248 249 250 251
    size_t N, C, H, W;

    N = new_dims[0];
    C = new_dims[1];
    H = new_dims[2];
    W = new_dims[3];

    width_of_one_block_ = W;
    height_of_one_block_ = H;

D
dolphin8 已提交
252 253
    size_t width = W * ((C + 3) / 4);
    size_t height = H * N;
L
liuruilong 已提交
254 255 256

    image_width_ = width;
    image_height_ = height;
L
liuruilong 已提交
257
    image_dims_ = make_ddim({image_width_, image_height_});
Y
yangfei 已提交
258
    c_block_ = width / W;
L
liuruilong 已提交
259

L
liuruilong 已提交
260 261 262 263
    DLOG << " tensor dim " << tensor_dims_;
    DLOG << " 赋值时: image width: " << image_width_;
    DLOG << " 赋值时: image height: " << image_height_;

D
dolphin8 已提交
264
    std::unique_ptr<half_t[]> imageData{};
265
    int count = 0;
L
liuruilong 已提交
266
    if (tensor_data != nullptr) {
D
dolphin8 已提交
267
      imageData.reset(new half_t[width * height * 4]);
L
liuruilong 已提交
268
      float *p = tensor_data;
269 270
      size_t i0 = 0;
      for (int n = 0; n < N; n++) {
L
liuruilong 已提交
271
        for (int c = 0; c < c_block_ * 4; c++) {
D
dolphin8 已提交
272
          size_t i1 = i0 + (c / 4) * W;
273 274 275
          for (int h = 0; h < H; h++) {
            size_t i2 = (i1 << 2) + c % 4;
            for (int w = 0; w < W; w++) {
L
liuruilong 已提交
276 277 278 279 280 281 282 283 284 285
              if (c < C) {
                // int x = (n * width * H + h * width + (c / 4) * W + w) * 4 +
                // (c % 4);
                imageData[i2] = Float2Half(*p);
                i2 += 4;
                p++;
              } else {
                imageData[i2] = 0.0;
                i2 += 4;
              }
286 287 288 289 290 291
            }
            i1 += width;
          }
        }
        i0 += width * H;
      }
D
dolphin8 已提交
292
    }
D
dolphin8 已提交
293
    InitCLImage(context, width, height, imageData.get());
L
liuruilong 已提交
294 295
  }

L
liuruilong 已提交
296
  bool initialized_ = false;
L
liuruilong 已提交
297
  std::unique_ptr<_cl_mem, CLMemDeleter> cl_image_;
L
liuruilong 已提交
298 299 300 301 302
  size_t image_width_;
  size_t width_of_one_block_;
  size_t height_of_one_block_;
  size_t image_height_;
  size_t c_block_;
303
  DDim tensor_dims_;
L
liuruilong 已提交
304 305
  DDim image_dims_;
  float *tensor_data_;
L
liuruilong 已提交
306
  cl_context context_;
Y
yangfei 已提交
307
  cl_command_queue command_queue_;
L
liuruilong 已提交
308 309
};

L
liuruilong 已提交
310 311
void TensorToCLImage(Tensor *tensor, CLImage *image,
                     cl_command_queue commandQueue);
Y
yangfei 已提交
312

L
liuruilong 已提交
313 314
void CLImageToTensor(CLImage *image, Tensor *tensor,
                     cl_command_queue commandQueue);
L
liuruilong 已提交
315

L
liuruilong 已提交
316 317 318 319
#ifdef PADDLE_MOBILE_DEBUG
Print &operator<<(Print &printer, const CLImage &image);
#endif

320 321
}  // namespace framework
}  // namespace paddle_mobile