omega-helix
This project has not described itself yet.
Brought by drakestapleton
omega-helix / native / gpu / bf_dna.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note native/gpu/bf_dna.om: GB10 SM121 GPU backend for ollm-dna (TARGET=dna-gb10). |
| 3 | note Zero CUDA/libc; implements dn_be_* over gk.om / bf_kernels.om / batch.om. |
| 4 | |
| 5 | global bf_st ulong 32 |
| 6 | note bf_st: 0=mw_id, 1=wbase_va, 2=gpu_open, 3=batch_mode |
| 7 | global bf_ao ulong 32 |
| 8 | note bf_ao byte offsets inside arena (gbmem 5): |
| 9 | note 0=H 1=XN 2=Q 3=K 4=V 5=O 6=G 7=U 8=Z 9=DZ 10=COS 11=SIN 12=TOKS 13=TGTS 14=ST0 15=ST1 16=JSD 17=DL 18=LB 19=LG 20=SCR 21=SCR_CAP 22=GRP_SH |
| 10 | |
| 11 | fn gbpre to void |
| 12 | end |
| 13 | |
| 14 | fn gbpost to bool |
| 15 | ret true |
| 16 | end |
| 17 | |
| 18 | fn dn_be_is_gpu to bool |
| 19 | ret true |
| 20 | end |
| 21 | |
| 22 | fn dn_be_dump_kernels path_len ulong to bool |
| 23 | ret bf_dump_kernels_file path_len |
| 24 | end |
| 25 | |
| 26 | fn bf_fail_stop code ulong to void |
| 27 | c = nvstop |
| 28 | exit int code |
| 29 | end |
| 30 | |
| 31 | fn bf_u32_to_io o ulong v ulong to void |
| 32 | put dn_io o uchar and v 255 |
| 33 | put dn_io add o 1 uchar and shr v 8 255 |
| 34 | put dn_io add o 2 uchar and shr v 16 255 |
| 35 | put dn_io add o 3 uchar and shr v 24 255 |
| 36 | end |
| 37 | |
| 38 | fn bf_u16_to_io o ulong v ulong to void |
| 39 | put dn_io o uchar and v 255 |
| 40 | put dn_io add o 1 uchar and shr v 8 255 |
| 41 | end |
| 42 | |
| 43 | fn bf_io_u32 o ulong to ulong |
| 44 | v = ulong get dn_io o |
| 45 | v = or v shl ulong get dn_io add o 1 8 |
| 46 | v = or v shl ulong get dn_io add o 2 16 |
| 47 | ret or v shl ulong get dn_io add o 3 24 |
| 48 | end |
| 49 | |
| 50 | fn bf_io_u16 o ulong to ulong |
| 51 | v = ulong get dn_io o |
| 52 | ret or v shl ulong get dn_io add o 1 8 |
| 53 | end |
| 54 | |
| 55 | fn bf_aptr idx ulong to ulong |
| 56 | ret add gkbase get bf_ao idx |
| 57 | end |
| 58 | |
| 59 | fn bf_wptr sl ulong to ulong |
| 60 | ret add get bf_st 1 get dn_woff sl |
| 61 | end |
| 62 | |
| 63 | fn bf_pt s uint addr ulong to void |
| 64 | put gkarg s gblo addr |
| 65 | put gkarg add s 1 gbhi addr |
| 66 | end |
| 67 | |
| 68 | fn bf_launch id uint na uint gx ulong gy ulong to void |
| 69 | put gkna 0 na |
| 70 | if not gklaunch id uint gx uint gy 1 |
| 71 | bf_fail_stop add 1100 ulong id |
| 72 | end |
| 73 | end |
| 74 | |
| 75 | fn bf_al4k v ulong to ulong |
| 76 | ret mul div add v 4095 4096 4096 |
| 77 | end |
| 78 | |
| 79 | fn bf_plan_arena to ulong |
| 80 | v = get dn_st 0 |
| 81 | d = get dn_st 1 |
| 82 | qd = get dn_st 13 |
| 83 | kvd = get dn_st 14 |
| 84 | f = get dn_st 5 |
| 85 | he = div get dn_st 4 2 |
| 86 | p = ulong 0 |
| 87 | put bf_ao 0 p |
| 88 | p = add p bf_al4k mul 256 d |
| 89 | put bf_ao 1 p |
| 90 | p = add p bf_al4k mul 256 d |
| 91 | put bf_ao 2 p |
| 92 | p = add p bf_al4k mul 256 qd |
| 93 | put bf_ao 3 p |
| 94 | p = add p bf_al4k mul 256 kvd |
| 95 | put bf_ao 4 p |
| 96 | p = add p bf_al4k mul 256 kvd |
| 97 | put bf_ao 5 p |
| 98 | p = add p bf_al4k mul 256 qd |
| 99 | put bf_ao 6 p |
| 100 | p = add p bf_al4k mul 256 f |
| 101 | put bf_ao 7 p |
| 102 | p = add p bf_al4k mul 256 f |
| 103 | put bf_ao 8 p |
| 104 | p = add p bf_al4k mul 256 d |
| 105 | put bf_ao 9 p |
| 106 | p = add p bf_al4k mul 256 d |
| 107 | put bf_ao 10 p |
| 108 | p = add p bf_al4k mul 512 he |
| 109 | put bf_ao 11 p |
| 110 | p = add p bf_al4k mul 512 he |
| 111 | put bf_ao 12 p |
| 112 | p = add p 4096 |
| 113 | put bf_ao 13 p |
| 114 | p = add p 4096 |
| 115 | put bf_ao 14 p |
| 116 | p = add p 4096 |
| 117 | put bf_ao 15 p |
| 118 | p = add p 4096 |
| 119 | put bf_ao 16 p |
| 120 | p = add p 4096 |
| 121 | put bf_ao 17 p |
| 122 | p = add p bf_al4k mul 256 v |
| 123 | put bf_ao 18 p |
| 124 | p = add p bf_al4k mul 512 v |
| 125 | put bf_ao 19 p |
| 126 | p = add p bf_al4k mul 512 v |
| 127 | put bf_ao 20 p |
| 128 | sc_cap = ulong 33554432 |
| 129 | if ge v 32768 |
| 130 | sc_cap = 167772160 |
| 131 | end |
| 132 | put bf_ao 21 sc_cap |
| 133 | p = add p sc_cap |
| 134 | grp = div get dn_st 2 get dn_st 3 |
| 135 | gsh = ulong 0 |
| 136 | if eq grp 2 |
| 137 | gsh = 1 |
| 138 | end |
| 139 | if eq grp 4 |
| 140 | gsh = 2 |
| 141 | end |
| 142 | if eq grp 8 |
| 143 | gsh = 3 |
| 144 | end |
| 145 | if eq grp 16 |
| 146 | gsh = 4 |
| 147 | end |
| 148 | put bf_ao 22 gsh |
| 149 | ret p |
| 150 | end |
| 151 | |
| 152 | fn bf_upload_weights fd int pb ulong to void |
| 153 | mw = nvalloc pb true |
| 154 | if le mw 0 |
| 155 | exit 70 |
| 156 | end |
| 157 | put bf_st 0 ulong mw |
| 158 | put bf_st 1 mapaddr mw |
| 159 | pos = ulong 256 |
| 160 | remb = pb |
| 161 | while gt remb 0 bound 9223372036854775807 |
| 162 | cn = remb |
| 163 | if gt cn DN_CHUNK |
| 164 | cn = DN_CHUNK |
| 165 | end |
| 166 | dn_pread_all fd pos cn |
| 167 | rw = mapwrite mw sub pos 256 dn_io 0 cn |
| 168 | if ne rw long cn |
| 169 | bf_fail_stop 70 |
| 170 | end |
| 171 | pos = add pos cn |
| 172 | remb = sub remb cn |
| 173 | end |
| 174 | end |
| 175 | |
| 176 | fn bf_io_f64 o ulong to f64 |
| 177 | u = uint bf_io_u32 o |
| 178 | f = bitcast f32 u |
| 179 | ret f64 f |
| 180 | end |
| 181 | |
| 182 | fn bf_upload_rope_chunk ao_idx ulong is_sin bool pos ulong cnt ulong to void |
| 183 | for i ulong cnt |
| 184 | v = get dn_rcos add pos i |
| 185 | if is_sin |
| 186 | v = get dn_rsin add pos i |
| 187 | end |
| 188 | b = mul i 4 |
| 189 | u = uint bitcast uint f32 v |
| 190 | bf_u32_to_io b ulong u |
| 191 | end |
| 192 | nb = mul cnt 4 |
| 193 | dst = add get bf_ao ao_idx mul pos 4 |
| 194 | r = mapwrite get gbmem 5 dst dn_io 0 nb |
| 195 | if ne r long nb |
| 196 | bf_fail_stop 70 |
| 197 | end |
| 198 | end |
| 199 | |
| 200 | fn bf_upload_rope to void |
| 201 | he = div get dn_st 4 2 |
| 202 | tot = mul 128 he |
| 203 | pos = ulong 0 |
| 204 | while lt pos tot bound 9223372036854775807 |
| 205 | cnt = sub tot pos |
| 206 | if gt cnt 262144 |
| 207 | cnt = 262144 |
| 208 | end |
| 209 | bf_upload_rope_chunk 10 false pos cnt |
| 210 | bf_upload_rope_chunk 11 true pos cnt |
| 211 | pos = add pos cnt |
| 212 | end |
| 213 | end |
| 214 | |
| 215 | fn dn_be_open to void |
| 216 | asz = bf_plan_arena |
| 217 | if not gkopen asz |
| 218 | bf_fail_stop 70 |
| 219 | end |
| 220 | if not gbbopen |
| 221 | bf_fail_stop 70 |
| 222 | end |
| 223 | if not gkallh |
| 224 | bf_fail_stop 70 |
| 225 | end |
| 226 | if not bf_emit_all |
| 227 | bf_fail_stop 70 |
| 228 | end |
| 229 | rc = mapwrite get gbmem 4 0 gkcode 0 1605632 |
| 230 | if ne rc long 1605632 |
| 231 | bf_fail_stop 70 |
| 232 | end |
| 233 | bf_upload_weights int get dn_st 18 get dn_st 12 |
| 234 | bf_upload_rope |
| 235 | put gkinv 0 1 |
| 236 | put bf_st 2 1 |
| 237 | end |
| 238 | |
| 239 | fn dn_be_close to void |
| 240 | if ne get bf_st 2 0 |
| 241 | r = nvstop |
| 242 | put bf_st 2 0 |
| 243 | end |
| 244 | end |
| 245 | |
| 246 | fn bf_gemmh_tt m ulong n ulong k ulong a_va ulong b_va ulong c_va ulong to void |
| 247 | gkoph 0 a_va uint mul k 2 2 0 0 |
| 248 | gkoph 1 b_va uint mul k 2 2 0 0 |
| 249 | gkopc c_va uint mul n 2 2 0 0 f32 1.0f64 |
| 250 | if not gkmmh uint m uint n uint k 1 1 2 |
| 251 | bf_fail_stop 99 |
| 252 | end |
| 253 | end |
| 254 | |
| 255 | fn bf_gemmh_tt_add m ulong n ulong k ulong a_va ulong b_va ulong c_va ulong to void |
| 256 | gkoph 0 a_va uint mul k 2 2 0 0 |
| 257 | gkoph 1 b_va uint mul k 2 2 0 0 |
| 258 | gkopc c_va uint mul n 2 2 0 0 f32 1.0f64 |
| 259 | if not gkmmh uint m uint n uint k 1 1 3 |
| 260 | bf_fail_stop 99 |
| 261 | end |
| 262 | end |
| 263 | |
| 264 | fn bf_gemmh_f32 m ulong n ulong k ulong a_va ulong b_va ulong c_va ulong to void |
| 265 | gkoph 0 a_va uint mul k 2 2 0 0 |
| 266 | gkoph 1 b_va uint mul k 2 2 0 0 |
| 267 | gkopc c_va uint mul n 4 4 0 0 f32 1.0f64 |
| 268 | if not gkmmh uint m uint n uint k 1 1 0 |
| 269 | bf_fail_stop 99 |
| 270 | end |
| 271 | end |
| 272 | |
| 273 | fn bf_gemmh_bwd_wlm m ulong d ulong v ulong a_va ulong b_va ulong c_va ulong to void |
| 274 | gkoph 0 a_va uint mul v 2 2 0 0 |
| 275 | gkoph 1 b_va 2 uint mul d 2 0 0 |
| 276 | gkopc c_va uint mul d 2 2 0 0 f32 1.0f64 |
| 277 | if not gkmmh uint m uint d uint v 1 1 2 |
| 278 | bf_fail_stop 99 |
| 279 | end |
| 280 | end |
| 281 | |
| 282 | fn bf_rec_rms x_va ulong gw_va ulong y_va ulong to void |
| 283 | put gkarg 0 mul uint 128 32 |
| 284 | bf_pt 2 x_va |
| 285 | bf_pt 4 gw_va |
| 286 | bf_pt 6 y_va |
| 287 | bf_launch 88 8 16 1 |
| 288 | end |
| 289 | |
| 290 | fn bf_rec_qkn x_va ulong gw_va ulong nh ulong do_norm bool to void |
| 291 | bf_pt 2 x_va |
| 292 | bf_pt 4 gw_va |
| 293 | bf_pt 6 bf_aptr 10 |
| 294 | bf_pt 8 bf_aptr 11 |
| 295 | put gkarg 10 uint nh |
| 296 | dn = uint 0 |
| 297 | if do_norm |
| 298 | dn = 1 |
| 299 | end |
| 300 | put gkarg 11 dn |
| 301 | gx = div add mul nh 32 255 256 |
| 302 | bf_launch 90 12 gx 128 |
| 303 | end |
| 304 | |
| 305 | fn bf_rec_layer l ulong to void |
| 306 | b = add 8 mul l 24 |
| 307 | d = get dn_st 1 |
| 308 | qd = get dn_st 13 |
| 309 | kvd = get dn_st 14 |
| 310 | f = get dn_st 5 |
| 311 | bf_rec_rms bf_aptr 0 bf_wptr b bf_aptr 1 |
| 312 | bf_gemmh_tt 128 qd d bf_aptr 1 bf_wptr add b 1 bf_aptr 2 |
| 313 | bf_gemmh_tt 128 kvd d bf_aptr 1 bf_wptr add b 2 bf_aptr 3 |
| 314 | bf_gemmh_tt 128 kvd d bf_aptr 1 bf_wptr add b 3 bf_aptr 4 |
| 315 | do_qk = ne and get dn_st 7 8 0 |
| 316 | bf_rec_qkn bf_aptr 2 bf_wptr add b 5 get dn_st 2 do_qk |
| 317 | bf_rec_qkn bf_aptr 3 bf_wptr add b 6 get dn_st 3 do_qk |
| 318 | bf_pt 0 bf_aptr 2 |
| 319 | bf_pt 2 bf_aptr 3 |
| 320 | bf_pt 4 bf_aptr 4 |
| 321 | bf_pt 6 bf_aptr 5 |
| 322 | bf_launch 91 8 div add mul get dn_st 2 32 255 256 128 |
| 323 | bf_gemmh_tt_add 128 d qd bf_aptr 5 bf_wptr add b 4 bf_aptr 0 |
| 324 | bf_rec_rms bf_aptr 0 bf_wptr add b 7 bf_aptr 1 |
| 325 | bf_gemmh_tt 128 f d bf_aptr 1 bf_wptr add b 8 bf_aptr 6 |
| 326 | bf_gemmh_tt 128 f d bf_aptr 1 bf_wptr add b 9 bf_aptr 7 |
| 327 | nv8 = div mul 128 f 8 |
| 328 | put gkarg 0 uint nv8 |
| 329 | bf_pt 2 bf_aptr 6 |
| 330 | bf_pt 4 bf_aptr 7 |
| 331 | bf_launch 89 6 div add nv8 255 256 1 |
| 332 | bf_gemmh_tt_add 128 d f bf_aptr 6 bf_wptr add b 10 bf_aptr 0 |
| 333 | end |
| 334 | |
| 335 | fn bf_bat_beg cnt ulong to void |
| 336 | if not gbbegin cnt |
| 337 | bf_fail_stop 70 |
| 338 | end |
| 339 | put gkbat 0 1 |
| 340 | end |
| 341 | |
| 342 | fn bf_bat_end to void |
| 343 | put gkbat 0 0 |
| 344 | if not gkbsub |
| 345 | bf_fail_stop 70 |
| 346 | end |
| 347 | end |
| 348 | |
| 349 | fn bf_run_trunk_gpu to void |
| 350 | l = get dn_st 6 |
| 351 | ls = ulong 0 |
| 352 | while lt ls l bound 1024 |
| 353 | lend = add ls 12 |
| 354 | if gt lend l |
| 355 | lend = l |
| 356 | end |
| 357 | cnt = mul sub lend ls 13 |
| 358 | if eq ls 0 |
| 359 | cnt = add cnt 1 |
| 360 | end |
| 361 | if eq lend l |
| 362 | cnt = add cnt 1 |
| 363 | end |
| 364 | bf_bat_beg cnt |
| 365 | if eq ls 0 |
| 366 | dv8 = div get dn_st 1 8 |
| 367 | put gkarg 0 uint dv8 |
| 368 | bf_pt 2 bf_wptr 0 |
| 369 | bf_pt 4 bf_aptr 12 |
| 370 | bf_pt 6 bf_aptr 0 |
| 371 | put gkarg 8 uint mul get dn_st 1 2 |
| 372 | bf_launch 94 9 div add dv8 255 256 128 |
| 373 | end |
| 374 | for li ulong sub lend ls |
| 375 | bf_rec_layer add ls li |
| 376 | end |
| 377 | if eq lend l |
| 378 | bf_rec_rms bf_aptr 0 bf_wptr 2 bf_aptr 8 |
| 379 | end |
| 380 | bf_bat_end |
| 381 | ls = lend |
| 382 | end |
| 383 | end |
| 384 | |
| 385 | fn bf_upload_toks_tgts ct ulong to void |
| 386 | for t ulong 128 |
| 387 | tv = ulong 0 |
| 388 | yv = ulong 0 |
| 389 | if lt t ct |
| 390 | tv = get dn_toks t |
| 391 | yv = get dn_tgts t |
| 392 | end |
| 393 | bf_u32_to_io mul t 4 tv |
| 394 | bf_u32_to_io add 512 mul t 4 yv |
| 395 | end |
| 396 | r1 = mapwrite get gbmem 5 get bf_ao 12 dn_io 0 512 |
| 397 | r2 = mapwrite get gbmem 5 get bf_ao 13 dn_io 512 512 |
| 398 | if or ne r1 512 ne r2 512 |
| 399 | bf_fail_stop 70 |
| 400 | end |
| 401 | put gkinv 0 1 |
| 402 | end |
| 403 | |
| 404 | fn bf_dl_bf16_chunk src ulong pos ulong cnt ulong dst_id ulong negate bool to void |
| 405 | nb = mul cnt 2 |
| 406 | r = mapread get gbmem 5 src dn_io 0 nb |
| 407 | if ne r long nb |
| 408 | bf_fail_stop 70 |
| 409 | end |
| 410 | for i ulong cnt |
| 411 | v = ubf16_to_f64 bf_io_u16 mul i 2 |
| 412 | if negate |
| 413 | v = sub 0.0f64 v |
| 414 | end |
| 415 | if eq dst_id 0 |
| 416 | put dn_zbuf add pos i v |
| 417 | else |
| 418 | put dn_rbuf add pos i v |
| 419 | end |
| 420 | end |
| 421 | end |
| 422 | |
| 423 | fn bf_dl_bf16_to_f64 ao_idx ulong n_elem ulong dst_id ulong negate bool to void |
| 424 | base = get bf_ao ao_idx |
| 425 | pos = ulong 0 |
| 426 | while lt pos n_elem bound 9223372036854775807 |
| 427 | cnt = sub n_elem pos |
| 428 | if gt cnt 524288 |
| 429 | cnt = 524288 |
| 430 | end |
| 431 | bf_dl_bf16_chunk add base mul pos 2 pos cnt dst_id negate |
| 432 | pos = add pos cnt |
| 433 | end |
| 434 | end |
| 435 | |
| 436 | fn bf_ul_f64_chunk dst ulong pos ulong cnt ulong to void |
| 437 | for i ulong cnt |
| 438 | bf_u16_to_io mul i 2 uf64_to_bf16 get dn_zbuf add pos i |
| 439 | end |
| 440 | nb = mul cnt 2 |
| 441 | r = mapwrite get gbmem 5 dst dn_io 0 nb |
| 442 | if ne r long nb |
| 443 | bf_fail_stop 70 |
| 444 | end |
| 445 | end |
| 446 | |
| 447 | fn bf_ul_f64_to_bf16 ao_idx ulong n_elem ulong to void |
| 448 | base = get bf_ao ao_idx |
| 449 | pos = ulong 0 |
| 450 | while lt pos n_elem bound 9223372036854775807 |
| 451 | cnt = sub n_elem pos |
| 452 | if gt cnt 524288 |
| 453 | cnt = 524288 |
| 454 | end |
| 455 | bf_ul_f64_chunk add base mul pos 2 pos cnt |
| 456 | pos = add pos cnt |
| 457 | end |
| 458 | put gkinv 0 1 |
| 459 | end |
| 460 | |
| 461 | fn bf_dl_stats_tok t ulong is_guard bool to void |
| 462 | bo = mul t 16 |
| 463 | mx = bf_io_f64 bo |
| 464 | sm = bf_io_f64 add bo 4 |
| 465 | ly = bf_io_f64 add bo 8 |
| 466 | am = bf_io_u32 add bo 12 |
| 467 | ce = sub add mx uflog sm ly |
| 468 | hit = ulong 0 |
| 469 | if eq am get dn_tgts t |
| 470 | hit = 1 |
| 471 | end |
| 472 | if is_guard |
| 473 | put dn_gl t ce |
| 474 | put dn_gh t hit |
| 475 | else |
| 476 | put dn_bl t ce |
| 477 | put dn_bh t hit |
| 478 | end |
| 479 | end |
| 480 | |
| 481 | fn bf_dl_jsd ct ulong ao_jsd ulong to void |
| 482 | r = mapread get gbmem 5 get bf_ao ao_jsd dn_io 0 512 |
| 483 | if ne r 512 |
| 484 | bf_fail_stop 70 |
| 485 | end |
| 486 | for t ulong ct |
| 487 | jv = bf_io_f64 mul t 4 |
| 488 | if lt jv 0.0f64 |
| 489 | jv = 0.0f64 |
| 490 | end |
| 491 | put dn_jsd t jv |
| 492 | end |
| 493 | end |
| 494 | |
| 495 | fn bf_dl_stats ct ulong ao_st ulong ao_jsd ulong is_guard bool to void |
| 496 | r = mapread get gbmem 5 get bf_ao ao_st dn_io 0 2048 |
| 497 | if ne r 2048 |
| 498 | bf_fail_stop 70 |
| 499 | end |
| 500 | for t ulong ct |
| 501 | bf_dl_stats_tok t is_guard |
| 502 | end |
| 503 | if is_guard |
| 504 | bf_dl_jsd ct ao_jsd |
| 505 | end |
| 506 | end |
| 507 | |
| 508 | fn bf_rec_lm_ce do_grad bool is_guard bool to void |
| 509 | v = get dn_st 0 |
| 510 | d = get dn_st 1 |
| 511 | lmw = add get bf_st 1 dn_lmw_off |
| 512 | lg_ao = bf_aptr 18 |
| 513 | if is_guard |
| 514 | lg_ao = bf_aptr 19 |
| 515 | end |
| 516 | bf_gemmh_f32 128 v d bf_aptr 8 lmw lg_ao |
| 517 | if is_guard |
| 518 | put gkarg 0 mul uint 128 32 |
| 519 | bf_pt 2 bf_aptr 19 |
| 520 | bf_pt 4 bf_aptr 18 |
| 521 | bf_pt 6 bf_aptr 13 |
| 522 | bf_pt 8 bf_aptr 14 |
| 523 | bf_pt 10 bf_aptr 15 |
| 524 | bf_pt 12 bf_aptr 16 |
| 525 | bf_launch 93 14 16 1 |
| 526 | else |
| 527 | put gkarg 0 mul uint 128 32 |
| 528 | bf_pt 2 bf_aptr 18 |
| 529 | bf_pt 4 bf_aptr 13 |
| 530 | bf_pt 6 bf_aptr 14 |
| 531 | bf_pt 8 bf_aptr 17 |
| 532 | wdl = uint 0 |
| 533 | if do_grad |
| 534 | wdl = 1 |
| 535 | end |
| 536 | put gkarg 10 wdl |
| 537 | bf_launch 92 11 16 1 |
| 538 | if do_grad |
| 539 | bf_gemmh_bwd_wlm 128 d v bf_aptr 17 lmw bf_aptr 9 |
| 540 | end |
| 541 | end |
| 542 | end |
| 543 | |
| 544 | fn dn_be_win_run need_res bool to void |
| 545 | ct = get dn_st 10 |
| 546 | d = get dn_st 1 |
| 547 | bf_upload_toks_tgts ct |
| 548 | bf_run_trunk_gpu |
| 549 | cnt = ulong 2 |
| 550 | if need_res |
| 551 | cnt = 3 |
| 552 | end |
| 553 | bf_bat_beg cnt |
| 554 | bf_rec_lm_ce need_res false |
| 555 | bf_bat_end |
| 556 | bf_dl_bf16_to_f64 8 mul ct d 0 false |
| 557 | bf_dl_stats ct 14 16 false |
| 558 | if need_res |
| 559 | bf_dl_bf16_to_f64 9 mul ct d 1 true |
| 560 | end |
| 561 | end |
| 562 | |
| 563 | fn bf_scale_rbuf n ulong d ulong to void |
| 564 | for i ulong mul n d |
| 565 | rv = div get dn_rbuf i f64 n |
| 566 | put dn_rbuf i rv |
| 567 | end |
| 568 | end |
| 569 | |
| 570 | fn dn_be_lm_bwd n ulong want_grad bool to f64 |
| 571 | d = get dn_st 1 |
| 572 | for t ulong 128 |
| 573 | yv = ulong 0 |
| 574 | if lt t n |
| 575 | yv = get dn_tgts t |
| 576 | end |
| 577 | bf_u32_to_io mul t 4 yv |
| 578 | end |
| 579 | r = mapwrite get gbmem 5 get bf_ao 13 dn_io 0 512 |
| 580 | if ne r 512 |
| 581 | bf_fail_stop 70 |
| 582 | end |
| 583 | bf_ul_f64_to_bf16 8 mul n d |
| 584 | cnt = ulong 2 |
| 585 | if want_grad |
| 586 | cnt = 3 |
| 587 | end |
| 588 | bf_bat_beg cnt |
| 589 | bf_rec_lm_ce want_grad false |
| 590 | bf_bat_end |
| 591 | bf_dl_stats n 14 16 false |
| 592 | ls = 0.0f64 |
| 593 | for t2 ulong n |
| 594 | ls = add ls get dn_bl t2 |
| 595 | end |
| 596 | if want_grad |
| 597 | bf_dl_bf16_to_f64 9 mul n d 1 false |
| 598 | bf_scale_rbuf n d |
| 599 | end |
| 600 | ret div ls f64 n |
| 601 | end |
| 602 | |
| 603 | fn dn_be_guard_eval_win ct ulong any_act bool to void |
| 604 | if not any_act |
| 605 | for t ulong ct |
| 606 | put dn_gl t get dn_bl t |
| 607 | put dn_gh t get dn_bh t |
| 608 | put dn_jsd t 0.0f64 |
| 609 | end |
| 610 | ret |
| 611 | end |
| 612 | d = get dn_st 1 |
| 613 | bf_ul_f64_to_bf16 8 mul ct d |
| 614 | bf_bat_beg 2 |
| 615 | bf_rec_lm_ce false true |
| 616 | bf_bat_end |
| 617 | bf_dl_stats ct 15 16 true |
| 618 | for t2 ulong ct |
| 619 | if eq get dn_active t2 0 |
| 620 | put dn_jsd t2 0.0f64 |
| 621 | end |
| 622 | end |
| 623 | end |
| 624 | |
| 625 | note CPU fallback & padded-128 HMMA Slot-86 backend for dn_be_gemm_abT |
| 626 | fn bf_gemm_cpu_row ab ulong n ulong k ulong cb ulong to void |
| 627 | for o ulong n |
| 628 | bb = mul o k |
| 629 | acc = 0.0f64 |
| 630 | for j ulong k |
| 631 | av = get dn_mm_a add ab j |
| 632 | bv = get dn_mm_b add bb j |
| 633 | acc = add acc mul av bv |
| 634 | end |
| 635 | put dn_mm_c add cb o acc |
| 636 | end |
| 637 | end |
| 638 | |
| 639 | fn bf_gemm_cpu m ulong n ulong k ulong to void |
| 640 | for i ulong m |
| 641 | bf_gemm_cpu_row mul i k n k mul i n |
| 642 | end |
| 643 | end |
| 644 | |
| 645 | fn bf_ul_mm_pad_fill arr_id ulong r ulong rows ulong cols ulong pos ulong cnt ulong to void |
| 646 | for j ulong cnt |
| 647 | v = 0.0f64 |
| 648 | if lt r rows |
| 649 | idx = add mul r cols add pos j |
| 650 | if eq arr_id 0 |
| 651 | v = get dn_mm_a idx |
| 652 | else |
| 653 | v = get dn_mm_b idx |
| 654 | end |
| 655 | end |
| 656 | h = uf64_to_bf16 v |
| 657 | bf_u16_to_io mul j 2 h |
| 658 | end |
| 659 | end |
| 660 | |
| 661 | fn bf_ul_mm_pad_row arr_id ulong ao_r ulong r ulong rows ulong cols ulong to void |
| 662 | pos = ulong 0 |
| 663 | while lt pos cols bound 9223372036854775807 |
| 664 | cnt = sub cols pos |
| 665 | if gt cnt 524288 |
| 666 | cnt = 524288 |
| 667 | end |
| 668 | bf_ul_mm_pad_fill arr_id r rows cols pos cnt |
| 669 | dst = add ao_r mul pos 2 |
| 670 | nb = mul cnt 2 |
| 671 | rw = mapwrite get gbmem 5 dst dn_io 0 nb |
| 672 | if ne rw long nb |
| 673 | bf_fail_stop 70 |
| 674 | end |
| 675 | pos = add pos cnt |
| 676 | end |
| 677 | end |
| 678 | |
| 679 | fn bf_ul_mm_pad arr_id ulong ao_off ulong rows ulong cols ulong p_rows ulong to void |
| 680 | rs = mul cols 2 |
| 681 | for r ulong p_rows |
| 682 | ao_r = add ao_off mul r rs |
| 683 | bf_ul_mm_pad_row arr_id ao_r r rows cols |
| 684 | end |
| 685 | end |
| 686 | |
| 687 | fn bf_ul_mm_chunk_fill rr ulong m ulong k ulong pos ulong cnt ulong to void |
| 688 | for j ulong cnt |
| 689 | v = 0.0f64 |
| 690 | if lt rr m |
| 691 | v = get dn_mm_a add mul rr k add pos j |
| 692 | end |
| 693 | h = uf64_to_bf16 v |
| 694 | bf_u16_to_io mul j 2 h |
| 695 | end |
| 696 | end |
| 697 | |
| 698 | fn bf_ul_mm_chunk_row ao_r ulong rr ulong m ulong k ulong to void |
| 699 | pos = ulong 0 |
| 700 | while lt pos k bound 9223372036854775807 |
| 701 | cnt = sub k pos |
| 702 | if gt cnt 524288 |
| 703 | cnt = 524288 |
| 704 | end |
| 705 | bf_ul_mm_chunk_fill rr m k pos cnt |
| 706 | dst = add ao_r mul pos 2 |
| 707 | nb = mul cnt 2 |
| 708 | rw = mapwrite get gbmem 5 dst dn_io 0 nb |
| 709 | if ne rw long nb |
| 710 | bf_fail_stop 70 |
| 711 | end |
| 712 | pos = add pos cnt |
| 713 | end |
| 714 | end |
| 715 | |
| 716 | fn bf_ul_mm_chunk a_r0 ulong nrp ulong m ulong k ulong ao_off ulong to void |
| 717 | rs = mul k 2 |
| 718 | for r ulong nrp |
| 719 | ao_r = add ao_off mul r rs |
| 720 | bf_ul_mm_chunk_row ao_r add a_r0 r m k |
| 721 | end |
| 722 | end |
| 723 | |
| 724 | fn bf_dl_mm_store ob ulong n ulong pos ulong cnt ulong to void |
| 725 | for j ulong cnt |
| 726 | v = bf_io_f64 mul j 4 |
| 727 | put dn_mm_c add ob add pos j v |
| 728 | end |
| 729 | end |
| 730 | |
| 731 | fn bf_dl_mm_row ao_r ulong rr ulong m ulong n ulong to void |
| 732 | if ge rr m |
| 733 | ret |
| 734 | end |
| 735 | ob = mul rr n |
| 736 | pos = ulong 0 |
| 737 | while lt pos n bound 9223372036854775807 |
| 738 | cnt = sub n pos |
| 739 | if gt cnt 262144 |
| 740 | cnt = 262144 |
| 741 | end |
| 742 | src = add ao_r mul pos 4 |
| 743 | nb = mul cnt 4 |
| 744 | rrw = mapread get gbmem 5 src dn_io 0 nb |
| 745 | if ne rrw long nb |
| 746 | bf_fail_stop 70 |
| 747 | end |
| 748 | bf_dl_mm_store ob n pos cnt |
| 749 | pos = add pos cnt |
| 750 | end |
| 751 | end |
| 752 | |
| 753 | fn bf_dl_mm_chunk c_r0 ulong nrp ulong m ulong n ulong np ulong ao_off ulong to void |
| 754 | rs = mul np 4 |
| 755 | for r ulong nrp |
| 756 | ao_r = add ao_off mul r rs |
| 757 | bf_dl_mm_row ao_r add c_r0 r m n |
| 758 | end |
| 759 | end |
| 760 | |
| 761 | fn bf_gemm_calc_mc n ulong k ulong to ulong |
| 762 | np = mul div add n 127 128 128 |
| 763 | b_bytes = mul mul np k 2 |
| 764 | min_a = mul mul 128 k 2 |
| 765 | min_c = mul mul 128 np 4 |
| 766 | need = add b_bytes add min_a min_c |
| 767 | cap = get bf_ao 21 |
| 768 | if ge need cap |
| 769 | ret 0 |
| 770 | end |
| 771 | rem_b = sub cap b_bytes |
| 772 | row_b = add mul k 2 mul np 4 |
| 773 | ret mul div div rem_b row_b 128 128 |
| 774 | end |
| 775 | |
| 776 | fn bf_gemm_run nrp ulong np ulong k ulong a_off ulong b_off ulong c_off ulong to void |
| 777 | put gkinv 0 1 |
| 778 | bf_bat_beg 1 |
| 779 | bf_gemmh_f32 nrp np k add gkbase a_off add gkbase b_off add gkbase c_off |
| 780 | bf_bat_end |
| 781 | end |
| 782 | |
| 783 | fn bf_gemm_step r0 ulong mc ulong m ulong n ulong k ulong np ulong to ulong |
| 784 | nr = sub m r0 |
| 785 | if gt nr mc |
| 786 | nr = mc |
| 787 | end |
| 788 | nrp = mul div add nr 127 128 128 |
| 789 | b_off = get bf_ao 20 |
| 790 | a_off = add b_off mul mul np k 2 |
| 791 | c_off = add a_off mul mul nrp k 2 |
| 792 | bf_ul_mm_chunk r0 nrp m k a_off |
| 793 | bf_gemm_run nrp np k a_off b_off c_off |
| 794 | bf_dl_mm_chunk r0 nrp m n np c_off |
| 795 | ret nrp |
| 796 | end |
| 797 | |
| 798 | fn dn_be_gemm_abT m ulong n ulong k ulong to void |
| 799 | work = mul mul m n k |
| 800 | if or lt k 128 or ne rem k 128 0 lt work 1048576 |
| 801 | bf_gemm_cpu m n k |
| 802 | ret |
| 803 | end |
| 804 | mc = bf_gemm_calc_mc n k |
| 805 | if eq mc 0 |
| 806 | bf_gemm_cpu m n k |
| 807 | ret |
| 808 | end |
| 809 | np = mul div add n 127 128 128 |
| 810 | b_off = get bf_ao 20 |
| 811 | bf_ul_mm_pad 1 b_off n k np |
| 812 | r0 = ulong 0 |
| 813 | while lt r0 m bound 9223372036854775807 |
| 814 | nrp = bf_gemm_step r0 mc m n k np |
| 815 | r0 = add r0 nrp |
| 816 | end |
| 817 | end |