interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / train / omega-llm / native / ckpt.om

320 lines · 7364 bytes Raw Blame History
1note SPDX-License-Identifier: AGPL-3.0-only
2note omegallm ckpt.om (aien-train#52): checkpoint OLLMCK01 / OLLMCK02 and gradient OLLMGR01 files
3note (CONTRACT section 6, CONTRACT-V1 section 6). Needs math.om, data.om, model.om, adam.om before it.
4note Files are streamed: a save encodes the f64 payload little-endian in chunks of OCKC values into
5note ock, hashes each chunk (FNV-1a 64) and writes it at its file position through the platform
6note unit (ocwopen, ocwput, ocwend; lio.om writes PATH.tmp, fsync, rename; eio.om the file in
7note place), then writes the header at position 0. A load reads the header and then the payload in
8note chunks through oprd into opw, hashing and decoding into the pools in one pass.
9note CK01 header 112 bytes (FNV at 104); CK02 120 bytes (G at 104, FNV at 112); CK03 144 bytes (G at
10note 104, the loop LP, LC, NP at 112, 120, 128, FNV at 136; docs/LOOP.md); GR01 112 bytes, step
11note field = B, one payload. A model with a loop (NP > 1) is always CK03 and a CK03 file always
12note has NP > 1, so a model without a loop keeps its CK01 or CK02 bytes. Largest file at the v3
13note maxima: 144 + 24 P with P = 29995392 (Wolfram: 144+24*29995392 -> 719889552); a file over
14note OCKCAP = 719889553 bytes is refused with 15 before its header is read, as v0 refused a file
15note over its image (exactly OCKCAP bytes goes to the header checks, then 15).
16note Magic as a little-endian u64 (Wolfram FromDigits[Reverse[ToCharacterCode["OLLMCK01"]],256]
17note -> 3544415659172318287; "OLLMGR01" -> 3544423372933581903; "OLLMCK02" -> 3616473253210246223;
18note "OLLMCK03" -> 3688530847248174159).
19note Load refusals (exit codes): 10 file shorter than its header or than header + 24 P, 11 bad magic,
20note 12 a shape zero or over the maximum, V < 256, D mod H != 0, TOTAL > 999999, (CK02, CK03) G = 0,
21note G > B or B mod G != 0, or (CK03) NP < 2, LC = 0, LP + LC > L or a loop the pools cannot hold,
22note 13 P disagrees with the shapes, 14 FNV-1a mismatch, 15 file longer than header + 24 P, 17 a
23note CK03 file given to a command that does not run loops (it loads with ockload, not ockloop);
24note 70 I/O.
25const OCKCAP ulong 719889553
26const OCKC ulong 65536
27global ock uchar 524288
28
29fn ocput8 o ulong v ulong to void
30for k ulong 8
31put ock add o k uchar and shr v mul k 8 255
32end
33end
34
35
36note little-endian u64 at ock[o .. o + 8)
37fn ocget8 o ulong to ulong
38v = ulong 0
39for k ulong 8
40v = or v shl ulong get ock add o k mul k 8
41end
42ret v
43end
44
45note decode m values from ock into gradient pool [o .. o + m) (a worker's gradient block, lio.om)
46fn ocdecb o ulong m ulong to void
47for j ulong m
48ogrp add o j bitcast f64 ocget8 mul j 8
49end
50end
51note little-endian u64 at opw[o .. o + 8)
52fn opw8 o ulong to ulong
53v = ulong 0
54for k ulong 8
55v = or v shl ulong get opw add o k mul k 8
56end
57ret v
58end
59
60note FNV-1a 64 step over ock[0 .. n) from state h (basis cbf29ce484222325, prime 100000001b3)
61fn ocfnvk h ulong n ulong to ulong
62x = h
63for i ulong n
64x = mul xor x ulong get ock i 1099511628211
65end
66ret x
67end
68
69fn ocfnvw h ulong n ulong to ulong
70x = h
71for i ulong n
72x = mul xor x ulong get opw i 1099511628211
73end
74ret x
75end
76
77note header fields from og into ock[0 .. 104); magic mg, step field st
78fn ochead mg ulong st ulong to void
79ocput8 0 mg
80for i ulong 7
81ocput8 add 8 mul i 8 get og i
82end
83ocput8 64 get og 40
84ocput8 72 st
85ocput8 80 get og 42
86ocput8 88 get og 43
87ocput8 96 get og 8
88end
89
90note encode m values of pool k (0 params, 1 m, 2 v, 3 GRD, 4 BLK) from value index i into ock
91fn ocenc k ulong i ulong m ulong to void
92if eq k 0
93for j ulong m
94ocput8 mul j 8 bitcast ulong owg add i j
95end
96ret
97end
98if ge k 3
99o = add get og 10 i
100if eq k 4
101o = add get og 8 i
102end
103for j ulong m
104ocput8 mul j 8 bitcast ulong ogrg add o j
105end
106ret
107end
108o = add get og add 10 k i
109for j ulong m
110ocput8 mul j 8 bitcast ulong omvg add o j
111end
112end
113
114note decode m values from opw into pool k (0 params, 1 m, 2 v) from value index i
115fn ocdec k ulong i ulong m ulong to void
116if eq k 0
117for j ulong m
118owp add i j bitcast f64 opw8 mul j 8
119end
120ret
121end
122o = add get og add 10 k i
123for j ulong m
124omvp add o j bitcast f64 opw8 mul j 8
125end
126end
127
128note stream pools ks .. ke-1 (ocenc numbering) after a header of hl bytes; returns the FNV-1a
129fn ocbody ks ulong ke ulong hl ulong to ulong
130p = get og 8
131h = ulong 14695981039346656037
132pos = hl
133nc = div add p sub OCKC 1 OCKC
134for k ulong sub ke ks
135for c ulong nc
136i = mul c OCKC
137m = sub p i
138if gt m OCKC
139m = OCKC
140end
141ocenc add ks k i m
142nb = mul m 8
143h = ocfnvk h nb
144ocwput pos nb
145pos = add pos nb
146end
147end
148ret h
149end
150
151note write checkpoint (params, m, v) to argv[ai]: OLLMCK03 when the model has a loop (og 59 > 1),
152note else with the loaded or initialised magic (og 46)
153fn ocksave ai ulong to void
154ocwopen ai
155hl = ulong 112
156if eq get og 46 2
157hl = 120
158end
159if gt get og 59 1
160hl = 144
161end
162h = ocbody 0 3 hl
163if eq hl 112
164ochead 3544415659172318287 get og 41
165ocput8 104 h
166end
167if eq hl 120
168ochead 3616473253210246223 get og 41
169ocput8 104 get og 44
170ocput8 112 h
171end
172if eq hl 144
173ochead 3688530847248174159 get og 41
174ocput8 104 get og 44
175ocput8 112 get og 57
176ocput8 120 get og 58
177ocput8 128 get og 59
178ocput8 136 h
179end
180ocwput 0 hl
181ocwend ai
182end
183
184note write gradient file GR01: header with step = B, payload GRD (for either checkpoint magic)
185fn ogrsave ai ulong to void
186ocwopen ai
187h = ocbody 3 4 112
188ochead 3544423372933581903 get og 6
189ocput8 104 h
190ocwput 0 112
191ocwend ai
192end
193
194note load the checkpoint named by argv[ai] for a command that runs loops (train, eval, sample,
195note grad, gcheck, tune, kvcmp, serve on the CPU)
196fn ockloop ai ulong to void
197put og 60 1
198ockload ai
199end
200
201note the magic mg of a checkpoint header: its length (112, 120 or 144) and og 46; exit 11 otherwise
202fn ockmag mg ulong to ulong
203if eq mg 3544415659172318287
204put og 46 1
205ret 112
206end
207if eq mg 3616473253210246223
208put og 46 2
209ret 120
210end
211if ne mg 3688530847248174159
212exit 11
213end
214put og 46 3
215ret 144
216end
217
218note load the checkpoint named by argv[ai] into og, ow (params) and omv (m, v)
219fn ockload ai ulong to void
220n = ofsz ai
221if gt n OCKCAP
222exit 15
223end
224hn = oprd ai 0 0 144
225if lt n 112
226exit 10
227end
228hl = ockmag opw8 0
229if lt n hl
230exit 10
231end
232for i ulong 7
233put og i opw8 add 8 mul i 8
234end
235put og 57 0
236put og 58 0
237put og 59 0
238if eq hl 144
239if eq get og 60 0
240exit 17
241end
242put og 57 opw8 112
243put og 58 opw8 120
244put og 59 opw8 128
245if lt get og 59 2
246exit 12
247end
248end
249if not ogeom
250exit 12
251end
252put og 40 opw8 64
253put og 41 opw8 72
254put og 42 opw8 80
255put og 43 opw8 88
256if gt get og 42 999999
257exit 12
258end
259g = ulong 1
260if ge hl 120
261g = opw8 104
262end
263if not ogok2 g
264exit 12
265end
266put og 44 g
267put og 45 1
268p = get og 8
269if ne opw8 96 p
270exit 13
271end
272hs = opw8 sub hl 8
273pb = mul 24 p
274if lt n add hl pb
275exit 10
276end
277if gt n add hl pb
278exit 15
279end
280if ne ocklp ai hl p hs 1
281exit 14
282end
283end
284
285note one pass over the payload at hl: read, hash and decode each chunk; exit 70 on a short read.
286note Returns 1 when the payload FNV equals hs, else 0.
287fn ocklp ai ulong hl ulong p ulong hs ulong to ulong
288h = ulong 14695981039346656037
289nc = div add p sub OCKC 1 OCKC
290pos = hl
291for k ulong 3
292for c ulong nc
293i = mul c OCKC
294m = sub p i
295if gt m OCKC
296m = OCKC
297end
298nb = mul m 8
299if ne oprd ai pos 0 nb nb
300exit 70
301end
302h = ocfnvw h nb
303ocdec k i m
304pos = add pos nb
305end
306end
307if eq h hs
308ret 1
309end
310ret 0
311end
312
313note G against the loaded shapes: 1 <= G <= B and B mod G = 0
314fn ogok2 g ulong to bool
315b = get og 6
316if or eq g 0 gt g b
317ret false
318end
319ret eq rem b g 0
320end