Download code/tt_embgemma/tt/mk_vattn/kernels/va_writer.cpp from changh95/embeddinggemma-2: direct link, hf CLI and curl.
- Browser
- Download file 2.99 kB
-
https://huggingface.co/changh95/embeddinggemma-2/resolve/main/code/tt_embgemma/tt/mk_vattn/kernels/va_writer.cpp
- Command line
-
hf download hf://changh95/embeddinggemma-2/code/tt_embgemma/tt/mk_vattn/kernels/va_writer.cpp
-
curl -L -o va_writer.cpp https://huggingface.co/changh95/embeddinggemma-2/resolve/main/code/tt_embgemma/tt/mk_vattn/kernels/va_writer.cpp
2.99 kB
| // SPDX-License-Identifier: Apache-2.0 | |
| // | |
| // mk_vattn writer (BRISC): fills the constant tiles, writes the output tiles of this core, then its status page. | |
| using namespace vax; | |
| constexpr uint32_t PT = get_compile_time_arg_val(0); | |
| constexpr auto acc = TensorAccessorArgs<1>(); // DRAM interleaved bf16 TILE (o) | |
| constexpr auto acc_status = TensorAccessorArgs<acc.next_compile_time_args_offset()>(); // uint32 ROW_MAJOR pages | |
| // bf16 tile in face order: element (r, c) at face (r / 16) * 2 + c / 16, offset (r % 16) * 16 + c % 16 | |
| FORCE_INLINE void fill_const(uint32_t addr, bool col0_only) { | |
| volatile tt_l1_ptr uint16_t* p = reinterpret_cast<volatile tt_l1_ptr uint16_t*>(addr); | |
| for (uint32_t i = 0; i < 1024; ++i) { | |
| p[i] = 0; | |
| } | |
| for (uint32_t r = 0; r < 32; ++r) { | |
| for (uint32_t c = 0; c < 32; ++c) { | |
| if (col0_only && c != 0) { | |
| continue; | |
| } | |
| const uint32_t idx = ((r / 16) * 2 + c / 16) * 256 + (r % 16) * 16 + (c % 16); | |
| p[idx] = 0x3F80; // 1.0 | |
| } | |
| } | |
| } | |
| void kernel_main() { | |
| const uint32_t h = get_arg_val<uint32_t>(A_HEAD); | |
| const uint32_t q0 = get_arg_val<uint32_t>(A_Q0); | |
| const uint32_t nq = get_arg_val<uint32_t>(A_NQ); | |
| const uint32_t core = get_arg_val<uint32_t>(A_CORE); | |
| g_spin = get_common_arg_val<uint32_t>(K_SPIN); | |
| g_who = 2; | |
| // clear the sync words of this core (the TRISC / NCRISC only read W_ABORT after a spin of 4096 iterations) | |
| *sync_ptr(W_ABORT) = 0; | |
| *sync_ptr(W_CODE) = 0; | |
| *sync_ptr(W_ARG) = 0; | |
| *sync_ptr(W_WHO) = 0; | |
| if (nq > 0) { | |
| cbr_back(CB_C, 2); | |
| const uint32_t base = get_write_ptr(CB_C); | |
| fill_const(base + C_ONES * T16, false); | |
| fill_const(base + C_C0 * T16, true); | |
| cb_push_back(CB_C, 2); | |
| const auto oa = TensorAccessor(acc, get_common_arg_val<uint32_t>(K_O), T16); | |
| for (uint32_t i = 0; i < nq; ++i) { | |
| cbw_front(CB_O, 2); | |
| const uint32_t src = get_read_ptr(CB_O); | |
| const uint32_t page = (h * PT + q0 + i) * 2; | |
| noc_async_write_page(page, oa, src); | |
| noc_async_write_page(page + 1, oa, src + T16); | |
| wr_bar(); | |
| cb_pop_front(CB_O, 2); | |
| } | |
| } | |
| // status page: abort word, code, argument, who, nq | |
| invalidate_l1_cache(); | |
| const uint32_t st = cb_base(CB_SYNC) + STATUS_WORDS * 4; // the second 2 KB of the 4 KB sync CB | |
| volatile tt_l1_ptr uint32_t* s = reinterpret_cast<volatile tt_l1_ptr uint32_t*>(st); | |
| for (uint32_t i = 0; i < 16; ++i) { | |
| s[i] = 0; | |
| } | |
| s[0] = *sync_ptr(W_ABORT) == ABORT_MAGIC ? 1 : 0; | |
| s[1] = *sync_ptr(W_CODE); | |
| s[2] = *sync_ptr(W_ARG); | |
| s[3] = *sync_ptr(W_WHO); | |
| s[4] = nq; | |
| s[5] = 0x5A5A0000u | h; | |
| const auto sa = TensorAccessor(acc_status, get_common_arg_val<uint32_t>(K_STATUS), STATUS_WORDS * 4); | |
| noc_async_write_page(core, sa, st); | |
| wr_bar(); | |
| } | |