rdt2-base-p150 / code /tt_rdt2 /tt /kernels /pxf_addnorm_writer.cpp
changh95's picture
tt-model push rdt2-base-p150 (container)
9d8d25c verified
Raw History Blame Contribute Delete
4.21 kB
// SPDX-License-Identifier: Apache-2.0
//
// RDT2-FM fused PREFIX op "addnorm", writer (BRISC, NoC 1).
// 1. the core's partial row sums of (x + delta)^2 (one fp32 tile, column 0) -> slot c of AN_CB_STAT on every core
// of the row group (itself included), then +1 on each one's arrival semaphore (data first: write barrier);
// 2. x + delta (fp32; only with WRX) of the core's tiles to DRAM;
// 3. once all P partials of the group have landed here: hand AN_CB_STAT to the compute kernel (push P) and reset
// the semaphore (every increment has arrived); then h (bf16) to DRAM.
// The P cores of row group r are the consecutive logical cores k = r P .. r P + P - 1 (k = y GX + x).
#include <cstdint>
#include "api/dataflow/dataflow_api.h"
#include "pxf_common.hpp"
using namespace pxf;
namespace {
constexpr uint32_t P = get_compile_time_arg_val(AN_CT_P);
constexpr uint32_t GX = get_compile_time_arg_val(AN_CT_GX);
constexpr uint32_t NT = get_compile_time_arg_val(AN_CT_NT);
constexpr uint32_t WRX = get_compile_time_arg_val(AN_CT_WRX);
constexpr auto acc_x = TensorAccessorArgs<AN_CT_ACC0>();
constexpr auto acc_d = TensorAccessorArgs<acc_x.next_compile_time_args_offset()>();
constexpr auto acc_g = TensorAccessorArgs<acc_d.next_compile_time_args_offset()>();
constexpr auto acc_xo = TensorAccessorArgs<acc_g.next_compile_time_args_offset()>();
constexpr auto acc_ho = TensorAccessorArgs<acc_xo.next_compile_time_args_offset()>();
FORCE_INLINE uint32_t cb_base(uint32_t cb) {
const auto& iface = get_local_cb_interface(cb);
return (iface.fifo_limit - iface.fifo_size) << cb_addr_shift;
}
FORCE_INLINE uint64_t peer_addr(uint32_t k, uint32_t addr) {
const uint32_t x = k % GX, y = k / GX;
return get_noc_addr(get_common_arg_val<uint32_t>(AN_C_NX0 + x), get_common_arg_val<uint32_t>(AN_C_NX0 + GX + y),
addr);
}
} // namespace
void kernel_main() {
const AnShare sh = an_share(get_absolute_logical_x(), get_absolute_logical_y(), get_compile_time_arg_val(AN_CT_GX),
get_compile_time_arg_val(AN_CT_P), get_compile_time_arg_val(AN_CT_NT));
const uint32_t r = sh.r;
const uint32_t c = sh.c;
const uint32_t c0 = sh.c0;
const uint32_t nc = sh.nc;
const uint32_t sem = get_semaphore(AN_SEM);
volatile tt_l1_ptr uint32_t* sem_ptr = reinterpret_cast<volatile tt_l1_ptr uint32_t*>(sem);
// 1. partial -> slot c of every group member
cb_wait_front(AN_CB_PART, 1);
const uint32_t src = get_read_ptr(AN_CB_PART);
const uint32_t dst = cb_base(AN_CB_STAT) + c * T32;
for (uint32_t j = 0; j < P; ++j) {
noc_async_write(src, peer_addr(r * P + j, dst), T32);
}
noc_async_write_barrier();
for (uint32_t j = 0; j < P; ++j) {
noc_semaphore_inc(peer_addr(r * P + j, sem), 1);
}
cb_pop_front(AN_CB_PART, 1);
// 3a. x + delta while the other partials arrive
const uint32_t row0 = r * NT + c0;
if constexpr (WRX) { // every x' tile was packed into AN_CB_XN before the partial: write it from there (the
// compute pops AN_CB_XN only at its very end, nothing overwrites it before)
const auto xo = TensorAccessor(acc_xo, get_common_arg_val<uint32_t>(AN_C_XO), T32);
const uint32_t a = cb_base(AN_CB_XN);
for (uint32_t i = 0; i < nc; ++i) {
noc_async_write(a + i * T32, xo.get_noc_addr(row0 + i), T32);
}
}
// 2. all partials of the group here
noc_semaphore_wait(sem_ptr, P);
noc_semaphore_set(sem_ptr, 0);
cb_reserve_back(AN_CB_STAT, P);
cb_push_back(AN_CB_STAT, P);
// 3b. h
const auto ho = TensorAccessor(acc_ho, get_common_arg_val<uint32_t>(AN_C_HO), T16);
for (uint32_t j0 = 0; j0 < nc; j0 += AN_CHUNK) {
const uint32_t n = nc - j0 < AN_CHUNK ? nc - j0 : AN_CHUNK;
cb_wait_front(AN_CB_H, n);
const uint32_t a = get_read_ptr(AN_CB_H);
for (uint32_t i = 0; i < n; ++i) {
noc_async_write(a + i * T16, ho.get_noc_addr(row0 + j0 + i), T16);
}
noc_async_writes_flushed();
cb_pop_front(AN_CB_H, n);
}
noc_async_write_barrier();
noc_async_atomic_barrier();
}