interplane.Get Omega
Read in your language

aien-sovereign-core-mv

AienOS host runtime: inference scheduling, paged KV state, the aien CLI and Spark verification tooling. Its inference engine is in alpha testing (DGX Spark GB10); Omega is the implementation destination.

Brought by drakestapleton

branch main Branches Tags History

aien-sovereign-core-mv / om / flint / ops.om

1836 lines · 34466 bytes Raw Blame History
1note flint-ops: forward-operator parity for the pinned Flint checkpoint
2note (aien-architecture-mv#353, aien-sovereign-core-mv#802, P0 forward lane).
3note
4note Usage: flint-ops OP FIXTURE INVENTORY SHARD_DIR
5note flint-ops selftest
6note OP is rmsnorm, qkv, attn, router, shared, expert or moe. FIXTURE is written by
7note test/opref.py from the pinned reference modules; INVENTORY is the TSV flint-manifest
8note printed for SHARD_DIR. Each operator recomputes the reference output in f64 from the
9note real BF16 shard weights and carries a first-order bound E on the error of the
10note reference's own f32 computation (gamma_n = n u / (1 - n u), u = 2^-24). An element
11note passes when |om - ref| <= 2 E. The router reports a token as a tie, not a mismatch, when
12note the reference could hold the om rank-7 expert in its top-6 within those per-element bounds.
13note
14note Prints one line: flint-op OP layer L elems N worst_ppm P ties K PASS|FAIL, where P is
15note max |om - ref| / (2 E) in millionths. Exit 0 PASS, 1 FAIL, 2 and up refused.
16note
17note Layer kinds follow the pinned config.json (manifest CFG_PIN): no_rope_layer_indices
18note 0, 1, 5, 9, .., 45, 48 use full attention without RoPE; every other layer uses
19note RoPE and the sliding window of FWIN keys including the query itself.
20
21const FWIN ulong 513
22const FHID ulong 2048
23note FULP: libm budget in ulps for each of the reference's powf, cosf and sinf calls in RoPE.
24note glibc 2.39 lists 1 ulp for AArch64 powf/cosf/sinf, SLEEF documents 1.0 (u10) and 3.5 (u35);
25note 4 covers all of them (test/TOLERANCE.md, RoPE libm budget). test/opref.py reads this line,
26note measures the reference's calls against float64 and refuses fixtures above FULP ulps.
27const FULP ulong 4
28global ga uchar 16384
29global gas ulong 8
30global gal ulong 8
31global gpth uchar 4352
32global gfx uchar 8388608
33global gtsv uchar 8388608
34global gstg uchar 4194304
35global gw f32 16777216
36global gv f64 8192
37global gve f64 8192
38global gacc f64 16
39global gy f64 4096
40global gye f64 4096
41global gx0 f64 1048576
42global ge0 f64 1048576
43global gx1 f64 1048576
44global ge1 f64 1048576
45global gx2 f64 1048576
46global ge2 f64 1048576
47global gx3 f64 1048576
48global ge3 f64 1048576
49global gacm f64 16384
50global gace f64 16384
51global gaca f64 16384
52global gsc f64 1024
53global gse f64 1024
54global gsg f64 1024
55global gsge f64 1024
56global gsk f64 64
57global gch uchar 256
58global gtk ulong 8
59global gtw f64 8
60global gtwe f64 8
61global gmk ulong 512
62global gmw f64 512
63global gmwe f64 512
64global gtie uchar 64
65global gcm f64 8
66global gst ulong 32
67global gnm uchar 256
68global gob uchar 4096
69global gdg uchar 24
70global gh8 uchar 8
71global gstb uchar 128
72global gfh f64 4096
73global gfa f64 4096
74global gfq f64 8192
75global gfo f64 8192
76global gfs ulong 4
77text tofwd "forward"
78text tembd "model.embed_tokens.weight"
79text tfnorm "model.norm.weight"
80text tlmhead "lm_head.weight"
81text tdense "mlp."
82
83text tu "usage: flint-ops OP FIXTURE INVENTORY SHARD_DIR | flint-ops selftest"
84text tio "io"
85text tnreg "not-regular-file"
86text tfx "fixture"
87text tnf "tensor-not-in-inventory"
88text tdt "tensor-dtype-or-shape"
89text tref "refused "
90text tfop "flint-op "
91text tlay " layer "
92text telm " elems "
93text tppm " worst_ppm "
94text tties " ties "
95text tpass " PASS"
96text tfail " FAIL"
97text tbf "BF16"
98text tpre "model.layers."
99text tln0 "input_layernorm.weight"
100text tln1 "post_attention_layernorm.weight"
101text tq "self_attn.q_proj.weight"
102text tk "self_attn.k_proj.weight"
103text tv "self_attn.v_proj.weight"
104text tob "self_attn.o_proj.weight"
105text tqn "self_attn.q_norm.weight"
106text tkn "self_attn.k_norm.weight"
107text tsnk "self_attn.softmax_offset"
108text trt "mlp.gate.weight"
109text tshp "mlp.shared_expert."
110text texp "mlp.experts."
111text tgp "gate_proj.weight"
112text tup "up_proj.weight"
113text tdp "down_proj.weight"
114text tormsn "rmsnorm"
115text toqkv "qkv"
116text toatt "attn"
117text tortr "router"
118text toshr "shared"
119text toexp "expert"
120text tomoe "moe"
121text tostt "selftest"
122
123note gst slots: 0 fixture bytes, 1 inventory bytes, 2 op, 3 layer, 4 T, 5 S, 6 aux,
124note 7 tensor-name length, 8 output fill, 9 fstat size, 10 shard-name start in gtsv,
125note 11 shard-name length, 12 begin, 13 end, 14 rows, 15 cols.
126note gcm slots: 0 worst |om - ref| / (2 E), 1 failures, 2 elements, 3 ties.
127
128fn op_dec v ulong to ulong
129n = ulong 0
130x = v
131while true bound 25
132put gdg n add 48 uchar rem x 10
133n = add n 1
134x = div x 10
135if eq x 0
136break
137end
138end
139ret n
140end
141
142fn ob_c c uchar to void
143p = get gst 8
144if lt p 4096
145put gob p c
146put gst 8 add p 1
147end
148end
149
150fn ob_tx t view uchar * to void
151for i ulong len t
152ob_c get t i
153end
154end
155
156fn ob_u v ulong to void
157n = op_dec v
158for i ulong n
159ob_c get gdg sub sub n i 1
160end
161end
162
163fn ob_flush fd int to void
164o = ulong 0
165m = get gst 8
166while lt o m bound 4096
167w = write fd gob o sub m o
168if le w 0
169exit 3
170end
171o = add o ulong w
172end
173put gst 8 0
174end
175
176fn op_die c ulong t view uchar * to void
177put gst 8 0
178ob_tx tref
179ob_u c
180ob_c 32
181ob_tx t
182ob_c 10
183ob_flush 2
184exit int c
185end
186
187fn op_le64 o ulong to ulong
188v = ulong 0
189for i ulong 8
190v = or v shl ulong get gstb add o i mul 8 i
191end
192ret v
193end
194
195fn op_open n ulong to long
196fd = fopen gpth 0 n 557056 0
197if lt fd 0
198if eq fd -40
199op_die 4 tnreg
200end
201op_die 3 tio
202end
203r = fstat int fd gstb 0 128
204if ne r 0
205op_die 3 tio
206end
207note S_IFMT sits in the low 16 bits of st_mode
208md = or ulong get gstb 16 shl ulong get gstb 17 8
209if ne and md 61440 32768
210op_die 4 tnreg
211end
212put gst 9 op_le64 48
213ret fd
214end
215
216fn op_close fd long to void
217r = fclose int fd
218if ne r 0
219op_die 3 tio
220end
221end
222
223fn op_argp a ulong to ulong
224n = get gal a
225if or eq n 0 gt n 4095
226op_die 2 tu
227end
228o = get gas a
229for i ulong n
230put gpth i get ga add o i
231end
232ret n
233end
234
235fn op_argeq a ulong t view uchar * to bool
236n = get gal a
237if ne n len t
238ret false
239end
240o = get gas a
241for i ulong n
242if ne get ga add o i get t i
243ret false
244end
245end
246ret true
247end
248
249fn rd_fx to void
250fd = op_open op_argp 2
251n = get gst 9
252if or lt n 32 gt n len gfx
253op_die 7 tfx
254end
255o = ulong 0
256while lt o n bound 999999
257r = pread int fd gfx o sub n o long o
258if le r 0
259op_die 3 tio
260end
261o = add o ulong r
262end
263op_close fd
264put gst 0 n
265end
266
267fn rd_tsv to void
268fd = op_open op_argp 3
269n = get gst 9
270if gt n len gtsv
271op_die 5 tnf
272end
273o = ulong 0
274while lt o n bound 999999
275r = pread int fd gtsv o sub n o long o
276if le r 0
277op_die 3 tio
278end
279o = add o ulong r
280end
281op_close fd
282put gst 1 n
283end
284
285note fixture words are little-endian u32 or f32
286fn fx_w i ulong to ulong
287b = mul i 4
288v = or ulong get gfx b shl ulong get gfx add b 1 8
289v = or v shl ulong get gfx add b 2 16
290v = or v shl ulong get gfx add b 3 24
291ret v
292end
293
294fn fx_f i ulong to f64
295ret f64 bitcast f32 uint fx_w i
296end
297
298fn fx_need w ulong to void
299if ne get gst 0 mul 4 w
300op_die 7 tfx
301end
302end
303
304note load T rows of 2048 fixture words at word xo into gx0, with zero error in ge0
305fn fx_x0 xo ulong T ulong to void
306for i ulong mul T FHID
307put gx0 i fx_f add xo i
308put ge0 i 0.0f64
309end
310end
311
312fn gam n ulong to f64
313x = mul f64 n 5.9604644775390625e-8f64
314ret div x sub 1.0f64 x
315end
316
317fn fl_swa L ulong to bool
318ret not or or lt L 2 eq L 48 eq rem L 4 1
319end
320
321note cmp: one output element, om against the reference value ref, passing when
322note |om - ref| <= 2 e; a NaN anywhere fails
323fn cmp om f64 ref f64 e f64 to void
324d = fabs sub om ref
325t = mul 2.0f64 e
326put gcm 2 add get gcm 2 1.0f64
327if not le d t
328put gcm 1 add get gcm 1 1.0f64
329end
330if gt d 0.0f64
331r = div d fabs t
332if gt r get gcm 0
333put gcm 0 r
334end
335end
336end
337
338fn cm_reset to void
339for i ulong 8
340put gcm i 0.0f64
341end
342end
343
344fn report nm view uchar * to int
345put gst 8 0
346ob_tx tfop
347ob_tx nm
348ob_tx tlay
349ob_u get gst 3
350ob_tx telm
351ob_u ulong get gcm 2
352ob_tx tppm
353ob_u ulong mul get gcm 0 1000000.0f64
354ob_tx tties
355ob_u ulong get gcm 3
356f = gt get gcm 1 0.0f64
357if f
358ob_tx tfail
359else
360ob_tx tpass
361end
362ob_c 10
363ob_flush 1
364if f
365ret 1
366end
367ret 0
368end
369
370note f64 exp: k = round(x / ln 2), Cody-Waite reduction with ln2_hi (32 bits) and
371note ln2_lo, Taylor degree 13 on |r| <= ln 2 / 2, scale by 2^k through the exponent field
372fn om_exp x f64 to f64
373if lt x -708.0f64
374ret 0.0f64
375end
376if gt x 709.0f64
377ret div 1.0f64 0.0f64
378end
379kf = mul x 1.442695040888963407f64
380if lt kf 0.0f64
381kf = sub kf 0.5f64
382else
383kf = add kf 0.5f64
384end
385k = long kf
386fk = f64 k
387r = sub sub x mul fk 0.6931471803691238165f64 mul fk 1.908214929270587816e-10f64
388p = 1.0f64
389for j ulong 13
390p = add 1.0f64 mul div r f64 sub 13 j p
391end
392s = bitcast f64 shl ulong add k 1023 52
393ret mul p s
394end
395
396fn om_sig x f64 to f64
397ret div 1.0f64 add 1.0f64 om_exp neg x
398end
399
400note sin (c false) or cos (c true) of x in [0, 2^20): k = round(x 2 / pi), reduction by
401note pio2_hi (33 bits) and pio2_lo, Taylor through degree 21 on |r| <= pi / 4
402fn om_sc x f64 c bool to f64
403k = ulong add mul x 0.6366197723675813431f64 0.5f64
404fk = f64 k
405r = sub sub x mul fk 1.570796326734125614f64 mul fk 6.077100506506192601e-11f64
406z = mul r r
407s = 1.0f64
408co = 1.0f64
409for j ulong 10
410m = f64 mul 2 sub 10 j
411s = sub 1.0f64 mul div z mul m add m 1.0f64 s
412co = sub 1.0f64 mul div z mul sub m 1.0f64 m co
413end
414s = mul r s
415q = k
416if c
417q = add q 1
418end
419q = and q 3
420if eq q 0
421ret s
422end
423if eq q 1
424ret co
425end
426if eq q 2
427ret neg s
428end
429ret neg co
430end
431
432note tensor names: model.layers.L. followed by a suffix
433fn nm_c c uchar to void
434n = get gst 7
435if ge n 256
436op_die 5 tnf
437end
438put gnm n c
439put gst 7 add n 1
440end
441
442fn nm_tx t view uchar * to void
443for i ulong len t
444nm_c get t i
445end
446end
447
448fn nm_u v ulong to void
449n = op_dec v
450for i ulong n
451nm_c get gdg sub sub n i 1
452end
453end
454
455fn nm_lay L ulong to void
456put gst 7 0
457nm_tx tpre
458nm_u L
459nm_c 46
460end
461
462fn tsv_eqn p ulong n ulong to bool
463if gt add add p n 1 get gst 1
464ret false
465end
466for i ulong n
467if ne get gtsv add p i get gnm i
468ret false
469end
470end
471ret eq get gtsv add p n 9
472end
473
474fn tsv_nl p ulong to ulong
475m = get gst 1
476q = p
477while lt q m bound 99999
478if eq get gtsv q 10
479ret add q 1
480end
481q = add q 1
482end
483ret m
484end
485
486note index of the tab or newline that ends the field starting at p
487fn tsv_fld p ulong to ulong
488m = get gst 1
489q = p
490while lt q m bound 99999
491c = get gtsv q
492if or eq c 9 eq c 10
493ret q
494end
495q = add q 1
496end
497op_die 5 tnf
498ret m
499end
500
501fn tsv_num p ulong e ulong to ulong
502if or ge p e gt sub e p 19
503op_die 6 tdt
504end
505v = ulong 0
506for i ulong sub e p
507c = get gtsv add p i
508if or lt c 48 gt c 57
509op_die 6 tdt
510end
511v = add mul v 10 ulong sub c 48
512end
513ret v
514end
515
516fn tsv_bf p ulong e ulong to bool
517if ne sub e p 4
518ret false
519end
520for i ulong 4
521if ne get gtsv add p i get tbf i
522ret false
523end
524end
525ret true
526end
527
528fn tsv_x p ulong e ulong to ulong
529q = p
530while lt q e bound 99999
531if eq get gtsv q 120
532ret q
533end
534q = add q 1
535end
536ret e
537end
538
539note row fields after the name: shard, dtype, shape (R or RxC), begin, end
540fn tsv_row p ulong to void
541e = tsv_fld p
542put gst 10 p
543put gst 11 sub e p
544p2 = add e 1
545e2 = tsv_fld p2
546if not tsv_bf p2 e2
547op_die 6 tdt
548end
549p3 = add e2 1
550e3 = tsv_fld p3
551x = tsv_x p3 e3
552put gst 14 tsv_num p3 x
553put gst 15 0
554if lt x e3
555put gst 15 tsv_num add x 1 e3
556end
557p4 = add e3 1
558e4 = tsv_fld p4
559put gst 12 tsv_num p4 e4
560p5 = add e4 1
561e5 = tsv_fld p5
562put gst 13 tsv_num p5 e5
563end
564
565fn tsv_find to void
566n = get gst 7
567m = get gst 1
568p = ulong 0
569f = false
570while lt p m bound 99999
571if tsv_eqn p n
572tsv_row add p add n 1
573f = true
574break
575end
576p = tsv_nl p
577end
578if not f
579op_die 5 tnf
580end
581end
582
583fn sh_open to long
584d = get gal 4
585if or eq d 0 gt d 2048
586op_die 2 tu
587end
588n = op_argp 4
589put gpth n 47
590n = add n 1
591s = get gst 10
592k = get gst 11
593if gt k 1024
594op_die 5 tnf
595end
596for i ulong k
597put gpth add n i get gtsv add s i
598end
599ret op_open add n k
600end
601
602fn pf_stg fd long n ulong pos ulong to void
603o = ulong 0
604while lt o n bound 999999
605r = pread int fd gstg o sub n o long add pos o
606if le r 0
607op_die 3 tio
608end
609o = add o ulong r
610end
611end
612
613note a BF16 value is the high half of the f32 with the same bits, so the decode is exact
614fn dec_stg c ulong d ulong to void
615for i ulong div c 2
616lo = uint get gstg mul i 2
617hi = uint get gstg add mul i 2 1
618put gw add d i bitcast f32 or shl hi 24 shl lo 16
619end
620end
621
622note ld_t: the tensor named in gnm must be BF16 of shape rows x cols (cols 0: a vector);
623note it lands in gw[0 ..) as f32
624fn ld_t rows ulong cols ulong to void
625tsv_find
626if or ne get gst 14 rows ne get gst 15 cols
627op_die 6 tdt
628end
629k = rows
630if gt cols 0
631k = mul rows cols
632end
633nb = mul k 2
634if or ne sub get gst 13 get gst 12 nb gt k len gw
635op_die 6 tdt
636end
637ld_bytes nb 0
638end
639
640note ld_rows: bounded row slice of a manifest-admitted BF16 matrix, never allocate the vocabulary.
641fn ld_rows rows ulong cols ulong first ulong count ulong to void
642tsv_find
643if or ne get gst 14 rows ne get gst 15 cols
644op_die 6 tdt
645end
646if or gt first rows gt count sub rows first
647op_die 6 tdt
648end
649n = mul count cols
650if gt n len gw
651op_die 6 tdt
652end
653size = mul mul rows cols 2
654extent = sub get gst 13 get gst 12
655if ne extent size
656op_die 6 tdt
657end
658ld_bytes mul n 2 mul mul first cols 2
659end
660
661fn ld_bytes nb ulong off ulong to void
662fd = sh_open
663r = pread int fd gh8 0 8 long 0
664if ne r 8
665op_die 3 tio
666end
667hl = ulong 0
668for i ulong 8
669hl = or hl shl ulong get gh8 i mul 8 i
670end
671if gt hl 100000000
672op_die 3 tio
673end
674base = add add add 8 hl get gst 12 off
675o = ulong 0
676while lt o nb bound 99999
677c = sub nb o
678if gt c 1048576
679c = ulong 1048576
680end
681pf_stg fd c add base o
682dec_stg c div o 2
683o = add o c
684end
685op_close fd
686end
687
688note dotr: row r of gw (K columns) against gv; gacc 1 gets sum |w v|, gacc 2 gets
689note sum |w| gve; returns sum w v
690fn dotr r ulong k ulong to f64
691b = mul r k
692s = 0.0f64
693a = 0.0f64
694e = 0.0f64
695for i ulong k
696w = f64 get gw add b i
697p = mul w get gv i
698s = add s p
699a = add a fabs p
700e = add e mul fabs w get gve i
701end
702put gacc 1 a
703put gacc 2 e
704ret s
705end
706
707note error bound of the last dotr: propagated input error plus gamma_K sum |w v|
708fn dote k ulong to f64
709ret add get gacc 2 mul gam k get gacc 1
710end
711
712note matdot: forward integration has its own fixed comparison budget, not dote bounds.
713note Avoid accumulating two unused per-product error sums there. Keep the f64 products
714note and reduction order identical to dotr. Router dotr remains bounded for tie reporting;
715note every local operator still calls the original bounded dotr path.
716fn matdot r ulong k ulong to f64
717if ne get gst 2 8
718ret dotr r k
719end
720b = mul r k
721s = 0.0f64
722for i ulong k
723w = f64 get gw add b i
724p = mul w get gv i
725s = add s p
726end
727put gacc 1 0.0f64
728put gacc 2 0.0f64
729ret s
730end
731
732note rmsv: RMSNorm of gv[0 .. n) with weight gw[wo ..) into gy, bound into gye:
733note E_i = |w_i| r (E_xi + |x_i| r^2 / n sum |x_j| E_xj) + gamma_(n+6) |y_i|
734fn rms_rc n ulong to void
735ss = 0.0f64
736ea = 0.0f64
737for i ulong n
738v = get gv i
739ss = add ss mul v v
740ea = add ea mul fabs v get gve i
741end
742eps = f64 f32 0.000001
743r = div 1.0f64 fsqrt add div ss f64 n eps
744put gacc 6 r
745put gacc 7 div mul mul r r ea f64 n
746end
747
748fn rmsv n ulong wo ulong to void
749rms_rc n
750r = get gacc 6
751c = get gacc 7
752g = gam add n 6
753for i ulong n
754w = f64 get gw add wo i
755v = get gv i
756y = mul mul w v r
757put gy i y
758q = add get gve i mul fabs v c
759q = mul q mul fabs w r
760put gye i add q mul g fabs y
761end
762end
763
764note rope_cs: cos and sin of pos inv_freq_i, inv_freq_i = exp(-(2 i / 128) ln 10000), into
765note gacc 3 and 4; gacc 5 gets the reference's angle and cos/sin error under FULP ulps per
766note libm call (1 ulp <= 2u relative): the angle 1 / pow(10000, 2i/128) * pos carries
767note gamma_(2 FULP + 2) relative, and cos/sin add gamma_(2 FULP) absolute
768fn rope_cs pos ulong i ulong to void
769f = om_exp neg mul f64 i div 9.210340371976182736f64 64.0f64
770th = mul f64 pos f
771put gacc 3 om_sc th true
772put gacc 4 om_sc th false
773put gacc 5 add mul th gam add mul 2 FULP 2 gam mul 2 FULP
774end
775
776note rope_one: rotate_half RoPE of the pair (i, i + 64) of gy at pos, in place;
777note E_out = |c| E_a + |s| E_b + (|a| + |b|) E_cs + gamma_2 (|a c| + |b s|)
778fn rope_one pos ulong i ulong to void
779rope_cs pos i
780g2 = gam 2
781c = get gacc 3
782s = get gacc 4
783a = get gy i
784b = get gy add i 64
785x = mul add fabs a fabs b get gacc 5
786t = add fabs mul a c fabs mul b s
787put gacc 9 add x mul g2 t
788t = add fabs mul b c fabs mul a s
789put gacc 10 add x mul g2 t
790put gy i sub mul a c mul b s
791put gy add i 64 add mul b c mul a s
792a = get gye i
793b = get gye add i 64
794t = add mul fabs c a mul fabs s b
795put gye i add get gacc 9 t
796t = add mul fabs c b mul fabs s a
797put gye add i 64 add get gacc 10 t
798end
799
800fn ropev pos ulong to void
801for i ulong 64
802rope_one pos i
803end
804end
805
806fn op_rmsn to int
807L = get gst 3
808T = get gst 4
809aux = get gst 6
810if or or eq T 0 gt T 512 gt aux 1
811op_die 7 tfx
812end
813fx_need add 8 mul T 4096
814nm_lay L
815if eq aux 0
816nm_tx tln0
817else
818nm_tx tln1
819end
820ld_t FHID 0
821yo = add 8 mul T FHID
822for t ulong T
823for i ulong FHID
824put gv i fx_f add 8 add mul t FHID i
825put gve i 0.0f64
826end
827rmsv FHID 0
828for i ulong FHID
829cmp get gy i fx_f add yo add mul t FHID i get gye i
830end
831end
832r = report tormsn
833ret r
834end
835
836note cmp_x1: gx1/ge1[o .. o + n) against fixture words fo + o ..
837fn cmp_x1 o ulong n ulong fo ulong to void
838for i ulong n
839j = add o i
840cmp get gx1 j fx_f add fo j get ge1 j
841end
842end
843
844note projfx: rows x 2048 weights in gw against the S fixture rows at word xo, into gx1/ge1
845fn projfx rows ulong xo ulong S ulong to void
846for t ulong S
847for i ulong FHID
848put gv i fx_f add xo add mul t FHID i
849put gve i 0.0f64
850end
851for r ulong rows
852o = add mul t rows r
853put gx1 o dotr r FHID
854put ge1 o dote FHID
855end
856end
857end
858
859note hnorm: head h of row t of gx1 (nh heads of 128) through q_norm/k_norm in gw, RoPE at
860note pos when rp, compared with the fixture head at word ref
861fn hnorm t ulong h ulong nh ulong rp bool ref ulong to void
862b = mul add mul t nh h 128
863for d ulong 128
864put gv d get gx1 add b d
865put gve d get ge1 add b d
866end
867rmsv 128 0
868if rp
869ropev fx_w add 8 t
870end
871for d ulong 128
872cmp get gy d fx_f add ref add b d get gye d
873end
874end
875
876fn op_qkv to int
877L = get gst 3
878S = get gst 5
879if or eq S 0 gt S 64
880op_die 7 tfx
881end
882xo = add 8 S
883qo = add xo mul S FHID
884ko = add qo mul S 4096
885vo = add ko mul S 512
886fx_need add vo mul S 512
887rp = fl_swa L
888nm_lay L
889nm_tx tq
890ld_t 4096 FHID
891projfx 4096 xo S
892nm_lay L
893nm_tx tqn
894ld_t 128 0
895for t ulong S
896for h ulong 32
897hnorm t h 32 rp qo
898end
899end
900nm_lay L
901nm_tx tk
902ld_t 512 FHID
903projfx 512 xo S
904nm_lay L
905nm_tx tkn
906ld_t 128 0
907for t ulong S
908for h ulong 4
909hnorm t h 4 rp ko
910end
911end
912nm_lay L
913nm_tx tv
914ld_t 512 FHID
915projfx 512 xo S
916cmp_x1 0 mul S 512 vo
917r = report toqkv
918ret r
919end
920
921note qk_dot: gv[0 .. 128) against gx2[kb ..); gacc 1 gets sum |q k|
922fn qk_dot kb ulong to f64
923s = 0.0f64
924a = 0.0f64
925for d ulong 128
926x = mul get gv d get gx2 add kb d
927s = add s x
928a = add a fabs x
929end
930put gacc 1 a
931ret s
932end
933
934note scores of the query head in gv against keys lo .. p of kv head g (gx2) into gsc/gse;
935note E_s = gamma_130 scale sum |q k| (the fixture q and k are exact inputs); returns count
936fn att_sc g ulong p ulong lo ulong to ulong
937sc = 0.08838834764831844055f64
938es = mul gam 130 sc
939n = add sub p lo 1
940for j ulong n
941s = qk_dot mul add mul add lo j 4 g 128
942put gsc j mul sc s
943put gse j mul es get gacc 1
944end
945ret n
946end
947
948note att_sm: softmax over the n scores and the sink sk, in place: gsc becomes p_j, gse the
949note relative bound rho_j = eps_j + eps_Z + gamma_(T+4), eps_j = E_sj + E_m + u |s_j - m| +
950note gamma_2, eps_Z = sum_k e_k eps_k / Z (sink included, weighted by its own term)
951fn sm_max n ulong sk f64 to void
952m = sk
953em = 0.0f64
954for j ulong n
955m = fmax m get gsc j
956em = fmax em get gse j
957end
958put gacc 6 m
959put gacc 7 em
960end
961
962note sm_eps: eps for a score x with bound e against the max m
963fn sm_eps x f64 e f64 to f64
964u = 5.9604644775390625e-8f64
965t = add e get gacc 7
966t = add t mul u fabs sub x get gacc 6
967ret add t gam 2
968end
969
970fn sm_exp n ulong to void
971m = get gacc 6
972z = 0.0f64
973ez = 0.0f64
974for j ulong n
975x = get gsc j
976ex = om_exp sub x m
977ej = sm_eps x get gse j
978put gsc j ex
979put gse j ej
980z = add z ex
981ez = add ez mul ex ej
982end
983put gacc 8 z
984put gacc 9 ez
985end
986
987fn att_sm n ulong sk f64 T ulong to void
988sm_max n sk
989sm_exp n
990es = om_exp sub sk get gacc 6
991z = add get gacc 8 es
992ez = add get gacc 9 mul es sm_eps sk 0.0f64
993ez = add div ez z gam add T 4
994for j ulong n
995put gsc j div get gsc j z
996put gse j add get gse j ez
997end
998end
999
1000note one head h of selected row t at position p: core output into gx1/ge1 row t;
1001note E_o = sum_j p_j |v_jd| (rho_j + gamma_T)
1002fn att_hd t ulong h ulong p ulong lo ulong qo ulong to void
1003g = div h 8
1004qb = mul add mul t 32 h 128
1005for d ulong 128
1006put gv d fx_f add qo add qb d
1007end
1008n = att_sc g p lo
1009att_sm n get gsk h get gst 4
1010put gacc 11 gam get gst 4
1011kb = mul add mul lo 4 g 128
1012for d ulong 128
1013put gx1 add qb d pv_d n add kb d
1014put ge1 add qb d get gacc 1
1015end
1016end
1017
1018note pv_d: sum_j p_j v_jd over the n kept keys, v of key j at gx3[b + 512 j]; gacc 1 gets
1019note the bound sum_j p_j |v_jd| (rho_j + gamma_T), gamma_T in gacc 11
1020fn pv_d n ulong b ulong to f64
1021s = 0.0f64
1022e = 0.0f64
1023for j ulong n
1024pv = mul get gsc j get gx3 add b mul j 512
1025s = add s pv
1026r = add get gse j get gacc 11
1027e = add e mul fabs pv r
1028end
1029put gacc 1 e
1030ret s
1031end
1032
1033note att_row: all 32 heads of selected row t, compared with the fixture core output at oo
1034fn att_row t ulong qo ulong oo ulong to void
1035p = fx_w add 8 t
1036if ge p get gst 4
1037op_die 7 tfx
1038end
1039lo = ulong 0
1040if and fl_swa get gst 3 gt add p 1 FWIN
1041lo = sub add p 1 FWIN
1042end
1043for h ulong 32
1044att_hd t h p lo qo
1045end
1046cmp_x1 mul t 4096 4096 oo
1047end
1048
1049note att_y: o_proj (in gw) of core row t of gx1/ge1, compared with the fixture at yo
1050fn att_y t ulong yo ulong to void
1051b = mul t 4096
1052for i ulong 4096
1053put gv i get gx1 add b i
1054put gve i get ge1 add b i
1055end
1056for r ulong FHID
1057y = dotr r 4096
1058cmp y fx_f add yo add mul t FHID r dote 4096
1059end
1060end
1061
1062fn att_kv ko ulong vo ulong n ulong to void
1063for i ulong n
1064put gx2 i fx_f add ko i
1065put gx3 i fx_f add vo i
1066end
1067end
1068
1069fn op_attn to int
1070T = get gst 4
1071S = get gst 5
1072if or or eq S 0 gt S 64 or eq T 0 gt T 2048
1073op_die 7 tfx
1074end
1075qo = add 8 S
1076ko = add qo mul S 4096
1077vo = add ko mul T 512
1078oo = add vo mul T 512
1079yo = add oo mul S 4096
1080fx_need add yo mul S FHID
1081att_kv ko vo mul T 512
1082nm_lay get gst 3
1083nm_tx tsnk
1084ld_t 32 0
1085for h ulong 32
1086put gsk h f64 get gw h
1087end
1088for t ulong S
1089att_row t qo oo
1090end
1091nm_lay get gst 3
1092nm_tx tob
1093ld_t FHID 4096
1094for t ulong S
1095att_y t yo
1096end
1097r = report toatt
1098ret r
1099end
1100
1101note rt_tie: true when some om top-6 score s_k (gtk ranks 1..6) is within 2 (E_k + E_7) of
1102note the rank-7 score, the only case in which a reference whose scores sit within 2 E of the
1103note om scores can rank the rank-7 expert above one of the om top-6.
1104fn rt_tie to bool
1105s7 = get gsg get gtk 6
1106e7 = get gsge get gtk 6
1107tie = false
1108for k ulong 6
1109c = get gtk k
1110if lt sub get gsg c s7 mul 2.0f64 add get gsge c e7
1111tie = true
1112end
1113end
1114ret tie
1115end
1116
1117note route: router logits of gv against gw (256 x 2048) into gsc/gse, sigmoid scores into
1118note gsg/gsge, top-7 by score into gtk (rank 7 is the boundary competitor), renormalized
1119note top-6 weights into gtw/gtwe. Returns rt_tie.
1120fn route to bool
1121g4 = gam 4
1122for e ulong 256
1123l = dotr e FHID
1124el = dote FHID
1125put gsc e l
1126put gse e el
1127sc = om_sig l
1128put gsg e sc
1129put gsge e add mul fabs mul sc sub 1.0f64 sc el mul g4 sc
1130put gch e 0
1131end
1132for k ulong 7
1133b = ulong 256
1134bv = 0.0f64
1135for e ulong 256
1136if eq get gch e 0
1137if or eq b 256 gt get gsg e bv
1138b = e
1139bv = get gsg e
1140end
1141end
1142end
1143put gtk k b
1144put gch b 1
1145end
1146s = 0.0f64
1147es = 0.0f64
1148for k ulong 6
1149s = add s get gsg get gtk k
1150es = add es get gsge get gtk k
1151end
1152g7 = gam 7
1153for k ulong 6
1154e = get gtk k
1155w = div get gsg e s
1156put gtw k w
1157put gtwe k add div add get gsge e mul w es s mul g7 w
1158end
1159t = rt_tie
1160ret t
1161end
1162
1163note rt_cmp: the reference top-6 of token t as an index -> weight map; an index missing from
1164note the om top-6 is accepted only on a tie token and only as the om rank-7 competitor
1165fn rt_cmp t ulong tie bool io ulong wo ulong to void
1166for k ulong 6
1167e = fx_w add io add mul t 6 k
1168rw = fx_f add wo add mul t 6 k
1169f = ulong 9
1170for j ulong 6
1171if eq get gtk j e
1172f = j
1173end
1174end
1175if lt f 6
1176cmp get gtw f rw get gtwe f
1177else
1178put gcm 2 add get gcm 2 1.0f64
1179if not and tie eq e get gtk 6
1180put gcm 1 add get gcm 1 1.0f64
1181end
1182end
1183end
1184if tie
1185put gcm 3 add get gcm 3 1.0f64
1186end
1187end
1188
1189fn op_route to int
1190L = get gst 3
1191T = get gst 4
1192if or eq T 0 gt T 512
1193op_die 7 tfx
1194end
1195lo = add 8 mul T FHID
1196io = add lo mul T 256
1197wo = add io mul T 6
1198fx_need add wo mul T 6
1199nm_lay L
1200nm_tx trt
1201ld_t 256 FHID
1202for t ulong T
1203for i ulong FHID
1204put gv i fx_f add 8 add mul t FHID i
1205put gve i 0.0f64
1206end
1207tie = route
1208for e ulong 256
1209cmp get gsc e fx_f add lo add mul t 256 e get gse e
1210end
1211rt_cmp t tie io wo
1212end
1213r = report tortr
1214ret r
1215end
1216
1217fn ld_mlp L ulong ex ulong j ulong I ulong to void
1218nm_lay L
1219if eq ex 998
1220nm_tx tdense
1221else
1222if lt ex 256
1223nm_tx texp
1224nm_u ex
1225nm_c 46
1226else
1227nm_tx tshp
1228end
1229end
1230if eq j 0
1231nm_tx tgp
1232ld_t I FHID
1233end
1234if eq j 1
1235nm_tx tup
1236ld_t I FHID
1237end
1238if eq j 2
1239nm_tx tdp
1240ld_t FHID I
1241end
1242end
1243
1244note mlp_g/mlp_u: gate and up projections of rows t0 .. t1 of gx0/ge0 into gx1 and gx2
1245fn mlp_g I ulong t0 ulong t1 ulong to void
1246for t ulong sub t1 t0
1247b = mul add t0 t FHID
1248for i ulong FHID
1249put gv i get gx0 add b i
1250put gve i get ge0 add b i
1251end
1252for r ulong I
1253o = add mul add t0 t I r
1254put gx1 o matdot r FHID
1255put ge1 o dote FHID
1256end
1257end
1258end
1259
1260fn mlp_u I ulong t0 ulong t1 ulong to void
1261for t ulong sub t1 t0
1262b = mul add t0 t FHID
1263for i ulong FHID
1264put gv i get gx0 add b i
1265put gve i get ge0 add b i
1266end
1267for r ulong I
1268o = add mul add t0 t I r
1269put gx2 o matdot r FHID
1270put ge2 o dote FHID
1271end
1272end
1273end
1274
1275note mlp_h: h = silu(g) up into gx1/ge1; E_s = 1.1 E_g + gamma_4 |s| (1.1 bounds max |silu'|),
1276note E_h = |up| E_s + |s| E_up + gamma_1 |h|
1277fn mlp_h I ulong t0 ulong t1 ulong to void
1278for i ulong mul sub t1 t0 I
1279mlp_h1 add mul t0 I i
1280end
1281end
1282
1283fn mlp_h1 o ulong to void
1284g = get gx1 o
1285s = mul g om_sig g
1286es = mul 1.1f64 get ge1 o
1287es = add es mul gam 4 fabs s
1288up = get gx2 o
1289h = mul s up
1290put gx1 o h
1291e = add mul fabs up es mul fabs s get ge2 o
1292put ge1 o add e mul gam 1 fabs h
1293end
1294
1295fn mlp_d I ulong t0 ulong t1 ulong to void
1296for t ulong sub t1 t0
1297b = mul add t0 t I
1298for i ulong I
1299put gv i get gx1 add b i
1300put gve i get ge1 add b i
1301end
1302for r ulong FHID
1303o = add mul add t0 t FHID r
1304put gx3 o matdot r I
1305put ge3 o dote I
1306end
1307end
1308end
1309
1310note mlp: FlintMLP of layer L (ex >= 256: shared expert) or routed expert ex, intermediate
1311note size I, rows t0 .. t1 of gx0/ge0 into gx3/ge3: down(silu(gate x) up x)
1312fn mlp L ulong ex ulong I ulong t0 ulong t1 ulong to void
1313ld_mlp L ex 0 I
1314mlp_g I t0 t1
1315ld_mlp L ex 1 I
1316mlp_u I t0 t1
1317mlp_h I t0 t1
1318ld_mlp L ex 2 I
1319mlp_d I t0 t1
1320end
1321
1322fn cmp_x3 n ulong fo ulong to void
1323for i ulong n
1324cmp get gx3 i fx_f add fo i get ge3 i
1325end
1326end
1327
1328fn op_mlp ex ulong I ulong nm view uchar * to int
1329L = get gst 3
1330T = get gst 4
1331if or eq T 0 gt T 8
1332op_die 7 tfx
1333end
1334yo = add 8 mul T FHID
1335fx_need add yo mul T FHID
1336fx_x0 8 T
1337mlp L ex I 0 T
1338cmp_x3 mul T FHID yo
1339r = report nm
1340ret r
1341end
1342
1343note moe_acc: add w_k y_k of token t into gacm with bound gace and sum |w y| in gaca
1344fn moe_acc t ulong w f64 ew f64 to void
1345for i ulong FHID
1346o = add mul t FHID i
1347y = get gx3 o
1348put gacm o add get gacm o mul w y
1349e = add get gace o mul fabs w get ge3 o
1350put gace o add e mul fabs y ew
1351put gaca o add get gaca o fabs mul w y
1352end
1353end
1354
1355note op_moe: FlintSparseMoeBlock = sum_k w_k expert_k(x) + shared_expert(x); bound
1356note sum_k (|w_k| E_yk + |y_k| E_wk) + E_S + gamma_8 (sum_k |w_k y_k| + |S|)
1357fn moe_rt t ulong to void
1358for i ulong FHID
1359put gv i get gx0 add mul t FHID i
1360put gve i 0.0f64
1361end
1362tie = route
1363put gtie t 0
1364if tie
1365put gtie t 1
1366put gcm 3 add get gcm 3 1.0f64
1367end
1368for k ulong 6
1369j = add mul t 6 k
1370put gmk j get gtk k
1371put gmw j get gtw k
1372put gmwe j get gtwe k
1373end
1374end
1375
1376fn moe_ex L ulong t ulong to void
1377for k ulong 6
1378j = add mul t 6 k
1379mlp L get gmk j 512 t add t 1
1380moe_acc t get gmw j get gmwe j
1381end
1382end
1383
1384fn moe_cmp t ulong yo ulong to void
1385g8 = gam 8
1386for i ulong FHID
1387o = add mul t FHID i
1388v = add get gacm o get gx3 o
1389e = add get gace o get ge3 o
1390a = add get gaca o fabs get gx3 o
1391cmp v fx_f add yo o add e mul g8 a
1392end
1393end
1394
1395fn op_moe to int
1396L = get gst 3
1397T = get gst 4
1398if or eq T 0 gt T 8
1399op_die 7 tfx
1400end
1401yo = add 8 mul T FHID
1402fx_need add yo mul T FHID
1403fx_x0 8 T
1404nm_lay L
1405nm_tx trt
1406ld_t 256 FHID
1407for t ulong T
1408moe_rt t
1409end
1410for i ulong mul T FHID
1411put gacm i 0.0f64
1412put gace i 0.0f64
1413put gaca i 0.0f64
1414end
1415for t ulong T
1416if eq get gtie t 0
1417moe_ex L t
1418end
1419end
1420mlp L 999 1024 0 T
1421for t ulong T
1422if eq get gtie t 0
1423moe_cmp t yo
1424end
1425end
1426r = report tomoe
1427ret r
1428end
1429
1430note selftest: om_exp, om_sig and om_sc against Wolfram Language values (20 digits)
1431fn st_one om f64 ref f64 rel f64 to void
1432cmp om ref mul rel fabs ref
1433end
1434
1435fn op_self to int
1436put gst 3 0
1437st_one om_exp 1.0f64 2.718281828459045235f64 2.0e-15f64
1438st_one om_exp -20.25f64 1.605228055185611609e-9f64 2.0e-15f64
1439st_one om_exp 8.125f64 3377.867931673534884f64 2.0e-15f64
1440st_one om_exp -87.5f64 9.98235093056924756e-39f64 2.0e-15f64
1441st_one om_sc 1.0f64 false 0.8414709848078965067f64 2.0e-15f64
1442st_one om_sc 1.0f64 true 0.5403023058681397174f64 2.0e-15f64
1443st_one om_sc 518.75f64 false -0.3776084404242524912f64 2.0e-15f64
1444st_one om_sc 518.75f64 true -0.925965369611825039f64 2.0e-15f64
1445st_one om_sc 0.001f64 false 9.99999833333341667e-4f64 2.0e-15f64
1446st_one om_sc 3.0f64 true -0.989992496600445457f64 2.0e-15f64
1447st_one om_sig 1.5f64 0.8175744761936436596f64 2.0e-15f64
1448rope_cs 519 1
1449st_one get gacc 3 -0.982429600544478341f64 1.0e-12f64
1450st_one get gacc 4 -0.1866335446108676431f64 1.0e-12f64
1451st_one get gacc 5 2.683614206850094353e-4f64 1.0e-12f64
1452r = report tostt
1453ret r
1454end
1455
1456note Complete bounded host forward, two prompt tokens maximum. No teacher forcing:
1457note only token IDs are inputs; the fixture's intermediate tensors are comparison targets.
1458note This f64-compute/BF16-storage numerical profile is NOT bf16-activation qualification.
1459note Integration acceptance is |om-ref| <= 8 gamma_2048 (1+|ref|), fixed before execution.
1460note The existing per-operator bounds remain the tighter local checks.
1461fn fw_cmp n ulong to void
1462o = get gfs 1
1463for i ulong n
1464r = fx_f add o i
1465e = mul mul 4.0f64 gam 2048 add 1.0f64 fabs r
1466cmp get gfa i r e
1467end
1468put gfs 1 add o n
1469end
1470
1471fn fw_hidden to void
1472for i ulong mul get gfs 0 FHID
1473put gfa i get gfh i
1474end
1475fw_cmp mul get gfs 0 FHID
1476end
1477
1478fn fw_norm L ulong post bool to void
1479nm_lay L
1480if post
1481nm_tx tln1
1482else
1483nm_tx tln0
1484end
1485ld_t FHID 0
1486for t ulong get gfs 0
1487for i ulong FHID
1488put gv i get gfh add mul t FHID i
1489put gve i 0.0f64
1490end
1491rmsv FHID 0
1492for i ulong FHID
1493o = add mul t FHID i
1494put gx0 o get gy i
1495put ge0 o 0.0f64
1496put gfa o get gy i
1497end
1498end
1499fw_cmp mul get gfs 0 FHID
1500end
1501
1502fn fw_proj rows ulong to void
1503for t ulong get gfs 0
1504for i ulong FHID
1505put gv i get gx0 add mul t FHID i
1506put gve i 0.0f64
1507end
1508for r ulong rows
1509put gx1 add mul t rows r matdot r FHID
1510end
1511end
1512end
1513
1514fn fw_head t ulong h ulong nh ulong rp bool to void
1515b = mul add mul t nh h 128
1516for d ulong 128
1517put gv d get gx1 add b d
1518put gve d 0.0f64
1519end
1520rmsv 128 0
1521if rp
1522ropev t
1523end
1524for d ulong 128
1525if eq nh 32
1526put gfq add b d get gy d
1527else
1528put gx2 add b d get gy d
1529end
1530end
1531end
1532
1533fn fw_qk L ulong q bool to void
1534rows = ulong 512
1535nh = ulong 4
1536nm_lay L
1537if q
1538nm_tx tq
1539rows = ulong 4096
1540nh = ulong 32
1541else
1542nm_tx tk
1543end
1544ld_t rows FHID
1545fw_proj rows
1546nm_lay L
1547if q
1548nm_tx tqn
1549else
1550nm_tx tkn
1551end
1552ld_t 128 0
1553for t ulong get gfs 0
1554for h ulong nh
1555fw_head t h nh fl_swa L
1556end
1557end
1558end
1559
1560fn fw_atthead t ulong h ulong to void
1561b = mul add mul t 32 h 128
1562for d ulong 128
1563put gv d get gfq add b d
1564end
1565g = div h 8
1566n = att_sc g t 0
1567att_sm n get gsk h get gfs 0
1568put gacc 11 0.0f64
1569for d ulong 128
1570v = pv_d n add mul g 128 d
1571put gfo add b d v
1572end
1573end
1574
1575fn fw_att L ulong to void
1576fw_qk L true
1577fw_qk L false
1578nm_lay L
1579nm_tx tv
1580ld_t 512 FHID
1581fw_proj 512
1582for i ulong mul get gfs 0 512
1583put gx3 i get gx1 i
1584end
1585nm_lay L
1586nm_tx tsnk
1587ld_t 32 0
1588for h ulong 32
1589put gsk h f64 get gw h
1590end
1591for t ulong get gfs 0
1592for h ulong 32
1593fw_atthead t h
1594end
1595end
1596nm_lay L
1597nm_tx tob
1598ld_t FHID 4096
1599for t ulong get gfs 0
1600for i ulong 4096
1601put gv i get gfo add mul t 4096 i
1602put gve i 0.0f64
1603end
1604for r ulong FHID
1605put gfa add mul t FHID r matdot r 4096
1606end
1607end
1608fw_cmp mul get gfs 0 FHID
1609for i ulong mul get gfs 0 FHID
1610put gfh i add get gfh i get gfa i
1611end
1612end
1613
1614fn fw_moe L ulong to void
1615T = get gfs 0
1616nm_lay L
1617nm_tx trt
1618ld_t 256 FHID
1619for t ulong T
1620moe_rt t
1621end
1622for i ulong mul T FHID
1623put gacm i 0.0f64
1624put gace i 0.0f64
1625put gaca i 0.0f64
1626end
1627for t ulong T
1628moe_ex L t
1629end
1630mlp L 999 1024 0 T
1631for i ulong mul T FHID
1632put gx3 i add get gacm i get gx3 i
1633end
1634end
1635
1636fn fw_layer L ulong to void
1637fw_norm L false
1638fw_att L
1639fw_norm L true
1640if lt L 2
1641mlp L 998 6144 0 get gfs 0
1642else
1643fw_moe L
1644end
1645for i ulong mul get gfs 0 FHID
1646put gfa i get gx3 i
1647end
1648fw_cmp mul get gfs 0 FHID
1649for i ulong mul get gfs 0 FHID
1650put gfh i add get gfh i get gfa i
1651end
1652fw_hidden
1653put gst 3 L
1654r = report tofwd
1655if ne r 0
1656exit 1
1657end
1658end
1659
1660fn fw_embed to void
1661for t ulong get gfs 0
1662id = fx_w add 8 t
1663if ge id 150144
1664op_die 7 tfx
1665end
1666put gst 7 0
1667nm_tx tembd
1668ld_rows 150144 FHID id 1
1669for i ulong FHID
1670put gfh add mul t FHID i f64 get gw i
1671end
1672end
1673fw_hidden
1674end
1675
1676fn fw_final to void
1677put gst 7 0
1678nm_tx tfnorm
1679ld_t FHID 0
1680for t ulong get gfs 0
1681for i ulong FHID
1682put gv i get gfh add mul t FHID i
1683put gve i 0.0f64
1684end
1685rmsv FHID 0
1686for i ulong FHID
1687o = add mul t FHID i
1688put gfh o get gy i
1689put gfa o get gy i
1690end
1691end
1692fw_cmp mul get gfs 0 FHID
1693end
1694
1695fn fw_logits to void
1696for t ulong get gfs 0
1697for r ulong 150144
1698if eq rem r 2048 0
1699put gst 7 0
1700nm_tx tlmhead
1701n = sub ulong 150144 r
1702if gt n 2048
1703n = ulong 2048
1704end
1705ld_rows 150144 FHID r n
1706for i ulong FHID
1707put gv i get gfh add mul t FHID i
1708put gve i 0.0f64
1709end
1710end
1711v = matdot rem r 2048 FHID
1712put gfa 0 v
1713fw_cmp 1
1714end
1715end
1716end
1717
1718fn op_forward to int
1719T = get gst 4
1720if or eq T 0 gt T 2
1721op_die 7 tfx
1722end
1723note words = header + IDs + embeddings + five tensors/layer + final norm + logits
1724fx_need add add add 8 T mul mul T FHID 247 mul T 150144
1725put gfs 0 T
1726put gfs 1 add 8 T
1727fw_embed
1728for L ulong 49
1729fw_layer L
1730end
1731fw_final
1732fw_logits
1733put gst 3 49
1734r = report tofwd
1735ret r
1736end
1737
1738fn op_code to ulong
1739if op_argeq 1 tormsn
1740ret 1
1741end
1742if op_argeq 1 toqkv
1743ret 2
1744end
1745if op_argeq 1 toatt
1746ret 3
1747end
1748if op_argeq 1 tortr
1749ret 4
1750end
1751if op_argeq 1 toshr
1752ret 5
1753end
1754if op_argeq 1 toexp
1755ret 6
1756end
1757if op_argeq 1 tomoe
1758ret 7
1759end
1760if op_argeq 1 tofwd
1761ret 8
1762end
1763op_die 2 tu
1764ret 0
1765end
1766
1767fn main to int
1768c = argcnt
1769if and ne c 5 ne c 2
1770op_die 2 tu
1771end
1772o = ulong 0
1773n = ulong c
1774for i ulong n
1775r = argread i ga o sub len ga o
1776if lt r 0
1777op_die 2 tu
1778end
1779put gas i o
1780put gal i ulong r
1781o = add o ulong r
1782end
1783cm_reset
1784if eq c 2
1785if not op_argeq 1 tostt
1786op_die 2 tu
1787end
1788s = op_self
1789ret s
1790end
1791op = op_code
1792rd_fx
1793rd_tsv
1794if or ne fx_w 0 1347374150 ne fx_w 1 op
1795op_die 7 tfx
1796end
1797put gst 2 op
1798put gst 3 fx_w 2
1799put gst 4 fx_w 3
1800put gst 5 fx_w 4
1801put gst 6 fx_w 5
1802if eq op 8
1803s = op_forward
1804ret s
1805end
1806if eq op 1
1807s = op_rmsn
1808ret s
1809end
1810if eq op 2
1811s = op_qkv
1812ret s
1813end
1814if eq op 3
1815s = op_attn
1816ret s
1817end
1818if eq op 4
1819s = op_route
1820ret s
1821end
1822if eq op 5
1823s = op_mlp 999 1024 toshr
1824ret s
1825end
1826if eq op 6
1827e = get gst 6
1828if ge e 256
1829op_die 7 tfx
1830end
1831s = op_mlp e 512 toexp
1832ret s
1833end
1834s = op_moe
1835ret s
1836end