fpga_api.h 3.2 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 21 22 23
#include <cstddef>
#include <iostream>
#include <limits>

// memory management;

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

int open_device();
int close_device();

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

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

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

H
hanbuhe 已提交
44 45 46 47
struct BNArgs {
  bool enabled;
  void* bias_address;
  void* scale_address;
Z
zhangyang 已提交
48 49
};

H
hanbuhe 已提交
50 51 52 53
/**
Conv and Pooling kernel
*/
struct KernelArgs {
Z
zhangyang 已提交
54 55 56
  uint32_t width;
  uint32_t height;
  uint32_t stride_w;
H
hanbuhe 已提交
57
  uint32_t stride_h;
Z
zhangyang 已提交
58 59
};

H
hanbuhe 已提交
60 61
struct ImageInputArgs {
  void* address;  // input featuremap virtual address
Z
zhangyang 已提交
62
  uint32_t channels;
H
hanbuhe 已提交
63 64 65 66 67 68 69 70 71
  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 已提交
72
};
Z
zhangyang 已提交
73

H
hanbuhe 已提交
74
struct ConvArgs {
Z
zhangyang 已提交
75
  bool relu_enabled;
H
hanbuhe 已提交
76 77 78
  float scale;  // input scale;
  void* bias_address;
  void* filter_address;
Z
zhangyang 已提交
79 80 81
  uint32_t filter_num;
  uint32_t group_num;

H
hanbuhe 已提交
82 83
  struct BNArgs bn;
  struct ImageInputArgs image;  // input image;
H
hanbuhe 已提交
84
  struct ImageOutputArgs output;
H
hanbuhe 已提交
85
  struct KernelArgs kernel;
Z
zhangyang 已提交
86 87
};

H
hanbuhe 已提交
88 89 90 91 92
struct PoolingArgs {
  float scale;
  struct ImageInputArgs image;  // input image;
  struct ImageOutputArgs output;
  struct KernelArgs kernel;
Z
zhangyang 已提交
93 94
};

H
hanbuhe 已提交
95 96
// elementwise add arguments
struct EWAddArgs {
Z
zhangyang 已提交
97
  bool relu_enabled;
H
hanbuhe 已提交
98 99 100 101 102 103 104
  float scale;

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

H
hanbuhe 已提交
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
struct FpgaRegWriteArgs {
  uint64_t address;  //
  uint64_t value;
};

struct FpgaRegReadArgs {
  uint64_t address;
  uint64_t value;
};

#define IOCTL_FPGA_MAGIC 'FPGA'

#define IOCTL_VERSION _IOW(IOCTL_FPGA_MAGIC, 01, struct VersionArgs)
#define IOCTL_FPGA_REG_READ _IOW(IOCTL_FPGA_MAGIC, 02, struct FpgaRegReadArgs)
#define IOCTL_FPGA_REG_WRITE _IOW(IOCTL_FPGA_MAGIC, 03, struct FpgaRegWriteArgs)

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

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

Z
zhangyang 已提交
135 136 137
int ComputeFpgaConv(struct FpgaConvArgs args);
int ComputeFpgaPool(struct FpgaPoolArgs args);
int ComputeFpgaEWAdd(struct FpgaEWAddArgs args);
H
hanbuhe 已提交
138 139

}  // namespace fpga
Z
zhangyang 已提交
140
}  // namespace paddle_mobile