Download code/tt_laya/tt/mk/kernels/lm_ncrisc.cpp from changh95/laya: direct link, hf CLI and curl.
- Browser
- Download file 7.37 kB
-
https://huggingface.co/changh95/laya/resolve/main/code/tt_laya/tt/mk/kernels/lm_ncrisc.cpp
- Command line
-
hf download hf://changh95/laya/code/tt_laya/tt/mk/kernels/lm_ncrisc.cpp
-
curl -L -o lm_ncrisc.cpp https://huggingface.co/changh95/laya/resolve/main/code/tt_laya/tt/mk/kernels/lm_ncrisc.cpp
7.37 kB
| // SPDX-License-Identifier: Apache-2.0 | |
| // | |
| // laya megakernel, NCRISC (NoC 0): the weight ring of a band core (the VLA-JEPA PREFIX megakernel's stream, with | |
| // bounded waits and the plan's active bands). | |
| // feeder (x, 0): reads its column's pages (one contiguous page range per layer arena) from DRAM straight into its | |
| // own ring slot, pushes them for its own TRISC and multicasts each page to the same slot of the | |
| // column's other active band cores (rows 1 .. nba - 1), then the cumulative valid count (W_WVAL). A | |
| // page goes out only when every receiver granted it (per-receiver cumulative credits W_CRED + y). | |
| // receiver : grants ring slots as its TRISC frees them (atomic add on the feeder's W_CRED + y) and pushes the | |
| // pages the feeder flagged valid. | |
| // The NCRISC never waits on the op sequence, only on credits / DRAM, so the weight stream runs as far ahead of the | |
| // TRISC as the ring allows (across op and layer boundaries). | |
| using namespace lmx; | |
| namespace { | |
| constexpr auto acc_dram = TensorAccessorArgs<CT_ACC0>(); | |
| constexpr uint32_t DEPTH = LM_DEPTH; // DRAM page reads in flight (transaction ids 1 .. DEPTH) | |
| static_assert(DEPTH >= 1 && DEPTH <= 12, "read depth"); | |
| FORCE_INLINE uint32_t ring_outstanding_tiles() { | |
| invalidate_l1_cache(); | |
| asm volatile("" ::: "memory"); | |
| const uint16_t received = (uint16_t)get_cb_tiles_received_ptr(CB_W)[0]; | |
| const uint16_t acked = (uint16_t)reg_read((uintptr_t)get_cb_tiles_acked_ptr(CB_W)); | |
| return (uint16_t)(received - acked); | |
| } | |
| FORCE_INLINE uint32_t freed_pages(uint32_t pushed) { | |
| const uint32_t out = ring_outstanding_tiles(); | |
| return pushed - (out + PAGE_TILES - 1) / PAGE_TILES; | |
| } | |
| struct Stream { | |
| uint32_t nle, nhl, npe, nph, p0e, p0h; | |
| uint32_t total() const { return nle * npe + nhl * nph; } | |
| // (arena address, page) of stream page s | |
| void at(uint32_t s, uint32_t& arena, uint32_t& page) const { | |
| if (s < nle * npe) { | |
| const uint32_t l = s / npe; | |
| arena = ca(K_ARENA0 + l); | |
| page = p0e + (s - l * npe); | |
| } else { | |
| const uint32_t q = s - nle * npe, l = q / nph; | |
| arena = ca(K_ARENA0 + NLE + l); | |
| page = p0h + (q - l * nph); | |
| } | |
| } | |
| }; | |
| void receiver(const Stream& st, uint32_t y) { | |
| const uint32_t total = st.total(); | |
| const uint64_t cred = get_noc_addr(rt(A_FX), rt(A_FY), sync_addr(W_CRED + y)); | |
| uint32_t granted = 0, pushed = 0, idle = 0; | |
| while (pushed < total && !g_abort) { | |
| WAYPOINT("NRCV"); | |
| bool prog = false; | |
| 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; | |
| prog = true; | |
| } | |
| } | |
| const uint32_t v = sync_read(W_WVAL); | |
| if (v > pushed) { | |
| cb_push_back(CB_W, (v - pushed) * PAGE_TILES); | |
| pushed = v; | |
| prog = true; | |
| } | |
| if (prog) { | |
| idle = 0; | |
| } else if (++idle > g_spin) { | |
| abort_note(AB_SYNC, 0x5000 + pushed); | |
| } else { | |
| peer_abort(idle); | |
| } | |
| } | |
| at_bar(); | |
| } | |
| void feeder(const Stream& st, uint32_t nrecv) { | |
| const uint32_t total = st.total(); | |
| const uint32_t fx = rt(A_FX); | |
| const uint32_t y0 = nrecv ? noc_y(1) : 0, y1 = nrecv ? noc_y(nrecv) : 0; | |
| const uint32_t base = cb_base(CB_W); | |
| const uint32_t wval = sync_addr(W_WVAL); | |
| uint32_t rd_issued = 0, rd_done = 0, mc_done = 0, mc_flushed = 0, idle = 0; | |
| if (nrecv == 0) { | |
| mc_done = total; // nothing to multicast | |
| } | |
| while ((mc_done < total || rd_done < total) && !g_abort) { | |
| WAYPOINT("NFED"); | |
| bool prog = false; | |
| // reads land in order: push for the own TRISC | |
| 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); | |
| prog = true; | |
| } | |
| // multicast the oldest landed page once every receiver has room for it | |
| if (nrecv && 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) { // the source word of page mc_done - N_SRCV is reused | |
| 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)); | |
| noc_async_write_multicast(a, mcast_addr(fx, y0, fx, y1, a), PAGE_BYTES, nrecv, true); | |
| *reinterpret_cast<volatile tt_l1_ptr uint32_t*>(src) = mc_done + 1; | |
| noc_semaphore_set_multicast(src, mcast_addr(fx, y0, fx, y1, wval), nrecv); | |
| ++mc_done; | |
| prog = true; | |
| } | |
| } | |
| // next DRAM read into the own ring: the slot must be free locally and its previous page multicast + flushed | |
| if (rd_issued < total && rd_issued - rd_done < DEPTH && rd_issued < freed_pages(rd_done) + SLOTS) { | |
| bool ok = true; | |
| if (nrecv && rd_issued >= SLOTS) { | |
| const uint32_t prev = rd_issued - SLOTS; // the page that last used this slot | |
| if (mc_done <= prev) { | |
| ok = false; | |
| } else if (mc_flushed <= prev) { | |
| wr_flush(); | |
| mc_flushed = mc_done; | |
| } | |
| } | |
| if (ok) { | |
| uint32_t arena, page; | |
| st.at(rd_issued, arena, page); | |
| noc_async_read_set_trid(1 + (rd_issued % DEPTH)); | |
| const auto acc = TensorAccessor(acc_dram, arena, PAGE_BYTES); | |
| noc_async_read_page(page, acc, base + (rd_issued % SLOTS) * PAGE_BYTES); | |
| ++rd_issued; | |
| prog = true; | |
| } | |
| } | |
| if (prog) { | |
| idle = 0; | |
| } else if (++idle > g_spin) { | |
| abort_note(AB_SYNC, 0x6000 + rd_done); | |
| } else { | |
| peer_abort(idle); | |
| } | |
| } | |
| noc_async_read_set_trid(0); | |
| // exit only once every read landed and every page / flag multicast is acknowledged | |
| rd_bar(); | |
| wr_bar(); | |
| } | |
| } // namespace | |
| void kernel_main() { | |
| g_dbg_word = W_DBG + 2; | |
| wait_go(get_semaphore(1)); // SEM_GO: the BRISCs zeroed every sync word and landed the plan | |
| const Plan pl = plan_l1(); | |
| if (pl.spin()) { | |
| g_spin = pl.spin(); | |
| } | |
| const uint32_t y = rt(A_Y); | |
| const uint32_t nba = pl.nba(); | |
| if (y >= nba) { | |
| return; // inactive band: no weight stream | |
| } | |
| Stream st; | |
| st.nle = pl.nl(); | |
| st.nhl = pl.nhl(); | |
| st.npe = rt(A_NPE); | |
| st.nph = rt(A_NPH); | |
| st.p0e = rt(A_P0E); | |
| st.p0h = rt(A_P0H); | |
| if (y == 0) { | |
| feeder(st, nba - 1); | |
| } else { | |
| receiver(st, y); | |
| } | |
| } | |