-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
tensor.c
881 lines (720 loc) · 17.6 KB
/
tensor.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
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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
/**********************************************************************************/
/* Copyright (c) 2023 Mark Seminatore */
/* All rights reserved. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining a copy */
/* of this software and associated documentation files(the "Software"), to deal */
/* in the Software without restriction, including without limitation the rights */
/* to use, copy, modify, merge, publish, distribute, sublicense, and / or sell */
/* copies of the Software, and to permit persons to whom the Software is */
/* furnished to do so, subject to the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be included in all */
/* copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR */
/* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, */
/* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE */
/* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER */
/* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, */
/* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE */
/* SOFTWARE. */
/**********************************************************************************/
#ifdef _WIN32
# define _CRT_SECURE_NO_WARNINGS
#endif
#include <assert.h>
#include <stdio.h>
#include <math.h>
#include "tensor.h"
#if defined(USE_BLAS)
# include <cblas.h>
#endif
#define TENSOR_ALIGN 32
//------------------------------
// aligned alloc where needed
//------------------------------
static void *tmalloc(int size)
{
#if defined(_WIN32) && !defined(_WIN64)
return _aligned_malloc(size, TENSOR_ALIGN);
#else
return malloc(size);
#endif
}
//------------------------------
// aligned free where needed
//------------------------------
static void tfree(void *block)
{
#if defined(_WIN32) && !defined(_WIN64)
_aligned_free(block);
#else
free(block);
#endif
}
//------------------------------
// aligned realloc where needed
//------------------------------
static void *trealloc(void *block, int size)
{
#if defined(_WIN32) && !defined(_WIN64)
return _aligned_realloc(block, size, TENSOR_ALIGN);
#else
return realloc(block, size);
#endif
}
//------------------------------
// create a new tensor
//
// Note: tensors are ROW major
//------------------------------
PTensor tensor_create(int rows, int cols)
{
PTensor t = NULL;
t = malloc(sizeof(Tensor));
if (!t)
return NULL;
t->rows = rows;
t->cols = cols;
// make sure each row of tensor is properly aligned
t->stride = (cols + TENSOR_ALIGN - 1) & (SIZE_MAX ^ 0x1f);
// only rank 2 tensors supported now
t->rank = 2;
t->values = tmalloc(rows * cols * sizeof(real));
if (!t->values)
{
free(t);
t = NULL;
}
return t;
}
//------------------------------
// create new tensor from array
//------------------------------
PTensor tensor_create_from_array(int rows, int cols, real *array)
{
PTensor t = tensor_create(rows, cols);
assert(t);
assert(array);
if (!t || !array)
return NULL;
tensor_set_from_array(t, rows, cols, array);
return t;
}
//------------------------------
// set tensor values from array
//------------------------------
void tensor_set_from_array(PTensor t, int rows, int cols, real *array)
{
assert(t);
assert(array);
if (!t || !array)
return;
if (t->rows != rows || t->cols != cols)
{
assert(0 && "tensor: invalid shape");
return;
}
// TODO - check values alignment?
int limit = t->rows * t->cols;
for (int i = 0; i < limit; i++)
t->values[i] = *array++;
}
//------------------------------
// free tensor memory
//------------------------------
void tensor_free(PTensor t)
{
if (!t)
{
assert(t);
return;
}
t->rows = t->cols = t->stride = -1;
tfree(t->values);
free(t);
}
//------------------------------
//
//------------------------------
PTensor tensor_copy(PTensor t)
{
PTensor r = tensor_create(t->rows, t->cols);
int limit = t->rows * t->cols;
for (int i = 0; i < limit; i++)
r->values[i] = t->values[i];
return r;
}
//------------------------------
// fill tensor with given value
//------------------------------
void tensor_fill(PTensor t, real val)
{
assert(t);
if (!t)
return;
int limit = t->rows * t->cols;
int i = 0;
for (; i < limit; i++)
t->values[i] = val;
}
//------------------------------
// fill tensor with given value
//------------------------------
void tensor_random_uniform(PTensor t, real min, real max)
{
assert(t);
if (!t)
return;
int limit = t->rows * t->cols;
for (int i = 0; i < limit; i++)
{
real r = (real)rand() / (real)RAND_MAX;
real scale = max - min;
r *= scale;
r += min;
t->values[i] = r;
}
}
//------------------------------
// create new tensor of ones
//------------------------------
PTensor tensor_ones(int rows, int cols)
{
PTensor t = tensor_create(rows, cols);
if (!t)
return NULL;
tensor_fill(t, 1.0);
return t;
}
//------------------------------
// create new tensor of zeros
//------------------------------
PTensor tensor_zeros(int rows, int cols)
{
PTensor t = tensor_create(rows, cols);
if (!t)
return NULL;
tensor_fill(t, 0.0);
return t;
}
//----------------------------------
// create new tensor of rand values
//----------------------------------
PTensor tensor_create_random_uniform(int rows, int cols, real min, real max)
{
PTensor t = tensor_create(rows, cols);
if (!t)
return NULL;
tensor_random_uniform(t, min, max);
return t;
}
//--------------------------------
// Heaviside or unit setp function
// returns y = if (y > 0) then 1 else 0
//--------------------------------
PTensor tensor_heaviside(PTensor a)
{
if (!a)
{
assert(0 && "tensor: invalid tensor.");
return NULL;
}
int limit = a->rows * a->cols;
int i = 0;
for (; i < limit; i++)
{
a->values[i] = a->values[i] > (real)0.0 ? (real)1.0 : (real)0.0;
}
return a;
}
//------------------------------
// returns y = alpha * x + y
//------------------------------
PTensor tensor_axpy(real alpha, PTensor x, PTensor y)
{
if (!x || !y || x->rows != y->rows || x->cols != y->cols)
{
assert(0 && "tensor: invalid shape.");
return NULL;
}
int limit = x->rows * x->cols;
#ifdef USE_BLAS
cblas_saxpy(limit, alpha, x->values, 1, y->values, 1);
#else
int i = 0;
if (alpha == 1.0)
{
for (; i < limit; i++)
y->values[i] += x->values[i];
}
else
{
for (; i < limit; i++)
y->values[i] += alpha * x->values[i];
}
#endif
return y;
}
//----------------------------------
// returns y = alpha * x + beta * y
//----------------------------------
PTensor tensor_axpby(real alpha, PTensor x, real beta, PTensor y)
{
if (!x || !y || x->rows != y->rows || x->cols != y->cols)
{
assert(0 && "tensor: invalid shape.");
return NULL;
}
int limit = x->rows * x->cols;
#ifdef USE_BLAS
cblas_saxpby(limit, alpha, x->values, 1, beta, y->values, 1);
#else
int i = 0;
if (alpha == 1.0 && beta == 1.0)
{
for (; i < limit; i++)
y->values[i] += x->values[i];
}
else if (alpha == 1.0)
{
for (; i < limit; i++)
y->values[i] = x->values[i] + beta * y->values[i];
}
else if (beta == 1.0)
{
for (; i < limit; i++)
y->values[i] = alpha * x->values[i] + y->values[i];
}
else
{
for (; i < limit; i++)
y->values[i] = alpha * x->values[i] + beta * y->values[i];
}
#endif
return y;
}
//------------------------------
// add scalar to tensor
//------------------------------
PTensor tensor_add_scalar(PTensor t, real val)
{
if (!t)
return NULL;
int limit = t->rows * t->cols;
int i = 0;
for (; i < limit; i++)
t->values[i] += val;
return t;
}
//------------------------------
// add two tensors (a = a + b)
//------------------------------
PTensor tensor_add(PTensor a, PTensor b)
{
if (!a || !b)
return NULL;
// shape must be the same
if (a->rows != b->rows || a->cols != b->cols)
{
assert(0 && "tensor_add shapes not equal");
return NULL;
}
int limit = a->rows * a->cols;
int i = 0;
for (; i < limit; i++)
a->values[i] += b->values[i];
return a;
}
//------------------------------
// sub two tensors (a = a - b)
//------------------------------
PTensor tensor_sub(PTensor a, PTensor b)
{
if (!a || !b)
return NULL;
// shape must be the same
if (a->rows != b->rows || a->cols != b->cols)
{
assert(0 && "tensor_sub shapes not equal");
return NULL;
}
int limit = a->rows * a->cols;
int i = 0;
for (; i < limit; i++)
a->values[i] -= b->values[i];
return a;
}
//------------------------------
// square the tensor values
//------------------------------
PTensor tensor_square(PTensor t)
{
if (!t)
return NULL;
int limit = t->rows * t->cols;
int i = 0;
for (; i < limit; i++)
t->values[i] *= t->values[i];
return t;
}
//------------------------------
// multiply tensor by a scalar
//------------------------------
PTensor tensor_mul_scalar(PTensor t, real alpha)
{
if (!t)
return NULL;
int limit = t->rows * t->cols;
int i = 0;
#ifdef USE_BLAS
cblas_sscal(limit, alpha, t->values, 1);
#else
for (; i < limit; i++)
t->values[i] *= alpha;
#endif
return t;
}
//------------------------------
// multiply two tensors
// a = a * b
//------------------------------
PTensor tensor_mul(PTensor a, PTensor b)
{
if (!a || !b)
return NULL;
// shape must be the same
if (a->rows != b->rows || a->cols != b->cols)
{
assert(0 && "tensor: invalid shape");
return NULL;
}
int limit = a->rows * a->cols;
int i = 0;
for (; i < limit; i++)
a->values[i] *= b->values[i];
return a;
}
//------------------------------
// divide tensor a by b
// a = a / b
//------------------------------
PTensor tensor_div(PTensor a, PTensor b)
{
if (!a || !b)
return NULL;
if (a->cols != b->cols || b->rows != 1)
{
assert(0 && "tensor: invalid shape");
return NULL;
}
int limit = a->rows * a->cols;
int i = 0;
for (; i < limit; i++)
a->values[i] /= b->values[i];
return a;
}
//-------------------------------
// return a tensor containing max
// col values from each row of t
//-------------------------------
PTensor tensor_max(PTensor t)
{
if (!t)
return NULL;
PTensor r = tensor_zeros(1, t->cols);
for (int row = 0; row < t->rows; row++)
{
for (int col = 0; col < t->cols; col++)
{
real a = tensor_get_element(r, 0, col);
real b = tensor_get_element(t, row, col);
real val = max(a, b);
tensor_set_element(r, 0, col, val);
}
}
return r;
}
//------------------------------
// get a tensor component
//------------------------------
real tensor_get_element(PTensor t, int row, int col)
{
if (!t || row > t->rows || col > t->cols)
return 0.0;
return t->values[row * t->cols + col];
}
//------------------------------
// set a tensor component
//------------------------------
void tensor_set_element(PTensor t, int row, int col, real val)
{
if (!t || row > t->rows || col > t->cols)
return;
t->values[row * t->cols + col] = val;
}
//-------------------------------------------
// slice out rows from the end of the tensor
//-------------------------------------------
PTensor tensor_slice_rows(PTensor t, int row_start)
{
PTensor r;
assert(t);
if (!t || row_start > t->rows)
return NULL;
r = tensor_create(t->rows - row_start, t->cols);
if (!r)
return NULL;
// copy the elements
real *v = &(t->values[row_start * t->cols]);
for (int i = 0; i < (t->rows - row_start) * t->cols; i++)
r->values[i] = *v++;
// adjust size of t to remove sliced rows
t->rows -= (t->rows - row_start);
// release t's extra memory
t->values = trealloc(t->values, t->rows * t->cols * sizeof(real));
assert(t->values);
return r;
}
//-------------------------------------------
// slice out cols from the end of the tensor
//-------------------------------------------
PTensor tensor_slice_cols(PTensor t, int col_start)
{
PTensor r;
assert(t);
if (!t || col_start > t->cols)
return NULL;
// create result tensor
r = tensor_create(t->rows, t->cols - col_start);
if (!r)
return NULL;
// copy the elements
for (int row = 0; row < t->rows; row++)
{
for (int col = col_start; col < t->cols; col++)
{
real v = tensor_get_element(t, row, col);
tensor_set_element(r, row, col - col_start, v);
}
}
// fixup t to remove the cols
real *values = t->values;
for (int row = 0; row < t->rows; row++)
{
for (int col = 0; col < t->cols; col++)
{
if (col >= col_start)
continue;
*values++ = t->values[row * t->cols + col];
}
}
// adjust size of t to remove sliced cols
t->cols -= (t->cols - col_start);
// release t's extra memory
t->values = trealloc(t->values, t->rows * t->cols * sizeof(real));
assert(t->values);
return r;
}
//-------------------------------
// turn int vector to onehot
//-------------------------------
PTensor tensor_onehot(PTensor t, int classes)
{
if (t->cols > 1)
{
assert(t->cols == 1);
return NULL;
}
PTensor r = tensor_zeros(t->rows, classes);
for (int row = 0; row < t->rows; row++)
{
tensor_set_element(r, row, (int)tensor_get_element(t, row, 0), (real)1.0);
}
return r;
}
//--------------------------------
// return the horizontal sum of t
//--------------------------------
real tensor_sum(PTensor t)
{
real sum;
if (!t || t->rows > 1)
{
assert(0 && "tensor: invalid tensor");
return (real)0.0;
}
int limit = t->rows * t->cols;
#ifdef USE_BLAS
sum = cblas_sasum(limit, t->values, 1);
#else
sum = (real)0.0;
int i = 0;
for (; i < limit; i++)
sum += t->values[i];
#endif
return sum;
}
//------------------------------
// return exp of tensor t
//------------------------------
PTensor tensor_exp(PTensor t)
{
if (!t)
return NULL;
int limit = t->rows * t->cols;
int i = 0;
for (; i < limit; i++)
t->values[i] = (real)exp(t->values[i]);
return t;
}
//-------------------------------------
// return tensor containing argmax of t
//-------------------------------------
PTensor tensor_argmax(PTensor t)
{
assert(0);
if (!t)
return NULL;
return NULL;
}
//----------------------------------
// compute the matrix-vector product
// y = alpha * Ax + beta * v
//----------------------------------
PTensor tensor_matvec(TENSOR_TRANSPOSE trans, real alpha, PTensor mtx, real beta, PTensor v, PTensor dest)
{
if (trans == Tensor_NoTranspose && mtx->cols != v->cols)
{
assert(0 && "tensor: invalid shape.");
return NULL;
}
#ifdef USE_BLAS
cblas_sgemv(CblasRowMajor, (trans == Tensor_NoTranspose) ? CblasNoTrans : CblasTrans, mtx->rows, mtx->cols, alpha, mtx->values, mtx->cols, v->values, 1, beta, dest->values, 1);
#else
real sum;
if (trans == Tensor_NoTranspose)
{
assert(dest->rows == 1);
// for each row of the matrix
for (int mtx_row = 0; mtx_row < mtx->rows; mtx_row++)
{
sum = (real)0.0;
for (int mtx_col = 0; mtx_col < mtx->cols; mtx_col++)
{
sum += alpha * mtx->values[mtx_row * mtx->cols + mtx_col] * v->values[mtx_col];
}
dest->values[mtx_row] = beta * dest->values[mtx_row] + sum;
}
}
else
{
assert(dest->rows == 1);
for (int mtx_col = 0; mtx_col < mtx->cols; mtx_col++)
{
sum = (real)0.0;
for (int mtx_row = 0; mtx_row < mtx->rows; mtx_row++)
{
sum += alpha * mtx->values[mtx_row * mtx->cols + mtx_col] * v->values[mtx_row];
}
dest->values[mtx_col] = beta * dest->values[mtx_col] + sum;
}
}
#endif
return dest;
}
//---------------------------------
// compute the tensor outer product
// dest += alpha * a * b
//---------------------------------
PTensor tensor_outer(real alpha, PTensor a, PTensor b, PTensor dest)
{
if (a->cols != dest->rows || b->cols != dest->cols)
{
assert(0 && "tensor: invalid shape.");
return NULL;
}
#ifdef USE_BLAS
cblas_sger(CblasRowMajor, dest->rows, dest->cols, alpha, a->values, 1, b->values, 1, dest->values, dest->cols);
#else
if (alpha == 1.0)
{
for (int a_col = 0; a_col < a->cols; a_col++)
{
for (int b_col = 0; b_col < b->cols; b_col++)
{
dest->values[a_col * b->cols + b_col] += a->values[a_col] * b->values[b_col];
}
}
}
else
{
for (int a_col = 0; a_col < a->cols; a_col++)
{
for (int b_col = 0; b_col < b->cols; b_col++)
{
dest->values[a_col * b->cols + b_col] += alpha * a->values[a_col] * b->values[b_col];
}
}
}
#endif
return dest;
}
//-------------------------------
//
//-------------------------------
PTensor tensor_gemm(real alpha, PTensor A, PTensor B, real beta, PTensor C)
{
return C;
}
//-------------------------------
// write tensor to CSV file
//-------------------------------
int tensor_save_to_file(PTensor t, const char *filename)
{
assert(filename);
assert(t);
if (!t || !filename)
return -1;
FILE *fptr = fopen(filename, "wt");
if (!fptr)
return -1;
for (int row = 0; row < t->rows; row++)
{
for (int col = 0; col < t->cols; col++)
{
if (col != 0)
fputs(", ", fptr);
fprintf(fptr, "%f", t->values[row * t->cols + col]);
}
fputc('\n', fptr);
}
fclose(fptr);
return 0;
}
//------------------------------
// print the tensor
//------------------------------
void tensor_print(PTensor t)
{
assert(t);
if (!t)
return;
real *v = t->values;
putchar('[');
for (int row = 0; row < t->rows; row++)
{
putchar('[');
for (int col = 0; col < t->cols; col++)
{
if (col != 0)
putchar(',');
printf(" %3.2g", *v++);
}
putchar(']');
if (row + 1 != t->rows)
puts("");
}
puts("]\n");
}