omega-helix
This project has not described itself yet.
Brought by drakestapleton
omega-helix / native / gpu / gg.om
| 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 56 |
| 11 | global gknv4 uint 1 |
| 12 | global ggw4_ok ulong 1 |
| 13 | global ggn4p ulong 3 |
| 14 | global ggn4s f64 2 |
| 15 | global ggn4q uint 1 |
| 16 | global ggn4ep uint 1 |
| 17 | global ggenv uchar 64 |
| 18 | text ggnv4e "OLLM_NVFP4=1" |
| 19 | global ggcn ulong 65537 |
| 20 | 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) |
| 21 | global ggs uint 8 |
| 22 | note OMUTG: 1 in the product; 0 in mutant g (DK = -DS^T Q), which the parity check must catch. |
| 23 | const OMUTG ulong 1 |
| 24 | note Looped models (docs/LOOP.md): the layer region holds one activation block per layer application |
| 25 | note (og 56 of them), and application l reads the parameters and adds to the gradient of parameter layer |
| 26 | note olp l (model.om ovg1..ovw2). Every weight-gradient launch accumulates into G, which ggbwd zeroes |
| 27 | note once, so a core's gradient is the sum over its passes. Without a loop og 56 = L and olp l = l. |
| 28 | note OMUTLG: 1 in the product; 0 in mutant lg (the w2 gradient of an application that repeats an |
| 29 | note earlier parameter layer is not launched, model.om's mutant lp on the chip), which gcmp and gcmp2 on |
| 30 | note a looped checkpoint must catch. Without a loop the two are the same. |
| 31 | const OMUTLG ulong 1 |
| 32 | note Frozen-core training on the chip (spec.om trainf's FREEZE_LAYERS F). ggfz: 0 the frozen |
| 33 | note parameter count fz = og 31 + F og 32 (the embeddings wte and wpe, then layers 0..F-1), 1 F. With |
| 34 | note fz > 0, ggbwd stops after layer F and launches no gradient into [0, fz) (the tied head's wte |
| 35 | note term, the WPE column sums, the embedding scatter), so G[0, fz) stays the zero ggbwd wrote; ggadam |
| 36 | note clips over that G and steps only [fz, P); ggdnst brings back only [fz, P), so the frozen f64 |
| 37 | note weights and moments on the host stay bit for bit (spext checks them by hash). A looped |
| 38 | note checkpoint with F > 0 is exit 17, as cmdtrainf (ockload) refuses it on the CPU. |
| 39 | note OMUTFZ: 1 in the product; 0 in mutant fz (the chip ignores the freeze: full backward, Adam and |
| 40 | note download), which gcmp and gacmp with FREEZE_LAYERS and spext after gtrainf must catch. |
| 41 | const OMUTFZ ulong 1 |
| 42 | global ggfz ulong 2 |
| 43 | |
| 44 | note GPU setup failed (ggopen): a clear stage and error on stderr, then exit 75. L1014: a setup |
| 45 | note that failed before buffers 2 and 3 existed used to reach ggdie's mapget of the unmapped slots, which |
| 46 | note trapped BOUNDS (exit 103) with no message. stdout stays free for the program's own output. |
| 47 | note Error 81 is RM status 0x51 NV_ERR_NO_MEMORY (open-gpu-kernel-modules nvstatuscodes.h). Only stage 35 |
| 48 | note (the compute object, context.om) waits on it, 50 x 100 ms, because there it means another job holds |
| 49 | note the GPU. At any other stage, such as 21 (a memory allocation), there is no wait and no retry, so |
| 50 | note "try again" is not offered; the cause there is not established by this code, so the text names none. |
| 51 | text ggsa "omega: GPU setup failed at stage " |
| 52 | text ggsb ", error " |
| 53 | text ggsc " (driver status 81, out of memory: seen while another job holds the GPU; it outlasted the bounded wait). Try again.\n" |
| 54 | 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" |
| 55 | text ggsd " (positive is driver status, negative is errno or internal; the stage is the nvstage step in gpu/context.om).\n" |
| 56 | |
| 57 | fn ggerr1 n ulong to void |
| 58 | p = ulong 0 |
| 59 | while lt p n bound 4097 |
| 60 | w = write 2 oout p sub n p |
| 61 | if le w 0 |
| 62 | exit 70 |
| 63 | end |
| 64 | p = add p ulong w |
| 65 | end |
| 66 | put oon 0 0 |
| 67 | end |
| 68 | |
| 69 | fn ggsetupfail to void |
| 70 | oflush |
| 71 | r = write 2 ggsa 0 len ggsa |
| 72 | oputu ulong get nvstage 0 |
| 73 | ggerr1 get oon 0 |
| 74 | r = write 2 ggsb 0 len ggsb |
| 75 | oputl get nverr 0 |
| 76 | ggerr1 get oon 0 |
| 77 | if eq get nverr 0 81 |
| 78 | if eq get nvstage 0 35 |
| 79 | r = write 2 ggsc 0 len ggsc |
| 80 | else |
| 81 | r = write 2 ggse 0 len ggse |
| 82 | end |
| 83 | else |
| 84 | r = write 2 ggsd 0 len ggsd |
| 85 | end |
| 86 | exit 75 |
| 87 | end |
| 88 | |
| 89 | fn ggdie c ulong to void |
| 90 | oputc 71 |
| 91 | oputc 88 |
| 92 | oputc 32 |
| 93 | oputu c |
| 94 | oputc 32 |
| 95 | oputl get nverr 0 |
| 96 | oputc 32 |
| 97 | oputu ulong get nvstage 0 |
| 98 | note diagnostics: launches done, mark1, mark2, QMD semaphore; zero while slots 2 and 3 are unmapped |
| 99 | note (a failed ggopen), where mapget of an unmapped slot trapped BOUNDS (exit 103, no message) |
| 100 | oputc 32 |
| 101 | oputl get gkt 0 |
| 102 | oputc 32 |
| 103 | if and ne get gbmem 3 0 ne get gbmem 2 0 |
| 104 | oputu mapget get gbmem 3 0 4 |
| 105 | oputc 32 |
| 106 | oputu mapget get gbmem 3 16 4 |
| 107 | oputc 32 |
| 108 | oputu mapget get gbmem 2 8192 4 |
| 109 | oputc 32 |
| 110 | oputu get gbst 4 |
| 111 | oputc 32 |
| 112 | oputu get gbst 5 |
| 113 | else |
| 114 | oputc 48 |
| 115 | oputc 32 |
| 116 | oputc 48 |
| 117 | oputc 32 |
| 118 | oputc 48 |
| 119 | end |
| 120 | oputc 10 |
| 121 | oflush |
| 122 | exit 84 |
| 123 | end |
| 124 | |
| 125 | fn ggcd n ulong k ulong to ulong |
| 126 | ret div add n sub k 1 k |
| 127 | end |
| 128 | |
| 129 | fn ggr to ulong |
| 130 | ret mul get og 6 get og 1 |
| 131 | end |
| 132 | |
| 133 | note P rounded up to the SQN chunk of 512; the gradient buffer is that long and zeroed to it |
| 134 | fn ggp5 to ulong |
| 135 | ret mul ggcd get og 8 512 512 |
| 136 | end |
| 137 | |
| 138 | fn ggput i ulong n ulong to void |
| 139 | c = get gga 47 |
| 140 | put gga i c |
| 141 | b = mul div add mul n 4 255 256 256 |
| 142 | put gga 47 add c b |
| 143 | end |
| 144 | |
| 145 | fn ggplan_nv4 r ulong d ulong f ulong to void |
| 146 | if eq get gknv4 0 0 |
| 147 | put gga 41 0 |
| 148 | put gga 42 0 |
| 149 | put gga 43 0 |
| 150 | put gga 44 0 |
| 151 | put gga 48 0 |
| 152 | put gga 49 0 |
| 153 | put gga 50 0 |
| 154 | put gga 51 0 |
| 155 | ret |
| 156 | end |
| 157 | mx = mul 3 mul r d |
| 158 | t = mul r f |
| 159 | if gt t mx |
| 160 | mx = t |
| 161 | end |
| 162 | t = mul d f |
| 163 | if gt t mx |
| 164 | mx = t |
| 165 | end |
| 166 | t = mul r get og 0 |
| 167 | if gt t mx |
| 168 | mx = t |
| 169 | end |
| 170 | t = mul d get og 0 |
| 171 | if gt t mx |
| 172 | mx = t |
| 173 | end |
| 174 | nnv = ggcd mx 8 |
| 175 | nsx = ggcd mx 64 |
| 176 | ggput 41 nnv |
| 177 | ggput 42 nsx |
| 178 | ggput 43 nnv |
| 179 | ggput 44 nsx |
| 180 | wlay = add mul 4 mul d d mul 2 mul d f |
| 181 | wtot = mul get og 5 wlay |
| 182 | wnv = ggcd wtot 8 |
| 183 | wsx = ggcd wtot 64 |
| 184 | ggput 48 wnv |
| 185 | ggput 49 wsx |
| 186 | ggput 50 wnv |
| 187 | ggput 51 wsx |
| 188 | end |
| 189 | |
| 190 | note arena plan for the dims in og; returns its size in bytes |
| 191 | fn ggplan to ulong |
| 192 | tt = get og 1 |
| 193 | d = get og 2 |
| 194 | h = get og 3 |
| 195 | f = get og 4 |
| 196 | bb = get og 6 |
| 197 | r = ggr |
| 198 | rd = mul r d |
| 199 | pp = mul mul bb h mul tt tt |
| 200 | put gga 47 0 |
| 201 | for i ulong 8 |
| 202 | ggput add 24 i rd |
| 203 | end |
| 204 | ggput 32 r |
| 205 | ggput 33 r |
| 206 | ggput 34 mul r f |
| 207 | ggput 35 mul r f |
| 208 | ggput 36 pp |
| 209 | put gga 7 get gga 47 |
| 210 | put gga 47 0 |
| 211 | ggput 0 get og 8 |
| 212 | ggput 1 ggp5 |
| 213 | ggput 2 r |
| 214 | ggput 3 r |
| 215 | ggput 4 add get og 0 1 |
| 216 | ggput 5 r |
| 217 | put gga 6 get gga 47 |
| 218 | ls = mul get gga 7 get og 56 |
| 219 | put gga 47 add get gga 47 ls |
| 220 | ggput 8 rd |
| 221 | ggput 9 r |
| 222 | ggput 10 rd |
| 223 | ggput 11 mul r get og 0 |
| 224 | ggput 12 mul r 3 |
| 225 | ggput 13 rd |
| 226 | ggput 14 rd |
| 227 | ggput 15 mul rd 3 |
| 228 | ggput 16 rd |
| 229 | ggput 17 mul r f |
| 230 | ggput 18 pp |
| 231 | ggput 19 rd |
| 232 | ggput 20 mul bb d |
| 233 | ggput 21 get og 8 |
| 234 | ggput 22 get og 8 |
| 235 | sn = div ggp5 512 |
| 236 | if lt sn 2048 |
| 237 | sn = 2048 |
| 238 | end |
| 239 | ggput 23 sn |
| 240 | note 37: split-K partials for ggsk, up to 4M floats (16 MB) so 128x128 dW fills 3.0 waves on 48 SMs |
| 241 | ggput 37 4194304 |
| 242 | note 38: packed-BF16 weight shadow (P * 2 bytes = ggp5 / 2 u32 words) for native BF16-IO 128x128 GEMMs |
| 243 | ggput 38 div ggp5 2 |
| 244 | note 39: packed-BF16 dlogits scratch (R * V * 2 bytes) so F32->BF16 after CE is out-of-place & race-free |
| 245 | ggput 39 div mul r get og 0 2 |
| 246 | note 41..44: NVFP4 quantized scratch buffers (A_nv4, Sa_nv4, B_nv4, Sb_nv4) when gknv4 != 0 |
| 247 | ggplan_nv4 r d f |
| 248 | ret get gga 47 |
| 249 | end |
| 250 | |
| 251 | fn gga1 i ulong to ulong |
| 252 | ret add get gga 40 get gga i |
| 253 | end |
| 254 | |
| 255 | fn ggl l ulong f ulong to ulong |
| 256 | o = add get gga 6 mul l get gga 7 |
| 257 | ret add add get gga 40 o get gga add 24 f |
| 258 | end |
| 259 | |
| 260 | note address of the output of layer application l (the next hin, or HF after the last one) |
| 261 | fn ggout l ulong to ulong |
| 262 | if lt add l 1 get og 56 |
| 263 | ret ggl add l 1 0 |
| 264 | end |
| 265 | ret gga1 8 |
| 266 | end |
| 267 | |
| 268 | fn ggw o ulong to ulong |
| 269 | ret add gga1 0 mul o 4 |
| 270 | end |
| 271 | |
| 272 | fn ggwh o ulong to ulong |
| 273 | ret add gga1 38 mul o 2 |
| 274 | end |
| 275 | |
| 276 | fn ggg o ulong to ulong |
| 277 | ret add gga1 1 mul o 4 |
| 278 | end |
| 279 | |
| 280 | note GEMM tiles need m, n multiples of 32 and k of 16: V T D F E multiples of 32; R = B T; |
| 281 | note softmax rows keep T/32 values per lane in R32..R47, so T is at most 512 |
| 282 | fn ggok to bool |
| 283 | for i ulong 5 |
| 284 | v = get og i |
| 285 | if eq i 3 |
| 286 | v = get og 7 |
| 287 | end |
| 288 | if eq i 1 |
| 289 | v = get og 1 |
| 290 | end |
| 291 | if or eq v 0 ne rem v 32 0 |
| 292 | ret false |
| 293 | end |
| 294 | end |
| 295 | if gt get og 0 65536 |
| 296 | ret false |
| 297 | end |
| 298 | if gt get og 1 512 |
| 299 | ret false |
| 300 | end |
| 301 | ret le mul get og 6 get og 3 65535 |
| 302 | end |
| 303 | |
| 304 | note open the chip and emit the kernels for the dims in og |
| 305 | fn ggopen to void |
| 306 | if not ggok |
| 307 | ggdie 1 |
| 308 | end |
| 309 | asz = ggplan |
| 310 | v = get og 0 |
| 311 | tt = get og 1 |
| 312 | d = get og 2 |
| 313 | h = get og 3 |
| 314 | f = get og 4 |
| 315 | bb = get og 6 |
| 316 | grset v tt d h f bb |
| 317 | if not gropen asz |
| 318 | ggsetupfail |
| 319 | end |
| 320 | if not gbbopen |
| 321 | ggdie 122 |
| 322 | end |
| 323 | put gga 40 gkbase |
| 324 | ggstr |
| 325 | end |
| 326 | |
| 327 | note pointer j (1-based) into arg slots 2j, 2j + 1 |
| 328 | fn ggpt j uint a ulong to void |
| 329 | put gkarg mul j 2 gblo a |
| 330 | put gkarg add mul j 2 1 gbhi a |
| 331 | end |
| 332 | |
| 333 | fn ggu s uint v ulong to void |
| 334 | put gkarg s uint v |
| 335 | end |
| 336 | |
| 337 | fn ggrun id uint na uint gx ulong gy ulong gz ulong to void |
| 338 | put gkna 0 na |
| 339 | x = uint gx |
| 340 | y = uint gy |
| 341 | z = uint gz |
| 342 | if not gklaunch id x y z |
| 343 | ggdie add 100 ulong id |
| 344 | end |
| 345 | end |
| 346 | |
| 347 | fn ggmm m ulong n ulong k ulong nz ulong h2 ulong bt bool to void |
| 348 | a = uint m |
| 349 | b = uint n |
| 350 | c = uint k |
| 351 | e = uint nz |
| 352 | g = uint h2 |
| 353 | if not gkmm a b c e g bt |
| 354 | ggdie 99 |
| 355 | end |
| 356 | end |
| 357 | |
| 358 | note Packed-BF16 Native-IO GEMM & conversion helpers (slots 67..74, 82..83) |
| 359 | fn ggmmh m ulong n ulong k ulong nz ulong h2 ulong fl uint to void |
| 360 | a = uint m |
| 361 | b = uint n |
| 362 | c = uint k |
| 363 | e = uint nz |
| 364 | g = uint h2 |
| 365 | if not gkmmh a b c e g fl |
| 366 | ggdie 99 |
| 367 | end |
| 368 | end |
| 369 | |
| 370 | note NVFP4 (OMMA.SF.16864) mode state, env check, quantizer (slots 88/89), and 128x128 GEMM (slots 90..95) |
| 371 | fn ggsnv4 v uint to void |
| 372 | put gknv4 0 v |
| 373 | end |
| 374 | |
| 375 | fn ggnv4_eq n long to bool |
| 376 | if ne n long len ggnv4e |
| 377 | ret false |
| 378 | end |
| 379 | for j ulong len ggnv4e |
| 380 | if ne get ggenv j get ggnv4e j |
| 381 | ret false |
| 382 | end |
| 383 | end |
| 384 | ret true |
| 385 | end |
| 386 | |
| 387 | fn ggnv4_env to bool |
| 388 | ec = ulong envcnt |
| 389 | for i ulong ec bound 4096 |
| 390 | n = envread i ggenv 0 64 |
| 391 | if ggnv4_eq n |
| 392 | ret true |
| 393 | end |
| 394 | end |
| 395 | ret false |
| 396 | end |
| 397 | |
| 398 | fn ggnv4_shape m ulong n ulong k ulong to bool |
| 399 | if or eq m 0 or eq n 0 eq k 0 |
| 400 | ret false |
| 401 | end |
| 402 | if or ne rem m 128 0 ne rem n 128 0 |
| 403 | ret false |
| 404 | end |
| 405 | ret eq rem k 64 0 |
| 406 | end |
| 407 | |
| 408 | fn ggnv4_ok to bool |
| 409 | if not ggbf_ok |
| 410 | ret false |
| 411 | end |
| 412 | if eq get gknv4 0 0 |
| 413 | ret false |
| 414 | end |
| 415 | ret eq rem get og 2 128 0 |
| 416 | end |
| 417 | |
| 418 | fn ggdw_nv4_ok m ulong n ulong to bool |
| 419 | if not ggnv4_ok |
| 420 | ret false |
| 421 | end |
| 422 | if not ggnv4_shape m n ggr |
| 423 | ret false |
| 424 | end |
| 425 | ret ge mul m n 786432 |
| 426 | end |
| 427 | |
| 428 | fn ggqnv4_p ps f64 to void |
| 429 | put ggn4q 0 bitcast uint f32 ps |
| 430 | end |
| 431 | |
| 432 | fn ggmm4_sc sa f64 sb f64 to void |
| 433 | put ggn4s 0 sa |
| 434 | put ggn4s 1 sb |
| 435 | end |
| 436 | |
| 437 | note Quantize BF16 operand to NVFP4 E2M1 + UE4M3 block-16 scales: |
| 438 | note tr=false -> slot 88 (row-major [m,k], stride=k*2, grid=(m,1,1)) |
| 439 | note tr=true -> slot 89 (transposed [k,m]->[m,k], stride=m*2, grid=(m/32,k/64,1)) |
| 440 | note gkarg[9] holds f32 pre_scale bits (defaults to 1.0f = 0x3f800000 when ggn4q[0] == 0) |
| 441 | fn ggqnv4 src ulong dst ulong sc ulong m ulong k ulong tr bool to void |
| 442 | ggpt 0 src |
| 443 | ggpt 1 dst |
| 444 | ggpt 2 sc |
| 445 | ggu 6 m |
| 446 | ggu 7 k |
| 447 | ps = get ggn4q 0 |
| 448 | if eq ps 0 |
| 449 | ps = 1065353216 |
| 450 | end |
| 451 | ggu 9 ulong ps |
| 452 | if tr |
| 453 | ggu 8 mul m 2 |
| 454 | ggrun 89 10 div m 32 div k 64 1 |
| 455 | ret |
| 456 | end |
| 457 | ggu 8 mul k 2 |
| 458 | ggrun 88 10 div m 8 div k 64 1 |
| 459 | end |
| 460 | |
| 461 | fn ggmm4_id fl uint ep uint to uint |
| 462 | if eq ep 1 |
| 463 | ret 94 |
| 464 | end |
| 465 | if eq ep 2 |
| 466 | ret 95 |
| 467 | end |
| 468 | bt = ne and fl 1 0 |
| 469 | obf = ne and fl 2 0 |
| 470 | if obf |
| 471 | if bt |
| 472 | ret 91 |
| 473 | end |
| 474 | ret 90 |
| 475 | end |
| 476 | if bt |
| 477 | ret 93 |
| 478 | end |
| 479 | ret 92 |
| 480 | end |
| 481 | |
| 482 | fn ggmm4_ab anv ulong asx ulong bnv ulong bsx ulong m ulong n ulong to void |
| 483 | ggpt 0 anv |
| 484 | ggpt 1 asx |
| 485 | ggu 4 mul m 32 |
| 486 | ggu 5 mul m 4 |
| 487 | ggu 6 0 |
| 488 | ggu 7 0 |
| 489 | ggpt 4 bnv |
| 490 | ggpt 5 bsx |
| 491 | ggu 12 mul n 32 |
| 492 | ggu 13 mul n 4 |
| 493 | ggu 14 0 |
| 494 | ggu 15 0 |
| 495 | end |
| 496 | |
| 497 | fn gggrad_sc to f64 |
| 498 | s = mul 2.0f64 f64 ggr |
| 499 | if lt s 4096.0f64 |
| 500 | ret 4096.0f64 |
| 501 | end |
| 502 | ret s |
| 503 | end |
| 504 | |
| 505 | fn ggw4_el lp ulong wi ulong to ulong |
| 506 | d = get og 2 |
| 507 | f = get og 4 |
| 508 | dd = mul d d |
| 509 | df = mul d f |
| 510 | wlay = add mul 4 dd mul 2 df |
| 511 | base = mul lp wlay |
| 512 | if lt wi 4 |
| 513 | ret add base mul wi dd |
| 514 | end |
| 515 | if eq wi 4 |
| 516 | ret add base mul 4 dd |
| 517 | end |
| 518 | ret add base add mul 4 dd df |
| 519 | end |
| 520 | |
| 521 | fn ggw4_dt lp ulong wi ulong tr bool to ulong |
| 522 | el = ggw4_el lp wi |
| 523 | if tr |
| 524 | ret add gga1 48 div el 2 |
| 525 | end |
| 526 | ret add gga1 50 div el 2 |
| 527 | end |
| 528 | |
| 529 | fn ggw4_sc lp ulong wi ulong tr bool to ulong |
| 530 | el = ggw4_el lp wi |
| 531 | if tr |
| 532 | ret add gga1 49 div el 16 |
| 533 | end |
| 534 | ret add gga1 51 div el 16 |
| 535 | end |
| 536 | |
| 537 | fn ggmm4_ab_z m ulong n ulong ks ulong to void |
| 538 | ggpt 0 gga1 41 |
| 539 | ggpt 1 gga1 42 |
| 540 | ggu 4 mul m 32 |
| 541 | ggu 5 mul m 4 |
| 542 | ggu 6 div mul ks m 2 |
| 543 | ggu 7 div mul ks m 16 |
| 544 | ggpt 4 gga1 43 |
| 545 | ggpt 5 gga1 44 |
| 546 | ggu 12 mul n 32 |
| 547 | ggu 13 mul n 4 |
| 548 | ggu 14 div mul ks n 2 |
| 549 | ggu 15 div mul ks n 16 |
| 550 | end |
| 551 | |
| 552 | fn ggmm4_lch_z m ulong n ulong k ulong nz ulong fl uint to void |
| 553 | ep = get gkep 0 |
| 554 | put gkep 0 0 |
| 555 | put nverr 0 0 |
| 556 | put gkarg 23 gkmagic uint nz uint nz |
| 557 | if ne get nverr 0 0 |
| 558 | put gkfl 0 0 |
| 559 | ggdie 99 |
| 560 | end |
| 561 | put gkarg 24 neg uint nz |
| 562 | put gkarg 22 uint div k 64 |
| 563 | put gkarg 26 0 |
| 564 | put gkarg 27 0 |
| 565 | put gkarg 28 1073741824 |
| 566 | put gkbp 7 0 |
| 567 | put gkps 0 uint m |
| 568 | put gkps 1 uint n |
| 569 | put gkps 2 uint k |
| 570 | if ne ep 0 |
| 571 | ep_sc = get ggn4ep 0 |
| 572 | put ggn4ep 0 0 |
| 573 | put gkarg 6 gblo gga1 43 |
| 574 | put gkarg 7 gbhi gga1 43 |
| 575 | put gkarg 14 gblo gga1 44 |
| 576 | put gkarg 15 gbhi gga1 44 |
| 577 | put gkarg 26 uint m |
| 578 | put gkarg 27 ep_sc |
| 579 | put gkna 0 32 |
| 580 | else |
| 581 | put gkna 0 29 |
| 582 | end |
| 583 | id = ggmm4_id fl ep |
| 584 | if not gkmmsw id uint div n 128 uint div m 128 uint nz 2 |
| 585 | ggdie 99 |
| 586 | end |
| 587 | end |
| 588 | |
| 589 | fn ggmm4_lch m ulong n ulong k ulong fl uint to void |
| 590 | ggmm4_lch_z m n k 1 fl |
| 591 | end |
| 592 | |
| 593 | fn ggks_nv4 m ulong n ulong to ulong |
| 594 | r = ggr |
| 595 | b128 = mul div m 128 div n 128 |
| 596 | mn = mul m n |
| 597 | s = ulong 1 |
| 598 | while lt mul b128 s 144 bound 8 |
| 599 | s2 = mul s 2 |
| 600 | if gt s2 16 |
| 601 | ret s |
| 602 | end |
| 603 | if ne rem r mul s2 64 0 |
| 604 | ret s |
| 605 | end |
| 606 | if gt mul mn s2 4194304 |
| 607 | ret s |
| 608 | end |
| 609 | s = s2 |
| 610 | end |
| 611 | ret s |
| 612 | end |
| 613 | |
| 614 | fn ggmm4_pt a ulong b ulong c ulong to void |
| 615 | put ggn4p 0 a |
| 616 | put ggn4p 1 b |
| 617 | put ggn4p 2 c |
| 618 | end |
| 619 | |
| 620 | note Cached-weight NVFP4 GEMM: ggn4p[0]=x, ggn4p[2]=c, ggn4s[0]=sa |
| 621 | fn ggmm4_cwp lp ulong wi ulong tr bool n ulong k ulong fl uint to void |
| 622 | sa = get ggn4s 0 |
| 623 | if eq sa 0.0f64 |
| 624 | sa = 1.0f64 |
| 625 | end |
| 626 | put ggn4s 0 1.0f64 |
| 627 | ggmm4_ab gga1 41 gga1 42 ggw4_dt lp wi tr ggw4_sc lp wi tr ggr n |
| 628 | al = div 1.0f64 mul sa 16.0f64 |
| 629 | gkopc get ggn4p 2 uint mul n 2 2 0 0 f32 al |
| 630 | ggmm4_lch ggr n k fl |
| 631 | end |
| 632 | |
| 633 | fn ggmm4_cwp2 lp ulong wi ulong tr bool n ulong k ulong fl uint to void |
| 634 | sa = get ggn4s 0 |
| 635 | if eq sa 0.0f64 |
| 636 | sa = 1.0f64 |
| 637 | end |
| 638 | put ggn4s 0 1.0f64 |
| 639 | ggmm4_ab gga1 43 gga1 44 ggw4_dt lp wi tr ggw4_sc lp wi tr ggr n |
| 640 | al = div 1.0f64 mul sa 16.0f64 |
| 641 | gkopc get ggn4p 2 uint mul n 2 2 0 0 f32 al |
| 642 | ggmm4_lch ggr n k fl |
| 643 | end |
| 644 | |
| 645 | fn ggmm4_cw lp ulong wi ulong tr bool n ulong k ulong fl uint to void |
| 646 | sa = get ggn4s 0 |
| 647 | if eq sa 0.0f64 |
| 648 | sa = 1.0f64 |
| 649 | end |
| 650 | ggqnv4_p sa |
| 651 | ggqnv4 get ggn4p 0 gga1 41 gga1 42 ggr k false |
| 652 | ggqnv4_p 1.0f64 |
| 653 | ggmm4_cwp lp wi tr n k fl |
| 654 | end |
| 655 | |
| 656 | note On-chip BF16->NVFP4 quantize (with 2-level tensor pre-scale ggn4s[0..1]) + K-looped 128x128 GEMM (slots 88/89 + 90..95) |
| 657 | fn ggmm4 m ulong n ulong k ulong fl uint ta bool tb bool to void |
| 658 | sa = get ggn4s 0 |
| 659 | sb = get ggn4s 1 |
| 660 | if eq sa 0.0f64 |
| 661 | sa = 1.0f64 |
| 662 | end |
| 663 | if eq sb 0.0f64 |
| 664 | sb = 1.0f64 |
| 665 | end |
| 666 | put ggn4s 0 1.0f64 |
| 667 | put ggn4s 1 1.0f64 |
| 668 | ggqnv4_p sa |
| 669 | ggqnv4 get ggn4p 0 gga1 41 gga1 42 m k ta |
| 670 | ggqnv4_p sb |
| 671 | ggqnv4 get ggn4p 1 gga1 43 gga1 44 n k tb |
| 672 | ggqnv4_p 1.0f64 |
| 673 | ggmm4_ab gga1 41 gga1 42 gga1 43 gga1 44 m n |
| 674 | al = div 1.0f64 mul sa sb |
| 675 | if ne and fl 2 0 |
| 676 | gkopc get ggn4p 2 uint mul n 2 2 0 0 f32 al |
| 677 | else |
| 678 | gkopc get ggn4p 2 uint mul n 4 4 0 0 f32 al |
| 679 | end |
| 680 | ggmm4_lch m n k fl |
| 681 | end |
| 682 | |
| 683 | fn ggxwh_bf x ulong w ulong c ulong n ulong k ulong bt bool to void |
| 684 | fl = uint 2 |
| 685 | if bt |
| 686 | fl = 3 |
| 687 | end |
| 688 | k2 = uint mul k 2 |
| 689 | n2 = uint mul n 2 |
| 690 | gkoph 0 x k2 2 0 0 |
| 691 | gkoph 1 w 2 n2 0 0 |
| 692 | gkopc c n2 2 0 0 f32 1.0f64 |
| 693 | ggmmh ggr n k 1 1 fl |
| 694 | end |
| 695 | |
| 696 | fn ggxwh x ulong w ulong c ulong n ulong k ulong bt bool to void |
| 697 | fl = uint 2 |
| 698 | if bt |
| 699 | fl = 3 |
| 700 | end |
| 701 | if and ggnv4_ok ggnv4_shape ggr n k |
| 702 | ggmm4_pt x w c |
| 703 | ggmm4 ggr n k fl false true |
| 704 | ret |
| 705 | end |
| 706 | ggmm4_sc 1.0f64 1.0f64 |
| 707 | ggxwh_bf x w c n k bt |
| 708 | end |
| 709 | |
| 710 | fn ggxwth x ulong w ulong c ulong n ulong k ulong bt bool to void |
| 711 | fl = uint 2 |
| 712 | if bt |
| 713 | fl = 3 |
| 714 | end |
| 715 | if and ggnv4_ok ggnv4_shape ggr n k |
| 716 | ggmm4_pt x w c |
| 717 | ggmm4 ggr n k fl false false |
| 718 | ret |
| 719 | end |
| 720 | ggmm4_sc 1.0f64 1.0f64 |
| 721 | k2 = uint mul k 2 |
| 722 | n2 = uint mul n 2 |
| 723 | gkoph 0 x k2 2 0 0 |
| 724 | gkoph 1 w k2 2 0 0 |
| 725 | gkopc c n2 2 0 0 f32 1.0f64 |
| 726 | ggmmh ggr n k 1 1 fl |
| 727 | end |
| 728 | |
| 729 | fn ggskh m ulong n ulong nz ulong s ulong g ulong gz uint to void |
| 730 | mn = mul m n |
| 731 | p = gga1 37 |
| 732 | n4 = uint mul n 4 |
| 733 | c1 = uint mul mul mn s 4 |
| 734 | c2 = uint mul mn 4 |
| 735 | gkopc p n4 4 c1 c2 f32 1.0f64 |
| 736 | k = div ggr s |
| 737 | z = mul nz s |
| 738 | ggmmh m n k z s 0 |
| 739 | ggskr mn nz s g gz |
| 740 | end |
| 741 | |
| 742 | fn ggf2b n ulong src ulong dst ulong to void |
| 743 | n8 = div n 8 |
| 744 | ggu 0 n8 |
| 745 | ggpt 1 src |
| 746 | ggpt 2 dst |
| 747 | ggrun 82 6 ggcd n8 256 1 1 |
| 748 | end |
| 749 | |
| 750 | fn ggb2f n ulong src ulong dst ulong to void |
| 751 | n8 = div n 8 |
| 752 | ggu 0 n8 |
| 753 | ggpt 1 src |
| 754 | ggpt 2 dst |
| 755 | ggrun 83 6 ggcd n8 256 1 1 |
| 756 | end |
| 757 | |
| 758 | note C[R][n] (+)= X[R][k] W[k][n] |
| 759 | fn ggxw x ulong w ulong c ulong n ulong k ulong bt bool to void |
| 760 | k4 = uint mul k 4 |
| 761 | n4 = uint mul n 4 |
| 762 | gkopa 0 x k4 4 0 0 |
| 763 | gkopa 1 w 4 n4 0 0 |
| 764 | gkopc c n4 4 0 0 f32 1.0f64 |
| 765 | ggmm ggr n k 1 1 bt |
| 766 | end |
| 767 | |
| 768 | note C[R][n] (+)= X[R][k] W[n][k]^T |
| 769 | fn ggxwt x ulong w ulong c ulong n ulong k ulong bt bool to void |
| 770 | k4 = uint mul k 4 |
| 771 | n4 = uint mul n 4 |
| 772 | gkopa 0 x k4 4 0 0 |
| 773 | gkopa 1 w k4 4 0 0 |
| 774 | gkopc c n4 4 0 0 f32 1.0f64 |
| 775 | ggmm ggr n k 1 1 bt |
| 776 | end |
| 777 | |
| 778 | note G[kk][n] += X[R][kk]^T DY[R][n], split over R when the grid is small (ggks) |
| 779 | fn ggwg x ulong dy ulong g ulong kk ulong n ulong to void |
| 780 | k4 = uint mul kk 4 |
| 781 | n4 = uint mul n 4 |
| 782 | s = ggks kk n 1 |
| 783 | if eq s 1 |
| 784 | gkopa 0 x 4 k4 0 0 |
| 785 | gkopa 1 dy 4 n4 0 0 |
| 786 | gkopc g n4 4 0 0 f32 1.0f64 |
| 787 | ggmm kk n ggr 1 1 true |
| 788 | ret |
| 789 | end |
| 790 | sl = div ggr s |
| 791 | gkopa 0 x 4 k4 0 uint mul mul sl kk 4 |
| 792 | gkopa 1 dy 4 n4 0 uint mul mul sl n 4 |
| 793 | ggsk kk n 1 s g 0 |
| 794 | end |
| 795 | |
| 796 | note Split-K for the weight gradients, whose depth is R = B T. A grid of (m/32)(n/32)nz blocks |
| 797 | note leaves most SMs idle (rung-6 gprof: id 4 at 1.7-6 TFLOP/s on 64-256 blocks), so ggks doubles |
| 798 | note the split s (up to 16, R/s a multiple of 16) until the grid reaches 1024 blocks: then |
| 799 | note blocks s < 2048, so the m n nz s partials fit the 2^21 floats of slot 37. |
| 800 | note Under gkbf 1 a slice depth R/s must also be a multiple of the BF16 K tile 32 (gk.om gkbfok): |
| 801 | note with R = 128 (the small shape B 4, T 32) the f32 rule took s = 8, depth 16, which gkmm refuses |
| 802 | note with nverr -22, so gbcmp, gbcmp2 and gtrainb ended in ggdie (exit 84). At R = 16384 (B 64, T 256) |
| 803 | note every s <= 16 leaves a depth that is a multiple of 32: the split there is unchanged. |
| 804 | fn ggks m ulong n ulong nz ulong to ulong |
| 805 | r = ggr |
| 806 | if ggbf_ok |
| 807 | b128 = mul mul div m 128 div n 128 nz |
| 808 | if ge b128 96 |
| 809 | ret 1 |
| 810 | end |
| 811 | mnz = mul mul m n nz |
| 812 | s = ulong 1 |
| 813 | while lt mul b128 s 288 bound 8 |
| 814 | s2 = mul s 2 |
| 815 | if gt s2 16 |
| 816 | ret s |
| 817 | end |
| 818 | if ne rem r mul s2 32 0 |
| 819 | ret s |
| 820 | end |
| 821 | if gt mul mnz s2 4194304 |
| 822 | ret s |
| 823 | end |
| 824 | s = s2 |
| 825 | end |
| 826 | ret s |
| 827 | end |
| 828 | b = mul mul div m 32 div n 32 nz |
| 829 | kq = ulong 16 |
| 830 | if eq get gkbf 0 1 |
| 831 | kq = 32 |
| 832 | end |
| 833 | s = ulong 1 |
| 834 | while lt mul b s 1024 bound 8 |
| 835 | s2 = mul s 2 |
| 836 | if or gt s2 16 ne rem r mul s2 kq 0 |
| 837 | ret s |
| 838 | end |
| 839 | s = s2 |
| 840 | end |
| 841 | ret s |
| 842 | end |
| 843 | |
| 844 | note Operands A and B are set (gkopa) with the slice stride as s2. Batch z = z1 s + z2 (z1 < nz the |
| 845 | note caller's batch, z2 < s the slice) writes partial P[z1][z2] = A[z1 z2]^T B[z1 z2] into slot 37, |
| 846 | note then COLSUM 14 adds G[z1] (byte stride gz) += sum over z2 of P[z1][z2], in slice order. |
| 847 | note Mutant p (OMUTP 0) adds one slice fewer, which gcmp2 must catch. |
| 848 | const OMUTP ulong 1 |
| 849 | fn ggsk m ulong n ulong nz ulong s ulong g ulong gz uint to void |
| 850 | mn = mul m n |
| 851 | p = gga1 37 |
| 852 | n4 = uint mul n 4 |
| 853 | c1 = uint mul mul mn s 4 |
| 854 | c2 = uint mul mn 4 |
| 855 | gkopc p n4 4 c1 c2 f32 1.0f64 |
| 856 | k = div ggr s |
| 857 | z = mul nz s |
| 858 | ggmm m n k z s false |
| 859 | ggskr mn nz s g gz |
| 860 | end |
| 861 | |
| 862 | note COLSUM 14 over slot 37: G[c gz + i] += sum over z2 < s of P[c][z2][i], vectorized 256 thr x 4 floats |
| 863 | fn ggskr mn ulong nz ulong s ulong g ulong gz uint to void |
| 864 | kc = s |
| 865 | if eq OMUTP 0 |
| 866 | kc = sub s 1 |
| 867 | end |
| 868 | mn4 = div mn 4 |
| 869 | ggu 0 mn4 |
| 870 | ggu 1 kc |
| 871 | ggpt 1 gga1 37 |
| 872 | ggu 4 mul mn 4 |
| 873 | ggu 5 mul mul mn s 4 |
| 874 | ggpt 3 g |
| 875 | ggu 8 ulong gz |
| 876 | cid = uint 14 |
| 877 | if ggow_ok |
| 878 | cid = 13 |
| 879 | end |
| 880 | ggrun cid 9 ggcd mn4 256 nz 1 |
| 881 | end |
| 882 | |
| 883 | note 19 COPY (x into y), 20 ZERO (x) over n elements (256 threads x 4 floats via LDG.E.128/STG.E.128) |
| 884 | fn ggew id uint n ulong x ulong y ulong to void |
| 885 | n4 = div n 4 |
| 886 | ggu 0 n4 |
| 887 | ggpt 1 x |
| 888 | ggpt 2 y |
| 889 | ggrun id 6 ggcd n4 256 1 1 |
| 890 | end |
| 891 | |
| 892 | fn ggrmsf_h v ulong g ulong y ulong s ulong to void |
| 893 | r = mul ggr 32 |
| 894 | ggu 0 r |
| 895 | ggpt 1 v |
| 896 | ggpt 2 g |
| 897 | ggpt 3 y |
| 898 | ggpt 4 s |
| 899 | ggpt 5 0 |
| 900 | ggpt 6 0 |
| 901 | ggu 14 ggr |
| 902 | ggu 15 0 |
| 903 | ggrun 75 16 ggcd r 256 1 1 |
| 904 | end |
| 905 | |
| 906 | fn ggrmsf_h_nv4 v ulong g ulong y ulong s ulong to void |
| 907 | r = mul ggr 32 |
| 908 | ggu 0 r |
| 909 | ggpt 1 v |
| 910 | ggpt 2 g |
| 911 | ggpt 3 y |
| 912 | ggpt 4 s |
| 913 | ggpt 5 gga1 41 |
| 914 | ggpt 6 gga1 42 |
| 915 | ggu 14 ggr |
| 916 | ggu 15 1 |
| 917 | ggrun 75 16 ggcd r 256 1 1 |
| 918 | end |
| 919 | |
| 920 | global ggdgb ulong 2 |
| 921 | fn ggrms_g to ulong |
| 922 | g = div ggr 8 |
| 923 | if and le get og 2 512 gt g 144 |
| 924 | g = 144 |
| 925 | end |
| 926 | ret g |
| 927 | end |
| 928 | fn ggow_ok to bool |
| 929 | if not ggbf_ok |
| 930 | ret false |
| 931 | end |
| 932 | if ne get og 59 1 |
| 933 | ret false |
| 934 | end |
| 935 | ret eq ggfzn 0 |
| 936 | end |
| 937 | |
| 938 | fn ggsk_nv4 dy ulong g ulong kk ulong n ulong sb f64 sa f64 to void |
| 939 | s = ggks_nv4 kk n |
| 940 | ggqnv4_p sb |
| 941 | ggqnv4 dy gga1 43 gga1 44 n ggr true |
| 942 | ggqnv4_p 1.0f64 |
| 943 | al = div 1.0f64 mul sa sb |
| 944 | n4 = uint mul n 4 |
| 945 | if eq s 1 |
| 946 | fl = uint 1 |
| 947 | if ggow_ok |
| 948 | fl = 0 |
| 949 | end |
| 950 | ggmm4_ab gga1 41 gga1 42 gga1 43 gga1 44 kk n |
| 951 | gkopc g n4 4 0 0 f32 al |
| 952 | ggmm4_lch kk n ggr fl |
| 953 | ret |
| 954 | end |
| 955 | ks = div ggr s |
| 956 | mn = mul kk n |
| 957 | ggmm4_ab_z kk n ks |
| 958 | gkopc gga1 37 n4 4 0 uint mul mn 4 f32 al |
| 959 | ggmm4_lch_z kk n ks s 0 |
| 960 | ggskr mn 1 s g 0 |
| 961 | end |
| 962 | |
| 963 | fn ggwgh_bf x ulong dy ulong g ulong kk ulong n ulong to void |
| 964 | k2 = uint mul kk 2 |
| 965 | n2 = uint mul n 2 |
| 966 | n4 = uint mul n 4 |
| 967 | s = ggks kk n 1 |
| 968 | if eq s 1 |
| 969 | fl = uint 1 |
| 970 | if ggow_ok |
| 971 | fl = 0 |
| 972 | end |
| 973 | gkoph 0 x 2 k2 0 0 |
| 974 | gkoph 1 dy 2 n2 0 0 |
| 975 | gkopc g n4 4 0 0 f32 1.0f64 |
| 976 | ggmmh kk n ggr 1 1 fl |
| 977 | ret |
| 978 | end |
| 979 | sl = div ggr s |
| 980 | gkoph 0 x 2 k2 0 uint mul mul sl kk 2 |
| 981 | gkoph 1 dy 2 n2 0 uint mul mul sl n 2 |
| 982 | ggskh kk n 1 s g 0 |
| 983 | end |
| 984 | |
| 985 | fn ggwgh x ulong dy ulong g ulong kk ulong n ulong to void |
| 986 | sa = get ggn4s 0 |
| 987 | sb = get ggn4s 1 |
| 988 | if eq sa 0.0f64 |
| 989 | sa = 1.0f64 |
| 990 | end |
| 991 | if eq sb 0.0f64 |
| 992 | sb = 1.0f64 |
| 993 | end |
| 994 | put ggn4s 0 1.0f64 |
| 995 | put ggn4s 1 1.0f64 |
| 996 | if ggdw_nv4_ok kk n |
| 997 | ggqnv4_p sa |
| 998 | ggqnv4 x gga1 41 gga1 42 kk ggr true |
| 999 | ggsk_nv4 dy g kk n sb sa |
| 1000 | ret |
| 1001 | end |
| 1002 | ggwgh_bf x dy g kk n |
| 1003 | end |
| 1004 | fn ggain_cnt to ulong |
| 1005 | if and ggbf_ok le ggrms_g 256 |
| 1006 | ret 1 |
| 1007 | end |
| 1008 | ret 2 |
| 1009 | end |
| 1010 | fn ggdef_gain to bool |
| 1011 | if not ggbf_ok |
| 1012 | ret false |
| 1013 | end |
| 1014 | if ne get og 59 1 |
| 1015 | ret false |
| 1016 | end |
| 1017 | gx = ggrms_g |
| 1018 | if gt gx 256 |
| 1019 | ret false |
| 1020 | end |
| 1021 | nl = sub get og 56 ggfzl |
| 1022 | if or eq nl 0 gt nl 16 |
| 1023 | ret false |
| 1024 | end |
| 1025 | ret le mul mul 2 nl gx ggr |
| 1026 | end |
| 1027 | fn ggrmsb_h_ex v ulong g ulong s ulong ws ulong owdh ulong to void |
| 1028 | r = mul ggr 32 |
| 1029 | gx = ggrms_g |
| 1030 | ggu 0 r |
| 1031 | ggpt 1 v |
| 1032 | ggpt 2 g |
| 1033 | ggpt 3 s |
| 1034 | ggpt 4 gga1 14 |
| 1035 | ggpt 5 gga1 13 |
| 1036 | ggpt 6 ws |
| 1037 | ggu 14 owdh |
| 1038 | ggu 15 mul gx 256 |
| 1039 | ggrun 76 16 gx 1 1 |
| 1040 | end |
| 1041 | fn ggrmsb_h v ulong g ulong s ulong to void |
| 1042 | ggrmsb_h_ex v g s gga1 19 0 |
| 1043 | end |
| 1044 | fn ggws_off idx ulong to ulong |
| 1045 | if eq get ggdgb 1 0 |
| 1046 | ret gga1 19 |
| 1047 | end |
| 1048 | ret add gga1 19 mul mul idx ggrms_g mul get og 2 4 |
| 1049 | end |
| 1050 | fn ggrmsb_gain l ulong ffn bool to void |
| 1051 | idx = sub l get ggdgb 0 |
| 1052 | if not ffn |
| 1053 | idx = add get ggdgb 1 idx |
| 1054 | end |
| 1055 | ws = ggws_off idx |
| 1056 | v = ggl l 0 |
| 1057 | go = ovg1 l |
| 1058 | s = ggl l 8 |
| 1059 | if ffn |
| 1060 | v = ggl l 6 |
| 1061 | go = ovg2 l |
| 1062 | s = ggl l 9 |
| 1063 | end |
| 1064 | g = ggw go |
| 1065 | ggrmsb_h_ex v g s ws 0 |
| 1066 | if eq get ggdgb 1 0 |
| 1067 | ggain_h go |
| 1068 | end |
| 1069 | end |
| 1070 | |
| 1071 | note RMSF/RMSB run one warp per row: arg 0 and the thread count are R*32 |
| 1072 | fn ggrmsf v ulong g ulong y ulong s ulong to void |
| 1073 | r = mul ggr 32 |
| 1074 | ggu 0 r |
| 1075 | ggpt 1 v |
| 1076 | ggpt 2 g |
| 1077 | ggpt 3 y |
| 1078 | ggpt 4 s |
| 1079 | ggrun 10 10 ggcd r 64 1 1 |
| 1080 | end |
| 1081 | |
| 1082 | note dh += RMS backward of da; the gain terms go to WS |
| 1083 | fn ggrmsb v ulong g ulong s ulong to void |
| 1084 | r = mul ggr 32 |
| 1085 | ggu 0 r |
| 1086 | ggpt 1 v |
| 1087 | ggpt 2 g |
| 1088 | ggpt 3 s |
| 1089 | ggpt 4 gga1 14 |
| 1090 | ggpt 5 gga1 13 |
| 1091 | ggpt 6 gga1 19 |
| 1092 | ggrun 11 14 ggcd r 64 1 1 |
| 1093 | end |
| 1094 | |
| 1095 | note gain gradient at parameter offset go += column sums of WS (grid-stride <=144xD or 8-warp CTA-reduced R/8) |
| 1096 | fn ggain_h go ulong to void |
| 1097 | d = get og 2 |
| 1098 | d4 = div d 4 |
| 1099 | gx = ggrms_g |
| 1100 | cid = uint 14 |
| 1101 | if ggow_ok |
| 1102 | cid = 13 |
| 1103 | end |
| 1104 | if le gx 256 |
| 1105 | ggu 0 d4 |
| 1106 | ggu 1 gx |
| 1107 | ggpt 1 gga1 19 |
| 1108 | ggu 4 mul d 4 |
| 1109 | ggu 5 0 |
| 1110 | ggpt 3 ggg go |
| 1111 | ggu 8 0 |
| 1112 | ggrun cid 9 ggcd d4 256 1 1 |
| 1113 | ret |
| 1114 | end |
| 1115 | tt8 = div get og 1 8 |
| 1116 | bb = get og 6 |
| 1117 | ggu 0 d4 |
| 1118 | ggu 1 tt8 |
| 1119 | ggpt 1 gga1 19 |
| 1120 | ggu 4 mul d 4 |
| 1121 | ggu 5 mul mul tt8 d 4 |
| 1122 | ggpt 3 gga1 20 |
| 1123 | ggu 8 mul d 4 |
| 1124 | ggrun 13 9 ggcd d4 256 bb 1 |
| 1125 | ggu 0 d4 |
| 1126 | ggu 1 bb |
| 1127 | ggpt 1 gga1 20 |
| 1128 | ggu 4 mul d 4 |
| 1129 | ggu 5 0 |
| 1130 | ggpt 3 ggg go |
| 1131 | ggu 8 0 |
| 1132 | ggrun cid 9 ggcd d4 256 1 1 |
| 1133 | end |
| 1134 | fn ggain_def lo ulong nl ulong to void |
| 1135 | d = get og 2 |
| 1136 | d4 = div d 4 |
| 1137 | gx = ggrms_g |
| 1138 | cid = uint 14 |
| 1139 | if ggow_ok |
| 1140 | cid = 13 |
| 1141 | end |
| 1142 | gd4 = mul mul gx d 4 |
| 1143 | l4 = mul get og 32 4 |
| 1144 | ggu 0 d4 |
| 1145 | ggu 1 gx |
| 1146 | ggpt 1 gga1 19 |
| 1147 | ggu 4 mul d 4 |
| 1148 | ggu 5 gd4 |
| 1149 | ggpt 3 ggg ovg2 lo |
| 1150 | ggu 8 l4 |
| 1151 | ggrun cid 9 ggcd d4 256 nl 1 |
| 1152 | ggu 0 d4 |
| 1153 | ggu 1 gx |
| 1154 | ggpt 1 add gga1 19 mul nl gd4 |
| 1155 | ggu 4 mul d 4 |
| 1156 | ggu 5 gd4 |
| 1157 | ggpt 3 ggg ovg1 lo |
| 1158 | ggu 8 l4 |
| 1159 | ggrun cid 9 ggcd d4 256 nl 1 |
| 1160 | end |
| 1161 | |
| 1162 | note gain gradient at parameter offset go += column sums of WS: vectorized 256 thr x 4 floats |
| 1163 | fn ggain go ulong to void |
| 1164 | d = get og 2 |
| 1165 | d4 = div d 4 |
| 1166 | tt = get og 1 |
| 1167 | bb = get og 6 |
| 1168 | ggu 0 d4 |
| 1169 | ggu 1 tt |
| 1170 | ggpt 1 gga1 19 |
| 1171 | ggu 4 mul d 4 |
| 1172 | ggu 5 mul mul tt d 4 |
| 1173 | ggpt 3 gga1 20 |
| 1174 | ggu 8 mul d 4 |
| 1175 | ggrun 13 9 ggcd d4 256 bb 1 |
| 1176 | ggu 0 d4 |
| 1177 | ggu 1 bb |
| 1178 | ggpt 1 gga1 20 |
| 1179 | ggu 4 mul d 4 |
| 1180 | ggu 5 0 |
| 1181 | ggpt 3 ggg go |
| 1182 | ggu 8 0 |
| 1183 | ggrun 14 9 ggcd d4 256 1 1 |
| 1184 | end |
| 1185 | |
| 1186 | note softmax rows: one warp per row, T 32 threads per score block, grid (T/2, B H) |
| 1187 | fn ggsoft id uint p ulong dp ulong to void |
| 1188 | tt = get og 1 |
| 1189 | ggu 0 mul tt 32 |
| 1190 | ggpt 1 p |
| 1191 | ggpt 2 dp |
| 1192 | ggrun id 6 div tt 2 mul get og 6 get og 3 1 |
| 1193 | end |
| 1194 | |
| 1195 | note attention GEMM over the B H (sequence, head) batch; operands set by gghd/ggpl/ggch/ggcp first; |
| 1196 | note cz: gkopz causal mode (1 A = P or DS, 2 A = P^T or DS^T, 3 C = S or DP) |
| 1197 | fn ggbh m ulong n ulong k ulong bt bool cz uint to void |
| 1198 | gkopz cz |
| 1199 | ggmm m n k mul get og 6 get og 3 get og 3 bt |
| 1200 | end |
| 1201 | |
| 1202 | note head-layout operand (rows of E inside D-wide rows, batch b then head); tr: k runs along rows |
| 1203 | fn gghd ab uint p ulong tr bool to void |
| 1204 | s0 = get ggs 0 |
| 1205 | s1 = get ggs 1 |
| 1206 | s2 = get ggs 2 |
| 1207 | if tr |
| 1208 | gkopa ab p 4 s0 s1 s2 |
| 1209 | else |
| 1210 | gkopa ab p s0 4 s1 s2 |
| 1211 | end |
| 1212 | end |
| 1213 | |
| 1214 | note T x T score operand (batch b then head); tr: transposed |
| 1215 | fn ggpl ab uint p ulong tr bool to void |
| 1216 | s3 = get ggs 3 |
| 1217 | s4 = get ggs 4 |
| 1218 | s5 = get ggs 5 |
| 1219 | if tr |
| 1220 | gkopa ab p 4 s3 s5 s4 |
| 1221 | else |
| 1222 | gkopa ab p s3 4 s5 s4 |
| 1223 | end |
| 1224 | end |
| 1225 | |
| 1226 | fn ggch c ulong al f64 to void |
| 1227 | s0 = get ggs 0 |
| 1228 | s1 = get ggs 1 |
| 1229 | s2 = get ggs 2 |
| 1230 | gkopc c s0 4 s1 s2 f32 al |
| 1231 | end |
| 1232 | |
| 1233 | fn ggcp c ulong al f64 to void |
| 1234 | s3 = get ggs 3 |
| 1235 | s4 = get ggs 4 |
| 1236 | s5 = get ggs 5 |
| 1237 | gkopc c s3 4 s5 s4 f32 al |
| 1238 | end |
| 1239 | |
| 1240 | note stride table for the dims in og (bytes) |
| 1241 | fn ggstr to void |
| 1242 | d = get og 2 |
| 1243 | tt = get og 1 |
| 1244 | put ggs 0 uint mul d 4 |
| 1245 | put ggs 1 uint mul tt mul d 4 |
| 1246 | put ggs 2 uint mul get og 7 4 |
| 1247 | put ggs 3 uint mul tt 4 |
| 1248 | put ggs 4 uint mul tt mul tt 4 |
| 1249 | put ggs 5 uint mul get og 3 mul tt mul tt 4 |
| 1250 | put ggs 6 uint mul ggr mul d 4 |
| 1251 | put ggs 7 uint mul d mul d 4 |
| 1252 | end |
| 1253 | |
| 1254 | fn ggqkv_nv4 l ulong to void |
| 1255 | d = get og 2 |
| 1256 | lp = olp l |
| 1257 | if ne get ggw4_ok 0 0 |
| 1258 | for p ulong 3 |
| 1259 | ggmm4_ab gga1 41 gga1 42 ggw4_dt lp p true ggw4_sc lp p true ggr d |
| 1260 | gkopc add ggl l 2 mul p ulong get ggs 6 uint mul d 2 2 0 0 f32 0.0625f64 |
| 1261 | ggmm4_lch ggr d d 2 |
| 1262 | end |
| 1263 | ret |
| 1264 | end |
| 1265 | ggqnv4_p 1.0f64 |
| 1266 | ggqnv4 ggl l 1 gga1 41 gga1 42 ggr d false |
| 1267 | dd2 = mul mul d d 2 |
| 1268 | w0 = ggwh ovwq l |
| 1269 | ggqnv4_p 16.0f64 |
| 1270 | for p ulong 3 |
| 1271 | ggqnv4 add w0 mul p dd2 gga1 43 gga1 44 d d true |
| 1272 | ggmm4_ab gga1 41 gga1 42 gga1 43 gga1 44 ggr d |
| 1273 | gkopc add ggl l 2 mul p ulong get ggs 6 uint mul d 2 2 0 0 f32 0.0625f64 |
| 1274 | ggmm4_lch ggr d d 2 |
| 1275 | end |
| 1276 | ggqnv4_p 1.0f64 |
| 1277 | end |
| 1278 | |
| 1279 | note q, k, u = a wq, a wk, a wv as one batched GEMM (wq wk wv and q k u are contiguous) |
| 1280 | fn ggqkv l ulong to void |
| 1281 | d = get og 2 |
| 1282 | a = ggl l 1 |
| 1283 | c = ggl l 2 |
| 1284 | if and ggnv4_ok ggnv4_shape ggr d d |
| 1285 | ggqkv_nv4 l |
| 1286 | ret |
| 1287 | end |
| 1288 | if ggbf_ok |
| 1289 | d2 = uint mul d 2 |
| 1290 | s6 = get ggs 6 |
| 1291 | w2 = ggwh ovwq l |
| 1292 | gkoph 0 a d2 2 0 0 |
| 1293 | gkoph 1 w2 2 d2 0 uint mul mul d d 2 |
| 1294 | gkopc c d2 2 0 s6 f32 1.0f64 |
| 1295 | ggmmh ggr d d 3 3 2 |
| 1296 | ret |
| 1297 | end |
| 1298 | w = ggw ovwq l |
| 1299 | s0 = get ggs 0 |
| 1300 | s6 = get ggs 6 |
| 1301 | s7 = get ggs 7 |
| 1302 | gkopa 0 a s0 4 0 0 |
| 1303 | gkopa 1 w 4 s0 0 s7 |
| 1304 | gkopc c s0 4 0 s6 f32 1.0f64 |
| 1305 | ggmm ggr d d 3 3 false |
| 1306 | end |
| 1307 | |
| 1308 | note true when SM121 HMMA.16816 Fused Flash-Attention-2 (slots 63..66) is active for this shape |
| 1309 | fn ggfa_ok to bool |
| 1310 | if or eq get gkbf 0 0 ne get gkbck 0 0 |
| 1311 | ret false |
| 1312 | end |
| 1313 | if eq OMUTG 0 |
| 1314 | ret false |
| 1315 | end |
| 1316 | tt = get og 1 |
| 1317 | if or lt tt 64 ne rem tt 64 0 |
| 1318 | ret false |
| 1319 | end |
| 1320 | e = get og 7 |
| 1321 | ret or eq e 32 eq e 64 |
| 1322 | end |
| 1323 | note true when 128x128 fast epilogues (residual-fused beta + single-store ReLU) are active in ggffn/ggbffn |
| 1324 | note ggnofp[0] = 1 during gtrainf/gtrainb/gprof (skips duplicate FP store; gcmp2/gbcmp2 keeps FP for ogmask rho check) |
| 1325 | global ggnofp uint 1 |
| 1326 | fn ggsnofp v uint to void |
| 1327 | put ggnofp 0 v |
| 1328 | end |
| 1329 | fn ggfast_ffn to bool |
| 1330 | if or eq get gkbf 0 0 ne get gkbck 0 0 |
| 1331 | ret false |
| 1332 | end |
| 1333 | if or ne rem ggr 128 0 ne rem get og 2 128 0 |
| 1334 | ret false |
| 1335 | end |
| 1336 | ret eq rem get og 4 128 0 |
| 1337 | end |
| 1338 | fn ggskip_fp to bool |
| 1339 | ret and ggfast_ffn ne get ggnofp 0 0 |
| 1340 | end |
| 1341 | note true when full Packed-BF16 Native-IO training mode (slots 67..83, 2 B/elem activations + W_bf16) is active |
| 1342 | fn ggbf_ok to bool |
| 1343 | if not ggskip_fp |
| 1344 | ret false |
| 1345 | end |
| 1346 | ret ggfa_ok |
| 1347 | end |
| 1348 | note P = softmax(q k^T / sqrt(E)), o = P u; uses SM121 HMMA.16816 Flash-Attention-2 (FA2F slot 63) when ggfa_ok |
| 1349 | fn ggatt l ulong to void |
| 1350 | tt = get og 1 |
| 1351 | e = get og 7 |
| 1352 | pp = ggl l 12 |
| 1353 | q = ggl l 2 |
| 1354 | k = ggl l 3 |
| 1355 | u = ggl l 4 |
| 1356 | o = ggl l 5 |
| 1357 | if ggfa_ok |
| 1358 | ggpt 0 q |
| 1359 | ggpt 1 k |
| 1360 | ggpt 2 u |
| 1361 | ggpt 3 o |
| 1362 | ggpt 4 pp |
| 1363 | fid = uint 63 |
| 1364 | if ggbf_ok |
| 1365 | fid = 77 |
| 1366 | end |
| 1367 | ggrun fid 10 div tt 64 get og 3 get og 6 |
| 1368 | ret |
| 1369 | end |
| 1370 | gghd 0 q false |
| 1371 | gghd 1 k false |
| 1372 | isq = div 1.0f64 fsqrt f64 e |
| 1373 | ggcp pp isq |
| 1374 | ggbh tt tt e false 3 |
| 1375 | ggsoft 15 pp pp |
| 1376 | ggpl 0 pp false |
| 1377 | gghd 1 u true |
| 1378 | ggch o 1.0f64 |
| 1379 | ggbh tt e tt false 1 |
| 1380 | end |
| 1381 | |
| 1382 | note hm = hin + o wo; bn = rms(hm) g2; out = hm + relu(bn w1) w2 |
| 1383 | fn ggffn_h l ulong to void |
| 1384 | d = get og 2 |
| 1385 | f = get og 4 |
| 1386 | hm = ggl l 6 |
| 1387 | lp = olp l |
| 1388 | cw = and ne get ggw4_ok 0 0 and ggnv4_ok and ggnv4_shape ggr d d ggnv4_shape ggr f d |
| 1389 | gkopfl 8 ggl l 0 |
| 1390 | if and cw ge d 512 |
| 1391 | ggmm4_pt ggl l 5 0 hm |
| 1392 | ggmm4_sc 1.0f64 1.0f64 |
| 1393 | ggmm4_cw lp 3 true d d 3 |
| 1394 | else |
| 1395 | ggxwh_bf ggl l 5 ggwh ovwo l hm d d true |
| 1396 | end |
| 1397 | if cw |
| 1398 | ggrmsf_h_nv4 hm ggw ovg2 l ggl l 7 ggl l 9 |
| 1399 | gkope 1 ggl l 11 |
| 1400 | put ggn4ep 0 1065353216 |
| 1401 | ggmm4_pt ggl l 7 0 ggl l 10 |
| 1402 | ggmm4_sc 1.0f64 1.0f64 |
| 1403 | ggmm4_cwp lp 4 true f d 2 |
| 1404 | else |
| 1405 | ggrmsf_h hm ggw ovg2 l ggl l 7 ggl l 9 |
| 1406 | gkope 1 ggl l 11 |
| 1407 | ggmm4_sc 1.0f64 16.0f64 |
| 1408 | ggxwh ggl l 7 ggwh ovw1 l ggl l 10 f d false |
| 1409 | end |
| 1410 | gkopfl 8 hm |
| 1411 | if cw |
| 1412 | ggmm4_pt ggl l 11 0 ggout l |
| 1413 | ggmm4_sc 1.0f64 1.0f64 |
| 1414 | ggmm4_cwp2 lp 5 true d f 3 |
| 1415 | else |
| 1416 | ggmm4_sc 1.0f64 16.0f64 |
| 1417 | ggxwh ggl l 11 ggwh ovw2 l ggout l d f true |
| 1418 | end |
| 1419 | end |
| 1420 | |
| 1421 | fn ggffn l ulong to void |
| 1422 | if ggbf_ok |
| 1423 | ggffn_h l |
| 1424 | ret |
| 1425 | end |
| 1426 | d = get og 2 |
| 1427 | f = get og 4 |
| 1428 | rd = mul ggr d |
| 1429 | hm = ggl l 6 |
| 1430 | x = ggl l 0 |
| 1431 | ff = ggfast_ffn |
| 1432 | if not ff |
| 1433 | ggew 19 rd x hm |
| 1434 | end |
| 1435 | x5 = ggl l 5 |
| 1436 | w = ggw ovwo l |
| 1437 | if ff |
| 1438 | gkopfl 8 x |
| 1439 | end |
| 1440 | ggxw x5 w hm d d true |
| 1441 | x7 = ggl l 7 |
| 1442 | w = ggw ovg2 l |
| 1443 | s = ggl l 9 |
| 1444 | ggrmsf hm w x7 s |
| 1445 | fp = ggl l 10 |
| 1446 | w = ggw ovw1 l |
| 1447 | y = ggl l 11 |
| 1448 | gkope 1 y |
| 1449 | if ggskip_fp |
| 1450 | gkopfl 4 0 |
| 1451 | end |
| 1452 | ggxw x7 w fp f d false |
| 1453 | o = ggout l |
| 1454 | if not ff |
| 1455 | ggew 19 rd hm o |
| 1456 | end |
| 1457 | w = ggw ovw2 l |
| 1458 | if ff |
| 1459 | gkopfl 8 hm |
| 1460 | end |
| 1461 | ggxw y w o d f true |
| 1462 | end |
| 1463 | |
| 1464 | fn ggfl l ulong to void |
| 1465 | x = ggl l 0 |
| 1466 | w = ggw ovg1 l |
| 1467 | a = ggl l 1 |
| 1468 | s = ggl l 8 |
| 1469 | if ggbf_ok |
| 1470 | if and ne get ggw4_ok 0 0 and ggnv4_ok ggnv4_shape ggr get og 2 get og 2 |
| 1471 | ggrmsf_h_nv4 x w a s |
| 1472 | else |
| 1473 | ggrmsf_h x w a s |
| 1474 | end |
| 1475 | else |
| 1476 | ggrmsf x w a s |
| 1477 | end |
| 1478 | ggqkv l |
| 1479 | ggatt l |
| 1480 | ggffn l |
| 1481 | end |
| 1482 | |
| 1483 | note ggnoce 1 (oglast): stop after logits GEMM. ggnoce 2 (gfcache): stop right after the LV layers |
| 1484 | note (leaving final hidden state HF in gga1 8, skipping final RMS, logits and CE). |
| 1485 | global ggnoce uint 1 |
| 1486 | fn ggb_ok to bool |
| 1487 | ret and and eq get gkbck 0 0 eq get gkpf 0 0 and eq get gkbat 0 0 gt get gbpkt 0 0 |
| 1488 | end |
| 1489 | |
| 1490 | fn ggquant_w1 lp ulong to void |
| 1491 | d = get og 2 |
| 1492 | f = get og 4 |
| 1493 | w0 = ggwh opwq lp |
| 1494 | dd2 = mul mul d d 2 |
| 1495 | for p ulong 3 |
| 1496 | w = add w0 mul p dd2 |
| 1497 | ggqnv4 w ggw4_dt lp p true ggw4_sc lp p true d d true |
| 1498 | end |
| 1499 | if ge d 512 |
| 1500 | wo = add w0 mul 3 dd2 |
| 1501 | ggqnv4 wo ggw4_dt lp 3 true ggw4_sc lp 3 true d d true |
| 1502 | ggqnv4 wo ggw4_dt lp 3 false ggw4_sc lp 3 false d d false |
| 1503 | end |
| 1504 | w1 = ggwh opw1 lp |
| 1505 | ggqnv4 w1 ggw4_dt lp 4 true ggw4_sc lp 4 true f d true |
| 1506 | ggqnv4 w1 ggw4_dt lp 4 false ggw4_sc lp 4 false d f false |
| 1507 | w2 = ggwh opw2 lp |
| 1508 | ggqnv4 w2 ggw4_dt lp 5 true ggw4_sc lp 5 true d f true |
| 1509 | ggqnv4 w2 ggw4_dt lp 5 false ggw4_sc lp 5 false f d false |
| 1510 | end |
| 1511 | |
| 1512 | fn ggquant_w to void |
| 1513 | put ggw4_ok 0 0 |
| 1514 | if not ggnv4_ok |
| 1515 | ret |
| 1516 | end |
| 1517 | d = get og 2 |
| 1518 | f = get og 4 |
| 1519 | if or not ggnv4_shape d d d not ggnv4_shape f d d |
| 1520 | ret |
| 1521 | end |
| 1522 | ggqnv4_p 16.0f64 |
| 1523 | nl = get og 5 |
| 1524 | per = ulong 7 |
| 1525 | if ge d 512 |
| 1526 | per = 9 |
| 1527 | end |
| 1528 | if and ggb_ok le mul nl per 512 |
| 1529 | if not gbbegin mul nl per |
| 1530 | ggdie 120 |
| 1531 | end |
| 1532 | put gkbat 0 1 |
| 1533 | for lp ulong nl bound 64 |
| 1534 | ggquant_w1 lp |
| 1535 | end |
| 1536 | put gkbat 0 0 |
| 1537 | if not gkbsub |
| 1538 | ggdie 121 |
| 1539 | end |
| 1540 | else |
| 1541 | for lp ulong nl bound 64 |
| 1542 | ggquant_w1 lp |
| 1543 | end |
| 1544 | end |
| 1545 | ggqnv4_p 1.0f64 |
| 1546 | put ggw4_ok 0 1 |
| 1547 | end |
| 1548 | fn ggfwd_emb to void |
| 1549 | d = get og 2 |
| 1550 | x = ggl 0 0 |
| 1551 | if ggbf_ok |
| 1552 | x = gga1 13 |
| 1553 | end |
| 1554 | ggu 0 d |
| 1555 | ggpt 1 x |
| 1556 | x = ggw 0 |
| 1557 | ggpt 2 x |
| 1558 | x = ggw get og 30 |
| 1559 | ggpt 3 x |
| 1560 | x = gga1 2 |
| 1561 | ggpt 4 x |
| 1562 | tt = get og 1 |
| 1563 | bb = get og 6 |
| 1564 | ggrun 9 10 ggcd d 64 tt bb |
| 1565 | if ggbf_ok |
| 1566 | ggf2b mul ggr d gga1 13 ggl 0 0 |
| 1567 | end |
| 1568 | end |
| 1569 | fn ggfwd_head to void |
| 1570 | if eq get ggnoce 0 2 |
| 1571 | ret |
| 1572 | end |
| 1573 | d = get og 2 |
| 1574 | v = get og 0 |
| 1575 | if ggbf_ok |
| 1576 | ggrmsf_h gga1 8 ggw get og 33 gga1 10 gga1 9 |
| 1577 | d2 = uint mul d 2 |
| 1578 | v2 = uint mul v 2 |
| 1579 | gkoph 0 gga1 10 d2 2 0 0 |
| 1580 | gkoph 1 ggwh 0 d2 2 0 0 |
| 1581 | gkopc gga1 11 v2 2 0 0 f32 1.0f64 |
| 1582 | ggmmh ggr v d 1 1 2 |
| 1583 | if ne get ggnoce 0 0 |
| 1584 | ret |
| 1585 | end |
| 1586 | r = mul ggr 32 |
| 1587 | ggu 0 r |
| 1588 | ggpt 1 gga1 11 |
| 1589 | ggpt 2 gga1 3 |
| 1590 | ggpt 3 gga1 12 |
| 1591 | ggpt 4 gga1 39 |
| 1592 | ggrun 87 10 ggcd r 256 1 1 |
| 1593 | ret |
| 1594 | end |
| 1595 | x = gga1 8 |
| 1596 | w = ggw get og 33 |
| 1597 | z = gga1 10 |
| 1598 | s = gga1 9 |
| 1599 | ggrmsf x w z s |
| 1600 | x = gga1 11 |
| 1601 | w = ggw 0 |
| 1602 | ggxwt z w x v d false |
| 1603 | if ne get ggnoce 0 0 |
| 1604 | ret |
| 1605 | end |
| 1606 | r = mul ggr 32 |
| 1607 | ggu 0 r |
| 1608 | ggpt 1 x |
| 1609 | x = gga1 3 |
| 1610 | ggpt 2 x |
| 1611 | x = gga1 12 |
| 1612 | ggpt 3 x |
| 1613 | ggrun 21 8 ggcd r 64 1 1 |
| 1614 | end |
| 1615 | fn ggfwd_hcnt to ulong |
| 1616 | nc = get ggnoce 0 |
| 1617 | if eq nc 2 |
| 1618 | ret 0 |
| 1619 | end |
| 1620 | if eq nc 1 |
| 1621 | ret 2 |
| 1622 | end |
| 1623 | ret 3 |
| 1624 | end |
| 1625 | fn ggfwd_ecnt to ulong |
| 1626 | if ggbf_ok |
| 1627 | ret 2 |
| 1628 | end |
| 1629 | ret 1 |
| 1630 | end |
| 1631 | fn ggfwd_lcnt to ulong |
| 1632 | c = ulong 11 |
| 1633 | if ggfa_ok |
| 1634 | c = sub c 2 |
| 1635 | end |
| 1636 | if ggfast_ffn |
| 1637 | c = sub c 2 |
| 1638 | end |
| 1639 | if ggnv4_ok |
| 1640 | d = get og 2 |
| 1641 | f = get og 4 |
| 1642 | r = ggr |
| 1643 | cw = and ne get ggw4_ok 0 0 and ggnv4_shape r d d ggnv4_shape r f d |
| 1644 | if cw |
| 1645 | note QKV: 3 GEMMs vs 1 BF16 (+2, 0 quant!); W1: 0 quant; W2: 0 quant (fused into slot 94!); O: +1 quant iff d>=512 |
| 1646 | c = add c 2 |
| 1647 | if ge d 512 |
| 1648 | c = add c 1 |
| 1649 | end |
| 1650 | else |
| 1651 | if ggnv4_shape r d d |
| 1652 | c = add c 6 |
| 1653 | end |
| 1654 | if ggnv4_shape r f d |
| 1655 | c = add c 2 |
| 1656 | end |
| 1657 | if ggnv4_shape r d f |
| 1658 | c = add c 2 |
| 1659 | end |
| 1660 | end |
| 1661 | end |
| 1662 | ret c |
| 1663 | end |
| 1664 | fn ggfwd_bat to void |
| 1665 | lv = get og 56 |
| 1666 | lc = ggfwd_lcnt |
| 1667 | l0 = ulong 0 |
| 1668 | while lt l0 lv bound 1024 |
| 1669 | l1 = add l0 16 |
| 1670 | if gt l1 lv |
| 1671 | l1 = lv |
| 1672 | end |
| 1673 | cnt = mul sub l1 l0 lc |
| 1674 | if eq l0 0 |
| 1675 | cnt = add cnt ggfwd_ecnt |
| 1676 | end |
| 1677 | if eq l1 lv |
| 1678 | cnt = add cnt ggfwd_hcnt |
| 1679 | end |
| 1680 | if not gbbegin cnt |
| 1681 | ggdie 120 |
| 1682 | end |
| 1683 | put gkbat 0 1 |
| 1684 | if eq l0 0 |
| 1685 | ggfwd_emb |
| 1686 | end |
| 1687 | l = l0 |
| 1688 | while lt l l1 bound 32 |
| 1689 | ggfl l |
| 1690 | l = add l 1 |
| 1691 | end |
| 1692 | if eq l1 lv |
| 1693 | ggfwd_head |
| 1694 | end |
| 1695 | put gkbat 0 0 |
| 1696 | if not gkbsub |
| 1697 | ggdie 121 |
| 1698 | end |
| 1699 | l0 = l1 |
| 1700 | end |
| 1701 | end |
| 1702 | fn ggfwd to void |
| 1703 | if ggb_ok |
| 1704 | ggfwd_bat |
| 1705 | ret |
| 1706 | end |
| 1707 | ggfwd_emb |
| 1708 | for l ulong get og 56 bound 9223372036854775807 |
| 1709 | ggfl l |
| 1710 | end |
| 1711 | ggfwd_head |
| 1712 | end |
| 1713 | |
| 1714 | fn ggbffn_h l ulong to void |
| 1715 | d = get og 2 |
| 1716 | f = get og 4 |
| 1717 | lp = olp l |
| 1718 | gs = gggrad_sc |
| 1719 | cw = and ne get ggw4_ok 0 0 and ggnv4_ok and ggnv4_shape ggr f d ggnv4_shape ggr d f |
| 1720 | gkope 2 ggl l 11 |
| 1721 | if cw |
| 1722 | put ggn4ep 0 bitcast uint f32 gs |
| 1723 | ggmm4_pt gga1 13 0 gga1 17 |
| 1724 | ggmm4_sc gs 1.0f64 |
| 1725 | ggmm4_cw lp 5 false f d 2 |
| 1726 | ggmm4_pt gga1 17 0 gga1 14 |
| 1727 | ggmm4_sc gs 1.0f64 |
| 1728 | ggmm4_cwp2 lp 4 false d f 2 |
| 1729 | else |
| 1730 | ggmm4_sc gs 16.0f64 |
| 1731 | ggxwth gga1 13 ggwh ovw2 l gga1 17 f d false |
| 1732 | end |
| 1733 | if or eq OMUTLG 1 eq lp l |
| 1734 | ggmm4_sc 1.0f64 gs |
| 1735 | ggwgh ggl l 11 gga1 13 ggg ovw2 l f d |
| 1736 | end |
| 1737 | ggmm4_sc 1.0f64 gs |
| 1738 | ggwgh ggl l 7 gga1 17 ggg ovw1 l d f |
| 1739 | if not cw |
| 1740 | ggmm4_sc gs 16.0f64 |
| 1741 | ggxwth gga1 17 ggwh ovw1 l gga1 14 d f false |
| 1742 | end |
| 1743 | ggrmsb_gain l true |
| 1744 | end |
| 1745 | |
| 1746 | note layer l FFN backward: DH holds d out on entry, d hm on exit. The dF GEMM's fused epilogue (gkope 2) |
| 1747 | note applies the ReLU mask FP > 0; the forward W1 GEMM (ggffn, gkope 1) stores both FP and Y. |
| 1748 | fn ggbffn l ulong to void |
| 1749 | if ggbf_ok |
| 1750 | ggbffn_h l |
| 1751 | ret |
| 1752 | end |
| 1753 | d = get og 2 |
| 1754 | f = get og 4 |
| 1755 | dh = gga1 13 |
| 1756 | df = gga1 17 |
| 1757 | da = gga1 14 |
| 1758 | w = ggw ovw2 l |
| 1759 | x = ggl l 10 |
| 1760 | if ggskip_fp |
| 1761 | x = ggl l 11 |
| 1762 | end |
| 1763 | gkope 2 x |
| 1764 | ggxwt dh w df f d false |
| 1765 | x = ggl l 11 |
| 1766 | w = ggg ovw2 l |
| 1767 | if or eq OMUTLG 1 eq olp l l |
| 1768 | ggwg x dh w f d |
| 1769 | end |
| 1770 | x = ggl l 7 |
| 1771 | w = ggg ovw1 l |
| 1772 | ggwg x df w d f |
| 1773 | w = ggw ovw1 l |
| 1774 | ggxwt df w da d f false |
| 1775 | x = ggl l 6 |
| 1776 | w = ggw ovg2 l |
| 1777 | s = ggl l 9 |
| 1778 | ggrmsb x w s |
| 1779 | ggain ovg2 l |
| 1780 | end |
| 1781 | |
| 1782 | fn ggfa_bwd l ulong to void |
| 1783 | tt = get og 1 |
| 1784 | h = get og 3 |
| 1785 | bb = get og 6 |
| 1786 | dn = gga1 16 |
| 1787 | pp = ggl l 12 |
| 1788 | dp = gga1 18 |
| 1789 | dq = gga1 15 |
| 1790 | dk = add dq ulong get ggs 6 |
| 1791 | du = add dk ulong get ggs 6 |
| 1792 | id_d = uint 64 |
| 1793 | id_q = uint 65 |
| 1794 | id_kv = uint 66 |
| 1795 | if ggbf_ok |
| 1796 | id_d = 78 |
| 1797 | id_q = 79 |
| 1798 | id_kv = 80 |
| 1799 | end |
| 1800 | ggpt 0 ggl l 5 |
| 1801 | ggpt 1 dn |
| 1802 | ggpt 2 dp |
| 1803 | ggrun id_d 6 div tt 2 h bb |
| 1804 | ggpt 0 ggl l 2 |
| 1805 | ggpt 1 ggl l 3 |
| 1806 | ggpt 2 ggl l 4 |
| 1807 | ggpt 3 dn |
| 1808 | ggpt 4 pp |
| 1809 | ggpt 5 dp |
| 1810 | ggpt 6 dq |
| 1811 | ggrun id_q 14 div tt 64 h bb |
| 1812 | ggpt 0 ggl l 2 |
| 1813 | ggpt 1 ggl l 3 |
| 1814 | ggpt 2 ggl l 4 |
| 1815 | ggpt 3 dn |
| 1816 | ggpt 4 pp |
| 1817 | ggpt 5 dp |
| 1818 | ggpt 6 dk |
| 1819 | ggpt 7 du |
| 1820 | ggrun id_kv 16 div tt 64 h bb |
| 1821 | end |
| 1822 | |
| 1823 | note layer l attention backward: DO, DP, DU, DS (in DP), DQ, DK, then DA. Causal as in ggatt: DP |
| 1824 | note on and below the diagonal (cz 3, SOFTB reads dp above it as 0), DU and DK start at the diagonal |
| 1825 | note (cz 2), DQ stops there (cz 1). |
| 1826 | fn ggbatt l ulong to void |
| 1827 | d = get og 2 |
| 1828 | tt = get og 1 |
| 1829 | e = get og 7 |
| 1830 | dh = gga1 13 |
| 1831 | dn = gga1 16 |
| 1832 | if ggbf_ok |
| 1833 | gs = gggrad_sc |
| 1834 | if and ge d 512 and ne get ggw4_ok 0 0 and ggnv4_ok ggnv4_shape ggr d d |
| 1835 | ggmm4_pt dh 0 dn |
| 1836 | ggmm4_sc gs 1.0f64 |
| 1837 | ggmm4_cw olp l 3 false d d 2 |
| 1838 | else |
| 1839 | k2 = uint mul d 2 |
| 1840 | gkoph 0 dh k2 2 0 0 |
| 1841 | gkoph 1 ggwh ovwo l k2 2 0 0 |
| 1842 | gkopc dn k2 2 0 0 f32 1.0f64 |
| 1843 | ggmmh ggr d d 1 1 2 |
| 1844 | end |
| 1845 | ggmm4_sc 1.0f64 gs |
| 1846 | ggwgh ggl l 5 dh ggg ovwo l d d |
| 1847 | ggfa_bwd l |
| 1848 | ret |
| 1849 | end |
| 1850 | w = ggw ovwo l |
| 1851 | ggxwt dh w dn d d false |
| 1852 | x = ggl l 5 |
| 1853 | w = ggg ovwo l |
| 1854 | ggwg x dh w d d |
| 1855 | if ggfa_ok |
| 1856 | ggfa_bwd l |
| 1857 | ret |
| 1858 | end |
| 1859 | dp = gga1 18 |
| 1860 | pp = ggl l 12 |
| 1861 | dq = gga1 15 |
| 1862 | dk = add dq ulong get ggs 6 |
| 1863 | du = add dk ulong get ggs 6 |
| 1864 | x = ggl l 4 |
| 1865 | gghd 0 dn false |
| 1866 | gghd 1 x false |
| 1867 | ggcp dp 1.0f64 |
| 1868 | ggbh tt tt e false 3 |
| 1869 | ggpl 0 pp true |
| 1870 | gghd 1 dn true |
| 1871 | ggch du 1.0f64 |
| 1872 | ggbh tt e tt false 2 |
| 1873 | ggsoft 16 pp dp |
| 1874 | x = ggl l 3 |
| 1875 | ggpl 0 dp false |
| 1876 | gghd 1 x true |
| 1877 | ggch dq 1.0f64 |
| 1878 | ggbh tt e tt false 1 |
| 1879 | al = 1.0f64 |
| 1880 | if eq OMUTG 0 |
| 1881 | al = neg 1.0f64 |
| 1882 | end |
| 1883 | x = ggl l 2 |
| 1884 | ggpl 0 dp true |
| 1885 | gghd 1 x true |
| 1886 | ggch dk al |
| 1887 | ggbh tt e tt false 2 |
| 1888 | end |
| 1889 | |
| 1890 | fn ggbqkv_h l ulong to void |
| 1891 | d = get og 2 |
| 1892 | da = gga1 14 |
| 1893 | dq = gga1 15 |
| 1894 | d2 = uint mul d 2 |
| 1895 | s6 = get ggs 6 |
| 1896 | s7 = get ggs 7 |
| 1897 | dd2 = uint mul mul d d 2 |
| 1898 | note Fused 3-way GEMM (slot 85): dA = dQ Wq^T + dK Wk^T + dV Wv^T in ONE launch! |
| 1899 | gkoph 0 dq d2 2 sub s6 d2 0 |
| 1900 | gkoph 1 ggwh ovwq l d2 2 sub dd2 d2 0 |
| 1901 | gkopc da d2 2 0 0 f32 1.0f64 |
| 1902 | ggmmh ggr d d 1 1 8 |
| 1903 | a = ggl l 1 |
| 1904 | w = ggg ovwq l |
| 1905 | if ggdw_nv4_ok d d |
| 1906 | ggqnv4_p 1.0f64 |
| 1907 | ggqnv4 a gga1 41 gga1 42 d ggr true |
| 1908 | for p ulong 3 |
| 1909 | ggsk_nv4 add dq mul p ulong s6 add w mul p ulong s7 d d gggrad_sc 1.0f64 |
| 1910 | end |
| 1911 | else |
| 1912 | k = ggks d d 3 |
| 1913 | if eq k 1 |
| 1914 | gkoph 0 a 2 d2 0 0 |
| 1915 | gkoph 1 dq 2 d2 0 s6 |
| 1916 | gkopc w get ggs 0 4 0 s7 f32 1.0f64 |
| 1917 | ggmmh d d ggr 3 3 1 |
| 1918 | else |
| 1919 | sl = uint div ggr k |
| 1920 | gkoph 0 a 2 d2 0 mul sl d2 |
| 1921 | gkoph 1 dq 2 d2 s6 mul sl d2 |
| 1922 | ggskh d d 3 k w s7 |
| 1923 | end |
| 1924 | end |
| 1925 | ggrmsb_gain l false |
| 1926 | end |
| 1927 | |
| 1928 | 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 |
| 1929 | fn ggbqkv l ulong to void |
| 1930 | if ggbf_ok |
| 1931 | ggbqkv_h l |
| 1932 | ret |
| 1933 | end |
| 1934 | d = get og 2 |
| 1935 | da = gga1 14 |
| 1936 | dq = gga1 15 |
| 1937 | dk = add dq ulong get ggs 6 |
| 1938 | du = add dk ulong get ggs 6 |
| 1939 | w = ggw ovwq l |
| 1940 | ggxwt dq w da d d false |
| 1941 | w = ggw ovwk l |
| 1942 | ggxwt dk w da d d true |
| 1943 | w = ggw ovwv l |
| 1944 | ggxwt du w da d d true |
| 1945 | a = ggl l 1 |
| 1946 | w = ggg ovwq l |
| 1947 | s0 = get ggs 0 |
| 1948 | s6 = get ggs 6 |
| 1949 | s7 = get ggs 7 |
| 1950 | k = ggks d d 3 |
| 1951 | if eq k 1 |
| 1952 | gkopa 0 a 4 s0 0 0 |
| 1953 | gkopa 1 dq 4 s0 0 s6 |
| 1954 | gkopc w s0 4 0 s7 f32 1.0f64 |
| 1955 | ggmm d d ggr 3 3 true |
| 1956 | else |
| 1957 | sl = uint div ggr k |
| 1958 | gkopa 0 a 4 s0 0 mul sl s0 |
| 1959 | gkopa 1 dq 4 s0 s6 mul sl s0 |
| 1960 | ggsk d d 3 k w s7 |
| 1961 | end |
| 1962 | x = ggl l 0 |
| 1963 | w = ggw ovg1 l |
| 1964 | s = ggl l 8 |
| 1965 | ggrmsb x w s |
| 1966 | ggain ovg1 l |
| 1967 | end |
| 1968 | |
| 1969 | fn ggbl l ulong to void |
| 1970 | ggbffn l |
| 1971 | ggbatt l |
| 1972 | ggbqkv l |
| 1973 | end |
| 1974 | |
| 1975 | fn ggks_ex m ulong n ulong nz ulong to ulong |
| 1976 | if gt ggks m n nz 1 |
| 1977 | ret 1 |
| 1978 | end |
| 1979 | ret 0 |
| 1980 | end |
| 1981 | |
| 1982 | fn ggbwd_head to void |
| 1983 | d = get og 2 |
| 1984 | v = get og 0 |
| 1985 | r = ggr |
| 1986 | owdh = ggow_ok |
| 1987 | if not owdh |
| 1988 | ggew 20 ggp5 gga1 1 gga1 1 |
| 1989 | ggew 20 mul r d gga1 13 gga1 13 |
| 1990 | end |
| 1991 | if ggbf_ok |
| 1992 | ggxwh_bf gga1 39 ggwh 0 gga1 14 d v false |
| 1993 | if eq ggfzn 0 |
| 1994 | ggwgh_bf gga1 39 gga1 10 ggg 0 v d |
| 1995 | end |
| 1996 | owf = ulong 0 |
| 1997 | if owdh |
| 1998 | owf = 1 |
| 1999 | end |
| 2000 | ggrmsb_h_ex gga1 8 ggw get og 33 gga1 9 gga1 19 owf |
| 2001 | ggain_h get og 33 |
| 2002 | ret |
| 2003 | end |
| 2004 | ggxw gga1 11 ggw 0 gga1 14 d v false |
| 2005 | if eq ggfzn 0 |
| 2006 | ggwg gga1 11 gga1 10 ggg 0 v d |
| 2007 | end |
| 2008 | ggrmsb gga1 8 ggw get og 33 gga1 9 |
| 2009 | ggain get og 33 |
| 2010 | end |
| 2011 | |
| 2012 | fn ggbwd_emb to void |
| 2013 | if ne ggfzn 0 |
| 2014 | ret |
| 2015 | end |
| 2016 | d = get og 2 |
| 2017 | v = get og 0 |
| 2018 | tt = get og 1 |
| 2019 | td = mul tt d |
| 2020 | td4 = div td 4 |
| 2021 | dh = gga1 13 |
| 2022 | if ggbf_ok |
| 2023 | ggb2f mul ggr d gga1 13 gga1 16 |
| 2024 | dh = gga1 16 |
| 2025 | end |
| 2026 | ggu 0 td4 |
| 2027 | ggu 1 get og 6 |
| 2028 | ggpt 1 dh |
| 2029 | ggu 4 mul td 4 |
| 2030 | ggu 5 0 |
| 2031 | ggpt 3 ggg get og 30 |
| 2032 | ggu 8 0 |
| 2033 | cid = uint 14 |
| 2034 | if ggow_ok |
| 2035 | cid = 13 |
| 2036 | end |
| 2037 | ggrun cid 9 ggcd td4 256 1 1 |
| 2038 | ggu 0 d |
| 2039 | ggpt 1 dh |
| 2040 | ggpt 2 gga1 4 |
| 2041 | ggpt 3 gga1 5 |
| 2042 | ggpt 4 ggg 0 |
| 2043 | ggrun 22 10 ggcd d 64 v 1 |
| 2044 | end |
| 2045 | |
| 2046 | fn ggbwd_hcnt to ulong |
| 2047 | c = add 4 ggain_cnt |
| 2048 | if ggow_ok |
| 2049 | c = sub c 2 |
| 2050 | end |
| 2051 | if eq ggfzn 0 |
| 2052 | c = add c add 1 ggks_ex get og 0 get og 2 1 |
| 2053 | end |
| 2054 | note LM-head backward stays in BF16 for NVFP4 convergence parity |
| 2055 | ret c |
| 2056 | end |
| 2057 | |
| 2058 | fn ggbwd_lcnt l ulong to ulong |
| 2059 | d = get og 2 |
| 2060 | f = get og 4 |
| 2061 | c = ulong 20 |
| 2062 | if ggfa_ok |
| 2063 | c = sub c 2 |
| 2064 | end |
| 2065 | if ggbf_ok |
| 2066 | c = sub c 2 |
| 2067 | end |
| 2068 | c = add c ggks_ex d f 1 |
| 2069 | c = add c ggks_ex d d 1 |
| 2070 | c = add c ggks_ex d d 3 |
| 2071 | if or eq OMUTLG 1 eq olp l l |
| 2072 | c = add c add 1 ggks_ex f d 1 |
| 2073 | end |
| 2074 | if ggbf_ok |
| 2075 | if ggdef_gain |
| 2076 | c = sub c 4 |
| 2077 | else |
| 2078 | c = sub c mul 2 sub 2 ggain_cnt |
| 2079 | end |
| 2080 | end |
| 2081 | if ggnv4_ok |
| 2082 | r = ggr |
| 2083 | cw = and ne get ggw4_ok 0 0 and ggnv4_shape r f d ggnv4_shape r d f |
| 2084 | if ggnv4_shape r f d |
| 2085 | if cw |
| 2086 | c = add c 1 |
| 2087 | else |
| 2088 | c = add c 2 |
| 2089 | end |
| 2090 | end |
| 2091 | if and or eq OMUTLG 1 eq olp l l ggdw_nv4_ok f d |
| 2092 | c = add c 2 |
| 2093 | if gt ggks_nv4 f d 1 |
| 2094 | c = add c 1 |
| 2095 | end |
| 2096 | c = sub c ggks_ex f d 1 |
| 2097 | end |
| 2098 | if ggdw_nv4_ok d f |
| 2099 | c = add c 2 |
| 2100 | if gt ggks_nv4 d f 1 |
| 2101 | c = add c 1 |
| 2102 | end |
| 2103 | c = sub c ggks_ex d f 1 |
| 2104 | end |
| 2105 | if ggnv4_shape r d f |
| 2106 | if not cw |
| 2107 | c = add c 2 |
| 2108 | end |
| 2109 | end |
| 2110 | if and ge d 512 and ne get ggw4_ok 0 0 ggnv4_shape r d d |
| 2111 | c = add c 1 |
| 2112 | end |
| 2113 | if ggdw_nv4_ok d d |
| 2114 | c = add c 2 |
| 2115 | if gt ggks_nv4 d d 1 |
| 2116 | c = add c 1 |
| 2117 | end |
| 2118 | c = sub c ggks_ex d d 1 |
| 2119 | q4 = ulong 6 |
| 2120 | if gt ggks_nv4 d d 1 |
| 2121 | q4 = 9 |
| 2122 | end |
| 2123 | c = sub add c add 1 q4 add 1 ggks_ex d d 3 |
| 2124 | end |
| 2125 | end |
| 2126 | ret c |
| 2127 | end |
| 2128 | |
| 2129 | fn ggbwd_ecnt to ulong |
| 2130 | if eq ggfzn 0 |
| 2131 | if ggbf_ok |
| 2132 | ret 3 |
| 2133 | end |
| 2134 | ret 2 |
| 2135 | end |
| 2136 | ret 0 |
| 2137 | end |
| 2138 | |
| 2139 | fn ggbwd_bat to void |
| 2140 | lv = get og 56 |
| 2141 | fzl = ggfzl |
| 2142 | if le lv fzl |
| 2143 | cnt = add ggbwd_hcnt ggbwd_ecnt |
| 2144 | if not gbbegin cnt |
| 2145 | ggdie 120 |
| 2146 | end |
| 2147 | put gkbat 0 1 |
| 2148 | ggbwd_head |
| 2149 | ggbwd_emb |
| 2150 | put gkbat 0 0 |
| 2151 | if not gkbsub |
| 2152 | ggdie 121 |
| 2153 | end |
| 2154 | ret |
| 2155 | end |
| 2156 | hi = lv |
| 2157 | first = true |
| 2158 | while gt hi fzl bound 1024 |
| 2159 | lo = fzl |
| 2160 | if gt sub hi fzl 8 |
| 2161 | lo = sub hi 8 |
| 2162 | end |
| 2163 | nl = sub hi lo |
| 2164 | defg = and ggdef_gain gt nl 0 |
| 2165 | cnt = ulong 0 |
| 2166 | if first |
| 2167 | cnt = add cnt ggbwd_hcnt |
| 2168 | end |
| 2169 | l = hi |
| 2170 | while gt l lo bound 16 |
| 2171 | l = sub l 1 |
| 2172 | cnt = add cnt ggbwd_lcnt l |
| 2173 | end |
| 2174 | if defg |
| 2175 | cnt = add cnt 2 |
| 2176 | end |
| 2177 | if eq lo fzl |
| 2178 | cnt = add cnt ggbwd_ecnt |
| 2179 | end |
| 2180 | if not gbbegin cnt |
| 2181 | ggdie 120 |
| 2182 | end |
| 2183 | put gkbat 0 1 |
| 2184 | if first |
| 2185 | ggbwd_head |
| 2186 | end |
| 2187 | if defg |
| 2188 | put ggdgb 0 lo |
| 2189 | put ggdgb 1 nl |
| 2190 | end |
| 2191 | l2 = hi |
| 2192 | while gt l2 lo bound 16 |
| 2193 | l2 = sub l2 1 |
| 2194 | ggbl l2 |
| 2195 | end |
| 2196 | if defg |
| 2197 | ggain_def lo nl |
| 2198 | put ggdgb 1 0 |
| 2199 | end |
| 2200 | if eq lo fzl |
| 2201 | ggbwd_emb |
| 2202 | end |
| 2203 | put gkbat 0 0 |
| 2204 | if not gkbsub |
| 2205 | ggdie 121 |
| 2206 | end |
| 2207 | first = false |
| 2208 | hi = lo |
| 2209 | end |
| 2210 | end |
| 2211 | |
| 2212 | fn ggbwd to void |
| 2213 | if ggb_ok |
| 2214 | ggbwd_bat |
| 2215 | ret |
| 2216 | end |
| 2217 | ggbwd_head |
| 2218 | lv = get og 56 |
| 2219 | fzl = ggfzl |
| 2220 | defg = and ggdef_gain gt lv fzl |
| 2221 | if defg |
| 2222 | put ggdgb 0 fzl |
| 2223 | put ggdgb 1 sub lv fzl |
| 2224 | end |
| 2225 | l = lv |
| 2226 | while gt l fzl bound 9223372036854775807 |
| 2227 | l = sub l 1 |
| 2228 | ggbl l |
| 2229 | end |
| 2230 | if defg |
| 2231 | ggain_def fzl sub lv fzl |
| 2232 | put ggdgb 1 0 |
| 2233 | end |
| 2234 | ggbwd_emb |
| 2235 | end |
| 2236 | |
| 2237 | note Mutant f: 1 in the product, 0 in the mutant, which uploads layer 0's W1 times 1 + 2^-10 (a |
| 2238 | note forward error on the chip that gcmp2's FP check must name). |
| 2239 | const OMUTF ulong 1 |
| 2240 | |
| 2241 | note W[i] as f32 for i in chunk q of P (a loop runs at most 999999 passes) |
| 2242 | fn ggupc q ulong p ulong to void |
| 2243 | h = get gbmem 5 |
| 2244 | wo = get gga 0 |
| 2245 | i0 = mul q OCH |
| 2246 | for j ulong sub oce q p i0 |
| 2247 | i = add i0 j |
| 2248 | x = bitcast uint f32 ggupv i |
| 2249 | m = mapset h add wo mul i 4 4 ulong x |
| 2250 | end |
| 2251 | end |
| 2252 | |
| 2253 | note the uploaded value of W[i]: the parameter at virtual index i in the product |
| 2254 | fn ggupv i ulong to f64 |
| 2255 | w = owg i |
| 2256 | if eq OMUTF 0 |
| 2257 | a = opw1 0 |
| 2258 | if and ge i a lt i add a mul get og 2 get og 4 |
| 2259 | w = mul w 1.0009765625f64 |
| 2260 | end |
| 2261 | end |
| 2262 | ret w |
| 2263 | end |
| 2264 | |
| 2265 | note upload the f32 parameters |
| 2266 | fn ggupw to void |
| 2267 | p = get og 8 |
| 2268 | for q ulong ocn p |
| 2269 | ggupc q p |
| 2270 | end |
| 2271 | put gkinv 0 1 |
| 2272 | end |
| 2273 | |
| 2274 | note upload the batch tokens and the CSR of x (rows sorted by token) |
| 2275 | fn ggupb to void |
| 2276 | ggupt |
| 2277 | ggcsr |
| 2278 | put gkinv 0 1 |
| 2279 | end |
| 2280 | |
| 2281 | note n values of the Adam pool from s as f32 to arena byte offset o + 4 j, and back (one chunk of an |
| 2282 | note Adam moment) |
| 2283 | fn ggupmc s ulong o ulong n ulong to void |
| 2284 | h = get gbmem 5 |
| 2285 | for j ulong n |
| 2286 | x = bitcast uint f32 omvg add s j |
| 2287 | m = mapset h add o mul j 4 4 ulong x |
| 2288 | end |
| 2289 | end |
| 2290 | |
| 2291 | fn ggdnmc s ulong o ulong n ulong to void |
| 2292 | h = get gbmem 5 |
| 2293 | for j ulong n |
| 2294 | x = uint mapget h add o mul j 4 4 |
| 2295 | omvp add s j f64 bitcast f32 x |
| 2296 | end |
| 2297 | end |
| 2298 | |
| 2299 | note direct OLLMCK05 mmap -> GPU arena load and GPU arena -> OLLMCK05 atomic save |
| 2300 | fn ggck5_rd off ulong nb ulong to void |
| 2301 | if ne get ock5_has 0 0 |
| 2302 | ommap_read get ock5_mh 0 off nb |
| 2303 | ret |
| 2304 | end |
| 2305 | if ne oprd get ock5_ai 0 off 0 nb nb |
| 2306 | exit 70 |
| 2307 | end |
| 2308 | end |
| 2309 | |
| 2310 | fn ggck5_copy_to_gpu src_off ulong dst_off ulong nb ulong to void |
| 2311 | h = get gbmem 5 |
| 2312 | done = ulong 0 |
| 2313 | while lt done nb bound 999999 |
| 2314 | chunk = sub nb done |
| 2315 | if gt chunk OPWCAP |
| 2316 | chunk = OPWCAP |
| 2317 | end |
| 2318 | ggck5_rd add src_off done chunk |
| 2319 | w = mapwrite h add dst_off done opw 0 chunk |
| 2320 | if ne w long chunk |
| 2321 | ggdie 85 |
| 2322 | end |
| 2323 | done = add done chunk |
| 2324 | end |
| 2325 | end |
| 2326 | |
| 2327 | fn ggck5_bf16_expand cnt ulong to void |
| 2328 | j = cnt |
| 2329 | while gt j 0 bound 200000 |
| 2330 | j = sub j 1 |
| 2331 | w16 = ock5_opw2 mul j 2 |
| 2332 | opwput4 mul j 4 shl w16 16 |
| 2333 | end |
| 2334 | end |
| 2335 | |
| 2336 | fn ggck5_bf16_to_gpu src_off ulong dst_off ulong p ulong to void |
| 2337 | h = get gbmem 5 |
| 2338 | i0 = ulong 0 |
| 2339 | while lt i0 p bound 999999 |
| 2340 | cnt = sub p i0 |
| 2341 | if gt cnt OCK5C |
| 2342 | cnt = OCK5C |
| 2343 | end |
| 2344 | ggck5_rd add src_off mul i0 2 mul cnt 2 |
| 2345 | ggck5_bf16_expand cnt |
| 2346 | nb4 = mul cnt 4 |
| 2347 | w = mapwrite h add dst_off mul i0 4 opw 0 nb4 |
| 2348 | if ne w long nb4 |
| 2349 | ggdie 85 |
| 2350 | end |
| 2351 | i0 = add i0 cnt |
| 2352 | end |
| 2353 | end |
| 2354 | |
| 2355 | fn ggload_ck5_bf fl ulong p ulong to void |
| 2356 | h = get gbmem 5 |
| 2357 | ggew 20 ggp5 gga1 1 gga1 1 |
| 2358 | has_bf = ne and fl OCK5F_BF16 0 |
| 2359 | if and has_bf ne get ock5_st 7 0 |
| 2360 | ggck5_copy_to_gpu get ock5_st 6 get gga 38 mul p 2 |
| 2361 | z = mul p 2 |
| 2362 | ze = mul ggp5 2 |
| 2363 | if lt z ze |
| 2364 | if eq rem z 4 2 |
| 2365 | w0 = uint mapget h add get gga 38 sub z 2 4 |
| 2366 | m0 = mapset h add get gga 38 sub z 2 4 ulong and w0 65535 |
| 2367 | z = add z 2 |
| 2368 | end |
| 2369 | while lt z ze bound 512 |
| 2370 | m1 = mapset h add get gga 38 z 4 0 |
| 2371 | z = add z 4 |
| 2372 | end |
| 2373 | end |
| 2374 | put gkinv 0 1 |
| 2375 | else |
| 2376 | ggf2b ggp5 gga1 0 gga1 38 |
| 2377 | end |
| 2378 | if ggnv4_ok |
| 2379 | exp_wnv = sub get gga 47 get gga 48 |
| 2380 | has_nv = ne and fl OCK5F_NV4 0 |
| 2381 | ok_nv = and has_nv eq get ock5_st 9 exp_wnv |
| 2382 | if ok_nv |
| 2383 | d = get og 2 |
| 2384 | if and ggnv4_shape d d d ggnv4_shape get og 4 d d |
| 2385 | ggck5_copy_to_gpu get ock5_st 8 get gga 48 exp_wnv |
| 2386 | put gkinv 0 1 |
| 2387 | put ggw4_ok 0 1 |
| 2388 | ret |
| 2389 | end |
| 2390 | end |
| 2391 | ggquant_w |
| 2392 | end |
| 2393 | end |
| 2394 | |
| 2395 | fn ggload_ck5 load_mv bool to void |
| 2396 | p = get og 8 |
| 2397 | fl = get ock5_st 0 |
| 2398 | if ne and fl OCK5F_F32 0 |
| 2399 | ggck5_copy_to_gpu get ock5_st 2 get gga 0 mul p 4 |
| 2400 | else |
| 2401 | ggck5_bf16_to_gpu get ock5_st 2 get gga 0 p |
| 2402 | end |
| 2403 | put gkinv 0 1 |
| 2404 | if load_mv |
| 2405 | if ne and fl OCK5F_ZMV 0 |
| 2406 | ggew 20 p gga1 21 gga1 21 |
| 2407 | ggew 20 p gga1 22 gga1 22 |
| 2408 | else |
| 2409 | mv_off = get ock5_st 4 |
| 2410 | ggck5_copy_to_gpu mv_off get gga 21 mul p 4 |
| 2411 | ggck5_copy_to_gpu add mv_off mul p 4 get gga 22 mul p 4 |
| 2412 | put gkinv 0 1 |
| 2413 | end |
| 2414 | end |
| 2415 | if ggbf_ok |
| 2416 | ggload_ck5_bf fl p |
| 2417 | end |
| 2418 | put gkinv 0 1 |
| 2419 | ock5_drop_mh |
| 2420 | end |
| 2421 | |
| 2422 | fn ggsave_ck5_hash gb long off ulong chunk ulong p2 ulong h ulong to ulong |
| 2423 | nw_m = div chunk 8 |
| 2424 | curh = ommap_fnv64 gb off nw_m h |
| 2425 | tail_b = sub add chunk p2 mul nw_m 8 |
| 2426 | if gt tail_b 0 |
| 2427 | ret ocfnvw64_off curh mul nw_m 8 div tail_b 8 |
| 2428 | end |
| 2429 | ret curh |
| 2430 | end |
| 2431 | |
| 2432 | fn ggsave_ck5_sec src_off ulong slen ulong pad ulong h ulong pos ulong to ulong |
| 2433 | gb = get gbmem 5 |
| 2434 | done = ulong 0 |
| 2435 | curh = h |
| 2436 | curp = pos |
| 2437 | while lt done slen bound 999999 |
| 2438 | chunk = sub slen done |
| 2439 | if gt chunk 1048576 |
| 2440 | chunk = 1048576 |
| 2441 | end |
| 2442 | r = mapread gb add src_off done opw 0 chunk |
| 2443 | if ne r long chunk |
| 2444 | exit 70 |
| 2445 | end |
| 2446 | p2 = ulong 0 |
| 2447 | if eq add done chunk slen |
| 2448 | p2 = pad |
| 2449 | ock5_pad0 chunk p2 |
| 2450 | end |
| 2451 | curh = ggsave_ck5_hash gb add src_off done chunk p2 curh |
| 2452 | tb = add chunk p2 |
| 2453 | ocwput_opw curp tb |
| 2454 | curp = add curp tb |
| 2455 | done = add done chunk |
| 2456 | end |
| 2457 | ret curh |
| 2458 | end |
| 2459 | |
| 2460 | fn ggck5_plan to void |
| 2461 | p = get og 8 |
| 2462 | fl = OCK5F_F32 |
| 2463 | if ggbf_ok |
| 2464 | fl = or fl OCK5F_BF16 |
| 2465 | end |
| 2466 | if ne get ggw4_ok 0 0 |
| 2467 | fl = or fl OCK5F_NV4 |
| 2468 | end |
| 2469 | if gt get og 59 1 |
| 2470 | fl = or fl OCK5F_LOOP |
| 2471 | end |
| 2472 | orig = get ock5_st 1 |
| 2473 | if or eq orig 0 gt orig 3 |
| 2474 | orig = 2 |
| 2475 | if gt get og 59 1 |
| 2476 | orig = 3 |
| 2477 | end |
| 2478 | end |
| 2479 | w_off = OCK5HDR |
| 2480 | w_len = mul p 4 |
| 2481 | mv_off = ock5_align64 add w_off w_len |
| 2482 | mv_len = mul p 8 |
| 2483 | cur = ock5_align64 add mv_off mv_len |
| 2484 | wbf_off = ulong 0 |
| 2485 | wbf_len = ulong 0 |
| 2486 | if ne and fl OCK5F_BF16 0 |
| 2487 | wbf_off = cur |
| 2488 | wbf_len = mul p 2 |
| 2489 | cur = ock5_align64 add wbf_off wbf_len |
| 2490 | end |
| 2491 | wnv_off = ulong 0 |
| 2492 | wnv_len = ulong 0 |
| 2493 | if ne and fl OCK5F_NV4 0 |
| 2494 | wnv_off = cur |
| 2495 | wnv_len = sub get gga 47 get gga 48 |
| 2496 | cur = ock5_align64 add wnv_off wnv_len |
| 2497 | end |
| 2498 | put ock5_st 0 fl |
| 2499 | put ock5_st 1 orig |
| 2500 | put ock5_st 2 w_off |
| 2501 | put ock5_st 3 w_len |
| 2502 | put ock5_st 4 mv_off |
| 2503 | put ock5_st 5 mv_len |
| 2504 | put ock5_st 6 wbf_off |
| 2505 | put ock5_st 7 wbf_len |
| 2506 | put ock5_st 8 wnv_off |
| 2507 | put ock5_st 9 wnv_len |
| 2508 | put ock5_st 10 cur |
| 2509 | end |
| 2510 | |
| 2511 | fn ggsave_ck5_st gga_i ulong off_i ulong len_i ulong nxt ulong h ulong to ulong |
| 2512 | so = get ock5_st off_i |
| 2513 | sl = get ock5_st len_i |
| 2514 | pad = sub nxt add so sl |
| 2515 | ret ggsave_ck5_sec get gga gga_i sl pad h so |
| 2516 | end |
| 2517 | |
| 2518 | fn ggsave_ck5 ai ulong to void |
| 2519 | ggck5_plan |
| 2520 | ocwopen ai |
| 2521 | h = ulong 14695981039346656037 |
| 2522 | h = ggsave_ck5_st 0 2 3 get ock5_st 4 h |
| 2523 | m_len = mul get og 8 4 |
| 2524 | mv_off = get ock5_st 4 |
| 2525 | h = ggsave_ck5_sec get gga 21 m_len 0 h mv_off |
| 2526 | nxt = get ock5_st 10 |
| 2527 | if ne get ock5_st 6 0 |
| 2528 | nxt = get ock5_st 6 |
| 2529 | else |
| 2530 | if ne get ock5_st 8 0 |
| 2531 | nxt = get ock5_st 8 |
| 2532 | end |
| 2533 | end |
| 2534 | pad_v = sub nxt add mv_off get ock5_st 5 |
| 2535 | h = ggsave_ck5_sec get gga 22 m_len pad_v h add mv_off m_len |
| 2536 | if ne get ock5_st 6 0 |
| 2537 | nxt2 = get ock5_st 10 |
| 2538 | if ne get ock5_st 8 0 |
| 2539 | nxt2 = get ock5_st 8 |
| 2540 | end |
| 2541 | h = ggsave_ck5_st 38 6 7 nxt2 h |
| 2542 | end |
| 2543 | if ne get ock5_st 8 0 |
| 2544 | h = ggsave_ck5_st 48 8 9 get ock5_st 10 h |
| 2545 | end |
| 2546 | put ock5_st 11 h |
| 2547 | ock5_wr_hdr |
| 2548 | ocwend ai |
| 2549 | end |
| 2550 | |
| 2551 | note optimizer state (parameters, Adam pool) to the chip as f32 (m in gga 21, v in gga 22), and back |
| 2552 | fn ggupst to void |
| 2553 | ggupw |
| 2554 | p = get og 8 |
| 2555 | for q ulong ocn p |
| 2556 | i0 = mul q OCH |
| 2557 | n = sub oce q p i0 |
| 2558 | ggupmc add get og 11 i0 add get gga 21 mul i0 4 n |
| 2559 | ggupmc add get og 12 i0 add get gga 22 mul i0 4 n |
| 2560 | end |
| 2561 | put gkinv 0 1 |
| 2562 | end |
| 2563 | |
| 2564 | note only [fz, P) comes back (ggfz): the frozen f64 weights and moments stay as loaded |
| 2565 | fn ggdnst to void |
| 2566 | p = get og 8 |
| 2567 | fz = ggfzn |
| 2568 | for q ulong ocn p |
| 2569 | ggdnq q p fz |
| 2570 | end |
| 2571 | end |
| 2572 | |
| 2573 | note chunk q of P from its first index at or above fz: parameters, then both moments |
| 2574 | fn ggdnq q ulong p ulong fz ulong to void |
| 2575 | a = mul q OCH |
| 2576 | e = oce q p |
| 2577 | if le e fz |
| 2578 | ret |
| 2579 | end |
| 2580 | if lt a fz |
| 2581 | a = fz |
| 2582 | end |
| 2583 | n = sub e a |
| 2584 | ggdnwr a n |
| 2585 | ggdnmc add get og 11 a add get gga 21 mul a 4 n |
| 2586 | ggdnmc add get og 12 a add get gga 22 mul a 4 n |
| 2587 | end |
| 2588 | |
| 2589 | note chip parameters [a, a + n) back into the parameter banks as f64 (n at most OCH) |
| 2590 | fn ggdnwr a ulong n ulong to void |
| 2591 | h = get gbmem 5 |
| 2592 | o = get gga 0 |
| 2593 | for j ulong n |
| 2594 | i = add a j |
| 2595 | x = uint mapget h add o mul i 4 4 |
| 2596 | owp i f64 bitcast f32 x |
| 2597 | end |
| 2598 | end |
| 2599 | |
| 2600 | global ggub uchar 262144 |
| 2601 | fn ggub_u32 i ulong v ulong to void |
| 2602 | o = mul i 4 |
| 2603 | put ggub o uchar and v 255 |
| 2604 | put ggub add o 1 uchar and shr v 8 255 |
| 2605 | put ggub add o 2 uchar and shr v 16 255 |
| 2606 | put ggub add o 3 uchar and shr v 24 255 |
| 2607 | end |
| 2608 | |
| 2609 | fn ggupt to void |
| 2610 | h = get gbmem 5 |
| 2611 | xo = get gga 2 |
| 2612 | yo = get gga 3 |
| 2613 | r = ggr |
| 2614 | if le r 65536 |
| 2615 | for i ulong r |
| 2616 | ggub_u32 i get obx i |
| 2617 | end |
| 2618 | m = mapwrite h xo ggub 0 mul r 4 |
| 2619 | for i ulong r |
| 2620 | ggub_u32 i get oby i |
| 2621 | end |
| 2622 | m = mapwrite h yo ggub 0 mul r 4 |
| 2623 | ret |
| 2624 | end |
| 2625 | for i ulong r |
| 2626 | tk = get obx i |
| 2627 | m = mapset h add xo mul i 4 4 tk |
| 2628 | ty = get oby i |
| 2629 | m = mapset h add yo mul i 4 4 ty |
| 2630 | end |
| 2631 | end |
| 2632 | |
| 2633 | note counting sort of the R rows by input token: start (V + 1) and order (R) arrays |
| 2634 | fn ggcsr to void |
| 2635 | h = get gbmem 5 |
| 2636 | v = get og 0 |
| 2637 | r = ggr |
| 2638 | for c ulong add v 1 |
| 2639 | put ggcn c 0 |
| 2640 | end |
| 2641 | for i ulong r |
| 2642 | c1 = add get obx i 1 |
| 2643 | n = add get ggcn c1 1 |
| 2644 | put ggcn c1 n |
| 2645 | end |
| 2646 | for c ulong v |
| 2647 | c1 = add c 1 |
| 2648 | n = add get ggcn c1 get ggcn c |
| 2649 | put ggcn c1 n |
| 2650 | end |
| 2651 | so = get gga 4 |
| 2652 | v1 = add v 1 |
| 2653 | if le v1 65536 |
| 2654 | for c ulong v1 |
| 2655 | ggub_u32 c get ggcn c |
| 2656 | end |
| 2657 | m = mapwrite h so ggub 0 mul v1 4 |
| 2658 | else |
| 2659 | for c ulong v1 |
| 2660 | n = get ggcn c |
| 2661 | m = mapset h add so mul c 4 4 n |
| 2662 | end |
| 2663 | end |
| 2664 | so = get gga 5 |
| 2665 | if le r 65536 |
| 2666 | for i ulong r |
| 2667 | c = get obx i |
| 2668 | p = get ggcn c |
| 2669 | ggub_u32 p i |
| 2670 | put ggcn c add p 1 |
| 2671 | end |
| 2672 | m = mapwrite h so ggub 0 mul r 4 |
| 2673 | else |
| 2674 | for i ulong r |
| 2675 | c = get obx i |
| 2676 | p = get ggcn c |
| 2677 | m = mapset h add so mul p 4 4 i |
| 2678 | put ggcn c add p 1 |
| 2679 | end |
| 2680 | end |
| 2681 | end |
| 2682 | |
| 2683 | note GRD[i] = G[i] for i in chunk q of P |
| 2684 | fn ggdnc q ulong p ulong to void |
| 2685 | h = get gbmem 5 |
| 2686 | go = get gga 1 |
| 2687 | i0 = mul q OCH |
| 2688 | for j ulong sub oce q p i0 |
| 2689 | i = add i0 j |
| 2690 | x = uint mapget h add go mul i 4 4 |
| 2691 | ocsp i f64 bitcast f32 x |
| 2692 | end |
| 2693 | end |
| 2694 | |
| 2695 | fn ggdown to void |
| 2696 | p = get og 8 |
| 2697 | for q ulong ocn p |
| 2698 | ggdnc q p |
| 2699 | end |
| 2700 | end |
| 2701 | |
| 2702 | note one f32 at arena byte offset o, as f64 |
| 2703 | fn ggf o ulong to f64 |
| 2704 | x = uint mapget get gbmem 5 o 4 |
| 2705 | ret f64 bitcast f32 x |
| 2706 | end |
| 2707 | |
| 2708 | note row loss from the CE stats (m, sum, l_y) of row r: log(sum) + m - l_y |
| 2709 | fn ggrl r ulong to f64 |
| 2710 | o = add get gga 12 mul r 12 |
| 2711 | m = ggf o |
| 2712 | sm = ggf add o 4 |
| 2713 | ly = ggf add o 8 |
| 2714 | ret sub add flog sm m ly |
| 2715 | end |
| 2716 | |
| 2717 | fn ggloss to f64 |
| 2718 | tt = get og 1 |
| 2719 | for b ulong get og 6 |
| 2720 | s = 0.0f64 |
| 2721 | for t ulong tt |
| 2722 | s = add s ggrl add mul b tt t |
| 2723 | end |
| 2724 | put olos b div s f64 tt |
| 2725 | end |
| 2726 | ret olsum |
| 2727 | end |
| 2728 | |
| 2729 | note the step loss and gradient for the batch in obx/oby; ggopen first. full: upload the f32 |
| 2730 | note parameters first and download the gradient into ogr[0..P) after; otherwise both stay on chip. |
| 2731 | note phase wall times (ns): 0 upload, 1 forward, 2 backward, 3 download, 4 loss, 5 on-chip Adam |
| 2732 | global ggtm long 6 |
| 2733 | note Skip CPU ggloss on non-save/non-log training steps (saves ~1.04 ms/step of f64 flog on R=16384!) |
| 2734 | global ggnoloss uint 1 |
| 2735 | global ggsqn_req uint 1 |
| 2736 | global ggsqn_done uint 1 |
| 2737 | fn ggsqn_run to void |
| 2738 | n4 = div ggp5 4 |
| 2739 | ggu 0 n4 |
| 2740 | ggpt 1 gga1 1 |
| 2741 | ggpt 2 gga1 23 |
| 2742 | ggrun 23 6 256 1 1 |
| 2743 | end |
| 2744 | fn ggstep_bat_cnt to ulong |
| 2745 | lv = get og 56 |
| 2746 | fzl = ggfzl |
| 2747 | cnt = add add ggfwd_ecnt mul lv ggfwd_lcnt ggfwd_hcnt |
| 2748 | cnt = add cnt ggbwd_hcnt |
| 2749 | l = fzl |
| 2750 | while lt l lv bound 32 |
| 2751 | cnt = add cnt ggbwd_lcnt l |
| 2752 | l = add l 1 |
| 2753 | end |
| 2754 | if and ggdef_gain gt lv fzl |
| 2755 | cnt = add cnt 2 |
| 2756 | end |
| 2757 | cnt = add cnt ggbwd_ecnt |
| 2758 | ret add cnt 1 |
| 2759 | end |
| 2760 | fn ggstep_bat_ok to bool |
| 2761 | if or not ggb_ok not ggbf_ok |
| 2762 | ret false |
| 2763 | end |
| 2764 | if or ne get ggnoce 0 0 gt get og 56 16 |
| 2765 | ret false |
| 2766 | end |
| 2767 | if gt sub get og 56 ggfzl 8 |
| 2768 | ret false |
| 2769 | end |
| 2770 | ret le ggstep_bat_cnt 512 |
| 2771 | end |
| 2772 | fn ggpass_bat to void |
| 2773 | lv = get og 56 |
| 2774 | fzl = ggfzl |
| 2775 | if not gbbegin ggstep_bat_cnt |
| 2776 | ggdie 120 |
| 2777 | end |
| 2778 | put gkbat 0 1 |
| 2779 | ggfwd_emb |
| 2780 | for l ulong lv bound 32 |
| 2781 | ggfl l |
| 2782 | end |
| 2783 | ggfwd_head |
| 2784 | ggbwd_head |
| 2785 | defg = and ggdef_gain gt lv fzl |
| 2786 | if defg |
| 2787 | put ggdgb 0 fzl |
| 2788 | put ggdgb 1 sub lv fzl |
| 2789 | end |
| 2790 | l2 = lv |
| 2791 | while gt l2 fzl bound 32 |
| 2792 | l2 = sub l2 1 |
| 2793 | ggbl l2 |
| 2794 | end |
| 2795 | if defg |
| 2796 | ggain_def fzl sub lv fzl |
| 2797 | put ggdgb 1 0 |
| 2798 | end |
| 2799 | ggbwd_emb |
| 2800 | ggsqn_run |
| 2801 | put gkbat 0 0 |
| 2802 | end |
| 2803 | fn ggpass full bool to f64 |
| 2804 | t = monons |
| 2805 | if full |
| 2806 | ggupw |
| 2807 | end |
| 2808 | ggupb |
| 2809 | u = monons |
| 2810 | put ggtm 0 sub u t |
| 2811 | if and not full and eq get ggsqn_req 0 1 ggstep_bat_ok |
| 2812 | ggpass_bat |
| 2813 | t_mid = monons |
| 2814 | put ggtm 1 sub t_mid u |
| 2815 | if not gkbsub |
| 2816 | ggdie 121 |
| 2817 | end |
| 2818 | u = monons |
| 2819 | put ggtm 2 sub u t_mid |
| 2820 | put ggsqn_done 0 1 |
| 2821 | else |
| 2822 | ggfwd |
| 2823 | t = monons |
| 2824 | put ggtm 1 sub t u |
| 2825 | ggbwd |
| 2826 | u = monons |
| 2827 | put ggtm 2 sub u t |
| 2828 | end |
| 2829 | if full |
| 2830 | ggdown |
| 2831 | end |
| 2832 | t = monons |
| 2833 | put ggtm 3 sub t u |
| 2834 | l = 0.0f64 |
| 2835 | if eq get ggnoloss 0 0 |
| 2836 | l = ggloss |
| 2837 | end |
| 2838 | u = monons |
| 2839 | put ggtm 4 sub u t |
| 2840 | ret l |
| 2841 | end |
| 2842 | |
| 2843 | fn gggrad to f64 |
| 2844 | ret ggpass true |
| 2845 | end |
| 2846 | |
| 2847 | note clip and Adam on chip at step og[41], the f32 image of oadam: SQN partial sums of g^2, the |
| 2848 | note norm, clip factor, schedule and bias corrections on the host in f64, then ADAM over P |
| 2849 | fn ggadam to void |
| 2850 | p = get og 8 |
| 2851 | if eq get ggsqn_done 0 0 |
| 2852 | ggsqn_run |
| 2853 | else |
| 2854 | put ggsqn_done 0 0 |
| 2855 | end |
| 2856 | h = get gbmem 5 |
| 2857 | so = get gga 23 |
| 2858 | ss = 0.0f64 |
| 2859 | for c ulong 2048 |
| 2860 | x = uint mapget h add so mul c 4 4 |
| 2861 | ss = add ss f64 bitcast f32 x |
| 2862 | end |
| 2863 | nr = fsqrt ss |
| 2864 | cl = 1.0f64 |
| 2865 | if gt nr 1.0 |
| 2866 | cl = div 1.0 nr |
| 2867 | end |
| 2868 | s = get og 41 |
| 2869 | lrs = olrs s bitcast f64 get og 43 |
| 2870 | bc1 = sub 1.0 opowb 0.9 s |
| 2871 | bc2 = sub 1.0 opowb 0.99 s |
| 2872 | if eq OMUTC 0 |
| 2873 | bc1 = 1.0f64 |
| 2874 | bc2 = 1.0f64 |
| 2875 | end |
| 2876 | ggad24 p cl div lrs bc1 div 1.0f64 bc2 |
| 2877 | end |
| 2878 | |
| 2879 | note ADAM over [fz, P) (ggfz; all of P without a freeze): the frozen weights and moments are not |
| 2880 | note touched on the chip. Clip factor cl, lr / bc1 and 1 / bc2 from ggadam. |
| 2881 | fn ggad24 p ulong cl f64 lb f64 ib f64 to void |
| 2882 | fz = ggfzn |
| 2883 | n = sub p fz |
| 2884 | n4 = div n 4 |
| 2885 | o = mul fz 4 |
| 2886 | ggu 0 n4 |
| 2887 | ggpt 1 add gga1 0 o |
| 2888 | ggpt 2 add gga1 21 o |
| 2889 | ggpt 3 add gga1 22 o |
| 2890 | ggpt 4 add gga1 1 o |
| 2891 | ggu 10 ulong grf cl |
| 2892 | ggu 11 ulong grf lb |
| 2893 | ggu 12 ulong grf ib |
| 2894 | if ggbf_ok |
| 2895 | ggpt 7 add gga1 38 mul fz 2 |
| 2896 | ggrun 81 16 ggcd n4 256 1 1 |
| 2897 | ggquant_w |
| 2898 | ret |
| 2899 | end |
| 2900 | ggrun 24 13 ggcd n4 256 1 1 |
| 2901 | end |
| 2902 | |
| 2903 | fn ggtms to void |
| 2904 | oputc 71 |
| 2905 | oputc 84 |
| 2906 | for i ulong 6 |
| 2907 | oputc 32 |
| 2908 | oputl get ggtm i |
| 2909 | end |
| 2910 | for i ulong 4 |
| 2911 | oputc 32 |
| 2912 | oputl get gkt i |
| 2913 | end |
| 2914 | oputc 10 |
| 2915 | end |
| 2916 | |
| 2917 | note keep the CPU gradient: BLK[i] = GRD[i] (chunked: a loop runs at most 999999 passes) |
| 2918 | fn ggkeep p ulong to void |
| 2919 | for q ulong ocn p |
| 2920 | i0 = mul q OCH |
| 2921 | for j ulong sub oce q p i0 |
| 2922 | i = add i0 j |
| 2923 | ogrp add p i ocsg i |
| 2924 | end |
| 2925 | end |
| 2926 | end |
| 2927 | |
| 2928 | note ggcm[0] = max |GRD[i] - BLK[i]|, ggcm[1] = max |BLK[i]| over i < P |
| 2929 | global ggcm f64 2 |
| 2930 | fn ggmaxd p ulong to void |
| 2931 | put ggcm 0 0.0f64 |
| 2932 | put ggcm 1 0.0f64 |
| 2933 | for q ulong ocn p |
| 2934 | i0 = mul q OCH |
| 2935 | for j ulong sub oce q p i0 |
| 2936 | i = add i0 j |
| 2937 | rf = ogrg add p i |
| 2938 | dd = sub ocsg i rf |
| 2939 | if lt dd 0.0f64 |
| 2940 | dd = neg dd |
| 2941 | end |
| 2942 | if lt rf 0.0f64 |
| 2943 | rf = neg rf |
| 2944 | end |
| 2945 | if gt dd get ggcm 0 |
| 2946 | put ggcm 0 dd |
| 2947 | end |
| 2948 | if gt rf get ggcm 1 |
| 2949 | put ggcm 1 rf |
| 2950 | end |
| 2951 | end |
| 2952 | end |
| 2953 | end |
| 2954 | |
| 2955 | note the chip's frozen count and layer (ggfz; 0 in mutant fz) |
| 2956 | fn ggfzn to ulong |
| 2957 | if eq OMUTFZ 0 |
| 2958 | ret 0 |
| 2959 | end |
| 2960 | ret get ggfz 0 |
| 2961 | end |
| 2962 | |
| 2963 | fn ggfzl to ulong |
| 2964 | if eq OMUTFZ 0 |
| 2965 | ret 0 |
| 2966 | end |
| 2967 | ret get ggfz 1 |
| 2968 | end |
| 2969 | |
| 2970 | note FREEZE_LAYERS f for the loaded checkpoint, checked as cmdtrainf: f > L is exit 2, f > 0 on a |
| 2971 | note looped checkpoint exit 17 (ockload's refusal); fz = og 31 + f og 32 |
| 2972 | fn ggfzset f ulong to void |
| 2973 | if gt f get og 5 |
| 2974 | exit 2 |
| 2975 | end |
| 2976 | if and gt f 0 gt get og 59 1 |
| 2977 | exit 17 |
| 2978 | end |
| 2979 | put ggfz 0 0 |
| 2980 | put ggfz 1 f |
| 2981 | if gt f 0 |
| 2982 | put ggfz 0 add get og 31 mul f get og 32 |
| 2983 | end |
| 2984 | end |
| 2985 | |
| 2986 | note `GFZ F fz` |
| 2987 | fn ggfzpr to void |
| 2988 | oputc 71 |
| 2989 | oputc 70 |
| 2990 | oputc 90 |
| 2991 | oputc 32 |
| 2992 | oputu get ggfz 1 |
| 2993 | oputc 32 |
| 2994 | oputu get ggfz 0 |
| 2995 | oputc 10 |
| 2996 | oflush |
| 2997 | end |
| 2998 | |
| 2999 | note gcmp and gacmp arguments CK DATA S [FREEZE_LAYERS]: load CK, open DATA, batch S; FREEZE_LAYERS |
| 3000 | note sets ggfz for the chip and ofreeze / ofzlay (cmdtrainf's freeze) for the CPU reference |
| 3001 | fn ggcarg to void |
| 3002 | na = oargc |
| 3003 | if and ne na 5 ne na 6 |
| 3004 | exit 2 |
| 3005 | end |
| 3006 | ockloop 2 |
| 3007 | odopen 3 |
| 3008 | obatch oargu 4 |
| 3009 | if eq na 6 |
| 3010 | ggfzset oargu 5 |
| 3011 | ggfzpr |
| 3012 | put ofreeze 0 get ggfz 0 |
| 3013 | put ofzlay 0 get ggfz 1 |
| 3014 | end |
| 3015 | end |
| 3016 | |
| 3017 | note GRD[i] = 0 for i < fz: oadam's mask on the CPU reference gradient (the tied head still writes |
| 3018 | note the frozen wte's gradient on the CPU) |
| 3019 | fn ggfzz to void |
| 3020 | fz = get ggfz 0 |
| 3021 | i = ulong 0 |
| 3022 | while lt i fz bound 9223372036854775807 |
| 3023 | ocsp i 0.0f64 |
| 3024 | i = add i 1 |
| 3025 | end |
| 3026 | end |
| 3027 | |
| 3028 | note after gggrad: true without a freeze; else `GCF F fz nonzero` over the chip's GRD[0, fz), which must |
| 3029 | note all be 0 (no frozen gradient launched) |
| 3030 | fn ggfzok to bool |
| 3031 | fz = get ggfz 0 |
| 3032 | if eq fz 0 |
| 3033 | ret true |
| 3034 | end |
| 3035 | n = ulong 0 |
| 3036 | i = ulong 0 |
| 3037 | while lt i fz bound 9223372036854775807 |
| 3038 | if ne ocsg i 0.0f64 |
| 3039 | n = add n 1 |
| 3040 | end |
| 3041 | i = add i 1 |
| 3042 | end |
| 3043 | oputc 71 |
| 3044 | oputc 67 |
| 3045 | oputc 70 |
| 3046 | ggfzln n |
| 3047 | ret eq n 0 |
| 3048 | end |
| 3049 | |
| 3050 | note ` F fz n` and a newline |
| 3051 | fn ggfzln n ulong to void |
| 3052 | oputc 32 |
| 3053 | oputu get ggfz 1 |
| 3054 | oputc 32 |
| 3055 | oputu get ggfz 0 |
| 3056 | oputc 32 |
| 3057 | oputu n |
| 3058 | oputc 10 |
| 3059 | end |
| 3060 | |
| 3061 | note after gacmp's step: true without a freeze; else `GAF F fz changed`, the frozen parameters whose CPU |
| 3062 | note value is not BLK (the loaded f64) or whose chip value (GRD, ggdw) is not f32 of it |
| 3063 | fn ggfzad p ulong to bool |
| 3064 | fz = get ggfz 0 |
| 3065 | if eq fz 0 |
| 3066 | ret true |
| 3067 | end |
| 3068 | n = ulong 0 |
| 3069 | i = ulong 0 |
| 3070 | while lt i fz bound 9223372036854775807 |
| 3071 | n = add n ggfzd1 i p |
| 3072 | i = add i 1 |
| 3073 | end |
| 3074 | oputc 71 |
| 3075 | oputc 65 |
| 3076 | oputc 70 |
| 3077 | ggfzln n |
| 3078 | ret eq n 0 |
| 3079 | end |
| 3080 | |
| 3081 | fn ggfzd1 i ulong p ulong to ulong |
| 3082 | w0 = ogrg add p i |
| 3083 | if ne bitcast ulong owg i bitcast ulong w0 |
| 3084 | ret 1 |
| 3085 | end |
| 3086 | if ne bitcast ulong ocsg i bitcast ulong f64 f32 w0 |
| 3087 | ret 1 |
| 3088 | end |
| 3089 | ret 0 |
| 3090 | end |
| 3091 | |
| 3092 | note gcmp CK DATA S [FREEZE_LAYERS]: CPU ograd and GPU gradient for batch S; prints |
| 3093 | note `GC P maxabs_e12 maxref_e12 rel_e9 cpu_loss_micro gpu_loss_micro gpu_ns tol_e9` and GCMP PASS |
| 3094 | note (rel < tol, |loss difference| < 1e-5) or GCMP FAIL (exit 45). tol = R 2^-24 is the first-order |
| 3095 | note bound gamma_R on the f32 error of a sum of R = B T rows, the longest reduction of the weight |
| 3096 | note gradients, relative to the sum of magnitudes; the CPU reference is f64. ReLU units whose f64 |
| 3097 | note pre-activation lies within f32 resolution of zero can take the other side on the GPU (receipt: |
| 3098 | note evidence/OMEGALLM/gpu/README.md), and both sides are valid subgradients. With FREEZE_LAYERS F |
| 3099 | note (ggcarg) both sides freeze as trainf: the CPU reference is ograd with ofzlay F and GRD[0, fz) |
| 3100 | note zeroed (oadam's mask), the chip's G[0, fz) must be exactly 0 (`GCF F fz nonzero`, ggfzok). |
| 3101 | fn ggcmp to void |
| 3102 | ggcarg |
| 3103 | put og 10 0 |
| 3104 | lc = ograd |
| 3105 | p = get og 8 |
| 3106 | ggfzz |
| 3107 | ggkeep p |
| 3108 | ggopen |
| 3109 | t0 = monons |
| 3110 | lg = gggrad |
| 3111 | t1 = monons |
| 3112 | ggmaxd p |
| 3113 | mx = get ggcm 0 |
| 3114 | mr = get ggcm 1 |
| 3115 | rel = div mx mr |
| 3116 | dl = sub lg lc |
| 3117 | if lt dl 0.0f64 |
| 3118 | dl = neg dl |
| 3119 | end |
| 3120 | oputc 71 |
| 3121 | oputc 67 |
| 3122 | oputc 32 |
| 3123 | oputu p |
| 3124 | oputc 32 |
| 3125 | oputl rmic mul mx 1000000.0f64 |
| 3126 | oputc 32 |
| 3127 | oputl rmic mul mr 1000000.0f64 |
| 3128 | oputc 32 |
| 3129 | oputl rmic mul rel 1000.0f64 |
| 3130 | oputc 32 |
| 3131 | oputl rmic lc |
| 3132 | oputc 32 |
| 3133 | oputl rmic lg |
| 3134 | oputc 32 |
| 3135 | oputl sub t1 t0 |
| 3136 | oputc 32 |
| 3137 | tol = div f64 ggr 16777216.0f64 |
| 3138 | oputl rmic mul tol 1000.0f64 |
| 3139 | oputc 10 |
| 3140 | ggtms |
| 3141 | ok = and and lt rel tol lt dl 0.00001f64 ggfzok |
| 3142 | r = nvstop |
| 3143 | oputc 71 |
| 3144 | oputc 67 |
| 3145 | oputc 77 |
| 3146 | oputc 80 |
| 3147 | oputc 32 |
| 3148 | if ok |
| 3149 | oputc 80 |
| 3150 | oputc 65 |
| 3151 | oputc 83 |
| 3152 | oputc 83 |
| 3153 | oputc 10 |
| 3154 | oflush |
| 3155 | ret |
| 3156 | end |
| 3157 | oputc 70 |
| 3158 | oputc 65 |
| 3159 | oputc 73 |
| 3160 | oputc 76 |
| 3161 | oputc 10 |
| 3162 | oflush |
| 3163 | exit 45 |
| 3164 | end |
| 3165 | |
| 3166 | note true when x is finite (every comparison with NaN is false) |
| 3167 | fn ggfin x f64 to bool |
| 3168 | a = x |
| 3169 | if lt a 0.0f64 |
| 3170 | a = neg a |
| 3171 | end |
| 3172 | ret le a bitcast f64 ulong 9218868437227405311 |
| 3173 | end |
| 3174 | |
| 3175 | note x for printing: 1e6 when x is not finite or above 1e6 (rmic traps past the long range) |
| 3176 | fn ggclp x f64 to f64 |
| 3177 | if ggfin x |
| 3178 | if lt x 1000000.0f64 |
| 3179 | ret x |
| 3180 | end |
| 3181 | end |
| 3182 | ret 1000000.0f64 |
| 3183 | end |
| 3184 | |
| 3185 | note gcmp2 state. ggfs[j] = s_j for the row in flight (F <= 8192). ggk2: [0] ReLU |
| 3186 | note flips (sign of FP_chip differs from FP_ref), [1] non-finite FP values (chip or reference), |
| 3187 | note [2] layer of the largest rho, [3] non-finite gradient values (chip, K0 or K1). |
| 3188 | note ggk3: [0] rho max, [1] max |FP_ref| at a flip, [2] max |FP_chip - FP_ref|. |
| 3189 | global ggfs f64 8192 |
| 3190 | global ggk2 ulong 4 |
| 3191 | global ggk3 f64 3 |
| 3192 | fn ggk2z to void |
| 3193 | for k ulong 4 |
| 3194 | put ggk2 k 0 |
| 3195 | end |
| 3196 | for k ulong 3 |
| 3197 | put ggk3 k 0.0f64 |
| 3198 | end |
| 3199 | end |
| 3200 | |
| 3201 | note s_j = sum_k |bn_k| |W1[k F + j]| for row t of layer l (k < D ascending), the magnitude sum of |
| 3202 | note the FP dot product the chip rounds (layer l is staged: ogmask runs inside olayb) |
| 3203 | fn ggsj l ulong t ulong to void |
| 3204 | d = get og 2 |
| 3205 | f = get og 4 |
| 3206 | bn = add oabn l mul t d |
| 3207 | wv = ovw1 l |
| 3208 | inb = owinb wv |
| 3209 | w = ulong 0 |
| 3210 | if inb |
| 3211 | w = sub wv get og 34 |
| 3212 | else |
| 3213 | w = opx wv |
| 3214 | end |
| 3215 | for j ulong f |
| 3216 | put ggfs j 0.0f64 |
| 3217 | end |
| 3218 | for k ulong d |
| 3219 | a = get oa add bn k |
| 3220 | if lt a 0.0f64 |
| 3221 | a = neg a |
| 3222 | end |
| 3223 | if inb |
| 3224 | ggsjk_b a add w mul k f f |
| 3225 | else |
| 3226 | ggsjk a add w mul k f f |
| 3227 | end |
| 3228 | end |
| 3229 | end |
| 3230 | |
| 3231 | fn ggsjk a f64 wo ulong f ulong to void |
| 3232 | for j ulong f |
| 3233 | x = get ow add wo j |
| 3234 | if lt x 0.0f64 |
| 3235 | x = neg x |
| 3236 | end |
| 3237 | put ggfs j add get ggfs j mul a x |
| 3238 | end |
| 3239 | end |
| 3240 | |
| 3241 | fn ggsjk_b a f64 wo ulong f ulong to void |
| 3242 | for j ulong f |
| 3243 | x = get oww add wo j |
| 3244 | if lt x 0.0f64 |
| 3245 | x = neg x |
| 3246 | end |
| 3247 | put ggfs j add get ggfs j mul a x |
| 3248 | end |
| 3249 | end |
| 3250 | |
| 3251 | note model.om's ReLU mask hook (ogkm[0] = 1, gcmp2 only): for row t of layer l of the sequence in |
| 3252 | note flight, check every unit's reference FP against the chip's FP (left in the arena by gggrad), |
| 3253 | note then zero dF where the chip's FP is not > 0, the chip's fused dF mask (gkope 2; NaN included). |
| 3254 | fn ogmask yo ulong l ulong t ulong to void |
| 3255 | f = get og 4 |
| 3256 | xo = add oafp l mul t f |
| 3257 | ggsj l t |
| 3258 | row = add mul get ogkm 1 get og 1 t |
| 3259 | c0 = add sub ggl l 10 get gga 40 mul mul row f 4 |
| 3260 | for j ulong f |
| 3261 | c = ggf add c0 mul j 4 |
| 3262 | ggk1 get oa add xo j c j l |
| 3263 | if not gt c 0.0f64 |
| 3264 | put oa add yo j 0.0f64 |
| 3265 | end |
| 3266 | end |
| 3267 | end |
| 3268 | |
| 3269 | note one unit: non-finite count, |FP_chip - FP_ref|, rho = that over s_j (inf when s_j = 0 and the |
| 3270 | note values differ), and the flip count |
| 3271 | fn ggk1 v f64 c f64 j ulong l ulong to void |
| 3272 | if not and ggfin v ggfin c |
| 3273 | put ggk2 1 add get ggk2 1 1 |
| 3274 | ret |
| 3275 | end |
| 3276 | dd = sub c v |
| 3277 | if lt dd 0.0f64 |
| 3278 | dd = neg dd |
| 3279 | end |
| 3280 | if gt dd get ggk3 2 |
| 3281 | put ggk3 2 dd |
| 3282 | end |
| 3283 | ggkr dd get ggfs j l |
| 3284 | ggkfl v c |
| 3285 | end |
| 3286 | |
| 3287 | fn ggkr dd f64 s f64 l ulong to void |
| 3288 | if not gt dd 0.0f64 |
| 3289 | ret |
| 3290 | end |
| 3291 | rho = bitcast f64 ulong 9218868437227405312 |
| 3292 | if gt s 0.0f64 |
| 3293 | rho = div dd s |
| 3294 | end |
| 3295 | if gt rho get ggk3 0 |
| 3296 | put ggk3 0 rho |
| 3297 | put ggk2 2 l |
| 3298 | end |
| 3299 | end |
| 3300 | |
| 3301 | fn ggkfl v f64 c f64 to void |
| 3302 | a = gt v 0.0f64 |
| 3303 | b = gt c 0.0f64 |
| 3304 | if eq a b |
| 3305 | ret |
| 3306 | end |
| 3307 | put ggk2 0 add get ggk2 0 1 |
| 3308 | if lt v 0.0f64 |
| 3309 | v = neg v |
| 3310 | end |
| 3311 | if gt v get ggk3 1 |
| 3312 | put ggk3 1 v |
| 3313 | end |
| 3314 | end |
| 3315 | |
| 3316 | note Adam pool [o + i] = GRD[i] for i < P (the chip gradient at o = 0, K0 at o = P; gcmp2 has no |
| 3317 | note Adam step) |
| 3318 | fn ggstash p ulong o ulong to void |
| 3319 | for q ulong ocn p |
| 3320 | ggstq q p o |
| 3321 | end |
| 3322 | end |
| 3323 | |
| 3324 | fn ggstq q ulong p ulong o ulong to void |
| 3325 | i0 = mul q OCH |
| 3326 | for j ulong sub oce q p i0 |
| 3327 | i = add i0 j |
| 3328 | omvp add o i ocsg i |
| 3329 | end |
| 3330 | end |
| 3331 | |
| 3332 | note ggc2: [0] max |G_chip - K0|, [1] max |K0|, [2] max |G_chip - K1| over i < P, with the chip |
| 3333 | note gradient in the Adam pool [0, P), K0 in [P, 2P) and K1 in GRD; non-finite values count in ggk2[3] |
| 3334 | global ggc2 f64 3 |
| 3335 | fn gg2cmp p ulong to void |
| 3336 | for k ulong 3 |
| 3337 | put ggc2 k 0.0f64 |
| 3338 | end |
| 3339 | for q ulong ocn p |
| 3340 | i0 = mul q OCH |
| 3341 | for j ulong sub oce q p i0 |
| 3342 | gg2c1 add i0 j p |
| 3343 | end |
| 3344 | end |
| 3345 | end |
| 3346 | |
| 3347 | fn gg2c1 i ulong p ulong to void |
| 3348 | c = omvg i |
| 3349 | r0 = omvg add p i |
| 3350 | r1 = ocsg i |
| 3351 | if not and and ggfin c ggfin r0 ggfin r1 |
| 3352 | put ggk2 3 add get ggk2 3 1 |
| 3353 | ret |
| 3354 | end |
| 3355 | gg2mx 0 sub c r0 |
| 3356 | gg2mx 1 r0 |
| 3357 | gg2mx 2 sub c r1 |
| 3358 | end |
| 3359 | |
| 3360 | fn gg2mx k ulong x f64 to void |
| 3361 | if lt x 0.0f64 |
| 3362 | x = neg x |
| 3363 | end |
| 3364 | if gt x get ggc2 k |
| 3365 | put ggc2 k x |
| 3366 | end |
| 3367 | end |
| 3368 | |
| 3369 | note gcmp2 CK DATA S: branch-conditioned parity (evidence/OMEGALLM/gpu-max/README.md). The chip |
| 3370 | note gradient runs first and stays in the Adam pool; K0 is the CPU ograd with its own ReLU mask (gcmp's |
| 3371 | note reference), K1 the CPU ograd with the chip's FP sign as the ReLU mask (ogmask), which also checks |
| 3372 | note every FFN pre-activation. Prints `GC2 P rel0_e9 rel1_e9 tolr_e9 rho_e9 tolf_e9 rho_layer flips |
| 3373 | note vflip_e12 fpdiff_e12 nonfinite cpu_loss_micro gpu_loss_micro gpu_ns fails` and GCMP2 PASS, or |
| 3374 | note GCMP2 FAIL (exit 47). rel0 = max|G_chip - K0| / max|K0| (gcmp's rel), rel1 = max|G_chip - K1| / |
| 3375 | note max|K0|; rho = max |FP_chip - FP_ref| / s_j. fails is a bit set: 1 a non-finite FP or gradient |
| 3376 | note value, 2 rho >= tolf = max(D, F, T) 2^-24, 4 rel1 >= tolr = R 2^-24 (gcmp's gamma_R), 8 |chip |
| 3377 | note loss - K0 loss| >= 1e-5. |
| 3378 | fn ggcmp2 to void |
| 3379 | if ne oargc 5 |
| 3380 | exit 2 |
| 3381 | end |
| 3382 | ockloop 2 |
| 3383 | odopen 3 |
| 3384 | obatch oargu 4 |
| 3385 | p = get og 8 |
| 3386 | ggopen |
| 3387 | t0 = monons |
| 3388 | lg = gggrad |
| 3389 | t1 = monons |
| 3390 | ggstash p 0 |
| 3391 | put og 10 0 |
| 3392 | put ogkm 0 0 |
| 3393 | l0 = ograd |
| 3394 | ggstash p p |
| 3395 | for k ulong 4 |
| 3396 | put ggk2 k 0 |
| 3397 | end |
| 3398 | for k ulong 3 |
| 3399 | put ggk3 k 0.0f64 |
| 3400 | end |
| 3401 | put ogkm 0 1 |
| 3402 | put og 10 0 |
| 3403 | l1 = ograd |
| 3404 | put ogkm 0 0 |
| 3405 | gg2cmp p |
| 3406 | ggc2pr lg l0 sub t1 t0 |
| 3407 | end |
| 3408 | |
| 3409 | note the GC2 line and verdict of gcmp2 (lg chip loss, l0 K0 loss, ns chip gradient time); gbcmp2 |
| 3410 | note (gkbf 1) takes gb.om's verdict |
| 3411 | fn ggc2pr lg f64 l0 f64 ns long to void |
| 3412 | fl = ggc2fl lg l0 |
| 3413 | ggc2ln lg l0 ns fl |
| 3414 | ggtms |
| 3415 | r = nvstop |
| 3416 | if eq get gkbf 0 1 |
| 3417 | gbc2vd lg l0 fl |
| 3418 | ret |
| 3419 | end |
| 3420 | ggc2vd eq fl 0 |
| 3421 | end |
| 3422 | |
| 3423 | note ggc2[k] / max |K0|, clamped for printing |
| 3424 | fn gg2rl k ulong to f64 |
| 3425 | ret ggclp div get ggc2 k get ggc2 1 |
| 3426 | end |
| 3427 | |
| 3428 | note tolf = max(D, F, T) 2^-24 |
| 3429 | fn gg2tf to f64 |
| 3430 | n = fmax f64 get og 2 fmax f64 get og 4 f64 get og 1 |
| 3431 | ret div n 16777216.0f64 |
| 3432 | end |
| 3433 | |
| 3434 | note tolr = R 2^-24 (gcmp's gamma_R) |
| 3435 | fn gg2tr to f64 |
| 3436 | ret div f64 ggr 16777216.0f64 |
| 3437 | end |
| 3438 | |
| 3439 | note the gcmp2 fail bits (see ggcmp2); gb.om's BF16 bits under gkbf 1 |
| 3440 | fn ggc2fl lg f64 l0 f64 to ulong |
| 3441 | if eq get gkbf 0 1 |
| 3442 | ret gbc2fl lg l0 get ggk3 0 add get ggk2 1 get ggk2 3 |
| 3443 | end |
| 3444 | dl = sub lg l0 |
| 3445 | if lt dl 0.0f64 |
| 3446 | dl = neg dl |
| 3447 | end |
| 3448 | fl = ulong 0 |
| 3449 | if ne add get ggk2 1 get ggk2 3 0 |
| 3450 | fl = or fl 1 |
| 3451 | end |
| 3452 | if not lt ggclp get ggk3 0 gg2tf |
| 3453 | fl = or fl 2 |
| 3454 | end |
| 3455 | if not lt gg2rl 2 gg2tr |
| 3456 | fl = or fl 4 |
| 3457 | end |
| 3458 | if not lt dl 0.00001f64 |
| 3459 | fl = or fl 8 |
| 3460 | end |
| 3461 | ret fl |
| 3462 | end |
| 3463 | |
| 3464 | fn ggc2ln lg f64 l0 f64 ns long fl ulong to void |
| 3465 | oputc 71 |
| 3466 | oputc 67 |
| 3467 | oputc 50 |
| 3468 | oputc 32 |
| 3469 | oputu get og 8 |
| 3470 | gg2pe gg2rl 0 |
| 3471 | gg2pe gg2rl 2 |
| 3472 | gg2pe gg2tr |
| 3473 | gg2pe ggclp get ggk3 0 |
| 3474 | gg2pe gg2tf |
| 3475 | oputc 32 |
| 3476 | oputu get ggk2 2 |
| 3477 | oputc 32 |
| 3478 | oputu get ggk2 0 |
| 3479 | oputc 32 |
| 3480 | oputl rmic mul ggclp get ggk3 1 1000000.0f64 |
| 3481 | oputc 32 |
| 3482 | oputl rmic mul ggclp get ggk3 2 1000000.0f64 |
| 3483 | oputc 32 |
| 3484 | oputu add get ggk2 1 get ggk2 3 |
| 3485 | oputc 32 |
| 3486 | oputl rmic ggclp l0 |
| 3487 | oputc 32 |
| 3488 | oputl rmic ggclp lg |
| 3489 | oputc 32 |
| 3490 | oputl ns |
| 3491 | oputc 32 |
| 3492 | oputu fl |
| 3493 | oputc 10 |
| 3494 | end |
| 3495 | |
| 3496 | note " x_e9" |
| 3497 | fn gg2pe x f64 to void |
| 3498 | oputc 32 |
| 3499 | oputl rmic mul x 1000.0f64 |
| 3500 | end |
| 3501 | |
| 3502 | fn ggc2vd ok bool to void |
| 3503 | oputc 71 |
| 3504 | oputc 67 |
| 3505 | oputc 77 |
| 3506 | oputc 80 |
| 3507 | oputc 50 |
| 3508 | oputc 32 |
| 3509 | if ok |
| 3510 | oputc 80 |
| 3511 | oputc 65 |
| 3512 | oputc 83 |
| 3513 | oputc 83 |
| 3514 | oputc 10 |
| 3515 | oflush |
| 3516 | ret |
| 3517 | end |
| 3518 | oputc 70 |
| 3519 | oputc 65 |
| 3520 | oputc 73 |
| 3521 | oputc 76 |
| 3522 | oputc 10 |
| 3523 | oflush |
| 3524 | exit 47 |
| 3525 | end |
| 3526 | |
| 3527 | note gtrain CK TRAIN UNTIL EVERY: cmdtrain with the gradient from the GPU (K = 1, no workers); prints |
| 3528 | note `T s+1 loss_micro` at save points like train |
| 3529 | fn ggtrain to void |
| 3530 | if ne oargc 6 |
| 3531 | exit 2 |
| 3532 | end |
| 3533 | ockloop 2 |
| 3534 | odopen 3 |
| 3535 | un = oargu 4 |
| 3536 | ev = oargu 5 |
| 3537 | if eq ev 0 |
| 3538 | exit 2 |
| 3539 | end |
| 3540 | en = get og 42 |
| 3541 | if lt un en |
| 3542 | en = un |
| 3543 | end |
| 3544 | ggopen |
| 3545 | while lt get og 41 en bound 999999 |
| 3546 | s = get og 41 |
| 3547 | obatch s |
| 3548 | put og 10 0 |
| 3549 | ls = gggrad |
| 3550 | oadam |
| 3551 | s1 = add s 1 |
| 3552 | put og 41 s1 |
| 3553 | if or eq rem s1 ev 0 eq s1 en |
| 3554 | ocksave 2 |
| 3555 | oputc 84 |
| 3556 | oputc 32 |
| 3557 | oputu s1 |
| 3558 | oputc 32 |
| 3559 | oputl rmic ls |
| 3560 | oputc 10 |
| 3561 | oflush |
| 3562 | end |
| 3563 | end |
| 3564 | r = nvstop |
| 3565 | end |
| 3566 | |
| 3567 | note BLK[i] = the f64 parameter i before the step |
| 3568 | fn ggkw q ulong p ulong to void |
| 3569 | i0 = mul q OCH |
| 3570 | for j ulong sub oce q p i0 |
| 3571 | i = add i0 j |
| 3572 | ogrp add p i owg i |
| 3573 | end |
| 3574 | end |
| 3575 | |
| 3576 | note GRD[i] = chip parameter i as f64 |
| 3577 | fn ggdw q ulong p ulong to void |
| 3578 | h = get gbmem 5 |
| 3579 | wo = get gga 0 |
| 3580 | i0 = mul q OCH |
| 3581 | for j ulong sub oce q p i0 |
| 3582 | i = add i0 j |
| 3583 | x = uint mapget h add wo mul i 4 4 |
| 3584 | ocsp i f64 bitcast f32 x |
| 3585 | end |
| 3586 | end |
| 3587 | |
| 3588 | note over i < P with w0 = BLK[i], c = parameter i (CPU f64 result), g = GRD[i] (chip f32 result): |
| 3589 | note u = c - w0, d = (g - f32(w0)) - u; ggam 0 max |d|, 1 max |u|, 2 max (|d| - 2^-24 |c|), the part |
| 3590 | note of d above the half-ulp rounding of the f32 master weight |
| 3591 | global ggam f64 3 |
| 3592 | fn ggad1 i ulong p ulong to void |
| 3593 | w0 = ogrg add p i |
| 3594 | c = owg i |
| 3595 | u = sub c w0 |
| 3596 | g = sub ocsg i f64 f32 w0 |
| 3597 | d = fabs sub g u |
| 3598 | u = fabs u |
| 3599 | e = sub d mul fabs c 0.000000059604644775390625f64 |
| 3600 | if gt d get ggam 0 |
| 3601 | put ggam 0 d |
| 3602 | end |
| 3603 | if gt u get ggam 1 |
| 3604 | put ggam 1 u |
| 3605 | end |
| 3606 | if gt e get ggam 2 |
| 3607 | put ggam 2 e |
| 3608 | end |
| 3609 | end |
| 3610 | |
| 3611 | fn ggadc q ulong p ulong to void |
| 3612 | i0 = mul q OCH |
| 3613 | for j ulong sub oce q p i0 |
| 3614 | ggad1 add i0 j p |
| 3615 | end |
| 3616 | end |
| 3617 | |
| 3618 | note gacmp CK DATA S [FREEZE_LAYERS]: one step from CK on batch S with the chip gradient, Adam on the |
| 3619 | note CPU (oadam, f64) and on the chip (ggadam, f32); prints `GA P maxd_e12 maxu_e12 rel_e9 resid_e9 |
| 3620 | note tol_e9 adam_ns loss_micro` and GACMP PASS (resid = max(|d| - 2^-24 |c|) / max |u| < tol = 2^-16) |
| 3621 | note or GACMP FAIL (exit 46). With FREEZE_LAYERS (ggcarg) oadam runs with ofreeze fz and the chip |
| 3622 | note with ggfz; every frozen chip weight must still be its uploaded f32 and every frozen CPU weight |
| 3623 | note its loaded f64 (`GAF F fz changed`, ggfzad). |
| 3624 | fn ggacmp to void |
| 3625 | ggcarg |
| 3626 | put og 10 0 |
| 3627 | p = get og 8 |
| 3628 | ggopen |
| 3629 | ggupst |
| 3630 | lg = ggpass true |
| 3631 | for q ulong ocn p |
| 3632 | ggkw q p |
| 3633 | end |
| 3634 | oadam |
| 3635 | t0 = monons |
| 3636 | ggadam |
| 3637 | t1 = monons |
| 3638 | for q2 ulong ocn p |
| 3639 | ggdw q2 p |
| 3640 | end |
| 3641 | for k ulong 3 |
| 3642 | put ggam k 0.0f64 |
| 3643 | end |
| 3644 | for q3 ulong ocn p |
| 3645 | ggadc q3 p |
| 3646 | end |
| 3647 | mu = get ggam 1 |
| 3648 | rel = div get ggam 0 mu |
| 3649 | res = div get ggam 2 mu |
| 3650 | tol = div 1.0f64 65536.0f64 |
| 3651 | oputc 71 |
| 3652 | oputc 65 |
| 3653 | oputc 32 |
| 3654 | oputu p |
| 3655 | oputc 32 |
| 3656 | oputl rmic mul get ggam 0 1000000.0f64 |
| 3657 | oputc 32 |
| 3658 | oputl rmic mul mu 1000000.0f64 |
| 3659 | oputc 32 |
| 3660 | oputl rmic mul rel 1000.0f64 |
| 3661 | oputc 32 |
| 3662 | oputl rmic mul res 1000.0f64 |
| 3663 | oputc 32 |
| 3664 | oputl rmic mul tol 1000.0f64 |
| 3665 | oputc 32 |
| 3666 | oputl sub t1 t0 |
| 3667 | oputc 32 |
| 3668 | oputl rmic lg |
| 3669 | oputc 10 |
| 3670 | r = nvstop |
| 3671 | ok = and lt res tol ggfzad p |
| 3672 | oputc 71 |
| 3673 | oputc 65 |
| 3674 | oputc 67 |
| 3675 | oputc 77 |
| 3676 | oputc 80 |
| 3677 | oputc 32 |
| 3678 | if ok |
| 3679 | oputc 80 |
| 3680 | oputc 65 |
| 3681 | oputc 83 |
| 3682 | oputc 83 |
| 3683 | oputc 10 |
| 3684 | oflush |
| 3685 | ret |
| 3686 | end |
| 3687 | oputc 70 |
| 3688 | oputc 65 |
| 3689 | oputc 73 |
| 3690 | oputc 76 |
| 3691 | oputc 10 |
| 3692 | oflush |
| 3693 | exit 46 |
| 3694 | end |
| 3695 | |
| 3696 | note gtrainf CK TRAIN UNTIL EVERY [FREEZE_LAYERS]: gtrain with the parameters and Adam state resident |
| 3697 | note on the chip in f32 (ggadam); only the batch goes up each step. At save points the state comes back |
| 3698 | note to f64 for ocksave and `T s+1 loss_micro` is printed; GT at the end has the last step's phase |
| 3699 | note times. FREEZE_LAYERS F > 0 is trainf's frozen core on the chip (ggfz): `GFZ F fz` first, then the |
| 3700 | note embeddings and layers 0..F-1 are skipped in backward, untouched by ADAM and saved as loaded. |
| 3701 | fn ggtrainf to void |
| 3702 | na = oargc |
| 3703 | if and ne na 6 ne na 7 |
| 3704 | exit 2 |
| 3705 | end |
| 3706 | put og 61 1 |
| 3707 | ockloop 2 |
| 3708 | odopen 3 |
| 3709 | un = oargu 4 |
| 3710 | ev = oargu 5 |
| 3711 | if eq ev 0 |
| 3712 | exit 2 |
| 3713 | end |
| 3714 | if eq na 7 |
| 3715 | ggfzset oargu 6 |
| 3716 | ggfzpr |
| 3717 | end |
| 3718 | en = get og 42 |
| 3719 | if lt un en |
| 3720 | en = un |
| 3721 | end |
| 3722 | ggopen |
| 3723 | put ggnofp 0 1 |
| 3724 | if eq get og 46 5 |
| 3725 | ggload_ck5 true |
| 3726 | else |
| 3727 | ggupst |
| 3728 | if ggbf_ok |
| 3729 | ggew 20 ggp5 gga1 1 gga1 1 |
| 3730 | ggf2b ggp5 gga1 0 gga1 38 |
| 3731 | ggquant_w |
| 3732 | end |
| 3733 | end |
| 3734 | while lt get og 41 en bound 999999 |
| 3735 | s = get og 41 |
| 3736 | s1 = add s 1 |
| 3737 | need_l = or eq rem s1 ev 0 eq s1 en |
| 3738 | if need_l |
| 3739 | put ggnoloss 0 0 |
| 3740 | else |
| 3741 | put ggnoloss 0 1 |
| 3742 | end |
| 3743 | obatch s |
| 3744 | put og 10 0 |
| 3745 | put ggsqn_req 0 1 |
| 3746 | put ggsqn_done 0 0 |
| 3747 | ls = ggpass false |
| 3748 | put ggsqn_req 0 0 |
| 3749 | put ggnoloss 0 0 |
| 3750 | t = monons |
| 3751 | ggadam |
| 3752 | put ggtm 5 sub monons t |
| 3753 | put og 41 s1 |
| 3754 | if need_l |
| 3755 | if eq get og 46 5 |
| 3756 | ggsave_ck5 2 |
| 3757 | else |
| 3758 | ggdnst |
| 3759 | ocksave 2 |
| 3760 | end |
| 3761 | oputc 84 |
| 3762 | oputc 32 |
| 3763 | oputu s1 |
| 3764 | oputc 32 |
| 3765 | oputl rmic ls |
| 3766 | oputc 10 |
| 3767 | oflush |
| 3768 | end |
| 3769 | end |
| 3770 | ggtms |
| 3771 | oflush |
| 3772 | r = nvstop |
| 3773 | end |
| 3774 | |
| 3775 | note gprof CK TRAIN N: N gtrainf steps from CK, nothing saved. The last step runs with the launch |
| 3776 | note profile on (gk.om gkpf), then 64 ZERO launches of 64 floats measure the bare launch round trip. |
| 3777 | note Prints `T steps loss_micro`, one `GP id gx gy gz m n k wait_ns` per profiled launch, then GT. |
| 3778 | fn ggprof to void |
| 3779 | if ne oargc 5 |
| 3780 | exit 2 |
| 3781 | end |
| 3782 | put og 61 1 |
| 3783 | ockloop 2 |
| 3784 | odopen 3 |
| 3785 | nn = oargu 4 |
| 3786 | if eq nn 0 |
| 3787 | exit 2 |
| 3788 | end |
| 3789 | ggopen |
| 3790 | put ggnofp 0 1 |
| 3791 | if eq get og 46 5 |
| 3792 | ggload_ck5 true |
| 3793 | else |
| 3794 | ggupst |
| 3795 | if ggbf_ok |
| 3796 | ggew 20 ggp5 gga1 1 gga1 1 |
| 3797 | ggf2b ggp5 gga1 0 gga1 38 |
| 3798 | ggquant_w |
| 3799 | end |
| 3800 | end |
| 3801 | ls = 0.0f64 |
| 3802 | for i ulong nn |
| 3803 | s = get og 41 |
| 3804 | obatch s |
| 3805 | put og 10 0 |
| 3806 | if eq add i 1 nn |
| 3807 | put gkpf 0 1 |
| 3808 | end |
| 3809 | ls = ggpass false |
| 3810 | t = monons |
| 3811 | ggadam |
| 3812 | put ggtm 5 sub monons t |
| 3813 | put og 41 add s 1 |
| 3814 | end |
| 3815 | for i ulong 64 |
| 3816 | ggew 20 64 ggg 0 ggg 0 |
| 3817 | end |
| 3818 | put gkpf 0 0 |
| 3819 | oputc 84 |
| 3820 | oputc 32 |
| 3821 | oputu get og 41 |
| 3822 | oputc 32 |
| 3823 | oputl rmic ls |
| 3824 | oputc 10 |
| 3825 | for q ulong get gkpc 0 |
| 3826 | o = mul q 8 |
| 3827 | oputc 71 |
| 3828 | oputc 80 |
| 3829 | for j ulong 8 |
| 3830 | oputc 32 |
| 3831 | oputu get gkpl add o j |
| 3832 | end |
| 3833 | oputc 10 |
| 3834 | end |
| 3835 | ggtms |
| 3836 | oflush |
| 3837 | r = nvstop |
| 3838 | end |
| 3839 | |
| 3840 | note geval CK HELD W [MERGES]: eval (cli.om cmdeval) with the forward pass on the chip in f32. Windows |
| 3841 | note w < W at offsets w T, B per forward (og 6); the last set pads its spare rows with window 0 and |
| 3842 | note does not count them. Each window adds its mean row loss in window order and, when V > 256, the |
| 3843 | note decoded length of its T targets, as cmdeval does. Prints `E step W loss_micro bpb_micro` like |
| 3844 | note eval, then `GE gpu_ns` (upload, forwards and loss reads). Exits 2 and 23 as eval. |
| 3845 | global ggev f64 1 |
| 3846 | global ggen ulong 1 |
| 3847 | fn ggeval to void |
| 3848 | ww = ggevw |
| 3849 | t = monons |
| 3850 | ggopen |
| 3851 | if eq get og 46 5 |
| 3852 | ggload_ck5 false |
| 3853 | else |
| 3854 | ggupw |
| 3855 | end |
| 3856 | put ggev 0 0.0f64 |
| 3857 | put ggen 0 0 |
| 3858 | bb = get og 6 |
| 3859 | nc = div add ww sub bb 1 bb |
| 3860 | for c ulong nc |
| 3861 | ggevc c ww |
| 3862 | end |
| 3863 | ns = sub monons t |
| 3864 | ggevpr ww |
| 3865 | oputc 71 |
| 3866 | oputc 69 |
| 3867 | oputc 32 |
| 3868 | oputl ns |
| 3869 | oputc 10 |
| 3870 | oflush |
| 3871 | r = nvstop |
| 3872 | end |
| 3873 | |
| 3874 | note eval's argument checks; loads CK, MERGES and HELD and returns W |
| 3875 | fn ggevw to ulong |
| 3876 | na = oargc |
| 3877 | if and ne na 5 ne na 6 |
| 3878 | exit 2 |
| 3879 | end |
| 3880 | put og 61 1 |
| 3881 | ockloop 2 |
| 3882 | ww = oargu 4 |
| 3883 | v = get og 0 |
| 3884 | if gt v 256 |
| 3885 | if eq na 5 |
| 3886 | exit 2 |
| 3887 | end |
| 3888 | obload 5 v |
| 3889 | end |
| 3890 | odopen 3 |
| 3891 | tt = get og 1 |
| 3892 | m = get odn 0 |
| 3893 | if eq ww 0 |
| 3894 | exit 23 |
| 3895 | end |
| 3896 | if gt ww div sub m 1 tt |
| 3897 | exit 23 |
| 3898 | end |
| 3899 | ret ww |
| 3900 | end |
| 3901 | |
| 3902 | note window set c: rows b < B hold window c B + b (window 0 past W), one chip forward, then the sums |
| 3903 | fn ggevc c ulong ww ulong to void |
| 3904 | bb = get og 6 |
| 3905 | tt = get og 1 |
| 3906 | for b ulong bb |
| 3907 | w = add mul c bb b |
| 3908 | if ge w ww |
| 3909 | w = 0 |
| 3910 | end |
| 3911 | obrow b mul w tt |
| 3912 | end |
| 3913 | ggupb |
| 3914 | ggfwd |
| 3915 | for b ulong bb |
| 3916 | w = add mul c bb b |
| 3917 | if lt w ww |
| 3918 | ggevr b |
| 3919 | end |
| 3920 | end |
| 3921 | end |
| 3922 | |
| 3923 | note add row b's mean loss (CE stats left by ggfwd) and, when V > 256, its decoded target bytes |
| 3924 | fn ggevr b ulong to void |
| 3925 | tt = get og 1 |
| 3926 | o = mul b tt |
| 3927 | s = 0.0f64 |
| 3928 | for t ulong tt |
| 3929 | s = add s ggrl add o t |
| 3930 | end |
| 3931 | put ggev 0 add get ggev 0 div s f64 tt |
| 3932 | if gt get og 0 256 |
| 3933 | n = get ggen 0 |
| 3934 | for t ulong tt |
| 3935 | n = add n obtlen get oby add o t |
| 3936 | end |
| 3937 | put ggen 0 n |
| 3938 | end |
| 3939 | end |
| 3940 | |
| 3941 | note the E line of cmdeval from the sums |
| 3942 | fn ggevpr ww ulong to void |
| 3943 | lo = div get ggev 0 f64 ww |
| 3944 | bp = div lo 0.6931471805599453 |
| 3945 | if gt get og 0 256 |
| 3946 | tt = get og 1 |
| 3947 | bp = div mul bp f64 mul ww tt f64 get ggen 0 |
| 3948 | end |
| 3949 | oputc 69 |
| 3950 | oputc 32 |
| 3951 | oputu get og 41 |
| 3952 | oputc 32 |
| 3953 | oputu ww |
| 3954 | oputc 32 |
| 3955 | oputl rmic lo |
| 3956 | oputc 32 |
| 3957 | oputl rmic bp |
| 3958 | oputc 10 |
| 3959 | end |
| 3960 | |
| 3961 | 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 |
| 3962 | note in f32 against the CPU's f64 values for the same forward of batch S. Prints R, then the mean |
| 3963 | 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). |
| 3964 | note It located the CE sum bias that the Kahan sum in gr.om grce removes. |
| 3965 | global gdacc f64 6 |
| 3966 | global gdcpu f64 3 |
| 3967 | fn ggdiag to void |
| 3968 | if ne oargc 5 |
| 3969 | exit 2 |
| 3970 | end |
| 3971 | ockloop 2 |
| 3972 | odopen 3 |
| 3973 | obatch oargu 4 |
| 3974 | put og 10 0 |
| 3975 | ggopen |
| 3976 | ggupw |
| 3977 | ggupb |
| 3978 | ggfwd |
| 3979 | for i ulong 6 |
| 3980 | put gdacc i 0.0f64 |
| 3981 | end |
| 3982 | for b ulong get og 6 |
| 3983 | x = ofwd b |
| 3984 | for t ulong get og 1 |
| 3985 | ggdrow b t |
| 3986 | end |
| 3987 | end |
| 3988 | n = f64 ggr |
| 3989 | oputu ggr |
| 3990 | for i ulong 6 |
| 3991 | oputc 32 |
| 3992 | oputl rmic mul div get gdacc i n 1000.0f64 |
| 3993 | end |
| 3994 | oputc 10 |
| 3995 | oflush |
| 3996 | r = nvstop |
| 3997 | end |
| 3998 | |
| 3999 | fn ggdrow b ulong t ulong to void |
| 4000 | r = add mul b get og 1 t |
| 4001 | ggdcpu t get oby r |
| 4002 | ggdacc r |
| 4003 | put gdacc 4 add get gdacc 4 sub ggdps r 1.0f64 |
| 4004 | end |
| 4005 | |
| 4006 | note CPU f64 stats of row t of the last ofwd: max logit, log sum exp(l - max), logit of y |
| 4007 | fn ggdcpu t ulong y ulong to void |
| 4008 | d = get og 2 |
| 4009 | v = get og 0 |
| 4010 | pr = add get og 18 mul t v |
| 4011 | klogit add get og 17 mul t d pr v d |
| 4012 | m = bitcast f64 ulong 18442240474082181120 |
| 4013 | for j ulong v |
| 4014 | m = fmax m get oa add pr j |
| 4015 | end |
| 4016 | s = 0.0f64 |
| 4017 | for k ulong v |
| 4018 | s = add s fexp sub get oa add pr k m |
| 4019 | end |
| 4020 | put gdcpu 0 m |
| 4021 | put gdcpu 1 flog s |
| 4022 | put gdcpu 2 get oa add pr y |
| 4023 | end |
| 4024 | |
| 4025 | fn ggdacc r ulong to void |
| 4026 | o = add get gga 12 mul r 12 |
| 4027 | mg = sub ggf o get gdcpu 0 |
| 4028 | ls = sub flog ggf add o 4 get gdcpu 1 |
| 4029 | lg = sub ggf add o 8 get gdcpu 2 |
| 4030 | put gdacc 0 add get gdacc 0 mg |
| 4031 | put gdacc 1 add get gdacc 1 lg |
| 4032 | put gdacc 2 add get gdacc 2 ls |
| 4033 | put gdacc 3 add get gdacc 3 sub add ls mg lg |
| 4034 | put gdacc 5 add get gdacc 5 sub lg mg |
| 4035 | end |
| 4036 | |
| 4037 | note sum of the chip's probabilities of row r, recovered from the CE output L = (p - [j = y]) sc |
| 4038 | fn ggdps r ulong to f64 |
| 4039 | v = get og 0 |
| 4040 | sc = div 1.0f64 f64 ggr |
| 4041 | o = add get gga 11 mul mul r v 4 |
| 4042 | s = 0.0f64 |
| 4043 | for j ulong v |
| 4044 | s = add s div ggf add o mul j 4 sc |
| 4045 | end |
| 4046 | ret add s 1.0f64 |
| 4047 | end |
| 4048 | |
| 4049 | note gserve and gsample (aien-train DRA-292): serve and sample (serve.om, cli.om) with the per-token |
| 4050 | note forward on the chip. cli.om's ochipup calls ogchip once the checkpoint loads: B = 1 (one |
| 4051 | note context row, R = T), open the chip, upload the f32 parameters. olastf then calls oglast per |
| 4052 | note token: upload the T context tokens of obx row 0 (positions past CT hold earlier tokens or 0, |
| 4053 | note which the causal forward never reads for position CT - 1), ggdfwd (below: the rows up to the |
| 4054 | note context's last 64-row tile, no CE, so the logits stay logits), and the logits of position |
| 4055 | note CT - OMUTZ read back as f64 into TMP[0..V) (oa), where |
| 4056 | note argmax and draws run as on the CPU. |
| 4057 | note OMUTZ: 1 in the product; 0 in mutant z (the row past the context), which gscmp must catch. |
| 4058 | const OMUTZ ulong 1 |
| 4059 | fn ogchip to void |
| 4060 | put og 6 1 |
| 4061 | ggopen |
| 4062 | if eq get og 46 5 |
| 4063 | ggload_ck5 false |
| 4064 | else |
| 4065 | ggupw |
| 4066 | end |
| 4067 | end |
| 4068 | |
| 4069 | fn oglast to void |
| 4070 | ggupt |
| 4071 | put gkinv 0 1 |
| 4072 | ggdfwd |
| 4073 | ogrd |
| 4074 | end |
| 4075 | |
| 4076 | note the logits of position CT - OMUTZ (row CT - OMUTZ of slot 11) read back as f64 into TMP[0..V) (oa) |
| 4077 | fn ogrd to void |
| 4078 | v = get og 0 |
| 4079 | r = sub get og 9 OMUTZ |
| 4080 | o = add get gga 11 mul mul r v 4 |
| 4081 | tm = get og 27 |
| 4082 | for c ulong v |
| 4083 | put oa add tm c ggf add o mul c 4 |
| 4084 | end |
| 4085 | end |
| 4086 | |
| 4087 | note ggdfwd (ham/ollm-speed-kvdec): per-token GPU forward, including mixed-BF16 variants. ggfwd runs all |
| 4088 | note R = T rows every token; a causal forward never lets a row read a later row, so the logits of |
| 4089 | note position CT - 1 need rows 0..CT - 1 only. ggdfwd runs rows 0..rr - 1, rr = 64 ceil(CT / 64) |
| 4090 | note capped at T (whole 64-row tiles, so every GEMM keeps ggfwd's 64x64 kernels and per-element sums), |
| 4091 | note keeps the residual stream in place in layer 0's input hin (hin += o wo, hin += relu(bn w1) w2: the |
| 4092 | note same f32 adds as ggffn's COPY then accumulate, without its two COPY launches per layer), and runs |
| 4093 | note the final RMS and the logits GEMM on the last 64-row tile only (the one holding row CT - 1). |
| 4094 | note Rows at or past rr keep stale arena data that no computed row reads (causal GEMM tiles; softmax |
| 4095 | note chunks past a row's own are masked, gr.om k6). No CE launch (ggnoce's logits stay logits). |
| 4096 | note OMUTJ: 1 in the product; 0 in mutant j (rr = 64 floor(CT / 64), at least 64: row CT - 1 lies past |
| 4097 | note the computed rows once CT > 64 is not a multiple of 64), which gscmp and gdbench mode 2 must catch. |
| 4098 | const OMUTJ ulong 1 |
| 4099 | note ggdm[0]: the rows the ggd* GEMMs run over |
| 4100 | global ggdm ulong 1 |
| 4101 | note bfdec selects BF16 for attention-output and FFN projections only. QKV, |
| 4102 | note attention scores/value sums, normalization and final logits retain f32. |
| 4103 | global bfdec uint 1 |
| 4104 | |
| 4105 | fn ggdrows to ulong |
| 4106 | ct = get og 9 |
| 4107 | rr = mul div add ct 63 64 64 |
| 4108 | if eq OMUTJ 0 |
| 4109 | rr = mul div ct 64 64 |
| 4110 | if eq rr 0 |
| 4111 | rr = 64 |
| 4112 | end |
| 4113 | end |
| 4114 | tt = get og 1 |
| 4115 | if gt rr tt |
| 4116 | rr = tt |
| 4117 | end |
| 4118 | ret rr |
| 4119 | end |
| 4120 | |
| 4121 | note C[m][n] (+)= X[m][k] W[k][n] over the first m = ggdm[0] rows |
| 4122 | fn ggdxw x ulong w ulong c ulong n ulong k ulong bt bool to void |
| 4123 | k4 = uint mul k 4 |
| 4124 | n4 = uint mul n 4 |
| 4125 | gkopa 0 x k4 4 0 0 |
| 4126 | gkopa 1 w 4 n4 0 0 |
| 4127 | gkopc c n4 4 0 0 f32 1.0f64 |
| 4128 | ggmm get ggdm 0 n k 1 1 bt |
| 4129 | end |
| 4130 | |
| 4131 | note C[m][n] (+)= X[m][k] W[n][k]^T over the first m = ggdm[0] rows |
| 4132 | fn ggdxwt x ulong w ulong c ulong n ulong k ulong bt bool to void |
| 4133 | k4 = uint mul k 4 |
| 4134 | n4 = uint mul n 4 |
| 4135 | gkopa 0 x k4 4 0 0 |
| 4136 | gkopa 1 w k4 4 0 0 |
| 4137 | gkopc c n4 4 0 0 f32 1.0f64 |
| 4138 | ggmm get ggdm 0 n k 1 1 bt |
| 4139 | end |
| 4140 | |
| 4141 | note RMSF (kernel 10) over rows [r0, r0 + m) of the D-wide v and y and of s |
| 4142 | fn ggdrms v ulong g ulong y ulong s ulong r0 ulong m ulong to void |
| 4143 | o = mul r0 mul get og 2 4 |
| 4144 | r = mul m 32 |
| 4145 | ggu 0 r |
| 4146 | ggpt 1 add v o |
| 4147 | ggpt 2 g |
| 4148 | ggpt 3 add y o |
| 4149 | ggpt 4 add s mul r0 4 |
| 4150 | ggrun 10 10 ggcd r 64 1 1 |
| 4151 | end |
| 4152 | |
| 4153 | note q, k, u = a wq, a wk, a wv over the first ggdm[0] rows (ggqkv's batched GEMM) |
| 4154 | fn ggdqkv l ulong to void |
| 4155 | d = get og 2 |
| 4156 | a = ggl l 1 |
| 4157 | w = ggw ovwq l |
| 4158 | c = ggl l 2 |
| 4159 | s0 = get ggs 0 |
| 4160 | s6 = get ggs 6 |
| 4161 | s7 = get ggs 7 |
| 4162 | gkopa 0 a s0 4 0 0 |
| 4163 | gkopa 1 w 4 s0 0 s7 |
| 4164 | gkopc c s0 4 0 s6 f32 1.0f64 |
| 4165 | ggmm get ggdm 0 d d 3 3 false |
| 4166 | end |
| 4167 | |
| 4168 | note ggatt over the first rr = ggdm[0] rows: S (causal, rr x rr), SOFTF on rows < rr (grid rr / 2; |
| 4169 | note arg 0 stays the thread bound T 32), o = P u (causal, k = rr) |
| 4170 | fn ggdatt l ulong to void |
| 4171 | rr = get ggdm 0 |
| 4172 | e = get og 7 |
| 4173 | pp = ggl l 12 |
| 4174 | q = ggl l 2 |
| 4175 | k = ggl l 3 |
| 4176 | gghd 0 q false |
| 4177 | gghd 1 k false |
| 4178 | isq = div 1.0f64 fsqrt f64 e |
| 4179 | ggcp pp isq |
| 4180 | ggbh rr rr e false 3 |
| 4181 | ggu 0 mul get og 1 32 |
| 4182 | ggpt 1 pp |
| 4183 | ggpt 2 pp |
| 4184 | ggrun 15 6 div rr 2 mul get og 6 get og 3 1 |
| 4185 | u = ggl l 4 |
| 4186 | o = ggl l 5 |
| 4187 | ggpl 0 pp false |
| 4188 | gghd 1 u true |
| 4189 | ggch o 1.0f64 |
| 4190 | ggbh rr e rr false 1 |
| 4191 | end |
| 4192 | |
| 4193 | note hin += o wo; bn = rms(hin) g2; hin += relu(bn w1) w2, over the first ggdm[0] rows, hin = layer 0's |
| 4194 | fn ggdffn l ulong to void |
| 4195 | savedbf = get gkbf 0 |
| 4196 | d = get og 2 |
| 4197 | f = get og 4 |
| 4198 | h = ggl 0 0 |
| 4199 | x = ggl l 5 |
| 4200 | w = ggw ovwo l |
| 4201 | if ne get bfdec 0 0 |
| 4202 | put gkbf 0 1 |
| 4203 | end |
| 4204 | ggdxw x w h d d true |
| 4205 | put gkbf 0 savedbf |
| 4206 | x = ggl l 7 |
| 4207 | w = ggw ovg2 l |
| 4208 | s = ggl l 9 |
| 4209 | ggdrms h w x s 0 get ggdm 0 |
| 4210 | fp = ggl l 10 |
| 4211 | w = ggw ovw1 l |
| 4212 | y = ggl l 11 |
| 4213 | gkope 1 y |
| 4214 | if ne get bfdec 0 0 |
| 4215 | put gkbf 0 1 |
| 4216 | end |
| 4217 | ggdxw x w fp f d false |
| 4218 | put gkbf 0 savedbf |
| 4219 | w = ggw ovw2 l |
| 4220 | if ne get bfdec 0 0 |
| 4221 | put gkbf 0 1 |
| 4222 | end |
| 4223 | ggdxw y w h d f true |
| 4224 | put gkbf 0 savedbf |
| 4225 | end |
| 4226 | |
| 4227 | fn ggdfl l ulong to void |
| 4228 | h = ggl 0 0 |
| 4229 | w = ggw ovg1 l |
| 4230 | a = ggl l 1 |
| 4231 | s = ggl l 8 |
| 4232 | ggdrms h w a s 0 get ggdm 0 |
| 4233 | ggdqkv l |
| 4234 | ggdatt l |
| 4235 | ggdffn l |
| 4236 | end |
| 4237 | |
| 4238 | fn ggdfwd to void |
| 4239 | rr = ggdrows |
| 4240 | put ggdm 0 rr |
| 4241 | d = get og 2 |
| 4242 | x = ggl 0 0 |
| 4243 | ggu 0 d |
| 4244 | ggpt 1 x |
| 4245 | x = ggw 0 |
| 4246 | ggpt 2 x |
| 4247 | x = ggw get og 30 |
| 4248 | ggpt 3 x |
| 4249 | x = gga1 2 |
| 4250 | ggpt 4 x |
| 4251 | ggrun 9 10 ggcd d 64 rr get og 6 |
| 4252 | for l ulong get og 56 bound 9223372036854775807 |
| 4253 | ggdfl l |
| 4254 | end |
| 4255 | ggdhead rr |
| 4256 | end |
| 4257 | |
| 4258 | note final RMS and logits GEMM over the last 64-row tile of the rr rows (all rr when rr < 64) |
| 4259 | fn ggdhead rr ulong to void |
| 4260 | d = get og 2 |
| 4261 | tb = ulong 64 |
| 4262 | if lt rr 64 |
| 4263 | tb = rr |
| 4264 | end |
| 4265 | b = sub rr tb |
| 4266 | h = ggl 0 0 |
| 4267 | w = ggw get og 33 |
| 4268 | z = gga1 10 |
| 4269 | s = gga1 9 |
| 4270 | ggdrms h w z s b tb |
| 4271 | put ggdm 0 tb |
| 4272 | x = add z mul mul b d 4 |
| 4273 | w = ggw 0 |
| 4274 | v = get og 0 |
| 4275 | c = add gga1 11 mul mul b v 4 |
| 4276 | ggdxwt x w c v d false |
| 4277 | put ggdm 0 rr |
| 4278 | end |
| 4279 | |
| 4280 | note the forward of gdbench mode md: 0 ggfwd without CE (the gserve forward at edf39b0), 3 ggdfwd with |
| 4281 | note every 64x64 GEMM on the BF16 tensor-core kernels (gk.om gkbf 1, docs/omegallm/CONTRACT-BF16.md; |
| 4282 | note GEMMs on the 32x32 tile stay f32), 5 ggdfwd as one batch.om submission (ggdbat), 7 selective BF16, else ggdfwd |
| 4283 | note in f32 |
| 4284 | fn ggdfm md ulong to void |
| 4285 | if eq md 0 |
| 4286 | put ggnoce 0 1 |
| 4287 | ggfwd |
| 4288 | put ggnoce 0 0 |
| 4289 | ret |
| 4290 | end |
| 4291 | if eq md 3 |
| 4292 | put gkbf 0 1 |
| 4293 | ggdfwd |
| 4294 | put gkbf 0 0 |
| 4295 | ret |
| 4296 | end |
| 4297 | if eq md 7 |
| 4298 | put bfdec 0 1 |
| 4299 | ggdfwd |
| 4300 | put bfdec 0 0 |
| 4301 | ret |
| 4302 | end |
| 4303 | if eq md 5 |
| 4304 | ggdbat |
| 4305 | ret |
| 4306 | end |
| 4307 | ggdfwd |
| 4308 | end |
| 4309 | |
| 4310 | note ggdbat: ggdfwd's launches recorded (gk.om gkbat) into one batch.om batch of exactly 3 + 9 L |
| 4311 | note launches (EMB, nine per layer, final RMS, logits; batch.om refuses a different count before the |
| 4312 | note doorbell), sent once and waited once (gkbsub). The kernels, their order and arguments are |
| 4313 | note ggdfwd's; batch.om ends each body with a WFI release and a shader invalidate, so each grid |
| 4314 | note drains before the next starts. gdbench opens the batch packet (gbbopen) once after the chip. |
| 4315 | fn ggdbat to void |
| 4316 | if not gbbegin add 3 mul 9 get og 56 |
| 4317 | ggdie 120 |
| 4318 | end |
| 4319 | put gkbat 0 1 |
| 4320 | ggdfwd |
| 4321 | put gkbat 0 0 |
| 4322 | if not gkbsub |
| 4323 | ggdie 121 |
| 4324 | end |
| 4325 | end |
| 4326 | |
| 4327 | note FNV-1a over the f64 bits of the V logits in TMP (equal hashes: the same logits, bit for bit) |
| 4328 | fn ggdhash to ulong |
| 4329 | tm = get og 27 |
| 4330 | h = ulong 14695981039346656037 |
| 4331 | for c ulong get og 0 |
| 4332 | x = bitcast ulong get oa add tm c |
| 4333 | h = mul xor h x 1099511628211 |
| 4334 | end |
| 4335 | ret h |
| 4336 | end |
| 4337 | |
| 4338 | note one gdbench step in mode md on the current context: prints `GD step ct tok hash up_ns fwd_ns |
| 4339 | note rd_ns launches build_ns wait_ns` (up: the token upload, fwd: the forward's launches, rd: logits |
| 4340 | note read back and argmax; launches, build and wait from gk.om's gkt) and returns the token |
| 4341 | fn ggdstep s ulong md ulong to ulong |
| 4342 | k0 = get gkt 0 |
| 4343 | b0 = get gkt 1 |
| 4344 | w0 = get gkt 2 |
| 4345 | t0 = monons |
| 4346 | ggupt |
| 4347 | put gkinv 0 1 |
| 4348 | t1 = monons |
| 4349 | ggdfm md |
| 4350 | t2 = monons |
| 4351 | ogrd |
| 4352 | a = oargmax |
| 4353 | t3 = monons |
| 4354 | h = ggdhash |
| 4355 | oputc 71 |
| 4356 | oputc 68 |
| 4357 | oputc 32 |
| 4358 | oputu s |
| 4359 | oputc 32 |
| 4360 | oputu get og 9 |
| 4361 | oputc 32 |
| 4362 | oputu a |
| 4363 | oputc 32 |
| 4364 | oputu h |
| 4365 | oputc 32 |
| 4366 | oputl sub t1 t0 |
| 4367 | oputc 32 |
| 4368 | oputl sub t2 t1 |
| 4369 | oputc 32 |
| 4370 | oputl sub t3 t2 |
| 4371 | oputc 32 |
| 4372 | oputl sub get gkt 0 k0 |
| 4373 | oputc 32 |
| 4374 | oputl sub get gkt 1 b0 |
| 4375 | oputc 32 |
| 4376 | oputl sub get gkt 2 w0 |
| 4377 | oputc 10 |
| 4378 | oflush |
| 4379 | ret a |
| 4380 | end |
| 4381 | |
| 4382 | 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, |
| 4383 | 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` |
| 4384 | note (ndiff: logits whose f64 bits differ, read back from f32, so f32 bits; margin: ggfwd's top-1 |
| 4385 | note logit minus its top-2) and returns tok0. Totals: ggdnd ndiff, ggdmx maxdiff, ggdkd the steps whose |
| 4386 | note tokens differ although the margin exceeds 2 BFTOL. The logits slot is zeroed (ZERO 20) between |
| 4387 | note the two forwards, so ggdfwd cannot pass on ggfwd's rows. |
| 4388 | note BFTOL: the BF16 decode tolerance in nats, declared before its first chip run (ham/ollm-speed |
| 4389 | note 2026-11-10): modes 4 and 8 pass when maxdiff <= BFTOL and no high-margin step diverges. |
| 4390 | const BFTOL f64 0.25 |
| 4391 | global ggdcl f64 65536 |
| 4392 | global ggdmx f64 1 |
| 4393 | global ggdnd ulong 1 |
| 4394 | global ggdkd ulong 1 |
| 4395 | global ggdst f64 1 |
| 4396 | |
| 4397 | note ggdcl[a] minus the largest other ggdcl[c], c < V |
| 4398 | fn ggdmg a ulong to f64 |
| 4399 | b = bitcast f64 ulong 18442240474082181120 |
| 4400 | for c ulong get og 0 |
| 4401 | if ne c a |
| 4402 | b = fmax b get ggdcl c |
| 4403 | end |
| 4404 | end |
| 4405 | ret sub get ggdcl a b |
| 4406 | end |
| 4407 | |
| 4408 | fn ggdpar s ulong md ulong to ulong |
| 4409 | ggupt |
| 4410 | put gkinv 0 1 |
| 4411 | ggdfm 0 |
| 4412 | ogrd |
| 4413 | ggdkeep |
| 4414 | a0 = oargmax |
| 4415 | v = get og 0 |
| 4416 | ggew 20 mul ggr v gga1 11 gga1 11 |
| 4417 | if eq md 4 |
| 4418 | ggdfm 3 |
| 4419 | else |
| 4420 | ggdfm sub md 1 |
| 4421 | end |
| 4422 | ogrd |
| 4423 | a1 = oargmax |
| 4424 | nd = ggdcmp |
| 4425 | dm = get ggdst 0 |
| 4426 | put ggdnd 0 add get ggdnd 0 nd |
| 4427 | put ggdmx 0 fmax get ggdmx 0 dm |
| 4428 | mg = ggdmg a0 |
| 4429 | if and ne a0 a1 gt mg mul 2.0f64 BFTOL |
| 4430 | put ggdkd 0 add get ggdkd 0 1 |
| 4431 | end |
| 4432 | oputc 71 |
| 4433 | oputc 81 |
| 4434 | oputc 32 |
| 4435 | oputu s |
| 4436 | oputc 32 |
| 4437 | oputu get og 9 |
| 4438 | oputc 32 |
| 4439 | oputu a0 |
| 4440 | oputc 32 |
| 4441 | oputu a1 |
| 4442 | oputc 32 |
| 4443 | oputu nd |
| 4444 | oputc 32 |
| 4445 | oputl long mul dm 1000000000.0f64 |
| 4446 | oputc 32 |
| 4447 | oputl long mul mg 1000000000.0f64 |
| 4448 | oputc 10 |
| 4449 | oflush |
| 4450 | ret a0 |
| 4451 | end |
| 4452 | |
| 4453 | note ggdcl = the V logits in TMP |
| 4454 | fn ggdkeep to void |
| 4455 | tm = get og 27 |
| 4456 | for c ulong get og 0 |
| 4457 | x = get oa add tm c |
| 4458 | put ggdcl c x |
| 4459 | end |
| 4460 | end |
| 4461 | |
| 4462 | note the logits in TMP whose bits differ from ggdcl; the largest |difference| into ggdst[0] |
| 4463 | fn ggdcmp to ulong |
| 4464 | tm = get og 27 |
| 4465 | nd = ulong 0 |
| 4466 | dm = 0.0f64 |
| 4467 | for c ulong get og 0 |
| 4468 | x = get oa add tm c |
| 4469 | y = get ggdcl c |
| 4470 | if ne bitcast ulong x bitcast ulong y |
| 4471 | nd = add nd 1 |
| 4472 | end |
| 4473 | dm = fmax dm fabs sub x y |
| 4474 | end |
| 4475 | put ggdst 0 dm |
| 4476 | ret nd |
| 4477 | end |
| 4478 | |
| 4479 | fn ggdsel s ulong md ulong to ulong |
| 4480 | if or or or eq md 2 eq md 4 eq md 6 eq md 8 |
| 4481 | ret ggdpar s md |
| 4482 | end |
| 4483 | ret ggdstep s md |
| 4484 | end |
| 4485 | |
| 4486 | note gdbench CK N PROMPT MERGES MODE (ham/ollm-speed): the per-token chip forward, |
| 4487 | note Modes 7 and 8 select the BF16 decode policy and its comparison, respectively. |
| 4488 | note They retain QKV, attention and logits in f32; three projection GEMMs per layer use BF16. |
| 4489 | note timed, on the contexts of N greedy steps from token 0 then PROMPT's encoding (once the context |
| 4490 | note holds T tokens it slides, as ocpush does). MODE 0: ggfwd (all R = T rows, the gserve forward at |
| 4491 | note edf39b0); 1: ggdfwd (oglast's forward); 3: ggdfwd with BF16 GEMMs (ggdfm 3); 5: ggdfwd as one |
| 4492 | note batched submission (ggdbat); 7: selective BF16. Modes 2, 4, 6 and 8 compare ggfwd and ggdfwd |
| 4493 | note (2 f32, 4 BF16, 6 batched, 8 selective BF16) on every |
| 4494 | note step, one GQ line each (ggdpar), then `GDQ N ndiff maxdiff_e9 diverged`; modes 2 and 6 exit 41 |
| 4495 | note when any logit differs in its bits; modes 4 and 8 exit 42 when maxdiff exceeds BFTOL or a high-margin step |
| 4496 | note diverged. Modes 0, 1, 3, 5 and 7 print one GD line per step (ggdstep), then run one more forward |
| 4497 | note at the last context with the launch profile on (`GP id gx gy gz m n k wait_ns` per launch, gk.om |
| 4498 | note gkpf; batched launches log wait 0), 64 ZERO launches of 64 floats (the bare launch round trip, |
| 4499 | note GP id 20) and `GDT mode N load_ns open_ns` (checkpoint and merges load; chip open and f32 |
| 4500 | note parameter upload). |
| 4501 | fn ggdbench to void |
| 4502 | if ne oargc 7 |
| 4503 | exit 2 |
| 4504 | end |
| 4505 | t0 = monons |
| 4506 | ockloop 2 |
| 4507 | nn = oargu 3 |
| 4508 | md = oargu 6 |
| 4509 | v = get og 0 |
| 4510 | if or le v 256 gt md 8 |
| 4511 | exit 2 |
| 4512 | end |
| 4513 | obload 5 v |
| 4514 | t1 = monons |
| 4515 | ogchip |
| 4516 | if or eq md 5 eq md 6 |
| 4517 | if not gbbopen |
| 4518 | ggdie 122 |
| 4519 | end |
| 4520 | end |
| 4521 | t2 = monons |
| 4522 | put og 9 0 |
| 4523 | ocpush 0 |
| 4524 | np = obprompt 4 |
| 4525 | for i ulong np |
| 4526 | ocpush get obtk i |
| 4527 | end |
| 4528 | put ggdmx 0 0.0f64 |
| 4529 | put ggdnd 0 0 |
| 4530 | put ggdkd 0 0 |
| 4531 | for s ulong nn |
| 4532 | a = ggdsel s md |
| 4533 | ocpush a |
| 4534 | end |
| 4535 | if or or or eq md 2 eq md 4 eq md 6 eq md 8 |
| 4536 | ggdverd nn md |
| 4537 | ret |
| 4538 | end |
| 4539 | ggdprof md |
| 4540 | oputc 71 |
| 4541 | oputc 68 |
| 4542 | oputc 84 |
| 4543 | oputc 32 |
| 4544 | oputu md |
| 4545 | oputc 32 |
| 4546 | oputu nn |
| 4547 | oputc 32 |
| 4548 | oputl sub t1 t0 |
| 4549 | oputc 32 |
| 4550 | oputl sub t2 t1 |
| 4551 | oputc 10 |
| 4552 | oflush |
| 4553 | r = nvstop |
| 4554 | end |
| 4555 | |
| 4556 | note the parity modes' last line `GDQ N ndiff maxdiff_e9 diverged`, then their exit codes |
| 4557 | fn ggdverd nn ulong md ulong to void |
| 4558 | oputc 71 |
| 4559 | oputc 68 |
| 4560 | oputc 81 |
| 4561 | oputc 32 |
| 4562 | oputu nn |
| 4563 | oputc 32 |
| 4564 | oputu get ggdnd 0 |
| 4565 | oputc 32 |
| 4566 | oputl long mul get ggdmx 0 1000000000.0f64 |
| 4567 | oputc 32 |
| 4568 | oputu get ggdkd 0 |
| 4569 | oputc 10 |
| 4570 | oflush |
| 4571 | r = nvstop |
| 4572 | if and and ne md 4 ne md 8 gt get ggdnd 0 0 |
| 4573 | exit 41 |
| 4574 | end |
| 4575 | if and or eq md 4 eq md 8 or gt get ggdmx 0 BFTOL gt get ggdkd 0 0 |
| 4576 | exit 42 |
| 4577 | end |
| 4578 | end |
| 4579 | |
| 4580 | note one forward in mode md at the current context with the launch profile on, then 64 ZERO launches |
| 4581 | note of 64 floats; prints one GP line per profiled launch |
| 4582 | fn ggdprof md ulong to void |
| 4583 | put gkpc 0 0 |
| 4584 | put gkpf 0 1 |
| 4585 | ggupt |
| 4586 | put gkinv 0 1 |
| 4587 | ggdfm md |
| 4588 | for i ulong 64 |
| 4589 | ggew 20 64 ggg 0 ggg 0 |
| 4590 | end |
| 4591 | put gkpf 0 0 |
| 4592 | for q ulong get gkpc 0 |
| 4593 | o = mul q 8 |
| 4594 | oputc 71 |
| 4595 | oputc 80 |
| 4596 | for j ulong 8 |
| 4597 | oputc 32 |
| 4598 | oputu get gkpl add o j |
| 4599 | end |
| 4600 | oputc 10 |
| 4601 | end |
| 4602 | end |
| 4603 | |
| 4604 | note gscmp CK N PROMPT MERGES (V > 256): the chip forward against the CPU forward on the contexts of |
| 4605 | note N greedy steps from token 0 then PROMPT's encoding. Each step runs ofwd and olast (f64) into |
| 4606 | note gscl, then oglast (chip, f32) into TMP, and prints `GS step cpu_tok chip_tok maxdiff_nano |
| 4607 | note margin_nano`: maxdiff = max over the V logits of |chip - cpu|, margin = the CPU's top-1 logit |
| 4608 | note minus its top-2. The CPU token is pushed. Last line `GSCMP N diverged maxdiff_nano`: a step |
| 4609 | note diverges when the tokens differ although the margin exceeds 2 GSTOL. Exit 40 when a step |
| 4610 | note diverged or maxdiff exceeds GSTOL. |
| 4611 | const GSTOL f64 0.002 |
| 4612 | global gscl f64 65536 |
| 4613 | note gscmp totals: gsmd[0] the largest maxdiff, gskd[0] the diverged steps |
| 4614 | global gsmd f64 1 |
| 4615 | global gskd ulong 1 |
| 4616 | fn ggscmp to void |
| 4617 | if ne oargc 6 |
| 4618 | exit 2 |
| 4619 | end |
| 4620 | ockloop 2 |
| 4621 | nn = oargu 3 |
| 4622 | v = get og 0 |
| 4623 | if le v 256 |
| 4624 | exit 2 |
| 4625 | end |
| 4626 | obload 5 v |
| 4627 | ogchip |
| 4628 | put og 9 0 |
| 4629 | ocpush 0 |
| 4630 | np = obprompt 4 |
| 4631 | for i ulong np |
| 4632 | ocpush get obtk i |
| 4633 | end |
| 4634 | put gsmd 0 0.0f64 |
| 4635 | put gskd 0 0 |
| 4636 | for s ulong nn |
| 4637 | a = ggsst s |
| 4638 | ocpush a |
| 4639 | end |
| 4640 | md = get gsmd 0 |
| 4641 | kd = get gskd 0 |
| 4642 | oputc 71 |
| 4643 | oputc 83 |
| 4644 | oputc 67 |
| 4645 | oputc 77 |
| 4646 | oputc 80 |
| 4647 | oputc 32 |
| 4648 | oputu nn |
| 4649 | oputc 32 |
| 4650 | oputu kd |
| 4651 | oputc 32 |
| 4652 | oputl long mul md 1000000000.0f64 |
| 4653 | oputc 10 |
| 4654 | oflush |
| 4655 | r = nvstop |
| 4656 | if or gt kd 0 gt md GSTOL |
| 4657 | exit 40 |
| 4658 | end |
| 4659 | end |
| 4660 | |
| 4661 | note one gscmp step s on the current context: prints its GS line, updates the totals, returns the |
| 4662 | note CPU token |
| 4663 | fn ggsst s ulong to ulong |
| 4664 | a1 = ggscpu |
| 4665 | mg = ggsmg a1 |
| 4666 | oglast |
| 4667 | a2 = oargmax |
| 4668 | dm = ggsdf |
| 4669 | put gsmd 0 fmax get gsmd 0 dm |
| 4670 | if and ne a1 a2 gt mg mul 2.0f64 GSTOL |
| 4671 | put gskd 0 add get gskd 0 1 |
| 4672 | end |
| 4673 | oputc 71 |
| 4674 | oputc 83 |
| 4675 | oputc 32 |
| 4676 | oputu s |
| 4677 | oputc 32 |
| 4678 | oputu a1 |
| 4679 | oputc 32 |
| 4680 | oputu a2 |
| 4681 | oputc 32 |
| 4682 | oputl long mul dm 1000000000.0f64 |
| 4683 | oputc 32 |
| 4684 | oputl long mul mg 1000000000.0f64 |
| 4685 | oputc 10 |
| 4686 | oflush |
| 4687 | ret a1 |
| 4688 | end |
| 4689 | |
| 4690 | note the CPU forward's last-position logits into gscl; returns its argmax |
| 4691 | fn ggscpu to ulong |
| 4692 | v = get og 0 |
| 4693 | tm = get og 27 |
| 4694 | lf = ofwd 0 |
| 4695 | olast |
| 4696 | for c ulong v |
| 4697 | put gscl c get oa add tm c |
| 4698 | end |
| 4699 | ret oargmax |
| 4700 | end |
| 4701 | |
| 4702 | note max over c < V of |TMP[c] - gscl[c]| |
| 4703 | fn ggsdf to f64 |
| 4704 | tm = get og 27 |
| 4705 | dm = 0.0f64 |
| 4706 | for c ulong get og 0 |
| 4707 | x = fabs sub get oa add tm c get gscl c |
| 4708 | dm = fmax dm x |
| 4709 | end |
| 4710 | ret dm |
| 4711 | end |
| 4712 | |
| 4713 | note the CPU's top-1 logit gscl[a] minus its largest other logit |
| 4714 | fn ggsmg a ulong to f64 |
| 4715 | b = bitcast f64 ulong 18442240474082181120 |
| 4716 | for c ulong get og 0 |
| 4717 | if ne c a |
| 4718 | b = fmax b get gscl c |
| 4719 | end |
| 4720 | end |
| 4721 | ret sub get gscl a b |
| 4722 | end |
| 4723 | |
| 4724 | note gfcache BASE.ck TOKS W OUT.hc [BF16]: batched GB10 frozen-trunk hidden-state dumper writing |
| 4725 | note the canonical OLLMFH01 format (compatible with trainfc, spforge, and spcryst) at full GPU speed. |
| 4726 | note Streams B windows per batched GPU forward (ggnoce = 2 skips logits/CE) and supports NP > 1 looped |
| 4727 | note checkpoints and D <= 2048 with chunked OCKC writes. Optional arg 6 selects BF16 (1) or f32 (0); |
| 4728 | note default is 1 when T, D, F are multiples of 64, else 0. Prints `GFCACHE W T D L gpu_ns bf16`. |
| 4729 | fn ggfch_wry pos ulong b ulong to ulong |
| 4730 | tt = get og 1 |
| 4731 | bo = mul b tt |
| 4732 | for t ulong tt |
| 4733 | ocput8 mul t 8 get oby add bo t |
| 4734 | end |
| 4735 | nb = mul tt 8 |
| 4736 | ocwput pos nb |
| 4737 | ret add pos nb |
| 4738 | end |
| 4739 | |
| 4740 | fn ggfch_wr1 base ulong cnt ulong to void |
| 4741 | h = get gbmem 5 |
| 4742 | for j ulong cnt |
| 4743 | x = uint mapget h add base mul j 4 4 |
| 4744 | ocput8 mul j 8 bitcast ulong f64 bitcast f32 x |
| 4745 | end |
| 4746 | end |
| 4747 | |
| 4748 | fn ggfch_wr pos ulong b ulong to ulong |
| 4749 | pos = ggfch_wry pos b |
| 4750 | td = mul get og 1 get og 2 |
| 4751 | base = add get gga 8 mul mul b td 4 |
| 4752 | done = ulong 0 |
| 4753 | while lt done td bound 65536 |
| 4754 | cnt = sub td done |
| 4755 | if gt cnt OCKC |
| 4756 | cnt = OCKC |
| 4757 | end |
| 4758 | ggfch_wr1 add base mul done 4 cnt |
| 4759 | nb = mul cnt 8 |
| 4760 | ocwput pos nb |
| 4761 | pos = add pos nb |
| 4762 | done = add done cnt |
| 4763 | end |
| 4764 | ret pos |
| 4765 | end |
| 4766 | |
| 4767 | fn ggfch_defbf to uint |
| 4768 | if ne rem get og 1 64 0 |
| 4769 | ret 0 |
| 4770 | end |
| 4771 | if ne rem get og 2 64 0 |
| 4772 | ret 0 |
| 4773 | end |
| 4774 | if ne rem get og 4 64 0 |
| 4775 | ret 0 |
| 4776 | end |
| 4777 | ret 1 |
| 4778 | end |
| 4779 | |
| 4780 | fn ggfch_step c ulong ww ulong pos ulong to ulong |
| 4781 | bb = get og 6 |
| 4782 | tt = get og 1 |
| 4783 | for b ulong bb |
| 4784 | w = add mul c bb b |
| 4785 | if ge w ww |
| 4786 | w = 0 |
| 4787 | end |
| 4788 | obrow b mul w tt |
| 4789 | end |
| 4790 | ggupt |
| 4791 | put gkinv 0 1 |
| 4792 | put ggnoce 0 2 |
| 4793 | ggfwd |
| 4794 | put ggnoce 0 0 |
| 4795 | for b2 ulong bb |
| 4796 | w2 = add mul c bb b2 |
| 4797 | if lt w2 ww |
| 4798 | pos = ggfch_wr pos b2 |
| 4799 | end |
| 4800 | end |
| 4801 | ret pos |
| 4802 | end |
| 4803 | |
| 4804 | fn ggfch_pr ww ulong ns long bf uint to void |
| 4805 | oputc 71 |
| 4806 | oputc 70 |
| 4807 | oputc 67 |
| 4808 | oputc 65 |
| 4809 | oputc 67 |
| 4810 | oputc 72 |
| 4811 | oputc 69 |
| 4812 | oputc 32 |
| 4813 | oputu ww |
| 4814 | oputc 32 |
| 4815 | oputu get og 1 |
| 4816 | oputc 32 |
| 4817 | oputu get og 2 |
| 4818 | oputc 32 |
| 4819 | oputu get og 5 |
| 4820 | oputc 32 |
| 4821 | oputl ns |
| 4822 | oputc 32 |
| 4823 | oputu ulong bf |
| 4824 | oputc 10 |
| 4825 | oflush |
| 4826 | end |
| 4827 | |
| 4828 | fn ggfcache to void |
| 4829 | na = oargc |
| 4830 | if and ne na 6 ne na 7 |
| 4831 | exit 2 |
| 4832 | end |
| 4833 | ockloop 2 |
| 4834 | odopen 3 |
| 4835 | ww = oargu 4 |
| 4836 | if or eq ww 0 gt ww div sub get odn 0 1 get og 1 |
| 4837 | exit 23 |
| 4838 | end |
| 4839 | bf = ggfch_defbf |
| 4840 | if eq na 7 |
| 4841 | ba = oargu 6 |
| 4842 | if gt ba 1 |
| 4843 | exit 2 |
| 4844 | end |
| 4845 | bf = uint ba |
| 4846 | end |
| 4847 | bh = osp_basehash get og 5 get og 33 |
| 4848 | ocwopen 5 |
| 4849 | ocput8 0 OFHMAG |
| 4850 | ocput8 8 ww |
| 4851 | ocput8 16 get og 1 |
| 4852 | ocput8 24 get og 2 |
| 4853 | ocput8 32 get og 5 |
| 4854 | ocput8 40 bh |
| 4855 | ocwput 0 48 |
| 4856 | pos = ulong 48 |
| 4857 | t0 = monons |
| 4858 | ggopen |
| 4859 | ggupw |
| 4860 | put gkbf 0 bf |
| 4861 | bb = get og 6 |
| 4862 | nc = div add ww sub bb 1 bb |
| 4863 | for c ulong nc |
| 4864 | pos = ggfch_step c ww pos |
| 4865 | end |
| 4866 | put gkbf 0 0 |
| 4867 | ns = sub monons t0 |
| 4868 | ocwend 5 |
| 4869 | r = nvstop |
| 4870 | ggfch_pr ww ns bf |
| 4871 | end |
| 4872 | |
| 4873 | note ogcmd: the gb10 target's GPU commands. cli.om's ocmd offers every command word here first |
| 4874 | note (nogpu.om's ogcmd takes none on the CPU-only targets), so the gb10 unit is the aarch64 source |
| 4875 | note with gpu/ in place of nogpu.om and no renamed main. It runs the GPU commands here and the BF16 |
| 4876 | note training commands in gb.om and returns true; any other word returns false and ocmd goes on to |
| 4877 | note the CPU commands. The inference interface is documented in docs/omegallm/CONTRACT-BF16.md; |
| 4878 | note command constants encode little-endian word bytes. |
| 4879 | note nvfp4 M N K ITERS: live physical GB10 SM121 OMMA.SF.16864.F32.E2M1.E2M1.UE4M3.4X |
| 4880 | note in-register instruction-rate microbenchmark (slot 84, 500 TF hardware peak). |
| 4881 | note Loads per-thread operands/scales once before the K-loop, repeats 16 independent |
| 4882 | note OMMA.SF.16864 ops in registers per ktile, and verifies 64 multi-CTA/warp-0..7 |
| 4883 | note output lanes + 4 NaN sentinels (checked=68). Not a global-memory GEMM. Prints: |
| 4884 | note `NVFP4 M N K ITERS wall_ns tflops_x100 pct_peak_x100 expected got checked bad INREG_OMMA_MICROBENCH PASS/FAIL`. |
| 4885 | fn ggnvfp4_lch m ulong n ulong k ulong to void |
| 4886 | base = gkbase |
| 4887 | ggpt 0 base |
| 4888 | ggpt 1 add base 8192 |
| 4889 | ggpt 2 add base 16384 |
| 4890 | ggpt 3 add base 24576 |
| 4891 | ggpt 4 add base 65536 |
| 4892 | ggu 10 div k 64 |
| 4893 | ggu 11 n |
| 4894 | ggrun 84 12 div n 128 div m 128 1 |
| 4895 | end |
| 4896 | |
| 4897 | fn ggnvfp4_cpos r ulong c ulong n ulong to ulong |
| 4898 | ret add 65536 mul add mul r n c 4 |
| 4899 | end |
| 4900 | |
| 4901 | fn ggnvfp4_dr k ulong to ulong |
| 4902 | if eq k 1 |
| 4903 | ret 7 |
| 4904 | end |
| 4905 | if eq k 3 |
| 4906 | ret 99 |
| 4907 | end |
| 4908 | if eq k 4 |
| 4909 | ret 96 |
| 4910 | end |
| 4911 | if ge k 5 |
| 4912 | ret 103 |
| 4913 | end |
| 4914 | ret 0 |
| 4915 | end |
| 4916 | |
| 4917 | fn ggnvfp4_dc k ulong to ulong |
| 4918 | if or eq k 1 eq k 3 |
| 4919 | ret 6 |
| 4920 | end |
| 4921 | if or eq k 2 eq k 4 |
| 4922 | ret 64 |
| 4923 | end |
| 4924 | if eq k 5 |
| 4925 | ret 66 |
| 4926 | end |
| 4927 | if eq k 6 |
| 4928 | ret 68 |
| 4929 | end |
| 4930 | if eq k 7 |
| 4931 | ret 70 |
| 4932 | end |
| 4933 | ret 0 |
| 4934 | end |
| 4935 | |
| 4936 | fn ggnvfp4_p1 h long r ulong c ulong n ulong to void |
| 4937 | m = mapset h ggnvfp4_cpos r c n 4 2143289344 |
| 4938 | m = mapset h ggnvfp4_cpos r add c 1 n 4 2143289344 |
| 4939 | m = mapset h ggnvfp4_cpos add r 8 c n 4 2143289344 |
| 4940 | m = mapset h ggnvfp4_cpos add r 8 add c 1 n 4 2143289344 |
| 4941 | end |
| 4942 | |
| 4943 | fn ggnvfp4_c1 h long r ulong c ulong n ulong exp f64 to ulong |
| 4944 | bad = ulong 0 |
| 4945 | if ne f64 bitcast f32 uint mapget h ggnvfp4_cpos r c n 4 exp |
| 4946 | bad = add bad 1 |
| 4947 | end |
| 4948 | if ne f64 bitcast f32 uint mapget h ggnvfp4_cpos r add c 1 n 4 exp |
| 4949 | bad = add bad 1 |
| 4950 | end |
| 4951 | if ne f64 bitcast f32 uint mapget h ggnvfp4_cpos add r 8 c n 4 exp |
| 4952 | bad = add bad 1 |
| 4953 | end |
| 4954 | if ne f64 bitcast f32 uint mapget h ggnvfp4_cpos add r 8 add c 1 n 4 exp |
| 4955 | bad = add bad 1 |
| 4956 | end |
| 4957 | ret bad |
| 4958 | end |
| 4959 | |
| 4960 | fn ggnvfp4_poison m ulong n ulong to void |
| 4961 | h = get gbmem 5 |
| 4962 | rb1 = sub m 128 |
| 4963 | cb1 = sub n 128 |
| 4964 | for k ulong 8 |
| 4965 | dr = ggnvfp4_dr k |
| 4966 | dc = ggnvfp4_dc k |
| 4967 | ggnvfp4_p1 h dr dc n |
| 4968 | ggnvfp4_p1 h add rb1 dr add cb1 dc n |
| 4969 | end |
| 4970 | r = mapset h ggnvfp4_cpos 0 8 n 4 2143289344 |
| 4971 | r = mapset h ggnvfp4_cpos 0 9 n 4 2143289344 |
| 4972 | r = mapset h ggnvfp4_cpos 96 72 n 4 2143289344 |
| 4973 | r = mapset h ggnvfp4_cpos 96 73 n 4 2143289344 |
| 4974 | put gkinv 0 1 |
| 4975 | end |
| 4976 | |
| 4977 | fn ggnvfp4_chk m ulong n ulong exp f64 to ulong |
| 4978 | h = get gbmem 5 |
| 4979 | rb1 = sub m 128 |
| 4980 | cb1 = sub n 128 |
| 4981 | bad = ulong 0 |
| 4982 | for k ulong 8 |
| 4983 | dr = ggnvfp4_dr k |
| 4984 | dc = ggnvfp4_dc k |
| 4985 | bad = add bad ggnvfp4_c1 h dr dc n exp |
| 4986 | bad = add bad ggnvfp4_c1 h add rb1 dr add cb1 dc n exp |
| 4987 | end |
| 4988 | if ne uint mapget h ggnvfp4_cpos 0 8 n 4 2143289344 |
| 4989 | bad = add bad 1 |
| 4990 | end |
| 4991 | if ne uint mapget h ggnvfp4_cpos 0 9 n 4 2143289344 |
| 4992 | bad = add bad 1 |
| 4993 | end |
| 4994 | if ne uint mapget h ggnvfp4_cpos 96 72 n 4 2143289344 |
| 4995 | bad = add bad 1 |
| 4996 | end |
| 4997 | if ne uint mapget h ggnvfp4_cpos 96 73 n 4 2143289344 |
| 4998 | bad = add bad 1 |
| 4999 | end |
| 5000 | ret bad |
| 5001 | end |
| 5002 | |
| 5003 | fn ggnvfp4_pr m ulong n ulong k ulong it ulong ns long to void |
| 5004 | got = f64 bitcast f32 uint mapget get gbmem 5 65536 4 |
| 5005 | exp = f64 k |
| 5006 | bad = ggnvfp4_chk m n exp |
| 5007 | flops = mul mul 2.0f64 f64 mul m n mul f64 k f64 it |
| 5008 | tf = div flops mul f64 ns 1000.0f64 |
| 5009 | pct = div mul tf 100.0f64 500.0f64 |
| 5010 | oputc 78 |
| 5011 | oputc 86 |
| 5012 | oputc 70 |
| 5013 | oputc 80 |
| 5014 | oputc 52 |
| 5015 | oputc 32 |
| 5016 | oputu m |
| 5017 | oputc 32 |
| 5018 | oputu n |
| 5019 | oputc 32 |
| 5020 | oputu k |
| 5021 | oputc 32 |
| 5022 | oputu it |
| 5023 | oputc 32 |
| 5024 | oputl ns |
| 5025 | oputc 32 |
| 5026 | oputl long mul tf 100.0f64 |
| 5027 | oputc 32 |
| 5028 | oputl long mul pct 100.0f64 |
| 5029 | oputc 32 |
| 5030 | oputl long exp |
| 5031 | oputc 32 |
| 5032 | oputl long got |
| 5033 | oputc 32 |
| 5034 | oputu 68 |
| 5035 | oputc 32 |
| 5036 | oputu bad |
| 5037 | oputc 32 |
| 5038 | oputc 73 |
| 5039 | oputc 78 |
| 5040 | oputc 82 |
| 5041 | oputc 69 |
| 5042 | oputc 71 |
| 5043 | oputc 95 |
| 5044 | oputc 79 |
| 5045 | oputc 77 |
| 5046 | oputc 77 |
| 5047 | oputc 65 |
| 5048 | oputc 95 |
| 5049 | oputc 77 |
| 5050 | oputc 73 |
| 5051 | oputc 67 |
| 5052 | oputc 82 |
| 5053 | oputc 79 |
| 5054 | oputc 66 |
| 5055 | oputc 69 |
| 5056 | oputc 78 |
| 5057 | oputc 67 |
| 5058 | oputc 72 |
| 5059 | oputc 32 |
| 5060 | if eq bad 0 |
| 5061 | oputc 80 |
| 5062 | oputc 65 |
| 5063 | oputc 83 |
| 5064 | oputc 83 |
| 5065 | oputc 10 |
| 5066 | oflush |
| 5067 | r = nvstop |
| 5068 | ret |
| 5069 | end |
| 5070 | oputc 70 |
| 5071 | oputc 65 |
| 5072 | oputc 73 |
| 5073 | oputc 76 |
| 5074 | oputc 10 |
| 5075 | oflush |
| 5076 | r = nvstop |
| 5077 | exit 51 |
| 5078 | end |
| 5079 | |
| 5080 | fn ggnvfp4_fill to void |
| 5081 | h = get gbmem 5 |
| 5082 | for i ulong 2048 |
| 5083 | r = mapset h mul i 4 4 572662306 |
| 5084 | r = mapset h add 8192 mul i 4 4 572662306 |
| 5085 | r = mapset h add 16384 mul i 4 4 943208504 |
| 5086 | r = mapset h add 24576 mul i 4 4 943208504 |
| 5087 | end |
| 5088 | put gkinv 0 1 |
| 5089 | end |
| 5090 | |
| 5091 | fn ggnvfp4 to void |
| 5092 | if ne oargc 6 |
| 5093 | exit 2 |
| 5094 | end |
| 5095 | m = oargu 2 |
| 5096 | n = oargu 3 |
| 5097 | k = oargu 4 |
| 5098 | it = oargu 5 |
| 5099 | if or eq m 0 eq n 0 |
| 5100 | exit 2 |
| 5101 | end |
| 5102 | if or eq k 0 eq it 0 |
| 5103 | exit 2 |
| 5104 | end |
| 5105 | if or gt m 65536 gt n 65536 |
| 5106 | exit 2 |
| 5107 | end |
| 5108 | if gt mul m n 1073741824 |
| 5109 | exit 2 |
| 5110 | end |
| 5111 | if or gt k 16777216 gt it 1000000 |
| 5112 | exit 2 |
| 5113 | end |
| 5114 | if or ne rem m 128 0 ne rem n 128 0 |
| 5115 | exit 2 |
| 5116 | end |
| 5117 | if ne rem k 64 0 |
| 5118 | exit 2 |
| 5119 | end |
| 5120 | grset 256 32 64 2 128 2 |
| 5121 | if not gropen add mul mul m n 4 1048576 |
| 5122 | ggsetupfail |
| 5123 | end |
| 5124 | ggnvfp4_fill |
| 5125 | ggnvfp4_lch m n k |
| 5126 | ggnvfp4_poison m n |
| 5127 | t0 = monons |
| 5128 | for q ulong it bound 1000001 |
| 5129 | ggnvfp4_lch m n k |
| 5130 | end |
| 5131 | ns = sub monons t0 |
| 5132 | ggnvfp4_pr m n k it ns |
| 5133 | end |
| 5134 | |
| 5135 | text ggnv4sp_shm "/tmp/omega_vfio_kern.shm" |
| 5136 | const GGNV4SP_MAGIC ulong 5714301938159472177 |
| 5137 | const GGNV4SP_KSHM ulong 2097152 |
| 5138 | global ggnv4sp_st ulong 6 |
| 5139 | global ggnv4sp_in ulong 9 |
| 5140 | global ggnv4sp_pm ulong 8 |
| 5141 | global ggnv4sp_fp f64 4 |
| 5142 | |
| 5143 | fn ggnv4sp_open to long |
| 5144 | fd = int fopen ggnv4sp_shm 0 len ggnv4sp_shm 2 0 |
| 5145 | if lt fd 0 |
| 5146 | ret neg long 1 |
| 5147 | end |
| 5148 | ksh = mapnew fd 0 GGNV4SP_KSHM 3 1 |
| 5149 | sc = fclose fd |
| 5150 | if le ksh 0 |
| 5151 | ret neg long 2 |
| 5152 | end |
| 5153 | if ne mapget ksh 0 8 GGNV4SP_MAGIC |
| 5154 | md = mapdrop ksh |
| 5155 | ret neg long 3 |
| 5156 | end |
| 5157 | ret ksh |
| 5158 | end |
| 5159 | |
| 5160 | fn ggnv4sp_upcode ksh long to ulong |
| 5161 | grset 256 32 64 2 128 2 |
| 5162 | gkslot 96 |
| 5163 | grnvfp4sp |
| 5164 | base = mul ulong 96 16384 |
| 5165 | raw = sub get gkat 0 base |
| 5166 | csz = mul div add raw 15 16 16 |
| 5167 | if lt csz 512 |
| 5168 | csz = 512 |
| 5169 | end |
| 5170 | nw = div csz 4 |
| 5171 | for i ulong nw bound 8192 |
| 5172 | o = mul i 4 |
| 5173 | w = ulong get gkcode add base o |
| 5174 | w = or w shl ulong get gkcode add base add o 1 8 |
| 5175 | w = or w shl ulong get gkcode add base add o 2 16 |
| 5176 | w = or w shl ulong get gkcode add base add o 3 24 |
| 5177 | r = mapset ksh add 4096 o 4 w |
| 5178 | end |
| 5179 | ret csz |
| 5180 | end |
| 5181 | |
| 5182 | fn ggnv4sp_fill_in ksh long to void |
| 5183 | rw = get ggnv4sp_in 0 |
| 5184 | khi = get ggnv4sp_in 1 |
| 5185 | klo = get ggnv4sp_in 2 |
| 5186 | aw = get ggnv4sp_in 3 |
| 5187 | bw = get ggnv4sp_in 4 |
| 5188 | ew = get ggnv4sp_in 5 |
| 5189 | r = mapset ksh 65536 4 rw |
| 5190 | r = mapset ksh 65540 4 khi |
| 5191 | r = mapset ksh 65544 4 klo |
| 5192 | for i ulong 128 |
| 5193 | r = mapset ksh add 65600 mul i 4 4 aw |
| 5194 | r = mapset ksh add 66112 mul i 4 4 bw |
| 5195 | end |
| 5196 | for i ulong 32 |
| 5197 | r = mapset ksh add 66624 mul i 4 4 ew |
| 5198 | r = mapset ksh add 66752 mul i 4 4 943208504 |
| 5199 | r = mapset ksh add 66880 mul i 4 4 943208504 |
| 5200 | end |
| 5201 | end |
| 5202 | |
| 5203 | fn ggnv4sp_fire ksh long csz ulong to long |
| 5204 | cur = mapget ksh 8 8 |
| 5205 | nxt = add cur 1 |
| 5206 | r = mapset ksh 24 8 csz |
| 5207 | r = mapset ksh 32 8 6144 |
| 5208 | r = mapset ksh 40 8 128 |
| 5209 | r = mapset ksh 48 8 64 |
| 5210 | r = mapset ksh 56 8 4096 |
| 5211 | r = mapset ksh 64 8 8192 |
| 5212 | cpufence |
| 5213 | r = mapset ksh 8 8 nxt |
| 5214 | cpufence |
| 5215 | t0 = monons |
| 5216 | done = false |
| 5217 | for p ulong 200000000 |
| 5218 | ak = mapget ksh 16 8 |
| 5219 | if eq ak nxt |
| 5220 | done = true |
| 5221 | break |
| 5222 | end |
| 5223 | if gt sub monons t0 long 6000000000 |
| 5224 | break |
| 5225 | end |
| 5226 | cpurelax |
| 5227 | end |
| 5228 | if not done |
| 5229 | ret neg long 70 |
| 5230 | end |
| 5231 | put ggnv4sp_st 0 mapget ksh 72 8 |
| 5232 | put ggnv4sp_st 1 mapget ksh 80 8 |
| 5233 | put ggnv4sp_st 2 mapget ksh 88 8 |
| 5234 | ret long 0 |
| 5235 | end |
| 5236 | |
| 5237 | fn ggnv4sp_chk ksh long rw ulong ehi f64 elo f64 to ulong |
| 5238 | bad = ulong 0 |
| 5239 | for w ulong 192 |
| 5240 | want = elo |
| 5241 | if lt w rw |
| 5242 | want = ehi |
| 5243 | end |
| 5244 | bo = add 589824 mul w 32 |
| 5245 | for j ulong 8 |
| 5246 | v = f64 bitcast f32 uint mapget ksh add bo mul j 4 4 |
| 5247 | if ne v want |
| 5248 | bad = add bad 1 |
| 5249 | end |
| 5250 | end |
| 5251 | end |
| 5252 | for s ulong 16 |
| 5253 | if ne mapget ksh add 595968 mul s 4 4 0 |
| 5254 | bad = add bad 1 |
| 5255 | end |
| 5256 | end |
| 5257 | ret bad |
| 5258 | end |
| 5259 | |
| 5260 | fn ggnv4sp_ctrls ksh long csz ulong to ulong |
| 5261 | put ggnv4sp_in 6 get ggnv4sp_in 0 |
| 5262 | put ggnv4sp_in 7 get ggnv4sp_in 1 |
| 5263 | put ggnv4sp_in 8 get ggnv4sp_in 2 |
| 5264 | bad = ulong 0 |
| 5265 | note Control 1: invalid csz=100 MUST be rejected with status=22 |
| 5266 | rc = ggnv4sp_fire ksh 100 |
| 5267 | if or ne rc 0 ne get ggnv4sp_st 1 22 |
| 5268 | bad = add bad 1 |
| 5269 | end |
| 5270 | note Control 2: zero-operand A=0 MUST produce exact 0.0 across all 192 warps |
| 5271 | put ggnv4sp_in 0 0 |
| 5272 | put ggnv4sp_in 1 1 |
| 5273 | put ggnv4sp_in 2 1 |
| 5274 | put ggnv4sp_in 3 0 |
| 5275 | put ggnv4sp_in 4 572662306 |
| 5276 | put ggnv4sp_in 5 1145324612 |
| 5277 | ggnv4sp_fill_in ksh |
| 5278 | rc = ggnv4sp_fire ksh csz |
| 5279 | if or ne rc 0 or ne get ggnv4sp_st 1 0 ne get ggnv4sp_st 2 0 |
| 5280 | bad = add bad 1 |
| 5281 | else |
| 5282 | bad = add bad ggnv4sp_chk ksh 0 0.0f64 0.0f64 |
| 5283 | end |
| 5284 | note Control 3: full-grid E2M1 Tensor Core linearity (B=1.0 -> 64.0 vs B=2.0 -> 128.0 across 1552 lanes) |
| 5285 | put ggnv4sp_in 3 572662306 |
| 5286 | put ggnv4sp_in 4 572662306 |
| 5287 | put ggnv4sp_in 5 1145324612 |
| 5288 | ggnv4sp_fill_in ksh |
| 5289 | rc = ggnv4sp_fire ksh csz |
| 5290 | ma = f64 0.0f64 |
| 5291 | if and eq rc 0 and eq get ggnv4sp_st 1 0 eq get ggnv4sp_st 2 0 |
| 5292 | bad = add bad ggnv4sp_chk ksh 0 64.0f64 64.0f64 |
| 5293 | ma = f64 bitcast f32 uint mapget ksh 589824 4 |
| 5294 | else |
| 5295 | bad = add bad 1 |
| 5296 | end |
| 5297 | put ggnv4sp_in 4 1145324612 |
| 5298 | ggnv4sp_fill_in ksh |
| 5299 | rc = ggnv4sp_fire ksh csz |
| 5300 | mb = f64 0.0f64 |
| 5301 | if and eq rc 0 and eq get ggnv4sp_st 1 0 eq get ggnv4sp_st 2 0 |
| 5302 | bad = add bad ggnv4sp_chk ksh 0 128.0f64 128.0f64 |
| 5303 | mb = f64 bitcast f32 uint mapget ksh 589824 4 |
| 5304 | else |
| 5305 | bad = add bad 1 |
| 5306 | end |
| 5307 | if or ne ma 64.0f64 ne mb 128.0f64 |
| 5308 | bad = add bad 1 |
| 5309 | end |
| 5310 | put ggnv4sp_in 0 get ggnv4sp_in 6 |
| 5311 | put ggnv4sp_in 1 get ggnv4sp_in 7 |
| 5312 | put ggnv4sp_in 2 get ggnv4sp_in 8 |
| 5313 | put ggnv4sp_st 3 ulong long ma |
| 5314 | put ggnv4sp_st 4 ulong long mb |
| 5315 | ret bad |
| 5316 | end |
| 5317 | |
| 5318 | fn ggnv4sp_pr_tag to void |
| 5319 | oputc 78 |
| 5320 | oputc 86 |
| 5321 | oputc 70 |
| 5322 | oputc 80 |
| 5323 | oputc 52 |
| 5324 | oputc 83 |
| 5325 | oputc 80 |
| 5326 | oputc 32 |
| 5327 | end |
| 5328 | |
| 5329 | fn ggnv4sp_pr_scope to void |
| 5330 | oputc 73 |
| 5331 | oputc 78 |
| 5332 | oputc 82 |
| 5333 | oputc 69 |
| 5334 | oputc 71 |
| 5335 | oputc 95 |
| 5336 | oputc 79 |
| 5337 | oputc 77 |
| 5338 | oputc 77 |
| 5339 | oputc 65 |
| 5340 | oputc 95 |
| 5341 | oputc 83 |
| 5342 | oputc 80 |
| 5343 | oputc 95 |
| 5344 | oputc 49 |
| 5345 | oputc 54 |
| 5346 | oputc 56 |
| 5347 | oputc 49 |
| 5348 | oputc 50 |
| 5349 | oputc 56 |
| 5350 | oputc 32 |
| 5351 | end |
| 5352 | |
| 5353 | fn ggnv4sp_pr to void |
| 5354 | flops = mul mul 2.0f64 f64 mul get ggnv4sp_pm 0 get ggnv4sp_pm 1 mul f64 get ggnv4sp_pm 2 f64 get ggnv4sp_pm 3 |
| 5355 | tf = div flops mul f64 get ggnv4sp_pm 4 1000.0f64 |
| 5356 | pct = div mul tf 100.0f64 1000.0f64 |
| 5357 | ggnv4sp_pr_tag |
| 5358 | oputu get ggnv4sp_pm 0 |
| 5359 | oputc 32 |
| 5360 | oputu get ggnv4sp_pm 1 |
| 5361 | oputc 32 |
| 5362 | oputu get ggnv4sp_pm 2 |
| 5363 | oputc 32 |
| 5364 | oputu get ggnv4sp_pm 3 |
| 5365 | oputc 32 |
| 5366 | oputu get ggnv4sp_pm 4 |
| 5367 | oputc 32 |
| 5368 | oputu get ggnv4sp_pm 5 |
| 5369 | oputc 32 |
| 5370 | oputl long mul tf 100.0f64 |
| 5371 | oputc 32 |
| 5372 | oputl long mul pct 100.0f64 |
| 5373 | oputc 32 |
| 5374 | oputl long get ggnv4sp_fp 0 |
| 5375 | oputc 32 |
| 5376 | oputl long get ggnv4sp_fp 1 |
| 5377 | oputc 32 |
| 5378 | oputl long get ggnv4sp_fp 2 |
| 5379 | oputc 32 |
| 5380 | oputl long get ggnv4sp_fp 3 |
| 5381 | oputc 32 |
| 5382 | oputu 1552 |
| 5383 | oputc 32 |
| 5384 | oputu get ggnv4sp_pm 6 |
| 5385 | oputc 32 |
| 5386 | oputu get ggnv4sp_st 3 |
| 5387 | oputc 32 |
| 5388 | oputu get ggnv4sp_st 4 |
| 5389 | oputc 32 |
| 5390 | ggnv4sp_pr_scope |
| 5391 | if eq get ggnv4sp_pm 6 0 |
| 5392 | oputc 80 |
| 5393 | oputc 65 |
| 5394 | oputc 83 |
| 5395 | oputc 83 |
| 5396 | oputc 10 |
| 5397 | oflush |
| 5398 | ret |
| 5399 | end |
| 5400 | oputc 70 |
| 5401 | oputc 65 |
| 5402 | oputc 73 |
| 5403 | oputc 76 |
| 5404 | oputc 10 |
| 5405 | oflush |
| 5406 | exit 51 |
| 5407 | end |
| 5408 | |
| 5409 | fn ggnv4sp_ok rc long to ulong |
| 5410 | if ne rc 0 |
| 5411 | ret 1 |
| 5412 | end |
| 5413 | if ne get ggnv4sp_st 1 0 |
| 5414 | ret 1 |
| 5415 | end |
| 5416 | if ne get ggnv4sp_st 2 0 |
| 5417 | ret 1 |
| 5418 | end |
| 5419 | ret 0 |
| 5420 | end |
| 5421 | |
| 5422 | fn ggnv4sp_vfio ksh long to void |
| 5423 | csz = ggnv4sp_upcode ksh |
| 5424 | bad = ggnv4sp_ctrls ksh csz |
| 5425 | if ne bad 0 |
| 5426 | md = mapdrop ksh |
| 5427 | put ggnv4sp_pm 4 1 |
| 5428 | put ggnv4sp_pm 5 1 |
| 5429 | put ggnv4sp_pm 6 bad |
| 5430 | ggnv4sp_pr |
| 5431 | ret |
| 5432 | end |
| 5433 | put ggnv4sp_in 3 572662306 |
| 5434 | put ggnv4sp_in 4 572662306 |
| 5435 | put ggnv4sp_in 5 1145324612 |
| 5436 | ggnv4sp_fill_in ksh |
| 5437 | bad = add bad ggnv4sp_ok ggnv4sp_fire ksh csz |
| 5438 | if ne bad 0 |
| 5439 | md = mapdrop ksh |
| 5440 | put ggnv4sp_pm 4 1 |
| 5441 | put ggnv4sp_pm 5 1 |
| 5442 | put ggnv4sp_pm 6 bad |
| 5443 | ggnv4sp_pr |
| 5444 | ret |
| 5445 | end |
| 5446 | cns = ulong 0 |
| 5447 | t0 = monons |
| 5448 | for q ulong get ggnv4sp_pm 3 bound 1000001 |
| 5449 | bad = add bad ggnv4sp_ok ggnv4sp_fire ksh csz |
| 5450 | cns = add cns get ggnv4sp_st 0 |
| 5451 | end |
| 5452 | put ggnv4sp_pm 5 ulong sub monons t0 |
| 5453 | put ggnv4sp_pm 4 cns |
| 5454 | rw = get ggnv4sp_in 0 |
| 5455 | bad = add bad ggnv4sp_chk ksh rw get ggnv4sp_fp 0 get ggnv4sp_fp 2 |
| 5456 | put ggnv4sp_pm 6 bad |
| 5457 | put ggnv4sp_fp 1 f64 bitcast f32 uint mapget ksh 589824 4 |
| 5458 | put ggnv4sp_fp 3 f64 bitcast f32 uint mapget ksh add 589824 mul rw 32 4 |
| 5459 | md = mapdrop ksh |
| 5460 | ggnv4sp_pr |
| 5461 | end |
| 5462 | |
| 5463 | fn ggnv4sp_rm to void |
| 5464 | grset 256 32 64 2 128 2 |
| 5465 | if not gropen 65536 |
| 5466 | ggsetupfail |
| 5467 | end |
| 5468 | h = get gbmem 5 |
| 5469 | base = gkbase |
| 5470 | r = mapset h 0 4 get ggnv4sp_in 0 |
| 5471 | r = mapset h 4 4 get ggnv4sp_in 1 |
| 5472 | r = mapset h 8 4 get ggnv4sp_in 2 |
| 5473 | for i ulong 128 |
| 5474 | r = mapset h add 64 mul i 4 4 572662306 |
| 5475 | r = mapset h add 576 mul i 4 4 572662306 |
| 5476 | end |
| 5477 | for i ulong 32 |
| 5478 | r = mapset h add 1088 mul i 4 4 1145324612 |
| 5479 | r = mapset h add 1216 mul i 4 4 943208504 |
| 5480 | r = mapset h add 1344 mul i 4 4 943208504 |
| 5481 | end |
| 5482 | put gkinv 0 1 |
| 5483 | ggpt 0 base |
| 5484 | ggpt 1 add base 8192 |
| 5485 | ggrun 96 4 48 1 1 |
| 5486 | t0 = monons |
| 5487 | for q ulong get ggnv4sp_pm 3 bound 1000001 |
| 5488 | ggpt 0 base |
| 5489 | ggpt 1 add base 8192 |
| 5490 | ggrun 96 4 48 1 1 |
| 5491 | end |
| 5492 | wns = ulong sub monons t0 |
| 5493 | rw = get ggnv4sp_in 0 |
| 5494 | put ggnv4sp_fp 1 f64 bitcast f32 uint mapget h 8192 4 |
| 5495 | put ggnv4sp_fp 3 f64 bitcast f32 uint mapget h add 8192 mul rw 32 4 |
| 5496 | bad = ulong 0 |
| 5497 | for w ulong 192 |
| 5498 | want = get ggnv4sp_fp 2 |
| 5499 | if lt w rw |
| 5500 | want = get ggnv4sp_fp 0 |
| 5501 | end |
| 5502 | bo = add 8192 mul w 32 |
| 5503 | for j ulong 8 |
| 5504 | if ne f64 bitcast f32 uint mapget h add bo mul j 4 4 want |
| 5505 | bad = add bad 1 |
| 5506 | end |
| 5507 | end |
| 5508 | end |
| 5509 | put ggnv4sp_st 3 0 |
| 5510 | put ggnv4sp_st 4 0 |
| 5511 | put ggnv4sp_pm 4 wns |
| 5512 | put ggnv4sp_pm 5 wns |
| 5513 | put ggnv4sp_pm 6 bad |
| 5514 | rv = nvstop |
| 5515 | ggnv4sp_pr |
| 5516 | end |
| 5517 | |
| 5518 | fn ggnvfp4sp to void |
| 5519 | if ne oargc 6 |
| 5520 | exit 2 |
| 5521 | end |
| 5522 | m = oargu 2 |
| 5523 | n = oargu 3 |
| 5524 | k = oargu 4 |
| 5525 | it = oargu 5 |
| 5526 | if or eq m 0 eq n 0 |
| 5527 | exit 2 |
| 5528 | end |
| 5529 | if or eq k 0 eq it 0 |
| 5530 | exit 2 |
| 5531 | end |
| 5532 | if or gt m 65536 gt n 65536 |
| 5533 | exit 2 |
| 5534 | end |
| 5535 | if or gt k 16777216 gt it 1000000 |
| 5536 | exit 2 |
| 5537 | end |
| 5538 | if ne rem m 128 0 |
| 5539 | exit 2 |
| 5540 | end |
| 5541 | if ne rem n 128 0 |
| 5542 | exit 2 |
| 5543 | end |
| 5544 | if ne rem k 128 0 |
| 5545 | exit 2 |
| 5546 | end |
| 5547 | nstr = mul div m 16 div n 64 |
| 5548 | bstr = div nstr 192 |
| 5549 | rw = rem nstr 192 |
| 5550 | kt = div k 128 |
| 5551 | khi = mul add bstr 1 kt |
| 5552 | klo = mul bstr kt |
| 5553 | put ggnv4sp_pm 0 m |
| 5554 | put ggnv4sp_pm 1 n |
| 5555 | put ggnv4sp_pm 2 k |
| 5556 | put ggnv4sp_pm 3 it |
| 5557 | put ggnv4sp_in 0 rw |
| 5558 | put ggnv4sp_in 1 khi |
| 5559 | put ggnv4sp_in 2 klo |
| 5560 | put ggnv4sp_fp 0 mul f64 khi 64.0f64 |
| 5561 | put ggnv4sp_fp 2 mul f64 klo 64.0f64 |
| 5562 | ksh = ggnv4sp_open |
| 5563 | if gt ksh 0 |
| 5564 | ggnv4sp_vfio ksh |
| 5565 | ret |
| 5566 | end |
| 5567 | ggnv4sp_rm |
| 5568 | end |
| 5569 | |
| 5570 | fn ogcmd w ulong to bool |
| 5571 | if eq w 27696107117441895 |
| 5572 | put bfdec 0 1 |
| 5573 | put ogpu 0 1 |
| 5574 | put og 61 1 |
| 5575 | cmdserve |
| 5576 | r = nvstop |
| 5577 | ret true |
| 5578 | end |
| 5579 | if eq w 108235010962279 |
| 5580 | put bfdec 0 1 |
| 5581 | put ogpu 0 1 |
| 5582 | put og 61 1 |
| 5583 | cmdsamp |
| 5584 | r = nvstop |
| 5585 | ret true |
| 5586 | end |
| 5587 | if eq w 111559399797607 |
| 5588 | put ogpu 0 1 |
| 5589 | put og 61 1 |
| 5590 | cmdserve |
| 5591 | r = nvstop |
| 5592 | ret true |
| 5593 | end |
| 5594 | if eq w 28548202775016295 |
| 5595 | put ogpu 0 1 |
| 5596 | put og 61 1 |
| 5597 | cmdsamp |
| 5598 | r = nvstop |
| 5599 | ret true |
| 5600 | end |
| 5601 | if eq w 482871571303 |
| 5602 | ggscmp |
| 5603 | ret true |
| 5604 | end |
| 5605 | if eq w 29382723376407655 |
| 5606 | ggdbench |
| 5607 | ret true |
| 5608 | end |
| 5609 | if eq w 1886217063 |
| 5610 | ggcmp |
| 5611 | ret true |
| 5612 | end |
| 5613 | if eq w 216634581863 |
| 5614 | ggcmp2 |
| 5615 | ret true |
| 5616 | end |
| 5617 | if eq w 121398885512295 |
| 5618 | ggtrain |
| 5619 | ret true |
| 5620 | end |
| 5621 | if eq w 28831846509999207 |
| 5622 | ggtrainf |
| 5623 | ret true |
| 5624 | end |
| 5625 | if eq w 482871566695 |
| 5626 | ggacmp |
| 5627 | ret true |
| 5628 | end |
| 5629 | if eq w 465491617127 |
| 5630 | ggeval |
| 5631 | ret true |
| 5632 | end |
| 5633 | if eq w 444015928423 |
| 5634 | ggdiag |
| 5635 | ret true |
| 5636 | end |
| 5637 | if eq w 439956435047 |
| 5638 | ggprof |
| 5639 | ret true |
| 5640 | end |
| 5641 | if eq w 27705946603156583 |
| 5642 | gbtrain |
| 5643 | ret true |
| 5644 | end |
| 5645 | if eq w 108192095957095 |
| 5646 | gbprof |
| 5647 | ret true |
| 5648 | end |
| 5649 | if eq w 482871566951 |
| 5650 | gbcmp |
| 5651 | ret true |
| 5652 | end |
| 5653 | if eq w 55458452955751 |
| 5654 | gbcmp2 |
| 5655 | ret true |
| 5656 | end |
| 5657 | if eq w 29328856030863975 |
| 5658 | gbcmp2h |
| 5659 | ret true |
| 5660 | end |
| 5661 | if eq w 113668228801127 |
| 5662 | gbfrag |
| 5663 | ret true |
| 5664 | end |
| 5665 | if eq w 418346528103 |
| 5666 | gm_r = gm_selftest |
| 5667 | if ne gm_r 0 |
| 5668 | exit gm_r |
| 5669 | end |
| 5670 | ret true |
| 5671 | end |
| 5672 | if eq w 474365654119 |
| 5673 | gt_r = gt_selftest |
| 5674 | if ne gt_r 0 |
| 5675 | exit gt_r |
| 5676 | end |
| 5677 | ret true |
| 5678 | end |
| 5679 | if eq w 28543748692731495 |
| 5680 | ggfcache |
| 5681 | ret true |
| 5682 | end |
| 5683 | if eq w 225224062574 |
| 5684 | ggnvfp4 |
| 5685 | ret true |
| 5686 | end |
| 5687 | if eq w 31651866452850286 |
| 5688 | ggnvfp4sp |
| 5689 | ret true |
| 5690 | end |
| 5691 | if eq w 14758097674466407 |
| 5692 | gbtrain4 |
| 5693 | ret true |
| 5694 | end |
| 5695 | if eq w 7885070016531820135 |
| 5696 | gnv4gemm |
| 5697 | ret true |
| 5698 | end |
| 5699 | if eq w 31645470240960103 |
| 5700 | gnv4cmp |
| 5701 | ret true |
| 5702 | end |
| 5703 | ret false |
| 5704 | end |