cl_image_converter.cpp 8.1 KB
Newer Older
L
liuruilong 已提交
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 39 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 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 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 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 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
/* 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 "framework/cl/cl_image_converter.h"

namespace paddle_mobile {
namespace framework {

const DDim &CLImageConverterDefault::InitImageDimInfoWith(
    const DDim &tensor_dim) {
  size_t new_dims[] = {1, 1, 1, 1};
  for (int 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 make_ddim({width, height});
}

void CLImageConverterDefault::NCHWToImage(float *nchw, half_t *image,
                                          const DDim &tensor_dim) {
  size_t new_dims[] = {1, 1, 1, 1};
  for (int 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);

  DLOG << " tensor dim " << tensor_dim;
  DLOG << " image dim " << in_image_dim;

  size_t width = in_image_dim[0];
  size_t height = in_image_dim[1];

  int w_block = width / W;

  float *p = nchw;
  size_t i0 = 0;
  for (int n = 0; n < N; n++) {
    for (int c = 0; c < w_block * 4; c++) {
      size_t i1 = i0 + (c / 4) * W;
      for (int h = 0; h < H; h++) {
        size_t i2 = (i1 << 2) + c % 4;
        for (int w = 0; w < W; w++) {
          if (c < C) {
            // int x = (n * width * H + h * width + (c / 4) * W + w) * 4 +
            // (c % 4);
            image[i2] = Float2Half(*p);
            i2 += 4;
            p++;
          } else {
            image[i2] = 0.0;
            i2 += 4;
          }
        }
        i1 += width;
      }
    }
    i0 += width * H;
  }
}

void CLImageConverterDefault::ImageToNCHW(half_t *image, float *tensor,
                                          const DDim &image_dim,
                                          const DDim &tensor_dim) {
  size_t new_dims[] = {1, 1, 1, 1};
  for (int 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];

  int width = image_dim[0];
  int height = image_dim[0];

  float *p = tensor;

  size_t i0 = 0;
  for (int n = 0; n < N; n++) {
    for (int c = 0; c < C; c++) {
      size_t i1 = i0 + (c / 4) * W;
      for (int h = 0; h < H; h++) {
        size_t i2 = (i1 << 2) + c % 4;
        for (int w = 0; w < W; w++) {
          *p = Half2Float(image[i2]);
          i2 += 4;
          p++;
        }
        i1 += width;
      }
    }
    i0 += width * H;
  }
}

const DDim &CLImageConverterFolder::InitImageDimInfoWith(
    const DDim &tensor_dim) {
  if (tensor_dim.size() <= 2) {
    int 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];
    }
    int width = (tdim[1] + 3) / 4;
    int height = tdim[0];

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

    return make_ddim({width, height});

  } else {
    size_t new_dims[] = {1, 1, 1, 1};
    for (int 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 make_ddim({width, height});
  }
}

void CLImageConverterFolder::NCHWToImage(float *tensor, half_t *image,
                                         const DDim &tensor_dim) {
  PADDLE_MOBILE_ENFORCE(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 {
    int 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);
    int width = image_dim[0];

    for (int h = 0; h < tdim[0]; h++) {
      for (int w = 0; w < tdim[1]; w++) {
        image[(h * width + w / 4) * 4 + (w % 4)] =
            Float2Half(tensor[h * tdim[1] + w]);
      }
    }
  }
}

void CLImageConverterFolder::ImageToNCHW(half_t *image, float *tensor,
                                         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 {
    int width = image_dim[0];
    int height = image_dim[1];
    int H, W;

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

    for (int h = 0; h < H; h++) {
      for (int w = 0; w < W; w++) {
        p[h * W + w] = Half2Float(image[(h * width + w / 4) * 4 + (w % 4)]);
      }
    }
  }
}

const DDim &CLImageConverterNWBlock::InitImageDimInfoWith(
    const DDim &tensor_dim) {
  PADDLE_MOBILE_ENFORCE(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 make_ddim({width, height});
}

void CLImageConverterNWBlock::NCHWToImage(float *tensor, half_t *image,
                                          const DDim &tensor_dim) {
  PADDLE_MOBILE_ENFORCE(tensor_dim.size() == 4, " tensor dim is not 4");
  auto image_dim = InitImageDimInfoWith(tensor_dim);
  float *p = tensor;
  int N = tensor_dim[0];
  int C = tensor_dim[1];
  int H = tensor_dim[2];
  int W = tensor_dim[3];
  int width = image_dim[0];
  int height = image_dim[1];
  int block = image_dim[0] / tensor_dim[3];

  for (int n = 0; n < block * 4; n++) {
    for (int c = 0; c < C; c++) {
      for (int h = 0; h < H; ++h) {
        for (int w = 0; w < W; ++w) {
251 252
          int index = 4 * c * (width * H) + 4 * h * width + 4 * W * (n / 4) +
                      w * 4 + n % 4;
L
liuruilong 已提交
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
          if (n < N) {
            image[index] = Float2Half(*p);
            p++;
          } else {
            image[index] = 0.0;
          }
          if (index >= (width * height * 4)) {
            DLOG << " index out of range ";
          }
        }
      }
    }
  }
  DLOG << " init done";
}

void CLImageConverterNWBlock::ImageToNCHW(half_t *image, float *tensor,
                                          const DDim &image_dim,
                                          const DDim &tensor_dim) {
  PADDLE_MOBILE_ENFORCE(tensor_dim.size() == 4, " tensor dim is not 4");
  float *p = tensor;
  int N = tensor_dim[0];
  int C = tensor_dim[1];
  int H = tensor_dim[2];
  int W = tensor_dim[3];
  int width = image_dim[0];
  int height = image_dim[1];
  int block = image_dim[0] / tensor_dim[3];

  for (int n = 0; n < N; n++) {
    for (int c = 0; c < C; c++) {
      for (int h = 0; h < H; ++h) {
        for (int w = 0; w < W; ++w) {
286 287
          int index = 4 * c * (width * H) + 4 * h * width + 4 * W * (n / 4) +
                      w * 4 + n % 4;
L
liuruilong 已提交
288 289 290 291 292 293 294 295 296 297 298 299 300 301
          *p = Half2Float(image[index]);
          p++;
          if (index >= (width * height * 4)) {
            DLOG << " index out of range ";
          }
        }
      }
    }
  }
  DLOG << " init done";
}

}  // namespace framework
}  // namespace paddle_mobile