changh95's picture
tt-model push schift-ocr-1-beta (container)
ca1a504 verified
Raw History Blame Contribute Delete
12.4 kB
// SPDX-License-Identifier: Apache-2.0
//
// DeepSeek-OCR family DEC-MoE megakernel, NCRISC: the DRAM streams (from this workspace's GLM-OCR DEC
// tt/mk_dec/kernels/md_ncrisc.cpp, Apache-2.0, itself from the Qwen3-ASR DEC / Qwen3-TTS talker megakernel).
// MM : A (attention: qkv head + o_proj columns), M (MLP), H (lm_head): bank-local reads of whole packets from the
// core's own DRAM bank, up to DEPTH transaction ids in flight; a packet goes to its stream's ring as soon as
// the ring has room. M is SEGMENTED: per MoE layer [shared gate/up] [6 routed expert gate/up blocks]
// [shared down] [6 routed expert down blocks]; a routed segment is issued once the routing record of its
// layer has arrived (W_RT >= layer; ids at SYNC_ROUTE + (layer & 1) * 64), never blocking the A / H streams.
// ROUTER: its router column sticks (bf16, 40 tiles per MoE layer) into ring M.
// ATT : the K and V blocks (8 KB KV pages) of the group's key slot for every layer.
#include "md_dm.hpp"
using namespace mtx;
namespace {
constexpr auto acc = TensorAccessorArgs<CT_ACC0>();
constexpr uint32_t DEPTH = 8;
constexpr uint32_t PT_A = 8, PT_M = 4, PT_H = 8, PT_R = 2;
struct Stream {
uint32_t cb; // ring CB
uint32_t base; // ring base address
uint32_t bytes; // ring bytes (multiple of pk)
uint32_t pk; // packet bytes
uint32_t pages; // CB pages per packet
uint32_t total; // packets
uint32_t issued, landed;
uint32_t wr; // ring write offset
uint16_t issued_pages;
uint32_t src; // linear streams: bank-local byte offset of the next packet
uint32_t sync_word; // ATT: landing counter word (0 = push the ring instead)
uint32_t kv; // ATT: 0 = K, 1 = V
uint32_t seg; // 1 = segmented M stream (src from the segment walker)
};
struct Me {
uint32_t role, w, bank, vc, nl, flags, p, nb, done, j0, my;
uint32_t xn, nsc, rcol, hk;
};
Me me;
// ------------------------------------------------------------------ segmented M stream (MM cores)
struct MWalk {
uint32_t l; // current layer
uint32_t s; // segment index within the layer
uint32_t left; // packets left in the current segment (0 = segment not opened)
uint32_t src; // next packet's source offset
uint32_t ids[NSLOT];
bool ids_ok;
uint32_t off_m, tb, m0, moe, sgu, egu, sdn, edn;
};
MWalk mw;
FORCE_INLINE uint32_t layer_base(uint32_t l) {
return mw.off_m + (l == 0 ? 0u : (mw.m0 + (l - 1) * mw.moe) * mw.tb);
}
// open segment s of layer l: returns false when its routing is not known yet (or the layer has no such segment)
bool open_seg() {
const uint32_t l = mw.l;
const uint32_t b = layer_base(l);
if (l == 0) {
mw.src = b;
mw.left = mw.m0 / PT_M;
return true;
}
const uint32_t nr = mw.egu ? NSLOT : 0; // routed gate/up segments
uint32_t s = mw.s;
if (s == 0) {
mw.src = b;
mw.left = mw.sgu / PT_M;
return true;
}
if (s <= nr || s >= nr + 2) { // routed segment: needs the ids of layer l
if (!mw.ids_ok && (me.flags & F_ARM_STREAM)) { // timing arm: no router; experts 0..5
for (uint32_t k = 0; k < NSLOT; ++k) {
mw.ids[k] = k;
}
mw.ids_ok = true;
}
if (!mw.ids_ok) {
if (sync_read(W_RT) < l) {
return false;
}
volatile tt_l1_ptr uint32_t* r =
reinterpret_cast<volatile tt_l1_ptr uint32_t*>(cb_base(CB_SYNC) + SYNC_ROUTE + (l & 1) * 64);
for (uint32_t k = 0; k < NSLOT; ++k) {
const uint32_t e = r[k];
mw.ids[k] = e < NEXP ? e : 0;
}
mw.ids_ok = true;
}
}
if (s <= nr) {
mw.src = b + (mw.sgu + mw.ids[s - 1] * mw.egu) * mw.tb;
mw.left = mw.egu / PT_M;
} else if (s == nr + 1) {
mw.src = b + (mw.sgu + NEXP * mw.egu) * mw.tb;
mw.left = mw.sdn / PT_M;
} else {
mw.src = b + (mw.sgu + NEXP * mw.egu + mw.sdn + mw.ids[s - nr - 2] * mw.edn) * mw.tb;
mw.left = mw.edn / PT_M;
}
return true;
}
FORCE_INLINE uint32_t segs_of(uint32_t l) { return l == 0 ? 1u : (mw.egu ? 2u * NSLOT + 2u : NSLOT + 2u); }
// the source of the next M packet, false when blocked on routing
bool m_next(uint32_t& src) {
while (mw.left == 0) {
if (!open_seg()) {
return false;
}
if (mw.left == 0) { // empty segment
if (++mw.s == segs_of(mw.l)) {
mw.s = 0;
++mw.l;
mw.ids_ok = false;
}
}
}
src = mw.src;
return true;
}
FORCE_INLINE void m_adv() {
mw.src += PT_M * mw.tb;
if (--mw.left == 0) {
if (++mw.s == segs_of(mw.l)) {
mw.s = 0;
++mw.l;
mw.ids_ok = false;
}
}
}
FORCE_INLINE uint32_t free_pages(const Stream& s) {
invalidate_l1_cache();
const uint16_t acked = (uint16_t)reg_read((uintptr_t)get_cb_tiles_acked_ptr(s.cb));
return get_local_cb_interface(s.cb).fifo_num_pages - (uint16_t)(s.issued_pages - acked);
}
void run(Stream* st, uint32_t ns) {
uint8_t fifo[DEPTH];
uint32_t head = 0, n = 0, rr = 0, ntrid = 0;
const uint32_t wts = ca(K_WTS);
const uint32_t kvaddr = ca(K_KV);
const uint32_t NB = ca(K_NB);
const auto kvacc = TensorAccessor(acc, kvaddr, T16);
uint32_t it = 0, stuck = 0;
while (true) {
bool all = true;
for (uint32_t i = 0; i < ns; ++i) {
all = all && st[i].landed == st[i].total;
}
if (all) {
break;
}
bool progress = false;
if (n > 0 && ncrisc_noc_read_with_transaction_id_flushed(noc_index, 1 + (head % DEPTH))) {
invalidate_l1_cache();
Stream& s = st[fifo[head % DEPTH]];
++s.landed;
if (s.sync_word) {
*sync_ptr(s.sync_word) = s.landed;
} else {
cb_push_back(s.cb, s.pages);
}
++head;
--n;
progress = true;
}
if (n < DEPTH && !g_abort) {
for (uint32_t k = 0; k < ns; ++k) {
const uint32_t i = (rr + k) % ns;
Stream& s = st[i];
if (s.issued == s.total || free_pages(s) < s.pages) {
continue;
}
uint32_t src = s.src;
if (s.seg && !m_next(src)) {
continue; // routing of the layer not known yet
}
const uint32_t trid = 1 + (ntrid % DEPTH);
noc_async_read_set_trid(trid);
const uint32_t dst = s.base + s.wr;
if (s.sync_word == 0) {
if (!(me.flags & F_ARM_NOREAD)) {
noc_async_read(get_noc_addr_from_bank_id<true>(me.bank, wts + src), dst, s.pk);
}
if (s.seg) {
m_adv();
} else {
s.src += s.pk;
}
} else {
const uint32_t l = s.issued / me.my, j = me.j0 + s.issued % me.my;
const uint32_t t0 = (((l * 2 + s.kv) * NG + me.w) * NB + j) * HD_T;
for (uint32_t d = 0; d < HD_T; ++d) {
noc_async_read_page(t0 + d, kvacc, dst + d * T16);
}
}
s.wr += s.pk;
if (s.wr == s.bytes) {
s.wr = 0;
}
s.issued_pages += s.pages;
++s.issued;
fifo[ntrid % DEPTH] = i;
++ntrid;
++n;
rr = i + 1;
progress = true;
break;
}
}
// a read that never completes (a dropped DRAM request) must not hang the launch: once this RISC has aborted
// and the oldest transaction id stayed outstanding for another g_spin polls, forget every id in flight
// (record AB_TRID), clear the per-id counters and fall into the push-everything exit below
if (g_abort && n > 0 && !progress) {
if (++stuck > g_spin) {
*sync_ptr(W_DBG + 3) = 0x6000u | (1 + (head % DEPTH));
*sync_ptr(W_DBG + 2) = AB_TRID;
reset_noc_trid_barrier_counter(NOC_CLEAR_OUTSTANDING_REQ_MASK, noc_index);
n = 0;
}
} else {
stuck = 0;
}
if (g_abort && n == 0) {
for (uint32_t i = 0; i < ns; ++i) {
Stream& s = st[i];
while (s.landed < s.total) {
++s.landed;
if (s.sync_word) {
*sync_ptr(s.sync_word) = s.landed;
} else {
cb_push_back(s.cb, s.pages);
}
}
}
break;
}
if (progress) {
it = 0;
} else if (++it > g_spin) {
abort_note(AB_FRONT, 0x5000 + n);
} else {
peer_abort(it);
}
}
noc_async_read_set_trid(0);
}
} // namespace
void kernel_main() {
me.role = rt(A_ROLE);
if (me.role != R_MM && me.role != R_ATT && me.role != R_RT) {
return;
}
me.w = rt(A_W);
me.bank = rt(A_BANK);
me.vc = rt(A_VC);
me.nl = ca(K_NL);
me.flags = ca(K_FLAGS);
g_spin = ca(K_SPIN);
g_dbg_word = W_DBG + 2;
const uint32_t g = spin_go_nopeer(get_semaphore(1));
if (g == 0 || (g & GO_DONE)) {
return;
}
me.p = (g & GO_P) - 1;
me.nb = me.p / 32 + 1;
reset_noc_trid_barrier_counter(NOC_CLEAR_OUTSTANDING_REQ_MASK, noc_index);
if (me.role == R_MM) {
me.xn = rt(A_XN);
me.nsc = rt(A_NSC);
me.rcol = rt(A_RCOL);
me.hk = rt(A_HK);
const bool full = me.nl == N_LAYERS_FULL && !(me.flags & F_DUMP) && !(g & GO_NOHEAD);
const uint32_t a_tiles = (me.hk != HK_NONE ? HD_T * DT : 0u) + me.xn * AT;
mw.l = 0;
mw.s = 0;
mw.left = 0;
mw.ids_ok = false;
mw.off_m = rt(A_OFF_M);
mw.tb = MT_TB_M;
mw.m0 = FS * 2 * DT + me.xn * ITK0;
mw.sgu = me.nsc * 2 * DT;
mw.egu = me.rcol ? 2 * DT : 0u;
mw.sdn = me.xn * SH_T;
mw.edn = me.xn * 28;
mw.moe = mw.sgu + NEXP * mw.egu + mw.sdn + NEXP * mw.edn;
uint32_t m_pk = mw.m0 / PT_M; // packets the TRISC consumes over me.nl layers
for (uint32_t l = 1; l < me.nl; ++l) {
m_pk += (mw.sgu + NSLOT * mw.egu + mw.sdn + NSLOT * mw.edn) / PT_M;
}
Stream st[3] = {
{CB_WA, cb_base(CB_WA), MT_RING_A, PT_A * MT_TB_A, PT_A, me.nl * (a_tiles / PT_A), 0, 0, 0, 0,
rt(A_OFF_A), 0, 0, 0},
{CB_WM, cb_base(CB_WM), MT_RING_M, PT_M * MT_TB_M, PT_M, m_pk, 0, 0, 0, 0, 0, 0, 0, 1},
{CB_WH, cb_base(CB_WH), MT_RING_H, PT_H * MT_TB_H, PT_H, full ? (MT_HC * DT / PT_H) : 0u, 0, 0, 0, 0,
rt(A_OFF_H), 0, 0, 0},
};
run(st, 3);
} else if (me.role == R_RT) {
if (!(me.flags & (F_ARM_STREAM | F_FORCE_ROUTE)) && ca(K_STOP) == 0 && me.nl > 1) {
Stream st[1] = {
{CB_WR, cb_base(CB_WR), MT_RING_M, PT_R * T16, PT_R, (me.nl - 1) * (DT / PT_R), 0, 0, 0, 0,
rt(A_OFF_M), 0, 0, 0},
};
run(st, 1);
}
} else if (!(me.flags & F_ARM_STREAM) && ca(K_STOP) != 1) {
const uint32_t attk = ca(K_ATTK);
const uint32_t nbl = (me.nb + attk - 1) / attk;
me.j0 = rt(A_SLOT) * nbl;
if (me.j0 < me.nb) {
me.my = (me.j0 + nbl < me.nb ? me.j0 + nbl : me.nb) - me.j0;
const uint32_t rbytes = MT_KV_RING_BLOCKS * KV_PAGE;
Stream st[2] = {
{CB_KR, cb_base(CB_KR), rbytes, KV_PAGE, HD_T, me.nl * me.my, 0, 0, 0, 0, 0, W_KL, 0, 0},
{CB_VR, cb_base(CB_VR), rbytes, KV_PAGE, HD_T, me.nl * me.my, 0, 0, 0, 0, 0, W_VL, 1, 0},
};
run(st, 2);
}
}
exit_barriers(2);
}