changh95's picture
tt-model push paddleocr-vl (container)
22e0325 verified
Raw History Blame Contribute Delete
18.6 kB
// SPDX-License-Identifier: Apache-2.0
//
// Qwen3-ASR decode megakernel (DEC), BRISC. Adapted from the Qwen3-TTS talker megakernel (bundles/qwen3-tts
// tt/mk_talker/kernels/mt_brisc.cpp, same workspace, Apache-2.0): boot barrier, every exchange between cores, the
// small DRAM reads (STATE, the embedding row, constants, tables) and writes (KV append, logits, STATE).
// boot : every active core zeroes its sync words and counts in at the HUB (semaphore 0); the HUB reads STATE
// (p = L + ntok - 1; done when no token yet / stopped / KV full), then multicasts the go word
// (p + 1) | done << 31 into semaphore 1 of every core (reset at launch).
// MM : x0 = embed_tokens[S_LAST] (its slice, bf16 -> fp32); norm gathers (bf16 x slice -> CB_H of every core +
// flag W_NH[w] = gen), the q / k / v head -> its ATT core, the attention outputs (flags W_AO) -> CB_O, the
// SwiGLU gathers (-> CB_F, flags W_F); final: the logits slice -> DRAM page w, the local candidate list
// (greedy: the best entry; sampling: <= K_MAX entries) -> HUB.
// ATT : q heads -> CB_Q; the K / V blocks landed by the NCRISC -> CB_KR / CB_VR, the last one patched with the new
// k / v row (position p) and written back to the KV cache (8 KB pages); o -> CB_O of every core + flag.
// HUB : merge of the 32 lists, selection (greedy / Philox(seed, ntok) sampling), STATE: token ring, last token,
// ntok + 1, EOS / max-token stop.
// Every gather is single-buffered; the WAR argument is the dataflow (a slice of round g + 1 is sent only after every
// consumer of round g produced an output its sender waited for, md_trisc.cpp header).
#include "md_dm.hpp"
#define MKS_MAX_LISTS 32
#include "sampler.hpp"
using namespace mtx;
namespace {
constexpr auto acc = TensorAccessorArgs<CT_ACC0>();
constexpr uint32_t DT = MT_DT, IT = MT_IT, XS = MT_XS, FS = MT_FS, NHD = MT_NHD, D = MT_D;
constexpr uint32_t FS_CAP = MT_FS_CAP;
#ifndef MT_ATTS
#define MT_ATTS 1
#endif
constexpr uint32_t NH = 2 / MT_ATTS; // q heads per ATT core
#define NOINL __attribute__((noinline))
struct Me {
uint32_t idx, role, w, hk, slot, attx, atty;
uint32_t ax0, ay0, ax1, ay1, nall, nact, hubx, huby, nl, flags;
uint32_t p, nb, done, nohead;
};
Me me;
FORCE_INLINE auto dram(uint32_t addr, uint32_t page) { return TensorAccessor(acc, addr, page); }
// time stamps of layer 1 (and the step) at SYNC_PROF word i (always on; a few stores per step)
FORCE_INLINE void tstamp(uint32_t l, uint32_t i) {
if (l == 1) {
reinterpret_cast<volatile tt_l1_ptr uint32_t*>(cb_base(CB_SYNC) + SYNC_PROF)[i] = get_timestamp_32b();
}
}
FORCE_INLINE uint32_t stg(uint32_t k) { return cb_base(CB_SYNC) + SYNC_STG + k * 16; } // 16 B flag sources
// all-gather send: [src, src + bytes) -> dst on every MM core (incl. this one), then flag word w = v there.
// Unicast loop, data first then the flag per destination (same NoC, same command buffer: in order). No multicast:
// 32 concurrent multicasts to overlapping 2D rectangles hung the NoC (hang #2, 2026-11-30); unicasts cannot.
NOINL void mcast_flag(uint32_t src, uint32_t dst, uint32_t bytes, uint32_t flag_word, uint32_t v, uint32_t k) {
const uint32_t srcw = stg(k);
*reinterpret_cast<volatile tt_l1_ptr uint32_t*>(srcw) = v;
const uint32_t fa = sync_addr(flag_word);
for (uint32_t i = 0; i < NW; ++i) {
const uint32_t xy = ca(K_MM0 + i);
const uint32_t x = xy >> 16, y = xy & 0xFFFF;
noc_async_write(src, uni_addr(x, y, dst), bytes);
noc_async_write(srcw, uni_addr(x, y, fa), 4);
}
wr_flush();
}
// unicast [src, src + bytes) to dst on (x, y), then flag word w = v there (same NoC, same command buffer: in order)
NOINL void uni_flag(uint32_t x, uint32_t y, uint32_t src, uint32_t dst, uint32_t bytes, uint32_t flag_word,
uint32_t v, uint32_t k) {
const uint32_t srcw = stg(k);
*reinterpret_cast<volatile tt_l1_ptr uint32_t*>(srcw) = v;
noc_async_write(src, uni_addr(x, y, dst), bytes);
noc_async_write(srcw, uni_addr(x, y, sync_addr(flag_word)), 4);
wr_flush();
}
// ------------------------------------------------------------------ boot
void boot() {
const uint32_t sem_boot = get_semaphore(0);
const uint32_t sem_go = get_semaphore(1);
if (me.role == R_HUB) {
// STATE -> staging (ring A memory of the hub, past the H gather; CB_F / CB_E receive the f multicasts)
const uint32_t s = cb_base(CB_HG) + 16384;
noc_async_read_page(0, dram(ca(K_STATE), 8192), s);
rd_bar();
volatile tt_l1_ptr uint32_t* st = reinterpret_cast<volatile tt_l1_ptr uint32_t*>(s);
const uint32_t L = st[S_L], ntok = st[S_NTOK];
uint32_t done = st[S_DONE], nohead = 0;
uint32_t p = L + ntok - 1;
if (ntok == 0) {
done = 1; // the prefill has not produced token 0
}
if (p >= ca(K_MAXP) || (me.flags & F_BOOTONLY)) {
done = 1;
}
if (done) {
p = 0;
}
spin_l1_ge(sem_boot, me.nact - 1);
const uint32_t go = (p + 1) | (nohead ? GO_NOHEAD : 0u) | (done ? GO_DONE : 0u);
*reinterpret_cast<volatile tt_l1_ptr uint32_t*>(sem_go) = go;
noc_semaphore_set_multicast_loopback_src(sem_go, mcast_addr(me.ax0, me.ay0, me.ax1, me.ay1, sem_go), me.nall);
wr_flush();
} else {
noc_semaphore_inc(uni_addr(me.hubx, me.huby, sem_boot), 1);
at_bar();
}
const uint32_t g = spin_l1_ge(sem_go, 1);
me.done = (g == 0) || (g & GO_DONE);
me.nohead = (g & GO_NOHEAD) != 0;
me.p = (g & GO_P) - 1;
me.nb = me.p / 32 + 1;
}
// ------------------------------------------------------------------ MM
NOINL void norm_gather(uint32_t gen) {
cbw_front(CB_XB, 1);
mcast_flag(get_read_ptr(CB_XB), cb_base(CB_H) + me.w * XS * TINY16, XS * TINY16, W_NH + me.w, gen, 0);
cb_pop_front(CB_XB, 1);
spin_all(W_NH, NW, gen);
cbr_back(CB_H, DT);
cb_push_back(CB_H, DT);
cbr_back(CB_HD, NHD);
cb_push_back(CB_HD, NHD);
}
NOINL void send_head(uint32_t l) {
cbw_front(CB_QO, 1);
const uint32_t src = get_read_ptr(CB_QO);
uint32_t dst, flag;
if (me.hk == HK_Q) {
dst = cb_base(CB_Q) + me.slot * HD_T * TINY16;
flag = W_Q + me.slot;
} else if (me.hk == HK_K) {
dst = cb_base(CB_KVN);
flag = W_K;
} else {
dst = cb_base(CB_KVN) + 256;
flag = W_V;
}
uni_flag(me.attx, me.atty, src, dst, HD_T * TINY16, flag, l + 1, 1);
if (MT_ATTS == 2 && me.hk != HK_Q) { // split ATT: k / v rows to both cores of the group
uni_flag(rt(A_ATT2_X), rt(A_ATT2_Y), src, dst, HD_T * TINY16, flag, l + 1, 1);
}
cb_pop_front(CB_QO, 1);
}
NOINL void read_pages(uint32_t cb, uint32_t addr, uint32_t page_bytes, uint32_t page0, uint32_t n) {
cbr_back(cb, n);
const uint32_t a = get_write_ptr(cb);
const auto s = dram(addr, page_bytes);
for (uint32_t i = 0; i < n; ++i) {
noc_async_read_page(page0 + i, s, a + i * page_bytes);
}
rd_bar();
cb_push_back(cb, n);
}
void mm_main() {
// zero the dense tails (reductions read them) and the residual tile
l1_fill(cb_base(CB_ACC) + 512, (T32 - 512) / 4, 0);
l1_fill(cb_base(CB_ACS) + 512, (T32 - 512) / 4, 0);
read_pages(CB_CONST, ca(K_CONST), T16, 0, N_CONST);
// STATE (sampler words, last token) -> SYNC_SMP; x0 slice <- embed_tokens[S_LAST] (bf16 row, D * 2 B page)
uint32_t rope_p = me.p;
{
const uint32_t s = cb_base(CB_E);
noc_async_read_page(0, dram(ca(K_STATE), 8192), s);
rd_bar();
volatile tt_l1_ptr uint32_t* st = reinterpret_cast<volatile tt_l1_ptr uint32_t*>(s);
volatile tt_l1_ptr uint32_t* sm = reinterpret_cast<volatile tt_l1_ptr uint32_t*>(cb_base(CB_SYNC) + SYNC_SMP);
for (uint32_t i = 0; i < 18; ++i) {
sm[i] = st[i];
}
sm[18] = st[S_NTOK];
rope_p = me.p + st[S_ROPE]; // uint32 wrap-around = int32 delta (delta <= 0, p + delta >= 0)
const uint32_t tok = st[S_LAST];
const uint32_t e = s + 8192;
noc_async_read_page(tok, dram(ca(K_EMBED), D * 2), e);
rd_bar();
cbr_back(CB_X0, 1);
const uint32_t x0 = get_write_ptr(CB_X0);
l1_fill(x0, T32 / 4, 0);
volatile tt_l1_ptr uint16_t* src = reinterpret_cast<volatile tt_l1_ptr uint16_t*>(e) + me.w * XS * 32;
volatile tt_l1_ptr uint32_t* dst = reinterpret_cast<volatile tt_l1_ptr uint32_t*>(x0);
for (uint32_t i = 0; i < XS * 32; ++i) {
dst[i] = ((uint32_t)src[i]) << 16; // bf16 -> fp32 (exact)
}
cb_push_back(CB_X0, 1);
}
const bool qk = me.hk != HK_V;
if (qk) {
read_pages(CB_TAB, ca(K_ROPE), T32, 2 * rope_p, 2);
}
const uint32_t nl = me.nl;
for (uint32_t l = 0; l < nl; ++l) {
if (qk) {
read_pages(CB_G, ca(K_GAM), T32, l * 4 + (me.hk == HK_K ? 2 : 0), 2);
}
tstamp(l, 0);
norm_gather(2 * l + 1);
tstamp(l, 1);
if (ca(K_STOP) == 1) {
return;
}
send_head(l);
tstamp(l, 2);
spin_all(W_AO, NG * MT_ATTS, l + 1);
tstamp(l, 3);
cbr_back(CB_O, AT);
cb_push_back(CB_O, AT);
if (ca(K_STOP) == 2) {
return;
}
norm_gather(2 * l + 2);
tstamp(l, 4);
// SwiGLU gather
cbw_front(CB_FS, FS_CAP);
mcast_flag(get_read_ptr(CB_FS), cb_base(CB_F) + me.w * FS * TINY16, FS * TINY16, W_F + me.w, l + 1, 2);
cb_pop_front(CB_FS, FS_CAP);
spin_all(W_F, NW, l + 1);
tstamp(l, 5);
cbr_back(CB_F, IT);
cb_push_back(CB_F, IT);
}
tstamp(1, 8);
const bool dump = me.flags & F_DUMP;
const bool full = nl == N_LAYERS_FULL && !dump && !me.nohead;
norm_gather(2 * nl + 1);
if (dump) { // debug: the raw residual slice -> the logits page w (no head)
cbw_front(CB_HS, 1);
noc_async_write_page(me.w, dram(ca(K_LOGITS), MT_HC * TINY32), get_read_ptr(CB_HS));
wr_flush();
cb_pop_front(CB_HS, 1);
}
if (full) {
cbw_front(CB_LS, MT_HC);
const uint32_t lg = get_read_ptr(CB_LS);
// the raw logits of this core's columns (the logits output, host tests / host sampling) ...
noc_async_write_page(me.w, dram(ca(K_LOGITS), MT_HC * TINY32), lg);
if (!(me.flags & F_NOSAMPLE)) {
// ... and the local candidate list of ids w * HC * 32 .. (valid vocabulary only)
volatile tt_l1_ptr uint32_t* sm = reinterpret_cast<volatile tt_l1_ptr uint32_t*>(cb_base(CB_SYNC) + SYNC_SMP);
volatile tt_l1_ptr uint32_t* bits = reinterpret_cast<volatile tt_l1_ptr uint32_t*>(lg);
const uint32_t id0 = me.w * MT_HC * 32;
const uint32_t nv = sm[S_NVOCAB];
const uint32_t n = id0 >= nv ? 0u : (nv - id0 < MT_HC * 32 ? nv - id0 : MT_HC * 32);
const uint32_t cap = sm[S_DOSAMPLE] ? (ca(K_KMAX) > 64 ? 64 : ca(K_KMAX)) : 1u;
const uint32_t lst = cb_base(CB_SYNC) + SYNC_LIST;
mks::Entry* ent = reinterpret_cast<mks::Entry*>(lst + 16);
const uint32_t cnt = mks::local_sorted(bits, n, id0, ent, cap);
*reinterpret_cast<volatile tt_l1_ptr uint32_t*>(lst) = cnt;
noc_async_write(lst, uni_addr(me.hubx, me.huby, cb_base(CB_LG) + LIST0 + me.w * LIST_B), 16 + 8 * cnt);
}
const uint32_t srcw = stg(4);
*reinterpret_cast<volatile tt_l1_ptr uint32_t*>(srcw) = 1;
noc_async_write(srcw, uni_addr(me.hubx, me.huby, sync_addr(W_LS + me.w)), 4);
wr_flush();
cb_pop_front(CB_LS, MT_HC);
}
}
// ------------------------------------------------------------------ ATT
// row r of a 32-position block (4 bf16 tiles of 32 positions x 32 dims) <- the 128 bf16 values at src
FORCE_INLINE void patch_row(uint32_t blk, uint32_t src, uint32_t r) {
for (uint32_t d = 0; d < HD_T; ++d) {
for (uint32_t c2 = 0; c2 < 2; ++c2) {
const uint32_t face = (r / 16) * 2 + c2;
const uint32_t dst = blk + d * T16 + (face * 256 + (r % 16) * 16) * 2;
l1_copy(src + (32 * d + 16 * c2) * 2, dst, 8);
}
}
}
void att_main() {
if (ca(K_STOP) == 1) {
return;
}
const uint32_t g = me.w;
const uint32_t nb = me.nb, jb = nb - 1, r = me.p % 32;
const uint32_t NB = ca(K_NB);
read_pages(CB_CONST, ca(K_CONST), T16, 0, N_CONST);
read_pages(CB_MSK, ca(K_MASK), T16, r, 1);
const uint32_t kbase = cb_base(CB_KR), vbase = cb_base(CB_VR);
const uint32_t rb = MT_KV_RING_BLOCKS;
const auto kv = dram(ca(K_KV), T16);
uint32_t kt = 0, vt = 0;
for (uint32_t l = 0; l < me.nl; ++l) {
tstamp(l, 10);
spin_ge(W_Q, l + 1);
if (NH == 2) {
spin_ge(W_Q + 1, l + 1);
}
tstamp(l, 11);
cbr_back(CB_Q, 2 * HD_T); // full-capacity cycle: the q heads land at the CB base (fixed layout)
cb_push_back(CB_Q, 2 * HD_T);
for (uint32_t j = 0; j < nb; ++j, ++kt) {
spin_ge(W_KL, kt + 1);
if (j == jb) {
const uint32_t blk = kbase + (kt % rb) * KV_PAGE;
spin_ge(W_K, l + 1);
patch_row(blk, cb_base(CB_KVN), r);
if (me.slot == 0) { // split ATT: one core of the group writes the block back
for (uint32_t d = 0; d < HD_T; ++d) {
noc_async_write_page((((l * 2 + 0) * NG + g) * NB + jb) * HD_T + d, kv, blk + d * T16);
}
wr_flush();
}
}
cbr_back(CB_KR, HD_T);
cb_push_back(CB_KR, HD_T);
}
for (uint32_t j = 0; j < nb; ++j, ++vt) {
spin_ge(W_VL, vt + 1);
if (j == jb) {
const uint32_t blk = vbase + (vt % rb) * KV_PAGE;
spin_ge(W_V, l + 1);
patch_row(blk, cb_base(CB_KVN) + 256, r);
if (me.slot == 0) {
for (uint32_t d = 0; d < HD_T; ++d) {
noc_async_write_page((((l * 2 + 1) * NG + g) * NB + jb) * HD_T + d, kv, blk + d * T16);
}
wr_flush();
}
}
cbr_back(CB_VR, HD_T);
cb_push_back(CB_VR, HD_T);
}
tstamp(l, 12);
cbw_front(CB_QO, 1);
tstamp(l, 13);
mcast_flag(get_read_ptr(CB_QO), cb_base(CB_O) + (g * 2 + me.slot) * HD_T * TINY16, NH * HD_T * TINY16,
W_AO + g * MT_ATTS + me.slot, l + 1, 5);
tstamp(l, 14);
cb_pop_front(CB_QO, 1);
if (ca(K_STOP) == 2) {
return;
}
}
}
// ------------------------------------------------------------------ HUB
void hub_main() {
if (ca(K_STOP)) {
return;
}
const bool dump = me.flags & F_DUMP;
const bool full = me.nl == N_LAYERS_FULL && !dump && !me.nohead;
if (!full) {
wr_bar();
return;
}
spin_all(W_LS, NW, 1);
const uint32_t s = cb_base(CB_HG) + 16384; // STATE staged at boot
volatile tt_l1_ptr uint32_t* st = reinterpret_cast<volatile tt_l1_ptr uint32_t*>(s);
const uint32_t ntok = st[S_NTOK];
if (!(me.flags & F_NOSAMPLE)) {
const mks::Entry* lists[NW];
uint32_t counts[NW];
for (uint32_t w = 0; w < NW; ++w) {
const uint32_t a = cb_base(CB_LG) + LIST0 + w * LIST_B;
counts[w] = *reinterpret_cast<volatile tt_l1_ptr uint32_t*>(a);
lists[w] = reinterpret_cast<const mks::Entry*>(a + 16);
}
mks::Merger mg;
mg.init(lists, counts, NW);
mks::Entry best[64];
uint32_t wts[64];
const uint32_t kmax = st[S_DOSAMPLE] ? (ca(K_KMAX) > 64 ? 64 : ca(K_KMAX)) : 1u;
const uint32_t cnt = mks::merge_take(mg, best, kmax);
mks::Params pr{st[S_DOSAMPLE], st[S_INVT], st[S_TOPK], st[S_TOPP]};
const uint32_t u = mks::philox_u24(st[S_SEED_LO], st[S_SEED_HI], ntok, 0);
uint32_t t = mks::select_topk(best, cnt, pr, u, kmax, wts, nullptr);
if (t == mks::INVALID) {
t = st[S_EOS1];
}
st[S_TOKENS + ntok % S_TOKENS_MAX] = t; // circular ring (PaddleOCR-VL: up to 4096 positions)
st[S_LAST] = t;
const uint32_t n1 = ntok + 1;
st[S_NTOK] = n1;
if (t == st[S_EOS0] || t == st[S_EOS1]) {
st[S_DONE] = 1;
st[S_STOP] = 1;
} else if (n1 >= st[S_MAXNEW]) {
st[S_DONE] = 1;
st[S_STOP] = 2;
} else if (st[S_L] + n1 - 1 >= ca(K_MAXP)) {
st[S_DONE] = 1;
st[S_STOP] = 3;
}
} else {
st[S_NTOK] = ntok + 1; // logits only: the host samples and rewrites S_LAST / the ring
}
noc_async_write_page(0, dram(ca(K_STATE), 8192), s);
wr_bar();
}
} // namespace
void kernel_main() {
me.idx = rt(A_IDX);
me.role = rt(A_ROLE);
if (me.role == R_IDLE) {
return;
}
me.w = rt(A_W);
me.hk = rt(A_HK);
me.slot = rt(A_SLOT);
me.attx = rt(A_ATT_X);
me.atty = rt(A_ATT_Y);
me.ax0 = ca(K_ALL_X0);
me.ay0 = ca(K_ALL_Y0);
me.ax1 = ca(K_ALL_X1);
me.ay1 = ca(K_ALL_Y1);
me.nall = ca(K_N_ALL);
me.nact = ca(K_N_ACT);
me.hubx = ca(K_HUB_X);
me.huby = ca(K_HUB_Y);
me.nl = ca(K_NL);
me.flags = ca(K_FLAGS);
g_spin = ca(K_SPIN);
g_dbg_word = W_DBG;
for (uint32_t w = 0; w < N_SYNC; ++w) {
*sync_ptr(w) = 0;
}
boot();
if (me.role == R_MM || me.role == R_ATT) { // the go word -> the TRISC
const uint32_t gw = me.done ? GO_DONE : ((me.p + 1) | (me.nohead ? GO_NOHEAD : 0u));
cbr_back(CB_GO, 1);
*reinterpret_cast<volatile tt_l1_ptr uint32_t*>(get_write_ptr(CB_GO)) = gw;
cb_push_back(CB_GO, 1);
}
if (!me.done && !g_abort && !(me.flags & F_ARM_STREAM)) {
if (me.role == R_MM) {
mm_main();
} else if (me.role == R_ATT) {
att_main();
} else {
hub_main();
}
}
// diagnostics: the first 4 KB of the sync area (every sync word) -> debug page idx
{
*sync_ptr(W_DBG + 8) = me.p;
*sync_ptr(W_DBG + 9) = me.done;
noc_async_write_page(me.idx, dram(ca(K_DBG), SYNC_BYTES), cb_base(CB_SYNC));
}
exit_barriers(1);
}