DepthPro2 / code /tt_depthpro2 /tt /mk_enc /kernels /me_ncrisc.cpp
changh95's picture
tt-model push depthpro2 (container)
b7e49b8 verified
Raw History Blame Contribute Delete
5.38 kB
// SPDX-License-Identifier: Apache-2.0
//
// DepthPro2 ENC megakernel, NCRISC (NoC 0): the weight ring (mk_vision's mv_ncrisc.cpp protocol).
// feeder (x, 0): reads its column's pages segment by segment (embed arena x W waves, then block l's arena x W waves
// for l < nl) from DRAM straight into its own ring slot, pushes them for its own TRISC and multicasts
// each page to the same slot of rows 1 .. R - 1 of its column, then the cumulative valid count
// (W_WVAL). A page goes out only when every receiver granted it (per-receiver credits W_CRED + y).
// receiver : grants ring slots as its TRISC frees them and pushes the pages the feeder flagged valid.
#include "me_dm.hpp"
using namespace mex;
namespace {
constexpr auto acc_dram = TensorAccessorArgs<CT_ACC0>();
#ifndef ME_RING_DEPTH
#define ME_RING_DEPTH 4
#endif
constexpr uint32_t DEPTH = ME_RING_DEPTH;
constexpr uint32_t SLOTS = RING;
FORCE_INLINE uint32_t freed_pages(uint32_t pushed) {
const uint32_t out = ring_outstanding_tiles();
return pushed - (out + PAGE_TILES - 1) / PAGE_TILES;
}
void receiver(uint32_t total) {
const uint32_t y = rt(A_Y);
const uint64_t cred = get_noc_addr(rt(A_FX), rt(A_FY), sync_addr(W_CRED + y));
uint32_t granted = 0, pushed = 0;
while (pushed < total) {
WAYPOINT("NRCV");
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;
}
}
noc_async_atomic_barrier();
}
// segment s -> (arena address, first page of this column, pages): W embed segments, then W per block
struct Seg {
uint32_t addr, p0, np;
};
FORCE_INLINE Seg seg_of(uint32_t s) {
if (s < NW) {
return {ca(K_ARENA_E), rt(A_P0E), rt(A_NPE)};
}
const uint32_t l = (s - NW) / NW;
if (l == NL - 1) {
return {ca(K_ARENA0 + l), rt(A_P0BL), rt(A_NPBL)};
}
return {ca(K_ARENA0 + l), rt(A_P0B), rt(A_NPB)};
}
void feeder(uint32_t total) {
const uint32_t fx = rt(A_FX), y0 = rt(A_COLY0), y1 = rt(A_COLY1);
constexpr uint32_t nrecv = R - 1;
const uint32_t base = cb_base(CB_W);
const uint32_t wval = sync_addr(W_WVAL);
uint32_t rd_issued = 0, rd_done = 0, mc_done = 0, mc_flushed = 0;
uint32_t s = 0, lp = 0;
Seg seg = seg_of(0);
while (mc_done < total || rd_done < total) {
WAYPOINT("NFED");
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 (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) {
noc_async_writes_flushed();
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));
noc_async_write_multicast(a, mcast_addr(fx, y0, fx, y1, a), PAGE_BYTES, nrecv, true);
*reinterpret_cast<volatile tt_l1_ptr uint32_t*>(src) = mc_done + 1;
noc_semaphore_set_multicast(src, mcast_addr(fx, y0, fx, y1, wval), nrecv);
++mc_done;
}
}
if (rd_issued < total && 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) {
noc_async_writes_flushed();
mc_flushed = mc_done;
}
}
if (ok) {
noc_async_read_set_trid(1 + (rd_issued % DEPTH));
const auto acc = TensorAccessor(acc_dram, seg.addr, PAGE_BYTES);
noc_async_read_page(seg.p0 + lp, acc, base + (rd_issued % SLOTS) * PAGE_BYTES);
++rd_issued;
if (++lp == seg.np) {
lp = 0;
++s;
if (rd_issued < total) {
seg = seg_of(s);
}
}
}
}
}
noc_async_read_set_trid(0);
noc_async_write_barrier();
}
} // namespace
void kernel_main() {
const uint32_t role = rt(A_ROLE);
wait_go(get_semaphore(1)); // SEM_GO: the BRISCs zeroed every sync word
const uint32_t nl = ca(K_NL);
uint32_t total = NW * rt(A_NPE);
for (uint32_t l = 0; l < nl; ++l) {
total += NW * (l == NL - 1 ? rt(A_NPBL) : rt(A_NPB));
}
if (role & R_FEED) {
feeder(total);
} else {
receiver(total);
}
}