File size: 29,142 Bytes
ca1a504 | 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 | // SPDX-License-Identifier: Apache-2.0
//
// DeepSeek-OCR family DEC-MoE megakernel (one decode step), TRISC: all arithmetic. Adapted from this workspace's
// GLM-OCR DEC (bundles/glm-ocr tt/mk_dec/kernels/md_trisc.cpp, Apache-2.0), itself from the Qwen3-ASR DEC / Qwen3-TTS
// talker megakernel. DeepSeek changes: pre-norm layers (x += y, no sandwich norms), MHA (one head per ATT group),
// variable residual ownership (XN 1 / 2 tiles), layer 0 dense SwiGLU, MoE layers: shared columns then the 6 routed
// columns scaled by the router weights (CB_RW scalars), down projection in K-major order over the gathered 224
// SwiGLU tiles (shared, then slots 0..5), router cores (logits; softmax on router 0).
//
// Vector layouts (GLM probe tiny_probe.py): a vector is contiguous memory; matmul in0 / out use 1x32 ("tiny") tile
// views; elementwise / SFPU work uses dense 32x32 views of the same memory. A tiny matmul result in DST may be combined
// with another tiny matmul result or a UNIFORM tile, never with a copy_tile'd tiny tile.
// Matmuls MT_WFID (weights) / HiFi4 (fp32 operands), fp32 DST (8 tiles, full sync).
#include <cstdint>
#include "api/compute/compute_kernel_api.h"
#include "api/compute/eltwise_binary.h"
#include "api/compute/eltwise_binary_sfpu.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/recip.h"
#include "api/compute/eltwise_unary/rsqrt.h"
#include "api/compute/eltwise_unary/binop_with_scalar.h"
#include "api/compute/binary_max_min.h"
#include "api/compute/reconfig_data_format.h"
#include "api/compute/compute_kernel_hw_startup.h"
#include "md_defs.hpp"
using namespace mtx;
#define NOINL __attribute__((noinline, noclone))
ALWI volatile uint32_t* tsync_ptr(uint32_t w) {
const auto& iface = get_local_cb_interface(mtx::CB_SYNC);
return reinterpret_cast<volatile uint32_t*>(((iface.fifo_limit - iface.fifo_size) << 4) + w * mtx::SYNC_STRIDE);
}
#define TSTEP(v) UNPACK((*tsync_ptr(mtx::W_STEP) = (v)))
namespace {
constexpr uint32_t NHD = 2; // dense bf16 tiles of the gathered D-vector view
constexpr uint32_t PT_A = 8, PT_M = 4, PT_H = 8, PT_R = 2;
constexpr MathFidelity FID = MathFidelity::HiFi4;
constexpr MathFidelity WFID = MT_WFID == 2 ? MathFidelity::HiFi2 : (MT_WFID == 3 ? MathFidelity::HiFi3 : MathFidelity::HiFi4);
constexpr uint32_t CAP_ACC = T32 / TINY32; // 32
constexpr uint32_t CAP_QOT = T16 / TINY16; // 32
constexpr uint32_t CAP_S = 32768 / TINY32; // 256
constexpr uint32_t CAP_SD = 32768 / T32; // 8
constexpr uint32_t CAP_P = 16384 / T16; // 8
constexpr uint32_t CAP_PT = 16384 / TINY16; // 256
constexpr uint32_t CAP_FS = 512 / TINY16; // 8
constexpr uint32_t CAP_RL = 512 / TINY32; // 4
constexpr uint32_t CAP_MRG = MT_CAP_MRG;
constexpr uint32_t CAP_H = T32 / TINY16; // 64: CB_H / CB_O cycle at full capacity (fixed-layout gathers)
constexpr uint32_t CAP_O = T32 / TINY16;
bool g_nc = false;
struct Me {
uint32_t role, w, hk, nl, eps, inv_d, inv_hd, flags, neg;
uint32_t p, nb, done, nohead, stop;
uint32_t xn, rcol, nsc;
uint32_t slot, attk, my, sact, last;
};
Me me;
ALWI void acq() { if (!g_nc) { tile_regs_acquire(); } }
ALWI void com() { if (!g_nc) { tile_regs_commit(); } }
ALWI void wt() { if (!g_nc) { tile_regs_wait(); } }
ALWI void rel() { if (!g_nc) { tile_regs_release(); } }
template <MathFidelity F>
ALWI void mm_init_t(uint32_t in0, uint32_t in1, uint32_t transpose = 0) {
if (g_nc) {
return;
}
reconfig_full_operand(in1, in0);
MATH((llk_math_matmul_init<F, MM_THROTTLE>(in0, in1, transpose)));
UNPACK((llk_unpack_AB_matmul_init(in0, in1, transpose)));
}
template <MathFidelity F>
ALWI void mm_t(uint32_t in0, uint32_t in1, uint32_t t0, uint32_t t1, uint32_t dst) {
if (g_nc) {
return;
}
UNPACK((llk_unpack_AB_matmul(in0, in1, t0, t1)));
MATH((llk_math_matmul<F, MM_THROTTLE>(dst)));
}
ALWI void mm_init_f(uint32_t in0, uint32_t in1, uint32_t transpose = 0) { mm_init_t<FID>(in0, in1, transpose); }
ALWI void mm_f(uint32_t in0, uint32_t in1, uint32_t t0, uint32_t t1, uint32_t dst) { mm_t<FID>(in0, in1, t0, t1, dst); }
NOINL void load(uint32_t cb, uint32_t tile, uint32_t dst) {
if (g_nc) {
return;
}
reconfig_full_operand_srca(cb);
copy_init(cb);
copy_tile(cb, tile, dst);
}
NOINL void fmul(uint32_t a, uint32_t b, uint32_t o) {
if (g_nc) {
return;
}
mul_binary_tile_init();
mul_binary_tile(a, b, o);
}
NOINL void fadd(uint32_t a, uint32_t b, uint32_t o) {
if (g_nc) {
return;
}
add_binary_tile_init();
add_binary_tile(a, b, o);
}
NOINL void fsub(uint32_t a, uint32_t b, uint32_t o) {
if (g_nc) {
return;
}
sub_binary_tile_init();
sub_binary_tile(a, b, o);
}
NOINL void fmax(uint32_t a, uint32_t b, uint32_t o) {
if (g_nc) {
return;
}
binary_max_tile_init();
binary_max_tile(a, b, o);
}
NOINL void fscale(uint32_t a, uint32_t bits) {
if (g_nc) {
return;
}
binop_with_scalar_tile_init();
mul_unary_tile(a, bits);
}
NOINL void faddc(uint32_t a, uint32_t bits) {
if (g_nc) {
return;
}
binop_with_scalar_tile_init();
add_unary_tile(a, bits);
}
NOINL void frsqrt(uint32_t a) {
if (g_nc) {
return;
}
rsqrt_tile_init();
rsqrt_tile(a);
}
NOINL void fexp(uint32_t a) {
if (g_nc) {
return;
}
exp_tile_init<false>();
exp_tile<false>(a);
}
NOINL void frecip(uint32_t a) {
if (g_nc) {
return;
}
recip_tile_init();
recip_tile(a);
}
NOINL void fsilu(uint32_t a) {
if (g_nc) {
return;
}
silu_tile_init();
silu_tile(a);
}
NOINL void pack_to(uint32_t dst, uint32_t cb, uint32_t idx) {
if (g_nc) {
return;
}
pack_reconfig_data_format<true>(cb);
pack_tile<true>(dst, cb, idx);
}
ALWI void vreserve(uint32_t cb, uint32_t cap) { cb_reserve_back(cb, cap); }
ALWI void vpush(uint32_t cb, uint32_t cap) { cb_push_back(cb, cap); }
ALWI void vwait(uint32_t cb, uint32_t cap) { cb_wait_front(cb, cap); }
ALWI void vpop(uint32_t cb, uint32_t cap) { cb_pop_front(cb, cap); }
ALWI void vgive(uint32_t a, uint32_t cap_a, uint32_t b, uint32_t cap_b) {
cb_wait_front(a, cap_a);
cb_pop_front(a, cap_a);
cb_reserve_back(b, cap_b);
cb_push_back(b, cap_b);
}
ALWI void vhand(uint32_t a, uint32_t cap_a, uint32_t b, uint32_t cap_b) {
cb_wait_front(a, cap_a);
cb_pop_front(a, cap_a);
cb_reserve_back(b, cap_b);
cb_push_back(b, cap_b);
cb_wait_front(b, cap_b);
}
// weight ring consumer: pages of PT tiles
struct WS {
uint32_t cb, pt, t, pages;
ALWI uint32_t next() {
if (t == 0) {
cb_wait_front(cb, pt);
}
return t;
}
ALWI void done() {
if (++t == pt) {
cb_pop_front(cb, pt);
t = 0;
++pages;
}
}
};
WS wsa{CB_WA, PT_A, 0, 0}, wsm{CB_WM, PT_M, 0, 0}, wsh{CB_WH, PT_H, 0, 0};
// DST[dst0 + c] += sum_k in0[k] @ W(c, k) for c < nc (column sticks: column c, then k)
NOINL void mm_cols(uint32_t in0, WS& ws, uint32_t kt, uint32_t nc, uint32_t dst0) {
mm_init_t<WFID>(in0, ws.cb);
for (uint32_t c = 0; c < nc; ++c) {
for (uint32_t k = 0; k < kt; ++k) {
const uint32_t w = ws.next();
mm_t<WFID>(in0, ws.cb, k, w, dst0 + c);
ws.done();
}
}
}
// K-major groups: DST[c] += sum over the listed in0 tiles k_j: in0[k_j] @ W(j, c), c < nc
// k_j = base + j * stride for j < n
NOINL void mm_kmajor(uint32_t in0, WS& ws, uint32_t base, uint32_t stride, uint32_t n, uint32_t nc) {
mm_init_t<WFID>(in0, ws.cb);
for (uint32_t j = 0; j < n; ++j) {
const uint32_t k = base + j * stride;
for (uint32_t c = 0; c < nc; ++c) {
const uint32_t w = ws.next();
mm_t<WFID>(in0, ws.cb, k, w, c);
ws.done();
}
}
}
// uniform total of the dense fp32 tile in CB_T0 (pushed, 1 page): T0 @ ONES -> T1 (row sums); pops T0
NOINL void total_t0() {
vwait(CB_T0, 1);
vreserve(CB_T1, 1);
acq();
mm_init_f(CB_T0, CB_CONST);
mm_f(CB_T0, CB_CONST, 0, C_ONES, 0);
com();
wt();
pack_to(0, CB_T1, 0);
rel();
vpush(CB_T1, 1);
vpop(CB_T0, 1);
vwait(CB_T1, 1);
}
ALWI void total_t1(uint32_t dst) {
mm_init_f(CB_CONST, CB_T1);
mm_f(CB_CONST, CB_T1, C_ONES, 0, dst);
}
// rstd of the gathered vector (dense bf16 view, NHD tiles; the tail beyond D is zero) -> CB_RS (uniform, pushed)
NOINL void rstd_h() {
vreserve(CB_T0, 1);
acq();
load(CB_HD, 0, 0);
fmul(0, 0, 0);
for (uint32_t t = 1; t < NHD; ++t) {
load(CB_HD, t, 1);
fmul(1, 1, 1);
fadd(0, 1, 0);
}
com();
wt();
pack_to(0, CB_T0, 0);
rel();
vpush(CB_T0, 1);
total_t0();
vreserve(CB_RS, 1);
acq();
total_t1(0);
fscale(0, me.inv_d);
faddc(0, me.eps);
frsqrt(0);
com();
wt();
pack_to(0, CB_RS, 0);
rel();
vpush(CB_RS, 1);
vpop(CB_T1, 1);
vwait(CB_RS, 1);
}
// ================================================================ MM core
NOINL void norm_send() {
vwait(CB_X, 1);
vreserve(CB_XB, 1);
acq();
load(CB_X, 0, 0);
com();
wt();
pack_to(0, CB_XB, 0);
rel();
vpush(CB_XB, 1);
}
NOINL void qkv() {
rstd_h();
acq();
mm_cols(CB_H, wsa, DT, HD_T, 0);
load(CB_RS, 0, 4);
for (uint32_t c = 0; c < HD_T; ++c) {
fmul(c, 4, c);
}
com();
vpop(CB_RS, 1);
if (me.hk == HK_V) {
vreserve(CB_QOT, CAP_QOT);
wt();
for (uint32_t c = 0; c < HD_T; ++c) {
pack_to(c, CB_QOT, c);
}
rel();
vpush(CB_QOT, CAP_QOT);
vgive(CB_QOT, CAP_QOT, CB_QO, 1);
return;
}
vreserve(CB_ACC, CAP_ACC);
vreserve(CB_ACS, CAP_ACC);
wt();
for (uint32_t c = 0; c < HD_T; ++c) {
pack_to(c, CB_ACC, c);
pack_to(c, CB_ACS, c ^ 2);
}
rel();
vpush(CB_ACC, CAP_ACC);
vpush(CB_ACS, CAP_ACC);
vhand(CB_ACC, CAP_ACC, CB_ACCD, 1);
vhand(CB_ACS, CAP_ACC, CB_ACSD, 1);
// rope (half-split layout): acc * cos + swap(acc) * sinS (the q tables carry 128^-0.5)
vreserve(CB_QO, 1);
acq();
load(CB_ACCD, 0, 0);
load(CB_TAB, 0, 2);
fmul(0, 2, 0);
load(CB_ACSD, 0, 3);
load(CB_TAB, 1, 2);
fmul(3, 2, 3);
fadd(0, 3, 0);
com();
wt();
pack_to(0, CB_QO, 0);
rel();
vpush(CB_QO, 1);
vpop(CB_ACCD, 1);
vpop(CB_ACSD, 1);
}
// residual update: DST 0 .. xn-1 = this core's y tiles (acquired, not committed): x += y (skip: debug, x unchanged)
NOINL void residual_add(bool skip = false) {
com();
vreserve(CB_ACC, CAP_ACC);
wt();
for (uint32_t c = 0; c < me.xn; ++c) {
pack_to(c, CB_ACC, c);
}
rel();
vpush(CB_ACC, CAP_ACC);
vhand(CB_ACC, CAP_ACC, CB_ACCD, 1);
acq();
load(CB_ACCD, 0, 0);
load(CB_X, 0, 1);
if (skip) {
load(CB_X, 0, 0);
} else {
fadd(0, 1, 0);
}
com();
vpop(CB_X, 1);
vpop(CB_ACCD, 1);
vreserve(CB_X, 1);
wt();
pack_to(0, CB_X, 0);
rel();
vpush(CB_X, 1);
}
NOINL void oproj() {
vwait(CB_O, CAP_O);
acq();
mm_cols(CB_O, wsa, AT, me.xn, 0);
vpop(CB_O, CAP_O);
residual_add(me.flags & F_DBG_NO_ATT);
}
// SwiGLU of DST 2j (gate) / 2j + 1 (up) already accumulated: DST 2j = silu(g rstd) * (u rstd) [* weight]
// (DST 7 = rstd uniform). The GLM order: every matmul of the section first, then the rstd load and the SFPU ops.
ALWI void swiglu_post(uint32_t j, bool scaled, uint32_t wbits) {
fmul(2 * j, 7, 2 * j);
fmul(2 * j + 1, 7, 2 * j + 1);
fsilu(2 * j);
fmul(2 * j, 2 * j + 1, 2 * j);
if (scaled) {
fscale(2 * j, wbits);
}
}
// SwiGLU columns of this core -> CB_FS tiles 0..6 (layer 0: dense columns 7w..7w+6; MoE: shared first, then the
// routed slots; tile index: w < 28: slot k -> k, shared -> 6; w >= 28: shared j -> j)
NOINL void gateup(uint32_t l) {
vwait(CB_H, CAP_H);
vwait(CB_HD, NHD);
rstd_h();
uint32_t wb[NSLOT] = {0, 0, 0, 0, 0, 0};
const bool moe = l > 0;
if (moe && me.rcol) {
cb_wait_front(CB_RW, 1);
for (uint32_t k = 0; k < NSLOT; ++k) {
wb[k] = read_tile_value(CB_RW, 0, k);
}
cb_pop_front(CB_RW, 1);
} else if (moe) {
cb_wait_front(CB_RW, 1);
cb_pop_front(CB_RW, 1);
}
vreserve(CB_FS, CAP_FS);
// the column order of the M stream: dense: 7 columns; MoE: shared (nsc), then routed (6 if rcol)
const uint32_t nsh = moe ? me.nsc : FS;
const uint32_t nrt = moe && me.rcol ? NSLOT : 0;
const uint32_t ncol = nsh + nrt;
for (uint32_t c0 = 0; c0 < ncol; c0 += 3) { // 3 columns per DST section (6 tiles + rstd in DST 7)
const uint32_t nb = ncol - c0 < 3 ? ncol - c0 : 3;
acq();
for (uint32_t j = 0; j < nb; ++j) {
mm_cols(CB_H, wsm, DT, 2, 2 * j); // gate -> 2j, up -> 2j + 1
}
load(CB_RS, 0, 7);
for (uint32_t j = 0; j < nb; ++j) {
const uint32_t c = c0 + j;
const bool routed = c >= nsh;
swiglu_post(j, routed, routed ? wb[c - nsh] : 0u);
}
com();
wt();
for (uint32_t j = 0; j < nb; ++j) {
const uint32_t c = c0 + j;
uint32_t idx;
if (!moe) {
idx = c;
} else if (me.rcol) {
idx = c < nsh ? 6 : c - nsh;
} else {
idx = c;
}
pack_to(2 * j, CB_FS, idx);
}
rel();
}
vpush(CB_FS, CAP_FS);
vpop(CB_RS, 1);
vpop(CB_H, CAP_H);
vpop(CB_HD, NHD);
}
// down: y[c] = sum over the gathered SwiGLU tiles in the stream's K order; x += y
NOINL void down(uint32_t l) {
vwait(CB_F, IT);
acq();
if (l == 0) {
mm_kmajor(CB_F, wsm, 0, 1, ITK0, me.xn);
} else {
mm_kmajor(CB_F, wsm, 6, FS, NRC, me.xn); // shared columns 0..27 (f 7t + 6)
mm_kmajor(CB_F, wsm, IT - SH_T + NRC, 1, SH_T - NRC, me.xn); // shared columns 28..55 (f 168 + t)
for (uint32_t k = 0; k < NSLOT; ++k) {
mm_kmajor(CB_F, wsm, k, FS, NRC, me.xn); // slot k columns 0..27 (f 7t + k)
}
}
vpop(CB_F, IT);
residual_add(me.flags & F_DBG_NO_MLP);
}
NOINL void final_out(bool full) {
vwait(CB_H, CAP_H);
vwait(CB_HD, NHD);
if (me.flags & F_DUMP) {
vreserve(CB_HS, 1);
acq();
load(CB_X, 0, 0);
com();
wt();
pack_to(0, CB_HS, 0);
rel();
vpush(CB_HS, 1);
vpop(CB_H, CAP_H);
vpop(CB_HD, NHD);
return;
}
if (full) {
rstd_h();
vreserve(CB_LS, MT_HC);
for (uint32_t c0 = 0; c0 < MT_HC; c0 += 7) {
const uint32_t nc = MT_HC - c0 < 7 ? MT_HC - c0 : 7;
acq();
mm_cols(CB_H, wsh, DT, nc, 0);
load(CB_RS, 0, 7);
for (uint32_t c = 0; c < nc; ++c) {
fmul(c, 7, c);
}
com();
wt();
for (uint32_t c = 0; c < nc; ++c) {
pack_to(c, CB_LS, c0 + c);
}
rel();
}
vpush(CB_LS, MT_HC);
vpop(CB_RS, 1);
}
vpop(CB_H, CAP_H);
vpop(CB_HD, NHD);
}
uint32_t a_tiles() { return (me.hk != HK_NONE ? HD_T * DT : 0u) + me.xn * AT; }
uint32_t m_layer_tiles(uint32_t l) {
if (l == 0) {
return FS * 2 * DT + me.xn * ITK0;
}
return me.nsc * 2 * DT + (me.rcol ? NSLOT * 2 * DT : 0u) + me.xn * SH_T + NSLOT * me.xn * 28;
}
void mm_stream_only() {
uint32_t na = me.nl * (a_tiles() / PT_A), nm = 0;
for (uint32_t l = 0; l < me.nl; ++l) {
nm += m_layer_tiles(l) / PT_M;
}
const bool full = me.nl == N_LAYERS_FULL && !(me.flags & F_DUMP) && !me.nohead;
const uint32_t nh = full ? MT_HC * DT / PT_H : 0;
uint32_t a = 0, m = 0;
while (a < na || m < nm) {
if (a < na) {
cb_wait_front(CB_WA, PT_A);
cb_pop_front(CB_WA, PT_A);
++a;
}
if (m < nm) {
cb_wait_front(CB_WM, PT_M);
cb_pop_front(CB_WM, PT_M);
++m;
}
}
for (uint32_t i = 0; i < nh; ++i) {
cb_wait_front(CB_WH, PT_H);
cb_pop_front(CB_WH, PT_H);
}
}
void drain_rings() {
// debug stops: consume every page the NCRISC still streams (it streams all me.nl layers regardless)
uint32_t na = me.nl * (a_tiles() / PT_A), nm = 0;
for (uint32_t l = 0; l < me.nl; ++l) {
nm += m_layer_tiles(l) / PT_M;
}
uint32_t a = wsa.pages, m = wsm.pages;
while (a < na || m < nm) {
if (a < na) {
cb_wait_front(CB_WA, PT_A);
cb_pop_front(CB_WA, PT_A);
++a;
}
if (m < nm) {
cb_wait_front(CB_WM, PT_M);
cb_pop_front(CB_WM, PT_M);
++m;
}
}
}
void mm_main() {
if (me.flags & F_ARM_STREAM) {
mm_stream_only();
return;
}
cb_wait_front(CB_CONST, N_CONST);
if (me.hk == HK_Q || me.hk == HK_K) {
cb_wait_front(CB_TAB, 2);
}
vwait(CB_X0, 1);
vreserve(CB_X, 1);
acq();
load(CB_X0, 0, 0);
com();
wt();
pack_to(0, CB_X, 0);
rel();
vpush(CB_X, 1);
vpop(CB_X0, 1);
for (uint32_t l = 0; l < me.nl; ++l) {
TSTEP(l * 16 + 1);
norm_send();
vwait(CB_H, CAP_H);
vwait(CB_HD, NHD);
TSTEP(l * 16 + 2);
if (me.stop == 1) {
drain_rings();
return;
}
if (me.hk != HK_NONE) {
qkv();
}
vpop(CB_H, CAP_H);
vpop(CB_HD, NHD);
TSTEP(l * 16 + 3);
if (me.stop == 2) {
vwait(CB_O, CAP_O);
drain_rings();
return;
}
oproj();
TSTEP(l * 16 + 4);
norm_send();
TSTEP(l * 16 + 5);
gateup(l);
TSTEP(l * 16 + 6);
down(l);
}
TSTEP(0xF00);
norm_send();
final_out(me.nl == N_LAYERS_FULL && !(me.flags & F_DUMP) && !me.nohead);
}
// ================================================================ ROUTER core
// logits of experts [32 r, 32 r + 32) = (h @ W') * rstd -> CB_RL; router 0: softmax over the 64 assembled logits
// (CB_RSM, tail NEG) -> CB_RPR
NOINL void rt_softmax() {
vwait(CB_RSM, 1);
// M = max (copy to T0, scalar max reduce -> T1, broadcast -> uniform T0)
vreserve(CB_T0, 1);
acq();
load(CB_RSM, 0, 0);
com();
wt();
pack_to(0, CB_T0, 0);
rel();
vpush(CB_T0, 1);
vwait(CB_T0, 1);
vreserve(CB_T1, 1);
acq();
if (!g_nc) {
reconfig_full_operand(CB_T0, CB_CONST);
reduce_init<PoolType::MAX, ReduceDim::REDUCE_SCALAR>(CB_T0, CB_CONST, CB_T1);
reduce_tile<PoolType::MAX, ReduceDim::REDUCE_SCALAR>(CB_T0, CB_CONST, 0, C_ONES, 0);
reduce_uninit(CB_T0);
}
com();
wt();
pack_to(0, CB_T1, 0);
rel();
vpush(CB_T1, 1);
vpop(CB_T0, 1);
vwait(CB_T1, 1);
vreserve(CB_T0, 1);
acq();
if (!g_nc) {
reconfig_full_operand(CB_CONST, CB_T1);
mul_bcast_scalar_init(CB_CONST, CB_T1);
mul_tiles_bcast_scalar(CB_CONST, CB_T1, C_ONES, 0, 0);
}
com();
wt();
pack_to(0, CB_T0, 0);
rel();
vpush(CB_T0, 1);
vpop(CB_T1, 1);
vwait(CB_T0, 1);
// e = exp(x - M) -> CB_RE (exact copy for the final product) and T0 (the matmul sum operand)
acq();
load(CB_RSM, 0, 0);
load(CB_T0, 0, 1);
fsub(0, 1, 0);
fexp(0);
com();
vpop(CB_T0, 1);
vpop(CB_RSM, 1);
vreserve(CB_T0, 1);
vreserve(CB_RE, 1);
wt();
pack_to(0, CB_T0, 0);
pack_to(0, CB_RE, 0);
rel();
vpush(CB_T0, 1);
vpush(CB_RE, 1);
total_t0(); // T1 = row sums of e (pops T0)
vwait(CB_RE, 1);
vreserve(CB_RPR, 1);
acq();
total_t1(1); // uniform sum
frecip(1);
load(CB_RE, 0, 0);
fmul(0, 1, 0);
com();
wt();
pack_to(0, CB_RPR, 0);
rel();
vpop(CB_T1, 1);
vpop(CB_RE, 1);
vpush(CB_RPR, 1);
}
void rt_main() {
if (me.stop || me.nl < 2 || (me.flags & F_FORCE_ROUTE)) {
return;
}
cb_wait_front(CB_CONST, N_CONST);
WS wsr{CB_WR, PT_R, 0, 0};
for (uint32_t l = 1; l < me.nl; ++l) {
vwait(CB_H, CAP_H);
vwait(CB_HD, NHD);
rstd_h();
vreserve(CB_RL, CAP_RL);
acq();
mm_cols(CB_H, wsr, DT, 1, 0);
load(CB_RS, 0, 7);
fmul(0, 7, 0);
com();
wt();
pack_to(0, CB_RL, 0);
rel();
vpush(CB_RL, CAP_RL);
vpop(CB_RS, 1);
vpop(CB_H, CAP_H);
vpop(CB_HD, NHD);
if (me.w == 0) {
rt_softmax();
}
}
}
// ================================================================ ATT core (head g, key slot s)
NOINL void att_partial() {
const uint32_t nb = me.my;
const uint32_t st = (nb + 31) / 32;
const uint32_t tl = (nb - 1) / 32;
vwait(CB_Q, 8);
vreserve(CB_SD, CAP_SD);
acq();
load(CB_CONST, C_ONES, 0);
fscale(0, me.neg);
com();
wt();
pack_to(0, CB_SD, tl);
rel();
vpush(CB_SD, CAP_SD);
vwait(CB_SD, CAP_SD);
vpop(CB_SD, CAP_SD);
vreserve(CB_S, CAP_S);
for (uint32_t j0 = 0; j0 < nb; j0 += 8) {
const uint32_t n = nb - j0 < 8 ? nb - j0 : 8;
acq();
for (uint32_t jj = 0; jj < n; ++jj) {
const uint32_t j = j0 + jj;
cb_wait_front(CB_KR, HD_T);
mm_init_t<WFID>(CB_Q, CB_KR, 1);
for (uint32_t d = 0; d < HD_T; ++d) {
mm_t<WFID>(CB_Q, CB_KR, d, d, jj);
}
cb_pop_front(CB_KR, HD_T);
if (me.last && j + 1 == nb) {
mm_init_f(CB_CT, CB_MSK);
mm_f(CB_CT, CB_MSK, 32 * C_E0, 0, jj);
}
}
com();
wt();
for (uint32_t jj = 0; jj < n; ++jj) {
pack_to(jj, CB_S, j0 + jj);
}
rel();
}
vpush(CB_S, CAP_S);
vhand(CB_S, CAP_S, CB_SD, CAP_SD);
// m = max(S)
vreserve(CB_T0, 1);
acq();
load(CB_SD, 0, 0);
for (uint32_t t = 1; t < st; ++t) {
load(CB_SD, t, 2);
fmax(0, 2, 0);
}
com();
wt();
pack_to(0, CB_T0, 0);
rel();
vpush(CB_T0, 1);
vwait(CB_T0, 1);
vreserve(CB_T1, 1);
acq();
if (!g_nc) {
reconfig_full_operand(CB_T0, CB_CONST);
reduce_init<PoolType::MAX, ReduceDim::REDUCE_SCALAR>(CB_T0, CB_CONST, CB_T1);
reduce_tile<PoolType::MAX, ReduceDim::REDUCE_SCALAR>(CB_T0, CB_CONST, 0, C_ONES, 0);
reduce_uninit(CB_T0);
}
com();
wt();
pack_to(0, CB_T1, 0);
rel();
vpush(CB_T1, 1);
vpop(CB_T0, 1);
vwait(CB_T1, 1);
vreserve(CB_T0, 1);
acq();
if (!g_nc) {
reconfig_full_operand(CB_CONST, CB_T1);
mul_bcast_scalar_init(CB_CONST, CB_T1);
mul_tiles_bcast_scalar(CB_CONST, CB_T1, C_ONES, 0, 0);
}
com();
wt();
pack_to(0, CB_T0, 0);
rel();
vpush(CB_T0, 1);
vpop(CB_T1, 1);
vwait(CB_T0, 1);
// mz = m M01 -> CB_PRM
vreserve(CB_PRM, 1);
acq();
load(CB_T0, 0, 0);
load(CB_CONST, C_M01, 1);
fmul(0, 1, 0);
com();
wt();
pack_to(0, CB_PRM, 0);
rel();
vpush(CB_PRM, 1);
constexpr uint32_t EXN = 6;
for (uint32_t t0 = 0; t0 < st; t0 += EXN) {
const uint32_t n = st - t0 < EXN ? st - t0 : EXN;
acq();
load(CB_T0, 0, 0);
for (uint32_t j = 0; j < n; ++j) {
const uint32_t d = 1 + j;
load(CB_SD, t0 + j, d);
fsub(d, 0, d);
fexp(d);
}
com();
wt();
for (uint32_t j = 0; j < n; ++j) {
pack_to(1 + j, CB_P, t0 + j);
}
rel();
}
vpop(CB_T0, 1);
vpop(CB_SD, CAP_SD);
vpush(CB_P, CAP_P);
vwait(CB_P, CAP_P);
// l = sum(P) (bf16 P) -> lz = l M01
vreserve(CB_T1, 1);
acq();
mm_init_f(CB_P, CB_CONST);
for (uint32_t t = 0; t < st; ++t) {
mm_f(CB_P, CB_CONST, t, C_ONES, 0);
}
com();
wt();
pack_to(0, CB_T1, 0);
rel();
vpush(CB_T1, 1);
vwait(CB_T1, 1);
vreserve(CB_PRL, 1);
acq();
mm_init_f(CB_CONST, CB_T1);
mm_f(CB_CONST, CB_T1, C_ONES, 0, 0);
load(CB_CONST, C_M01, 2);
fmul(0, 2, 0);
com();
wt();
pack_to(0, CB_PRL, 0);
rel();
vpop(CB_T1, 1);
vpush(CB_PRL, 1);
vhand(CB_P, CAP_P, CB_PT, CAP_PT);
// O = P V (V streamed): DST 0..3 (unnormalized)
acq();
for (uint32_t j = 0; j < nb; ++j) {
cb_wait_front(CB_VR, HD_T);
mm_init_t<WFID>(CB_PT, CB_VR);
for (uint32_t d = 0; d < HD_T; ++d) {
mm_t<WFID>(CB_PT, CB_VR, j, d, d);
}
cb_pop_front(CB_VR, HD_T);
}
com();
vreserve(CB_ACC, CAP_ACC);
wt();
for (uint32_t c = 0; c < HD_T; ++c) {
pack_to(c, CB_ACC, c);
}
rel();
vpush(CB_ACC, CAP_ACC);
vpop(CB_PT, CAP_PT);
vhand(CB_ACC, CAP_ACC, CB_ACCD, 1);
vreserve(CB_PRO, 1);
acq();
load(CB_ACCD, 0, 0);
com();
wt();
pack_to(0, CB_PRO, 0);
rel();
vpush(CB_PRO, 1);
vpop(CB_ACCD, 1);
vpop(CB_Q, 8);
}
// slot 0: o = sum_s w_s O_s / sum_s w_s lz_s, w_s = exp(mz_s - M) -> CB_QO (bf16)
NOINL void att_merge() {
vwait(CB_PRO, 1);
vwait(CB_PRM, 1);
vwait(CB_PRL, 1);
if (me.sact > 1) {
vwait(CB_MRG, CAP_MRG);
}
vreserve(CB_T0, 1);
acq();
load(CB_PRM, 0, 0);
for (uint32_t s = 1; s < me.sact; ++s) {
load(CB_MRG, 3 * (s - 1) + 1, 1);
fmax(0, 1, 0);
}
com();
wt();
pack_to(0, CB_T0, 0);
rel();
vpush(CB_T0, 1);
vwait(CB_T0, 1);
vreserve(CB_QO, 1);
acq();
load(CB_T0, 0, 3);
load(CB_PRM, 0, 2);
fsub(2, 3, 2);
fexp(2);
load(CB_PRO, 0, 0);
fmul(0, 2, 0);
load(CB_PRL, 0, 1);
fmul(1, 2, 1);
for (uint32_t s = 1; s < me.sact; ++s) {
const uint32_t b = 3 * (s - 1);
load(CB_MRG, b + 1, 2);
fsub(2, 3, 2);
fexp(2);
load(CB_MRG, b, 4);
fmul(4, 2, 4);
fadd(0, 4, 0);
load(CB_MRG, b + 2, 4);
fmul(4, 2, 4);
fadd(1, 4, 1);
}
frecip(1);
fmul(0, 1, 0);
com();
wt();
pack_to(0, CB_QO, 0);
rel();
vpush(CB_QO, 1);
vpop(CB_T0, 1);
vpop(CB_PRO, 1);
vpop(CB_PRM, 1);
vpop(CB_PRL, 1);
if (me.sact > 1) {
vpop(CB_MRG, CAP_MRG);
}
}
void att_main() {
if (me.stop == 1) {
return;
}
cb_wait_front(CB_CONST, N_CONST);
cb_wait_front(CB_MSK, 1);
if (me.slot * ((me.nb + me.attk - 1) / me.attk) >= me.nb) {
return;
}
for (uint32_t l = 0; l < me.nl; ++l) {
TSTEP(l * 16 + 1);
att_partial();
if (me.slot == 0) {
att_merge();
}
if (me.stop == 2) {
return;
}
}
TSTEP(0xF00);
}
} // namespace
void kernel_main() {
me.role = get_arg_val<uint32_t>(A_ROLE);
me.w = get_arg_val<uint32_t>(A_W);
me.hk = get_arg_val<uint32_t>(A_HK);
me.xn = get_arg_val<uint32_t>(A_XN);
me.rcol = get_arg_val<uint32_t>(A_RCOL);
me.nsc = get_arg_val<uint32_t>(A_NSC);
me.nl = get_common_arg_val<uint32_t>(K_NL);
me.eps = get_common_arg_val<uint32_t>(K_EPS);
me.inv_d = get_common_arg_val<uint32_t>(K_INV_D);
me.inv_hd = get_common_arg_val<uint32_t>(K_INV_HD);
me.flags = get_common_arg_val<uint32_t>(K_FLAGS);
me.neg = get_common_arg_val<uint32_t>(K_NEG);
me.stop = get_common_arg_val<uint32_t>(K_STOP);
if (me.role != R_MM && me.role != R_ATT && me.role != R_RT) {
return;
}
cb_wait_front(CB_GO, 1);
const uint32_t g = read_tile_value(CB_GO, 0, 0);
cb_pop_front(CB_GO, 1);
if (g == 0 || (g & GO_DONE)) {
return;
}
me.p = (g & GO_P) - 1;
me.nohead = (g & GO_NOHEAD) != 0;
me.nb = me.p / 32 + 1;
me.slot = get_arg_val<uint32_t>(A_SLOT);
me.attk = get_common_arg_val<uint32_t>(K_ATTK);
{
const uint32_t nbl = (me.nb + me.attk - 1) / me.attk;
const uint32_t j0 = me.slot * nbl;
const uint32_t j1 = j0 + nbl < me.nb ? j0 + nbl : me.nb;
me.my = j0 < me.nb ? j1 - j0 : 0;
me.last = j0 < me.nb && j1 == me.nb;
me.sact = (me.nb + nbl - 1) / nbl;
}
g_nc = (me.flags & F_NOCOMPUTE) != 0;
if (!g_nc) {
compute_kernel_hw_startup<SrcOrder::Reverse>(CB_H, CB_WA, CB_ACC);
}
if (me.role == R_MM) {
mm_main();
} else if (me.role == R_RT) {
if (!(me.flags & F_ARM_STREAM)) {
rt_main();
}
} else if (!(me.flags & F_ARM_STREAM)) {
att_main();
}
}
|