omega-helix
This project has not described itself yet.
Brought by drakestapleton
omega-helix / native / spiral.om
| 1 | note SPDX-License-Identifier: AGPL-3.0-only |
| 2 | note omegallm spiral.om: Spiral-2048 Pure-Omega Razor MoE engine. |
| 3 | note Implements: |
| 4 | note 1. OLLMSP02 Merkle-signed razor-strand format (exact f64 and W4 4-bit block-32, rc=61 on 1-bit tamper) |
| 5 | note 2. `ollm spcryst`: Zero-backprop direct analytical crystallization via Arboris/Rho-1 surprisal-weighted |
| 6 | note Ridge regression solved in one shot with Cholesky (sp_chol / sp_chol_solve from sp_solve.om) |
| 7 | note 3. `ollm spweval` & `ollm spsamp`: Sparse-FFN Helix Router (sproute) with Top-K centroid cosine routing |
| 8 | note across up to 2048 Merkle-verified OLLMSP02 strands. |
| 9 | note Uses only static small globals + ocs cold store so it compiles cleanly on macos, aarch64, gb10, and x86linux. |
| 10 | |
| 11 | const OSP2_MAG ulong 3616478819487861839 |
| 12 | const OSP2_HDR ulong 80 |
| 13 | const OSP2_MAXS ulong 2048 |
| 14 | const OSP2_MAXF ulong 640 |
| 15 | const OSP2_MAXD ulong 2048 |
| 16 | const OSP2_CALN ulong 128 |
| 17 | |
| 18 | global sp2_sid ulong 2048 |
| 19 | global sp2_fexp ulong 2048 |
| 20 | global sp2_coff ulong 2048 |
| 21 | global sp2_woff ulong 2048 |
| 22 | global sp2_sim f64 2048 |
| 23 | global sp2_topi ulong 32 |
| 24 | global sp2_topw f64 32 |
| 25 | global sp2_phi f64 640 |
| 26 | global sp2_dz f64 2048 |
| 27 | global sp2_rvec f64 2048 |
| 28 | global sp2_hdr ulong 10 |
| 29 | global sp2_caly ulong 128 |
| 30 | global sp2_st ulong 16 |
| 31 | |
| 32 | note Deterministic sparse signed hash-orthogonal feature projection phi = ReLU(W_up * z), |
| 33 | note with bias feature phi[f_exp - 1] = 1.0. Reads z from oa[z_off .. z_off + d). |
| 34 | fn sp2_feat_oa z_off ulong d ulong f_exp ulong sid ulong to void |
| 35 | fm1 = sub f_exp 1 |
| 36 | for f ulong fm1 |
| 37 | seed = add mul sid 1000003 mul add f 1 65537 |
| 38 | acc = 0.0f64 |
| 39 | for k ulong 4 |
| 40 | h = smx add seed mul k 2654435761 |
| 41 | idx = rem shr h 1 d |
| 42 | v = get oa add z_off idx |
| 43 | if eq and h 1 0 |
| 44 | acc = add acc v |
| 45 | else |
| 46 | acc = sub acc v |
| 47 | end |
| 48 | end |
| 49 | u = mul acc 0.5f64 |
| 50 | if gt u 0.0f64 |
| 51 | put sp2_phi f u |
| 52 | else |
| 53 | put sp2_phi f 0.0f64 |
| 54 | end |
| 55 | end |
| 56 | put sp2_phi fm1 1.0f64 |
| 57 | end |
| 58 | |
| 59 | note Same projection reading z from ocs[z_off .. z_off + d). |
| 60 | fn sp2_feat_ocs z_off ulong d ulong f_exp ulong sid ulong to void |
| 61 | fm1 = sub f_exp 1 |
| 62 | for f ulong fm1 |
| 63 | seed = add mul sid 1000003 mul add f 1 65537 |
| 64 | acc = 0.0f64 |
| 65 | for k ulong 4 |
| 66 | h = smx add seed mul k 2654435761 |
| 67 | idx = rem shr h 1 d |
| 68 | v = ocsg add z_off idx |
| 69 | if eq and h 1 0 |
| 70 | acc = add acc v |
| 71 | else |
| 72 | acc = sub acc v |
| 73 | end |
| 74 | end |
| 75 | u = mul acc 0.5f64 |
| 76 | if gt u 0.0f64 |
| 77 | put sp2_phi f u |
| 78 | else |
| 79 | put sp2_phi f 0.0f64 |
| 80 | end |
| 81 | end |
| 82 | put sp2_phi fm1 1.0f64 |
| 83 | end |
| 84 | |
| 85 | note Compute logits for z at oa[z_off .. +d) into oa[og[27] .. +V), return CE loss for target y. |
| 86 | fn sp2_ce_oa z_off ulong y ulong to f64 |
| 87 | v = get og 0 |
| 88 | d = get og 2 |
| 89 | lo = get og 27 |
| 90 | klogit z_off lo v d |
| 91 | mx = get oa lo |
| 92 | for i ulong v |
| 93 | x = get oa add lo i |
| 94 | if gt x mx |
| 95 | mx = x |
| 96 | end |
| 97 | end |
| 98 | sm = 0.0f64 |
| 99 | for i2 ulong v |
| 100 | sm = add sm fexp sub get oa add lo i2 mx |
| 101 | end |
| 102 | ret sub add mx flog sm get oa add lo y |
| 103 | end |
| 104 | |
| 105 | fn sp2_soft_lo z_off ulong v ulong d ulong to f64 |
| 106 | lo = get og 27 |
| 107 | klogit z_off lo v d |
| 108 | mx = get oa lo |
| 109 | for i ulong v |
| 110 | x = get oa add lo i |
| 111 | if gt x mx |
| 112 | mx = x |
| 113 | end |
| 114 | end |
| 115 | sm = 0.0f64 |
| 116 | for i2 ulong v |
| 117 | e = fexp sub get oa add lo i2 mx |
| 118 | put oa add lo i2 e |
| 119 | sm = add sm e |
| 120 | end |
| 121 | ret sm |
| 122 | end |
| 123 | |
| 124 | fn sp2_sub_wte vw ulong d ulong pv f64 to void |
| 125 | for j ulong d |
| 126 | put sp2_rvec j sub get sp2_rvec j mul pv owg add vw j |
| 127 | end |
| 128 | end |
| 129 | |
| 130 | note Compute exact hidden-space steepest-descent residual sp2_rvec = WTE[y] - E_p[WTE] |
| 131 | note and return token surprisal (-log p[y]) for z at oa[z_off .. +d). |
| 132 | fn sp2_tok_grad z_off ulong y ulong to f64 |
| 133 | v = get og 0 |
| 134 | d = get og 2 |
| 135 | lo = get og 27 |
| 136 | inv_sm = div 1.0f64 sp2_soft_lo z_off v d |
| 137 | surp = sub 0.0f64 flog fmax mul get oa add lo y inv_sm 1.0e-30f64 |
| 138 | yw = mul y d |
| 139 | for j ulong d |
| 140 | put sp2_rvec j owg add yw j |
| 141 | end |
| 142 | for vv ulong v |
| 143 | pv = mul get oa add lo vv inv_sm |
| 144 | if gt pv 0.000001f64 |
| 145 | sp2_sub_wte mul vv d d pv |
| 146 | end |
| 147 | end |
| 148 | ret surp |
| 149 | end |
| 150 | |
| 151 | fn sp2_acc_row a ulong wa f64 f_exp ulong d ulong to void |
| 152 | ga = add get sp2_st 3 mul a f_exp |
| 153 | b = a |
| 154 | while lt b f_exp bound 1024 |
| 155 | pb = get sp2_phi b |
| 156 | if gt pb 0.0f64 |
| 157 | ocsp add ga b add ocsg add ga b mul wa pb |
| 158 | end |
| 159 | b = add b 1 |
| 160 | end |
| 161 | ba = add get sp2_st 4 mul a d |
| 162 | for j2 ulong d |
| 163 | ocsp add ba j2 add ocsg add ba j2 mul wa get sp2_rvec j2 |
| 164 | end |
| 165 | end |
| 166 | |
| 167 | note Accumulate one token into upper-triangle Gram G (ocs[g_off..]), RHS B (ocs[b_off..]), |
| 168 | note and weighted centroid mu (ocs[c_off..]). |
| 169 | fn sp2_acc_tok z_off ulong w_tok f64 f_exp ulong d ulong to void |
| 170 | c_off = get sp2_st 5 |
| 171 | for j ulong d |
| 172 | ocsp add c_off j add ocsg add c_off j mul w_tok get oa add z_off j |
| 173 | end |
| 174 | for a ulong f_exp |
| 175 | pa = get sp2_phi a |
| 176 | if gt pa 0.0f64 |
| 177 | sp2_acc_row a mul w_tok pa f_exp d |
| 178 | end |
| 179 | end |
| 180 | end |
| 181 | |
| 182 | note Symmetrize upper-triangle G and add lam * I on diagonal. |
| 183 | fn sp2_sym_reg g_off ulong f_exp ulong lam f64 to void |
| 184 | for a ulong f_exp |
| 185 | ga = add g_off mul a f_exp |
| 186 | ocsp add ga a add ocsg add ga a lam |
| 187 | b = add a 1 |
| 188 | while lt b f_exp bound 1024 |
| 189 | v = ocsg add ga b |
| 190 | ocsp add g_off add mul b f_exp a v |
| 191 | b = add b 1 |
| 192 | end |
| 193 | end |
| 194 | end |
| 195 | |
| 196 | fn sp2_add_oa ztmp ulong ba ulong d ulong sc f64 to void |
| 197 | for j ulong d |
| 198 | idx = add ztmp j |
| 199 | put oa idx add get oa idx mul sc ocsg add ba j |
| 200 | end |
| 201 | end |
| 202 | |
| 203 | fn sp2_cal_apply zo ulong b_off ulong f_exp ulong eta f64 to void |
| 204 | d = get og 2 |
| 205 | ztmp = get og 17 |
| 206 | sp2_feat_ocs zo d f_exp get sp2_st 6 |
| 207 | for a ulong f_exp |
| 208 | pa = get sp2_phi a |
| 209 | if gt pa 0.0f64 |
| 210 | sp2_add_oa ztmp add b_off mul a d d mul eta pa |
| 211 | end |
| 212 | end |
| 213 | end |
| 214 | |
| 215 | fn sp2_copy_ocs_oa zo ulong ztmp ulong d ulong to void |
| 216 | for j ulong d |
| 217 | put oa add ztmp j ocsg add zo j |
| 218 | end |
| 219 | end |
| 220 | |
| 221 | note Evaluate mean CE loss on calibration mini-batch (ncal tokens in ocs[cal_z..]) with step scale eta. |
| 222 | fn sp2_cal_loss ncal ulong cal_z ulong f_exp ulong eta f64 to f64 |
| 223 | d = get og 2 |
| 224 | ztmp = get og 17 |
| 225 | ls = 0.0f64 |
| 226 | for i ulong ncal |
| 227 | zo = add cal_z mul i d |
| 228 | sp2_copy_ocs_oa zo ztmp d |
| 229 | if gt eta 0.0f64 |
| 230 | sp2_cal_apply zo get sp2_st 4 f_exp eta |
| 231 | end |
| 232 | ls = add ls sp2_ce_oa ztmp get sp2_caly i |
| 233 | end |
| 234 | ret div ls f64 ncal |
| 235 | end |
| 236 | |
| 237 | note Compute little-endian u32 into ock[o..o+4) |
| 238 | fn sp2_put4 o ulong v uint to void |
| 239 | put ock o uchar and v 255 |
| 240 | put ock add o 1 uchar and shr v 8 255 |
| 241 | put ock add o 2 uchar and shr v 16 255 |
| 242 | put ock add o 3 uchar and shr v 24 255 |
| 243 | end |
| 244 | |
| 245 | note Read little-endian u32 from opw[o..o+4) |
| 246 | fn sp2_get4 o ulong to uint |
| 247 | b0 = uint get opw o |
| 248 | b1 = shl uint get opw add o 1 8 |
| 249 | b2 = shl uint get opw add o 2 16 |
| 250 | b3 = shl uint get opw add o 3 24 |
| 251 | ret or or b0 b1 or b2 b3 |
| 252 | end |
| 253 | |
| 254 | fn sp2_q4_nib v f64 sc f64 to ulong |
| 255 | if le sc 0.0f64 |
| 256 | ret 8 |
| 257 | end |
| 258 | r = div v sc |
| 259 | if gt r 7.0f64 |
| 260 | r = 7.0f64 |
| 261 | end |
| 262 | if lt r -7.0f64 |
| 263 | r = -7.0f64 |
| 264 | end |
| 265 | if ge r 0.0f64 |
| 266 | ret ulong add 8 long add r 0.5f64 |
| 267 | end |
| 268 | ret ulong add 8 long sub r 0.5f64 |
| 269 | end |
| 270 | |
| 271 | fn sp2_q4_pair off ulong cnt ulong p ulong sc f64 to uchar |
| 272 | q0 = ulong 8 |
| 273 | q1 = ulong 8 |
| 274 | i0 = mul p 2 |
| 275 | if lt i0 cnt |
| 276 | q0 = sp2_q4_nib ocsg add off i0 sc |
| 277 | end |
| 278 | i1 = add i0 1 |
| 279 | if lt i1 cnt |
| 280 | q1 = sp2_q4_nib ocsg add off i1 sc |
| 281 | end |
| 282 | ret uchar or and q0 15 shl and q1 15 4 |
| 283 | end |
| 284 | |
| 285 | note Quantize one 32-element f64 block from ocs[off..off+cnt) into 20 bytes at ock[bo..bo+20): |
| 286 | note 4B f32 scale (max_abs / 7.0) + 16B packed signed 4-bit nibbles (q in [-7..+7] stored as q + 8). |
| 287 | fn sp2_q4_blk off ulong cnt ulong bo ulong to void |
| 288 | mx = 0.0f64 |
| 289 | for i ulong cnt |
| 290 | v = fabs ocsg add off i |
| 291 | if gt v mx |
| 292 | mx = v |
| 293 | end |
| 294 | end |
| 295 | sc32 = f32 div mx 7.0f64 |
| 296 | sp2_put4 bo bitcast uint sc32 |
| 297 | sc = f64 sc32 |
| 298 | for p ulong 16 |
| 299 | put ock add bo add 4 p sp2_q4_pair off cnt p sc |
| 300 | end |
| 301 | end |
| 302 | |
| 303 | note Dequantize one 20-byte W4 block from opw[bo..bo+20) into ocs[off..off+cnt). |
| 304 | fn sp2_dq4_blk bo ulong off ulong cnt ulong to void |
| 305 | sc = f64 bitcast f32 sp2_get4 bo |
| 306 | for p ulong 16 |
| 307 | bt = ulong get opw add bo add 4 p |
| 308 | i0 = mul p 2 |
| 309 | if lt i0 cnt |
| 310 | ocsp add off i0 mul f64 sub long and bt 15 8 sc |
| 311 | end |
| 312 | i1 = add i0 1 |
| 313 | if lt i1 cnt |
| 314 | ocsp add off i1 mul f64 sub long and shr bt 4 15 8 sc |
| 315 | end |
| 316 | end |
| 317 | end |
| 318 | |
| 319 | fn sp2_wr_f64_chunk base ulong cnt ulong to void |
| 320 | for k ulong cnt |
| 321 | ocput8 mul k 8 bitcast ulong ocsg add base k |
| 322 | end |
| 323 | end |
| 324 | |
| 325 | fn sp2_wr_f64 b_off ulong nw ulong pos ulong ph ulong to ulong |
| 326 | done = ulong 0 |
| 327 | p = pos |
| 328 | h = ph |
| 329 | while lt done nw bound 65536 |
| 330 | cnt = sub nw done |
| 331 | if gt cnt OCKC |
| 332 | cnt = OCKC |
| 333 | end |
| 334 | sp2_wr_f64_chunk add b_off done cnt |
| 335 | nb = mul cnt 8 |
| 336 | h = ocfnvk h nb |
| 337 | ocwput p nb |
| 338 | p = add p nb |
| 339 | done = add done cnt |
| 340 | end |
| 341 | put sp2_st 7 p |
| 342 | ret h |
| 343 | end |
| 344 | |
| 345 | fn sp2_wr_w4_chunk b_off ulong nw ulong b_done ulong bcnt ulong to void |
| 346 | for bi ulong bcnt |
| 347 | elem0 = mul add b_done bi 32 |
| 348 | ecnt = sub nw elem0 |
| 349 | if gt ecnt 32 |
| 350 | ecnt = 32 |
| 351 | end |
| 352 | sp2_q4_blk add b_off elem0 ecnt mul bi 20 |
| 353 | end |
| 354 | end |
| 355 | |
| 356 | fn sp2_wr_w4 b_off ulong nw ulong pos ulong ph ulong to ulong |
| 357 | nblk = div add nw 31 32 |
| 358 | b_done = ulong 0 |
| 359 | p = pos |
| 360 | h = ph |
| 361 | while lt b_done nblk bound 65536 |
| 362 | bcnt = sub nblk b_done |
| 363 | if gt bcnt 16384 |
| 364 | bcnt = 16384 |
| 365 | end |
| 366 | sp2_wr_w4_chunk b_off nw b_done bcnt |
| 367 | nb2 = mul bcnt 20 |
| 368 | h = ocfnvk h nb2 |
| 369 | ocwput p nb2 |
| 370 | p = add p nb2 |
| 371 | b_done = add b_done bcnt |
| 372 | end |
| 373 | put sp2_st 7 p |
| 374 | ret h |
| 375 | end |
| 376 | |
| 377 | note Write OLLMSP02 pack to argv[out_ai]. Centroid at ocs[c_off..+d), W_down at ocs[b_off..+nw). |
| 378 | note Returns payload_bytes written. |
| 379 | fn sp2_write_pack out_ai ulong base_h ulong f_exp ulong sid ulong qbits ulong to ulong |
| 380 | d = get og 2 |
| 381 | c_off = get sp2_st 5 |
| 382 | b_off = get sp2_st 4 |
| 383 | nw = mul f_exp d |
| 384 | ocwopen out_ai |
| 385 | ph = ulong 14695981039346656037 |
| 386 | pos = OSP2_HDR |
| 387 | for j ulong d |
| 388 | ocput8 mul j 8 bitcast ulong ocsg add c_off j |
| 389 | end |
| 390 | cb = mul d 8 |
| 391 | ph = ocfnvk ph cb |
| 392 | ocwput pos cb |
| 393 | pos = add pos cb |
| 394 | if eq qbits 64 |
| 395 | ph = sp2_wr_f64 b_off nw pos ph |
| 396 | else |
| 397 | ph = sp2_wr_w4 b_off nw pos ph |
| 398 | end |
| 399 | pbytes = sub get sp2_st 7 OSP2_HDR |
| 400 | ocput8 0 OSP2_MAG |
| 401 | ocput8 8 base_h |
| 402 | ocput8 16 get og 5 |
| 403 | ocput8 24 d |
| 404 | ocput8 32 f_exp |
| 405 | ocput8 40 sid |
| 406 | ocput8 48 qbits |
| 407 | ocput8 56 pbytes |
| 408 | ocput8 64 ph |
| 409 | hh = ocfnvk ulong 14695981039346656037 72 |
| 410 | ocput8 72 hh |
| 411 | ocwput 0 OSP2_HDR |
| 412 | ocwend out_ai |
| 413 | ret pbytes |
| 414 | end |
| 415 | |
| 416 | fn sp2_rd_f64_chunk base ulong cnt ulong to void |
| 417 | for j2 ulong cnt |
| 418 | ocsp add base j2 bitcast f64 opw8 mul j2 8 |
| 419 | end |
| 420 | end |
| 421 | |
| 422 | fn sp2_rd_f64 ai ulong woff ulong nw ulong pos ulong ph ulong to ulong |
| 423 | done = ulong 0 |
| 424 | p = pos |
| 425 | h = ph |
| 426 | while lt done nw bound 65536 |
| 427 | cnt = sub nw done |
| 428 | if gt cnt OCKC |
| 429 | cnt = OCKC |
| 430 | end |
| 431 | nb = mul cnt 8 |
| 432 | if ne oprd ai p 0 nb nb |
| 433 | exit 60 |
| 434 | end |
| 435 | h = ocfnvw h nb |
| 436 | sp2_rd_f64_chunk add woff done cnt |
| 437 | p = add p nb |
| 438 | done = add done cnt |
| 439 | end |
| 440 | put sp2_st 7 p |
| 441 | ret h |
| 442 | end |
| 443 | |
| 444 | fn sp2_rd_w4_chunk woff ulong nw ulong b_done ulong bcnt ulong to void |
| 445 | for bi ulong bcnt |
| 446 | elem0 = mul add b_done bi 32 |
| 447 | ecnt = sub nw elem0 |
| 448 | if gt ecnt 32 |
| 449 | ecnt = 32 |
| 450 | end |
| 451 | sp2_dq4_blk mul bi 20 add woff elem0 ecnt |
| 452 | end |
| 453 | end |
| 454 | |
| 455 | fn sp2_rd_w4 ai ulong woff ulong nw ulong pos ulong ph ulong to ulong |
| 456 | nblk = div add nw 31 32 |
| 457 | b_done = ulong 0 |
| 458 | p = pos |
| 459 | h = ph |
| 460 | while lt b_done nblk bound 65536 |
| 461 | bcnt = sub nblk b_done |
| 462 | if gt bcnt 16384 |
| 463 | bcnt = 16384 |
| 464 | end |
| 465 | nb2 = mul bcnt 20 |
| 466 | if ne oprd ai p 0 nb2 nb2 |
| 467 | exit 60 |
| 468 | end |
| 469 | h = ocfnvw h nb2 |
| 470 | sp2_rd_w4_chunk woff nw b_done bcnt |
| 471 | p = add p nb2 |
| 472 | b_done = add b_done bcnt |
| 473 | end |
| 474 | put sp2_st 7 p |
| 475 | ret h |
| 476 | end |
| 477 | |
| 478 | fn sp2_chk_hdr ai ulong base_h ulong to void |
| 479 | if ne oprd ai 0 0 OSP2_HDR OSP2_HDR |
| 480 | exit 60 |
| 481 | end |
| 482 | if ne opw8 0 OSP2_MAG |
| 483 | exit 60 |
| 484 | end |
| 485 | if ne ocfnvw ulong 14695981039346656037 72 opw8 72 |
| 486 | exit 61 |
| 487 | end |
| 488 | for k ulong 10 |
| 489 | put sp2_hdr k opw8 mul k 8 |
| 490 | end |
| 491 | if ne get sp2_hdr 1 base_h |
| 492 | exit 62 |
| 493 | end |
| 494 | if or ne get sp2_hdr 2 get og 5 ne get sp2_hdr 3 get og 2 |
| 495 | exit 62 |
| 496 | end |
| 497 | f_exp = get sp2_hdr 4 |
| 498 | qbits = get sp2_hdr 6 |
| 499 | if or eq f_exp 0 gt f_exp OSP2_MAXF |
| 500 | exit 60 |
| 501 | end |
| 502 | if and ne qbits 64 ne qbits 4 |
| 503 | exit 60 |
| 504 | end |
| 505 | nw = mul f_exp get og 2 |
| 506 | exp_pb = mul get og 2 8 |
| 507 | if eq qbits 64 |
| 508 | exp_pb = add exp_pb mul nw 8 |
| 509 | else |
| 510 | exp_pb = add exp_pb mul div add nw 31 32 20 |
| 511 | end |
| 512 | if ne get sp2_hdr 7 exp_pb |
| 513 | exit 60 |
| 514 | end |
| 515 | end |
| 516 | |
| 517 | note Load and Merkle-verify one OLLMSP02 pack from argv[ai] into strand slot s. |
| 518 | note Exits: 60 bad magic/format/truncation, 61 1-bit tamper (header or payload hash), 62 base/shape mismatch. |
| 519 | fn sp2_load_pack ai ulong s ulong base_h ulong to void |
| 520 | sp2_chk_hdr ai base_h |
| 521 | d = get og 2 |
| 522 | nw = mul get sp2_hdr 4 d |
| 523 | cur = get sp2_st 1 |
| 524 | if not ocsgrow add cur add d nw |
| 525 | exit 70 |
| 526 | end |
| 527 | put sp2_sid s get sp2_hdr 5 |
| 528 | put sp2_fexp s get sp2_hdr 4 |
| 529 | put sp2_coff s cur |
| 530 | put sp2_woff s add cur d |
| 531 | put sp2_st 1 add cur add d nw |
| 532 | cb = mul d 8 |
| 533 | if ne oprd ai OSP2_HDR 0 cb cb |
| 534 | exit 60 |
| 535 | end |
| 536 | ph = ocfnvw ulong 14695981039346656037 cb |
| 537 | sp2_rd_f64_chunk cur d |
| 538 | if eq get sp2_hdr 6 64 |
| 539 | ph = sp2_rd_f64 ai add cur d nw add OSP2_HDR cb ph |
| 540 | else |
| 541 | ph = sp2_rd_w4 ai add cur d nw add OSP2_HDR cb ph |
| 542 | end |
| 543 | if ne oprd ai get sp2_st 7 0 1 0 |
| 544 | exit 60 |
| 545 | end |
| 546 | if ne ph get sp2_hdr 8 |
| 547 | exit 61 |
| 548 | end |
| 549 | end |
| 550 | |
| 551 | fn sp2_pick_topk ns ulong kk ulong to void |
| 552 | for k ulong kk |
| 553 | best_s = ulong 0 |
| 554 | best_v = -1.0e30f64 |
| 555 | for s2 ulong ns |
| 556 | used = false |
| 557 | for p ulong k |
| 558 | if eq get sp2_topi p s2 |
| 559 | used = true |
| 560 | end |
| 561 | end |
| 562 | if not used |
| 563 | sv = get sp2_sim s2 |
| 564 | if gt sv best_v |
| 565 | best_v = sv |
| 566 | best_s = s2 |
| 567 | end |
| 568 | end |
| 569 | end |
| 570 | put sp2_topi k best_s |
| 571 | put sp2_topw k best_v |
| 572 | end |
| 573 | mx = get sp2_topw 0 |
| 574 | sm = 0.0f64 |
| 575 | for k2 ulong kk |
| 576 | w = fexp mul 4.0f64 sub get sp2_topw k2 mx |
| 577 | put sp2_topw k2 w |
| 578 | sm = add sm w |
| 579 | end |
| 580 | for k3 ulong kk |
| 581 | put sp2_topw k3 div get sp2_topw k3 sm |
| 582 | end |
| 583 | put sp2_st 2 get sp2_sid get sp2_topi 0 |
| 584 | end |
| 585 | |
| 586 | fn sp2_add_dz row ulong d ulong sc f64 to void |
| 587 | for j ulong d |
| 588 | put sp2_dz j add get sp2_dz j mul sc ocsg add row j |
| 589 | end |
| 590 | end |
| 591 | |
| 592 | fn sp2_apply_1k z_off ulong si ulong gw f64 d ulong to void |
| 593 | fe = get sp2_fexp si |
| 594 | wo = get sp2_woff si |
| 595 | sp2_feat_oa z_off d fe get sp2_sid si |
| 596 | for a ulong fe |
| 597 | pa = get sp2_phi a |
| 598 | if gt pa 0.0f64 |
| 599 | sp2_add_dz add wo mul a d d mul gw pa |
| 600 | end |
| 601 | end |
| 602 | end |
| 603 | |
| 604 | fn sp2_dot_oa_ocs z_off ulong co ulong d ulong to f64 |
| 605 | dot = 0.0f64 |
| 606 | for j ulong d |
| 607 | dot = add dot mul get oa add z_off j ocsg add co j |
| 608 | end |
| 609 | ret dot |
| 610 | end |
| 611 | |
| 612 | fn sp2_score_strands z_off ulong ns ulong d ulong to void |
| 613 | zn = 0.0f64 |
| 614 | for j ulong d |
| 615 | v = get oa add z_off j |
| 616 | zn = add zn mul v v |
| 617 | end |
| 618 | zn = fsqrt fmax zn 1.0e-30f64 |
| 619 | for s ulong ns |
| 620 | put sp2_sim s div sp2_dot_oa_ocs z_off get sp2_coff s d zn |
| 621 | end |
| 622 | end |
| 623 | |
| 624 | fn sp2_add_dz_to_oa z_off ulong d ulong to void |
| 625 | for j ulong d |
| 626 | idx = add z_off j |
| 627 | put oa idx add get oa idx get sp2_dz j |
| 628 | end |
| 629 | end |
| 630 | |
| 631 | note Sparse-FFN Helix Router (sproute): route z at oa[z_off..+d) to Top-K loaded strands via |
| 632 | note centroid cosine similarity, evaluate only the Top-K thin FFN experts, and add weighted residual to oa[z_off..+d). |
| 633 | fn sp2_route_apply z_off ulong top_k ulong to void |
| 634 | ns = get sp2_st 0 |
| 635 | d = get og 2 |
| 636 | sp2_score_strands z_off ns d |
| 637 | kk = top_k |
| 638 | if gt kk ns |
| 639 | kk = ns |
| 640 | end |
| 641 | sp2_pick_topk ns kk |
| 642 | for j3 ulong d |
| 643 | put sp2_dz j3 0.0f64 |
| 644 | end |
| 645 | for k4 ulong kk |
| 646 | sp2_apply_1k z_off get sp2_topi k4 get sp2_topw k4 d |
| 647 | end |
| 648 | sp2_add_dz_to_oa z_off d |
| 649 | end |
| 650 | |
| 651 | fn sp2_save_cal cal_z ulong z_off ulong yt ulong d ulong to void |
| 652 | ncal = get sp2_st 8 |
| 653 | if lt ncal OSP2_CALN |
| 654 | czo = add cal_z mul ncal d |
| 655 | for j ulong d |
| 656 | ocsp add czo j get oa add z_off j |
| 657 | end |
| 658 | put sp2_caly ncal yt |
| 659 | put sp2_st 8 add ncal 1 |
| 660 | end |
| 661 | end |
| 662 | |
| 663 | fn sp2_cryst_tok t ulong f_exp ulong sid ulong cal_z ulong to void |
| 664 | d = get og 2 |
| 665 | z_off = add get og 17 mul t d |
| 666 | yt = get oby t |
| 667 | w_tok = add 0.25f64 fmin sp2_tok_grad z_off yt 8.0f64 |
| 668 | sp2_feat_oa z_off d f_exp sid |
| 669 | sp2_acc_tok z_off w_tok f_exp d |
| 670 | sp2_save_cal cal_z z_off yt d |
| 671 | end |
| 672 | |
| 673 | fn sp2_cryst_stream nwin ulong max_toks ulong f_exp ulong sid ulong cal_z ulong to ulong |
| 674 | tt = get og 1 |
| 675 | toks = ulong 0 |
| 676 | put sp2_st 8 0 |
| 677 | for w ulong nwin |
| 678 | obrow 0 mul w tt |
| 679 | dummy = ofwd 0 |
| 680 | for t ulong tt |
| 681 | if ge toks max_toks |
| 682 | break |
| 683 | end |
| 684 | sp2_cryst_tok t f_exp sid cal_z |
| 685 | toks = add toks 1 |
| 686 | end |
| 687 | end |
| 688 | ret toks |
| 689 | end |
| 690 | |
| 691 | fn sp2_norm_cent c_off ulong d ulong to void |
| 692 | cn = 0.0f64 |
| 693 | for jc ulong d |
| 694 | vc = ocsg add c_off jc |
| 695 | cn = add cn mul vc vc |
| 696 | end |
| 697 | cn = fsqrt fmax cn 1.0e-30f64 |
| 698 | for jc2 ulong d |
| 699 | ocsp add c_off jc2 div ocsg add c_off jc2 cn |
| 700 | end |
| 701 | end |
| 702 | |
| 703 | fn sp2_cryst_cal ncal ulong cal_z ulong b_off ulong f_exp ulong to f64 |
| 704 | base_cal = sp2_cal_loss ncal cal_z f_exp 0.0f64 |
| 705 | put sp2_st 9 bitcast ulong base_cal |
| 706 | best_l = base_cal |
| 707 | best_eta = 0.0f64 |
| 708 | etas = new f64 7 0.0f64 |
| 709 | put etas 0 0.05f64 |
| 710 | put etas 1 0.15f64 |
| 711 | put etas 2 0.35f64 |
| 712 | put etas 3 0.7f64 |
| 713 | put etas 4 1.0f64 |
| 714 | put etas 5 1.5f64 |
| 715 | put etas 6 2.5f64 |
| 716 | for ei ulong 7 |
| 717 | et = get etas ei |
| 718 | cl = sp2_cal_loss ncal cal_z f_exp et |
| 719 | if lt cl best_l |
| 720 | best_l = cl |
| 721 | best_eta = et |
| 722 | end |
| 723 | end |
| 724 | nw = mul f_exp get og 2 |
| 725 | for wi ulong nw bound 16777216 |
| 726 | ocsp add b_off wi mul ocsg add b_off wi best_eta |
| 727 | end |
| 728 | ret best_l |
| 729 | end |
| 730 | |
| 731 | fn sp2_cryst_print toks ulong best_l f64 pbytes ulong ms ulong to void |
| 732 | oputc 83 |
| 733 | oputc 80 |
| 734 | oputc 67 |
| 735 | oputc 82 |
| 736 | oputc 89 |
| 737 | oputc 83 |
| 738 | oputc 84 |
| 739 | oputc 32 |
| 740 | oputu get og 5 |
| 741 | oputc 32 |
| 742 | oputu get og 2 |
| 743 | oputc 32 |
| 744 | oputu get sp2_st 10 |
| 745 | oputc 32 |
| 746 | oputu get sp2_st 6 |
| 747 | oputc 32 |
| 748 | oputu get sp2_st 11 |
| 749 | oputc 32 |
| 750 | oputu toks |
| 751 | oputc 32 |
| 752 | oputl rmic bitcast f64 get sp2_st 9 |
| 753 | oputc 32 |
| 754 | oputl rmic best_l |
| 755 | oputc 32 |
| 756 | oputu pbytes |
| 757 | oputc 32 |
| 758 | oputu ms |
| 759 | oputc 10 |
| 760 | oflush |
| 761 | end |
| 762 | |
| 763 | fn sp2_cryst_parse to void |
| 764 | na = oargc |
| 765 | if or lt na 7 gt na 10 |
| 766 | exit 2 |
| 767 | end |
| 768 | ockloop 2 |
| 769 | f_exp = oargu 5 |
| 770 | max_toks = ulong 4096 |
| 771 | if ge na 8 |
| 772 | if gt oargu 7 0 |
| 773 | max_toks = oargu 7 |
| 774 | end |
| 775 | end |
| 776 | sid = ulong 1 |
| 777 | if ge na 9 |
| 778 | sid = oargu 8 |
| 779 | end |
| 780 | qbits = ulong 4 |
| 781 | if ge na 10 |
| 782 | qbits = oargu 9 |
| 783 | end |
| 784 | if or lt f_exp 2 gt f_exp OSP2_MAXF |
| 785 | exit 2 |
| 786 | end |
| 787 | if and ne qbits 4 ne qbits 64 |
| 788 | exit 2 |
| 789 | end |
| 790 | if gt get og 2 OSP2_MAXD |
| 791 | exit 12 |
| 792 | end |
| 793 | odopen 3 |
| 794 | if le get odn 0 get og 1 |
| 795 | exit 20 |
| 796 | end |
| 797 | put sp2_st 6 sid |
| 798 | put sp2_st 10 f_exp |
| 799 | put sp2_st 11 qbits |
| 800 | put sp2_st 12 max_toks |
| 801 | end |
| 802 | |
| 803 | note spcryst BASE.ck DOMAIN.tok OUT.sp2 F_EXP LAMBDA_MILLI [MAX_TOKS] [STRAND_ID] [QUANT_BITS] |
| 804 | fn cmdspcryst to void |
| 805 | t0 = monons |
| 806 | sp2_cryst_parse |
| 807 | f_exp = get sp2_st 10 |
| 808 | d = get og 2 |
| 809 | b_off = mul f_exp f_exp |
| 810 | c_off = add b_off mul f_exp d |
| 811 | cal_z = add c_off d |
| 812 | if not ocsgrow add cal_z mul OSP2_CALN d |
| 813 | exit 70 |
| 814 | end |
| 815 | put sp2_st 3 0 |
| 816 | put sp2_st 4 b_off |
| 817 | put sp2_st 5 c_off |
| 818 | sp_zero_ocs 0 add cal_z mul OSP2_CALN d |
| 819 | tt = get og 1 |
| 820 | nwin = div sub get odn 0 1 tt |
| 821 | if gt nwin div add get sp2_st 12 sub tt 1 tt |
| 822 | nwin = div add get sp2_st 12 sub tt 1 tt |
| 823 | end |
| 824 | if eq nwin 0 |
| 825 | nwin = 1 |
| 826 | end |
| 827 | toks = sp2_cryst_stream nwin get sp2_st 12 f_exp get sp2_st 6 cal_z |
| 828 | sp2_norm_cent c_off d |
| 829 | sp2_sym_reg 0 f_exp fmax div f64 oargu 6 1000.0f64 0.0001f64 |
| 830 | if ne sp_chol f_exp 0 0 |
| 831 | exit 43 |
| 832 | end |
| 833 | sp_chol_solve f_exp d 0 b_off |
| 834 | best_l = sp2_cryst_cal get sp2_st 8 cal_z b_off f_exp |
| 835 | pbytes = sp2_write_pack 4 osp_basehash get og 5 get og 33 f_exp get sp2_st 6 get sp2_st 11 |
| 836 | sp2_cryst_print toks best_l pbytes div ulong sub monons t0 1000000 |
| 837 | end |
| 838 | |
| 839 | fn sp2_weval_win w ulong top_k ulong to f64 |
| 840 | tt = get og 1 |
| 841 | d = get og 2 |
| 842 | obrow 0 mul w tt |
| 843 | bl = ofwd 0 |
| 844 | put sp2_st 13 bitcast ulong add bitcast f64 get sp2_st 13 bl |
| 845 | wl = 0.0f64 |
| 846 | for t ulong tt |
| 847 | z_off = add get og 17 mul t d |
| 848 | sp2_route_apply z_off top_k |
| 849 | wl = add wl sp2_ce_oa z_off get oby t |
| 850 | end |
| 851 | ret div wl f64 tt |
| 852 | end |
| 853 | |
| 854 | note spweval BASE.ck EVAL.tok TOP_K W S1.sp2 [S2.sp2 ...] |
| 855 | fn cmdspweval to void |
| 856 | na = oargc |
| 857 | if lt na 7 |
| 858 | exit 2 |
| 859 | end |
| 860 | ockloop 2 |
| 861 | odopen 3 |
| 862 | top_k = oargu 4 |
| 863 | ww = oargu 5 |
| 864 | ns = sub na 6 |
| 865 | if or eq top_k 0 or eq ww 0 gt ns OSP2_MAXS |
| 866 | exit 2 |
| 867 | end |
| 868 | if gt top_k 32 |
| 869 | exit 2 |
| 870 | end |
| 871 | if gt ww div sub get odn 0 1 get og 1 |
| 872 | exit 23 |
| 873 | end |
| 874 | base_h = osp_basehash get og 5 get og 33 |
| 875 | put sp2_st 0 ns |
| 876 | put sp2_st 1 0 |
| 877 | for s ulong ns |
| 878 | sp2_load_pack add 6 s s base_h |
| 879 | end |
| 880 | put sp2_st 13 0 |
| 881 | wov_ls = 0.0f64 |
| 882 | for w ulong ww |
| 883 | wov_ls = add wov_ls sp2_weval_win w top_k |
| 884 | end |
| 885 | blo = div bitcast f64 get sp2_st 13 f64 ww |
| 886 | wlo = div wov_ls f64 ww |
| 887 | oputc 83 |
| 888 | oputc 80 |
| 889 | oputc 87 |
| 890 | oputc 69 |
| 891 | oputc 86 |
| 892 | oputc 65 |
| 893 | oputc 76 |
| 894 | oputc 32 |
| 895 | oputu ns |
| 896 | oputc 32 |
| 897 | oputu top_k |
| 898 | oputc 32 |
| 899 | oputu ww |
| 900 | oputc 32 |
| 901 | oputl rmic blo |
| 902 | oputc 32 |
| 903 | oputl rmic wlo |
| 904 | oputc 32 |
| 905 | oputl rmic div wlo 0.6931471805599453 |
| 906 | oputc 10 |
| 907 | oflush |
| 908 | end |
| 909 | |
| 910 | fn sp2_argmax_lo lo ulong v ulong to ulong |
| 911 | best = ulong 0 |
| 912 | bv = get oa lo |
| 913 | for i ulong v |
| 914 | x = get oa add lo i |
| 915 | if gt x bv |
| 916 | bv = x |
| 917 | best = i |
| 918 | end |
| 919 | end |
| 920 | ret best |
| 921 | end |
| 922 | |
| 923 | fn sp2_soft_tau lo ulong v ulong best ulong tau f64 to f64 |
| 924 | mx = div get oa add lo best tau |
| 925 | sm = 0.0f64 |
| 926 | for i ulong v |
| 927 | e = fexp sub div get oa add lo i tau mx |
| 928 | put oa add lo i e |
| 929 | sm = add sm e |
| 930 | end |
| 931 | ret sm |
| 932 | end |
| 933 | |
| 934 | note Pick token from logits at oa[og[27]..+V) with temperature tm_milli and PRNG state st. |
| 935 | fn sp2_pick_tok st ulong tm ulong to ulong |
| 936 | v = get og 0 |
| 937 | lo = get og 27 |
| 938 | best = sp2_argmax_lo lo v |
| 939 | if eq tm 0 |
| 940 | ret best |
| 941 | end |
| 942 | sm = sp2_soft_tau lo v best div f64 tm 1000.0f64 |
| 943 | u = mul mul f64 shr smx st 11 1.1102230246251565e-16 sm |
| 944 | acc = 0.0f64 |
| 945 | for i3 ulong v |
| 946 | acc = add acc get oa add lo i3 |
| 947 | if lt u acc |
| 948 | ret i3 |
| 949 | end |
| 950 | end |
| 951 | ret best |
| 952 | end |
| 953 | |
| 954 | note Check if argv[ai] is an existing file beginning with OLLMSP02 magic (non-fatal if missing). |
| 955 | fn sp2_is_sp2 ai ulong to bool |
| 956 | r = oargr ai |
| 957 | if le r 0 |
| 958 | ret false |
| 959 | end |
| 960 | fd = fopen oarg 0 ulong r 0 0 |
| 961 | if lt fd 0 |
| 962 | ret false |
| 963 | end |
| 964 | k = read int fd opw 0 8 |
| 965 | c = fclose int fd |
| 966 | if ne k 8 |
| 967 | ret false |
| 968 | end |
| 969 | ret eq opw8 0 OSP2_MAG |
| 970 | end |
| 971 | |
| 972 | fn sp2_init_prompt v ulong to ulong |
| 973 | plen = oargr 3 |
| 974 | is_num = gt plen 0 |
| 975 | for pi ulong ulong plen |
| 976 | ch = get oarg pi |
| 977 | if or lt ch 48 gt ch 57 |
| 978 | is_num = false |
| 979 | end |
| 980 | end |
| 981 | put og 9 0 |
| 982 | if is_num |
| 983 | ocpush 0 |
| 984 | ret oargu 3 |
| 985 | end |
| 986 | sh = ulong 14695981039346656037 |
| 987 | for pi2 ulong ulong plen |
| 988 | bt = ulong get oarg pi2 |
| 989 | sh = mul xor sh bt 1099511628211 |
| 990 | ocpush rem bt v |
| 991 | end |
| 992 | ret sh |
| 993 | end |
| 994 | |
| 995 | fn sp2_samp_step seed ulong tm ulong top_k ulong to ulong |
| 996 | olastf |
| 997 | d = get og 2 |
| 998 | v = get og 0 |
| 999 | z_off = add get og 17 mul sub get og 9 1 d |
| 1000 | sp2_route_apply z_off top_k |
| 1001 | klogit z_off get og 27 v d |
| 1002 | tok = sp2_pick_tok seed tm |
| 1003 | if eq v 256 |
| 1004 | oputc tok |
| 1005 | else |
| 1006 | oputu tok |
| 1007 | oputc 32 |
| 1008 | end |
| 1009 | ocpush tok |
| 1010 | if ne tm 0 |
| 1011 | ret add seed 11400714819323198485 |
| 1012 | end |
| 1013 | ret seed |
| 1014 | end |
| 1015 | |
| 1016 | note spwsamp / spsamp BASE.ck SEED_OR_PROMPT MAX_TOK TOP_K TEMP_MILLI S1.sp2 [S2.sp2 ...] |
| 1017 | fn cmdspwsamp to void |
| 1018 | na = oargc |
| 1019 | if lt na 8 |
| 1020 | exit 2 |
| 1021 | end |
| 1022 | ockloop 2 |
| 1023 | max_tok = oargu 4 |
| 1024 | top_k = oargu 5 |
| 1025 | tm = oargu 6 |
| 1026 | ns = sub na 7 |
| 1027 | if or eq max_tok 0 or eq top_k 0 or gt top_k 32 gt ns OSP2_MAXS |
| 1028 | exit 2 |
| 1029 | end |
| 1030 | base_h = osp_basehash get og 5 get og 33 |
| 1031 | put sp2_st 0 ns |
| 1032 | put sp2_st 1 0 |
| 1033 | for s ulong ns |
| 1034 | sp2_load_pack add 7 s s base_h |
| 1035 | end |
| 1036 | seed = sp2_init_prompt get og 0 |
| 1037 | for step ulong max_tok bound 16384 |
| 1038 | seed = sp2_samp_step seed tm top_k |
| 1039 | end |
| 1040 | oputc 10 |
| 1041 | oflush |
| 1042 | end |