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