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

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