changh95's picture
tt-model push paddleocr-vl (container)
22e0325 verified
Raw History Blame Contribute Delete
28.9 kB
// SPDX-License-Identifier: Apache-2.0
//
// PaddleOCR-VL DEC megakernel (copied from the Qwen3-ASR one, bundles/qwen3-asr, Apache-2.0; MT_QKNORM 0 = ERNIE: no
// q / k RMSNorm). Qwen3-ASR decode megakernel (DEC, one decode step), TRISC: all arithmetic. Adapted from the Qwen3-TTS talker
// megakernel (bundles/qwen3-tts tt/mk_talker/kernels/mt_trisc.cpp, same workspace, Apache-2.0): the final stage is the
// tied LM head (MT_HC column tiles per MM core in groups of <= 7 DST tiles, fmt H) instead of the codec head + H.
//
// Vector layouts (probe tiny_probe.py): a vector is contiguous memory; matmul in0 / out use 1x32 ("tiny") tile views
// of it; elementwise / SFPU work uses dense 32x32 views of the same memory (element e = memory element e; vectors
// shorter than a tile are zero-padded where a reduction reads them). A tiny matmul result in DST may be combined with
// another tiny matmul result or a UNIFORM tile (all elements equal), never with a copy_tile'd tiny tile.
//
// MM core w, per layer l (the BRISC lands inputs / ships outputs in the same order; the NCRISC streams weights):
// norm_send : bf16(x) slice -> CB_XB (the BRISC multicasts it into every core's CB_H)
// qkv : rstd = rsqrt(mean(h^2) + eps) from the gathered bf16 h; acc[4] = h @ W'(head w) * rstd;
// q / k: RMSNorm over the head, gamma (q: x 128^-0.5), RoPE -> bf16; v: bf16 -> CB_QO
// oproj : x += o @ Wo'[:, slice]
// norm_send, gateup: f = SiLU(h2 @ Wg' * rstd2) * (h2 @ Wu' * rstd2) -> CB_FS
// down : x += f @ Wd'[:, slice]
// final : logits slice = (h @ Wh') * rstd -> CB_LS (Wh' = diag(g_final) embed^T columns of core w)
// ATT core g, per layer: scores of both q heads over the key blocks (K streamed), softmax (fp32), P V (V streamed),
// o -> CB_QO (the BRISC multicasts it into every core's CB_O).
// Matmuls HiFi4, 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))
// progress word (debug: the dbg dump shows where a stalled TRISC waits): W_STEP = layer * 16 + phase (UNPACK thread)
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 DT = MT_DT; // D / 32
constexpr uint32_t IT = MT_IT; // I / 32
constexpr uint32_t XS = MT_XS; // residual column tiles per MM core
constexpr uint32_t FS = MT_FS; // SwiGLU column tiles per MM core
constexpr uint32_t NHD = MT_NHD; // dense 32x32 tiles of the D-vector
constexpr uint32_t PT_A = MT_PT_A; // packet tiles of the rings
constexpr uint32_t PT_M = MT_PT_M;
constexpr uint32_t PT_H = MT_PT_H;
constexpr MathFidelity FID = MathFidelity::HiFi4;
#ifndef MT_ATTS
#define MT_ATTS 1
#endif
constexpr uint32_t NH = 2 / MT_ATTS; // q heads per ATT core (MT_ATTS = 2: one head per core)
#ifndef MT_MMBLK
#define MT_MMBLK 0
#endif
// weight matmuls (bf16 activations x bf16 / bfp8 weights) and the bf16 x bf16 attention matmuls: MT_WFID (2 / 4)
constexpr MathFidelity WFID = MT_WFID == 2 ? MathFidelity::HiFi2 : (MT_WFID == 3 ? MathFidelity::HiFi3 : MathFidelity::HiFi4);
// full-capacity page counts of the fixed-layout tiny views (CB total / page)
constexpr uint32_t CAP_ACC = T32 / TINY32; // 32
constexpr uint32_t CAP_QOT = T16 / TINY16; // 32
constexpr uint32_t CAP_S = 32768 / TINY32; // 256 (2 heads x 4 dense tiles)
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 = MT_FS_CAP; // CB_FS pages
bool g_nc = false; // F_NOCOMPUTE: CB protocol only
struct Me {
uint32_t role, w, hk, nl, eps, inv_d, inv_hd, flags, neg;
uint32_t p, nb, done, nohead, stop;
};
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(); } }
// ---------------------------------------------------------------- LLK wrappers (full operand reconfig: the tile
// shapes change between tiny and 32x32 views)
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); // SrcA <- in1, SrcB <- 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)));
}
// HiFi4: every matmul with an fp32 operand (reductions over ONES, scalar broadcasts)
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);
}
// fixed-layout views: full-capacity cycles keep every view at its base address
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); }
// the pack thread wrote view a; make view b (same memory) readable: pop a, push b (no data movement)
// the pack thread wrote view a; give view b (same memory) to a data-movement consumer
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
uint32_t wsa_pages = 0; // pages of ring A popped so far (debug stops)
struct WS {
uint32_t cb, pt, t;
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;
if (cb == CB_WA) {
++wsa_pages;
}
}
}
ALWI void adv(uint32_t n) { // n tiles of one aligned group (t + n <= pt)
t += n;
if (t == pt) {
cb_pop_front(cb, pt);
t = 0;
if (cb == CB_WA) {
++wsa_pages;
}
}
}
};
WS wsa{CB_WA, PT_A, 0}, wsm{CB_WM, PT_M, 0}, wsh{CB_WH, PT_H, 0};
// DST[dst0 + c] += sum_k in0[k] @ W(c, k) for c < nc (weights column-major 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();
}
}
}
// MT_MMBLK (arena layout 2): weights k-major in groups of nc columns (for every k the nc tiles of the nc output
// columns, contiguous; groups never straddle a packet or the ring end); one block matmul per k (in0 tile k unpacked
// once, ct_dim = nc in1 tiles streamed) -> DST[dst0 .. dst0 + nc - 1]
#if MT_MMBLK
NOINL void mm_blk(uint32_t in0, WS& s, uint32_t kt, uint32_t nc, uint32_t dst0) {
if (!g_nc) {
reconfig_full_operand(s.cb, in0);
MATH((llk_math_matmul_init<WFID, MM_THROTTLE>(in0, s.cb, 0, nc, 1)));
UNPACK((llk_unpack_AB_matmul_init(in0, s.cb, 0, nc, 1, 1)));
}
for (uint32_t k = 0; k < kt; ++k) {
const uint32_t w = s.next();
if (!g_nc) {
UNPACK((llk_unpack_AB_matmul(in0, s.cb, k, w, nc, 1, 1)));
MATH((llk_math_matmul<WFID, MM_THROTTLE>(dst0, nc, 1)));
}
s.adv(nc);
}
}
#endif
#if MT_MMBLK
#define MM_W(in0, s, kt, nc, dst0) mm_blk(in0, s, kt, nc, dst0)
#else
#define MM_W(in0, s, kt, nc, dst0) mm_cols(in0, s, kt, nc, dst0)
#endif
// uniform total of the dense fp32 tile in CB_T0 (pushed, 1 page): DST[dst] = sum(T0) in every element.
// T0 @ ONES (row sums in every column) -> T1; ONES @ T1 (column sums). Pops T0.
NOINL void total_t0(uint32_t dst_after) {
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);
(void)dst_after;
}
// DST[dst] = ONES @ T1 (needs the section acquired); pops T1 after the caller's commit
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 h (CB_HD dense bf16, NHD tiles): -> CB_RS (uniform, 1 page 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(0);
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);
}
// x += the tiny fp32 tiles DST[0 .. n-1] (packed to CB_ACC, read back through the dense view)
NOINL void residual_add(uint32_t n) {
com();
vreserve(CB_ACC, CAP_ACC);
wt();
for (uint32_t c = 0; c < n; ++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);
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 qkv() {
vwait(CB_H, DT);
vwait(CB_HD, NHD);
rstd_h();
acq();
MM_W(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) { // v: bf16 as is
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); // the BRISC sends and pops CB_QO
vpop(CB_H, DT);
vpop(CB_HD, NHD);
return;
}
// q / k: the accumulators in natural (CB_ACC) and half-swapped (CB_ACS: tiles 2, 3, 0, 1) order
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);
vpop(CB_H, DT);
vpop(CB_HD, NHD);
vhand(CB_ACC, CAP_ACC, CB_ACCD, 1);
vhand(CB_ACS, CAP_ACC, CB_ACSD, 1);
#if MT_QKNORM
// head sum of squares (the dense tile holds the 128 values, the rest is zero)
vreserve(CB_T0, 1);
acq();
load(CB_ACCD, 0, 0);
fmul(0, 0, 0);
com();
wt();
pack_to(0, CB_T0, 0);
rel();
vpush(CB_T0, 1);
total_t0(0);
#endif
cb_wait_front(CB_G, 2);
vreserve(CB_QO, 1);
acq();
#if MT_QKNORM
total_t1(1);
fscale(1, me.inv_hd);
faddc(1, me.eps);
frsqrt(1); // DST1 = rq (uniform)
#endif
load(CB_ACCD, 0, 0);
#if MT_QKNORM
fmul(0, 1, 0);
#endif
load(CB_G, 0, 2);
fmul(0, 2, 0); // n = acc * rq * g (MT_QKNORM 0: n = acc * g, g = 128^-0.5 for q, 1 for k)
load(CB_ACSD, 0, 3);
#if MT_QKNORM
fmul(3, 1, 3);
#endif
load(CB_G, 1, 2);
fmul(3, 2, 3); // swap(n)
load(CB_TAB, 0, 2);
fmul(0, 2, 0); // n * cos
load(CB_TAB, 1, 2);
fmul(3, 2, 3); // swap(n) * sinS
fadd(0, 3, 0);
com();
wt();
pack_to(0, CB_QO, 0); // bf16, dense: the first 128 values are the head
rel();
vpush(CB_QO, 1);
#if MT_QKNORM
vpop(CB_T1, 1);
#endif
vpop(CB_ACCD, 1);
vpop(CB_ACSD, 1);
cb_pop_front(CB_G, 2);
}
NOINL void oproj() {
vwait(CB_O, AT);
acq();
MM_W(CB_O, wsa, AT, XS, 0);
vpop(CB_O, AT);
residual_add(XS);
}
NOINL void gateup() {
vwait(CB_H, DT);
vwait(CB_HD, NHD);
rstd_h();
vreserve(CB_FS, CAP_FS);
for (uint32_t f0 = 0; f0 < FS; f0 += 2) {
const uint32_t nb = FS - f0 < 2 ? FS - f0 : 2;
acq();
#if MT_MMBLK
mm_blk(CB_H, wsm, DT, 2 * nb, 0); // per k: gate f0, up f0 (, gate f0 + 1, up f0 + 1) -> DST 0 .. 2 nb - 1
#else
for (uint32_t j = 0; j < nb; ++j) {
mm_cols(CB_H, wsm, DT, 2, 2 * j); // gate -> 2j, up -> 2j + 1
}
#endif
load(CB_RS, 0, 7);
for (uint32_t j = 0; j < nb; ++j) {
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);
}
com();
wt();
for (uint32_t j = 0; j < nb; ++j) {
pack_to(2 * j, CB_FS, f0 + j);
}
rel();
}
vpush(CB_FS, CAP_FS);
vpop(CB_RS, 1);
vpop(CB_H, DT);
vpop(CB_HD, NHD);
}
NOINL void down() {
vwait(CB_F, IT);
acq();
MM_W(CB_F, wsm, IT, XS, 0);
vpop(CB_F, IT);
residual_add(XS);
}
NOINL void final_out(bool full) {
vwait(CB_H, DT);
vwait(CB_HD, NHD);
if (me.flags & F_DUMP) { // debug: the raw residual (no head)
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, DT);
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, DT);
vpop(CB_HD, NHD);
}
// consume every weight page the NCRISC still streams in this run (debug stops)
void drain_rings() {
const uint32_t na = me.nl * (MT_A_TILES / PT_A), nm = me.nl * (MT_M_TILES / PT_M);
uint32_t a = wsa.t ? 1 : 0, m = wsm.t ? 1 : 0;
if (wsa.t) {
cb_pop_front(CB_WA, PT_A);
}
if (wsm.t) {
cb_pop_front(CB_WM, PT_M);
}
// pages already consumed by qkv: count them from the tile counter of the A stream
a += wsa_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_stream_only() {
const uint32_t na = me.nl * (MT_A_TILES / PT_A), nm = me.nl * (MT_M_TILES / PT_M);
const bool full = me.nl == N_LAYERS_FULL && !(me.flags & F_DUMP) && !me.nohead;
const uint32_t nh = full ? MT_H_TILES / PT_H : 0;
uint32_t a = 0, m = 0;
while (a < na || m < nm) { // pop whichever ring is ahead (the NCRISC fills both)
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 mm_main() {
if (me.flags & F_ARM_STREAM) {
mm_stream_only();
return;
}
cb_wait_front(CB_CONST, N_CONST);
const bool qk = me.hk != HK_V;
if (qk) {
cb_wait_front(CB_TAB, 2);
}
// x0 (landed by the BRISC in CB_X0) -> CB_X: CB_X is then produced by the packer only
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();
TSTEP(l * 16 + 2);
if (me.stop == 1) { // debug stop: the first norm gather landed; drain the weight rings
vwait(CB_H, DT);
vwait(CB_HD, NHD);
drain_rings();
return;
}
qkv();
TSTEP(l * 16 + 3);
if (me.stop == 2) { // debug stop: the attention outputs landed
vwait(CB_O, AT);
drain_rings();
return;
}
oproj();
TSTEP(l * 16 + 4);
norm_send();
TSTEP(l * 16 + 5);
gateup();
TSTEP(l * 16 + 6);
down();
}
TSTEP(0xF00);
norm_send();
final_out(me.nl == N_LAYERS_FULL && !(me.flags & F_DUMP) && !me.nohead);
}
// ================================================================ ATT core (kv group g)
// scores of both heads for key block j -> CB_S tiny tiles h * 128 + j (all blocks of the layer: K streamed)
NOINL void att_layer() {
const uint32_t nb = me.nb;
const uint32_t st = (nb + 31) / 32; // dense score tiles per head
const uint32_t tl = (nb - 1) / 32; // the dense tile holding the last block
vwait(CB_Q, 2 * HD_T); // full-capacity cycle (fixed layout); heads 0 .. NH - 1 used
// the tail of the last dense score tile of each head = NEG (stale entries beyond the last block)
vreserve(CB_SD, CAP_SD);
acq();
load(CB_CONST, C_ONES, 0);
fscale(0, me.neg);
com();
wt();
pack_to(0, CB_SD, tl);
pack_to(0, CB_SD, 4 + 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 / NH) {
const uint32_t n = nb - j0 < 8 / NH ? nb - j0 : 8 / NH;
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 h = 0; h < NH; ++h) {
for (uint32_t d = 0; d < HD_T; ++d) {
mm_t<WFID>(CB_Q, CB_KR, h * HD_T + d, d, NH * jj + h);
}
}
cb_pop_front(CB_KR, HD_T);
if (j + 1 == nb) { // causal mask of the last block: += e0 @ MASK(p % 32)
mm_init_f(CB_CT, CB_MSK);
for (uint32_t h = 0; h < NH; ++h) {
mm_f(CB_CT, CB_MSK, 32 * C_E0, 0, NH * jj + h);
}
}
}
com();
wt();
for (uint32_t jj = 0; jj < n; ++jj) {
for (uint32_t h = 0; h < NH; ++h) {
pack_to(NH * jj + h, CB_S, h * 128 + j0 + jj);
}
}
rel();
}
vpush(CB_S, CAP_S);
vhand(CB_S, CAP_S, CB_SD, CAP_SD);
// both heads in shared DST sections (DST h): m_h = max(S_h) (elementwise over the dense tiles, then a scalar
// reduce, broadcast to a uniform tile); P = exp(S - m) bf16 (CB_P tiles h * 4 + t); 1/l_h -> CB_RS tile h
vreserve(CB_P, CAP_P);
vreserve(CB_T0, NH);
acq();
for (uint32_t h = 0; h < NH; ++h) {
load(CB_SD, h * 4, h);
for (uint32_t t = 1; t < st; ++t) {
load(CB_SD, h * 4 + t, 2);
fmax(h, 2, h);
}
}
com();
wt();
for (uint32_t h = 0; h < NH; ++h) {
pack_to(h, CB_T0, h);
}
rel();
vpush(CB_T0, NH);
vwait(CB_T0, NH);
vreserve(CB_T1, NH);
acq();
if (!g_nc) {
reconfig_full_operand(CB_T0, CB_CONST);
reduce_init<PoolType::MAX, ReduceDim::REDUCE_SCALAR>(CB_T0, CB_CONST, CB_T1);
for (uint32_t h = 0; h < NH; ++h) {
reduce_tile<PoolType::MAX, ReduceDim::REDUCE_SCALAR>(CB_T0, CB_CONST, h, C_ONES, h);
}
reduce_uninit(CB_T0);
}
com();
wt();
for (uint32_t h = 0; h < NH; ++h) {
pack_to(h, CB_T1, h);
}
rel();
vpush(CB_T1, NH);
vpop(CB_T0, NH);
vwait(CB_T1, NH);
vreserve(CB_T0, NH);
acq();
if (!g_nc) { // m_h uniform: ONES * T1[h][0][0]
reconfig_full_operand(CB_CONST, CB_T1);
mul_bcast_scalar_init(CB_CONST, CB_T1);
for (uint32_t h = 0; h < NH; ++h) {
mul_tiles_bcast_scalar(CB_CONST, CB_T1, C_ONES, h, h);
}
}
com();
wt();
for (uint32_t h = 0; h < NH; ++h) {
pack_to(h, CB_T0, h);
}
rel();
vpush(CB_T0, NH);
vpop(CB_T1, NH);
vwait(CB_T0, NH);
constexpr uint32_t EXN = (8 - NH) / NH; // score tiles per head per DST section (after the NH m tiles)
for (uint32_t t0 = 0; t0 < st; t0 += EXN) { // DST h = m_h, then up to EXN score tiles per head
const uint32_t n = st - t0 < EXN ? st - t0 : EXN;
acq();
for (uint32_t h = 0; h < NH; ++h) {
load(CB_T0, h, h);
}
for (uint32_t h = 0; h < NH; ++h) {
for (uint32_t j = 0; j < n; ++j) {
const uint32_t d = NH + h * n + j;
load(CB_SD, h * 4 + t0 + j, d);
fsub(d, h, d);
fexp(d);
}
}
com();
wt();
for (uint32_t h = 0; h < NH; ++h) {
for (uint32_t j = 0; j < n; ++j) {
pack_to(NH + h * n + j, CB_P, h * 4 + t0 + j);
}
}
rel();
}
vpop(CB_T0, NH);
vpop(CB_SD, CAP_SD);
vpush(CB_P, CAP_P);
vwait(CB_P, CAP_P);
// l_h = sum(P_h) from the bf16 P (exact products): P_t @ ONES summed over t -> T1 tile h; ONES @ T1; 1/l
vreserve(CB_T1, NH);
acq();
mm_init_f(CB_P, CB_CONST);
for (uint32_t h = 0; h < NH; ++h) {
for (uint32_t t = 0; t < st; ++t) {
mm_f(CB_P, CB_CONST, h * 4 + t, C_ONES, h);
}
}
com();
wt();
for (uint32_t h = 0; h < NH; ++h) {
pack_to(h, CB_T1, h);
}
rel();
vpush(CB_T1, NH);
vwait(CB_T1, NH);
vreserve(CB_RS, 2);
acq();
mm_init_f(CB_CONST, CB_T1);
for (uint32_t h = 0; h < NH; ++h) {
mm_f(CB_CONST, CB_T1, C_ONES, h, h);
frecip(h);
}
com();
wt();
for (uint32_t h = 0; h < NH; ++h) {
pack_to(h, CB_RS, h);
}
rel();
vpop(CB_T1, NH);
vpush(CB_RS, 2);
vhand(CB_P, CAP_P, CB_PT, CAP_PT);
// O_h = P_h V (V streamed): DST 0..3 head 0, 4..7 head 1
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 h = 0; h < NH; ++h) {
for (uint32_t d = 0; d < HD_T; ++d) {
mm_t<WFID>(CB_PT, CB_VR, h * 128 + j, d, h * HD_T + d);
}
}
cb_pop_front(CB_VR, HD_T);
}
com();
vreserve(CB_ACC, CAP_ACC);
wt();
for (uint32_t c = 0; c < NH * 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);
vwait(CB_RS, 2);
// o / l with the two-valued scale z = (1/l0) M01 + (1/l1) M10 (head 0 = elements 0..127, head 1 = 128..255)
vreserve(CB_QO, 1);
acq();
load(CB_RS, 0, 1);
if (NH == 2) {
load(CB_CONST, C_M01, 2);
fmul(1, 2, 1);
load(CB_RS, 1, 3);
load(CB_CONST, C_M10, 2);
fmul(3, 2, 3);
fadd(1, 3, 1);
}
load(CB_ACCD, 0, 0);
fmul(0, 1, 0);
com();
wt();
pack_to(0, CB_QO, 0); // dense bf16: o of head 0 (256 B) then head 1 (256 B); the BRISC multicasts them
rel();
vpush(CB_QO, 1);
vpop(CB_RS, 2);
vpop(CB_ACCD, 1);
vpop(CB_Q, 2 * HD_T);
}
void att_main() {
if (me.stop == 1) {
return;
}
cb_wait_front(CB_CONST, N_CONST);
cb_wait_front(CB_MSK, 1);
for (uint32_t l = 0; l < me.nl; ++l) {
TSTEP(l * 16 + 1);
att_layer();
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.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) {
return;
}
// the go word ((p + 1) | NOHEAD | DONE) from the BRISC through CB_GO (CBs reset at launch; read_tile_value
// hands the UNPACK thread's read to MATH and PACK)
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; // done (or the boot never completed: the DM RISCs abort)
}
me.p = (g & GO_P) - 1;
me.nohead = (g & GO_NOHEAD) != 0;
me.nb = me.p / 32 + 1;
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.flags & F_ARM_STREAM)) {
att_main();
}
}