zynqmp_api.cpp 12.9 KB
Newer Older
C
Chon 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/* 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 <fcntl.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
Y
Yan Chunwei 已提交
21
#include <algorithm>
C
Chon 已提交
22 23
#include <cstring>
#include <map>
Y
Yan Chunwei 已提交
24
#include <utility>
C
Chon 已提交
25

26
#include "lite/backends/fpga/KD/llapi/zynqmp_api.h"
C
Chon 已提交
27

Y
Yan Chunwei 已提交
28
namespace paddle {
C
Chon 已提交
29 30
namespace zynqmp {

31
#define PADDLE_MOBILE_OS_LINUX
C
Chon 已提交
32 33 34 35 36 37 38 39 40

static int fd = -1;
static const char *device_path = "/dev/fpgadrv0";
static std::map<void *, size_t> memory_map;

static size_t memory_size_max = 0;
static size_t memory_size = 0;

static inline int do_ioctl(uint64_t req, const void *arg) {
41 42
#ifdef PADDLE_MOBILE_OS_LINUX
  return ioctl(fd, req, arg);
C
Chon 已提交
43
#else
44
  return -1;
C
Chon 已提交
45 46 47 48
#endif
}

int open_device() {
49
  // std::cout << "open_device" << std::endl;
C
Chon 已提交
50 51 52
  if (fd == -1) {
    fd = open(device_path, O_RDWR);
  }
53
  // std::cout << "open_device fd:" << fd << std::endl;
C
Chon 已提交
54 55 56 57 58 59 60 61 62 63 64 65
  return fd;
}

void close_device() { close(fd); }

void reset_device() {
  FpgaResetArgs args;
  do_ioctl(IOCTL_FPGA_RESET, &args);
}

// memory management;
void *fpga_malloc(size_t size) {
66 67 68 69 70 71
// std::cout << "fpga malloc: 0x" << std::hex << size  << std::dec << "  (" <<
// size << ") - ";
#ifdef ENABLE_DEBUG
// std::cout << "fpga_malloc:" << size << std::endl;
#endif
#ifdef PADDLE_MOBILE_OS_LINUX
C
Chon 已提交
72 73 74 75 76 77
  void *ptr = reinterpret_cast<void *>(
      mmap64(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0));
  if (ptr == NULL) {
    std::cout << "not enough memory !";
    exit(-1);
  }
78
  // std::cout << std::hex << ptr << std::dec << std::endl;
C
Chon 已提交
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
  memory_map.insert(std::make_pair(ptr, size));
  memory_size += size;
  if (memory_size > memory_size_max) {
    memory_size_max = memory_size;
  }
  return ptr;
#else
  return malloc(size);
#endif
}

size_t fpga_get_memory_size(void *ptr) { return memory_map[ptr]; }

size_t fpga_get_memory_size_max() { return memory_size_max; }

size_t fpga_diagnose_memory(int detailed) {
  size_t total = 0;
96 97
  //        size_t size = 0;
  //        int i = 0;
C
Chon 已提交
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
  auto iter = memory_map.begin();  // std::map<void *, size_t>::iterator
  while (iter != memory_map.end()) {
    total += iter->second;
    iter++;
  }
  return total;
}

void fpga_free(void *ptr) {
  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);
  }

  memory_size -= size;

116
#ifdef PADDLE_MOBILE_OS_LINUX
C
Chon 已提交
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133

  munmap(ptr, size);
#else
  free(ptr);
#endif
}

void fpga_copy(void *dst, const void *src, int size) { memcpy(dst, src, size); }

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

int fpga_invalidate(void *address, size_t size) {
134 135 136
  // std::cout <<
  // "=================================================================================="
  // << std::endl;
C
Chon 已提交
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
  struct MemoryCacheArgs args;
  args.address = address;
  args.size = size;
  return do_ioctl(IOCTL_MEMCACHE_INVAL, &args);
}

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

int flush_cache(void *addr, int size) {
  struct MemoryCacheArgs args;
  args.address = addr;
  args.size = size;
  return do_ioctl(IOCTL_MEMCACHE_FLUSH, &args);
}

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

161 162 163 164 165
int fpga_reset() {
  struct FpgaResetArgs args;
  return  do_ioctl(IOCTL_FPGA_RESET, &args);
}

C
Chon 已提交
166
int ioctl_conv(const struct ConvArgs &args) {
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
#ifdef ENABLE_DEBUG
//        std::cout << "======Compute Basic Conv======";
//        std::cout << "   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;
//  std::cout << "   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;
//  std::cout << "   kernel_height:" << args.kernel.height
//       << "   kernel_width:" << args.kernel.width
//       << "   stride_h:" << args.kernel.stride_h
//       << "   stride_w:" << args.kernel.stride_w;
//  std::cout << "   out_address:" << args.output.address
//       << "   out_scale_address:" << args.output.scale_address;
//
//       float* in_scale = (float*)args.image.scale_address;
//       std::cout << "inv_scale:" << in_scale[0] << "," << in_scale[1] <<
//       std::endl;

#endif

C
Chon 已提交
194
  return do_ioctl(IOCTL_CONFIG_CONV, &args);
195 196

  // return 0;
C
Chon 已提交
197 198 199
}

int compute_fpga_conv_basic(const struct ConvArgs &args) {
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
#ifdef ENABLE_DEBUG

//        std::cout << "======Compute Basic Conv======";
//        std::cout << "   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;
//  std::cout << "   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;
//  std::cout << "   kernel_height:" << args.kernel.height
//       << "   kernel_width:" << args.kernel.width
//       << "   stride_h:" << args.kernel.stride_h
//       << "   stride_w:" << args.kernel.stride_w;
//  std::cout << "   out_address:" << args.output.address
//       << "   out_scale_address:" << args.output.scale_address;

// float *in_scale = (float *)args.image.scale_address;
//        std::cout << " scale:" << in_scale[0] << "," << in_scale[1] <<
//        std::endl;

// float *filter_scale = (float *)args.filter_scale_address;
//        std::cout << " filter scale:" << filter_scale[0] << "," <<
//        filter_scale[1] << std::endl;

#endif
C
Chon 已提交
231 232 233 234
  return do_ioctl(IOCTL_CONFIG_CONV, &args);
}

int compute_fpga_conv(const struct SplitConvArgs &args) {
235
  // return do_ioctl(IOCTL_CONFIG_CONV, &args);
C
Chon 已提交
236 237 238
  int split_num = args.split_num;
  int ret = -1;
  for (int i = 0; i < split_num; i++) {
239
    // ComputeBasicConv(args.conv_args[i]);
C
Chon 已提交
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
    ret = compute_fpga_conv_basic(args.conv_arg[i]);
  }

  if (split_num > 1) {
    std::cout << "Split num > 1 !!!!!!!!!!!!!!!!!!" << std::endl;
    exit(-1);
  }
  return ret;
}

int compute_fpga_pool(const struct PoolingArgs &args) {
  return do_ioctl(IOCTL_CONFIG_POOLING, &args);
}

int compute_fpga_ewadd(const struct EWAddArgs &args) {
  return do_ioctl(IOCTL_CONFIG_EW, &args);
}

Y
Yan Chunwei 已提交
258
int get_device_info(const struct DeviceInfo &args) {
259 260
  // DeviceInfo info;
  // struct DeviceInfo* a = &info;
Y
Yan Chunwei 已提交
261
  int ret = do_ioctl(IOCTL_DEVICE_INFO, &args);
262
  // std::cout << "a." << a->filter_cap << std::endl;
Y
Yan Chunwei 已提交
263 264 265
  return ret;
}

C
Chon 已提交
266
int perform_bypass(const struct BypassArgs &args) {
267
  int ret = -1;
C
Chon 已提交
268 269 270 271 272 273 274 275 276 277 278 279 280 281
  int size = args.image.channels * args.image.width * args.image.height;
  int max_size = 1 << 21;

  float times = 1.0 * size / max_size;
  int count = static_cast<int>(times);

  void *input_address = args.image.address;
  int type_size =
      args.input_data_type == DATA_TYPE_FP32 ? sizeof(float) : sizeof(int16_t);

  void *output_address = args.output.address;
  int out_type_size =
      args.output_data_type == DATA_TYPE_FP32 ? sizeof(float) : sizeof(int16_t);

Y
Yan Chunwei 已提交
282
  float scales[2];
C
Chon 已提交
283 284 285
  struct BypassArgs bypassArgs = args;
  bypassArgs.image.width = 1;
  bypassArgs.image.height = 1;
Y
Yan Chunwei 已提交
286
  bypassArgs.output.scale_address = scales;
C
Chon 已提交
287

288

Y
Yan Chunwei 已提交
289
  float scale = 0;
C
Chon 已提交
290 291 292 293 294 295
  for (int i = 0; i < count; ++i) {
    bypassArgs.image.channels = max_size;
    bypassArgs.image.address =
        reinterpret_cast<char *>(input_address + i * max_size * type_size);
    bypassArgs.output.address =
        reinterpret_cast<char *>(output_address + i * max_size * out_type_size);
296
    ret = do_ioctl(IOCTL_CONFIG_BYPASS, &bypassArgs);
Y
Yan Chunwei 已提交
297 298
    scale = std::max(scale, scales[0]);

C
Chon 已提交
299 300 301 302 303 304
    if (ret != 0) {
      return ret;
    }
  }

  int remainder = size - max_size * count;
305 306 307 308 309 310 311 312 313 314 315 316
  // std::cout << "remainder:" << remainder << std::endl;
  if (remainder > 0) {
    bypassArgs.image.channels = remainder;
    bypassArgs.image.address =
        reinterpret_cast<char *>(input_address + count * max_size * type_size);
    bypassArgs.output.address = reinterpret_cast<char *>(
        output_address + count * max_size * out_type_size);
    ret = do_ioctl(IOCTL_CONFIG_BYPASS, &bypassArgs);
    scale = std::max(scale, scales[0]);

  }
 
Y
Yan Chunwei 已提交
317 318 319
  args.output.scale_address[0] = scale;
  args.output.scale_address[1] = 1.0f / scale;
  return ret;
C
Chon 已提交
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 346 347 348
}

int compute_fpga_concat(const struct ConcatArgs &args) { return -1; }

int compute_fpga_scale(const struct ScaleArgs &args) {
#ifdef ENABLE_DEBUG
  std::cout << "======Compute Scale======";
  std::cout << "scale_address:" << args.scale_address << std::endl;
  std::cout << "bias_address:" << args.bias_address << std::endl;

  std::cout << "wc_alignment:" << args.wc_alignment << std::endl;
  std::cout << "channel_alignment:" << args.channel_alignment << std::endl;

  std::cout << "   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;

  std::cout << "   out_address:" << args.output.address
            << "   out_scale_address:" << args.output.scale_address;

#endif
  return do_ioctl(IOCTL_CONFIG_SCALE, &args);
}

int compute_fpga_dwconv(const struct DWconvArgs &args) {
Y
Yan Chunwei 已提交
349
#ifdef ENABLE_DEBUG
C
Chon 已提交
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
  std::cout << "======Compute Basic Conv======";
  std::cout << "   relu_enabled:" << args.relu_enabled
            << "   filter_address:" << args.filter_address;
  std::cout << "   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;
  std::cout << "   kernel_height:" << args.kernel.height
            << "   kernel_width:" << args.kernel.width
            << "   stride_h:" << args.kernel.stride_h
            << "   stride_w:" << args.kernel.stride_w;
  std::cout << "   out_address:" << args.output.address
            << "   out_scale_address:" << args.output.scale_address;

367 368 369
  // float *in_scale = (float *)args.image.scale_address;
  // std::cout << "inv_scale:" << in_scale[0] << "," << in_scale[1] <<
  // std::endl;
Y
Yan Chunwei 已提交
370
#endif
C
Chon 已提交
371 372 373
  return do_ioctl(IOCTL_CONFIG_DWCONV, &args);
}

374 375 376 377 378 379 380 381
int config_activation(const struct ActiveParamterArgs& args) {
    return do_ioctl(IOCTL_CONFIG_ACTIVATION_PARAMETER, &args);
}

// int config_power(const struct PowerArgs& args) {
//     return do_ioctl(IOCTL_CONFIG_POWER, &args);
// }

C
Chon 已提交
382 383 384 385
int config_inplace(const struct InplaceArgs &args) {
  return do_ioctl(IOCTL_CONFIG_INPLACE, &args);
}

Y
Yan Chunwei 已提交
386 387 388 389 390 391 392 393 394 395 396
int config_norm_param(const struct NormalizeParameterArgs &args) {
  return do_ioctl(IOCTL_CONFIG_NORMALIZE_PARAMETER, &args);
}

int compute_norm(const struct NormalizeArgs &args) {
  return do_ioctl(IOCTL_CONFIG_NORMALIZE, &args);
}

int compute_fpga_resize(const struct ResizeArgs &args) {
  return do_ioctl(IOCTL_CONFIG_RESIZE, &args);
}
C
Chon 已提交
397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422

int16_t fp32_2_fp16(float fp32_num) {
  unsigned long tmp = *(unsigned long *)(&fp32_num);  // NOLINT
  auto t = (int16_t)(((tmp & 0x007fffff) >> 13) | ((tmp & 0x80000000) >> 16) |
                     (((tmp & 0x7f800000) >> 13) - (112 << 10)));
  if (tmp & 0x1000) {
    t++;  // roundoff
  }
  return t;
}

float fp16_2_fp32(int16_t fp16_num) {
  if (0 == fp16_num) {
    return 0;
  }
  int frac = (fp16_num & 0x3ff);
  int exp = ((fp16_num & 0x7c00) >> 10) + 112;
  int s = fp16_num & 0x8000;
  int tmp = 0;
  float fp32_num = 0;
  tmp = s << 16 | exp << 23 | frac << 13;
  fp32_num = *(float *)&tmp;  // NOLINT
  return fp32_num;
}

}  // namespace zynqmp
Y
Yan Chunwei 已提交
423
}  // namespace paddle