File size: 25,850 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 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 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 | // SPDX-License-Identifier: Apache-2.0
//
// DepthPro2 DEC megakernel, TRISC: all arithmetic.
// tilize : ROW_MAJOR runs of the unit's L1 band -> bf16 A tiles (straight from the band at any 16 B aligned
// address: the tilize unpacker gets the run's address, its row pitch is the band's pixel pitch x stride;
// input ReLU = packer ReLU of this pack); residual maps -> the fp32 accumulators (L1 accumulation)
// matmul : per output tile, the k-block's taps x channel tiles (+ the bias tile through the ones operand E01 in
// k-block 0; HC: the per-pixel-class bias through the selector tile), fp32 DST (half sync, 4 tiles),
// packed into the fp32 accumulators (L1 accumulation from the second contribution on), or (UP / RA with
// one k-block) pack-untilized straight into the ROW_MAJOR output block
// epilog : C3: accumulators -> (packer ReLU) -> pack-untilize -> CB_OUT; HC: relu(acc) -> bf16 Z (4 sub-pixel
// tiles) -> R = sum_s w4_s Z_s^T + b4 (transposed matmuls, rows = sub-pixels) -> ReLU -> fp32 CB_HO
// Every CB with layer-dependent use is cycled at full capacity (pointers stay at the base); the weight ring is
// consumed in blocks that never wrap (skip pages, md_plan.hpp ring_skip, mirrored by the NCRISC).
#include <cstdint>
#include "api/compute/compute_kernel_api.h"
#include "api/compute/matmul.h"
#include "api/compute/tilize.h"
#include "api/compute/pack_untilize.h"
#include "api/compute/tile_move_copy.h"
#include "api/compute/pack.h"
#include "api/compute/reconfig_data_format.h"
#include "api/compute/compute_kernel_hw_startup.h"
#include "stream_io_map.h"
#include "dev_mem_map.h"
#include "hostdev/dev_msgs.h"
#include "md_plan.hpp"
#define NOINL __attribute__((noinline, noclone))
using namespace mdx;
namespace {
#ifdef MD_FID
constexpr MathFidelity FID = static_cast<MathFidelity>(MD_FID);
#else
constexpr MathFidelity FID = MathFidelity::HiFi4;
#endif
constexpr uint32_t A_CAP = MD_A_CAP;
constexpr uint32_t ACC_CAP = MD_ACC_CAP;
constexpr uint32_t TAPA_CAP = MD_TAPA_CAP;
constexpr uint32_t SEL_CAP = MD_SEL_CAP;
constexpr uint32_t OUT_BLK = 8;
constexpr uint32_t Z_CAP = 16;
Plan pl;
uint32_t ring_pos = 0, RING = 0;
uint32_t g_abort = 0, g_spin = 1u << 26;
// L1 byte address of a CB's buffer from this program's CB config (the MATH thread has no CB interfaces)
NOINL uint32_t cb_addr(uint32_t cb) {
tt_l1_ptr mailboxes_t* const mb = (tt_l1_ptr mailboxes_t*)(MEM_MAILBOX_BASE);
const tt_l1_ptr launch_msg_t* lm = &(mb->launch[mb->launch_msg_rd_ptr]);
const uint32_t base = lm->kernel_config.kernel_config_base[ProgrammableCoreType::TENSIX];
const volatile tt_l1_ptr uint32_t* cfg =
reinterpret_cast<const volatile tt_l1_ptr uint32_t*>(base + lm->kernel_config.local_cb_offset);
return cfg[cb * 4]; // [fifo_addr, fifo_size, num_pages, page_size] in bytes
}
// ---------------------------------------------------------------- bounded waits (hang protection, md_dm.hpp)
uint32_t g_sync = 0;
ALWI volatile uint32_t* dbg_ptr(uint32_t w) { return reinterpret_cast<volatile uint32_t*>(g_sync + w * SYNC_STRIDE); }
NOINL void note(uint32_t word, uint32_t code, uint32_t arg) {
if (!g_abort) {
g_abort = code;
*dbg_ptr(word) = code;
*dbg_ptr(word + 1) = arg;
*dbg_ptr(W_ABORT) = ABORT_MAGIC; // releases the data-movement RISCs of this core
}
}
// another RISC of this core aborted (polled every 4096 spins)
ALWI bool peer_abort(uint32_t it, uint32_t word) {
if ((it & 4095) == 0 && *dbg_ptr(W_ABORT) == ABORT_MAGIC) {
note(word, AB_PEER, 0);
return true;
}
return false;
}
// cb_wait_front (UNPACK)
NOINL void cbw(uint32_t cb, uint32_t n) {
UNPACK(({
if (!g_abort) {
volatile uint32_t* rp = get_cb_tiles_received_ptr(cb);
uint32_t it = 0;
while (true) {
const uint16_t have = (uint16_t)reg_read((uint32_t)rp) - (uint16_t)get_local_cb_interface(cb).tiles_acked;
if (have >= (uint16_t)n) {
break;
}
if (++it > g_spin) {
note(W_DBG + 4, AB_FRONT, cb);
break;
}
if (peer_abort(it, W_DBG + 4)) {
break;
}
}
}
}));
}
// cb_reserve_back (PACK)
NOINL void cbr(uint32_t cb, uint32_t n) {
PACK(({
if (!g_abort) {
volatile uint32_t* ap = get_cb_tiles_acked_ptr(cb);
const uint16_t rcv = (uint16_t)get_local_cb_interface(cb).tiles_received;
uint32_t it = 0;
while (true) {
const uint16_t acked = (uint16_t)reg_read((uint32_t)ap);
const uint16_t fr = (uint16_t)get_local_cb_interface(cb).fifo_num_pages - (uint16_t)(rcv - acked);
if ((int32_t)fr >= (int32_t)n) {
break;
}
if (++it > g_spin) {
note(W_DBG + 6, AB_BACK, cb);
break;
}
if (peer_abort(it, W_DBG + 6)) {
break;
}
}
}
}));
}
// ---------------------------------------------------------------- profiling (MD_PROF builds, UNPACK thread)
#if MD_PROF
uint32_t pf_l = 0;
uint64_t pf_t = 0;
NOINL void pf_start(uint32_t l) {
UNPACK(({
pf_l = l;
pf_t = ckernel::read_wall_clock();
}));
}
// attribute the time since the last mark to slot (2 w_wait, 3 in_wait, 4 tilize, 5 mm, 6 epilogue)
NOINL void pf_mark(uint32_t slot) {
UNPACK(({
const uint64_t now = ckernel::read_wall_clock();
if (pf_l < 32) {
volatile uint32_t* p = reinterpret_cast<volatile uint32_t*>(g_sync + SYNC_PROF);
p[8 * pf_l + slot] += (uint32_t)(now - pf_t);
}
pf_t = now;
}));
}
#else
ALWI void pf_start(uint32_t) {}
ALWI void pf_mark(uint32_t) {}
#endif
// ---------------------------------------------------------------- plan (all three threads)
NOINL void wait_plan_all() {
volatile uint32_t* p = get_cb_tiles_received_ptr(CB_PLAN);
uint32_t it = 0;
while ((p[0] & 0xFFFFu) == 0) {
if (++it > g_spin) {
break;
}
}
asm volatile("" ::: "memory");
pl.t = reinterpret_cast<const uint32_t*>(cb_addr(CB_PLAN));
pl.nm = pl.t[H_NM];
}
// ---------------------------------------------------------------- LLK wrappers
ALWI void mm_init_f(uint32_t in0, uint32_t in1, uint32_t transpose = 0) {
reconfig_data_format(in1, in0); // matmul: SrcA <- in1, SrcB <- in0
MATH((llk_math_matmul_init<FID, MM_THROTTLE>(in0, in1, transpose)));
UNPACK((llk_unpack_AB_matmul_init(in0, in1, transpose)));
}
ALWI void mm_f(uint32_t in0, uint32_t in1, uint32_t t0, uint32_t t1, uint32_t dst) {
UNPACK((llk_unpack_AB_matmul(in0, in1, t0, t1)));
MATH((llk_math_matmul<FID, MM_THROTTLE>(dst)));
}
NOINL void mm_init_n(uint32_t in0, uint32_t in1, uint32_t transpose) { mm_init_f(in0, in1, transpose); }
NOINL void pk(uint32_t ocb, uint32_t l1acc) {
pack_reconfig_data_format(ocb);
PACK((llk_pack_reconfig_l1_acc(l1acc)));
}
NOINL void relu_on() { PACK((llk_pack_relu_config(ReluConfig::zero()))); }
NOINL void relu_off() { PACK((llk_pack_relu_config(ReluConfig::none()))); }
// tilize (BH: the regular tilize unpack only handles 1-tile-wide rows, so rows wider than one tile go through the
// fast-tilize path, like DA3): the run's L1 address is set as the input CB's read pointer, the row pitch (in 32-datum
// column tiles) is given at init; 2-4 tile chunks. One tile out of a wider row is tilized as a 2-tile chunk whose
// second tile lands in the next output slot (rewritten by the next run, or the spare slot at the end).
uint32_t ft_saved = 0;
NOINL void ft_begin(uint32_t icb, uint32_t pitch, uint32_t ocb, bool relu) {
UNPACK((ft_saved = get_local_cb_interface(icb).fifo_rd_ptr));
reconfig_data_format_srca(icb);
pk(ocb, 0);
if (relu) {
relu_on();
}
fast_tilize_init(icb, pitch, ocb);
}
NOINL void ft_end(uint32_t icb, uint32_t ocb, uint32_t pitch, bool relu) {
fast_tilize_uninit(icb, ocb, pitch);
if (relu) {
relu_off();
}
UNPACK((get_local_cb_interface(icb).fifo_rd_ptr = ft_saved));
}
// nt channel tiles of the 32-row run starting at L1 address (src16 << 4) -> ocb[oidx ..]
NOINL void ft_run(uint32_t icb, uint32_t src16, uint32_t nt, uint32_t pitch, uint32_t ocb, uint32_t oidx) {
UNPACK((get_local_cb_interface(icb).fifo_rd_ptr = src16));
fast_tilize_block(icb, (pitch > 1 && nt < 2) ? 2 : nt, ocb, 0, oidx);
}
// L1 byte address >> 4 of a CB's read pointer (UNPACK thread; 0 elsewhere)
ALWI uint32_t rd16(uint32_t cb) {
uint32_t a = 0;
UNPACK((a = get_local_cb_interface(cb).fifo_rd_ptr));
return a;
}
// ---------------------------------------------------------------- weight ring
NOINL void w_wait(uint32_t np) {
pf_mark(5);
const uint32_t skip = ring_skip(ring_pos, np, RING);
if (skip) {
cbw(CB_W, skip * PAGE_TILES);
cb_pop_front(CB_W, skip * PAGE_TILES);
ring_pos += skip;
}
cbw(CB_W, np * PAGE_TILES);
pf_mark(2);
}
NOINL void w_pop(uint32_t np) {
cb_pop_front(CB_W, np * PAGE_TILES);
ring_pos += np;
}
// ---------------------------------------------------------------- layer context
struct LC {
uint32_t l, op, flags, s, ct, kc, nkb, n, nbw, nbi, nres, nbias, bandp, pxb, u, boff;
};
NOINL void lc_of(uint32_t l, LC& c) {
c.l = l;
c.op = pl.lf(l, L_OP);
c.flags = pl.lf(l, L_FLAGS);
c.s = pl.lf(l, L_S);
c.ct = pl.lf(l, L_CT);
c.kc = pl.lf(l, L_KC);
c.nkb = pl.lf(l, L_NKB);
c.n = pl.lf(l, L_N);
c.nbw = pl.lf(l, L_NBW);
c.nbi = pl.lf(l, L_NBI);
c.nres = pl.lf(l, L_NRES);
c.nbias = pl.lf(l, L_NBIAS);
c.bandp = pl.lf(l, L_BANDP);
c.pxb = c.ct * 64; // bytes per pixel of the input map (C3 / UP / HC)
c.u = pl.lf(l, L_U);
c.boff = pl.lf(l, L_BOFF); // C3 / HC: band row data (x = -1) behind the left edge chunk slack
}
// ---------------------------------------------------------------- epilogues / 1x1 passes (templated pack-untilize)
template <uint32_t NS, uint32_t NBW>
NOINL void epi_untilize(uint32_t mt, bool relu) {
cbw(CB_ACC, ACC_CAP);
reconfig_data_format_srca(CB_ACC);
copy_init(CB_ACC);
PACK((llk_pack_reconfig_l1_acc(0)));
pack_untilize_dest_init<NS, NBW>(CB_OUT);
if (relu) {
relu_on();
}
for (uint32_t m = 0; m < mt; ++m) {
cbr(CB_OUT, OUT_BLK);
for (uint32_t b = 0; b < NBW / NS; ++b) {
tile_regs_acquire();
for (uint32_t i = 0; i < NS; ++i) {
copy_tile(CB_ACC, m * NBW + b * NS + i, i);
}
tile_regs_commit();
tile_regs_wait();
pack_untilize_dest<NS, NBW>(CB_OUT, 1, b);
tile_regs_release();
}
cb_push_back(CB_OUT, OUT_BLK);
}
if (relu) {
relu_off();
}
pack_untilize_uninit(CB_OUT);
cb_pop_front(CB_ACC, ACC_CAP);
}
NOINL void epi_untilize_n(uint32_t nbw, uint32_t mt, bool relu) {
switch (nbw) {
case 1: epi_untilize<1, 1>(mt, relu); break;
case 2: epi_untilize<2, 2>(mt, relu); break;
case 4: epi_untilize<4, 4>(mt, relu); break;
default: epi_untilize<4, 8>(mt, relu); break;
}
}
// per-pixel matmul (UP / RA), output tile (m, n) = E01 bias(n) [nbias] + sum_cc A[m * astride + a0 + cc] W(cc, n),
// packed into the fp32 accumulators (l1acc from the second k-block on); epi_untilize writes CB_OUT
NOINL void mm1x1_acc(uint32_t in0, uint32_t mt, uint32_t astride, uint32_t a0, uint32_t kc, uint32_t nbias,
uint32_t nbw, uint32_t l1acc) {
const uint32_t ns = nbw < 4 ? nbw : 4;
mm_init_f(in0, CB_W);
pk(CB_ACC, l1acc);
for (uint32_t m = 0; m < mt; ++m) {
for (uint32_t n0 = 0; n0 < nbw; n0 += ns) {
tile_regs_acquire();
for (uint32_t i = 0; i < ns; ++i) {
const uint32_t n = n0 + i;
if (nbias) {
mm_f(CB_CONST, CB_W, C_E01, n, i);
}
for (uint32_t cc = 0; cc < kc; ++cc) {
mm_f(in0, CB_W, m * astride + a0 + cc, nbias + cc * nbw + n, i);
}
}
tile_regs_commit();
tile_regs_wait();
for (uint32_t i = 0; i < ns; ++i) {
pack_tile<true>(i, CB_ACC, m * nbw + n0 + i);
}
tile_regs_release();
}
}
}
// ---------------------------------------------------------------- C3 / HC
ALWI uint32_t aidx(uint32_t dxi, uint32_t j, uint32_t kx, uint32_t cc, uint32_t rows, uint32_t bw, uint32_t kc) {
return ((dxi * rows + j) * bw + kx) * kc + cc;
}
// residual map -> accumulators (first overwrites, second accumulates): the unit's tiles (r, kx, n) of the CB_RES band
// are tilized (bf16, CB_A as scratch, chunks of whole M tiles) and copied through DST into the fp32 CB_ACC
NOINL void res_add(const LC& c, const Unit& un, uint32_t ri, uint32_t bwmax) {
cbw(CB_RES, 1);
const uint32_t r16 = rd16(CB_RES);
const uint32_t n = c.n, mt = un.bh * un.bw;
const uint32_t pitch16 = 32 * bwmax * n * 4; // 32 BW pixels x n tiles x 64 B, in 16 B
const uint32_t per = (A_CAP - 1) / n; // M tiles per chunk
for (uint32_t m0 = 0; m0 < mt; m0 += per) {
const uint32_t m1 = m0 + per < mt ? m0 + per : mt;
ft_begin(CB_RES, n, CB_A, false);
cbr(CB_A, A_CAP);
for (uint32_t m = m0; m < m1; ++m) {
const uint32_t r = m / un.bw, kx = m - r * un.bw;
ft_run(CB_RES, r16 + r * pitch16 + kx * 32 * n * 4, n, n, CB_A, (m - m0) * n);
}
cb_push_back(CB_A, A_CAP);
ft_end(CB_RES, CB_A, n, false);
cbw(CB_A, A_CAP);
reconfig_data_format_srca(CB_A);
copy_init(CB_A);
pk(CB_ACC, ri > 0 ? 1 : 0);
for (uint32_t m = m0; m < m1; ++m) {
for (uint32_t n0 = 0; n0 < n; n0 += 4) {
const uint32_t ns = n - n0 < 4 ? n - n0 : 4;
tile_regs_acquire();
for (uint32_t i = 0; i < ns; ++i) {
copy_tile(CB_A, (m - m0) * n + n0 + i, i);
}
tile_regs_commit();
tile_regs_wait();
for (uint32_t i = 0; i < ns; ++i) {
pack_tile<true>(i, CB_ACC, m * c.nbw + n0 + i);
}
tile_regs_release();
}
}
cb_pop_front(CB_A, A_CAP);
}
cb_pop_front(CB_RES, 1);
}
// k-block kb of the band -> A tiles: version (dx + 1, band row j, tile kx, channel tile cc)
NOINL void c3_tilize(const LC& c, const Unit& un, uint32_t kb, uint32_t b16, uint32_t ntz) {
const uint32_t rows = c.s * (un.bh - 1) + 3;
const bool relu = (c.flags & F_RELU_IN) != 0;
const uint32_t pitch = c.s * c.ct;
ft_begin(CB_BAND, pitch, CB_A, relu);
cbr(CB_A, A_CAP);
const uint32_t px16 = c.pxb >> 4;
for (uint32_t dxi = 0; dxi < 3; ++dxi) {
for (uint32_t j = 0; j < rows; ++j) {
for (uint32_t kx = 0; kx < un.bw; ++kx) {
const uint32_t src16 = b16 + j * (c.bandp >> 4) + (c.s * 32 * kx + dxi) * px16 + kb * c.kc * 4;
ft_run(CB_BAND, src16, ntz, pitch, CB_A, aidx(dxi, j, kx, 0, rows, un.bw, ntz));
}
}
}
cb_push_back(CB_A, A_CAP);
ft_end(CB_BAND, CB_A, pitch, relu);
pf_mark(4);
}
// akc: channel tiles per A version (the tilized k-block, 2 in pair mode), coff: this k-block's first of them
NOINL void c3_pass(const LC& c, const Unit& un, uint32_t kb, uint32_t akc, uint32_t coff, bool pop) {
const uint32_t rows = c.s * (un.bh - 1) + 3, bw = un.bw, kc = c.kc, nbw = c.nbw, s = c.s;
const uint32_t ns = nbw < 4 ? nbw : 4;
const bool hc = c.op == OP_HC;
const uint32_t off = kb == 0 ? c.nbias : 0;
cbw(CB_A, A_CAP);
mm_init_f(CB_A, CB_W);
pk(CB_ACC, (kb > 0 || c.nres > 0) ? 1 : 0);
for (uint32_t r = 0; r < un.bh; ++r) {
for (uint32_t kx = 0; kx < bw; ++kx) {
const uint32_t m = r * bw + kx;
for (uint32_t n0 = 0; n0 < nbw; n0 += ns) {
tile_regs_acquire();
for (uint32_t i = 0; i < ns; ++i) {
const uint32_t n = n0 + i;
if (kb == 0) {
if (hc) {
mm_f(CB_SEL, CB_W, m, n, i);
} else {
mm_f(CB_CONST, CB_W, C_E01, n, i);
}
}
for (uint32_t cc = 0; cc < kc; ++cc) {
if (hc) {
const uint32_t a_ = n >> 1, b_ = n & 1;
for (uint32_t o = 0; o < 4; ++o) {
const uint32_t j = r + a_ + (o >> 1); // r + 1 + oy, oy = a - 1 + (o >> 1)
const uint32_t dxi = b_ + (o & 1); // ox + 1
mm_f(CB_A, CB_W, aidx(dxi, j, kx, cc + coff, rows, bw, akc), off + (cc * 4 + n) * 4 + o, i);
}
} else {
for (uint32_t t = 0; t < 9; ++t) {
const uint32_t j = s * r + t / 3; // s r + 1 + dy
mm_f(CB_A, CB_W, aidx(t % 3, j, kx, cc + coff, rows, bw, akc), off + (cc * 9 + t) * nbw + n,
i);
}
}
}
}
tile_regs_commit();
tile_regs_wait();
for (uint32_t i = 0; i < ns; ++i) {
pack_tile<true>(i, CB_ACC, m * nbw + n0 + i);
}
tile_regs_release();
}
}
}
if (pop) {
cb_pop_front(CB_A, A_CAP);
}
}
// HC epilogue: per M tile, relu(acc) -> Z (bf16, 4 sub-pixel tiles); R = sum_s (w4 hi + lo)_s Z_s^T + b4; relu
NOINL void epi_hc(uint32_t mt) {
cbw(CB_ACC, ACC_CAP);
for (uint32_t m0 = 0; m0 < mt; m0 += 4) {
const uint32_t nz = mt - m0 < 4 ? mt - m0 : 4;
reconfig_data_format_srca(CB_ACC);
copy_init(CB_ACC);
pk(CB_Z, 0);
relu_on();
cbr(CB_Z, Z_CAP);
for (uint32_t i = 0; i < nz; ++i) {
tile_regs_acquire();
for (uint32_t sb = 0; sb < 4; ++sb) {
copy_tile(CB_ACC, (m0 + i) * 4 + sb, sb);
}
tile_regs_commit();
tile_regs_wait();
for (uint32_t sb = 0; sb < 4; ++sb) {
pack_tile<true>(sb, CB_Z, i * 4 + sb);
}
tile_regs_release();
}
cb_push_back(CB_Z, Z_CAP);
cbw(CB_Z, Z_CAP);
mm_init_n(CB_CONST, CB_Z, 1);
pk(CB_HO, 0);
for (uint32_t i = 0; i < nz; ++i) {
cbr(CB_HO, 1);
tile_regs_acquire();
for (uint32_t sb = 0; sb < 4; ++sb) {
mm_f(CB_CONST, CB_Z, C_W4H + sb, i * 4 + sb, 0);
mm_f(CB_CONST, CB_Z, C_W4L + sb, i * 4 + sb, 0);
}
mm_f(CB_CONST, CB_CONST, C_E4, C_B4T, 0);
tile_regs_commit();
tile_regs_wait();
pack_tile(0, CB_HO);
tile_regs_release();
cb_push_back(CB_HO, 1);
}
relu_off();
cb_pop_front(CB_Z, Z_CAP);
}
cb_pop_front(CB_ACC, ACC_CAP);
}
// ---------------------------------------------------------------- layer
NOINL void run_layer(uint32_t l, uint32_t x, uint32_t y) {
LC c;
lc_of(l, c);
pf_start(l);
Sched sc;
sched_of(pl, l, x, y, sc);
const uint32_t bp0 = pl.lf(l, L_BP0), bp = pl.lf(l, L_BP);
for (uint32_t gi = 0; gi < sc.ng; ++gi) {
for (uint32_t k = 0; k < sc.rounds; ++k) {
const uint32_t u = k * sc.span + sc.off;
const bool valid = u < c.u;
if (c.op == OP_RA) {
uint32_t m0 = 0, mt = 0;
if (valid) {
ra_unit(pl, l, u, m0, mt);
pf_mark(5);
cbw(CB_TAPA, TAPA_CAP);
pf_mark(3);
}
for (uint32_t nbi = 0; nbi < c.nbi; ++nbi) {
if (valid) {
cbr(CB_ACC, ACC_CAP);
}
for (uint32_t kb = 0; kb < c.nkb; ++kb) {
const uint32_t np = kb == 0 ? bp0 : bp;
w_wait(np);
if (valid) {
mm1x1_acc(CB_TAPA, mt, c.ct, kb * c.kc, c.kc, kb == 0 ? c.nbias : 0, c.nbw, kb > 0 ? 1 : 0);
}
w_pop(np);
}
if (valid) {
cb_push_back(CB_ACC, ACC_CAP);
epi_untilize_n(c.nbw, mt, false);
}
}
if (valid) {
cb_pop_front(CB_TAPA, TAPA_CAP);
}
continue;
}
Unit un;
if (valid) {
unit_of(pl, l, u, un);
}
if (c.op == OP_UP) {
const uint32_t mt = valid ? un.bh * un.bw : 0;
if (valid) {
pf_mark(5);
cbw(CB_BAND, 1);
pf_mark(3);
const uint32_t b16 = rd16(CB_BAND);
ft_begin(CB_BAND, c.ct, CB_A, false);
cbr(CB_A, A_CAP);
for (uint32_t r = 0; r < un.bh; ++r) {
for (uint32_t kx = 0; kx < un.bw; ++kx) {
const uint32_t src16 = b16 + r * (c.bandp >> 4) + kx * 32 * (c.pxb >> 4);
ft_run(CB_BAND, src16, c.ct, c.ct, CB_A, (r * un.bw + kx) * c.ct);
}
}
cb_push_back(CB_A, A_CAP);
ft_end(CB_BAND, CB_A, c.ct, false);
cb_pop_front(CB_BAND, 1);
pf_mark(4);
cbw(CB_A, A_CAP);
}
for (uint32_t nbi = 0; nbi < c.nbi; ++nbi) {
w_wait(bp0);
if (valid) {
cbr(CB_ACC, ACC_CAP);
mm1x1_acc(CB_A, mt, c.ct, 0, c.kc, c.nbias, c.nbw, 0);
}
w_pop(bp0);
if (valid) {
cb_push_back(CB_ACC, ACC_CAP);
epi_untilize_n(c.nbw, mt, false);
}
}
if (valid) {
cb_pop_front(CB_A, A_CAP);
}
continue;
}
// C3 / HC: n-blocks nbi of this group (nbi > 1 only for C3 without residuals: whole output pixels of the
// unit on one core, the BRISC stages them in L1 and writes whole pages); the band stays for every n-block
const uint32_t bwmax = pl.lf(l, L_BW);
uint32_t b16 = 0;
const bool pair = pl.lf(l, L_PAIR) != 0;
for (uint32_t nbi = 0; nbi < c.nbi; ++nbi) {
if (valid) {
cbr(CB_ACC, ACC_CAP);
if (nbi == 0) {
for (uint32_t ri = 0; ri < c.nres; ++ri) {
res_add(c, un, ri, bwmax);
}
pf_mark(4);
cbw(CB_BAND, 1);
pf_mark(3);
b16 = rd16(CB_BAND) + (c.boff >> 4);
if (c.op == OP_HC) {
cbw(CB_SEL, SEL_CAP);
}
}
}
for (uint32_t kb = 0; kb < c.nkb; ++kb) {
const uint32_t np = kb == 0 ? bp0 : bp;
w_wait(np);
if (valid) {
if (!pair || (kb & 1) == 0) {
c3_tilize(c, un, kb, b16, pair ? 2 : c.kc);
}
c3_pass(c, un, kb, pair ? 2 : c.kc, pair ? (kb & 1) : 0, !pair || (kb & 1) == 1);
}
w_pop(np);
}
if (valid) {
if (nbi + 1 == c.nbi) {
cb_pop_front(CB_BAND, 1);
}
cb_push_back(CB_ACC, ACC_CAP);
pf_mark(5);
if (c.op == OP_HC) {
cb_pop_front(CB_SEL, SEL_CAP);
epi_hc(un.bh * un.bw);
} else {
epi_untilize_n(c.nbw, un.bh * un.bw, (c.flags & F_RELU_OUT) != 0);
}
pf_mark(6);
}
}
}
}
}
} // namespace
void kernel_main() {
const uint32_t x = get_arg_val<uint32_t>(A_X), y = get_arg_val<uint32_t>(A_Y);
uint32_t nl = get_common_arg_val<uint32_t>(K_NL);
g_spin = get_common_arg_val<uint32_t>(K_SPIN);
g_sync = cb_addr(CB_SYNC);
compute_kernel_hw_startup(CB_A, CB_W, CB_ACC);
wait_plan_all();
RING = pl.hdr(H_RING);
if (nl > pl.hdr(H_NL)) {
nl = pl.hdr(H_NL);
}
cbw(CB_CONST, N_CONST);
const uint32_t ngrp = pl.hdr(H_NGRP);
for (uint32_t grp = 0; grp < ngrp; ++grp) {
for (uint32_t l = 0; l < nl; ++l) {
run_layer(l, x, y);
pf_mark(5);
}
}
}
|