omega-helix
This project has not described itself yet.
Brought by drakestapleton
omega-helix / native / cli.om
| 1 | note SPDX-License-Identifier: AGPL-3.0-only |
| 2 | note omegallm cli.om (aien-train#52): the ollm commands (CONTRACT section 7). The platform unit |
| 3 | note (lio.om or eio.om) follows it and holds main. |
| 4 | note Command words are matched as little-endian u64 packings of their bytes (Wolfram |
| 5 | note FromDigits[Reverse[ToCharacterCode[w]],256]): prep 1885696624, init 1953066601, |
| 6 | note grad 1684107879, train 474214396532, eval 1818326629, base 1702060386, sample 111516417089907, |
| 7 | note gcheck 118074647274343, mathck 118074698391917, adamck 118074781033569; v1: bpefit |
| 8 | note 127996038312034, bpeenc 109325798699106, bpedec 109287127216226 (bpe.om); serve 435778905459 |
| 9 | note (serve.om on aarch64, noserve.om elsewhere). Fine-tune: tune 1701737844. |
| 10 | note Exit codes: 0 ok; 2 usage; 10-15 checkpoint refusals (ckpt.om); 17 a looped checkpoint given |
| 11 | note to a command that does not run loops (grow and the specialist commands; init, train, eval, |
| 12 | note sample, grad, gcheck, tune, kvcmp, serve and the GB10 commands run them); 20 data file shorter than |
| 13 | note T + 1 bytes; 21 data over 2^25 bytes or OFFS not exactly 8 B bytes; 22 an OFFS offset with |
| 14 | note o + T >= N; 23 eval W = 0 or W T + 1 > M; 24 prep story over 999999 bytes; 40 gcheck over |
| 15 | note tolerance; 41 adamck over tolerance; 42 mathck over tolerance; 70 I/O. |
| 16 | |
| 17 | fn ocmdw to ulong |
| 18 | r = oargr 1 |
| 19 | if or le r 0 gt r 8 |
| 20 | ret 0 |
| 21 | end |
| 22 | v = ulong 0 |
| 23 | for k ulong ulong r |
| 24 | v = or v shl ulong get oarg k mul k 8 |
| 25 | end |
| 26 | ret v |
| 27 | end |
| 28 | |
| 29 | fn ocmdlo to ulong |
| 30 | r = oargr 1 |
| 31 | if or le r 8 gt r 16 |
| 32 | ret 0 |
| 33 | end |
| 34 | v = ulong 0 |
| 35 | for k ulong 8 |
| 36 | v = or v shl ulong get oarg k mul k 8 |
| 37 | end |
| 38 | ret v |
| 39 | end |
| 40 | |
| 41 | fn ocmdhi to ulong |
| 42 | r = oargr 1 |
| 43 | if or le r 8 gt r 16 |
| 44 | ret 0 |
| 45 | end |
| 46 | v = ulong 0 |
| 47 | n = sub ulong r 8 |
| 48 | for k ulong n |
| 49 | v = or v shl ulong get oarg add 8 k mul k 8 |
| 50 | end |
| 51 | ret v |
| 52 | end |
| 53 | |
| 54 | note run the command named by argument 1; the platform main checks that it exists. ogcmd takes the |
| 55 | note GPU commands on the gb10 target (gpu/gg.om) and none elsewhere (nogpu.om). |
| 56 | fn ocmd to int |
| 57 | w = ocmdw |
| 58 | if ogcmd w |
| 59 | ret 0 |
| 60 | end |
| 61 | if eq w 1885696624 |
| 62 | cmdprep |
| 63 | ret 0 |
| 64 | end |
| 65 | if eq w 127996038312034 |
| 66 | cmdbpfit |
| 67 | ret 0 |
| 68 | end |
| 69 | if eq w 109325798699106 |
| 70 | cmdbpenc |
| 71 | ret 0 |
| 72 | end |
| 73 | if eq w 109287127216226 |
| 74 | cmdbpdec |
| 75 | ret 0 |
| 76 | end |
| 77 | if eq w 1953066601 |
| 78 | cmdinit |
| 79 | ret 0 |
| 80 | end |
| 81 | if eq w 8391157623388991081 |
| 82 | cmdinitfast |
| 83 | ret 0 |
| 84 | end |
| 85 | if eq w 128038904163171 |
| 86 | cmdckfast |
| 87 | ret 0 |
| 88 | end |
| 89 | if eq w 224250981219 |
| 90 | cmdckf64 |
| 91 | ret 0 |
| 92 | end |
| 93 | if eq w 1684107879 |
| 94 | cmdgrad |
| 95 | ret 0 |
| 96 | end |
| 97 | if eq w 474214396532 |
| 98 | cmdtrain |
| 99 | ret 0 |
| 100 | end |
| 101 | if eq w 121398885512310 |
| 102 | cmdvtrain |
| 103 | ret 0 |
| 104 | end |
| 105 | if eq w 1818326629 |
| 106 | cmdeval |
| 107 | ret 0 |
| 108 | end |
| 109 | if eq w 465491617142 |
| 110 | cmdveval |
| 111 | ret 0 |
| 112 | end |
| 113 | if eq w 1702060386 |
| 114 | cmdbase |
| 115 | ret 0 |
| 116 | end |
| 117 | if eq w 111516417089907 |
| 118 | cmdsamp |
| 119 | ret 0 |
| 120 | end |
| 121 | if eq w 28548202775016302 |
| 122 | cmdnsamp |
| 123 | ret 0 |
| 124 | end |
| 125 | if eq w 118074647274343 |
| 126 | cmdgchk |
| 127 | ret 0 |
| 128 | end |
| 129 | if eq w 118074698391917 |
| 130 | cmdmath |
| 131 | ret 0 |
| 132 | end |
| 133 | if eq w 118074781033569 |
| 134 | cmdadamck |
| 135 | ret 0 |
| 136 | end |
| 137 | if eq w 435778905459 |
| 138 | cmdserve |
| 139 | ret 0 |
| 140 | end |
| 141 | if eq w 1701737844 |
| 142 | cmdtune |
| 143 | ret 0 |
| 144 | end |
| 145 | note grow: append initially silent residual layers and restart training. |
| 146 | if eq w 2003792487 |
| 147 | cmdgrow |
| 148 | ret 0 |
| 149 | end |
| 150 | note kvcmp (ham/ollm-speed) 482871572075 |
| 151 | if eq w 482871572075 |
| 152 | cmdkvcmp |
| 153 | ret 0 |
| 154 | end |
| 155 | note adaptcmp 8101240656462177377 (adapteval and adaptsamp are 9 bytes: under w = 0 below) |
| 156 | if eq w 8101240656462177377 |
| 157 | cmdadcmp |
| 158 | ret 0 |
| 159 | end |
| 160 | if eq w 112624400429684 |
| 161 | cmdtrainf |
| 162 | ret 0 |
| 163 | end |
| 164 | if eq w 30506458620916852 |
| 165 | cmdttteval |
| 166 | ret 0 |
| 167 | end |
| 168 | if eq w 7809653406635421540 |
| 169 | cmddolaeval |
| 170 | ret 0 |
| 171 | end |
| 172 | if eq w 31645462707860596 |
| 173 | cmdtttsamp |
| 174 | ret 0 |
| 175 | end |
| 176 | if eq w 500236120179 |
| 177 | cmdspext |
| 178 | ret 0 |
| 179 | end |
| 180 | if eq w 482921771123 |
| 181 | cmdspapp |
| 182 | ret 0 |
| 183 | end |
| 184 | if eq w 123615088701555 |
| 185 | if and ge oargc 8 sp2_is_sp2 7 |
| 186 | cmdspwsamp |
| 187 | else |
| 188 | cmdspsamp |
| 189 | end |
| 190 | ret 0 |
| 191 | end |
| 192 | if eq w 119165853986931 |
| 193 | cmdspeval |
| 194 | ret 0 |
| 195 | end |
| 196 | if eq w 111499018330982 |
| 197 | cmdfcache |
| 198 | ret 0 |
| 199 | end |
| 200 | if eq w 27978647094784628 |
| 201 | cmdtrainfc |
| 202 | ret 0 |
| 203 | end |
| 204 | if eq w 30792271479792486 |
| 205 | cmdfcachem |
| 206 | ret 0 |
| 207 | end |
| 208 | if eq w 28542713673379955 |
| 209 | cmdspmerge |
| 210 | ret 0 |
| 211 | end |
| 212 | if eq w 30227092724740211 |
| 213 | cmdspstack |
| 214 | ret 0 |
| 215 | end |
| 216 | if eq w 31369549780578919 |
| 217 | cmdgrowpro |
| 218 | ret 0 |
| 219 | end |
| 220 | if eq w 495773380723 |
| 221 | cmdspens |
| 222 | ret 0 |
| 223 | end |
| 224 | if eq w 123598092333171 |
| 225 | cmdspclip |
| 226 | ret 0 |
| 227 | end |
| 228 | if eq w 28273260578173043 |
| 229 | cmdspguard |
| 230 | ret 0 |
| 231 | end |
| 232 | if eq w 28542713840693363 |
| 233 | cmdspforge |
| 234 | ret 0 |
| 235 | end |
| 236 | if eq w 32778062745792627 |
| 237 | cmdspcryst |
| 238 | ret 0 |
| 239 | end |
| 240 | if eq w 30506458621112435 |
| 241 | cmdspweval |
| 242 | ret 0 |
| 243 | end |
| 244 | if eq w 31645462708056179 |
| 245 | cmdspwsamp |
| 246 | ret 0 |
| 247 | end |
| 248 | if eq w 7311138133506355297 |
| 249 | cmdapuweave |
| 250 | ret 0 |
| 251 | end |
| 252 | if eq w 6778223 |
| 253 | cmdomg_cli |
| 254 | ret 0 |
| 255 | end |
| 256 | if eq w 32485562537636449 |
| 257 | cmdarboris_cli |
| 258 | ret 0 |
| 259 | end |
| 260 | if eq w 7759218 |
| 261 | rr = rev_ollm_cmd |
| 262 | if ne rr 0 |
| 263 | exit rr |
| 264 | end |
| 265 | ret 0 |
| 266 | end |
| 267 | if eq w 0 |
| 268 | lo = ocmdlo |
| 269 | hi = ocmdhi |
| 270 | if and eq lo 7020664736201273459 eq hi 28784 |
| 271 | cmdspchainapp |
| 272 | ret 0 |
| 273 | end |
| 274 | if and eq lo 7308895112352985203 eq hi 7102838 |
| 275 | cmdspchaineval |
| 276 | ret 0 |
| 277 | end |
| 278 | if and eq lo 8317701428883976307 eq hi 7368033 |
| 279 | cmdspchainsamp |
| 280 | ret 0 |
| 281 | end |
| 282 | if and eq lo 7022912219706909793 eq hi 108 |
| 283 | cmdadeval |
| 284 | ret 0 |
| 285 | end |
| 286 | if and eq lo 7881707766813910113 eq hi 112 |
| 287 | cmdadsamp |
| 288 | ret 0 |
| 289 | end |
| 290 | end |
| 291 | exit 2 |
| 292 | end |
| 293 | |
| 294 | note init CK V T D H F L B SEED TOTAL LRMICRO [G [LP LC NP]]; lr = f64(LRMICRO)/1000000.0. Without G |
| 295 | note the file is OLLMCK01 (G = 1); with G it is OLLMCK02 (CONTRACT-V1 section 6). LP LC NP make the |
| 296 | note LC layers from layer LP a core that runs NP times with the same weights (model.om, docs/LOOP.md): |
| 297 | note the file is then OLLMCK03. NP 1 is no loop and gives the OLLMCK02 file; NP 0 is exit 2; a core |
| 298 | note that is empty or does not fit in the L layers is exit 12. |
| 299 | fn oinit_setup to void |
| 300 | na = oargc |
| 301 | if and and ne na 13 ne na 14 ne na 17 |
| 302 | exit 2 |
| 303 | end |
| 304 | for i ulong 7 |
| 305 | put og i oargu add 3 i |
| 306 | end |
| 307 | put og 57 0 |
| 308 | put og 58 0 |
| 309 | put og 59 0 |
| 310 | if eq na 17 |
| 311 | oloopa |
| 312 | end |
| 313 | if not ogeom |
| 314 | exit 12 |
| 315 | end |
| 316 | put og 40 oargu 10 |
| 317 | tt = oargu 11 |
| 318 | if gt tt 999999 |
| 319 | exit 12 |
| 320 | end |
| 321 | g = ulong 1 |
| 322 | put og 46 1 |
| 323 | if ge na 14 |
| 324 | g = oargu 13 |
| 325 | put og 46 2 |
| 326 | end |
| 327 | if not ogok2 g |
| 328 | exit 12 |
| 329 | end |
| 330 | put og 44 g |
| 331 | put og 42 tt |
| 332 | put og 43 bitcast ulong div f64 oargu 12 1000000.0 |
| 333 | put og 41 0 |
| 334 | oinit |
| 335 | end |
| 336 | |
| 337 | fn cmdinit to void |
| 338 | oinit_setup |
| 339 | ocksave 2 |
| 340 | oputc 73 |
| 341 | oputc 32 |
| 342 | oputu get og 8 |
| 343 | oputc 10 |
| 344 | oflush |
| 345 | end |
| 346 | |
| 347 | fn cmdinitfast to void |
| 348 | oinit_setup |
| 349 | fl = or or OCK5F_F32 OCK5F_BF16 OCK5F_ZMV |
| 350 | orig = get og 46 |
| 351 | if gt get og 59 1 |
| 352 | fl = or fl OCK5F_LOOP |
| 353 | orig = 3 |
| 354 | end |
| 355 | ock5_save_cpu 2 fl orig |
| 356 | oputc 73 |
| 357 | oputc 32 |
| 358 | oputu get og 8 |
| 359 | oputc 10 |
| 360 | oflush |
| 361 | end |
| 362 | |
| 363 | fn ock5_mv_is_zero to bool |
| 364 | n = mul 2 get og 8 |
| 365 | i0 = ulong 0 |
| 366 | while lt i0 n bound 999999 |
| 367 | cnt = sub n i0 |
| 368 | if gt cnt OCK5C |
| 369 | cnt = OCK5C |
| 370 | end |
| 371 | for j ulong cnt |
| 372 | if ne bitcast uint f32 omvg add i0 j 0 |
| 373 | ret false |
| 374 | end |
| 375 | end |
| 376 | i0 = add i0 cnt |
| 377 | end |
| 378 | ret true |
| 379 | end |
| 380 | |
| 381 | fn cmdckfast_mode ai ulong to ulong |
| 382 | r = oargr ai |
| 383 | if or le r 0 gt r 8 |
| 384 | exit 2 |
| 385 | end |
| 386 | v = ulong 0 |
| 387 | for k ulong ulong r |
| 388 | v = or v shl ulong get oarg k mul k 8 |
| 389 | end |
| 390 | if eq v 3289958 |
| 391 | ret 0 |
| 392 | end |
| 393 | if eq v 909207138 |
| 394 | ret 1 |
| 395 | end |
| 396 | if eq v 225224062574 |
| 397 | ret 2 |
| 398 | end |
| 399 | exit 2 |
| 400 | end |
| 401 | |
| 402 | fn cmdckfast to void |
| 403 | na = oargc |
| 404 | if and ne na 4 ne na 5 |
| 405 | exit 2 |
| 406 | end |
| 407 | md = ulong 1 |
| 408 | if eq na 5 |
| 409 | md = cmdckfast_mode 4 |
| 410 | end |
| 411 | ockloop 2 |
| 412 | fl = OCK5F_F32 |
| 413 | if ge md 1 |
| 414 | fl = or fl OCK5F_BF16 |
| 415 | end |
| 416 | if ock5_mv_is_zero |
| 417 | fl = or fl OCK5F_ZMV |
| 418 | end |
| 419 | if gt get og 59 1 |
| 420 | fl = or fl OCK5F_LOOP |
| 421 | end |
| 422 | orig = get og 46 |
| 423 | if eq orig 5 |
| 424 | orig = get ock5_st 1 |
| 425 | end |
| 426 | if or eq orig 0 gt orig 3 |
| 427 | orig = 2 |
| 428 | if gt get og 59 1 |
| 429 | orig = 3 |
| 430 | end |
| 431 | end |
| 432 | ock5_save_cpu 3 fl orig |
| 433 | oputc 67 |
| 434 | oputc 70 |
| 435 | oputc 32 |
| 436 | oputu get og 8 |
| 437 | oputc 32 |
| 438 | oputu fl |
| 439 | oputc 10 |
| 440 | oflush |
| 441 | end |
| 442 | |
| 443 | fn cmdckf64 to void |
| 444 | if ne oargc 4 |
| 445 | exit 2 |
| 446 | end |
| 447 | ockloop 2 |
| 448 | if eq get og 46 5 |
| 449 | orig = get ock5_st 1 |
| 450 | if or eq orig 0 gt orig 3 |
| 451 | orig = 2 |
| 452 | end |
| 453 | if gt get og 59 1 |
| 454 | orig = 3 |
| 455 | end |
| 456 | put og 46 orig |
| 457 | end |
| 458 | ocksave 3 |
| 459 | oputc 67 |
| 460 | oputc 54 |
| 461 | oputc 32 |
| 462 | oputu get og 8 |
| 463 | oputc 32 |
| 464 | oputu get og 46 |
| 465 | oputc 10 |
| 466 | oflush |
| 467 | end |
| 468 | |
| 469 | note the loop arguments LP LC NP of init (argv 14 .. 16) into og 57 .. 59; NP 1 leaves no loop |
| 470 | fn oloopa to void |
| 471 | np = oargu 16 |
| 472 | if eq np 0 |
| 473 | exit 2 |
| 474 | end |
| 475 | if gt np 1 |
| 476 | put og 57 oargu 14 |
| 477 | put og 58 oargu 15 |
| 478 | put og 59 np |
| 479 | end |
| 480 | end |
| 481 | |
| 482 | note little-endian u64 at ochk[o..o+8) |
| 483 | fn ochk64 o ulong to ulong |
| 484 | v = ulong 0 |
| 485 | for k ulong 8 |
| 486 | x = ulong get ochk add o k |
| 487 | v = or v shl x mul k 8 |
| 488 | end |
| 489 | ret v |
| 490 | end |
| 491 | |
| 492 | note row b from offset ochk[b] (checked by ooffok) |
| 493 | fn ooffrow b ulong to void |
| 494 | obrow b ochk64 mul b 8 |
| 495 | end |
| 496 | |
| 497 | note exit 22 when any offset has o >= N - T (written so that an offset near 2^64 cannot wrap) |
| 498 | fn ooffok to void |
| 499 | n = get odn 0 |
| 500 | tt = get og 1 |
| 501 | for b ulong get og 6 |
| 502 | if ge ochk64 mul b 8 sub n tt |
| 503 | exit 22 |
| 504 | end |
| 505 | end |
| 506 | end |
| 507 | |
| 508 | note grad CK DATA OFFS OUT: the gradient of CONTRACT-V1 section 5 (G blocks, K = 1) |
| 509 | fn cmdgrad to void |
| 510 | if ne oargc 6 |
| 511 | exit 2 |
| 512 | end |
| 513 | ockloop 2 |
| 514 | odopen 3 |
| 515 | ooffrd 4 |
| 516 | ooffok |
| 517 | for b ulong get og 6 |
| 518 | ooffrow b |
| 519 | end |
| 520 | ls = ograd |
| 521 | ogrsave 5 |
| 522 | oputc 71 |
| 523 | oputc 32 |
| 524 | oputx bitcast ulong ls |
| 525 | oputc 32 |
| 526 | oputl rmic ls |
| 527 | oputc 10 |
| 528 | oflush |
| 529 | end |
| 530 | |
| 531 | note train CK TRAIN UNTIL EVERY [K]: steps step .. min(UNTIL, TOTAL)-1; after step s, when |
| 532 | note (s+1) mod EVERY = 0 or s+1 is the end, save CK and print `T s+1 loss_micro(step s)`. |
| 533 | note K worker processes (CONTRACT-V1 section 5.1): K < 1, K > G or G mod K != 0 is exit 29. |
| 534 | fn cmdtrain to void |
| 535 | na = oargc |
| 536 | if and ne na 6 ne na 7 |
| 537 | exit 2 |
| 538 | end |
| 539 | ockloop 2 |
| 540 | odopen 3 |
| 541 | un = oargu 4 |
| 542 | ev = oargu 5 |
| 543 | if eq ev 0 |
| 544 | exit 2 |
| 545 | end |
| 546 | k = ulong 1 |
| 547 | if eq na 7 |
| 548 | k = oargu 6 |
| 549 | end |
| 550 | gg = get og 44 |
| 551 | if or eq k 0 gt k gg |
| 552 | exit 29 |
| 553 | end |
| 554 | if ne rem gg k 0 |
| 555 | exit 29 |
| 556 | end |
| 557 | put og 45 k |
| 558 | en = get og 42 |
| 559 | if lt un en |
| 560 | en = un |
| 561 | end |
| 562 | while lt get og 41 en bound 999999 |
| 563 | ls = ostep |
| 564 | s1 = get og 41 |
| 565 | if or eq rem s1 ev 0 eq s1 en |
| 566 | ocksave 2 |
| 567 | oputc 84 |
| 568 | oputc 32 |
| 569 | oputu s1 |
| 570 | oputc 32 |
| 571 | oputl rmic ls |
| 572 | oputc 10 |
| 573 | oflush |
| 574 | end |
| 575 | end |
| 576 | end |
| 577 | |
| 578 | note vtrain CK TRAIN UNTIL EVERY NP_MIN NP_MAX: variable-NP looped-core training (COCONUT/recurrent-depth). |
| 579 | note Cycles cur_np across [NP_MIN .. NP_MAX] per step, preserving the base checkpoint's NP in the saved header. |
| 580 | fn cmdvtrain to void |
| 581 | if ne oargc 8 |
| 582 | exit 2 |
| 583 | end |
| 584 | ockloop 2 |
| 585 | if eq get og 58 0 |
| 586 | exit 17 |
| 587 | end |
| 588 | odopen 3 |
| 589 | un = oargu 4 |
| 590 | ev = oargu 5 |
| 591 | npmin = oargu 6 |
| 592 | npmax = oargu 7 |
| 593 | if or eq ev 0 or eq npmin 0 or gt npmin npmax gt npmax 6 |
| 594 | exit 2 |
| 595 | end |
| 596 | orig = get og 59 |
| 597 | put og 59 npmax |
| 598 | if not ogeom |
| 599 | exit 12 |
| 600 | end |
| 601 | put og 59 orig |
| 602 | x0 = ogeom |
| 603 | put og 45 1 |
| 604 | en = get og 42 |
| 605 | if lt un en |
| 606 | en = un |
| 607 | end |
| 608 | ovtloop orig en ev npmin npmax |
| 609 | end |
| 610 | |
| 611 | fn ovtloop orig ulong en ulong ev ulong npmin ulong npmax ulong to void |
| 612 | span = add sub npmax npmin 1 |
| 613 | while lt get og 41 en bound 999999 |
| 614 | cnp = add npmin rem get og 41 span |
| 615 | put og 59 cnp |
| 616 | if not ogeom |
| 617 | exit 12 |
| 618 | end |
| 619 | ls = ostep |
| 620 | s1 = get og 41 |
| 621 | if or eq rem s1 ev 0 eq s1 en |
| 622 | put og 59 orig |
| 623 | x1 = ogeom |
| 624 | ocksave 2 |
| 625 | ovtprint s1 cnp ls |
| 626 | end |
| 627 | end |
| 628 | put og 59 orig |
| 629 | x2 = ogeom |
| 630 | end |
| 631 | |
| 632 | fn ovtprint s1 ulong cnp ulong ls f64 to void |
| 633 | oputc 86 |
| 634 | oputc 84 |
| 635 | oputc 32 |
| 636 | oputu s1 |
| 637 | oputc 32 |
| 638 | oputu cnp |
| 639 | oputc 32 |
| 640 | oputl rmic ls |
| 641 | oputc 10 |
| 642 | oflush |
| 643 | end |
| 644 | |
| 645 | note veval CK HELD W NP [MERGES]: evaluate a looped OLLMCK03 checkpoint at arbitrary loop count NP. |
| 646 | fn cmdveval to void |
| 647 | na = oargc |
| 648 | if and ne na 6 ne na 7 |
| 649 | exit 2 |
| 650 | end |
| 651 | ockloop 2 |
| 652 | ww = oargu 4 |
| 653 | np = oargu 5 |
| 654 | if or eq np 0 gt np 6 |
| 655 | exit 2 |
| 656 | end |
| 657 | if gt get og 58 0 |
| 658 | put og 59 np |
| 659 | if not ogeom |
| 660 | exit 12 |
| 661 | end |
| 662 | end |
| 663 | v = get og 0 |
| 664 | if gt v 256 |
| 665 | if eq na 6 |
| 666 | exit 2 |
| 667 | end |
| 668 | obload 6 v |
| 669 | end |
| 670 | odopen 3 |
| 671 | ovevrun ww np |
| 672 | end |
| 673 | |
| 674 | fn ovevrun ww ulong np ulong to void |
| 675 | tt = get og 1 |
| 676 | m = get odn 0 |
| 677 | if or eq ww 0 gt ww div sub m 1 tt |
| 678 | exit 23 |
| 679 | end |
| 680 | v = get og 0 |
| 681 | ls = 0.0f64 |
| 682 | nb = ulong 0 |
| 683 | for w ulong ww |
| 684 | obrow 0 mul w tt |
| 685 | ls = add ls ofwd 0 |
| 686 | if gt v 256 |
| 687 | nb = add nb oevnb tt |
| 688 | end |
| 689 | end |
| 690 | lo = div ls f64 ww |
| 691 | bp = div lo 0.6931471805599453 |
| 692 | if gt v 256 |
| 693 | bp = div mul bp f64 mul ww tt f64 nb |
| 694 | end |
| 695 | oputc 86 |
| 696 | oputc 69 |
| 697 | oputc 32 |
| 698 | oputu get og 41 |
| 699 | oputc 32 |
| 700 | oputu np |
| 701 | oputc 32 |
| 702 | oputu ww |
| 703 | oputc 32 |
| 704 | oputl rmic lo |
| 705 | oputc 32 |
| 706 | oputl rmic bp |
| 707 | oputc 10 |
| 708 | oflush |
| 709 | end |
| 710 | |
| 711 | note eval CK HELD W [MERGES]: windows at 0, T, .., (W-1)T; prints `E step W loss_micro bpb_micro`. |
| 712 | note V > 256 needs MERGES (else exit 2); bpb = ((loss/ln2) f64(W T))/f64(nbytes) with nbytes the |
| 713 | note decoded length of the W T targets (CONTRACT-V1 section 7.1); V = 256 keeps loss/ln2. |
| 714 | fn cmdeval to void |
| 715 | na = oargc |
| 716 | if and ne na 5 ne na 6 |
| 717 | exit 2 |
| 718 | end |
| 719 | ockloop 2 |
| 720 | ww = oargu 4 |
| 721 | v = get og 0 |
| 722 | if gt v 256 |
| 723 | if eq na 5 |
| 724 | exit 2 |
| 725 | end |
| 726 | obload 5 v |
| 727 | end |
| 728 | odopen 3 |
| 729 | tt = get og 1 |
| 730 | m = get odn 0 |
| 731 | note W T + 1 > N, tested as W > (N - 1) / T so that a huge W cannot wrap W T |
| 732 | if eq ww 0 |
| 733 | exit 23 |
| 734 | end |
| 735 | if gt ww div sub m 1 tt |
| 736 | exit 23 |
| 737 | end |
| 738 | ls = 0.0f64 |
| 739 | nb = ulong 0 |
| 740 | for w ulong ww |
| 741 | obrow 0 mul w tt |
| 742 | ls = add ls ofwd 0 |
| 743 | if gt v 256 |
| 744 | nb = add nb oevnb tt |
| 745 | end |
| 746 | end |
| 747 | lo = div ls f64 ww |
| 748 | bp = div lo 0.6931471805599453 |
| 749 | if gt v 256 |
| 750 | bp = div mul bp f64 mul ww tt f64 nb |
| 751 | end |
| 752 | oputc 69 |
| 753 | oputc 32 |
| 754 | oputu get og 41 |
| 755 | oputc 32 |
| 756 | oputu ww |
| 757 | oputc 32 |
| 758 | oputl rmic lo |
| 759 | oputc 32 |
| 760 | oputl rmic bp |
| 761 | oputc 10 |
| 762 | oflush |
| 763 | end |
| 764 | |
| 765 | note decoded bytes of the targets oby[0 .. n) |
| 766 | fn oevnb n ulong to ulong |
| 767 | s = ulong 0 |
| 768 | for t ulong n |
| 769 | s = add s obtlen get oby t |
| 770 | end |
| 771 | ret s |
| 772 | end |
| 773 | |
| 774 | note append token c to the context window obx[0 .. CT), keeping the last T tokens |
| 775 | fn ocpush c ulong to void |
| 776 | cn = get og 9 |
| 777 | tt = get og 1 |
| 778 | if lt cn tt |
| 779 | put obx cn c |
| 780 | put og 9 add cn 1 |
| 781 | else |
| 782 | for i ulong sub tt 1 |
| 783 | put obx i get obx add i 1 |
| 784 | end |
| 785 | put obx sub tt 1 c |
| 786 | end |
| 787 | end |
| 788 | |
| 789 | note sample CK SEED N TEMPMILLI PROMPT [MERGES]. Context = token 0, then PROMPT (bytes when V = 256, |
| 790 | note its encoding when V > 256, CONTRACT-V1 section 7.2); the model reads the last T context tokens |
| 791 | note at positions 0..; the next token comes from the logits at the last position. |
| 792 | note TEMPMILLI 0: argmax (lowest index on ties), no draw. Otherwise softmax(logits/(TEMPMILLI/1000)) |
| 793 | note and u = (splitmix64 stream out >> 11) 2^-53 with state = SEED, out = mix(state += GOLD); |
| 794 | note the token is the smallest c with u < p[0] + .. + p[c], else the last c with p[c] > 0. |
| 795 | note A generated token 0 ends the output and is not written; another writes its decoded bytes. |
| 796 | fn cmdsamp to void |
| 797 | na = oargc |
| 798 | if and ne na 7 ne na 8 |
| 799 | exit 2 |
| 800 | end |
| 801 | ockloop 2 |
| 802 | st = oargu 3 |
| 803 | nn = oargu 4 |
| 804 | if gt nn 999999 |
| 805 | exit 2 |
| 806 | end |
| 807 | tm = oargu 5 |
| 808 | v = get og 0 |
| 809 | if gt v 256 |
| 810 | if eq na 7 |
| 811 | exit 2 |
| 812 | end |
| 813 | obload 7 v |
| 814 | end |
| 815 | ochipup |
| 816 | osampleloaded st nn tm |
| 817 | end |
| 818 | |
| 819 | note nsample CK SEED N TEMPMILLI PROMPT MERGES ADAPT_EXTRA THRESH_MILLI |
| 820 | fn cmdnsamp to void |
| 821 | if ne oargc 10 |
| 822 | exit 2 |
| 823 | end |
| 824 | ockloop 2 |
| 825 | st = oargu 3 |
| 826 | nn = oargu 4 |
| 827 | if gt nn 999999 |
| 828 | exit 2 |
| 829 | end |
| 830 | tm = oargu 5 |
| 831 | v = get og 0 |
| 832 | if gt v 256 |
| 833 | obload 7 v |
| 834 | end |
| 835 | onlreset |
| 836 | put onlc 4 oargu 8 |
| 837 | put onlc 5 oargu 9 |
| 838 | ochipup |
| 839 | osampleloaded st nn tm |
| 840 | onlreset |
| 841 | end |
| 842 | |
| 843 | note Embedding entry: checkpoint and tokenizer already loaded; argument 6 supplies the prompt. |
| 844 | note The caller keeps exclusive ownership of the model pools until this returns. |
| 845 | fn osampleloaded seed ulong nn ulong tm ulong to void |
| 846 | if gt nn 999999 |
| 847 | exit 2 |
| 848 | end |
| 849 | st = seed |
| 850 | v = get og 0 |
| 851 | put og 9 0 |
| 852 | ocpush 0 |
| 853 | if gt v 256 |
| 854 | r = oargr 6 |
| 855 | if or lt r 0 gt r 4000 |
| 856 | exit 2 |
| 857 | end |
| 858 | np = obprompt 6 |
| 859 | for i ulong np |
| 860 | ocpush get obtk i |
| 861 | end |
| 862 | else |
| 863 | r = oargr 6 |
| 864 | if or lt r 0 gt r 4096 |
| 865 | exit 2 |
| 866 | end |
| 867 | for i ulong ulong r |
| 868 | ocpush ulong get oarg i |
| 869 | end |
| 870 | end |
| 871 | for g ulong nn |
| 872 | c = osstep st tm |
| 873 | if ne tm 0 |
| 874 | st = add st 11400714819323198485 |
| 875 | end |
| 876 | if eq c 0 |
| 877 | break |
| 878 | end |
| 879 | if gt v 256 |
| 880 | obtput c |
| 881 | else |
| 882 | oputc c |
| 883 | end |
| 884 | note Flush each decoded token so interactive consumers see progress. Bytes and sampling stay unchanged. |
| 885 | oflush |
| 886 | ocpush c |
| 887 | end |
| 888 | oflush |
| 889 | end |
| 890 | |
| 891 | note one sampling step (cmdsamp, serve.om): the next token from the logits at the last context |
| 892 | note position, argmax when TEMPMILLI tm is 0, else a draw with the splitmix64 stream state st. |
| 893 | fn osstep st ulong tm ulong to ulong |
| 894 | olastf |
| 895 | if eq tm 0 |
| 896 | ret oargmax |
| 897 | end |
| 898 | ret odraw st div f64 tm 1000.0 |
| 899 | end |
| 900 | |
| 901 | note ogpu[0] = 1 when the per-token forward runs on the chip: gpu/gg.om's gserve and gsample set it |
| 902 | note before cmdserve or cmdsamp; nogpu.om's ogchip and oglast refuse with exit 2 on the CPU targets. |
| 903 | global ogpu ulong 1 |
| 904 | |
| 905 | note after the checkpoint loads: on the chip path, open the chip and upload the parameters once |
| 906 | note (gpu/gg.om plans one activation block per layer application, so a looped model runs there too). |
| 907 | fn ochipup to void |
| 908 | if ne get ogpu 0 0 |
| 909 | ogchip |
| 910 | end |
| 911 | end |
| 912 | |
| 913 | note forward of the context in obx row 0, then its last position's logits into TMP[0..V) (oa), |
| 914 | note on the chip when ogpu[0] = 1 (oglast), else on the CPU (ofwdi, model.om: only the rows past the |
| 915 | note prefix the previous call computed, then olast) |
| 916 | fn olastf to void |
| 917 | if ne get ogpu 0 0 |
| 918 | oglast |
| 919 | ret |
| 920 | end |
| 921 | t0 = ofwdi |
| 922 | olast |
| 923 | if or ne get onlc 4 0 ne get onlc 10 0 |
| 924 | onladapt |
| 925 | end |
| 926 | end |
| 927 | |
| 928 | note kvcmp CK N PROMPT MERGES (ham/ollm-speed, V > 256, N V <= 2^20): ofwdi against ofwd 0. Pass 1: |
| 929 | note N greedy steps from token 0 then PROMPT's encoding with ofwdi then olast (the CPU decode), every |
| 930 | note step's V logits kept in okvr; prints `KI step ct t0 tok ns`. Pass 2: the same contexts again |
| 931 | note (pass 1's tokens pushed) with ofwd 0 then olast; prints `KF step ct tok ndiff ns`, ndiff = logits |
| 932 | note whose f64 bits differ from pass 1's. Before step N/2 of both passes the context token at CT - 5 |
| 933 | note (when CT > 5) becomes its successor mod V, so ofwdi must find the changed prefix. Last line |
| 934 | note `KVCMP N ndiff inc_ns full_ns` (forward and logits time summed over the steps); exit 43 when |
| 935 | note ndiff > 0. |
| 936 | global okvr f64 1048576 |
| 937 | global okvk ulong 4096 |
| 938 | fn kvstart to void |
| 939 | put og 9 0 |
| 940 | ocpush 0 |
| 941 | np = obprompt 4 |
| 942 | for i ulong np |
| 943 | ocpush get obtk i |
| 944 | end |
| 945 | end |
| 946 | |
| 947 | fn kvpert s ulong nn ulong to void |
| 948 | ct = get og 9 |
| 949 | if and eq s div nn 2 gt ct 5 |
| 950 | i = sub ct 5 |
| 951 | put obx i rem add get obx i 1 get og 0 |
| 952 | end |
| 953 | end |
| 954 | |
| 955 | fn kvline a ulong b ulong c ulong d ulong e long to void |
| 956 | oputc 32 |
| 957 | oputu a |
| 958 | oputc 32 |
| 959 | oputu b |
| 960 | oputc 32 |
| 961 | oputu c |
| 962 | oputc 32 |
| 963 | oputu d |
| 964 | oputc 32 |
| 965 | oputl e |
| 966 | oputc 10 |
| 967 | oflush |
| 968 | end |
| 969 | |
| 970 | note kvcmp totals: okvs[0] pass 1 ns, [1] pass 2 ns, [2] differing logits |
| 971 | global okvs ulong 3 |
| 972 | fn cmdkvcmp to void |
| 973 | if ne oargc 6 |
| 974 | exit 2 |
| 975 | end |
| 976 | ockloop 2 |
| 977 | nn = oargu 3 |
| 978 | v = get og 0 |
| 979 | if or le v 256 or gt nn 4096 gt mul nn v 1048576 |
| 980 | exit 2 |
| 981 | end |
| 982 | obload 5 v |
| 983 | put okvs 0 0 |
| 984 | put okvs 1 0 |
| 985 | put okvs 2 0 |
| 986 | kvstart |
| 987 | for s ulong nn |
| 988 | kvpert s nn |
| 989 | c = kvinc s |
| 990 | ocpush c |
| 991 | end |
| 992 | kvstart |
| 993 | for s2 ulong nn |
| 994 | kvpert s2 nn |
| 995 | kvfull s2 |
| 996 | ocpush get okvk s2 |
| 997 | end |
| 998 | oputc 75 |
| 999 | oputc 86 |
| 1000 | oputc 67 |
| 1001 | oputc 77 |
| 1002 | oputc 80 |
| 1003 | oputc 32 |
| 1004 | oputu nn |
| 1005 | oputc 32 |
| 1006 | oputu get okvs 2 |
| 1007 | oputc 32 |
| 1008 | oputu get okvs 0 |
| 1009 | oputc 32 |
| 1010 | oputu get okvs 1 |
| 1011 | oputc 10 |
| 1012 | oflush |
| 1013 | if gt get okvs 2 0 |
| 1014 | exit 43 |
| 1015 | end |
| 1016 | end |
| 1017 | |
| 1018 | note pass 1 step s: ofwdi then olast, logits into okvr row s, KI line; returns the greedy token |
| 1019 | fn kvinc s ulong to ulong |
| 1020 | a = onsnow |
| 1021 | t0 = ofwdi |
| 1022 | olast |
| 1023 | e = sub onsnow a |
| 1024 | put okvs 0 add get okvs 0 e |
| 1025 | c = oargmax |
| 1026 | kvkeep s |
| 1027 | put okvk s c |
| 1028 | oputc 75 |
| 1029 | oputc 73 |
| 1030 | kvline s get og 9 t0 c long e |
| 1031 | ret c |
| 1032 | end |
| 1033 | |
| 1034 | note okvr row s = the V logits in TMP |
| 1035 | fn kvkeep s ulong to void |
| 1036 | v = get og 0 |
| 1037 | tm = get og 27 |
| 1038 | o = mul s v |
| 1039 | for j ulong v |
| 1040 | x = get oa add tm j |
| 1041 | put okvr add o j x |
| 1042 | end |
| 1043 | end |
| 1044 | |
| 1045 | note the logits in TMP whose f64 bits differ from okvr row s |
| 1046 | fn kvdiff s ulong to ulong |
| 1047 | v = get og 0 |
| 1048 | tm = get og 27 |
| 1049 | o = mul s v |
| 1050 | nd = ulong 0 |
| 1051 | for j ulong v |
| 1052 | x = bitcast ulong get oa add tm j |
| 1053 | y = bitcast ulong get okvr add o j |
| 1054 | if ne x y |
| 1055 | nd = add nd 1 |
| 1056 | end |
| 1057 | end |
| 1058 | ret nd |
| 1059 | end |
| 1060 | |
| 1061 | note pass 2 step s: ofwd 0 then olast, compared bit for bit with okvr row s, KF line |
| 1062 | fn kvfull s ulong to void |
| 1063 | a = onsnow |
| 1064 | lf = ofwd 0 |
| 1065 | olast |
| 1066 | e = sub onsnow a |
| 1067 | put okvs 1 add get okvs 1 e |
| 1068 | nd = kvdiff s |
| 1069 | put okvs 2 add get okvs 2 nd |
| 1070 | oputc 75 |
| 1071 | oputc 70 |
| 1072 | kvline s get og 9 oargmax nd long e |
| 1073 | end |
| 1074 | |
| 1075 | note ======== Adaptive loop commands (model.om onlx; docs/LOOP.md "Per-token exit and messages") |
| 1076 | note THRESH is the exit threshold of a row's normalized core delta times 1e6, GAIN the inter-pass |
| 1077 | note message gain times 1e6; 0 0 runs the hooks for telemetry only and computes eval's bits. |
| 1078 | note Every command prints, after its own lines, |
| 1079 | note `AX NP rows exited mean_exit_milli d0min_micro d0max_micro msgnorm_milli fnv` (rows counted, |
| 1080 | note rows that left the loop early, their mean passes, the range of pass-0 deltas, the mean message |
| 1081 | note norm, FNV-1a 64 over every counted row's exit pass, message norm bits and pass-0 delta bits) |
| 1082 | note and `AH c1 .. cNP` (rows by the passes they ran). |
| 1083 | note oadh: rows by passes; oadk: [0] fnv [1] rows [2] exited early [3] passes summed; |
| 1084 | note oada: [0] min pass-0 delta [1] max [2] message norms summed; oadx: [0] telemetry differences |
| 1085 | global oadh ulong 64 |
| 1086 | global oadk ulong 4 |
| 1087 | global oada f64 4 |
| 1088 | global oadx ulong 2 |
| 1089 | global oadhs ulong 4096 |
| 1090 | |
| 1091 | fn oadset th ulong gn ulong to void |
| 1092 | onlreset |
| 1093 | oadclr |
| 1094 | put onlx 0 th |
| 1095 | put onlx 1 gn |
| 1096 | put onlx 6 1 |
| 1097 | end |
| 1098 | |
| 1099 | fn oadclr to void |
| 1100 | for i ulong 8 |
| 1101 | put onlx i 0 |
| 1102 | end |
| 1103 | end |
| 1104 | |
| 1105 | fn oadzero to void |
| 1106 | for i ulong 64 |
| 1107 | put oadh i 0 |
| 1108 | end |
| 1109 | put oadk 0 14695981039346656037 |
| 1110 | put oadk 1 0 |
| 1111 | put oadk 2 0 |
| 1112 | put oadk 3 0 |
| 1113 | for j ulong 4 |
| 1114 | put oada j 0.0f64 |
| 1115 | end |
| 1116 | end |
| 1117 | |
| 1118 | note FNV-1a 64 of oadk[0] over the 8 little-endian bytes of w |
| 1119 | fn oadfnv w ulong to void |
| 1120 | h = get oadk 0 |
| 1121 | for b ulong 8 |
| 1122 | h = xor h and shr w mul b 8 255 |
| 1123 | h = mul h 1099511628211 |
| 1124 | end |
| 1125 | put oadk 0 h |
| 1126 | end |
| 1127 | |
| 1128 | fn oadacc t0 ulong n ulong to void |
| 1129 | for i ulong n |
| 1130 | oadrow add t0 i |
| 1131 | end |
| 1132 | end |
| 1133 | |
| 1134 | fn oadrow t ulong to void |
| 1135 | xp = get onlxp t |
| 1136 | if le get og 59 1 |
| 1137 | xp = ulong 1 |
| 1138 | end |
| 1139 | k = xp |
| 1140 | if gt k 63 |
| 1141 | k = 63 |
| 1142 | end |
| 1143 | put oadh k add get oadh k 1 |
| 1144 | put oadk 1 add get oadk 1 1 |
| 1145 | put oadk 3 add get oadk 3 xp |
| 1146 | if lt xp get og 59 |
| 1147 | put oadk 2 add get oadk 2 1 |
| 1148 | end |
| 1149 | oadrow2 t xp |
| 1150 | end |
| 1151 | |
| 1152 | fn oadrow2 t ulong xp ulong to void |
| 1153 | d0 = get onlxd t |
| 1154 | if or eq get oadk 1 1 lt d0 get oada 0 |
| 1155 | put oada 0 d0 |
| 1156 | end |
| 1157 | if gt d0 get oada 1 |
| 1158 | put oada 1 d0 |
| 1159 | end |
| 1160 | mn = get onlmn t |
| 1161 | put oada 2 add get oada 2 mn |
| 1162 | oadfnv xp |
| 1163 | oadfnv bitcast ulong mn |
| 1164 | oadfnv bitcast ulong d0 |
| 1165 | end |
| 1166 | |
| 1167 | fn oadprint to void |
| 1168 | rows = get oadk 1 |
| 1169 | dn = rows |
| 1170 | if eq dn 0 |
| 1171 | dn = 1 |
| 1172 | end |
| 1173 | oputc 65 |
| 1174 | oputc 88 |
| 1175 | oadsp get og 59 |
| 1176 | oadsp rows |
| 1177 | oadsp get oadk 2 |
| 1178 | oadsp div mul get oadk 3 1000 dn |
| 1179 | oputc 32 |
| 1180 | oputl rmic get oada 0 |
| 1181 | oputc 32 |
| 1182 | oputl rmic get oada 1 |
| 1183 | oputc 32 |
| 1184 | oputl long mul div get oada 2 f64 dn 1000.0 |
| 1185 | oputc 32 |
| 1186 | oputx get oadk 0 |
| 1187 | oputc 10 |
| 1188 | oputc 65 |
| 1189 | oputc 72 |
| 1190 | np = get og 59 |
| 1191 | if gt np 63 |
| 1192 | np = 63 |
| 1193 | end |
| 1194 | if eq np 0 |
| 1195 | np = 1 |
| 1196 | end |
| 1197 | for p ulong np |
| 1198 | oputc 32 |
| 1199 | oputu get oadh add p 1 |
| 1200 | end |
| 1201 | oputc 10 |
| 1202 | oflush |
| 1203 | end |
| 1204 | |
| 1205 | note adapteval CK HELD W THRESH GAIN [MERGES]: eval's windows with the adaptive loop; prints |
| 1206 | note `AE step W loss_micro bpb_micro` (eval's E line when THRESH and GAIN are 0), then AX and AH over |
| 1207 | note every row of every window. |
| 1208 | fn cmdadeval to void |
| 1209 | na = oargc |
| 1210 | if and ne na 7 ne na 8 |
| 1211 | exit 2 |
| 1212 | end |
| 1213 | ockloop 2 |
| 1214 | ww = oargu 4 |
| 1215 | v = get og 0 |
| 1216 | if gt v 256 |
| 1217 | if eq na 7 |
| 1218 | exit 2 |
| 1219 | end |
| 1220 | obload 7 v |
| 1221 | end |
| 1222 | odopen 3 |
| 1223 | tt = get og 1 |
| 1224 | if or eq ww 0 gt ww div sub get odn 0 1 tt |
| 1225 | exit 23 |
| 1226 | end |
| 1227 | oadset oargu 5 oargu 6 |
| 1228 | oadzero |
| 1229 | ls = 0.0f64 |
| 1230 | nb = ulong 0 |
| 1231 | for w ulong ww |
| 1232 | obrow 0 mul w tt |
| 1233 | ls = add ls ofwd 0 |
| 1234 | oadacc 0 get og 9 |
| 1235 | if gt v 256 |
| 1236 | nb = add nb oevnb tt |
| 1237 | end |
| 1238 | end |
| 1239 | oadeline ls ww nb |
| 1240 | oadprint |
| 1241 | oadclr |
| 1242 | end |
| 1243 | |
| 1244 | fn oadeline ls f64 ww ulong nb ulong to void |
| 1245 | lo = div ls f64 ww |
| 1246 | bp = div lo 0.6931471805599453 |
| 1247 | if gt get og 0 256 |
| 1248 | bp = div mul bp f64 mul ww get og 1 f64 nb |
| 1249 | end |
| 1250 | oputc 65 |
| 1251 | oputc 69 |
| 1252 | oputc 32 |
| 1253 | oputu get og 41 |
| 1254 | oputc 32 |
| 1255 | oputu ww |
| 1256 | oputc 32 |
| 1257 | oputl rmic lo |
| 1258 | oputc 32 |
| 1259 | oputl rmic bp |
| 1260 | oputc 10 |
| 1261 | oflush |
| 1262 | end |
| 1263 | |
| 1264 | note adaptsamp CK SEED N TEMPMILLI PROMPT MERGES THRESH GAIN (V > 256): sample's context and draws |
| 1265 | note on the cached decode with the adaptive loop; one `AS step ct tok exit_pass msgnorm_milli` line |
| 1266 | note per token (the new row's passes and message norm), then `ADAPTSAMP n fnv`, AX and AH over the |
| 1267 | note new rows. A token 0 ends it. |
| 1268 | fn cmdadsamp to void |
| 1269 | if ne oargc 10 |
| 1270 | exit 2 |
| 1271 | end |
| 1272 | ockloop 2 |
| 1273 | nn = oargu 4 |
| 1274 | v = get og 0 |
| 1275 | if or le v 256 gt nn 4096 |
| 1276 | exit 2 |
| 1277 | end |
| 1278 | obload 7 v |
| 1279 | oadset oargu 8 oargu 9 |
| 1280 | oadzero |
| 1281 | put okv 0 0 |
| 1282 | put og 9 0 |
| 1283 | ocpush 0 |
| 1284 | np = obprompt 6 |
| 1285 | for i ulong np |
| 1286 | ocpush get obtk i |
| 1287 | end |
| 1288 | oadsrun oargu 3 nn oargu 5 |
| 1289 | oadclr |
| 1290 | end |
| 1291 | |
| 1292 | fn oadsrun st ulong nn ulong tm ulong to void |
| 1293 | ng = ulong 0 |
| 1294 | for g ulong nn |
| 1295 | c = osstep st tm |
| 1296 | if ne tm 0 |
| 1297 | st = add st 11400714819323198485 |
| 1298 | end |
| 1299 | t = sub get og 9 1 |
| 1300 | oadacc t 1 |
| 1301 | oadfnv c |
| 1302 | oputc 65 |
| 1303 | oputc 83 |
| 1304 | kvline g add t 1 c get onlxp t long mul get onlmn t 1000.0 |
| 1305 | ng = add ng 1 |
| 1306 | if eq c 0 |
| 1307 | break |
| 1308 | end |
| 1309 | ocpush c |
| 1310 | end |
| 1311 | oputs9 ng |
| 1312 | oadprint |
| 1313 | end |
| 1314 | |
| 1315 | fn oputs9 ng ulong to void |
| 1316 | oputc 65 |
| 1317 | oputc 68 |
| 1318 | oputc 65 |
| 1319 | oputc 80 |
| 1320 | oputc 84 |
| 1321 | oputc 83 |
| 1322 | oputc 65 |
| 1323 | oputc 77 |
| 1324 | oputc 80 |
| 1325 | oputc 32 |
| 1326 | oputu ng |
| 1327 | oputc 32 |
| 1328 | oputx get oadk 0 |
| 1329 | oputc 10 |
| 1330 | oflush |
| 1331 | end |
| 1332 | |
| 1333 | note adaptcmp CK N PROMPT MERGES THRESH GAIN: kvcmp with the adaptive loop. Pass 1 (ofwdi then olast) |
| 1334 | note prints `AI step ct t0 tok exit_pass`; pass 2 (ofwd 0 then olast) prints `AF step ct tok ndiff |
| 1335 | note xdiff`, ndiff = logits whose bits differ from pass 1's, xdiff = 1 when the telemetry of the CT |
| 1336 | note rows (exit passes, message norm bits, pass-0 delta bits) differs. Last |
| 1337 | note `ADAPTCMP N ndiff xdiff inc_ns full_ns`, AX and AH over the final context; exit 43 when ndiff or |
| 1338 | note xdiff is not 0. |
| 1339 | fn cmdadcmp to void |
| 1340 | if ne oargc 8 |
| 1341 | exit 2 |
| 1342 | end |
| 1343 | ockloop 2 |
| 1344 | nn = oargu 3 |
| 1345 | v = get og 0 |
| 1346 | if or le v 256 or gt nn 4096 gt mul nn v 1048576 |
| 1347 | exit 2 |
| 1348 | end |
| 1349 | obload 5 v |
| 1350 | oadset oargu 6 oargu 7 |
| 1351 | put okvs 0 0 |
| 1352 | put okvs 1 0 |
| 1353 | put okvs 2 0 |
| 1354 | put oadx 0 0 |
| 1355 | kvstart |
| 1356 | for s ulong nn |
| 1357 | kvpert s nn |
| 1358 | c = oadinc s |
| 1359 | ocpush c |
| 1360 | end |
| 1361 | kvstart |
| 1362 | for s2 ulong nn |
| 1363 | kvpert s2 nn |
| 1364 | oadfull s2 |
| 1365 | ocpush get okvk s2 |
| 1366 | end |
| 1367 | oadzero |
| 1368 | oadacc 0 get og 9 |
| 1369 | oadcline nn |
| 1370 | oadprint |
| 1371 | oadclr |
| 1372 | if or gt get okvs 2 0 gt get oadx 0 0 |
| 1373 | exit 43 |
| 1374 | end |
| 1375 | end |
| 1376 | |
| 1377 | fn oadcline nn ulong to void |
| 1378 | oputc 65 |
| 1379 | oputc 68 |
| 1380 | oputc 65 |
| 1381 | oputc 80 |
| 1382 | oputc 84 |
| 1383 | oputc 67 |
| 1384 | oputc 77 |
| 1385 | oputc 80 |
| 1386 | oputc 32 |
| 1387 | oputu nn |
| 1388 | oadsp get okvs 2 |
| 1389 | oadsp get oadx 0 |
| 1390 | oadsp get okvs 0 |
| 1391 | oadsp get okvs 1 |
| 1392 | oputc 10 |
| 1393 | oflush |
| 1394 | end |
| 1395 | |
| 1396 | fn oadsp v ulong to void |
| 1397 | oputc 32 |
| 1398 | oputu v |
| 1399 | end |
| 1400 | |
| 1401 | note FNV-1a 64 of the telemetry of rows 0 .. n - 1 |
| 1402 | fn oadrowh n ulong to ulong |
| 1403 | oadzero |
| 1404 | oadacc 0 n |
| 1405 | ret get oadk 0 |
| 1406 | end |
| 1407 | |
| 1408 | fn oadinc s ulong to ulong |
| 1409 | a = onsnow |
| 1410 | t0 = ofwdi |
| 1411 | olast |
| 1412 | e = sub onsnow a |
| 1413 | put okvs 0 add get okvs 0 e |
| 1414 | c = oargmax |
| 1415 | kvkeep s |
| 1416 | put okvk s c |
| 1417 | ct = get og 9 |
| 1418 | put oadhs s oadrowh ct |
| 1419 | oputc 65 |
| 1420 | oputc 73 |
| 1421 | kvline s ct t0 c long get onlxp sub ct 1 |
| 1422 | ret c |
| 1423 | end |
| 1424 | |
| 1425 | fn oadfull s ulong to void |
| 1426 | a = onsnow |
| 1427 | lf = ofwd 0 |
| 1428 | olast |
| 1429 | e = sub onsnow a |
| 1430 | put okvs 1 add get okvs 1 e |
| 1431 | nd = kvdiff s |
| 1432 | put okvs 2 add get okvs 2 nd |
| 1433 | ct = get og 9 |
| 1434 | xd = ulong 0 |
| 1435 | if ne oadrowh ct get oadhs s |
| 1436 | xd = 1 |
| 1437 | end |
| 1438 | put oadx 0 add get oadx 0 xd |
| 1439 | oputc 65 |
| 1440 | oputc 70 |
| 1441 | kvline s ct oargmax nd long xd |
| 1442 | end |
| 1443 | |
| 1444 | note logits of the last context position into TMP[0..V) (oa) |
| 1445 | fn olast to void |
| 1446 | d = get og 2 |
| 1447 | z = add get og 17 mul sub get og 9 1 d |
| 1448 | klogit z get og 27 get og 0 d |
| 1449 | end |
| 1450 | |
| 1451 | fn oargmax to ulong |
| 1452 | tm = get og 27 |
| 1453 | b = ulong 0 |
| 1454 | bv = get oa tm |
| 1455 | for c ulong get og 0 |
| 1456 | if gt get oa add tm c bv |
| 1457 | bv = get oa add tm c |
| 1458 | b = c |
| 1459 | end |
| 1460 | end |
| 1461 | ret b |
| 1462 | end |
| 1463 | |
| 1464 | fn odraw st ulong tau f64 to ulong |
| 1465 | tm = get og 27 |
| 1466 | v = get og 0 |
| 1467 | m = bitcast f64 ulong 18442240474082181120 |
| 1468 | for c ulong v |
| 1469 | x = div get oa add tm c tau |
| 1470 | put oa add tm c x |
| 1471 | m = fmax m x |
| 1472 | end |
| 1473 | sm = osoft tm v m |
| 1474 | u = mul f64 shr smx st 11 1.1102230246251565e-16 |
| 1475 | acc = 0.0f64 |
| 1476 | la = ulong 0 |
| 1477 | for k ulong v |
| 1478 | pk = get oa add tm k |
| 1479 | acc = add acc pk |
| 1480 | if lt u acc |
| 1481 | ret k |
| 1482 | end |
| 1483 | if gt pk 0.0 |
| 1484 | la = k |
| 1485 | end |
| 1486 | end |
| 1487 | ret la |
| 1488 | end |
| 1489 | |
| 1490 | note gcheck constants (derivation and Wolfram queries in docs/omegallm/README.md). GCH2 = GCH/4 is |
| 1491 | note the retry step for an index whose difference at GCH crossed a ReLU kink. |
| 1492 | const GCH f64 0.000001 |
| 1493 | const GCH2 f64 0.00000025 |
| 1494 | const GCFLOOR f64 0.000001 |
| 1495 | const GCTOL f64 0.001 |
| 1496 | note mathck: max relative error allowed against the correctly rounded reference |
| 1497 | const MCTOL f64 0.000000000000001 |
| 1498 | |
| 1499 | note gcheck CK TRAIN: the hand gradient of the step loss for the batch of the header step is |
| 1500 | note compared with central differences (L(p+h) - L(p-h))/((p+h) - (p-h)) at 4 indices of every |
| 1501 | note tensor instance (wte, wpe, per layer g1 wq wk wv wo g2 w1 w2, gf), drawn by splitmix64. |
| 1502 | note rel = |num - g| / max(|num|, |g|, GCFLOOR). An index with rel > GCTOL at GCH is measured |
| 1503 | note again at GCH2 and keeps the smaller rel (a wrong hand gradient fails at both steps). Prints |
| 1504 | note one `c index g_e12 num_e12` line per index (num of the kept step), then |
| 1505 | note `C count maxrel_bits_hex maxrel_e9 worst_index retries`; exit 40 when maxrel > GCTOL. |
| 1506 | fn cmdgchk to void |
| 1507 | if ne oargc 4 |
| 1508 | exit 2 |
| 1509 | end |
| 1510 | ockloop 2 |
| 1511 | odopen 3 |
| 1512 | obatch get og 41 |
| 1513 | ls = ograd |
| 1514 | d = get og 2 |
| 1515 | cnt = ulong 0 |
| 1516 | put og 50 0 |
| 1517 | put og 51 0 |
| 1518 | put og 52 0 |
| 1519 | put og 53 0 |
| 1520 | ogcten 0 mul get og 0 d 0 |
| 1521 | ogcten get og 30 mul get og 1 d 1 |
| 1522 | for l ulong get og 5 |
| 1523 | ogcten opg1 l d 2 |
| 1524 | ogcten opwq l mul d d 3 |
| 1525 | ogcten opwk l mul d d 4 |
| 1526 | ogcten opwv l mul d d 5 |
| 1527 | ogcten opwo l mul d d 6 |
| 1528 | ogcten opg2 l d 7 |
| 1529 | ogcten opw1 l mul d get og 4 8 |
| 1530 | ogcten opw2 l mul d get og 4 9 |
| 1531 | end |
| 1532 | ogcten get og 33 d 10 |
| 1533 | mx = bitcast f64 get og 51 |
| 1534 | oputc 67 |
| 1535 | oputc 32 |
| 1536 | oputu get og 50 |
| 1537 | oputc 32 |
| 1538 | oputx get og 51 |
| 1539 | oputc 32 |
| 1540 | oputl long mul mx 1000000000.0 |
| 1541 | oputc 32 |
| 1542 | oputu get og 52 |
| 1543 | oputc 32 |
| 1544 | oputu get og 53 |
| 1545 | oputc 10 |
| 1546 | oflush |
| 1547 | if gt mx GCTOL |
| 1548 | exit 40 |
| 1549 | end |
| 1550 | end |
| 1551 | |
| 1552 | note step-loss for the batch in obx/oby without touching the gradient |
| 1553 | fn oloss to f64 |
| 1554 | ls = 0.0f64 |
| 1555 | for b ulong get og 6 |
| 1556 | ls = add ls ofwd b |
| 1557 | end |
| 1558 | ret div ls f64 get og 6 |
| 1559 | end |
| 1560 | |
| 1561 | fn ogcten o ulong n ulong tag ulong to void |
| 1562 | for j ulong 4 |
| 1563 | i = add o rem smx xor get og 40 add mul get og 50 4 j n |
| 1564 | ogc1 i |
| 1565 | end |
| 1566 | end |
| 1567 | |
| 1568 | note central difference (L(p+h) - L(p-h))/((p+h) - (p-h)) at parameter i; restores p |
| 1569 | fn ogcnum i ulong h f64 to f64 |
| 1570 | p0 = owg i |
| 1571 | pp = add p0 h |
| 1572 | pm = sub p0 h |
| 1573 | owp i pp |
| 1574 | lp = oloss |
| 1575 | owp i pm |
| 1576 | lm = oloss |
| 1577 | owp i p0 |
| 1578 | ret div sub lp lm sub pp pm |
| 1579 | end |
| 1580 | |
| 1581 | fn ogcrel num f64 g f64 to f64 |
| 1582 | den = fmax fmax fabs num fabs g GCFLOOR |
| 1583 | ret div fabs sub num g den |
| 1584 | end |
| 1585 | |
| 1586 | note one gradient check at parameter i; updates og[50] count, og[51] max rel bits, og[52] worst |
| 1587 | note index, og[53] retries at GCH2 |
| 1588 | fn ogc1 i ulong to void |
| 1589 | g = ocsg i |
| 1590 | num = ogcnum i GCH |
| 1591 | rel = ogcrel num g |
| 1592 | if gt rel GCTOL |
| 1593 | put og 53 add get og 53 1 |
| 1594 | n2 = ogcnum i GCH2 |
| 1595 | r2 = ogcrel n2 g |
| 1596 | if lt r2 rel |
| 1597 | num = n2 |
| 1598 | rel = r2 |
| 1599 | end |
| 1600 | end |
| 1601 | oputc 99 |
| 1602 | oputc 32 |
| 1603 | oputu i |
| 1604 | oputc 32 |
| 1605 | oputl long mul g 1000000000000.0 |
| 1606 | oputc 32 |
| 1607 | oputl long mul num 1000000000000.0 |
| 1608 | oputc 10 |
| 1609 | put og 50 add get og 50 1 |
| 1610 | if gt rel bitcast f64 get og 51 |
| 1611 | put og 51 bitcast ulong rel |
| 1612 | put og 52 i |
| 1613 | end |
| 1614 | end |
| 1615 | |
| 1616 | note mathck REF: REF lines are `e XBITS RBITS` or `l XBITS RBITS` (16 lowercase hex digits each, |
| 1617 | note 36 bytes per line); RBITS is the correctly rounded Wolfram value of exp or log at the exact |
| 1618 | note f64 XBITS. Prints `M e count max_ulp maxrel_bits_hex maxrel_e18` and the same for l; |
| 1619 | note exit 42 when a special value (r = 0, inf or NaN) differs in bits, maxrel > MCTOL, or REF has |
| 1620 | note no e line or no l line (a check that checked nothing fails). |
| 1621 | fn cmdmath to void |
| 1622 | if ne oargc 3 |
| 1623 | exit 2 |
| 1624 | end |
| 1625 | n = odread 2 |
| 1626 | if ne rem n 36 0 |
| 1627 | exit 21 |
| 1628 | end |
| 1629 | for k ulong 6 |
| 1630 | put og add 50 k 0 |
| 1631 | end |
| 1632 | for i ulong div n 36 |
| 1633 | omline mul i 36 |
| 1634 | end |
| 1635 | omprint 101 0 |
| 1636 | omprint 108 3 |
| 1637 | if or gt bitcast f64 get og 52 MCTOL gt bitcast f64 get og 55 MCTOL |
| 1638 | exit 42 |
| 1639 | end |
| 1640 | if or eq get og 50 0 eq get og 53 0 |
| 1641 | exit 42 |
| 1642 | end |
| 1643 | end |
| 1644 | |
| 1645 | fn ohex o ulong to ulong |
| 1646 | v = ulong 0 |
| 1647 | for k ulong 16 |
| 1648 | c = odb add o k |
| 1649 | if lt c 58 |
| 1650 | v = or shl v 4 sub c 48 |
| 1651 | else |
| 1652 | v = or shl v 4 sub c 87 |
| 1653 | end |
| 1654 | end |
| 1655 | ret v |
| 1656 | end |
| 1657 | |
| 1658 | note og[50+b]: count, max ulp, max rel bits (b = 0 for exp, 3 for log) |
| 1659 | fn omline o ulong to void |
| 1660 | kd = odb o |
| 1661 | x = bitcast f64 ohex add o 2 |
| 1662 | rb = ohex add o 19 |
| 1663 | b = ulong 0 |
| 1664 | y = 0.0f64 |
| 1665 | if eq kd 101 |
| 1666 | y = fexp x |
| 1667 | else |
| 1668 | b = ulong 3 |
| 1669 | y = flog x |
| 1670 | end |
| 1671 | yb = bitcast ulong y |
| 1672 | put og add 50 b add get og add 50 b 1 |
| 1673 | r = bitcast f64 rb |
| 1674 | if or or eq r 0.0 ne r r eq fabs r bitcast f64 ulong 9218868437227405312 |
| 1675 | if ne yb rb |
| 1676 | exit 42 |
| 1677 | end |
| 1678 | ret |
| 1679 | end |
| 1680 | ul = ulong 0 |
| 1681 | if gt yb rb |
| 1682 | ul = sub yb rb |
| 1683 | else |
| 1684 | ul = sub rb yb |
| 1685 | end |
| 1686 | if gt ul get og add 51 b |
| 1687 | put og add 51 b ul |
| 1688 | end |
| 1689 | rel = div fabs sub y r fabs r |
| 1690 | if gt rel bitcast f64 get og add 52 b |
| 1691 | put og add 52 b bitcast ulong rel |
| 1692 | end |
| 1693 | end |
| 1694 | |
| 1695 | fn omprint c ulong b ulong to void |
| 1696 | oputc 77 |
| 1697 | oputc 32 |
| 1698 | oputc c |
| 1699 | oputc 32 |
| 1700 | oputu get og add 50 b |
| 1701 | oputc 32 |
| 1702 | oputu get og add 51 b |
| 1703 | oputc 32 |
| 1704 | oputx get og add 52 b |
| 1705 | oputc 32 |
| 1706 | oputl long mul bitcast f64 get og add 52 b 1e18 |
| 1707 | oputc 10 |
| 1708 | oflush |
| 1709 | end |
| 1710 | |
| 1711 | note tune CK OUT TOTAL LRMICRO: fine-tune entry. Load CK (any checkpoint ckpt.om accepts), keep |
| 1712 | note its shapes, G and weights, and start a new schedule on them: step 0, the given TOTAL and |
| 1713 | note lr = f64(LRMICRO)/1000000.0, Adam moments m = v = 0 (so warmup and bias correction restart). |
| 1714 | note Save to OUT with the loaded magic and print `U P`. train, gtrain and gtrainf then continue |
| 1715 | note from OUT on any token file. TOTAL 0 or over 999999 exits 12; wrong argument count exits 2. |
| 1716 | fn cmdtune to void |
| 1717 | if ne oargc 6 |
| 1718 | exit 2 |
| 1719 | end |
| 1720 | ockloop 2 |
| 1721 | tt = oargu 4 |
| 1722 | if or eq tt 0 gt tt 999999 |
| 1723 | exit 12 |
| 1724 | end |
| 1725 | put og 41 0 |
| 1726 | put og 42 tt |
| 1727 | put og 43 bitcast ulong div f64 oargu 5 1000000.0 |
| 1728 | ozerom mul 2 get og 8 |
| 1729 | ocksave 3 |
| 1730 | oputc 85 |
| 1731 | oputc 32 |
| 1732 | oputu get og 8 |
| 1733 | oputc 10 |
| 1734 | oflush |
| 1735 | end |
| 1736 | |
| 1737 | |
| 1738 | note grow CK OUT L TOTAL LRMICRO: keep embeddings, old layers and final gain byte for byte. |
| 1739 | note Added layers use the own initializer with Wo and W2 zeroed, so both residual branches |
| 1740 | note initially contribute zero. Their input projections remain initialized and trainable. |
| 1741 | note The existing gradient cold store snapshots old weights while geometry changes; oinit |
| 1742 | note writes parameters and moments only. Since P grows, new moments cannot alias the snapshot. |
| 1743 | note Like tune, restart step and Adam, preserving seed, G, vocabulary and checkpoint format. |
| 1744 | note OUT uses normal checkpoint save semantics (an existing path is replaced). |
| 1745 | fn ogrowsnap n ulong to void |
| 1746 | for q ulong ocn n |
| 1747 | i = mul q OCH |
| 1748 | ie = oce q n |
| 1749 | while lt i ie bound 999999 |
| 1750 | v = owg i |
| 1751 | note Reject non-finite source weights before attempting a growth operation. |
| 1752 | if eq and shr bitcast ulong v 52 2047 2047 |
| 1753 | exit 54 |
| 1754 | end |
| 1755 | ocsp i v |
| 1756 | i = add i 1 |
| 1757 | end |
| 1758 | end |
| 1759 | end |
| 1760 | |
| 1761 | fn ogrowcopy n ulong to void |
| 1762 | for q ulong ocn n |
| 1763 | i = mul q OCH |
| 1764 | ie = oce q n |
| 1765 | while lt i ie bound 999999 |
| 1766 | owp i ocsg i |
| 1767 | i = add i 1 |
| 1768 | end |
| 1769 | end |
| 1770 | end |
| 1771 | |
| 1772 | note Keep loop temporaries out of cmdgrow: every target must fit its register budget. |
| 1773 | fn ogrowsilent first ulong to void |
| 1774 | for j ulong sub get og 5 first |
| 1775 | l = add first j |
| 1776 | oiset opwo l mul get og 2 get og 2 0.0 |
| 1777 | oiset opw2 l mul get og 2 get og 4 0.0 |
| 1778 | end |
| 1779 | end |
| 1780 | |
| 1781 | fn cmdgrow to void |
| 1782 | if ne oargc 7 |
| 1783 | exit 2 |
| 1784 | end |
| 1785 | ockload 2 |
| 1786 | ll = oargu 4 |
| 1787 | tt = oargu 5 |
| 1788 | lr = oargu 6 |
| 1789 | if or le ll get og 5 or eq tt 0 gt tt 999999 |
| 1790 | exit 12 |
| 1791 | end |
| 1792 | if eq lr 0 |
| 1793 | exit 12 |
| 1794 | end |
| 1795 | oldl = get og 5 |
| 1796 | oldg = get og 33 |
| 1797 | ogrowsnap get og 8 |
| 1798 | put og 5 ll |
| 1799 | if not ogeom |
| 1800 | exit 12 |
| 1801 | end |
| 1802 | put og 41 0 |
| 1803 | put og 42 tt |
| 1804 | put og 43 bitcast ulong div f64 lr 1000000.0 |
| 1805 | oinit |
| 1806 | ogrowcopy oldg |
| 1807 | for j ulong get og 2 |
| 1808 | owp add get og 33 j ocsg add oldg j |
| 1809 | end |
| 1810 | ogrowsilent oldl |
| 1811 | ocksave 3 |
| 1812 | oputc 71 |
| 1813 | oputc 32 |
| 1814 | oputu oldl |
| 1815 | oputc 32 |
| 1816 | oputu ll |
| 1817 | oputc 32 |
| 1818 | oputu get og 8 |
| 1819 | oputc 10 |
| 1820 | oflush |
| 1821 | end |