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 / seed / tilelab / tilelab.om

435 lines · 8569 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note seed/tilelab/tilelab.om: OmegaTileLab. Native tile planner and streaming-softmax algebra (host program, seeded Omega).
3note Run: tilelab 1 M N K [REGS] plan a BF16 GEMM tile schedule for CUDApp omega_gpu_gemm (rt ct wm wn band)
4note tilelab 2 [MUT] streaming (m,s,p) merge over 32 scores vs the direct softmax-weighted sum, f64
5note MUT 0 correct, 1 tile-local normalisation, 2 merge without rescale, 3 wrong tree order
6note Exit: 0 ok, 1 numeric tolerance failed, 2 refused (shape, limit or no configuration fits), 3 usage.
7note The GEMM model is calibrated on GB10 chip timings of the CUDApp kernel (no shared memory: each warp loads its own
8note 16rt x 8ct operands, band orders CTAs in column bands so packed B stays in L2). BM = 16 rt wm, BN = 8 ct wn,
9note Kp = K rounded up to 32, mt = ceil(M/BM), nt = ceil(N/BN). l2_bytes = 2 Kp (M nt + N mt) (operand bytes per CTA tile);
10note dram_bytes = 2 M Kp ceil(nt/band) + 2 Kp N when band >= 2 and the band of B, 2 Kp min(N, band BN) bytes, fits a
11note 32 MiB L2 budget; otherwise (band < 2 or the band overflows) 2 M Kp nt + 2 Kp N mt.
12note cost_ns = l2_bytes / 1827.5440 + 2 M N Kp / 37166.0364 + dram_bytes / 1890.5139, constants and the 32 MiB budget
13note fitted to 96 GB10 sweep medians (seed/tilelab/WOLFRAM.md). An empirical RANKING, not a time prediction and not
14note physical bandwidths. Held out per shape, its top 2 plus the default reach the best of 32 configurations on two of
15note three shapes and come within 1.9% on the third, so the planner names the top 2 and the CUDApp default
16note (rt2 ct4 wm2 wn4 band8) as `measure` lines and the chip decides between them.
17om2
18text msg "tilelab: usage: tilelab 1 M N K [REGS] | tilelab 2 [MUT]\nrefused: dimension zero or above 16384\nrefused: no configuration fits the register budget\ncfg rt=\n ct=\n wm=\n wn=\n band=\n bm=\n bn=\n regs=\n ctas=\n l2_bytes=\n cost_ns=\npick \nrefused: unknown mode\nsmx direct_plus100_e9=\n seq_err_ppt=\n tree_err_ppt=\n one_err_ppt=\n tol_ppt=\nsmx FAIL\nsmx OK\nmut=\nmeasure \n dram_bytes=\n"
19global dig uchar 32
20global ab uchar 256
21global argbad uchar 1
22global xs f64 32
23global vs f64 32
24global ms f64 64
25global ss f64 64
26global ps f64 64
27
28note ---- output ----
29fn say id ulong to void
30k = ulong 1
31s = ulong 0
32for i ulong len msg
33if eq get msg i 10
34if eq k id
35r = write 1 msg s sub i s
36ret
37end
38k = add k 1
39s = add i 1
40end
41end
42exit 99
43end
44fn newline to void
45put dig 0 10
46r = write 1 dig 0 1
47end
48fn num v ulong to void
49x = v
50n = ulong 0
51while ge x 10 bound 64
52put dig n uchar add 48 rem x 10
53x = div x 10
54n = add n 1
55end
56put dig n uchar add 48 x
57n = add n 1
58for i ulong n
59r = write 1 dig sub sub n i 1 1
60end
61end
62
63fn argdec i ulong to ulong
64note Keep parse status separate: every ulong, including the old failure sentinel, is valid decimal input.
65put argbad 0 0
66r = argread i ab 0 20
67if lt r 1
68put argbad 0 1
69ret 0xffffffffffffffff
70end
71v = ulong 0
72for k ulong ulong r
73c = get ab k
74if or lt c 48 gt c 57
75put argbad 0 1
76ret 0xffffffffffffffff
77end
78digit = ulong sub c 48
79note Refuse before either unsigned operation could wrap.
80if gt v div sub ulong 0xffffffffffffffff digit 10
81put argbad 0 1
82ret 0xffffffffffffffff
83end
84v = add mul v 10 digit
85end
86ret v
87end
88note ---- planner ----
89fn cdiv a ulong b ulong to ulong
90ret div add a sub b 1 b
91end
92fn pw2 v ulong to ulong
93ret shl ulong 1 v
94end
95fn crt i ulong to ulong
96ret pw2 rem i 3
97end
98fn cct i ulong to ulong
99ret pw2 rem div i 3 3
100end
101fn cwm i ulong to ulong
102ret pw2 rem div i 9 3
103end
104fn cwn i ulong to ulong
105ret pw2 rem div i 27 3
106end
107fn cband i ulong to ulong
108bj = div i 81
109if eq bj 0
110ret 0
111end
112ret pw2 add bj 1
113end
114fn cregs i ulong to ulong
115rt = crt i
116ct = cct i
117ret mul 4 add add mul 4 rt mul 2 ct mul rt ct
118end
119fn cbm i ulong to ulong
120ret mul mul 16 crt i cwm i
121end
122fn cbn i ulong to ulong
123ret mul mul 8 cct i cwn i
124end
125fn cctas i ulong m ulong n ulong to ulong
126ret mul cdiv m cbm i cdiv n cbn i
127end
128fn cl2 i ulong m ulong n ulong kp ulong to ulong
129x = mul m cdiv n cbn i
130y = mul n cdiv m cbm i
131ret mul mul 2 kp add x y
132end
133fn cdram i ulong m ulong n ulong kp ulong to ulong
134a = mul mul 2 m kp
135b = mul mul 2 kp n
136nt = cdiv n cbn i
137bd = cband i
138bw = mul bd cbn i
139if gt bw n
140bw = n
141end
142bb = mul mul 2 kp bw
143if or lt bd 2 gt bb 33554432
144ret add mul a nt mul b cdiv m cbm i
145end
146ret add mul a cdiv nt bd b
147end
148fn ccost i ulong m ulong n ulong kp ulong to f64
149t2 = div f64 cl2 i m n kp 1827.5440f64
150tc = div f64 mul mul 2 m mul n kp 37166.0364f64
151td = div f64 cdram i m n kp 1890.5139f64
152ret add add t2 tc td
153end
154fn show i ulong m ulong n ulong kp ulong to void
155say 4
156num crt i
157say 5
158num cct i
159say 6
160num cwm i
161say 7
162num cwn i
163say 8
164num cband i
165say 9
166num cbm i
167say 10
168num cbn i
169say 11
170num cregs i
171say 12
172num cctas i m n
173say 13
174num cl2 i m n kp
175say 26
176num cdram i m n kp
177say 14
178num ulong ccost i m n kp
179newline
180end
181fn cfgline i ulong to void
182say 4
183num crt i
184say 5
185num cct i
186say 6
187num cwm i
188say 7
189num cwn i
190say 8
191num cband i
192newline
193end
194fn pick bi ulong to void
195say 15
196cfgline bi
197end
198fn measure i ulong to void
199say 25
200cfgline i
201end
202fn plan m ulong n ulong k ulong regs ulong to int
203if or or eq m 0 eq n 0 eq k 0
204say 2
205newline
206ret 2
207end
208if or or gt m 16384 gt n 16384 gt k 16384
209say 2
210newline
211ret 2
212end
213kp = mul cdiv k 32 32
214best = 1.0e300f64
215next = 1.0e300f64
216bi = ulong 9999
217b2 = ulong 9999
218for i ulong 405
219if le cregs i regs
220show i m n kp
221c = ccost i m n kp
222if lt c best
223next = best
224b2 = bi
225best = c
226bi = i
227else
228if lt c next
229next = c
230b2 = i
231end
232end
233end
234end
235if eq bi 9999
236say 3
237newline
238ret 2
239end
240pick bi
241measure bi
242if ne b2 9999
243measure b2
244end
245dflt = ulong 232
246if and le cregs dflt regs and ne dflt bi ne dflt b2
247measure dflt
248end
249ret 0
250end
251
252note ---- streaming softmax ----
253note exp by argument halving: exp(y) = exp(y / 256)^256, Taylor to 14 terms
254fn ex y f64 to f64
255z = div y 256.0f64
256t = 1.0f64
257s = 1.0f64
258for i uint 14
259t = div mul t z f64 add i 1
260s = add s t
261end
262for j uint 8
263s = mul s s
264end
265ret s
266end
267fn vabs d f64 to f64
268if lt d 0.0f64
269ret sub 0.0f64 d
270end
271ret d
272end
273fn vmax x1 f64 x2 f64 to f64
274if gt x1 x2
275ret x1
276end
277ret x2
278end
279note summary of tile t over scores [lo, hi): m = max, s = sum e^(x-m), p = sum v e^(x-m)
280fn tile t uint lo uint hi uint to void
281m = get xs lo
282for i uint sub hi lo
283m = vmax m get xs add lo i
284end
285s = 0.0f64
286p = 0.0f64
287for i uint sub hi lo
288w = ex sub get xs add lo i m
289s = add s w
290p = add p mul w get vs add lo i
291end
292put ms t m
293put ss t s
294put ps t p
295end
296note merge summary b into a (slot a), mutant 2 skips the rescale
297fn merge a uint b uint mut ulong to void
298m1 = get ms a
299m2 = get ms b
300m = vmax m1 m2
301e1 = ex sub m1 m
302e2 = ex sub m2 m
303if eq mut 2
304e1 = 1.0f64
305e2 = 1.0f64
306end
307put ss a add mul e1 get ss a mul e2 get ss b
308put ps a add mul e1 get ps a mul e2 get ps b
309put ms a m
310end
311fn ppt d f64 r f64 to ulong
312e = div vabs d vabs r
313if not lt e 1.0e30f64
314ret 0xffffffffffffffff
315end
316ret ulong mul e 1.0e12f64
317end
318fn fixture to void
319for i uint 32
320put xs i div sub f64 rem add mul i 37 11 64 32.0f64 4.0f64
321put vs i div sub f64 rem add mul i 53 7 41 20.0f64 8.0f64
322end
323end
324fn direct to f64
325m = get xs 0
326for i uint 32
327m = vmax m get xs i
328end
329s = 0.0f64
330p = 0.0f64
331for i uint 32
332w = ex sub get xs i m
333s = add s w
334p = add p mul w get vs i
335end
336ret div p s
337end
338fn tiles base uint to void
339for t uint 4
340tile add base t mul t 8 add mul t 8 8
341end
342end
343fn seqfold mut ulong to f64
344tiles 0
345merge 0 1 mut
346merge 0 2 mut
347merge 0 3 mut
348ret div get ps 0 get ss 0
349end
350fn treefold mut ulong to f64
351tiles 8
352merge 8 9 mut
353merge 10 11 mut
354if eq mut 3
355merge 9 11 mut
356else
357merge 8 10 mut
358end
359ret div get ps 8 get ss 8
360end
361note mutant 1: each tile normalised on its own, the four outputs averaged
362fn localnorm to f64
363tiles 16
364one = 0.0f64
365for t uint 4
366one = add one div div get ps add 16 t get ss add 16 t 4.0f64
367end
368ret one
369end
370fn smx mut ulong to int
371fixture
372ref = direct
373sq = ppt sub seqfold mut ref ref
374tq = ppt sub treefold mut ref ref
375oq = ppt sub localnorm ref ref
376say 17
377num ulong mul add ref 100.0f64 1.0e9f64
378say 18
379num sq
380say 19
381num tq
382say 20
383num oq
384say 21
385num 2000000
386newline
387bad = or gt sq 2000000 gt tq 2000000
388if eq mut 1
389bad = gt oq 2000000
390end
391if bad
392say 22
393newline
394ret 1
395end
396say 23
397newline
398ret 0
399end
400
401fn main to int
402if lt argcnt 2
403say 1
404newline
405ret 3
406end
407sel = argdec 1
408if eq sel 1
409if lt argcnt 5
410say 1
411newline
412ret 3
413end
414regs = ulong 100
415if ge argcnt 6
416regs = argdec 5
417if ne get argbad 0 0
418say 1
419newline
420ret 3
421end
422end
423ret plan argdec 2 argdec 3 argdec 4 regs
424end
425if eq sel 2
426mut = ulong 0
427if ge argcnt 3
428mut = argdec 2
429end
430ret smx mut
431end
432say 16
433newline
434ret 3
435end