-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathneuralnetwork.c
357 lines (328 loc) · 8.89 KB
/
neuralnetwork.c
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <sleep.h>
#include <time.h>
#include "platform.h"
#include "xparameters.h"
#include "xuartps_hw.h"
#include "xtime_l.h"
#include "xil_cache.h"
#include "xil_cache_l.h"
#include "main.h"
#include "neuralnetwork.h"
#include "accregs.h"
double hard_time = 0;
double soft_time = 0;
/*
* Compute the results with the IP
* WARNING: arrays sizes must match the following:
* - frames: uint8_t[FRAMES_NB][FSIZE]
* - results: uint32_t[FRAMES_NB][NEU2]
* - weights_level1: int16_t[NEU1][FSIZE]
* - weights_level2: int16_t[NEU2][NEU1]
* - constants_recode_level1: int16_t[NEU1]
* - constants_recode_level2: int16_t[NEU2]
* The function expect the adress of the first element in the array.
*/
void nn_hardware(uint8_t *frames, uint32_t *results,
int16_t *weights_level1,
int16_t *weights_level2,
int16_t *constants_recode_level1,
int16_t *constants_recode_level2)
{
nn_process_config(weights_level1, weights_level2,
constants_recode_level1);
nn_process_frames(frames, results, constants_recode_level2);
}
/*
* Compute the results only with the software, without using the IP.
* We use this as a reference.
* WARNING: arrays sizes must match the following:
* - frames: uint8_t[FRAMES_NB][FSIZE]
* - results: uint32_t[FRAMES_NB][NEU2]
* - weights_level1: int16_t[NEU1][FSIZE]
* - weights_level2: int16_t[NEU2][NEU1]
* - constants_recode_level1: int16_t[NEU1]
* - constants_recode_level2: int16_t[NEU2]
* The function expect the adress of the first element in the array.
*/
void nn_software(uint8_t *frames, uint32_t *results,
int16_t *weights_level1,
int16_t *weights_level2,
int16_t *constants_recode_level1,
int16_t *constants_recode_level2)
{
int64_t out1[NEU1] = {0};
int64_t out2[NEU2] = {0};
int32_t i, n, image;
XTime oldtime;
XTime_GetTime(&oldtime);
for (image = 0; image < FRAMES_NB; image++) {
/*
* Initialization
*/
for (n = 0; n < NEU1; n++) {
out1[n] = 0;
}
for (n = 0; n < NEU2; n++) {
out2[n] = 0;
}
/*
* First layer computation
*/
for (i = 0; i < FSIZE; i++) {
for (n = 0; n < NEU1; n++) {
out1[n] += frames[image * FSIZE + i] *
weights_level1[n * FSIZE + i];
}
}
/*
* Cut to 32 bits values and apply activation function
*/
for (n = 0; n < NEU1; n++) {
out1[n] = cut(out1[n]);
out1[n] += constants_recode_level1[n];
out1[n] = (out1[n] > 0) ? out1[n] : 0;
out1[n] = cut(out1[n]);
}
/*
* Second layer computation
*/
for (i = 0; i < NEU1; i++) {
for (n = 0; n < NEU2; n++) {
out2[n] += out1[i] *
weights_level2[n * NEU1 + i];
}
}
/*
* Apply second layer constants.
*/
for (n = 0; n < NEU2; n++) {
out2[n] = cut(out2[n]);
out2[n] += constants_recode_level2[n];
out2[n] = (out2[n] > 0) ? out2[n] : 0;
out2[n] = cut(out2[n]);
results[image * NEU2 + n] = out2[n];
}
}
soft_time = XTime_DiffCurrReal_Double(&oldtime);
}
/*
* Send RESET signal to the IP.
*/
void nn_process_clear()
{
// Reset the accelerator
accreg_clear();
while(accreg_check_clear());
accreg_set_lvl1_fsize(FSIZE);
accreg_set_lvl1_nbneu(NEU1);
accreg_set_lvl2_nbneu(NEU2);
}
/*
* Configure the whole network.
* WARNING: array sizes must match the following:
* - weights_level1: int16_t[NEU1][FSIZE]
* - weights_level2: int16_t[NEU2][NEU1]
* - constants_recode_level1: int16_t[NEU1]
* The function expect the adress of the first element in the array.
*/
void nn_process_config(int16_t *weights_level1, int16_t *weights_level2,
int16_t *constants_recode_level1)
{
nn_config_level(weights_level1, LEVEL1);
nn_config_level(weights_level2, LEVEL2);
nn_config_level(constants_recode_level1, RECODE1);
}
/*
* Send the frames to the IP and get back the results.
* We need to pass the second layer recode constants to add them in the
* software as there is not hardware layer for that.
* WARNING: arrays sizes must match the following:
* - frames: uint8_t[FRAMES_NB][FSIZE]
* - results: uint32_t[FRAMES_NB][NEU2]
* - constants_recode_level2: uint16_t[NEU2]
* The function expect the adress of the first element in the array.
*/
void nn_process_frames(uint8_t *frames, uint32_t *results,
int16_t *constants_recode_level2)
{
XTime oldtime;
int i, j;
uint32_t* frames_buffer_alloc = NULL;
uint32_t* frames_buffer = NULL;
int32_t* out_buffer_alloc = NULL;
int32_t* out_buffer = NULL;
const unsigned frames_bufsize =
FRAMES_NB * FSIZE * sizeof(*frames_buffer) +
16 * 4;
const unsigned out_bufsize = FRAMES_NB * NEU2 * sizeof(*out_buffer) +
16 * 4;
nn_process_clear();
frames_buffer_alloc = malloc_check(frames_bufsize);
out_buffer_alloc = malloc_check(out_bufsize);
frames_buffer = (void*)uint_roundup((long)frames_buffer_alloc, 16 * 4);
out_buffer = (void*)uint_roundup((long)out_buffer_alloc, 16 * 4);
for (j = 0; j < FRAMES_NB; j++) {
for (i = 0; i < FSIZE; i++) {
frames_buffer[j * FSIZE + i] = frames[j * FSIZE + i];
}
}
// Flush cached data to DDR memory
Xil_DCacheFlush();
XTime_GetTime(&oldtime);
// Set the number of results to get
accreg_set_nboutputs(FRAMES_NB * NEU2);
// Send the frames, receive results
accreg_set_wmode_frame();
accreg_wr(10, (long)frames_buffer);
accreg_wr(12, MINIMUM_BURSTS(FRAMES_NB * FSIZE));
accreg_wr(11, (long)out_buffer);
accreg_wr(13, MINIMUM_BURSTS(FRAMES_NB * NEU2));
while(accreg_check_busyr());
while(accreg_check_busyw());
// Force the cache to get data from DDR
Xil_DCacheInvalidateRange((unsigned)out_buffer, out_bufsize);
/*
* This is not done in the IP so we do it in the software.
*/
for (i = 0; i < FRAMES_NB * NEU2; i++) {
out_buffer[i] += constants_recode_level2[i % NEU2];
out_buffer[i] = (out_buffer[i] > 0) ? out_buffer[i] : 0;
}
hard_time = XTime_DiffCurrReal_Double(&oldtime);
for (j = 0; j < FRAMES_NB; j++) {
for (i = 0; i < NEU2; i++) {
results[j * NEU2 + i] = out_buffer[j * NEU2 + i];
}
}
free(frames_buffer_alloc);
free(out_buffer_alloc);
nn_process_clear();
}
/*
* Cut and keep only the 32 low-order bits from in.
* Keep signed bits for 32 high-order bits.
*/
int64_t cut(int64_t in)
{
bool negative;
negative = (in < 0) ? true : false;
if (negative)
in = -in;
in &= 0xFFFFFFFF;
if (negative)
in = -in;
return in;
}
/*
* Configure the given level.
* level argument must be LEVEL1, LEVEL2 or RECODE1.
* Possible types of weights argument:
* - LEVEL1: int16_t[NEU1][FSIZE]
* - LEVEL2: int16_t[NEU2][NEU1]
* - RECODE1: int16_t[NEU1]
*/
void nn_config_level(void *weights, enum level level)
{
int i, j;
int32_t *config_buffer_alloc = NULL, *config_buffer = NULL;
size_t bufsize;
// On rajoute 16 * 4 pour pouvoir réaligner ensuite sans perdre de
// données.
switch (level) {
case LEVEL1:
bufsize = NEU1 * FSIZE * sizeof(int32_t) + 16 * 4;
break;
case LEVEL2:
bufsize = NEU2 * NEU1 * sizeof(int32_t) + 16 * 4;
break;
case RECODE1:
bufsize = NEU1 * sizeof(int32_t) + 16 * 4;
break;
default:
abort_printf("configuring an undefined level\n");
break;
}
nn_process_clear();
config_buffer_alloc = malloc_check(bufsize);
/*
* On réaligne la zone mémoire allouée sur une frontière de 16 * 4
* octets avant de la remplir pour la préparer aux bursts.
*/
config_buffer = (void *)uint_roundup((long)config_buffer_alloc, 16 * 4);
switch (level) {
case LEVEL1:
for (j = 0; j < NEU1; j++) {
for (i = 0; i < FSIZE; i++) {
config_buffer[j * FSIZE + i] =
((int16_t *)weights)[j * FSIZE + i];
}
}
break;
case LEVEL2:
for (j = 0; j < NEU2; j++) {
for (i = 0; i < NEU1; i++) {
config_buffer[j * NEU1 + i] =
((int16_t *)weights)[j * NEU1 + i];
}
}
break;
case RECODE1:
for (i = 0; i < NEU1; i++) {
config_buffer[i] = ((int16_t *)weights)[i];
}
break;
default:
abort_printf("configuring an undefined level\n");
break;
}
Xil_DCacheFlush();
nn_process_clear();
accreg_wr(10, (uint32_t)config_buffer);
/*
* L'écriture dans le registre 12 déclenche le burst.
*/
switch (level) {
case LEVEL1:
accreg_set_wmode_lvl1();
accreg_wr(12, MINIMUM_BURSTS(NEU1 * FSIZE));
break;
case LEVEL2:
accreg_set_wmode_lvl2();
accreg_wr(12, MINIMUM_BURSTS(NEU1 * NEU2));
break;
case RECODE1:
accreg_set_wmode_rec12();
accreg_wr(12, MINIMUM_BURSTS(NEU1));
break;
default:
abort_printf("configuring an undefined level\n");
break;
}
while(accreg_check_busyr());
free(config_buffer_alloc);
nn_process_clear();
}
/*
* Classify the given results ie,or each frame computes the selected neuron.
* WARNING: arrays sizes must match the following:
* - results: uint32_t[FRAMES_NB][NEU2]
* - classification: uint32_t[FRAMES_NB]
* The function expect the adress of the first element in the array.
*/
void classify(uint32_t *results, uint32_t *classification)
{
int i, j;
uint32_t max[FRAMES_NB] = {0};
for (i = 0; i < FRAMES_NB; i++) {
for (j = 0; j < NEU2; j++) {
if (results[i * NEU2 + j] > max[i]) {
max[i] = results[i * NEU2 + j];
classification[i] = j;
}
}
}
}