api.h 5.4 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 23 24

// memory management;

Z
zhangyang 已提交
25
namespace paddle_mobile {
H
hanbuhe 已提交
26 27 28 29 30
namespace fpga {

int open_device();
int close_device();

Z
zhangyang 已提交
31 32 33
void* fpga_malloc(size_t size);
void fpga_free(void* ptr);
void fpga_copy(void* dst, const void* src, size_t num);
H
hanbuhe 已提交
34

H
hanbuhe 已提交
35 36 37 38 39 40 41 42 43 44 45 46
enum DataConvertType {
  DATA_NO_CONVERT = 0,
  DATA_FP32_TO_FP16 = 1,
  DATA_FP16_TO_FP32 = 2,
};

enum LayoutConvertType {
  LAYOUT_NO_CONVERT = 0,
  LAYOUT_CHW_TO_HWC = 1,
  LAYOUT_HWC_TO_CHW = 2,
};

H
hanbuhe 已提交
47 48
struct VersionArgs {
  void* buffer;
Z
zhangyang 已提交
49 50 51
};

struct MemoryCopyArgs {
Z
zhangyang 已提交
52
  void* src;
H
hanbuhe 已提交
53
  void* dest;
Z
zhangyang 已提交
54 55 56
  size_t size;
};

H
hanbuhe 已提交
57 58 59 60
/**
Conv and Pooling kernel
*/
struct KernelArgs {
Z
zhangyang 已提交
61 62 63
  uint32_t width;
  uint32_t height;
  uint32_t stride_w;
H
hanbuhe 已提交
64
  uint32_t stride_h;
Z
zhangyang 已提交
65 66
};

H
hanbuhe 已提交
67
struct ImageInputArgs {
H
hanbuhe 已提交
68 69
  void* address;         // input featuremap virtual address
  float* scale_address;  // input scale address;
Z
zhangyang 已提交
70
  uint32_t channels;
H
hanbuhe 已提交
71 72 73 74 75 76 77 78 79
  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 已提交
80
};
Z
zhangyang 已提交
81

H
hanbuhe 已提交
82
struct ConvArgs {
Z
zhangyang 已提交
83
  bool relu_enabled;
H
hanbuhe 已提交
84
  void* sb_address;  // scale and bias are interlaced;
H
hanbuhe 已提交
85
  void* filter_address;
H
hanbuhe 已提交
86
  float* filter_scale_address;
Z
zhangyang 已提交
87 88 89
  uint32_t filter_num;
  uint32_t group_num;

H
hanbuhe 已提交
90
  struct KernelArgs kernel;
H
hanbuhe 已提交
91
  struct ImageInputArgs image;  // input image;
H
hanbuhe 已提交
92
  struct ImageOutputArgs output;
Z
zhangyang 已提交
93 94
};

Z
zhangyang 已提交
95 96 97 98 99 100 101 102 103 104 105
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 已提交
106 107 108 109 110
struct WrapperConvArgs {
  uint32_t split_num;
  uint32_t group_num;
  uint32_t filter_num;
  struct ImageOutputArgs output;
Z
zhangyang 已提交
111 112
  struct ConvArgs* conv_args;
  struct ConcatArgs concat_arg;
Z
zhangyang 已提交
113 114
};

H
hanbuhe 已提交
115
struct PoolingArgs {
H
hanbuhe 已提交
116
  struct KernelArgs kernel;
H
hanbuhe 已提交
117 118
  struct ImageInputArgs image;  // input image;
  struct ImageOutputArgs output;
Z
zhangyang 已提交
119 120
};

H
hanbuhe 已提交
121 122
// elementwise add arguments
struct EWAddArgs {
Z
zhangyang 已提交
123
  bool relu_enabled;
H
hanbuhe 已提交
124 125 126 127 128 129

  float const0;  // output0 = const0 x input0 + const1 x input1;
  float const1;
  struct ImageInputArgs image0;
  struct ImageInputArgs image1;
  struct ImageOutputArgs output;
H
hanbuhe 已提交
130 131
};

H
hanbuhe 已提交
132 133
struct BypassArgs {
  enum DataConvertType convert_type;
H
hanbuhe 已提交
134
  enum LayoutConvertType layout_type;
H
hanbuhe 已提交
135 136 137 138
  struct ImageInputArgs image;
  struct ImageOutputArgs output;
};

H
hanbuhe 已提交
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
struct FpgaRegWriteArgs {
  uint64_t address;  //
  uint64_t value;
};

#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)

#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 已提交
157
#define IOCTL_CONFIG_BYPASS _IOW(IOCTL_FPGA_MAGIC, 24, struct BypassArgs)
H
hanbuhe 已提交
158 159
#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 已提交
160

H
hanbuhe 已提交
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
enum FPGA_ERR_TYPE {
  ERR_IOCTL_CMD = -1,
  ERR_TIMEOUT = -2,
  ERR_COMPLETION_TIMEOUT = -3,
  ERR_INVALID_FPGA_ADDR = -4,
  ERR_NOMEM = -5,
  ERR_NO_RESERVE_MEM = -6,
  ERR_COPY_FROM_USER = -7,
  ERR_COPY_TO_USER = -8,
  ERR_DEL_TIMER = -9,
  ERR_ENABLE_MSI = -10,
  ERR_REGISTER_IRQ = -11,
  ERR_PCIE_REGISTER = -12,
  ERR_PCIE_PROBE = -13,
  ERR_REGISTER_BLOCK = -14,
  ERR_ALLOC_GENDISK = -15,
  ERR_INIT_QUEUE = -16,
  ERR_WAIT = -17,
  ERR_ECC_ERROR = -31,
  ERR_FPGA_FAIL_STOP = -64,
  ERR_FPGA_DEBUG_STOP = -113,
  DEV_TMP_UNAVAILABLE = -128
};

H
hanbuhe 已提交
185 186
//============================== API =============================

H
hanbuhe 已提交
187
int PerformBypass(const struct BypassArgs& args);
Z
zhangyang 已提交
188
int ComputeFpgaConv(const struct WrapperConvArgs& args);
H
hanbuhe 已提交
189 190
int ComputeFpgaPool(const struct PoolingArgs& args);
int ComputeFpgaEWAdd(const struct EWAddArgs& args);
Z
zhangyang 已提交
191
int ComputeFPGAConcat(const struct ConcatArgs& args);
H
hanbuhe 已提交
192

Z
zhangyang 已提交
193
static inline int align_to_x(int num, int x) { return (num + x - 1) / x * x; }
Z
zhangyang 已提交
194 195
void format_image(framework::Tensor* image_tensor);
void format_ofm(framework::Tensor* ofm_tensor);  // only allocate memory
Z
zhangyang 已提交
196 197
float filter_find_max(framework::Tensor* filter_tensor);
int get_element_num_per_div(framework::Tensor* filter_tensor, int group_num);
Z
zhangyang 已提交
198 199 200 201
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 已提交
202 203
void format_filter(framework::Tensor* filter_tensor, float max_value,
                   int group_num);
Z
zhangyang 已提交
204 205
void format_bias_scale_array(float** bias_scale_array,
                             int element_num_per_division, int num);
Z
zhangyang 已提交
206 207
void format_concat_output(framework::Tensor* out, int height, int width,
                          int image_num, uint32_t* channel_num);
Z
zhangyang 已提交
208

H
hanbuhe 已提交
209
}  // namespace fpga
Z
zhangyang 已提交
210
}  // namespace paddle_mobile