bevfusion-p150 / code /tt_bevfusion /tt /kernels /vfe_expand.cpp
changh95's picture
tt-model push bevfusion-p150 (container)
5528e28 verified
Raw History Blame Contribute Delete
4.48 kB
// SPDX-License-Identifier: Apache-2.0
// P1 compact VFE input (tt/vfe_kernel.py, OPT_PLAN P1): the first sparse layer's L1 gather table built on the device
// from the compact upload. Input: bf16 ROW_MAJOR [CAP / 32 + 1, 32 * VW] (32 voxels of VW bf16 per page: [hi | lo]
// of the 4 VFE channels, in key order). Output: bf16 ROW_MAJOR [1, 1, CAP + 1, OW] L1 interleaved (64 B pages):
// row r = [compact row r | zeros]; only the frame's rows [0, count) (count = meta word 0, the l1 count; whole blocks
// of 32) and the zero sentinel row CAP are written: no other row is ever gathered (every index is a live row or
// the sentinel). Pure data movement: the table bytes equal the padded upload's.
//
// The same source runs on both data-movement RISCs of every core (worker wid of NW): live blocks in NW contiguous
// ranges; worker 0 also writes the sentinel row. Each block: one 32 * VW * 2 B page read (prefetched one block
// ahead), 32 rows copied into a zero-padded staging area (two, alternating) and written as 32 page writes.
//
// CT args: [0] per-core RT-arg count (P3) [1] CAP [2] NW [3] VB (compact row bytes) [4] OB (output row bytes)
// [5] CB (scratch) [6] META_BYTES, then the TensorAccessorArgs of the compact input, meta, output.
// Common RT args: [input, meta, output addresses]. Per-core RT args: [wid].
#include <cstdint>
#include "api/dataflow/dataflow_api.h"
constexpr uint32_t CAP = get_compile_time_arg_val(1);
constexpr uint32_t NW = get_compile_time_arg_val(2);
constexpr uint32_t VB = get_compile_time_arg_val(3);
constexpr uint32_t OB = get_compile_time_arg_val(4);
constexpr uint32_t CB_ID = get_compile_time_arg_val(5);
constexpr uint32_t META_BYTES = get_compile_time_arg_val(6);
constexpr auto in_args = TensorAccessorArgs<7>();
constexpr auto meta_args = TensorAccessorArgs<in_args.next_compile_time_args_offset()>();
constexpr auto out_args = TensorAccessorArgs<meta_args.next_compile_time_args_offset()>();
constexpr uint32_t PB = 32 * VB; // compact page bytes
constexpr uint32_t SB = 32 * OB; // staging bytes (32 output rows)
// scratch layout (bytes; tt/vfe_kernel.scratch_bytes mirrors it)
constexpr uint32_t O_HDR = 0;
constexpr uint32_t O_IN = 64;
constexpr uint32_t O_ST = O_IN + 2 * PB;
static_assert(VB % 4 == 0 && OB % 16 == 0 && VB <= OB, "row sizes");
void kernel_main() {
const uint32_t wid = get_arg_val<uint32_t>(0);
const auto src = TensorAccessor(in_args, get_common_arg_val<uint32_t>(0), PB);
const auto meta = TensorAccessor(meta_args, get_common_arg_val<uint32_t>(1), META_BYTES);
const auto dst = TensorAccessor(out_args, get_common_arg_val<uint32_t>(2), OB);
const uint32_t base = get_write_ptr(CB_ID);
noc_async_read(meta.get_noc_addr(0, 0), base + O_HDR, 64);
// both staging areas zeroed once: only the first VB bytes of a row are rewritten per block
uint32_t* st = reinterpret_cast<uint32_t*>(base + O_ST);
for (uint32_t i = 0; i < 2 * SB / 4; ++i) {
st[i] = 0;
}
noc_async_read_barrier();
invalidate_l1_cache();
const uint32_t cnt = reinterpret_cast<const uint32_t*>(base + O_HDR)[0];
uint32_t nlb = (cnt + 31) >> 5;
if (nlb > CAP / 32) {
nlb = CAP / 32;
}
const uint32_t b0 = wid * nlb / NW, b1 = (wid + 1) * nlb / NW;
if (wid == 0) {
noc_async_write(base + O_ST, dst.get_noc_addr(CAP), OB); // the zero sentinel row
}
if (b0 < b1) {
noc_async_read(src.get_noc_addr(b0), base + O_IN, PB);
}
uint32_t cur = 0;
for (uint32_t b = b0; b < b1; ++b) {
noc_async_read_barrier();
invalidate_l1_cache();
if (b + 1 < b1) {
noc_async_read(src.get_noc_addr(b + 1), base + O_IN + (cur ^ 1) * PB, PB);
}
noc_async_writes_flushed(); // staging[cur] was the source of the writes two blocks ago
const uint32_t* in = reinterpret_cast<const uint32_t*>(base + O_IN + cur * PB);
const uint32_t sa = base + O_ST + cur * SB;
uint32_t* so = reinterpret_cast<uint32_t*>(sa);
for (uint32_t r = 0; r < 32; ++r) {
#pragma GCC unroll 8
for (uint32_t w = 0; w < VB / 4; ++w) {
so[r * (OB / 4) + w] = in[r * (VB / 4) + w];
}
}
const uint32_t row0 = b * 32;
for (uint32_t r = 0; r < 32; ++r) {
noc_async_write(sa + r * OB, dst.get_noc_addr(row0 + r), OB);
}
cur ^= 1;
}
noc_async_write_barrier();
}