Download code/tt_depthpro2/tt/mk_dec/kernels/md_trisc.cpp from changh95/DepthPro2: direct link, hf CLI and curl.
- Browser
- Download file 25.9 kB
-
https://huggingface.co/changh95/DepthPro2/resolve/main/code/tt_depthpro2/tt/mk_dec/kernels/md_trisc.cpp
- Command line
-
hf download hf://changh95/DepthPro2/code/tt_depthpro2/tt/mk_dec/kernels/md_trisc.cpp
-
curl -L -o md_trisc.cpp https://huggingface.co/changh95/DepthPro2/resolve/main/code/tt_depthpro2/tt/mk_dec/kernels/md_trisc.cpp
25.9 kB
| // 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). | |
| using namespace mdx; | |
| namespace { | |
| constexpr MathFidelity FID = static_cast<MathFidelity>(MD_FID); | |
| constexpr MathFidelity FID = MathFidelity::HiFi4; | |
| 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) | |
| 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; | |
| })); | |
| } | |
| ALWI void pf_start(uint32_t) {} | |
| ALWI void pf_mark(uint32_t) {} | |
| // ---------------------------------------------------------------- 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); | |
| } | |
| } | |
| } | |