cl_image_converter.cc 13.1 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 28 29 30 31 32 33 34 35 36 37 38
/* 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_converter.h"
#include <glog/logging.h>
#include <vector>

namespace paddle {
namespace lite {

DDim CLImageConverterDefault::InitImageDimInfoWith(const DDim &tensor_dim) {
  size_t new_dims[] = {1, 1, 1, 1};
  for (size_t j = 0; j < tensor_dim.size(); ++j) {
    new_dims[4 - tensor_dim.size() + j] = tensor_dim[j];
  }
  size_t N, C, H, W;
  N = new_dims[0];
  C = new_dims[1];
  H = new_dims[2];
  W = new_dims[3];
  size_t width = W * ((C + 3) / 4);
  size_t height = H * N;
  return DDim(
      std::vector<DDim::value_type>({static_cast<DDim::value_type>(width),
                                     static_cast<DDim::value_type>(height)}));
}

Z
ZhenWang 已提交
39
void CLImageConverterDefault::NCHWToImage(float *nchw, float *image,
Z
Zhen Wang 已提交
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
                                          const DDim &tensor_dim) {
  size_t new_dims[] = {1, 1, 1, 1};
  for (size_t j = 0; j < tensor_dim.size(); ++j) {
    new_dims[4 - tensor_dim.size() + j] = tensor_dim[j];
  }

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

  DDim in_image_dim = InitImageDimInfoWith(tensor_dim);

  VLOG(3) << " tensor dim: " << tensor_dim;
  VLOG(3) << " image dim: " << in_image_dim;

  size_t width = in_image_dim[0];
  size_t w_block = width / W;

  float *p = nchw;
  size_t i0 = 0;
  for (size_t n = 0; n < N; n++) {
    for (size_t c = 0; c < w_block * 4; c++) {
      size_t i1 = i0 + (c / 4) * W;
      for (size_t h = 0; h < H; h++) {
        size_t i2 = (i1 << 2) + c % 4;
        for (size_t w = 0; w < W; w++) {
          if (c < C) {
            // size_t x = (n * width * H + h * width + (c / 4) * W + w) * 4 +
            // (c % 4);
Z
ZhenWang 已提交
71
            image[i2] = *p;
Z
Zhen Wang 已提交
72 73 74 75 76 77 78 79 80 81 82 83 84 85
            i2 += 4;
            p++;
          } else {
            image[i2] = 0.0;
            i2 += 4;
          }
        }
        i1 += width;
      }
    }
    i0 += width * H;
  }
}

Z
ZhenWang 已提交
86
void CLImageConverterDefault::ImageToNCHW(float *image, float *tensor,
Z
Zhen Wang 已提交
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
                                          const DDim &image_dim,
                                          const DDim &tensor_dim) {
  size_t new_dims[] = {1, 1, 1, 1};
  for (size_t j = 0; j < tensor_dim.size(); ++j) {
    new_dims[4 - tensor_dim.size() + j] = tensor_dim[j];
  }

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

  size_t width = image_dim[0];
  float *p = tensor;

  size_t i0 = 0;
  for (size_t n = 0; n < N; n++) {
    for (size_t c = 0; c < C; c++) {
      size_t i1 = i0 + (c / 4) * W;
      for (size_t h = 0; h < H; h++) {
        size_t i2 = (i1 << 2) + c % 4;
        for (size_t w = 0; w < W; w++) {
Z
ZhenWang 已提交
110
          *p = image[i2];
Z
Zhen Wang 已提交
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
          i2 += 4;
          p++;
        }
        i1 += width;
      }
    }
    i0 += width * H;
  }
}

DDim CLImageConverterFolder::InitImageDimInfoWith(const DDim &tensor_dim) {
  if (tensor_dim.size() <= 2) {
    size_t tdim[2] = {1, 1};
    if (tensor_dim.size() == 1) {
      tdim[1] = tensor_dim[0];
    } else {
      tdim[0] = tensor_dim[0];
      tdim[1] = tensor_dim[1];
    }
    size_t width = (tdim[1] + 3) / 4;
    size_t height = tdim[0];

    width_of_one_block_ = width;
    height_of_one_block_ = height;
    c_block_ = 1;

    return DDim(
        std::vector<DDim::value_type>({static_cast<DDim::value_type>(width),
                                       static_cast<DDim::value_type>(height)}));

  } else {
    size_t new_dims[] = {1, 1, 1, 1};
    for (size_t j = 0; j < tensor_dim.size(); ++j) {
      new_dims[4 - tensor_dim.size() + j] = tensor_dim[j];
    }
    size_t N, C, H, W;
    N = new_dims[0];
    C = new_dims[1];
    H = new_dims[2];
    W = new_dims[3];
    size_t width = W * ((C + 3) / 4);
    size_t height = H * N;

    width_of_one_block_ = W;
    height_of_one_block_ = H;
    c_block_ = width / W;

    return DDim(
        std::vector<DDim::value_type>({static_cast<DDim::value_type>(width),
                                       static_cast<DDim::value_type>(height)}));
  }
}

Z
ZhenWang 已提交
164
void CLImageConverterFolder::NCHWToImage(float *tensor, float *image,
Z
Zhen Wang 已提交
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
                                         const DDim &tensor_dim) {
  CHECK(tensor_dim.size() <= 4 && tensor_dim.size() > 0)
      << " Tensor dim is not support!";

  if (tensor_dim.size() > 2) {
    CLImageConverterDefault default_converter;
    default_converter.NCHWToImage(tensor, image, tensor_dim);

  } else {
    size_t tdim[2] = {1, 1};
    if (tensor_dim.size() == 1) {
      tdim[1] = tensor_dim[0];
    } else {
      tdim[0] = tensor_dim[0];
      tdim[1] = tensor_dim[1];
    }

    DDim image_dim = InitImageDimInfoWith(tensor_dim);
    size_t width = image_dim[0];

    for (size_t h = 0; h < tdim[0]; h++) {
      for (size_t w = 0; w < tdim[1]; w++) {
Z
ZhenWang 已提交
187
        image[(h * width + w / 4) * 4 + (w % 4)] = tensor[h * tdim[1] + w];
Z
Zhen Wang 已提交
188 189 190 191 192
      }
    }
  }
}

Z
ZhenWang 已提交
193
void CLImageConverterFolder::ImageToNCHW(float *image, float *tensor,
Z
Zhen Wang 已提交
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
                                         const DDim &image_dim,
                                         const DDim &tensor_dim) {
  if (tensor_dim.size() > 2) {
    CLImageConverterDefault default_converter;
    default_converter.ImageToNCHW(image, tensor, image_dim, tensor_dim);

  } else {
    size_t width = image_dim[0];
    size_t H = 1, W = 1;

    if (tensor_dim.size() == 2) {
      H = tensor_dim[0];
      W = tensor_dim[1];
    } else if (tensor_dim.size() == 1) {
      W = tensor_dim[0];
    }

    float *p = tensor;

    for (size_t h = 0; h < H; h++) {
      for (size_t w = 0; w < W; w++) {
Z
ZhenWang 已提交
215
        p[h * W + w] = image[(h * width + w / 4) * 4 + (w % 4)];
Z
Zhen Wang 已提交
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
      }
    }
  }
}

DDim CLImageConverterNWBlock::InitImageDimInfoWith(const DDim &tensor_dim) {
  CHECK(tensor_dim.size() == 4) << " Tensor dim is not 4.";
  size_t N, C, H, W;
  N = tensor_dim[0];
  C = tensor_dim[1];
  H = tensor_dim[2];
  W = tensor_dim[3];
  size_t width = W * ((N + 3) / 4);
  size_t height = C * H;
  return DDim(
      std::vector<DDim::value_type>({static_cast<DDim::value_type>(width),
                                     static_cast<DDim::value_type>(height)}));
}

Z
ZhenWang 已提交
235
void CLImageConverterNWBlock::NCHWToImage(float *tensor, float *image,
Z
Zhen Wang 已提交
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
                                          const DDim &tensor_dim) {
  CHECK(tensor_dim.size() == 4) << " Tensor dim is not 4.";
  auto image_dim = InitImageDimInfoWith(tensor_dim);
  float *p = tensor;
  size_t N = tensor_dim[0];
  size_t C = tensor_dim[1];
  size_t H = tensor_dim[2];
  size_t W = tensor_dim[3];
  size_t width = image_dim[0];
  size_t height = image_dim[1];
  size_t block = image_dim[0] / tensor_dim[3];

  for (size_t n = 0; n < block * 4; n++) {
    for (size_t c = 0; c < C; c++) {
      for (size_t h = 0; h < H; ++h) {
        for (size_t w = 0; w < W; ++w) {
          size_t index = 4 * c * (width * H) + 4 * h * width + 4 * W * (n / 4) +
                         w * 4 + n % 4;
          if (n < N) {
Z
ZhenWang 已提交
255
            image[index] = *p;
Z
Zhen Wang 已提交
256 257 258 259 260 261 262 263 264 265 266 267 268 269
            p++;
          } else {
            image[index] = 0.0;
          }
          if (index >= (width * height * 4)) {
            LOG(INFO) << " index out of range ";
          }
        }
      }
    }
  }
  VLOG(3) << " init done";
}

Z
ZhenWang 已提交
270
void CLImageConverterNWBlock::ImageToNCHW(float *image, float *tensor,
Z
Zhen Wang 已提交
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
                                          const DDim &image_dim,
                                          const DDim &tensor_dim) {
  CHECK(tensor_dim.size() == 4) << " Tensor dim is not 4.";
  float *p = tensor;
  size_t N = tensor_dim[0];
  size_t C = tensor_dim[1];
  size_t H = tensor_dim[2];
  size_t W = tensor_dim[3];
  size_t width = image_dim[0];
  size_t height = image_dim[1];

  for (size_t n = 0; n < N; n++) {
    for (size_t c = 0; c < C; c++) {
      for (size_t h = 0; h < H; ++h) {
        for (size_t w = 0; w < W; ++w) {
          size_t index = 4 * c * (width * H) + 4 * h * width + 4 * W * (n / 4) +
                         w * 4 + n % 4;
Z
ZhenWang 已提交
288
          *p = image[index];
Z
Zhen Wang 已提交
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
          p++;
          if (index >= (width * height * 4)) {
            LOG(INFO) << " index out of range ";
          }
        }
      }
    }
  }
  VLOG(3) << " init done";
}

DDim CLImageConverterDWBlock::InitImageDimInfoWith(const DDim &tensor_dim) {
  CHECK(tensor_dim.size() == 4) << " Tensor dim is not 4.";
  size_t N, C, H, W;
  N = tensor_dim[0];
  C = tensor_dim[1];
  H = tensor_dim[2];
  W = tensor_dim[3];
  size_t width = W * ((N + 3) / 4);
  size_t height = C * H;
  return DDim(
      std::vector<DDim::value_type>({static_cast<DDim::value_type>(width),
                                     static_cast<DDim::value_type>(height)}));
}

Z
ZhenWang 已提交
314
void CLImageConverterDWBlock::NCHWToImage(float *tensor, float *image,
Z
Zhen Wang 已提交
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
                                          const DDim &tensor_dim) {
  size_t new_dims[] = {1, 1, 1, 1};
  for (size_t j = 0; j < tensor_dim.size(); ++j) {
    new_dims[4 - tensor_dim.size() + j] = tensor_dim[j];
  }

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

  DDim in_image_dim = InitImageDimInfoWith(tensor_dim);

  VLOG(3) << " tensor dim: " << tensor_dim;
  VLOG(3) << " image dim: " << in_image_dim;

  size_t width = in_image_dim[0];
  size_t w_block = width / W;

  float *p = tensor;
  size_t i0 = 0;
  for (size_t n = 0; n < N; n++) {
    for (size_t c = 0; c < w_block * 4; c++) {
      size_t i1 = i0 + (c / 4) * W;
      for (size_t h = 0; h < H; h++) {
        size_t i2 = (i1 << 2) + c % 4;
        for (size_t w = 0; w < W; w++) {
          if (c < C) {
            // size_t x = (n * width * H + h * width + (c / 4) * W + w) * 4 +
            // (c % 4);
Z
ZhenWang 已提交
346
            image[i2] = *p;
Z
Zhen Wang 已提交
347 348 349 350 351 352 353 354 355 356 357 358 359 360
            i2 += 4;
            p++;
          } else {
            image[i2] = 0.0;
            i2 += 4;
          }
        }
        i1 += width;
      }
    }
    i0 += width * H;
  }
}

Z
ZhenWang 已提交
361
void CLImageConverterDWBlock::ImageToNCHW(float *image, float *tensor,
Z
Zhen Wang 已提交
362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
                                          const DDim &image_dim,
                                          const DDim &tensor_dim) {
  CHECK(tensor_dim.size() == 4) << " Tensor dim is not 4.";
  float *p = tensor;
  size_t N = tensor_dim[1];
  size_t C = tensor_dim[0];
  size_t H = tensor_dim[2];
  size_t W = tensor_dim[3];
  size_t width = image_dim[0];

  size_t i0 = 0;
  for (size_t n = 0; n < N; n++) {
    for (size_t c = 0; c < C; c++) {
      size_t i1 = i0 + (c / 4) * W;
      for (size_t h = 0; h < H; h++) {
        size_t i2 = (i1 << 2) + c % 4;
        for (size_t w = 0; w < W; w++) {
Z
ZhenWang 已提交
379
          *p = image[i2];
Z
Zhen Wang 已提交
380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411
          i2 += 4;
          p++;
        }
        i1 += width;
      }
    }
    i0 += width * H;
  }
}

DDim CLImageConverterNormal::InitImageDimInfoWith(const DDim &tensor_dim) {
  size_t new_dims[] = {1, 1, 1, 1};
  for (size_t j = 0; j < tensor_dim.size(); ++j) {
    new_dims[4 - tensor_dim.size() + j] = tensor_dim[j];
  }
  size_t N, C, H, W;
  N = new_dims[0];
  C = new_dims[1];
  H = new_dims[2];
  W = new_dims[3];
  size_t width = W * ((C + 3) / 4);
  size_t height = H * N;

  width_of_one_block_ = W;
  height_of_one_block_ = H;
  c_block_ = width / W;

  return DDim(
      std::vector<DDim::value_type>({static_cast<DDim::value_type>(width),
                                     static_cast<DDim::value_type>(height)}));
}

Z
ZhenWang 已提交
412
void CLImageConverterNormal::NCHWToImage(float *tensor, float *image,
Z
Zhen Wang 已提交
413 414 415 416 417 418 419 420
                                         const DDim &tensor_dim) {
  CHECK(tensor_dim.size() <= 4 && tensor_dim.size() > 0)
      << " Tensor dim is not support!";

  CLImageConverterDefault default_converter;
  default_converter.NCHWToImage(tensor, image, tensor_dim);
}

Z
ZhenWang 已提交
421
void CLImageConverterNormal::ImageToNCHW(float *image, float *tensor,
Z
Zhen Wang 已提交
422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440
                                         const DDim &image_dim,
                                         const DDim &tensor_dim) {
  CLImageConverterDefault default_converter;
  default_converter.ImageToNCHW(image, tensor, image_dim, tensor_dim);
}

DDim CLImageConverterWinoTransWeight::InitImageDimInfoWith(
    const DDim &tensor_dim) {
  CHECK(tensor_dim.size() == 4) << " Tensor dim is not 4.";
  size_t N, C;
  N = tensor_dim[0];
  C = tensor_dim[1];
  size_t width = (C + 3) / 4;
  size_t height = N * 16;  // N * (wino_blk_size + 2) * (wino_blk_size + 2)
  return DDim(
      std::vector<DDim::value_type>({static_cast<DDim::value_type>(width),
                                     static_cast<DDim::value_type>(height)}));
}

Z
ZhenWang 已提交
441
void CLImageConverterWinoTransWeight::NCHWToImage(float *tensor, float *image,
Z
Zhen Wang 已提交
442 443
                                                  const DDim &tensor_dim) {}

Z
ZhenWang 已提交
444
void CLImageConverterWinoTransWeight::ImageToNCHW(float *image, float *tensor,
Z
Zhen Wang 已提交
445 446 447 448 449
                                                  const DDim &image_dim,
                                                  const DDim &tensor_dim) {}

}  // namespace lite
}  // namespace paddle