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