File size: 15,918 Bytes
5528e28 | 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 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 | // SPDX-License-Identifier: Apache-2.0
// M0 / M0b device rulebook, v7 (tt/rulebook_kernel.py, OPT_PLAN M0b; v5 = rb_map.cpp). v7 = v5 plus, per x tap,
// the start of each of its KY target columns kept for the rows of one output (x, y) column: a later row of the same
// output column resumes every target column there (its z targets ascend) instead of re-walking from the x tap's
// pointer, so the keys of a dense column are skipped once per column group, not once per row. Same maps.
// Based on v5: 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];
uint32_t Q[KX * KY]; // v7: per x tap and target column, the absolute key index to resume at
uint32_t G[KX]; // v7: per x tap, the output (x, y) column (key >> 6) Q belongs to (RB_INVALID: none)
for (uint32_t a = 0; a < KX; ++a) {
G[a] = RB_INVALID;
for (uint32_t bb = 0; bb < KY; ++bb) {
Q[a * KY + bb] = 0;
}
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];
uint32_t gcol = G[a];
uint32_t qb[KY];
#pragma GCC unroll 4
for (uint32_t bb = 0; bb < KY; ++bb) {
qb[bb] = Q[a * KY + bb];
}
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;
}
gcol = RB_INVALID;
continue;
}
const uint32_t col = o >> 6;
const bool fresh = col != gcol;
gcol = col;
const uint32_t xk = static_cast<uint32_t>(xp) << 17;
if (fresh) {
if (xp != sxc) {
sxc = xp;
const uint32_t xs = xsv[xp];
if (xs > sP) {
sP = xs;
}
}
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 group's merges 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;
#pragma GCC unroll 4
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));
if (!fresh) {
qi = qb[bb] - sc0; // a later row of the group: resume the target column
}
while (cache[qi] < tk) {
++qi;
}
qb[bb] = sc0 + qi; // the group's next row targets keys >= tk
// 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;
G[a] = gcol;
#pragma GCC unroll 4
for (uint32_t bb = 0; bb < KY; ++bb) {
Q[a * KY + bb] = qb[bb];
}
}
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();
}
|