omega-helix
This project has not described itself yet.
Brought by drakestapleton
omega-helix / native / apu_weave.om
| 1 | note SPDX-License-Identifier: AGPL-3.0-only |
| 2 | note native/apu_weave.om: Full-APU Simultaneous Co-Execution Fabric core for Spiral-2048 OLLMSP02. |
| 3 | note Portable pure seeded OM-2 core (compiles on gb10, aarch64, x86linux, and a64macos). |
| 4 | note Platform fabric backend supplies `apu_fabric_run` (20-core Grace fork/pipe/cpuset + 48-SM Blackwell |
| 5 | note streaming ring on Linux/GB10; portable single-process driver on macOS). |
| 6 | note |
| 7 | note CLI: |
| 8 | note ollm[-gb10] apuweave <base.ck> <shard_list_or_dir> <out_dir> <f_exp> <lambda_milli> <max_toks> <quant_bits> [workers=16] |
| 9 | |
| 10 | const APU_MAX_SHARDS ulong 2048 |
| 11 | const APU_PATH_STRIDE ulong 256 |
| 12 | const APU_MAX_F ulong 640 |
| 13 | const APU_MAX_D ulong 2048 |
| 14 | const APU_CALN ulong 128 |
| 15 | |
| 16 | note Direct-array Cholesky + calibration workspace (copy-on-write per forked Grace worker; zero mapget overhead!) |
| 17 | global apu_g f64 409600 |
| 18 | global apu_b f64 1310720 |
| 19 | global apu_c f64 2048 |
| 20 | global apu_z f64 2048 |
| 21 | global apu_rv f64 2048 |
| 22 | global apu_phi f64 640 |
| 23 | global apu_acti ulong 640 |
| 24 | global apu_actv f64 640 |
| 25 | global apu_calz f64 262144 |
| 26 | global apu_caly ulong 128 |
| 27 | global apu_l0 f64 65536 |
| 28 | global apu_dl f64 65536 |
| 29 | global apu_etav f64 8 |
| 30 | global apu_etas f64 8 |
| 31 | global apu_dz f64 2048 |
| 32 | |
| 33 | note Shard catalog & output directory buffers |
| 34 | global apu_spaths uchar 524288 |
| 35 | global apu_slen ulong 2048 |
| 36 | global apu_sids ulong 2048 |
| 37 | global apu_odir uchar 512 |
| 38 | global apu_odirn ulong 1 |
| 39 | global apu_pbuf uchar 1024 |
| 40 | global apu_ptmp uchar 1028 |
| 41 | global apu_pk uchar 2100 |
| 42 | |
| 43 | note Configuration & aggregate state: |
| 44 | note apu_cfg: 0=n_shards, 1=f_exp, 2=lam_milli, 3=max_toks, 4=qbits, 5=workers, |
| 45 | note 6=base_hash, 7=gpu_mode(0=cpu,1=bf16,2=f32), 8=n_pref, 9=n_verif, |
| 46 | note 10=n_train_win, 11=n_held_win, 12=ncal, 13=gpu_active_ns, 14=cpu_active_ns, |
| 47 | note 15=total_w4_bytes, 16=tamper_ok, 17=sum_base_eval_u6, 18=sum_wov_eval_u6, |
| 48 | note 19=t_start_ns, 20=wave_seq, 21=cpu_span_start, 22=cpu_span_end, 23=sum_cal_drop_u6 |
| 49 | global apu_cfg ulong 32 |
| 50 | global apu_io uchar 65536 |
| 51 | global apu_hdr ulong 12 |
| 52 | global apu_tph ulong 1 |
| 53 | global apu_wfd int 1 |
| 54 | |
| 55 | note ---- Small string/output helpers ---- |
| 56 | |
| 57 | fn apu_puts s view uchar * to void |
| 58 | n = len s |
| 59 | for i ulong n bound 4096 |
| 60 | oputc ulong get s i |
| 61 | end |
| 62 | end |
| 63 | |
| 64 | fn apu_put4_io o ulong v uint to void |
| 65 | put apu_io o uchar and v 255 |
| 66 | put apu_io add o 1 uchar and shr v 8 255 |
| 67 | put apu_io add o 2 uchar and shr v 16 255 |
| 68 | put apu_io add o 3 uchar and shr v 24 255 |
| 69 | end |
| 70 | |
| 71 | fn apu_get4_io o ulong to uint |
| 72 | b0 = uint get apu_io o |
| 73 | b1 = shl uint get apu_io add o 1 8 |
| 74 | b2 = shl uint get apu_io add o 2 16 |
| 75 | b3 = shl uint get apu_io add o 3 24 |
| 76 | ret or or b0 b1 or b2 b3 |
| 77 | end |
| 78 | |
| 79 | fn apu_put8_io o ulong v ulong to void |
| 80 | for i ulong 8 |
| 81 | put apu_io add o i uchar and shr v mul i 8 255 |
| 82 | end |
| 83 | end |
| 84 | |
| 85 | fn apu_get8_io o ulong to ulong |
| 86 | v = ulong 0 |
| 87 | for i ulong 8 |
| 88 | v = or v shl ulong get apu_io add o i mul i 8 |
| 89 | end |
| 90 | ret v |
| 91 | end |
| 92 | |
| 93 | fn apu_fnv_io h ulong off ulong n ulong to ulong |
| 94 | x = h |
| 95 | for i ulong n bound 65536 |
| 96 | x = mul xor x ulong get apu_io add off i 1099511628211 |
| 97 | end |
| 98 | ret x |
| 99 | end |
| 100 | |
| 101 | note ---- Shard path helpers ---- |
| 102 | |
| 103 | fn apu_spath_set idx ulong s_off ulong slen ulong sid ulong to void |
| 104 | if or ge idx APU_MAX_SHARDS gt slen sub APU_PATH_STRIDE 1 |
| 105 | ret |
| 106 | end |
| 107 | dst = mul idx APU_PATH_STRIDE |
| 108 | for i ulong slen bound 256 |
| 109 | put apu_spaths add dst i get apu_pbuf add s_off i |
| 110 | end |
| 111 | put apu_spaths add dst slen 0 |
| 112 | put apu_slen idx slen |
| 113 | put apu_sids idx sid |
| 114 | end |
| 115 | |
| 116 | fn apu_copy_spath idx ulong to ulong |
| 117 | n = get apu_slen idx |
| 118 | src = mul idx APU_PATH_STRIDE |
| 119 | for i ulong n bound 256 |
| 120 | put apu_pbuf i get apu_spaths add src i |
| 121 | end |
| 122 | put apu_pbuf n 0 |
| 123 | ret n |
| 124 | end |
| 125 | |
| 126 | fn apu_has_tok_ext src ulong end_i ulong base_i ulong to bool |
| 127 | if lt sub end_i base_i 4 |
| 128 | ret false |
| 129 | end |
| 130 | if ne get apu_spaths add src sub end_i 4 46 |
| 131 | ret false |
| 132 | end |
| 133 | if ne get apu_spaths add src sub end_i 3 116 |
| 134 | ret false |
| 135 | end |
| 136 | if ne get apu_spaths add src sub end_i 2 111 |
| 137 | ret false |
| 138 | end |
| 139 | ret eq get apu_spaths add src sub end_i 1 107 |
| 140 | end |
| 141 | |
| 142 | fn apu_find_base src ulong sn ulong to ulong |
| 143 | last_sl = ulong 0 |
| 144 | has_sl = false |
| 145 | for j ulong sn bound 256 |
| 146 | if eq get apu_spaths add src j 47 |
| 147 | last_sl = j |
| 148 | has_sl = true |
| 149 | end |
| 150 | end |
| 151 | if has_sl |
| 152 | ret add last_sl 1 |
| 153 | end |
| 154 | ret 0 |
| 155 | end |
| 156 | |
| 157 | note Build output .sp2 path into apu_pbuf: <out_dir>/<stem>.sp2; returns length. |
| 158 | fn apu_make_outpath idx ulong to ulong |
| 159 | p = get apu_odirn 0 |
| 160 | for i ulong p bound 512 |
| 161 | put apu_pbuf i get apu_odir i |
| 162 | end |
| 163 | if gt p 0 |
| 164 | if ne get apu_pbuf sub p 1 47 |
| 165 | put apu_pbuf p 47 |
| 166 | p = add p 1 |
| 167 | end |
| 168 | end |
| 169 | src = mul idx APU_PATH_STRIDE |
| 170 | sn = get apu_slen idx |
| 171 | base_i = apu_find_base src sn |
| 172 | end_i = sn |
| 173 | if apu_has_tok_ext src end_i base_i |
| 174 | end_i = sub end_i 4 |
| 175 | end |
| 176 | stem_n = sub end_i base_i |
| 177 | if or eq stem_n 0 gt add p add stem_n 5 1000 |
| 178 | exit 2 |
| 179 | end |
| 180 | for k ulong stem_n bound 256 |
| 181 | put apu_pbuf add p k get apu_spaths add src add base_i k |
| 182 | end |
| 183 | p = add p stem_n |
| 184 | put apu_pbuf p 46 |
| 185 | put apu_pbuf add p 1 115 |
| 186 | put apu_pbuf add p 2 112 |
| 187 | put apu_pbuf add p 3 50 |
| 188 | put apu_pbuf add p 4 0 |
| 189 | ret add p 4 |
| 190 | end |
| 191 | |
| 192 | fn apu_skip_ws p ulong lend ulong to ulong |
| 193 | a = p |
| 194 | while lt a lend bound 1024 |
| 195 | c = get apu_io a |
| 196 | if and ne c 32 ne c 9 |
| 197 | break |
| 198 | end |
| 199 | a = add a 1 |
| 200 | end |
| 201 | ret a |
| 202 | end |
| 203 | |
| 204 | fn apu_scan_tok p ulong lend ulong to ulong |
| 205 | b = p |
| 206 | while lt b lend bound 1024 |
| 207 | c = get apu_io b |
| 208 | if or or eq c 32 eq c 9 or eq c 13 eq c 10 |
| 209 | break |
| 210 | end |
| 211 | b = add b 1 |
| 212 | end |
| 213 | ret b |
| 214 | end |
| 215 | |
| 216 | fn apu_try_num a ulong b ulong to ulong |
| 217 | n = sub b a |
| 218 | if or eq n 0 gt n 18 |
| 219 | ret 0 |
| 220 | end |
| 221 | v = ulong 0 |
| 222 | for i ulong n bound 20 |
| 223 | ch = ulong get apu_io add a i |
| 224 | if or lt ch 48 gt ch 57 |
| 225 | ret 0 |
| 226 | end |
| 227 | v = add mul v 10 sub ch 48 |
| 228 | end |
| 229 | ret v |
| 230 | end |
| 231 | |
| 232 | note Parse a shard list file (fd already open at offset 0). |
| 233 | note Supports both 1-column (<path.tok>) and 3-column (<sid> <path.tok> <out.sp2>) formats! |
| 234 | fn apu_parse_list_line lstart ulong lend ulong to void |
| 235 | a = apu_skip_ws lstart lend |
| 236 | if ge a lend |
| 237 | ret |
| 238 | end |
| 239 | if eq get apu_io a 35 |
| 240 | ret |
| 241 | end |
| 242 | b = apu_scan_tok a lend |
| 243 | sid_v = apu_try_num a b |
| 244 | if gt sid_v 0 |
| 245 | a = apu_skip_ws b lend |
| 246 | b = apu_scan_tok a lend |
| 247 | end |
| 248 | pn = sub b a |
| 249 | cnt = get apu_cfg 0 |
| 250 | if or eq pn 0 or ge pn APU_PATH_STRIDE ge cnt APU_MAX_SHARDS |
| 251 | ret |
| 252 | end |
| 253 | for k ulong pn bound 256 |
| 254 | put apu_pbuf k get apu_io add a k |
| 255 | end |
| 256 | if eq sid_v 0 |
| 257 | sid_v = add cnt 1 |
| 258 | end |
| 259 | apu_spath_set cnt 0 pn sid_v |
| 260 | put apu_cfg 0 add cnt 1 |
| 261 | end |
| 262 | |
| 263 | fn apu_scan_buf tot ulong eof bool to ulong |
| 264 | ls = ulong 0 |
| 265 | i = ulong 0 |
| 266 | while lt i tot bound 65536 |
| 267 | if eq get apu_io i 10 |
| 268 | apu_parse_list_line ls i |
| 269 | ls = add i 1 |
| 270 | end |
| 271 | i = add i 1 |
| 272 | end |
| 273 | if eof |
| 274 | if lt ls tot |
| 275 | apu_parse_list_line ls tot |
| 276 | end |
| 277 | ret 0 |
| 278 | end |
| 279 | remn = sub tot ls |
| 280 | for m ulong remn bound 65536 |
| 281 | put apu_io m get apu_io add ls m |
| 282 | end |
| 283 | ret remn |
| 284 | end |
| 285 | |
| 286 | fn apu_load_list_fd fd int to void |
| 287 | remn = ulong 0 |
| 288 | eof = false |
| 289 | while not eof bound 1024 |
| 290 | if ge remn 65536 |
| 291 | exit 2 |
| 292 | end |
| 293 | r = read fd apu_io remn sub 65536 remn |
| 294 | if lt r 0 |
| 295 | exit 70 |
| 296 | end |
| 297 | if eq r 0 |
| 298 | eof = true |
| 299 | end |
| 300 | remn = apu_scan_buf add remn ulong r eof |
| 301 | end |
| 302 | end |
| 303 | |
| 304 | fn apu_cmp_bytes oi ulong oj ulong mn ulong to int |
| 305 | for k ulong mn bound 256 |
| 306 | a = int get apu_spaths add oi k |
| 307 | b = int get apu_spaths add oj k |
| 308 | if ne a b |
| 309 | ret sub a b |
| 310 | end |
| 311 | end |
| 312 | ret int 0 |
| 313 | end |
| 314 | |
| 315 | note Lexicographic compare of shard path i vs j (< 0 if i < j) |
| 316 | fn apu_cmp_spath i ulong j ulong to int |
| 317 | ni = get apu_slen i |
| 318 | nj = get apu_slen j |
| 319 | mn = ni |
| 320 | if lt nj mn |
| 321 | mn = nj |
| 322 | end |
| 323 | r = apu_cmp_bytes mul i APU_PATH_STRIDE mul j APU_PATH_STRIDE mn |
| 324 | if ne r 0 |
| 325 | ret r |
| 326 | end |
| 327 | if lt ni nj |
| 328 | ret int -1 |
| 329 | end |
| 330 | if gt ni nj |
| 331 | ret int 1 |
| 332 | end |
| 333 | ret int 0 |
| 334 | end |
| 335 | |
| 336 | fn apu_swap_bytes oi ulong oj ulong cnt ulong to void |
| 337 | for k ulong cnt bound 256 |
| 338 | ti = get apu_spaths add oi k |
| 339 | tj = get apu_spaths add oj k |
| 340 | put apu_spaths add oi k tj |
| 341 | put apu_spaths add oj k ti |
| 342 | end |
| 343 | end |
| 344 | |
| 345 | fn apu_swap_spath i ulong j ulong to void |
| 346 | ni = get apu_slen i |
| 347 | nj = get apu_slen j |
| 348 | mx = ni |
| 349 | if gt nj mx |
| 350 | mx = nj |
| 351 | end |
| 352 | apu_swap_bytes mul i APU_PATH_STRIDE mul j APU_PATH_STRIDE add mx 1 |
| 353 | put apu_slen i nj |
| 354 | put apu_slen j ni |
| 355 | end |
| 356 | |
| 357 | note Deterministic insertion sort of discovered directory shards + assign 1-based strand IDs |
| 358 | fn apu_sort_shards to void |
| 359 | n = get apu_cfg 0 |
| 360 | i = ulong 1 |
| 361 | while lt i n bound 2048 |
| 362 | j = i |
| 363 | while gt j 0 bound 2048 |
| 364 | if le apu_cmp_spath sub j 1 j 0 |
| 365 | break |
| 366 | end |
| 367 | apu_swap_spath sub j 1 j |
| 368 | j = sub j 1 |
| 369 | end |
| 370 | i = add i 1 |
| 371 | end |
| 372 | for s ulong n bound 2048 |
| 373 | put apu_sids s add s 1 |
| 374 | end |
| 375 | end |
| 376 | |
| 377 | note ---- Fast Direct-Array Sparse ReLU Feature & Gram/RHS Accumulation ---- |
| 378 | |
| 379 | fn apu_feat_one f ulong d ulong sid ulong to f64 |
| 380 | seed = add mul sid 1000003 mul add f 1 65537 |
| 381 | acc = 0.0f64 |
| 382 | for k ulong 4 |
| 383 | h = smx add seed mul k 2654435761 |
| 384 | v = get apu_z rem shr h 1 d |
| 385 | if eq and h 1 0 |
| 386 | acc = add acc v |
| 387 | else |
| 388 | acc = sub acc v |
| 389 | end |
| 390 | end |
| 391 | ret mul acc 0.5f64 |
| 392 | end |
| 393 | |
| 394 | note Exact same hash-orthogonal ReLU feature formula as native/spiral.om (100% OLLMSP02 parity!) |
| 395 | note Also builds compact non-zero active index/value lists apu_acti/apu_actv for 4x faster Gram & 2x faster RHS! |
| 396 | fn apu_feat_z d ulong f_exp ulong sid ulong to ulong |
| 397 | fm1 = sub f_exp 1 |
| 398 | nact = ulong 0 |
| 399 | for f ulong fm1 bound 640 |
| 400 | u = apu_feat_one f d sid |
| 401 | if gt u 0.0f64 |
| 402 | put apu_phi f u |
| 403 | put apu_acti nact f |
| 404 | put apu_actv nact u |
| 405 | nact = add nact 1 |
| 406 | else |
| 407 | put apu_phi f 0.0f64 |
| 408 | end |
| 409 | end |
| 410 | put apu_phi fm1 1.0f64 |
| 411 | put apu_acti nact fm1 |
| 412 | put apu_actv nact 1.0f64 |
| 413 | ret add nact 1 |
| 414 | end |
| 415 | |
| 416 | fn apu_zero_cryst f_exp ulong d ulong to void |
| 417 | gg = mul f_exp f_exp |
| 418 | for i ulong gg bound 409600 |
| 419 | put apu_g i 0.0f64 |
| 420 | end |
| 421 | bd = mul f_exp d |
| 422 | for j ulong bd bound 1310720 |
| 423 | put apu_b j 0.0f64 |
| 424 | end |
| 425 | for k ulong d bound 2048 |
| 426 | put apu_c k 0.0f64 |
| 427 | end |
| 428 | end |
| 429 | |
| 430 | fn apu_acc_gram_row ga ulong wa f64 ia ulong nact ulong to void |
| 431 | ib = ia |
| 432 | while lt ib nact bound 640 |
| 433 | idx = add ga get apu_acti ib |
| 434 | dv = mul wa get apu_actv ib |
| 435 | put apu_g idx add get apu_g idx dv |
| 436 | ib = add ib 1 |
| 437 | end |
| 438 | end |
| 439 | |
| 440 | fn apu_acc_rhs_row ba ulong wa f64 d ulong to void |
| 441 | for j ulong d bound 2048 |
| 442 | idx = add ba j |
| 443 | dv = mul wa get apu_rv j |
| 444 | put apu_b idx add get apu_b idx dv |
| 445 | end |
| 446 | end |
| 447 | |
| 448 | note Accumulate one token (apu_z[0..d), apu_rv[0..d)) with surprisal weight w_tok into apu_g, apu_b, apu_c. |
| 449 | fn apu_acc_tok w_tok f64 f_exp ulong d ulong sid ulong to void |
| 450 | for j ulong d bound 2048 |
| 451 | put apu_c j add get apu_c j mul w_tok get apu_z j |
| 452 | end |
| 453 | nact = apu_feat_z d f_exp sid |
| 454 | for ia ulong nact bound 640 |
| 455 | a = get apu_acti ia |
| 456 | wa = mul w_tok get apu_actv ia |
| 457 | apu_acc_gram_row mul a f_exp wa ia nact |
| 458 | apu_acc_rhs_row mul a d wa d |
| 459 | end |
| 460 | end |
| 461 | |
| 462 | fn apu_norm_cent d ulong to void |
| 463 | cn = 0.0f64 |
| 464 | for j ulong d bound 2048 |
| 465 | v = get apu_c j |
| 466 | cn = add cn mul v v |
| 467 | end |
| 468 | inv = div 1.0f64 fsqrt fmax cn 1.0e-30f64 |
| 469 | for j2 ulong d bound 2048 |
| 470 | put apu_c j2 mul get apu_c j2 inv |
| 471 | end |
| 472 | end |
| 473 | |
| 474 | fn apu_sym_reg f_exp ulong lam f64 to void |
| 475 | for a ulong f_exp bound 640 |
| 476 | ga = mul a f_exp |
| 477 | put apu_g add ga a add get apu_g add ga a lam |
| 478 | b = add a 1 |
| 479 | while lt b f_exp bound 640 |
| 480 | v = get apu_g add ga b |
| 481 | put apu_g add mul b f_exp a v |
| 482 | b = add b 1 |
| 483 | end |
| 484 | end |
| 485 | end |
| 486 | |
| 487 | note ---- Fast Direct-Array In-Place Cholesky (LL^T) & Multi-RHS Triangular Solve ---- |
| 488 | |
| 489 | fn apu_chol_diag j ulong m ulong to f64 |
| 490 | row = mul j m |
| 491 | acc = get apu_g add row j |
| 492 | for s ulong j bound 640 |
| 493 | v = get apu_g add row s |
| 494 | acc = sub acc mul v v |
| 495 | end |
| 496 | if le acc 1.0e-15f64 |
| 497 | ret 0.0f64 |
| 498 | end |
| 499 | ret fsqrt acc |
| 500 | end |
| 501 | |
| 502 | fn apu_chol_off i ulong j ulong m ulong invd f64 to void |
| 503 | ri = mul i m |
| 504 | rj = mul j m |
| 505 | acc = get apu_g add ri j |
| 506 | for s ulong j bound 640 |
| 507 | vi = get apu_g add ri s |
| 508 | vj = get apu_g add rj s |
| 509 | acc = sub acc mul vi vj |
| 510 | end |
| 511 | put apu_g add ri j mul acc invd |
| 512 | end |
| 513 | |
| 514 | fn apu_chol m ulong to ulong |
| 515 | for j ulong m bound 640 |
| 516 | diag = apu_chol_diag j m |
| 517 | if le diag 0.0f64 |
| 518 | ret 1 |
| 519 | end |
| 520 | put apu_g add mul j m j diag |
| 521 | invd = div 1.0f64 diag |
| 522 | i = add j 1 |
| 523 | while lt i m bound 640 |
| 524 | apu_chol_off i j m invd |
| 525 | i = add i 1 |
| 526 | end |
| 527 | end |
| 528 | ret 0 |
| 529 | end |
| 530 | |
| 531 | fn apu_sub_row_axpy bi ulong bj ulong d ulong sc f64 to void |
| 532 | for c ulong d bound 2048 |
| 533 | idx = add bi c |
| 534 | vb = get apu_b add bj c |
| 535 | cur = get apu_b idx |
| 536 | put apu_b idx sub cur mul sc vb |
| 537 | end |
| 538 | end |
| 539 | |
| 540 | fn apu_scale_row bi ulong d ulong invd f64 to void |
| 541 | for c ulong d bound 2048 |
| 542 | idx = add bi c |
| 543 | put apu_b idx mul get apu_b idx invd |
| 544 | end |
| 545 | end |
| 546 | |
| 547 | note Cache-friendly row-major triangular solve across all D columns simultaneously! |
| 548 | fn apu_fwd_sub_row i ulong m ulong d ulong to void |
| 549 | rl = mul i m |
| 550 | bi = mul i d |
| 551 | for j ulong i bound 640 |
| 552 | apu_sub_row_axpy bi mul j d d get apu_g add rl j |
| 553 | end |
| 554 | apu_scale_row bi d div 1.0f64 get apu_g add rl i |
| 555 | end |
| 556 | |
| 557 | fn apu_bwd_sub_row i ulong m ulong d ulong to void |
| 558 | bi = mul i d |
| 559 | j = add i 1 |
| 560 | while lt j m bound 640 |
| 561 | apu_sub_row_axpy bi mul j d d get apu_g add mul j m i |
| 562 | j = add j 1 |
| 563 | end |
| 564 | apu_scale_row bi d div 1.0f64 get apu_g add mul i m i |
| 565 | end |
| 566 | |
| 567 | fn apu_chol_solve m ulong d ulong to void |
| 568 | for i ulong m bound 640 |
| 569 | apu_fwd_sub_row i m d |
| 570 | end |
| 571 | idx = m |
| 572 | while gt idx 0 bound 640 |
| 573 | idx = sub idx 1 |
| 574 | apu_bwd_sub_row idx m d |
| 575 | end |
| 576 | end |
| 577 | |
| 578 | note ---- 4x Faster Linear Step-Size Calibration: L(z + eta*dz) = L_0 + eta * dL ---- |
| 579 | |
| 580 | note Direct ow access (WTE is at offset 0 of ow, guaranteed < og[34]) + 4x unrolled fused 2-vector dot product! |
| 581 | fn apu_pair_row r ulong d ulong to void |
| 582 | w = mul r d |
| 583 | s0 = 0.0f64 |
| 584 | s1 = 0.0f64 |
| 585 | j = ulong 0 |
| 586 | while le add j 4 d bound 2048 |
| 587 | wj = get ow add w j |
| 588 | s0 = add s0 mul get apu_z j wj |
| 589 | s1 = add s1 mul get apu_dz j wj |
| 590 | j1 = add j 1 |
| 591 | wj = get ow add w j1 |
| 592 | s0 = add s0 mul get apu_z j1 wj |
| 593 | s1 = add s1 mul get apu_dz j1 wj |
| 594 | j1 = add j 2 |
| 595 | wj = get ow add w j1 |
| 596 | s0 = add s0 mul get apu_z j1 wj |
| 597 | s1 = add s1 mul get apu_dz j1 wj |
| 598 | j1 = add j 3 |
| 599 | wj = get ow add w j1 |
| 600 | s0 = add s0 mul get apu_z j1 wj |
| 601 | s1 = add s1 mul get apu_dz j1 wj |
| 602 | j = add j 4 |
| 603 | end |
| 604 | while lt j d bound 4 |
| 605 | wj = get ow add w j |
| 606 | s0 = add s0 mul get apu_z j wj |
| 607 | s1 = add s1 mul get apu_dz j wj |
| 608 | j = add j 1 |
| 609 | end |
| 610 | put apu_l0 r s0 |
| 611 | put apu_dl r s1 |
| 612 | end |
| 613 | |
| 614 | note Compute L_0 = apu_z @ WTE^T into apu_l0 and dL = apu_dz @ WTE^T into apu_dl in ONE fused pass over WTE! |
| 615 | fn apu_pair_logits v ulong d ulong to void |
| 616 | for r ulong v bound 65536 |
| 617 | apu_pair_row r d |
| 618 | end |
| 619 | end |
| 620 | |
| 621 | fn apu_ce_lin v ulong y ulong eta f64 to f64 |
| 622 | d0 = get apu_dl 0 |
| 623 | mx = add get apu_l0 0 mul eta d0 |
| 624 | for r ulong v bound 65536 |
| 625 | dr = get apu_dl r |
| 626 | x = add get apu_l0 r mul eta dr |
| 627 | if gt x mx |
| 628 | mx = x |
| 629 | end |
| 630 | end |
| 631 | sm = 0.0f64 |
| 632 | for r2 ulong v bound 65536 |
| 633 | dr2 = get apu_dl r2 |
| 634 | x2 = add get apu_l0 r2 mul eta dr2 |
| 635 | sm = add sm fexp sub x2 mx |
| 636 | end |
| 637 | dy = get apu_dl y |
| 638 | ly = add get apu_l0 y mul eta dy |
| 639 | ret sub add mx flog sm ly |
| 640 | end |
| 641 | |
| 642 | fn apu_add_dz_row ba ulong pa f64 d ulong to void |
| 643 | for j2 ulong d bound 2048 |
| 644 | vb = get apu_b add ba j2 |
| 645 | cur = get apu_dz j2 |
| 646 | put apu_dz j2 add cur mul pa vb |
| 647 | end |
| 648 | end |
| 649 | |
| 650 | fn apu_eval_dz d ulong f_exp ulong sid ulong to void |
| 651 | for j ulong d bound 2048 |
| 652 | put apu_dz j 0.0f64 |
| 653 | end |
| 654 | nact = apu_feat_z d f_exp sid |
| 655 | for ia ulong nact bound 640 |
| 656 | a = get apu_acti ia |
| 657 | pa = get apu_actv ia |
| 658 | apu_add_dz_row mul a d pa d |
| 659 | end |
| 660 | end |
| 661 | |
| 662 | fn apu_init_etas to void |
| 663 | put apu_etav 0 0.0f64 |
| 664 | put apu_etav 1 0.05f64 |
| 665 | put apu_etav 2 0.15f64 |
| 666 | put apu_etav 3 0.35f64 |
| 667 | put apu_etav 4 0.7f64 |
| 668 | put apu_etav 5 1.0f64 |
| 669 | put apu_etav 6 1.5f64 |
| 670 | put apu_etav 7 2.5f64 |
| 671 | for k ulong 8 |
| 672 | put apu_etas k 0.0f64 |
| 673 | end |
| 674 | end |
| 675 | |
| 676 | fn apu_cal_sample ci ulong f_exp ulong sid ulong to void |
| 677 | d = get og 2 |
| 678 | zo = mul ci d |
| 679 | for j ulong d bound 2048 |
| 680 | vz = get apu_calz add zo j |
| 681 | put apu_z j vz |
| 682 | end |
| 683 | apu_eval_dz d f_exp sid |
| 684 | v = get og 0 |
| 685 | apu_pair_logits v d |
| 686 | yt = get apu_caly ci |
| 687 | for ei ulong 8 |
| 688 | ls = apu_ce_lin v yt get apu_etav ei |
| 689 | put apu_etas ei add get apu_etas ei ls |
| 690 | end |
| 691 | end |
| 692 | |
| 693 | note Calibrate best_eta over ncal samples in apu_calz/apu_caly; scales apu_b *= best_eta. |
| 694 | note Writes base_cal_u6 -> apu_hdr[0], cryst_cal_u6 -> apu_hdr[1]. |
| 695 | fn apu_calibrate ncal ulong f_exp ulong sid ulong to void |
| 696 | apu_init_etas |
| 697 | for ci ulong ncal bound 128 |
| 698 | apu_cal_sample ci f_exp sid |
| 699 | end |
| 700 | fn_nc = f64 ncal |
| 701 | base_l = div get apu_etas 0 fn_nc |
| 702 | best_l = base_l |
| 703 | best_eta = 0.0f64 |
| 704 | for ei2 ulong 7 |
| 705 | cl = div get apu_etas add ei2 1 fn_nc |
| 706 | if lt cl best_l |
| 707 | best_l = cl |
| 708 | best_eta = get apu_etav add ei2 1 |
| 709 | end |
| 710 | end |
| 711 | nw = mul f_exp get og 2 |
| 712 | for wi ulong nw bound 1310720 |
| 713 | put apu_b wi mul get apu_b wi best_eta |
| 714 | end |
| 715 | put apu_hdr 0 ulong rmic base_l |
| 716 | put apu_hdr 1 ulong rmic best_l |
| 717 | end |
| 718 | |
| 719 | note ---- Canonical OLLMSP02 Atomic Writer (from apu_pbuf path, W4 or f64) ---- |
| 720 | |
| 721 | fn apu_wopen_pbuf plen ulong to void |
| 722 | for i ulong plen bound 1024 |
| 723 | put apu_ptmp i get apu_pbuf i |
| 724 | end |
| 725 | put apu_ptmp plen 46 |
| 726 | put apu_ptmp add plen 1 116 |
| 727 | put apu_ptmp add plen 2 109 |
| 728 | put apu_ptmp add plen 3 112 |
| 729 | fd = int fopen apu_ptmp 0 add plen 4 577 420 |
| 730 | if lt fd 0 |
| 731 | exit 70 |
| 732 | end |
| 733 | put apu_wfd 0 fd |
| 734 | end |
| 735 | |
| 736 | fn apu_wput pos ulong n ulong to void |
| 737 | fd = get apu_wfd 0 |
| 738 | p = ulong 0 |
| 739 | while lt p n bound 65536 |
| 740 | w = pwrite fd apu_io p sub n p long add pos p |
| 741 | if le w 0 |
| 742 | exit 70 |
| 743 | end |
| 744 | p = add p ulong w |
| 745 | end |
| 746 | end |
| 747 | |
| 748 | fn apu_wend_pbuf plen ulong to void |
| 749 | fd = get apu_wfd 0 |
| 750 | if ne fsync fd 0 |
| 751 | exit 70 |
| 752 | end |
| 753 | if ne fclose fd 0 |
| 754 | exit 70 |
| 755 | end |
| 756 | put apu_pk 0 2 |
| 757 | put apu_pk 1 0 |
| 758 | put apu_pk 2 uchar and add plen 4 255 |
| 759 | put apu_pk 3 uchar shr add plen 4 8 |
| 760 | for i ulong add plen 4 bound 1028 |
| 761 | put apu_pk add 4 i get apu_ptmp i |
| 762 | end |
| 763 | q = add 8 plen |
| 764 | put apu_pk q uchar and plen 255 |
| 765 | put apu_pk add q 1 uchar shr plen 8 |
| 766 | for j ulong plen bound 1024 |
| 767 | put apu_pk add add q 2 j get apu_pbuf j |
| 768 | end |
| 769 | r = renames apu_pk 0 add add q 2 plen 0 |
| 770 | if ne r 0 |
| 771 | exit 70 |
| 772 | end |
| 773 | end |
| 774 | |
| 775 | fn apu_q4_nib v f64 sc f64 to ulong |
| 776 | if le sc 0.0f64 |
| 777 | ret 8 |
| 778 | end |
| 779 | r = div v sc |
| 780 | if gt r 7.0f64 |
| 781 | r = 7.0f64 |
| 782 | end |
| 783 | if lt r -7.0f64 |
| 784 | r = -7.0f64 |
| 785 | end |
| 786 | if ge r 0.0f64 |
| 787 | ret ulong add 8 long add r 0.5f64 |
| 788 | end |
| 789 | ret ulong add 8 long sub r 0.5f64 |
| 790 | end |
| 791 | |
| 792 | fn apu_q4_pair off ulong cnt ulong p ulong sc f64 to uchar |
| 793 | q0 = ulong 8 |
| 794 | q1 = ulong 8 |
| 795 | i0 = mul p 2 |
| 796 | if lt i0 cnt |
| 797 | q0 = apu_q4_nib get apu_b add off i0 sc |
| 798 | end |
| 799 | i1 = add i0 1 |
| 800 | if lt i1 cnt |
| 801 | q1 = apu_q4_nib get apu_b add off i1 sc |
| 802 | end |
| 803 | ret uchar or and q0 15 shl and q1 15 4 |
| 804 | end |
| 805 | |
| 806 | fn apu_q4_blk off ulong cnt ulong bo ulong to void |
| 807 | mx = 0.0f64 |
| 808 | for i ulong cnt bound 32 |
| 809 | v = fabs get apu_b add off i |
| 810 | if gt v mx |
| 811 | mx = v |
| 812 | end |
| 813 | end |
| 814 | sc32 = f32 div mx 7.0f64 |
| 815 | apu_put4_io bo bitcast uint sc32 |
| 816 | sc = f64 sc32 |
| 817 | for p ulong 16 |
| 818 | put apu_io add bo add 4 p apu_q4_pair off cnt p sc |
| 819 | end |
| 820 | end |
| 821 | |
| 822 | fn apu_wr_w4_blk nw ulong bd ulong bcnt ulong to void |
| 823 | for bi ulong bcnt bound 2048 |
| 824 | e0 = mul add bd bi 32 |
| 825 | ec = sub nw e0 |
| 826 | if gt ec 32 |
| 827 | ec = ulong 32 |
| 828 | end |
| 829 | apu_q4_blk e0 ec mul bi 20 |
| 830 | end |
| 831 | end |
| 832 | |
| 833 | fn apu_wr_w4 nw ulong pos ulong ph ulong to ulong |
| 834 | nblk = div add nw 31 32 |
| 835 | bd = ulong 0 |
| 836 | p = pos |
| 837 | h = ph |
| 838 | while lt bd nblk bound 65536 |
| 839 | bcnt = sub nblk bd |
| 840 | if gt bcnt 2048 |
| 841 | bcnt = ulong 2048 |
| 842 | end |
| 843 | apu_wr_w4_blk nw bd bcnt |
| 844 | nb = mul bcnt 20 |
| 845 | h = apu_fnv_io h 0 nb |
| 846 | apu_wput p nb |
| 847 | p = add p nb |
| 848 | bd = add bd bcnt |
| 849 | end |
| 850 | put apu_hdr 7 p |
| 851 | ret h |
| 852 | end |
| 853 | |
| 854 | fn apu_wr_f64_chunk dn ulong cnt ulong to void |
| 855 | for k ulong cnt bound 4096 |
| 856 | apu_put8_io mul k 8 bitcast ulong get apu_b add dn k |
| 857 | end |
| 858 | end |
| 859 | |
| 860 | fn apu_wr_f64 nw ulong pos ulong ph ulong to ulong |
| 861 | dn = ulong 0 |
| 862 | p = pos |
| 863 | h = ph |
| 864 | while lt dn nw bound 65536 |
| 865 | cnt = sub nw dn |
| 866 | if gt cnt 4096 |
| 867 | cnt = ulong 4096 |
| 868 | end |
| 869 | apu_wr_f64_chunk dn cnt |
| 870 | nb = mul cnt 8 |
| 871 | h = apu_fnv_io h 0 nb |
| 872 | apu_wput p nb |
| 873 | p = add p nb |
| 874 | dn = add dn cnt |
| 875 | end |
| 876 | put apu_hdr 7 p |
| 877 | ret h |
| 878 | end |
| 879 | |
| 880 | fn apu_wr_cent d ulong to ulong |
| 881 | for j ulong d bound 2048 |
| 882 | apu_put8_io mul j 8 bitcast ulong get apu_c j |
| 883 | end |
| 884 | cb = mul d 8 |
| 885 | ph = apu_fnv_io ulong 14695981039346656037 0 cb |
| 886 | apu_wput OSP2_HDR cb |
| 887 | ret ph |
| 888 | end |
| 889 | |
| 890 | note Atomically write OLLMSP02 pack to apu_pbuf[0..plen). Returns total file bytes (80 + payload_bytes). |
| 891 | fn apu_write_sp2 plen ulong base_h ulong f_exp ulong sid ulong qbits ulong to ulong |
| 892 | d = get og 2 |
| 893 | apu_wopen_pbuf plen |
| 894 | ph = apu_wr_cent d |
| 895 | if eq qbits 64 |
| 896 | ph = apu_wr_f64 mul f_exp d add OSP2_HDR mul d 8 ph |
| 897 | else |
| 898 | ph = apu_wr_w4 mul f_exp d add OSP2_HDR mul d 8 ph |
| 899 | end |
| 900 | apu_put8_io 0 OSP2_MAG |
| 901 | apu_put8_io 8 base_h |
| 902 | apu_put8_io 16 get og 5 |
| 903 | apu_put8_io 24 d |
| 904 | apu_put8_io 32 f_exp |
| 905 | apu_put8_io 40 sid |
| 906 | apu_put8_io 48 qbits |
| 907 | apu_put8_io 56 sub get apu_hdr 7 OSP2_HDR |
| 908 | apu_put8_io 64 ph |
| 909 | apu_put8_io 72 apu_fnv_io ulong 14695981039346656037 0 72 |
| 910 | apu_wput 0 OSP2_HDR |
| 911 | apu_wend_pbuf plen |
| 912 | ret get apu_hdr 7 |
| 913 | end |
| 914 | |
| 915 | note ---- Live Merkle + 1-Bit Tamper (rc=61) + Held-Out W4 Verifier ---- |
| 916 | |
| 917 | fn apu_rd_exact fd int pos ulong n ulong to bool |
| 918 | p = ulong 0 |
| 919 | while lt p n bound 65536 |
| 920 | r = pread fd apu_io p sub n p long add pos p |
| 921 | if le r 0 |
| 922 | ret false |
| 923 | end |
| 924 | p = add p ulong r |
| 925 | end |
| 926 | ret true |
| 927 | end |
| 928 | |
| 929 | fn apu_dq4_pair off ulong cnt ulong p ulong bt ulong sc f64 to void |
| 930 | i0 = mul p 2 |
| 931 | if lt i0 cnt |
| 932 | q0 = f64 sub long and bt 15 8 |
| 933 | put apu_b add off i0 mul q0 sc |
| 934 | end |
| 935 | i1 = add i0 1 |
| 936 | if lt i1 cnt |
| 937 | q1 = f64 sub long and shr bt 4 15 8 |
| 938 | put apu_b add off i1 mul q1 sc |
| 939 | end |
| 940 | end |
| 941 | |
| 942 | fn apu_dq4_blk bo ulong off ulong cnt ulong to void |
| 943 | sc = f64 bitcast f32 apu_get4_io bo |
| 944 | for p ulong 16 |
| 945 | bt = ulong get apu_io add bo add 4 p |
| 946 | apu_dq4_pair off cnt p bt sc |
| 947 | end |
| 948 | end |
| 949 | |
| 950 | fn apu_vfy_w4_blk nw ulong bd ulong bcnt ulong to void |
| 951 | for bi ulong bcnt bound 2048 |
| 952 | e0 = mul add bd bi 32 |
| 953 | ec = sub nw e0 |
| 954 | if gt ec 32 |
| 955 | ec = ulong 32 |
| 956 | end |
| 957 | apu_dq4_blk mul bi 20 e0 ec |
| 958 | end |
| 959 | end |
| 960 | |
| 961 | fn apu_vfy_read_w4 fd int nw ulong pos ulong ph ulong to ulong |
| 962 | nblk = div add nw 31 32 |
| 963 | bd = ulong 0 |
| 964 | p = pos |
| 965 | h = ph |
| 966 | while lt bd nblk bound 65536 |
| 967 | bcnt = sub nblk bd |
| 968 | if gt bcnt 2048 |
| 969 | bcnt = ulong 2048 |
| 970 | end |
| 971 | nb = mul bcnt 20 |
| 972 | if not apu_rd_exact fd p nb |
| 973 | put apu_hdr 10 60 |
| 974 | ret 0 |
| 975 | end |
| 976 | h = apu_fnv_io h 0 nb |
| 977 | put apu_tph 0 apu_fnv_io get apu_tph 0 0 nb |
| 978 | apu_vfy_w4_blk nw bd bcnt |
| 979 | p = add p nb |
| 980 | bd = add bd bcnt |
| 981 | end |
| 982 | put apu_hdr 7 p |
| 983 | ret h |
| 984 | end |
| 985 | |
| 986 | fn apu_vfy_f64_chunk dn ulong cnt ulong to void |
| 987 | for k ulong cnt bound 4096 |
| 988 | put apu_b add dn k bitcast f64 apu_get8_io mul k 8 |
| 989 | end |
| 990 | end |
| 991 | |
| 992 | fn apu_vfy_read_f64 fd int nw ulong pos ulong ph ulong to ulong |
| 993 | dn = ulong 0 |
| 994 | p = pos |
| 995 | h = ph |
| 996 | while lt dn nw bound 65536 |
| 997 | cnt = sub nw dn |
| 998 | if gt cnt 4096 |
| 999 | cnt = ulong 4096 |
| 1000 | end |
| 1001 | nb = mul cnt 8 |
| 1002 | if not apu_rd_exact fd p nb |
| 1003 | put apu_hdr 10 60 |
| 1004 | ret 0 |
| 1005 | end |
| 1006 | h = apu_fnv_io h 0 nb |
| 1007 | put apu_tph 0 apu_fnv_io get apu_tph 0 0 nb |
| 1008 | apu_vfy_f64_chunk dn cnt |
| 1009 | p = add p nb |
| 1010 | dn = add dn cnt |
| 1011 | end |
| 1012 | put apu_hdr 7 p |
| 1013 | ret h |
| 1014 | end |
| 1015 | |
| 1016 | fn apu_vfy_hdr fd int base_h ulong to ulong |
| 1017 | if not apu_rd_exact fd 0 OSP2_HDR |
| 1018 | ret 60 |
| 1019 | end |
| 1020 | if ne apu_get8_io 0 OSP2_MAG |
| 1021 | ret 60 |
| 1022 | end |
| 1023 | if ne apu_fnv_io ulong 14695981039346656037 0 72 apu_get8_io 72 |
| 1024 | ret 61 |
| 1025 | end |
| 1026 | for k ulong 10 |
| 1027 | put apu_hdr k apu_get8_io mul k 8 |
| 1028 | end |
| 1029 | if ne get apu_hdr 1 base_h |
| 1030 | ret 62 |
| 1031 | end |
| 1032 | if ne get apu_hdr 2 get og 5 |
| 1033 | ret 62 |
| 1034 | end |
| 1035 | if ne get apu_hdr 3 get og 2 |
| 1036 | ret 62 |
| 1037 | end |
| 1038 | ret 0 |
| 1039 | end |
| 1040 | |
| 1041 | fn apu_vfy_cent fd int d ulong to ulong |
| 1042 | cb = mul d 8 |
| 1043 | if not apu_rd_exact fd OSP2_HDR cb |
| 1044 | put apu_hdr 10 60 |
| 1045 | ret 0 |
| 1046 | end |
| 1047 | ph = apu_fnv_io ulong 14695981039346656037 0 cb |
| 1048 | orig_b0 = get apu_io 0 |
| 1049 | put apu_io 0 uchar xor ulong orig_b0 1 |
| 1050 | put apu_tph 0 apu_fnv_io ulong 14695981039346656037 0 cb |
| 1051 | put apu_io 0 orig_b0 |
| 1052 | for j ulong d bound 2048 |
| 1053 | put apu_c j bitcast f64 apu_get8_io mul j 8 |
| 1054 | end |
| 1055 | ret ph |
| 1056 | end |
| 1057 | |
| 1058 | note Verify Merkle header + full payload + live 1-bit tamper check on apu_pbuf[0..plen). |
| 1059 | note Loads dequantized W_down into apu_b and centroid into apu_c. |
| 1060 | note Returns 0 on full PASS (with apu_hdr[11]=61 proving 1-bit tamper fails closed with rc=61), else error code. |
| 1061 | fn apu_verify_pack plen ulong base_h ulong to ulong |
| 1062 | put apu_hdr 10 0 |
| 1063 | put apu_hdr 11 0 |
| 1064 | fd = int fopen apu_pbuf 0 plen 0 0 |
| 1065 | if lt fd 0 |
| 1066 | ret 60 |
| 1067 | end |
| 1068 | rc = apu_vfy_hdr fd base_h |
| 1069 | if ne rc 0 |
| 1070 | c0 = fclose fd |
| 1071 | ret rc |
| 1072 | end |
| 1073 | d = get og 2 |
| 1074 | ph = apu_vfy_cent fd d |
| 1075 | if eq get apu_hdr 10 0 |
| 1076 | nw = mul get apu_hdr 4 d |
| 1077 | pos = add OSP2_HDR mul d 8 |
| 1078 | if eq get apu_hdr 6 64 |
| 1079 | ph = apu_vfy_read_f64 fd nw pos ph |
| 1080 | else |
| 1081 | ph = apu_vfy_read_w4 fd nw pos ph |
| 1082 | end |
| 1083 | end |
| 1084 | c5 = fclose fd |
| 1085 | if ne get apu_hdr 10 0 |
| 1086 | ret get apu_hdr 10 |
| 1087 | end |
| 1088 | if ne ph get apu_hdr 8 |
| 1089 | ret 61 |
| 1090 | end |
| 1091 | if ne get apu_tph 0 ph |
| 1092 | put apu_hdr 11 61 |
| 1093 | ret 0 |
| 1094 | end |
| 1095 | put apu_hdr 11 0 |
| 1096 | ret 61 |
| 1097 | end |
| 1098 | |
| 1099 | fn apu_eval_one hi ulong f_exp ulong sid ulong to void |
| 1100 | d = get og 2 |
| 1101 | zo = mul hi d |
| 1102 | for j ulong d bound 2048 |
| 1103 | put apu_z j get apu_calz add zo j |
| 1104 | end |
| 1105 | apu_eval_dz d f_exp sid |
| 1106 | apu_pair_logits get og 0 d |
| 1107 | end |
| 1108 | |
| 1109 | note Evaluate loaded W4/f64 pack (in apu_b) on nheld samples in apu_calz/apu_caly. |
| 1110 | note Writes base_eval_u6 -> apu_hdr[0], woven_eval_u6 -> apu_hdr[1]. |
| 1111 | fn apu_eval_loaded nheld ulong f_exp ulong sid ulong to void |
| 1112 | v = get og 0 |
| 1113 | bs = 0.0f64 |
| 1114 | ws = 0.0f64 |
| 1115 | for hi ulong nheld bound 128 |
| 1116 | apu_eval_one hi f_exp sid |
| 1117 | yt = get apu_caly hi |
| 1118 | bs = add bs apu_ce_lin v yt 0.0f64 |
| 1119 | ws = add ws apu_ce_lin v yt 1.0f64 |
| 1120 | end |
| 1121 | put apu_hdr 0 ulong rmic div bs f64 nheld |
| 1122 | put apu_hdr 1 ulong rmic div ws f64 nheld |
| 1123 | end |
| 1124 | |
| 1125 | note ---- Machine-Readable Telemetry Printers ---- |
| 1126 | |
| 1127 | text apu_t_wave "APU_WAVE " |
| 1128 | text apu_t_strand "APU_STRAND " |
| 1129 | text apu_t_sum "APU_SUMMARY " |
| 1130 | text apu_t_ov "APU_OVERLAP " |
| 1131 | |
| 1132 | fn apu_print_wave wid ulong nsh ulong gpu_ms ulong rdep ulong act_c ulong act_v ulong to void |
| 1133 | apu_puts apu_t_wave |
| 1134 | oputu wid |
| 1135 | oputc 32 |
| 1136 | oputu nsh |
| 1137 | oputc 32 |
| 1138 | oputu gpu_ms |
| 1139 | oputc 32 |
| 1140 | oputu rdep |
| 1141 | oputc 32 |
| 1142 | oputu act_c |
| 1143 | oputc 32 |
| 1144 | oputu act_v |
| 1145 | oputc 32 |
| 1146 | oputu get apu_cfg 7 |
| 1147 | oputc 10 |
| 1148 | oflush |
| 1149 | end |
| 1150 | |
| 1151 | fn apu_print_strand1 sid ulong toks ulong bcal ulong ccal ulong bev ulong wev ulong to void |
| 1152 | apu_puts apu_t_strand |
| 1153 | oputu sid |
| 1154 | oputc 32 |
| 1155 | oputu toks |
| 1156 | oputc 32 |
| 1157 | oputu bcal |
| 1158 | oputc 32 |
| 1159 | oputu ccal |
| 1160 | oputc 32 |
| 1161 | oputu bev |
| 1162 | oputc 32 |
| 1163 | oputu wev |
| 1164 | end |
| 1165 | |
| 1166 | fn apu_print_strand2 pb ulong gms ulong cms ulong vms ulong trc ulong anov ulong to void |
| 1167 | oputc 32 |
| 1168 | oputu pb |
| 1169 | oputc 32 |
| 1170 | oputu gms |
| 1171 | oputc 32 |
| 1172 | oputu cms |
| 1173 | oputc 32 |
| 1174 | oputu vms |
| 1175 | oputc 32 |
| 1176 | oputu trc |
| 1177 | oputc 32 |
| 1178 | oputu anov |
| 1179 | oputc 10 |
| 1180 | oflush |
| 1181 | end |
| 1182 | |
| 1183 | fn apu_print_overlap wall_ms ulong w_eff ulong gpu_ms ulong cpu_ms ulong ov_x1000 ulong to void |
| 1184 | span_ms = ulong 0 |
| 1185 | if gt get apu_cfg 22 get apu_cfg 21 |
| 1186 | span_ms = div sub get apu_cfg 22 get apu_cfg 21 1000000 |
| 1187 | end |
| 1188 | pipe_ov = div mul add gpu_ms span_ms 1000 w_eff |
| 1189 | apu_puts apu_t_ov |
| 1190 | oputu wall_ms |
| 1191 | oputc 32 |
| 1192 | oputu gpu_ms |
| 1193 | oputc 32 |
| 1194 | oputu span_ms |
| 1195 | oputc 32 |
| 1196 | oputu cpu_ms |
| 1197 | oputc 32 |
| 1198 | oputu pipe_ov |
| 1199 | oputc 32 |
| 1200 | oputu ov_x1000 |
| 1201 | oputc 10 |
| 1202 | oflush |
| 1203 | end |
| 1204 | |
| 1205 | fn apu_print_summary wall_ms ulong to void |
| 1206 | ns = get apu_cfg 0 |
| 1207 | w_eff = wall_ms |
| 1208 | if eq w_eff 0 |
| 1209 | w_eff = ulong 1 |
| 1210 | end |
| 1211 | gpu_ms = div get apu_cfg 13 1000000 |
| 1212 | cpu_ms = div get apu_cfg 14 1000000 |
| 1213 | ov_x1000 = div mul add gpu_ms cpu_ms 1000 w_eff |
| 1214 | m_bev = ulong 0 |
| 1215 | m_wev = ulong 0 |
| 1216 | if gt ns 0 |
| 1217 | m_bev = div get apu_cfg 17 ns |
| 1218 | m_wev = div get apu_cfg 18 ns |
| 1219 | end |
| 1220 | apu_puts apu_t_sum |
| 1221 | oputu ns |
| 1222 | oputc 32 |
| 1223 | oputu add get apu_cfg 5 add get apu_cfg 8 get apu_cfg 9 |
| 1224 | oputc 32 |
| 1225 | oputu wall_ms |
| 1226 | oputc 32 |
| 1227 | oputu gpu_ms |
| 1228 | oputc 32 |
| 1229 | oputu cpu_ms |
| 1230 | oputc 32 |
| 1231 | oputu div mul ns 1000000 w_eff |
| 1232 | oputc 32 |
| 1233 | oputu ov_x1000 |
| 1234 | oputc 32 |
| 1235 | oputu get apu_cfg 15 |
| 1236 | oputc 32 |
| 1237 | oputu m_bev |
| 1238 | oputc 32 |
| 1239 | oputu m_wev |
| 1240 | oputc 32 |
| 1241 | oputl sub long m_bev long m_wev |
| 1242 | oputc 32 |
| 1243 | oputu get apu_cfg 16 |
| 1244 | oputc 32 |
| 1245 | oputu get apu_cfg 7 |
| 1246 | oputc 10 |
| 1247 | apu_print_overlap wall_ms w_eff gpu_ms cpu_ms ov_x1000 |
| 1248 | end |
| 1249 | |
| 1250 | fn apu_parse_odir to void |
| 1251 | n = oargp 4 |
| 1252 | if gt n 500 |
| 1253 | exit 2 |
| 1254 | end |
| 1255 | for i ulong n bound 512 |
| 1256 | put apu_odir i get oarg i |
| 1257 | end |
| 1258 | put apu_odir n 0 |
| 1259 | put apu_odirn 0 n |
| 1260 | end |
| 1261 | |
| 1262 | fn apu_parse_cli to void |
| 1263 | na = oargc |
| 1264 | if and ne na 9 ne na 10 |
| 1265 | exit 2 |
| 1266 | end |
| 1267 | ockloop 2 |
| 1268 | apu_parse_odir |
| 1269 | for k ulong 32 |
| 1270 | put apu_cfg k 0 |
| 1271 | end |
| 1272 | f_exp = oargu 5 |
| 1273 | max_t = oargu 7 |
| 1274 | qbits = oargu 8 |
| 1275 | wrk = ulong 16 |
| 1276 | if eq na 10 |
| 1277 | wrk = oargu 9 |
| 1278 | end |
| 1279 | if or lt f_exp 2 or gt f_exp APU_MAX_F eq max_t 0 |
| 1280 | exit 2 |
| 1281 | end |
| 1282 | if or and ne qbits 4 ne qbits 64 or eq wrk 0 gt wrk 16 |
| 1283 | exit 2 |
| 1284 | end |
| 1285 | if gt get og 2 APU_MAX_D |
| 1286 | exit 12 |
| 1287 | end |
| 1288 | put apu_cfg 1 f_exp |
| 1289 | put apu_cfg 2 oargu 6 |
| 1290 | put apu_cfg 3 max_t |
| 1291 | put apu_cfg 4 qbits |
| 1292 | put apu_cfg 5 wrk |
| 1293 | put apu_cfg 6 osp_basehash get og 5 get og 33 |
| 1294 | ntw = div add max_t sub get og 1 1 get og 1 |
| 1295 | if eq ntw 0 |
| 1296 | ntw = ulong 1 |
| 1297 | end |
| 1298 | if gt ntw 32 |
| 1299 | ntw = ulong 32 |
| 1300 | end |
| 1301 | put apu_cfg 10 ntw |
| 1302 | put apu_cfg 11 1 |
| 1303 | end |
| 1304 | |
| 1305 | fn cmdapuweave to void |
| 1306 | apu_parse_cli |
| 1307 | put apu_cfg 19 onsnow |
| 1308 | apu_fabric_run |
| 1309 | w_ms = div sub onsnow get apu_cfg 19 1000000 |
| 1310 | apu_print_summary w_ms |
| 1311 | if ne get apu_cfg 16 get apu_cfg 0 |
| 1312 | exit 61 |
| 1313 | end |
| 1314 | end |