-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathmodel.c
376 lines (325 loc) · 11.6 KB
/
model.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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/*
* Copyright (c) 2019-2021 Ibrahim Umit Akgun
* Copyright (c) 2019-2021 Ali Selman Aydin
* Copyright (c) 2019-2021 Erez Zadok
* Copyright (c) 2019-2021 Stony Brook University
* Copyright (c) 2019-2021 The Research Foundation of SUNY
*
* You can redistribute it and/or modify it under the terms of the Apache
* License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0).
*/
#include <model.h>
#include <utility.h>
typedef struct mt_thread_param {
model_multithreading *multithreading;
model_data *data;
void *param;
} mt_thread_param;
#ifdef ML_MODEL_DEBUG
void set_custom_weights(layers *layer_list) {
layer *current_layer;
int layer_counter = 0;
traverse_layers_forward(layer_list, current_layer) {
switch (current_layer->type) {
case LINEAR_LAYER: {
if (layer_counter == 0) {
matrix *layer_weights = allocate_matrix(2, 2, FLOAT);
matrix *bias = allocate_matrix(1, 2, FLOAT);
layer_weights->vals.f[mat_index(layer_weights, 0, 0)] = 0.4061598182;
layer_weights->vals.f[mat_index(layer_weights, 0, 1)] = 0.4216538072;
layer_weights->vals.f[mat_index(layer_weights, 1, 0)] = 0.3950368762;
layer_weights->vals.f[mat_index(layer_weights, 1, 1)] = 0.1445891261;
bias->vals.f[mat_index(bias, 0, 0)] = -0.3683853745;
bias->vals.f[mat_index(bias, 0, 1)] = -0.4919803143;
((linear_layer *)current_layer->internal)->w = layer_weights;
((linear_layer *)current_layer->internal)->bias_vector = bias;
}
if (layer_counter == 1) {
matrix *layer_weights = allocate_matrix(1, 2, FLOAT);
matrix *bias = allocate_matrix(1, 1, FLOAT);
layer_weights->vals.f[mat_index(layer_weights, 0, 0)] = 0.2827379107;
layer_weights->vals.f[mat_index(layer_weights, 0, 1)] = 0.3375059962;
bias->vals.f[mat_index(bias, 0, 0)] = -0.1888595223;
((linear_layer *)current_layer->internal)->w = layer_weights;
((linear_layer *)current_layer->internal)->bias_vector = bias;
}
layer_counter++;
kml_debug("linear layer weights\n");
print_matrix(((linear_layer *)current_layer->internal)->w);
kml_debug("--------------------------------------------\n");
kml_debug("linear layer bias\n");
print_matrix(((linear_layer *)current_layer->internal)->bias_vector);
kml_debug("--------------------------------------------\n");
}
default:
break;
}
}
}
#endif
void set_weights_biases_from_file(layer *layer, char *weight_name,
char *bias_name) {
filep weight_file = kml_file_open(weight_name, "r", O_RDONLY);
filep bias_file = kml_file_open(bias_name, "r", O_RDONLY);
switch (layer->type) {
case LINEAR_LAYER: {
linear_layer *linear = (linear_layer *)layer->internal;
load_matrix_from_file(weight_file, linear->w);
load_matrix_from_file(bias_file, linear->bias_vector);
#ifdef ML_MODEL_DEBUG
kml_debug(
"-----------------------------------------------------------\n");
kml_debug("weights: \n");
print_matrix(linear->w);
kml_debug("biases: \n");
print_matrix(linear->bias_vector);
kml_debug(
"-----------------------------------------------------------\n");
#endif
break;
}
default:
break;
}
kml_file_close(weight_file);
kml_file_close(bias_file);
}
#ifdef KML_KERNEL
EXPORT_SYMBOL(set_weights_biases_from_file);
#endif
void set_random_weights(layers *layer_list, val modula) {
layer *current_layer;
kml_random_init();
traverse_layers_forward(layer_list, current_layer) {
switch (current_layer->type) {
case LINEAR_LAYER: {
set_random_matrix(((linear_layer *)current_layer->internal)->w, modula);
set_random_matrix(
((linear_layer *)current_layer->internal)->bias_vector, modula);
break;
}
default:
break;
}
}
}
#ifdef KML_KERNEL
EXPORT_SYMBOL(set_random_weights);
#endif
void save_weights_biases_to_file(layer *layer, char *weight_name,
char *bias_name) {
filep weight_file = kml_file_open(weight_name, "w+", O_RDWR);
filep bias_file = kml_file_open(bias_name, "w+", O_RDWR);
switch (layer->type) {
case LINEAR_LAYER: {
linear_layer *linear = (linear_layer *)layer->internal;
save_matrix_to_file(weight_file, linear->w);
save_matrix_to_file(bias_file, linear->bias_vector);
#ifdef ML_MODEL_DEBUG
kml_debug(
"-----------------------------------------------------------\n");
kml_debug("weights: \n");
print_matrix(linear->w);
kml_debug("biases: \n");
print_matrix(linear->bias_vector);
kml_debug(
"-----------------------------------------------------------\n");
#endif
break;
}
default:
kml_debug("!!! there is no weights and biases for this layer\n");
break;
}
kml_file_close(weight_file);
kml_file_close(bias_file);
}
#ifdef KML_KERNEL
EXPORT_SYMBOL(save_weights_biases_to_file);
#endif
void print_weigths(layers *layer_list) {
layer *current_layer;
traverse_layers_forward(layer_list, current_layer) {
switch (current_layer->type) {
case LINEAR_LAYER: {
kml_debug("linear layer w:\n");
print_matrix(((linear_layer *)current_layer->internal)->w);
break;
}
default:
break;
}
}
}
void print_biases(layers *layer_list) {
layer *current_layer;
traverse_layers_forward(layer_list, current_layer) {
switch (current_layer->type) {
case LINEAR_LAYER: {
kml_debug("linear layer bias:\n");
print_matrix(((linear_layer *)current_layer->internal)->bias_vector);
break;
}
default:
break;
}
}
}
#ifdef ML_MODEL_DEBUG
void print_gradients(layers *layer_list) {
layer *current_layer;
traverse_layers_forward(layer_list, current_layer) {
switch (current_layer->type) {
case LINEAR_LAYER: {
kml_debug("linear layer gradient:\n");
print_matrix(((linear_layer *)current_layer->internal)->gradient);
kml_debug("linear layer bias gradient:\n");
print_matrix(((linear_layer *)current_layer->internal)->bias_gradient);
break;
}
case SIGMOID_LAYER: {
kml_debug("sigmoid layer gradient:\n");
print_matrix(((sigmoid_layer *)current_layer->internal)->gradient);
}
default:
break;
}
}
}
#endif
static thread_ret async_thread_fn(void *param) {
int change_pointer;
int completion_lock = 1;
mt_thread_param *mt_param = (mt_thread_param *)param;
#ifdef KML_KERNEL
uint64_t waiting_count = 0;
kernel_fpu_begin();
#endif
#ifndef KML_KERNEL
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
#endif
while (1) {
if (kml_atomic_int_read(&mt_param->multithreading->request_queued) > 0) {
change_pointer =
kml_atomic_int_read(&(mt_param->multithreading->mt_consume_pointer));
while (
!kml_atomic_cmpxchg(&(mt_param->multithreading->mt_consume_pointer),
&change_pointer, (change_pointer + 1) % 32)) {
change_pointer = kml_atomic_int_read(
&(mt_param->multithreading->mt_consume_pointer));
}
do {
} while (!kml_atomic_int_read(
&(mt_param->multithreading->request_completion[change_pointer])));
mt_param->data->input =
mt_param->multithreading->mt_buffers[change_pointer].x;
mt_param->data->output =
mt_param->multithreading->mt_buffers[change_pointer].y;
mt_param->multithreading->model_training_inferecing_fn(mt_param->param);
do {
} while (!kml_atomic_cmpxchg(
&(mt_param->multithreading->request_completion[change_pointer]),
&completion_lock, 0));
if (kml_atomic_fetch_sub(&(mt_param->multithreading->request_queued),
1) == 0)
kml_assert(false);
}
#ifdef KML_KERNEL
else {
waiting_count++;
if (waiting_count == 1000) {
kernel_fpu_end();
msleep(1);
waiting_count = 0;
kernel_fpu_begin();
}
}
if (kthread_should_stop()) {
break;
}
#endif
}
#ifdef KML_KERNEL
kernel_fpu_end();
#endif
return DEFAULT_THREAD_RET;
}
void create_async_thread(model_multithreading *multithreading, model_data *data,
kml_thread_func func, void *param) {
mt_thread_param *mt_param = kml_malloc(sizeof(mt_thread_param));
multithreading->model_training_inferecing_fn = func;
mt_param->multithreading = multithreading;
mt_param->data = data;
mt_param->param = param;
kml_create_thread(&(multithreading->async_thread), async_thread_fn, mt_param);
}
void set_data_async(model_data *data, model_multithreading *multithreading) {
matrix *input_exch, *output_exch;
int change_pointer;
int request_count;
int completion_lock = 0;
retry_queue:
do {
request_count = kml_atomic_int_read(&(multithreading->request_queued));
} while (request_count == 32);
if (!kml_atomic_cmpxchg(&(multithreading->request_queued), &request_count,
request_count + 1)) {
goto retry_queue;
}
do {
change_pointer = kml_atomic_int_read(&(multithreading->mt_list_pointer));
} while (!kml_atomic_cmpxchg(&(multithreading->mt_list_pointer),
&change_pointer, (change_pointer + 1) % 32));
input_exch = data->collect_input;
output_exch = data->collect_output;
data->collect_input = multithreading->mt_buffers[change_pointer].x;
data->collect_output = multithreading->mt_buffers[change_pointer].y;
multithreading->mt_buffers[change_pointer].x = input_exch;
multithreading->mt_buffers[change_pointer].y = output_exch;
do {
} while (
!kml_atomic_cmpxchg(&(multithreading->request_completion[change_pointer]),
&completion_lock, 1));
}
void init_multithreading_execution(model_multithreading *multithreading,
int sample_size, int num_features) {
int idx_mt_list = 0;
kml_atomic_int_init(&(multithreading->mt_list_pointer), 0);
kml_atomic_int_init(&(multithreading->mt_consume_pointer), 0);
kml_atomic_int_init(&(multithreading->request_queued), 0);
for (idx_mt_list = 0; idx_mt_list < MULTITHREADING_BUFFER_SIZE;
++idx_mt_list) {
multithreading->mt_buffers[idx_mt_list].x =
allocate_matrix(sample_size, num_features, FLOAT);
multithreading->mt_buffers[idx_mt_list].y =
allocate_matrix(sample_size, 1, FLOAT);
kml_atomic_int_init(&(multithreading->request_completion[idx_mt_list]), 0);
}
}
void clean_multithreading_execution(model_multithreading *multithreading) {
int mt_list_clear = 0;
kml_exit_thread(multithreading->async_thread);
#ifndef KML_KERNEL
#ifndef __APPLE__
pthread_join(multithreading->async_thread, NULL);
#endif
#endif
for (mt_list_clear = 0; mt_list_clear < MULTITHREADING_BUFFER_SIZE;
++mt_list_clear) {
free_matrix(multithreading->mt_buffers[mt_list_clear].x);
free_matrix(multithreading->mt_buffers[mt_list_clear].y);
}
}
void wait_for_draining_pipeline(model_multithreading *multithreading) {
int idx_mt_list = 0;
kml_assert(kml_atomic_int_read(&multithreading->request_queued) >= 0);
while (kml_atomic_int_read(&multithreading->request_queued) != 0 ||
kml_atomic_int_read(&multithreading->mt_list_pointer) !=
kml_atomic_int_read(&multithreading->mt_consume_pointer)) {
}
for (idx_mt_list = 0; idx_mt_list < MULTITHREADING_BUFFER_SIZE;
++idx_mt_list) {
kml_atomic_int_init(&(multithreading->request_completion[idx_mt_list]), 0);
}
}