fpga_common.h 8.1 KB
Newer Older
Z
zhangyang 已提交
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

Z
zhangyang 已提交
17
#include <cstddef>
Z
zhangyang 已提交
18
#include <cstdint>
Z
zhangyang 已提交
19 20
#include <memory>
#include <vector>
Z
zhangyang 已提交
21

22
#ifdef PADDLE_MOBILE_FPGA_V1
23 24 25 26 27
#define IMAGE_ALIGNMENT (16)           // Aligned to 16
#define FILTER_NUM_ALIGNMENT (32)      // Filter number aligned to 32
#define FILTER_ELEMENT_ALIGNMENT (16)  // Filter element number aligned to 16
#define BS_NUM_ALIGNMENT (8)
#define BIAS_NUM_ALIGNMENT (16)
J
jameswu2014 已提交
28
#define ROW_PARALLEL_NUM (2)
29
#endif
30 31 32 33 34
#ifdef PADDLE_MOBILE_FPGA_V2
#define IMAGE_ALIGNMENT (32)           // Aligned to 32
#define FILTER_NUM_ALIGNMENT (32)      // Filter number aligned to 32
#define FILTER_ELEMENT_ALIGNMENT (16)  // Filter element number aligned to 16
#define BS_NUM_ALIGNMENT (8)
35 36
#define BIAS_SCALE_DMA_NUM (4)
#define RESULT_ALIGNMENT (32)
J
jameswu2014 已提交
37

38 39
#define PE_COLUMN (8)
#define ROW_PARALLEL_NUM (2)
J
jameswu2014 已提交
40

41
#define BIAS_NUM_ALIGNMENT (16)
42

43
#endif
44

45 46
namespace paddle_mobile {
namespace fpga {
47

Z
zhangyang 已提交
48
enum DataType {
49
  DATA_TYPE_INT8 = 2,
Z
zhangyang 已提交
50 51 52 53 54 55 56 57 58
  DATA_TYPE_FP32 = 1,
  DATA_TYPE_FP16 = 0,
};

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

59 60 61 62 63
enum ActivationType {
  NONE = 0,
  LEAKYRELU = 1,
  SIGMOID = 2,
  TANH = 3,
qnqinan's avatar
qnqinan 已提交
64
  SOFTMAX = 4,
65 66 67
};

struct ActivationArgs {
68
  enum ActivationType activation_type = NONE;
69 70 71
  int16_t leaky_relu_negative_slope;
};

Z
zhangyang 已提交
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
struct KernelArgs {
  uint32_t width;
  uint32_t height;
  uint32_t stride_w;
  uint32_t stride_h;
};

struct ImageInputArgs {
  void* address;         // input featuremap virtual address
  float* 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;
  uint64_t timer_cnt;    // time counter for FPGA computation
93 94
  struct ActivationArgs
      activation;  // To select activation and specify (Leaky)Relu parameter.
Z
zhangyang 已提交
95
};
96

J
jameswu2014 已提交
97
// #ifdef PADDLE_MOBILE_FPGA_V1
98 99 100 101
struct ConvDriverParam {
  uint64_t filter_per_group;
  uint64_t channel_per_group;
  uint64_t image_one_pad_per_row;
102 103
  uint64_t deconv_param;

J
jameswu2014 已提交
104
  // new
105 106 107 108
  uint64_t col_padding_up;
  uint64_t col_padding_down;
  uint64_t row_padding_up;
  uint64_t row_padding_down;
109 110 111 112 113

  uint64_t image_block_amount_per_row;
  uint64_t filter_pad_width_mul_channel;
  uint64_t image_win_cnt;
  uint64_t image_win_cnt_last;
J
jameswu2014 已提交
114

115 116 117 118 119
  uint64_t filter_row;
  uint64_t filter_width;
  uint64_t filter_height;
  uint64_t skip_window;
  uint64_t stride_h;
J
jameswu2014 已提交
120

121
  uint64_t filter_amount_all;
122
  uint64_t prog_full_cnt;
123 124
  uint64_t filter_align;
  uint64_t filter_num;
J
jameswu2014 已提交
125

126 127 128 129 130
  uint64_t output_width;
  uint64_t output_amount_per_row;
  uint64_t res_row_data_align4_pad;
  uint64_t cal_res_num;
  uint64_t last_cal_res_row_num;
131
  uint64_t post_prog_full_cnt;
J
jameswu2014 已提交
132

133 134 135 136
  uint64_t deconv_skip_row;      // paralvl*deconv_group
  uint64_t deconv_res_skip_row;  // deconv_group * result_amount_per_row
  uint64_t deconv_ena;
  uint64_t deconv_dump;
J
jameswu2014 已提交
137

138 139 140
  uint64_t output_address_phy;
  uint64_t output_height;
  uint64_t result_amount_per_row_multi_para;
J
jameswu2014 已提交
141

142
  uint64_t sb_address_phy;
143
  uint64_t fpga_bias_scale_len;
144
  uint64_t filter_amount_whole;
J
jameswu2014 已提交
145

146 147
  uint64_t filter_address_phy;
  uint64_t filters_amount_whole;
J
jameswu2014 已提交
148

149 150 151
  uint64_t image_address_phy;
  uint64_t image_hight;
  uint64_t image_amount_per_row;
J
jameswu2014 已提交
152

153 154 155
  uint64_t image_amount_per_row_multi_win_first;
  uint64_t image_amount_per_row_multi_win;
  uint64_t filter_pad_hight;
J
jameswu2014 已提交
156

157 158 159
  uint64_t image_block_num;
  uint64_t image_block_len;
  uint64_t image_block_len_last;
160

161
  uint64_t cmd;
162 163 164 165 166 167 168 169 170 171 172 173
};

struct EWAddDriverParam {
  uint64_t image0_address_phy;
  uint64_t image1_address_phy;
  uint64_t datalen;
  uint64_t image_image_pixel;
  uint64_t image_amount_per_row;
  uint64_t output_address_phy;
  uint64_t coefficient;
  uint64_t cmd;
};
174 175 176 177 178 179 180

struct DeconvTxParm {
  uint32_t omit_size;
  uint32_t sub_conv_num;
  uint32_t deconv_en;
  uint32_t out_addr_offset;
};
Z
zhangyang 已提交
181 182

struct ConvArgs {
183
  bool relu_enabled;
Z
zhangyang 已提交
184 185 186 187 188 189 190 191 192
  void* sb_address;  // scale and bias
  void* filter_address;
  float* filter_scale_address;
  uint32_t filter_num;
  uint32_t group_num;

  struct KernelArgs kernel;
  struct ImageInputArgs image;  // input image;
  struct ImageOutputArgs output;
193

J
jameswu2014 已提交
194
  // #ifdef PADDLE_MOBILE_FPGA_V1
195
  struct DeconvTxParm deconv_tx_param;
196
  struct ConvDriverParam driver;
Z
zhangyang 已提交
197 198 199 200
};

struct ConcatArgs {
  uint32_t image_num;
201 202 203
#ifdef PADDLE_MOBILE_FPGA_V2
  int8_t** images_in;
#else
Z
zhangyang 已提交
204
  int16_t** images_in;
205
#endif
Z
zhangyang 已提交
206 207 208 209
  float** scales_in;
  void* image_out;
  float* scale_out;
  uint32_t* channel_num;
Z
zhangyang 已提交
210
  uint32_t* aligned_channel_num;  // Not used so far. Reserved for V2.
Z
zhangyang 已提交
211
  uint32_t out_channel;
212 213
  uint32_t height;
  uint32_t width;
214
  std::vector<std::shared_ptr<char>> vector_concat_space;
215 216
};

Z
zhangyang 已提交
217 218 219 220 221 222 223
struct SplitConvArgs {
  uint32_t split_num;
  uint32_t group_num;
  uint32_t filter_num;
  struct ImageOutputArgs output;
  struct ConvArgs* conv_arg;
  struct ConcatArgs concat_arg;
Z
zhangyang 已提交
224 225 226
  std::shared_ptr<ConvArgs> shared_conv_arg;
  std::vector<std::shared_ptr<char>> vector_concat_space;
  std::vector<std::shared_ptr<char>> vector_conv_space;
Z
zhangyang 已提交
227 228
};

229 230
struct SplitArgs {
  uint32_t image_num;
231 232 233
#ifdef PADDLE_MOBILE_FPGA_V2
  int8_t* image_in;
#else
234
  int16_t* image_in;
235
#endif
236 237 238 239
  float* scale_in;
  void** images_out;
  float** scales_out;
  uint32_t* out_channel_nums;
Z
zhangyang 已提交
240 241
  uint32_t height;
  uint32_t width;
242
  std::vector<std::shared_ptr<char>> vector_split_space;
Z
zhangyang 已提交
243 244 245 246 247 248 249 250 251 252 253
};

struct PoolingArgs {
  int16_t mode;  // mode: 0:max, 1:avg
  int16_t kernel_reciprocal;
  struct KernelArgs kernel;
  struct ImageInputArgs image;  // input image;
  struct ImageOutputArgs output;
};

struct EWAddArgs {
254
  bool relu_enabled;
Z
zhangyang 已提交
255 256 257 258 259
  uint32_t const0;  // output0 = const0 x input0 + const1 x input1;
  uint32_t const1;
  struct ImageInputArgs image0;
  struct ImageInputArgs image1;
  struct ImageOutputArgs output;
J
jameswu2014 已提交
260
  // #ifdef PADDLE_MOBILE_FPGA_V1
261
  struct EWAddDriverParam driver;
Z
zhangyang 已提交
262 263 264 265 266 267 268 269 270 271 272 273
};

struct BypassArgs {
  enum DataType input_data_type;
  enum DataType output_data_type;
  enum LayoutType input_layout_type;
  enum LayoutType output_layout_type;
  struct ImageInputArgs image;
  struct ImageOutputArgs output;
};

struct DeconvArgs {
Z
zhangyang 已提交
274 275 276 277 278 279 280
  uint32_t sub_conv_num;
  uint32_t group_num;
  uint32_t filter_num;
  uint32_t omit_size;
  uint32_t sub_output_width;
  uint32_t sub_output_height;
  struct ImageOutputArgs output;
Z
zhangyang 已提交
281
  std::vector<std::shared_ptr<SplitConvArgs>> split_conv_args;
Z
zhangyang 已提交
282
};
283
struct DWconvArgs {
qnqinan's avatar
qnqinan 已提交
284
  uint32_t sub_conv_num;
285
  bool relu_enabled;
286 287 288 289 290
  void* bias_address;
  void* filter_address;
  struct KernelArgs kernel;
  struct ImageInputArgs image;
  struct ImageOutputArgs output;
291
  std::vector<std::shared_ptr<char>> vector_dwconv_space;
292
};
qnqinan's avatar
qnqinan 已提交
293 294 295 296 297 298 299 300 301 302 303 304 305

struct DWDeconvArgs {
  uint32_t sub_conv_num;
  uint32_t group_num;
  uint32_t filter_num;
  uint32_t omit_size;
  uint32_t sub_output_width;
  uint32_t sub_output_height;
  struct ImageOutputArgs output;
  std::vector<std::shared_ptr<DWconvArgs>> dw_conv_args;
  std::vector<std::shared_ptr<char>> vector_dw_conv_space;
};

J
jameswu2014 已提交
306 307
// static inline int align_to_x(int num, int x) { return (num + x - 1) / x * x;
// }
308 309 310
static inline uint32_t align_to_x(int64_t num, int64_t x) {
  return ((uint32_t)(num + x) - 1) / (uint32_t)x * (uint32_t)x;
}
Z
zhangyang 已提交
311

Z
zhangyang 已提交
312 313 314
int16_t fp32_2_fp16(float fp32_num);
float fp16_2_fp32(int16_t fp16_num);

Z
zhangyang 已提交
315 316 317 318 319 320 321 322
int open_device();
int close_device();
void* fpga_malloc(size_t size);
void fpga_free(void* ptr);
void fpga_copy(void* dest, const void* src, size_t num);
int fpga_flush(void* address, size_t size);
int fpga_invalidate(void* address, size_t size);

323 324 325
uint64_t vaddr_to_paddr(void* address);
void expand_conv_arg(ConvArgs* arg);
void expand_EW_arg(EWAddArgs* arg);
326
inline int32_t convertmantissa(int32_t i);
327 328 329

uint32_t paddle_mobile_version();

Z
zhangyang 已提交
330 331
}  // namespace fpga
}  // namespace paddle_mobile