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 / core / sovereign-core / om / qwen-native / flash / project.om

333 lines · 6804 bytes Raw Blame History
1note Flash-Next matrix layer on GB10. Dense BF16 matrices stay resident in bump arenas.
2note Routed experts occupy fixed device slots, least recently used first out. Host mode computes the same products on the CPU.
3global qxy long 2
4global qdarena long 8
5global qdused ulong 8
6global qdcur ulong 1
7global qgoff ulong 196608
8global qearena long 32
9global qemap uint 24576
10global qeown uint 26624
11global qestamp ulong 26624
12note qecfg: 0 host mode, 1 slot limit, 2 clock, 3 slots filled, 4 expert loads, 5 evictions, 6 device launches.
13global qecfg ulong 8
14const qdsize ulong 2147483648
15const qeslots ulong 832
16const qestride ulong 4919296
17fn qtransfer fd int origin ulong n ulong dev long devoff ulong to void
18off = ulong 0
19while lt off n bound 65536
20w = sub n off
21if gt w len qbuf
22w = len qbuf
23end
24if ne pread fd qbuf 0 w long add origin off long w
25qwfail 48
26end
27if ne mapwrite dev add devoff off qbuf 0 w long w
28qwfail 48
29end
30off = add off w
31end
32end
33
34fn qfdof id ulong to int
35ret int get qwfd ulong get qwfile id
36end
37
38fn qdense id ulong to ulong
39g = get qgoff id
40if ne g 0
41ret sub g 1
42end
43bytes = mul get qwcount id ulong get qwdtype id
44need = mul div add bytes 4095 4096 4096
45if gt need qdsize
46qwfail 49
47end
48a = get qdcur 0
49if gt add get qdused a need qdsize
50a = add a 1
51put qdcur 0 a
52end
53if ge a 8
54qwfail 49
55end
56h = get qdarena a
57if le h 0
58h = nvalloc qdsize true
59if le h 0
60qwfail 50
61end
62put qdarena a h
63end
64off = get qdused a
65qtransfer qfdof id get qwoff id bytes h off
66put qdused a add off need
67loc = or shl a 40 off
68put qgoff id add loc 1
69ret loc
70end
71
72fn qvictim to ulong
73total = get qecfg 1
74used = get qecfg 3
75if lt used total
76put qecfg 3 add used 1
77ret used
78end
79slot = ulong 0
80best = get qestamp 0
81for i ulong total
82if lt get qestamp i best
83best = get qestamp i
84slot = i
85end
86end
87old = ulong get qeown slot
88if eq old 0
89qwfail 49
90end
91put qemap sub old 1 0
92put qecfg 5 add get qecfg 5 1
93ret slot
94end
95
96fn qeput h long base ulong id ulong j ulong to void
97qtransfer qfdof id get qwoff id 1638400 h add base mul j 1638400
98sid = ulong get qscale id
99qtransfer qfdof sid get qwoff sid 200 h add add base 4915200 mul j 200
100end
101
102fn qefill layer ulong e ulong slot ulong to void
103a = div slot qeslots
104h = get qearena a
105if le h 0
106h = nvalloc mul qeslots qestride false
107if le h 0
108qwfail 50
109end
110put qearena a h
111end
112first = add add mul layer 1600 64 mul e 3
113for j ulong 3
114qeput h mul rem slot qeslots qestride ulong get qrole add first j j
115end
116end
117
118fn qexpert layer ulong e ulong to ulong
119key = add mul layer 512 e
120clock = add get qecfg 2 1
121put qecfg 2 clock
122s = ulong get qemap key
123if ne s 0
124put qestamp sub s 1 clock
125ret sub s 1
126end
127slot = qvictim
128qefill layer e slot
129put qemap key uint add slot 1
130put qeown slot uint add key 1
131put qestamp slot clock
132put qecfg 4 add get qecfg 4 1
133ret slot
134end
135
136fn qgpuopen to void
137if not lqopen
138qwfail 50
139end
140x = nvalloc 1048576 true
141if le x 0
142qwfail 50
143end
144put qxy 0 x
145y = nvalloc 1048576 true
146if le y 0
147qwfail 50
148end
149put qxy 1 y
150end
151
152fn qbf j ulong to f32
153a = mul j 2
154bits = or uint get qbuf a shl uint get qbuf add a 1 8
155ret bitcast f32 shl bits 16
156end
157
158note Round a binary32 value to the nearest BF16 value, ties to even, as the reference tensors store it.
159fn qrb x f32 to f32
160u = bitcast uint x
161a = and u 2139095040
162if eq a 2139095040
163ret x
164end
165ret bitcast f32 shl shr add add u 32767 and shr u 16 1 16 16
166end
167
168note Matrix products read their input from fin and leave BF16-rounded results in fout.
169global fin f32 10240
170global fout f32 248320
171fn qfillinput n ulong to void
172if gt n len fin
173qwfail 52
174end
175for j ulong n
176bits = bitcast uint get fin j
177if ne mapset get qxy 0 mul j 4 4 ulong bits 0
178qwfail 52
179end
180end
181end
182
183note One device product y[pos..pos+m] = W[m,k] x. W and its scale tiles lie in one mapping.
184note qmvarg: 0 mapping, 1 weight offset, 2 scale offset, 3 format, 4 rows, 5 columns.
185global qmvarg ulong 6
186fn qmv to void
187h = long get qmvarg 0
188fmt = uint get qmvarg 3
189m = uint get qmvarg 4
190k = uint get qmvarg 5
191if gt ulong m len fout
192qwfail 52
193end
194qfillinput ulong k
195sr = uint 0
196sc = uint 0
197put lqmap 0 h
198put lqoff 0 get qmvarg 1
199put lqlen 0 mul mul ulong m ulong k sub 2 ulong fmt
200put lqlen 1 0
201if eq fmt 1
202sr = add div sub m 1 128 1
203sc = add div sub k 1 128 1
204put lqmap 1 h
205put lqoff 1 get qmvarg 2
206put lqlen 1 mul mul ulong sr ulong sc 2
207end
208put lqmap 2 get qxy 0
209put lqmap 3 get qxy 1
210put lqoff 2 0
211put lqoff 3 0
212put lqlen 2 mul ulong k 4
213put lqlen 3 mul ulong m 4
214if not lqrun fmt m k sr sc
215qwfail 53
216end
217put qecfg 6 add get qecfg 6 1
218for j ulong ulong m
219bits = uint mapget get qxy 1 mul j 4 4
220put fout j qrb bitcast f32 bits
221end
222end
223
224note Host product with binary64 accumulation. Rows are read from the checkpoint in bounded batches.
225note qhs: 0 rows, 1 columns, 2 element bytes, 3 scale columns, 4 first row of the batch in qbuf.
226global qhscale f32 512
227global qhs ulong 5
228fn qhrow i ulong to f32
229k = get qhs 1
230base = mul i mul k get qhs 2
231acc = 0.0f64
232if eq get qhs 2 2
233base = div base 2
234for j ulong k
235acc = add acc mul f64 qbf add base j f64 get fin j
236end
237ret qrb f32 acc
238end
239tile = mul div add get qhs 4 i 128 get qhs 3
240for j ulong k
241wv = mul bitcast f32 lqbits uint get qbuf add base j get qhscale add tile div j 128
242acc = add acc mul f64 wv f64 get fin j
243end
244ret qrb f32 acc
245end
246
247fn qhscales id ulong to void
248put qhs 3 0
249sid = ulong get qscale id
250if eq sid 0
251ret
252end
253n = get qwcount sid
254if gt n 512
255qwfail 52
256end
257put qhs 3 get qwshape add mul sid 4 1
258qwread sid 0 n
259for j ulong n
260put qhscale j qbf j
261end
262end
263
264fn qhostmv id ulong to void
265m = get qwshape mul id 4
266k = get qwshape add mul id 4 1
267put qhs 0 m
268put qhs 1 k
269put qhs 2 ulong get qwdtype id
270if or gt m len fout gt k len fin
271qwfail 52
272end
273qhscales id
274per = div len qbuf mul k get qhs 2
275if eq per 0
276qwfail 52
277end
278r = ulong 0
279while lt r m bound 1048576
280nr = sub m r
281if gt nr per
282nr = per
283end
284qwread id mul r k mul nr k
285put qhs 4 r
286for i ulong nr
287put fout add r i qhrow i
288end
289r = add r nr
290end
291end
292
293fn fmat id ulong to void
294if eq get qecfg 0 1
295qhostmv id
296ret
297end
298if ne get qwdtype id 2
299qwfail 52
300end
301a = mul id 4
302loc = qdense id
303put qmvarg 0 ulong get qdarena shr loc 40
304put qmvarg 1 and loc 1099511627775
305put qmvarg 2 0
306put qmvarg 3 0
307put qmvarg 4 get qwshape a
308put qmvarg 5 get qwshape add a 1
309qmv
310end
311
312note Product with one matrix of a routed expert. The key is (layer * 512 + expert) * 4 + matrix (0 gate, 1 up, 2 down).
313fn fexpert key ulong to void
314j = rem key 4
315e = rem div key 4 512
316layer = div key 2048
317id = ulong get qrole add add mul layer 1600 64 add mul e 3 j
318if eq get qecfg 0 1
319qhostmv id
320ret
321end
322slot = qexpert layer e
323h = get qearena div slot qeslots
324base = mul rem slot qeslots qestride
325a = mul id 4
326put qmvarg 0 ulong h
327put qmvarg 1 add base mul j 1638400
328put qmvarg 2 add add base 4915200 mul j 200
329put qmvarg 3 1
330put qmvarg 4 get qwshape a
331put qmvarg 5 get qwshape add a 1
332qmv
333end