-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfunction.h
More file actions
255 lines (220 loc) · 6.68 KB
/
Copy pathfunction.h
File metadata and controls
255 lines (220 loc) · 6.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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
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
106
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
#pragma once
#include <ap_int.h>
#include <hls_stream.h>
// using namespace hls;
// #include <iostream>
using namespace std;
#include "stream_tools.h"
#include <assert.h>
/**
* padding 函数
* 数据宽度为 IN_BIT * SIMD
*
*/
template <unsigned IN_BIT, unsigned SIMD, unsigned P>
void padding_var(
// 将每一数竖看成一个元素
stream<ap_uint<IN_BIT * SIMD> > &in, stream<ap_uint<IN_BIT * SIMD> > &out,
const unsigned in_row, //
const unsigned in_col, //
const unsigned in_simd_pre_ch, // ch / simd
const unsigned reps = 1) {
// const unsigned OUT_ROW = in_row + 2 * P;
const unsigned OUT_COL = in_col + 2 * P;
// const unsigned DATA_NUM_PRE_CH = in_ch / SIMD;
for (unsigned rep = 0; rep < reps; rep++) {
for (unsigned h = 0; h < P; h++) {
for (unsigned s = 0; s < OUT_COL; s++) {
// 将一 ch 的数据置零
append_zero<IN_BIT * SIMD>(out, in_simd_pre_ch);
}
}
for (unsigned h = 0; h < in_row; h++) {
for (unsigned s = 0; s < OUT_COL; s++) {
// #pragma HLS PIPELINE II=1
if ((s < P) || (s >= OUT_COL - P)) {
// temp_out = 0;
append_zero<IN_BIT * SIMD>(out, in_simd_pre_ch);
} else {
// cout << "in size :" << in.size() << endl;
stream_move<IN_BIT * SIMD>(in, out, in_simd_pre_ch);
}
// out.write(temp_out);
}
}
for (unsigned h = 0; h < P; h++) {
for (unsigned i = 0; i < OUT_COL; i++) {
append_zero<IN_BIT * SIMD>(out, in_simd_pre_ch);
}
}
}
}
/**
* padding 函数
*/
template <unsigned IN_ROW, unsigned IN_COL, unsigned IN_CH, unsigned IN_BIT,
unsigned P>
void padding(
// 将每一数竖看成一个元素
stream<ap_uint<IN_CH * IN_BIT> > &in, stream<ap_uint<IN_CH * IN_BIT> > &out,
const unsigned reps = 1) {
const unsigned OUT_ROW = IN_ROW + 2 * P;
const unsigned OUT_COL = IN_COL + 2 * P;
ap_uint<IN_CH *IN_BIT> temp_out = 0;
for (unsigned rep = 0; rep < reps; rep++) {
for (unsigned h = 0; h < P; h++) {
for (unsigned s = 0; s < OUT_COL; s++) {
out.write(0);
}
}
for (unsigned h = 0; h < IN_ROW; h++) {
for (unsigned s = 0; s < OUT_COL; s++) {
#pragma HLS PIPELINE II = 1
if ((s < P) || (s >= OUT_COL - P)) {
temp_out = 0;
} else {
temp_out = in.read();
}
out.write(temp_out);
}
}
for (unsigned h = 0; h < P; h++) {
for (unsigned i = 0; i < OUT_COL; i++) {
out.write(0);
}
}
}
}
/**
* 实现量化激活算法
* 使用二分查找
* TODO
* 丢失精度 暂时弃用
*/
// int d = 0;
// template < unsigned IN_BIT,
// unsigned OUT_BIT,
// unsigned INC_BIT,
// unsigned BIAS_BIT>
// ap_uint<OUT_BIT> bn_qurelu( ap_int<IN_BIT> in,
// ap_int<INC_BIT> inc,
// ap_int<BIAS_BIT> bias ) {
// if (d < 16) {
// cout << d << " in " << in << " inc " << inc << " bias " << bias
// << endl; d ++;
// }
// ap_int<IN_BIT> target = in + bias;
// ap_uint<OUT_BIT> index = 1 << (OUT_BIT - 1);
// // 计算所使用的数据宽度 INC_BIT+OUT_BIT
// ap_int<INC_BIT+OUT_BIT> inc_exp = inc;
// // 直接对inc移位会溢出 所以初始化时需要 位宽扩展
// ap_int<INC_BIT+OUT_BIT + 1> mid = inc_exp << (OUT_BIT - 1);
// for(int i=OUT_BIT-2; i >= 0; i --) {
// #pragma HLS UNROLL
// // TODO
// // 因为不能特别确定 IN_BIT 和 inc_BIT 关系 所以这里可能有精度损失
// ap_int<INC_BIT+OUT_BIT> inc_shift = inc_exp << i;
// ap_uint<INC_BIT+OUT_BIT> one_shift = 1 << i;
// if(target < mid) {
// mid -= inc_shift;
// index -= one_shift;
// } else if(mid < target){
// mid += inc_shift;
// index += one_shift;
// }
// }
// if(target < mid) {
// index --;
// }
// return index;
// }
// int d = 0;
template <unsigned IN_BIT, unsigned OUT_BIT, unsigned INC_BIT,
unsigned BIAS_BIT,
unsigned DATA_BIT, unsigned W_BIT, unsigned L_SHIFT>
ap_uint<OUT_BIT> bn_qurelu(ap_int<IN_BIT> in, ap_int<INC_BIT> inc,
ap_int<BIAS_BIT> bias) {
const unsigned D = 1 << (W_BIT - 1 + DATA_BIT + L_SHIFT);
ap_int<IN_BIT> bn_res = in * inc + bias;
ap_uint<OUT_BIT> res;
if (bn_res > 0) {
bn_res = (bn_res + (D >> 1)) >> (W_BIT - 1 + DATA_BIT + L_SHIFT);
if (bn_res > 15) {
res = 15;
} else {
res = bn_res;
}
} else {
res = 0;
}
return res;
}
template <unsigned IN_BIT, unsigned OUT_BIT, unsigned INC_BIT,
unsigned BIAS_BIT,
unsigned DATA_BIT, unsigned W_BIT, unsigned L_SHIFT>
ap_uint<OUT_BIT> bn_qurelu_fixed(ap_int<IN_BIT> in, ap_int<INC_BIT> inc,
ap_int<BIAS_BIT> bias) {
const unsigned D = 1 << (W_BIT - 1 + DATA_BIT + L_SHIFT);
ap_int<IN_BIT + INC_BIT + 1> bn_res = in * inc + bias;
ap_uint<OUT_BIT> res;
if (bn_res > 0) {
bn_res = (bn_res + (D >> 1)) >> (W_BIT - 1 + DATA_BIT + L_SHIFT);
if (bn_res > 15) {
res = 15;
} else {
res = bn_res;
}
} else {
res = 0;
}
return res;
}
/**
* 批正则化 和 量化激活函数
*/
// template < unsigned IN_BIT,
// unsigned OUT_BIT,
// unsigned INC_BIT,
// unsigned BIAS_BIT,
// unsigned SHIFT>
// void bn_qurelu( ap_int<IN_BIT> in,
// ap_uint<INC_BIT> inc,
// ap_int<BIAS_BIT> bias )
// {
// target = target + bias;
// int index = 1 << (BIT - 1);
// int mid = inc << (BIT - 1);
// for(int i=BIT-2; i >= 0; i --) {
// int inc_shift = inc << i;
// int one_shift = 1 << i;
// if(target < mid) {
// mid -= inc_shift;
// index -= one_shift;
// } else if(mid < target){
// mid += inc_shift;
// index += one_shift;
// }
// }
// if(target < mid) {
// index --;
// }
// return index;
// }
// template<int IN_CH, int IN_ROW, int IN_COL, int IN_BIT, int OUT_BIT>
// void conv_bn_qrelu(int in[IN_CH][IN_ROW][IN_COL], int
// out[IN_CH][IN_ROW][IN_COL], int w[IN_CH], int b[IN_CH]) {
// for(int ch=0; ch < IN_CH; ch ++) {
// for(int row=0; row < IN_ROW; row ++) {
// for(int col=0; col < IN_COL; col ++) {
// out[ch][row][col] = qrelu_search<OUT_BIT>(in[ch][row][col],
// w[ch], b[ch]);
// }
// }
// }
// }
// template<int LEN, int IN_BIT, int OUT_BIT>
// void linear_bn_qrelu(int in[LEN], int out[LEN], int w[LEN], int b[LEN]) {
// for(int i=0; i < LEN; i ++) {
// out[i] = qrelu_search<OUT_BIT>(in[i], w[i], b[i]);
// }
// }