-
Notifications
You must be signed in to change notification settings - Fork 15
/
test_ad_lms_vector.scala
1504 lines (1201 loc) · 44.9 KB
/
test_ad_lms_vector.scala
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
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package lantern
import scala.util.continuations._
import scala.util.continuations
import scala.virtualization.lms._
import org.scala_lang.virtualized.virtualize
import org.scala_lang.virtualized.SourceContext
import scala.collection.mutable.ArrayBuffer
import scala.collection.{Seq => NSeq}
import scala.math._
import org.scalatest.FunSuite
import java.io.PrintWriter
import java.io.File
class AdLMSVectorTest extends FunSuite {
test("array0") {
val array0 = new DslDriverC[String, Unit] with TensorExp {
@virtualize
def snippet(a: Rep[String]): Rep[Unit] = {
val addr = getMallocAddr()
//printf("address is at %ld \\n", addr)
resetMallocAddr(addr)
//printf("now lets use some memory\\n")
val mem = Tensor.zeros(100)
val addr1 = getMallocAddr()
//printf("Now address is at %ld \\n", addr1)
resetMallocAddr(addr)
val addr2 = getMallocAddr()
//printf("after reset, the address is back to %ld\\n", addr2)
//assertions
if (addr + 400 != addr1) printf("ERROR: addr did not increase by 800")
if (addr != addr2) printf("ERROR: addr did not reset to the give value")
}
}
array0.eval("abc")
}
test("array1") {
val array1 = new DslDriverC[String, Unit] with TensorExp {
@virtualize
def snippet(a: Rep[String]): Rep[Unit] = {
val length = 2
val res = Tensor.randinit(length)
val res2 = Tensor.randinit(length, seed = Some(5))
val result = res dot res2
// assertions
if (res.data(0) * res2.data(0) + res.data(1) * res2.data(1) != result.data(0))
println("ERROR: the dot product of two vectors is not correct")
}
}
array1.eval("abc")
}
test("array1_1") {
val array1_1 = new DslDriverC[String, Unit] with TensorExp {
@virtualize
def snippet(a: Rep[String]): Rep[Unit] = {
val dim0 = 2
val dim1 = 3
val matrix = Tensor.rand(dim0, dim1)
val vector = Tensor.randinit(dim1, seed = Some(4))
//println("the result is:")
val result = matrix dot vector
//result.print()
if (matrix(0, 0) * vector(0) + matrix(0, 1) * vector(1) + matrix(0, 2) * vector(2) != result(0))
printf("ERROR: the matrix vector dot product is wrong on the first element of result, %.3f != %.3f\\n", matrix(0, 0) * vector(0) + matrix(0, 1) * vector(1) + matrix(0, 2) * vector(2), result(0))
if (matrix(1, 0) * vector(0) + matrix(1, 1) * vector(1) + matrix(1, 2) * vector(2) != result(1))
printf("ERROR: the matrix vector dot product is wrong on the second element of result, %.3f != %.3f\\n", matrix(1, 0) * vector(0) + matrix(1, 1) * vector(1) + matrix(1, 2) * vector(2), result(1))
}
}
array1_1.eval("abc")
}
test("array2") {
val array2 = new DslDriverC[String, Unit] with TensorExp {
@virtualize
def snippet(a: Rep[String]): Rep[Unit] = {
// read training data from file (for now just use random)
val length = 2
val v = Tensor.randinit(length)
// calculate gradient
val grad = gradR(t => t dot t)(v)
// assertions
Tensor.assertEqual(v * 2.0f, grad)
// construct TensorR for closure
val tv = TensorR(v)
val loss = gradR_loss(dummy => tv dot tv)(Tensor.zeros(1))
Tensor.assertEqual((v dot v), loss)
Tensor.assertEqual(tv.d, grad)
}
}
array2.eval("2.0f")
}
test("array2_1"){
val array2_1 = new DslDriverC[String, Unit] with TensorExp {
@virtualize
def snippet(a: Rep[String]): Rep[Unit] = {
val dim0 = 2
val vector = Tensor.randinit(dim0, seed = Some(4))
// initialize tensors for closure
val ve = new TensorR(vector, Tensor.zeros(dim0))
val half = new TensorR(Tensor.halves(dim0), Tensor.zeros(dim0))
// define function of model
def model(dummy: TensorR): TensorR @diff = {
((ve dot ve) * half).sum()
}
val loss = gradR_loss(model)(Tensor.zeros(1))
Tensor.assertEqual(loss, ((vector dot vector) * Tensor.halves(dim0)).sum(), "1")
Tensor.assertEqual(ve.d, vector * 2.0f ,"2")
Tensor.assertEqual(half.d, Tensor.fill((vector dot vector).data(0), 2), "3")
()
}
}
array2_1.eval("abc")
}
test("array2_2") {
val array2_2 = new DslDriverC[String, Unit] with TensorExp {
@virtualize
def snippet(a: Rep[String]): Rep[Unit] = {
val dim0 = 2
val dim1 = 3
val matrix = Tensor.rand(dim0, dim1)
val vector = Tensor.randinit(dim1, seed = Some(4))
// initialize tensors for closure
val ma = TensorR(matrix)
val ve = TensorR(vector)
// define function of model
def model(dummy: TensorR): TensorR @diff = {
(ma dot ve).sum()
}
val loss = gradR_loss(model)(Tensor.zeros(1))
Tensor.assertEqual(loss, (matrix dot vector).sum(), "11")
Tensor.assertEqual(ma.d, Tensor.expand(vector, dim0), "12")
val sol = matrix.sumOnDim1()
Tensor.assertEqual(ve.d, sol, "13")
()
}
}
array2_2.eval("abc")
}
test("testTrans") {
val testTrans = new DslDriverC[String, Unit] with TensorExp {
@virtualize
def snippet(a: Rep[String]): Rep[Unit] = {
val idx = var_new(0)
val t = Tensor.fill(seq => { idx += 1; idx }, 2, 3)
Tensor.assertEqual(t.trans(), Tensor.fromData(1.0f, 4.0f, 2.0f, 5.0f, 3.0f, 6.0f).resize(3, 2), "Transpose invalid")
}
}
testTrans.eval("abs")
}
test("array2_3") {
val array2_3 = new DslDriverC[String, Unit] with TensorExp {
@virtualize
def snippet(a: Rep[String]): Rep[Unit] = {
val vocab_size = 3
val hidden_size = 10
val Wxh = Tensor.randinit(hidden_size, vocab_size, 0.1f) // input to hidden
val Whh = Tensor.randinit(hidden_size, hidden_size, 0.1f) // hidden to hidden
val Why = Tensor.randinit(vocab_size, hidden_size, 0.1f) // hidden to output
val bh = Tensor.randinit(hidden_size)
val by = Tensor.randinit(vocab_size)
val hprev = Tensor.randinit(hidden_size)
val hprev_next = Tensor.zeros_like(hprev) // this vector catches the new hidden value, see the NOTE below
// wrap as tensors
val Wxh1 = TensorR(Wxh)
val Whh1 = TensorR(Whh)
val Why1 = TensorR(Why)
val bh1 = TensorR(bh)
val by1 = TensorR(by)
val hprev1 = TensorR(hprev)
// encode input and output
val x_data = NewArray[Int](3); x_data(0) = 0; x_data(1) = 1; x_data(2) = 2
val y_data = NewArray[Int](3); y_data(0) = 2; y_data(1) = 0; y_data(2) = 1
//val x_data = mutableStaticData(scala.Array(0, 1, 2))
//val y_data = mutableStaticData(scala.Array(2, 0, 1))
// our method of loss and gradient calculation
def lossFun: (TensorR => TensorR @diff) = { (dummy: TensorR) =>
val loss = TensorR(Tensor.zeros(1))
val in = ArrayBuffer[TensorR]()
in.append(loss)
in.append(hprev1)
val outputs = LOOPSM(in)(1) { i => t =>
// get input as one-hot tensor
val x = Tensor.zeros(vocab_size)
x.data(x_data(i)) = 1
val x1 = TensorR(x)
// get output as one-hot tensor
val y = Tensor.zeros(vocab_size)
y.data(y_data(i)) = 1
val y1 = TensorR(y)
val tmp = (Wxh1 dot x1)
val h1 = (tmp + (Whh1 dot t(1)) + bh1).tanh() // use hidden state and x1 to compute hidden state
val e1 = (Why1.dot(h1) + by1).exp() // use new hidden state to compute unnormalized prob
val p1 = e1 / e1.sum() // use unnormalized prob to compute normalize prob
generate_comment("Compute new loss")
val newloss = t(0) - (p1 dot y1).log() // loss is updated by original loss t(0) and additional loss
generate_comment("Done computing loss")
val out = ArrayBuffer[TensorR]()
out.append(newloss)
out.append(h1)
out
}
hprev_next.copy_data(outputs(1).x) // update the hidden state with the result from LOOP
outputs(0) // return the final loss
}
val loss1 = gradR_loss(lossFun)(Tensor.zeros(1))
//printf("bh1\\n")
//bh1.d.printRaw(hidden_size)
generate_comment("Compute real value")
// correct method of loss and gradient calculation, adapting from Numpy
// preset space for gradients
val dWxh = Tensor.zeros_like(Wxh)
val dWhh = Tensor.zeros_like(Whh)
val dWhy = Tensor.zeros_like(Why)
val dbh = Tensor.zeros_like(bh)
val dby = Tensor.zeros_like(by)
val dhnext = Tensor.zeros_like(hprev)
val sum_loss = Tensor.zeros(1)
val hprev_new = Tensor.zeros_like(hprev)
def lossOneCycle(i: Int, hprev: Tensor): Unit = {
// get input as one-hot tensor
val x = Tensor.zeros(vocab_size)
x.data(x_data(i)) = 1
// get output as one-hot tensor
val y = Tensor.zeros(vocab_size)
y.data(y_data(i)) = 1
// forward pass
val tmp = (Wxh dot x)
val hs = (tmp + (Whh dot hprev) + bh).tanh()
val ys = (Why dot hs) + by
val ye = ys.exp()
val ps = ye / ye.sum()
sum_loss -= (ps dot y).log()
if (i < 0) lossOneCycle(i + 1, hs)
else hprev_new.copy_data(hs)
// backward pass
val dy = Tensor.copy(ps)
dy.data(y_data(i)) -= 1
dWhy += (dy cart hs)
dby += dy
val dh = (Why.trans() dot dy) + dhnext
val dhraw = (Tensor.ones(1) - hs * hs) * dh
dbh += dhraw
dWxh += (dhraw cart x)
dWhh += (dhraw cart hprev)
dhnext.copy_data(Whh.trans() dot dhraw)
()
}
lossOneCycle(0, hprev)
// assertions
Tensor.assertEqual(loss1, sum_loss, "loss")
Tensor.assertEqual(hprev_next, hprev_new, "hidden")
Tensor.assertEqual(Wxh1.d, dWxh, "dWxh")
Tensor.assertEqual(Whh1.d, dWhh, "dWhh")
Tensor.assertEqual(Why1.d, dWhy, "dWhy")
Tensor.assertEqual(bh1.d, dbh, "dbh")
Tensor.assertEqual(by1.d, dby, "dby")
}
}
array2_3.eval("abc")
}
test("array2_4"){
val array2_4 = new DslDriverC[String, Unit] with TensorExp {
@virtualize
def snippet (a: Rep[String]): Rep[Unit] = {
val vocab_size = 3
val by = Tensor.zeros(vocab_size)
val by1 = TensorR(by)
val y = Tensor.zeros(vocab_size)
y.data(1) = 1
val y1 = TensorR(y)
def lossFun = { (dummy: TensorR) =>
val e1 = (by1).exp()
val p1 = e1 / e1.sum()
(p1 dot y1).log()
}
val dummy = gradR(lossFun)(Tensor.zeros(1))
// FIXME: need a correct implementation of gradient to check with
}
}
array2_4.eval("abc")
}
test("array2_5") {
val array2_5 = new DslDriverC[String, Unit] with TensorExp {
@virtualize
def snippet (a: Rep[String]): Rep[Unit] = {
val vocab_size = 3
val e = Tensor.ones(vocab_size)
val e1 = TensorR(e)
val a = Tensor.ones(vocab_size)
val a1 = TensorR(a)
val y = Tensor.zeros(vocab_size)
y.data(1) = 1
val y1 = TensorR(y)
def lossFun = { (dummy: TensorR) =>
//e1.sum()
val p1 = a1 / e1.sum()
(p1 dot y1).log()
}
val dummy = gradR(lossFun)(Tensor.zeros(1))
// FIXME: need a correct implementation of gradient to check with
}
}
array2_5.eval("abc")
}
test("array3") {
val array3 = new DslDriverC[String, Unit] with TensorExp {
@virtualize
def snippet(a: Rep[String]): Rep[Unit] = {
// use random array as input
val length = 2
val v = Tensor.randinit(length)
//v.print()
// calcuate gradient
val grad = gradR(t => {val y = IF(t.x.data(0) > 0.0f) {t + t}{t * t}
y.sum() })(v)
// another way of implementing it
val grad1 = gradR(t => (t + t).sum())(v)
val grad2 = gradR(t => (t * t).sum())(v)
if (v(0) > 0) Tensor.assertEqual(grad, grad1)
else Tensor.assertEqual(grad, grad2)
}
}
//println(array3.code)
array3.eval("abc")
}
test("array4") {
val array4 = new DslDriverC[String, Unit] with TensorExp {
@virtualize
def snippet(a: Rep[String]): Rep[Unit] = {
// use random array as input
val length = 2
Tensor.randseed()
val v = Tensor.randinit(length)
v.print()
val halfv = Tensor.halves(length)
val half = (new TensorR(halfv, Tensor.zeros(length)))
// calculate gradient
val grad = gradR(t => {val y = LOOP(t)(t => t.x.data(0) > 0.1f)(t => t * half)
y.sum() })(v)
// show gradient
grad.print()
//println("Tensor in closure can also accumulate gradient, which is important")
half.d.print()
// FIXME: Implement the correct gradient and assertEqual
}
}
array4.eval("abc")
}
test("array4_1") {
val array4_1 = new DslDriverC[String, Unit] with TensorExp {
@virtualize
def snippet(a: Rep[String]): Rep[Unit] = {
val length = 2
val v = Tensor.randinit(length)
// v.print()
val half = new TensorR(Tensor.halves(length), Tensor.zeros(length))
val grad = gradR(t => {
val y = LOOPS(t)(3)(i => t => t * half )
y.sum()
})(v)
val save_half_grad = Tensor.zeros(length)
save_half_grad.copy_data(half.d)
// alternative implementation
half.d.clear()
val grad2 = gradR( t => {
(t * half * half * half).sum()
})(v)
// assertion
Tensor.assertEqual(grad, grad2)
Tensor.assertEqual(save_half_grad, half.d)
}
}
array4_1.eval("abc")
}
test("array4_2") {
// test using array data by closure
val array4_2 = new DslDriverC[String, Unit] with TensorExp {
def snippet(a: Rep[String]): Rep[Unit] = {
// random initialization
val length = 3
val v = Tensor.randinit(length)
// v.print()
// get data from "file" (more like generate static data and lift it to Rep type)
val ddim0 = 2
val ddim1 = 3
val data1 = NewArray[Float](ddim1)
val data2 = NewArray[Float](ddim1)
for (i <- (0 until ddim1): Rep[Range]) {
data1(i) = (i + 1)
data2(i) = (i + 1) * 2
}
val data = NewArray[Array[Float]](ddim0)
data(0) = data1; data(1) = data2
val model: TensorR => TensorR @diff = { (x: TensorR) =>
val y = LOOPS(x)(ddim0)(i => x1 => {
val data_point = TensorR(Tensor(data(i), ddim1))
x1 * data_point
})
y.sum()
}
val grad = gradR(model)(v)
// alternative implememetation
val grad1 = gradR(t =>
(t * TensorR(Tensor(data(0), ddim1)) * TensorR(Tensor(data(1), ddim1))).sum()
)(v)
// assertion
Tensor.assertEqual(grad, grad1)
}
}
array4_2.eval("abc")
}
test("array4_4") {
val array4_4 = new DslDriverC[String, Unit] with TensorExp {
def snippet(a: Rep[String]): Rep[Unit] = {
val length = 2
val v = Tensor.randinit(length)
//v.print()
val u = Tensor.randinit(length, seed = Some(5))
//u.print()
val half = new TensorR(Tensor.halves(length), Tensor.zeros(length))
val vv = TensorR(v)
val uu = TensorR(u)
val dummy = gradR(dum => {
val in = ArrayBuffer[TensorR](vv, uu)
val y = LOOPSM(in)(3)(i => ins => {
val vvv = ins(0) * half
val uuu = ins(1) * half
ArrayBuffer[TensorR](vvv, uuu)
})
y(1).sum() + y(0).sum()})(Tensor.zeros(1))
// save gradients
val save_vv_grad = Tensor.zeros(length); save_vv_grad.copy_data(vv.d); vv.clear_grad()
val save_uu_grad = Tensor.zeros(length); save_uu_grad.copy_data(uu.d); uu.clear_grad()
val save_ha_grad = Tensor.zeros(length); save_ha_grad.copy_data(half.d); half.clear_grad()
// alternative implementation
val dummy1 = gradR(dum => {
(vv * half * half * half + uu * half * half * half).sum()
})(Tensor.zeros(1))
// assertions
Tensor.assertEqual(save_ha_grad, half.d)
Tensor.assertEqual(save_vv_grad, vv.d)
Tensor.assertEqual(save_uu_grad, uu.d)
}
}
array4_4.eval("abc")
}
test("array5") {
val array5 = new DslDriverC[String, Unit] with TensorExp {
def snippet(a: Rep[String]): Rep[Unit] = {
val length = 2
val v = Tensor.randinit(length)
//v.print()
val grad = gradR(t => (t * t).sum())(v)
//grad.print()
Tensor.assertEqual(grad, v * 2.0f)
}
}
println("run test case in array5")
array5.eval("abc")
}
test("array6") {
val array6 = new DslDriverC[String, Unit] with TensorExp {
def snippet(a: Rep[String]): Rep[Unit] = {
val length = 2
val v = Tensor.randinit(length)
//v.print()
val grad = gradR(t => (t / t).sum())(v)
//grad.print()
Tensor.assertEqual(grad, Tensor.zeros(length))
}
}
array6.eval("abc")
}
test("array7") {
val array7 = new DslDriverC[String, Unit] with TensorExp {
def snippet(a: Rep[String]): Rep[Unit] = {
val length = 2
val v = Tensor.randinit(length)
//v.print()
val grad = gradR(t => (t.tanh()).sum())(v)
//grad.print()
val e1 = v.tanh();
val ee = Tensor.ones(length) - e1 * e1
Tensor.assertEqual(grad, ee)
}
}
array7.eval("abc")
}
test("array7_1") {
val array7_1 = new DslDriverC[String, Unit] with TensorExp {
def snippet(a: Rep[String]): Rep[Unit] = {
val length = 2
val v = Tensor.randinit(length)
val grad = gradR(t => (t.sigmoid()).sum())(v)
val e1 = v.sigmoid()
val ee = (Tensor.ones(1) - e1) * e1
Tensor.assertEqual(grad, ee)
}
}
array7_1.eval("abc")
}
test("array8"){
val array8 = new DslDriverC[String, Unit] with TensorExp {
def snippet(a: Rep[String]): Rep[Unit] = {
val length = 2
val v = Tensor.randinit(length)
// v.print()
val grad = gradR(t => (t.exp()).sum())(v)
//grad.print()
Tensor.assertEqual(grad, v.exp())
}
}
array8.eval("abc")
}
test("array9") {
val array9 = new DslDriverC[String, Unit] with TensorExp {
def snippet(a: Rep[String]): Rep[Unit] = {
val length = 2
val v = Tensor.randPositive(length)
//v.print()
val grad = gradR(t => (t.log()).sum())(v)
//grad.print()
Tensor.assertEqual(grad, Tensor.ones(length) / v)
}
}
array9.eval("abc")
}
test("array10") {
val array10 = new DslDriverC[String, Unit] with TensorExp {
def snippet(a: Rep[String]): Rep[Unit] = {
val length = 2
val v = Tensor.randinit(length)
//v.print()
val arra = NewArray[Array[Float]](2)
arra(0) = NewArray[Float](2)
arra(0)(0) = 4.0f
arra(0)(1) = 2.0f
arra(1) = NewArray[Float](2)
arra(1)(0) = 1.5f
arra(1)(1) = 2.0f
// create a model that recursively use the data in arr (originated from list)
def model: TensorR => TensorR @diff = { (x: TensorR) =>
LOOPL(x)(arra.length)(i => x1 => new TensorR(Tensor(arra(i), length), Tensor.zeros(length)) * x1)
}
val grad = gradR(t => (model(t)).sum())(v)
//grad.print()
val grad1 = gradR(t =>
(t * TensorR(Tensor(arra(0), length)) * TensorR(Tensor(arra(1), length))).sum()
)(v)
Tensor.assertEqual(grad, grad1)
}
}
array10.eval("abc")
}
test("array11") {
val array11 = new DslDriverC[String, Unit] with TensorExp {
def snippet(a: Rep[String]): Rep[Unit] = {
val length = 2
val v = Tensor.randinit(length)
//v.print()
/*
5.0f, 4.0f
/ \
/ \
3.0f, 2.0f 1.5f, 1.4f
*/
val arra = NewArray[Array[Float]](3)
arra(0) = NewArray[Float](2)
arra(0)(0) = 5.0f; arra(0)(1) = 4.0f
arra(1) = NewArray[Float](2)
arra(1)(0) = 3.0f; arra(1)(1) = 2.0f
arra(2) = NewArray[Float](2)
arra(2)(0) = 1.5f; arra(2)(1) = 1.4f
val lch1 = NewArray[Int](3)
lch1(0) = 1; lch1(1) = -1; lch1(2) = -1
val rch1 = NewArray[Int](3)
rch1(0) = 2; rch1(1) = -1; rch1(2) = -1
// create a model that recursively use the data (originated from tree)
def model: TensorR => TensorR @diff = { (x: TensorR) =>
LOOPT(0)(x)(lch1, rch1){ (l: TensorR, r: TensorR, i: Rep[Int]) =>
l * r * new TensorR(Tensor(arra(i), length), Tensor.zeros(length))
}
}
val grad = gradR(t => model(t).sum())(v)
//grad.print()
def model1: TensorR => TensorR @diff = { (x: TensorR) =>
val leftchild = x * TensorR(Tensor(arra(1), length)) * x
val rightchild = x * TensorR(Tensor(arra(2), length)) * x
val root = leftchild * TensorR(Tensor(arra(0), length)) * rightchild
root.sum()
}
val grad1 = gradR(model1)(v)
// assertion
Tensor.assertEqual(grad, grad1)
}
}
array11.eval("abc")
}
test("array11_1") {
val array11_1 = new DslDriverC[String, Unit] with TensorExp {
def snippet(a: Rep[String]): Rep[Unit] = {
val length = 2
val v = Tensor.randinit(length)
//v.print()
/*
5.0f, 4.0f
/ \
/ \
3.0f, 2.0f 1.5f, 1.4f
*/
val arra = NewArray[Array[Float]](3)
arra(0) = NewArray[Float](2)
arra(0)(0) = 5.0f; arra(0)(1) = 4.0f
arra(1) = NewArray[Float](2)
arra(1)(0) = 3.0f; arra(1)(1) = 2.0f
arra(2) = NewArray[Float](2)
arra(2)(0) = 1.5f; arra(2)(1) = 1.4f
val lch1 = NewArray[Int](3)
lch1(0) = 1; lch1(1) = -1; lch1(2) = -1
val rch1 = NewArray[Int](3)
rch1(0) = 2; rch1(1) = -1; rch1(2) = -1
val add: TensorR = TensorR(Tensor.ones(length))
// create a model that recursively use the data (originated from tree)
def model: TensorR => TensorR @diff = { (x: TensorR) =>
val in = new ArrayBuffer[TensorR](); in.append(x); in.append(add)
val tmp = LOOPTM(0)(in)(lch1, rch1){ (l: ArrayBuffer[TensorR], r: ArrayBuffer[TensorR], i: Rep[Int]) =>
val curr = TensorR(Tensor(arra(i), length))
val new_x = l(0) * r(0) * curr; val new_add = l(1) + r(1) + curr
val out = new ArrayBuffer[TensorR](); out.append(new_x); out.append(new_add)
out
}
tmp(0).sum() + tmp(1).sum()
}
val grad = gradR(t => model(t))(v)
//grad.print()
// save gradient of add
val save_grad_add = Tensor.zeros(length); save_grad_add.copy_data(add.d); add.clear_grad()
def model1: TensorR => TensorR @diff = { (x: TensorR) =>
val val1 = TensorR(Tensor(arra(1), length))
val leftchild = x * val1 * x; val leftch = add + val1 + add
val val2 = TensorR(Tensor(arra(2), length))
val rightchild = x * val2 * x; val rightch = add + val2 + add
val val0 = TensorR(Tensor(arra(0), length))
val root = leftchild * val0 * rightchild; val root2 = leftch + val0 + rightch
root.sum() + root2.sum()
}
val grad1 = gradR(model1)(v)
// assertion
Tensor.assertEqual(grad, grad1)
Tensor.assertEqual(save_grad_add, add.d)
}
}
array11_1.eval("abc")
}
test("cnn_test1") {
val cnn_test1 = new DslDriverC[String, Unit] with TensorExp with ScannerLowerExp {
@virtualize
def snippet(a: Rep[String]): Rep[Unit] = {
val iPane = 1
val iRow = 16
val iCol = 20
val input = Tensor.ones(iPane, iRow, iCol)
val kOut = 1
val kIn = iPane
val kRow = 3
val kCol = 3
val kernel = Tensor.ones(kOut, kIn, kRow, kCol)
val res = input.conv2D(kernel, 1, 1)
Tensor.assertEqual(res, Tensor.fill((kRow * kCol * kIn) * 1.0f, kOut, iRow - kRow + 1, iCol - kCol + 1), "CNN 1")
}
}
cnn_test1.eval("abc")
}
test("cnn_test2") {
val cnn_test2 = new DslDriverC[String, Unit] with TensorExp with ScannerLowerExp {
@virtualize
def snippet(a: Rep[String]): Rep[Unit] = {
val iPane = 1
val iRow = 16
val iCol = 20
val input = Tensor.ones(iPane, iRow, iCol)
val kOut = 1
val kIn = iPane
val kRow = 3
val kCol = 3
val kernel = Tensor.fill((i: NSeq[Rep[Int]]) => if (i(2) == kRow/2 && i(3) == kCol/2) 1.0f else 0.0f, kOut, kIn, kRow, kCol)
val res = input.conv2D(kernel, 1, 1)
Tensor.assertEqual(res, Tensor.fill(1.0f, kOut, iRow - kRow + 1, iCol - kCol + 1), "CNN 2")
}
}
cnn_test2.eval("abc")
}
test("cnn_test3") {
val cnn_test3 = new DslDriverC[String, Unit] with TensorExp with ScannerLowerExp {
@virtualize
def snippet(a: Rep[String]): Rep[Unit] = {
val iPane = 1
val iRow = 16
val iCol = 20
val input = Tensor.ones(iPane, iRow, iCol)
val kOut = 1
val kIn = iPane
val kRow = 3
val kCol = 3
val kernel = Tensor.fill((i: NSeq[Rep[Int]]) => if (i(2) == kRow/2 && i(3) == kCol/2) 1.0f else 0.0f ,kOut, kIn, kRow, kCol)
val res = input.conv2D(kernel, 2, 2)
Tensor.assertEqual(res, Tensor.fill(1.0f, kOut, (iRow - kRow)/2 + 1, (iCol - kCol)/2 + 1), "CNN 3")
}
}
cnn_test3.eval("abc")
}
test("cnn_back_test1") {
val cnn_back_test1 = new DslDriverC[String, Unit] with TensorExp with ScannerLowerExp {
@virtualize
def snippet(a: Rep[String]): Rep[Unit] = {
val iPane = 1
val iRow = 16
val iCol = 20
val input = Tensor.ones(iPane, iRow, iCol)
val kOut = 1
val kIn = iPane
val kRow = 3
val kCol = 3
val kernel = Tensor.ones(kOut, kIn, kRow, kCol)
val varInput = TensorR(input)
val varKernel = TensorR(kernel)
val rS = 1
val cS = 1
val tot = NewArray[Long](2)
def lossFun = { (dummy: TensorR) =>
val res = varInput.conv(varKernel, rS, cS, tot)
res.sum()
}
val loss = gradR_loss(lossFun)(Tensor.zeros(1))
val resR = (iRow - kRow)/rS + 1
val resC = (iCol - kCol)/cS + 1
Tensor.assertEqual(loss, Tensor.scalar(resR * resC * 9.0f), "BACK - LOSS")
Tensor.assertEqual(varKernel.d, Tensor.fill(resR * resC * 1.0f, kIn, kOut, kRow, kCol), "BACK 1 - KERNEL D")
}
}
cnn_back_test1.eval("abc")
}
test("cnn_back_test2") {
val cnn_back_test2 = new DslDriverC[String, Unit] with TensorExp with ScannerLowerExp {
@virtualize
def snippet(a: Rep[String]): Rep[Unit] = {
val iPane = 1
val iRow = 16
val iCol = 20
val input = Tensor.ones(iPane, iRow, iCol)
val kOut = 1
val kIn = iPane
val kRow = 3
val kCol = 3
val kernel = Tensor.fill((i: NSeq[Rep[Int]]) => if (i(2) == kRow/2 && i(3) == kCol/2) 1.0f else 0.0f ,kOut, kIn, kRow, kCol)
val varInput = TensorR(input)
val varKernel = TensorR(kernel)
val rS = 1
val cS = 1
val tot = NewArray[Long](2)
def lossFun = { (dummy: TensorR) =>
val res = varInput.conv(varKernel, rS, cS, tot)
res.sum()
}
val loss = gradR_loss(lossFun)(Tensor.zeros(1))
val resR = (iRow - kRow)/rS + 1
val resC = (iCol - kCol)/cS + 1
Tensor.assertEqual(loss, Tensor.scalar(resR * resC * 1.0f), "BACK 2 - LOSS")
Tensor.assertEqual(varKernel.d, Tensor.fill(resR * resC * 1.0f, kIn, kOut, kRow, kCol), "BACK 2 - KERNEL D")
}
}
cnn_back_test2.eval("abc")
}
test("cnn_back_test3") {
val cnn_back_test3 = new DslDriverC[String, Unit] with TensorExp with ScannerLowerExp {
@virtualize
def snippet(a: Rep[String]): Rep[Unit] = {
val iPane = 1
val iRow = 16
val iCol = 20
val input = Tensor.ones(iPane, iRow, iCol)
val kOut = 1
val kIn = iPane
val kRow = 3
val kCol = 3
val kernel = Tensor.fill((i: NSeq[Rep[Int]]) => if (i(2) == kRow/2 && i(3) == kCol/2) 1.0f else 0.0f, kOut, kIn, kRow, kCol)
val varInput = TensorR(input)
val varKernel = TensorR(kernel)
val rS = 2
val cS = 2
val tot = NewArray[Long](2)
def lossFun = { (dummy: TensorR) =>
val res = varInput.conv(varKernel, rS, cS, tot)
res.sum()
}
val loss = gradR_loss(lossFun)(Tensor.zeros(1))
val resR = (iRow - kRow)/rS + 1
val resC = (iCol - kCol)/cS + 1
Tensor.assertEqual(loss, Tensor.scalar(resR * resC * 1.0f), "BACK 2 - LOSS")
Tensor.assertEqual(varKernel.d, Tensor.fill(resR * resC * 1.0f, kIn, kOut, kRow, kCol), "BACK 2 - KERNEL D")
}
}
cnn_back_test3.eval("abc")
}
test("cnn_test4") {
val cnn_test4 = new DslDriverC[String, Unit] with TensorExp with ScannerLowerExp {