Download code/tt_laya/tt/mk/kernels/lm_brisc.cpp from changh95/laya: direct link, hf CLI and curl.
- Browser
- Download file 19.3 kB
-
https://huggingface.co/changh95/laya/resolve/main/code/tt_laya/tt/mk/kernels/lm_brisc.cpp
- Command line
-
hf download hf://changh95/laya/code/tt_laya/tt/mk/kernels/lm_brisc.cpp
-
curl -L -o lm_brisc.cpp https://huggingface.co/changh95/laya/resolve/main/code/tt_laya/tt/mk/kernels/lm_brisc.cpp
19.3 kB
| // SPDX-License-Identifier: Apache-2.0 | |
| // | |
| // laya megakernel, BRISC (NoC 1): the plan, the boot barrier, every DRAM read of the op inputs (embeddings, vector | |
| // tiles, rope tables, attention q / K / V and padding masks), the q / k / v staging writes, the hub's per-layer QKV | |
| // barrier, every exchange between cores and the final hidden-state writes: | |
| // STAT row all-gather of the norm partials (R tiles fp32 per core -> slot [x] of CB_STAT0 / CB_STAT1 by parity) | |
| // IX row all-gather of the normalised slices (-> CB_INX at [r][n0 ..]) | |
| // IF row all-gather of the GeGLU / head-FFN slices (-> CB_INF at [r][f0 ..]) | |
| // IA attention item (row, head), query tile i -> the band of global tile g: CB_INA [g / NB][2 head ..] | |
| // Every gather is a multicast of the slice to the band's grid row (loopback when the sender is in it) and one flag | |
| // word set to a cumulative generation; a landing CB is pushed at full capacity once every flag of the round reached | |
| // it. The BRISC walks the TRISC's order (lm_trisc.cpp). WAR safety of every landing buffer follows from the dataflow | |
| // (research/laya/SPEC.md section 4.3). | |
| using namespace lmx; | |
| namespace { | |
| constexpr auto acc_dram = TensorAccessorArgs<CT_ACC0>(); | |
| struct Me { | |
| uint32_t idx, x, y, u0, nu, n0, nx, f0, nf, h0, nh; | |
| uint32_t R, T, nba, nl, nhl; | |
| uint32_t g[R_MAX]; // global row tiles of the band | |
| bool band, hub; | |
| uint32_t rowx0, rowx1; | |
| }; | |
| Me me; | |
| Plan pl; | |
| Vec vec; | |
| FORCE_INLINE auto dram(uint32_t addr, uint32_t page_bytes) { return TensorAccessor(acc_dram, addr, page_bytes); } | |
| LM_NOINL void rd32(uint32_t addr, uint32_t page, uint32_t l1) { noc_async_read_page(page, dram(addr, T32), l1); } | |
| LM_NOINL void rd16(uint32_t addr, uint32_t page, uint32_t l1) { noc_async_read_page(page, dram(addr, T16), l1); } | |
| FORCE_INLINE void step(uint32_t v) { | |
| *sync_ptr(W_STEP) = v; | |
| const uint32_t l = v >> 4, ph = v & 15; | |
| if (l < 32) { | |
| reinterpret_cast<volatile tt_l1_ptr uint32_t*>(cb_base(CB_SYNC) + SYNC_PROF)[512 + l * 16 + ph] = | |
| get_timestamp_32b(); | |
| } | |
| } | |
| // multicast [src, src + bytes) to the same-offset address dst on every core of grid row `row` (linked), then flag | |
| // word = v; the sender is in the rectangle when row == me.y (loopback variants) | |
| LM_NOINL void row_mcast(uint32_t row, uint32_t src, uint32_t dst, uint32_t bytes, uint32_t flag_word, uint32_t src_word, | |
| uint32_t v) { | |
| const uint32_t y = noc_y(row); | |
| const uint32_t srcw = sync_addr(W_SRC + src_word); | |
| *reinterpret_cast<volatile tt_l1_ptr uint32_t*>(srcw) = v; | |
| if (row == me.y) { | |
| noc_async_write_multicast_loopback_src(src, mcast_addr(me.rowx0, y, me.rowx1, y, dst), bytes, C, true); | |
| noc_semaphore_set_multicast_loopback_src(srcw, mcast_addr(me.rowx0, y, me.rowx1, y, sync_addr(flag_word)), C); | |
| } else { | |
| noc_async_write_multicast(src, mcast_addr(me.rowx0, y, me.rowx1, y, dst), bytes, C, true); | |
| noc_semaphore_set_multicast(srcw, mcast_addr(me.rowx0, y, me.rowx1, y, sync_addr(flag_word)), C); | |
| } | |
| wr_flush(); | |
| } | |
| // data only (not linked), to grid row `row` (default: the own band row); the caller sends the flag after a write | |
| // barrier | |
| LM_NOINL void row_data(uint32_t src, uint32_t dst, uint32_t bytes, uint32_t row = 0xFF) { | |
| row = row == 0xFF ? me.y : row; | |
| const uint32_t y = noc_y(row); | |
| if (row == me.y) { | |
| noc_async_write_multicast_loopback_src(src, mcast_addr(me.rowx0, y, me.rowx1, y, dst), bytes, C, false); | |
| } else { | |
| noc_async_write_multicast(src, mcast_addr(me.rowx0, y, me.rowx1, y, dst), bytes, C, false); | |
| } | |
| wr_flush(); | |
| } | |
| LM_NOINL void row_flag(uint32_t flag_word, uint32_t src_word, uint32_t v, uint32_t row = 0xFF) { | |
| row = row == 0xFF ? me.y : row; | |
| const uint32_t y = noc_y(row); | |
| const uint32_t srcw = sync_addr(W_SRC + src_word); | |
| *reinterpret_cast<volatile tt_l1_ptr uint32_t*>(srcw) = v; | |
| if (row == me.y) { | |
| noc_semaphore_set_multicast_loopback_src(srcw, mcast_addr(me.rowx0, y, me.rowx1, y, sync_addr(flag_word)), C); | |
| } else { | |
| noc_semaphore_set_multicast(srcw, mcast_addr(me.rowx0, y, me.rowx1, y, sync_addr(flag_word)), C); | |
| } | |
| wr_flush(); | |
| } | |
| LM_NOINL void wait_all(uint32_t w0, uint32_t n, uint32_t v) { | |
| for (uint32_t i = 0; i < n; ++i) { | |
| spin_ge(w0 + i, v); | |
| } | |
| } | |
| // per-op vector slice tiles (row-replicated fp32) -> CB_G, n0 .. n0 + n - 1 of section `sec` (+ an optional second) | |
| LM_NOINL void vec_in(uint32_t sec, uint32_t n0, uint32_t n, uint32_t sec2 = 0xFFFFFFFFu, uint32_t n2 = 0) { | |
| rback(CB_G, G_CAP); | |
| const uint32_t a = get_write_ptr(CB_G); | |
| for (uint32_t j = 0; j < n; ++j) { | |
| rd32(ca(K_VEC), sec + n0 + j, a + j * T32); | |
| } | |
| if (sec2 != 0xFFFFFFFFu) { | |
| for (uint32_t j = 0; j < n2; ++j) { | |
| rd32(ca(K_VEC), sec2 + n0 + j, a + (n + j) * T32); | |
| } | |
| } | |
| rd_bar(); | |
| cb_push_back(CB_G, G_CAP); | |
| } | |
| void load_args() { | |
| me.idx = rt(A_IDX); | |
| me.x = rt(A_X); | |
| me.y = rt(A_Y); | |
| me.u0 = rt(A_U0); | |
| me.nu = rt(A_NU); | |
| me.n0 = rt(A_N0); | |
| me.nx = rt(A_NX); | |
| me.f0 = rt(A_F0); | |
| me.nf = rt(A_NF); | |
| me.h0 = rt(A_H0); | |
| me.nh = rt(A_NH); | |
| me.hub = me.idx == ca(K_N_ALL) - 1; | |
| me.rowx0 = noc_x(0); | |
| me.rowx1 = noc_x(C - 1); | |
| vec.DT = DT; | |
| vec.FH = FH; | |
| vec.NL = NLE; | |
| vec.NHLV = NHLE; | |
| } | |
| // the plan -> L1 (every RISC reads it there), then every core's arrival at the hub, which multicasts go | |
| void boot() { | |
| const uint32_t pa = cb_base(CB_SYNC) + SYNC_PLAN; | |
| noc_async_read_page(0, dram(ca(K_PLAN), PLAN_WORDS * 4), pa); | |
| rd_bar(); | |
| pl = plan_l1(); | |
| if (pl.spin()) { | |
| g_spin = pl.spin(); | |
| } | |
| const uint32_t sem_boot = get_semaphore(0); | |
| const uint32_t sem_go = get_semaphore(1); | |
| noc_semaphore_inc(get_noc_addr(ca(K_HUB_X), ca(K_HUB_Y), sem_boot), 1); | |
| at_bar(); | |
| if (me.hub) { | |
| volatile tt_l1_ptr uint32_t* b = reinterpret_cast<volatile tt_l1_ptr uint32_t*>(sem_boot); | |
| WAYPOINT("BOOT"); | |
| uint32_t it = 0; | |
| while (true) { | |
| invalidate_l1_cache(); | |
| if (*b >= ca(K_N_ALL)) { | |
| break; | |
| } | |
| if (++it > g_spin) { | |
| abort_note(AB_SYNC, 998); | |
| break; | |
| } | |
| } | |
| volatile tt_l1_ptr uint32_t* go = reinterpret_cast<volatile tt_l1_ptr uint32_t*>(sem_go); | |
| *go = 1; | |
| noc_semaphore_set_multicast_loopback_src( | |
| sem_go, mcast_addr(ca(K_ALL_X0), ca(K_ALL_Y0), ca(K_ALL_X1), ca(K_ALL_Y1), sem_go), ca(K_N_ALL)); | |
| wr_flush(); | |
| } | |
| wait_go(sem_go); | |
| me.T = pl.T(); | |
| me.nba = pl.nba(); | |
| me.nl = pl.nl(); | |
| me.nhl = pl.nhl(); | |
| me.band = me.y < me.nba; | |
| me.R = me.band ? band_rows(me.T, NB, me.y) : 0; | |
| for (uint32_t r = 0; r < R_MAX; ++r) { | |
| me.g[r] = me.y + r * NB; | |
| } | |
| cb_push_back(CB_PLAN, 1); // the TRISC reads the plan | |
| } | |
| // constants; band: rope tables, type-embedding slices, the embedding slices (-> CB_X) | |
| void init_inputs() { | |
| rback(CB_CONST, N_CONST); | |
| for (uint32_t i = 0; i < N_CONST; ++i) { | |
| rd16(ca(K_CONST), i, get_write_ptr(CB_CONST) + i * T16); | |
| } | |
| rd_bar(); | |
| cb_push_back(CB_CONST, N_CONST); | |
| if (!me.band) { | |
| return; | |
| } | |
| rback(CB_TAB, R_MAX * 4); | |
| { | |
| const uint32_t a = get_write_ptr(CB_TAB); | |
| for (uint32_t r = 0; r < me.R; ++r) { | |
| for (uint32_t th = 0; th < 2; ++th) { | |
| for (uint32_t k = 0; k < 2; ++k) { | |
| rd32(ca(K_ROPE), rope_tile(th, me.g[r], k), a + ((r * 2 + th) * 2 + k) * T32); | |
| } | |
| } | |
| } | |
| } | |
| // type embedding = one-hot(lane -> qtype) @ [te_0; te_1; te_2] (the final norm's epilogue): the table slices and | |
| // the band's one-hot tiles | |
| rback(CB_TE, NX_MAX); | |
| { | |
| const uint32_t a = get_write_ptr(CB_TE); | |
| for (uint32_t j = 0; j < me.nx; ++j) { | |
| rd32(ca(K_VEC), vec.te3() + me.n0 + j, a + j * T32); | |
| } | |
| } | |
| rback(CB_OH, R_MAX); | |
| { | |
| const uint32_t a = get_write_ptr(CB_OH); | |
| for (uint32_t r = 0; r < me.R; ++r) { | |
| rd16(ca(K_OH), me.g[r], a + r * T16); | |
| } | |
| } | |
| rback(CB_E, R_MAX * NX_MAX); | |
| { | |
| const uint32_t a = get_write_ptr(CB_E); | |
| for (uint32_t r = 0; r < me.R; ++r) { | |
| for (uint32_t j = 0; j < me.nx; ++j) { | |
| rd32(ca(K_E), me.g[r] * DT + me.n0 + j, a + (r * NX_MAX + j) * T32); | |
| } | |
| } | |
| } | |
| rd_bar(); | |
| cb_push_back(CB_TAB, R_MAX * 4); | |
| cb_push_back(CB_TE, NX_MAX); | |
| cb_push_back(CB_OH, R_MAX); | |
| cb_push_back(CB_E, R_MAX * NX_MAX); | |
| } | |
| // ------------------------------------------------------------------ norm n: partials, gamma (/ beta), slices | |
| // mode: 0 = INX (the next op's input), 1 = X (the final norm: the residual itself), 2 = both (the embedding norm) | |
| void norm(uint32_t n, uint32_t gsec, uint32_t bsec, uint32_t mode) { | |
| const uint32_t par = n & 1; | |
| const uint32_t stcb = par ? CB_STAT1 : CB_STAT0; | |
| const uint32_t stw = par ? W_ST1 : W_ST0; | |
| // this core's R partials -> slot x of every band core's STAT (slots of R_MAX tiles) | |
| wfront(CB_STO, R_MAX); | |
| row_mcast(me.y, get_read_ptr(CB_STO), cb_base(stcb) + me.x * R_MAX * T32, me.R * T32, stw + me.x, SRC_ST, n + 1); | |
| cb_pop_front(CB_STO, R_MAX); | |
| if (bsec != 0xFFFFFFFFu) { | |
| vec_in(gsec, me.n0, me.nx, bsec, me.nx); | |
| } else { | |
| vec_in(gsec, me.n0, me.nx); | |
| } | |
| wait_all(stw, C, n + 1); | |
| land(stcb, C_MAX * R_MAX); | |
| if (mode == 1) { | |
| return; | |
| } | |
| // the normalised slices (R x nx bf16) -> [r][n0 ..] of every band core's INX | |
| wfront(CB_OUT, OUT_CAP); | |
| const uint32_t s = get_read_ptr(CB_OUT); | |
| for (uint32_t r = 0; r < me.R; ++r) { | |
| row_data(s + r * me.nx * T16, cb_base(CB_INX) + (r * DT + me.n0) * T16, me.nx * T16); | |
| } | |
| wr_bar(); // every slice landed on every band core before the flag | |
| row_flag(W_IX + me.x, SRC_IX, n + 1); | |
| cb_pop_front(CB_OUT, OUT_CAP); | |
| wait_all(W_IX, C, n + 1); | |
| land(CB_INX, R_MAX * DT); | |
| } | |
| // ------------------------------------------------------------------ QKV(l): units -> staging, hub arrival | |
| void qkv(uint32_t ls, uint32_t L, bool head) { | |
| if (head) { // in-proj biases of the column's units (2 tiles each) | |
| rback(CB_G, G_CAP); | |
| const uint32_t a = get_write_ptr(CB_G); | |
| for (uint32_t u = 0; u < me.nu; ++u) { | |
| for (uint32_t d = 0; d < 2; ++d) { | |
| rd32(ca(K_VEC), vec.bin(L - NLE) + (me.u0 + u) * 2 + d, a + (u * 2 + d) * T32); | |
| } | |
| } | |
| rd_bar(); | |
| cb_push_back(CB_G, G_CAP); | |
| } | |
| const auto st = dram(ca(K_STAGE), STAGE_PAGE); | |
| for (uint32_t u = 0; u < me.nu; ++u) { | |
| wfront(CB_OUT, OUT_CAP); | |
| const uint32_t s = get_read_ptr(CB_OUT); | |
| for (uint32_t r = 0; r < me.R; ++r) { | |
| noc_async_write_page(spage(ls, H, me.u0 + u, me.g[r]), st, s + r * 2 * T16); | |
| } | |
| wr_flush(); | |
| cb_pop_front(CB_OUT, OUT_CAP); | |
| } | |
| wr_bar(); | |
| noc_semaphore_inc(get_noc_addr(ca(K_HUB_X), ca(K_HUB_Y), sync_addr(W_QCNT)), 1); | |
| at_bar(); | |
| } | |
| void hub_barrier(uint32_t l) { | |
| spin_ge(W_QCNT, (l + 1) * C * me.nba); | |
| const uint32_t srcw = sync_addr(W_SRC + SRC_QKV); | |
| *reinterpret_cast<volatile tt_l1_ptr uint32_t*>(srcw) = l + 1; | |
| noc_semaphore_set_multicast_loopback_src( | |
| srcw, mcast_addr(ca(K_ALL_X0), ca(K_ALL_Y0), ca(K_ALL_X1), ca(K_ALL_Y1), sync_addr(W_QKV)), ca(K_N_ALL)); | |
| wr_flush(); | |
| } | |
| // ------------------------------------------------------------------ attention items of layer l | |
| void att(uint32_t l, bool global) { | |
| spin_ge(W_QKV, l + 1); | |
| const auto st = dram(ca(K_STAGE), STAGE_PAGE); | |
| const uint32_t last_row = pl.w[PL_NROWS] - 1; | |
| for (uint32_t s = 0; s < IT_MAX; ++s) { | |
| const uint32_t it = pl.item(me.idx, s); | |
| if (it == 0) { | |
| break; | |
| } | |
| const uint32_t rho = it_rho(it), h = it_head(it), i0 = it_i0(it), n = it_n(it); | |
| const uint32_t base = pl.row_base(rho), t = pl.row_tiles(rho); | |
| const uint32_t ra = pl.row_a(rho), re = pl.row_e(rho), seg0 = pl.row_seg0(rho); | |
| const uint32_t kj0 = global ? 0 : (i0 >= 2 ? i0 - 2 : 0); | |
| const uint32_t kj1 = global ? t : (i0 + n + 2 < t ? i0 + n + 2 : t); | |
| rback(CB_AKV, AKV_CAP); | |
| const uint32_t a = get_write_ptr(CB_AKV); | |
| for (uint32_t i = i0; i < i0 + n; ++i) { | |
| noc_async_read_page(spage(l, H, h, base + i), st, a + (AKV_Q + i * 2) * T16); | |
| } | |
| for (uint32_t j = kj0; j < kj1; ++j) { | |
| noc_async_read_page(spage(l, H, H + h, base + j), st, a + (AKV_K + j * 2) * T16); | |
| noc_async_read_page(spage(l, H, 2 * H + h, base + j), st, a + (AKV_V + j * 2) * T16); | |
| } | |
| if (re < 32 && kj1 == t) { // key columns past the row's end (its last tile) | |
| rd16(ca(K_PAD), re, a + AKV_M * T16); | |
| } | |
| if (ra > 0 && kj0 == 0) { // key columns before the row's start (its first tile) | |
| rd16(ca(K_PAD), 32 + ra, a + (AKV_M + 1) * T16); | |
| } | |
| rd_bar(); | |
| cb_push_back(CB_AKV, AKV_CAP); | |
| // the context of each query tile -> its band's CB_INA (every core of that grid row): the row's lanes only | |
| // when the tile holds other rows too (packed rows), with this row's segment flag | |
| for (uint32_t i = i0; i < i0 + n; ++i) { | |
| const uint32_t g = base + i; | |
| const uint32_t band = g % NB, r = g / NB; | |
| const uint32_t lo = i == 0 ? ra : 0; | |
| const uint32_t hi = (i + 1 == t && rho != last_row) ? re : 32; | |
| const uint32_t flag = W_IA + (r * 16 + h) * NSEG + (i == 0 ? seg0 : 0); | |
| const uint32_t dst = cb_base(CB_INA) + (r * DT + 2 * h) * T16; | |
| wfront(CB_OUT, OUT_CAP); | |
| const uint32_t src = get_read_ptr(CB_OUT); | |
| if (lo == 0 && hi == 32) { | |
| row_mcast(band, src, dst, 2 * T16, flag, SRC_IA, l + 1); | |
| } else { | |
| // a lane range of the two bf16 tiles: faces 0 / 1 hold lanes 0..15, faces 2 / 3 lanes 16..31, | |
| // 32 bytes per lane and face | |
| for (uint32_t d = 0; d < 2; ++d) { | |
| for (uint32_t f = 0; f < 4; ++f) { | |
| const uint32_t fb = (f >> 1) * 16; | |
| const uint32_t l0 = lo > fb ? lo - fb : 0; | |
| const uint32_t l1 = hi < fb + 16 ? (hi > fb ? hi - fb : 0) : 16; | |
| if (l1 > l0) { | |
| const uint32_t off = d * T16 + f * 512 + l0 * 32; | |
| row_data(src + off, dst + off, (l1 - l0) * 32, band); | |
| } | |
| } | |
| } | |
| wr_bar(); | |
| row_flag(flag, SRC_IA, l + 1, band); | |
| } | |
| cb_pop_front(CB_OUT, OUT_CAP); | |
| } | |
| } | |
| } | |
| // ------------------------------------------------------------------ O(l), FFN(l) | |
| void oproj(uint32_t l, uint32_t L, bool head) { | |
| for (uint32_t r = 0; r < me.R; ++r) { | |
| const uint32_t ns = pl.tile_nseg(me.g[r]); | |
| for (uint32_t h = 0; h < H; ++h) { | |
| wait_all(W_IA + (r * 16 + h) * NSEG, ns, l + 1); | |
| } | |
| } | |
| land(CB_INA, R_MAX * DT); | |
| if (head) { | |
| vec_in(vec.bout(L - NLE), me.n0, me.nx); | |
| } | |
| } | |
| void ffn_in(uint32_t l, uint32_t L, bool head) { | |
| const uint32_t nblk = head ? (me.nh + FB - 1) / FB : (me.nf + PB - 1) / PB; | |
| const uint32_t per = head ? FB : PB; | |
| const uint32_t n = head ? me.nh : me.nf; | |
| const uint32_t c0 = head ? me.h0 : me.f0; | |
| if (head) { | |
| rback(CB_G, G_CAP); | |
| const uint32_t a = get_write_ptr(CB_G); | |
| for (uint32_t j = 0; j < me.nh; ++j) { | |
| rd32(ca(K_VEC), vec.b1(L - NLE) + me.h0 + j, a + j * T32); | |
| } | |
| rd_bar(); | |
| cb_push_back(CB_G, G_CAP); | |
| } | |
| for (uint32_t b = 0; b < nblk; ++b) { | |
| const uint32_t j0 = b * per; | |
| const uint32_t nb = n - j0 < per ? n - j0 : per; | |
| wfront(CB_OUT, OUT_CAP); | |
| const uint32_t s = get_read_ptr(CB_OUT); | |
| for (uint32_t r = 0; r < me.R; ++r) { | |
| row_data(s + r * per * T16, cb_base(CB_INF) + (r * F_MAX + c0 + j0) * T16, nb * T16); | |
| } | |
| cb_pop_front(CB_OUT, OUT_CAP); | |
| } | |
| wr_bar(); | |
| row_flag(W_IF + me.x, SRC_IF, l + 1); | |
| wait_all(W_IF, C, l + 1); | |
| land(CB_INF, AKV_CAP); // the whole allocation (its attention views cycle it whole too) | |
| if (head) { | |
| vec_in(vec.b2(L - NLE), me.n0, me.nx); | |
| } | |
| } | |
| void write_out() { | |
| wfront(CB_HOUT, R_MAX * NX_MAX); | |
| const uint32_t s = get_read_ptr(CB_HOUT); | |
| const auto hd = dram(ca(K_H), T32); | |
| for (uint32_t r = 0; r < me.R; ++r) { | |
| for (uint32_t j = 0; j < me.nx; ++j) { | |
| noc_async_write_page(me.g[r] * DT + me.n0 + j, hd, s + (r * NX_MAX + j) * T32); | |
| } | |
| } | |
| wr_bar(); | |
| cb_pop_front(CB_HOUT, R_MAX * NX_MAX); | |
| } | |
| } // namespace | |
| void kernel_main() { | |
| load_args(); | |
| g_dbg_word = W_DBG; | |
| for (uint32_t w = 0; w < N_SYNC; ++w) { | |
| *sync_ptr(w) = 0; | |
| } | |
| boot(); | |
| step(0xD0D0); | |
| exit_barriers(); | |
| return; | |
| init_inputs(); | |
| uint32_t n = 0; | |
| if (me.band) { | |
| norm(n++, vec.emb(), 0xFFFFFFFFu, 2); // x = LN_emb(e): residual + layer 0's input | |
| } | |
| const uint32_t nl = me.nl, nhl = me.nhl; | |
| for (uint32_t l = 0; l < nl + nhl; ++l) { | |
| const bool head = l >= nl; | |
| const uint32_t L = head ? NLE + (l - nl) : l; // arena / layer index of the full model | |
| step(l * 16 + 1); | |
| if (me.band) { | |
| if (head) { | |
| norm(n++, vec.n1w(L - NLE), vec.n1b(L - NLE), 0); | |
| } else if (l > 0) { | |
| norm(n++, vec.g1(l), 0xFFFFFFFFu, 0); | |
| } | |
| step(l * 16 + 2); | |
| qkv(l, L, head); | |
| } | |
| step(l * 16 + 3); | |
| if (me.hub) { | |
| hub_barrier(l); | |
| } | |
| step(l * 16 + 4); | |
| att(l, head || (l % GEVERY) == 0); | |
| step(l * 16 + 5); | |
| if (me.band) { | |
| oproj(l, L, head); | |
| step(l * 16 + 6); | |
| if (head) { | |
| norm(n++, vec.n2w(L - NLE), vec.n2b(L - NLE), 0); | |
| } else { | |
| norm(n++, vec.g2(l), 0xFFFFFFFFu, 0); | |
| } | |
| step(l * 16 + 7); | |
| ffn_in(l, L, head); | |
| step(l * 16 + 8); | |
| if (!head && l + 1 == nl) { | |
| norm(n++, vec.fin(), 0xFFFFFFFFu, 1); // h = LN_final(x) + type_emb (the head's residual) | |
| } | |
| } | |
| } | |
| if (me.band) { | |
| write_out(); | |
| } | |
| step(0xD0D0); | |
| exit_barriers(); | |
| // debug dump: this core's sync words (aborts, progress) -> page idx of the debug buffer (after the other RISCs | |
| // had time to record theirs; every wait is bounded, so they end too) | |
| for (volatile uint32_t spin = 0; spin < 20000; spin = spin + 1) { | |
| } | |
| noc_async_write_page(me.idx, dram(ca(K_DBG), DBG_PAGE), cb_base(CB_SYNC)); | |
| uint32_t it = 0; | |
| while (!ncrisc_noc_nonposted_writes_flushed(noc_index) && ++it < (g_spin << 2)) { | |
| } | |
| } | |