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

331 lines · 7004 bytes Raw Blame History
1note SPDX-License-Identifier: AGPL-3.0-only
2note omegallm adam.om (aien-train#52): init, global-norm clip, Adam with bias correction, the LR
3note schedule, gradient blocks (CONTRACT-V1 section 5) and one training step (CONTRACT section 5).
4note Needs math.om, data.om, model.om.
5note Exact operation order (agreed with the oracle lane, recorded in CONTRACT section 5):
6note n = sqrt(sum_i g_i^2) (i ascending); if n > 1: c = 1/n, g_i = g_i c.
7note b^(s+1) by s+1 repeated multiplications starting from 1.0; bc = 1 - b^(s+1).
8note m = 0.9 m + (1.0 - 0.9) g; v = 0.99 v + (1.0 - 0.99)(g g);
9note p = p - (lr_s (m/bc1)) / (sqrt(v/bc2) + 1e-8).
10note lr_s = (lr (s+1))/100 for s < 100, else lr (1 - (0.9 (s-100))/max(1, TOTAL-100)).
11
12note Mutant c (bias correction removed): 1 in the product, 0 in the mutant.
13const OMUTC ulong 1
14
15note Mutant d (gradient blocks combined in reverse order, G-1 first): 1 in the product, 0 in the
16note mutant.
17const OMUTD ulong 1
18
19note per-sequence losses of the current batch
20global olos f64 64
21note ofreeze: when > 0, parameters i < ofreeze[0] are frozen (gradient zeroed, m/v/w untouched).
22global ofreeze ulong 1
23
24note init (CONTRACT section 5): r_i = (splitmix64(S+i) >> 11) 2^-53 (N[2^-53] ->
25note 1.1102230246251565*^-16); w = (2 r - 1) 0.034641016151377546 (N[0.02 Sqrt[3]]); wo and w2
26note times 1/sqrt(2L); gains 1; m = v = 0.
27fn oinit to void
28p = get og 8
29sd = get og 40
30for q ulong ocn p
31i = mul q OCH
32ie = oce q p
33while lt i ie bound 999999
34r = mul f64 shr smx add sd i 11 1.1102230246251565e-16
35owp i mul sub add r r 1.0 0.034641016151377546
36i = add i 1
37end
38end
39d = get og 2
40ri = div 1.0f64 fsqrt f64 mul 2 get og 5
41for l ulong get og 5
42oiset opg1 l d 1.0
43oiset opg2 l d 1.0
44oiscale opwo l mul d d ri
45oiscale opw2 l mul d get og 4 ri
46end
47oiset get og 33 d 1.0
48ozerom mul 2 p
49end
50
51fn ozerom n ulong to void
52for q ulong ocn n
53i = mul q OCH
54ie = oce q n
55while lt i ie bound 999999
56omvp i 0.0f64
57i = add i 1
58end
59end
60end
61
62fn oiset o ulong n ulong v f64 to void
63for i ulong n
64owp add o i v
65end
66end
67
68fn oiscale o ulong n ulong v f64 to void
69for i ulong n
70owp add o i mul owg add o i v
71end
72end
73
74fn olrs s ulong lr f64 to f64
75if lt s 100
76ret div mul lr f64 add s 1 100.0
77end
78den = ulong 1
79if gt get og 42 101
80den = sub get og 42 100
81end
82ret mul lr sub 1.0 div mul 0.9 f64 sub s 100 f64 den
83end
84
85fn opowb b f64 s ulong to f64
86pw = 1.0f64
87for i ulong add s 1
88pw = mul pw b
89end
90ret pw
91end
92
93note clip the gradient to global norm 1 and apply one Adam update at step og[41]
94fn oadam to void
95p = get og 8
96fz = get ofreeze 0
97ss = 0.0f64
98for q ulong ocn p
99i = mul q OCH
100ie = oce q p
101while lt i ie bound 999999
102if lt i fz
103ocsp i 0.0f64
104else
105g = ocsg i
106ss = add ss mul g g
107end
108i = add i 1
109end
110end
111nr = fsqrt ss
112if gt nr 1.0
113oclip div 1.0 nr
114end
115s = get og 41
116lrs = olrs s bitcast f64 get og 43
117bc1 = sub 1.0 opowb 0.9 s
118bc2 = sub 1.0 opowb 0.99 s
119if eq OMUTC 0
120bc1 = 1.0f64
121bc2 = 1.0f64
122end
123put ofs 3 lrs
124put ofs 4 bc1
125put ofs 5 bc2
126for q2 ulong ocn p
127k = mul q2 OCH
128ke = oce q2 p
129while lt k ke bound 999999
130oadam1 k
131k = add k 1
132end
133end
134end
135
136note g_i = g_i c for i < P
137fn oclip c f64 to void
138p = get og 8
139for q ulong ocn p
140j = mul q OCH
141je = oce q p
142while lt j je bound 999999
143ocsp j mul ocsg j c
144j = add j 1
145end
146end
147end
148
149note moments of parameter i: m = 0.9 m + (1 - 0.9) g; v = 0.99 v + (1 - 0.99) g^2
150fn oadam1 i ulong to void
151if lt i get ofreeze 0
152ret
153end
154g = ogrg add get og 10 i
155mo = add get og 11 i
156m = omvg mo
157m = mul 0.9 m
158a = sub 1.0f64 0.9
159m = add m mul a g
160omvp mo m
161vo = add get og 12 i
162v = omvg vo
163v = mul 0.99 v
164a = sub 1.0f64 0.99
165v = add v mul a mul g g
166omvp vo v
167oadam2 i m v
168end
169
170note p[i] -= lr (m/bc1) / (sqrt(v/bc2) + 1e-8) with lr, bc1, bc2 in ofs 3, 4, 5
171fn oadam2 i ulong m f64 v f64 to void
172u = div m get ofs 4
173u = mul get ofs 3 u
174w = div v get ofs 5
175w = add fsqrt w 0.00000001
176u = div u w
177w = owg i
178owp i sub w u
179end
180
181note one training step at step og[41] on TRAIN; returns the batch loss at the pre-update
182note parameters and increments the step. ogradw (lio.om, eio.om) runs the K workers.
183fn ostep to f64
184s = get og 41
185obatch s
186ls = ogradw
187oadam
188put og 41 add s 1
189ret ls
190end
191
192note gradient of the step loss for the batch in obx/oby (no clip, no update); returns the loss.
193note CONTRACT-V1 section 5: G = og[44] blocks of R = B/G sequences; block j is accumulated from
194note zero for b ascending; GRD = block 0, then GRD[i] = GRD[i] + block j[i] for j ascending.
195note G = 1 is the v0 gradient bit for bit. Every block is accumulated in BLK (CONTRACT-V3 section 2)
196note and block 0 copied into GRD, the same bits as accumulating it in GRD.
197fn ograd to f64
198gg = get og 44
199for i ulong gg
200j = i
201if eq OMUTD 0
202j = sub sub gg 1 i
203end
204oblkc j
205ogfold eq i 0
206end
207ret olsum
208end
209
210note block j into BLK (gradient pool [P, 2P), zeroed first); losses into olos
211fn oblkc j ulong to void
212p = get og 8
213put og 10 p
214ozerog p p
215bb = get og 6
216sc = div 1.0f64 f64 mul get og 1 bb
217r = div bb get og 44
218b = mul j r
219for i ulong r
220put olos b ofwd b
221obwd b sc
222b = add b 1
223end
224put og 10 0
225end
226
227note BLK into GRD: copied for the first block, else GRD[i] = GRD[i] + BLK[i] (ogadd)
228fn ogfold first bool to void
229if not first
230ogadd
231ret
232end
233p = get og 8
234for q ulong ocn p
235i = mul q OCH
236ie = oce q p
237while lt i ie bound 999999
238ocsp i ogrg add p i
239i = add i 1
240end
241end
242end
243
244note GRD[i] = GRD[i] + BLK[i] for i ascending
245fn ogadd to void
246p = get og 8
247for q ulong ocn p
248i = mul q OCH
249ie = oce q p
250while lt i ie bound 999999
251ogadd1 i p
252i = add i 1
253end
254end
255end
256
257note GRD[i] += BLK[i] (BLK at P)
258fn ogadd1 i ulong p ulong to void
259x = ogrg add p i
260ocsp i add ocsg i x
261end
262
263note step loss: sum of olos[b] for b ascending from 0.0, divided by f64 B
264fn olsum to f64
265ls = 0.0f64
266bb = get og 6
267for b ulong bb
268ls = add ls get olos b
269end
270ret div ls f64 bb
271end
272
273note adamck: Adam self-check on the small shape. With a constant gradient g from zero moments,
274note exact arithmetic gives m/bc1 = g and v/bc2 = g^2 at every step, so after K steps
275note p = -sum_s lr_s g/(|g| + 1e-8). Prints `A K maxrel_bits_hex maxrel_e15` and exits 41 when
276note maxrel > 1e-12. Without bias correction (mutant c) the step-1 ratio is exactly 1 and the step-2
277note ratio is 1.347; the query and the predicted 0.63909 are in docs/omegallm/README.md, "Sizing".
278fn cmdadamck to void
279put og 0 256
280put og 1 8
281put og 2 16
282put og 3 2
283put og 4 32
284put og 5 2
285put og 6 2
286if not ogeom
287exit 12
288end
289put og 42 200
290put og 43 bitcast ulong 0.001f64
291put og 41 0
292p = get og 8
293for i ulong p
294x = sub f64 rem i 7 3.0
295x = mul x 0.0001
296ogrp add get og 10 i x
297end
298for k ulong 5
299oadam
300put og 41 add get og 41 1
301end
302mx = 0.0f64
303for j ulong p
304mx = fmax mx oadamex j
305end
306oputc 65
307oputc 32
308oputu 5
309oputc 32
310oputx bitcast ulong mx
311oputc 32
312oputl long mul mx 1000000000000000.0
313oputc 10
314oflush
315if gt mx 0.000000000001
316exit 41
317end
318end
319
320fn oadamex j ulong to f64
321g = ogrg add get og 10 j
322w = 0.0f64
323for s ulong 5
324w = sub w div mul olrs s bitcast f64 get og 43 g add fabs g 0.00000001
325end
326d = fabs sub owg j w
327if eq w 0.0
328ret d
329end
330ret div d fabs w
331end