Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-mv/blob/main/train/omega-llm/native/gpu/gg.om # 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 Sign in to star 1 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-mv / train / omega-llm / native / gpu / gg.om` 2715 lines ยท 56136 bytes Raw Blame History 1 note SPDX-License-Identifier: AGPL-3.0-only 2 note gg.om: ollm GPU gradient (aien-train#52, rung 4). The step loss and its gradient for the batch in 3 note obx/oby, computed on the GB10 by gk.om GEMMs and gr.om row kernels over all R = B T rows at 4 note once in f32; the gradient lands in ogr[0..P) as f64 and the loss in olos, so oadam and olsum run 5 note unchanged on the CPU. Arena (gbmem 5) byte offsets in gga: 0 W, 1 G, 2 X, 3 Y, 4 CSR start 6 note (V + 1), 5 CSR order (R), 6 first layer, 7 layer stride, 8 HF, 9 SF, 10 Z, 11 logits (R V), 7 note 12 CE stats (R 3), 13 DH, 14 DA, 15 DQ DK DU (contiguous), 16 DO, 17 DF, 18 DP, 19 WS, 20 CS; 8 note layer fields 24 + f: 0 hin 1 a 2 q 3 k 4 u 5 o 6 hm 7 bn 8 s1 9 s2 10 fp 11 rl 12 P; 9 note 40 the arena device address, 47 the plan cursor. s1, s2 and SF hold 1/s (the CPU keeps s). 10 global gga ulong 48 11 global ggcn ulong 65537 12 note byte strides: 0 D, 1 T D, 2 E, 3 T, 4 T T, 5 H T T, 6 R D, 7 D D (each times 4) 13 global ggs uint 8 14 note OMUTG: 1 in the product; 0 in mutant g (DK = -DS^T Q), which the parity check must catch. 15 const OMUTG ulong 1 16 note Looped models (docs/LOOP.md): the layer region holds one activation block per layer application 17 note (og 56 of them), and application l reads the parameters and adds to the gradient of parameter layer 18 note olp l (model.om ovg1..ovw2). Every weight-gradient launch accumulates into G, which ggbwd zeroes 19 note once, so a core's gradient is the sum over its passes. Without a loop og 56 = L and olp l = l. 20 note OMUTLG: 1 in the product; 0 in mutant lg (the w2 gradient of an application that repeats an 21 note earlier parameter layer is not launched, model.om's mutant lp on the chip), which gcmp and gcmp2 on 22 note a looped checkpoint must catch. Without a loop the two are the same. 23 const OMUTLG ulong 1 24 25 note GPU setup failed (ggopen): a clear stage and error on stderr, then exit 75. L1014: a setup 26 note that failed before buffers 2 and 3 existed used to reach ggdie's mapget of the unmapped slots, which 27 note trapped BOUNDS (exit 103) with no message. stdout stays free for the program's own output. 28 note Error 81 is RM status 0x51 NV_ERR_NO_MEMORY (open-gpu-kernel-modules nvstatuscodes.h). Only stage 35 29 note (the compute object, context.om) waits on it, 50 x 100 ms, because there it means another job holds 30 note the GPU. At any other stage, such as 21 (a memory allocation), there is no wait and no retry, so 31 note "try again" is not offered; the cause there is not established by this code, so the text names none. 32 text ggsa "omega: GPU setup failed at stage " 33 text ggsb ", error " 34 text ggsc " (driver status 81, out of memory: seen while another job holds the GPU; it outlasted the bounded wait). Try again.\n" 35 text ggse " (driver status 81, out of memory, at a step with no wait and no retry; only stage 35 waits for a busy GPU).\n" 36 text ggsd " (positive is driver status, negative is errno or internal; the stage is the nvstage step in gpu/context.om).\n" 37 38 fn ggerr1 n ulong to void 39 p = ulong 0 40 while lt p n bound 4097 41 w = write 2 oout p sub n p 42 if le w 0 43 exit 70 44 end 45 p = add p ulong w 46 end 47 put oon 0 0 48 end 49 50 fn ggsetupfail to void 51 oflush 52 r = write 2 ggsa 0 len ggsa 53 oputu ulong get nvstage 0 54 ggerr1 get oon 0 55 r = write 2 ggsb 0 len ggsb 56 oputl get nverr 0 57 ggerr1 get oon 0 58 if eq get nverr 0 81 59 if eq get nvstage 0 35 60 r = write 2 ggsc 0 len ggsc 61 else 62 r = write 2 ggse 0 len ggse 63 end 64 else 65 r = write 2 ggsd 0 len ggsd 66 end 67 exit 75 68 end 69 70 fn ggdie c ulong to void 71 oputc 71 72 oputc 88 73 oputc 32 74 oputu c 75 oputc 32 76 oputl get nverr 0 77 oputc 32 78 oputu ulong get nvstage 0 79 note diagnostics: launches done, mark1, mark2, QMD semaphore; zero while slots 2 and 3 are unmapped 80 note (a failed ggopen), where mapget of an unmapped slot trapped BOUNDS (exit 103, no message) 81 oputc 32 82 oputl get gkt 0 83 oputc 32 84 if and ne get gbmem 3 0 ne get gbmem 2 0 85 oputu mapget get gbmem 3 0 4 86 oputc 32 87 oputu mapget get gbmem 3 16 4 88 oputc 32 89 oputu mapget get gbmem 2 8192 4 90 else 91 oputc 48 92 oputc 32 93 oputc 48 94 oputc 32 95 oputc 48 96 end 97 oputc 10 98 oflush 99 exit 84 100 end 101 102 fn ggcd n ulong k ulong to ulong 103 ret div add n sub k 1 k 104 end 105 106 fn ggr to ulong 107 ret mul get og 6 get og 1 108 end 109 110 note P rounded up to the SQN chunk of 512; the gradient buffer is that long and zeroed to it 111 fn ggp5 to ulong 112 ret mul ggcd get og 8 512 512 113 end 114 115 fn ggput i ulong n ulong to void 116 c = get gga 47 117 put gga i c 118 b = mul div add mul n 4 255 256 256 119 put gga 47 add c b 120 end 121 122 note arena plan for the dims in og; returns its size in bytes 123 fn ggplan to ulong 124 tt = get og 1 125 d = get og 2 126 h = get og 3 127 f = get og 4 128 bb = get og 6 129 r = ggr 130 rd = mul r d 131 pp = mul mul bb h mul tt tt 132 put gga 47 0 133 for i ulong 8 134 ggput add 24 i rd 135 end 136 ggput 32 r 137 ggput 33 r 138 ggput 34 mul r f 139 ggput 35 mul r f 140 ggput 36 pp 141 put gga 7 get gga 47 142 put gga 47 0 143 ggput 0 get og 8 144 ggput 1 ggp5 145 ggput 2 r 146 ggput 3 r 147 ggput 4 add get og 0 1 148 ggput 5 r 149 put gga 6 get gga 47 150 ls = mul get gga 7 get og 56 151 put gga 47 add get gga 47 ls 152 ggput 8 rd 153 ggput 9 r 154 ggput 10 rd 155 ggput 11 mul r get og 0 156 ggput 12 mul r 3 157 ggput 13 rd 158 ggput 14 rd 159 ggput 15 mul rd 3 160 ggput 16 rd 161 ggput 17 mul r f 162 ggput 18 pp 163 ggput 19 rd 164 ggput 20 mul bb d 165 ggput 21 get og 8 166 ggput 22 get og 8 167 ggput 23 div ggp5 512 168 note 37: split-K partials for ggsk, at most 2^21 floats (ggks stops doubling once a grid has 1024 blocks) 169 ggput 37 `2097152` 170 ret get gga 47 171 end 172 173 fn gga1 i ulong to ulong 174 ret add get gga 40 get gga i 175 end 176 177 fn ggl l ulong f ulong to ulong 178 o = add get gga 6 mul l get gga 7 179 ret add add get gga 40 o get gga add 24 f 180 end 181 182 note address of the output of layer application l (the next hin, or HF after the last one) 183 fn ggout l ulong to ulong 184 if lt add l 1 get og 56 185 ret ggl add l 1 0 186 end 187 ret gga1 8 188 end 189 190 fn ggw o ulong to ulong 191 ret add gga1 0 mul o 4 192 end 193 194 fn ggg o ulong to ulong 195 ret add gga1 1 mul o 4 196 end 197 198 note GEMM tiles need m, n multiples of 32 and k of 16: V T D F E multiples of 32; R = B T; 199 note softmax rows keep T/32 values per lane in R32..R47, so T is at most 512 200 fn ggok to bool 201 for i ulong 5 202 v = get og i 203 if eq i 3 204 v = get og 7 205 end 206 if eq i 1 207 v = get og 1 208 end 209 if or eq v 0 ne rem v 32 0 210 ret false 211 end 212 end 213 if gt get og 0 65536 214 ret false 215 end 216 if gt get og 1 512 217 ret false 218 end 219 ret le mul get og 6 get og 3 65535 220 end 221 222 note open the chip and emit the kernels for the dims in og 223 fn ggopen to void 224 if not ggok 225 ggdie 1 226 end 227 asz = ggplan 228 v = get og 0 229 tt = get og 1 230 d = get og 2 231 h = get og 3 232 f = get og 4 233 bb = get og 6 234 grset v tt d h f bb 235 if not gropen asz 236 ggsetupfail 237 end 238 put gga 40 gkbase 239 ggstr 240 end 241 242 note pointer j (1-based) into arg slots 2j, 2j + 1 243 fn ggpt j uint a ulong to void 244 put gkarg mul j 2 gblo a 245 put gkarg add mul j 2 1 gbhi a 246 end 247 248 fn ggu s uint v ulong to void 249 put gkarg s uint v 250 end 251 252 fn ggrun id uint na uint gx ulong gy ulong gz ulong to void 253 put gkna 0 na 254 x = uint gx 255 y = uint gy 256 z = uint gz 257 if not gklaunch id x y z 258 ggdie add 100 ulong id 259 end 260 end 261 262 fn ggmm m ulong n ulong k ulong nz ulong h2 ulong bt bool to void 263 a = uint m 264 b = uint n 265 c = uint k 266 e = uint nz 267 g = uint h2 268 if not gkmm a b c e g bt 269 ggdie 99 270 end 271 end 272 273 note C[R][n] (+)= X[R][k] W[k][n] 274 fn ggxw x ulong w ulong c ulong n ulong k ulong bt bool to void 275 k4 = uint mul k 4 276 n4 = uint mul n 4 277 gkopa 0 x k4 4 0 0 278 gkopa 1 w 4 n4 0 0 279 gkopc c n4 4 0 0 f32 1.0f64 280 ggmm ggr n k 1 1 bt 281 end 282 283 note C[R][n] (+)= X[R][k] W[n][k]^T 284 fn ggxwt x ulong w ulong c ulong n ulong k ulong bt bool to void 285 k4 = uint mul k 4 286 n4 = uint mul n 4 287 gkopa 0 x k4 4 0 0 288 gkopa 1 w k4 4 0 0 289 gkopc c n4 4 0 0 f32 1.0f64 290 ggmm ggr n k 1 1 bt 291 end 292 293 note G[kk][n] += X[R][kk]^T DY[R][n], split over R when the grid is small (ggks) 294 fn ggwg x ulong dy ulong g ulong kk ulong n ulong to void 295 k4 = uint mul kk 4 296 n4 = uint mul n 4 297 s = ggks kk n 1 298 if eq s 1 299 gkopa 0 x 4 k4 0 0 300 gkopa 1 dy 4 n4 0 0 301 gkopc g n4 4 0 0 f32 1.0f64 302 ggmm kk n ggr 1 1 true 303 ret 304 end 305 sl = div ggr s 306 gkopa 0 x 4 k4 0 uint mul mul sl kk 4 307 gkopa 1 dy 4 n4 0 uint mul mul sl n 4 308 ggsk kk n 1 s g 0 309 end 310 311 note Split-K for the weight gradients, whose depth is R = B T. A grid of (m/32)(n/32)nz blocks 312 note leaves most SMs idle (rung-6 gprof: id 4 at 1.7-6 TFLOP/s on 64-256 blocks), so ggks doubles 313 note the split s (up to 16, R/s a multiple of 16) until the grid reaches 1024 blocks: then 314 note blocks s < 2048, so the m n nz s partials fit the 2^21 floats of slot 37. 315 fn ggks m ulong n ulong nz ulong to ulong 316 b = mul mul div m 32 div n 32 nz 317 r = ggr 318 s = ulong 1 319 while lt mul b s 1024 bound 8 320 s2 = mul s 2 321 if or gt s2 16 ne rem r mul s2 16 0 322 ret s 323 end 324 s = s2 325 end 326 ret s 327 end 328 329 note Operands A and B are set (gkopa) with the slice stride as s2. Batch z = z1 s + z2 (z1 < nz the 330 note caller's batch, z2 < s the slice) writes partial P[z1][z2] = A[z1 z2]^T B[z1 z2] into slot 37, 331 note then COLSUM 14 adds G[z1] (byte stride gz) += sum over z2 of P[z1][z2], in slice order. 332 note Mutant p (OMUTP 0) adds one slice fewer, which gcmp2 must catch. 333 const OMUTP ulong 1 334 fn ggsk m ulong n ulong nz ulong s ulong g ulong gz uint to void 335 mn = mul m n 336 p = gga1 37 337 n4 = uint mul n 4 338 c1 = uint mul mul mn s 4 339 c2 = uint mul mn 4 340 gkopc p n4 4 c1 c2 f32 1.0f64 341 k = div ggr s 342 z = mul nz s 343 ggmm m n k z s false 344 ggskr mn nz s g gz 345 end 346 347 note COLSUM 14 over slot 37: G[c gz + i] += sum over z2 < s of P[c][z2][i], i < mn, c < nz 348 fn ggskr mn ulong nz ulong s ulong g ulong gz uint to void 349 kc = s 350 if eq OMUTP 0 351 kc = sub s 1 352 end 353 ggu 0 mn 354 ggu 1 kc 355 ggpt 1 gga1 37 356 ggu 4 mul mn 4 357 ggu 5 mul mul mn s 4 358 ggpt 3 g 359 ggu 8 ulong gz 360 ggrun 14 9 ggcd mn 64 nz 1 361 end 362 363 note 19 COPY (x into y), 20 ZERO (x) over n elements (the FFN ReLU is fused into the GEMMs, gkope) 364 fn ggew id uint n ulong x ulong y ulong to void 365 ggu 0 n 366 ggpt 1 x 367 ggpt 2 y 368 ggrun id 6 ggcd n 64 1 1 369 end 370 371 note RMSF/RMSB run one warp per row: arg 0 and the thread count are R*32 372 fn ggrmsf v ulong g ulong y ulong s ulong to void 373 r = mul ggr 32 374 ggu 0 r 375 ggpt 1 v 376 ggpt 2 g 377 ggpt 3 y 378 ggpt 4 s 379 ggrun 10 10 ggcd r 64 1 1 380 end 381 382 note dh += RMS backward of da; the gain terms go to WS 383 fn ggrmsb v ulong g ulong s ulong to void 384 r = mul ggr 32 385 ggu 0 r 386 ggpt 1 v 387 ggpt 2 g 388 ggpt 3 s 389 ggpt 4 gga1 14 390 ggpt 5 gga1 13 391 ggpt 6 gga1 19 392 ggrun 11 14 ggcd r 64 1 1 393 end 394 395 note gain gradient at parameter offset go += column sums of WS: per sequence (slot 13), then over B 396 fn ggain go ulong to void 397 d = get og 2 398 tt = get og 1 399 bb = get og 6 400 ggu 0 d 401 ggu 1 tt 402 ggpt 1 gga1 19 403 ggu 4 mul d 4 404 ggu 5 mul mul tt d 4 405 ggpt 3 gga1 20 406 ggu 8 mul d 4 407 ggrun 13 9 ggcd d 64 bb 1 408 ggu 0 d 409 ggu 1 bb 410 ggpt 1 gga1 20 411 ggu 4 mul d 4 412 ggu 5 0 413 ggpt 3 ggg go 414 ggu 8 0 415 ggrun 14 9 ggcd d 64 1 1 416 end 417 418 note softmax rows: one warp per row, T 32 threads per score block, grid (T/2, B H) 419 fn ggsoft id uint p ulong dp ulong to void 420 tt = get og 1 421 ggu 0 mul tt 32 422 ggpt 1 p 423 ggpt 2 dp 424 ggrun id 6 div tt 2 mul get og 6 get og 3 1 425 end 426 427 note attention GEMM over the B H (sequence, head) batch; operands set by gghd/ggpl/ggch/ggcp first; 428 note cz: gkopz causal mode (1 A = P or DS, 2 A = P^T or DS^T, 3 C = S or DP) 429 fn ggbh m ulong n ulong k ulong bt bool cz uint to void 430 gkopz cz 431 ggmm m n k mul get og 6 get og 3 get og 3 bt 432 end 433 434 note head-layout operand (rows of E inside D-wide rows, batch b then head); tr: k runs along rows 435 fn gghd ab uint p ulong tr bool to void 436 s0 = get ggs 0 437 s1 = get ggs 1 438 s2 = get ggs 2 439 if tr 440 gkopa ab p 4 s0 s1 s2 441 else 442 gkopa ab p s0 4 s1 s2 443 end 444 end 445 446 note T x T score operand (batch b then head); tr: transposed 447 fn ggpl ab uint p ulong tr bool to void 448 s3 = get ggs 3 449 s4 = get ggs 4 450 s5 = get ggs 5 451 if tr 452 gkopa ab p 4 s3 s5 s4 453 else 454 gkopa ab p s3 4 s5 s4 455 end 456 end 457 458 fn ggch c ulong al f64 to void 459 s0 = get ggs 0 460 s1 = get ggs 1 461 s2 = get ggs 2 462 gkopc c s0 4 s1 s2 f32 al 463 end 464 465 fn ggcp c ulong al f64 to void 466 s3 = get ggs 3 467 s4 = get ggs 4 468 s5 = get ggs 5 469 gkopc c s3 4 s5 s4 f32 al 470 end 471 472 note stride table for the dims in og (bytes) 473 fn ggstr to void 474 d = get og 2 475 tt = get og 1 476 put ggs 0 uint mul d 4 477 put ggs 1 uint mul tt mul d 4 478 put ggs 2 uint mul get og 7 4 479 put ggs 3 uint mul tt 4 480 put ggs 4 uint mul tt mul tt 4 481 put ggs 5 uint mul get og 3 mul tt mul tt 4 482 put ggs 6 uint mul ggr mul d 4 483 put ggs 7 uint mul d mul d 4 484 end 485 486 note q, k, u = a wq, a wk, a wv as one batched GEMM (wq wk wv and q k u are contiguous) 487 fn ggqkv l ulong to void 488 d = get og 2 489 a = ggl l 1 490 w = ggw ovwq l 491 c = ggl l 2 492 s0 = get ggs 0 493 s6 = get ggs 6 494 s7 = get ggs 7 495 gkopa 0 a s0 4 0 0 496 gkopa 1 w 4 s0 0 s7 497 gkopc c s0 4 0 s6 f32 1.0f64 498 ggmm ggr d d 3 3 false 499 end 500 501 note P = softmax(q k^T / sqrt(E)), o = P u; causal: only S on and below the diagonal is formed 502 note (gkmm cz 3) and o stops at the diagonal (cz 1), since P is exactly 0 above it 503 fn ggatt l ulong to void 504 tt = get og 1 505 e = get og 7 506 pp = ggl l 12 507 q = ggl l 2 508 k = ggl l 3 509 gghd 0 q false 510 gghd 1 k false 511 isq = div 1.0f64 fsqrt f64 e 512 ggcp pp isq 513 ggbh tt tt e false 3 514 ggsoft 15 pp pp 515 u = ggl l 4 516 o = ggl l 5 517 ggpl 0 pp false 518 gghd 1 u true 519 ggch o 1.0f64 520 ggbh tt e tt false 1 521 end 522 523 note hm = hin + o wo; bn = rms(hm) g2; out = hm + relu(bn w1) w2 524 fn ggffn l ulong to void 525 d = get og 2 526 f = get og 4 527 rd = mul ggr d 528 hm = ggl l 6 529 x = ggl l 0 530 ggew 19 rd x hm 531 x = ggl l 5 532 w = ggw ovwo l 533 ggxw x w hm d d true 534 x = ggl l 7 535 w = ggw ovg2 l 536 s = ggl l 9 537 ggrmsf hm w x s 538 fp = ggl l 10 539 w = ggw ovw1 l 540 y = ggl l 11 541 gkope 1 y 542 ggxw x w fp f d false 543 x = y 544 o = ggout l 545 ggew 19 rd hm o 546 w = ggw ovw2 l 547 ggxw x w o d f true 548 end 549 550 fn ggfl l ulong to void 551 x = ggl l 0 552 w = ggw ovg1 l 553 a = ggl l 1 554 s = ggl l 8 555 ggrmsf x w a s 556 ggqkv l 557 ggatt l 558 ggffn l 559 end 560 561 note ggnoce 1 (set by oglast only): ggfwd stops after the logits GEMM. CE (kernel 21) rewrites the 562 note logits in place to (softmax - [c = y]) / (T B) for training, which keeps their argmax but is not a 563 note logit (DRA-292 window gserve-w1: greedy matched, maxdiff 21.6, temperature draws drifted). 564 global ggnoce uint 1 565 fn ggfwd to void 566 d = get og 2 567 x = ggl 0 0 568 ggu 0 d 569 ggpt 1 x 570 x = ggw 0 571 ggpt 2 x 572 x = ggw get og 30 573 ggpt 3 x 574 x = gga1 2 575 ggpt 4 x 576 tt = get og 1 577 bb = get og 6 578 ggrun 9 10 ggcd d 64 tt bb 579 for l ulong get og 56 bound `9223372036854775807` 580 ggfl l 581 end 582 x = gga1 8 583 w = ggw get og 33 584 z = gga1 10 585 s = gga1 9 586 ggrmsf x w z s 587 x = gga1 11 588 w = ggw 0 589 v = get og 0 590 ggxwt z w x v d false 591 if ne get ggnoce 0 0 592 ret 593 end 594 r = mul ggr 32 595 ggu 0 r 596 ggpt 1 x 597 x = gga1 3 598 ggpt 2 x 599 x = gga1 12 600 ggpt 3 x 601 ggrun 21 8 ggcd r 64 1 1 602 end 603 604 note layer l FFN backward: DH holds d out on entry, d hm on exit. The dF GEMM's fused epilogue (gkope 2) 605 note applies the ReLU mask FP > 0; the forward W1 GEMM (ggffn, gkope 1) stores both FP and Y. 606 fn ggbffn l ulong to void 607 d = get og 2 608 f = get og 4 609 dh = gga1 13 610 df = gga1 17 611 da = gga1 14 612 w = ggw ovw2 l 613 x = ggl l 10 614 gkope 2 x 615 ggxwt dh w df f d false 616 x = ggl l 11 617 w = ggg ovw2 l 618 if or eq OMUTLG 1 eq olp l l 619 ggwg x dh w f d 620 end 621 x = ggl l 7 622 w = ggg ovw1 l 623 ggwg x df w d f 624 w = ggw ovw1 l 625 ggxwt df w da d f false 626 x = ggl l 6 627 w = ggw ovg2 l 628 s = ggl l 9 629 ggrmsb x w s 630 ggain ovg2 l 631 end 632 633 note layer l attention backward: DO, DP, DU, DS (in DP), DQ, DK, then DA. Causal as in ggatt: DP 634 note on and below the diagonal (cz 3, SOFTB reads dp above it as 0), DU and DK start at the diagonal 635 note (cz 2), DQ stops there (cz 1). 636 fn ggbatt l ulong to void 637 d = get og 2 638 tt = get og 1 639 e = get og 7 640 dh = gga1 13 641 dn = gga1 16 642 w = ggw ovwo l 643 ggxwt dh w dn d d false 644 x = ggl l 5 645 w = ggg ovwo l 646 ggwg x dh w d d 647 dp = gga1 18 648 x = ggl l 4 649 gghd 0 dn false 650 gghd 1 x false 651 ggcp dp 1.0f64 652 ggbh tt tt e false 3 653 pp = ggl l 12 654 dq = gga1 15 655 dk = add dq ulong get ggs 6 656 du = add dk ulong get ggs 6 657 ggpl 0 pp true 658 gghd 1 dn true 659 ggch du 1.0f64 660 ggbh tt e tt false 2 661 ggsoft 16 pp dp 662 x = ggl l 3 663 ggpl 0 dp false 664 gghd 1 x true 665 ggch dq 1.0f64 666 ggbh tt e tt false 1 667 al = 1.0f64 668 if eq OMUTG 0 669 al = neg 1.0f64 670 end 671 x = ggl l 2 672 ggpl 0 dp true 673 gghd 1 x true 674 ggch dk al 675 ggbh tt e tt false 2 676 end 677 678 note DA = DQ wq^T + DK wk^T + DU wv^T; G wq wk wv += a^T (DQ DK DU) batched; then the rms backward 679 fn ggbqkv l ulong to void 680 d = get og 2 681 da = gga1 14 682 dq = gga1 15 683 dk = add dq ulong get ggs 6 684 du = add dk ulong get ggs 6 685 w = ggw ovwq l 686 ggxwt dq w da d d false 687 w = ggw ovwk l 688 ggxwt dk w da d d true 689 w = ggw ovwv l 690 ggxwt du w da d d true 691 a = ggl l 1 692 w = ggg ovwq l 693 s0 = get ggs 0 694 s6 = get ggs 6 695 s7 = get ggs 7 696 k = ggks d d 3 697 if eq k 1 698 gkopa 0 a 4 s0 0 0 699 gkopa 1 dq 4 s0 0 s6 700 gkopc w s0 4 0 s7 f32 1.0f64 701 ggmm d d ggr 3 3 true 702 else 703 sl = uint div ggr k 704 gkopa 0 a 4 s0 0 mul sl s0 705 gkopa 1 dq 4 s0 s6 mul sl s0 706 ggsk d d 3 k w s7 707 end 708 x = ggl l 0 709 w = ggw ovg1 l 710 s = ggl l 8 711 ggrmsb x w s 712 ggain ovg1 l 713 end 714 715 fn ggbl l ulong to void 716 ggbffn l 717 ggbatt l 718 ggbqkv l 719 end 720 721 fn ggbwd to void 722 d = get og 2 723 v = get og 0 724 tt = get og 1 725 r = ggr 726 ggew 20 ggp5 gga1 1 gga1 1 727 ggew 20 mul r d gga1 13 gga1 13 728 ggxw gga1 11 ggw 0 gga1 14 d v false 729 ggwg gga1 11 gga1 10 ggg 0 v d 730 ggrmsb gga1 8 ggw get og 33 gga1 9 731 ggain get og 33 732 l = get og 56 733 while gt l 0 bound `9223372036854775807` 734 l = sub l 1 735 ggbl l 736 end 737 td = mul tt d 738 ggu 0 td 739 ggu 1 get og 6 740 ggpt 1 gga1 13 741 ggu 4 mul td 4 742 ggu 5 0 743 ggpt 3 ggg get og 30 744 ggu 8 0 745 ggrun 14 9 ggcd td 64 1 1 746 ggu 0 d 747 ggpt 1 gga1 13 748 ggpt 2 gga1 4 749 ggpt 3 gga1 5 750 ggpt 4 ggg 0 751 ggrun 22 10 ggcd d 64 v 1 752 end 753 754 note Mutant f: 1 in the product, 0 in the mutant, which uploads layer 0's W1 times 1 + 2^-10 (a 755 note forward error on the chip that gcmp2's FP check must name). 756 const OMUTF ulong 1 757 758 note W[i] as f32 for i in chunk q of P (a loop runs at most 999999 passes) 759 fn ggupc q ulong p ulong to void 760 h = get gbmem 5 761 wo = get gga 0 762 i0 = mul q OCH 763 for j ulong sub oce q p i0 764 i = add i0 j 765 x = bitcast uint f32 ggupv i 766 m = mapset h add wo mul i 4 4 ulong x 767 end 768 end 769 770 note the uploaded value of W[i]: the parameter at virtual index i in the product 771 fn ggupv i ulong to f64 772 w = owg i 773 if eq OMUTF 0 774 a = opw1 0 775 if and ge i a lt i add a mul get og 2 get og 4 776 w = mul w 1.`0009765625f64` 777 end 778 end 779 ret w 780 end 781 782 note upload the f32 parameters 783 fn ggupw to void 784 p = get og 8 785 for q ulong ocn p 786 ggupc q p 787 end 788 put gkinv 0 1 789 end 790 791 note upload the batch tokens and the CSR of x (rows sorted by token) 792 fn ggupb to void 793 ggupt 794 ggcsr 795 put gkinv 0 1 796 end 797 798 note n values of the Adam pool from s as f32 to arena byte offset o + 4 j, and back (one chunk of an 799 note Adam moment) 800 fn ggupmc s ulong o ulong n ulong to void 801 h = get gbmem 5 802 for j ulong n 803 x = bitcast uint f32 omvg add s j 804 m = mapset h add o mul j 4 4 ulong x 805 end 806 end 807 808 fn ggdnmc s ulong o ulong n ulong to void 809 h = get gbmem 5 810 for j ulong n 811 x = uint mapget h add o mul j 4 4 812 omvp add s j f64 bitcast f32 x 813 end 814 end 815 816 note chip parameters of chunk q back into the parameter banks as f64 817 fn ggdnwc q ulong p ulong to void 818 h = get gbmem 5 819 o = get gga 0 820 i0 = mul q OCH 821 for j ulong sub oce q p i0 822 i = add i0 j 823 x = uint mapget h add o mul i 4 4 824 owp i f64 bitcast f32 x 825 end 826 end 827 828 note optimizer state (parameters, Adam pool) to the chip as f32 (m in gga 21, v in gga 22), and back 829 fn ggupst to void 830 ggupw 831 p = get og 8 832 for q ulong ocn p 833 i0 = mul q OCH 834 n = sub oce q p i0 835 ggupmc add get og 11 i0 add get gga 21 mul i0 4 n 836 ggupmc add get og 12 i0 add get gga 22 mul i0 4 n 837 end 838 put gkinv 0 1 839 end 840 841 fn ggdnst to void 842 p = get og 8 843 for q ulong ocn p 844 ggdnwc q p 845 i0 = mul q OCH 846 n = sub oce q p i0 847 ggdnmc add get og 11 i0 add get gga 21 mul i0 4 n 848 ggdnmc add get og 12 i0 add get gga 22 mul i0 4 n 849 end 850 end 851 852 fn ggupt to void 853 h = get gbmem 5 854 xo = get gga 2 855 yo = get gga 3 856 for i ulong ggr 857 tk = get obx i 858 m = mapset h add xo mul i 4 4 tk 859 ty = get oby i 860 m = mapset h add yo mul i 4 4 ty 861 end 862 end 863 864 note counting sort of the R rows by input token: start (V + 1) and order (R) arrays 865 fn ggcsr to void 866 h = get gbmem 5 867 v = get og 0 868 for c ulong add v 1 869 put ggcn c 0 870 end 871 for i ulong ggr 872 c1 = add get obx i 1 873 n = add get ggcn c1 1 874 put ggcn c1 n 875 end 876 for c ulong v 877 c1 = add c 1 878 n = add get ggcn c1 get ggcn c 879 put ggcn c1 n 880 end 881 so = get gga 4 882 for c ulong add v 1 883 n = get ggcn c 884 m = mapset h add so mul c 4 4 n 885 end 886 so = get gga 5 887 for i ulong ggr 888 c = get obx i 889 p = get ggcn c 890 m = mapset h add so mul p 4 4 i 891 put ggcn c add p 1 892 end 893 end 894 895 note GRD[i] = G[i] for i in chunk q of P 896 fn ggdnc q ulong p ulong to void 897 h = get gbmem 5 898 go = get gga 1 899 i0 = mul q OCH 900 for j ulong sub oce q p i0 901 i = add i0 j 902 x = uint mapget h add go mul i 4 4 903 ocsp i f64 bitcast f32 x 904 end 905 end 906 907 fn ggdown to void 908 p = get og 8 909 for q ulong ocn p 910 ggdnc q p 911 end 912 end 913 914 note one f32 at arena byte offset o, as f64 915 fn ggf o ulong to f64 916 x = uint mapget get gbmem 5 o 4 917 ret f64 bitcast f32 x 918 end 919 920 note row loss from the CE stats (m, sum, l_y) of row r: log(sum) + m - l_y 921 fn ggrl r ulong to f64 922 o = add get gga 12 mul r 12 923 m = ggf o 924 sm = ggf add o 4 925 ly = ggf add o 8 926 ret sub add flog sm m ly 927 end 928 929 fn ggloss to f64 930 tt = get og 1 931 for b ulong get og 6 932 s = 0.0f64 933 for t ulong tt 934 s = add s ggrl add mul b tt t 935 end 936 put olos b div s f64 tt 937 end 938 ret olsum 939 end 940 941 note the step loss and gradient for the batch in obx/oby; ggopen first. full: upload the f32 942 note parameters first and download the gradient into ogr[0..P) after; otherwise both stay on chip. 943 note phase wall times (ns): 0 upload, 1 forward, 2 backward, 3 download, 4 loss, 5 on-chip Adam 944 global ggtm long 6 945 fn ggpass full bool to f64 946 t = monons 947 if full 948 ggupw 949 end 950 ggupb 951 u = monons 952 put ggtm 0 sub u t 953 ggfwd 954 t = monons 955 put ggtm 1 sub t u 956 ggbwd 957 u = monons 958 put ggtm 2 sub u t 959 if full 960 ggdown 961 end 962 t = monons 963 put ggtm 3 sub t u 964 l = ggloss 965 u = monons 966 put ggtm 4 sub u t 967 ret l 968 end 969 970 fn gggrad to f64 971 ret ggpass true 972 end 973 974 note clip and Adam on chip at step og[41], the f32 image of oadam: SQN partial sums of g^2, the 975 note norm, clip factor, schedule and bias corrections on the host in f64, then ADAM over P 976 fn ggadam to void 977 p = get og 8 978 nc = div ggp5 512 979 ggu 0 nc 980 ggpt 1 gga1 1 981 ggpt 2 gga1 23 982 ggrun 23 6 ggcd nc 64 1 1 983 h = get gbmem 5 984 so = get gga 23 985 ss = 0.0f64 986 for c ulong nc 987 x = uint mapget h add so mul c 4 4 988 ss = add ss f64 bitcast f32 x 989 end 990 nr = fsqrt ss 991 cl = 1.0f64 992 if gt nr 1.0 993 cl = div 1.0 nr 994 end 995 s = get og 41 996 lrs = olrs s bitcast f64 get og 43 997 bc1 = sub 1.0 opowb 0.9 s 998 bc2 = sub 1.0 opowb 0.99 s 999 if eq OMUTC 0 1000 bc1 = 1.0f64 1001 bc2 = 1.0f64 1002 end 1003 ggu 0 p 1004 ggpt 1 gga1 0 1005 ggpt 2 gga1 21 1006 ggpt 3 gga1 22 1007 ggpt 4 gga1 1 1008 ggu 10 ulong grf cl 1009 ggu 11 ulong grf div lrs bc1 1010 ggu 12 ulong grf div 1.0f64 bc2 1011 ggrun 24 13 ggcd p 64 1 1 1012 end 1013 1014 fn ggtms to void 1015 oputc 71 1016 oputc 84 1017 for i ulong 6 1018 oputc 32 1019 oputl get ggtm i 1020 end 1021 for i ulong 4 1022 oputc 32 1023 oputl get gkt i 1024 end 1025 oputc 10 1026 end 1027 1028 note keep the CPU gradient: BLK[i] = GRD[i] (chunked: a loop runs at most 999999 passes) 1029 fn ggkeep p ulong to void 1030 for q ulong ocn p 1031 i0 = mul q OCH 1032 for j ulong sub oce q p i0 1033 i = add i0 j 1034 ogrp add p i ocsg i 1035 end 1036 end 1037 end 1038 1039 note ggcm[0] = max |GRD[i] - BLK[i]|, ggcm[1] = max |BLK[i]| over i < P 1040 global ggcm f64 2 1041 fn ggmaxd p ulong to void 1042 put ggcm 0 0.0f64 1043 put ggcm 1 0.0f64 1044 for q ulong ocn p 1045 i0 = mul q OCH 1046 for j ulong sub oce q p i0 1047 i = add i0 j 1048 rf = ogrg add p i 1049 dd = sub ocsg i rf 1050 if lt dd 0.0f64 1051 dd = neg dd 1052 end 1053 if lt rf 0.0f64 1054 rf = neg rf 1055 end 1056 if gt dd get ggcm 0 1057 put ggcm 0 dd 1058 end 1059 if gt rf get ggcm 1 1060 put ggcm 1 rf 1061 end 1062 end 1063 end 1064 end 1065 1066 note gcmp CK DATA S: CPU ograd and GPU gradient for batch S; prints 1067 note `GC P maxabs_e12 maxref_e12 rel_e9 cpu_loss_micro gpu_loss_micro gpu_ns tol_e9` and GCMP PASS 1068 note (rel < tol, |loss difference| < 1e-5) or GCMP FAIL (exit 45). tol = R 2^-24 is the first-order 1069 note bound gamma_R on the f32 error of a sum of R = B T rows, the longest reduction of the weight 1070 note gradients, relative to the sum of magnitudes; the CPU reference is f64. ReLU units whose f64 1071 note pre-activation lies within f32 resolution of zero can take the other side on the GPU (receipt: 1072 note evidence/OMEGALLM/gpu/README.md), and both sides are valid subgradients. 1073 fn ggcmp to void 1074 if ne oargc 5 1075 exit 2 1076 end 1077 ockloop 2 1078 odopen 3 1079 obatch oargu 4 1080 put og 10 0 1081 lc = ograd 1082 p = get og 8 1083 ggkeep p 1084 ggopen 1085 t0 = monons 1086 lg = gggrad 1087 t1 = monons 1088 ggmaxd p 1089 mx = get ggcm 0 1090 mr = get ggcm 1 1091 rel = div mx mr 1092 dl = sub lg lc 1093 if lt dl 0.0f64 1094 dl = neg dl 1095 end 1096 oputc 71 1097 oputc 67 1098 oputc 32 1099 oputu p 1100 oputc 32 1101 oputl rmic mul mx `1000000`.0f64 1102 oputc 32 1103 oputl rmic mul mr `1000000`.0f64 1104 oputc 32 1105 oputl rmic mul rel 1000.0f64 1106 oputc 32 1107 oputl rmic lc 1108 oputc 32 1109 oputl rmic lg 1110 oputc 32 1111 oputl sub t1 t0 1112 oputc 32 1113 tol = div f64 ggr `16777216`.0f64 1114 oputl rmic mul tol 1000.0f64 1115 oputc 10 1116 ggtms 1117 ok = and lt rel tol lt dl 0.`00001f64` 1118 r = nvstop 1119 oputc 71 1120 oputc 67 1121 oputc 77 1122 oputc 80 1123 oputc 32 1124 if ok 1125 oputc 80 1126 oputc 65 1127 oputc 83 1128 oputc 83 1129 oputc 10 1130 oflush 1131 ret 1132 end 1133 oputc 70 1134 oputc 65 1135 oputc 73 1136 oputc 76 1137 oputc 10 1138 oflush 1139 exit 45 1140 end 1141 1142 note true when x is finite (every comparison with NaN is false) 1143 fn ggfin x f64 to bool 1144 a = x 1145 if lt a 0.0f64 1146 a = neg a 1147 end 1148 ret le a bitcast f64 ulong `9218868437227405311` 1149 end 1150 1151 note x for printing: 1e6 when x is not finite or above 1e6 (rmic traps past the long range) 1152 fn ggclp x f64 to f64 1153 if ggfin x 1154 if lt x `1000000`.0f64 1155 ret x 1156 end 1157 end 1158 ret `1000000`.0f64 1159 end 1160 1161 note gcmp2 state. ggfs[j] = s_j for the row in flight (F <= 1536, CONTRACT-V2). ggk2: [0] ReLU 1162 note flips (sign of FP_chip differs from FP_ref), [1] non-finite FP values (chip or reference), 1163 note [2] layer of the largest rho, [3] non-finite gradient values (chip, K0 or K1). 1164 note ggk3: [0] rho max, [1] max |FP_ref| at a flip, [2] max |FP_chip - FP_ref|. 1165 global ggfs f64 1536 1166 global ggk2 ulong 4 1167 global ggk3 f64 3 1168 1169 note s_j = sum_k |bn_k| |W1[k F + j]| for row t of layer l (k < D ascending), the magnitude sum of 1170 note the FP dot product the chip rounds (layer l is staged: ogmask runs inside olayb) 1171 fn ggsj l ulong t ulong to void 1172 d = get og 2 1173 f = get og 4 1174 bn = add oabn l mul t d 1175 w = opx ovw1 l 1176 for j ulong f 1177 put ggfs j 0.0f64 1178 end 1179 for k ulong d 1180 a = get oa add bn k 1181 if lt a 0.0f64 1182 a = neg a 1183 end 1184 ggsjk a add w mul k f f 1185 end 1186 end 1187 1188 fn ggsjk a f64 wo ulong f ulong to void 1189 for j ulong f 1190 x = get ow add wo j 1191 if lt x 0.0f64 1192 x = neg x 1193 end 1194 put ggfs j add get ggfs j mul a x 1195 end 1196 end 1197 1198 note model.om's ReLU mask hook (ogkm[0] = 1, gcmp2 only): for row t of layer l of the sequence in 1199 note flight, check every unit's reference FP against the chip's FP (left in the arena by gggrad), 1200 note then zero dF where the chip's FP is not > 0, the chip's fused dF mask (gkope 2; NaN included). 1201 fn ogmask yo ulong l ulong t ulong to void 1202 f = get og 4 1203 xo = add oafp l mul t f 1204 ggsj l t 1205 row = add mul get ogkm 1 get og 1 t 1206 c0 = add sub ggl l 10 get gga 40 mul mul row f 4 1207 for j ulong f 1208 c = ggf add c0 mul j 4 1209 ggk1 get oa add xo j c j l 1210 if not gt c 0.0f64 1211 put oa add yo j 0.0f64 1212 end 1213 end 1214 end 1215 1216 note one unit: non-finite count, |FP_chip - FP_ref|, rho = that over s_j (inf when s_j = 0 and the 1217 note values differ), and the flip count 1218 fn ggk1 v f64 c f64 j ulong l ulong to void 1219 if not and ggfin v ggfin c 1220 put ggk2 1 add get ggk2 1 1 1221 ret 1222 end 1223 dd = sub c v 1224 if lt dd 0.0f64 1225 dd = neg dd 1226 end 1227 if gt dd get ggk3 2 1228 put ggk3 2 dd 1229 end 1230 ggkr dd get ggfs j l 1231 ggkfl v c 1232 end 1233 1234 fn ggkr dd f64 s f64 l ulong to void 1235 if not gt dd 0.0f64 1236 ret 1237 end 1238 rho = bitcast f64 ulong `9218868437227405312` 1239 if gt s 0.0f64 1240 rho = div dd s 1241 end 1242 if gt rho get ggk3 0 1243 put ggk3 0 rho 1244 put ggk2 2 l 1245 end 1246 end 1247 1248 fn ggkfl v f64 c f64 to void 1249 a = gt v 0.0f64 1250 b = gt c 0.0f64 1251 if eq a b 1252 ret 1253 end 1254 put ggk2 0 add get ggk2 0 1 1255 if lt v 0.0f64 1256 v = neg v 1257 end 1258 if gt v get ggk3 1 1259 put ggk3 1 v 1260 end 1261 end 1262 1263 note Adam pool [o + i] = GRD[i] for i < P (the chip gradient at o = 0, K0 at o = P; gcmp2 has no 1264 note Adam step) 1265 fn ggstash p ulong o ulong to void 1266 for q ulong ocn p 1267 ggstq q p o 1268 end 1269 end 1270 1271 fn ggstq q ulong p ulong o ulong to void 1272 i0 = mul q OCH 1273 for j ulong sub oce q p i0 1274 i = add i0 j 1275 omvp add o i ocsg i 1276 end 1277 end 1278 1279 note ggc2: [0] max |G_chip - K0|, [1] max |K0|, [2] max |G_chip - K1| over i < P, with the chip 1280 note gradient in the Adam pool [0, P), K0 in [P, 2P) and K1 in GRD; non-finite values count in ggk2[3] 1281 global ggc2 f64 3 1282 fn gg2cmp p ulong to void 1283 for k ulong 3 1284 put ggc2 k 0.0f64 1285 end 1286 for q ulong ocn p 1287 i0 = mul q OCH 1288 for j ulong sub oce q p i0 1289 gg2c1 add i0 j p 1290 end 1291 end 1292 end 1293 1294 fn gg2c1 i ulong p ulong to void 1295 c = omvg i 1296 r0 = omvg add p i 1297 r1 = ocsg i 1298 if not and and ggfin c ggfin r0 ggfin r1 1299 put ggk2 3 add get ggk2 3 1 1300 ret 1301 end 1302 gg2mx 0 sub c r0 1303 gg2mx 1 r0 1304 gg2mx 2 sub c r1 1305 end 1306 1307 fn gg2mx k ulong x f64 to void 1308 if lt x 0.0f64 1309 x = neg x 1310 end 1311 if gt x get ggc2 k 1312 put ggc2 k x 1313 end 1314 end 1315 1316 note gcmp2 CK DATA S: branch-conditioned parity (evidence/OMEGALLM/gpu-max/README.md). The chip 1317 note gradient runs first and stays in the Adam pool; K0 is the CPU ograd with its own ReLU mask (gcmp's 1318 note reference), K1 the CPU ograd with the chip's FP sign as the ReLU mask (ogmask), which also checks 1319 note every FFN pre-activation. Prints `GC2 P rel0_e9 rel1_e9 tolr_e9 rho_e9 tolf_e9 rho_layer flips 1320 note vflip_e12 fpdiff_e12 nonfinite cpu_loss_micro gpu_loss_micro gpu_ns fails` and GCMP2 PASS, or 1321 note GCMP2 FAIL (exit 47). rel0 = max|G_chip - K0| / max|K0| (gcmp's rel), rel1 = max|G_chip - K1| / 1322 note max|K0|; rho = max |FP_chip - FP_ref| / s_j. fails is a bit set: 1 a non-finite FP or gradient 1323 note value, 2 rho >= tolf = max(D, F, T) 2^-24, 4 rel1 >= tolr = R 2^-24 (gcmp's gamma_R), 8 |chip 1324 note loss - K0 loss| >= 1e-5. 1325 fn ggcmp2 to void 1326 if ne oargc 5 1327 exit 2 1328 end 1329 ockloop 2 1330 odopen 3 1331 obatch oargu 4 1332 p = get og 8 1333 ggopen 1334 t0 = monons 1335 lg = gggrad 1336 t1 = monons 1337 ggstash p 0 1338 put og 10 0 1339 put ogkm 0 0 1340 l0 = ograd 1341 ggstash p p 1342 for k ulong 4 1343 put ggk2 k 0 1344 end 1345 for k ulong 3 1346 put ggk3 k 0.0f64 1347 end 1348 put ogkm 0 1 1349 put og 10 0 1350 l1 = ograd 1351 put ogkm 0 0 1352 gg2cmp p 1353 ggc2pr lg l0 sub t1 t0 1354 end 1355 1356 note the GC2 line and verdict of gcmp2 (lg chip loss, l0 K0 loss, ns chip gradient time); gbcmp2 1357 note (gkbf 1) takes gb.om's verdict 1358 fn ggc2pr lg f64 l0 f64 ns long to void 1359 fl = ggc2fl lg l0 1360 ggc2ln lg l0 ns fl 1361 ggtms 1362 r = nvstop 1363 if eq get gkbf 0 1 1364 gbc2vd lg l0 fl 1365 ret 1366 end 1367 ggc2vd eq fl 0 1368 end 1369 1370 note ggc2[k] / max |K0|, clamped for printing 1371 fn gg2rl k ulong to f64 1372 ret ggclp div get ggc2 k get ggc2 1 1373 end 1374 1375 note tolf = max(D, F, T) 2^-24 1376 fn gg2tf to f64 1377 n = fmax f64 get og 2 fmax f64 get og 4 f64 get og 1 1378 ret div n `16777216`.0f64 1379 end 1380 1381 note tolr = R 2^-24 (gcmp's gamma_R) 1382 fn gg2tr to f64 1383 ret div f64 ggr `16777216`.0f64 1384 end 1385 1386 note the gcmp2 fail bits (see ggcmp2); gb.om's BF16 bits under gkbf 1 1387 fn ggc2fl lg f64 l0 f64 to ulong 1388 if eq get gkbf 0 1 1389 ret gbc2fl lg l0 get ggk3 0 add get ggk2 1 get ggk2 3 1390 end 1391 dl = sub lg l0 1392 if lt dl 0.0f64 1393 dl = neg dl 1394 end 1395 fl = ulong 0 1396 if ne add get ggk2 1 get ggk2 3 0 1397 fl = or fl 1 1398 end 1399 if not lt ggclp get ggk3 0 gg2tf 1400 fl = or fl 2 1401 end 1402 if not lt gg2rl 2 gg2tr 1403 fl = or fl 4 1404 end 1405 if not lt dl 0.`00001f64` 1406 fl = or fl 8 1407 end 1408 ret fl 1409 end 1410 1411 fn ggc2ln lg f64 l0 f64 ns long fl ulong to void 1412 oputc 71 1413 oputc 67 1414 oputc 50 1415 oputc 32 1416 oputu get og 8 1417 gg2pe gg2rl 0 1418 gg2pe gg2rl 2 1419 gg2pe gg2tr 1420 gg2pe ggclp get ggk3 0 1421 gg2pe gg2tf 1422 oputc 32 1423 oputu get ggk2 2 1424 oputc 32 1425 oputu get ggk2 0 1426 oputc 32 1427 oputl rmic mul ggclp get ggk3 1 `1000000`.0f64 1428 oputc 32 1429 oputl rmic mul ggclp get ggk3 2 `1000000`.0f64 1430 oputc 32 1431 oputu add get ggk2 1 get ggk2 3 1432 oputc 32 1433 oputl rmic ggclp l0 1434 oputc 32 1435 oputl rmic ggclp lg 1436 oputc 32 1437 oputl ns 1438 oputc 32 1439 oputu fl 1440 oputc 10 1441 end 1442 1443 note " x_e9" 1444 fn gg2pe x f64 to void 1445 oputc 32 1446 oputl rmic mul x 1000.0f64 1447 end 1448 1449 fn ggc2vd ok bool to void 1450 oputc 71 1451 oputc 67 1452 oputc 77 1453 oputc 80 1454 oputc 50 1455 oputc 32 1456 if ok 1457 oputc 80 1458 oputc 65 1459 oputc 83 1460 oputc 83 1461 oputc 10 1462 oflush 1463 ret 1464 end 1465 oputc 70 1466 oputc 65 1467 oputc 73 1468 oputc 76 1469 oputc 10 1470 oflush 1471 exit 47 1472 end 1473 1474 note gtrain CK TRAIN UNTIL EVERY: cmdtrain with the gradient from the GPU (K = 1, no workers); prints 1475 note `T s+1 loss_micro` at save points like train 1476 fn ggtrain to void 1477 if ne oargc 6 1478 exit 2 1479 end 1480 ockloop 2 1481 odopen 3 1482 un = oargu 4 1483 ev = oargu 5 1484 if eq ev 0 1485 exit 2 1486 end 1487 en = get og 42 1488 if lt un en 1489 en = un 1490 end 1491 ggopen 1492 while lt get og 41 en bound 999999 1493 s = get og 41 1494 obatch s 1495 put og 10 0 1496 ls = gggrad 1497 oadam 1498 s1 = add s 1 1499 put og 41 s1 1500 if or eq rem s1 ev 0 eq s1 en 1501 ocksave 2 1502 oputc 84 1503 oputc 32 1504 oputu s1 1505 oputc 32 1506 oputl rmic ls 1507 oputc 10 1508 oflush 1509 end 1510 end 1511 r = nvstop 1512 end 1513 1514 note BLK[i] = the f64 parameter i before the step 1515 fn ggkw q ulong p ulong to void 1516 i0 = mul q OCH 1517 for j ulong sub oce q p i0 1518 i = add i0 j 1519 ogrp add p i owg i 1520 end 1521 end 1522 1523 note GRD[i] = chip parameter i as f64 1524 fn ggdw q ulong p ulong to void 1525 h = get gbmem 5 1526 wo = get gga 0 1527 i0 = mul q OCH 1528 for j ulong sub oce q p i0 1529 i = add i0 j 1530 x = uint mapget h add wo mul i 4 4 1531 ocsp i f64 bitcast f32 x 1532 end 1533 end 1534 1535 note over i < P with w0 = BLK[i], c = parameter i (CPU f64 result), g = GRD[i] (chip f32 result): 1536 note u = c - w0, d = (g - f32(w0)) - u; ggam 0 max |d|, 1 max |u|, 2 max (|d| - 2^-24 |c|), the part 1537 note of d above the half-ulp rounding of the f32 master weight 1538 global ggam f64 3 1539 fn ggad1 i ulong p ulong to void 1540 w0 = ogrg add p i 1541 c = owg i 1542 u = sub c w0 1543 g = sub ocsg i f64 f32 w0 1544 d = fabs sub g u 1545 u = fabs u 1546 e = sub d mul fabs c 0.`000000059604644775390625f64` 1547 if gt d get ggam 0 1548 put ggam 0 d 1549 end 1550 if gt u get ggam 1 1551 put ggam 1 u 1552 end 1553 if gt e get ggam 2 1554 put ggam 2 e 1555 end 1556 end 1557 1558 fn ggadc q ulong p ulong to void 1559 i0 = mul q OCH 1560 for j ulong sub oce q p i0 1561 ggad1 add i0 j p 1562 end 1563 end 1564 1565 note gacmp CK DATA S: one step from CK on batch S with the chip gradient, Adam on the CPU (oadam, f64) 1566 note and on the chip (ggadam, f32); prints `GA P maxd_e12 maxu_e12 rel_e9 resid_e9 tol_e9 adam_ns 1567 note loss_micro` and GACMP PASS (resid = max(|d| - 2^-24 |c|) / max |u| < tol = 2^-16) or GACMP FAIL 1568 note (exit 46) 1569 fn ggacmp to void 1570 if ne oargc 5 1571 exit 2 1572 end 1573 ockloop 2 1574 odopen 3 1575 obatch oargu 4 1576 put og 10 0 1577 p = get og 8 1578 ggopen 1579 ggupst 1580 lg = ggpass true 1581 for q ulong ocn p 1582 ggkw q p 1583 end 1584 oadam 1585 t0 = monons 1586 ggadam 1587 t1 = monons 1588 for q2 ulong ocn p 1589 ggdw q2 p 1590 end 1591 for k ulong 3 1592 put ggam k 0.0f64 1593 end 1594 for q3 ulong ocn p 1595 ggadc q3 p 1596 end 1597 mu = get ggam 1 1598 rel = div get ggam 0 mu 1599 res = div get ggam 2 mu 1600 tol = div 1.0f64 65536.0f64 1601 oputc 71 1602 oputc 65 1603 oputc 32 1604 oputu p 1605 oputc 32 1606 oputl rmic mul get ggam 0 `1000000`.0f64 1607 oputc 32 1608 oputl rmic mul mu `1000000`.0f64 1609 oputc 32 1610 oputl rmic mul rel 1000.0f64 1611 oputc 32 1612 oputl rmic mul res 1000.0f64 1613 oputc 32 1614 oputl rmic mul tol 1000.0f64 1615 oputc 32 1616 oputl sub t1 t0 1617 oputc 32 1618 oputl rmic lg 1619 oputc 10 1620 r = nvstop 1621 oputc 71 1622 oputc 65 1623 oputc 67 1624 oputc 77 1625 oputc 80 1626 oputc 32 1627 if lt res tol 1628 oputc 80 1629 oputc 65 1630 oputc 83 1631 oputc 83 1632 oputc 10 1633 oflush 1634 ret 1635 end 1636 oputc 70 1637 oputc 65 1638 oputc 73 1639 oputc 76 1640 oputc 10 1641 oflush 1642 exit 46 1643 end 1644 1645 note gtrainf CK TRAIN UNTIL EVERY: gtrain with the parameters and Adam state resident on the chip in 1646 note f32 (ggadam); only the batch goes up each step. At save points the state comes back to f64 for 1647 note ocksave and `T s+1 loss_micro` is printed; GT at the end has the last step's phase times. 1648 fn ggtrainf to void 1649 if ne oargc 6 1650 exit 2 1651 end 1652 ockloop 2 1653 odopen 3 1654 un = oargu 4 1655 ev = oargu 5 1656 if eq ev 0 1657 exit 2 1658 end 1659 en = get og 42 1660 if lt un en 1661 en = un 1662 end 1663 ggopen 1664 ggupst 1665 while lt get og 41 en bound 999999 1666 s = get og 41 1667 obatch s 1668 put og 10 0 1669 ls = ggpass false 1670 t = monons 1671 ggadam 1672 put ggtm 5 sub monons t 1673 s1 = add s 1 1674 put og 41 s1 1675 if or eq rem s1 ev 0 eq s1 en 1676 ggdnst 1677 ocksave 2 1678 oputc 84 1679 oputc 32 1680 oputu s1 1681 oputc 32 1682 oputl rmic ls 1683 oputc 10 1684 oflush 1685 end 1686 end 1687 ggtms 1688 oflush 1689 r = nvstop 1690 end 1691 1692 note gprof CK TRAIN N: N gtrainf steps from CK, nothing saved. The last step runs with the launch 1693 note profile on (gk.om gkpf), then 64 ZERO launches of 64 floats measure the bare launch round trip. 1694 note Prints `T steps loss_micro`, one `GP id gx gy gz m n k wait_ns` per profiled launch, then GT. 1695 fn ggprof to void 1696 if ne oargc 5 1697 exit 2 1698 end 1699 ockloop 2 1700 odopen 3 1701 nn = oargu 4 1702 if eq nn 0 1703 exit 2 1704 end 1705 ggopen 1706 ggupst 1707 ls = 0.0f64 1708 for i ulong nn 1709 s = get og 41 1710 obatch s 1711 put og 10 0 1712 if eq add i 1 nn 1713 put gkpf 0 1 1714 end 1715 ls = ggpass false 1716 t = monons 1717 ggadam 1718 put ggtm 5 sub monons t 1719 put og 41 add s 1 1720 end 1721 for i ulong 64 1722 ggew 20 64 ggg 0 ggg 0 1723 end 1724 put gkpf 0 0 1725 oputc 84 1726 oputc 32 1727 oputu get og 41 1728 oputc 32 1729 oputl rmic ls 1730 oputc 10 1731 for q ulong get gkpc 0 1732 o = mul q 8 1733 oputc 71 1734 oputc 80 1735 for j ulong 8 1736 oputc 32 1737 oputu get gkpl add o j 1738 end 1739 oputc 10 1740 end 1741 ggtms 1742 oflush 1743 r = nvstop 1744 end 1745 1746 note geval CK HELD W [MERGES]: eval (cli.om cmdeval) with the forward pass on the chip in f32. Windows 1747 note w < W at offsets w T, B per forward (og 6); the last set pads its spare rows with window 0 and 1748 note does not count them. Each window adds its mean row loss in window order and, when V > 256, the 1749 note decoded length of its T targets, as cmdeval does. Prints `E step W loss_micro bpb_micro` like 1750 note eval, then `GE gpu_ns` (upload, forwards and loss reads). Exits 2 and 23 as eval. 1751 global ggev f64 1 1752 global ggen ulong 1 1753 fn ggeval to void 1754 ww = ggevw 1755 t = monons 1756 ggopen 1757 ggupw 1758 put ggev 0 0.0f64 1759 put ggen 0 0 1760 bb = get og 6 1761 nc = div add ww sub bb 1 bb 1762 for c ulong nc 1763 ggevc c ww 1764 end 1765 ns = sub monons t 1766 ggevpr ww 1767 oputc 71 1768 oputc 69 1769 oputc 32 1770 oputl ns 1771 oputc 10 1772 oflush 1773 r = nvstop 1774 end 1775 1776 note eval's argument checks; loads CK, MERGES and HELD and returns W 1777 fn ggevw to ulong 1778 na = oargc 1779 if and ne na 5 ne na 6 1780 exit 2 1781 end 1782 ockloop 2 1783 ww = oargu 4 1784 v = get og 0 1785 if gt v 256 1786 if eq na 5 1787 exit 2 1788 end 1789 obload 5 v 1790 end 1791 odopen 3 1792 tt = get og 1 1793 m = get odn 0 1794 if eq ww 0 1795 exit 23 1796 end 1797 if gt ww div sub m 1 tt 1798 exit 23 1799 end 1800 ret ww 1801 end 1802 1803 note window set c: rows b < B hold window c B + b (window 0 past W), one chip forward, then the sums 1804 fn ggevc c ulong ww ulong to void 1805 bb = get og 6 1806 tt = get og 1 1807 for b ulong bb 1808 w = add mul c bb b 1809 if ge w ww 1810 w = 0 1811 end 1812 obrow b mul w tt 1813 end 1814 ggupb 1815 ggfwd 1816 for b ulong bb 1817 w = add mul c bb b 1818 if lt w ww 1819 ggevr b 1820 end 1821 end 1822 end 1823 1824 note add row b's mean loss (CE stats left by ggfwd) and, when V > 256, its decoded target bytes 1825 fn ggevr b ulong to void 1826 tt = get og 1 1827 o = mul b tt 1828 s = 0.0f64 1829 for t ulong tt 1830 s = add s ggrl add o t 1831 end 1832 put ggev 0 add get ggev 0 div s f64 tt 1833 if gt get og 0 256 1834 n = get ggen 0 1835 for t ulong tt 1836 n = add n obtlen get oby add o t 1837 end 1838 put ggen 0 n 1839 end 1840 end 1841 1842 note the E line of cmdeval from the sums 1843 fn ggevpr ww ulong to void 1844 lo = div get ggev 0 f64 ww 1845 bp = div lo 0.`6931471805599453` 1846 if gt get og 0 256 1847 tt = get og 1 1848 bp = div mul bp f64 mul ww tt f64 get ggen 0 1849 end 1850 oputc 69 1851 oputc 32 1852 oputu get og 41 1853 oputc 32 1854 oputu ww 1855 oputc 32 1856 oputl rmic lo 1857 oputc 32 1858 oputl rmic bp 1859 oputc 10 1860 end 1861 1862 note gdiag CK DATA S: the CE kernel's per-row stats (max m, sum of exp(l - m), logit l_y) on the chip 1863 note in f32 against the CPU's f64 values for the same forward of batch S. Prints R, then the mean 1864 note over rows times 1e9 of: d m, d l_y, d log sum, d loss, (sum of the chip's p) - 1, d (l_y - m). 1865 note It located the CE sum bias that the Kahan sum in gr.om grce removes. 1866 global gdacc f64 6 1867 global gdcpu f64 3 1868 fn ggdiag to void 1869 if ne oargc 5 1870 exit 2 1871 end 1872 ockloop 2 1873 odopen 3 1874 obatch oargu 4 1875 put og 10 0 1876 ggopen 1877 ggupw 1878 ggupb 1879 ggfwd 1880 for i ulong 6 1881 put gdacc i 0.0f64 1882 end 1883 for b ulong get og 6 1884 x = ofwd b 1885 for t ulong get og 1 1886 ggdrow b t 1887 end 1888 end 1889 n = f64 ggr 1890 oputu ggr 1891 for i ulong 6 1892 oputc 32 1893 oputl rmic mul div get gdacc i n 1000.0f64 1894 end 1895 oputc 10 1896 oflush 1897 r = nvstop 1898 end 1899 1900 fn ggdrow b ulong t ulong to void 1901 r = add mul b get og 1 t 1902 ggdcpu t get oby r 1903 ggdacc r 1904 put gdacc 4 add get gdacc 4 sub ggdps r 1.0f64 1905 end 1906 1907 note CPU f64 stats of row t of the last ofwd: max logit, log sum exp(l - max), logit of y 1908 fn ggdcpu t ulong y ulong to void 1909 d = get og 2 1910 v = get og 0 1911 pr = add get og 18 mul t v 1912 klogit add get og 17 mul t d pr v d 1913 m = bitcast f64 ulong `18442240474082181120` 1914 for j ulong v 1915 m = fmax m get oa add pr j 1916 end 1917 s = 0.0f64 1918 for k ulong v 1919 s = add s fexp sub get oa add pr k m 1920 end 1921 put gdcpu 0 m 1922 put gdcpu 1 flog s 1923 put gdcpu 2 get oa add pr y 1924 end 1925 1926 fn ggdacc r ulong to void 1927 o = add get gga 12 mul r 12 1928 mg = sub ggf o get gdcpu 0 1929 ls = sub flog ggf add o 4 get gdcpu 1 1930 lg = sub ggf add o 8 get gdcpu 2 1931 put gdacc 0 add get gdacc 0 mg 1932 put gdacc 1 add get gdacc 1 lg 1933 put gdacc 2 add get gdacc 2 ls 1934 put gdacc 3 add get gdacc 3 sub add ls mg lg 1935 put gdacc 5 add get gdacc 5 sub lg mg 1936 end 1937 1938 note sum of the chip's probabilities of row r, recovered from the CE output L = (p - [j = y]) sc 1939 fn ggdps r ulong to f64 1940 v = get og 0 1941 sc = div 1.0f64 f64 ggr 1942 o = add get gga 11 mul mul r v 4 1943 s = 0.0f64 1944 for j ulong v 1945 s = add s div ggf add o mul j 4 sc 1946 end 1947 ret add s 1.0f64 1948 end 1949 1950 note gserve and gsample (aien-train DRA-292): serve and sample (serve.om, cli.om) with the per-token 1951 note forward on the chip. cli.om's ochipup calls ogchip once the checkpoint loads: B = 1 (one 1952 note context row, R = T), open the chip, upload the f32 parameters. olastf then calls oglast per 1953 note token: upload the T context tokens of obx row 0 (positions past CT hold earlier tokens or 0, 1954 note which the causal forward never reads for position CT - 1), ggdfwd (below: the rows up to the 1955 note context's last 64-row tile, no CE, so the logits stay logits), and the logits of position 1956 note CT - OMUTZ read back as f64 into TMP[0..V) (oa), where 1957 note argmax and draws run as on the CPU. 1958 note OMUTZ: 1 in the product; 0 in mutant z (the row past the context), which gscmp must catch. 1959 const OMUTZ ulong 1 1960 fn ogchip to void 1961 put og 6 1 1962 ggopen 1963 ggupw 1964 end 1965 1966 fn oglast to void 1967 ggupt 1968 put gkinv 0 1 1969 ggdfwd 1970 ogrd 1971 end 1972 1973 note the logits of position CT - OMUTZ (row CT - OMUTZ of slot 11) read back as f64 into TMP[0..V) (oa) 1974 fn ogrd to void 1975 v = get og 0 1976 r = sub get og 9 OMUTZ 1977 o = add get gga 11 mul mul r v 4 1978 tm = get og 27 1979 for c ulong v 1980 put oa add tm c ggf add o mul c 4 1981 end 1982 end 1983 1984 note ggdfwd (ham/ollm-speed-kvdec): per-token GPU forward, including mixed-BF16 variants. ggfwd runs all 1985 note R = T rows every token; a causal forward never lets a row read a later row, so the logits of 1986 note position CT - 1 need rows 0..CT - 1 only. ggdfwd runs rows 0..rr - 1, rr = 64 ceil(CT / 64) 1987 note capped at T (whole 64-row tiles, so every GEMM keeps ggfwd's 64x64 kernels and per-element sums), 1988 note keeps the residual stream in place in layer 0's input hin (hin += o wo, hin += relu(bn w1) w2: the 1989 note same f32 adds as ggffn's COPY then accumulate, without its two COPY launches per layer), and runs 1990 note the final RMS and the logits GEMM on the last 64-row tile only (the one holding row CT - 1). 1991 note Rows at or past rr keep stale arena data that no computed row reads (causal GEMM tiles; softmax 1992 note chunks past a row's own are masked, gr.om k6). No CE launch (ggnoce's logits stay logits). 1993 note OMUTJ: 1 in the product; 0 in mutant j (rr = 64 floor(CT / 64), at least 64: row CT - 1 lies past 1994 note the computed rows once CT > 64 is not a multiple of 64), which gscmp and gdbench mode 2 must catch. 1995 const OMUTJ ulong 1 1996 note ggdm[0]: the rows the ggd* GEMMs run over 1997 global ggdm ulong 1 1998 note bfdec selects BF16 for attention-output and FFN projections only. QKV, 1999 note attention scores/value sums, normalization and final logits retain f32. 2000 global bfdec uint 1 2001 2002 fn ggdrows to ulong 2003 ct = get og 9 2004 rr = mul div add ct 63 64 64 2005 if eq OMUTJ 0 2006 rr = mul div ct 64 64 2007 if eq rr 0 2008 rr = 64 2009 end 2010 end 2011 tt = get og 1 2012 if gt rr tt 2013 rr = tt 2014 end 2015 ret rr 2016 end 2017 2018 note C[m][n] (+)= X[m][k] W[k][n] over the first m = ggdm[0] rows 2019 fn ggdxw x ulong w ulong c ulong n ulong k ulong bt bool to void 2020 k4 = uint mul k 4 2021 n4 = uint mul n 4 2022 gkopa 0 x k4 4 0 0 2023 gkopa 1 w 4 n4 0 0 2024 gkopc c n4 4 0 0 f32 1.0f64 2025 ggmm get ggdm 0 n k 1 1 bt 2026 end 2027 2028 note C[m][n] (+)= X[m][k] W[n][k]^T over the first m = ggdm[0] rows 2029 fn ggdxwt x ulong w ulong c ulong n ulong k ulong bt bool to void 2030 k4 = uint mul k 4 2031 n4 = uint mul n 4 2032 gkopa 0 x k4 4 0 0 2033 gkopa 1 w k4 4 0 0 2034 gkopc c n4 4 0 0 f32 1.0f64 2035 ggmm get ggdm 0 n k 1 1 bt 2036 end 2037 2038 note RMSF (kernel 10) over rows [r0, r0 + m) of the D-wide v and y and of s 2039 fn ggdrms v ulong g ulong y ulong s ulong r0 ulong m ulong to void 2040 o = mul r0 mul get og 2 4 2041 r = mul m 32 2042 ggu 0 r 2043 ggpt 1 add v o 2044 ggpt 2 g 2045 ggpt 3 add y o 2046 ggpt 4 add s mul r0 4 2047 ggrun 10 10 ggcd r 64 1 1 2048 end 2049 2050 note q, k, u = a wq, a wk, a wv over the first ggdm[0] rows (ggqkv's batched GEMM) 2051 fn ggdqkv l ulong to void 2052 d = get og 2 2053 a = ggl l 1 2054 w = ggw ovwq l 2055 c = ggl l 2 2056 s0 = get ggs 0 2057 s6 = get ggs 6 2058 s7 = get ggs 7 2059 gkopa 0 a s0 4 0 0 2060 gkopa 1 w 4 s0 0 s7 2061 gkopc c s0 4 0 s6 f32 1.0f64 2062 ggmm get ggdm 0 d d 3 3 false 2063 end 2064 2065 note ggatt over the first rr = ggdm[0] rows: S (causal, rr x rr), SOFTF on rows < rr (grid rr / 2; 2066 note arg 0 stays the thread bound T 32), o = P u (causal, k = rr) 2067 fn ggdatt l ulong to void 2068 rr = get ggdm 0 2069 e = get og 7 2070 pp = ggl l 12 2071 q = ggl l 2 2072 k = ggl l 3 2073 gghd 0 q false 2074 gghd 1 k false 2075 isq = div 1.0f64 fsqrt f64 e 2076 ggcp pp isq 2077 ggbh rr rr e false 3 2078 ggu 0 mul get og 1 32 2079 ggpt 1 pp 2080 ggpt 2 pp 2081 ggrun 15 6 div rr 2 mul get og 6 get og 3 1 2082 u = ggl l 4 2083 o = ggl l 5 2084 ggpl 0 pp false 2085 gghd 1 u true 2086 ggch o 1.0f64 2087 ggbh rr e rr false 1 2088 end 2089 2090 note hin += o wo; bn = rms(hin) g2; hin += relu(bn w1) w2, over the first ggdm[0] rows, hin = layer 0's 2091 fn ggdffn l ulong to void 2092 savedbf = get gkbf 0 2093 d = get og 2 2094 f = get og 4 2095 h = ggl 0 0 2096 x = ggl l 5 2097 w = ggw ovwo l 2098 if ne get bfdec 0 0 2099 put gkbf 0 1 2100 end 2101 ggdxw x w h d d true 2102 put gkbf 0 savedbf 2103 x = ggl l 7 2104 w = ggw ovg2 l 2105 s = ggl l 9 2106 ggdrms h w x s 0 get ggdm 0 2107 fp = ggl l 10 2108 w = ggw ovw1 l 2109 y = ggl l 11 2110 gkope 1 y 2111 if ne get bfdec 0 0 2112 put gkbf 0 1 2113 end 2114 ggdxw x w fp f d false 2115 put gkbf 0 savedbf 2116 w = ggw ovw2 l 2117 if ne get bfdec 0 0 2118 put gkbf 0 1 2119 end 2120 ggdxw y w h d f true 2121 put gkbf 0 savedbf 2122 end 2123 2124 fn ggdfl l ulong to void 2125 h = ggl 0 0 2126 w = ggw ovg1 l 2127 a = ggl l 1 2128 s = ggl l 8 2129 ggdrms h w a s 0 get ggdm 0 2130 ggdqkv l 2131 ggdatt l 2132 ggdffn l 2133 end 2134 2135 fn ggdfwd to void 2136 rr = ggdrows 2137 put ggdm 0 rr 2138 d = get og 2 2139 x = ggl 0 0 2140 ggu 0 d 2141 ggpt 1 x 2142 x = ggw 0 2143 ggpt 2 x 2144 x = ggw get og 30 2145 ggpt 3 x 2146 x = gga1 2 2147 ggpt 4 x 2148 ggrun 9 10 ggcd d 64 rr get og 6 2149 for l ulong get og 56 bound `9223372036854775807` 2150 ggdfl l 2151 end 2152 ggdhead rr 2153 end 2154 2155 note final RMS and logits GEMM over the last 64-row tile of the rr rows (all rr when rr < 64) 2156 fn ggdhead rr ulong to void 2157 d = get og 2 2158 tb = ulong 64 2159 if lt rr 64 2160 tb = rr 2161 end 2162 b = sub rr tb 2163 h = ggl 0 0 2164 w = ggw get og 33 2165 z = gga1 10 2166 s = gga1 9 2167 ggdrms h w z s b tb 2168 put ggdm 0 tb 2169 x = add z mul mul b d 4 2170 w = ggw 0 2171 v = get og 0 2172 c = add gga1 11 mul mul b v 4 2173 ggdxwt x w c v d false 2174 put ggdm 0 rr 2175 end 2176 2177 note the forward of gdbench mode md: 0 ggfwd without CE (the gserve forward at `edf39b0`), 3 ggdfwd with 2178 note every 64x64 GEMM on the BF16 tensor-core kernels (gk.om gkbf 1, docs/omegallm/CONTRACT-BF16.md; 2179 note GEMMs on the 32x32 tile stay f32), 5 ggdfwd as one batch.om submission (ggdbat), 7 selective BF16, else ggdfwd 2180 note in f32 2181 fn ggdfm md ulong to void 2182 if eq md 0 2183 put ggnoce 0 1 2184 ggfwd 2185 put ggnoce 0 0 2186 ret 2187 end 2188 if eq md 3 2189 put gkbf 0 1 2190 ggdfwd 2191 put gkbf 0 0 2192 ret 2193 end 2194 if eq md 7 2195 put bfdec 0 1 2196 ggdfwd 2197 put bfdec 0 0 2198 ret 2199 end 2200 if eq md 5 2201 ggdbat 2202 ret 2203 end 2204 ggdfwd 2205 end 2206 2207 note ggdbat: ggdfwd's launches recorded (gk.om gkbat) into one batch.om batch of exactly 3 + 9 L 2208 note launches (EMB, nine per layer, final RMS, logits; batch.om refuses a different count before the 2209 note doorbell), sent once and waited once (gkbsub). The kernels, their order and arguments are 2210 note ggdfwd's; batch.om ends each body with a WFI release and a shader invalidate, so each grid 2211 note drains before the next starts. gdbench opens the batch packet (gbbopen) once after the chip. 2212 fn ggdbat to void 2213 if not gbbegin add 3 mul 9 get og 56 2214 ggdie 120 2215 end 2216 put gkbat 0 1 2217 ggdfwd 2218 put gkbat 0 0 2219 if not gkbsub 2220 ggdie 121 2221 end 2222 end 2223 2224 note FNV-1a over the f64 bits of the V logits in TMP (equal hashes: the same logits, bit for bit) 2225 fn ggdhash to ulong 2226 tm = get og 27 2227 h = ulong `14695981039346656037` 2228 for c ulong get og 0 2229 x = bitcast ulong get oa add tm c 2230 h = mul xor h x `1099511628211` 2231 end 2232 ret h 2233 end 2234 2235 note one gdbench step in mode md on the current context: prints `GD step ct tok hash up_ns fwd_ns 2236 note rd_ns launches build_ns wait_ns` (up: the token upload, fwd: the forward's launches, rd: logits 2237 note read back and argmax; launches, build and wait from gk.om's gkt) and returns the token 2238 fn ggdstep s ulong md ulong to ulong 2239 k0 = get gkt 0 2240 b0 = get gkt 1 2241 w0 = get gkt 2 2242 t0 = monons 2243 ggupt 2244 put gkinv 0 1 2245 t1 = monons 2246 ggdfm md 2247 t2 = monons 2248 ogrd 2249 a = oargmax 2250 t3 = monons 2251 h = ggdhash 2252 oputc 71 2253 oputc 68 2254 oputc 32 2255 oputu s 2256 oputc 32 2257 oputu get og 9 2258 oputc 32 2259 oputu a 2260 oputc 32 2261 oputu h 2262 oputc 32 2263 oputl sub t1 t0 2264 oputc 32 2265 oputl sub t2 t1 2266 oputc 32 2267 oputl sub t3 t2 2268 oputc 32 2269 oputl sub get gkt 0 k0 2270 oputc 32 2271 oputl sub get gkt 1 b0 2272 oputc 32 2273 oputl sub get gkt 2 w0 2274 oputc 10 2275 oflush 2276 ret a 2277 end 2278 2279 note gdbench comparison step (2, 4, 6 or 8): ggfwd's logits into ggdcl, then ggdfwd's (f32 in mode 2, BF16 in mode 4, 2280 note mode 6 batched, mode 8 selective BF16) into TMP on the same context; prints `GQ step ct tok0 tok1 ndiff maxdiff_e9 margin_e9` 2281 note (ndiff: logits whose f64 bits differ, read back from f32, so f32 bits; margin: ggfwd's top-1 2282 note logit minus its top-2) and returns tok0. Totals: ggdnd ndiff, ggdmx maxdiff, ggdkd the steps whose 2283 note tokens differ although the margin exceeds 2 BFTOL. The logits slot is zeroed (ZERO 20) between 2284 note the two forwards, so ggdfwd cannot pass on ggfwd's rows. 2285 note BFTOL: the BF16 decode tolerance in nats, declared before its first chip run (ham/ollm-speed 2286 note 2026-11-10): modes 4 and 8 pass when maxdiff <= BFTOL and no high-margin step diverges. 2287 const BFTOL f64 0.25 2288 global ggdcl f64 65536 2289 global ggdmx f64 1 2290 global ggdnd ulong 1 2291 global ggdkd ulong 1 2292 global ggdst f64 1 2293 2294 note ggdcl[a] minus the largest other ggdcl[c], c < V 2295 fn ggdmg a ulong to f64 2296 b = bitcast f64 ulong `18442240474082181120` 2297 for c ulong get og 0 2298 if ne c a 2299 b = fmax b get ggdcl c 2300 end 2301 end 2302 ret sub get ggdcl a b 2303 end 2304 2305 fn ggdpar s ulong md ulong to ulong 2306 ggupt 2307 put gkinv 0 1 2308 ggdfm 0 2309 ogrd 2310 ggdkeep 2311 a0 = oargmax 2312 v = get og 0 2313 ggew 20 mul ggr v gga1 11 gga1 11 2314 if eq md 4 2315 ggdfm 3 2316 else 2317 ggdfm sub md 1 2318 end 2319 ogrd 2320 a1 = oargmax 2321 nd = ggdcmp 2322 dm = get ggdst 0 2323 put ggdnd 0 add get ggdnd 0 nd 2324 put ggdmx 0 fmax get ggdmx 0 dm 2325 mg = ggdmg a0 2326 if and ne a0 a1 gt mg mul 2.0f64 BFTOL 2327 put ggdkd 0 add get ggdkd 0 1 2328 end 2329 oputc 71 2330 oputc 81 2331 oputc 32 2332 oputu s 2333 oputc 32 2334 oputu get og 9 2335 oputc 32 2336 oputu a0 2337 oputc 32 2338 oputu a1 2339 oputc 32 2340 oputu nd 2341 oputc 32 2342 oputl long mul dm `1000000000`.0f64 2343 oputc 32 2344 oputl long mul mg `1000000000`.0f64 2345 oputc 10 2346 oflush 2347 ret a0 2348 end 2349 2350 note ggdcl = the V logits in TMP 2351 fn ggdkeep to void 2352 tm = get og 27 2353 for c ulong get og 0 2354 x = get oa add tm c 2355 put ggdcl c x 2356 end 2357 end 2358 2359 note the logits in TMP whose bits differ from ggdcl; the largest |difference| into ggdst[0] 2360 fn ggdcmp to ulong 2361 tm = get og 27 2362 nd = ulong 0 2363 dm = 0.0f64 2364 for c ulong get og 0 2365 x = get oa add tm c 2366 y = get ggdcl c 2367 if ne bitcast ulong x bitcast ulong y 2368 nd = add nd 1 2369 end 2370 dm = fmax dm fabs sub x y 2371 end 2372 put ggdst 0 dm 2373 ret nd 2374 end 2375 2376 fn ggdsel s ulong md ulong to ulong 2377 if or or or eq md 2 eq md 4 eq md 6 eq md 8 2378 ret ggdpar s md 2379 end 2380 ret ggdstep s md 2381 end 2382 2383 note gdbench CK N PROMPT MERGES MODE (ham/ollm-speed): the per-token chip forward, 2384 note Modes 7 and 8 select the BF16 decode policy and its comparison, respectively. 2385 note They retain QKV, attention and logits in f32; three projection GEMMs per layer use BF16. 2386 note timed, on the contexts of N greedy steps from token 0 then PROMPT's encoding (once the context 2387 note holds T tokens it slides, as ocpush does). MODE 0: ggfwd (all R = T rows, the gserve forward at 2388 note `edf39b0`); 1: ggdfwd (oglast's forward); 3: ggdfwd with BF16 GEMMs (ggdfm 3); 5: ggdfwd as one 2389 note batched submission (ggdbat); 7: selective BF16. Modes 2, 4, 6 and 8 compare ggfwd and ggdfwd 2390 note (2 f32, 4 BF16, 6 batched, 8 selective BF16) on every 2391 note step, one GQ line each (ggdpar), then `GDQ N ndiff maxdiff_e9 diverged`; modes 2 and 6 exit 41 2392 note when any logit differs in its bits; modes 4 and 8 exit 42 when maxdiff exceeds BFTOL or a high-margin step 2393 note diverged. Modes 0, 1, 3, 5 and 7 print one GD line per step (ggdstep), then run one more forward 2394 note at the last context with the launch profile on (`GP id gx gy gz m n k wait_ns` per launch, gk.om 2395 note gkpf; batched launches log wait 0), 64 ZERO launches of 64 floats (the bare launch round trip, 2396 note GP id 20) and `GDT mode N load_ns open_ns` (checkpoint and merges load; chip open and f32 2397 note parameter upload). 2398 fn ggdbench to void 2399 if ne oargc 7 2400 exit 2 2401 end 2402 t0 = monons 2403 ockloop 2 2404 nn = oargu 3 2405 md = oargu 6 2406 v = get og 0 2407 if or le v 256 gt md 8 2408 exit 2 2409 end 2410 obload 5 v 2411 t1 = monons 2412 ogchip 2413 if or eq md 5 eq md 6 2414 if not gbbopen 2415 ggdie 122 2416 end 2417 end 2418 t2 = monons 2419 put og 9 0 2420 ocpush 0 2421 np = obprompt 4 2422 for i ulong np 2423 ocpush get obtk i 2424 end 2425 put ggdmx 0 0.0f64 2426 put ggdnd 0 0 2427 put ggdkd 0 0 2428 for s ulong nn 2429 a = ggdsel s md 2430 ocpush a 2431 end 2432 if or or or eq md 2 eq md 4 eq md 6 eq md 8 2433 ggdverd nn md 2434 ret 2435 end 2436 ggdprof md 2437 oputc 71 2438 oputc 68 2439 oputc 84 2440 oputc 32 2441 oputu md 2442 oputc 32 2443 oputu nn 2444 oputc 32 2445 oputl sub t1 t0 2446 oputc 32 2447 oputl sub t2 t1 2448 oputc 10 2449 oflush 2450 r = nvstop 2451 end 2452 2453 note the parity modes' last line `GDQ N ndiff maxdiff_e9 diverged`, then their exit codes 2454 fn ggdverd nn ulong md ulong to void 2455 oputc 71 2456 oputc 68 2457 oputc 81 2458 oputc 32 2459 oputu nn 2460 oputc 32 2461 oputu get ggdnd 0 2462 oputc 32 2463 oputl long mul get ggdmx 0 `1000000000`.0f64 2464 oputc 32 2465 oputu get ggdkd 0 2466 oputc 10 2467 oflush 2468 r = nvstop 2469 if and and ne md 4 ne md 8 gt get ggdnd 0 0 2470 exit 41 2471 end 2472 if and or eq md 4 eq md 8 or gt get ggdmx 0 BFTOL gt get ggdkd 0 0 2473 exit 42 2474 end 2475 end 2476 2477 note one forward in mode md at the current context with the launch profile on, then 64 ZERO launches 2478 note of 64 floats; prints one GP line per profiled launch 2479 fn ggdprof md ulong to void 2480 put gkpc 0 0 2481 put gkpf 0 1 2482 ggupt 2483 put gkinv 0 1 2484 ggdfm md 2485 for i ulong 64 2486 ggew 20 64 ggg 0 ggg 0 2487 end 2488 put gkpf 0 0 2489 for q ulong get gkpc 0 2490 o = mul q 8 2491 oputc 71 2492 oputc 80 2493 for j ulong 8 2494 oputc 32 2495 oputu get gkpl add o j 2496 end 2497 oputc 10 2498 end 2499 end 2500 2501 note gscmp CK N PROMPT MERGES (V > 256): the chip forward against the CPU forward on the contexts of 2502 note N greedy steps from token 0 then PROMPT's encoding. Each step runs ofwd and olast (f64) into 2503 note gscl, then oglast (chip, f32) into TMP, and prints `GS step cpu_tok chip_tok maxdiff_nano 2504 note margin_nano`: maxdiff = max over the V logits of |chip - cpu|, margin = the CPU's top-1 logit 2505 note minus its top-2. The CPU token is pushed. Last line `GSCMP N diverged maxdiff_nano`: a step 2506 note diverges when the tokens differ although the margin exceeds 2 GSTOL. Exit 40 when a step 2507 note diverged or maxdiff exceeds GSTOL. 2508 const GSTOL f64 0.002 2509 global gscl f64 65536 2510 note gscmp totals: gsmd[0] the largest maxdiff, gskd[0] the diverged steps 2511 global gsmd f64 1 2512 global gskd ulong 1 2513 fn ggscmp to void 2514 if ne oargc 6 2515 exit 2 2516 end 2517 ockloop 2 2518 nn = oargu 3 2519 v = get og 0 2520 if le v 256 2521 exit 2 2522 end 2523 obload 5 v 2524 ogchip 2525 put og 9 0 2526 ocpush 0 2527 np = obprompt 4 2528 for i ulong np 2529 ocpush get obtk i 2530 end 2531 put gsmd 0 0.0f64 2532 put gskd 0 0 2533 for s ulong nn 2534 a = ggsst s 2535 ocpush a 2536 end 2537 md = get gsmd 0 2538 kd = get gskd 0 2539 oputc 71 2540 oputc 83 2541 oputc 67 2542 oputc 77 2543 oputc 80 2544 oputc 32 2545 oputu nn 2546 oputc 32 2547 oputu kd 2548 oputc 32 2549 oputl long mul md `1000000000`.0f64 2550 oputc 10 2551 oflush 2552 r = nvstop 2553 if or gt kd 0 gt md GSTOL 2554 exit 40 2555 end 2556 end 2557 2558 note one gscmp step s on the current context: prints its GS line, updates the totals, returns the 2559 note CPU token 2560 fn ggsst s ulong to ulong 2561 a1 = ggscpu 2562 mg = ggsmg a1 2563 oglast 2564 a2 = oargmax 2565 dm = ggsdf 2566 put gsmd 0 fmax get gsmd 0 dm 2567 if and ne a1 a2 gt mg mul 2.0f64 GSTOL 2568 put gskd 0 add get gskd 0 1 2569 end 2570 oputc 71 2571 oputc 83 2572 oputc 32 2573 oputu s 2574 oputc 32 2575 oputu a1 2576 oputc 32 2577 oputu a2 2578 oputc 32 2579 oputl long mul dm `1000000000`.0f64 2580 oputc 32 2581 oputl long mul mg `1000000000`.0f64 2582 oputc 10 2583 oflush 2584 ret a1 2585 end 2586 2587 note the CPU forward's last-position logits into gscl; returns its argmax 2588 fn ggscpu to ulong 2589 v = get og 0 2590 tm = get og 27 2591 lf = ofwd 0 2592 olast 2593 for c ulong v 2594 put gscl c get oa add tm c 2595 end 2596 ret oargmax 2597 end 2598 2599 note max over c < V of |TMP[c] - gscl[c]| 2600 fn ggsdf to f64 2601 tm = get og 27 2602 dm = 0.0f64 2603 for c ulong get og 0 2604 x = fabs sub get oa add tm c get gscl c 2605 dm = fmax dm x 2606 end 2607 ret dm 2608 end 2609 2610 note the CPU's top-1 logit gscl[a] minus its largest other logit 2611 fn ggsmg a ulong to f64 2612 b = bitcast f64 ulong `18442240474082181120` 2613 for c ulong get og 0 2614 if ne c a 2615 b = fmax b get gscl c 2616 end 2617 end 2618 ret sub get gscl a b 2619 end 2620 2621 note ogcmd: the gb10 target's GPU commands. cli.om's ocmd offers every command word here first 2622 note (nogpu.om's ogcmd takes none on the CPU-only targets), so the gb10 unit is the aarch64 source 2623 note with gpu/ in place of nogpu.om and no renamed main. It runs the GPU commands here and the BF16 2624 note training commands in gb.om and returns true; any other word returns false and ocmd goes on to 2625 note the CPU commands. The inference interface is documented in docs/omegallm/CONTRACT-BF16.md; 2626 note command constants encode little-endian word bytes. 2627 fn ogcmd w ulong to bool 2628 if eq w `27696107117441895` 2629 put bfdec 0 1 2630 put ogpu 0 1 2631 cmdserve 2632 r = nvstop 2633 ret true 2634 end 2635 if eq w `108235010962279` 2636 put bfdec 0 1 2637 put ogpu 0 1 2638 cmdsamp 2639 r = nvstop 2640 ret true 2641 end 2642 if eq w `111559399797607` 2643 put ogpu 0 1 2644 cmdserve 2645 r = nvstop 2646 ret true 2647 end 2648 if eq w `28548202775016295` 2649 put ogpu 0 1 2650 cmdsamp 2651 r = nvstop 2652 ret true 2653 end 2654 if eq w `482871571303` 2655 ggscmp 2656 ret true 2657 end 2658 if eq w `29382723376407655` 2659 ggdbench 2660 ret true 2661 end 2662 if eq w `1886217063` 2663 ggcmp 2664 ret true 2665 end 2666 if eq w `216634581863` 2667 ggcmp2 2668 ret true 2669 end 2670 if eq w `121398885512295` 2671 ggtrain 2672 ret true 2673 end 2674 if eq w `28831846509999207` 2675 ggtrainf 2676 ret true 2677 end 2678 if eq w `482871566695` 2679 ggacmp 2680 ret true 2681 end 2682 if eq w `465491617127` 2683 ggeval 2684 ret true 2685 end 2686 if eq w `444015928423` 2687 ggdiag 2688 ret true 2689 end 2690 if eq w `439956435047` 2691 ggprof 2692 ret true 2693 end 2694 if eq w `27705946603156583` 2695 gbtrain 2696 ret true 2697 end 2698 if eq w `108192095957095` 2699 gbprof 2700 ret true 2701 end 2702 if eq w `482871566951` 2703 gbcmp 2704 ret true 2705 end 2706 if eq w `55458452955751` 2707 gbcmp2 2708 ret true 2709 end 2710 if eq w `113668228801127` 2711 gbfrag 2712 ret true 2713 end 2714 ret false 2715 end