zynqmp_api.h 9.6 KB
Newer Older
C
Chon 已提交
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

T
TianXiaogang 已提交
17 18 19
#ifndef PADDLE_LITE_SRC_FPGA_KD_ZYNQMP_API_H
#define PADDLE_LITE_SRC_FPGA_KD_ZYNQMP_API_H

C
Chon 已提交
20 21 22 23 24
#include <stdint.h>
#include <cstddef>
#include <iostream>
#include <limits>

Y
Yan Chunwei 已提交
25
namespace paddle {
C
Chon 已提交
26 27 28 29 30 31
namespace zynqmp {

typedef int16_t half;

#define IMAGE_ALIGNMENT 16           // Aligned to 16
#define FILTER_ELEMENT_ALIGNMENT 16  // Filter element number aligned to 16
32 33 34
// #define FILTER_NUM_ALIGNMENT 32      // Filter number aligned to 32  replace
// by filter.hpp "get_filter_num_alignment()"
// #define FILTER_ELEMENT_ALIGNMENT 64  // Filter element number aligned to 64
C
Chon 已提交
35 36 37 38 39 40 41 42 43 44 45 46 47
#define BS_NUM_ALIGNMENT 8
#define BIAS_NUM_ALIGNMENT 16

enum DDataType {
  DATA_TYPE_FP32 = 1,
  DATA_TYPE_FP16 = 0,
};

enum DLayoutType {
  LAYOUT_CHW = 1,
  LAYOUT_HWC = 0,
};

T
TianXiaogang 已提交
48
enum ActiveType {
49 50 51 52 53
  TYPE_NONE = 0,
  TYPE_RELU = 1,
  TYPE_RELU6 = 2,
  TYPE_LEAKY_RELU = 3,
  TYPE_SIGMOID = 4,
C
Chon 已提交
54 55
};

56
struct DeviceInfoArgs {
Y
Yan Chunwei 已提交
57
  uint32_t filter_cap;
58 59
  uint32_t version;
  uint16_t device_type;
60
  uint32_t column;
61 62 63 64 65 66
  uint32_t reserved1;
  uint32_t reserved2;
  uint32_t reserved3;
  uint32_t reserved4;
  uint32_t reserved5;
  uint32_t reserved6;
Y
Yan Chunwei 已提交
67 68
};

69 70 71 72 73
struct VersionArgs {
  void* buffer;
  size_t size;
};

C
Chon 已提交
74 75 76 77 78 79 80 81 82 83 84
struct MemoryCopyArgs {
  void* src;
  void* dest;
  size_t size;
};

struct MemoryCacheArgs {
  void* address;
  size_t size;
};

85 86 87
struct MemoryBarrierArgs {
  uint16_t dummy;
};
C
Chon 已提交
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

struct BNArgs {
  bool enabled;
  void* bias_address;
  void* scale_address;
};

/**
Conv and Pooling kernel
*/
struct KernelArgs {
  uint32_t width;
  uint32_t height;
  uint32_t stride_w;
  uint32_t stride_h;
};

struct ImageInputArgs {
  void* address;        // input featuremap virtual address
  void* scale_address;  // input scale address;
  uint32_t channels;
  uint32_t width;  // featuremap width
  uint32_t height;
  uint32_t pad_width;  // padding width;
  uint32_t pad_height;
};

struct ImageOutputArgs {
  void* address;         // output result address;
  float* scale_address;  // output scale address;
};

120 121 122 123 124 125 126 127
struct DeconvArgs {
  bool enabled;
  uint16_t sub_kernel_num;   // which is the stride of deconv, means that deconv
                             // will be divided into several sub conv operation
  uint16_t invalid_col_num;  // which will be dumped in the left and right for
                             // each row directly in FPGA
};

C
Chon 已提交
128 129 130 131 132 133 134
struct ConvArgs {
  bool relu_enabled;
  void* sb_address;  // scale and bias are interlaced;
  void* filter_address;
  void* filter_scale_address;
  uint32_t filter_num;
  uint32_t group_num;
T
TianXiaogang 已提交
135
  uint32_t dilation;
C
Chon 已提交
136

137
  struct DeconvArgs deconv;
C
Chon 已提交
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
  struct KernelArgs kernel;
  struct ImageInputArgs image;  // input image;
  struct ImageOutputArgs output;
};

struct DWconvArgs {
  bool relu_enabled;
  void* bias_address;
  void* filter_address;
  struct KernelArgs kernel;
  struct ImageInputArgs image;
  struct ImageOutputArgs output;
  uint16_t out_width;
  uint16_t out_height;
  uint16_t sub_conv_num;
};

struct PoolingArgs {
  uint16_t mode;
  uint16_t kernel_reciprocal;
  struct KernelArgs kernel;
  struct ImageInputArgs image;  // input image;
  struct ImageOutputArgs output;
  uint16_t out_width;
  uint16_t out_height;
};

// elementwise add arguments
struct EWAddArgs {
  bool relu_enabled;

  uint32_t const0;  // output0 = const0 x input0 + const1 x input1;
  uint32_t const1;
  struct ImageInputArgs image0;
  struct ImageInputArgs image1;
  struct ImageOutputArgs output;
};

struct BypassArgs {
  enum DDataType input_data_type;
  enum DDataType output_data_type;
  enum DLayoutType input_layout_type;
  enum DLayoutType output_layout_type;
  struct ImageInputArgs image;
  struct ImageOutputArgs output;
};

struct ScaleArgs {
  void* scale_address;
  void* bias_address;
  uint32_t wc_alignment;
  uint32_t channel_alignment;

  struct ImageInputArgs image;
  struct ImageOutputArgs output;
};

struct NormalizeArgs {
  void* input_image_address;
  void* output_image_address;
  uint32_t image_width;
  uint32_t image_height;
  uint32_t image_channel;
  uint32_t* output_scale_address;
};

204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
struct PreprocessArgs {
  void* input_image_address;
  void* output_image_address;
  uint32_t input_width;
  uint32_t input_height;
  uint32_t output_width;
  uint32_t output_height;
  uint32_t height_ratio;
  uint32_t width_ratio;
  uint16_t mean0;
  uint16_t mean1;
  uint16_t mean2;
  uint16_t scale0;
  uint16_t scale1;
  uint16_t scale2;
  uint32_t rd_ring_buf_size;
  uint32_t wr_ring_buf_size;
  uint32_t vedio_in_fomat;
  uint32_t vedio_out_fomat;
  uint32_t vedio_source;
  bool mean_scale_enabled;
};

Y
Yan Chunwei 已提交
227 228 229 230 231 232 233 234 235 236 237 238 239
struct ResizeArgs {
  void* input_image_address;
  void* output_image_address;
  uint32_t input_width;
  uint32_t input_height;
  uint32_t image_channel;
  uint32_t output_width;
  uint32_t output_height;
  uint32_t height_ratio;
  uint32_t width_ratio;
  uint32_t* output_scale_address;
};

C
Chon 已提交
240 241 242 243 244 245 246 247 248 249 250
struct PowerParameterArgs {
  uint16_t shift;
  uint16_t scale;
  uint16_t power;
};

struct NormalizeParameterArgs {
  uint32_t channel;
  uint32_t hight_width;
};

T
TianXiaogang 已提交
251
struct ActiveParamterArgs {
252
  enum ActiveType type;
T
TianXiaogang 已提交
253 254 255
  uint16_t leaky_relu_factor;
};

256 257 258 259
struct GlobalPoolArgs {
  uint16_t global_pool_factor;
};

C
Chon 已提交
260
struct InplaceArgs {
261
  bool leaky_relu_enable;
C
Chon 已提交
262
  bool relu_enable;
T
TianXiaogang 已提交
263 264
  bool sigmoid_enable;
  bool relu6_enable;
C
Chon 已提交
265 266
  bool power_enable;
  bool normalize_enable;
267
  bool global_pool_en;
C
Chon 已提交
268 269 270 271 272 273 274 275 276 277 278 279
};

struct FpgaRegWriteArgs {
  uint64_t address;  //
  uint64_t value;
};

struct FpgaRegReadArgs {
  uint64_t address;
  uint64_t value;
};

T
TianXiaogang 已提交
280
struct FpgaResetArgs {
281
  uint32_t dummy;
T
TianXiaogang 已提交
282
};
C
Chon 已提交
283 284

#define IOCTL_FPGA_MAGIC (('F' + 'P' + 'G' + 'A') / 4)
285
// #define IOCTL_MEMORY_MAGIC                  (('M' + 'E' + 'M' + 'Y') / 4)
C
Chon 已提交
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304

#define IOCTL_VERSION _IOW(IOCTL_FPGA_MAGIC, 01, struct VersionArgs)

#define IOCTL_SEPARATOR_0 10

#define IOCTL_MEM_COPY _IOW(IOCTL_FPGA_MAGIC, 11, struct MemoryCopyArgs)
#define IOCTL_MEMCACHE_INVAL _IOW(IOCTL_FPGA_MAGIC, 12, struct MemoryCacheArgs)
#define IOCTL_MEMCACHE_FLUSH _IOW(IOCTL_FPGA_MAGIC, 13, struct MemoryCacheArgs)
#define IOCTL_MEMORY_BARRIER \
  _IOW(IOCTL_FPGA_MAGIC, 14, struct MemoryBarrierArgs)

#define IOCTL_SEPARATOR_1 20

#define IOCTL_CONFIG_CONV _IOW(IOCTL_FPGA_MAGIC, 21, struct ConvArgs)
#define IOCTL_CONFIG_POOLING _IOW(IOCTL_FPGA_MAGIC, 22, struct PoolingArgs)
#define IOCTL_CONFIG_EW _IOW(IOCTL_FPGA_MAGIC, 23, struct EWAddArgs)
#define IOCTL_CONFIG_BYPASS _IOW(IOCTL_FPGA_MAGIC, 24, struct BypassArgs)
#define IOCTL_CONFIG_SCALE _IOW(IOCTL_FPGA_MAGIC, 25, struct ScaleArgs)
#define IOCTL_CONFIG_NORMALIZE _IOW(IOCTL_FPGA_MAGIC, 26, struct NormalizeArgs)
Y
Yan Chunwei 已提交
305
#define IOCTL_CONFIG_RESIZE _IOW(IOCTL_FPGA_MAGIC, 30, struct ResizeArgs)
C
Chon 已提交
306 307 308 309 310 311 312
#define IOCTL_CONFIG_DWCONV _IOW(IOCTL_FPGA_MAGIC, 31, struct DWconvArgs)

#define IOCTL_CONFIG_INPLACE _IOW(IOCTL_FPGA_MAGIC, 40, struct InplaceArgs)
#define IOCTL_CONFIG_POWER_PARAMETER \
  _IOW(IOCTL_FPGA_MAGIC, 41, struct PowerParameterArgs)
#define IOCTL_CONFIG_NORMALIZE_PARAMETER \
  _IOW(IOCTL_FPGA_MAGIC, 42, struct NormalizeParameterArgs)
T
TianXiaogang 已提交
313 314
#define IOCTL_CONFIG_ACTIVATION_PARAMETER \
  _IOW(IOCTL_FPGA_MAGIC, 43, struct ActiveParamterArgs)
315 316 317
#define IOCTL_CONFIG_GLOBAL_POOL_PARAMETER \
  _IOW(IOCTL_FPGA_MAGIC, 44, struct GlobalPoolArgs)

C
Chon 已提交
318 319 320 321
#define IOCTL_FPGA_REG_READ _IOW(IOCTL_FPGA_MAGIC, 50, struct FpgaRegReadArgs)
#define IOCTL_FPGA_REG_WRITE _IOW(IOCTL_FPGA_MAGIC, 51, struct FpgaRegWriteArgs)
#define IOCTL_FPGA_RESET _IOW(IOCTL_FPGA_MAGIC, 52, struct FpgaResetArgs)

322
#define IOCTL_DEVICE_INFO _IOW(IOCTL_FPGA_MAGIC, 100, struct DeviceInfoArgs)
C
Chon 已提交
323

324 325
#define IOCTL_SEPARATOR_2 200
#define IOCTL_PREPROCESS _IOW(IOCTL_FPGA_MAGIC, 201, struct PreprocessArgs)
C
Chon 已提交
326

327
//============================== API =============================
C
Chon 已提交
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344

inline int align_to_x(int num, int x) { return (num + x - 1) / x * x; }
int open_device();
void close_device();
void reset_device();

void* fpga_malloc(size_t size);
void fpga_free(void* ptr);
size_t fpga_get_memory_size(void* ptr);
size_t fpga_get_memory_size_max();
size_t fpga_diagnose_memory(int detailed);

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

int fpga_flush(void* address, size_t size);
int fpga_invalidate(void* address, size_t size);

345
int get_device_info(const struct DeviceInfoArgs& args);
Y
Yan Chunwei 已提交
346

C
Chon 已提交
347 348 349 350 351 352
int perform_bypass(const struct BypassArgs& args);
int compute_fpga_conv_basic(const struct ConvArgs& args);
int compute_fpga_pool(const struct PoolingArgs& args);
int compute_fpga_ewadd(const struct EWAddArgs& args);
int compute_fpga_scale(const struct ScaleArgs& args);
int compute_fpga_concat(const struct ConcatArgs& args);
Y
Yan Chunwei 已提交
353 354
int compute_fpga_resize(const struct ResizeArgs& args);

T
TianXiaogang 已提交
355
int config_activation(const struct ActiveParamterArgs& args);
356
int config_global_pool(const struct GlobalPoolArgs& args);
C
Chon 已提交
357 358
int config_power(const struct PowerArgs& args);
int compute_fpga_dwconv(const struct DWconvArgs& args);
Y
Yan Chunwei 已提交
359 360
int config_norm_param(const struct NormalizeParameterArgs& args);
int compute_norm(const struct NormalizeArgs& args);
C
Chon 已提交
361 362 363 364 365 366

int config_inplace(const struct InplaceArgs& args);

int flush_cache(void* addr, int size);
int invalidate_cache(void* addr, int size);

T
TianXiaogang 已提交
367
int fpga_reset();
368
int compute_preprocess(const struct PreprocessArgs& args);
T
TianXiaogang 已提交
369

C
Chon 已提交
370 371 372
int16_t fp32_2_fp16(float fp32_num);
float fp16_2_fp32(int16_t fp16_num);
}  // namespace zynqmp
Y
Yan Chunwei 已提交
373
}  // namespace paddle
T
TianXiaogang 已提交
374 375

#endif  // PADDLE_LITE_SRC_FPGA_KD_ZYNQMP_API_H