// SPDX-License-Identifier: Apache-2.0 // // laya megakernel, TRISC: all arithmetic of one launch (LLK usage after the VLA-JEPA PREFIX and all-MiniLM-L6-v2 // megakernels). Band core (x, b) with row tiles r < R, per layer (the BRISC lands the inputs / ships the outputs in // the same order; the column's weight stream, arena.py, holds the weight tiles in exactly this consumption order): // NORM : partials P_r = (sum_j x_rj) @ ONESL + (sum_j x_rj^2) @ ONESR (one fp32 tile per row tile: row sums in // columns 0..15 / 16..31) -> the band; then mean / rstd from the C landed partials (FPU selector matmuls, // fp32 SFPU statistics) and y = (x - mean) rstd g (+ b) -> bf16 (the next op's input) and / or fp32 (x) // QKV : per unit (head of q / k / v): acc[r][0..1] = sum_k in[r][k] W (K = D_T); q / k of encoder layers: RoPE // (fp32 tables; the 1/8 attention scale is folded into Wq); head layers: + bias -> bf16 (staging) // ATT : items (row, head): per query tile i, S = q_i K_j^T (+ key-padding / sliding-window masks) over the key // tiles of the row (global) or i-2 .. i+2 (sliding), fp32 softmax, O_i = P V / l -> bf16 (the band's CB_INA) // O : x += ctx @ Wo[:, slice] (+ bias) (fp32 residual), then NORM 2 // FFN : encoder: GeGLU pairs u, g = in @ Wi -> GELU(u) g; head: ReLU(in @ W1 + b1) -> bf16 (the row gather) // DOWN : x += f @ Wo2[:, slice] (+ b2), then the next NORM (or the final norm, or the output) // Matmuls: bf16 operands, fp32 DST (full sync, 8 tiles), HiFi4 (LM_FID overrides). fp32 data enters the FPU as tf32 // (norm statistics, P); residual, LN, softmax stay fp32. DST rule (Blackhole, DepthPro2 MK_LOG r2): in a section, // every FPU accumulation into a fresh tile comes before any fp32 copy_tile into DST tile >= 4. #include #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/bcast.h" #include "api/compute/matmul.h" #include "api/compute/reduce.h" #include "api/compute/tile_move_copy.h" #include "api/compute/eltwise_unary/recip.h" #include "api/compute/eltwise_unary/rsqrt.h" #include "api/compute/reconfig_data_format.h" #include "api/compute/compute_kernel_hw_startup.h" #include "dev_mem_map.h" #include "hostdev/dev_msgs.h" #include "lm_plan.hpp" #if defined(TRISC_MATH) || defined(TRISC_PACK) #include "llk_math_eltwise_binary_sfpu_macros.h" #include "lm_sfpu.hpp" #endif #define NOINL __attribute__((noinline, noclone)) using namespace lmx; namespace { #ifdef LM_FID constexpr MathFidelity FID = static_cast(LM_FID); #else constexpr MathFidelity FID = MathFidelity::HiFi4; #endif constexpr uint32_t C = get_compile_time_arg_val(CT_C); constexpr uint32_t NB = get_compile_time_arg_val(CT_NB); constexpr uint32_t DT = get_compile_time_arg_val(CT_DT); constexpr uint32_t FE = get_compile_time_arg_val(CT_FE); constexpr uint32_t FH = get_compile_time_arg_val(CT_FH); constexpr uint32_t NLE = get_compile_time_arg_val(CT_NL); constexpr uint32_t H = get_compile_time_arg_val(CT_H); constexpr uint32_t GEVERY = get_compile_time_arg_val(CT_GEVERY); uint32_t g_abort = 0, g_spin = 1u << 24, g_sync = 0; // L1 byte address of a CB's buffer from this program's CB config (the MATH thread has no CB interfaces) NOINL uint32_t cb_addr(uint32_t cb) { tt_l1_ptr mailboxes_t* const mb = (tt_l1_ptr mailboxes_t*)(MEM_MAILBOX_BASE); const tt_l1_ptr launch_msg_t* lm = &(mb->launch[mb->launch_msg_rd_ptr]); const uint32_t base = lm->kernel_config.kernel_config_base[ProgrammableCoreType::TENSIX]; const volatile tt_l1_ptr uint32_t* cfg = reinterpret_cast(base + lm->kernel_config.local_cb_offset); return cfg[cb * 4]; // [fifo_addr, fifo_size, num_pages, page_size] in bytes } ALWI volatile uint32_t* sw(uint32_t w) { return reinterpret_cast(g_sync + w * SYNC_STRIDE); } NOINL void note(uint32_t word, uint32_t code, uint32_t arg) { if (!g_abort) { g_abort = code; *sw(word) = code; *sw(word + 1) = arg; *sw(W_ABORT) = ABORT_MAGIC; } } ALWI bool peer_abort(uint32_t it, uint32_t word) { if ((it & 4095) == 0 && *sw(W_ABORT) == ABORT_MAGIC) { note(word, AB_PEER, 0); return true; } return false; } // cb_wait_front (UNPACK), bounded NOINL void cbw(uint32_t cb, uint32_t n) { UNPACK(({ if (!g_abort) { volatile uint32_t* rp = get_cb_tiles_received_ptr(cb); uint32_t it = 0; while (true) { const uint16_t have = (uint16_t)reg_read((uint32_t)rp) - (uint16_t)get_local_cb_interface(cb).tiles_acked; if (have >= (uint16_t)n) { break; } if (++it > g_spin) { note(W_DBG + 4, AB_FRONT, cb); break; } if (peer_abort(it, W_DBG + 4)) { break; } } } })); } // cb_reserve_back (PACK), bounded NOINL void cbr(uint32_t cb, uint32_t n) { PACK(({ if (!g_abort) { volatile uint32_t* ap = get_cb_tiles_acked_ptr(cb); const uint16_t rcv = (uint16_t)get_local_cb_interface(cb).tiles_received; uint32_t it = 0; while (true) { const uint16_t acked = (uint16_t)reg_read((uint32_t)ap); const uint16_t fr = (uint16_t)get_local_cb_interface(cb).fifo_num_pages - (uint16_t)(rcv - acked); if ((int32_t)fr >= (int32_t)n) { break; } if (++it > g_spin) { note(W_DBG + 6, AB_BACK, cb); break; } if (peer_abort(it, W_DBG + 6)) { break; } } } })); } NOINL void tstep(uint32_t v) { UNPACK((*sw(W_TSTEP) = v)); #ifdef LM_PROF UNPACK(({ const uint32_t l = v >> 4, ph = v & 15; if (l < 32) { reinterpret_cast(g_sync + SYNC_PROF)[l * 16 + ph] = (uint32_t)ckernel::read_wall_clock(); } })); #endif } // ---------------------------------------------------------------- plan (all three threads) Plan g_plan; NOINL void wait_plan_all() { volatile uint32_t* p = get_cb_tiles_received_ptr(CB_PLAN); uint32_t it = 0; while ((p[0] & 0xFFFFu) == 0) { if (++it > g_spin) { break; } } asm volatile("" ::: "memory"); g_plan.w = reinterpret_cast(cb_addr(CB_SYNC) + SYNC_PLAN); } // ---------------------------------------------------------------- LLK wrappers ALWI void mm_init_f(uint32_t in0, uint32_t in1, uint32_t transpose) { reconfig_data_format(in1, in0); // matmul: SrcA <- in1, SrcB <- in0 MATH((llk_math_matmul_init(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(dst))); } NOINL void mm_init(uint32_t in0, uint32_t in1, uint32_t transpose) { mm_init_f(in0, in1, transpose); } NOINL void mm(uint32_t in0, uint32_t in1, uint32_t t0, uint32_t t1, uint32_t dst) { mm_f(in0, in1, t0, t1, dst); } // fp32 / bf16 tile -> DST (UnpackToDestFp32 CBs keep fp32 exactly) 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 frsqrt(uint32_t a) { rsqrt_tile_init(); rsqrt_tile(a); } NOINL void frecip(uint32_t a) { recip_tile_init(); recip_tile(a); } NOINL void f_lnstat(uint32_t a) { mul_binary_tile_init(); MATH(SFPU_BINARY_CALL_NO_TEMPLATE_ARGS(DST_SYNC_MODE, DST_ACCUM_MODE, lm_lnstat, a, 0, 0, VectorMode::RC)); } NOINL void f_lnapply(uint32_t u, uint32_t b, uint32_t beta) { mul_binary_tile_init(); MATH(SFPU_BINARY_CALL_NO_TEMPLATE_ARGS(DST_SYNC_MODE, DST_ACCUM_MODE, lm_lnapply, u, b, beta, VectorMode::RC)); } NOINL void f_geglu_n(uint32_t a, uint32_t n) { MATH((::ckernel::llk_math_eltwise_unary_sfpu_init(::ckernel::sfpu::lm_gelu_init))); MATH(SFPU_BINARY_CALL_NO_TEMPLATE_ARGS(DST_SYNC_MODE, DST_ACCUM_MODE, lm_geglu_n, a, n, 0, VectorMode::RC)); } NOINL void f_relu_n(uint32_t a, uint32_t n) { mul_binary_tile_init(); MATH(SFPU_BINARY_CALL_NO_TEMPLATE_ARGS(DST_SYNC_MODE, DST_ACCUM_MODE, lm_relu_n, a, n, 0, VectorMode::RC)); } NOINL void f_rope(uint32_t a, uint32_t c) { mul_binary_tile_init(); MATH(SFPU_BINARY_CALL_NO_TEMPLATE_ARGS(DST_SYNC_MODE, DST_ACCUM_MODE, lm_rope, a, c, 0, VectorMode::RC)); } NOINL void f_subexp_n(uint32_t n, uint32_t m) { mul_binary_tile_init(); MATH(SFPU_BINARY_CALL_NO_TEMPLATE_ARGS(DST_SYNC_MODE, DST_ACCUM_MODE, lm_subexp_n, n, m, 0, VectorMode::RC)); } NOINL void pack_to(uint32_t dst, uint32_t cb, uint32_t idx) { pack_reconfig_data_format(cb); pack_tile(dst, cb, idx); } // weight stream: tiles are consumed in pages of PAGE_TILES (bounded waits) constexpr uint32_t SLOTS_T = get_compile_time_arg_val(CT_SLOTS); struct WS { uint32_t t = 0; // tile index inside the current page (relative to the ring's read pointer) uint32_t slot = 0; // ring slot of the current page ALWI uint32_t next() { if (t == 0) { cbw(CB_W, PAGE_TILES); } return t; } ALWI void done() { if (++t == PAGE_TILES) { cb_pop_front(CB_W, PAGE_TILES); t = 0; slot = slot + 1 == SLOTS_T ? 0 : slot + 1; } } // tiles [t, t + n) lie before the ring's end (contiguous in L1) ALWI bool contig(uint32_t n) const { return t + n <= (SLOTS_T - slot) * PAGE_TILES; } // wait until tiles [t, t + n) are in the ring ALWI void need(uint32_t n) { const uint32_t pages = (t + n + PAGE_TILES - 1) / PAGE_TILES; cbw(CB_W, pages * PAGE_TILES); } ALWI void advance(uint32_t n) { t += n; while (t >= PAGE_TILES) { cb_pop_front(CB_W, PAGE_TILES); t -= PAGE_TILES; slot = slot + 1 == SLOTS_T ? 0 : slot + 1; } } }; WS ws; NOINL void skip_w(uint32_t n) { for (uint32_t i = 0; i < n; ++i) { ws.next(); ws.done(); } } // ---------------------------------------------------------------- state struct Me { uint32_t idx, x, y, u0, nu, n0, nx, f0, nf, h0, nh, lpe, lph; uint32_t R, T, nba, nl, nhl; uint32_t g[R_MAX]; bool band; }; Me me; // DST[r * nj + j] += sum_{k < K} in0[r * kstride + k] @ W for every r < R, j < nj (weights k-major, nj per k) // block form (LM_MMB): one LLK call per k for the R x nj output block (in0 rows r * kstride + k reused across the nj // weight tiles of row k, which are contiguous in the ring); a k-row that wraps the ring's end takes the tile path ALWI void mm_block_init(uint32_t in0, uint32_t nj, uint32_t kstride) { reconfig_data_format(CB_W, in0); MATH((llk_math_matmul_init(in0, CB_W, 0, nj, me.R))); UNPACK((llk_unpack_AB_matmul_init(in0, CB_W, 0, nj, me.R, kstride))); } NOINL void mm_stream_r(uint32_t in0, uint32_t K, uint32_t kstride, uint32_t nj) { #ifdef LM_MMB if (me.R * nj > 1) { mm_block_init(in0, nj, kstride); for (uint32_t k = 0; k < K; ++k) { if (ws.contig(nj)) { ws.need(nj); UNPACK((llk_unpack_AB_matmul(in0, CB_W, k, ws.t, nj, me.R, kstride))); MATH((llk_math_matmul(0, nj, me.R))); ws.advance(nj); } else { mm_init_f(in0, CB_W, 0); for (uint32_t j = 0; j < nj; ++j) { const uint32_t w = ws.next(); for (uint32_t r = 0; r < me.R; ++r) { mm_f(in0, CB_W, r * kstride + k, w, r * nj + j); } ws.done(); } mm_block_init(in0, nj, kstride); } } return; } #endif mm_init_f(in0, CB_W, 0); for (uint32_t k = 0; k < K; ++k) { for (uint32_t j = 0; j < nj; ++j) { const uint32_t w = ws.next(); for (uint32_t r = 0; r < me.R; ++r) { mm_f(in0, CB_W, r * kstride + k, w, r * nj + j); } ws.done(); } } } // ================================================================ norms // partials of the residual slice (CB_X, R x nx fp32) -> CB_STO (R tiles); CB_X stays at the front NOINL void partials() { cbw(CB_X, R_MAX * NX_MAX); cbw(CB_CONST, N_CONST); cbr(CB_STO, R_MAX); for (uint32_t r = 0; r < me.R; ++r) { // s1 = sum_j x, s2 = sum_j x^2 (fp32 SFPU) tile_regs_acquire(); load(CB_X, r * NX_MAX, 0); fmul(0, 0, 1); for (uint32_t j = 1; j < me.nx; ++j) { load(CB_X, r * NX_MAX + j, 2); fmul(2, 2, 3); fadd(0, 2, 0); fadd(1, 3, 1); } tile_regs_commit(); cbr(CB_SQ, 2); tile_regs_wait(); pack_to(0, CB_SQ, 0); pack_to(1, CB_SQ, 1); tile_regs_release(); cb_push_back(CB_SQ, 2); // P = s1 @ ONESL + s2 @ ONESR (row sums in columns 0..15 / 16..31) cbw(CB_SQ, 2); tile_regs_acquire(); mm_init(CB_SQ, CB_CONST, 0); mm(CB_SQ, CB_CONST, 0, C_ONESL, 0); mm(CB_SQ, CB_CONST, 1, C_ONESR, 0); tile_regs_commit(); tile_regs_wait(); pack_to(0, CB_STO, r); tile_regs_release(); cb_pop_front(CB_SQ, 2); } cb_push_back(CB_STO, R_MAX); } // mean / rstd of row tile r from the C landed partials -> CB_MR (2 tiles) NOINL void stats(uint32_t stcb, uint32_t r) { tile_regs_acquire(); mm_init(stcb, CB_CONST, 0); for (uint32_t c = 0; c < C; ++c) { mm_f(stcb, CB_CONST, c * R_MAX + r, C_SEL0, 0); } for (uint32_t c = 0; c < C; ++c) { mm_f(stcb, CB_CONST, c * R_MAX + r, C_SEL16, 1); } f_lnstat(0); frsqrt(1); tile_regs_commit(); cbr(CB_MR, 2); tile_regs_wait(); pack_to(0, CB_MR, 0); pack_to(1, CB_MR, 1); tile_regs_release(); cb_push_back(CB_MR, 2); } // norm n: mode 0 = y -> bf16 CB_OUT (INX); 1 = y (+ type embedding) -> CB_X in place (the head's residual) and its // partials; 2 = y -> CB_X in place and bf16 CB_OUT (the embedding norm: residual + layer 0's input). beta: CB_G holds // g | b. In-place packs into CB_X are safe: slot (r, j) is loaded before it is packed, and CB_X's FIFO pointers stay // at the base (every push / pop cycles its whole capacity). NOINL void norm(uint32_t n, uint32_t mode, bool beta) { const uint32_t stcb = (n & 1) ? CB_STAT1 : CB_STAT0; cbw(CB_G, G_CAP); cbw(stcb, C_MAX * R_MAX); cbw(CB_X, R_MAX * NX_MAX); if (mode != 1) { cbr(CB_OUT, OUT_CAP); } if (mode == 1) { cbw(CB_TE, NX_MAX); cbw(CB_OH, R_MAX); } for (uint32_t r = 0; r < me.R; ++r) { stats(stcb, r); cbw(CB_MR, 2); for (uint32_t j = 0; j < me.nx; ++j) { tile_regs_acquire(); if (mode == 1) { // te = one-hot(lane -> qtype) @ [te_0; te_1; te_2] (FPU first: DST zero-flag rule) mm_init(CB_OH, CB_TE, 0); mm(CB_OH, CB_TE, r, j, 5); } load(CB_X, r * NX_MAX + j, 0); load(CB_MR, 0, 1); load(CB_MR, 1, 2); load(CB_G, j, 3); if (beta) { load(CB_G, me.nx + j, 4); } f_lnapply(0, 1, beta ? 1 : 0); if (mode == 1) { fadd(0, 5, 0); } tile_regs_commit(); tile_regs_wait(); if (mode != 1) { pack_to(0, CB_OUT, r * me.nx + j); } if (mode != 0) { pack_to(0, CB_X, r * NX_MAX + j); } tile_regs_release(); } cb_pop_front(CB_MR, 2); } cb_pop_front(stcb, C_MAX * R_MAX); cb_pop_front(CB_G, G_CAP); if (mode != 1) { cb_push_back(CB_OUT, OUT_CAP); } if (mode == 1) { partials(); } } // ================================================================ QKV(l) NOINL void qkv(uint32_t l, bool head) { const bool global = head || (l % GEVERY) == 0; cbw(CB_INX, R_MAX * DT); if (head) { cbw(CB_G, G_CAP); } else { cbw(CB_TAB, R_MAX * 4); } for (uint32_t u = 0; u < me.nu; ++u) { const uint32_t unit = me.u0 + u; const bool rope = !head && unit < 2 * H; tile_regs_acquire(); mm_stream_r(CB_INX, DT, DT, 2); if (rope) { for (uint32_t r = 0; r < me.R; ++r) { const uint32_t tb = (r * 2 + (global ? 0 : 1)) * 2; load(CB_TAB, tb, 4); load(CB_TAB, tb + 1, 5); f_rope(r * 2, 4); } } else if (head) { load(CB_G, u * 2, 4); load(CB_G, u * 2 + 1, 5); for (uint32_t r = 0; r < me.R; ++r) { fadd(r * 2, 4, r * 2); fadd(r * 2 + 1, 5, r * 2 + 1); } } tile_regs_commit(); cbr(CB_OUT, OUT_CAP); tile_regs_wait(); pack_reconfig_data_format(CB_OUT); for (uint32_t t = 0; t < me.R * 2; ++t) { pack_tile(t, CB_OUT, t); } tile_regs_release(); cb_push_back(CB_OUT, OUT_CAP); } cb_pop_front(CB_INX, R_MAX * DT); if (head) { cb_pop_front(CB_G, G_CAP); } } // ================================================================ attention items of layer l NOINL void att_item(uint32_t it, bool global) { const uint32_t rho = it_rho(it), i0 = it_i0(it), n = it_n(it); const uint32_t t = g_plan.row_tiles(rho); const uint32_t pad = g_plan.row_e(rho) < 32; // key columns past the row's end in its last tile const uint32_t padl = g_plan.row_a(rho) > 0; // key columns before the row's start in its first tile cbw(CB_AKV, AKV_CAP); for (uint32_t i = i0; i < i0 + n; ++i) { const uint32_t j0 = global ? 0 : (i >= 2 ? i - 2 : 0); const uint32_t j1 = global ? t : (i + 3 < t ? i + 3 : t); const uint32_t nk = j1 - j0; // scores S_j = q_i K_j^T (+ masks), <= 7 key tiles per DST section -> CB_AS (fp32) at AS_S + j - j0 cbr(CB_AS, AS_CAP); for (uint32_t c0 = 0; c0 < nk; c0 += 7) { const uint32_t n = nk - c0 < 7 ? nk - c0 : 7; tile_regs_acquire(); mm_init(CB_AKV, CB_AKV, 1); for (uint32_t jj = 0; jj < n; ++jj) { const uint32_t j = j0 + c0 + jj; mm_f(CB_AKV, CB_AKV, AKV_Q + i * 2, AKV_K + j * 2, jj); mm_f(CB_AKV, CB_AKV, AKV_Q + i * 2 + 1, AKV_K + j * 2 + 1, jj); } mm_init(CB_CONST, CB_CONST, 0); for (uint32_t jj = 0; jj < n; ++jj) { const uint32_t j = j0 + c0 + jj; if (!global && j + 2 == i) { mm_f(CB_CONST, CB_CONST, C_IDENT, C_MM2, jj); } if (!global && j == i + 2) { mm_f(CB_CONST, CB_CONST, C_IDENT, C_MP2, jj); } } if ((pad && j0 + c0 + n == t) || (padl && j0 + c0 == 0)) { mm_init(CB_CONST, CB_AKV, 0); if (pad && j0 + c0 + n == t) { // the row's last key tile is in this section mm_f(CB_CONST, CB_AKV, C_IDENT, AKV_M, n - 1); } if (padl && j0 + c0 == 0) { // the row's first key tile is in this section mm_f(CB_CONST, CB_AKV, C_IDENT, AKV_M + 1, 0); } } tile_regs_commit(); tile_regs_wait(); pack_reconfig_data_format(CB_AS); for (uint32_t jj = 0; jj < n; ++jj) { pack_tile(jj, CB_AS, AS_S + c0 + jj); } tile_regs_release(); } cb_push_back(CB_AS, AS_CAP); // row max (FPU reduce on the fp32 scores through the CB_AP view, tf32 operand: only consistency matters) cbw(CB_AS, AS_CAP); tile_regs_acquire(); reconfig_data_format(CB_AP, CB_CONST); reduce_init(CB_AP, CB_CONST, CB_MAX); for (uint32_t jj = 0; jj < nk; ++jj) { reduce_tile(CB_AP, CB_CONST, AS_S + jj, C_ONES, 0); } tile_regs_commit(); cbr(CB_MAX, 1); tile_regs_wait(); pack_to(0, CB_MAX, 0); tile_regs_release(); reduce_uninit(CB_AP); cb_push_back(CB_MAX, 1); // broadcast over the columns cbw(CB_MAX, 1); tile_regs_acquire(); reconfig_data_format(CB_CONST, CB_MAX); mul_bcast_cols_init(CB_CONST, CB_MAX); mul_tiles_bcast_cols(CB_CONST, CB_MAX, C_ONES, 0, 0); tile_regs_commit(); cbr(CB_MF, 1); tile_regs_wait(); pack_to(0, CB_MF, 0); tile_regs_release(); cb_push_back(CB_MF, 1); cb_pop_front(CB_MAX, 1); // P = exp(S - max) (fp32) -> CB_AP at AP_P .., <= 6 tiles per section (DST 7 holds the max) cbw(CB_MF, 1); cbr(CB_AP, AS_CAP); for (uint32_t p0 = 0; p0 < nk; p0 += 6) { const uint32_t n = nk - p0 < 6 ? nk - p0 : 6; tile_regs_acquire(); for (uint32_t j = 0; j < n; ++j) { load(CB_AS, AS_S + p0 + j, j); } load(CB_MF, 0, 7); f_subexp_n(n, 7); tile_regs_commit(); tile_regs_wait(); pack_reconfig_data_format(CB_AP); for (uint32_t j = 0; j < n; ++j) { pack_tile(j, CB_AP, AP_P + p0 + j); } tile_regs_release(); } cb_push_back(CB_AP, AS_CAP); cb_pop_front(CB_AS, AS_CAP); cb_pop_front(CB_MF, 1); // O = (P V) / (P 1) cbw(CB_AP, AS_CAP); tile_regs_acquire(); mm_init(CB_AP, CB_CONST, 0); for (uint32_t p = 0; p < nk; ++p) { mm_f(CB_AP, CB_CONST, AP_P + p, C_ONES, 2); } mm_init(CB_AP, CB_AKV, 0); for (uint32_t p = 0; p < nk; ++p) { mm_f(CB_AP, CB_AKV, AP_P + p, AKV_V + (j0 + p) * 2, 0); mm_f(CB_AP, CB_AKV, AP_P + p, AKV_V + (j0 + p) * 2 + 1, 1); } frecip(2); fmul(0, 2, 0); fmul(1, 2, 1); tile_regs_commit(); cbr(CB_OUT, OUT_CAP); tile_regs_wait(); pack_to(0, CB_OUT, 0); pack_to(1, CB_OUT, 1); tile_regs_release(); cb_push_back(CB_OUT, OUT_CAP); cb_pop_front(CB_AP, AS_CAP); } cb_pop_front(CB_AKV, AKV_CAP); } NOINL void att(uint32_t l, bool head) { const bool global = head || (l % GEVERY) == 0; for (uint32_t s = 0; s < IT_MAX; ++s) { const uint32_t it = g_plan.item(me.idx, s); if (it == 0) { break; } att_item(it, global); } } // ================================================================ O / DOWN: x += in @ W[:, slice] (+ bias), partials NOINL void resid(uint32_t in0, uint32_t K, uint32_t kstride, bool bias) { const uint32_t cyc = in0 == CB_INF ? AKV_CAP : kstride * R_MAX; // tiles one landing cycle pushes cbw(in0, cyc); if (bias) { cbw(CB_G, G_CAP); } cbw(CB_X, R_MAX * NX_MAX); tile_regs_acquire(); mm_stream_r(in0, K, kstride, me.nx); for (uint32_t r = 0; r < me.R; ++r) { for (uint32_t j = 0; j < me.nx; ++j) { load(CB_X, r * NX_MAX + j, 7); fadd(r * me.nx + j, 7, r * me.nx + j); if (bias) { load(CB_G, j, 7); fadd(r * me.nx + j, 7, r * me.nx + j); } } } tile_regs_commit(); cb_pop_front(CB_X, R_MAX * NX_MAX); cbr(CB_X, R_MAX * NX_MAX); tile_regs_wait(); pack_reconfig_data_format(CB_X); for (uint32_t r = 0; r < me.R; ++r) { for (uint32_t j = 0; j < me.nx; ++j) { pack_tile(r * me.nx + j, CB_X, r * NX_MAX + j); } } tile_regs_release(); cb_push_back(CB_X, R_MAX * NX_MAX); cb_pop_front(in0, cyc); if (bias) { cb_pop_front(CB_G, G_CAP); } } // ================================================================ FFN up: GeGLU pairs (encoder) / ReLU(W1 + b1) (head) NOINL void ffn_up(bool head) { cbw(CB_INX, R_MAX * DT); if (head) { cbw(CB_G, G_CAP); } const uint32_t per = head ? FB : PB; const uint32_t n = head ? me.nh : me.nf; for (uint32_t j0 = 0; j0 < n; j0 += per) { const uint32_t nb = n - j0 < per ? n - j0 : per; tile_regs_acquire(); if (head) { mm_stream_r(CB_INX, DT, DT, nb); // DST r * nb + jj for (uint32_t jj = 0; jj < nb; ++jj) { load(CB_G, j0 + jj, 7); for (uint32_t r = 0; r < me.R; ++r) { fadd(r * nb + jj, 7, r * nb + jj); } } f_relu_n(0, me.R * nb); } else { mm_stream_r(CB_INX, DT, DT, 2 * nb); // DST r * 2nb + 2jj (u) / + 1 (g) f_geglu_n(0, me.R * nb); } tile_regs_commit(); cbr(CB_OUT, OUT_CAP); tile_regs_wait(); pack_reconfig_data_format(CB_OUT); for (uint32_t r = 0; r < me.R; ++r) { for (uint32_t jj = 0; jj < nb; ++jj) { pack_tile(head ? r * nb + jj : r * 2 * nb + 2 * jj, CB_OUT, r * per + jj); } } tile_regs_release(); cb_push_back(CB_OUT, OUT_CAP); } cb_pop_front(CB_INX, R_MAX * DT); if (head) { cb_pop_front(CB_G, G_CAP); } } // the embedding slices (CB_E, landed by the BRISC) -> CB_X (the TRISC is CB_X's only producer) NOINL void x_from_e() { cbw(CB_E, R_MAX * NX_MAX); cbr(CB_X, R_MAX * NX_MAX); for (uint32_t r = 0; r < me.R; ++r) { for (uint32_t j = 0; j < me.nx; ++j) { tile_regs_acquire(); load(CB_E, r * NX_MAX + j, 0); tile_regs_commit(); tile_regs_wait(); pack_to(0, CB_X, r * NX_MAX + j); tile_regs_release(); } } cb_push_back(CB_X, R_MAX * NX_MAX); cb_pop_front(CB_E, R_MAX * NX_MAX); } NOINL void out_h() { cbw(CB_X, R_MAX * NX_MAX); cbr(CB_HOUT, R_MAX * NX_MAX); for (uint32_t r = 0; r < me.R; ++r) { for (uint32_t j = 0; j < me.nx; ++j) { tile_regs_acquire(); load(CB_X, r * NX_MAX + j, 0); tile_regs_commit(); tile_regs_wait(); pack_to(0, CB_HOUT, r * NX_MAX + j); tile_regs_release(); } } cb_push_back(CB_HOUT, R_MAX * NX_MAX); } } // namespace void kernel_main() { me.idx = get_arg_val(A_IDX); me.x = get_arg_val(A_X); me.y = get_arg_val(A_Y); me.u0 = get_arg_val(A_U0); me.nu = get_arg_val(A_NU); me.n0 = get_arg_val(A_N0); me.nx = get_arg_val(A_NX); me.f0 = get_arg_val(A_F0); me.nf = get_arg_val(A_NF); me.h0 = get_arg_val(A_H0); me.nh = get_arg_val(A_NH); me.lpe = get_arg_val(A_LPE); me.lph = get_arg_val(A_LPH); g_sync = cb_addr(CB_SYNC); compute_kernel_hw_startup(CB_INX, CB_W, CB_OUT); wait_plan_all(); if (g_plan.spin()) { g_spin = g_plan.spin(); } me.T = g_plan.T(); me.nba = g_plan.nba(); me.nl = g_plan.nl(); me.nhl = g_plan.nhl(); me.band = me.y < me.nba; me.R = me.band ? band_rows(me.T, NB, me.y) : 0; cbw(CB_PLAN, 1); #ifdef LM_STREAM_ONLY // timing arm: the weight stream alone (the TRISC pops every page, nothing else runs) if (me.band) { for (uint32_t l = 0; l < me.nl + me.nhl; ++l) { const bool head = l >= me.nl; tstep(l * 16 + 1); skip_w(head ? 2 * me.nu * DT + me.nx * DT + me.nh * DT + me.nx * FH + me.lph : 2 * me.nu * DT + me.nx * DT + 2 * me.nf * DT + me.nx * FE + me.lpe); } } tstep(0xD0D0); cb_pop_front(CB_PLAN, 1); return; #endif cbw(CB_CONST, N_CONST); uint32_t n = 0; if (me.band) { x_from_e(); partials(); // of e (CB_X), for the embedding norm norm(n++, 2, false); // x = LN_emb(e): residual + layer 0's input } const uint32_t nl = me.nl, nhl = me.nhl; for (uint32_t l = 0; l < nl + nhl; ++l) { const bool head = l >= nl; if (me.band) { tstep(l * 16 + 1); if (head || l > 0) { norm(n++, 0, head); } tstep(l * 16 + 2); qkv(l, head); } tstep(l * 16 + 4); att(l, head); if (me.band) { tstep(l * 16 + 5); resid(CB_INA, DT, DT, head); // x += ctx @ Wo (+ bias) partials(); tstep(l * 16 + 6); norm(n++, 0, head); tstep(l * 16 + 7); ffn_up(head); tstep(l * 16 + 8); resid(CB_INF, head ? FH : FE, F_MAX, head); // x += f @ Wo2 (+ b2) partials(); skip_w(head ? me.lph : me.lpe); if (!head && l + 1 == nl) { norm(n++, 1, false); // h = LN_final(x) + type_emb; partials of h follow inside } } } if (me.band) { out_h(); } tstep(0xD0D0); cb_pop_front(CB_PLAN, 1); }