// 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 #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(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(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))); } // 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(); exp_tile(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(); exp_tile(a); #else exp_tile_init(); exp_tile(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(::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(); gelu_tile(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(); gelu_tile(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(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(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(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(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(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(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(((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(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(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(4, CB_SUMS, 4 * s); pack_tile(6, CB_SUMS, 4 * s + 1); pack_tile(5, CB_SUMS, 4 * s + 2); pack_tile(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(0, CB_SUMS, 0); pack_tile(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(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(0, CB_SC, 0); pack_tile(1, CB_SC, 1); pack_tile(2, CB_SC, 2); pack_tile(5, CB_SC, 3); pack_tile(3, CB_SC, 4); pack_tile(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(s, CB_EXT, s); } pack_reconfig_data_format(CB_RSTD); for (uint32_t s = 0; s < RB; ++s) { pack_tile(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 indexes from the write pointer pack_tile(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(i, CB_OUT, i); } if (lo) { for (uint32_t i = 0; i < 2 * RB; ++i) { pack_tile(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(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(0, CB_OUT, 2 * s); pack_tile(1, CB_OUT, 2 * s + 1); if (lo) { pack_tile(2, CB_OUTLO, 2 * s); pack_tile(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_())); PACK((_llk_pack_dest_init_())); } NOINL void h_end() { MATH((_llk_math_pack_sync_init_())); PACK((_llk_pack_dest_init_())); tile_regs_acquire(); tile_regs_commit(); tile_regs_wait(); tile_regs_release(); } ALWI void h_acquire() { MATH((_llk_math_wait_for_dest_available_())); } ALWI void h_commit() { MATH((_llk_math_dest_section_done_())); } 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(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_(); })); } 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(::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(0, CB_FO, slot * RB); pack_tile(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(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(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(((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(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(CB_S32F, CB_CONST, CB_MX); for (uint32_t j = 0; j < nk; ++j) { reduce_tile(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(); for (uint32_t t = 0; t < n; ++t) { exp_tile(t); } #else MATH(SFPU_BINARY_CALL_NO_TEMPLATE_ARGS(DST_SYNC_MODE, DST_ACCUM_MODE, me_subexp_n, 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, 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(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(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(0, CB_MFN, 0); pack_tile(2, CB_MFN, 1); pack_tile(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(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(0, CB_AO, 0); pack_tile(1, CB_AO, 1); } else { pack_reconfig_data_format(CB_ST); pack_tile(0, CB_ST, 0); pack_tile(1, CB_ST, 1); pack_tile(2, CB_ST, 2); pack_tile(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(0, CB_AO, 0); pack_tile(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(A_X); me.y = get_arg_val(A_Y); me.h0 = get_arg_val(A_H0); me.nh = get_arg_val(A_NH); 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.npe = get_arg_val(A_NPE); me.npb = get_arg_val(A_NPB); me.npbl = get_arg_val(A_NPBL); me.nl = get_common_arg_val(K_NL); me.dump = get_common_arg_val(K_DUMP); me.eps = get_common_arg_val(K_EPS); me.inv_d = get_common_arg_val(K_INVD); me.inv_t = get_common_arg_val(K_INVT); compute_kernel_hw_startup(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)); } } }