interplane.Get Omega
Read in your language

aien-train

AIEN training programme: language-model history, GB10 constraints and the route to training on the project engine. Plans and runnable checks are distinguished.

Brought by drakestapleton

branch main Branches Tags History

aien-train / native / si19 / select.om

809 lines · 15975 bytes Raw Blame History
1note SPDX-License-Identifier: AGPL-3.0-only
2note aien-train#25 ORIG-2: an original candidate-selection scorer for the omexp-v1 records
3note (docs/si19/ORIG1-CONTRACT.md), trained from a seeded random initialization with plain
4note SGD on the squared error of the score against the normalized relative cost y. Two
5note kinds: 1 affine (one 12-weight row per arm, 96 params; the constant feature x0 is
6note the bias) and 2 one-hidden-layer ReLU (20 inputs = 12 state + 8 arm one-hot, H=32,
7note 705 params). Every binary32 value is a bit pattern in a ulong; arithmetic goes only
8note through add32/mul32; negation is xor with the sign bit. No float words.
9note Trainer state (SS): 0 kind, 1 H, 2 seed, 3 lr bits (0 = no-update control), 4 epoch,
10note 5 cursor into SR, 6 updates, 7 rng state, 8 mu16 (wsenc), 9 sh, 10 ntrain, 11 data
11note fnv (RC[2] at init), 12 rng state at the start of the current epoch (SR is rebuilt from
12note it), 13 best DEV loss sum bits (+inf until the first epoch), 14 epochs since the DEV
13note loss improved, 15 nparams, 16 control (0 normal, 1 shuffled targets, 2 no-update),
14note 17 last TRAIN loss sum bits, 18 best epoch, 19 FNV-1a over the initial params,
15note 20..31 reserved 0. SP current params, SB params of the best DEV epoch (the init until
16note the first epoch), SG gradients, SZ inputs (SZ[20] = last score), SH/SA hidden
17note pre-activations/activations, SX shuffled-target permutation over pair = r*8 + a,
18note SR epoch order, SI checkpoint image bytes, SE evaluation totals.
19note Everything a resumed run needs is in SS, SP and SB: SR comes from SS[12], SX from the
20note seed and the dataset. The scorer ranks admissible arms only; it never widens them.
21global SP ulong 1024
22global SG ulong 1024
23global SB ulong 1024
24global SZ ulong 24
25global SH ulong 64
26global SA ulong 64
27global SS ulong 32
28global SI uchar 32768
29global SE ulong 64
30global SX ulong 16384
31global SR ulong 2048
32
33fn slnext s ulong to ulong
34note One step of the trainer LCG (the wsrand recurrence) on a caller-held state.
35ret and add mul s 1664525 1013904223 4294967295
36end
37
38fn slrand to ulong
39n = slnext get SS 7
40put SS 7 n
41ret shr n 8
42end
43
44fn slkey x ulong to long
45note Order key of a finite binary32 bit pattern: integer order equals value order.
46if ne and x 2147483648 0
47ret neg long and x 2147483647
48end
49ret long x
50end
51
52fn sllt a ulong b ulong to bool
53ret lt slkey a slkey b
54end
55
56fn slnpar kind ulong to ulong
57if eq kind 1
58ret 96
59end
60ret 705
61end
62
63fn sldraw to ulong
64note One weight: uniform in {-16..15}/128, exact binary32 bits.
65ret wmbits sub long rem slrand 32 16 7
66end
67
68fn slweights to void
69note Affine: 96 weights. MLP: W1 (640) and W2 (32) drawn in that order, b1 and b2 zero.
70if eq get SS 0 1
71for i ulong 96
72put SP i sldraw
73end
74ret
75end
76for i ulong 640
77put SP i sldraw
78end
79for h ulong 32
80put SP add 672 h sldraw
81end
82end
83
84fn slndraw to ulong
85note Number of LCG draws slweights makes for the current kind.
86if eq get SS 0 1
87ret 96
88end
89ret 672
90end
91
92fn slfnvp n ulong to ulong
93note FNV-1a 64 over the words SP[0..n).
94h = ulong 14695981039346656037
95for i ulong n
96h = mul xor h get SP i 1099511628211
97end
98ret h
99end
100
101fn slfnvb n ulong to ulong
102h = ulong 14695981039346656037
103for i ulong n
104h = mul xor h get SB i 1099511628211
105end
106ret h
107end
108
109fn slpairok r ulong a ulong to bool
110note (r, a) is a TRAIN pair: an admitted TRAIN record and an admissible arm.
111if ge r get RC 0
112ret false
113end
114if ne oxpart r 0
115ret false
116end
117ret oxcand r a
118end
119
120fn slxpack to ulong
121note Compact the TRAIN pairs r*8+a in record order into SX[0..np); returns np.
122k = ulong 0
123for r ulong get RC 0
124for a ulong 8
125if slpairok r a
126put SX k add mul r 8 a
127k = add k 1
128end
129end
130end
131ret k
132end
133
134fn slxmix s ulong np ulong to ulong
135note Fisher-Yates over SX[0..np) driven by the caller-held LCG state; returns the state.
136i = np
137while gt i 1 bound 16384
138i = sub i 1
139s = slnext s
140j = rem shr s 8 add i 1
141t = get SX i
142put SX i get SX j
143put SX j t
144end
145ret s
146end
147
148fn slxspread np ulong to void
149note Spread the shuffled list: pair p of rank k gets SX[p] = list[k]; other p map to p.
150note Walking p downward never overwrites a list entry that is still needed (k <= p).
151k = np
152p = ulong 16384
153while gt p 0 bound 16384
154p = sub p 1
155if slpairok div p 8 rem p 8
156k = sub k 1
157put SX p get SX k
158else
159put SX p p
160end
161end
162end
163
164fn slshuf s ulong to ulong
165note Shuffled-target control: a permutation of the TRAIN pairs from the trainer LCG.
166np = slxpack
167s = slxmix s np
168slxspread np
169ret s
170end
171
172fn slinit kind ulong seed ulong lr ulong ctrl ulong to void
173note Declared initialization: seeded LCG weights, zero biases, SB = SP, no data prior.
174for i ulong 32
175put SS i 0
176end
177for i ulong 1024
178put SP i 0
179put SG i 0
180put SB i 0
181end
182put SS 0 kind
183put SS 1 32
184put SS 2 seed
185put SS 3 lr
186put SS 7 seed
187put SS 11 get RC 2
188put SS 13 2139095040
189put SS 15 slnpar kind
190put SS 16 ctrl
191slweights
192put SS 19 slfnvp get SS 15
193for i ulong 1024
194put SB i get SP i
195end
196if eq ctrl 1
197put SS 7 slshuf get SS 7
198end
199end
200
201fn slfloor s long c long to long
202q = div s c
203if and lt s 0 ne mul q c s
204q = sub q 1
205end
206ret q
207end
208
209fn slisqrt v long to long
210r = long 0
211while le mul add r 1 add r 1 v bound 100000
212r = add r 1
213end
214ret r
215end
216
217fn slsh mu long c long to ulong
218note sh = max(0, bitlen(s16) - 5), s16 the integer sqrt of the mean square deviation.
219v = long 0
220for r ulong get RC 0
221for a ulong 8
222if slpairok r a
223d = sub oxt16 r a mu
224v = add v mul d d
225end
226end
227end
228s = slisqrt div v c
229b = ulong 0
230while gt s 0 bound 64
231s = shr s 1
232b = add b 1
233end
234if le b 5
235ret 0
236end
237ret sub b 5
238end
239
240fn slnorm to void
241note mu16 = floor of the mean t16 over TRAIN pairs; sh per contract s.4; ntrain records.
242s = long 0
243c = long 0
244nt = ulong 0
245for r ulong get RC 0
246if eq oxpart r 0
247nt = add nt 1
248end
249for a ulong 8
250if slpairok r a
251s = add s oxt16 r a
252c = add c 1
253end
254end
255end
256if eq c 0
257exit 84
258end
259mu = slfloor s c
260put SS 8 wsenc mu
261put SS 9 slsh mu c
262put SS 10 nt
263end
264
265fn sltarget r ulong a ulong to ulong
266note y = (t16 - mu16) / 2^(4 + sh) as exact binary32; control 1 reads the pair through SX.
267if eq get SS 16 1
268p = get SX add mul r 8 a
269r = div p 8
270a = rem p 8
271end
272ret wmbits sub oxt16 r a wsdec get SS 8 add 4 get SS 9
273end
274
275fn slinput r ulong a ulong to void
276note SZ[0..11] = x / 16 exactly, SZ[12..19] = arm one-hot.
277oxfeat r
278for j ulong 12
279put SZ j wmbits get XF j 4
280end
281for k ulong 8
282put SZ add 12 k 0
283end
284put SZ add 12 a 1065353216
285end
286
287fn slaff a ulong to ulong
288acc = ulong 0
289b = mul a 12
290for j ulong 12
291acc = add32 acc mul32 get SP add b j get SZ j
292end
293ret acc
294end
295
296fn slhid h ulong to ulong
297acc = get SP add 640 h
298for i ulong 20
299acc = add32 acc mul32 get SP add mul i 32 h get SZ i
300end
301ret acc
302end
303
304fn slpos x ulong to bool
305note Strictly positive binary32 (the ReLU gate; +0, -0 and negatives are closed).
306ret and eq and x 2147483648 0 ne x 0
307end
308
309fn slmlp to ulong
310nh = get SS 1
311for h ulong nh
312z = slhid h
313put SH h z
314if slpos z
315put SA h z
316else
317put SA h 0
318end
319end
320acc = get SP 704
321for h ulong nh
322acc = add32 acc mul32 get SP add 672 h get SA h
323end
324ret acc
325end
326
327fn slfwd r ulong a ulong to ulong
328note Score of arm a in record r; keeps SZ, SH, SA and SZ[20] = score for slback.
329slinput r a
330out = ulong 0
331if eq get SS 0 1
332out = slaff a
333else
334out = slmlp
335end
336put SZ 20 out
337ret out
338end
339
340fn slbhid h ulong dh ulong to void
341k = add 640 h
342put SG k add32 get SG k dh
343for i ulong 20
344k = add mul i 32 h
345put SG k add32 get SG k mul32 dh get SZ i
346end
347end
348
349fn slbmlp g ulong to void
350note Backward through W2 and the ReLU gate: units at pre-activation exactly 0 pass nothing.
351put SG 704 add32 get SG 704 g
352nh = get SS 1
353for h ulong nh
354k = add 672 h
355put SG k add32 get SG k mul32 g get SA h
356if slpos get SH h
357slbhid h mul32 g get SP k
358end
359end
360end
361
362fn slback r ulong a ulong y ulong to void
363note dL/dtheta of L = (score - y)^2 for the (r, a) of the last slfwd, accumulated into SG.
364d = add32 get SZ 20 xor y 2147483648
365g = add32 d d
366if eq get SS 0 1
367b = mul a 12
368for j ulong 12
369k = add b j
370put SG k add32 get SG k mul32 g get SZ j
371end
372ret
373end
374slbmlp g
375end
376
377fn slstep to void
378note SP = SP + (-(lr * SG)); SG cleared; a non-finite parameter stops the program (86).
379lr = get SS 3
380n = get SS 15
381for i ulong n
382v = add32 get SP i xor mul32 lr get SG i 2147483648
383if not affinite v
384exit 86
385end
386put SP i v
387put SG i 0
388end
389end
390
391fn slpair r ulong a ulong to void
392y = sltarget r a
393out = slfwd r a
394slback r a y
395slstep
396put SS 6 add get SS 6 1
397end
398
399fn slperm s ulong to ulong
400note SR[0..ntrain) = TRAIN record indices, Fisher-Yates from the caller-held LCG state.
401k = ulong 0
402for r ulong get RC 0
403if eq oxpart r 0
404put SR k r
405k = add k 1
406end
407end
408i = k
409while gt i 1 bound 2048
410i = sub i 1
411s = slnext s
412j = rem shr s 8 add i 1
413t = get SR i
414put SR i get SR j
415put SR j t
416end
417ret s
418end
419
420fn slrun n ulong to void
421note Up to n TRAIN records from the cursor, every admissible arm one update. The epoch
422note order is rebuilt from SS[12]; SS[12] is fixed (and the rng advanced) only at cursor 0
423note and only when there is work to do, so a zero-length run leaves the state untouched.
424if or eq n 0 ge get SS 5 get SS 10
425ret
426end
427if eq get SS 5 0
428put SS 12 get SS 7
429put SS 7 slperm get SS 12
430else
431s = slperm get SS 12
432end
433c = get SS 5
434e = add c n
435if gt e get SS 10
436e = get SS 10
437end
438while lt c e bound 2048
439r = get SR c
440for a ulong 8
441if oxcand r a
442slpair r a
443end
444end
445c = add c 1
446put SS 5 c
447end
448end
449
450fn slrloss r ulong to ulong
451note Squared error summed over the admissible arms of record r.
452s = ulong 0
453for a ulong 8
454if oxcand r a
455d = add32 slfwd r a xor sltarget r a 2147483648
456s = add32 s mul32 d d
457end
458end
459ret s
460end
461
462fn slloss part ulong to ulong
463s = ulong 0
464for r ulong get RC 0
465if eq oxpart r part
466s = add32 s slrloss r
467end
468end
469ret s
470end
471
472fn slcheck to void
473note DEV loss after an epoch: early-stop bookkeeping and the SB snapshot when improved.
474dev = slloss 1
475put SS 17 slloss 0
476if lt dev get SS 13
477put SS 13 dev
478put SS 14 0
479put SS 18 get SS 4
480for i ulong 1024
481put SB i get SP i
482end
483else
484put SS 14 add get SS 14 1
485end
486end
487
488fn slepoch to void
489slrun get SS 10
490put SS 4 add get SS 4 1
491put SS 5 0
492put SS 12 get SS 7
493slcheck
494end
495
496fn slgo maxep ulong patience ulong stop ulong to bool
497e = get SS 4
498if ge e maxep
499ret false
500end
501if ge get SS 14 patience
502ret false
503end
504if and ne stop 0 ge e stop
505ret false
506end
507ret true
508end
509
510fn sltrain maxep ulong patience ulong stop ulong to void
511note Epochs until maxep, until DEV has not improved for patience epochs, or until the
512note epoch counter reaches stop (0 = no partial stop).
513while slgo maxep patience stop bound 65
514slepoch
515end
516end
517
518fn slbest to void
519note Install the best-on-DEV parameters as the current ones (evaluation only).
520for i ulong 1024
521put SP i get SB i
522end
523end
524
525fn siput pos ulong v ulong to void
526for i ulong 8
527put SI add pos i uchar and shr v mul i 8 255
528end
529end
530
531fn siget pos ulong to ulong
532v = ulong 0
533for i ulong 8
534v = or v shl ulong get SI add pos i mul i 8
535end
536ret v
537end
538
539fn sifnv base ulong n ulong to ulong
540note FNV-1a 64 over SI[base..base+n): integrity of the image, not authentication.
541h = ulong 14695981039346656037
542for i ulong n
543h = mul xor h ulong get SI add base i 1099511628211
544end
545ret h
546end
547
548fn sllen kind ulong to ulong
549note Image length: 16 header + 32 SS words + SP + SB + 8 fnv = 280 + 16 nparams.
550ret add 280 mul 16 slnpar kind
551end
552
553fn slsave base ulong to ulong
554note Image: magic "SI19SLCK", version 1, SS[0..31], SP[0..n), SB[0..n), FNV-1a 64.
555n = get SS 15
556siput base 5423262296609737043
557siput add base 8 1
558for i ulong 32
559siput add add base 16 mul i 8 get SS i
560end
561p = add base 272
562for i ulong n
563siput add p mul i 8 get SP i
564end
565p = add p mul n 8
566for i ulong n
567siput add p mul i 8 get SB i
568end
569p = add p mul n 8
570siput p sifnv base sub p base
571ret add 8 sub p base
572end
573
574fn sllrok lr ulong to bool
575note Admitted learning rates: 0 (no-update control), 2^-8, 2^-10, 2^-12 (contract s.8).
576if or eq lr 0 eq lr wmbits 1 8
577ret true
578end
579ret or eq lr wmbits 1 10 eq lr wmbits 1 12
580end
581
582fn slrange base ulong to int
583note State ranges of an image: epoch, cursor, updates, lr, control, ntrain.
584if gt siget add base 48 64
585ret -6
586end
587if gt siget add base 56 siget add base 96
588ret -6
589end
590if gt siget add base 64 1048576
591ret -6
592end
593if not sllrok siget add base 40
594ret -6
595end
596if gt siget add base 144 2
597ret -6
598end
599if gt siget add base 176 1
600ret -6
601end
602if gt siget add base 96 2048
603ret -6
604end
605ret 0
606end
607
608fn slfinite base ulong n ulong to bool
609note Every SP and SB word of the image is a finite binary32.
610c = mul 2 n
611for i ulong c
612if not affinite siget add add base 272 mul i 8
613ret false
614end
615end
616ret true
617end
618
619fn slxseed to ulong
620note The rng state right after slweights: the state slinit built SX from.
621s = get SS 2
622k = slndraw
623for i ulong k
624s = slnext s
625end
626ret s
627end
628
629fn slcopy base ulong n ulong to void
630for i ulong 32
631put SS i siget add add base 16 mul i 8
632end
633for i ulong 1024
634put SP i 0
635put SB i 0
636put SG i 0
637end
638p = add base 272
639for i ulong n
640put SP i siget add p mul i 8
641end
642p = add p mul n 8
643for i ulong n
644put SB i siget add p mul i 8
645end
646if eq get SS 16 1
647p = slshuf slxseed
648end
649end
650
651fn slverify base ulong n ulong to int
652note Every image check except the training dataset binding; replaces nothing.
653if and ne n 1816 ne n 11560
654ret -1
655end
656if ne siget base 5423262296609737043
657ret -2
658end
659if ne siget add base 8 1
660ret -3
661end
662kind = siget add base 16
663if or lt kind 1 gt kind 2
664ret -4
665end
666if ne n sllen kind
667ret -4
668end
669if ne siget add base 24 32
670ret -4
671end
672if ne siget add base 136 slnpar kind
673ret -4
674end
675if ne sifnv base sub n 8 siget add base sub n 8
676ret -5
677end
678st = slrange base
679if ne st 0
680ret st
681end
682if not slfinite base slnpar kind
683ret -7
684end
685ret 0
686end
687
688fn sladmit base ulong n ulong to int
689note Admission of SI[base..base+n) for inference and evaluation over any admitted
690note dataset: every check except the training dataset binding; nothing is replaced
691note before every check passed. The bound dataset hash stays readable at base+104.
692st = slverify base n
693if ne st 0
694ret st
695end
696slcopy base slnpar siget add base 16
697ret 0
698end
699
700fn slbound base ulong to bool
701note Does the image's training dataset hash equal the admitted dataset's?
702ret eq siget add base 104 get RC 2
703end
704
705fn slload base ulong n ulong to int
706note Admission for resumed training: slverify plus the dataset binding (-8), checked
707note before anything is replaced.
708st = slverify base n
709if ne st 0
710ret st
711end
712if not slbound base
713ret -8
714end
715slcopy base slnpar siget add base 16
716ret 0
717end
718
719fn slpick r ulong to ulong
720note The model's choice: admissible arm with the smallest score, lowest index on a tie.
721best = ulong 0
722bs = slfwd r 0
723for k ulong 7
724a = add k 1
725if oxcand r a
726s = slfwd r a
727if sllt s bs
728bs = s
729best = a
730end
731end
732end
733ret best
734end
735
736fn slstat r ulong to ulong
737note Best-static baseline arm of the record's core class, reference when not admissible.
738a = oxstatic oxcore r
739if oxcand r a
740ret a
741end
742ret 0
743end
744
745fn slnear r ulong to ulong
746note Retrieval baseline: the nearest TRAIN record's oracle arm, reference when not admissible.
747k = oxnear r
748if ge k 1048576
749ret 0
750end
751a = oxbest k
752if oxcand r a
753ret a
754end
755ret 0
756end
757
758fn slwl r ulong cm ulong a ulong k ulong to void
759note Paired bookkeeping against arm a: SE[k] wins (model cheaper), SE[k+1] losses.
760c = oxcost r a
761if lt cm c
762put SE k add get SE k 1
763end
764if gt cm c
765put SE add k 1 add get SE add k 1 1
766end
767end
768
769fn slevalr r ulong to void
770m = slpick r
771cm = oxcost r m
772put SE 0 add get SE 0 1
773put SE 1 add get SE 1 cm
774co = oxcost r oxbest r
775put SE 2 add get SE 2 co
776a = oxrule r
777put SE 3 add get SE 3 oxcost r a
778slwl r cm a 9
779put SE 4 add get SE 4 oxcost r 0
780slwl r cm 0 15
781a = slstat r
782put SE 5 add get SE 5 oxcost r a
783slwl r cm a 13
784a = slnear r
785put SE 6 add get SE 6 oxcost r a
786slwl r cm a 11
787if eq cm co
788put SE 8 add get SE 8 1
789end
790end
791
792fn slevalc part ulong core ulong to void
793note Totals over the records of a partition into SE[0..16] (spec layout); core 3 = all
794note core classes. Top-1 agreement counts a choice whose cost equals the oracle's.
795for i ulong 17
796put SE i 0
797end
798for r ulong get RC 0
799if eq oxpart r part
800if or eq core 3 eq oxcore r core
801slevalr r
802end
803end
804end
805end
806
807fn sleval part ulong to void
808slevalc part 3
809end