interplane.Get Omega
Read in your language

omega-helix

This project has not described itself yet.

Brought by drakestapleton

branch main Branches Tags History

omega-helix / native / sp_solve.om

1130 lines · 27393 bytes Raw Blame History
1note SPDX-License-Identifier: AGPL-3.0-only
2note omegallm sp_solve.om (Pillar 7): Closed-form f64 specialist solver replacing iterative backprop.
3note Reads OLLMFH01 (fcache / fcachem) cached frozen-trunk hidden states H and target tokens y,
4note solves Ridge regression (Primal Cholesky LL^T for D <= N, Dual Woodbury for N < D),
5note runs 1-step Gauss-Newton softmax cross-entropy refinement (sp_gauss_newton_step),
6note and exports valid Merkle-hashed OLLMSP01 .sp specialist packs (cmdspforge).
7note Compatible with both plain target a64macos and om2 (gb10).
8
9const OSP_MAXN ulong 65536
10const OSP_EPS f64 0.000000000000001
11
12global osp_tgt ulong 65536
13global osp_st ulong 32
14global osp_fl f64 16
15
16note Zero ocs[off .. off + n) safely for any n (chunked via ocn/oce to stay under loop bound)
17fn sp_zero_ocs off ulong n ulong to void
18for q ulong ocn n
19i = mul q OCH
20ie = oce q n
21while lt i ie bound 999999
22ocsp add off i 0.0f64
23i = add i 1
24end
25end
26end
27
28note Dot product sum_{i=0..n-1} ocs[a + i*ast] * ocs[b + i*bst]
29fn sp_dot a ulong ast ulong b ulong bst ulong n ulong to f64
30acc = 0.0f64
31ia = a
32ib = b
33for i ulong n
34acc = add acc mul ocsg ia ocsg ib
35ia = add ia ast
36ib = add ib bst
37end
38ret acc
39end
40
41note Compute L[j, j] = sqrt(A[j, j] - sum_{s<j} L[j, s]^2); returns 0.0 on non-SPD pivot
42fn sp_chol_diag j ulong m ulong ao ulong to f64
43row = add ao mul j m
44acc = ocsg add row j
45for s ulong j
46v = ocsg add row s
47acc = sub acc mul v v
48end
49if le acc OSP_EPS
50ret 0.0f64
51end
52ret fsqrt acc
53end
54
55note Compute L[i, j] = (A[i, j] - sum_{s<j} L[i, s]*L[j, s]) * invd
56fn sp_chol_off i ulong j ulong m ulong ao ulong invd f64 to void
57ri = add ao mul i m
58rj = add ao mul j m
59acc = ocsg add ri j
60for s ulong j
61acc = sub acc mul ocsg add ri s ocsg add rj s
62end
63ocsp add ri j mul acc invd
64end
65
66note In-place Cholesky factorization A = L L^T on m x m matrix at ocs[ao .. ao + m*m).
67note Returns 0 on success, 1 if matrix is not positive definite.
68fn sp_chol m ulong ao ulong to ulong
69for j ulong m
70diag = sp_chol_diag j m ao
71if le diag 0.0f64
72ret 1
73end
74ocsp add ao add mul j m j diag
75invd = div 1.0f64 diag
76i = add j 1
77while lt i m bound 999999
78sp_chol_off i j m ao invd
79i = add i 1
80end
81end
82ret 0
83end
84
85note Forward substitution L z = B[:, col] for m x k matrix B at ocs[bo..]
86fn sp_fwd_sub m ulong k ulong lo ulong bo ulong col ulong to void
87for i ulong m
88acc = ocsg add bo add mul i k col
89rl = add lo mul i m
90for j ulong i
91acc = sub acc mul ocsg add rl j ocsg add bo add mul j k col
92end
93ocsp add bo add mul i k col div acc ocsg add rl i
94end
95end
96
97note Backward substitution L^T x = z[:, col] for m x k matrix at ocs[bo..]
98fn sp_bwd_sub m ulong k ulong lo ulong bo ulong col ulong to void
99idx = m
100while gt idx 0 bound 999999
101i = sub idx 1
102acc = ocsg add bo add mul i k col
103j = add i 1
104while lt j m bound 999999
105acc = sub acc mul ocsg add lo add mul j m i ocsg add bo add mul j k col
106j = add j 1
107end
108ocsp add bo add mul i k col div acc ocsg add lo add mul i m i
109idx = i
110end
111end
112
113note Solve (L L^T) X = B in place in ocs[bo .. bo + m*k)
114fn sp_chol_solve m ulong k ulong lo ulong bo ulong to void
115for col ulong k
116sp_fwd_sub m k lo bo col
117sp_bwd_sub m k lo bo col
118end
119end
120
121note Form primal normal matrix G = H^T H + lam I (d x d) at ocs[ao..]
122note H is n x d at ocs[ho..]
123fn sp_gram_primal n ulong d ulong ho ulong ao ulong lam f64 to void
124for a ulong d
125for b ulong add a 1
126v = sp_dot add ho a d add ho b d n
127if eq a b
128v = add v lam
129end
130ocsp add ao add mul a d b v
131ocsp add ao add mul b d a v
132end
133end
134end
135
136note Form dual Gram matrix K = H H^T + lam I (n x n) at ocs[ao..]
137fn sp_gram_dual n ulong d ulong ho ulong ao ulong lam f64 to void
138for i ulong n
139ri = add ho mul i d
140for j ulong add i 1
141rj = add ho mul j d
142v = sp_dot ri 1 rj 1 d
143if eq i j
144v = add v lam
145end
146ocsp add ao add mul i n j v
147ocsp add ao add mul j n i v
148end
149end
150end
151
152note Compute R = H^T Y (d x c) at ocs[ro..], H (n x d) at ho, Y (n x c) at yo
153fn sp_ht_y n ulong d ulong c ulong ho ulong yo ulong to void
154ro = get osp_st 5
155for a ulong d
156for col ulong c
157v = sp_dot add ho a d add yo col c n
158ocsp add ro add mul a c col v
159end
160end
161end
162
163note Compute W = H^T A (d x c) at ocs[wo..], H (n x d) at ho, A (n x c) at ao
164fn sp_ht_a n ulong d ulong c ulong ho ulong ao ulong to void
165wo = get osp_st 5
166for a ulong d
167for col ulong c
168v = sp_dot add ho a d add ao col c n
169ocsp add wo add mul a c col v
170end
171end
172end
173
174note Primal Ridge solver (d <= n): (H^T H + lam I) W = H^T Y
175note Reads ho=osp_st[3], yo=osp_st[4], writes W to osp_st[5], uses A at osp_st[6].
176fn sp_ridge_primal n ulong d ulong c ulong lam f64 to ulong
177ho = get osp_st 3
178yo = get osp_st 4
179wo = get osp_st 5
180ao = get osp_st 6
181sp_gram_primal n d ho ao lam
182sp_ht_y n d c ho yo
183rc = sp_chol d ao
184if ne rc 0
185ret rc
186end
187sp_chol_solve d c ao wo
188ret 0
189end
190
191note Dual Woodbury Ridge solver (n < d): W = H^T (H H^T + lam I)^(-1) Y
192note Reads ho=osp_st[3], yo=osp_st[4], writes W to osp_st[5], uses A at osp_st[6], B at osp_st[7].
193fn sp_ridge_woodbury n ulong d ulong c ulong lam f64 to ulong
194ho = get osp_st 3
195yo = get osp_st 4
196ao = get osp_st 6
197bo = get osp_st 7
198sp_gram_dual n d ho ao lam
199for i ulong n
200for col ulong c
201idx = add mul i c col
202ocsp add bo idx ocsg add yo idx
203end
204end
205rc = sp_chol n ao
206if ne rc 0
207ret rc
208end
209sp_chol_solve n c ao bo
210sp_ht_a n d c ho bo
211ret 0
212end
213
214note Automatic regime-switching Ridge solver: Primal Cholesky if d <= n, Dual Woodbury if n < d.
215note Sets osp_st[9] = 0 (primal) or 1 (Woodbury). Returns 0 on success.
216fn sp_ridge_solve n ulong d ulong c ulong lam f64 to ulong
217if or eq n 0 or eq d 0 or eq c 0 le lam 0.0f64
218ret 1
219end
220if le d n
221put osp_st 9 0
222ret sp_ridge_primal n d c lam
223end
224put osp_st 9 1
225ret sp_ridge_woodbury n d c lam
226end
227
228note ---- Softmax Cross-Entropy & 1-Step Gauss-Newton Refinement ----
229
230note Compute row i logits L[i] = H[i] . W into ocs[rp .. rp + c); returns max logit.
231fn sp_row_logits rh ulong wo ulong rp ulong d ulong c ulong to f64
232mx = bitcast f64 ulong 18442240474082181120
233for col ulong c
234v = sp_dot rh 1 add wo col c d
235ocsp add rp col v
236if gt v mx
237mx = v
238end
239end
240ret mx
241end
242
243note Softmax in place at ocs[rp .. rp + c) given max mx; returns log-sum-exp sum sm.
244fn sp_row_soft rp ulong c ulong mx f64 to f64
245sm = 0.0f64
246for col ulong c
247ev = fexp sub ocsg add rp col mx
248ocsp add rp col ev
249sm = add sm ev
250end
251for col2 ulong c
252ocsp add rp col2 div ocsg add rp col2 sm
253end
254ret sm
255end
256
257note Compute row i logits L[i] = H[i] . W into ocs[po + i*c .. +c), apply softmax in place,
258note and return row cross-entropy loss -log P[i, target_i].
259fn sp_softmax_row i ulong d ulong c ulong po ulong to f64
260rp = add po mul i c
261mx = sp_row_logits add get osp_st 3 mul i d get osp_st 5 rp d c
262yi = get osp_tgt i
263ly = ocsg add rp yi
264sm = sp_row_soft rp c mx
265ret sub add mx flog sm ly
266end
267
268note Compute P = softmax(H W) at ocs[po .. po + n*c) and mean cross-entropy loss over n samples.
269fn sp_eval_ce_mat n ulong d ulong c ulong po ulong to f64
270ls = 0.0f64
271for i ulong n
272ls = add ls sp_softmax_row i d c po
273end
274ret div ls f64 n
275end
276
277note Weighted dot product sum_{i=0..n-1} w[i] * H[i, a] * H[i, b]
278fn sp_wdot a ulong b ulong d ulong woff ulong n ulong to f64
279ho = get osp_st 3
280ia = add ho a
281ib = add ho b
282acc = 0.0f64
283for i ulong n
284v = mul ocsg ia ocsg ib
285acc = add acc mul ocsg add woff i v
286ia = add ia d
287ib = add ib d
288end
289ret acc
290end
291
292note Form weighted primal normal matrix G = H^T diag(w) H + lam I (d x d) at ocs[osp_st[6]..],
293note where w[0..n) is at ocs[woff..] and lam is osp_fl[0].
294fn sp_wgram_primal n ulong d ulong woff ulong to void
295ao = get osp_st 6
296lam = get osp_fl 0
297for a ulong d
298for b ulong add a 1
299acc = sp_wdot a b d woff n
300if eq a b
301acc = add acc lam
302end
303ocsp add ao add mul a d b acc
304ocsp add ao add mul b d a acc
305end
306end
307end
308
309note Form weighted primal RHS rhs = H^T r (d x 1) at ocs[bo..], r[0..n) at ocs[roff..].
310fn sp_w_rhs n ulong d ulong roff ulong bo ulong to void
311ho = get osp_st 3
312for a ulong d
313ha = add ho a
314v = sp_dot ha d roff 1 n
315ocsp add bo a v
316end
317end
318
319note Copy 1 solved column from ocs[bo..bo+d) into column col of ocs[dwo..] (d x c)
320fn sp_copy_col dwo ulong d ulong c ulong col ulong to void
321bo = get osp_st 7
322for a ulong d
323idx = add dwo add mul a c col
324val = ocsg add bo a
325ocsp idx val
326end
327end
328
329note Solve 1 class column col via Primal weighted Cholesky: (H^T diag(w) H + lam I) dW[:, col] = H^T r
330fn sp_gn_col_primal n ulong d ulong c ulong col ulong to ulong
331scr = get osp_st 8
332woff = add scr add mul n c mul d c
333roff = add woff n
334sp_wgram_primal n d woff
335sp_w_rhs n d roff get osp_st 7
336rc = sp_chol d get osp_st 6
337if ne rc 0
338ret rc
339end
340sp_chol_solve d 1 get osp_st 6 get osp_st 7
341sp_copy_col add scr mul n c d c col
342ret 0
343end
344
345note Precompute unregularized K0 = H H^T (n x n) at ocs[koff..] for Dual weighted Woodbury
346fn sp_precomp_k0 n ulong d ulong ho ulong koff ulong to void
347for i ulong n
348ri = add ho mul i d
349for j ulong add i 1
350rj = add ho mul j d
351v = sp_dot ri 1 rj 1 d
352ij = add mul i n j
353ji = add mul j n i
354ocsp add koff ij v
355ocsp add koff ji v
356end
357end
358end
359
360note Compute row i (columns 0..i) of M = S K0 S + lam I into ocs[osp_st[6]..]
361fn sp_gn_wood_row i ulong n ulong soff ulong koff ulong to void
362ao = get osp_st 6
363si = ocsg add soff i
364for j ulong add i 1
365pij = ocsg add koff add mul i n j
366v = mul si mul pij ocsg add soff j
367if eq i j
368v = add v get osp_fl 0
369end
370ocsp add ao add mul i n j v
371ocsp add ao add mul j n i v
372end
373end
374
375note Build M = S K0 S + lam I (n x n) at ocs[osp_st[6]..], s at ocs[soff..], K0 at ocs[koff..], lam=osp_fl[0].
376fn sp_gn_wood_mat n ulong soff ulong koff ulong to void
377for i ulong n
378sp_gn_wood_row i n soff koff
379end
380end
381
382fn sp_gn_wood_rhs n ulong soff ulong roff ulong koff ulong to void
383bo = get osp_st 7
384for i ulong n
385kr = add koff mul i n
386u = sp_dot kr 1 roff 1 n
387ocsp add bo i mul ocsg add soff i u
388end
389end
390
391note Compute alpha[i] = (r[i] - s[i] * q[i]) / lam from q at ocs[osp_st[7]..] into ocs[aloff..]
392fn sp_gn_wood_alpha n ulong roff ulong soff ulong aloff ulong to void
393bo = get osp_st 7
394lam = get osp_fl 0
395for i ulong n
396rv = ocsg add roff i
397sq = mul ocsg add soff i ocsg add bo i
398ocsp add aloff i div sub rv sq lam
399end
400end
401
402note Compute column col of dW = H^T alpha into ocs[dwo..] (d x c)
403fn sp_gn_wood_ht n ulong d ulong c ulong col ulong to void
404scr = get osp_st 8
405aloff = add scr add add mul n c mul d c add mul n 3 mul n n
406ho = get osp_st 3
407dwo = add scr mul n c
408for a ulong d
409idx = add dwo add mul a c col
410ha = add ho a
411val = sp_dot ha d aloff 1 n
412ocsp idx val
413end
414end
415
416note Finish Dual Weighted Woodbury column col after Cholesky solve in bo
417fn sp_gn_wood_fin n ulong d ulong c ulong col ulong to void
418scr = get osp_st 8
419roff = add scr add add mul n c mul d c n
420soff = add roff add n mul n n
421sp_gn_wood_alpha n roff soff add soff n
422sp_gn_wood_ht n d c col
423end
424
425note Solve 1 class column col via Dual Weighted Woodbury (n < d):
426note M = S K0 S + lam I, q = M^(-1) S K0 r, alpha = (r - S q) / lam, dW[:, col] = H^T alpha.
427fn sp_gn_col_wood n ulong d ulong c ulong col ulong to ulong
428scr = get osp_st 8
429woff = add scr add mul n c mul d c
430roff = add woff n
431koff = add roff n
432soff = add koff mul n n
433for i ulong n
434ocsp add soff i fsqrt ocsg add woff i
435end
436sp_gn_wood_mat n soff koff
437sp_gn_wood_rhs n soff roff koff
438rc = sp_chol n get osp_st 6
439if ne rc 0
440ret rc
441end
442sp_chol_solve n 1 get osp_st 6 get osp_st 7
443sp_gn_wood_fin n d c col
444ret 0
445end
446
447note Fill Fisher weights w[i] = p[i, col]*(1 - p[i, col]) and residual r[i] = y_onehot[i, col] - p[i, col]
448fn sp_gn_fill_wr n ulong c ulong col ulong po ulong to void
449woff = add po add mul n c mul get osp_st 1 c
450roff = add woff n
451for i ulong n
452pv = ocsg add po add mul i c col
453ocsp add woff i mul pv sub 1.0f64 pv
454rv = sub 0.0f64 pv
455if eq get osp_tgt i col
456rv = sub 1.0f64 pv
457end
458ocsp add roff i rv
459end
460end
461
462note Apply W[a, col] += eta * dW[a, col] across d x c
463fn sp_gn_apply d ulong c ulong dwo ulong eta f64 to void
464wo = get osp_st 5
465for a ulong d
466for col ulong c
467idx = add mul a c col
468v = add ocsg add wo idx mul eta ocsg add dwo idx
469ocsp add wo idx v
470end
471end
472end
473
474note 1-Step Gauss-Newton / damped Newton refinement for softmax cross-entropy on cached features H:
475note Solves (H^T diag(p*(1-p)) H + lam I) dW = H^T (y_onehot - p) per class with auto Primal/Woodbury switch,
476note updates W <- W + eta * dW, and records loss_before in osp_fl[2] and loss_after in osp_fl[3].
477note Layout in ocs: H at osp_st[3], W at osp_st[5], A at osp_st[6], B at osp_st[7], SCR at osp_st[8].
478note SCR requires n*c + d*c + 2*n (plus n*n + 2*n when n < d).
479fn sp_gauss_newton_step n ulong d ulong c ulong lam f64 eta f64 to ulong
480if or eq n 0 or eq d 0 or eq c 0 le lam 0.0f64
481ret 1
482end
483put osp_st 0 n
484put osp_st 1 d
485put osp_st 2 c
486put osp_fl 0 lam
487scr = get osp_st 8
488put osp_fl 2 sp_eval_ce_mat n d c scr
489roff = add scr add add mul n c mul d c n
490if gt d n
491put osp_st 9 1
492sp_precomp_k0 n d get osp_st 3 add roff n
493else
494put osp_st 9 0
495end
496for col ulong c
497sp_gn_fill_wr n c col scr
498rc = ulong 0
499if le d n
500rc = sp_gn_col_primal n d c col
501else
502rc = sp_gn_col_wood n d c col
503end
504if ne rc 0
505ret rc
506end
507end
508sp_gn_apply d c add scr mul n c eta
509put osp_fl 3 sp_eval_ce_mat n d c scr
510ret 0
511end
512
513note ---- End-to-End Closed-Form Specialist Forging: OLLMFH01 -> OLLMSP01 ----
514
515note Copy dd f64s from opw[src..] into ocs[dst..]
516fn sp_load_fc_row dst ulong src ulong dd ulong to void
517for d ulong dd
518v = bitcast f64 opw8 add src mul d 8
519ocsp add dst d v
520end
521end
522
523note Load one OLLMFH01 window from opw into hraw (ocs) and osp_tgt; returns updated active count n.
524fn sp_load_fc_win n ulong hraw ulong tt ulong dd ulong to ulong
525vv = get og 0
526off = mul tt 8
527for t ulong tt
528yt = opw8 mul t 8
529if lt yt 65536
530if ge yt vv
531exit 60
532end
533if ge n OSP_MAXN
534exit 23
535end
536put osp_tgt n yt
537sp_load_fc_row add hraw mul n dd add off mul mul t dd 8 dd
538n = add n 1
539end
540end
541ret n
542end
543
544note Stream OLLMFH01 cache at argv[ai] into hraw (ocs[hraw..]), osp_tgt[0..n), counting active tokens.
545fn sp_load_fcache ai ulong hraw ulong to ulong
546tt = get og 1
547dd = get og 2
548ww = get osp_st 15
549rec = get osp_st 16
550n = ulong 0
551for w ulong ww
552pos = add 48 mul w rec
553if ne oprd ai pos 0 rec rec
554exit 60
555end
556n = sp_load_fc_win n hraw tt dd
557end
558if eq n 0
559exit 23
560end
561ret n
562end
563
564note Copy w1 columns [c0 .. ff) from layer bl-1 to layer bl
565fn sp_init_w1_clone bl ulong c0 ulong dd ulong ff ulong to void
566src1 = opw1 sub bl 1
567dst1 = opw1 bl
568for r ulong dd
569col = c0
570while lt col ff bound 999999
571idx = add mul r ff col
572owp add dst1 idx owg add src1 idx
573col = add col 1
574end
575end
576end
577
578note Initialize specialist w1 (dd x ff): [I_D, -I_D, cloned features] when ff >= 2*dd, else clone bl-1.
579fn sp_init_w1 bl ulong dd ulong ff ulong to void
580oiset opw1 bl mul dd ff 0.0f64
581if ge ff mul 2 dd
582dst1 = opw1 bl
583for d ulong dd
584oc1 = add mul d ff d
585oc2 = add mul d ff add dd d
586owp add dst1 oc1 1.0f64
587owp add dst1 oc2 -1.0f64
588end
589sp_init_w1_clone bl mul 2 dd dd ff
590ret
591end
592sp_init_w1_clone bl 0 dd ff
593end
594
595note Initialize specialist layer bl: attention zeroed (hm = h), g2 = 1.0,
596note w1 = [I_D, -I_D, cloned features], w2 = 0.0, gf = gf_base.
597fn sp_init_spec_layer bl ulong gf_snap ulong to void
598dd = get og 2
599ff = get og 4
600oiset opg1 bl dd 1.0f64
601oiset opwq bl mul dd dd 0.0f64
602oiset opwk bl mul dd dd 0.0f64
603oiset opwv bl mul dd dd 0.0f64
604oiset opwo bl mul dd dd 0.0f64
605oiset opg2 bl dd 1.0f64
606sp_init_w1 bl dd ff
607oiset opw2 bl mul ff dd 0.0f64
608gf = get og 33
609for g ulong dd
610owp add gf g ocsg add gf_snap g
611end
612end
613
614note Compute one ReLU feature f of row hr
615fn sp_build_feat_one hr ulong s0 f64 w1o ulong f ulong to f64
616dd = get og 2
617ff = get og 4
618acc = 0.0f64
619for d ulong dd
620x3 = div ocsg add hr d s0
621w3 = get ow add w1o add mul d ff f
622acc = add acc mul x3 w3
623end
624if lt acc 0.0f64
625ret 0.0f64
626end
627ret acc
628end
629
630note Compute Phi[i, 0..ff) = relu((H_raw[i] / s0) . w1) into ocs[pr..pr+ff)
631fn sp_build_feat_f hr ulong s0 f64 pr ulong to void
632ff = get og 4
633w1o = opx opw1 get osp_st 10
634for f ulong ff
635ocsp add pr f sp_build_feat_one hr s0 w1o f
636end
637end
638
639note Compute z0[i] = rms(H_raw[i], gf_base) into ocs[zr..zr+dd)
640fn sp_build_z0 hr ulong s0 f64 zr ulong dd ulong to void
641gfo = opx get og 33
642for d ulong dd
643x2 = div ocsg add hr d s0
644w2 = get ow add gfo d
645ocsp add zr d mul x2 w2
646end
647end
648
649note Compute row i of Phi[i] = relu(rms(H_raw[i], 1.0) . w1) and z0[i], s0[i].
650fn sp_build_feat_row i ulong hraw ulong phio ulong zo ulong s0o ulong to void
651dd = get og 2
652ff = get og 4
653hr = add hraw mul i dd
654ss = 0.0f64
655for d ulong dd
656v = ocsg add hr d
657ss = add ss mul v v
658end
659s0 = fsqrt add div ss f64 dd 0.00001f64
660ocsp add s0o i s0
661sp_build_z0 hr s0 add zo mul i dd dd
662sp_build_feat_f hr s0 add phio mul i ff
663end
664
665note Compute Phi[i, 0..ff) = relu(rms(H_raw[i], g2=1.0) . w1) into ocs[phio..],
666note and z0[i, 0..dd) = rms(H_raw[i], gf_base) into ocs[zo..], s0[i] into ocs[s0o..].
667fn sp_build_features n ulong hraw ulong phio ulong zo ulong s0o ulong to void
668for i ulong n
669sp_build_feat_row i hraw phio zo s0o
670end
671end
672
673note Dot product of columns a, b of wte (vv x dd)
674fn sp_wte_dot a ulong b ulong dd ulong vv ulong to f64
675acc = 0.0f64
676for v ulong vv
677row = mul v dd
678acc = add acc mul owg add row a owg add row b
679end
680ret acc
681end
682
683note Form G_wte = (1/vv) * wte^T wte + mu_z I (dd x dd) at ocs[gwte..] and factor with Cholesky
684fn sp_factor_wte gwte ulong mu f64 to ulong
685dd = get og 2
686vv = get og 0
687invv = div 1.0f64 f64 vv
688muz = mul mu 0.001f64
689if lt muz 0.000001f64
690muz = 0.000001f64
691end
692for a ulong dd
693for b ulong add a 1
694acc = mul sp_wte_dot a b dd vv invv
695if eq a b
696acc = add acc muz
697end
698ocsp add gwte add mul a dd b acc
699ocsp add gwte add mul b dd a acc
700end
701end
702ret sp_chol dd gwte
703end
704
705note Scale ocs[wteo..wteo+dd) by gf_base / si
706fn sp_forge_apply_gf wteo ulong si f64 dd ulong to void
707gfo = opx get og 33
708for d ulong dd
709vx = div ocsg add wteo d si
710wx = get ow add gfo d
711ocsp add wteo d mul vx wx
712end
713end
714
715note Compute row i hidden state hd = H_raw[i] + Phi[i] . W2, RMSNorm into wteo, return RMS scale si.
716fn sp_forge_row_norm i ulong wo ulong wteo ulong to f64
717dd = get og 2
718ff = get og 4
719hr = add get osp_st 17 mul i dd
720pr = add get osp_st 3 mul i ff
721ss = 0.0f64
722for d ulong dd
723hd = add ocsg add hr d sp_dot pr 1 add wo d dd ff
724ocsp add wteo d hd
725ss = add ss mul hd hd
726end
727si = fsqrt add div ss f64 dd 0.00001f64
728sp_forge_apply_gf wteo si dd
729ret si
730end
731
732note Compute row i logits from wteo[0..dd) into lo_off[0..vv); returns max logit.
733fn sp_forge_row_logits wteo ulong lo_off ulong vv ulong dd ulong to f64
734mx = bitcast f64 ulong 18442240474082181120
735for v ulong vv
736lv = 0.0f64
737vr = mul v dd
738for d ulong dd
739lv = add lv mul ocsg add wteo d owg add vr d
740end
741ocsp add lo_off v lv
742if gt lv mx
743mx = lv
744end
745end
746ret mx
747end
748
749note Accumulate rv * wte[v2, 0..dd) into ocs[dz_off..dz_off+dd)
750fn sp_forge_gz_acc rv f64 vr ulong dz_off ulong dd ulong to void
751for d ulong dd
752dp = add dz_off d
753dw = mul rv owg add vr d
754ocsp dp add ocsg dp dw
755end
756end
757
758note Accumulate g_z = (y_onehot - p) . wte into ocs[dz_off .. dz_off + dd) and return Fisher trace weight.
759fn sp_forge_row_gz yi ulong lo_off ulong vv ulong dd ulong dz_off ulong to f64
760p2 = 0.0f64
761for v ulong vv
762pv = ocsg add lo_off v
763p2 = add p2 mul pv pv
764end
765for d ulong dd
766ocsp add dz_off d 0.0f64
767end
768for v2 ulong vv
769rv = sub 0.0f64 ocsg add lo_off v2
770if eq v2 yi
771rv = add rv 1.0f64
772end
773sp_forge_gz_acc rv mul v2 dd dz_off dd
774end
775fw = sub 1.0f64 p2
776if lt fw 1.0e-6f64
777fw = 1.0e-6f64
778end
779ret fw
780end
781
782note Scale dz[0..dd) by si / gf_base into ocs[yr..yr+dd)
783fn sp_forge_scale_dz yr ulong dz_off ulong si f64 dd ulong to void
784gfo = opx get og 33
785for d ulong dd
786gfd = get ow add gfo d
787sc = si
788if gt fabs gfd 1.0e-8f64
789sc = div si gfd
790end
791ocsp add yr d mul sc ocsg add dz_off d
792end
793end
794
795note Compute Fisher weight and z-space target dY_h[i] from softmax probs at lo_off[0..vv).
796fn sp_forge_row_grad i ulong si f64 gwte ulong lo_off ulong to void
797dd = get og 2
798vv = get og 0
799dz_off = add lo_off vv
800fw = sp_forge_row_gz get osp_tgt i lo_off vv dd dz_off
801ocsp add get osp_st 18 i fw
802sp_chol_solve dd 1 gwte dz_off
803sp_forge_scale_dz add get osp_st 4 mul i dd dz_off si dd
804end
805
806note Evaluate one sample row i for sp_forge_eval_step; returns cross-entropy loss.
807fn sp_forge_eval_row i ulong gwte ulong wo ulong comp_tgt ulong to f64
808wteo = get osp_st 19
809dd = get og 2
810vv = get og 0
811lo_off = add wteo dd
812si = sp_forge_row_norm i wo wteo
813mx = sp_forge_row_logits wteo lo_off vv dd
814ly = ocsg add lo_off get osp_tgt i
815sm = sp_row_soft lo_off vv mx
816if ne comp_tgt 0
817sp_forge_row_grad i si gwte lo_off
818end
819ret sub add mx flog sm ly
820end
821
822note Evaluate CE loss on current w2 (at ocs[wo..]) over n cached samples.
823note If comp_tgt != 0, writes z-space target residual dz into ocs[yo .. +n*dd) and
824note scalar Fisher trace weight w[i] = 1 - sum_v p[i,v]^2 into ocs[s0o + i].
825fn sp_forge_eval_step n ulong gwte ulong wo ulong comp_tgt ulong to f64
826ls = 0.0f64
827for i ulong n
828ls = add ls sp_forge_eval_row i gwte wo comp_tgt
829end
830ret div ls f64 n
831end
832
833note Compute H^T alpha into column col of ocs[osp_st[5]..] (ff x dd) from alpha at ocs[osp_st[7]..]
834fn sp_forge_wridge_ht n ulong ff ulong dd ulong col ulong to void
835ho = get osp_st 3
836wo = get osp_st 5
837bo = get osp_st 7
838for a ulong ff
839ha = add ho a
840idx = add wo add mul a dd col
841val = sp_dot ha ff bo 1 n
842ocsp idx val
843end
844end
845
846note Solve one column of weighted Woodbury in sp_forge_wridge
847fn sp_forge_wridge_col col ulong n ulong ff ulong dd ulong to void
848yo = get osp_st 4
849koff = get osp_st 20
850soff = add koff mul n n
851aloff = add soff n
852for i ulong n
853ocsp add aloff i ocsg add yo add mul i dd col
854end
855sp_gn_wood_rhs n soff aloff koff
856sp_chol_solve n 1 get osp_st 6 get osp_st 7
857sp_forge_wridge_alpha n aloff soff
858sp_forge_wridge_ht n ff dd col
859end
860
861fn sp_forge_wridge_alpha n ulong aloff ulong soff ulong to void
862bo = get osp_st 7
863lam = get osp_fl 0
864for i ulong n
865rv = ocsg add aloff i
866sq = mul ocsg add soff i ocsg add bo i
867ocsp add bo i div sub rv sq lam
868end
869end
870
871note Solve weighted Ridge (Phi^T diag(w) Phi + lam I) dW = Phi^T dY with auto Primal/Woodbury switch.
872note Phi at osp_st[3] (n x ff), dY at osp_st[4] (n x dd), w at osp_st[18] (n), result dW at osp_st[5].
873fn sp_forge_wridge n ulong ff ulong dd ulong lam f64 to ulong
874put osp_fl 0 lam
875if le ff n
876put osp_st 9 0
877sp_wgram_primal n ff get osp_st 18
878sp_ht_y n ff dd get osp_st 3 get osp_st 4
879rc = sp_chol ff get osp_st 6
880if ne rc 0
881ret rc
882end
883sp_chol_solve ff dd get osp_st 6 get osp_st 5
884ret 0
885end
886put osp_st 9 1
887koff = get osp_st 20
888soff = add koff mul n n
889sp_precomp_k0 n ff get osp_st 3 koff
890for i ulong n
891ocsp add soff i fsqrt ocsg add get osp_st 18 i
892end
893sp_gn_wood_mat n soff koff
894rc2 = sp_chol n get osp_st 6
895if ne rc2 0
896ret rc2
897end
898for col ulong dd
899sp_forge_wridge_col col n ff dd
900end
901ret 0
902end
903
904note Try trial weights tmo = wcur + eta * dw
905fn sp_forge_fill_trial wcur ulong dw ulong tmo ulong ndw ulong eta f64 to void
906for j ulong ndw
907v = add ocsg add wcur j mul eta ocsg add dw j
908ocsp add tmo j v
909end
910end
911
912note Run Armijo backtracking over eta in {1, 0.5, ..., 1/32} on w2_cur + eta*dw -> updates w2_cur in place.
913fn sp_forge_damped_accept n ulong gwte ulong wcur ulong dw ulong lcur f64 to f64
914ndw = mul get og 4 get og 2
915tmo = get osp_st 21
916eta = 1.0f64
917best_l = lcur
918best_eta = 0.0f64
919for k ulong 6
920sp_forge_fill_trial wcur dw tmo ndw eta
921ltry = sp_forge_eval_step n gwte tmo 0
922if lt ltry best_l
923best_l = ltry
924best_eta = eta
925break
926end
927eta = mul eta 0.5f64
928end
929if gt best_eta 0.0f64
930sp_forge_fill_trial wcur dw wcur ndw best_eta
931end
932ret best_l
933end
934
935note Copy solved w2 from ocs[wo..] into virtual parameters ow[opw2(bl)..]
936fn sp_commit_w2 bl ulong wo ulong to void
937dd = get og 2
938ff = get og 4
939base = opw2 bl
940n = mul ff dd
941for i ulong n
942owp add base i ocsg add wo i
943end
944end
945
946note Snapshot ow[0..n) into ocs[dst .. dst + n) without touching ocs[0..dst)
947fn sp_snap_base dst ulong n ulong to void
948for q ulong ocn n
949i = mul q OCH
950ie = oce q n
951while lt i ie bound 999999
952ocsp add dst i owg i
953i = add i 1
954end
955end
956end
957
958note Restore ow[0..n) from ocs[src .. src + n)
959fn sp_rest_base src ulong n ulong to void
960for q ulong ocn n
961i = mul q OCH
962ie = oce q n
963while lt i ie bound 999999
964owp i ocsg add src i
965i = add i 1
966end
967end
968end
969
970note Compute ocs scratch layout for spforge_consolidate_traces; stores offsets in osp_st.
971note Returns gwte offset.
972fn sp_forge_layout n ulong ff ulong dd ulong to ulong
973put osp_st 0 n
974put osp_st 1 ff
975put osp_st 2 dd
976put osp_st 17 0
977off = mul n dd
978put osp_st 3 off
979off = add off mul n ff
980put osp_st 4 off
981off = add off mul n dd
982put osp_st 5 off
983zo = add off mul ff dd
984s0o = add zo mul n dd
985put osp_st 18 s0o
986gwte = add s0o n
987off = add gwte mul dd dd
988put osp_st 6 off
989msz = ff
990if gt n ff
991msz = n
992end
993off = add off mul msz msz
994put osp_st 7 off
995off = add off mul msz dd
996put osp_st 19 off
997off = add off add dd add get og 0 dd
998put osp_st 22 off
999off = add off mul ff dd
1000put osp_st 21 off
1001put osp_st 20 add off mul ff dd
1002sp_build_features n 0 get osp_st 3 zo s0o
1003ret gwte
1004end
1005
1006note Run Ridge + gns-step Gauss-Newton solve inside spforge_consolidate_traces
1007fn sp_forge_solve_loop n ulong gwte ulong lam f64 gns ulong to void
1008ff = get og 4
1009dd = get og 2
1010wcur = get osp_st 22
1011sp_zero_ocs wcur mul ff dd
1012l0 = sp_forge_eval_step n gwte wcur 1
1013put osp_fl 2 l0
1014if ne sp_ridge_solve n ff dd lam 0
1015exit 42
1016end
1017l1 = sp_forge_damped_accept n gwte wcur get osp_st 5 l0
1018for gs ulong gns
1019l1 = sp_forge_eval_step n gwte wcur 1
1020if eq sp_forge_wridge n ff dd lam 0
1021l1 = sp_forge_damped_accept n gwte wcur get osp_st 5 l1
1022end
1023end
1024put osp_fl 3 l1
1025end
1026
1027note Sleep-consolidation entrypoint (for Arboris sleep export / fcachem replay):
1028note Assumes BASE.ck is loaded in (og, ow), n cached hidden states H_raw (n x dd) are in
1029note ocs[0 .. n*dd) and target token IDs are in osp_tgt[0 .. n).
1030note Solves Ridge (Primal/Woodbury) + gns-step Gauss-Newton refinement and writes OLLMSP01
1031note pack to argv[outai]. Returns 0 on success (loss_before in osp_fl[2], loss_after in osp_fl[3]).
1032fn spforge_consolidate_traces outai ulong n ulong lam f64 gns ulong to ulong
1033if or eq n 0 gt n OSP_MAXN
1034ret 23
1035end
1036if le lam 0.0f64
1037ret 2
1038end
1039bl = get og 5
1040if ge bl 16
1041ret 12
1042end
1043fz = get og 33
1044bh = osp_basehash bl fz
1045put osp_st 10 bl
1046put osp_st 11 add bl 1
1047put osp_st 12 fz
1048put osp_st 14 bh
1049snap = mul n get og 2
1050sp_snap_base snap add fz get og 2
1051put og 5 add bl 1
1052if not ogeom
1053ret 12
1054end
1055sp_rest_base snap fz
1056put osp_st 13 sub get og 8 fz
1057sp_init_spec_layer bl add snap fz
1058gwte = sp_forge_layout n get og 4 get og 2
1059if ne sp_factor_wte gwte lam 0
1060ret 42
1061end
1062sp_forge_solve_loop n gwte lam gns
1063sp_commit_w2 bl get osp_st 22
1064osp_wrpack outai bl add bl 1 bh fz get osp_st 13
1065ret 0
1066end
1067
1068note spforge BASE.ck CACHE.hc OUT.sp [LAMBDA_MICRO [GN_STEPS]]
1069note Forges a closed-form 1-layer OLLMSP01 specialist pack from cached OLLMFH01 features
1070note using Ridge regression (Primal Cholesky / Dual Woodbury) + 1-step Gauss-Newton refinement.
1071fn cmdspforge to void
1072na = oargc
1073if or lt na 5 gt na 7
1074exit 2
1075end
1076c_ocs = ocsinit
1077lam = 0.1f64
1078if ge na 6
1079lm = oargu 5
1080if eq lm 0
1081exit 2
1082end
1083lam = div f64 lm 1000000.0f64
1084end
1085gns = ulong 1
1086if ge na 7
1087gns = oargu 6
1088if gt gns 16
1089exit 2
1090end
1091end
1092ockloop 2
1093bl = get og 5
1094if ge bl 16
1095exit 12
1096end
1097fz = get og 33
1098bh = osp_basehash bl fz
1099ofh_rdhdr 3 bl bh
1100put osp_st 15 get ottt_st 2
1101put osp_st 16 get ottt_st 3
1102n = sp_load_fcache 3 0
1103rc = spforge_consolidate_traces 4 n lam gns
1104if ne rc 0
1105exit int rc
1106end
1107oputc 83
1108oputc 80
1109oputc 70
1110oputc 79
1111oputc 82
1112oputc 71
1113oputc 69
1114oputc 32
1115oputu bl
1116oputc 32
1117oputu add bl 1
1118oputc 32
1119oputu n
1120oputc 32
1121oputu get osp_st 9
1122oputc 32
1123oputl rmic get osp_fl 2
1124oputc 32
1125oputl rmic get osp_fl 3
1126oputc 32
1127oputu get osp_st 13
1128oputc 10
1129oflush
1130end