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 / omegallm / lio.om

497 lines · 9728 bytes Raw Blame History
1note SPDX-License-Identifier: AGPL-3.0-only
2note omegallm lio.om (aien-train#52): the Linux platform unit, the only unit that uses host words.
3note It is concatenated last for targets aarch64 and x86linux; eio.om replaces it for x86efi. Both
4note define the same functions: oargc, oargr, oflush, ocwopen, ocwput, ocwend, ofsz, ogradw, ocsinit,
5note ocsg, ocsp, odread, owflush, owopen, owclose, ooffrd, oprd, obmsave and main. Every other unit is
6note free of host words.
7global owfd int 2
8global opth uchar 2100
9global opk uchar 4200
10global ocfd int 1
11global opp uchar 8
12global ost uchar 8
13global owrf int 64
14global owpd int 64
15
16note the argument count (argv[0] included)
17fn oargc to ulong
18ret ulong argcnt
19end
20
21note argv[i] into oarg (at most 4096 bytes); returns the argread result (negative: no such argument)
22fn oargr i ulong to long
23ret argread i oarg 0 4096
24end
25
26fn main to int
27if lt oargc 2
28exit 2
29end
30ret ocmd
31end
32
33note onsnow: monotonic nanoseconds (kvcmp's step timing in cli.om)
34fn onsnow to ulong
35ret ulong monons
36end
37
38fn oflush to void
39n = get oon 0
40p = ulong 0
41while lt p n bound 4097
42w = write 1 oout p sub n p
43if le w 0
44exit 70
45end
46p = add p ulong w
47end
48put oon 0 0
49end
50
51note v1 checkpoint stream (ckpt.om): open PATH.tmp for the path in argv[ai] (created, emptied)
52fn ocwopen ai ulong to void
53pl = oargp ai
54if gt pl 2000
55exit 2
56end
57for i ulong pl
58put opth i get oarg i
59end
60put opth pl 46
61put opth add pl 1 116
62put opth add pl 2 109
63put opth add pl 3 112
64fd = int fopen opth 0 add pl 4 577 420
65if lt fd 0
66exit 70
67end
68put ocfd 0 fd
69end
70
71note write ock[0 .. n) at byte position pos of the open PATH.tmp
72fn ocwput pos ulong n ulong to void
73fd = get ocfd 0
74p = ulong 0
75while lt p n bound 999999
76w = pwrite fd ock p sub n p long add pos p
77if le w 0
78exit 70
79end
80p = add p ulong w
81end
82end
83
84note fsync and close PATH.tmp, then rename it onto the path in argv[ai]
85fn ocwend ai ulong to void
86fd = get ocfd 0
87if ne fsync fd 0
88exit 70
89end
90if ne fclose fd 0
91exit 70
92end
93ocrename oargp ai
94end
95
96note rename opth[0 .. pl+4) to opth[0 .. pl) via a packed two-path list
97fn ocrename pl ulong to void
98put opk 0 2
99put opk 1 0
100put opk 2 uchar and add pl 4 255
101put opk 3 uchar shr add pl 4 8
102for i ulong add pl 4
103put opk add 4 i get opth i
104end
105q = add 8 pl
106put opk q uchar and pl 255
107put opk add q 1 uchar shr pl 8
108for j ulong pl
109put opk add add q 2 j get opth j
110end
111r = renames opk 0 add add q 2 pl 0
112if ne r 0
113exit 70
114end
115end
116
117note size in bytes of the file named by argv[ai] (seek to its end)
118fn ofsz ai ulong to ulong
119n = oargp ai
120fd = int fopen oarg 0 n 0 0
121if lt fd 0
122exit 70
123end
124sz = fseek fd 0 2
125if lt sz 0
126exit 70
127end
128if ne fclose fd 0
129exit 70
130end
131ret ulong sz
132end
133
134note cold store (CONTRACT-V3 section 2): GRD and the Adam moments, 3 P values at the CONTRACT-V3
135note maxima (3 29995392 -> 89986176 values, 719889408 bytes), in one private anonymous mapping made
136note on first use (mapnew prot 3 read/write, flags 34 private|anonymous; zero-filled, and inherited
137note copy-on-write by the gradient workers). A failed mapping is an I/O refusal (exit 70).
138const OCSN ulong 89986176
139global ocsh long 1
140global ocsf ulong 1
141
142note capacity of the cold store in f64 values (maps it on the first call)
143fn ocsinit to ulong
144if eq get ocsf 0 0
145h = mapnew int -1 0 mul OCSN 8 3 34
146if lt h 0
147exit 70
148end
149put ocsh 0 h
150put ocsf 0 1
151end
152ret OCSN
153end
154
155fn ocsg i ulong to f64
156ret bitcast f64 mapget get ocsh 0 mul i 8 8
157end
158
159fn ocsp i ulong v f64 to void
160x = mapset get ocsh 0 mul i 8 8 bitcast ulong v
161end
162
163note gradient of the step loss with K = og[45] processes (CONTRACT-V1 section 5.1). K = 1 is
164note ograd. For K > 1 the parent forks workers w = 1 .. K-1, each with its own pipe; worker w
165note computes blocks w G/K .. (w+1) G/K - 1 (each from zero into its own BLK copy), writes each
166note block's f64 bits and then its sequences' losses to the pipe and exits 0. The parent computes
167note blocks 0 .. G/K - 1 itself, then reads the workers in ascending w, adding each block into GRD
168note in ascending j as ograd does, and the losses into olos; olsum sums them in ascending b. So the
169note gradient and loss are the same bits for every K. A failed fork, pipe, read or a worker that
170note does not exit 0 is an I/O refusal (exit 70).
171fn ogradw to f64
172k = get og 45
173if le k 1
174ret ograd
175end
176per = div get og 44 k
177oflush
178for w ulong sub k 1
179ofork1 w per
180end
181for j ulong per
182oblkc j
183ogfold eq j 0
184end
185for w ulong sub k 1
186ojoin1 w per
187end
188ret olsum
189end
190
191note fork worker w (blocks (w+1) per .. (w+2) per - 1); keep its pipe read end and pid
192fn ofork1 w ulong per ulong to void
193r = pipefds opp 0 8 0
194if ne r 0
195exit 70
196end
197rf = int or ulong get opp 0 shl ulong get opp 1 8
198wf = int or ulong get opp 4 shl ulong get opp 5 8
199pid = fork
200if lt pid 0
201exit 70
202end
203if eq pid 0
204c = fclose rf
205owork wf mul add w 1 per per
206exit 0
207end
208c2 = fclose wf
209put owrf w rf
210put owpd w int pid
211end
212
213note read worker w's blocks into GRD in ascending j and its losses into olos, then reap it
214fn ojoin1 w ulong per ulong to void
215rf = get owrf w
216for j ulong per
217orecvb rf
218ogadd
219end
220r = div get og 6 get og 44
221m = mul per r
222orecvl rf mul add w 1 m m
223c = fclose rf
224pd = get owpd w
225x = waitpid pd ost 0 4 0
226if ne x long pd
227exit 70
228end
229s0 = get ost 0
230s1 = get ost 1
231if ne or s0 s1 0
232exit 70
233end
234end
235
236note worker: blocks j0 .. j0 + n - 1 into BLK (gradient pool from P), each sent as it is done, then
237note the losses of its sequences
238fn owork fd int j0 ulong n ulong to void
239for t ulong n
240oblkc add j0 t
241osendb fd
242end
243r = div get og 6 get og 44
244b0 = mul j0 r
245m = mul n r
246for i ulong m
247ocput8 mul i 8 bitcast ulong get olos add b0 i
248end
249owall fd mul m 8
250end
251
252fn owall fd int n ulong to void
253p = ulong 0
254while lt p n bound 999999
255w = write fd ock p sub n p
256if le w 0
257exit 70
258end
259p = add p ulong w
260end
261end
262
263fn ordall fd int n ulong to void
264p = ulong 0
265while lt p n bound 999999
266r = read fd ock p sub n p
267if le r 0
268exit 70
269end
270p = add p ulong r
271end
272end
273
274note send BLK (gradient pool [P .. 2P)) in chunks of OCKC values
275fn osendb fd int to void
276p = get og 8
277nc = div add p sub OCKC 1 OCKC
278for c ulong nc
279i = mul c OCKC
280m = sub p i
281if gt m OCKC
282m = OCKC
283end
284ocenc 4 i m
285owall fd mul m 8
286end
287end
288
289note receive one block into BLK (gradient pool [P .. 2P))
290fn orecvb fd int to void
291p = get og 8
292nc = div add p sub OCKC 1 OCKC
293for c ulong nc
294i = mul c OCKC
295m = sub p i
296if gt m OCKC
297m = OCKC
298end
299ordall fd mul m 8
300ocdecb add p i m
301end
302end
303
304note receive n losses into olos[b0 ..)
305fn orecvl fd int b0 ulong n ulong to void
306ordall fd mul n 8
307for i ulong n
308put olos add b0 i bitcast f64 ocget8 mul i 8
309end
310end
311
312note Read the file named by argv[i] into od0/od1; refuse more than ODCAP bytes (exit 21).
313note Each read pass adds at least one byte or ends, so ODCAP + 2 passes suffice: 64 outer passes
314note of at most 999999 inner passes each cover 2^25 + 2.
315fn odread i ulong to ulong
316n = oargp i
317fd = int fopen oarg 0 n 0 0
318if lt fd 0
319exit 70
320end
321p = ulong 0
322dn = false
323for o ulong 64
324q = ulong 0
325while and not dn lt q 999999 bound 999999
326q = add q 1
327r = long 0
328if lt p ODHALF
329r = read fd od0 p sub ODHALF p
330else
331if lt p ODCAP
332r = read fd od1 sub p ODHALF sub ODCAP p
333else
334r = read fd ochk 0 1
335end
336end
337if lt r 0
338exit 70
339end
340if eq r 0
341dn = true
342else
343p = add p ulong r
344if gt p ODCAP
345exit 21
346end
347end
348end
349if dn
350break
351end
352end
353if not dn
354exit 70
355end
356c = fclose fd
357put odn 0 p
358ret p
359end
360
361note prep output: two buffered writers (k = 0 TRAIN, k = 1 HELD).
362fn owflush k ulong to void
363n = get owbn k
364fd = get owfd k
365p = ulong 0
366while lt p n bound 65537
367w = long 0
368if eq k 0
369w = write fd owb0 p sub n p
370else
371w = write fd owb1 p sub n p
372end
373if le w 0
374exit 70
375end
376p = add p ulong w
377end
378put owbn k 0
379end
380
381fn owopen k ulong i ulong to void
382n = oargp i
383fd = int fopen oarg 0 n 577 420
384if lt fd 0
385exit 70
386end
387put owfd k fd
388put owbn k 0
389end
390
391fn owclose k ulong to void
392owflush k
393fd = get owfd k
394if ne fsync fd 0
395exit 70
396end
397if ne fclose fd 0
398exit 70
399end
400end
401
402note read argv[ai] (B little-endian u64 offsets) into ochk; exit 21 unless it holds exactly 8 B bytes
403fn ooffrd ai ulong to void
404n = oargp ai
405fd = int fopen oarg 0 n 0 0
406if lt fd 0
407exit 70
408end
409p = ulong 0
410dn = false
411while not dn bound 70000
412r = read fd ochk p sub 65536 p
413if lt r 0
414exit 70
415end
416if eq r 0
417dn = true
418else
419p = add p ulong r
420if ge p 65536
421exit 21
422end
423end
424end
425c = fclose fd
426if ne p mul 8 get og 6
427exit 21
428end
429end
430
431
432note v1 positioned read (CONTRACT-V1 section 3): read up to n bytes of the file named by argv[ai],
433note from byte pos, into opw[off .. off + n). The file is opened and closed on every call. Returns
434note the bytes read: n, fewer only when the file ends first, 0 at or after its end. Exit 70 on an
435note I/O error, 2 on a bad argument; off + n over len opw traps 3. Each pread pass adds at least one
436note byte or ends, so n + 1 <= 2097153 passes suffice: 3 outer passes of at most 999999 inner ones.
437fn oprd ai ulong pos ulong off ulong n ulong to ulong
438pl = oargp ai
439fd = int fopen oarg 0 pl 0 0
440if lt fd 0
441exit 70
442end
443p = ulong 0
444dn = false
445for o ulong 3
446while and not dn lt p n bound 999999
447r = pread fd opw add off p sub n p long add pos p
448if lt r 0
449exit 70
450end
451if eq r 0
452dn = true
453else
454p = add p ulong r
455end
456end
457end
458if ne fclose fd 0
459exit 70
460end
461ret p
462end
463
464note v1 (CONTRACT-V1 section 7): write obm[0 .. n) (bpe.om) to the path in argv[ai] as ocwrite does:
465note PATH.tmp, fsync, rename
466fn obmsave ai ulong n ulong to void
467pl = oargp ai
468if gt pl 2000
469exit 2
470end
471for i ulong pl
472put opth i get oarg i
473end
474put opth pl 46
475put opth add pl 1 116
476put opth add pl 2 109
477put opth add pl 3 112
478fd = int fopen opth 0 add pl 4 577 420
479if lt fd 0
480exit 70
481end
482p = ulong 0
483while lt p n bound 999999
484w = write fd obm p sub n p
485if le w 0
486exit 70
487end
488p = add p ulong w
489end
490if ne fsync fd 0
491exit 70
492end
493if ne fclose fd 0
494exit 70
495end
496ocrename pl
497end