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 / adtrain / mlp.om

409 lines · 7066 bytes Raw Blame History
1note SPDX-License-Identifier: AGPL-3.0-only
2note adtrain: an 8x4 -> 8 -> 2 relu MLP trained by full-batch gradient descent.
3note Gradients come from the native reverse-mode tape (omega-mv native/autodiff664);
4note nothing here is hand-derived. Unit order: metadata.om, tape.om, this file.
5note Graph, nodes 0..16: X W1 b1 W2 b2 T MM1 BC1 ADD1 RELU MM2 BC2 ADD2 SUB MUL SUM1 SUM0.
6note Params (58, MP order): W1[4,8] 0..31, b1[1,8] 32..39, W2[8,2] 40..55, b2[1,2] 56..57.
7note Exits: 86 non-finite loss or parameter, 87 tape refusal, 74 file I/O.
8global MX f32 32
9global MT f32 16
10global MP f32 58
11global MG f32 58
12global MS ulong 2
13global MB uchar 1024
14global MQ uchar 64
15text MCKP "adtrain.ck"
16text MFXP "adtrain.fix"
17text MCKM "ADCKPT01"
18text MFXM "ADFX"
19
20fn mlbits x f32 to ulong
21ret ulong bitcast uint x
22end
23
24note Bit-pattern finiteness test of aien-train native/ecosystem6/affine.om affinite.
25fn mlfinite x f32 to bool
26b = mlbits x
27if gt b 4294967295
28ret false
29end
30ret lt and b 2147483647 2139095040
31end
32
33fn mlnext to ulong
34x = get MS 0
35x = xor x shl x 13
36x = xor x shr x 7
37x = xor x shl x 17
38put MS 0 x
39ret x
40end
41
42note Next generator value k in -8..8.
43fn mlk to long
44ret sub long rem mlnext 17 8
45end
46
47note Dyadic sample k / scale; exact in binary32.
48fn mldy scale f32 to f32
49ret div f32 mlk scale
50end
51
52note Data X, T (k/8) then W1, W2 (k/16) from one xorshift64 stream; biases 0.
53fn mlinit seed ulong to void
54put MS 0 seed
55put MS 1 0
56for i ulong 32
57put MX i mldy 8.0f32
58end
59for i ulong 16
60put MT i mldy 8.0f32
61end
62for i ulong 32
63put MP i mldy 16.0f32
64end
65for i ulong 16
66put MP add 40 i mldy 16.0f32
67end
68for i ulong 8
69put MP add 32 i 0.0f32
70end
71put MP 56 0.0f32
72put MP 57 0.0f32
73end
74
75fn mllr to f32
76ret div 1.0f32 64.0f32
77end
78
79fn mlid got int want ulong to void
80if ne got int want
81exit 87
82end
83end
84
85fn mlldp id ulong off ulong cnt ulong to void
86for i ulong cnt
87mlid tpset id i get MP add off i 0
88end
89end
90
91fn mlleaf to void
92mlid tpleaf 8 4 0 0
93mlid tpleaf 4 8 1 1
94mlid tpleaf 1 8 1 2
95mlid tpleaf 8 2 1 3
96mlid tpleaf 1 2 1 4
97mlid tpleaf 8 2 0 5
98for i ulong 32
99mlid tpset 0 i get MX i 0
100end
101mlldp 1 0 32
102mlldp 2 32 8
103mlldp 3 40 16
104mlldp 4 56 2
105for i ulong 16
106mlid tpset 5 i get MT i 0
107end
108end
109
110note Forward pass on a fresh tape; node ids are checked against the fixed order.
111fn mlfwd to f32
112tpreset
113mlleaf
114mlid tpmm 0 1 6
115mlid tpbcast 2 8 7
116mlid tpadd 6 7 8
117mlid tprelu 8 9
118mlid tpmm 9 3 10
119mlid tpbcast 4 8 11
120mlid tpadd 10 11 12
121mlid tpsub 12 5 13
122mlid tpmul 13 13 14
123mlid tpsum 14 1 15
124mlid tpsum 15 0 16
125l = tpval 16 0
126if not mlfinite l
127exit 86
128end
129ret l
130end
131
132note Forward + backward; gradients of the 58 parameters into MG. Returns the loss.
133fn mlgrad to f32
134l = mlfwd
135mlid tpback 16 0
136for i ulong 32
137put MG i tpgrad 1 i
138end
139for i ulong 8
140put MG add 32 i tpgrad 2 i
141end
142for i ulong 16
143put MG add 40 i tpgrad 3 i
144end
145put MG 56 tpgrad 4 0
146put MG 57 tpgrad 4 1
147ret l
148end
149
150note p = p - round(lr * g); every parameter must stay finite.
151fn mlupd to void
152lr = mllr
153for i ulong 58
154p = sub get MP i mul lr get MG i
155if not mlfinite p
156exit 86
157end
158put MP i p
159end
160end
161
162note One training step: loss at the current parameters, then the update.
163fn mlstep to f32
164l = mlgrad
165mlupd
166put MS 1 add get MS 1 1
167ret l
168end
169
170note Relu pattern of ADD1 (node 8) after mlfwd: 1 where the input is > 0.
171fn mlmask to void
172for i ulong 64
173put MQ i 0
174if gt tpval 8 i 0.0f32
175put MQ i 1
176end
177end
178end
179
180fn mlsame to bool
181for i ulong 64
182v = uchar 0
183if gt tpval 8 i 0.0f32
184v = uchar 1
185end
186if ne v get MQ i
187ret false
188end
189end
190ret true
191end
192
193fn mlput pos ulong v ulong to void
194for i ulong 8
195put MB add pos i uchar and shr v mul i 8 255
196end
197end
198
199fn mlget pos ulong to ulong
200v = ulong 0
201for i ulong 8
202v = or v shl ulong get MB add pos i mul i 8
203end
204ret v
205end
206
207fn mlput4 pos ulong v ulong to void
208for i ulong 4
209put MB add pos i uchar and shr v mul i 8 255
210end
211end
212
213note FNV-1a 64 of the four little-endian bytes of w, continuing h.
214fn mlfnv4 h ulong w ulong to ulong
215for i ulong 4
216h = mul xor h and shr w mul i 8 255 1099511628211
217end
218ret h
219end
220
221note Data binding: FNV-1a 64 over the 32 X and 16 T bit patterns.
222fn mlhash to ulong
223h = ulong 14695981039346656037
224for i ulong 32
225h = mlfnv4 h mlbits get MX i
226end
227for i ulong 16
228h = mlfnv4 h mlbits get MT i
229end
230ret h
231end
232
233fn mlimgfnv n ulong to ulong
234h = ulong 14695981039346656037
235for i ulong n
236h = mul xor h ulong get MB i 1099511628211
237end
238ret h
239end
240
241note The u64 whose little-endian bytes are the text "ADCKPT01".
242fn mlmagic to ulong
243v = ulong 0
244for i ulong 8
245v = or v shl ulong get MCKM i mul i 8
246end
247ret v
248end
249
250fn mlwrfile fd int n ulong to void
251o = ulong 0
252while lt o n bound 1025
253w = write fd MB o sub n o
254if le w 0
255exit 74
256end
257o = add o ulong w
258end
259end
260
261note 536-byte image, u64 LE words: magic, 1, step, 58, lr bits, loss bits,
262note data hash, 0, 58 params (f32 bits zero-extended), FNV-1a 64 of words 0..65.
263fn cksave loss f32 to void
264mlput 0 mlmagic
265mlput 8 1
266mlput 16 get MS 1
267mlput 24 58
268mlput 32 mlbits mllr
269mlput 40 mlbits loss
270mlput 48 mlhash
271mlput 56 0
272for i ulong 58
273mlput add 64 mul i 8 mlbits get MP i
274end
275mlput 528 mlimgfnv 528
276fd = int fopen MCKP 0 len MCKP 578 384
277if lt fd 0
278exit 74
279end
280mlwrfile fd 536
281if ne fsync fd 0
282exit 74
283end
284if ne fclose fd 0
285exit 74
286end
287end
288
289fn mlrdfile fd int to ulong
290n = ulong 0
291while lt n 1024 bound 1025
292r = read fd MB n sub 1024 n
293if lt r 0
294exit 74
295end
296if eq r 0
297ret n
298end
299n = add n ulong r
300end
301ret n
302end
303
304note Every structural check of an image already in MB; -1 on any failure.
305fn ckcheck to int
306if ne mlget 528 mlimgfnv 528
307ret -1
308end
309if ne mlget 0 mlmagic
310ret -1
311end
312if or ne mlget 8 1 ne mlget 24 58
313ret -1
314end
315if or gt mlget 16 200 ne mlget 32 mlbits mllr
316ret -1
317end
318if ne mlget 56 0
319ret -1
320end
321for i ulong 58
322w = mlget add 64 mul i 8
323if not mlfinite bitcast f32 uint and w 4294967295
324ret -1
325end
326if gt w 4294967295
327ret -1
328end
329end
330ret 0
331end
332
333note Load adtrain.ck: -1 corrupt (size, checksum, header, params), -2 data hash
334note differs from the current X, T. A refused load changes no parameter.
335fn ckload to int
336fd = int fopen MCKP 0 len MCKP 0 0
337if lt fd 0
338ret -1
339end
340n = mlrdfile fd
341if ne fclose fd 0
342exit 74
343end
344if ne n 536
345ret -1
346end
347st = ckcheck
348if ne st 0
349ret st
350end
351if ne mlget 48 mlhash
352ret -2
353end
354for i ulong 58
355put MP i bitcast f32 uint mlget add 64 mul i 8
356end
357put MS 1 mlget 16
358ret 0
359end
360
361fn mlfxp pos ulong x f32 to ulong
362mlput4 pos mlbits x
363ret add pos 4
364end
365
366note Fixture for the C M21 oracle (omega-mv tests/test_omega_ad_fixture.c):
367note u32 LE words "ADFX", 1, 8, 4, 8, 2, X W1 b1 W2 b2 T, then Y loss dW1 db1
368note dW2 db2 from the last mlgrad: 187 words, 748 bytes.
369fn mlfix to void
370for i ulong 4
371put MB i get MFXM i
372end
373mlput4 4 1
374mlput4 8 8
375mlput4 12 4
376mlput4 16 8
377mlput4 20 2
378p = ulong 24
379for i ulong 32
380p = mlfxp p get MX i
381end
382for i ulong 58
383p = mlfxp p get MP i
384end
385for i ulong 16
386p = mlfxp p get MT i
387end
388for i ulong 16
389p = mlfxp p tpval 12 i
390end
391p = mlfxp p tpval 16 0
392for i ulong 58
393p = mlfxp p get MG i
394end
395if ne p 748
396exit 74
397end
398fd = int fopen MFXP 0 len MFXP 578 384
399if lt fd 0
400exit 74
401end
402mlwrfile fd 748
403if ne fsync fd 0
404exit 74
405end
406if ne fclose fd 0
407exit 74
408end
409end