File size: 9,060 Bytes
b7e49b8 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 | // 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);
}
|