omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / train / omega-llm / 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 1684107879 |
| 82 | cmdgrad |
| 83 | ret 0 |
| 84 | end |
| 85 | if eq w 474214396532 |
| 86 | cmdtrain |
| 87 | ret 0 |
| 88 | end |
| 89 | if eq w 1818326629 |
| 90 | cmdeval |
| 91 | ret 0 |
| 92 | end |
| 93 | if eq w 1702060386 |
| 94 | cmdbase |
| 95 | ret 0 |
| 96 | end |
| 97 | if eq w 111516417089907 |
| 98 | cmdsamp |
| 99 | ret 0 |
| 100 | end |
| 101 | if eq w 118074647274343 |
| 102 | cmdgchk |
| 103 | ret 0 |
| 104 | end |
| 105 | if eq w 118074698391917 |
| 106 | cmdmath |
| 107 | ret 0 |
| 108 | end |
| 109 | if eq w 118074781033569 |
| 110 | cmdadamck |
| 111 | ret 0 |
| 112 | end |
| 113 | if eq w 435778905459 |
| 114 | cmdserve |
| 115 | ret 0 |
| 116 | end |
| 117 | if eq w 1701737844 |
| 118 | cmdtune |
| 119 | ret 0 |
| 120 | end |
| 121 | note grow: append initially silent residual layers and restart training. |
| 122 | if eq w 2003792487 |
| 123 | cmdgrow |
| 124 | ret 0 |
| 125 | end |
| 126 | note kvcmp (ham/ollm-speed) 482871572075 |
| 127 | if eq w 482871572075 |
| 128 | cmdkvcmp |
| 129 | ret 0 |
| 130 | end |
| 131 | if eq w 112624400429684 |
| 132 | cmdtrainf |
| 133 | ret 0 |
| 134 | end |
| 135 | if eq w 30506458620916852 |
| 136 | cmdttteval |
| 137 | ret 0 |
| 138 | end |
| 139 | if eq w 7809653406635421540 |
| 140 | cmddolaeval |
| 141 | ret 0 |
| 142 | end |
| 143 | if eq w 31645462707860596 |
| 144 | cmdtttsamp |
| 145 | ret 0 |
| 146 | end |
| 147 | if eq w 500236120179 |
| 148 | cmdspext |
| 149 | ret 0 |
| 150 | end |
| 151 | if eq w 482921771123 |
| 152 | cmdspapp |
| 153 | ret 0 |
| 154 | end |
| 155 | if eq w 123615088701555 |
| 156 | cmdspsamp |
| 157 | ret 0 |
| 158 | end |
| 159 | if eq w 119165853986931 |
| 160 | cmdspeval |
| 161 | ret 0 |
| 162 | end |
| 163 | if eq w 111499018330982 |
| 164 | cmdfcache |
| 165 | ret 0 |
| 166 | end |
| 167 | if eq w 27978647094784628 |
| 168 | cmdtrainfc |
| 169 | ret 0 |
| 170 | end |
| 171 | if eq w 30792271479792486 |
| 172 | cmdfcachem |
| 173 | ret 0 |
| 174 | end |
| 175 | if eq w 28542713673379955 |
| 176 | cmdspmerge |
| 177 | ret 0 |
| 178 | end |
| 179 | if eq w 30227092724740211 |
| 180 | cmdspstack |
| 181 | ret 0 |
| 182 | end |
| 183 | if eq w 31369549780578919 |
| 184 | cmdgrowpro |
| 185 | ret 0 |
| 186 | end |
| 187 | if eq w 495773380723 |
| 188 | cmdspens |
| 189 | ret 0 |
| 190 | end |
| 191 | if eq w 123598092333171 |
| 192 | cmdspclip |
| 193 | ret 0 |
| 194 | end |
| 195 | if eq w 0 |
| 196 | lo = ocmdlo |
| 197 | hi = ocmdhi |
| 198 | if and eq lo 7020664736201273459 eq hi 28784 |
| 199 | cmdspchainapp |
| 200 | ret 0 |
| 201 | end |
| 202 | if and eq lo 7308895112352985203 eq hi 7102838 |
| 203 | cmdspchaineval |
| 204 | ret 0 |
| 205 | end |
| 206 | if and eq lo 8317701428883976307 eq hi 7368033 |
| 207 | cmdspchainsamp |
| 208 | ret 0 |
| 209 | end |
| 210 | end |
| 211 | exit 2 |
| 212 | end |
| 213 | |
| 214 | note init CK V T D H F L B SEED TOTAL LRMICRO [G [LP LC NP]]; lr = f64(LRMICRO)/1000000.0. Without G |
| 215 | note the file is OLLMCK01 (G = 1); with G it is OLLMCK02 (CONTRACT-V1 section 6). LP LC NP make the |
| 216 | note LC layers from layer LP a core that runs NP times with the same weights (model.om, docs/LOOP.md): |
| 217 | note the file is then OLLMCK03. NP 1 is no loop and gives the OLLMCK02 file; NP 0 is exit 2; a core |
| 218 | note that is empty or does not fit in the L layers is exit 12. |
| 219 | fn cmdinit to void |
| 220 | na = oargc |
| 221 | if and and ne na 13 ne na 14 ne na 17 |
| 222 | exit 2 |
| 223 | end |
| 224 | for i ulong 7 |
| 225 | put og i oargu add 3 i |
| 226 | end |
| 227 | put og 57 0 |
| 228 | put og 58 0 |
| 229 | put og 59 0 |
| 230 | if eq na 17 |
| 231 | oloopa |
| 232 | end |
| 233 | if not ogeom |
| 234 | exit 12 |
| 235 | end |
| 236 | put og 40 oargu 10 |
| 237 | tt = oargu 11 |
| 238 | if gt tt 999999 |
| 239 | exit 12 |
| 240 | end |
| 241 | g = ulong 1 |
| 242 | put og 46 1 |
| 243 | if ge na 14 |
| 244 | g = oargu 13 |
| 245 | put og 46 2 |
| 246 | end |
| 247 | if not ogok2 g |
| 248 | exit 12 |
| 249 | end |
| 250 | put og 44 g |
| 251 | put og 42 tt |
| 252 | put og 43 bitcast ulong div f64 oargu 12 1000000.0 |
| 253 | put og 41 0 |
| 254 | oinit |
| 255 | ocksave 2 |
| 256 | oputc 73 |
| 257 | oputc 32 |
| 258 | oputu get og 8 |
| 259 | oputc 10 |
| 260 | oflush |
| 261 | end |
| 262 | |
| 263 | note the loop arguments LP LC NP of init (argv 14 .. 16) into og 57 .. 59; NP 1 leaves no loop |
| 264 | fn oloopa to void |
| 265 | np = oargu 16 |
| 266 | if eq np 0 |
| 267 | exit 2 |
| 268 | end |
| 269 | if gt np 1 |
| 270 | put og 57 oargu 14 |
| 271 | put og 58 oargu 15 |
| 272 | put og 59 np |
| 273 | end |
| 274 | end |
| 275 | |
| 276 | note little-endian u64 at ochk[o..o+8) |
| 277 | fn ochk64 o ulong to ulong |
| 278 | v = ulong 0 |
| 279 | for k ulong 8 |
| 280 | x = ulong get ochk add o k |
| 281 | v = or v shl x mul k 8 |
| 282 | end |
| 283 | ret v |
| 284 | end |
| 285 | |
| 286 | note row b from offset ochk[b] (checked by ooffok) |
| 287 | fn ooffrow b ulong to void |
| 288 | obrow b ochk64 mul b 8 |
| 289 | end |
| 290 | |
| 291 | note exit 22 when any offset has o >= N - T (written so that an offset near 2^64 cannot wrap) |
| 292 | fn ooffok to void |
| 293 | n = get odn 0 |
| 294 | tt = get og 1 |
| 295 | for b ulong get og 6 |
| 296 | if ge ochk64 mul b 8 sub n tt |
| 297 | exit 22 |
| 298 | end |
| 299 | end |
| 300 | end |
| 301 | |
| 302 | note grad CK DATA OFFS OUT: the gradient of CONTRACT-V1 section 5 (G blocks, K = 1) |
| 303 | fn cmdgrad to void |
| 304 | if ne oargc 6 |
| 305 | exit 2 |
| 306 | end |
| 307 | ockloop 2 |
| 308 | odopen 3 |
| 309 | ooffrd 4 |
| 310 | ooffok |
| 311 | for b ulong get og 6 |
| 312 | ooffrow b |
| 313 | end |
| 314 | ls = ograd |
| 315 | ogrsave 5 |
| 316 | oputc 71 |
| 317 | oputc 32 |
| 318 | oputx bitcast ulong ls |
| 319 | oputc 32 |
| 320 | oputl rmic ls |
| 321 | oputc 10 |
| 322 | oflush |
| 323 | end |
| 324 | |
| 325 | note train CK TRAIN UNTIL EVERY [K]: steps step .. min(UNTIL, TOTAL)-1; after step s, when |
| 326 | note (s+1) mod EVERY = 0 or s+1 is the end, save CK and print `T s+1 loss_micro(step s)`. |
| 327 | note K worker processes (CONTRACT-V1 section 5.1): K < 1, K > G or G mod K != 0 is exit 29. |
| 328 | fn cmdtrain to void |
| 329 | na = oargc |
| 330 | if and ne na 6 ne na 7 |
| 331 | exit 2 |
| 332 | end |
| 333 | ockloop 2 |
| 334 | odopen 3 |
| 335 | un = oargu 4 |
| 336 | ev = oargu 5 |
| 337 | if eq ev 0 |
| 338 | exit 2 |
| 339 | end |
| 340 | k = ulong 1 |
| 341 | if eq na 7 |
| 342 | k = oargu 6 |
| 343 | end |
| 344 | gg = get og 44 |
| 345 | if or eq k 0 gt k gg |
| 346 | exit 29 |
| 347 | end |
| 348 | if ne rem gg k 0 |
| 349 | exit 29 |
| 350 | end |
| 351 | put og 45 k |
| 352 | en = get og 42 |
| 353 | if lt un en |
| 354 | en = un |
| 355 | end |
| 356 | while lt get og 41 en bound 999999 |
| 357 | ls = ostep |
| 358 | s1 = get og 41 |
| 359 | if or eq rem s1 ev 0 eq s1 en |
| 360 | ocksave 2 |
| 361 | oputc 84 |
| 362 | oputc 32 |
| 363 | oputu s1 |
| 364 | oputc 32 |
| 365 | oputl rmic ls |
| 366 | oputc 10 |
| 367 | oflush |
| 368 | end |
| 369 | end |
| 370 | end |
| 371 | |
| 372 | note eval CK HELD W [MERGES]: windows at 0, T, .., (W-1)T; prints `E step W loss_micro bpb_micro`. |
| 373 | note V > 256 needs MERGES (else exit 2); bpb = ((loss/ln2) f64(W T))/f64(nbytes) with nbytes the |
| 374 | note decoded length of the W T targets (CONTRACT-V1 section 7.1); V = 256 keeps loss/ln2. |
| 375 | fn cmdeval to void |
| 376 | na = oargc |
| 377 | if and ne na 5 ne na 6 |
| 378 | exit 2 |
| 379 | end |
| 380 | ockloop 2 |
| 381 | ww = oargu 4 |
| 382 | v = get og 0 |
| 383 | if gt v 256 |
| 384 | if eq na 5 |
| 385 | exit 2 |
| 386 | end |
| 387 | obload 5 v |
| 388 | end |
| 389 | odopen 3 |
| 390 | tt = get og 1 |
| 391 | m = get odn 0 |
| 392 | note W T + 1 > N, tested as W > (N - 1) / T so that a huge W cannot wrap W T |
| 393 | if eq ww 0 |
| 394 | exit 23 |
| 395 | end |
| 396 | if gt ww div sub m 1 tt |
| 397 | exit 23 |
| 398 | end |
| 399 | ls = 0.0f64 |
| 400 | nb = ulong 0 |
| 401 | for w ulong ww |
| 402 | obrow 0 mul w tt |
| 403 | ls = add ls ofwd 0 |
| 404 | if gt v 256 |
| 405 | nb = add nb oevnb tt |
| 406 | end |
| 407 | end |
| 408 | lo = div ls f64 ww |
| 409 | bp = div lo 0.6931471805599453 |
| 410 | if gt v 256 |
| 411 | bp = div mul bp f64 mul ww tt f64 nb |
| 412 | end |
| 413 | oputc 69 |
| 414 | oputc 32 |
| 415 | oputu get og 41 |
| 416 | oputc 32 |
| 417 | oputu ww |
| 418 | oputc 32 |
| 419 | oputl rmic lo |
| 420 | oputc 32 |
| 421 | oputl rmic bp |
| 422 | oputc 10 |
| 423 | oflush |
| 424 | end |
| 425 | |
| 426 | note decoded bytes of the targets oby[0 .. n) |
| 427 | fn oevnb n ulong to ulong |
| 428 | s = ulong 0 |
| 429 | for t ulong n |
| 430 | s = add s obtlen get oby t |
| 431 | end |
| 432 | ret s |
| 433 | end |
| 434 | |
| 435 | note append token c to the context window obx[0 .. CT), keeping the last T tokens |
| 436 | fn ocpush c ulong to void |
| 437 | cn = get og 9 |
| 438 | tt = get og 1 |
| 439 | if lt cn tt |
| 440 | put obx cn c |
| 441 | put og 9 add cn 1 |
| 442 | else |
| 443 | for i ulong sub tt 1 |
| 444 | put obx i get obx add i 1 |
| 445 | end |
| 446 | put obx sub tt 1 c |
| 447 | end |
| 448 | end |
| 449 | |
| 450 | note sample CK SEED N TEMPMILLI PROMPT [MERGES]. Context = token 0, then PROMPT (bytes when V = 256, |
| 451 | note its encoding when V > 256, CONTRACT-V1 section 7.2); the model reads the last T context tokens |
| 452 | note at positions 0..; the next token comes from the logits at the last position. |
| 453 | note TEMPMILLI 0: argmax (lowest index on ties), no draw. Otherwise softmax(logits/(TEMPMILLI/1000)) |
| 454 | note and u = (splitmix64 stream out >> 11) 2^-53 with state = SEED, out = mix(state += GOLD); |
| 455 | note the token is the smallest c with u < p[0] + .. + p[c], else the last c with p[c] > 0. |
| 456 | note A generated token 0 ends the output and is not written; another writes its decoded bytes. |
| 457 | fn cmdsamp to void |
| 458 | na = oargc |
| 459 | if and ne na 7 ne na 8 |
| 460 | exit 2 |
| 461 | end |
| 462 | ockloop 2 |
| 463 | st = oargu 3 |
| 464 | nn = oargu 4 |
| 465 | if gt nn 999999 |
| 466 | exit 2 |
| 467 | end |
| 468 | tm = oargu 5 |
| 469 | v = get og 0 |
| 470 | if gt v 256 |
| 471 | if eq na 7 |
| 472 | exit 2 |
| 473 | end |
| 474 | obload 7 v |
| 475 | end |
| 476 | ochipup |
| 477 | osampleloaded st nn tm |
| 478 | end |
| 479 | |
| 480 | note Embedding entry: checkpoint and tokenizer already loaded; argument 6 supplies the prompt. |
| 481 | note The caller keeps exclusive ownership of the model pools until this returns. |
| 482 | fn osampleloaded seed ulong nn ulong tm ulong to void |
| 483 | if gt nn 999999 |
| 484 | exit 2 |
| 485 | end |
| 486 | st = seed |
| 487 | v = get og 0 |
| 488 | put og 9 0 |
| 489 | ocpush 0 |
| 490 | if gt v 256 |
| 491 | r = oargr 6 |
| 492 | if or lt r 0 gt r 4000 |
| 493 | exit 2 |
| 494 | end |
| 495 | np = obprompt 6 |
| 496 | for i ulong np |
| 497 | ocpush get obtk i |
| 498 | end |
| 499 | else |
| 500 | r = oargr 6 |
| 501 | if or lt r 0 gt r 4096 |
| 502 | exit 2 |
| 503 | end |
| 504 | for i ulong ulong r |
| 505 | ocpush ulong get oarg i |
| 506 | end |
| 507 | end |
| 508 | for g ulong nn |
| 509 | c = osstep st tm |
| 510 | if ne tm 0 |
| 511 | st = add st 11400714819323198485 |
| 512 | end |
| 513 | if eq c 0 |
| 514 | break |
| 515 | end |
| 516 | if gt v 256 |
| 517 | obtput c |
| 518 | else |
| 519 | oputc c |
| 520 | end |
| 521 | note Flush each decoded token so interactive consumers see progress. Bytes and sampling stay unchanged. |
| 522 | oflush |
| 523 | ocpush c |
| 524 | end |
| 525 | oflush |
| 526 | end |
| 527 | |
| 528 | note one sampling step (cmdsamp, serve.om): the next token from the logits at the last context |
| 529 | note position, argmax when TEMPMILLI tm is 0, else a draw with the splitmix64 stream state st. |
| 530 | fn osstep st ulong tm ulong to ulong |
| 531 | olastf |
| 532 | if eq tm 0 |
| 533 | ret oargmax |
| 534 | end |
| 535 | ret odraw st div f64 tm 1000.0 |
| 536 | end |
| 537 | |
| 538 | note ogpu[0] = 1 when the per-token forward runs on the chip: gpu/gg.om's gserve and gsample set it |
| 539 | note before cmdserve or cmdsamp; nogpu.om's ogchip and oglast refuse with exit 2 on the CPU targets. |
| 540 | global ogpu ulong 1 |
| 541 | |
| 542 | note after the checkpoint loads: on the chip path, open the chip and upload the parameters once |
| 543 | note (gpu/gg.om plans one activation block per layer application, so a looped model runs there too). |
| 544 | fn ochipup to void |
| 545 | if ne get ogpu 0 0 |
| 546 | ogchip |
| 547 | end |
| 548 | end |
| 549 | |
| 550 | note forward of the context in obx row 0, then its last position's logits into TMP[0..V) (oa), |
| 551 | note on the chip when ogpu[0] = 1 (oglast), else on the CPU (ofwdi, model.om: only the rows past the |
| 552 | note prefix the previous call computed, then olast) |
| 553 | fn olastf to void |
| 554 | if ne get ogpu 0 0 |
| 555 | oglast |
| 556 | ret |
| 557 | end |
| 558 | t0 = ofwdi |
| 559 | olast |
| 560 | end |
| 561 | |
| 562 | note kvcmp CK N PROMPT MERGES (ham/ollm-speed, V > 256, N V <= 2^20): ofwdi against ofwd 0. Pass 1: |
| 563 | note N greedy steps from token 0 then PROMPT's encoding with ofwdi then olast (the CPU decode), every |
| 564 | note step's V logits kept in okvr; prints `KI step ct t0 tok ns`. Pass 2: the same contexts again |
| 565 | note (pass 1's tokens pushed) with ofwd 0 then olast; prints `KF step ct tok ndiff ns`, ndiff = logits |
| 566 | note whose f64 bits differ from pass 1's. Before step N/2 of both passes the context token at CT - 5 |
| 567 | note (when CT > 5) becomes its successor mod V, so ofwdi must find the changed prefix. Last line |
| 568 | note `KVCMP N ndiff inc_ns full_ns` (forward and logits time summed over the steps); exit 43 when |
| 569 | note ndiff > 0. |
| 570 | global okvr f64 1048576 |
| 571 | global okvk ulong 4096 |
| 572 | fn kvstart to void |
| 573 | put og 9 0 |
| 574 | ocpush 0 |
| 575 | np = obprompt 4 |
| 576 | for i ulong np |
| 577 | ocpush get obtk i |
| 578 | end |
| 579 | end |
| 580 | |
| 581 | fn kvpert s ulong nn ulong to void |
| 582 | ct = get og 9 |
| 583 | if and eq s div nn 2 gt ct 5 |
| 584 | i = sub ct 5 |
| 585 | put obx i rem add get obx i 1 get og 0 |
| 586 | end |
| 587 | end |
| 588 | |
| 589 | fn kvline a ulong b ulong c ulong d ulong e long to void |
| 590 | oputc 32 |
| 591 | oputu a |
| 592 | oputc 32 |
| 593 | oputu b |
| 594 | oputc 32 |
| 595 | oputu c |
| 596 | oputc 32 |
| 597 | oputu d |
| 598 | oputc 32 |
| 599 | oputl e |
| 600 | oputc 10 |
| 601 | oflush |
| 602 | end |
| 603 | |
| 604 | note kvcmp totals: okvs[0] pass 1 ns, [1] pass 2 ns, [2] differing logits |
| 605 | global okvs ulong 3 |
| 606 | fn cmdkvcmp to void |
| 607 | if ne oargc 6 |
| 608 | exit 2 |
| 609 | end |
| 610 | ockloop 2 |
| 611 | nn = oargu 3 |
| 612 | v = get og 0 |
| 613 | if or le v 256 or gt nn 4096 gt mul nn v 1048576 |
| 614 | exit 2 |
| 615 | end |
| 616 | obload 5 v |
| 617 | put okvs 0 0 |
| 618 | put okvs 1 0 |
| 619 | put okvs 2 0 |
| 620 | kvstart |
| 621 | for s ulong nn |
| 622 | kvpert s nn |
| 623 | c = kvinc s |
| 624 | ocpush c |
| 625 | end |
| 626 | kvstart |
| 627 | for s2 ulong nn |
| 628 | kvpert s2 nn |
| 629 | kvfull s2 |
| 630 | ocpush get okvk s2 |
| 631 | end |
| 632 | oputc 75 |
| 633 | oputc 86 |
| 634 | oputc 67 |
| 635 | oputc 77 |
| 636 | oputc 80 |
| 637 | oputc 32 |
| 638 | oputu nn |
| 639 | oputc 32 |
| 640 | oputu get okvs 2 |
| 641 | oputc 32 |
| 642 | oputu get okvs 0 |
| 643 | oputc 32 |
| 644 | oputu get okvs 1 |
| 645 | oputc 10 |
| 646 | oflush |
| 647 | if gt get okvs 2 0 |
| 648 | exit 43 |
| 649 | end |
| 650 | end |
| 651 | |
| 652 | note pass 1 step s: ofwdi then olast, logits into okvr row s, KI line; returns the greedy token |
| 653 | fn kvinc s ulong to ulong |
| 654 | a = onsnow |
| 655 | t0 = ofwdi |
| 656 | olast |
| 657 | e = sub onsnow a |
| 658 | put okvs 0 add get okvs 0 e |
| 659 | c = oargmax |
| 660 | kvkeep s |
| 661 | put okvk s c |
| 662 | oputc 75 |
| 663 | oputc 73 |
| 664 | kvline s get og 9 t0 c long e |
| 665 | ret c |
| 666 | end |
| 667 | |
| 668 | note okvr row s = the V logits in TMP |
| 669 | fn kvkeep s ulong to void |
| 670 | v = get og 0 |
| 671 | tm = get og 27 |
| 672 | o = mul s v |
| 673 | for j ulong v |
| 674 | x = get oa add tm j |
| 675 | put okvr add o j x |
| 676 | end |
| 677 | end |
| 678 | |
| 679 | note the logits in TMP whose f64 bits differ from okvr row s |
| 680 | fn kvdiff s ulong to ulong |
| 681 | v = get og 0 |
| 682 | tm = get og 27 |
| 683 | o = mul s v |
| 684 | nd = ulong 0 |
| 685 | for j ulong v |
| 686 | x = bitcast ulong get oa add tm j |
| 687 | y = bitcast ulong get okvr add o j |
| 688 | if ne x y |
| 689 | nd = add nd 1 |
| 690 | end |
| 691 | end |
| 692 | ret nd |
| 693 | end |
| 694 | |
| 695 | note pass 2 step s: ofwd 0 then olast, compared bit for bit with okvr row s, KF line |
| 696 | fn kvfull s ulong to void |
| 697 | a = onsnow |
| 698 | lf = ofwd 0 |
| 699 | olast |
| 700 | e = sub onsnow a |
| 701 | put okvs 1 add get okvs 1 e |
| 702 | nd = kvdiff s |
| 703 | put okvs 2 add get okvs 2 nd |
| 704 | oputc 75 |
| 705 | oputc 70 |
| 706 | kvline s get og 9 oargmax nd long e |
| 707 | end |
| 708 | |
| 709 | note logits of the last context position into TMP[0..V) (oa) |
| 710 | fn olast to void |
| 711 | d = get og 2 |
| 712 | z = add get og 17 mul sub get og 9 1 d |
| 713 | klogit z get og 27 get og 0 d |
| 714 | end |
| 715 | |
| 716 | fn oargmax to ulong |
| 717 | tm = get og 27 |
| 718 | b = ulong 0 |
| 719 | bv = get oa tm |
| 720 | for c ulong get og 0 |
| 721 | if gt get oa add tm c bv |
| 722 | bv = get oa add tm c |
| 723 | b = c |
| 724 | end |
| 725 | end |
| 726 | ret b |
| 727 | end |
| 728 | |
| 729 | fn odraw st ulong tau f64 to ulong |
| 730 | tm = get og 27 |
| 731 | v = get og 0 |
| 732 | m = bitcast f64 ulong 18442240474082181120 |
| 733 | for c ulong v |
| 734 | x = div get oa add tm c tau |
| 735 | put oa add tm c x |
| 736 | m = fmax m x |
| 737 | end |
| 738 | sm = osoft tm v m |
| 739 | u = mul f64 shr smx st 11 1.1102230246251565e-16 |
| 740 | acc = 0.0f64 |
| 741 | la = ulong 0 |
| 742 | for k ulong v |
| 743 | pk = get oa add tm k |
| 744 | acc = add acc pk |
| 745 | if lt u acc |
| 746 | ret k |
| 747 | end |
| 748 | if gt pk 0.0 |
| 749 | la = k |
| 750 | end |
| 751 | end |
| 752 | ret la |
| 753 | end |
| 754 | |
| 755 | note gcheck constants (derivation and Wolfram queries in docs/omegallm/README.md). GCH2 = GCH/4 is |
| 756 | note the retry step for an index whose difference at GCH crossed a ReLU kink. |
| 757 | const GCH f64 0.000001 |
| 758 | const GCH2 f64 0.00000025 |
| 759 | const GCFLOOR f64 0.000001 |
| 760 | const GCTOL f64 0.001 |
| 761 | note mathck: max relative error allowed against the correctly rounded reference |
| 762 | const MCTOL f64 0.000000000000001 |
| 763 | |
| 764 | note gcheck CK TRAIN: the hand gradient of the step loss for the batch of the header step is |
| 765 | note compared with central differences (L(p+h) - L(p-h))/((p+h) - (p-h)) at 4 indices of every |
| 766 | note tensor instance (wte, wpe, per layer g1 wq wk wv wo g2 w1 w2, gf), drawn by splitmix64. |
| 767 | note rel = |num - g| / max(|num|, |g|, GCFLOOR). An index with rel > GCTOL at GCH is measured |
| 768 | note again at GCH2 and keeps the smaller rel (a wrong hand gradient fails at both steps). Prints |
| 769 | note one `c index g_e12 num_e12` line per index (num of the kept step), then |
| 770 | note `C count maxrel_bits_hex maxrel_e9 worst_index retries`; exit 40 when maxrel > GCTOL. |
| 771 | fn cmdgchk to void |
| 772 | if ne oargc 4 |
| 773 | exit 2 |
| 774 | end |
| 775 | ockloop 2 |
| 776 | odopen 3 |
| 777 | obatch get og 41 |
| 778 | ls = ograd |
| 779 | d = get og 2 |
| 780 | cnt = ulong 0 |
| 781 | put og 50 0 |
| 782 | put og 51 0 |
| 783 | put og 52 0 |
| 784 | put og 53 0 |
| 785 | ogcten 0 mul get og 0 d 0 |
| 786 | ogcten get og 30 mul get og 1 d 1 |
| 787 | for l ulong get og 5 |
| 788 | ogcten opg1 l d 2 |
| 789 | ogcten opwq l mul d d 3 |
| 790 | ogcten opwk l mul d d 4 |
| 791 | ogcten opwv l mul d d 5 |
| 792 | ogcten opwo l mul d d 6 |
| 793 | ogcten opg2 l d 7 |
| 794 | ogcten opw1 l mul d get og 4 8 |
| 795 | ogcten opw2 l mul d get og 4 9 |
| 796 | end |
| 797 | ogcten get og 33 d 10 |
| 798 | mx = bitcast f64 get og 51 |
| 799 | oputc 67 |
| 800 | oputc 32 |
| 801 | oputu get og 50 |
| 802 | oputc 32 |
| 803 | oputx get og 51 |
| 804 | oputc 32 |
| 805 | oputl long mul mx 1000000000.0 |
| 806 | oputc 32 |
| 807 | oputu get og 52 |
| 808 | oputc 32 |
| 809 | oputu get og 53 |
| 810 | oputc 10 |
| 811 | oflush |
| 812 | if gt mx GCTOL |
| 813 | exit 40 |
| 814 | end |
| 815 | end |
| 816 | |
| 817 | note step-loss for the batch in obx/oby without touching the gradient |
| 818 | fn oloss to f64 |
| 819 | ls = 0.0f64 |
| 820 | for b ulong get og 6 |
| 821 | ls = add ls ofwd b |
| 822 | end |
| 823 | ret div ls f64 get og 6 |
| 824 | end |
| 825 | |
| 826 | fn ogcten o ulong n ulong tag ulong to void |
| 827 | for j ulong 4 |
| 828 | i = add o rem smx xor get og 40 add mul get og 50 4 j n |
| 829 | ogc1 i |
| 830 | end |
| 831 | end |
| 832 | |
| 833 | note central difference (L(p+h) - L(p-h))/((p+h) - (p-h)) at parameter i; restores p |
| 834 | fn ogcnum i ulong h f64 to f64 |
| 835 | p0 = owg i |
| 836 | pp = add p0 h |
| 837 | pm = sub p0 h |
| 838 | owp i pp |
| 839 | lp = oloss |
| 840 | owp i pm |
| 841 | lm = oloss |
| 842 | owp i p0 |
| 843 | ret div sub lp lm sub pp pm |
| 844 | end |
| 845 | |
| 846 | fn ogcrel num f64 g f64 to f64 |
| 847 | den = fmax fmax fabs num fabs g GCFLOOR |
| 848 | ret div fabs sub num g den |
| 849 | end |
| 850 | |
| 851 | note one gradient check at parameter i; updates og[50] count, og[51] max rel bits, og[52] worst |
| 852 | note index, og[53] retries at GCH2 |
| 853 | fn ogc1 i ulong to void |
| 854 | g = ocsg i |
| 855 | num = ogcnum i GCH |
| 856 | rel = ogcrel num g |
| 857 | if gt rel GCTOL |
| 858 | put og 53 add get og 53 1 |
| 859 | n2 = ogcnum i GCH2 |
| 860 | r2 = ogcrel n2 g |
| 861 | if lt r2 rel |
| 862 | num = n2 |
| 863 | rel = r2 |
| 864 | end |
| 865 | end |
| 866 | oputc 99 |
| 867 | oputc 32 |
| 868 | oputu i |
| 869 | oputc 32 |
| 870 | oputl long mul g 1000000000000.0 |
| 871 | oputc 32 |
| 872 | oputl long mul num 1000000000000.0 |
| 873 | oputc 10 |
| 874 | put og 50 add get og 50 1 |
| 875 | if gt rel bitcast f64 get og 51 |
| 876 | put og 51 bitcast ulong rel |
| 877 | put og 52 i |
| 878 | end |
| 879 | end |
| 880 | |
| 881 | note mathck REF: REF lines are `e XBITS RBITS` or `l XBITS RBITS` (16 lowercase hex digits each, |
| 882 | note 36 bytes per line); RBITS is the correctly rounded Wolfram value of exp or log at the exact |
| 883 | note f64 XBITS. Prints `M e count max_ulp maxrel_bits_hex maxrel_e18` and the same for l; |
| 884 | note exit 42 when a special value (r = 0, inf or NaN) differs in bits, maxrel > MCTOL, or REF has |
| 885 | note no e line or no l line (a check that checked nothing fails). |
| 886 | fn cmdmath to void |
| 887 | if ne oargc 3 |
| 888 | exit 2 |
| 889 | end |
| 890 | n = odread 2 |
| 891 | if ne rem n 36 0 |
| 892 | exit 21 |
| 893 | end |
| 894 | for k ulong 6 |
| 895 | put og add 50 k 0 |
| 896 | end |
| 897 | for i ulong div n 36 |
| 898 | omline mul i 36 |
| 899 | end |
| 900 | omprint 101 0 |
| 901 | omprint 108 3 |
| 902 | if or gt bitcast f64 get og 52 MCTOL gt bitcast f64 get og 55 MCTOL |
| 903 | exit 42 |
| 904 | end |
| 905 | if or eq get og 50 0 eq get og 53 0 |
| 906 | exit 42 |
| 907 | end |
| 908 | end |
| 909 | |
| 910 | fn ohex o ulong to ulong |
| 911 | v = ulong 0 |
| 912 | for k ulong 16 |
| 913 | c = odb add o k |
| 914 | if lt c 58 |
| 915 | v = or shl v 4 sub c 48 |
| 916 | else |
| 917 | v = or shl v 4 sub c 87 |
| 918 | end |
| 919 | end |
| 920 | ret v |
| 921 | end |
| 922 | |
| 923 | note og[50+b]: count, max ulp, max rel bits (b = 0 for exp, 3 for log) |
| 924 | fn omline o ulong to void |
| 925 | kd = odb o |
| 926 | x = bitcast f64 ohex add o 2 |
| 927 | rb = ohex add o 19 |
| 928 | b = ulong 0 |
| 929 | y = 0.0f64 |
| 930 | if eq kd 101 |
| 931 | y = fexp x |
| 932 | else |
| 933 | b = ulong 3 |
| 934 | y = flog x |
| 935 | end |
| 936 | yb = bitcast ulong y |
| 937 | put og add 50 b add get og add 50 b 1 |
| 938 | r = bitcast f64 rb |
| 939 | if or or eq r 0.0 ne r r eq fabs r bitcast f64 ulong 9218868437227405312 |
| 940 | if ne yb rb |
| 941 | exit 42 |
| 942 | end |
| 943 | ret |
| 944 | end |
| 945 | ul = ulong 0 |
| 946 | if gt yb rb |
| 947 | ul = sub yb rb |
| 948 | else |
| 949 | ul = sub rb yb |
| 950 | end |
| 951 | if gt ul get og add 51 b |
| 952 | put og add 51 b ul |
| 953 | end |
| 954 | rel = div fabs sub y r fabs r |
| 955 | if gt rel bitcast f64 get og add 52 b |
| 956 | put og add 52 b bitcast ulong rel |
| 957 | end |
| 958 | end |
| 959 | |
| 960 | fn omprint c ulong b ulong to void |
| 961 | oputc 77 |
| 962 | oputc 32 |
| 963 | oputc c |
| 964 | oputc 32 |
| 965 | oputu get og add 50 b |
| 966 | oputc 32 |
| 967 | oputu get og add 51 b |
| 968 | oputc 32 |
| 969 | oputx get og add 52 b |
| 970 | oputc 32 |
| 971 | oputl long mul bitcast f64 get og add 52 b 1e18 |
| 972 | oputc 10 |
| 973 | oflush |
| 974 | end |
| 975 | |
| 976 | note tune CK OUT TOTAL LRMICRO: fine-tune entry. Load CK (any checkpoint ckpt.om accepts), keep |
| 977 | note its shapes, G and weights, and start a new schedule on them: step 0, the given TOTAL and |
| 978 | note lr = f64(LRMICRO)/1000000.0, Adam moments m = v = 0 (so warmup and bias correction restart). |
| 979 | note Save to OUT with the loaded magic and print `U P`. train, gtrain and gtrainf then continue |
| 980 | note from OUT on any token file. TOTAL 0 or over 999999 exits 12; wrong argument count exits 2. |
| 981 | fn cmdtune to void |
| 982 | if ne oargc 6 |
| 983 | exit 2 |
| 984 | end |
| 985 | ockloop 2 |
| 986 | tt = oargu 4 |
| 987 | if or eq tt 0 gt tt 999999 |
| 988 | exit 12 |
| 989 | end |
| 990 | put og 41 0 |
| 991 | put og 42 tt |
| 992 | put og 43 bitcast ulong div f64 oargu 5 1000000.0 |
| 993 | ozerom mul 2 get og 8 |
| 994 | ocksave 3 |
| 995 | oputc 85 |
| 996 | oputc 32 |
| 997 | oputu get og 8 |
| 998 | oputc 10 |
| 999 | oflush |
| 1000 | end |
| 1001 | |
| 1002 | |
| 1003 | note grow CK OUT L TOTAL LRMICRO: keep embeddings, old layers and final gain byte for byte. |
| 1004 | note Added layers use the own initializer with Wo and W2 zeroed, so both residual branches |
| 1005 | note initially contribute zero. Their input projections remain initialized and trainable. |
| 1006 | note The existing gradient cold store snapshots old weights while geometry changes; oinit |
| 1007 | note writes parameters and moments only. Since P grows, new moments cannot alias the snapshot. |
| 1008 | note Like tune, restart step and Adam, preserving seed, G, vocabulary and checkpoint format. |
| 1009 | note OUT uses normal checkpoint save semantics (an existing path is replaced). |
| 1010 | fn ogrowsnap n ulong to void |
| 1011 | for q ulong ocn n |
| 1012 | i = mul q OCH |
| 1013 | ie = oce q n |
| 1014 | while lt i ie bound 999999 |
| 1015 | v = owg i |
| 1016 | note Reject non-finite source weights before attempting a growth operation. |
| 1017 | if eq and shr bitcast ulong v 52 2047 2047 |
| 1018 | exit 54 |
| 1019 | end |
| 1020 | ocsp i v |
| 1021 | i = add i 1 |
| 1022 | end |
| 1023 | end |
| 1024 | end |
| 1025 | |
| 1026 | fn ogrowcopy n ulong to void |
| 1027 | for q ulong ocn n |
| 1028 | i = mul q OCH |
| 1029 | ie = oce q n |
| 1030 | while lt i ie bound 999999 |
| 1031 | owp i ocsg i |
| 1032 | i = add i 1 |
| 1033 | end |
| 1034 | end |
| 1035 | end |
| 1036 | |
| 1037 | note Keep loop temporaries out of cmdgrow: every target must fit its register budget. |
| 1038 | fn ogrowsilent first ulong to void |
| 1039 | for j ulong sub get og 5 first |
| 1040 | l = add first j |
| 1041 | oiset opwo l mul get og 2 get og 2 0.0 |
| 1042 | oiset opw2 l mul get og 2 get og 4 0.0 |
| 1043 | end |
| 1044 | end |
| 1045 | |
| 1046 | fn cmdgrow to void |
| 1047 | if ne oargc 7 |
| 1048 | exit 2 |
| 1049 | end |
| 1050 | ockload 2 |
| 1051 | ll = oargu 4 |
| 1052 | tt = oargu 5 |
| 1053 | lr = oargu 6 |
| 1054 | if or le ll get og 5 or eq tt 0 gt tt 999999 |
| 1055 | exit 12 |
| 1056 | end |
| 1057 | if eq lr 0 |
| 1058 | exit 12 |
| 1059 | end |
| 1060 | oldl = get og 5 |
| 1061 | oldg = get og 33 |
| 1062 | ogrowsnap get og 8 |
| 1063 | put og 5 ll |
| 1064 | if not ogeom |
| 1065 | exit 12 |
| 1066 | end |
| 1067 | put og 41 0 |
| 1068 | put og 42 tt |
| 1069 | put og 43 bitcast ulong div f64 lr 1000000.0 |
| 1070 | oinit |
| 1071 | ogrowcopy oldg |
| 1072 | for j ulong get og 2 |
| 1073 | owp add get og 33 j ocsg add oldg j |
| 1074 | end |
| 1075 | ogrowsilent oldl |
| 1076 | ocksave 3 |
| 1077 | oputc 71 |
| 1078 | oputc 32 |
| 1079 | oputu oldl |
| 1080 | oputc 32 |
| 1081 | oputu ll |
| 1082 | oputc 32 |
| 1083 | oputu get og 8 |
| 1084 | oputc 10 |
| 1085 | oflush |
| 1086 | end |