DepthPro2 / code /tt_depthpro2 /tt /mk_dec /kernels /md_ncrisc.cpp
changh95's picture
tt-model push depthpro2 (container)
b7e49b8 verified
Raw History Blame Contribute Delete
9.06 kB
// SPDX-License-Identifier: Apache-2.0
//
// DepthPro2 DEC megakernel, NCRISC (NoC 0): the weight ring of each grid row (mk_enc's ring protocol, row multicast).
// feeder (0, y): walks the row's schedule and, page by page, reads the weight-arena page from DRAM straight into
// its own ring slot, pushes it for its own TRISC and multicasts it to the same slot of columns
// 1 .. GX - 1 of its row, then the cumulative valid count (W_WVAL). A page goes out only when every
// receiver granted it (per-receiver credits W_CRED + x). Blocks never wrap the ring: the pages a
// block would wrap over are sent as skip pages (any arena page; the TRISC pops them unused).
// receiver : grants ring slots as its TRISC frees them and pushes the pages the feeder flagged valid.
#include "md_dm.hpp"
using namespace mdx;
namespace {
constexpr auto acc_dram = TensorAccessorArgs<CT_ACC0>();
#ifndef MD_FEED_DEPTH
#define MD_FEED_DEPTH 4
#endif
constexpr uint32_t DEPTH = MD_FEED_DEPTH; // outstanding weight-page DRAM reads (transaction ids 1 .. DEPTH)
constexpr uint32_t PAGE_BYTES = PAGE_TILES * T16;
constexpr uint32_t NRECV = GX - 1;
Plan pl;
uint32_t SLOTS = 0;
FORCE_INLINE uint32_t freed_pages(uint32_t pushed) {
const uint32_t out = ring_outstanding_tiles();
return pushed - (out + PAGE_TILES - 1) / PAGE_TILES;
}
// ------------------------------------------------------------------ the row's page sequence
// calls emit(arena page) for every ring page of the row, in order
template <typename F>
FORCE_INLINE void walk(uint32_t x, uint32_t y, uint32_t nl, F&& emit) {
uint32_t pos = 0;
const uint32_t ngrp = pl.hdr(H_NGRP);
for (uint32_t grp = 0; grp < ngrp; ++grp) {
for (uint32_t l = 0; l < nl; ++l) {
Sched s;
sched_of(pl, l, x, y, s);
const uint32_t nbi_n = pl.lf(l, L_NBI), nkb = pl.lf(l, L_NKB);
for (uint32_t gi = 0; gi < s.ng; ++gi) {
const uint32_t g = s.g0 + gi * s.gstep;
for (uint32_t k = 0; k < s.rounds; ++k) {
for (uint32_t nbi = 0; nbi < nbi_n; ++nbi) {
const uint32_t nb = g * nbi_n + nbi;
for (uint32_t kb = 0; kb < nkb; ++kb) {
const uint32_t np = blk_pages(pl, l, kb);
const uint32_t skip = ring_skip(pos, np, SLOTS);
for (uint32_t i = 0; i < skip; ++i) {
emit(0u);
}
const uint32_t p0 = blk_page(pl, l, nb, kb);
for (uint32_t i = 0; i < np; ++i) {
emit(p0 + i);
}
pos += skip + np;
}
}
}
}
}
}
}
// ------------------------------------------------------------------ receiver
void receiver(uint32_t total, uint32_t x) {
const uint64_t cred = get_noc_addr(rt(A_FX), rt(A_FY), sync_addr(W_CRED + x));
uint32_t granted = 0, pushed = 0, idle = 0;
while (pushed < total && !g_abort) {
WAYPOINT("NRCV");
if (++idle > g_spin) {
abort_note(AB_SYNC, W_WVAL);
break;
}
if (peer_abort(idle)) {
break;
}
if (granted < total) {
uint32_t lim = freed_pages(pushed) + SLOTS;
if (lim > total) {
lim = total;
}
if (lim > granted) {
noc_semaphore_inc(cred, lim - granted);
granted = lim;
}
}
const uint32_t v = sync_read(W_WVAL);
if (v > pushed) {
cb_push_back(CB_W, (v - pushed) * PAGE_TILES);
pushed = v;
idle = 0;
}
}
at_bar();
}
// ------------------------------------------------------------------ feeder
struct Feeder {
uint32_t rd_issued = 0, rd_done = 0, mc_done = 0, mc_flushed = 0;
uint32_t base = 0, wval = 0, x0 = 0, x1 = 0, ry = 0, wts = 0;
FORCE_INLINE void service() {
if (rd_done < rd_issued && ncrisc_noc_read_with_transaction_id_flushed(noc_index, 1 + (rd_done % DEPTH))) {
invalidate_l1_cache();
++rd_done;
cb_push_back(CB_W, PAGE_TILES);
}
if (NRECV > 0 && mc_done < rd_done) {
uint32_t mn = 0xFFFFFFFFu;
for (uint32_t r = 1; r <= NRECV; ++r) {
const uint32_t c = sync_read(W_CRED + r);
mn = c < mn ? c : mn;
}
if (mn > mc_done) {
if (mc_done - mc_flushed >= N_SRCV) {
wr_flush();
mc_flushed = mc_done;
}
const uint32_t a = base + (mc_done % SLOTS) * PAGE_BYTES;
const uint32_t src = sync_addr(W_SRCV + (mc_done % N_SRCV));
#if defined(MD_MCAST_SAFE) && MD_MCAST_SAFE == 1
// unlinked data multicast, acked before the flag multicast (no held link; strict ordering)
noc_async_write_multicast(a, mcast_addr(x0, ry, x1, ry, a), PAGE_BYTES, NRECV, false);
wr_bar();
*reinterpret_cast<volatile tt_l1_ptr uint32_t*>(src) = mc_done + 1;
noc_semaphore_set_multicast(src, mcast_addr(x0, ry, x1, ry, wval), NRECV);
wr_bar();
#elif defined(MD_MCAST_SAFE) && MD_MCAST_SAFE == 2
// unlinked data multicast, flag ordered behind it by the static multicast VC (no barrier)
noc_async_write_multicast(a, mcast_addr(x0, ry, x1, ry, a), PAGE_BYTES, NRECV, false);
*reinterpret_cast<volatile tt_l1_ptr uint32_t*>(src) = mc_done + 1;
noc_semaphore_set_multicast(src, mcast_addr(x0, ry, x1, ry, wval), NRECV);
#else
noc_async_write_multicast(a, mcast_addr(x0, ry, x1, ry, a), PAGE_BYTES, NRECV, true);
*reinterpret_cast<volatile tt_l1_ptr uint32_t*>(src) = mc_done + 1;
noc_semaphore_set_multicast(src, mcast_addr(x0, ry, x1, ry, wval), NRECV);
#endif
++mc_done;
#ifdef MD_FEED_SERIAL
wr_flush(); // one page multicast in flight at a time (NoC-load experiment)
mc_flushed = mc_done;
#endif
}
} else if (NRECV == 0) {
mc_done = rd_done;
}
}
FORCE_INLINE void emit(uint32_t page) {
uint32_t idle = 0;
while (!g_abort) {
WAYPOINT("NFED");
if (++idle > g_spin) {
abort_note(AB_CRED, rd_issued);
return;
}
if (peer_abort(idle)) {
return;
}
if (rd_issued - rd_done < DEPTH && rd_issued < freed_pages(rd_done) + SLOTS) {
bool ok = true;
if (rd_issued >= SLOTS) {
const uint32_t prev = rd_issued - SLOTS;
if (mc_done <= prev) {
ok = false;
} else if (mc_flushed <= prev) {
wr_flush();
mc_flushed = mc_done;
}
}
if (ok) {
noc_async_read_set_trid(1 + (rd_issued % DEPTH));
const auto acc = TensorAccessor(acc_dram, wts, PAGE_BYTES);
noc_async_read_page(page, acc, base + (rd_issued % SLOTS) * PAGE_BYTES);
++rd_issued;
return;
}
}
service();
}
}
FORCE_INLINE void finish() {
uint32_t idle = 0;
while ((rd_done < rd_issued || mc_done < rd_done) && !g_abort) {
service();
if (++idle > g_spin) {
abort_note(AB_CRED, 1000000 + mc_done);
break;
}
if (peer_abort(idle)) {
break;
}
}
noc_async_read_set_trid(0);
wr_bar();
}
};
} // namespace
void kernel_main() {
const uint32_t role = rt(A_ROLE);
const uint32_t x = rt(A_X), y = rt(A_Y);
g_spin = ca(K_SPIN);
g_dbg_word = W_DBG + 2;
wait_go(get_semaphore(1)); // SEM_GO: the BRISCs zeroed every sync word
pl = wait_plan();
if (g_abort) {
return;
}
SLOTS = pl.hdr(H_RING);
uint32_t nl = ca(K_NL);
if (nl > pl.hdr(H_NL)) {
nl = pl.hdr(H_NL);
}
if (role & R_FEED) {
Feeder f;
f.base = cb_base(CB_W);
f.wval = sync_addr(W_WVAL);
f.x0 = rt(A_ROWX0);
f.x1 = rt(A_ROWX1);
f.ry = rt(A_ROWY);
f.wts = ca(K_WTS);
walk(x, y, nl, [&](uint32_t page) { f.emit(page); });
f.finish();
} else {
uint32_t total = 0;
walk(x, y, nl, [&](uint32_t) { ++total; });
receiver(total, x);
}
noc_async_read_set_trid(0);
exit_barriers(2);
}