-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmaths.h
407 lines (316 loc) · 10.9 KB
/
maths.h
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
#ifndef MATHS_INCLUDE_H
#define MATHS_INCLUDE_H
#include "utils.h"
#include <math.h> //expf
#include <immintrin.h>
static inline float sigmoid_one( float value )
{
return 1.0f / (1.0f + expf( -value ));
}
static inline void relu_inplace ( float *arr, int array_count );
static inline float mean ( float *arr, int arr_count );
static inline void mytanh ( const float *arr, int count, float *out );
static inline void mytanh_inplace ( float *arr, int count );
static inline void mysigmoid ( const float *arr, int count, float *out );
static inline void mysigmoid_inplace ( float *arr, int count );
static inline void add_arrays ( const float *array_a, int count, const float *array_b, float *array_out );
static inline void add_arrays_inplace ( float *array_a, int count, const float *array_b );
// row1: row2:
// [a b c] [d e f]
//
// result:
// ad + be + cf
#if VADC_SLOW
#define dotproduct dotproduct_slow
#else
#define dotproduct dotproduct_simd
#endif // VADC_SLOW
static inline float dotproduct_slow ( const float *arr, int count, const float *arr2, int count2 );
static inline float dotproduct_unrolled ( const float *arr, int count, const float *arr2, int count2 );
static inline float dotproduct_unrolled2 ( const float *arr, int count, const float *arr2, int count2 );
static inline float dotproduct_simd ( const float *arr, int count, const float *arr2, int count2 );
// mat1_row: mat2_transposed:
// [a b c] [j l n]
// [k m o]
//
// result:
// [aj+bl+cn ak+bm+co]
static inline void mydot_arrarr ( const float *arr, int count, const float *arr2, int arr2_rows, float *arr_out );
// mata: matb:
// [a b c] [j k]
// [d e f] x [l m]
// [g h i] [n o]
//
// result:
// [aj+bl+cn ak+bm+co]
// [dj+el+fn dk+em+fo]
// [gj+hl+in gk+hm+io]
static inline void mymatmul ( float *mata, int mata_rows, int mata_cols, float *matb_transposed, int matb_transposed_rows, int matb_transposed_cols, float *out_result );
static inline void convolve_muladd ( float *arr, int count, float kernel, float *arr_out );
static inline void convolve_mc ( float *arr, int in_channel_count, int array_count, float *kernels, float *arr_out, float bias );
static inline void convolve_mc_mf_bias ( float *arr, int in_channel_count, int array_count, float *kernels, int filter_count, float *arr_out, float *bias );
static inline void convolve_mc_mf ( float *arr, int in_channel_count, int array_count, float *kernels, int filter_count, float *arr_out );
static inline void convolve_mc_mf_batch_bias ( int batch, float *arr, int in_channel_count, int array_count, float *kernels, int filter_count, float *arr_out, float *bias );
static inline void convolve_mc_mf_batch ( int batch, float *arr, int in_channel_count, int array_count, float *kernels, int filter_count, float *arr_out );
#endif // MATHS_INCLUDE_H
#if defined(MATHS_IMPLEMENTATION)
static inline void relu_inplace ( float *arr, int array_count )
{
for ( int i = 0; i < array_count; ++i )
{
if ( arr[i] < 0.0f )
{
arr[i] = 0.0f;
}
}
}
static inline float mean ( float *arr, int arr_count )
{
float result = 0.0f;
float divisor = (float)arr_count;
for ( int i = 0; i < arr_count; ++i )
{
result += arr[i];
}
return result / divisor;
}
// row1: row2:
// [a b c] [d e f]
//
// result:
// ad + be + cf
static inline float dotproduct_simd ( const float *arr, int count, const float *arr2, int count2 )
{
VAR_UNUSED(count2);
// TracyCZone(dotproduct, true);
// Assert(count == count2);
// int mincount = count > count2 ? count2 : count;
int mincount = count;
int wide = 16;
__m256 r = _mm256_setzero_ps();
float result = 0.0f;
for ( int i = 0; i < (mincount / wide) * wide; i += wide )
{
__m256 a = _mm256_loadu_ps(arr + i);
__m256 b = _mm256_loadu_ps(arr2 + i);
__m256 c = _mm256_loadu_ps(arr + i + 8);
__m256 d = _mm256_loadu_ps(arr2 + i + 8);
__m256 ab = _mm256_mul_ps(a, b);
__m256 cd = _mm256_mul_ps(c, d);
__m256 abcd = _mm256_hadd_ps(ab, cd);
r = _mm256_add_ps(r, abcd);
}
result = result + ((float *)&r)[0] + ((float *)&r)[1] + ((float *)&r)[2] + ((float *)&r)[3] + ((float *)&r)[4] + ((float *)&r)[5] + ((float *)&r)[6] + ((float *)&r)[7];
for ( int i = (mincount / wide) * wide; i < mincount; ++i )
{
float value = arr[i] * arr2[i];
result += value;
}
// TracyCZoneEnd(dotproduct);
return result;
}
static inline float dotproduct_unrolled ( const float *arr, int count, const float *arr2, int count2 )
{
VAR_UNUSED(count2);
int mincount = count;
int wide = 8;
float result = 0.0f;
for ( int i = 0; i < (mincount / wide) * wide; i += wide )
{
float value0 = arr[i+0] * arr2[i+0];
float value1 = arr[i+1] * arr2[i+1];
float value2 = arr[i+2] * arr2[i+2];
float value3 = arr[i+3] * arr2[i+3];
float value4 = arr[i+4] * arr2[i+4];
float value5 = arr[i+5] * arr2[i+5];
float value6 = arr[i+6] * arr2[i+6];
float value7 = arr[i+7] * arr2[i+7];
float value01 = value0 + value1;
float value23 = value2 + value3;
float value45 = value4 + value5;
float value67 = value6 + value7;
float value0123 = value01 + value23;
float value4567 = value45 + value67;
result = result + value0123 + value4567;
}
for ( int i = (mincount / wide) * wide; i < mincount; ++i )
{
float value = arr[i] * arr2[i];
result += value;
}
return result;
}
static inline float dotproduct_unrolled2 ( const float *arr, int count, const float *arr2, int count2 )
{
VAR_UNUSED(count2);
int mincount = count;
int wide = 8;
float value01 = 0.0f;
float value23 = 0.0f;
float value45 = 0.0f;
float value67 = 0.0f;
int i;
for ( i = 0; i < mincount - 7; i += wide )
{
float value0 = arr[i+0] * arr2[i+0];
float value1 = arr[i+1] * arr2[i+1];
float value2 = arr[i+2] * arr2[i+2];
float value3 = arr[i+3] * arr2[i+3];
float value4 = arr[i+4] * arr2[i+4];
float value5 = arr[i+5] * arr2[i+5];
float value6 = arr[i+6] * arr2[i+6];
float value7 = arr[i+7] * arr2[i+7];
value01 += value0 + value1;
value23 += value2 + value3;
value45 += value4 + value5;
value67 += value6 + value7;
}
float value0123 = value01 + value23;
float value4567 = value45 + value67;
float result = value0123 + value4567;
for ( ; i < mincount; ++i )
{
float value = arr[i] * arr2[i];
result += value;
}
return result;
}
static inline float dotproduct_slow ( const float *arr, int count, const float *arr2, int count2 )
{
VAR_UNUSED(count2);
int mincount = count;
float result = 0.0f;
for ( int i = 0; i < mincount; ++i )
{
float value = arr[i] * arr2[i];
result += value;
}
return result;
}
// mat1_row: mat2_transposed:
// [a b c] [j l n]
// [k m o]
//
// result:
// [aj+bl+cn ak+bm+co]
static inline void mydot_arrarr ( const float *arr, int count, const float *arr2, int arr2_rows, float *arr_out )
{
TracyCZone(mydot_arrarr, true);
for ( int i = 0; i < arr2_rows; ++i )
{
float value = dotproduct_simd( arr, count, arr2 + i * count, count );
arr_out[i] = value;
}
TracyCZoneEnd(mydot_arrarr);
}
// mata: matb:
// [a b c] [j k]
// [d e f] x [l m]
// [g h i] [n o]
//
// result:
// [aj+bl+cn ak+bm+co]
// [dj+el+fn dk+em+fo]
// [gj+hl+in gk+hm+io]
static inline void mymatmul ( float *mata, int mata_rows, int mata_cols, float *matb_transposed, int matb_transposed_rows, int matb_transposed_cols, float *out_result )
{
TracyCZone(mymatmul, true);
VAR_UNUSED( matb_transposed_cols );
int mata_stride = mata_cols;
int out_stride = matb_transposed_rows;
for ( int i = 0; i < mata_rows; ++i )
{
mydot_arrarr( mata + i * mata_stride, mata_cols, matb_transposed, matb_transposed_rows, out_result + i * out_stride );
}
TracyCZoneEnd(mymatmul);
}
static inline void mytanh ( const float *arr, int count, float *out )
{
for ( int i = 0; i < count; ++i )
{
out[i] = tanhf( arr[i] );
}
}
static inline void mytanh_inplace ( float *arr, int count )
{
for ( int i = 0; i < count; ++i )
{
float value = arr[i];
arr[i] = tanhf( value );
}
}
static inline void mysigmoid ( const float *arr, int count, float *out )
{
for ( int i = 0; i < count; ++i )
{
out[i] = 1.0f / (1.0f + expf( -arr[i] ));
}
}
static inline void mysigmoid_inplace ( float *arr, int count )
{
for ( int i = 0; i < count; ++i )
{
float value = arr[i];
arr[i] = 1.0f / (1.0f + expf( -value ));
}
}
static inline void add_arrays ( const float *array_a, int count, const float *array_b, float *array_out )
{
for ( int i = 0; i < count; ++i )
{
array_out[i] = array_a[i] + array_b[i];
}
}
static inline void add_arrays_inplace ( float *array_a, int count, const float *array_b )
{
for ( int i = 0; i < count; ++i )
{
array_a[i] += array_b[i];
}
}
static inline void convolve_muladd ( float *arr, int count, float kernel, float *arr_out )
{
for ( int i = 0; i < count; ++i )
{
arr_out[i] += kernel * arr[i];
}
}
static inline void convolve_mc ( float *arr, int in_channel_count, int array_count, float *kernels, float *arr_out, float bias )
{
memset( arr_out, 0, array_count * sizeof( float ) );
for ( int i = 0; i < in_channel_count; ++i )
{
int stride = i * array_count;
convolve_muladd( arr + stride, array_count, kernels[i], arr_out );
}
for ( int i = 0; i < array_count; ++i )
{
arr_out[i] += bias;
}
}
static inline void convolve_mc_mf_bias ( float *arr, int in_channel_count, int array_count, float *kernels, int filter_count, float *arr_out, float *bias )
{
for ( int i = 0; i < filter_count; ++i )
{
int out_stride = i * array_count;
convolve_mc( arr, in_channel_count, array_count, kernels + i * in_channel_count, arr_out + out_stride, bias ? bias[i] : 0.0f );
}
}
static inline void convolve_mc_mf ( float *arr, int in_channel_count, int array_count, float *kernels, int filter_count, float *arr_out )
{
convolve_mc_mf_bias( arr, in_channel_count, array_count, kernels, filter_count, arr_out, 0 );
}
static inline void convolve_mc_mf_batch_bias ( int batch, float *arr, int in_channel_count, int array_count, float *kernels, int filter_count, float *arr_out, float *bias )
{
for ( int i = 0; i < batch; ++i )
{
int stride = i * in_channel_count * array_count;
int out_stride = i * array_count * filter_count;
convolve_mc_mf_bias( arr + stride, in_channel_count, array_count, kernels, filter_count, arr_out + out_stride, bias );
}
}
static inline void convolve_mc_mf_batch ( int batch, float *arr, int in_channel_count, int array_count, float *kernels, int filter_count, float *arr_out )
{
convolve_mc_mf_batch_bias( batch, arr, in_channel_count, array_count, kernels, filter_count, arr_out, 0 );
}
#endif //MATHS_IMPLEMENTATION