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 / tests / autodiff664 / tape-test.om

526 lines · 9195 bytes Raw Blame History
1note SPDX-License-Identifier: AGPL-3.0-only
2note Native tape tests. Expected values come from separate test code paths:
3note a test-local E1 tile, a sequential sum, an f64 widening reference and
4note IEEE identities. No expected value is a hand-entered constant.
5global QC ulong 1
6global QD uchar 24
7global QR ulong 1
8global QL f32 32
9global QF f32 4
10text QPASS "NATIVE_TAPE_PASS checks="
11text QFAIL "NATIVE_TAPE_FAIL check="
12text QNL "\n"
13
14fn qnum n ulong to void
15if eq n 0
16put QD 0 48
17w = write 1 QD 0 1
18ret
19end
20p = ulong 24
21while gt n 0 bound 24
22p = sub p 1
23put QD p uchar add 48 rem n 10
24n = div n 10
25end
26w = write 1 QD p sub 24 p
27if ne w long sub 24 p
28exit 90
29end
30end
31
32fn qck ok bool to void
33put QC 0 add get QC 0 1
34if not ok
35w = write 1 QFAIL 0 len QFAIL
36qnum get QC 0
37w = write 1 QNL 0 1
38exit 1
39end
40end
41
42fn qbits x f32 to uint
43ret bitcast uint x
44end
45
46fn qeq x f32 y f32 to bool
47ret eq qbits x qbits y
48end
49
50fn qnext to ulong
51x = get QR 0
52x = xor x shl x 13
53x = xor x shr x 7
54x = xor x shl x 17
55put QR 0 x
56ret x
57end
58
59note Random binary32 in +-[1,2) with a random 23-bit fraction, so products round.
60fn qrand to f32
61r = qnext
62one = qbits 1.0f32
63mask = sub shl ulong 1 23 ulong 1
64b = or one uint and r mask
65if ne and r 4294967296 0
66b = or b shl uint 1 31
67end
68ret bitcast f32 b
69end
70
71fn qpos to f32
72ret fabs qrand
73end
74
75note Test-local E1 tile over QL[0..n).
76fn qtree n ulong to f32
77for i ulong sub 32 n
78put QL add n i neg 0.0f32
79end
80w = ulong 32
81for s ulong 5
82w = shr w 1
83for i ulong w
84put QL i add get QL i get QL add i w
85end
86end
87ret get QL 0
88end
89
90fn qseq n ulong to f32
91s = neg 0.0f32
92for i ulong n
93s = add s get QL i
94end
95ret s
96end
97
98fn qfill id int to void
99n = ulong id
100for i ulong mul tprows n tpcols n
101st = tpset n i qrand
102end
103end
104
105fn qfillp id int to void
106n = ulong id
107for i ulong mul tprows n tpcols n
108st = tpset n i qpos
109end
110end
111
112note L = sum0(sum1(x * s)): seeds grad(x) with s exactly.
113fn qloss x int s int to int
114m = tpmul ulong x ulong s
115r = tpsum ulong m 1
116ret tpsum ulong r 0
117end
118
119fn trefuse to void
120tpreset
121a = tpleaf 2 3 1
122b = tpleaf 3 2 1
123qck eq a 0
124qck eq b 1
125qck eq tpadd ulong a ulong b -32
126qck eq tpmm ulong a ulong a -32
127qck eq tpleaf 5 13 1 -30
128qck eq tpleaf 0 3 1 -30
129qck eq tpsum ulong a 2 -32
130qck eq tpbcast ulong a 4 -32
131qck eq tpset ulong a 6 1.0f32 -32
132qck eq tpset 9 0 1.0f32 -32
133qck eq get TK 0 2
134k1 = tpleaf 1 33 1
135k2 = tpleaf 33 1 1
136qck eq tpmm ulong k1 ulong k2 -30
137qck eq get TK 0 4
138end
139
140fn tcap to void
141tpreset
142for i ulong 32
143qck eq tpleaf 1 1 1 int i
144end
145qck eq tpleaf 1 1 1 -30
146qck eq get TK 0 32
147end
148
149note An op on a node whose allocator snapshot is not live fails in M20 emcheck.
150fn tstale to void
151tpreset
152a = tpleaf 1 2 1
153b = tpleaf 1 2 1
154put TS mul ulong b 8 0
155st = tpadd ulong a ulong b
156qck lt st 0
157qck ne st -32
158qck eq get TK 0 2
159put TS mul ulong b 8 1
160qck eq tpadd ulong a ulong b 2
161end
162
163fn qmmref a ulong b ulong i ulong j ulong to f32
164k = tpcols a
165nn = tpcols b
166for q ulong k
167x = tpval a add mul i k q
168y = tpval b add mul q nn j
169put QL q mul x y
170end
171ret qtree k
172end
173
174fn qgaref s ulong b ulong i ulong q ulong to f32
175nn = tpcols s
176for j ulong nn
177x = tpval s add mul i nn j
178y = tpval b add mul q nn j
179put QL j mul x y
180end
181ret qtree nn
182end
183
184fn qgbref a ulong s ulong q ulong j ulong to f32
185k = tpcols a
186nn = tpcols s
187for i ulong tprows a
188x = tpval a add mul i k q
189y = tpval s add mul i nn j
190put QL i mul x y
191end
192ret qtree tprows a
193end
194
195fn tmm to void
196tpreset
197a = ulong tpleaf 3 5 1
198b = ulong tpleaf 5 4 1
199s = ulong tpleaf 3 4 0
200qfill int a
201qfill int b
202qfill int s
203p = ulong tpmm a b
204root = qloss int p int s
205qck ge root 0
206qck eq tpback ulong root 0
207for i ulong 3
208for j ulong 4
209qck qeq tpval p add mul i 4 j qmmref a b i j
210end
211end
212for i ulong 3
213for q ulong 5
214qck qeq tpgrad a add mul i 5 q qgaref s b i q
215end
216end
217for q ulong 5
218for j ulong 4
219qck qeq tpgrad b add mul q 4 j qgbref a s q j
220end
221end
222qck not tphasg s
223end
224
225note The tape's dot product must follow the E1 tile, not a sequential sum.
226fn torder to void
227differ = ulong 0
228for t ulong 64
229tpreset
230a = ulong tpleaf 1 20 0
231b = ulong tpleaf 20 1 0
232qfill int a
233qfill int b
234p = ulong tpmm a b
235for q ulong 20
236put QL q mul tpval a q tpval b q
237end
238want = qtree 20
239for q ulong 20
240put QL q mul tpval a q tpval b q
241end
242seq = qseq 20
243qck qeq tpval p 0 want
244if not qeq want seq
245differ = add differ 1
246end
247end
248qck gt differ 0
249end
250
251fn qnan to f32
252ret bitcast f32 or shl uint 255 23 shl uint 1 22
253end
254
255fn trelu to void
256tpreset
257x = ulong tpleaf 1 5 1
258s = ulong tpleaf 1 5 0
259qfillp int s
260st = tpset x 0 neg 1.0f32
261st = tpset x 1 neg 0.0f32
262st = tpset x 2 0.0f32
263st = tpset x 3 1.0f32
264st = tpset x 4 qnan
265r = ulong tprelu x
266root = qloss int r int s
267qck eq tpback ulong root 0
268for i ulong 5
269if eq i 3
270qck qeq tpval r i 1.0f32
271qck qeq tpgrad x i tpval s i
272else
273qck eq qbits tpval r i 0
274qck eq qbits tpgrad x i 0
275end
276end
277end
278
279fn tbcast to void
280tpreset
281b = ulong tpleaf 1 3 1
282s = ulong tpleaf 4 3 0
283qfill int b
284qfill int s
285bb = ulong tpbcast b 4
286for i ulong 4
287for j ulong 3
288qck qeq tpval bb add mul i 3 j tpval b j
289end
290end
291root = qloss int bb int s
292qck eq tpback ulong root 0
293for j ulong 3
294for i ulong 4
295put QL i tpval s add mul i 3 j
296end
297qck qeq tpgrad b j qtree 4
298end
299end
300
301note x*x and x+x: second contribution is added to the stored first one.
302fn tfanin to void
303tpreset
304x = ulong tpleaf 2 2 1
305qfill int x
306m = ulong tpmul x x
307r = ulong tpsum m 1
308root = ulong tpsum r 0
309qck eq tpback root 0
310for i ulong 4
311v = tpval x i
312qck qeq tpgrad x i add v v
313end
314tpreset
315y = ulong tpleaf 1 3 1
316s = ulong tpleaf 1 3 0
317qfill int y
318st = tpset s 0 neg 0.0f32
319st = tpset s 1 qrand
320st = tpset s 2 qrand
321d = ulong tpadd y y
322root = ulong qloss int d int s
323qck eq tpback root 0
324for i ulong 3
325w = tpval s i
326qck qeq tpgrad y i add w w
327end
328qck eq qbits tpgrad y 0 qbits neg 0.0f32
329end
330
331fn tsub to void
332tpreset
333a = ulong tpleaf 2 3 1
334b = ulong tpleaf 2 3 1
335s = ulong tpleaf 2 3 0
336qfill int a
337qfill int b
338qfill int s
339d = ulong tpsub a b
340for i ulong 6
341qck qeq tpval d i sub tpval a i tpval b i
342end
343root = ulong qloss int d int s
344qck eq tpback root 0
345for i ulong 6
346qck qeq tpgrad a i tpval s i
347qck qeq tpgrad b i neg tpval s i
348end
349end
350
351fn tsums to void
352tpreset
353x = ulong tpleaf 3 4 0
354qfill int x
355r1 = ulong tpsum x 1
356c1 = ulong tpsum r1 0
357c0 = ulong tpsum x 0
358r0 = ulong tpsum c0 1
359for i ulong 3
360for j ulong 4
361put QL j tpval x add mul i 4 j
362end
363put QF i qtree 4
364end
365for i ulong 3
366qck qeq tpval r1 i get QF i
367put QL i get QF i
368end
369qck qeq tpval c1 0 qtree 3
370for j ulong 4
371for i ulong 3
372put QL i tpval x add mul i 4 j
373end
374qck qeq tpval c0 j qtree 3
375end
376for j ulong 4
377put QL j tpval c0 j
378end
379qck qeq tpval r0 0 qtree 4
380end
381
382fn tstate to void
383tpreset
384x = ulong tpleaf 1 2 1
385qck eq tpback x -33
386tpreset
387z = ulong tpleaf 1 1 0
388qck eq tpback z -33
389tpreset
390y = ulong tpleaf 1 1 1
391qck eq tpback y 0
392qck eq tpback y -31
393qck eq tpleaf 1 1 1 -31
394qck qeq tpgrad y 0 1.0f32
395end
396
397note No contraction: a*b + c with c = -round(a*b) is exactly +0 unfused,
398note while the f64 widening of the exact product leaves the rounding error.
399fn qfma a f32 b f32 c f32 to f32
400ret add mul a b c
401end
402
403fn tnofma to void
404seen = ulong 0
405for t ulong 64
406a = qrand
407b = qrand
408put QF 0 mul a b
409c = neg get QF 0
410got = qfma a b c
411wide = f32 add mul f64 a f64 b f64 c
412qck eq qbits got 0
413if ne qbits wide 0
414seen = add seen 1
415end
416end
417qck gt seen 0
418end
419
420note Signed-zero padding: every reduction of n < 32 inputs that are all -0
421note must give -0 (bits 0x80000000), because the pad lanes are -0 and
422note -0 + -0 = -0. A +0 pad gives +0 (-0 + +0 = +0 under round to nearest).
423note Random values cannot see the pad, so the checks above do not catch it.
424fn qnz x f32 to bool
425ret eq qbits x qbits neg 0.0f32
426end
427
428fn qfilln id int to void
429n = ulong id
430for i ulong mul tprows n tpcols n
431st = tpset n i neg 0.0f32
432end
433end
434
435fn tnzsum to void
436qck eq qbits neg 0.0f32 shl uint 1 31
437qck ne qbits 0.0f32 qbits neg 0.0f32
438tpreset
439x = ulong tpleaf 3 5 0
440qfilln int x
441r1 = ulong tpsum x 1
442c0 = ulong tpsum x 0
443t = ulong tpsum r1 0
444for i ulong 3
445qck qnz tpval r1 i
446end
447for j ulong 5
448qck qnz tpval c0 j
449end
450qck qnz tpval t 0
451end
452
453note Matmul forward over k and its backward reductions over j (gA) and i (gB).
454note -0 times a positive value is -0; s = -0 makes the seeded gradient -0.
455fn tnzmm to void
456tpreset
457a = ulong tpleaf 2 3 1
458b = ulong tpleaf 3 4 1
459s = ulong tpleaf 2 4 0
460qfilln int a
461qfillp int b
462qfilln int s
463p = ulong tpmm a b
464root = ulong qloss int p int s
465qck eq tpback root 0
466for i ulong 8
467qck qnz tpval p i
468end
469for i ulong 6
470qck qnz tpgrad a i
471end
472tpreset
473a = ulong tpleaf 2 3 1
474b = ulong tpleaf 3 4 1
475s = ulong tpleaf 2 4 0
476qfillp int a
477qfillp int b
478qfilln int s
479p = ulong tpmm a b
480root = ulong qloss int p int s
481qck eq tpback root 0
482for i ulong 6
483qck qnz tpgrad a i
484end
485for i ulong 12
486qck qnz tpgrad b i
487end
488end
489
490note Broadcast backward sums g over rows with the E1 tile.
491fn tnzbc to void
492tpreset
493b = ulong tpleaf 1 3 1
494s = ulong tpleaf 4 3 0
495qfillp int b
496qfilln int s
497bb = ulong tpbcast b 4
498root = ulong qloss int bb int s
499qck eq tpback root 0
500for j ulong 3
501qck qnz tpgrad b j
502end
503end
504
505fn main to int
506put QR 0 88172645463325252
507trefuse
508tcap
509tstale
510tmm
511torder
512trelu
513tbcast
514tfanin
515tsub
516tsums
517tstate
518tnofma
519tnzsum
520tnzmm
521tnzbc
522w = write 1 QPASS 0 len QPASS
523qnum get QC 0
524w = write 1 QNL 0 1
525ret 0
526end