interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / src / autodiff / test_autodiff.om

185 lines · 5018 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note test_autodiff.om -- Pure seeded OM-2 M21 OMEGA_AUTODIFF reverse-mode tape tests.
3
4global tad_c ulong 2
5
6text tad_pass "M21_OMEGA_AUTODIFF_TESTS_PASS\n"
7text tad_fail "M21_OMEGA_AUTODIFF_TESTS_FAIL\n"
8
9fn ad_chk cond bool to void
10 put tad_c 0 add get tad_c 0 1
11 if not cond
12 put tad_c 1 add get tad_c 1 1
13 end
14end
15
16fn ad_near a f32 b f32 tol f32 to bool
17 ret le fabs sub a b tol
18end
19
20fn test_basic_ops_verify ia ulong ib ulong to void
21 ga = new f32 3 0.0f32
22 gb = new f32 3 0.0f32
23 ad_chk eq ad_grad_read_f32 ia ga AD_OK
24 ad_chk eq ad_grad_read_f32 ib gb AD_OK
25 ad_chk ad_near get ga 1 1.75f32 0.0001f32
26 ad_chk ad_near get gb 1 5.0f32 0.0001f32
27 ad_chk ad_near get ga 2 2.8333333f32 0.0001f32
28 ad_chk ad_near get gb 2 10.0f32 0.0001f32
29 ids = new ulong 2 0
30 put ids 0 ia
31 put ids 1 ib
32 flat = new f32 6 0.0f32
33 ad_chk eq ad_grads_flatten ids flat AD_OK
34 ad_chk ad_near get flat 1 1.75f32 0.0001f32
35 ad_chk ad_near get flat 4 5.0f32 0.0001f32
36end
37
38fn test_basic_ops to void
39 ad_chk eq ot_ctx_init 128 OT_OK
40 ad_chk eq ad_tape_init 64 1024 AD_OK
41 sh = new ulong 8 0
42 put sh 0 3
43 da = new f32 3 0.0f32
44 db = new f32 3 0.0f32
45 put da 0 2.0f32
46 put da 1 4.0f32
47 put da 2 9.0f32
48 put db 0 3.0f32
49 put db 1 2.0f32
50 put db 2 3.0f32
51 ha = new ulong 2 0
52 hb = new ulong 2 0
53 ad_chk eq ot_from_f32 1 sh da ha OT_OK
54 ad_chk eq ot_from_f32 1 sh db hb OT_OK
55 ids = new ulong 8 0
56 tmp = new ulong 1 0
57 ad_chk eq ad_leaf get ha 0 get ha 1 1 tmp AD_OK
58 put ids 0 get tmp 0
59 ad_chk eq ad_leaf get hb 0 get hb 1 1 tmp AD_OK
60 put ids 1 get tmp 0
61 ad_chk eq ad_binary AD_MUL get ids 0 get ids 1 tmp AD_OK
62 put ids 2 get tmp 0
63 ad_chk eq ad_sqrt get ids 0 tmp AD_OK
64 put ids 3 get tmp 0
65 ad_chk eq ad_binary AD_ADD get ids 3 get ids 2 tmp AD_OK
66 put ids 4 get tmp 0
67 ad_chk eq ad_binary AD_DIV get ids 0 get ids 1 tmp AD_OK
68 put ids 5 get tmp 0
69 ad_chk eq ad_binary AD_SUB get ids 4 get ids 5 tmp AD_OK
70 put ids 6 get tmp 0
71 ad_chk eq ad_reduce AD_SUM get ids 6 0 0 tmp AD_OK
72 put ids 7 get tmp 0
73 ad_chk eq ad_backward get ids 7 AD_OK
74 ad_chk eq ad_backward get ids 7 AD_ERR_STATE
75 test_basic_ops_verify get ids 0 get ids 1
76 ad_tape_release
77 r1 = ot_release get ha 0 get ha 1
78 r1 = ot_release get hb 0 get hb 1
79 lc = new ulong 2 0
80 ot_live_counts lc
81 ad_chk and eq get lc 0 0 eq get lc 1 0
82end
83
84fn test_matmul_max_mean_bcast to void
85 ad_chk eq ot_ctx_init 128 OT_OK
86 ad_chk eq ad_tape_init 64 1024 AD_OK
87 sha = new ulong 8 0
88 shb = new ulong 8 0
89 shc = new ulong 8 0
90 put sha 0 2
91 put sha 1 2
92 put shb 0 2
93 put shb 1 2
94 put shc 0 2
95 da = new f32 4 0.0f32
96 db = new f32 4 0.0f32
97 dc = new f32 2 0.0f32
98 put da 0 1.0f32
99 put da 1 2.0f32
100 put da 2 3.0f32
101 put da 3 4.0f32
102 put db 0 2.0f32
103 put db 1 -1.0f32
104 put db 2 0.0f32
105 put db 3 3.0f32
106 put dc 0 0.0f32
107 put dc 1 5.0f32
108 ha = new ulong 2 0
109 hb = new ulong 2 0
110 hc = new ulong 2 0
111 ad_chk eq ot_from_f32 2 sha da ha OT_OK
112 ad_chk eq ot_from_f32 2 shb db hb OT_OK
113 ad_chk eq ot_from_f32 1 shc dc hc OT_OK
114 ids = new ulong 8 0
115 tmp = new ulong 1 0
116 ad_chk eq ad_leaf get ha 0 get ha 1 1 tmp AD_OK
117 put ids 0 get tmp 0
118 ad_chk eq ad_leaf get hb 0 get hb 1 1 tmp AD_OK
119 put ids 1 get tmp 0
120 ad_chk eq ad_leaf get hc 0 get hc 1 1 tmp AD_OK
121 put ids 2 get tmp 0
122 ad_chk eq ad_matmul get ids 0 get ids 1 tmp AD_OK
123 put ids 3 get tmp 0
124 ad_chk eq ad_binary AD_MAX get ids 3 get ids 2 tmp AD_OK
125 put ids 4 get tmp 0
126 ad_chk eq ad_reduce AD_MEAN get ids 4 1 0 tmp AD_OK
127 put ids 5 get tmp 0
128 ad_chk eq ad_reduce AD_MEAN get ids 5 0 0 tmp AD_OK
129 put ids 6 get tmp 0
130 ad_chk eq ad_backward get ids 6 AD_OK
131 gc = new f32 2 0.0f32
132 ad_chk eq ad_grad_read_f32 get ids 2 gc AD_OK
133 ad_chk ad_near get gc 0 0.0f32 0.0001f32
134 ad_chk ad_near get gc 1 0.25f32 0.0001f32
135 ad_tape_release
136end
137
138fn test_transpose_and_seed to void
139 ad_chk eq ot_ctx_init 128 OT_OK
140 ad_chk eq ad_tape_init 64 1024 AD_OK
141 sh = new ulong 8 0
142 put sh 0 2
143 put sh 1 3
144 dat = new f32 6 0.0f32
145 for i ulong 6
146 put dat i f32 add i 1
147 end
148 ha = new ulong 2 0
149 ad_chk eq ot_from_f32 2 sh dat ha OT_OK
150 ia = new ulong 1 0
151 it = new ulong 1 0
152 ad_chk eq ad_leaf get ha 0 get ha 1 1 ia AD_OK
153 ad_chk eq ad_transpose get ia 0 it AD_OK
154 shs = new ulong 8 0
155 put shs 0 3
156 put shs 1 2
157 sd = new f32 6 0.0f32
158 for i ulong 6
159 put sd i f32 add i 10
160 end
161 hs = new ulong 2 0
162 ad_chk eq ot_from_f32 2 shs sd hs OT_OK
163 ad_chk eq ad_backward_seed get it 0 get hs 0 get hs 1 AD_OK
164 ga = new f32 6 0.0f32
165 ad_chk eq ad_grad_read_f32 get ia 0 ga AD_OK
166 ad_chk eq get ga 0 10.0f32
167 ad_chk eq get ga 1 12.0f32
168 ad_chk eq get ga 2 14.0f32
169 ad_chk eq get ga 3 11.0f32
170 ad_chk eq get ga 4 13.0f32
171 ad_chk eq get ga 5 15.0f32
172 ad_tape_release
173end
174
175fn main to int
176 test_basic_ops
177 test_matmul_max_mean_bcast
178 test_transpose_and_seed
179 if ne get tad_c 1 0
180 w = write 1 tad_fail 0 len tad_fail
181 ret 1
182 end
183 w = write 1 tad_pass 0 len tad_pass
184 ret 0
185end