changh95's picture
tt-model push bevfusion-p150 (container)
5528e28 verified
Raw History Blame Contribute Delete
14.3 kB
// SPDX-License-Identifier: Apache-2.0
// M0 device rulebook (tt/rulebook_kernel.py, OPT_PLAN M0): one neighbour map of the sparse encoder as the C13
// tile-ordered im2col index that K6 reads, built from the levels' sorted packed keys (key = x << 17 | y << 6 | z,
// ascending; rows past a level's count hold 0xFFFFFFFF). The same source runs on both data-movement RISCs of every
// core; each is one worker `wid` of NW.
//
// Output row o (key X, Y, Z) at tap (a, b, d): the input row whose key is
// (X << SHX) - PX + a, (Y << SHY) - PY + b, (Z << SHZ) - PZ + d (inside the input grid), else SENT.
// Rows of one x tap `a` are visited in ascending key, so their targets ascend: one forward-only pointer P[a] per x
// tap, moved to the x row's start (`xstart`, from the frame's meta) when the row's x changes and advanced to the first
// key >= the row's first target column; the KY * KZ targets are then found by a forward scan from P[a] (at most KY
// columns of ZIN keys). The input keys stream through a CW-key L1 cache per x tap (refilled at P[a] when a key past
// it is needed; the input keys tensor carries CW keys of 0xFFFFFFFF padding past the capacity, so a refill never
// reads past it).
//
// Work split: the frame's live output blocks (ceil(count / 32), count from the meta header) in NW contiguous ranges,
// then the dead blocks (all SENT) in NW ranges. Each block: 32 keys -> KV x 32 indices (tap-major) -> one page of
// the index tensor. Each worker writes its hit count to page MAP_ID * NW + wid of the pairs tensor (64 B pages).
//
// CT args: [0] per-core RT-arg count (P3) [1..3] KX KY KZ [4..6] SHX SHY SHZ [7..9] PX PY PZ [10..12] XIN YIN ZIN
// [13] SENT [14] NBLK [15] NW [16] CNT_OFF (meta word of the output count) [17] XS_OFF (meta word of the input
// level's xstart) [18] CW [19] MAP_ID [20] CB (scratch) [21] META_BYTES, then the TensorAccessorArgs of the input
// keys, output keys, meta, index, pairs.
// Common RT args: [kin, kout, meta, index, pairs addresses]. Per-core RT args: [wid].
#include <cstdint>
#include "api/dataflow/dataflow_api.h"
constexpr uint32_t KX = get_compile_time_arg_val(1);
constexpr uint32_t KY = get_compile_time_arg_val(2);
constexpr uint32_t KZ = get_compile_time_arg_val(3);
constexpr uint32_t SHX = get_compile_time_arg_val(4);
constexpr uint32_t SHY = get_compile_time_arg_val(5);
constexpr uint32_t SHZ = get_compile_time_arg_val(6);
constexpr int32_t PX = get_compile_time_arg_val(7);
constexpr int32_t PY = get_compile_time_arg_val(8);
constexpr int32_t PZ = get_compile_time_arg_val(9);
constexpr int32_t XIN = get_compile_time_arg_val(10);
constexpr int32_t YIN = get_compile_time_arg_val(11);
constexpr int32_t ZIN = get_compile_time_arg_val(12);
constexpr uint32_t SENT = get_compile_time_arg_val(13);
constexpr uint32_t NBLK = get_compile_time_arg_val(14);
constexpr uint32_t NW = get_compile_time_arg_val(15);
constexpr uint32_t CNT_OFF = get_compile_time_arg_val(16);
constexpr uint32_t XS_OFF = get_compile_time_arg_val(17);
constexpr uint32_t CW = get_compile_time_arg_val(18);
constexpr uint32_t MAP_ID = get_compile_time_arg_val(19);
constexpr uint32_t CB_ID = get_compile_time_arg_val(20);
constexpr uint32_t META_BYTES = get_compile_time_arg_val(21);
constexpr auto kin_args = TensorAccessorArgs<22>();
constexpr auto kout_args = TensorAccessorArgs<kin_args.next_compile_time_args_offset()>();
constexpr auto meta_args = TensorAccessorArgs<kout_args.next_compile_time_args_offset()>();
constexpr auto idx_args = TensorAccessorArgs<meta_args.next_compile_time_args_offset()>();
constexpr auto pr_args = TensorAccessorArgs<idx_args.next_compile_time_args_offset()>();
constexpr uint32_t KV = KX * KY * KZ;
constexpr uint32_t IDXB = KV * 128;
constexpr uint32_t XSB = (((XIN + 1) * 4 + 63) / 64) * 64;
constexpr uint32_t CWB = CW * 4;
// scratch layout (bytes; tt/rulebook_kernel.scratch_bytes mirrors it)
constexpr uint32_t O_HDR = 0;
constexpr uint32_t O_XS = 64;
constexpr uint32_t O_CACHE = O_XS + XSB;
constexpr uint32_t O_OKEY = O_CACHE + KX * CWB;
constexpr uint32_t O_OBUF = O_OKEY + 256;
constexpr uint32_t O_SENT = O_OBUF + 2 * IDXB;
constexpr uint32_t O_PAIR = O_SENT + IDXB;
constexpr uint32_t RB_INVALID = 0xFFFFFFFFu;
constexpr uint32_t SPAN = KY * ZIN + KZ + 1; // keys a row's merge for one x tap may touch past P
static_assert(SPAN + 32 <= CW, "cache too small for the merge span");
void kernel_main() {
const uint32_t wid = get_arg_val<uint32_t>(0);
const auto kin = TensorAccessor(kin_args, get_common_arg_val<uint32_t>(0), 128);
const auto kout = TensorAccessor(kout_args, get_common_arg_val<uint32_t>(1), 128);
const auto meta = TensorAccessor(meta_args, get_common_arg_val<uint32_t>(2), META_BYTES);
const auto idx = TensorAccessor(idx_args, get_common_arg_val<uint32_t>(3), IDXB);
const auto prs = TensorAccessor(pr_args, get_common_arg_val<uint32_t>(4), 64);
const uint32_t base = get_write_ptr(CB_ID);
#ifdef RB_PROF
const uint32_t t_start = reg_read(RISCV_DEBUG_REG_WALL_CLOCK_L);
uint32_t tl = 0, tc = 0, tw = 0, nl = 0;
#endif
noc_async_read(meta.get_noc_addr(0, 0), base + O_HDR, 64);
noc_async_read(meta.get_noc_addr(0, XS_OFF * 4), base + O_XS, XSB);
uint32_t* sb = reinterpret_cast<uint32_t*>(base + O_SENT);
for (uint32_t i = 0; i < KV * 32; ++i) {
sb[i] = SENT;
}
noc_async_read_barrier();
invalidate_l1_cache();
const uint32_t* hdr = reinterpret_cast<const uint32_t*>(base + O_HDR);
const uint32_t* xsv = reinterpret_cast<const uint32_t*>(base + O_XS);
const uint32_t cnt = hdr[CNT_OFF];
uint32_t nlb = (cnt + 31) >> 5;
if (nlb > NBLK) {
nlb = NBLK;
}
const uint32_t lb0 = wid * nlb / NW, lb1 = (wid + 1) * nlb / NW;
const uint32_t nd = NBLK - nlb;
const uint32_t db0 = nlb + wid * nd / NW, db1 = nlb + (wid + 1) * nd / NW;
uint32_t c0[KX];
int32_t xc[KX];
uint32_t P[KX];
for (uint32_t a = 0; a < KX; ++a) {
c0[a] = 0x80000000u; // empty: every key index p < 2^28 gives p - c0 >= CW
xc[a] = -1;
P[a] = 0;
}
// refill x tap a's cache at `at` (a multiple of 32); returns the new cache base
auto load = [&](uint32_t a, uint32_t at) __attribute__((noinline)) -> uint32_t {
const uint32_t dst = base + O_CACHE + a * CWB;
const uint32_t pg = at >> 5;
for (uint32_t i = 0; i < CW / 32; ++i) {
noc_async_read(kin.get_noc_addr(pg + i), dst + i * 128, 128);
}
#ifdef RB_PROF
uint32_t t0 = reg_read(RISCV_DEBUG_REG_WALL_CLOCK_L);
#endif
noc_async_read_barrier();
#ifdef RB_PROF
tl += reg_read(RISCV_DEBUG_REG_WALL_CLOCK_L) - t0; ++nl;
#endif
invalidate_l1_cache();
return at;
};
uint32_t hits = 0;
uint32_t cur = 0;
const uint32_t okey[2] = {base + O_OKEY, base + O_OKEY + 128};
const uint32_t obuf[2] = {base + O_OBUF, base + O_OBUF + IDXB};
if (lb0 < lb1) {
noc_async_read(kout.get_noc_addr(lb0), okey[0], 128);
}
for (uint32_t b = lb0; b < lb1; ++b) {
noc_async_read_barrier();
invalidate_l1_cache();
const uint32_t sel = (b - lb0) & 1;
if (b + 1 < lb1) {
noc_async_read(kout.get_noc_addr(b + 1), okey[sel ^ 1], 128);
}
const uint32_t* ok = reinterpret_cast<const uint32_t*>(okey[sel]);
#ifdef RB_PROF
uint32_t tw0 = reg_read(RISCV_DEBUG_REG_WALL_CLOCK_L);
#endif
noc_async_writes_flushed(); // obuf[cur] was the source of the write two blocks ago
#ifdef RB_PROF
tw += reg_read(RISCV_DEBUG_REG_WALL_CLOCK_L) - tw0; uint32_t tc0 = reg_read(RISCV_DEBUG_REG_WALL_CLOCK_L); uint32_t tl0 = tl;
#endif
uint32_t* ob = reinterpret_cast<uint32_t*>(obuf[cur]);
#ifndef RB_NOCOMPUTE
// one x tap at a time over the block's 32 rows: the tap's walk state lives in registers
uint32_t bhits = 0;
for (uint32_t a = 0; a < KX; ++a) {
uint32_t sP = P[a], sc0 = c0[a];
int32_t sxc = xc[a];
const uint32_t* cache = reinterpret_cast<const uint32_t*>(base + O_CACHE + a * CWB);
uint32_t* oa = ob + a * KY * KZ * 32;
for (uint32_t v = 0; v < 32; ++v) {
uint32_t* ca = oa + v;
const uint32_t o = ok[v];
const int32_t X = static_cast<int32_t>(o >> 17);
const int32_t Y = static_cast<int32_t>((o >> 6) & 0x7FF);
const int32_t Z = static_cast<int32_t>(o & 63);
const int32_t xp = (X << SHX) - PX + static_cast<int32_t>(a);
const int32_t y0 = (Y << SHY) - PY;
const int32_t bf = y0 < 0 ? -y0 : 0;
const int32_t z0 = (Z << SHZ) - PZ;
// the KZ z taps inside the input grid: [dlo, dhi)
const uint32_t dlo = z0 < 0 ? static_cast<uint32_t>(-z0) : 0;
const int32_t dh = ZIN - z0;
const uint32_t dhi = dh < static_cast<int32_t>(KZ) ? (dh < 0 ? 0 : static_cast<uint32_t>(dh)) : KZ;
if (o == RB_INVALID || xp < 0 || xp >= XIN || bf >= static_cast<int32_t>(KY) || y0 + bf >= YIN ||
dlo >= dhi) {
for (uint32_t t = 0; t < KY * KZ; ++t) {
ca[t * 32] = SENT;
}
continue;
}
if (xp != sxc) {
sxc = xp;
const uint32_t xs = xsv[xp];
if (xs > sP) {
sP = xs;
}
}
const uint32_t xk = static_cast<uint32_t>(xp) << 17;
const uint32_t colbase = xk | (static_cast<uint32_t>(y0 + bf) << 6);
while (true) {
if (sP - sc0 >= CW) {
sc0 = load(a, sP & ~31u);
}
if (cache[sP - sc0] >= colbase) {
break;
}
++sP;
}
// every key the targets' merge can touch lies in [sP, sP + SPAN): one refill at sP covers it
if (sP - sc0 + SPAN > CW) {
sc0 = load(a, sP & ~31u);
}
uint32_t qi = sP - sc0;
for (uint32_t bb = 0; bb < KY; ++bb) {
uint32_t* cd = ca + bb * KZ * 32;
const int32_t yp = y0 + static_cast<int32_t>(bb);
if (yp < 0 || yp >= YIN) {
for (uint32_t d = 0; d < KZ; ++d) {
cd[d * 32] = SENT;
}
continue;
}
for (uint32_t d = 0; d < dlo; ++d) {
cd[d * 32] = SENT;
}
uint32_t tk = xk | (static_cast<uint32_t>(yp) << 6) | static_cast<uint32_t>(z0 + static_cast<int32_t>(dlo));
while (cache[qi] < tk) {
++qi;
}
// sorted unique keys: a merge of the column's targets tk, tk + 1, ... with the keys from qi
if (dlo == 0 && dhi == KZ) { // all z taps inside the grid: branch-free
if constexpr (KZ == 3) {
// the three candidate keys loaded up front (no load waits on the previous compare)
const uint32_t* cp = cache + qi;
const uint32_t k0 = cp[0], k1 = cp[1], k2 = cp[2];
const uint32_t h0 = k0 == tk ? 1u : 0u;
const uint32_t c1 = h0 ? k1 : k0;
const uint32_t h1 = c1 == tk + 1 ? 1u : 0u;
const uint32_t n2 = h0 + h1;
const uint32_t c2 = n2 == 0 ? k0 : (n2 == 1 ? k1 : k2);
const uint32_t h2 = c2 == tk + 2 ? 1u : 0u;
const uint32_t r0 = sc0 + qi;
cd[0] = h0 ? r0 : SENT;
cd[32] = h1 ? r0 + h0 : SENT;
cd[64] = h2 ? r0 + n2 : SENT;
qi += n2 + h2;
bhits += n2 + h2;
} else {
#pragma GCC unroll 8
for (uint32_t d = 0; d < KZ; ++d) {
const uint32_t h = cache[qi] == tk + d ? 1u : 0u;
cd[d * 32] = h ? sc0 + qi : SENT;
qi += h;
bhits += h;
}
}
continue;
}
for (uint32_t d = dlo; d < dhi; ++d, ++tk) {
uint32_t r = SENT;
if (cache[qi] == tk) {
r = sc0 + qi;
++qi;
++bhits;
}
cd[d * 32] = r;
}
for (uint32_t d = dhi; d < KZ; ++d) {
cd[d * 32] = SENT;
}
}
}
P[a] = sP;
c0[a] = sc0;
xc[a] = sxc;
}
hits += bhits;
#ifdef RB_PROF
tc += reg_read(RISCV_DEBUG_REG_WALL_CLOCK_L) - tc0 - (tl - tl0);
#endif
#else
for (uint32_t t = 0; t < KV * 32; ++t) {
ob[t] = SENT;
}
#endif
#ifndef RB_NOWRITE
noc_async_write(obuf[cur], idx.get_noc_addr(b), IDXB);
#endif
cur ^= 1;
}
for (uint32_t b = db0; b < db1; ++b) {
#ifndef RB_NOWRITE
noc_async_write(base + O_SENT, idx.get_noc_addr(b), IDXB);
#endif
}
noc_async_writes_flushed();
uint32_t* pp = reinterpret_cast<uint32_t*>(base + O_PAIR);
pp[0] = hits;
for (uint32_t i = 1; i < 16; ++i) {
pp[i] = 0;
}
#ifdef RB_PROF
pp[1] = tl; pp[2] = tc; pp[3] = tw; pp[4] = reg_read(RISCV_DEBUG_REG_WALL_CLOCK_L) - t_start; pp[5] = nl;
#endif
noc_async_write(base + O_PAIR, prs.get_noc_addr(MAP_ID * NW + wid), 64);
noc_async_write_barrier();
}