api.h 5.9 KB
Newer Older
H
hanbuhe 已提交
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 <stdint.h>
H
hanbuhe 已提交
18 19 20
#include <cstddef>
#include <iostream>
#include <limits>
Z
zhangyang 已提交
21
#include "framework/tensor.h"
H
hanbuhe 已提交
22

Z
zhangyang 已提交
23
namespace paddle_mobile {
H
hanbuhe 已提交
24 25
namespace fpga {

H
hanbuhe 已提交
26 27 28
enum DataType {
  DATA_TYPE_FP32 = 1,
  DATA_TYPE_FP16 = 0,
H
hanbuhe 已提交
29 30
};

H
hanbuhe 已提交
31 32 33
enum LayoutType {
  LAYOUT_CHW = 1,
  LAYOUT_HWC = 0,
H
hanbuhe 已提交
34 35
};

H
hanbuhe 已提交
36 37
struct VersionArgs {
  void* buffer;
Z
zhangyang 已提交
38 39 40
};

struct MemoryCopyArgs {
Z
zhangyang 已提交
41
  void* src;
H
hanbuhe 已提交
42
  void* dest;
Z
zhangyang 已提交
43 44 45
  size_t size;
};

H
hanbuhe 已提交
46
struct KernelArgs {
Z
zhangyang 已提交
47 48 49
  uint32_t width;
  uint32_t height;
  uint32_t stride_w;
H
hanbuhe 已提交
50
  uint32_t stride_h;
Z
zhangyang 已提交
51 52
};

H
hanbuhe 已提交
53
struct ImageInputArgs {
H
hanbuhe 已提交
54 55
  void* address;         // input featuremap virtual address
  float* scale_address;  // input scale address;
Z
zhangyang 已提交
56
  uint32_t channels;
H
hanbuhe 已提交
57 58 59 60 61 62 63 64 65
  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;
Z
zhangyang 已提交
66
};
Z
zhangyang 已提交
67

H
hanbuhe 已提交
68
struct ConvArgs {
Z
zhangyang 已提交
69
  bool relu_enabled;
H
hanbuhe 已提交
70
  void* sb_address;  // scale and bias are interlaced;
H
hanbuhe 已提交
71
  void* filter_address;
72
  float* filter_scale_address;
Z
zhangyang 已提交
73 74 75
  uint32_t filter_num;
  uint32_t group_num;

H
hanbuhe 已提交
76
  struct KernelArgs kernel;
H
hanbuhe 已提交
77
  struct ImageInputArgs image;  // input image;
H
hanbuhe 已提交
78
  struct ImageOutputArgs output;
Z
zhangyang 已提交
79 80
};

Z
zhangyang 已提交
81 82 83 84 85 86 87 88 89 90 91
struct ConcatArgs {
  uint32_t image_num;
  half** images_in;
  float** scales_in;
  void* image_out;
  float* scale_out;
  uint32_t* channel_num;
  uint32_t height;
  uint32_t width;
};

Z
zhangyang 已提交
92
struct SplitConvArgs {
Z
zhangyang 已提交
93 94 95 96
  uint32_t split_num;
  uint32_t group_num;
  uint32_t filter_num;
  struct ImageOutputArgs output;
Z
zhangyang 已提交
97 98
  struct ConvArgs* conv_args;
  struct ConcatArgs concat_arg;
Z
zhangyang 已提交
99 100
};

Z
zhangyang 已提交
101 102 103 104 105 106 107 108
struct GroupConvArgs {
  uint32_t group_num;
  uint32_t filter_num;
  struct ImageOutputArgs output;
  struct SplitConvArgs* conv_args;
  struct ConcatArgs concat_arg;
};

H
hanbuhe 已提交
109
struct PoolingArgs {
Z
zhangyang 已提交
110 111
  int16_t mode;  // mode: 0:max, 1:avg
  half kernel_reciprocal;
H
hanbuhe 已提交
112
  struct KernelArgs kernel;
H
hanbuhe 已提交
113 114
  struct ImageInputArgs image;  // input image;
  struct ImageOutputArgs output;
Z
zhangyang 已提交
115 116
};

H
hanbuhe 已提交
117
struct EWAddArgs {
Z
zhangyang 已提交
118
  bool relu_enabled;
H
hanbuhe 已提交
119

Z
zhangyang 已提交
120 121
  uint32_t const0;  // output0 = const0 x input0 + const1 x input1;
  uint32_t const1;
H
hanbuhe 已提交
122 123 124
  struct ImageInputArgs image0;
  struct ImageInputArgs image1;
  struct ImageOutputArgs output;
H
hanbuhe 已提交
125 126
};

H
hanbuhe 已提交
127
struct BypassArgs {
H
hanbuhe 已提交
128 129 130 131
  enum DataType input_data_type;
  enum DataType output_data_type;
  enum LayoutType input_layout_type;
  enum LayoutType output_layout_type;
H
hanbuhe 已提交
132 133 134 135
  struct ImageInputArgs image;
  struct ImageOutputArgs output;
};

H
hanbuhe 已提交
136 137 138 139 140
struct FpgaRegWriteArgs {
  uint64_t address;  //
  uint64_t value;
};

H
hanbuhe 已提交
141 142 143 144 145
struct FpgaRegReadArgs {
  uint64_t address;
  uint64_t value;
};

146 147 148 149 150
struct MemoryCacheArgs {
  void* address;
  size_t size;
};

H
hanbuhe 已提交
151 152 153 154 155 156 157
#define IOCTL_FPGA_MAGIC 'FPGA'

#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)
158 159
#define IOCTL_MEMCACHE_INVAL _IOW(IOCTL_FPGA_MAGIC, 12, struct MemoryCacheArgs)
#define IOCTL_MEMCACHE_FLUSH _IOW(IOCTL_FPGA_MAGIC, 13, struct MemoryCacheArgs)
H
hanbuhe 已提交
160 161 162 163 164 165

#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)
H
hanbuhe 已提交
166
#define IOCTL_CONFIG_BYPASS _IOW(IOCTL_FPGA_MAGIC, 24, struct BypassArgs)
H
hanbuhe 已提交
167 168
#define IOCTL_FPGA_REG_READ _IOW(IOCTL_FPGA_MAGIC, 28, struct FpgaRegReadArgs)
#define IOCTL_FPGA_REG_WRITE _IOW(IOCTL_FPGA_MAGIC, 29, struct FpgaRegWriteArgs)
H
hanbuhe 已提交
169 170 171

//============================== API =============================

H
hanbuhe 已提交
172 173 174 175 176 177
int open_device();
int close_device();

void* fpga_malloc(size_t size);
void fpga_free(void* ptr);
void fpga_copy(void* dst, const void* src, size_t num);
178 179
int fpga_flush(void* address, size_t size);
int fpga_invalidate(void* address, size_t size);
H
hanbuhe 已提交
180

H
hanbuhe 已提交
181
int PerformBypass(const struct BypassArgs& args);
Z
zhangyang 已提交
182
int ComputeFpgaConv(const struct SplitConvArgs& args);
H
hanbuhe 已提交
183 184
int ComputeFpgaPool(const struct PoolingArgs& args);
int ComputeFpgaEWAdd(const struct EWAddArgs& args);
Z
zhangyang 已提交
185
int ComputeFPGAConcat(const struct ConcatArgs& args);
H
hanbuhe 已提交
186

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

Z
zhangyang 已提交
189
int get_align_image_cw(int cw);
Z
zhangyang 已提交
190
void format_image(framework::Tensor* image_tensor);
Z
zhangyang 已提交
191 192
void format_fp16_ofm(framework::Tensor* ofm_tensor);  // only allocate memory
void format_fp32_ofm(framework::Tensor* ofm_tensor);
193

Z
zhangyang 已提交
194
float filter_find_max(framework::Tensor* filter_tensor);
195
int get_filter_num_per_div(framework::Tensor* filter_tensor, int group_num);
Z
zhangyang 已提交
196 197 198
int get_plit_num(framework::Tensor* filter_tensor);
int get_aligned_filter_element_num(int chw);
int get_aligned_filter_num(int num);
Z
zhangyang 已提交
199 200
void format_filter(framework::Tensor* filter_tensor, float max_value,
                   int group_num);
Z
zhangyang 已提交
201
void format_fc_filter(framework::Tensor* filter_tensor, float max_value);
Z
zhangyang 已提交
202 203
void format_bias_scale_array(float** bias_scale_array,
                             int element_num_per_division, int num);
Z
zhangyang 已提交
204 205
void format_concat_output(framework::Tensor* out, int height, int width,
                          int image_num, uint32_t* channel_num);
Z
zhangyang 已提交
206

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

Z
zhangyang 已提交
212 213 214
half fp32_2_fp16(float fp32_num);
float fp16_2_fp32(half fp16_num);

H
hanbuhe 已提交
215
}  // namespace fpga
Z
zhangyang 已提交
216
}  // namespace paddle_mobile