interplane.Get Omega
Read in your language

aienux

Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.

Brought by drakestapleton

branch main Branches Tags History

aienux / native / om / lm1b / lm1b.in.om

1429 lines · 25237 bytes Raw Blame History
1note SPDX-License-Identifier: AGPL-3.0-or-later
2note lm1b: Llama-3.2-1B-Instruct Q4_K_M forward pass in seeded Omega (aienux native/om/lm1b, arch#314).
3note TEMPLATE. gen.sh expands the marker lines (GEN_...) into lm1b.om; edit this file, then run gen.sh.
4note Contract v1: fd 3 = model (pread only), fd 0 = request lines, fd 1 = response lines. See README.md.
5note Numbers mirror aienos-infer (src/math.rs, src/quant.rs, src/decode.rs, src/lib.rs) on its plain f32
6note path: same operations in the same order, one IEEE rounding per word, no fused multiply-add.
7note omc refuses a function that returns f32 (error 5), so float helpers return the f32 bits as uint.
8const NE ulong 2048
9const NF ulong 8192
10const NV ulong 128256
11const CTX ulong 256
12const KVD ulong 512
13const AX ulong 0
14const AH ulong 2048
15const AQ ulong 4096
16const AK ulong 6144
17const AV ulong 6656
18const AAT ulong 7168
19const AG ulong 9216
20const AU ulong 17408
21const AT ulong 25600
22const ASC ulong 27648
23const ARC ulong 27904
24const ARS ulong 27936
25const ALG ulong 32768
26const K3 ulong 0
27const K5 ulong 1
28const K7 ulong 2
29const K9 ulong 3
30const K6 ulong 4
31const K24 ulong 5
32const K120 ulong 6
33const K5040 ulong 7
34const K362 ulong 8
35const K720 ulong 9
36const K40320 ulong 10
37const KPI ulong 11
38const KTAU ulong 12
39const KPI2 ulong 13
40const KLN2 ulong 14
41const KL2E ulong 15
42const KSQ2 ulong 16
43const KHI ulong 17
44const KLO ulong 18
45const KNINF ulong 19
46const KEPS ulong 20
47const KNZ ulong 21
48const KSCALE ulong 22
49const KTS ulong 23
50const KM87 ulong 24
51const K88 ulong 25
52const SOB ulong 0
53const SCUR ulong 1
54const SMF ulong 2
55const SRB ulong 3
56const SDS ulong 4
57const SFLT ulong 5
58const SFILL ulong 6
59const SSTART ulong 7
60const SEOF ulong 8
61const SSKIP ulong 9
62const SLS ulong 10
63const SPC ulong 11
64const SPE ulong 12
65const SERR ulong 13
66const SIDS ulong 14
67const SIDN ulong 15
68const SNID ulong 16
69const SGEN ulong 17
70const SBUD ulong 18
71const SSTOP ulong 19
72const SFAULT ulong 20
73const SNTOK ulong 21
74const SCAN ulong 22
75const IBCAP ulong 65536
76const WLOAD ulong 0
77const WOK ulong 1
78const WBYTES ulong 2
79const WTENS ulong 3
80const WSHA ulong 4
81const WPASSES ulong 5
82const WJOB ulong 6
83const WSTATUS ulong 7
84const WCANCEL ulong 8
85const WREFUSED ulong 9
86const WREASON ulong 10
87const WTOKENS ulong 11
88const WBITS ulong 12
89const WACCT ulong 13
90const WROWB ulong 14
91const WSYNTAX ulong 15
92const WTOOLONG ulong 16
93const WTOOMANY ulong 17
94const WCONTEXT ulong 18
95const WBADID ulong 19
96const WRANGE ulong 20
97const WEMPTY ulong 21
98const WSIZE ulong 22
99const WMAGIC ulong 23
100const WHEADER ulong 24
101const WMANI ulong 25
102const WQMARK ulong 26
103const WGEN ulong 27
104const WBUDGET ulong 28
105const WSTOP ulong 29
106const WFAULT ulong 30
107const WIDS ulong 31
108const WREAD ulong 32
109const FSIZE ulong 807694368
110const NTENS ulong 147
111@@GEN_GLOBALS@@
112global rb uchar 8192
113global ac f32 161024
114global kc f32 2097152
115global vc f32 2097152
116global nw f32 67584
117global rf f32 32
118global cf f32 32
119global kd f32 8
120global ko ulong 8
121global tof ulong 160
122global tty ulong 160
123global toff ulong 160
124global ttyp ulong 160
125global st ulong 32
126global ob uchar 65536
127global ib uchar 65536
128global mf uchar 65536
129global one uchar 8
130global ids ulong 512
131global tk ulong 512
132global tb ulong 512
133text tw "load ok bytes tensors sha256 passes job status cancelled refused reason tokens bits acct rowbytes syntax toolong toomany context badid range empty size magic header manifest ? gen budget stop fault ids read "
134@@GEN_MANIFEST@@
135note ---- vendored SHA-256 (benchmarks-mv om/data/sha256.om at 567c5da, unchanged; see README) ----
136@@SHA256@@
137note ---- output: one line at a time in ob, flushed on newline ----
138fn oc c uchar to void
139n = get st SOB
140put ob n c
141put st SOB add n 1
142end
143fn ow k ulong to void
144for i ulong 16
145c = get tw add mul k 16 i
146if eq c 32
147ret
148end
149oc c
150end
151end
152fn osp to void
153oc 32
154end
155fn ou v ulong to void
156d = new uchar 24 0
157n = ulong 0
158x = v
159while true bound 30
160put d n uchar add 48 rem x 10
161n = add n 1
162x = div x 10
163if eq x 0
164break
165end
166end
167for i ulong n
168oc get d sub sub n 1 i
169end
170end
171fn onl to void
172oc 10
173n = get st SOB
174r = write int 1 ob 0 n
175if ne r long n
176exit 4
177end
178put st SOB 0
179end
180fn refuse w ulong to void
181put st SOB 0
182ow WLOAD
183osp
184ow WREFUSED
185osp
186ow WREASON
187osp
188ow w
189onl
190exit 3
191end
192note ---- manifest text rebuilt from the header, compared byte for byte with the pinned text ----
193fn mc c uchar to void
194n = get st SMF
195if ge n 65536
196refuse WMANI
197end
198put mf n c
199put st SMF add n 1
200end
201fn mu v ulong to void
202d = new uchar 24 0
203n = ulong 0
204x = v
205while true bound 30
206put d n uchar add 48 rem x 10
207n = add n 1
208x = div x 10
209if eq x 0
210break
211end
212end
213for i ulong n
214mc get d sub sub n 1 i
215end
216end
217fn msg v ulong nb ulong to void
218note signed decimal of the low nb bits of v (nb 8, 16, 32 or 64)
219x = v
220neg1 = false
221if eq nb 64
222if ne shr x 63 0
223neg1 = true
224end
225else
226if ne and shr x sub nb 1 1 0
227neg1 = true
228x = or x not sub shl ulong 1 nb 1
229end
230end
231if neg1
232mc 45
233mu neg x
234else
235mu x
236end
237end
238fn hrd to ulong
239c = get st SCUR
240if ge c 16777216
241refuse WHEADER
242end
243put st SCUR add c 1
244ret ulong get m0 c
245end
246fn hrn n ulong to ulong
247v = ulong 0
248for i ulong n
249v = or v shl hrd mul i 8
250end
251ret v
252end
253fn mstr to void
254note a GGUF string: its length, then the bytes when short and plain (rule shared with the Rust dumper)
255n = hrn 8
256c = get st SCUR
257if gt n 16777216
258refuse WHEADER
259end
260if gt add c n 16777216
261refuse WHEADER
262end
263ok = le n 40
264for i ulong n
265b = get m0 add c i
266if or or lt b 32 gt b 126 or eq b 34 eq b 92
267ok = false
268end
269end
270mu n
271if ok
272mc 32
273for i ulong n
274mc get m0 add c i
275end
276end
277put st SCUR add c n
278end
279fn tsize e ulong to ulong
280if or or eq e 0 eq e 1 eq e 7
281ret 1
282end
283if or eq e 2 eq e 3
284ret 2
285end
286if or or eq e 4 eq e 5 eq e 6
287ret 4
288end
289if or or eq e 10 eq e 11 eq e 12
290ret 8
291end
292ret 0
293end
294fn mval t ulong to void
295note one metadata value of type t, as the manifest prints it
296if or or eq t 0 eq t 2 eq t 4
297mu hrn tsize t
298ret
299end
300if eq t 10
301mu hrn 8
302ret
303end
304if or eq t 6 eq t 12
305mu hrn tsize t
306ret
307end
308if eq t 7
309mu hrn 1
310ret
311end
312if or or eq t 1 eq t 3 eq t 5
313sz = tsize t
314msg hrn sz mul sz 8
315ret
316end
317if eq t 11
318msg hrn 8 64
319ret
320end
321if eq t 8
322mstr
323ret
324end
325if eq t 9
326e = hrn 4
327n = hrn 8
328mu e
329mc 32
330mu n
331sz = tsize e
332if ne sz 0
333c = get st SCUR
334if gt n 16777216
335refuse WHEADER
336end
337put st SCUR add c mul n sz
338ret
339end
340if ne e 8
341refuse WHEADER
342end
343if gt n 999999
344refuse WHEADER
345end
346for i ulong n
347k = hrn 8
348if gt k 16777216
349refuse WHEADER
350end
351put st SCUR add get st SCUR k
352end
353ret
354end
355refuse WHEADER
356end
357fn header to void
358put st SCUR 0
359put st SMF 0
360if ne hrn 4 1179993927
361refuse WMAGIC
362end
363v = hrn 4
364nt = hrn 8
365nk = hrn 8
366if or gt nt 160 gt nk 1024
367refuse WHEADER
368end
369mc 103
370mc 103
371mc 117
372mc 102
373mc 32
374mu v
375mc 32
376mu nt
377mc 32
378mu nk
379mc 10
380hkvs nk
381htens nt
382hdone nt
383end
384fn hkvs nk ulong to void
385for i ulong nk
386mc 107
387mc 118
388mc 32
389mstr
390t = hrn 4
391mc 32
392mu t
393mc 32
394mval t
395mc 10
396end
397end
398fn htens nt ulong to void
399for i ulong nt
400mc 116
401mc 32
402n = hrn 8
403c = get st SCUR
404if gt n 255
405refuse WHEADER
406end
407for j ulong n
408mc get m0 add c j
409end
410put st SCUR add c n
411nd = hrn 4
412if or lt nd 1 gt nd 2
413refuse WMANI
414end
415d0 = hrn 8
416d1 = ulong 1
417if eq nd 2
418d1 = hrn 8
419end
420ty = hrn 4
421of = hrn 8
422mc 32
423mu ty
424mc 32
425mu nd
426mc 32
427mu d0
428mc 32
429mu d1
430mc 32
431mu of
432mc 10
433put toff i of
434put ttyp i ty
435end
436end
437fn hdone nt ulong to void
438ds = and add get st SCUR 31 not ulong 31
439mc 100
440mc 97
441mc 116
442mc 97
443mc 32
444mu ds
445mc 10
446put st SDS ds
447n = get st SMF
448if ne n len man
449refuse WMANI
450end
451for i ulong n
452if ne get mf i get man i
453refuse WMANI
454end
455end
456for i ulong nt
457s = ulong 0
458for j ulong 3
459s = add mul s 10 ulong sub get tslot add mul i 3 j 48
460end
461put tof s add ds get toff i
462put tty s get ttyp i
463end
464end
465note ---- f16 and the K-quant dot products (src/half.rs, src/quant.rs) ----
466fn f16 lo uchar hi uchar to uint
467h = or uint lo shl uint hi 8
468sg = shl shr h 15 31
469ex = and shr h 10 31
470mn = and h 1023
471if eq ex 0
472if eq mn 0
473ret sg
474end
475m = mn
476sh = uint 0
477while lt m 1024 bound 16
478m = shl m 1
479sh = add sh 1
480end
481ret or or sg shl sub 113 sh 23 shl and m 1023 13
482end
483if eq ex 31
484ret or or sg 2139095040 shl mn 13
485end
486ret or or sg shl add ex 112 23 shl mn 13
487end
488fn scm o ulong j ulong to uint
489note scale_min_k4: scale in the low byte, min in the next byte
490if lt j 4
491a = get rb add o j
492b = get rb add o add j 4
493ret or uint and a 63 shl uint and b 63 8
494end
495a = get rb add o add j 4
496b = get rb add o sub j 4
497c = get rb add o j
498sc = or and a 15 shl shr b 6 4
499m = or shr a 4 shl shr c 6 4
500ret or uint sc shl uint m 8
501end
502fn dq4h s f32 qo ulong xj ulong sh ulong to uint
503note 32 weights of one Q4_K sub-block (low nibbles when sh is 0, high when 4), summed in order
504d1 = get kd 2
505n1 = get kd 3
506a = s
507for l ulong 32
508q = shr get rb add qo l sh
509w = sub mul d1 f32 and q 15 n1
510a = add a mul w get ac add xj l
511end
512ret bitcast uint a
513end
514fn dq4j o ulong xb ulong j ulong s f32 to uint
515note sub-blocks 2 j and 2 j + 1; kd holds d, dmin, then this sub-block scale and min
516p = scm add o 4 mul j 2
517put kd 2 mul get kd 0 f32 and p 255
518put kd 3 mul get kd 1 f32 shr p 8
519t = bitcast f32 dq4h s add o add 16 mul j 32 add xb mul j 64 0
520p = scm add o 4 add mul j 2 1
521put kd 2 mul get kd 0 f32 and p 255
522put kd 3 mul get kd 1 f32 shr p 8
523ret dq4h t add o add 16 mul j 32 add add xb mul j 64 32 4
524end
525fn dq4b o ulong xb ulong to uint
526note one Q4_K block (144 bytes at rb o) against 256 activations at ac xb: dequantize, then sum in order
527put kd 0 bitcast f32 f16 get rb o get rb add o 1
528put kd 1 bitcast f32 f16 get rb add o 2 get rb add o 3
529s = get cf KNZ
530for j ulong 4
531s = bitcast f32 dq4j o xb j s
532end
533ret bitcast uint s
534end
535fn dq4 nb ulong xo ulong to uint
536acc = f32 0.0
537for b ulong nb
538acc = add acc bitcast f32 dq4b mul b 144 add xo mul b 256
539end
540ret bitcast uint acc
541end
542fn i8f u uchar to uint
543v = int u
544if ge v 128
545v = sub v 256
546end
547ret bitcast uint f32 v
548end
549fn q6v k ulong p ulong to int
550note one 6-bit Q6_K weight minus 32 at k past ko 0 (low bits) and ko 1 (high bits); pass p picks
551note the nibble (p / 2) and the two high bits (2 p)
552lo = and shr get rb add get ko 0 k mul shr p 1 4 15
553hi = and shr get rb add get ko 1 k mul p 2 3
554ret sub int or lo shl hi 4 32
555end
556fn dq6g s f32 p ulong to uint
557note 16 weights sharing one scale (kd 4); activations at ac ko 2
558a = s
559xp = get ko 2
560ds = get kd 4
561for k ulong 16
562q = q6v k p
563a = add a mul mul ds f32 q get ac add xp k
564end
565ret bitcast uint a
566end
567fn dq6p s f32 to uint
568note pass of a Q6_K block: 32 weights, two scales; kd 0 holds d; ko 3..6 hold the pass bases
569a = s
570for g ulong 2
571put ko 0 add get ko 3 mul g 16
572put ko 1 add get ko 4 mul g 16
573put ko 2 add get ko 5 mul g 16
574c = get rb add get ko 6 g
575put kd 4 mul get kd 0 bitcast f32 i8f c
576a = bitcast f32 dq6g a get ko 7
577end
578ret bitcast uint a
579end
580fn q6set o ulong xb ulong hf ulong p ulong to void
581note bases of pass p of half hf: ko 3 low bits, ko 4 high bits, ko 5 activations, ko 6 scales, ko 7 p
582put ko 3 add o mul hf 64
583put ko 3 add get ko 3 mul and p 1 32
584put ko 4 add add o 128 mul hf 32
585put ko 5 add xb mul hf 128
586put ko 5 add get ko 5 mul p 32
587put ko 6 add add o 192 mul hf 8
588put ko 6 add get ko 6 mul p 2
589put ko 7 p
590end
591fn dq6b o ulong xb ulong to uint
592note one Q6_K block (210 bytes at rb o) against 256 activations at ac xb, in index order
593put kd 0 bitcast f32 f16 get rb add o 208 get rb add o 209
594s = get cf KNZ
595for hf ulong 2
596for p ulong 4
597q6set o xb hf p
598s = bitcast f32 dq6p s
599end
600end
601ret bitcast uint s
602end
603fn dq6 nb ulong xo ulong to uint
604acc = f32 0.0
605for b ulong nb
606acc = add acc bitcast f32 dq6b mul b 210 add xo mul b 256
607end
608ret bitcast uint acc
609end
610fn dv6g p ulong to void
611yp = get ko 2
612ds = get kd 4
613for k ulong 16
614q = q6v k p
615put ac add yp k mul ds f32 q
616end
617end
618fn dv6 nb ulong yo ulong to void
619note dequantize the Q6_K row in rb into ac at yo (the embedding lookup)
620for b ulong nb
621o = mul b 210
622put kd 0 bitcast f32 f16 get rb add o 208 get rb add o 209
623for hf ulong 2
624for p ulong 4
625q6set o add yo mul b 256 hf p
626for g ulong 2
627put ko 0 add get ko 3 mul g 16
628put ko 1 add get ko 4 mul g 16
629put ko 2 add get ko 5 mul g 16
630c = get rb add get ko 6 g
631put kd 4 mul get kd 0 bitcast f32 i8f c
632dv6g p
633end
634end
635end
636end
637end
638@@GEN_CPSEG@@
639@@GEN_FB@@
640fn rowcp a ulong n ulong to void
641p = shr a 24
642o = and a 16777215
643k = n
644if gt add o n 16777216
645k = sub 16777216 o
646end
647cpseg p o 0 k
648if lt k n
649cpseg add p 1 0 k sub n k
650end
651end
652fn mv ti ulong xo ulong yo ulong rows ulong cols ulong to void
653ty = get tty ti
654nb = div cols 256
655rbs = mul nb 144
656if eq ty 14
657rbs = mul nb 210
658else
659if ne ty 12
660trap 10
661end
662end
663base = get tof ti
664for r ulong rows
665rowcp add base mul r rbs rbs
666v = f32 0.0
667if eq ty 12
668v = bitcast f32 dq4 nb xo
669else
670v = bitcast f32 dq6 nb xo
671end
672put ac add yo r v
673end
674put st SRB add get st SRB mul rows rbs
675end
676note ---- scalar math (src/math.rs) ----
677fn msqrt x f32 to uint
678if le x 0.0
679ret uint 0
680end
681y = bitcast f32 add shr bitcast uint x 1 532676608
682for i ulong 5
683y = mul 0.5 add y div x y
684end
685ret bitcast uint y
686end
687fn mexp a f32 to uint
688x = a
689if lt x get cf KM87
690x = get cf KM87
691end
692if gt x get cf K88
693x = get cf K88
694end
695sc = mul x get cf KL2E
696n = int 0
697if ge sc 0.0
698n = int add sc 0.5
699else
700n = int sub sc 0.5
701end
702nf = f32 n
703r = sub sub x mul nf get cf KHI mul nf get cf KLO
704t = add get cf K120 div r 720.0
705t = add get cf K24 mul r t
706t = add get cf K6 mul r t
707t = add 0.5 mul r t
708t = add 1.0 mul r t
709p = add 1.0 mul r t
710ret bitcast uint mul p bitcast f32 shl uint add n 127 23
711end
712fn mln a f32 to uint
713b = bitcast uint a
714e = sub int and shr b 23 255 127
715m = bitcast f32 or and b 8388607 1065353216
716if gt m get cf KSQ2
717m = mul m 0.5
718e = add e 1
719end
720y = div sub m 1.0 add m 1.0
721y2 = mul y y
722t = add get cf K9 div y2 11.0
723t = add get cf K7 mul y2 t
724t = add get cf K5 mul y2 t
725t = add get cf K3 mul y2 t
726t = add 1.0 mul y2 t
727s = mul y t
728ret bitcast uint add mul 2.0 s mul f32 e get cf KLN2
729end
730fn fmodp x f32 y f32 to uint
731note exact fmod for finite x >= 0 and normal y > 0 (integer long division, as musl fmodf)
732bx = bitcast uint x
733by = bitcast uint y
734ex = and shr bx 23 255
735ey = and shr by 23 255
736if or or ge ex 255 ne shr bx 31 0 or lt ey 24 ge ey 255
737trap 11
738end
739if lt x y
740ret bx
741end
742mx = ulong or and bx 8388607 8388608
743my = ulong or and by 8388607 8388608
744r = mx
745k = sub ex ey
746for i uint k
747if ge r my
748r = sub r my
749end
750r = shl r 1
751end
752if ge r my
753r = sub r my
754end
755if eq r 0
756ret uint 0
757end
758ret bitcast uint mul f32 r bitcast f32 shl sub ey 23 23
759end
760fn sincos a f32 i ulong to void
761r = bitcast f32 fmodp add a get cf KPI get cf KTAU
762if lt r 0.0
763r = add r get cf KTAU
764end
765x = sub r get cf KPI
766cs = f32 1.0
767if gt x get cf KPI2
768x = sub get cf KPI x
769cs = f32 -1.0
770else
771if lt x neg get cf KPI2
772x = sub neg get cf KPI x
773cs = f32 -1.0
774end
775end
776x2 = mul x x
777t = mul x2 get cf K362
778t = sub t get cf K5040
779t = mul x2 t
780t = add get cf K120 t
781t = mul x2 t
782t = sub t get cf K6
783t = mul x2 t
784t = add 1.0 t
785put ac add ARS i mul x t
786u = sub get cf K40320 div x2 3628800.0
787u = mul x2 u
788u = sub u get cf K720
789u = mul x2 u
790u = add get cf K24 u
791u = mul x2 u
792u = sub u 0.5
793u = mul x2 u
794u = add 1.0 u
795put ac add ARC i mul cs u
796end
797fn initc to void
798put cf K3 div f32 1.0 3.0
799put cf K5 div f32 1.0 5.0
800put cf K7 div f32 1.0 7.0
801put cf K9 div f32 1.0 9.0
802put cf K6 div f32 1.0 6.0
803put cf K24 div f32 1.0 24.0
804put cf K120 div f32 1.0 120.0
805put cf K5040 div f32 1.0 5040.0
806put cf K362 div f32 1.0 362880.0
807put cf K720 div f32 1.0 720.0
808put cf K40320 div f32 1.0 40320.0
809put cf KPI bitcast f32 uint 1078530011
810put cf KTAU bitcast f32 uint 1086918619
811put cf KPI2 bitcast f32 uint 1070141403
812put cf KLN2 bitcast f32 uint 1060205080
813put cf KL2E bitcast f32 uint 1069066811
814put cf KSQ2 bitcast f32 uint 1068827891
815put cf KHI bitcast f32 uint 1060208640
816put cf KLO bitcast f32 uint 3109978243
817put cf KNINF bitcast f32 uint 4286578688
818put cf KEPS bitcast f32 uint 925353388
819put cf KNZ bitcast f32 uint 2147483648
820put cf KM87 f32 -87.0
821put cf K88 f32 88.0
822put cf KSCALE div f32 1.0 bitcast f32 msqrt f32 64.0
823base = bitcast f32 uint 1223959552
824put cf KTS bitcast f32 mexp mul f32 -0.03125 bitcast f32 mln base
825end
826note ---- the forward pass (src/decode.rs) ----
827fn rmsn xo ulong wo ulong yo ulong to void
828ss = get cf KNZ
829for i ulong NE
830v = get ac add xo i
831ss = add ss mul v v
832end
833ss = div ss 2048.0
834sc = div 1.0 bitcast f32 msqrt add ss get cf KEPS
835for i ulong NE
836v = mul get ac add xo i sc
837put ac add yo i mul v get nw add wo i
838end
839end
840fn ropec pos ulong to void
841ts = get cf KTS
842th = f32 pos
843for i ulong 32
844sincos div th get rf i i
845th = mul th ts
846end
847end
848fn ropea o ulong nh ulong to void
849for h ulong nh
850for i ulong 32
851j = add add o mul h 64 mul i 2
852a = get ac j
853b = get ac add j 1
854c = get ac add ARC i
855s = get ac add ARS i
856put ac j sub mul a c mul b s
857put ac add j 1 add mul a s mul b c
858end
859end
860end
861fn smax n ulong to void
862mx = get cf KNINF
863for t ulong n
864mx = fmax mx get ac add ASC t
865end
866sm = f32 0.0
867for t ulong n
868v = bitcast f32 mexp sub get ac add ASC t mx
869put ac add ASC t v
870sm = add sm v
871end
872for t ulong n
873put ac add ASC t div get ac add ASC t sm
874end
875end
876fn ascore qo ulong kv ulong t ulong to uint
877note q . k over one head (64 values), summed in order from -0.0
878kq = add kv mul t KVD
879s = get cf KNZ
880for i ulong 64
881v = mul get ac add qo i get kc add kq i
882s = add s v
883end
884ret bitcast uint s
885end
886fn aacc ao ulong kv ulong t ulong to void
887vo = add kv mul t KVD
888w = get ac add ASC t
889for i ulong 64
890v = mul w get vc add vo i
891put ac add ao i add get ac add ao i v
892end
893end
894fn ahead base ulong np ulong h ulong to void
895kv = add base mul div h 4 64
896qo = add AQ mul h 64
897for t ulong np
898s = bitcast f32 ascore qo kv t
899put ac add ASC t mul s get cf KSCALE
900end
901smax np
902ao = add AAT mul h 64
903for i ulong 64
904put ac add ao i 0.0
905end
906for t ulong np
907aacc ao kv t
908end
909end
910fn attn l ulong pos ulong to void
911base = mul l mul CTX KVD
912for h ulong 32
913ahead base add pos 1 h
914end
915end
916fn resid to void
917for i ulong NE
918put ac add AX i add get ac add AX i get ac add AT i
919end
920end
921fn kvst l ulong pos ulong to void
922at = add mul l mul CTX KVD mul pos KVD
923for i ulong KVD
924put kc add at i get ac add AK i
925put vc add at i get ac add AV i
926end
927end
928fn silu to void
929for i ulong NF
930g = get ac add AG i
931e = add 1.0 bitcast f32 mexp neg g
932put ac add AG i mul div g e get ac add AU i
933end
934end
935fn fwd tok ulong pos ulong want bool to void
936rowcp add get tof 144 mul tok 1680 1680
937put st SRB add get st SRB 1680
938dv6 8 AX
939ropec pos
940for l ulong 16
941rmsn AX mul l NE AH
942li = mul l 9
943mv li AH AQ 2048 2048
944mv add li 1 AH AK 512 2048
945mv add li 2 AH AV 512 2048
946ropea AQ 32
947ropea AK 8
948kvst l pos
949attn l pos
950mv add li 3 AAT AT 2048 2048
951resid
952if and eq l 8 eq pos get st SFLT
953trap 12
954end
955rmsn AX mul add 16 l NE AH
956mv add li 6 AH AG 8192 2048
957mv add li 7 AH AU 8192 2048
958silu
959mv add li 8 AG AT 2048 8192
960resid
961end
962if want
963rmsn AX mul 32 NE AH
964mv 144 AH ALG NV 2048
965end
966end
967fn amax to ulong
968best = ulong 0
969bv = get ac ALG
970for i ulong NV
971v = get ac add ALG i
972if gt v bv
973best = i
974bv = v
975end
976end
977ret best
978end
979note ---- load: read, digest, admit ----
980fn loadf32 a ulong n ulong dst ulong to void
981note n little-endian f32 values at file address a into nw at dst
982for i ulong n
983o = add a mul i 4
984v = or or uint fb o shl uint fb add o 1 8 or shl uint fb add o 2 16 shl uint fb add o 3 24
985put nw add dst i bitcast f32 v
986end
987end
988fn admit to void
989@@GEN_LOAD@@
990x = pread int 3 one 0 1 long FSIZE
991if ne x 0
992refuse WSIZE
993end
994header
995hreset
996@@GEN_HASH@@
997ok = hfinal
998if not ok
999refuse WSHA
1000end
1001for i ulong 64
1002if ne get hd i get sha i
1003refuse WSHA
1004end
1005end
1006for l ulong 16
1007loadf32 get tof add mul l 9 4 NE mul l NE
1008loadf32 get tof add mul l 9 5 NE mul add 16 l NE
1009end
1010loadf32 get tof 145 NE mul 32 NE
1011for i ulong 32
1012o = add get tof 146 mul i 4
1013v = or or uint fb o shl uint fb add o 1 8 or shl uint fb add o 2 16 shl uint fb add o 3 24
1014put rf i bitcast f32 v
1015end
1016initc
1017ow WLOAD
1018osp
1019ow WOK
1020osp
1021ow WBYTES
1022osp
1023ou FSIZE
1024osp
1025ow WTENS
1026osp
1027ou NTENS
1028osp
1029ow WSHA
1030osp
1031for i ulong 64
1032oc get hd i
1033end
1034osp
1035ow WPASSES
1036oc 32
1037oc 48
1038onl
1039end
1040note ---- requests ----
1041fn getln to long
1042note next request line in ib at st SLS; length, -1 at end of input, -2 for a line over the buffer
1043while true bound 999999
1044s = get st SSTART
1045f = get st SFILL
1046i = s
1047while lt i f bound 999999
1048if eq get ib i 10
1049put st SSTART add i 1
1050if eq get st SSKIP 1
1051put st SSKIP 0
1052ret long -2
1053end
1054put st SLS s
1055ret long sub i s
1056end
1057i = add i 1
1058end
1059if eq get st SEOF 1
1060if lt s f
1061put st SSTART f
1062if eq get st SSKIP 1
1063put st SSKIP 0
1064ret long -2
1065end
1066put st SLS s
1067ret long sub f s
1068end
1069if eq get st SSKIP 1
1070put st SSKIP 0
1071ret long -2
1072end
1073ret long -1
1074end
1075n = sub f s
1076for j ulong n
1077put ib j get ib add s j
1078end
1079if eq n IBCAP
1080n = ulong 0
1081put st SSKIP 1
1082end
1083put st SSTART 0
1084put st SFILL n
1085r = read int 0 ib n sub IBCAP n
1086if le r 0
1087put st SEOF 1
1088else
1089put st SFILL add n ulong r
1090end
1091end
1092ret long -1
1093end
1094fn tword k ulong to bool
1095note the request word k at the cursor, then a space or the line end; the cursor moves past the word
1096p = get st SPC
1097e = get st SPE
1098n = ulong 0
1099for i ulong 16
1100c = get tw add mul k 16 i
1101if eq c 32
1102break
1103end
1104n = add n 1
1105end
1106if gt add p n e
1107ret false
1108end
1109for i ulong n
1110if ne get ib add p i get tw add mul k 16 i
1111ret false
1112end
1113end
1114q = add p n
1115if lt q e
1116if ne get ib q 32
1117ret false
1118end
1119end
1120put st SPC q
1121ret true
1122end
1123fn tsp to bool
1124note exactly one space, then more text
1125p = get st SPC
1126e = get st SPE
1127if ge add p 1 e
1128ret false
1129end
1130if ne get ib p 32
1131ret false
1132end
1133if eq get ib add p 1 32
1134ret false
1135end
1136put st SPC add p 1
1137ret true
1138end
1139fn tnum to ulong
1140note a decimal number of 1..18 digits; anything else sets st SERR
1141p = get st SPC
1142e = get st SPE
1143v = ulong 0
1144n = ulong 0
1145while lt p e bound 100
1146c = get ib p
1147if eq c 32
1148break
1149end
1150if or lt c 48 gt c 57
1151put st SERR 1
1152ret 0
1153end
1154v = add mul v 10 ulong sub c 48
1155n = add n 1
1156if gt n 18
1157put st SERR 1
1158ret 0
1159end
1160p = add p 1
1161end
1162if eq n 0
1163put st SERR 1
1164end
1165put st SPC p
1166ret v
1167end
1168fn oid to void
1169n = get st SIDN
1170if eq n 0
1171ow WQMARK
1172ret
1173end
1174s = get st SIDS
1175for i ulong n
1176oc get ib add s i
1177end
1178end
1179fn jref w ulong to void
1180put st SOB 0
1181ow WJOB
1182osp
1183oid
1184osp
1185ow WSTATUS
1186osp
1187ow WREFUSED
1188osp
1189ow WREASON
1190osp
1191ow w
1192onl
1193end
1194fn tfield k ulong to ulong
1195note " <word k> <number>"; a failure sets st SERR
1196if not tsp
1197put st SERR 1
1198ret 0
1199end
1200if not tword k
1201put st SERR 1
1202ret 0
1203end
1204if not tsp
1205put st SERR 1
1206ret 0
1207end
1208ret tnum
1209end
1210fn pid to bool
1211note the job id: 1..32 printable bytes up to the next space
1212p = get st SPC
1213e = get st SPE
1214q = p
1215while lt q e bound 100
1216c = get ib q
1217if eq c 32
1218break
1219end
1220if or lt c 33 gt c 126
1221ret false
1222end
1223q = add q 1
1224end
1225if gt sub q p 32
1226ret false
1227end
1228put st SIDS p
1229put st SIDN sub q p
1230put st SPC q
1231ret true
1232end
1233fn pids to ulong
1234note " <id>" repeated to the line end into ids; returns the count or a refusal word plus 1000
1235k = ulong 0
1236while lt get st SPC get st SPE bound 100000
1237if not tsp
1238ret add WSYNTAX 1000
1239end
1240v = tnum
1241if eq get st SERR 1
1242ret add WSYNTAX 1000
1243end
1244if ge k CTX
1245ret add WTOOMANY 1000
1246end
1247if ge v NV
1248ret add WBADID 1000
1249end
1250put ids k v
1251k = add k 1
1252end
1253ret k
1254end
1255fn parse ls ulong n ulong to ulong
1256note fills SIDS SIDN SGEN SBUD SSTOP SFAULT SNID and ids; returns 99 when accepted, else a refusal word
1257put st SPC ls
1258put st SPE add ls n
1259put st SERR 0
1260put st SIDN 0
1261if not tword WJOB
1262ret WSYNTAX
1263end
1264if not tsp
1265ret WSYNTAX
1266end
1267if not pid
1268ret WSYNTAX
1269end
1270put st SGEN tfield WGEN
1271put st SBUD tfield WBUDGET
1272put st SSTOP tfield WSTOP
1273put st SFAULT tfield WFAULT
1274if eq get st SERR 1
1275ret WSYNTAX
1276end
1277if not tsp
1278ret WSYNTAX
1279end
1280if not tword WIDS
1281ret WSYNTAX
1282end
1283k = pids
1284if ge k 1000
1285ret sub k 1000
1286end
1287if eq k 0
1288ret WEMPTY
1289end
1290if or eq get st SGEN 0 gt get st SFAULT 1
1291ret WRANGE
1292end
1293if gt sub add k get st SGEN 1 CTX
1294ret WCONTEXT
1295end
1296put st SNID k
1297ret 99
1298end
1299fn tpush t ulong to void
1300nt = get st SNTOK
1301put tk nt t
1302put tb nt ulong bitcast uint get ac add ALG t
1303put st SNTOK add nt 1
1304end
1305fn jrun to ulong
1306note prefill then greedy steps; returns the passes run; st SCAN 1 when the budget ran out
1307n = get st SNID
1308bud = get st SBUD
1309put st SRB 0
1310put st SCAN 0
1311put st SNTOK 0
1312put st SFLT 4294967295
1313if eq get st SFAULT 1
1314put st SFLT div n 2
1315end
1316pos = ulong 0
1317for i ulong n
1318if ge pos bud
1319put st SCAN 1
1320ret pos
1321end
1322fwd get ids i pos eq add i 1 n
1323pos = add pos 1
1324end
1325for s ulong get st SGEN
1326t = amax
1327tpush t
1328if eq t get st SSTOP
1329ret pos
1330end
1331if eq add s 1 get st SGEN
1332ret pos
1333end
1334if ge pos bud
1335put st SCAN 1
1336ret pos
1337end
1338fwd t pos true
1339pos = add pos 1
1340end
1341ret pos
1342end
1343fn kvclr pos ulong to void
1344note discard this job's KV state (the weights stay resident)
1345for l ulong 16
1346for p ulong pos
1347at = add mul l mul CTX KVD mul p KVD
1348for i ulong KVD
1349put kc add at i 0.0
1350put vc add at i 0.0
1351end
1352end
1353end
1354end
1355fn jout pos ulong to void
1356nt = get st SNTOK
1357put st SOB 0
1358ow WJOB
1359osp
1360oid
1361osp
1362ow WSTATUS
1363osp
1364if eq get st SCAN 1
1365ow WCANCEL
1366else
1367ow WOK
1368end
1369osp
1370ow WPASSES
1371osp
1372ou pos
1373osp
1374ow WTOKENS
1375for i ulong nt
1376osp
1377ou get tk i
1378end
1379onl
1380ow WBITS
1381for i ulong nt
1382osp
1383ou get tb i
1384end
1385onl
1386ow WACCT
1387osp
1388ow WJOB
1389osp
1390oid
1391osp
1392ow WROWB
1393osp
1394ou get st SRB
1395osp
1396ow WPASSES
1397osp
1398ou pos
1399onl
1400end
1401fn runjob to void
1402pos = jrun
1403kvclr pos
1404jout pos
1405end
1406fn main to int
1407admit
1408while true bound 999999
1409n = getln
1410if eq n -1
1411ret 0
1412end
1413if eq n -2
1414put st SIDN 0
1415jref WTOOLONG
1416continue
1417end
1418if eq n 0
1419continue
1420end
1421w = parse get st SLS ulong n
1422if ne w 99
1423jref w
1424continue
1425end
1426runjob
1427end
1428ret 0
1429end