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

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