changh95's picture
tt-model push embeddinggemma-2 (container)
5c33f00 verified
Raw History Blame Contribute Delete
2.34 kB
// SPDX-License-Identifier: Apache-2.0
//
// mk_vattn reader (NCRISC): the head's key-mask tiles, K and V (resident for the whole launch), then the query tiles
// of this core one by one (double-buffered). DRAM requests are whole 2 KB tile pages.
#include <cstdint>
#include "va_dm.hpp"
using namespace vax;
constexpr uint32_t PT = get_compile_time_arg_val(0); // key / query tiles of the bucket (P / 32)
constexpr auto acc = TensorAccessorArgs<1>(); // DRAM interleaved bf16 TILE (q, k, v, mask)
FORCE_INLINE auto dram(uint32_t addr) { return TensorAccessor(acc, addr, T16); }
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);
g_spin = get_common_arg_val<uint32_t>(K_SPIN);
g_who = 1;
if (nq == 0) {
return;
}
const auto qa = dram(get_common_arg_val<uint32_t>(K_Q));
const auto ka = dram(get_common_arg_val<uint32_t>(K_K));
const auto va = dram(get_common_arg_val<uint32_t>(K_V));
const auto ma = dram(get_common_arg_val<uint32_t>(K_M));
// key mask (shared by every head)
cbr_back(CB_M, PT);
{
const uint32_t base = get_write_ptr(CB_M);
for (uint32_t j = 0; j < PT; ++j) {
noc_async_read_page(j, ma, base + j * T16);
}
rd_bar();
}
cb_push_back(CB_M, PT);
// K and V of head h: tiles (h * PT + j) * 2 + d
cbr_back(CB_K, 2 * PT);
{
const uint32_t base = get_write_ptr(CB_K);
for (uint32_t t = 0; t < 2 * PT; ++t) {
noc_async_read_page(h * 2 * PT + t, ka, base + t * T16);
}
rd_bar();
}
cb_push_back(CB_K, 2 * PT);
cbr_back(CB_V, 2 * PT);
{
const uint32_t base = get_write_ptr(CB_V);
for (uint32_t t = 0; t < 2 * PT; ++t) {
noc_async_read_page(h * 2 * PT + t, va, base + t * T16);
}
rd_bar();
}
cb_push_back(CB_V, 2 * PT);
// query tiles
for (uint32_t i = 0; i < nq; ++i) {
cbr_back(CB_Q, 2);
const uint32_t base = get_write_ptr(CB_Q);
const uint32_t page = (h * PT + q0 + i) * 2;
noc_async_read_page(page, qa, base);
noc_async_read_page(page + 1, qa, base + T16);
rd_bar();
cb_push_back(CB_Q, 2);
}
}