api.cpp 16.7 KB
Newer Older
H
hanbuhe 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* 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. */

Z
zhangyang 已提交
15
#include "fpga/V2/api.h"
H
hanbuhe 已提交
16 17 18
#include <fcntl.h>
#include <sys/ioctl.h>
#include <algorithm>
19
#include <map>
Z
zhangyang 已提交
20 21 22
#include "fpga/V2/bias_scale.h"
#include "fpga/V2/filter.h"
#include "fpga/V2/image.h"
Z
zhangyang 已提交
23
#define FPGA_TEST_MODE
Z
zhangyang 已提交
24
// #define PADDLE_MOBILE_OS_LINUX
Z
zhangyang 已提交
25

Z
zhangyang 已提交
26
namespace paddle_mobile {
H
hanbuhe 已提交
27 28 29 30
namespace fpga {

static int fd = -1;
static const char *device_path = "/dev/fpgadrv0";
31
static std::map<void *, size_t> memory_map;
H
hanbuhe 已提交
32

H
hanbuhe 已提交
33
static inline int do_ioctl(int req, const void *arg) {
H
hanbuhe 已提交
34
#ifdef PADDLE_MOBILE_OS_LINUX
35 36 37
  int result = ioctl(fd, req, (uint64_t)arg);
  PADDLE_MOBILE_ENFORCE(result == 0, "ioctl didn't return correctly");
  return result;
H
hanbuhe 已提交
38 39 40
#else
  return -1;
#endif
Z
zhangyang 已提交
41
}
H
hanbuhe 已提交
42 43 44 45 46 47 48 49 50 51

int open_device() {
  if (fd == -1) {
    fd = open(device_path, O_RDWR);
  }
  return fd;
}

// memory management;
void *fpga_malloc(size_t size) {
52 53
  static uint64_t counter = 0;

H
hanbuhe 已提交
54
#ifdef PADDLE_MOBILE_OS_LINUX
55
  auto ptr = mmap64(nullptr, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
H
hanbuhe 已提交
56
#else
57
  auto ptr = malloc(size);
H
hanbuhe 已提交
58
#endif
59 60
  counter += size;
  memory_map.insert(std::make_pair(ptr, size));
Z
zhangyang 已提交
61 62
  //  DLOG << "Address: " << ptr << ", " << size << " bytes allocated. Total "
  //       << counter << " bytes";
63
  return ptr;
H
hanbuhe 已提交
64 65
}

66
void fpga_free(void *ptr) {
67 68 69 70 71 72 73
  static uint64_t counter = 0;
  size_t size = 0;

  auto iter = memory_map.find(ptr);  // std::map<void *, size_t>::iterator
  if (iter != memory_map.end()) {
    size = iter->second;
    memory_map.erase(iter);
Z
zhangyang 已提交
74
#ifdef PADDLE_MOBILE_OS_LINUX
75
    munmap(ptr, size);
76
#else
77
    free(ptr);
78
#endif
79
    counter += size;
Z
zhangyang 已提交
80 81
    //    DLOG << "Address: " << ptr << ", " << size << " bytes freed. Total "
    //         << counter << " bytes";
82 83 84
  } else {
    DLOG << "Invalid pointer";
  }
85
}
H
hanbuhe 已提交
86 87 88 89 90

void fpga_copy(void *dest, const void *src, size_t num) {
  memcpy(dest, src, num);
}

91 92 93 94 95 96 97 98 99 100 101 102 103 104
int fpga_flush(void *address, size_t size) {
  struct MemoryCacheArgs args = {nullptr};
  args.address = address;
  args.size = size;
  return do_ioctl(IOCTL_MEMCACHE_FLUSH, &args);
}

int fpga_invalidate(void *address, size_t size) {
  struct MemoryCacheArgs args = {nullptr};
  args.address = address;
  args.size = size;
  return do_ioctl(IOCTL_MEMCACHE_INVAL, &args);
}

Z
zhangyang 已提交
105
half fp32_2_fp16(float fp32_num) {
Z
zhangyang 已提交
106
  unsigned long tmp = *(unsigned long *)(&fp32_num);  // NOLINT
Z
zhangyang 已提交
107 108
  auto t = (half)(((tmp & 0x007fffff) >> 13) | ((tmp & 0x80000000) >> 16) |
                  (((tmp & 0x7f800000) >> 13) - (112 << 10)));
Z
zhangyang 已提交
109 110 111 112 113 114 115 116 117 118 119 120 121
  if (tmp & 0x1000) {
    t++;  // roundoff
  }
  return t;
}

float fp16_2_fp32(half fp16_num) {
  int frac = (fp16_num & 0x3ff);
  int exp = ((fp16_num & 0x7c00) >> 10) + 112;
  int s = fp16_num & 0x8000;
  int tmp = 0;
  float fp32_num;
  tmp = s << 16 | exp << 23 | frac << 13;
Z
zhangyang 已提交
122
  fp32_num = *(float *)&tmp;  // NOLINT
Z
zhangyang 已提交
123 124 125
  return fp32_num;
}

126
int ComputeBasicConv(const struct ConvArgs &args) {
127
#ifdef FPGA_TEST_MODE
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
  DLOG << "======Compute Basic Conv======";
  DLOG << "   relu_enabled:" << args.relu_enabled
       << "   sb_address:" << args.sb_address
       << "   filter_address:" << args.filter_address
       << "   filter_num:" << args.filter_num
       << "   group_num:" << args.group_num;
  DLOG << "   image_address:" << args.image.address
       << "   image_scale_address:" << args.image.scale_address
       << "   image_channels:" << args.image.channels
       << "   image_height:" << args.image.height
       << "   image_width:" << args.image.width
       << "   pad_height:" << args.image.pad_height
       << "   pad_width:" << args.image.pad_width;
  DLOG << "   kernel_height:" << args.kernel.height
       << "   kernel_width:" << args.kernel.width
       << "   stride_h:" << args.kernel.stride_h
       << "   stride_w:" << args.kernel.stride_w;
  DLOG << "   out_address:" << args.output.address
       << "   out_scale_address:" << args.output.scale_address;
147
#endif
148 149 150
  return do_ioctl(IOCTL_CONFIG_CONV, &args);
}

Z
zhangyang 已提交
151
int ComputeFpgaConv(const struct SplitConvArgs &args) {
Z
zhangyang 已提交
152
  ComputeBasicConv(args.conv_args[0]);
H
hanbuhe 已提交
153
}
Z
zhangyang 已提交
154

H
hanbuhe 已提交
155
int ComputeFpgaPool(const struct PoolingArgs &args) {
Z
zhangyang 已提交
156
#ifdef FPGA_TEST_MODE
157
  DLOG << "=============ComputeFpgaPool===========";
Z
zhangyang 已提交
158 159
  DLOG << "   mode:" << args.mode
       << "   kernel_reciprocal:" << fp16_2_fp32(args.kernel_reciprocal);
Z
zhangyang 已提交
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
  DLOG << "   image_address:" << args.image.address
       << "   image_scale_address:" << args.image.scale_address
       << "   image_channels:" << args.image.channels
       << "   image_height:" << args.image.height
       << "   image_width:" << args.image.width
       << "   pad_height:" << args.image.pad_height
       << "   pad_width:" << args.image.pad_width;
  DLOG << "   kernel_height:" << args.kernel.height
       << "   kernel_width:" << args.kernel.width
       << "   stride_h:" << args.kernel.stride_h
       << "   stride_w:" << args.kernel.stride_w;
  DLOG << "   out_address:" << args.output.address
       << "   out_scale_address:" << args.output.scale_address;
#endif

H
hanbuhe 已提交
175
  return do_ioctl(IOCTL_CONFIG_POOLING, &args);
H
hanbuhe 已提交
176
}
Z
zhangyang 已提交
177

H
hanbuhe 已提交
178
int ComputeFpgaEWAdd(const struct EWAddArgs &args) {
Z
zhangyang 已提交
179
#ifdef FPGA_TEST_MODE
180
  DLOG << "=============ComputeFpgaEWAdd===========";
181
  DLOG << "   relu_enabled:" << args.relu_enabled
Z
zhangyang 已提交
182 183
       << "   const0:" << fp16_2_fp32(int16_t(args.const0))
       << "   const1:" << fp16_2_fp32(int16_t(args.const1));
Z
zhangyang 已提交
184 185 186 187 188 189 190 191
  DLOG << "   image0_address:" << args.image0.address
       << "   image0_scale_address:" << args.image0.scale_address
       << "   image0_channels:" << args.image0.channels
       << "   image0_height:" << args.image0.height
       << "   image0_width:" << args.image0.width
       << "   pad0_height:" << args.image0.pad_height
       << "   pad0_width:" << args.image0.pad_width;
  DLOG << "   image1_address:" << args.image1.address
Z
zhangyang 已提交
192
       << "   image1_scale_address:" << args.image1.scale_address
Z
zhangyang 已提交
193 194 195 196 197 198 199 200 201
       << "   image1_channels:" << args.image1.channels
       << "   image1_height:" << args.image1.height
       << "   image1_width:" << args.image1.width
       << "   pad1_height:" << args.image1.pad_height
       << "   pad_width:" << args.image1.pad_width;
  DLOG << "   out_address:" << args.output.address
       << "   out_scale_address:" << args.output.scale_address;
#endif

H
hanbuhe 已提交
202 203 204
  return do_ioctl(IOCTL_CONFIG_EW, &args);
}
int PerformBypass(const struct BypassArgs &args) {
Z
zhangyang 已提交
205
#ifdef FPGA_TEST_MODE
206 207 208 209 210
  DLOG << "=============ComputeFpgaBypass===========";
  DLOG << "   input_type:" << args.input_data_type
       << "   output_type:" << args.output_data_type
       << "   input_layout_type:" << args.input_layout_type
       << "   output_layout_type:" << args.output_layout_type;
Z
zhangyang 已提交
211 212 213 214 215 216 217 218 219 220 221
  DLOG << "   image_address:" << args.image.address
       << "   image_scale_address:" << args.image.scale_address
       << "   image_channels:" << args.image.channels
       << "   image_height:" << args.image.height
       << "   image_width:" << args.image.width
       << "   pad_height:" << args.image.pad_height
       << "   pad_width:" << args.image.pad_width;
  DLOG << "   out_address:" << args.output.address
       << "   out_scale_address:" << args.output.scale_address;
#endif

H
hanbuhe 已提交
222
  return do_ioctl(IOCTL_CONFIG_BYPASS, &args);
H
hanbuhe 已提交
223
}
Z
zhangyang 已提交
224

Z
zhangyang 已提交
225
int ComputeFPGAConcat(const struct ConcatArgs &args) {
226 227 228 229
#ifdef FPGA_TEST_MODE
  DLOG << "=============ComputeFpgaConcat===========";
  DLOG << "   Image_num: " << args.image_num
       << "   out_address:" << args.image_out
Z
zhangyang 已提交
230 231
       << "   out_scale_address:" << args.scale_out
       << "   out_channel:" << args.out_channel;
232 233 234 235
  DLOG << "   image_height:" << args.height << "   image_width:" << args.width;
  for (int i = 0; i < args.image_num; i++) {
    DLOG << "   " << i << "th:        ";
    DLOG << "   channel_num:" << args.channel_num[i]
Z
zhangyang 已提交
236
         << "   aligned_channel_num:" << args.aligned_channel_num[i]
237 238 239 240 241
         << "   image_address:" << args.images_in[i]
         << "   image_scale_address:" << args.scales_in[i];
  }
#endif

Z
zhangyang 已提交
242 243
  image::concat_images(args.images_in, args.scales_in, args.image_out,
                       args.scale_out, args.image_num, args.channel_num,
Z
zhangyang 已提交
244 245
                       args.height, args.width, args.aligned_channel_num,
                       args.out_channel);
Z
zhangyang 已提交
246 247 248
  return 0;
}

Z
zhangyang 已提交
249 250
void format_image(framework::Tensor *image_tensor) {
  auto dims = image_tensor->dims();
Z
zhangyang 已提交
251
  auto channel = dims[1], height = dims[2], width = dims[3];
252
  auto data_ptr = image_tensor->data<float>();
Z
zhangyang 已提交
253
  size_t memory_size = channel * height * width * sizeof(float);
254
  auto new_data = (float *)fpga_malloc(memory_size);  // NOLINT
Z
zhangyang 已提交
255
  fpga_copy(new_data, data_ptr, memory_size);
Z
zhangyang 已提交
256 257 258 259
  int aligned_channel = filter::calc_aligned_channel((int)channel);  // NOLINT
  image::format_image(&new_data, (int)channel, (int)height,          // NOLINT
                      (int)width,                                    // NOLINT
                      aligned_channel);
Z
zhangyang 已提交
260 261 262
  image_tensor->reset_data_ptr(new_data);
}

Z
zhangyang 已提交
263
void format_fp16_ofm(framework::Tensor *ofm_tensor, int aligned_channel) {
Z
zhangyang 已提交
264
  auto dims = ofm_tensor->dims();
265 266
  size_t memory_size = 0;
  if (dims.size() == 4) {
Z
zhangyang 已提交
267 268
    auto height = dims[2], width = dims[3];
    memory_size = height * width * aligned_channel * sizeof(half);
269
  } else if (dims.size() == 2) {
Z
zhangyang 已提交
270
    memory_size = aligned_channel * sizeof(half);
271 272 273 274 275 276 277 278
  } else {
    DLOG << "Wrong ofm dimension";
  }
  auto p = fpga_malloc(memory_size);
  memset(p, 0, memory_size);
  ofm_tensor->reset_data_ptr(p);
}

Z
zhangyang 已提交
279
void format_fp32_ofm(framework::Tensor *ofm_tensor, int aligned_channel) {
280 281 282
  auto dims = ofm_tensor->dims();
  size_t memory_size = 0;
  if (dims.size() == 4) {
Z
zhangyang 已提交
283 284
    auto height = dims[2], width = dims[3];
    memory_size = height * width * aligned_channel * sizeof(float);
285
  } else if (dims.size() == 2) {
Z
zhangyang 已提交
286
    memory_size = aligned_channel * sizeof(float);
287 288 289 290 291 292
  } else {
    DLOG << "Wrong ofm dimension";
  }
  auto p = fpga_malloc(memory_size);
  memset(p, 0, memory_size);
  ofm_tensor->reset_data_ptr(p);
Z
zhangyang 已提交
293 294
}

Z
zhangyang 已提交
295 296
float filter_find_max(framework::Tensor *filter_tensor) {
  auto filter_ptr = filter_tensor->data<float>();
Z
zhangyang 已提交
297
  return filter::find_max(filter_ptr, (int)filter_tensor->numel());  // NOLINT
Z
zhangyang 已提交
298
}
Z
zhangyang 已提交
299

Z
zhangyang 已提交
300 301
int get_aligned_channel_num(int channel_num) {
  return filter::calc_aligned_channel(channel_num);
Z
zhangyang 已提交
302 303
}

Z
zhangyang 已提交
304
int get_aligned_filter_num(framework::Tensor *filter_tensor) {
Z
zhangyang 已提交
305
  auto dims = filter_tensor->dims();
Z
zhangyang 已提交
306
  return filter::calc_aligned_num((int)dims[0], (int)dims[1]);  // NOLINT
Z
zhangyang 已提交
307 308
}

Z
zhangyang 已提交
309 310 311
int get_conv_output_channel(framework::Tensor *filter_tensor) {
  int aligned_filter_num = get_aligned_filter_num(filter_tensor);
  return get_aligned_channel_num(aligned_filter_num);
Z
zhangyang 已提交
312
}
Z
zhangyang 已提交
313 314
void format_filter(framework::Tensor *filter_tensor, float max_value,
                   int group_num) {
315 316
  filter_tensor->scale[0] = float(max_value / 127.0);  // NOLINT
  filter_tensor->scale[1] = float(127.0 / max_value);  // NOLINT
Z
zhangyang 已提交
317
  auto dims = filter_tensor->dims();
Z
zhangyang 已提交
318
  auto num = dims[0], channel = dims[1], height = dims[2], width = dims[3];
319
  auto data_ptr = filter_tensor->data<float>();
Z
zhangyang 已提交
320
  size_t memory_size = num * channel * height * width * sizeof(float);
321
  auto new_data = (float *)fpga_malloc(memory_size);  // NOLINT
Z
zhangyang 已提交
322
  fpga_copy(new_data, data_ptr, memory_size);
Z
zhangyang 已提交
323 324 325
  filter::format_filter(&new_data, (int)num, (int)channel,  // NOLINT
                        (int)height,                        // NOLINT
                        (int)width, group_num, max_value);  // NOLINT
Z
zhangyang 已提交
326 327 328
  filter_tensor->reset_data_ptr(new_data);
}

Z
zhangyang 已提交
329 330 331 332 333 334 335 336 337
void format_fc_filter(framework::Tensor *filter_tensor, float max_value) {
  filter_tensor->scale[0] = float(max_value / 127.0);  // NOLINT
  filter_tensor->scale[1] = float(127.0 / max_value);  // NOLINT
  auto dims = filter_tensor->dims();
  auto num = dims[0], channel = dims[1], height = dims[2], width = dims[3];
  auto data_ptr = filter_tensor->data<float>();
  size_t memory_size = num * channel * height * width * sizeof(float);
  auto new_data = (float *)fpga_malloc(memory_size);  // NOLINT
  fpga_copy(new_data, data_ptr, memory_size);
Z
zhangyang 已提交
338 339 340
  filter::format_fc_filter(&new_data, (int)num, (int)channel,  // NOLINT
                           (int)height,                        // NOLINT
                           (int)width, 1, max_value);          // NOLINT
Z
zhangyang 已提交
341 342 343
  filter_tensor->reset_data_ptr(new_data);
}

Z
zhangyang 已提交
344 345 346 347 348 349
void format_bias_scale_array(float **bias_scale_array, int filter_num,
                             int filter_channel) {
  int num_after_alignment =
      filter::calc_aligned_num(filter_channel, filter_channel);
  bias_scale::format_bias_scale_array(bias_scale_array, filter_num,
                                      num_after_alignment);
Z
zhangyang 已提交
350 351
}

Z
zhangyang 已提交
352
void format_concat_output(framework::Tensor *out, int height, int width,
Z
zhangyang 已提交
353 354 355
                          uint32_t out_channel) {
  auto data_ptr = fpga_malloc(out_channel * height * width * sizeof(half));
  auto ddim = framework::make_ddim({1, out_channel, height, width});
Z
zhangyang 已提交
356 357 358 359
  out->Resize(ddim);
  out->reset_data_ptr(data_ptr);
}

Z
zhangyang 已提交
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
int format_conv_data(framework::Tensor *filter_tensor,
                     framework::Tensor *ofm_tensor, float *bs_ptr, int group) {
  float max_value = fpga::filter_find_max(filter_tensor);
  fpga::format_filter(filter_tensor, max_value, group);
  int aligned_num = get_aligned_filter_num(filter_tensor);
  fpga::format_bias_scale_array(&bs_ptr,
                                (int)filter_tensor->dims()[0],  // NOLINT
                                aligned_num);
  int aligned_channel = fpga::get_conv_output_channel(filter_tensor);
  fpga::format_fp16_ofm(ofm_tensor, aligned_channel);
  DLOG << aligned_channel;
  return aligned_channel;
}

int format_fc_data(framework::Tensor *filter_tensor,
                   framework::Tensor *ofm_tensor, float *bs_ptr) {
  float max_value = fpga::filter_find_max(filter_tensor);
  fpga::format_fc_filter(filter_tensor, max_value);
  int aligned_num = get_aligned_filter_num(filter_tensor);
  fpga::format_bias_scale_array(&bs_ptr,
                                (int)filter_tensor->dims()[0],  // NOLINT
                                aligned_num);
  int aligned_channel = fpga::get_conv_output_channel(filter_tensor);
  fpga::format_fp16_ofm(ofm_tensor, aligned_channel);
  DLOG << aligned_channel;
  return aligned_channel;
}

Z
zhangyang 已提交
388 389 390 391
void fill_split_arg(struct SplitConvArgs *arg, framework::Tensor *input,
                    framework::Tensor *out, framework::Tensor *filter,
                    bool relu_enabled, int group_num, int stride_h,
                    int stride_w, int padding_h, int padding_w, float *bs_ptr) {
392 393
  auto input_ptr = input->data<float>();
  auto filter_ptr = filter->data<float>();
394
  auto out_ptr = out->data<float>();
395 396

  arg->group_num = (uint32_t)group_num;
Z
zhangyang 已提交
397
  arg->split_num = 1;
398 399 400
  arg->filter_num = (uint32_t)filter->dims()[0];
  arg->output.address = out_ptr;
  arg->output.scale_address = out->scale;
401 402
  arg->conv_args =
      (ConvArgs *)fpga_malloc(arg->split_num * sizeof(ConvArgs));  // NOLINT
403 404 405 406

  arg->concat_arg.image_num = arg->split_num;
  arg->concat_arg.image_out = out_ptr;
  arg->concat_arg.scale_out = out->scale;
407 408
  arg->concat_arg.height = (uint32_t)out->dims()[2];
  arg->concat_arg.width = (uint32_t)out->dims()[3];
409 410

  int n = arg->split_num;
411 412 413 414
  arg->concat_arg.images_in =
      (half **)fpga_malloc(n * sizeof(int *));  // NOLINT
  arg->concat_arg.scales_in =
      (float **)fpga_malloc(n * sizeof(float *));  // NOLINT
415
  arg->concat_arg.channel_num =
416
      (uint32_t *)fpga_malloc(n * sizeof(uint32_t));  // NOLINT
417 418 419

  for (int i = 0; i < n; i++) {
    arg->conv_args[i].relu_enabled = relu_enabled;
Z
zhangyang 已提交
420 421 422 423
    arg->conv_args[i].sb_address = bs_ptr;
    arg->conv_args[i].filter_address = (int8_t *)filter_ptr;  // NOLINT
    arg->conv_args[i].filter_scale_address = filter->scale;
    arg->conv_args[i].filter_num = arg->filter_num;
424
    arg->conv_args[i].group_num = (uint32_t)group_num;
Z
zhangyang 已提交
425

426 427 428 429
    arg->conv_args[i].kernel.stride_h = (uint32_t)stride_h;
    arg->conv_args[i].kernel.stride_w = (uint32_t)stride_w;
    arg->conv_args[i].kernel.height = (uint32_t)filter->dims()[2];
    arg->conv_args[i].kernel.width = (uint32_t)filter->dims()[3];
Z
zhangyang 已提交
430

431
    arg->conv_args[i].image.address = input_ptr;
Z
zhangyang 已提交
432
    arg->conv_args[i].image.scale_address = input->scale;
433 434 435 436 437
    arg->conv_args[i].image.channels = (uint32_t)input->dims()[1];
    arg->conv_args[i].image.height = (uint32_t)input->dims()[2];
    arg->conv_args[i].image.width = (uint32_t)input->dims()[3];
    arg->conv_args[i].image.pad_height = (uint32_t)padding_h;
    arg->conv_args[i].image.pad_width = (uint32_t)padding_w;
Z
zhangyang 已提交
438 439 440

    arg->conv_args[i].output.address = out_ptr;
    arg->conv_args[i].output.scale_address = out->scale;
441 442 443
  }
}

H
hanbuhe 已提交
444
}  // namespace fpga
Z
zhangyang 已提交
445
}  // namespace paddle_mobile