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 / gr.om
| 1 | note SPDX-License-Identifier: AGPL-3.0-only |
| 2 | note gr.om: row and elementwise SM121 kernels for the ollm GPU step (slots 9..24), emitted after |
| 3 | note gkall with the model dimensions baked in as immediates. Words verified with nvdisasm -b SM121. |
| 4 | note Launch shape: 64 threads per block, R4 = ctaid.x 64 + tid.x, R3 = ctaid.y, R7 = ctaid.z; |
| 5 | note arg 0 is the R4 bound (threads at or past it exit). Registers: R8-R23 pointers and ints, |
| 6 | note R24-R29 constants, R26 = 1, R30 loop counter, R31 = -1, R32-R63 data, R64-R69 scalars. |
| 7 | global grd ulong 8 |
| 8 | note grd: 0 V 1 T 2 D 3 H 4 F 5 B |
| 9 | fn grf v f64 to uint |
| 10 | ret bitcast uint f32 v |
| 11 | end |
| 12 | fn grarg i uint to uint |
| 13 | ret add 896 mul i 4 |
| 14 | end |
| 15 | fn grfsetp p uint a uint b uint cc uint to void |
| 16 | gkpr or 29195 shl a 24 b or or 66060288 shl cc 12 shl p 17 |
| 17 | end |
| 18 | fn grfsel d uint a uint b uint p uint to void |
| 19 | gkfx or or 29192 shl d 16 shl a 24 b shl p 23 |
| 20 | end |
| 21 | fn grfmax d uint a uint b uint to void |
| 22 | gkfx or or 29193 shl d 16 shl a 24 b 125829120 |
| 23 | end |
| 24 | fn grfadd d uint a uint b uint to void |
| 25 | gkfx or or 29217 shl d 16 shl a 24 b 0 |
| 26 | end |
| 27 | fn grfsub d uint a uint b uint to void |
| 28 | gkfx or or 29217 shl d 16 shl a 24 or b 2147483648 0 |
| 29 | end |
| 30 | fn grmufu d uint a uint op uint to void |
| 31 | gkvr or 29448 shl d 16 a op |
| 32 | end |
| 33 | note pointer number i (odd) sits in arg slots i+1, i+2 so its LDC.64 is 8-byte aligned |
| 34 | fn grptr d uint i uint idx uint scale uint to void |
| 35 | gkldc64 d grarg add i 1 |
| 36 | gkimadw d idx scale d |
| 37 | end |
| 38 | fn grtop c uint n uint to ulong |
| 39 | gkmovi c n |
| 40 | ret div get gkat 0 16 |
| 41 | end |
| 42 | fn grbot c uint top ulong to void |
| 43 | gkiadd c c 31 |
| 44 | gkisge 0 c 1 |
| 45 | gkbra 0 top |
| 46 | end |
| 47 | fn gradv p uint n uint to void |
| 48 | gkimadw p 26 n p |
| 49 | end |
| 50 | fn grld8 d uint p uint first bool to void |
| 51 | for u uint 8 |
| 52 | gkldg add d u p mul u 4 or ne u 0 not first |
| 53 | end |
| 54 | end |
| 55 | fn grst8 p uint s uint to void |
| 56 | for u uint 8 |
| 57 | gkstg p add s u mul u 4 ne u 0 |
| 58 | end |
| 59 | end |
| 60 | fn grpro to void |
| 61 | gkldc 1 892 false |
| 62 | gks2r 0 33 true |
| 63 | gks2r 2 37 true |
| 64 | gks2r 3 38 true |
| 65 | gks2r 7 39 true |
| 66 | gkq 4278482860 27392 134220288 or gkcw 4 2 7 get gkpend 0 8192 |
| 67 | put gkpend 0 4 |
| 68 | gkimadi 4 2 64 0 |
| 69 | gkldc 5 896 false |
| 70 | gkisetp 0 4 5 6 |
| 71 | gkexitp 0 |
| 72 | gkmovi 31 4294967295 |
| 73 | gkmovi 26 1 |
| 74 | end |
| 75 | note 9 EMB: out[b T + t][i] = wte[x[b T + t]][i] + wpe[t][i]; grid (D/64, T, B); args D out wte wpe x |
| 76 | fn gremb to void |
| 77 | d = uint get grd 2 |
| 78 | t = uint get grd 1 |
| 79 | grpro |
| 80 | gkimadi 8 7 t 3 |
| 81 | grptr 12 7 8 4 |
| 82 | gkldg 9 12 0 false |
| 83 | gkimadi 10 9 d 4 |
| 84 | grptr 14 3 10 4 |
| 85 | gkimadi 11 3 d 4 |
| 86 | grptr 16 5 11 4 |
| 87 | gkimadi 20 8 d 4 |
| 88 | grptr 18 1 20 4 |
| 89 | gkldg 32 14 0 false |
| 90 | gkldg 33 16 0 true |
| 91 | grfadd 34 32 33 |
| 92 | gkstg 18 34 0 false |
| 93 | gkexit |
| 94 | end |
| 95 | note RMS kernels: one warp per row (threads R*32, arg 0); lane l holds columns l + 32 u, u < D/32, |
| 96 | note so each warp load and store is one coalesced 128-byte line and every row is read once. |
| 97 | note R8 = row, R9 = lane; ld/st w: n words at p + 128 u (gkldg/gkstg chain flag: first false). |
| 98 | fn grrrow to void |
| 99 | grpro |
| 100 | gkshr 8 4 5 |
| 101 | gkandi 9 4 31 |
| 102 | end |
| 103 | fn grrptr d uint i uint d4 uint to void |
| 104 | grptr d i 8 d4 |
| 105 | gkimadw d 9 4 d |
| 106 | end |
| 107 | fn grldw d uint p uint n uint first bool to void |
| 108 | for u uint n |
| 109 | gkldg add d u p mul u 128 or ne u 0 not first |
| 110 | end |
| 111 | end |
| 112 | fn grstw p uint s uint n uint to void |
| 113 | for u uint n |
| 114 | gkstg p add s u mul u 128 ne u 0 |
| 115 | end |
| 116 | end |
| 117 | note RMS register bank k (k < 4) of n = D/32 words: R32 + 8 k while n <= 8 (the v1 layout, scalars at |
| 118 | note R64..R67 above it); from n = 9 the banks sit above the scalars at R72 + n k, so D <= 384 (n <= 12) |
| 119 | note ends at R119 and grall raises the slot's GPR count to 128 (gk.om: highest GPR + 3, multiple of 8). |
| 120 | fn grrb k uint n uint to uint |
| 121 | if le n 8 |
| 122 | ret add 32 mul 8 k |
| 123 | end |
| 124 | ret add 72 mul n k |
| 125 | end |
| 126 | note 10 RMSF: per row r < R: s = 1/sqrt(sum v^2 / D + 1e-5); y = v s g; args R*32 v g y s |
| 127 | fn grrmsf to void |
| 128 | d = uint get grd 2 |
| 129 | d4 = mul d 4 |
| 130 | n = div d 32 |
| 131 | grrrow |
| 132 | gkmovi 24 grf div 1.0f64 f64 d |
| 133 | gkmovi 25 grf 0.00001f64 |
| 134 | grrptr 10 1 d4 |
| 135 | gkldc64 14 grarg 4 |
| 136 | gkimadw 14 9 4 14 |
| 137 | b0 = grrb 0 n |
| 138 | b1 = grrb 1 n |
| 139 | grldw b0 10 n true |
| 140 | grldw b1 14 n false |
| 141 | gkmovz 64 |
| 142 | for u uint n |
| 143 | gkffmaf 64 add b0 u add b0 u 64 |
| 144 | end |
| 145 | grwred 0 0 |
| 146 | gkffmaf 65 64 24 25 |
| 147 | grmufu 66 65 5120 |
| 148 | grptr 12 7 8 4 |
| 149 | gkstg 12 66 0 false |
| 150 | grrptr 16 5 d4 |
| 151 | for u uint n |
| 152 | gkfmulf add b0 u add b0 u 66 |
| 153 | gkfmulf add b0 u add b0 u add b1 u |
| 154 | end |
| 155 | grstw 16 b0 n |
| 156 | gkexit |
| 157 | end |
| 158 | note Mutant r: 1 in the product, 0 in the mutant, whose RMSB row sum sum(da g v) skips the lane-1 |
| 159 | note butterfly step (k1 from half the row), which gcmp2 must catch. |
| 160 | const OMUTR ulong 1 |
| 161 | note 11 RMSB: per row: k1 = s^2 sum(da g v)/D; dh += (da g - v k1) s; w = da v s; args R*32 v g s da dh w |
| 162 | fn grrmsb to void |
| 163 | d = uint get grd 2 |
| 164 | d4 = mul d 4 |
| 165 | n = div d 32 |
| 166 | grrrow |
| 167 | gkmovi 24 grf div 1.0f64 f64 d |
| 168 | grrptr 10 1 d4 |
| 169 | gkldc64 12 grarg 4 |
| 170 | gkimadw 12 9 4 12 |
| 171 | grrptr 14 7 d4 |
| 172 | grrptr 16 9 d4 |
| 173 | grptr 20 5 8 4 |
| 174 | b0 = grrb 0 n |
| 175 | b1 = grrb 1 n |
| 176 | b2 = grrb 2 n |
| 177 | b3 = grrb 3 n |
| 178 | grldw b0 14 n true |
| 179 | grldw b1 12 n false |
| 180 | grldw b2 10 n false |
| 181 | grldw b3 16 n false |
| 182 | gkldg 67 20 0 true |
| 183 | gkmovz 64 |
| 184 | for u uint n |
| 185 | gkfmulf add b1 u add b0 u add b1 u |
| 186 | gkffmaf 64 add b1 u add b2 u 64 |
| 187 | end |
| 188 | if eq OMUTR 0 |
| 189 | grwred 0 1 |
| 190 | else |
| 191 | grwred 0 0 |
| 192 | end |
| 193 | gkfmulf 66 67 67 |
| 194 | gkfmulf 66 66 64 |
| 195 | gkfmulf 66 66 24 |
| 196 | grrptr 18 11 d4 |
| 197 | for u uint n |
| 198 | gkfmulf add b0 u add b0 u add b2 u |
| 199 | gkfmulf add b0 u add b0 u 67 |
| 200 | gkfmulf add b2 u add b2 u 66 |
| 201 | grfsub add b1 u add b1 u add b2 u |
| 202 | gkfmulf add b1 u add b1 u 67 |
| 203 | grfadd add b3 u add b3 u add b1 u |
| 204 | end |
| 205 | grstw 16 b3 n |
| 206 | grstw 18 b0 n |
| 207 | gkexit |
| 208 | end |
| 209 | note 13/14 COLSUM: out[c ostr + i] (=, +=) sum_k in[c cstr + k kstr + i]; grid (n/64, chunks) |
| 210 | note arg slots: 0 n, 1 kcnt (>= 1), 2-3 in, 4 kstr, 5 cstr, 6-7 out, 8 ostr (strides in bytes) |
| 211 | fn grcol acc bool to void |
| 212 | grpro |
| 213 | grptr 10 1 4 4 |
| 214 | gkldc 20 grarg 5 false |
| 215 | gkimadwr 10 3 20 10 |
| 216 | gkldc 21 grarg 4 false |
| 217 | gkldc 30 grarg 1 false |
| 218 | gkmovz 64 |
| 219 | top = div get gkat 0 16 |
| 220 | gkldg 32 10 0 false |
| 221 | grfadd 64 64 32 |
| 222 | gkimadwr 10 26 21 10 |
| 223 | grbot 30 top |
| 224 | grptr 12 5 4 4 |
| 225 | gkldc 22 grarg 8 false |
| 226 | gkimadwr 12 3 22 12 |
| 227 | if acc |
| 228 | gkldg 33 12 0 false |
| 229 | grfadd 64 64 33 |
| 230 | end |
| 231 | gkstg 12 64 0 false |
| 232 | gkexit |
| 233 | end |
| 234 | note SHFL.BFLY PT, Rd, Ra, lane, 0x1f (codegen.c:671 SHFL.DOWN with the BFLY mode bits, sm121.tbl:183) |
| 235 | fn grshfl d uint a uint lane uint to void |
| 236 | gkvr or or 32649 shl d 16 shl a 24 or 201334528 shl lane 21 917504 |
| 237 | end |
| 238 | note Softmax kernels: one warp per row; lane l holds columns l + 32 u, u < T/32, in R32.. (p) and |
| 239 | note R48.. (dp), so every load and store of a warp is one coalesced 128-byte line. Threads: T 32 |
| 240 | note per score block (arg 0), grid (T/2, Z); R21 = row t, R22 = lane, R10/R12 = row base + 4 lane. |
| 241 | fn grsrow tt uint dp bool to void |
| 242 | grpro |
| 243 | gkshr 21 4 5 |
| 244 | gkandi 22 4 31 |
| 245 | gkimadi 8 3 tt 21 |
| 246 | grptr 10 1 8 mul tt 4 |
| 247 | gkimadw 10 22 4 10 |
| 248 | if dp |
| 249 | grptr 12 3 8 mul tt 4 |
| 250 | gkimadw 12 22 4 12 |
| 251 | end |
| 252 | end |
| 253 | note sum (op 0) or max (op 1) of R64 across the warp by butterfly, R65 scratch |
| 254 | fn grwred op uint skip uint to void |
| 255 | for i uint 5 |
| 256 | o = uint shr 16 i |
| 257 | if ne o skip |
| 258 | grshfl 65 64 o |
| 259 | if eq op 0 |
| 260 | grfadd 64 64 65 |
| 261 | else |
| 262 | grfmax 64 64 65 |
| 263 | end |
| 264 | end |
| 265 | end |
| 266 | end |
| 267 | note Mutant s: 1 in the product, 0 in the mutant, whose SOFTF row sum skips the lane-1 butterfly step |
| 268 | note (each lane then normalizes by half the row), which gcmp and gcmp2 must catch. |
| 269 | const OMUTS ulong 1 |
| 270 | note Causal chunks (k6): row t (R21) loads 32-column chunk u only if t >= 32 u (P u-1 for u 1..7; |
| 271 | note chunk 0 and chunks from 8 on stay PT, loaded and masked), and stores chunk u only if |
| 272 | note t >= 64 floor(u/2). The causal GEMMs read a P or DS row only up to the end of its diagonal tile |
| 273 | note (32 or 64 columns), so later chunks are neither read nor written and keep stale arena data. |
| 274 | note Predicates are set before the loads: an ISETP between them would wait on their scoreboard. |
| 275 | note Mutant w: 1 in the product, 0 in the mutant, whose loads also skip the diagonal chunk (t >= 32 u + 32). |
| 276 | const OMUTW ulong 1 |
| 277 | fn grcpred n uint off uint to void |
| 278 | for u uint n |
| 279 | if and ge u 1 le u 7 |
| 280 | gkisge sub u 1 21 add mul u 32 off |
| 281 | end |
| 282 | end |
| 283 | end |
| 284 | fn grcp u uint to uint |
| 285 | if or eq u 0 ge u 8 |
| 286 | ret 7 |
| 287 | end |
| 288 | ret sub u 1 |
| 289 | end |
| 290 | fn grcwo to uint |
| 291 | if eq OMUTW 0 |
| 292 | ret 32 |
| 293 | end |
| 294 | ret 0 |
| 295 | end |
| 296 | note 15 SOFTF: causal softmax of row t of score block z in place, zero above t; args T*32 P |
| 297 | fn grsoftf to void |
| 298 | tt = uint get grd 1 |
| 299 | n = div tt 32 |
| 300 | grsrow tt false |
| 301 | gkmovi 27 4286578688 |
| 302 | gkmovi 28 grf 1.4426950408889634f64 |
| 303 | gkmovi 29 grf neg 1.4426950408889634f64 |
| 304 | gkmovi 23 32 |
| 305 | grcpred n grcwo |
| 306 | for u uint n |
| 307 | gkldgp add 32 u 10 mul u 128 ne u 0 grcp u |
| 308 | end |
| 309 | gkimadi 20 22 1 255 |
| 310 | gkmovi 64 4286578688 |
| 311 | for u uint n |
| 312 | gkisetp 1 20 21 4 |
| 313 | grfsel add 32 u 27 add 32 u 1 |
| 314 | gkiadd 20 20 23 |
| 315 | grfmax 64 64 add 32 u |
| 316 | end |
| 317 | grwred 1 0 |
| 318 | gkfmulf 66 64 29 |
| 319 | gkmovz 64 |
| 320 | for u uint n |
| 321 | gkffmaf add 32 u add 32 u 28 66 |
| 322 | grmufu add 32 u add 32 u 2048 |
| 323 | grfadd 64 64 add 32 u |
| 324 | end |
| 325 | if eq OMUTS 0 |
| 326 | grwred 0 1 |
| 327 | else |
| 328 | grwred 0 0 |
| 329 | end |
| 330 | grmufu 67 64 4096 |
| 331 | grcpred n 0 |
| 332 | for u uint n |
| 333 | gkfmulf add 32 u add 32 u 67 |
| 334 | gkstgp 10 add 32 u mul u 128 ne u 0 grcp and u 4294967294 |
| 335 | end |
| 336 | gkexit |
| 337 | end |
| 338 | note 16 SOFTB: ds = p (dp - sum p dp) / sqrt(E) in place over dp; args T*32 P DP. p and dp above the |
| 339 | note diagonal (column > t) read as 0: their chunks may be unloaded (k6) or stale (the causal DP GEMM, |
| 340 | note gkmm cz 3, skips those tiles), and 0 times a stale Inf or NaN is NaN. |
| 341 | fn grsoftb to void |
| 342 | tt = uint get grd 1 |
| 343 | n = div tt 32 |
| 344 | e = div get grd 2 get grd 3 |
| 345 | grsrow tt true |
| 346 | gkmovi 29 grf div 1.0f64 fsqrt f64 e |
| 347 | grcpred n grcwo |
| 348 | for u uint n |
| 349 | gkldgp add 32 u 10 mul u 128 ne u 0 grcp u |
| 350 | end |
| 351 | for u uint n |
| 352 | gkldgp add 48 u 12 mul u 128 true grcp u |
| 353 | end |
| 354 | gkmovi 23 32 |
| 355 | gkimadi 20 22 1 255 |
| 356 | for u uint n |
| 357 | gkisetp 1 20 21 4 |
| 358 | grfsel add 48 u 255 add 48 u 1 |
| 359 | grfsel add 32 u 255 add 32 u 1 |
| 360 | gkiadd 20 20 23 |
| 361 | end |
| 362 | gkmovz 64 |
| 363 | for u uint n |
| 364 | gkffmaf 64 add 32 u add 48 u 64 |
| 365 | end |
| 366 | grwred 0 0 |
| 367 | grcpred n 0 |
| 368 | for u uint n |
| 369 | grfsub add 48 u add 48 u 64 |
| 370 | gkfmulf add 48 u add 48 u add 32 u |
| 371 | gkfmulf add 48 u add 48 u 29 |
| 372 | gkstgp 12 add 48 u mul u 128 ne u 0 grcp and u 4294967294 |
| 373 | end |
| 374 | gkexit |
| 375 | end |
| 376 | note 19 COPY y = x; 20 ZERO; args n then pointers. The FFN ReLU forward and backward mask are fused |
| 377 | note into the GEMM epilogue (gk.om gkepi, kernels 25/26/56/58), so slots 17/18 stay empty. |
| 378 | fn grew kind uint to void |
| 379 | grpro |
| 380 | grptr 10 1 4 4 |
| 381 | if eq kind 20 |
| 382 | gkstg 10 255 0 false |
| 383 | else |
| 384 | grptr 12 3 4 4 |
| 385 | gkldg 32 10 0 false |
| 386 | grfadd 34 32 255 |
| 387 | gkstg 12 34 0 false |
| 388 | end |
| 389 | gkexit |
| 390 | end |
| 391 | note 21 CE: per row r of logits L (R x V): m, e = exp(l - m), sum; L <- (e/sum - [c = y]) sc; |
| 392 | note stats[r] = (m, sum, l_y); sc = 1/(T B); args R*32 L y st. One warp per row (grrrow): lane l |
| 393 | note holds columns l + 32 u, read as 8 coalesced 128-byte lines per step of 256 columns. Pass 1 takes |
| 394 | note the lane max, then the warp max. Pass 2 sums e per lane without storing it, then the warp sum |
| 395 | note of the lane totals. Pass 3 recomputes e with the same FFMA and MUFU (bit-identical to pass 2), |
| 396 | note scales it and stores it. The lane that owns column y (lane y & 31, the thread that stored it) |
| 397 | note subtracts sc there and writes stats, so no lane reads another lane's store. l_y is loaded |
| 398 | note before any store, and the next instruction waits for that load. |
| 399 | note Each lane's sum is compensated (Kahan: R40 carries the lost low part, R41 y, R42 t) and the |
| 400 | note lane total is s - c. A plain sequential f32 sum over V 4096 in one thread biased log sum low by |
| 401 | note about 1.04e-5 relative on trained checkpoints (gdiag receipt). Mutant k (OMUTK 0) keeps the |
| 402 | note plain per-lane sum. Mutant x (OMUTX 0) skips the lane-1 butterfly step of the warp sum, which |
| 403 | note gcmp2 and geval must catch. Kernels launch with 72 GPRs (gkgpr in grall); Kahan state in |
| 404 | note R69-R71 hung this kernel (launch wait -110, evidence/OMEGALLM/geval/a1), so it uses R40-R42. |
| 405 | note The warp sum overwrites R64 and its scratch R65 (grwred), so R43 keeps m and pass 3 recomputes |
| 406 | note R65 = -m log2e from it (the same FMUL as pass 2, so the same value). |
| 407 | const OMUTK ulong 1 |
| 408 | const OMUTX ulong 1 |
| 409 | fn grce to void |
| 410 | v = uint get grd 0 |
| 411 | v4 = mul v 4 |
| 412 | it = div v 256 |
| 413 | sc = div 1.0f64 f64 mul get grd 1 get grd 5 |
| 414 | grrrow |
| 415 | gkmovi 28 grf 1.4426950408889634f64 |
| 416 | gkmovi 29 grf neg 1.4426950408889634f64 |
| 417 | gkmovi 25 grf sc |
| 418 | grptr 12 3 8 4 |
| 419 | gkldg 20 12 0 false |
| 420 | grptr 14 1 8 v4 |
| 421 | gkimadw 14 20 4 14 |
| 422 | gkldg 68 14 0 false |
| 423 | grrptr 10 1 v4 |
| 424 | gkmovi 64 4286578688 |
| 425 | top = grtop 30 it |
| 426 | grldw 32 10 8 true |
| 427 | for u uint 8 |
| 428 | grfmax 64 64 add 32 u |
| 429 | end |
| 430 | gradv 10 1024 |
| 431 | grbot 30 top |
| 432 | grwred 1 0 |
| 433 | grfadd 43 64 255 |
| 434 | gkfmulf 65 64 29 |
| 435 | grrptr 10 1 v4 |
| 436 | gkmovz 66 |
| 437 | gkmovz 40 |
| 438 | top2 = grtop 30 it |
| 439 | grldw 32 10 8 true |
| 440 | for u uint 8 |
| 441 | gkffmaf add 32 u add 32 u 28 65 |
| 442 | grmufu add 32 u add 32 u 2048 |
| 443 | if eq OMUTK 0 |
| 444 | grfadd 66 66 add 32 u |
| 445 | end |
| 446 | if ne OMUTK 0 |
| 447 | grfsub 41 add 32 u 40 |
| 448 | grfadd 42 66 41 |
| 449 | grfsub 66 42 66 |
| 450 | grfsub 40 66 41 |
| 451 | grfadd 66 42 255 |
| 452 | end |
| 453 | end |
| 454 | gradv 10 1024 |
| 455 | grbot 30 top2 |
| 456 | grfsub 64 66 40 |
| 457 | if eq OMUTX 0 |
| 458 | grwred 0 1 |
| 459 | else |
| 460 | grwred 0 0 |
| 461 | end |
| 462 | grmufu 67 64 4096 |
| 463 | gkfmulf 67 67 25 |
| 464 | gkfmulf 65 43 29 |
| 465 | grrptr 10 1 v4 |
| 466 | top3 = grtop 30 it |
| 467 | grldw 32 10 8 true |
| 468 | for u uint 8 |
| 469 | gkffmaf add 32 u add 32 u 28 65 |
| 470 | grmufu add 32 u add 32 u 2048 |
| 471 | gkfmulf add 32 u add 32 u 67 |
| 472 | end |
| 473 | grstw 10 32 8 |
| 474 | gradv 10 1024 |
| 475 | grbot 30 top3 |
| 476 | gkandi 21 20 31 |
| 477 | gkisetp 1 9 21 5 |
| 478 | gkexitp 1 |
| 479 | gkldg 33 14 0 false |
| 480 | grfsub 33 33 25 |
| 481 | gkstg 14 33 0 false |
| 482 | grptr 16 5 8 12 |
| 483 | gkstg 16 43 0 false |
| 484 | gkstg 16 64 4 true |
| 485 | gkstg 16 68 8 true |
| 486 | gkexit |
| 487 | end |
| 488 | note 22 CSRG: gw[c][i] += sum over k in [start[c], start[c+1]) of dh[ord[k]][i]; grid (D/64, V) |
| 489 | note args D dh start ord gw (start: V + 1 uints, ord: rows sorted by token, ascending within a token) |
| 490 | fn grcsr to void |
| 491 | d = uint get grd 2 |
| 492 | grpro |
| 493 | grptr 10 3 3 4 |
| 494 | gkldg 20 10 0 false |
| 495 | gkldg 21 10 4 true |
| 496 | gkisetp 1 20 21 2 |
| 497 | gkexitp 1 |
| 498 | gkimad 30 20 31 21 |
| 499 | grptr 12 5 20 4 |
| 500 | grptr 16 1 4 4 |
| 501 | gkmovz 64 |
| 502 | top = div get gkat 0 16 |
| 503 | gkldg 22 12 0 false |
| 504 | gkimadw 14 22 mul d 4 16 |
| 505 | gkldg 32 14 0 false |
| 506 | grfadd 64 64 32 |
| 507 | gradv 12 4 |
| 508 | grbot 30 top |
| 509 | gkimadi 23 3 d 4 |
| 510 | grptr 18 7 23 4 |
| 511 | gkldg 33 18 0 false |
| 512 | grfadd 64 64 33 |
| 513 | gkstg 18 64 0 false |
| 514 | gkexit |
| 515 | end |
| 516 | note 23 SQN: out[c] = sum over i in [512 c, 512 c + 512) of g[i]^2 (f32); args nchunk g out. The |
| 517 | note gradient buffer is padded with zeros to a multiple of 512 (ggplan, ggbwd), so no tail guard. |
| 518 | fn grsqn to void |
| 519 | grpro |
| 520 | grptr 10 1 4 2048 |
| 521 | gkmovz 64 |
| 522 | top = grtop 30 64 |
| 523 | grld8 32 10 true |
| 524 | for u uint 8 |
| 525 | gkffmaf 64 add 32 u add 32 u 64 |
| 526 | end |
| 527 | gradv 10 32 |
| 528 | grbot 30 top |
| 529 | grptr 12 3 4 4 |
| 530 | gkstg 12 64 0 false |
| 531 | gkexit |
| 532 | end |
| 533 | note OMUTH: 1 in the product; 0 in mutant h (beta1 0.8 on chip), which gacmp must catch. |
| 534 | const OMUTH ulong 1 |
| 535 | note 24 ADAM (f32, on chip): g = g c; m = b1 m + (1 - b1) g; v = b2 v + (1 - b2) g^2; |
| 536 | note w -= lrb m / (sqrt(v ibc2) + 1e-8), the f32 image of og.om oadam1/oadam2 with lrb = lr/bc1 and |
| 537 | note ibc2 = 1/bc2 from the host. Args P w m v g c lrb ibc2 (slots 0, 2-9, 10, 11, 12). |
| 538 | fn gradam to void |
| 539 | grpro |
| 540 | b1 = 0.9f64 |
| 541 | if eq OMUTH 0 |
| 542 | b1 = 0.8f64 |
| 543 | end |
| 544 | gkmovi 24 grf b1 |
| 545 | gkmovi 25 grf sub 1.0f64 0.9 |
| 546 | gkmovi 27 grf 0.99f64 |
| 547 | gkmovi 28 grf sub 1.0f64 0.99 |
| 548 | gkmovi 29 grf 0.00000001f64 |
| 549 | grptr 10 1 4 4 |
| 550 | grptr 12 3 4 4 |
| 551 | grptr 14 5 4 4 |
| 552 | grptr 16 7 4 4 |
| 553 | gkldc 64 grarg 10 false |
| 554 | gkldc 65 grarg 11 true |
| 555 | gkldc 66 grarg 12 true |
| 556 | gkldg 32 16 0 true |
| 557 | gkldg 33 12 0 true |
| 558 | gkldg 34 14 0 true |
| 559 | gkldg 35 10 0 true |
| 560 | gkfmulf 32 32 64 |
| 561 | gkfmulf 33 33 24 |
| 562 | gkffmaf 33 32 25 33 |
| 563 | gkfmulf 36 32 32 |
| 564 | gkfmulf 34 34 27 |
| 565 | gkffmaf 34 36 28 34 |
| 566 | gkfmulf 37 34 66 |
| 567 | grmufu 38 37 8192 |
| 568 | grfadd 38 38 29 |
| 569 | grmufu 39 38 4096 |
| 570 | gkfmulf 40 33 65 |
| 571 | gkfmulf 40 40 39 |
| 572 | grfsub 35 35 40 |
| 573 | gkstg 10 35 0 false |
| 574 | gkstg 12 33 0 true |
| 575 | gkstg 14 34 0 true |
| 576 | gkexit |
| 577 | end |
| 578 | note emit slots 9..24 (12, 17 and 18 empty) for dims V T D H F B; false when a kernel overflows its 16 KiB slot |
| 579 | fn grset v ulong t ulong d ulong h ulong f ulong b ulong to void |
| 580 | put grd 0 v |
| 581 | put grd 1 t |
| 582 | put grd 2 d |
| 583 | put grd 3 h |
| 584 | put grd 4 f |
| 585 | put grd 5 b |
| 586 | end |
| 587 | fn grall to bool |
| 588 | note the warp-per-row RMS kernels hold D/32 columns per lane (D <= 384: four banks end at R119, grrb), |
| 589 | note softmax T/32, and CE steps 256 columns |
| 590 | if or ne rem get grd 2 32 0 ne rem get grd 1 32 0 |
| 591 | ret false |
| 592 | end |
| 593 | if gt get grd 2 384 |
| 594 | ret false |
| 595 | end |
| 596 | if ne rem get grd 0 256 0 |
| 597 | ret false |
| 598 | end |
| 599 | for k uint 16 |
| 600 | id = add k 9 |
| 601 | gkslot id |
| 602 | if eq id 9 |
| 603 | gremb |
| 604 | end |
| 605 | if eq id 10 |
| 606 | grrmsf |
| 607 | end |
| 608 | if eq id 11 |
| 609 | grrmsb |
| 610 | end |
| 611 | if eq id 13 |
| 612 | grcol false |
| 613 | end |
| 614 | if eq id 14 |
| 615 | grcol true |
| 616 | end |
| 617 | if eq id 15 |
| 618 | grsoftf |
| 619 | end |
| 620 | if eq id 16 |
| 621 | grsoftb |
| 622 | end |
| 623 | if and ge id 19 le id 20 |
| 624 | grew id |
| 625 | end |
| 626 | if eq id 21 |
| 627 | grce |
| 628 | end |
| 629 | if eq id 22 |
| 630 | grcsr |
| 631 | end |
| 632 | if eq id 23 |
| 633 | grsqn |
| 634 | end |
| 635 | if eq id 24 |
| 636 | gradam |
| 637 | end |
| 638 | if gt get gkat 0 mul ulong add id 1 16384 |
| 639 | ret false |
| 640 | end |
| 641 | put gkthr id 64 |
| 642 | put gkgpr id 72 |
| 643 | if and gt get grd 2 256 or eq id 10 eq id 11 |
| 644 | put gkgpr id 128 |
| 645 | end |
| 646 | put gksm id 0 |
| 647 | end |
| 648 | ret true |
| 649 | end |
| 650 | note open the chip with the row slots 9..24 for the given dims, then gkopen's GEMM slots, arena asz bytes |
| 651 | fn gropen asz ulong to bool |
| 652 | if not grall |
| 653 | put nverr 0 neg long 7 |
| 654 | ret false |
| 655 | end |
| 656 | ret gkopen asz |
| 657 | end |