-
Notifications
You must be signed in to change notification settings - Fork 2
/
dl_layers.c
187 lines (150 loc) · 5.93 KB
/
dl_layers.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
#include "dl_layers.h"
struct cls_tensor_activations_1D convolution_1D_no_padding(struct cls_tensor_activations_1D input , struct cls_tensor_weights_1D weights){
struct cls_tensor_activations_1D outputs;
outputs.num_cols = compute_output_cols_convolution_1D(input.num_cols , weights.num_cols);
outputs.num_rows = weights.num_filters;
outputs.feature_map = (VALUE_TYPE **)malloc(outputs.num_rows * sizeof(VALUE_TYPE *));
for(int i = 0 ; i < outputs.num_rows ; i ++){
outputs.feature_map[i] = (VALUE_TYPE *)malloc(outputs.num_cols * sizeof(VALUE_TYPE));
}
int accumulated_data = 0;
for(int i = 0 ; i < weights.num_filters ; i ++){
for(int j = 0 ; j + weights.num_cols <= input.num_cols ; j ++){
for(int k = 0 ; k < weights.num_rows ; k ++){
for(int t = 0 ; t < weights.num_cols ; t ++){
accumulated_data += input.feature_map[k][j + t] * weights.filters[i][k][t];
}
}
accumulated_data += weights.bias[i];
accumulated_data = accumulated_data >> weights.shift;
// accumulated_data = round(accumulated_data * pow(2, weights.shift));
// accumulated_data = accumulated_data * pow(2, weights.shift);
if(accumulated_data > 127){
accumulated_data = 127;
}
if (weights.wReLU) {
if(accumulated_data <= 0){
accumulated_data = 0;
}
}
outputs.feature_map[i][j] = accumulated_data;
accumulated_data = 0;
}
}
// //debug
// char fo[1000];
// sprintf(fo, "layer_%d_output.dat", weights.layer_index);
// FILE *of = fopen(fo, "w");
// for(int j = 0 ; j < outputs.num_cols ; j ++)
// for(int i = 0 ; i < outputs.num_rows ; i ++)
// fprintf(of, "%d,\n", outputs.feature_map[i][j]);
// fclose(of);
return outputs;
}
int compute_output_cols_convolution_1D(int num_input_col , int num_weight_col){
return num_input_col - num_weight_col + 1;
}
struct cls_tensor_activations_1D read_activations_from_source_code(VALUE_TYPE *data , int num_rows , int num_cols){
struct cls_tensor_activations_1D output;
output.num_rows = num_rows;
output.num_cols = num_cols;
output.feature_map = (VALUE_TYPE **)malloc(num_rows * sizeof(VALUE_TYPE *));
for(int i = 0 ; i < num_rows ; i ++) {
output.feature_map[i] = (VALUE_TYPE *)malloc(num_cols * sizeof(VALUE_TYPE));
for(int j = 0 ; j < num_cols ; j ++){
output.feature_map[i][j] = data[j * num_rows + i];
}
}
return output;
}
void release_tensor_activations_1D(struct cls_tensor_activations_1D input) {
if (input.feature_map != NULL) {
for(int i = 0 ; i < input.num_rows ; i ++)
free(input.feature_map[i]);
free(input.feature_map);
}
}
struct cls_tensor_activations_1D flatten_activations(struct cls_tensor_activations_1D input){
struct cls_tensor_activations_1D output;
output.num_rows = input.num_cols * input.num_rows;
output.num_cols = 1;
output.feature_map = (VALUE_TYPE **)malloc(output.num_rows * sizeof(VALUE_TYPE *));
for(int i = 0 ; i < output.num_rows ; i ++) {
output.feature_map[i] = (VALUE_TYPE *)malloc(output.num_cols * sizeof(VALUE_TYPE));
for(int j = 0 ; j < output.num_cols ; j ++){
//printf("%d, %d\n", i%input.num_rows, i/input.num_rows);
output.feature_map[i][j] = input.feature_map[i%input.num_rows][i/input.num_rows];
}
}
return output;
}
struct cls_tensor_weights_1D read_weights_1D_from_source_code(int layer_idx, VALUE_TYPE *data , int *bias , int num_filters , int num_rows , int num_cols, int shift, bool wReLU){
struct cls_tensor_weights_1D weights;
weights.layer_index = layer_idx;
weights.num_filters = num_filters;
weights.num_rows = num_rows;
weights.num_cols = num_cols;
weights.shift = shift;
weights.wReLU = wReLU;
weights.filters = (VALUE_TYPE ***)malloc(num_filters * sizeof(VALUE_TYPE **));
weights.bias = (int *)malloc(num_filters * sizeof(int));
for(int i = 0 ; i < num_filters ; i ++){
weights.bias[i] = bias[i];
weights.filters[i] = (VALUE_TYPE **)malloc(num_rows * sizeof(VALUE_TYPE *));
for(int j = 0 ; j < num_rows ; j ++) {
weights.filters[i][j] = (VALUE_TYPE *)malloc(num_cols * sizeof(VALUE_TYPE));
for(int k = 0 ; k < num_cols ; k ++){
weights.filters[i][j][k] = data[i * num_cols * num_rows + k * num_rows + j];
}
}
}
return weights;
}
void release_tensor_weights_1D(struct cls_tensor_weights_1D weights) {
if (weights.bias != NULL)
free(weights.bias);
if (weights.filters != NULL) {
for(int i = 0 ; i < weights.num_filters ; i ++){
for(int j = 0 ; j < weights.num_rows ; j ++)
free(weights.filters[i][j]);
free(weights.filters[i]);
}
free(weights.filters);
}
}
int arg_max(struct cls_tensor_activations_1D final_out, int numOut) {
int max, index;
index = 0;
max = final_out.feature_map[0][0];
for (int c = 1; c < numOut; c++) {
if (final_out.feature_map[c][0] > max) {
index = c;
max = final_out.feature_map[c][0];
}
}
return index;
}
//VALUE_TYPE* read_values_from_file(char *filename , int number){
// if(filename == "" || filename == "NULL"){
// return 0;
// }
//
// FILE *file_io = fopen(filename, "rb");
// if(file_io == NULL){
// exit(1);
// }
// VALUE_TYPE* inputs_data = (VALUE_TYPE *) malloc(number * sizeof(VALUE_TYPE));
// size_t result = fread(inputs_data , sizeof(VALUE_TYPE) , number , file_io);
// if(result != number){
// exit(1);
// }
// fclose(file_io);
//
//// //debug
//// FILE *of = fopen("input.dat", "w");
//// for(int j = 0 ; j < number ; j ++)
//// fprintf(of, "%d,\n", inputs_data[j]);
//// fclose(of);
//
// return inputs_data;
//}