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 / train / blas / om / blas_test.om

308 lines · 7135 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note train/blas/om/blas_test.om: pure-Omega functional verification and negative controls for omega-blas (blas.om).
3note Compose after train/blas/om/blas.om (which begins with om2).
4
5text bt_pass "PASS omega-blas host verification (formats, gelu, pack/unpack, gemm, gemv, copy32, info)\n"
6text bt_fail "FAIL omega-blas host verification\n"
7
8fn bt_f32_near a uint b uint tol f32 to bool
9fa = bitcast f32 a
10fb = bitcast f32 b
11d = fabs sub fa fb
12ret le d tol
13end
14
15fn bt_test_formats to bool
16note f16: 1.0 = 0x3c00, -2.0 = 0xc000, 0.5 = 0x3800, 0.0 = 0x0000
17s16 = new ushort 4 0
18put s16 0 ushort 0x3c00
19put s16 1 ushort 0xc000
20put s16 2 ushort 0x3800
21put s16 3 ushort 0x0000
22d32 = new uint 4 0
23widen16 ulong 0 s16 d32
24if ne get d32 0 bitcast uint 1.0f32
25ret false
26end
27if ne get d32 1 bitcast uint -2.0f32
28ret false
29end
30if ne get d32 2 bitcast uint 0.5f32
31ret false
32end
33if ne get d32 3 bitcast uint 0.0f32
34ret false
35end
36note round-trip narrow16 f16
37r16 = new ushort 4 0
38narrow16 ulong 0 d32 r16
39for i ulong 4
40if ne get r16 i get s16 i
41ret false
42end
43end
44note bf16: 1.0 = 0x3f80, -3.5 = 0xc060
45put s16 0 ushort 0x3f80
46put s16 1 ushort 0xc060
47put s16 2 ushort 0x0000
48put s16 3 ushort 0x3f00
49widen16 ulong 1 s16 d32
50if ne get d32 0 bitcast uint 1.0f32
51ret false
52end
53if ne get d32 1 bitcast uint -3.5f32
54ret false
55end
56if ne get d32 2 bitcast uint 0.0f32
57ret false
58end
59if ne get d32 3 bitcast uint 0.5f32
60ret false
61end
62narrow16 ulong 1 d32 r16
63for i ulong 4
64if ne get r16 i get s16 i
65ret false
66end
67end
68note fp8 e4m3: 1.0 = 0x38 (exp=7, bias=7, man=0), -2.0 = 0xc0 (sign=1, exp=8, man=0)
69s8 = new uchar 2 0
70put s8 0 uchar 0x38
71put s8 1 uchar 0xc0
72d8 = new uint 2 0
73widen8 ulong 0 s8 d8
74if ne get d8 0 bitcast uint 1.0f32
75ret false
76end
77if ne get d8 1 bitcast uint -2.0f32
78ret false
79end
80note fp8 e5m2: 1.0 = 0x3c (exp=15, bias=15, man=0), -2.0 = 0xc0 (sign=1, exp=16, man=0)
81put s8 0 uchar 0x3c
82put s8 1 uchar 0xc0
83widen8 ulong 1 s8 d8
84if ne get d8 0 bitcast uint 1.0f32
85ret false
86end
87if ne get d8 1 bitcast uint -2.0f32
88ret false
89end
90ret true
91end
92
93fn bt_test_gelu to bool
94in_v = new uint 3 0
95out_v = new uint 3 0
96put in_v 0 bitcast uint 0.0f32
97put in_v 1 bitcast uint 1.0f32
98put in_v 2 bitcast uint -1.0f32
99gelu32 in_v out_v
100note gelu(0) = 0, gelu(1) ~= 0.841192, gelu(-1) ~= -0.158808
101if not bt_f32_near get out_v 0 bitcast uint 0.0f32 0.0001f32
102ret false
103end
104if not bt_f32_near get out_v 1 bitcast uint 0.841192f32 0.001f32
105ret false
106end
107if not bt_f32_near get out_v 2 bitcast uint -0.158808f32 0.001f32
108ret false
109end
110ret true
111end
112
113fn bt_test_gemm to bool
114note 2x2 column-major A = [[1, 3], [2, 4]] -> [1, 2, 3, 4]
115note 2x2 column-major B = [[5, 7], [6, 8]] -> [5, 6, 7, 8]
116note A*B = [[1*5+3*6, 1*7+3*8], [2*5+4*6, 2*7+4*8]] = [[23, 31], [34, 46]] -> col-major [23, 34, 31, 46]
117a = new uint 4 0
118put a 0 bitcast uint 1.0f32
119put a 1 bitcast uint 2.0f32
120put a 2 bitcast uint 3.0f32
121put a 3 bitcast uint 4.0f32
122b = new uint 4 0
123put b 0 bitcast uint 5.0f32
124put b 1 bitcast uint 6.0f32
125put b 2 bitcast uint 7.0f32
126put b 3 bitcast uint 8.0f32
127w = new uint 16 0
128pka = new uchar 64 0
129put pka 0 uchar 2
130put pka 8 uchar 2
131put pka 16 uchar 1
132put pka 32 uchar 2
133put pka 40 uchar 2
134pack32 pka a w
135note pack B^T (rows=n=2, cols=k=2, si=ld=2, sq=1, dst=4, w=2*2-2=2)
136pkb = new uchar 64 0
137put pkb 0 uchar 2
138put pkb 8 uchar 2
139put pkb 16 uchar 2
140put pkb 32 uchar 1
141put pkb 40 uchar 2
142put pkb 48 uchar 4
143pack32 pkb b w
144note gemm_k: m=2, n=2, k=2, ldc=2, offc=0, alpha=1.0, beta=0.0, act=0, wsa=0, wsb=4, mp=1, np=1
145c = new uint 4 0
146gp_buf = new uchar 128 0
147put gp_buf 0 uchar 2
148put gp_buf 8 uchar 2
149put gp_buf 16 uchar 2
150put gp_buf 24 uchar 2
151note alpha = 1.0f32 = 0x3f800000 little endian at offset 40
152put gp_buf 40 uchar 0x00
153put gp_buf 41 uchar 0x00
154put gp_buf 42 uchar 0x80
155put gp_buf 43 uchar 0x3f
156note wsb = 4 at offset 64, mp = 1 at 80, np = 1 at 88
157put gp_buf 64 uchar 4
158put gp_buf 80 uchar 1
159put gp_buf 88 uchar 1
160gemm_k gp_buf w c
161if not bt_f32_near get c 0 bitcast uint 23.0f32 0.001f32
162ret false
163end
164if not bt_f32_near get c 1 bitcast uint 34.0f32 0.001f32
165ret false
166end
167if not bt_f32_near get c 2 bitcast uint 31.0f32 0.001f32
168ret false
169end
170if not bt_f32_near get c 3 bitcast uint 46.0f32 0.001f32
171ret false
172end
173note Re-run with beta = 2.0f32 (0x40000000) and alpha = 1.0f32 -> 3 * C = [69, 102, 93, 138]
174put gp_buf 44 uchar 0x00
175put gp_buf 45 uchar 0x00
176put gp_buf 46 uchar 0x00
177put gp_buf 47 uchar 0x40
178gemm_k gp_buf w c
179if not bt_f32_near get c 0 bitcast uint 69.0f32 0.001f32
180ret false
181end
182if not bt_f32_near get c 3 bitcast uint 138.0f32 0.001f32
183ret false
184end
185ret true
186end
187
188fn bt_test_gemv_copy to bool
189note 2x2 col-major A = [[1, 3], [2, 4]], x = [10, 20], y = [1, 2], alpha=1, beta=1
190note y_new = [1 + 1*10 + 3*20, 2 + 2*10 + 4*20] = [71, 102]
191a = new uint 4 0
192put a 0 bitcast uint 1.0f32
193put a 1 bitcast uint 2.0f32
194put a 2 bitcast uint 3.0f32
195put a 3 bitcast uint 4.0f32
196w = new uint 8 0
197put w 0 bitcast uint 10.0f32
198put w 1 bitcast uint 20.0f32
199put w 2 bitcast uint 1.0f32
200put w 3 bitcast uint 2.0f32
201gv_buf = new uchar 80 0
202put gv_buf 0 uchar 2
203put gv_buf 8 uchar 2
204put gv_buf 16 uchar 2
205note alpha = 1.0f32 at 40, beta = 1.0f32 at 44, wx=0 at 48, wy=2 at 56
206put gv_buf 42 uchar 0x80
207put gv_buf 43 uchar 0x3f
208put gv_buf 46 uchar 0x80
209put gv_buf 47 uchar 0x3f
210put gv_buf 56 uchar 2
211gemv_k gv_buf a w
212if not bt_f32_near get w 2 bitcast uint 71.0f32 0.001f32
213ret false
214end
215if not bt_f32_near get w 3 bitcast uint 102.0f32 0.001f32
216ret false
217end
218note copy32 reverse stride: d[0..2] = a[2, 0]
219d = new uint 2 0
220cp_buf = new uchar 48 0
221put cp_buf 0 uchar 2
222put cp_buf 8 uchar 2
223note is = -2 (0xfffffffffffffffe) at offset 16
224put cp_buf 16 uchar 0xfe
225for k ulong 7
226put cp_buf add k ulong 17 uchar 0xff
227end
228note od = 0 at 24, id = 1 at 32
229put cp_buf 32 uchar 1
230copy32 cp_buf a d
231if ne get d 0 get a 2
232ret false
233end
234if ne get d 1 get a 0
235ret false
236end
237ret true
238end
239
240fn bt_test_info to bool
241note Valid gemm_info: ta='N'(78), tb='T'(84), m=4, n=4, k=4, lda=4, ldb=4, ldc=4 -> 0
242ck_buf = new uchar 80 0
243put ck_buf 0 uchar 78
244put ck_buf 8 uchar 84
245put ck_buf 16 uchar 4
246put ck_buf 24 uchar 4
247put ck_buf 32 uchar 4
248put ck_buf 40 uchar 4
249put ck_buf 48 uchar 4
250put ck_buf 56 uchar 4
251put ck_buf 64 uchar 1
252put ck_buf 72 uchar 1
253if ne gemm_info ck_buf ulong 0
254ret false
255end
256note Negative control: bad ta='X'(88) -> info 1
257put ck_buf 0 uchar 88
258if ne gemm_info ck_buf ulong 1
259ret false
260end
261note Restore ta='N', bad lda=2 < m=4 -> info 8
262put ck_buf 0 uchar 78
263put ck_buf 40 uchar 2
264if ne gemm_info ck_buf ulong 8
265ret false
266end
267note gemv_info: restore lda=4 -> 0; incx=0 -> info 8
268put ck_buf 40 uchar 4
269if ne gemv_info ck_buf ulong 0
270ret false
271end
272put ck_buf 64 uchar 0
273if ne gemv_info ck_buf ulong 8
274ret false
275end
276ret true
277end
278
279fn main to int
280ok = true
281if not bt_test_formats
282ok = false
283end
284if not bt_test_gelu
285ok = false
286end
287if not bt_test_gemm
288ok = false
289end
290if not bt_test_gemv_copy
291ok = false
292end
293if not bt_test_info
294ok = false
295end
296if not ok
297k = write int 1 bt_fail ulong 0 len bt_fail
298if lt k long 0
299ret int 1
300end
301ret int 1
302end
303k = write int 1 bt_pass ulong 0 len bt_pass
304if lt k long 0
305ret int 1
306end
307ret int 0
308end