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 / omegalora / lora.om

503 lines · 10868 bytes Raw Blame History
1note SPDX-License-Identifier: AGPL-3.0-only
2note omegalora lora.om (core v1): LoRA fine-tuning on a frozen base, written in Omega from scratch.
3note Network (one transformer MLP block shape): h0 = rmsnorm(x); p1 = (W1 + s*B1*A1) h0; a1 = silu(p1);
4note n1 = rmsnorm(a1); z = (W2 + s*B2*A2) n1; loss = mean cross-entropy(z, label).
5note W1, W2 are frozen. Only the LoRA factors A1 B1 A2 B2 learn (AdamW). Everything is f64 so this
6note file is the reference the GPU kernels are checked against.
7note stdin selects a deliberately broken build: 1 silu grad, 2 rmsnorm grad, 3 adam sign. 0 or empty = real.
8note exit 0 pass; 13 exp/log; 10 gradient check; 11 loss did not drop; 12 accuracy did not rise.
9note Constants (Wolfram, 2026-11-06, evidence/OMEGALORA/wolfram.txt): 1/ln2 1.4426950408889634,
10note ln2 0.6931471805599453, sqrt2 1.4142135623730951, bits(1.0) 4607182418800017408,
11note bits(+inf) 9218868437227405312, 2^52 - 1 4503599627370495. exp: degree 14 Taylor on [0, ln2),
12note remainder bound 2 ln2^15/15! = 6.26e-15. log: atanh series to s^41, |s| <= 0.1716, tail 5.8e-35.
13note pool layout (f64 slots): W1 0, W2 384, params 1024 (A1 +0, B1 +64, A2 +160, B2 +256; 304 total),
14note grads 2048, adam m 3072, adam v 4096, inputs 5120, activations 8192 (128 per sample),
15note backward scratch 16384, teacher W1t 20000, W2t 20480, factor temp 21000, matvec temp 30000, adam powers 31000.
16global m f64 32768
17global lab ulong 64
18global rs ulong 1
19global mut ulong 1
20global ob uchar 64
21fn putc c ulong to void
22put ob 0 uchar c
23w = write 1 ob 0 1
24end
25fn putu n ulong to void
26tmp = new uchar 24 0
27k = ulong 0
28x = n
29if eq x 0
30putc 48
31ret
32end
33while gt x 0
34put tmp k uchar add 48 rem x 10
35x = div x 10
36k = add k 1
37end
38while gt k 0
39k = sub k 1
40putc ulong get tmp k
41end
42end
43fn putf x f64 to void
44v = x
45if lt v 0.0f64
46putc 45
47v = neg v
48end
49ip = ulong v
50putu ip
51putc 46
52fr = sub v f64 ip
53for i ulong 12
54fr = mul fr 10.0f64
55d = ulong fr
56putc add 48 d
57fr = sub fr f64 d
58end
59putc 10
60end
61text tex "exp(1) "
62text tlg "log(10) "
63text tgc "gradient check, worst relative error "
64text tl0 "loss before "
65text tl1 "loss after "
66text ta0 "accuracy before "
67text ta1 "accuracy after "
68fn say t ulong to void
69w = long 0
70if eq t 0
71w = write 1 tex 0 len tex
72end
73if eq t 1
74w = write 1 tlg 0 len tlg
75end
76if eq t 2
77w = write 1 tgc 0 len tgc
78end
79if eq t 3
80w = write 1 tl0 0 len tl0
81end
82if eq t 4
83w = write 1 tl1 0 len tl1
84end
85if eq t 5
86w = write 1 ta0 0 len ta0
87end
88if eq t 6
89w = write 1 ta1 0 len ta1
90end
91end
92fn fexp x f64 to f64
93if lt x -708.0f64
94ret 0.0f64
95end
96if gt x 709.0f64
97ret bitcast f64 ulong 9218868437227405312
98end
99t = mul x 1.4426950408889634f64
100k = long t
101if gt f64 k t
102k = sub k 1
103end
104r = sub x mul f64 k 0.6931471805599453f64
105p = 1.0f64
106c = long 14
107while gt c 0
108p = add 1.0f64 div mul p r f64 c
109c = sub c 1
110end
111e = bitcast f64 shl ulong add k 1023 52
112ret mul p e
113end
114fn flog x f64 to f64
115b = bitcast ulong x
116ex = sub long and shr b 52 2047 1023
117mm = bitcast f64 or and b 4503599627370495 4607182418800017408
118if gt mm 1.4142135623730951f64
119mm = mul mm 0.5f64
120ex = add ex 1
121end
122s = div sub mm 1.0f64 add mm 1.0f64
123s2 = mul s s
124acc = 0.0f64
125c = long 41
126while gt c 0
127acc = add div 1.0f64 f64 c mul s2 acc
128c = sub c 2
129end
130ret add mul 2.0f64 mul s acc mul f64 ex 0.6931471805599453f64
131end
132fn rnd to f64
133x = get rs 0
134x = xor x shl x 13
135x = xor x shr x 7
136x = xor x shl x 17
137put rs 0 x
138ret sub div f64 shr x 11 4503599627370496.0f64 1.0f64
139end
140fn fill off ulong n ulong sc f64 to void
141for i ulong n
142put m add off i mul sc rnd
143end
144end
145fn zero off ulong n ulong to void
146for i ulong n
147put m add off i 0.0f64
148end
149end
150note sum over i < n of a[i*sa] * b[i]
151fn sdot a ulong sa ulong b ulong n ulong to f64
152s = 0.0f64
153ai = a
154for i ulong n
155av = get m ai
156s = add s mul av get m add b i
157ai = add ai sa
158end
159ret s
160end
161note y[i] += c * x[i] for i < n
162fn axpy y ulong x ulong n ulong c f64 to void
163for i ulong n
164yi = add y i
165xv = mul c get m add x i
166put m yi add get m yi xv
167end
168end
169note y = W x, W is rows x cols row-major
170fn mv w ulong x ulong y ulong rows ulong cols ulong to void
171for r ulong rows
172put m add y r sdot add w mul r cols 1 x cols
173end
174end
175note o += W^T g
176fn mvtacc w ulong g ulong o ulong rows ulong cols ulong to void
177for c ulong cols
178oi = add o c
179put m oi add get m oi sdot add w c cols g rows
180end
181end
182note dw += sc * g x^T
183fn outacc g ulong x ulong dw ulong rows ulong cols ulong sc f64 to void
184for r ulong rows
185axpy add dw mul r cols x cols mul sc get m add g r
186end
187end
188fn rmsn a ulong o ulong n ulong to f64
189ss = 0.0f64
190for i ulong n
191v = get m add a i
192ss = add ss mul v v
193end
194r = fsqrt add div ss f64 n 0.000001f64
195for i ulong n
196put m add o i div get m add a i r
197end
198ret r
199end
200fn sig p f64 to f64
201ret div 1.0f64 add 1.0f64 fexp neg p
202end
203note forward for sample i; lora adds s*B*A on both layers (s = alpha/r = 8/4 = 2)
204note sample slots: h0 +0 (16), t1 +16 (4), p1 +20 (24), a1 +44 (24), n1 +68 (24), r1 +92, t2 +93 (4), z +97 (12)
205fn base i ulong to ulong
206ret add 8192 mul i 128
207end
208fn silu a ulong p ulong n ulong to void
209for k ulong n
210v = get m add p k
211put m add a k mul v sig v
212end
213end
214fn fwd i ulong w1 ulong w2 ulong lora bool to void
215b = base i
216q = rmsn add 5120 mul i 16 b 16
217mv w1 b add b 20 24 16
218if lora
219mv 1024 b add b 16 4 16
220mv 1088 add b 16 30000 24 4
221axpy add b 20 30000 24 2.0f64
222end
223silu add b 44 add b 20 24
224put m add b 92 rmsn add b 44 add b 68 24
225mv w2 add b 68 add b 97 12 24
226if lora
227mv 1184 add b 68 add b 93 4 24
228mv 1280 add b 93 30000 12 4
229axpy add b 97 30000 12 2.0f64
230end
231end
232fn amax z ulong n ulong to ulong
233best = ulong 0
234for k ulong n
235if gt get m add z k get m add z best
236best = k
237end
238end
239ret best
240end
241fn lossi i ulong to f64
242z = add add 8192 mul i 128 97
243mx = get m add z amax z 12
244se = 0.0f64
245for k ulong 12
246se = add se fexp sub get m add z k mx
247end
248ret sub add mx flog se get m add z get lab i
249end
250fn total to f64
251s = 0.0f64
252for i ulong 64
253fwd i 0 384 true
254s = add s lossi i
255end
256ret div s 64.0f64
257end
258fn accuracy to f64
259c = 0.0f64
260for i ulong 64
261fwd i 0 384 true
262if eq amax add add 8192 mul i 128 97 12 get lab i
263c = add c 1.0f64
264end
265end
266ret div c 64.0f64
267end
268note backward for sample i (fwd i must have run with lora); adds into grads at 2048
269note backward scratch: dz 16384 (12), dt2 16400 (4), dn1 16404 (24), da1 16428 (24), dp1 16452 (24), dt1 16476 (4)
270fn scal y ulong n ulong c f64 to void
271for k ulong n
272put m add y k mul c get m add y k
273end
274end
275note dz = (softmax(z) - onehot(label)) / batch
276fn bce i ulong to void
277z = add base i 97
278mx = get m add z amax z 12
279se = 0.0f64
280for k ulong 12
281se = add se fexp sub get m add z k mx
282end
283y = get lab i
284for k ulong 12
285g = div fexp sub get m add z k mx se
286if eq k y
287g = sub g 1.0f64
288end
289put m add 16384 k div g 64.0f64
290end
291end
292note rmsnorm backward: da = (dn - n * mean(dn * n)) / r
293fn brms i ulong to void
294n1 = add base i 68
295r1 = get m add base i 92
296dot = div sdot 16404 1 n1 24 24.0f64
297mu = get mut 0
298for k ulong 24
299dn = get m add 16404 k
300nv = mul get m add n1 k dot
301v = sub dn nv
302if eq mu 2
303v = add dn nv
304end
305put m add 16428 k div v r1
306end
307end
308note silu backward: d silu(p) = sig(p) (1 + p (1 - sig(p)))
309fn bsilu i ulong to void
310p1 = add base i 20
311mu = get mut 0
312for k ulong 24
313p = get m add p1 k
314sg = sig p
315d = mul sg add 1.0f64 mul p sub 1.0f64 sg
316if eq mu 1
317d = mul sg sub 1.0f64 mul p sub 1.0f64 sg
318end
319dv = mul get m add 16428 k d
320put m add 16452 k dv
321end
322end
323fn back i ulong to void
324b = base i
325bce i
326outacc 16384 add b 93 2304 12 4 2.0f64
327zero 16400 4
328mvtacc 1280 16384 16400 12 4
329scal 16400 4 2.0f64
330outacc 16400 add b 68 2208 4 24 1.0f64
331zero 16404 24
332mvtacc 384 16384 16404 12 24
333mvtacc 1184 16400 16404 4 24
334brms i
335bsilu i
336outacc 16452 add b 16 2112 24 4 2.0f64
337zero 16476 4
338mvtacc 1088 16452 16476 24 4
339scal 16476 4 2.0f64
340outacc 16476 b 2048 4 16 1.0f64
341end
342fn grads to f64
343zero 2048 304
344s = 0.0f64
345for i ulong 64
346fwd i 0 384 true
347s = add s lossi i
348back i
349end
350ret div s 64.0f64
351end
352note AdamW: beta1 0.9, beta2 0.999, eps 1e-8, weight decay 0.01. b1t, b2t = beta^t
353fn adam1 j ulong lr f64 b1t f64 b2t f64 to void
354g = get m add 2048 j
355mo = mul 0.9f64 get m add 3072 j
356mo = add mo mul 0.1f64 g
357vo = mul 0.999f64 get m add 4096 j
358g = mul g g
359vo = add vo mul 0.001f64 g
360put m add 3072 j mo
361put m add 4096 j vo
362mo = div mo sub 1.0f64 b1t
363vo = div vo sub 1.0f64 b2t
364vo = add fsqrt vo 0.00000001f64
365pj = get m add 1024 j
366g = add div mo vo mul 0.01f64 pj
367g = mul lr g
368if eq get mut 0 3
369put m add 1024 j add pj g
370else
371put m add 1024 j sub pj g
372end
373end
374fn adam lr f64 to void
375b1t = mul get m 31000 0.9f64
376b2t = mul get m 31001 0.999f64
377put m 31000 b1t
378put m 31001 b2t
379for j ulong 304
380adam1 j lr b1t b2t
381end
382end
383fn fabsd x f64 to f64
384if lt x 0.0f64
385ret neg x
386end
387ret x
388end
389note teacher entry: t = w + sum_q u[r,q] v[q,c], u is rows x 4, v is 4 x cols
390fn tent w ulong u ulong v ulong cols ulong r ulong c ulong to f64
391s = get m add w add mul r cols c
392ret add s sdot add v c cols add u mul r 4 4
393end
394fn teach to void
395for r ulong 24
396for c ulong 16
397put m add 20000 add mul r 16 c tent 0 21000 21100 16 r c
398end
399end
400for r ulong 12
401for c ulong 24
402put m add 20480 add mul r 24 c tent 384 21200 21300 24 r c
403end
404end
405for i ulong 64
406fwd i 20000 20480 false
407put lab i amax add base i 97 12
408end
409end
410note relative error of the analytic gradient of parameter j against a central difference
411fn gerr j ulong to f64
412pi = add 1024 j
413p0 = get m pi
414put m pi add p0 0.00001f64
415lp = total
416put m pi sub p0 0.00001f64
417lmn = total
418put m pi p0
419num = div sub lp lmn 0.00002f64
420an = get m add 2048 j
421den = fmax add fabsd num fabsd an 0.00000001f64
422ret div fabsd sub num an den
423end
424fn gcheck to f64
425fill 1024 304 0.3f64
426ld = grads
427worst = 0.0f64
428j = ulong 0
429while lt j 304
430worst = fmax worst gerr j
431j = add j 3
432end
433ret worst
434end
435fn tinit to void
436fill 1024 64 0.1f64
437zero 1088 96
438fill 1184 96 0.1f64
439zero 1280 48
440zero 3072 304
441zero 4096 304
442put m 31000 1.0f64
443put m 31001 1.0f64
444end
445fn main to int
446k = read 0 ob 0 1
447if eq k 1
448put mut 0 ulong sub ulong get ob 0 48
449end
450put rs 0 ulong 88172645463325252
451note 1. exp and log against known constants
452say 0
453putf fexp 1.0f64
454say 1
455putf flog 10.0f64
456if gt fabsd sub fexp 1.0f64 2.718281828459045f64 0.00000000000001f64
457ret 13
458end
459if gt fabsd sub flog 10.0f64 2.302585092994046f64 0.00000000000001f64
460ret 13
461end
462note 2. data: frozen base, a hidden low-rank teacher change, inputs and teacher labels
463fill 0 384 0.5f64
464fill 384 288 0.5f64
465fill 5120 1024 1.0f64
466fill 21000 96 0.6f64
467fill 21100 64 0.6f64
468fill 21200 48 0.6f64
469fill 21300 96 0.6f64
470teach
471note 3. gradient check at a random point (B nonzero), every 3rd parameter
472worst = gcheck
473say 2
474putf worst
475if gt worst 0.000001f64
476ret 10
477end
478note 4. train from the base model (B zero) with AdamW
479tinit
480l0 = total
481a0 = accuracy
482for step ulong 400
483ld = grads
484adam 0.02f64
485end
486l1 = total
487a1 = accuracy
488say 3
489putf l0
490say 4
491putf l1
492say 5
493putf a0
494say 6
495putf a1
496if gt l1 mul 0.5f64 l0
497ret 11
498end
499if lt a1 add a0 0.2f64
500ret 12
501end
502ret 0
503end