File size: 49,622 Bytes
b7e49b8 d5afa0d b7e49b8 d5afa0d b7e49b8 d5afa0d b7e49b8 d5afa0d b7e49b8 d5afa0d b7e49b8 d5afa0d b7e49b8 d5afa0d b7e49b8 d5afa0d b7e49b8 d5afa0d b7e49b8 d5afa0d b7e49b8 d5afa0d b7e49b8 d5afa0d b7e49b8 d5afa0d b7e49b8 d5afa0d b7e49b8 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 | // SPDX-License-Identifier: Apache-2.0
//
// DepthPro2 ENC megakernel, TRISC: all arithmetic (LLK usage from the VLA-JEPA mk_vision megakernel).
//
// Core (x, y) holds RB slots per wave (row tiles ri = (w * RB + s) * R + y) and column x's N slice of every op.
// EMBED : x = patches @ Wpe + b (patch slots) | prefix constants (prefix slots)
// STATS : producer: per-slot row sums of x / x^2 over the slice -> one packed fp32 tile (columns 2s, 2s + 1);
// consumer: mu / E2 = sum of the row's C tiles @ SEL (x 1/D), rstd, std, EXT = [-mu | std] (bf16)
// QKV : per head unit: rstd (x @ W' + EXT @ [c ; bias']) -> RoPE (q / k of patch slots) -> bf16 -> staging
// ATT : items (head, q block): online softmax over key chunks, O / l -> bf16 -> staging
// PROJ : x += attn @ Wp' + E0 @ [b'] (LayerScale folded), fp32 residual
// FC1 : GELU(rstd (x @ W1' + EXT @ [c ; bias'])) | SiLU(g) u (SwiGLU) -> bf16 (row gather)
// FC2 : x += h @ W2' + E0 @ [b']
// Matmuls: bf16 weights, HiFi4 (ME_FID overrides), fp32 DST accumulation (full sync: 8 tiles); residual, statistics,
// softmax in fp32. A released DST reads as 0 for the FPU; every SFPU operand is written in the current section.
#include <cstdint>
#define REDUCE_OP (PoolType::MAX)
#define REDUCE_DIM (ReduceDim::REDUCE_ROW)
#include "api/compute/compute_kernel_api.h"
#include "api/compute/eltwise_binary.h"
#include "api/compute/eltwise_binary_sfpu.h"
#include "api/compute/binary_max_min.h"
#include "api/compute/bcast.h"
#include "api/compute/matmul.h"
#include "api/compute/reduce.h"
#include "api/compute/tile_move_copy.h"
#include "api/compute/eltwise_unary/exp.h"
#include "api/compute/eltwise_unary/gelu.h"
#include "api/compute/eltwise_unary/recip.h"
#include "api/compute/eltwise_unary/rsqrt.h"
#include "api/compute/eltwise_unary/sqrt.h"
#include "api/compute/eltwise_unary/binop_with_scalar.h"
#include "api/compute/reconfig_data_format.h"
#include "api/compute/compute_kernel_hw_startup.h"
#include "me_sched.hpp"
#if defined(TRISC_MATH) || defined(TRISC_PACK)
#include "llk_math_eltwise_binary_sfpu_macros.h"
#include "me_sfpu.hpp"
#endif
#define NOINL __attribute__((noinline, noclone))
using namespace mex;
namespace {
#ifdef ME_FID
constexpr MathFidelity FID = static_cast<MathFidelity>(ME_FID);
#else
constexpr MathFidelity FID = MathFidelity::HiFi4;
#endif
// ---------------------------------------------------------------- LLK wrappers
ALWI void mm_init_f(uint32_t in0, uint32_t in1, uint32_t transpose = 0) {
reconfig_data_format(in1, in0); // matmul: SrcA <- in1, SrcB <- in0
MATH((llk_math_matmul_init<FID, MM_THROTTLE>(in0, in1, transpose)));
UNPACK((llk_unpack_AB_matmul_init(in0, in1, transpose)));
}
ALWI void mm_f(uint32_t in0, uint32_t in1, uint32_t t0, uint32_t t1, uint32_t dst) {
UNPACK((llk_unpack_AB_matmul(in0, in1, t0, t1)));
MATH((llk_math_matmul<FID, MM_THROTTLE>(dst)));
}
// out-of-line matmul helpers (the kernel-config buffer holds every binary: code size matters; the hot matmul loops of
// mm_slots / PV keep the inline mm_f)
NOINL void mm_init_n(uint32_t in0, uint32_t in1, uint32_t transpose) { mm_init_f(in0, in1, transpose); }
NOINL void mm_n(uint32_t in0, uint32_t in1, uint32_t t0, uint32_t t1, uint32_t dst) { mm_f(in0, in1, t0, t1, dst); }
NOINL void load(uint32_t cb, uint32_t tile, uint32_t dst) {
reconfig_data_format_srca(cb);
copy_init(cb);
copy_tile(cb, tile, dst);
}
NOINL void fmul(uint32_t a, uint32_t b, uint32_t o) {
mul_binary_tile_init();
mul_binary_tile(a, b, o);
}
NOINL void fadd(uint32_t a, uint32_t b, uint32_t o) {
add_binary_tile_init();
add_binary_tile(a, b, o);
}
NOINL void fsub(uint32_t a, uint32_t b, uint32_t o) {
sub_binary_tile_init();
sub_binary_tile(a, b, o);
}
NOINL void fmax(uint32_t a, uint32_t b, uint32_t o) {
binary_max_tile_init();
binary_max_tile(a, b, o);
}
NOINL void faddc(uint32_t a, uint32_t bits) {
binop_with_scalar_tile_init();
add_unary_tile(a, bits);
}
NOINL void fmulc(uint32_t a, uint32_t bits) {
binop_with_scalar_tile_init();
mul_unary_tile(a, bits);
}
NOINL void frsqrt(uint32_t a) {
rsqrt_tile_init();
rsqrt_tile(a);
}
NOINL void fsqrt(uint32_t a) {
sqrt_tile_init();
sqrt_tile(a);
}
NOINL void frecip(uint32_t a) {
recip_tile_init();
recip_tile(a);
}
NOINL void fexp(uint32_t a) {
exp_tile_init<false>();
exp_tile<false>(a);
}
// the softmax exp: ME_EXP 0 fp32-accurate, 1 exp_21f (15 instructions, bf16-accurate: max rel err 1.7e-3)
NOINL void fexp_p(uint32_t a) {
#if ME_EXP == 1
exp_tile_init<false, 0x3F800000, InputClamping::ClampToNegative, false>();
exp_tile<false, false, InputClamping::ClampToNegative, 8, false>(a);
#else
exp_tile_init<false>();
exp_tile<false>(a);
#endif
}
// dst[a] = GELU(dst[a] * dst[r]): ME_GELU 1 polynomial (MoGe-2 v5, max |err| 2.1e-4), 0 exact erf GELU
NOINL void frgelu(uint32_t a, uint32_t r) {
#if ME_GELU == 1
MATH((::ckernel::llk_math_eltwise_unary_sfpu_init<SfpuType::gelu>(::ckernel::sfpu::me_gelu_init)));
MATH(SFPU_BINARY_CALL_NO_TEMPLATE_ARGS(DST_SYNC_MODE, DST_ACCUM_MODE, me_rgelu, a, r, a, VectorMode::RC));
#else
fmul(a, r, a);
gelu_tile_init<false>();
gelu_tile<false>(a);
#endif
}
NOINL void frswiglu(uint32_t g, uint32_t u, uint32_t r) {
silu_tile_init();
MATH(SFPU_BINARY_CALL_NO_TEMPLATE_ARGS(DST_SYNC_MODE, DST_ACCUM_MODE, me_rswiglu, g, u, r, VectorMode::RC));
}
NOINL void fsplit(uint32_t a, uint32_t lo) {
mul_binary_tile_init();
MATH(SFPU_BINARY_CALL_NO_TEMPLATE_ARGS(DST_SYNC_MODE, DST_ACCUM_MODE, me_split, a, lo, 0, VectorMode::RC));
}
NOINL void frope(uint32_t a, uint32_t r) {
mul_binary_tile_init();
MATH(SFPU_BINARY_CALL_NO_TEMPLATE_ARGS(DST_SYNC_MODE, DST_ACCUM_MODE, me_rope, a, r, 0, VectorMode::RC));
}
NOINL void frscale2(uint32_t a, uint32_t r) {
mul_binary_tile_init();
MATH(SFPU_BINARY_CALL_NO_TEMPLATE_ARGS(DST_SYNC_MODE, DST_ACCUM_MODE, me_rscale2, a, r, 0, VectorMode::RC));
}
NOINL void fgelu(uint32_t a) {
gelu_tile_init<false>();
gelu_tile<false>(a);
}
NOINL void fsilu(uint32_t a) {
silu_tile_init();
silu_tile(a);
}
NOINL void pack_to(uint32_t dst, uint32_t cb, uint32_t idx) {
pack_reconfig_data_format(cb);
pack_tile<true>(dst, cb, idx);
}
// weight stream: tiles are consumed in pages of PAGE_TILES (each wave's arena column is padded to whole pages)
struct WS {
uint32_t t = 0;
uint32_t used = 0; // tiles consumed in the current arena
ALWI uint32_t next() {
if (t == 0) {
cb_wait_front(CB_W, PAGE_TILES);
}
return t;
}
ALWI void done() {
++used;
if (++t == PAGE_TILES) {
cb_pop_front(CB_W, PAGE_TILES);
t = 0;
}
}
// n tiles (a power of two <= 8, starting n-aligned: never across a page)
ALWI void done_n(uint32_t n) {
used += n;
t += n;
if (t == PAGE_TILES) {
cb_pop_front(CB_W, PAGE_TILES);
t = 0;
}
}
};
WS ws;
// the largest power of two <= min(gmax, n) (geometry.pow2_groups)
ALWI uint32_t pow2_floor(uint32_t n, uint32_t gmax) {
uint32_t g = 1;
while (g * 2 <= n && g * 2 <= gmax) {
g *= 2;
}
return g;
}
NOINL void skip_w(uint32_t n) {
for (uint32_t i = 0; i < n; ++i) {
ws.next();
ws.done();
}
}
// end of an op segment: skip to the next page boundary (each op segment is page-padded)
NOINL void ws_align() {
if (ws.t != 0) {
skip_w(PAGE_TILES - ws.t);
}
}
// end of a wave's arena: skip the tiles not consumed (pad, or ops not run in a debug run)
NOINL void arena_end(uint32_t pages) {
skip_w(pages * PAGE_TILES - ws.used);
ws.used = 0;
}
// DST[s * nj + j] += sum_k in0[k * RB + s] @ W_k,j for every slot (block matmul: rt = RB slots, ct = nj, one unpack of
// the RB in0 tiles and the nj weight tiles per k); weights k-major, nj (a power of two) per k inside one page.
// Inactive slots compute garbage the callers discard (residual ops reload their x).
NOINL void mm_block_init(uint32_t in0, uint32_t in1, uint32_t ct, uint32_t rt) {
reconfig_data_format(in1, in0); // matmul: SrcA <- in1, SrcB <- in0
MATH((llk_math_matmul_init<FID, MM_THROTTLE>(in0, in1, 0, ct, rt)));
UNPACK((llk_unpack_AB_matmul_init(in0, in1, 0, ct, rt, 1)));
}
#ifdef ME_MM_BLOCK
NOINL void mm_slots(uint32_t in0, uint32_t K, uint32_t nj, uint32_t /*act*/) {
mm_block_init(in0, CB_W, nj, RB);
for (uint32_t k = 0; k < K; ++k) {
const uint32_t w = ws.next();
UNPACK((llk_unpack_AB_matmul(in0, CB_W, k * RB, w, nj, RB, 1)));
MATH((llk_math_matmul<FID, MM_THROTTLE>(0, nj, RB)));
ws.done_n(nj);
}
}
#else
// per-tile matmuls (measured faster than the LLK block matmul for these 2 x 2 .. 2 x 4 blocks: FC2 66 vs 109
// cycles per tile; HiFi4 peak is 64)
NOINL void mm_slots(uint32_t in0, uint32_t K, uint32_t nj, uint32_t /*act*/) {
mm_block_init(in0, CB_W, 1, 1);
for (uint32_t k = 0; k < K; ++k) {
const uint32_t w = ws.next();
for (uint32_t j = 0; j < nj; ++j) {
for (uint32_t s = 0; s < RB; ++s) {
UNPACK((llk_unpack_AB_matmul(in0, CB_W, k * RB + s, w + j)));
MATH((llk_math_matmul<FID, MM_THROTTLE>(s * nj + j)));
}
}
ws.done_n(nj);
}
}
#endif
// the extra K tile: DST[s * nj + j] += in0[tb + s * ts] @ X_j (ts = 0: one shared in0 tile; ts = 1: RB tiles)
NOINL void mm_extra(uint32_t in0, uint32_t tb, uint32_t ts, uint32_t nj, uint32_t /*act*/) {
if (ts == 1) {
mm_block_init(in0, CB_W, nj, RB);
const uint32_t w = ws.next();
UNPACK((llk_unpack_AB_matmul(in0, CB_W, tb, w, nj, RB, 1)));
MATH((llk_math_matmul<FID, MM_THROTTLE>(0, nj, RB)));
ws.done_n(nj);
return;
}
mm_block_init(in0, CB_W, nj, 1);
const uint32_t w = ws.next();
for (uint32_t s = 0; s < RB; ++s) {
UNPACK((llk_unpack_AB_matmul(in0, CB_W, tb, w, nj, 1, 1)));
MATH((llk_math_matmul<FID, MM_THROTTLE>(s * nj, nj, 1)));
}
ws.done_n(nj);
}
#if ME_PROF
ALWI volatile uint32_t* tprof_ptr() {
const auto& iface = get_local_cb_interface(CB_SYNC);
return reinterpret_cast<volatile uint32_t*>(((iface.fifo_limit - iface.fifo_size) << 4) + SYNC_PROF);
}
#define TPROF(i) UNPACK((tprof_ptr()[(i)] = get_timestamp_32b()))
#define TPROFP(i) PACK((tprof_ptr()[(i)] = get_timestamp_32b()))
#else
#define TPROF(i)
#define TPROFP(i)
#endif
bool wprof = false; // detail stamps of block 1, wave 0
#define WPROF(i) TPROF(wprof ? 256 + (i) : 511)
#define WPROFP(i) TPROFP(wprof ? 256 + (i) : 511)
bool aprof = false; // detail stamps of the first (q tile, chunk) of block 1's first item
#define APROF(i) TPROF(aprof ? 384 + (i) : 511)
#define APROFP(i) TPROFP(aprof ? 384 + (i) : 511)
// ---------------------------------------------------------------- debug probes (ME_PROBE)
uint32_t n_probe = N_PROBE; // probes left to take (armed by setting it to 0)
NOINL void probe(uint32_t cb, uint32_t tile) {
#if ME_PROBE
if (n_probe >= N_PROBE) {
return;
}
++n_probe;
cb_reserve_back(CB_PRB, 1);
tile_regs_acquire();
load(cb, tile, 0);
tile_regs_commit();
tile_regs_wait();
pack_to(0, CB_PRB, 0);
tile_regs_release();
cb_push_back(CB_PRB, 1);
#endif
}
NOINL void probe_fill() {
while (n_probe < N_PROBE) {
probe(CB_CF32, F_ZERO);
}
}
struct Me {
uint32_t x, y, h0, nh, n0, nx, f0, nf, eps, nl, dump, npe, npb, npbl, inv_d, inv_t;
uint32_t kd[RB];
};
Me me;
ALWI uint32_t gmin(uint32_t a, uint32_t b) { return a < b ? a : b; }
void kinds(uint32_t w) {
for (uint32_t s = 0; s < RB; ++s) {
me.kd[s] = kind_of(ri_of(w, me.y, s));
}
}
uint32_t mlp_mask(uint32_t l) {
uint32_t m = 0;
for (uint32_t s = 0; s < RB; ++s) {
m |= mlp_on(l, me.kd[s]) ? (1u << s) : 0u;
}
return m;
}
// ================================================================ residual x (CB_X: front = current x, s * NXM + j)
// x load (NW > 1): XL -> X (slots without a stored x stay 0)
NOINL void x_from_xl() {
cb_wait_front(CB_XL, RB * NXM);
cb_reserve_back(CB_X, RB * NXM);
for (uint32_t s = 0; s < RB; ++s) {
tile_regs_acquire();
const bool ld = me.kd[s] == KD_PREFIX || me.kd[s] == KD_PATCH;
for (uint32_t j = 0; j < me.nx; ++j) {
load(ld ? CB_XL : CB_CF32, ld ? s * NXM + j : F_ZERO, j);
}
tile_regs_commit();
tile_regs_wait();
pack_reconfig_data_format(CB_X);
for (uint32_t j = 0; j < me.nx; ++j) {
pack_tile<true>(j, CB_X, s * NXM + j);
}
tile_regs_release();
}
cb_push_back(CB_X, RB * NXM);
cb_pop_front(CB_XL, RB * NXM);
}
// x of slot s -> CB_XO (fp32, BRISC stores it to XD)
NOINL void x_out(uint32_t s) {
cb_reserve_back(CB_XO, NXM);
tile_regs_acquire();
for (uint32_t j = 0; j < me.nx; ++j) {
load(CB_X, s * NXM + j, j);
}
tile_regs_commit();
tile_regs_wait();
pack_reconfig_data_format(CB_XO);
for (uint32_t j = 0; j < me.nx; ++j) {
pack_tile<true>(j, CB_XO, j);
}
tile_regs_release();
cb_push_back(CB_XO, NXM);
}
// ================================================================ statistics
// producer: per-slot row sums of x / x^2 over the slice -> one packed fp32 tile STO (columns 2s, 2s + 1 = hi,
// 8 + 2s, 8 + 2s + 1 = lo: fp32-accurate through the consumer's tf32 matmuls); bf16(x) -> OUTX (j * RB + s)
NOINL void stats_out() {
const uint32_t nx = me.nx;
cb_wait_front(CB_X, RB * NXM);
cb_reserve_back(CB_SUMS, 4 * RB);
for (uint32_t s = 0; s < RB; ++s) {
tile_regs_acquire();
for (uint32_t j = 0; j < nx; ++j) {
load(CB_X, s * NXM + j, j);
}
// S at 4, Q at 5, square temp 6 (every SFPU operand written in this section)
load(CB_X, s * NXM, 4);
fmul(0, 0, 5);
for (uint32_t j = 1; j < nx; ++j) {
fadd(4, j, 4);
fmul(j, j, 6);
fadd(5, 6, 5);
}
fsplit(4, 6);
fsplit(5, 7);
tile_regs_commit();
tile_regs_wait();
pack_reconfig_data_format(CB_SUMS);
pack_tile<true>(4, CB_SUMS, 4 * s);
pack_tile<true>(6, CB_SUMS, 4 * s + 1);
pack_tile<true>(5, CB_SUMS, 4 * s + 2);
pack_tile<true>(7, CB_SUMS, 4 * s + 3);
tile_regs_release();
}
cb_push_back(CB_SUMS, 4 * RB);
// row sums (fp32 DST) -> hi / lo -> SUMS[0, 1] of the next cycle
cb_wait_front(CB_SUMS, 4 * RB);
tile_regs_acquire();
mm_init_n(CB_SUMS, CB_CONST, 0);
for (uint32_t s = 0; s < RB; ++s) {
mm_n(CB_SUMS, CB_CONST, 4 * s, C_L0 + 2 * s, 0);
mm_n(CB_SUMS, CB_CONST, 4 * s + 1, C_L0 + 2 * s, 0);
mm_n(CB_SUMS, CB_CONST, 4 * s + 2, C_L0 + 2 * s + 1, 0);
mm_n(CB_SUMS, CB_CONST, 4 * s + 3, C_L0 + 2 * s + 1, 0);
}
fsplit(0, 1);
tile_regs_commit();
cb_pop_front(CB_SUMS, 4 * RB);
cb_reserve_back(CB_SUMS, 4 * RB);
tile_regs_wait();
pack_reconfig_data_format(CB_SUMS);
pack_tile<true>(0, CB_SUMS, 0);
pack_tile<true>(1, CB_SUMS, 1);
tile_regs_release();
cb_push_back(CB_SUMS, 4 * RB);
cb_wait_front(CB_SUMS, 4 * RB);
cb_reserve_back(CB_STO, 1);
tile_regs_acquire();
mm_init_n(CB_SUMS, CB_CONST, 0);
mm_n(CB_SUMS, CB_CONST, 0, C_I, 0);
mm_n(CB_SUMS, CB_CONST, 1, C_SH8, 0);
tile_regs_commit();
tile_regs_wait();
pack_to(0, CB_STO, 0);
tile_regs_release();
cb_push_back(CB_STO, 1);
cb_pop_front(CB_SUMS, 4 * RB);
// bf16(x) slice, k-major
cb_reserve_back(CB_OUTX, NXM * RB);
for (uint32_t s = 0; s < RB; ++s) {
tile_regs_acquire();
for (uint32_t j = 0; j < nx; ++j) {
load(CB_X, s * NXM + j, j);
}
tile_regs_commit();
tile_regs_wait();
pack_reconfig_data_format(CB_OUTX);
for (uint32_t j = 0; j < nx; ++j) {
pack_tile<true>(j, CB_OUTX, j * RB + s);
}
tile_regs_release();
}
cb_push_back(CB_OUTX, NXM * RB);
}
// consumer: mu / rstd / std of every slot at once (columns s of one tile each, fp32 SFPU), then per slot
// EXT = [-mu | std] (bf16) and rstd broadcast over the columns (fp32) by selector matmuls
NOINL void stats_in() {
cb_wait_front(CB_STAT, C);
cb_reserve_back(CB_SC, 6);
tile_regs_acquire();
mm_init_n(CB_STAT, CB_CONST, 0);
for (uint32_t c = 0; c < C; ++c) {
mm_n(CB_STAT, CB_CONST, c, C_PA, 0);
}
for (uint32_t c = 0; c < C; ++c) {
mm_n(CB_STAT, CB_CONST, c, C_PB, 1);
}
fmulc(0, me.inv_d); // mu
fmulc(1, me.inv_d); // E[x^2]
fmul(0, 0, 4); // mu^2
fsub(1, 4, 2);
faddc(2, me.eps); // var + eps
fsub(1, 4, 3);
faddc(3, me.eps);
frsqrt(2); // rstd
fsqrt(3); // std
fsplit(0, 1); // mu hi (0) / lo (1)
fsplit(2, 5); // rstd hi (2) / lo (5): its broadcast below stays fp32-accurate
fsplit(3, 6); // std hi (3) / lo (6)
tile_regs_commit();
tile_regs_wait();
pack_reconfig_data_format(CB_SC);
pack_tile<true>(0, CB_SC, 0);
pack_tile<true>(1, CB_SC, 1);
pack_tile<true>(2, CB_SC, 2);
pack_tile<true>(5, CB_SC, 3);
pack_tile<true>(3, CB_SC, 4);
pack_tile<true>(6, CB_SC, 5);
tile_regs_release();
cb_push_back(CB_SC, 6);
cb_pop_front(CB_STAT, C);
cb_wait_front(CB_SC, 6);
cb_reserve_back(CB_EXT, RB);
cb_reserve_back(CB_RSTD, RB);
tile_regs_acquire();
mm_init_n(CB_SC, CB_CONST, 0);
for (uint32_t s = 0; s < RB; ++s) { // EXT = [-mu hi, -mu lo, std hi, std lo, -mu hi, std hi] (arena._ext)
mm_n(CB_SC, CB_CONST, 0, C_PM0 + s, s);
mm_n(CB_SC, CB_CONST, 1, C_PM1 + s, s);
mm_n(CB_SC, CB_CONST, 4, C_PS0 + s, s);
mm_n(CB_SC, CB_CONST, 5, C_PS1 + s, s);
}
for (uint32_t s = 0; s < RB; ++s) {
mm_n(CB_SC, CB_CONST, 2, C_SEL0 + s, 4 + s);
mm_n(CB_SC, CB_CONST, 3, C_SEL0 + s, 4 + s);
}
tile_regs_commit();
tile_regs_wait();
pack_reconfig_data_format(CB_EXT);
for (uint32_t s = 0; s < RB; ++s) {
pack_tile<true>(s, CB_EXT, s);
}
pack_reconfig_data_format(CB_RSTD);
for (uint32_t s = 0; s < RB; ++s) {
pack_tile<true>(4 + s, CB_RSTD, s);
}
tile_regs_release();
cb_push_back(CB_EXT, RB);
cb_push_back(CB_RSTD, RB);
cb_pop_front(CB_SC, 6);
cb_wait_front(CB_EXT, RB);
cb_wait_front(CB_RSTD, RB);
}
NOINL void stats_pop() {
cb_pop_front(CB_EXT, RB);
cb_pop_front(CB_RSTD, RB);
}
// ================================================================ residual matmul ops (embed / proj / fc2)
// mode 0: embed (in0 = patches, K = PE_T; prefix slots copy PFX), 1: proj (in0 = attention output, K = DT),
// 2: fc2 (in0 = FC1 output, K = FT). x_new = x_old (or 0 / PFX) + in0 @ W + E0 @ [b] for the active slots.
NOINL void residual_op(uint32_t mode, uint32_t act) {
const uint32_t K = mode == 0 ? PE_T : mode == 1 ? DT : FT;
cb_wait_front(CB_INF, INF_CAP);
WPROF(mode == 1 ? 8 : 9);
if (mode != 0) {
cb_wait_front(CB_X, RB * NXM);
}
cb_reserve_back(CB_X, RB * NXM);
for (uint32_t g0 = 0; g0 < me.nx;) {
const uint32_t nj = pow2_floor(me.nx - g0, G);
tile_regs_acquire();
if (mode != 0) { // every accumulator starts from x (fp32 unpack to DST)
for (uint32_t s = 0; s < RB; ++s) {
for (uint32_t j = 0; j < nj; ++j) {
load(CB_X, s * NXM + g0 + j, s * nj + j);
}
}
}
mm_slots(CB_INF, K, nj, act);
mm_extra(CB_CONST, C_E0, 0, nj, act);
// after the matmuls: inactive slots get their x back (frozen prefix) / prefix constants / zeros (embed).
// An fp32 unpack-to-DST into tile i >= 4 clears the DST zero flags of tile i & 3 (tt-llk budabackend#2730
// workaround, 4 fp32 tiles per bank): never before an FPU accumulation into a fresh tile (MK_LOG round 2).
for (uint32_t s = 0; s < RB; ++s) {
if (act & (1u << s)) {
continue;
}
for (uint32_t j = 0; j < nj; ++j) {
if (mode != 0) {
load(CB_X, s * NXM + g0 + j, s * nj + j);
} else if (me.kd[s] == KD_PREFIX) {
load(CB_PFX, g0 + j, s * nj + j);
} else {
load(CB_CF32, F_ZERO, s * nj + j);
}
}
}
tile_regs_commit();
tile_regs_wait();
pack_reconfig_data_format(CB_X);
for (uint32_t s = 0; s < RB; ++s) {
for (uint32_t j = 0; j < nj; ++j) {
// the new x sits after the old one in the FIFO: pack_tile<true> indexes from the write pointer
pack_tile<true>(s * nj + j, CB_X, s * NXM + g0 + j);
}
}
tile_regs_release();
g0 += nj;
}
ws_align();
if (mode != 0) {
cb_pop_front(CB_X, RB * NXM);
}
cb_push_back(CB_X, RB * NXM);
cb_pop_front(CB_INF, INF_CAP);
}
// ================================================================ QKV(l)
NOINL void qkv(uint32_t l) {
cb_wait_front(CB_INX, INX_CAP);
WPROF(10);
stats_in();
WPROFP(11);
cb_wait_front(CB_TAB, 2 * RB);
for (uint32_t jh = 0; jh < me.nh; ++jh) {
const uint32_t u = me.h0 + jh, ty = unit_type(u);
uint32_t act = 0;
for (uint32_t s = 0; s < RB; ++s) {
act |= ((qkv_mask(l, me.kd[s]) >> ty) & 1u) << s;
}
if constexpr (RB <= 2) { // epilogue in DST: accumulators 0 .. 3, rstd / cos / sin of a slot at 4 / 5 / 6
tile_regs_acquire();
mm_slots(CB_INX, DT, 2, act);
mm_extra(CB_EXT, 0, 1, 2, act);
for (uint32_t s = 0; s < RB; ++s) {
if (!(act & (1u << s))) {
continue;
}
load(CB_RSTD, s, 4);
if (ty < 2 && me.kd[s] == KD_PATCH) {
load(CB_TAB, 2 * s, 5);
load(CB_TAB, 2 * s + 1, 6);
frope(2 * s, 4);
} else {
frscale2(2 * s, 4);
}
}
const bool lo = ty == 1 && klo_wave(l, me.kd); // k: hi (bf16-exact) in place, lo at 4 + i
if (lo) {
for (uint32_t i = 0; i < 2 * RB; ++i) {
fsplit(i, 4 + i);
}
}
tile_regs_commit();
cb_reserve_back(CB_OUT, 2 * RB);
if (lo) {
cb_reserve_back(CB_OUTLO, 2 * RB);
}
tile_regs_wait();
pack_reconfig_data_format(CB_OUT);
for (uint32_t i = 0; i < 2 * RB; ++i) {
pack_tile<true>(i, CB_OUT, i);
}
if (lo) {
for (uint32_t i = 0; i < 2 * RB; ++i) {
pack_tile<true>(4 + i, CB_OUTLO, i);
}
}
tile_regs_release();
cb_push_back(CB_OUT, 2 * RB);
if (lo) {
cb_push_back(CB_OUTLO, 2 * RB);
}
continue;
}
tile_regs_acquire();
mm_slots(CB_INX, DT, 2, act);
mm_extra(CB_EXT, 0, 1, 2, act);
tile_regs_commit();
cb_reserve_back(CB_RAW, 8);
tile_regs_wait();
pack_reconfig_data_format(CB_RAW);
for (uint32_t i = 0; i < 2 * RB; ++i) {
pack_tile<true>(i, CB_RAW, i);
}
tile_regs_release();
cb_push_back(CB_RAW, 8);
cb_wait_front(CB_RAW, 8);
const bool lo = ty == 1 && klo_wave(l, me.kd);
cb_reserve_back(CB_OUT, 2 * RB);
if (lo) {
cb_reserve_back(CB_OUTLO, 2 * RB);
}
for (uint32_t s = 0; s < RB; ++s) {
if (!(act & (1u << s))) {
continue;
}
tile_regs_acquire();
load(CB_RAW, 2 * s, 0);
load(CB_RAW, 2 * s + 1, 1);
load(CB_RSTD, s, 4);
if (ty < 2 && me.kd[s] == KD_PATCH) {
load(CB_TAB, 2 * s, 5);
load(CB_TAB, 2 * s + 1, 6);
frope(0, 4);
} else {
frscale2(0, 4);
}
if (lo) {
fsplit(0, 2);
fsplit(1, 3);
}
tile_regs_commit();
tile_regs_wait();
pack_reconfig_data_format(CB_OUT);
pack_tile<true>(0, CB_OUT, 2 * s);
pack_tile<true>(1, CB_OUT, 2 * s + 1);
if (lo) {
pack_tile<true>(2, CB_OUTLO, 2 * s);
pack_tile<true>(3, CB_OUTLO, 2 * s + 1);
}
tile_regs_release();
}
cb_push_back(CB_OUT, 2 * RB);
if (lo) {
cb_push_back(CB_OUTLO, 2 * RB);
}
cb_pop_front(CB_RAW, 8);
}
ws_align();
cb_pop_front(CB_INX, INX_CAP);
stats_pop();
}
// ================================================================ FC1(l), pipelined (ME_FC1P: rb == 2, GELU)
// mk_vision's MV_FC1P (VLA-JEPA, MK_LOG 3.10): for this phase both threads switch the DST to half-sync (two halves
// of 4 fp32 tiles), so the math thread's matmuls into one half overlap the pack thread's SFPU epilogue (rstd x GELU)
// + pack of the other. Half layout: 0, 1 = the f tile's two slot accumulators, 2, 3 = the slots' rstd, loaded once
// per half (first two sections). A section is released without the half-wide ZEROACC: the pack thread clears only
// the accumulators (an FPU accumulation onto rows the SFPU wrote is wrong), so the operand tiles survive. h_end
// returns to full sync and closes with one empty standard section, whose release clears the whole DST.
#if ME_FC1P
NOINL void h_begin() {
MATH((_llk_math_pack_sync_init_<DstSync::SyncHalf, DST_ACCUM_MODE>()));
PACK((_llk_pack_dest_init_<DstSync::SyncHalf, DST_ACCUM_MODE>()));
}
NOINL void h_end() {
MATH((_llk_math_pack_sync_init_<DstSync::SyncFull, DST_ACCUM_MODE>()));
PACK((_llk_pack_dest_init_<DstSync::SyncFull, DST_ACCUM_MODE>()));
tile_regs_acquire();
tile_regs_commit();
tile_regs_wait();
tile_regs_release();
}
ALWI void h_acquire() { MATH((_llk_math_wait_for_dest_available_<DstSync::SyncHalf>())); }
ALWI void h_commit() { MATH((_llk_math_dest_section_done_<DstSync::SyncHalf, DST_ACCUM_MODE>())); }
ALWI void h_wait() {
PACK(TTI_SEMWAIT(p_stall::STALL_TDMA | p_stall::STALL_SYNC | p_stall::STALL_SFPU,
ckernel::semaphore::t6_sem(ckernel::semaphore::MATH_PACK), p_stall::STALL_ON_ZERO));
}
NOINL void h_release() {
PACK(({
TTI_STALLWAIT(p_stall::STALL_MATH | p_stall::STALL_SFPU | p_stall::STALL_SYNC, p_stall::PACK | p_stall::WAIT_SFPU);
ckernel::math::set_dst_write_addr<DstTileShape::Tile32x32, UnpackDestination::SrcRegs>(0);
for (uint32_t i = 0; i < 8; ++i) {
TT_ZEROACC(p_zeroacc::CLR_16, 1, 0, ADDR_MOD_7, i);
}
TTI_STALLWAIT(p_stall::STALL_SYNC, p_stall::MATH);
_llk_pack_dest_section_done_<DstSync::SyncHalf, DST_ACCUM_MODE, false>();
}));
}
NOINL void fc1(uint32_t /*act*/) {
static_assert(RB == 2 && !SWIGLU, "pipelined FC1");
cb_wait_front(CB_INX, INX_CAP);
WPROF(6);
stats_in();
WPROFP(7);
h_begin();
PACK((::ckernel::llk_math_eltwise_unary_sfpu_init<SfpuType::gelu>(::ckernel::sfpu::me_gelu_init)));
pack_reconfig_data_format(CB_FO);
for (uint32_t t = 0; t < me.nf; ++t) {
const uint32_t slot = t % FB;
h_acquire();
if (t < 2) { // the operand tiles of this half
load(CB_RSTD, 0, 2);
load(CB_RSTD, 1, 3);
mm_init_n(CB_INX, CB_W, 0);
}
for (uint32_t k = 0; k < DT; ++k) {
const uint32_t w = ws.next();
mm_f(CB_INX, CB_W, 2 * k, w, 0);
mm_f(CB_INX, CB_W, 2 * k + 1, w, 1);
ws.done();
}
{ // the extra K tile [-mu | std] @ [c ; bias'] (CB_EXT: same tile format as CB_INX, no re-init)
const uint32_t w = ws.next();
mm_f(CB_EXT, CB_W, 0, w, 0);
mm_f(CB_EXT, CB_W, 1, w, 1);
ws.done();
}
h_commit();
if (slot == 0) {
cb_reserve_back(CB_FO, FB * RB);
}
h_wait();
PACK(SFPU_BINARY_CALL_NO_TEMPLATE_ARGS(DST_SYNC_MODE, DST_ACCUM_MODE, me_rgelu, 0, 2, 0, VectorMode::RC));
PACK(SFPU_BINARY_CALL_NO_TEMPLATE_ARGS(DST_SYNC_MODE, DST_ACCUM_MODE, me_rgelu, 1, 3, 1, VectorMode::RC));
PACK(TTI_STALLWAIT(p_stall::STALL_PACK, p_stall::WAIT_SFPU));
pack_tile<true>(0, CB_FO, slot * RB);
pack_tile<true>(1, CB_FO, slot * RB + 1);
h_release();
if (slot + 1 == FB || t + 1 == me.nf) {
cb_push_back(CB_FO, FB * RB);
}
}
h_end();
ws_align();
cb_pop_front(CB_INX, INX_CAP);
stats_pop();
}
#else
// ================================================================ FC1(l)
NOINL void fc1(uint32_t act) {
constexpr uint32_t TW = SWIGLU ? 2 : 1; // weight tiles per hidden tile
cb_wait_front(CB_INX, INX_CAP);
WPROF(6);
stats_in();
WPROFP(7);
// epilogue in DST: accumulators s * nt + t (rb * nt <= 4), rstd of slot s at 4 + s (loaded after the matmuls)
for (uint32_t fb = 0; fb < me.nf;) {
const uint32_t nb = pow2_floor(me.nf - fb, FB), nt = nb * TW;
tile_regs_acquire();
mm_slots(CB_INX, DT, nt, act);
mm_extra(CB_EXT, 0, 1, nt, act);
for (uint32_t s = 0; s < RB; ++s) {
if (!(act & (1u << s))) {
continue;
}
load(CB_RSTD, s, 4 + s);
for (uint32_t jj = 0; jj < nb; ++jj) {
if (SWIGLU) {
frswiglu(s * nt + 2 * jj, s * nt + 2 * jj + 1, 4 + s);
} else {
frgelu(s * nt + jj, 4 + s);
}
}
}
tile_regs_commit();
cb_reserve_back(CB_FO, FB * RB);
tile_regs_wait();
pack_reconfig_data_format(CB_FO);
for (uint32_t s = 0; s < RB; ++s) {
for (uint32_t jj = 0; jj < nb; ++jj) {
pack_tile<true>(s * nt + (SWIGLU ? 2 * jj : jj), CB_FO, jj * RB + s);
}
}
tile_regs_release();
cb_push_back(CB_FO, FB * RB);
fb += nb;
}
ws_align();
cb_pop_front(CB_INX, INX_CAP);
stats_pop();
}
#endif // ME_FC1P
// ================================================================ tap / pool
NOINL void tap_out() {
for (uint32_t s = 0; s < RB; ++s) {
if (!tap_on(me.kd[s])) {
continue;
}
cb_reserve_back(CB_TAP, NXM);
tile_regs_acquire();
for (uint32_t j = 0; j < me.nx; ++j) {
load(CB_X, s * NXM + j, j);
}
tile_regs_commit();
tile_regs_wait();
pack_reconfig_data_format(CB_TAP);
for (uint32_t j = 0; j < me.nx; ++j) {
pack_tile<true>(j, CB_TAP, j);
}
tile_regs_release();
cb_push_back(CB_TAP, NXM);
}
}
// a sync word written by the BRISC before it pushed CB_CONST: the unpack thread (which waited on CB_CONST) reads it
// and hands it to the math / pack threads through the mailboxes (cb_api.h read_tile_value), so all three agree
NOINL uint32_t sync_value(uint32_t w) {
uint32_t v = 0;
UNPACK({
asm volatile("fence" ::: "memory");
const auto& iface = get_local_cb_interface(CB_SYNC);
v = reinterpret_cast<volatile uint32_t*>(((iface.fifo_limit - iface.fifo_size) << 4) + w * SYNC_STRIDE)[0];
ckernel::mailbox_write(ckernel::ThreadId::MathThreadId, v);
ckernel::mailbox_write(ckernel::ThreadId::PackThreadId, v);
})
MATH(v = ckernel::mailbox_read(ckernel::ThreadId::UnpackThreadId);)
PACK(v = ckernel::mailbox_read(ckernel::ThreadId::UnpackThreadId);)
return v;
}
// before LN1 of a joint block: prefix slots ship their x (XD), the pool slot's x = mean over frames < n of those
NOINL void pool() {
bool own = false;
for (uint32_t s = 0; s < RB; ++s) {
if (me.kd[s] == KD_PREFIX) {
x_out(s);
}
own = own || me.kd[s] == KD_POOL;
}
if (!own) {
return;
}
cb_wait_front(CB_X, RB * NXM);
cb_reserve_back(CB_X, RB * NXM);
for (uint32_t s = 0; s < RB; ++s) {
tile_regs_acquire();
if (me.kd[s] == KD_POOL) {
for (uint32_t t = 0; t < g_nv; ++t) {
cb_wait_front(CB_PL, NXM);
for (uint32_t j = 0; j < me.nx; ++j) {
if (t == 0) {
load(CB_PL, j, j);
} else {
load(CB_PL, j, 4 + j);
fadd(j, 4 + j, j);
}
}
cb_pop_front(CB_PL, NXM);
}
for (uint32_t j = 0; j < me.nx; ++j) {
fmulc(j, me.inv_t);
}
} else {
for (uint32_t j = 0; j < me.nx; ++j) {
load(CB_X, s * NXM + j, j);
}
}
tile_regs_commit();
tile_regs_wait();
pack_reconfig_data_format(CB_X);
for (uint32_t j = 0; j < me.nx; ++j) {
pack_tile<true>(j, CB_X, s * NXM + j);
}
tile_regs_release();
}
cb_pop_front(CB_X, RB * NXM);
cb_push_back(CB_X, RB * NXM);
}
// ================================================================ attention item
constexpr uint32_t SUB = 6; // score tiles per DST section (DST 0 .. 5; 6 = the shift, 7 = a mask temp)
// DST jj = q_i @ K_{j0 + jj}^T (+ mask), jj < n
NOINL void scores(uint32_t i, uint32_t j0, uint32_t n, const uint32_t* mk, bool first_chunk) {
mm_init_n(CB_AQ, CB_KV, 1);
for (uint32_t jj = 0; jj < n; ++jj) {
for (uint32_t d = 0; d < 2; ++d) {
mm_n(CB_AQ, CB_KV, 2 * i + d, 2 * (j0 + jj) + d, jj);
}
if (KLO == 2 || (KLO == 1 && first_chunk && j0 + jj == 0)) { // + q k_lo^T
for (uint32_t d = 0; d < 2; ++d) {
mm_n(CB_AQ, CB_KV, 2 * i + d, 4 * KC + (KLO == 2 ? 2 * (j0 + jj) : 0) + d, jj);
}
}
}
for (uint32_t jj = 0; jj < n; ++jj) {
if (mk[j0 + jj]) {
load(CB_CF32, mk[j0 + jj] == 1 ? F_MASKP : F_MASKN, 7);
fadd(jj, 7, jj);
}
}
}
// the score region (S32 / S32F / AP views, KC fp32 tiles): every view pushes / pops exactly KC tiles per use
ALWI void region_push(uint32_t cb) {
cb_push_back(cb, KC);
}
// row max of the nk fp32 tiles of the score region (FPU reduce; tf32 operand: rel err 2^-11) -> CB_MX
NOINL void rowmax(uint32_t nk) {
cb_wait_front(CB_S32F, KC);
cb_reserve_back(CB_MX, 1);
tile_regs_acquire();
reconfig_data_format(CB_S32F, CB_CONST);
reduce_init<PoolType::MAX, ReduceDim::REDUCE_ROW>(CB_S32F, CB_CONST, CB_MX);
for (uint32_t j = 0; j < nk; ++j) {
reduce_tile<PoolType::MAX, ReduceDim::REDUCE_ROW>(CB_S32F, CB_CONST, j, C_ONES, 0);
}
tile_regs_commit();
tile_regs_wait();
pack_to(0, CB_MX, 0);
tile_regs_release();
reduce_uninit(CB_S32F);
cb_push_back(CB_MX, 1);
cb_pop_front(CB_S32F, KC);
cb_wait_front(CB_MX, 1);
}
// DST dst = the CB_MX column vector broadcast over the columns
NOINL void bcast_m(uint32_t dst) {
reconfig_data_format(CB_CONST, CB_MX);
mul_bcast_cols_init(CB_CONST, CB_MX);
mul_tiles_bcast_cols(CB_CONST, CB_MX, C_ONES, 0, dst);
}
// dst[0 .. n) = exp(dst[0 .. n) - dst[m]): ME_EXP 1 fused sfpi exp_21f, 0 fused fp32-accurate exp, 2 the max
// subtraction then the stock TTI exp_21f per tile (result rounded to bf16 precision)
NOINL void fsubexp_n(uint32_t n, uint32_t m) {
#if ME_EXP == 2
MATH(SFPU_BINARY_CALL_NO_TEMPLATE_ARGS(DST_SYNC_MODE, DST_ACCUM_MODE, me_sub_n, n, m, 0, VectorMode::RC));
exp_tile_init<false, 0x3F800000, InputClamping::ClampToNegative, false>();
for (uint32_t t = 0; t < n; ++t) {
exp_tile<false, false, InputClamping::ClampToNegative, 8, false>(t);
}
#else
MATH(SFPU_BINARY_CALL_NO_TEMPLATE_ARGS(DST_SYNC_MODE, DST_ACCUM_MODE, me_subexp_n<ME_EXP == 1>, n, m, 0,
VectorMode::RC));
#endif
}
// dst[a] = exp(dst[a] - dst[m]) (fused SFPU)
NOINL void fsubexp(uint32_t a, uint32_t m) {
MATH(SFPU_BINARY_CALL_NO_TEMPLATE_ARGS(DST_SYNC_MODE, DST_ACCUM_MODE, me_subexp<ME_EXP == 1>, a, m, a,
VectorMode::RC));
}
// Item (head, q block): softmax over chunks of KC key tiles, online across chunks. Per (q tile, chunk):
// S = q K^T + mask (fp32, score region); m = row max of S (FPU reduce, tf32 operand: rel err 2^-11);
// giant-logit blocks (|S| up to 2.6e6): D = S - m1 in place, m2 = row max of D, m = m1 + m2 (err ~ |S| 2^-22);
// m_new = max(m_old, m), alpha = exp(m_old - m_new); P = exp(S - m_new) (fused SFPU, in place);
// O = alpha O + P V, l = alpha l + P @ ONES (state FIFO CB_ST across chunks; one chunk: O / l directly).
NOINL void att_item(const Item& it, bool two) {
const uint32_t L = seg_len(it);
const uint32_t nch = (L + KC - 1) / KC;
cb_wait_front(CB_AQ, QB * 2);
for (uint32_t c = 0; c < nch; ++c) {
const uint32_t p0 = c * KC, nk = gmin(KC, L - p0);
const bool last = c + 1 == nch;
uint32_t mk[KC];
for (uint32_t j = 0; j < nk; ++j) {
mk[j] = key_mask(seg_key(it, p0 + j));
}
cb_wait_front(CB_KV, CHT);
for (uint32_t i = 0; i < it.nq; ++i) {
APROF(0);
// S (fp32) -> region (views S32: P / D pass, S32F: max)
cb_reserve_back(CB_S32, KC);
cb_reserve_back(CB_S32F, KC);
for (uint32_t j0 = 0; j0 < nk; j0 += SUB) {
const uint32_t n = gmin(SUB, nk - j0);
tile_regs_acquire();
scores(i, j0, n, mk, c == 0);
tile_regs_commit();
tile_regs_wait();
pack_reconfig_data_format(CB_S32);
for (uint32_t jj = 0; jj < n; ++jj) {
pack_tile<true>(jj, CB_S32, j0 + jj);
}
tile_regs_release();
}
region_push(CB_S32);
region_push(CB_S32F);
APROFP(1);
rowmax(nk);
APROFP(2);
if (c < 2 && i == 0) {
probe(CB_MX, 0);
}
if (two) { // m1 -> CB_MF1; D = S - m1 in place -> m2
cb_reserve_back(CB_MF1, 1);
tile_regs_acquire();
bcast_m(0);
tile_regs_commit();
tile_regs_wait();
pack_to(0, CB_MF1, 0);
tile_regs_release();
cb_push_back(CB_MF1, 1);
cb_pop_front(CB_MX, 1);
cb_wait_front(CB_S32, KC);
cb_wait_front(CB_MF1, 1);
for (uint32_t j0 = 0; j0 < nk; j0 += SUB) {
const uint32_t n = gmin(SUB, nk - j0);
tile_regs_acquire();
for (uint32_t jj = 0; jj < n; ++jj) {
load(CB_S32, j0 + jj, jj);
}
load(CB_MF1, 0, 6);
for (uint32_t jj = 0; jj < n; ++jj) {
fsub(jj, 6, jj);
}
tile_regs_commit();
tile_regs_wait();
pack_reconfig_data_format(CB_S32);
for (uint32_t jj = 0; jj < n; ++jj) {
pack_tile<true>(jj, CB_S32, j0 + jj); // the slot S occupied: still the front of S32
}
tile_regs_release();
}
cb_pop_front(CB_S32, KC);
cb_reserve_back(CB_S32, KC);
cb_reserve_back(CB_S32F, KC);
region_push(CB_S32);
region_push(CB_S32F);
rowmax(nk);
}
// m_new = max(m_old, m), alpha = exp(m_old - m_new), shift = m_new (giant: m_new - m1, P from D)
cb_reserve_back(CB_MFN, 3);
tile_regs_acquire();
bcast_m(0);
if (two) {
load(CB_MF1, 0, 3);
fadd(0, 3, 0);
}
if (c > 0) {
cb_wait_front(CB_ST, 4);
load(CB_ST, 3, 1);
fmax(0, 1, 0);
fsub(1, 0, 2);
fexp(2);
}
if (two) {
fsub(0, 3, 4);
}
tile_regs_commit();
tile_regs_wait();
pack_reconfig_data_format(CB_MFN);
pack_tile<true>(0, CB_MFN, 0);
pack_tile<true>(2, CB_MFN, 1);
pack_tile<true>(two ? 4 : 0, CB_MFN, 2);
tile_regs_release();
cb_push_back(CB_MFN, 3);
cb_pop_front(CB_MX, 1);
if (two) {
cb_pop_front(CB_MF1, 1);
}
APROFP(3);
// P = exp(S - shift) (fused SFPU, in place: view AP)
cb_wait_front(CB_MFN, 3);
if (c < 2 && i == 0) {
probe(CB_MFN, 0);
probe(CB_MFN, 1);
}
cb_wait_front(CB_S32, KC);
cb_reserve_back(CB_AP, KC);
for (uint32_t j0 = 0; j0 < nk; j0 += SUB) {
const uint32_t n = gmin(SUB, nk - j0);
tile_regs_acquire();
reconfig_data_format_srca(CB_S32);
copy_init(CB_S32);
for (uint32_t jj = 0; jj < n; ++jj) {
copy_tile(CB_S32, j0 + jj, jj);
}
load(CB_MFN, 2, 6);
mul_binary_tile_init();
fsubexp_n(n, 6);
tile_regs_commit();
tile_regs_wait();
pack_reconfig_data_format(CB_AP);
for (uint32_t jj = 0; jj < n; ++jj) {
pack_tile<true>(jj, CB_AP, j0 + jj);
}
tile_regs_release();
}
cb_pop_front(CB_S32, KC);
region_push(CB_AP);
APROFP(4);
// O = alpha O + P V, l = alpha l + P @ ONES
cb_wait_front(CB_AP, KC);
if (c == 0 && i == 0) {
probe(CB_AP, 0);
probe(CB_AP, 1);
}
const bool direct = last && c == 0; // one chunk: O / l straight to the output
if (direct) {
cb_reserve_back(CB_AO, 2);
} else {
cb_reserve_back(CB_ST, 4);
}
tile_regs_acquire();
mm_init_n(CB_AP, CB_CONST, 0);
for (uint32_t j = 0; j < nk; ++j) {
mm_f(CB_AP, CB_CONST, j, C_ONES, 2);
}
mm_init_n(CB_AP, CB_KV, 0);
for (uint32_t j = 0; j < nk; ++j) {
for (uint32_t d = 0; d < 2; ++d) {
mm_f(CB_AP, CB_KV, j, 2 * KC + 2 * j + d, d);
}
}
if (c > 0) {
load(CB_ST, 0, 3);
load(CB_ST, 1, 4);
load(CB_ST, 2, 5);
load(CB_MFN, 1, 6);
fmul(3, 6, 3);
fadd(0, 3, 0);
fmul(4, 6, 4);
fadd(1, 4, 1);
fmul(5, 6, 5);
fadd(2, 5, 2);
}
if (direct) {
frecip(2);
fmul(0, 2, 0);
fmul(1, 2, 1);
} else {
load(CB_MFN, 0, 7);
}
tile_regs_commit();
tile_regs_wait();
if (direct) {
pack_reconfig_data_format(CB_AO);
pack_tile<true>(0, CB_AO, 0);
pack_tile<true>(1, CB_AO, 1);
} else {
pack_reconfig_data_format(CB_ST);
pack_tile<true>(0, CB_ST, 0);
pack_tile<true>(1, CB_ST, 1);
pack_tile<true>(2, CB_ST, 2);
pack_tile<true>(7, CB_ST, 3);
}
tile_regs_release();
if (c > 0) {
cb_pop_front(CB_ST, 4);
}
if (direct) {
cb_push_back(CB_AO, 2);
} else {
cb_push_back(CB_ST, 4);
if (c < 2 && i == 0) {
cb_wait_front(CB_ST, 4);
for (uint32_t q = 0; q < 4; ++q) {
probe(CB_ST, q);
}
}
}
cb_pop_front(CB_AP, KC);
cb_pop_front(CB_MFN, 3);
APROFP(5);
aprof = false;
}
cb_pop_front(CB_KV, CHT);
}
// O / l -> bf16 (several chunks)
for (uint32_t i = 0; nch > 1 && i < it.nq; ++i) {
cb_wait_front(CB_ST, 4);
cb_reserve_back(CB_AO, 2);
tile_regs_acquire();
load(CB_ST, 0, 0);
load(CB_ST, 1, 1);
load(CB_ST, 2, 2);
frecip(2);
fmul(0, 2, 0);
fmul(1, 2, 1);
tile_regs_commit();
tile_regs_wait();
pack_reconfig_data_format(CB_AO);
pack_tile<true>(0, CB_AO, 0);
pack_tile<true>(1, CB_AO, 1);
tile_regs_release();
cb_push_back(CB_AO, 2);
cb_pop_front(CB_ST, 4);
}
cb_pop_front(CB_AQ, QB * 2);
}
NOINL void att(uint32_t l) {
Item it;
TPROF(192 + 2 * l);
const uint32_t n = n_jobs(l);
bool first = true;
for (uint32_t g = me.x; g < n; g += C) {
job_of(l, g, me.y, it);
if (ME_PROBE && l == 0 && first && me.x == 0 && me.y == 0) {
n_probe = 0;
}
aprof = l == 1 && first;
att_item(it, giant(l));
if (ME_PROBE && l == 0 && first && me.x == 0 && me.y == 0) {
probe_fill();
}
first = false;
}
TPROFP(193 + 2 * l);
}
// ================================================================ waves
uint32_t tiles_qkv() { return me.nh * 2 * (DT + 1); }
// after the residual stream of block l (l = -1: after the embedding): pool, LN1 and QKV(l + 1)
NOINL void tail(int l) {
const uint32_t l1 = (uint32_t)(l + 1);
if (l1 >= NL) { // QKV(l + 1) runs even past a debug stop (nl < L): the staging then holds block nl's q / k / v
return;
}
if (POOL && joint(l1)) {
pool();
}
stats_out();
qkv(l1);
}
NOINL void wave_end(int l, bool store) {
if (store) {
for (uint32_t s = 0; s < RB; ++s) {
if (me.kd[s] == KD_PREFIX || me.kd[s] == KD_PATCH) {
x_out(s);
}
}
}
if (NW > 1) {
cb_pop_front(CB_X, RB * NXM);
}
cb_pop_front(CB_TAB, 2 * RB);
arena_end(l < 0 ? me.npe : ((uint32_t)l == NL - 1 ? me.npbl : me.npb));
}
} // namespace
void kernel_main() {
me.x = get_arg_val<uint32_t>(A_X);
me.y = get_arg_val<uint32_t>(A_Y);
me.h0 = get_arg_val<uint32_t>(A_H0);
me.nh = get_arg_val<uint32_t>(A_NH);
me.n0 = get_arg_val<uint32_t>(A_N0);
me.nx = get_arg_val<uint32_t>(A_NX);
me.f0 = get_arg_val<uint32_t>(A_F0);
me.nf = get_arg_val<uint32_t>(A_NF);
me.npe = get_arg_val<uint32_t>(A_NPE);
me.npb = get_arg_val<uint32_t>(A_NPB);
me.npbl = get_arg_val<uint32_t>(A_NPBL);
me.nl = get_common_arg_val<uint32_t>(K_NL);
me.dump = get_common_arg_val<uint32_t>(K_DUMP);
me.eps = get_common_arg_val<uint32_t>(K_EPS);
me.inv_d = get_common_arg_val<uint32_t>(K_INVD);
me.inv_t = get_common_arg_val<uint32_t>(K_INVT);
compute_kernel_hw_startup<SrcOrder::Reverse>(CB_INX, CB_W, CB_OUT);
cb_wait_front(CB_CONST, N_CONST);
cb_wait_front(CB_CF32, N_CF32);
cb_wait_front(CB_PFX, NXM);
if constexpr (POOL) { // runtime valid frames (me_brisc init_consts)
g_nv = sync_value(W_NV);
me.inv_t = sync_value(W_INVN);
}
// E: embedding (+ LN1 / QKV of block 0)
for (uint32_t w = 0; w < NW; ++w) {
kinds(w);
uint32_t act = 0;
for (uint32_t s = 0; s < RB; ++s) {
act |= (me.kd[s] == KD_PATCH ? 1u : 0u) << s;
}
residual_op(0, act);
tail(-1);
wave_end(-1, NW > 1 || (me.dump && me.nl == 0));
}
for (uint32_t l = 0; l < me.nl; ++l) {
att(l);
for (uint32_t w = 0; w < NW; ++w) {
kinds(w);
if (NW > 1) {
x_from_xl();
}
const uint32_t act = mlp_mask(l);
wprof = l == 1 && w == 0;
WPROF(0);
residual_op(1, act);
WPROFP(1);
stats_out();
WPROFP(2);
fc1(act);
WPROFP(3);
residual_op(2, act);
if (NW > 1 && l + 1 >= NL && w + 1 < NW) { // no QKV tail: tell the BRISC the FC2 reads of INF are done
cb_reserve_back(CB_TOK, 1);
cb_push_back(CB_TOK, 1);
}
WPROFP(4);
if (tap_of(l) >= 0) {
tap_out();
}
tail((int)l);
WPROFP(5);
wave_end((int)l, NW > 1 || (me.dump && l + 1 == me.nl));
}
}
}
|