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 / graph.om

566 lines · 11109 bytes Raw Blame History
1note One text sequence on the frozen Qwen3.8-Flash-Next-FP8 model: four residual streams, gated
2note hyper-connections around every block, one lexical n-gram layer, and 512 routed experts per layer.
3note Tensors that the reference stores as BF16 are rounded to BF16 at the same boundaries.
4note Working rows are globals because a global is never passed as an argument. Products go through fin and fout.
5note qrun: 0 model, 1 layer, 2 position, 3 stopped, 4 emitted.
6global qrun ulong 6
7global fh f32 10240
8global fnrm f32 10240
9global fkn f32 10240
10global fgv f32 10240
11global fx f32 2560
12global fdy f32 2560
13global fsh f32 2560
14global fval f32 2560
15global fga f32 640
16global finj f32 4
17global fvp f32 8388608
18global fvo uint 196608
19global fvn ulong 1
20fn qwroleof slot ulong to ulong
21ret ulong get qrole add mul get qrun 1 1600 slot
22end
23
24note Host copy of one small BF16 tensor, widened once. Returns its start in fvp.
25fn fvec id ulong to ulong
26o = ulong get fvo id
27if ne o 0
28ret sub o 1
29end
30if ne get qwdtype id 2
31qwfail 51
32end
33n = get qwcount id
34base = get fvn 0
35if or gt mul n 2 len qbuf gt add base n len fvp
36qwfail 51
37end
38qwread id 0 n
39for j ulong n
40put fvp add base j qbf j
41end
42put fvn 0 add base n
43put fvo id uint add base 1
44ret base
45end
46
47fn fsig x f32 to f32
48ret qrb f32 qb_sig f64 x
49end
50
51fn fsilu x f32 to f32
52ret qrb mul x f32 qb_sig f64 x
53end
54
55note Element i of a normalization source: 0 the residual streams, 1 the last product, 2 the gated lexical value.
56fn fsrc sel ulong i ulong to f32
57if eq sel 0
58ret get fh i
59end
60if eq sel 1
61ret get fout i
62end
63ret get fgv i
64end
65
66note Grouped RMS normalization of four 2560-wide streams with zero-centred gains. The result is fnrm.
67fn fgnorm wid ulong sel ulong to void
68wo = fvec wid
69for s ulong 4
70acc = 0.0f64
71for j ulong 2560
72v = f64 fsrc sel add mul s 2560 j
73acc = add acc mul v v
74end
75inv = div 1.0f64 fsqrt add div acc 2560.0f64 0.000001f64
76for j ulong 2560
77i = add mul s 2560 j
78gain = add 1.0f64 f64 get fvp add wo i
79put fnrm i qrb f32 mul mul f64 fsrc sel i inv gain
80end
81end
82end
83
84fn fnrmin to void
85for j ulong 10240
86put fin j get fnrm j
87end
88end
89
90fn fxin to void
91for j ulong 2560
92put fin j get fx j
93end
94end
95
96note Gated residual input. Reads fh, leaves the mixed block input in fx and, when iid is nonzero, the stream injection weights in finj.
97fn fhyper nid ulong did ulong uid ulong iid ulong to void
98fgnorm nid 0
99if ne iid 0
100fnrmin
101fmat iid
102for s ulong 4
103put finj s mul 2.0f32 fsig qrb mul get fout s 0.25f32
104end
105end
106fnrmin
107fmat did
108for j ulong 320
109put fin j fsilu qrb mul get fout j 0.25f32
110end
111fmat uid
112for j ulong 2560
113acc = 0.0f32
114for s ulong 4
115i = add mul s 2560 j
116acc = add acc qrb mul fsig get fout i get fnrm i
117end
118put fx j qrb mul acc 0.25f32
119end
120end
121
122note fh = fh + injection weight of each stream times the block output in fdy.
123fn fapply to void
124for s ulong 4
125wt = get finj s
126for j ulong 2560
127i = add mul s 2560 j
128put fh i qrb add get fh i qrb mul get fdy j wt
129end
130end
131end
132
133fn foutdy to void
134for j ulong 2560
135put fdy j get fout j
136end
137end
138
139fn fdelta to void
140p = new f32 16480 0.0f32
141w = new f32 41184 0.0f32
142z = new f32 6144 0.0f32
143fxin
144fmat qwroleof 6
145for j ulong 10240
146put p j get fout j
147end
148fmat qwroleof 7
149for j ulong 6144
150put p add 10240 j get fout j
151end
152fmat qwroleof 4
153for j ulong 48
154put p add 16384 mul j 2 get fout j
155end
156fmat qwroleof 5
157for j ulong 48
158put p add 16385 mul j 2 get fout j
159end
160o = fvec qwroleof 10
161for j ulong 40960
162put w j get fvp add o j
163end
164o = fvec qwroleof 2
165for j ulong 48
166put w add 40960 j get fvp add o j
167end
168o = fvec qwroleof 3
169for j ulong 48
170put w add 41008 j get fvp add o j
171end
172o = fvec qwroleof 8
173for j ulong 128
174put w add 41056 j get fvp add o j
175end
176qwen_delta_step 38 get qrun 1 get qrun 2 p w z
177for j ulong 6144
178put fin j qrb get z j
179end
180fmat qwroleof 9
181foutdy
182end
183
184fn fattention to void
185p = new f32 14336 0.0f32
186w = new f32 512 0.0f32
187z = new f32 6144 0.0f32
188fxin
189fmat qwroleof 13
190for j ulong 12288
191put p j get fout j
192end
193fmat qwroleof 14
194for j ulong 512
195put p add 12288 j get fout j
196end
197fmat qwroleof 15
198for j ulong 512
199put p add 12800 j get fout j
200end
201o = fvec qwroleof 11
202for j ulong 256
203put w j get fvp add o j
204end
205o = fvec qwroleof 12
206for j ulong 256
207put w add 256 j get fvp add o j
208end
209qwen_attention_step 38 get qrun 1 get qrun 2 p w z
210for j ulong 6144
211put fin j qrb get z j
212end
213fmat qwroleof 16
214foutdy
215end
216
217global qtop uint 10
218global qprob f32 10
219note Top ten of the 512 router scores now in fout, with their renormalized softmax weights.
220fn froute to void
221den = 0.0f64
222top = 0.0f64
223for rank ulong 10
224best = ulong 512
225for e ulong 512
226used = false
227for j ulong rank
228if eq ulong get qtop j e
229used = true
230end
231end
232if not used
233if eq best 512
234best = e
235else
236if gt get fout e get fout best
237best = e
238end
239end
240end
241end
242put qtop rank uint best
243if eq rank 0
244top = f64 get fout best
245end
246p = qb_exp sub f64 get fout best top
247den = add den p
248put qprob rank f32 p
249end
250for rank ulong 10
251put qprob rank qrb f32 div f64 get qprob rank den
252end
253note The reference accumulates expert outputs in ascending expert order.
254for i ulong 10
255for j ulong sub 9 i
256if gt get qtop j get qtop add j 1
257e = get qtop j
258put qtop j get qtop add j 1
259put qtop add j 1 e
260p = get qprob j
261put qprob j get qprob add j 1
262put qprob add j 1 p
263end
264end
265end
266end
267
268note fin = silu(fga) * fout over the 640 expert channels.
269fn fswiglu to void
270for j ulong 640
271put fin j qrb mul fsilu get fga j get fout j
272end
273end
274
275fn fgate to void
276for j ulong 640
277put fga j get fout j
278end
279end
280
281note Routed expert block. Reads fx, leaves the block output in fdy.
282fn fmoe to void
283layer = get qrun 1
284fxin
285fmat qwroleof 22
286fgate
287fxin
288fmat qwroleof 23
289fswiglu
290fmat qwroleof 24
291for j ulong 2560
292put fsh j get fout j
293put fdy j 0.0f32
294end
295fxin
296fmat qwroleof 20
297froute
298for r ulong 10
299key = mul add mul layer 512 ulong get qtop r 4
300fxin
301fexpert key
302fgate
303fxin
304fexpert add key 1
305fswiglu
306fexpert add key 2
307wt = get qprob r
308for j ulong 2560
309put fdy j qrb add get fdy j qrb mul get fout j wt
310end
311end
312fxin
313fmat qwroleof 21
314sg = fsig get fout 0
315for j ulong 2560
316put fdy j qrb add get fdy j qrb mul get fsh j sg
317end
318end
319
320note Lexical layer state: n-gram parameters, token history and nine past rows of the convolution input.
321note fng: 0-2 multipliers, 3-18 head offsets, 19-34 head sizes, 35 previous token, 36 older token, 37 loaded.
322global fng ulong 40
323global fngscale f32 1
324global fpast f32 92160
325fn fle64 j ulong to ulong
326v = ulong 0
327for i ulong 8
328v = or v shl ulong get qbuf add mul j 8 i mul 8 i
329end
330ret v
331end
332
333fn fngload to void
334if eq get fng 37 1
335ret
336end
337qwread qwroleof 42 0 3
338for j ulong 3
339put fng j fle64 j
340end
341qwread qwroleof 43 0 16
342for j ulong 16
343put fng add 3 j fle64 j
344end
345qwread qwroleof 44 0 16
346for j ulong 16
347put fng add 19 j fle64 j
348end
349total = ulong 0
350for j ulong 16
351n = get fng add 19 j
352if or ne get fng add 3 j total eq n 0
353qwfail 47
354end
355total = add total n
356end
357if gt total 320001536
358qwfail 47
359end
360for j ulong 3
361m = get fng j
362if or eq m 0 gt m 37143239288394
363qwfail 47
364end
365end
366qwread qwroleof 45 0 1
367put fngscale 0 qbf 0
368put fng 37 1
369end
370
371note One hashed FP8 row, scaled and rounded to BF16, into fin at head h.
372fn fnghead mix ulong h ulong to void
373row = add get fng add 3 h rem mix get fng add 19 h
374qwread ulong get qngram div row 2500012 mul rem row 2500012 160 160
375scale = get fngscale 0
376for j ulong 160
377put fin add mul h 160 j qrb mul scale bitcast f32 lqbits uint get qbuf j
378end
379end
380
381note The sixteen hashed rows of this token: eight from the bigram and eight from the trigram.
382fn fngather tok ulong to void
383bi = xor mul tok get fng 0 mul get fng 35 get fng 1
384tri = xor bi mul get fng 36 get fng 2
385for h ulong 8
386fnghead bi h
387fnghead tri add h 8
388end
389end
390
391note Signed square-root gate of one stream from its normalized key (fkn) and query (fnrm) rows.
392fn flexgate s ulong to f32
393acc = 0.0f32
394for j ulong 2560
395i = add mul s 2560 j
396acc = add acc qrb mul get fkn i get fnrm i
397end
398g = qrb div qrb acc 50.596442562694072f32
399mag = qrb fsqrt fmax fabs g 0.000001f32
400if lt g 0.0f32
401mag = neg mag
402end
403if eq g 0.0f32
404mag = 0.0f32
405end
406ret fsig mag
407end
408
409note Dilated depthwise convolution of channel c: taps at 9, 6 and 3 rows back, then the current row in fnrm.
410fn flexconv c ulong w ulong to f32
411pos = get qrun 2
412slot = rem pos 9
413tap = add w mul c 4
414total = mul get fnrm c get fvp add tap 3
415for j ulong 3
416if ge pos sub 9 mul j 3
417idx = rem add slot mul j 3 9
418idx = add mul idx 10240 c
419old = get fpast idx
420total = add total mul old get fvp add tap j
421end
422end
423ret fsilu qrb total
424end
425
426fn flexhistory tok ulong to void
427if eq get qrun 2 0
428put fng 35 248044
429put fng 36 248044
430end
431fngather tok
432put fng 36 get fng 35
433if eq tok 248044
434put fng 36 248044
435end
436put fng 35 tok
437end
438
439fn flexical tok ulong to void
440fngload
441flexhistory tok
442note fin holds the n-gram embedding for both projections; products do not modify fin.
443fmat qwroleof 41
444for j ulong 2560
445put fval j get fout j
446end
447fmat qwroleof 37
448fgnorm qwroleof 39 1
449for j ulong 10240
450put fkn j get fnrm j
451end
452fgnorm qwroleof 40 0
453for s ulong 4
454sg = flexgate s
455for j ulong 2560
456put fgv add mul s 2560 j qrb mul sg get fval j
457end
458end
459fgnorm qwroleof 38 2
460w = fvec qwroleof 36
461for c ulong 10240
462put fh c qrb add get fh c qrb add get fgv c flexconv c w
463end
464slot = rem get qrun 2 9
465for c ulong 10240
466put fpast add mul slot 10240 c get fnrm c
467end
468end
469
470fn flayer tok ulong to void
471if eq get qrun 1 1
472flexical tok
473end
474fhyper qwroleof 25 qwroleof 26 qwroleof 27 qwroleof 28
475if eq rem get qrun 1 4 3
476fattention
477else
478fdelta
479end
480fdebughalf
481fapply
482fhyper qwroleof 29 qwroleof 30 qwroleof 31 qwroleof 32
483fmoe
484fdebughalf
485fapply
486end
487
488fn qargmax n ulong to ulong
489best = ulong 0
490for j ulong n
491b = bitcast uint get fout j
492if eq and b 2139095040 2139095040
493qwfail 54
494end
495if gt get fout j get fout best
496best = j
497end
498end
499ret best
500end
501
502note Instrument: with fdebug set, the four residual streams after every layer go to stdout as BF16.
503global fdebug ulong 1
504fn fdebugrow to void
505for j ulong 10240
506bits = shr bitcast uint get fh j 16
507tkpc ulong and bits 255
508tkpc ulong shr bits 8
509end
510end
511
512note Instrument: with fdebug 2, the block input, injection weights and block output of both halves of
513note layers 0 to 3 go to stdout as raw binary32, 5124 values per half.
514fn fdebugword bits uint to void
515tkpc ulong and bits 255
516tkpc ulong and shr bits 8 255
517tkpc ulong and shr bits 16 255
518tkpc ulong shr bits 24
519end
520
521fn fdebughalf to void
522if or ne get fdebug 0 2 ge get qrun 1 4
523ret
524end
525for j ulong 2560
526fdebugword bitcast uint get fx j
527end
528for j ulong 4
529fdebugword bitcast uint get finj j
530end
531for j ulong 2560
532fdebugword bitcast uint get fdy j
533end
534end
535
536note One token through the model. With logits the final mixed row stays in fx and the vocabulary scores in fout.
537fn qtoken tok ulong pos ulong logits bool to ulong
538if or ge tok 248320 ge pos 2048
539qwfail 55
540end
541qdiskrecheck
542put qrun 2 pos
543qwread ulong get qglob 0 mul tok 2560 2560
544for j ulong 2560
545v = qbf j
546put fh j v
547put fh add 2560 j v
548put fh add 5120 j v
549put fh add 7680 j v
550end
551for l ulong 48
552put qrun 1 l
553flayer tok
554if eq get fdebug 0 1
555fdebugrow
556end
557end
558if not logits
559ret 0
560end
561fhyper ulong get qglob 2 ulong get qglob 3 ulong get qglob 4 0
562fxin
563fmat ulong get qglob 1
564qdiskrecheck
565ret qargmax 248320
566end