bevformer-p150 / code /tt_bevformer /tt /kernels /scatter_k_dm.cpp
changh95's picture
tt-model push bevformer-p150 (container)
55474c5 verified
Raw History Blame Contribute Delete
6.15 kB
// SPDX-License-Identifier: Apache-2.0
// scatter_k (tt/scatter_k.py, BEVFORMER_SCA_SCATTER=kernel; the SCA tail of OPT_PLAN section 4 MK4b): per 32-row block
// of the BEV queries, out = ((g0 + g1) + g2) * scale with g_k = table[index_k[q]] (a zero row for index_k[q] >= R),
// i.e. three rowgather_k gathers, two ttnn.add and one ttnn.multiply (column broadcast) in one program.
//
// table bf16 TILE [1, 1, R, D], L1 interleaved (the msda_mk SCA output: 32-byte face-row reads);
// idx_k uint32 ROW_MAJOR [1, N] (one page each);
// scale bf16 TILE [1, 1, N, 1] (column 0 of each block's tile);
// out bf16 TILE [1, 1, N, D].
// The same source runs on both data-movement RISC-Vs: PART 0 (NCRISC) gathers g0 and g1, PART 1 (BRISC) gathers g2,
// reads the scale tile and writes the output tiles. Blocks: contiguous ranges per core; common RT args only (P3).
#include <stdint.h>
#include "api/dataflow/dataflow_api.h"
constexpr uint32_t PART = get_compile_time_arg_val(0);
constexpr uint32_t R = get_compile_time_arg_val(1); // table rows (logical)
constexpr uint32_t N = get_compile_time_arg_val(2); // output rows (logical)
constexpr uint32_t DT = get_compile_time_arg_val(3); // 32-column tiles per row
constexpr uint32_t NWORK = get_compile_time_arg_val(4); // 32-row output blocks
constexpr uint32_t NC = get_compile_time_arg_val(5); // cores
constexpr uint32_t GX = get_compile_time_arg_val(6); // grid width
constexpr auto t_args = TensorAccessorArgs<7>();
constexpr auto i0_args = TensorAccessorArgs<t_args.next_compile_time_args_offset()>();
constexpr auto i1_args = TensorAccessorArgs<i0_args.next_compile_time_args_offset()>();
constexpr auto i2_args = TensorAccessorArgs<i1_args.next_compile_time_args_offset()>();
constexpr auto s_args = TensorAccessorArgs<i2_args.next_compile_time_args_offset()>();
constexpr auto o_args = TensorAccessorArgs<s_args.next_compile_time_args_offset()>();
constexpr uint32_t TB16 = 2048;
constexpr uint32_t cb_g0 = 0, cb_g1 = 1, cb_g2 = 2, cb_sc = 3, cb_out = 16;
constexpr uint32_t cb_idx = PART ? 9 : 8;
template <typename TT, typename TI>
static inline void gather_block(uint32_t w, uint32_t cb, const TT& tab, const TI& idx_acc, uint32_t ibuf,
uint64_t zeros) {
cb_reserve_back(cb, DT);
const uint32_t ob = get_write_ptr(cb);
const uint32_t nvalid = (w + 1) * 32 <= N ? 32 : N - w * 32;
noc_async_read(idx_acc.get_noc_addr(0) + w * 128, ibuf, 128);
noc_async_read_barrier();
volatile tt_l1_ptr uint32_t* idx = reinterpret_cast<volatile tt_l1_ptr uint32_t*>(ibuf);
for (uint32_t r = 0; r < 32; ++r) {
const uint32_t roff = (((r >> 4) << 1) << 9) + ((r & 15) << 5);
const uint32_t q = r < nvalid ? idx[r] : R;
if (q >= R) {
for (uint32_t t = 0; t < DT; ++t) {
noc_async_read(zeros, ob + t * TB16 + roff, 32);
noc_async_read(zeros, ob + t * TB16 + roff + 512, 32);
}
continue;
}
const uint32_t sr = q & 31;
const uint32_t soff = (((sr >> 4) << 1) << 9) + ((sr & 15) << 5);
const uint32_t st = (q >> 5) * DT;
for (uint32_t t = 0; t < DT; ++t) {
const uint64_t s = tab.get_noc_addr(st + t, soff);
noc_async_read(s, ob + t * TB16 + roff, 32);
noc_async_read(s + 512, ob + t * TB16 + roff + 512, 32);
}
}
noc_async_read_barrier();
cb_push_back(cb, DT);
}
template <typename TO>
static inline void write_block(uint32_t w, const TO& o_acc) {
cb_wait_front(cb_out, DT);
const uint32_t rp = get_read_ptr(cb_out);
for (uint32_t t = 0; t < DT; ++t) {
noc_async_write(rp + t * TB16, o_acc.get_noc_addr(w * DT + t), TB16);
}
noc_async_writes_flushed();
cb_pop_front(cb_out, DT);
}
void kernel_main() {
const uint32_t ci = get_absolute_logical_y() * GX + get_absolute_logical_x();
if (ci >= NC) {
return;
}
const uint32_t w0 = ci * NWORK / NC;
const uint32_t w1 = (ci + 1) * NWORK / NC;
if (w0 >= w1) {
return;
}
const auto tab = TensorAccessor(t_args, get_common_arg_val<uint32_t>(0), TB16);
const auto i0 = TensorAccessor(i0_args, get_common_arg_val<uint32_t>(1), N * 4);
const auto i1 = TensorAccessor(i1_args, get_common_arg_val<uint32_t>(2), N * 4);
const auto i2 = TensorAccessor(i2_args, get_common_arg_val<uint32_t>(3), N * 4);
const auto sc = TensorAccessor(s_args, get_common_arg_val<uint32_t>(4), TB16);
const auto o_acc = TensorAccessor(o_args, get_common_arg_val<uint32_t>(5), TB16);
const uint64_t zeros = get_noc_addr(MEM_ZEROS_BASE);
cb_reserve_back(cb_idx, 1);
const uint32_t ibuf = get_write_ptr(cb_idx);
for (uint32_t w = w0; w < w1; ++w) {
if constexpr (PART == 0) {
gather_block(w, cb_g0, tab, i0, ibuf, zeros);
gather_block(w, cb_g1, tab, i1, ibuf, zeros);
} else {
gather_block(w, cb_g2, tab, i2, ibuf, zeros);
cb_reserve_back(cb_sc, 1);
const uint32_t sa = get_write_ptr(cb_sc);
noc_async_read_tile(w, sc, sa);
noc_async_read_barrier();
// column 0 of every row over the row's 32 columns (binary_ng's column-broadcast fill): row r lives in
// faces 2 (r / 16) (columns 0-15) and + 1 (columns 16-31), row r % 16, 16 bf16 each
volatile tt_l1_ptr uint16_t* e = reinterpret_cast<volatile tt_l1_ptr uint16_t*>(sa);
for (uint32_t r = 0; r < 32; ++r) {
const uint32_t f0 = ((r >> 4) << 1) * 256 + (r & 15) * 16;
const uint16_t v = e[f0];
for (uint32_t c = 0; c < 16; ++c) {
e[f0 + c] = v;
e[f0 + 256 + c] = v;
}
}
cb_push_back(cb_sc, 1);
if (w > w0) {
write_block(w - 1, o_acc); // the previous block's output (computed meanwhile)
}
}
}
if constexpr (PART == 1) {
write_block(w1 - 1, o_acc);
noc_async_write_barrier();
}
}