interplane.Get Omega
Read in your language

omega-helix

This project has not described itself yet.

Brought by drakestapleton

branch main Branches Tags History

omega-helix / native / cli.om

1821 lines · 32788 bytes Raw Blame History
1note SPDX-License-Identifier: AGPL-3.0-only
2note omegallm cli.om (aien-train#52): the ollm commands (CONTRACT section 7). The platform unit
3note (lio.om or eio.om) follows it and holds main.
4note Command words are matched as little-endian u64 packings of their bytes (Wolfram
5note FromDigits[Reverse[ToCharacterCode[w]],256]): prep 1885696624, init 1953066601,
6note grad 1684107879, train 474214396532, eval 1818326629, base 1702060386, sample 111516417089907,
7note gcheck 118074647274343, mathck 118074698391917, adamck 118074781033569; v1: bpefit
8note 127996038312034, bpeenc 109325798699106, bpedec 109287127216226 (bpe.om); serve 435778905459
9note (serve.om on aarch64, noserve.om elsewhere). Fine-tune: tune 1701737844.
10note Exit codes: 0 ok; 2 usage; 10-15 checkpoint refusals (ckpt.om); 17 a looped checkpoint given
11note to a command that does not run loops (grow and the specialist commands; init, train, eval,
12note sample, grad, gcheck, tune, kvcmp, serve and the GB10 commands run them); 20 data file shorter than
13note T + 1 bytes; 21 data over 2^25 bytes or OFFS not exactly 8 B bytes; 22 an OFFS offset with
14note o + T >= N; 23 eval W = 0 or W T + 1 > M; 24 prep story over 999999 bytes; 40 gcheck over
15note tolerance; 41 adamck over tolerance; 42 mathck over tolerance; 70 I/O.
16
17fn ocmdw to ulong
18r = oargr 1
19if or le r 0 gt r 8
20ret 0
21end
22v = ulong 0
23for k ulong ulong r
24v = or v shl ulong get oarg k mul k 8
25end
26ret v
27end
28
29fn ocmdlo to ulong
30r = oargr 1
31if or le r 8 gt r 16
32ret 0
33end
34v = ulong 0
35for k ulong 8
36v = or v shl ulong get oarg k mul k 8
37end
38ret v
39end
40
41fn ocmdhi to ulong
42r = oargr 1
43if or le r 8 gt r 16
44ret 0
45end
46v = ulong 0
47n = sub ulong r 8
48for k ulong n
49v = or v shl ulong get oarg add 8 k mul k 8
50end
51ret v
52end
53
54note run the command named by argument 1; the platform main checks that it exists. ogcmd takes the
55note GPU commands on the gb10 target (gpu/gg.om) and none elsewhere (nogpu.om).
56fn ocmd to int
57w = ocmdw
58if ogcmd w
59ret 0
60end
61if eq w 1885696624
62cmdprep
63ret 0
64end
65if eq w 127996038312034
66cmdbpfit
67ret 0
68end
69if eq w 109325798699106
70cmdbpenc
71ret 0
72end
73if eq w 109287127216226
74cmdbpdec
75ret 0
76end
77if eq w 1953066601
78cmdinit
79ret 0
80end
81if eq w 8391157623388991081
82cmdinitfast
83ret 0
84end
85if eq w 128038904163171
86cmdckfast
87ret 0
88end
89if eq w 224250981219
90cmdckf64
91ret 0
92end
93if eq w 1684107879
94cmdgrad
95ret 0
96end
97if eq w 474214396532
98cmdtrain
99ret 0
100end
101if eq w 121398885512310
102cmdvtrain
103ret 0
104end
105if eq w 1818326629
106cmdeval
107ret 0
108end
109if eq w 465491617142
110cmdveval
111ret 0
112end
113if eq w 1702060386
114cmdbase
115ret 0
116end
117if eq w 111516417089907
118cmdsamp
119ret 0
120end
121if eq w 28548202775016302
122cmdnsamp
123ret 0
124end
125if eq w 118074647274343
126cmdgchk
127ret 0
128end
129if eq w 118074698391917
130cmdmath
131ret 0
132end
133if eq w 118074781033569
134cmdadamck
135ret 0
136end
137if eq w 435778905459
138cmdserve
139ret 0
140end
141if eq w 1701737844
142cmdtune
143ret 0
144end
145note grow: append initially silent residual layers and restart training.
146if eq w 2003792487
147cmdgrow
148ret 0
149end
150note kvcmp (ham/ollm-speed) 482871572075
151if eq w 482871572075
152cmdkvcmp
153ret 0
154end
155note adaptcmp 8101240656462177377 (adapteval and adaptsamp are 9 bytes: under w = 0 below)
156if eq w 8101240656462177377
157cmdadcmp
158ret 0
159end
160if eq w 112624400429684
161cmdtrainf
162ret 0
163end
164if eq w 30506458620916852
165cmdttteval
166ret 0
167end
168if eq w 7809653406635421540
169cmddolaeval
170ret 0
171end
172if eq w 31645462707860596
173cmdtttsamp
174ret 0
175end
176if eq w 500236120179
177cmdspext
178ret 0
179end
180if eq w 482921771123
181cmdspapp
182ret 0
183end
184if eq w 123615088701555
185if and ge oargc 8 sp2_is_sp2 7
186cmdspwsamp
187else
188cmdspsamp
189end
190ret 0
191end
192if eq w 119165853986931
193cmdspeval
194ret 0
195end
196if eq w 111499018330982
197cmdfcache
198ret 0
199end
200if eq w 27978647094784628
201cmdtrainfc
202ret 0
203end
204if eq w 30792271479792486
205cmdfcachem
206ret 0
207end
208if eq w 28542713673379955
209cmdspmerge
210ret 0
211end
212if eq w 30227092724740211
213cmdspstack
214ret 0
215end
216if eq w 31369549780578919
217cmdgrowpro
218ret 0
219end
220if eq w 495773380723
221cmdspens
222ret 0
223end
224if eq w 123598092333171
225cmdspclip
226ret 0
227end
228if eq w 28273260578173043
229cmdspguard
230ret 0
231end
232if eq w 28542713840693363
233cmdspforge
234ret 0
235end
236if eq w 32778062745792627
237cmdspcryst
238ret 0
239end
240if eq w 30506458621112435
241cmdspweval
242ret 0
243end
244if eq w 31645462708056179
245cmdspwsamp
246ret 0
247end
248if eq w 7311138133506355297
249cmdapuweave
250ret 0
251end
252if eq w 6778223
253cmdomg_cli
254ret 0
255end
256if eq w 32485562537636449
257cmdarboris_cli
258ret 0
259end
260if eq w 7759218
261rr = rev_ollm_cmd
262if ne rr 0
263exit rr
264end
265ret 0
266end
267if eq w 0
268lo = ocmdlo
269hi = ocmdhi
270if and eq lo 7020664736201273459 eq hi 28784
271cmdspchainapp
272ret 0
273end
274if and eq lo 7308895112352985203 eq hi 7102838
275cmdspchaineval
276ret 0
277end
278if and eq lo 8317701428883976307 eq hi 7368033
279cmdspchainsamp
280ret 0
281end
282if and eq lo 7022912219706909793 eq hi 108
283cmdadeval
284ret 0
285end
286if and eq lo 7881707766813910113 eq hi 112
287cmdadsamp
288ret 0
289end
290end
291exit 2
292end
293
294note init CK V T D H F L B SEED TOTAL LRMICRO [G [LP LC NP]]; lr = f64(LRMICRO)/1000000.0. Without G
295note the file is OLLMCK01 (G = 1); with G it is OLLMCK02 (CONTRACT-V1 section 6). LP LC NP make the
296note LC layers from layer LP a core that runs NP times with the same weights (model.om, docs/LOOP.md):
297note the file is then OLLMCK03. NP 1 is no loop and gives the OLLMCK02 file; NP 0 is exit 2; a core
298note that is empty or does not fit in the L layers is exit 12.
299fn oinit_setup to void
300na = oargc
301if and and ne na 13 ne na 14 ne na 17
302exit 2
303end
304for i ulong 7
305put og i oargu add 3 i
306end
307put og 57 0
308put og 58 0
309put og 59 0
310if eq na 17
311oloopa
312end
313if not ogeom
314exit 12
315end
316put og 40 oargu 10
317tt = oargu 11
318if gt tt 999999
319exit 12
320end
321g = ulong 1
322put og 46 1
323if ge na 14
324g = oargu 13
325put og 46 2
326end
327if not ogok2 g
328exit 12
329end
330put og 44 g
331put og 42 tt
332put og 43 bitcast ulong div f64 oargu 12 1000000.0
333put og 41 0
334oinit
335end
336
337fn cmdinit to void
338oinit_setup
339ocksave 2
340oputc 73
341oputc 32
342oputu get og 8
343oputc 10
344oflush
345end
346
347fn cmdinitfast to void
348oinit_setup
349fl = or or OCK5F_F32 OCK5F_BF16 OCK5F_ZMV
350orig = get og 46
351if gt get og 59 1
352fl = or fl OCK5F_LOOP
353orig = 3
354end
355ock5_save_cpu 2 fl orig
356oputc 73
357oputc 32
358oputu get og 8
359oputc 10
360oflush
361end
362
363fn ock5_mv_is_zero to bool
364n = mul 2 get og 8
365i0 = ulong 0
366while lt i0 n bound 999999
367cnt = sub n i0
368if gt cnt OCK5C
369cnt = OCK5C
370end
371for j ulong cnt
372if ne bitcast uint f32 omvg add i0 j 0
373ret false
374end
375end
376i0 = add i0 cnt
377end
378ret true
379end
380
381fn cmdckfast_mode ai ulong to ulong
382r = oargr ai
383if or le r 0 gt r 8
384exit 2
385end
386v = ulong 0
387for k ulong ulong r
388v = or v shl ulong get oarg k mul k 8
389end
390if eq v 3289958
391ret 0
392end
393if eq v 909207138
394ret 1
395end
396if eq v 225224062574
397ret 2
398end
399exit 2
400end
401
402fn cmdckfast to void
403na = oargc
404if and ne na 4 ne na 5
405exit 2
406end
407md = ulong 1
408if eq na 5
409md = cmdckfast_mode 4
410end
411ockloop 2
412fl = OCK5F_F32
413if ge md 1
414fl = or fl OCK5F_BF16
415end
416if ock5_mv_is_zero
417fl = or fl OCK5F_ZMV
418end
419if gt get og 59 1
420fl = or fl OCK5F_LOOP
421end
422orig = get og 46
423if eq orig 5
424orig = get ock5_st 1
425end
426if or eq orig 0 gt orig 3
427orig = 2
428if gt get og 59 1
429orig = 3
430end
431end
432ock5_save_cpu 3 fl orig
433oputc 67
434oputc 70
435oputc 32
436oputu get og 8
437oputc 32
438oputu fl
439oputc 10
440oflush
441end
442
443fn cmdckf64 to void
444if ne oargc 4
445exit 2
446end
447ockloop 2
448if eq get og 46 5
449orig = get ock5_st 1
450if or eq orig 0 gt orig 3
451orig = 2
452end
453if gt get og 59 1
454orig = 3
455end
456put og 46 orig
457end
458ocksave 3
459oputc 67
460oputc 54
461oputc 32
462oputu get og 8
463oputc 32
464oputu get og 46
465oputc 10
466oflush
467end
468
469note the loop arguments LP LC NP of init (argv 14 .. 16) into og 57 .. 59; NP 1 leaves no loop
470fn oloopa to void
471np = oargu 16
472if eq np 0
473exit 2
474end
475if gt np 1
476put og 57 oargu 14
477put og 58 oargu 15
478put og 59 np
479end
480end
481
482note little-endian u64 at ochk[o..o+8)
483fn ochk64 o ulong to ulong
484v = ulong 0
485for k ulong 8
486x = ulong get ochk add o k
487v = or v shl x mul k 8
488end
489ret v
490end
491
492note row b from offset ochk[b] (checked by ooffok)
493fn ooffrow b ulong to void
494obrow b ochk64 mul b 8
495end
496
497note exit 22 when any offset has o >= N - T (written so that an offset near 2^64 cannot wrap)
498fn ooffok to void
499n = get odn 0
500tt = get og 1
501for b ulong get og 6
502if ge ochk64 mul b 8 sub n tt
503exit 22
504end
505end
506end
507
508note grad CK DATA OFFS OUT: the gradient of CONTRACT-V1 section 5 (G blocks, K = 1)
509fn cmdgrad to void
510if ne oargc 6
511exit 2
512end
513ockloop 2
514odopen 3
515ooffrd 4
516ooffok
517for b ulong get og 6
518ooffrow b
519end
520ls = ograd
521ogrsave 5
522oputc 71
523oputc 32
524oputx bitcast ulong ls
525oputc 32
526oputl rmic ls
527oputc 10
528oflush
529end
530
531note train CK TRAIN UNTIL EVERY [K]: steps step .. min(UNTIL, TOTAL)-1; after step s, when
532note (s+1) mod EVERY = 0 or s+1 is the end, save CK and print `T s+1 loss_micro(step s)`.
533note K worker processes (CONTRACT-V1 section 5.1): K < 1, K > G or G mod K != 0 is exit 29.
534fn cmdtrain to void
535na = oargc
536if and ne na 6 ne na 7
537exit 2
538end
539ockloop 2
540odopen 3
541un = oargu 4
542ev = oargu 5
543if eq ev 0
544exit 2
545end
546k = ulong 1
547if eq na 7
548k = oargu 6
549end
550gg = get og 44
551if or eq k 0 gt k gg
552exit 29
553end
554if ne rem gg k 0
555exit 29
556end
557put og 45 k
558en = get og 42
559if lt un en
560en = un
561end
562while lt get og 41 en bound 999999
563ls = ostep
564s1 = get og 41
565if or eq rem s1 ev 0 eq s1 en
566ocksave 2
567oputc 84
568oputc 32
569oputu s1
570oputc 32
571oputl rmic ls
572oputc 10
573oflush
574end
575end
576end
577
578note vtrain CK TRAIN UNTIL EVERY NP_MIN NP_MAX: variable-NP looped-core training (COCONUT/recurrent-depth).
579note Cycles cur_np across [NP_MIN .. NP_MAX] per step, preserving the base checkpoint's NP in the saved header.
580fn cmdvtrain to void
581if ne oargc 8
582exit 2
583end
584ockloop 2
585if eq get og 58 0
586exit 17
587end
588odopen 3
589un = oargu 4
590ev = oargu 5
591npmin = oargu 6
592npmax = oargu 7
593if or eq ev 0 or eq npmin 0 or gt npmin npmax gt npmax 6
594exit 2
595end
596orig = get og 59
597put og 59 npmax
598if not ogeom
599exit 12
600end
601put og 59 orig
602x0 = ogeom
603put og 45 1
604en = get og 42
605if lt un en
606en = un
607end
608ovtloop orig en ev npmin npmax
609end
610
611fn ovtloop orig ulong en ulong ev ulong npmin ulong npmax ulong to void
612span = add sub npmax npmin 1
613while lt get og 41 en bound 999999
614cnp = add npmin rem get og 41 span
615put og 59 cnp
616if not ogeom
617exit 12
618end
619ls = ostep
620s1 = get og 41
621if or eq rem s1 ev 0 eq s1 en
622put og 59 orig
623x1 = ogeom
624ocksave 2
625ovtprint s1 cnp ls
626end
627end
628put og 59 orig
629x2 = ogeom
630end
631
632fn ovtprint s1 ulong cnp ulong ls f64 to void
633oputc 86
634oputc 84
635oputc 32
636oputu s1
637oputc 32
638oputu cnp
639oputc 32
640oputl rmic ls
641oputc 10
642oflush
643end
644
645note veval CK HELD W NP [MERGES]: evaluate a looped OLLMCK03 checkpoint at arbitrary loop count NP.
646fn cmdveval to void
647na = oargc
648if and ne na 6 ne na 7
649exit 2
650end
651ockloop 2
652ww = oargu 4
653np = oargu 5
654if or eq np 0 gt np 6
655exit 2
656end
657if gt get og 58 0
658put og 59 np
659if not ogeom
660exit 12
661end
662end
663v = get og 0
664if gt v 256
665if eq na 6
666exit 2
667end
668obload 6 v
669end
670odopen 3
671ovevrun ww np
672end
673
674fn ovevrun ww ulong np ulong to void
675tt = get og 1
676m = get odn 0
677if or eq ww 0 gt ww div sub m 1 tt
678exit 23
679end
680v = get og 0
681ls = 0.0f64
682nb = ulong 0
683for w ulong ww
684obrow 0 mul w tt
685ls = add ls ofwd 0
686if gt v 256
687nb = add nb oevnb tt
688end
689end
690lo = div ls f64 ww
691bp = div lo 0.6931471805599453
692if gt v 256
693bp = div mul bp f64 mul ww tt f64 nb
694end
695oputc 86
696oputc 69
697oputc 32
698oputu get og 41
699oputc 32
700oputu np
701oputc 32
702oputu ww
703oputc 32
704oputl rmic lo
705oputc 32
706oputl rmic bp
707oputc 10
708oflush
709end
710
711note eval CK HELD W [MERGES]: windows at 0, T, .., (W-1)T; prints `E step W loss_micro bpb_micro`.
712note V > 256 needs MERGES (else exit 2); bpb = ((loss/ln2) f64(W T))/f64(nbytes) with nbytes the
713note decoded length of the W T targets (CONTRACT-V1 section 7.1); V = 256 keeps loss/ln2.
714fn cmdeval to void
715na = oargc
716if and ne na 5 ne na 6
717exit 2
718end
719ockloop 2
720ww = oargu 4
721v = get og 0
722if gt v 256
723if eq na 5
724exit 2
725end
726obload 5 v
727end
728odopen 3
729tt = get og 1
730m = get odn 0
731note W T + 1 > N, tested as W > (N - 1) / T so that a huge W cannot wrap W T
732if eq ww 0
733exit 23
734end
735if gt ww div sub m 1 tt
736exit 23
737end
738ls = 0.0f64
739nb = ulong 0
740for w ulong ww
741obrow 0 mul w tt
742ls = add ls ofwd 0
743if gt v 256
744nb = add nb oevnb tt
745end
746end
747lo = div ls f64 ww
748bp = div lo 0.6931471805599453
749if gt v 256
750bp = div mul bp f64 mul ww tt f64 nb
751end
752oputc 69
753oputc 32
754oputu get og 41
755oputc 32
756oputu ww
757oputc 32
758oputl rmic lo
759oputc 32
760oputl rmic bp
761oputc 10
762oflush
763end
764
765note decoded bytes of the targets oby[0 .. n)
766fn oevnb n ulong to ulong
767s = ulong 0
768for t ulong n
769s = add s obtlen get oby t
770end
771ret s
772end
773
774note append token c to the context window obx[0 .. CT), keeping the last T tokens
775fn ocpush c ulong to void
776cn = get og 9
777tt = get og 1
778if lt cn tt
779put obx cn c
780put og 9 add cn 1
781else
782for i ulong sub tt 1
783put obx i get obx add i 1
784end
785put obx sub tt 1 c
786end
787end
788
789note sample CK SEED N TEMPMILLI PROMPT [MERGES]. Context = token 0, then PROMPT (bytes when V = 256,
790note its encoding when V > 256, CONTRACT-V1 section 7.2); the model reads the last T context tokens
791note at positions 0..; the next token comes from the logits at the last position.
792note TEMPMILLI 0: argmax (lowest index on ties), no draw. Otherwise softmax(logits/(TEMPMILLI/1000))
793note and u = (splitmix64 stream out >> 11) 2^-53 with state = SEED, out = mix(state += GOLD);
794note the token is the smallest c with u < p[0] + .. + p[c], else the last c with p[c] > 0.
795note A generated token 0 ends the output and is not written; another writes its decoded bytes.
796fn cmdsamp to void
797na = oargc
798if and ne na 7 ne na 8
799exit 2
800end
801ockloop 2
802st = oargu 3
803nn = oargu 4
804if gt nn 999999
805exit 2
806end
807tm = oargu 5
808v = get og 0
809if gt v 256
810if eq na 7
811exit 2
812end
813obload 7 v
814end
815ochipup
816osampleloaded st nn tm
817end
818
819note nsample CK SEED N TEMPMILLI PROMPT MERGES ADAPT_EXTRA THRESH_MILLI
820fn cmdnsamp to void
821if ne oargc 10
822exit 2
823end
824ockloop 2
825st = oargu 3
826nn = oargu 4
827if gt nn 999999
828exit 2
829end
830tm = oargu 5
831v = get og 0
832if gt v 256
833obload 7 v
834end
835onlreset
836put onlc 4 oargu 8
837put onlc 5 oargu 9
838ochipup
839osampleloaded st nn tm
840onlreset
841end
842
843note Embedding entry: checkpoint and tokenizer already loaded; argument 6 supplies the prompt.
844note The caller keeps exclusive ownership of the model pools until this returns.
845fn osampleloaded seed ulong nn ulong tm ulong to void
846if gt nn 999999
847exit 2
848end
849st = seed
850v = get og 0
851put og 9 0
852ocpush 0
853if gt v 256
854r = oargr 6
855if or lt r 0 gt r 4000
856exit 2
857end
858np = obprompt 6
859for i ulong np
860ocpush get obtk i
861end
862else
863r = oargr 6
864if or lt r 0 gt r 4096
865exit 2
866end
867for i ulong ulong r
868ocpush ulong get oarg i
869end
870end
871for g ulong nn
872c = osstep st tm
873if ne tm 0
874st = add st 11400714819323198485
875end
876if eq c 0
877break
878end
879if gt v 256
880obtput c
881else
882oputc c
883end
884note Flush each decoded token so interactive consumers see progress. Bytes and sampling stay unchanged.
885oflush
886ocpush c
887end
888oflush
889end
890
891note one sampling step (cmdsamp, serve.om): the next token from the logits at the last context
892note position, argmax when TEMPMILLI tm is 0, else a draw with the splitmix64 stream state st.
893fn osstep st ulong tm ulong to ulong
894olastf
895if eq tm 0
896ret oargmax
897end
898ret odraw st div f64 tm 1000.0
899end
900
901note ogpu[0] = 1 when the per-token forward runs on the chip: gpu/gg.om's gserve and gsample set it
902note before cmdserve or cmdsamp; nogpu.om's ogchip and oglast refuse with exit 2 on the CPU targets.
903global ogpu ulong 1
904
905note after the checkpoint loads: on the chip path, open the chip and upload the parameters once
906note (gpu/gg.om plans one activation block per layer application, so a looped model runs there too).
907fn ochipup to void
908if ne get ogpu 0 0
909ogchip
910end
911end
912
913note forward of the context in obx row 0, then its last position's logits into TMP[0..V) (oa),
914note on the chip when ogpu[0] = 1 (oglast), else on the CPU (ofwdi, model.om: only the rows past the
915note prefix the previous call computed, then olast)
916fn olastf to void
917if ne get ogpu 0 0
918oglast
919ret
920end
921t0 = ofwdi
922olast
923if or ne get onlc 4 0 ne get onlc 10 0
924onladapt
925end
926end
927
928note kvcmp CK N PROMPT MERGES (ham/ollm-speed, V > 256, N V <= 2^20): ofwdi against ofwd 0. Pass 1:
929note N greedy steps from token 0 then PROMPT's encoding with ofwdi then olast (the CPU decode), every
930note step's V logits kept in okvr; prints `KI step ct t0 tok ns`. Pass 2: the same contexts again
931note (pass 1's tokens pushed) with ofwd 0 then olast; prints `KF step ct tok ndiff ns`, ndiff = logits
932note whose f64 bits differ from pass 1's. Before step N/2 of both passes the context token at CT - 5
933note (when CT > 5) becomes its successor mod V, so ofwdi must find the changed prefix. Last line
934note `KVCMP N ndiff inc_ns full_ns` (forward and logits time summed over the steps); exit 43 when
935note ndiff > 0.
936global okvr f64 1048576
937global okvk ulong 4096
938fn kvstart to void
939put og 9 0
940ocpush 0
941np = obprompt 4
942for i ulong np
943ocpush get obtk i
944end
945end
946
947fn kvpert s ulong nn ulong to void
948ct = get og 9
949if and eq s div nn 2 gt ct 5
950i = sub ct 5
951put obx i rem add get obx i 1 get og 0
952end
953end
954
955fn kvline a ulong b ulong c ulong d ulong e long to void
956oputc 32
957oputu a
958oputc 32
959oputu b
960oputc 32
961oputu c
962oputc 32
963oputu d
964oputc 32
965oputl e
966oputc 10
967oflush
968end
969
970note kvcmp totals: okvs[0] pass 1 ns, [1] pass 2 ns, [2] differing logits
971global okvs ulong 3
972fn cmdkvcmp to void
973if ne oargc 6
974exit 2
975end
976ockloop 2
977nn = oargu 3
978v = get og 0
979if or le v 256 or gt nn 4096 gt mul nn v 1048576
980exit 2
981end
982obload 5 v
983put okvs 0 0
984put okvs 1 0
985put okvs 2 0
986kvstart
987for s ulong nn
988kvpert s nn
989c = kvinc s
990ocpush c
991end
992kvstart
993for s2 ulong nn
994kvpert s2 nn
995kvfull s2
996ocpush get okvk s2
997end
998oputc 75
999oputc 86
1000oputc 67
1001oputc 77
1002oputc 80
1003oputc 32
1004oputu nn
1005oputc 32
1006oputu get okvs 2
1007oputc 32
1008oputu get okvs 0
1009oputc 32
1010oputu get okvs 1
1011oputc 10
1012oflush
1013if gt get okvs 2 0
1014exit 43
1015end
1016end
1017
1018note pass 1 step s: ofwdi then olast, logits into okvr row s, KI line; returns the greedy token
1019fn kvinc s ulong to ulong
1020a = onsnow
1021t0 = ofwdi
1022olast
1023e = sub onsnow a
1024put okvs 0 add get okvs 0 e
1025c = oargmax
1026kvkeep s
1027put okvk s c
1028oputc 75
1029oputc 73
1030kvline s get og 9 t0 c long e
1031ret c
1032end
1033
1034note okvr row s = the V logits in TMP
1035fn kvkeep s ulong to void
1036v = get og 0
1037tm = get og 27
1038o = mul s v
1039for j ulong v
1040x = get oa add tm j
1041put okvr add o j x
1042end
1043end
1044
1045note the logits in TMP whose f64 bits differ from okvr row s
1046fn kvdiff s ulong to ulong
1047v = get og 0
1048tm = get og 27
1049o = mul s v
1050nd = ulong 0
1051for j ulong v
1052x = bitcast ulong get oa add tm j
1053y = bitcast ulong get okvr add o j
1054if ne x y
1055nd = add nd 1
1056end
1057end
1058ret nd
1059end
1060
1061note pass 2 step s: ofwd 0 then olast, compared bit for bit with okvr row s, KF line
1062fn kvfull s ulong to void
1063a = onsnow
1064lf = ofwd 0
1065olast
1066e = sub onsnow a
1067put okvs 1 add get okvs 1 e
1068nd = kvdiff s
1069put okvs 2 add get okvs 2 nd
1070oputc 75
1071oputc 70
1072kvline s get og 9 oargmax nd long e
1073end
1074
1075note ======== Adaptive loop commands (model.om onlx; docs/LOOP.md "Per-token exit and messages")
1076note THRESH is the exit threshold of a row's normalized core delta times 1e6, GAIN the inter-pass
1077note message gain times 1e6; 0 0 runs the hooks for telemetry only and computes eval's bits.
1078note Every command prints, after its own lines,
1079note `AX NP rows exited mean_exit_milli d0min_micro d0max_micro msgnorm_milli fnv` (rows counted,
1080note rows that left the loop early, their mean passes, the range of pass-0 deltas, the mean message
1081note norm, FNV-1a 64 over every counted row's exit pass, message norm bits and pass-0 delta bits)
1082note and `AH c1 .. cNP` (rows by the passes they ran).
1083note oadh: rows by passes; oadk: [0] fnv [1] rows [2] exited early [3] passes summed;
1084note oada: [0] min pass-0 delta [1] max [2] message norms summed; oadx: [0] telemetry differences
1085global oadh ulong 64
1086global oadk ulong 4
1087global oada f64 4
1088global oadx ulong 2
1089global oadhs ulong 4096
1090
1091fn oadset th ulong gn ulong to void
1092onlreset
1093oadclr
1094put onlx 0 th
1095put onlx 1 gn
1096put onlx 6 1
1097end
1098
1099fn oadclr to void
1100for i ulong 8
1101put onlx i 0
1102end
1103end
1104
1105fn oadzero to void
1106for i ulong 64
1107put oadh i 0
1108end
1109put oadk 0 14695981039346656037
1110put oadk 1 0
1111put oadk 2 0
1112put oadk 3 0
1113for j ulong 4
1114put oada j 0.0f64
1115end
1116end
1117
1118note FNV-1a 64 of oadk[0] over the 8 little-endian bytes of w
1119fn oadfnv w ulong to void
1120h = get oadk 0
1121for b ulong 8
1122h = xor h and shr w mul b 8 255
1123h = mul h 1099511628211
1124end
1125put oadk 0 h
1126end
1127
1128fn oadacc t0 ulong n ulong to void
1129for i ulong n
1130oadrow add t0 i
1131end
1132end
1133
1134fn oadrow t ulong to void
1135xp = get onlxp t
1136if le get og 59 1
1137xp = ulong 1
1138end
1139k = xp
1140if gt k 63
1141k = 63
1142end
1143put oadh k add get oadh k 1
1144put oadk 1 add get oadk 1 1
1145put oadk 3 add get oadk 3 xp
1146if lt xp get og 59
1147put oadk 2 add get oadk 2 1
1148end
1149oadrow2 t xp
1150end
1151
1152fn oadrow2 t ulong xp ulong to void
1153d0 = get onlxd t
1154if or eq get oadk 1 1 lt d0 get oada 0
1155put oada 0 d0
1156end
1157if gt d0 get oada 1
1158put oada 1 d0
1159end
1160mn = get onlmn t
1161put oada 2 add get oada 2 mn
1162oadfnv xp
1163oadfnv bitcast ulong mn
1164oadfnv bitcast ulong d0
1165end
1166
1167fn oadprint to void
1168rows = get oadk 1
1169dn = rows
1170if eq dn 0
1171dn = 1
1172end
1173oputc 65
1174oputc 88
1175oadsp get og 59
1176oadsp rows
1177oadsp get oadk 2
1178oadsp div mul get oadk 3 1000 dn
1179oputc 32
1180oputl rmic get oada 0
1181oputc 32
1182oputl rmic get oada 1
1183oputc 32
1184oputl long mul div get oada 2 f64 dn 1000.0
1185oputc 32
1186oputx get oadk 0
1187oputc 10
1188oputc 65
1189oputc 72
1190np = get og 59
1191if gt np 63
1192np = 63
1193end
1194if eq np 0
1195np = 1
1196end
1197for p ulong np
1198oputc 32
1199oputu get oadh add p 1
1200end
1201oputc 10
1202oflush
1203end
1204
1205note adapteval CK HELD W THRESH GAIN [MERGES]: eval's windows with the adaptive loop; prints
1206note `AE step W loss_micro bpb_micro` (eval's E line when THRESH and GAIN are 0), then AX and AH over
1207note every row of every window.
1208fn cmdadeval to void
1209na = oargc
1210if and ne na 7 ne na 8
1211exit 2
1212end
1213ockloop 2
1214ww = oargu 4
1215v = get og 0
1216if gt v 256
1217if eq na 7
1218exit 2
1219end
1220obload 7 v
1221end
1222odopen 3
1223tt = get og 1
1224if or eq ww 0 gt ww div sub get odn 0 1 tt
1225exit 23
1226end
1227oadset oargu 5 oargu 6
1228oadzero
1229ls = 0.0f64
1230nb = ulong 0
1231for w ulong ww
1232obrow 0 mul w tt
1233ls = add ls ofwd 0
1234oadacc 0 get og 9
1235if gt v 256
1236nb = add nb oevnb tt
1237end
1238end
1239oadeline ls ww nb
1240oadprint
1241oadclr
1242end
1243
1244fn oadeline ls f64 ww ulong nb ulong to void
1245lo = div ls f64 ww
1246bp = div lo 0.6931471805599453
1247if gt get og 0 256
1248bp = div mul bp f64 mul ww get og 1 f64 nb
1249end
1250oputc 65
1251oputc 69
1252oputc 32
1253oputu get og 41
1254oputc 32
1255oputu ww
1256oputc 32
1257oputl rmic lo
1258oputc 32
1259oputl rmic bp
1260oputc 10
1261oflush
1262end
1263
1264note adaptsamp CK SEED N TEMPMILLI PROMPT MERGES THRESH GAIN (V > 256): sample's context and draws
1265note on the cached decode with the adaptive loop; one `AS step ct tok exit_pass msgnorm_milli` line
1266note per token (the new row's passes and message norm), then `ADAPTSAMP n fnv`, AX and AH over the
1267note new rows. A token 0 ends it.
1268fn cmdadsamp to void
1269if ne oargc 10
1270exit 2
1271end
1272ockloop 2
1273nn = oargu 4
1274v = get og 0
1275if or le v 256 gt nn 4096
1276exit 2
1277end
1278obload 7 v
1279oadset oargu 8 oargu 9
1280oadzero
1281put okv 0 0
1282put og 9 0
1283ocpush 0
1284np = obprompt 6
1285for i ulong np
1286ocpush get obtk i
1287end
1288oadsrun oargu 3 nn oargu 5
1289oadclr
1290end
1291
1292fn oadsrun st ulong nn ulong tm ulong to void
1293ng = ulong 0
1294for g ulong nn
1295c = osstep st tm
1296if ne tm 0
1297st = add st 11400714819323198485
1298end
1299t = sub get og 9 1
1300oadacc t 1
1301oadfnv c
1302oputc 65
1303oputc 83
1304kvline g add t 1 c get onlxp t long mul get onlmn t 1000.0
1305ng = add ng 1
1306if eq c 0
1307break
1308end
1309ocpush c
1310end
1311oputs9 ng
1312oadprint
1313end
1314
1315fn oputs9 ng ulong to void
1316oputc 65
1317oputc 68
1318oputc 65
1319oputc 80
1320oputc 84
1321oputc 83
1322oputc 65
1323oputc 77
1324oputc 80
1325oputc 32
1326oputu ng
1327oputc 32
1328oputx get oadk 0
1329oputc 10
1330oflush
1331end
1332
1333note adaptcmp CK N PROMPT MERGES THRESH GAIN: kvcmp with the adaptive loop. Pass 1 (ofwdi then olast)
1334note prints `AI step ct t0 tok exit_pass`; pass 2 (ofwd 0 then olast) prints `AF step ct tok ndiff
1335note xdiff`, ndiff = logits whose bits differ from pass 1's, xdiff = 1 when the telemetry of the CT
1336note rows (exit passes, message norm bits, pass-0 delta bits) differs. Last
1337note `ADAPTCMP N ndiff xdiff inc_ns full_ns`, AX and AH over the final context; exit 43 when ndiff or
1338note xdiff is not 0.
1339fn cmdadcmp to void
1340if ne oargc 8
1341exit 2
1342end
1343ockloop 2
1344nn = oargu 3
1345v = get og 0
1346if or le v 256 or gt nn 4096 gt mul nn v 1048576
1347exit 2
1348end
1349obload 5 v
1350oadset oargu 6 oargu 7
1351put okvs 0 0
1352put okvs 1 0
1353put okvs 2 0
1354put oadx 0 0
1355kvstart
1356for s ulong nn
1357kvpert s nn
1358c = oadinc s
1359ocpush c
1360end
1361kvstart
1362for s2 ulong nn
1363kvpert s2 nn
1364oadfull s2
1365ocpush get okvk s2
1366end
1367oadzero
1368oadacc 0 get og 9
1369oadcline nn
1370oadprint
1371oadclr
1372if or gt get okvs 2 0 gt get oadx 0 0
1373exit 43
1374end
1375end
1376
1377fn oadcline nn ulong to void
1378oputc 65
1379oputc 68
1380oputc 65
1381oputc 80
1382oputc 84
1383oputc 67
1384oputc 77
1385oputc 80
1386oputc 32
1387oputu nn
1388oadsp get okvs 2
1389oadsp get oadx 0
1390oadsp get okvs 0
1391oadsp get okvs 1
1392oputc 10
1393oflush
1394end
1395
1396fn oadsp v ulong to void
1397oputc 32
1398oputu v
1399end
1400
1401note FNV-1a 64 of the telemetry of rows 0 .. n - 1
1402fn oadrowh n ulong to ulong
1403oadzero
1404oadacc 0 n
1405ret get oadk 0
1406end
1407
1408fn oadinc s ulong to ulong
1409a = onsnow
1410t0 = ofwdi
1411olast
1412e = sub onsnow a
1413put okvs 0 add get okvs 0 e
1414c = oargmax
1415kvkeep s
1416put okvk s c
1417ct = get og 9
1418put oadhs s oadrowh ct
1419oputc 65
1420oputc 73
1421kvline s ct t0 c long get onlxp sub ct 1
1422ret c
1423end
1424
1425fn oadfull s ulong to void
1426a = onsnow
1427lf = ofwd 0
1428olast
1429e = sub onsnow a
1430put okvs 1 add get okvs 1 e
1431nd = kvdiff s
1432put okvs 2 add get okvs 2 nd
1433ct = get og 9
1434xd = ulong 0
1435if ne oadrowh ct get oadhs s
1436xd = 1
1437end
1438put oadx 0 add get oadx 0 xd
1439oputc 65
1440oputc 70
1441kvline s ct oargmax nd long xd
1442end
1443
1444note logits of the last context position into TMP[0..V) (oa)
1445fn olast to void
1446d = get og 2
1447z = add get og 17 mul sub get og 9 1 d
1448klogit z get og 27 get og 0 d
1449end
1450
1451fn oargmax to ulong
1452tm = get og 27
1453b = ulong 0
1454bv = get oa tm
1455for c ulong get og 0
1456if gt get oa add tm c bv
1457bv = get oa add tm c
1458b = c
1459end
1460end
1461ret b
1462end
1463
1464fn odraw st ulong tau f64 to ulong
1465tm = get og 27
1466v = get og 0
1467m = bitcast f64 ulong 18442240474082181120
1468for c ulong v
1469x = div get oa add tm c tau
1470put oa add tm c x
1471m = fmax m x
1472end
1473sm = osoft tm v m
1474u = mul f64 shr smx st 11 1.1102230246251565e-16
1475acc = 0.0f64
1476la = ulong 0
1477for k ulong v
1478pk = get oa add tm k
1479acc = add acc pk
1480if lt u acc
1481ret k
1482end
1483if gt pk 0.0
1484la = k
1485end
1486end
1487ret la
1488end
1489
1490note gcheck constants (derivation and Wolfram queries in docs/omegallm/README.md). GCH2 = GCH/4 is
1491note the retry step for an index whose difference at GCH crossed a ReLU kink.
1492const GCH f64 0.000001
1493const GCH2 f64 0.00000025
1494const GCFLOOR f64 0.000001
1495const GCTOL f64 0.001
1496note mathck: max relative error allowed against the correctly rounded reference
1497const MCTOL f64 0.000000000000001
1498
1499note gcheck CK TRAIN: the hand gradient of the step loss for the batch of the header step is
1500note compared with central differences (L(p+h) - L(p-h))/((p+h) - (p-h)) at 4 indices of every
1501note tensor instance (wte, wpe, per layer g1 wq wk wv wo g2 w1 w2, gf), drawn by splitmix64.
1502note rel = |num - g| / max(|num|, |g|, GCFLOOR). An index with rel > GCTOL at GCH is measured
1503note again at GCH2 and keeps the smaller rel (a wrong hand gradient fails at both steps). Prints
1504note one `c index g_e12 num_e12` line per index (num of the kept step), then
1505note `C count maxrel_bits_hex maxrel_e9 worst_index retries`; exit 40 when maxrel > GCTOL.
1506fn cmdgchk to void
1507if ne oargc 4
1508exit 2
1509end
1510ockloop 2
1511odopen 3
1512obatch get og 41
1513ls = ograd
1514d = get og 2
1515cnt = ulong 0
1516put og 50 0
1517put og 51 0
1518put og 52 0
1519put og 53 0
1520ogcten 0 mul get og 0 d 0
1521ogcten get og 30 mul get og 1 d 1
1522for l ulong get og 5
1523ogcten opg1 l d 2
1524ogcten opwq l mul d d 3
1525ogcten opwk l mul d d 4
1526ogcten opwv l mul d d 5
1527ogcten opwo l mul d d 6
1528ogcten opg2 l d 7
1529ogcten opw1 l mul d get og 4 8
1530ogcten opw2 l mul d get og 4 9
1531end
1532ogcten get og 33 d 10
1533mx = bitcast f64 get og 51
1534oputc 67
1535oputc 32
1536oputu get og 50
1537oputc 32
1538oputx get og 51
1539oputc 32
1540oputl long mul mx 1000000000.0
1541oputc 32
1542oputu get og 52
1543oputc 32
1544oputu get og 53
1545oputc 10
1546oflush
1547if gt mx GCTOL
1548exit 40
1549end
1550end
1551
1552note step-loss for the batch in obx/oby without touching the gradient
1553fn oloss to f64
1554ls = 0.0f64
1555for b ulong get og 6
1556ls = add ls ofwd b
1557end
1558ret div ls f64 get og 6
1559end
1560
1561fn ogcten o ulong n ulong tag ulong to void
1562for j ulong 4
1563i = add o rem smx xor get og 40 add mul get og 50 4 j n
1564ogc1 i
1565end
1566end
1567
1568note central difference (L(p+h) - L(p-h))/((p+h) - (p-h)) at parameter i; restores p
1569fn ogcnum i ulong h f64 to f64
1570p0 = owg i
1571pp = add p0 h
1572pm = sub p0 h
1573owp i pp
1574lp = oloss
1575owp i pm
1576lm = oloss
1577owp i p0
1578ret div sub lp lm sub pp pm
1579end
1580
1581fn ogcrel num f64 g f64 to f64
1582den = fmax fmax fabs num fabs g GCFLOOR
1583ret div fabs sub num g den
1584end
1585
1586note one gradient check at parameter i; updates og[50] count, og[51] max rel bits, og[52] worst
1587note index, og[53] retries at GCH2
1588fn ogc1 i ulong to void
1589g = ocsg i
1590num = ogcnum i GCH
1591rel = ogcrel num g
1592if gt rel GCTOL
1593put og 53 add get og 53 1
1594n2 = ogcnum i GCH2
1595r2 = ogcrel n2 g
1596if lt r2 rel
1597num = n2
1598rel = r2
1599end
1600end
1601oputc 99
1602oputc 32
1603oputu i
1604oputc 32
1605oputl long mul g 1000000000000.0
1606oputc 32
1607oputl long mul num 1000000000000.0
1608oputc 10
1609put og 50 add get og 50 1
1610if gt rel bitcast f64 get og 51
1611put og 51 bitcast ulong rel
1612put og 52 i
1613end
1614end
1615
1616note mathck REF: REF lines are `e XBITS RBITS` or `l XBITS RBITS` (16 lowercase hex digits each,
1617note 36 bytes per line); RBITS is the correctly rounded Wolfram value of exp or log at the exact
1618note f64 XBITS. Prints `M e count max_ulp maxrel_bits_hex maxrel_e18` and the same for l;
1619note exit 42 when a special value (r = 0, inf or NaN) differs in bits, maxrel > MCTOL, or REF has
1620note no e line or no l line (a check that checked nothing fails).
1621fn cmdmath to void
1622if ne oargc 3
1623exit 2
1624end
1625n = odread 2
1626if ne rem n 36 0
1627exit 21
1628end
1629for k ulong 6
1630put og add 50 k 0
1631end
1632for i ulong div n 36
1633omline mul i 36
1634end
1635omprint 101 0
1636omprint 108 3
1637if or gt bitcast f64 get og 52 MCTOL gt bitcast f64 get og 55 MCTOL
1638exit 42
1639end
1640if or eq get og 50 0 eq get og 53 0
1641exit 42
1642end
1643end
1644
1645fn ohex o ulong to ulong
1646v = ulong 0
1647for k ulong 16
1648c = odb add o k
1649if lt c 58
1650v = or shl v 4 sub c 48
1651else
1652v = or shl v 4 sub c 87
1653end
1654end
1655ret v
1656end
1657
1658note og[50+b]: count, max ulp, max rel bits (b = 0 for exp, 3 for log)
1659fn omline o ulong to void
1660kd = odb o
1661x = bitcast f64 ohex add o 2
1662rb = ohex add o 19
1663b = ulong 0
1664y = 0.0f64
1665if eq kd 101
1666y = fexp x
1667else
1668b = ulong 3
1669y = flog x
1670end
1671yb = bitcast ulong y
1672put og add 50 b add get og add 50 b 1
1673r = bitcast f64 rb
1674if or or eq r 0.0 ne r r eq fabs r bitcast f64 ulong 9218868437227405312
1675if ne yb rb
1676exit 42
1677end
1678ret
1679end
1680ul = ulong 0
1681if gt yb rb
1682ul = sub yb rb
1683else
1684ul = sub rb yb
1685end
1686if gt ul get og add 51 b
1687put og add 51 b ul
1688end
1689rel = div fabs sub y r fabs r
1690if gt rel bitcast f64 get og add 52 b
1691put og add 52 b bitcast ulong rel
1692end
1693end
1694
1695fn omprint c ulong b ulong to void
1696oputc 77
1697oputc 32
1698oputc c
1699oputc 32
1700oputu get og add 50 b
1701oputc 32
1702oputu get og add 51 b
1703oputc 32
1704oputx get og add 52 b
1705oputc 32
1706oputl long mul bitcast f64 get og add 52 b 1e18
1707oputc 10
1708oflush
1709end
1710
1711note tune CK OUT TOTAL LRMICRO: fine-tune entry. Load CK (any checkpoint ckpt.om accepts), keep
1712note its shapes, G and weights, and start a new schedule on them: step 0, the given TOTAL and
1713note lr = f64(LRMICRO)/1000000.0, Adam moments m = v = 0 (so warmup and bias correction restart).
1714note Save to OUT with the loaded magic and print `U P`. train, gtrain and gtrainf then continue
1715note from OUT on any token file. TOTAL 0 or over 999999 exits 12; wrong argument count exits 2.
1716fn cmdtune to void
1717if ne oargc 6
1718exit 2
1719end
1720ockloop 2
1721tt = oargu 4
1722if or eq tt 0 gt tt 999999
1723exit 12
1724end
1725put og 41 0
1726put og 42 tt
1727put og 43 bitcast ulong div f64 oargu 5 1000000.0
1728ozerom mul 2 get og 8
1729ocksave 3
1730oputc 85
1731oputc 32
1732oputu get og 8
1733oputc 10
1734oflush
1735end
1736
1737
1738note grow CK OUT L TOTAL LRMICRO: keep embeddings, old layers and final gain byte for byte.
1739note Added layers use the own initializer with Wo and W2 zeroed, so both residual branches
1740note initially contribute zero. Their input projections remain initialized and trainable.
1741note The existing gradient cold store snapshots old weights while geometry changes; oinit
1742note writes parameters and moments only. Since P grows, new moments cannot alias the snapshot.
1743note Like tune, restart step and Adam, preserving seed, G, vocabulary and checkpoint format.
1744note OUT uses normal checkpoint save semantics (an existing path is replaced).
1745fn ogrowsnap n ulong to void
1746for q ulong ocn n
1747i = mul q OCH
1748ie = oce q n
1749while lt i ie bound 999999
1750v = owg i
1751note Reject non-finite source weights before attempting a growth operation.
1752if eq and shr bitcast ulong v 52 2047 2047
1753exit 54
1754end
1755ocsp i v
1756i = add i 1
1757end
1758end
1759end
1760
1761fn ogrowcopy n ulong to void
1762for q ulong ocn n
1763i = mul q OCH
1764ie = oce q n
1765while lt i ie bound 999999
1766owp i ocsg i
1767i = add i 1
1768end
1769end
1770end
1771
1772note Keep loop temporaries out of cmdgrow: every target must fit its register budget.
1773fn ogrowsilent first ulong to void
1774for j ulong sub get og 5 first
1775l = add first j
1776oiset opwo l mul get og 2 get og 2 0.0
1777oiset opw2 l mul get og 2 get og 4 0.0
1778end
1779end
1780
1781fn cmdgrow to void
1782if ne oargc 7
1783exit 2
1784end
1785ockload 2
1786ll = oargu 4
1787tt = oargu 5
1788lr = oargu 6
1789if or le ll get og 5 or eq tt 0 gt tt 999999
1790exit 12
1791end
1792if eq lr 0
1793exit 12
1794end
1795oldl = get og 5
1796oldg = get og 33
1797ogrowsnap get og 8
1798put og 5 ll
1799if not ogeom
1800exit 12
1801end
1802put og 41 0
1803put og 42 tt
1804put og 43 bitcast ulong div f64 lr 1000000.0
1805oinit
1806ogrowcopy oldg
1807for j ulong get og 2
1808owp add get og 33 j ocsg add oldg j
1809end
1810ogrowsilent oldl
1811ocksave 3
1812oputc 71
1813oputc 32
1814oputu oldl
1815oputc 32
1816oputu ll
1817oputc 32
1818oputu get og 8
1819oputc 10
1820oflush
1821end