-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_AE_mod.c
More file actions
318 lines (267 loc) · 6.79 KB
/
test_AE_mod.c
File metadata and controls
318 lines (267 loc) · 6.79 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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
#include "rt/rt_api.h"
#include "stats.h"
//#include "pulp-nn/include/pulp_nn.h"
//#include "int8_fully_layer_parameters.h"
#include "layer1_fully.h"
#include "layer2_fully.h"
#include "layer3_fully.h"
#include "layer4_fully.h"
/*#### DATA ####*/
/*EVERY LAYER HAS ITS OWN DIMENSION*/
/*
RT_FC_SHARED_DATA int8_t out_layer1_L2[(OUT_NEURONS)];
RT_L1_DATA int8_t input_data_L1[(IFM_H_FC * IFM_H_FC * IFM_CH_FC)];
RT_L1_DATA int8_t wt_layer1_L1[(OUT_NEURONS * IFM_H_FC * IFM_H_FC * IFM_CH_FC)];
RT_L1_DATA int8_t bias_L1[OUT_NEURONS];
RT_L1_DATA int8_t out_layer1_L1[(OUT_NEURONS)];
*/
/*i files sono in L1, a cosa serve il DMA?*/
/*Only bias vectors are always in cluster L1*/
RT_L1_DATA int8_t bias_layer1_L1[OUT_NEURONS_1];
RT_L1_DATA int8_t bias_layer2_L1[OUT_NEURONS_2];
RT_L1_DATA int8_t bias_layer3_L1[OUT_NEURONS_3];
RT_L1_DATA int8_t bias_layer4_L1[OUT_NEURONS_4];
RT_L1_DATA int8_t wt_layer1_L1[OUT_NEURONS_1 * IFM_H_FC_1 * IFM_H_FC_1 * IFM_CH_FC_1];
RT_L1_DATA int8_t wt_layer2_L1[OUT_NEURONS_2 * IFM_H_FC_2 * IFM_H_FC_2 * IFM_CH_FC_2];
RT_L1_DATA int8_t wt_layer3_L1[OUT_NEURONS_3 * IFM_H_FC_3 * IFM_H_FC_3 * IFM_CH_FC_3];
RT_L1_DATA int8_t wt_layer4_L1[OUT_NEURONS_4 * IFM_H_FC_4 * IFM_H_FC_4 * IFM_CH_FC_4];
RT_L1_DATA int8_t out_layer1_L1[OUT_NEURONS_1 ];
RT_L1_DATA int8_t out_layer2_L1[OUT_NEURONS_2 ];
RT_L1_DATA int8_t out_layer3_L1[OUT_NEURONS_3 ];
RT_L1_DATA int8_t out_layer4_L1[OUT_NEURONS_4 ];
RT_FC_SHARED_DATA int8_t out_L2[OUT_NEURONS_4];
//RT_FC_SHARED_DATA int8_t *out_L2;
int test_layers(unsigned int *input_data_L1)
{
if(rt_core_id()==0) //core 0 stuff, not FC
{
/*### PROFILING SECTION ###*/
#ifdef PROFILING_TOT
INIT_PROFILING();
START_PROFILING();
rt_team_barrier();
#endif
#ifdef PRINT
printf("PULP PARALLEL 1st LAYER\n");
#endif
/* transfer layer weights and activations from L2 to L1 memory through DMA */
rt_dma_copy_t cp1;
rt_dma_copy_t cp2;
#ifdef PRINT
printf("activations transfer from L2 to L1\n");
#endif
rt_dma_memcpy(
#ifdef Q8
input_data_L2, // ext
input_data_L1, // loc
IFM_H_FC_1 * IFM_H_FC_1 *IFM_CH_FC_1, // size
#endif
RT_DMA_DIR_EXT2LOC, // dir
0, // merge
&cp1 // copy
);
rt_dma_wait(&cp1);
#ifdef PRINT
printf("weights transfer from L2 to L1\n");
#endif
rt_dma_memcpy(
#ifdef Q8
wt_layer1_L2, // ext
wt_layer1_L1, // loc
OUT_NEURONS_1 * IFM_H_FC_1 * IFM_H_FC_1 *IFM_CH_FC_1, // size
#endif
RT_DMA_DIR_EXT2LOC, // dir
0, // merge
&cp2 // copy
);
rt_dma_wait(&cp2);
#ifdef Q8
//BIAS
for (int z= 0; z < OUT_NEURONS_1; z++)
{
bias_layer1_L1[z] = 0;
}
#endif
#ifdef PRINT
printf("end of transfer. Start Fully-connected kernel parallel exec \n");
#endif
}
rt_team_barrier();
/* Execution of LAYER 1 */
/*### PROFILING SECTION ###*/
#ifdef PROFILING
/* These functions allow for a complete profiling of the kernel execution */
/* Defined in "stats.h" header. See the file for complete info */
printf("Layer 1 profiling\n");
INIT_PROFILING();
START_PROFILING();
rt_team_barrier();
#endif
/*### LAYER 1 ###*/
#ifdef Q8
pulp_nn_linear_int8(input_data_L1, wt_layer1_L1, IFM_H_FC_1 * IFM_H_FC_1* IFM_CH_FC_1,
OUT_NEURONS_1, BIAS_SHIFT_FC_1, OUT_QF_FC_1, bias_layer1_L1, out_layer1_L1);
#endif
#ifdef PROFILING
STOP_PROFILING();
#endif
/*#### LAYER 2 ####*/
/*load the second weight layer*/
if(rt_core_id()==0)
{
rt_dma_copy_t cp3;
rt_dma_memcpy(
#ifdef Q8
wt_layer2_L2, // ext
wt_layer2_L1, // loc
OUT_NEURONS_2 * IFM_H_FC_2 * IFM_H_FC_2 *IFM_CH_FC_2, // size
#endif
RT_DMA_DIR_EXT2LOC, // dir
0, // merge
&cp3 // copy
);
rt_dma_wait(&cp3);
/*BIAS LAYER 2*/
for (int z= 0; z < OUT_NEURONS_2; z++)
{
bias_layer2_L1[z] = 0;
}
}
/*EXECUTION OF LAYER 2*/
rt_team_barrier();
#ifdef PROFILING
printf("Layer 2 profiling\n");
//INIT_PROFILING();
START_PROFILING();
/*Sync*/
rt_team_barrier();
#endif
/*LAYER 2*/
#ifdef Q8
pulp_nn_linear_int8(out_layer1_L1, wt_layer2_L1, IFM_H_FC_2 * IFM_H_FC_2* IFM_CH_FC_2,
OUT_NEURONS_2, BIAS_SHIFT_FC_2, OUT_QF_FC_2, bias_layer2_L1, out_layer2_L1);
pulp_nn_relu_int8(out_layer2_L1,1,OUT_NEURONS_2);
#endif
#ifdef PROFILING
STOP_PROFILING();
#endif
/*#### LAYER 3 ####*/
/*charge the second weight layer*/
if(rt_core_id()==0)
{
rt_dma_copy_t cp4;
rt_dma_memcpy(
#ifdef Q8
wt_layer3_L2, // ext
wt_layer3_L1, // loc
OUT_NEURONS_3 * IFM_H_FC_3 * IFM_H_FC_3 *IFM_CH_FC_3, // size
#endif
RT_DMA_DIR_EXT2LOC, // dir
0, // merge
&cp4 // copy
);
rt_dma_wait(&cp4);
/*BIAS LAYER 3*/
for (int z= 0; z < OUT_NEURONS_3; z++)
{
bias_layer3_L1[z] = 0;
}
}
/*EXECUTION OF LAYER 3*/
rt_team_barrier();
#ifdef PROFILING
printf("layer 3 profiling\n");
//INIT_PROFILING();
START_PROFILING();
/*Sync*/
rt_team_barrier();
#endif
/*LAYER 3*/
#ifdef Q8
pulp_nn_linear_int8(out_layer2_L1, wt_layer3_L1, IFM_H_FC_3 * IFM_H_FC_3* IFM_CH_FC_3,
OUT_NEURONS_3, BIAS_SHIFT_FC_3, OUT_QF_FC_3, bias_layer3_L1, out_layer3_L1);
pulp_nn_relu_int8(out_layer3_L1,1,OUT_NEURONS_3);
#endif
#ifdef PROFILING
STOP_PROFILING();
#endif
/*LAYER 4*/
/*charge the second weight layer*/
if(rt_core_id()==0)
{
rt_dma_copy_t cp5;
rt_dma_memcpy(
#ifdef Q8
wt_layer4_L2, // ext
wt_layer4_L1, // loc
OUT_NEURONS_4 * IFM_H_FC_4 * IFM_H_FC_4 *IFM_CH_FC_4, // size
#endif
RT_DMA_DIR_EXT2LOC, // dir
0, // merge
&cp5 // copy
);
rt_dma_wait(&cp5);
/*BIAS LAYER 4*/
for (int z= 0; z < OUT_NEURONS_4; z++)
{
bias_layer4_L1[z] = 0;
}
}
/*EXECUTION OF LAYER 4*/
rt_team_barrier();
#ifdef PROFILING
//INIT_PROFILING();i
printf("layer 4 profiling\n");
START_PROFILING();
/*Sync*/
rt_team_barrier();
/*LAYER 4*/
#endif
#ifdef Q8
pulp_nn_linear_int8(out_layer3_L1, wt_layer4_L1, IFM_H_FC_4 * IFM_H_FC_4* IFM_CH_FC_4,
OUT_NEURONS_4, BIAS_SHIFT_FC_4, OUT_QF_FC_4, bias_layer4_L1, out_layer4_L1);
#endif
#ifdef PROFILING
STOP_PROFILING();
#endif
#ifdef PROFILING_TOT
STOP_PROFILING();
#endif
if(rt_core_id()==0)
{
rt_dma_copy_t cp6;
rt_dma_memcpy(
#ifdef Q8
out_L2, // ext
out_layer4_L1, // loc
OUT_NEURONS_4, // size
#endif
RT_DMA_DIR_LOC2EXT, // dir
0, // merge
&cp6 // copy
);
// rt_dma_wait(&cp6); //we don't have to wait because out_L2 are statically allocated in L2 --> no FREE problem
}
#ifdef CHECKLAYER
int errors=0;
for (int i=0; i< OUT_NEURONS_4; i++)
{
if(out_L2[i] != checksum_layer4[i])
{
#ifdef PRINT
printf("exp: %X, real: %X, index: %d\n",checksum_layer4[i], out_L2[i],i );
#endif
errors ++;
}
}
#ifdef PRINT
if(errors!=0)
printf("check failed. number of errors: %d \n", errors);
else
printf("check ok. The layer has been tested successfully. \n");
#endif
#endif
//exit from core0
printf("post-free\n");
return (0);
}