fpga_common.h 5.3 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 19 20 21 22
#include <cstdint>

namespace paddle_mobile {
namespace fpga {

23 24 25 26 27 28 29
#ifdef PADDLE_MOBILE_FPGA_V1
#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
#endif

Z
zhangyang 已提交
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
enum DataType {
  DATA_TYPE_FP32 = 1,
  DATA_TYPE_FP16 = 0,
};

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

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
};
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
#ifdef PADDLE_MOBILE_FPGA_V1
struct ConvDriverParam {
  uint64_t image_address_phy;
  uint64_t filter_address_phy;
  uint64_t sb_address_phy;
  uint64_t output_address_phy;

  uint64_t output_height;
  uint64_t output_width;
  uint64_t filter_per_group;
  uint64_t channel_per_group;

  uint64_t image_amount_per_row;
  uint64_t image_one_pad_per_row;
  uint64_t filter_amount_all;
  uint64_t output_amount_per_row;

  uint64_t image_block_amount_per_row;
  uint64_t filter_pad_width_mul_channel;
  uint64_t image_amount_per_row_multi_win_first;
  uint64_t image_amount_per_row_multi_win;
  uint64_t image_block_num;
  uint64_t image_block_len;
  uint64_t image_block_len_last;
  uint64_t image_win_cnt;
  uint64_t image_win_cnt_last;
  uint64_t res_row_data_align4_pad;
  uint64_t prog_full_cnt;
  uint64_t post_prog_full_cnt;
  uint64_t fpga_bias_scale_len;
  uint64_t cmd;
};

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;
};
#endif
Z
zhangyang 已提交
106 107 108 109 110 111 112 113 114 115 116 117

struct ConvArgs {
  bool relu_enabled;
  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;
118 119 120 121 122 123 124 125

#ifdef PADDLE_MOBILE_FPGA_V2
  void* free_space;  // used by FPGA logic
#endif

#ifdef PADDLE_MOBILE_FPGA_V1
  struct ConvDriverParam driver;
#endif
Z
zhangyang 已提交
126 127 128 129 130 131 132 133 134
};

struct ConcatArgs {
  uint32_t image_num;
  int16_t** images_in;
  float** scales_in;
  void* image_out;
  float* scale_out;
  uint32_t* channel_num;
Z
zhangyang 已提交
135 136
  uint32_t* aligned_channel_num;
  uint32_t out_channel;
137 138 139 140
  uint32_t height;
  uint32_t width;
};

Z
zhangyang 已提交
141 142 143 144 145 146 147 148 149
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;
};

150 151 152 153 154 155 156
struct SplitArgs {
  uint32_t image_num;
  int16_t* image_in;
  float* scale_in;
  void** images_out;
  float** scales_out;
  uint32_t* out_channel_nums;
Z
zhangyang 已提交
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
  uint32_t height;
  uint32_t width;
};

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 {
  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;
176 177 178
#ifdef PADDLE_MOBILE_FPGA_V1
  struct EWAddDriverParam driver;
#endif
Z
zhangyang 已提交
179 180 181 182 183 184 185 186 187 188 189 190
};

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 已提交
191 192 193 194 195 196 197 198
  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;
  struct ConvArgs* conv_args;
Z
zhangyang 已提交
199
};
Z
zhangyang 已提交
200

Z
zhangyang 已提交
201
static inline int align_to_x(int num, int x) { return (num + x - 1) / x * x; }
Z
zhangyang 已提交
202

Z
zhangyang 已提交
203 204 205
int16_t fp32_2_fp16(float fp32_num);
float fp16_2_fp32(int16_t fp16_num);

Z
zhangyang 已提交
206 207 208 209 210 211 212 213
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);

214 215 216 217
uint64_t vaddr_to_paddr(void* address);
void expand_conv_arg(ConvArgs* arg);
void expand_EW_arg(EWAddArgs* arg);

Z
zhangyang 已提交
218 219
}  // namespace fpga
}  // namespace paddle_mobile