Download code/tt_rdt2/tt/kernels/pxf_mlp_writer.cpp from changh95/rdt2-base-p150: direct link, hf CLI and curl.
- Browser
- Download file 5.39 kB
-
https://huggingface.co/changh95/rdt2-base-p150/resolve/main/code/tt_rdt2/tt/kernels/pxf_mlp_writer.cpp
- Command line
-
hf download hf://changh95/rdt2-base-p150/code/tt_rdt2/tt/kernels/pxf_mlp_writer.cpp
-
curl -L -o pxf_mlp_writer.cpp https://huggingface.co/changh95/rdt2-base-p150/resolve/main/code/tt_rdt2/tt/kernels/pxf_mlp_writer.cpp
5.39 kB
| // SPDX-License-Identifier: Apache-2.0 | |
| // | |
| // RDT2-FM fused PREFIX op "swiglu_mm" (round 6 / prefix), h / output kernel (BRISC, NoC 1). | |
| // 1. h K-blocks (MT x KBW tiles, [r][k]) into MM_CB_IN0 on every core: core 0 reads the block from h (DRAM or L1 | |
| // interleaved) and multicasts it to the whole grid rectangle once every receiver has room for it (the ttnn 1-D | |
| // in0-multicast protocol: receivers clear their VALID word and +1 core 0's SEND semaphore; core 0 waits for | |
| // NC - 1, multicasts the data, then the VALID word, on one NoC path); | |
| // 2. the core's m tiles (bf16) as the compute packs them: row pair by row pair, column by column. | |
| using namespace pxf; | |
| namespace { | |
| constexpr uint32_t MT = get_compile_time_arg_val(MM_CT_MT); | |
| constexpr uint32_t KT = get_compile_time_arg_val(MM_CT_KT); | |
| constexpr uint32_t KBW = get_compile_time_arg_val(MM_CT_KBW); | |
| constexpr uint32_t NF = get_compile_time_arg_val(MM_CT_NF); | |
| constexpr uint32_t GX = get_compile_time_arg_val(MM_CT_GX); | |
| constexpr uint32_t NC = get_compile_time_arg_val(MM_CT_NC); | |
| constexpr uint32_t GY = get_compile_time_arg_val(MM_CT_GY); | |
| constexpr uint32_t NKB = KT / KBW; | |
| constexpr uint32_t BLK = MT * KBW; | |
| constexpr auto acc_h = TensorAccessorArgs<MM_CT_ACC0>(); | |
| constexpr auto acc_w = TensorAccessorArgs<acc_h.next_compile_time_args_offset()>(); | |
| constexpr auto acc_m = TensorAccessorArgs<acc_w.next_compile_time_args_offset()>(); | |
| FORCE_INLINE uint32_t nx(uint32_t x) { return get_common_arg_val<uint32_t>(MM_C_NX0 + x); } | |
| FORCE_INLINE uint32_t ny(uint32_t y) { return get_common_arg_val<uint32_t>(MM_C_NX0 + GX + y); } | |
| // multicast NoC address of the whole grid rectangle; NoC 1 addresses it end-first | |
| FORCE_INLINE uint64_t grid_mcast(uint32_t addr) { | |
| const uint32_t ax = nx(0), ay = ny(0), bx = nx(GX - 1), by = ny(GY - 1); | |
| const uint32_t lx = ax < bx ? ax : bx, hx = ax < bx ? bx : ax, ly = ay < by ? ay : by, hy = ay < by ? by : ay; | |
| if (noc_index == 0) { | |
| return NOC_MULTICAST_ADDR( | |
| DYNAMIC_NOC_X(0, lx), DYNAMIC_NOC_Y(0, ly), DYNAMIC_NOC_X(0, hx), DYNAMIC_NOC_Y(0, hy), addr); | |
| } | |
| return NOC_MULTICAST_ADDR( | |
| DYNAMIC_NOC_X(1, hx), DYNAMIC_NOC_Y(1, hy), DYNAMIC_NOC_X(1, lx), DYNAMIC_NOC_Y(1, ly), addr); | |
| } | |
| } // namespace | |
| void kernel_main() { | |
| const MmShare sh = mm_share(get_absolute_logical_x(), get_absolute_logical_y(), GX, NC, NF); | |
| const uint32_t send_sem = get_semaphore(MM_SEM_SEND); | |
| const uint32_t recv_sem = get_semaphore(MM_SEM_RECV); | |
| volatile tt_l1_ptr uint32_t* send_ptr = reinterpret_cast<volatile tt_l1_ptr uint32_t*>(send_sem); | |
| volatile tt_l1_ptr uint32_t* recv_ptr = reinterpret_cast<volatile tt_l1_ptr uint32_t*>(recv_sem); | |
| const bool sender = sh.k == 0; | |
| // 1. h blocks | |
| (void)sender; | |
| (void)send_ptr; | |
| (void)recv_ptr; | |
| for (uint32_t kb = 0; kb < NKB; ++kb) { | |
| cb_reserve_back(MM_CB_IN0, BLK); | |
| cb_push_back(MM_CB_IN0, BLK); | |
| } | |
| if (sender) { | |
| const auto ha = TensorAccessor(acc_h, get_common_arg_val<uint32_t>(MM_C_H), T16); | |
| *recv_ptr = 1; // the VALID word multicast after each block | |
| for (uint32_t kb = 0; kb < NKB; ++kb) { | |
| cb_reserve_back(MM_CB_IN0, BLK); | |
| const uint32_t l1 = get_write_ptr(MM_CB_IN0); | |
| { | |
| PXF_ZONE("MM_H"); | |
| for (uint32_t r = 0; r < MT; ++r) { | |
| const uint32_t page = r * KT + kb * KBW; | |
| for (uint32_t kk = 0; kk < KBW; ++kk) { | |
| noc_async_read(ha.get_noc_addr(page + kk), l1 + (r * KBW + kk) * T16, T16); | |
| } | |
| } | |
| noc_async_read_barrier(); | |
| } | |
| if constexpr (NC > 1) { | |
| PXF_ZONE("MM_MC"); | |
| noc_semaphore_wait(send_ptr, NC - 1); | |
| noc_semaphore_set(send_ptr, 0); | |
| noc_async_write_multicast(l1, grid_mcast(l1), BLK * T16, NC - 1, true); | |
| noc_async_writes_flushed(); | |
| noc_semaphore_set_multicast(recv_sem, grid_mcast(recv_sem), NC - 1); | |
| } | |
| cb_push_back(MM_CB_IN0, BLK); | |
| } | |
| } else { | |
| const uint64_t sender_sem = get_noc_addr(nx(0), ny(0), send_sem); | |
| for (uint32_t kb = 0; kb < NKB; ++kb) { | |
| cb_reserve_back(MM_CB_IN0, BLK); | |
| noc_semaphore_set(recv_ptr, 0); | |
| noc_semaphore_inc(sender_sem, 1); | |
| noc_semaphore_wait(recv_ptr, 1); | |
| cb_push_back(MM_CB_IN0, BLK); | |
| } | |
| } | |
| // 2. m tiles: (rows r0, r0 + 1) x column c0 + j, in the compute's order | |
| const auto ma = TensorAccessor(acc_m, get_common_arg_val<uint32_t>(MM_C_M), T16); | |
| for (uint32_t r0 = 0; r0 < MT; r0 += 2) { | |
| const uint32_t rt = MT - r0 < 2 ? MT - r0 : 2; | |
| for (uint32_t j = 0; j < sh.nc; ++j) { | |
| cb_wait_front(MM_CB_OUT, rt); | |
| const uint32_t a = get_read_ptr(MM_CB_OUT); | |
| for (uint32_t i = 0; i < rt; ++i) { | |
| noc_async_write(a + i * T16, ma.get_noc_addr((r0 + i) * NF + sh.c0 + j), T16); | |
| } | |
| noc_async_writes_flushed(); | |
| cb_pop_front(MM_CB_OUT, rt); | |
| } | |
| } | |
| noc_async_write_barrier(); | |
| noc_async_atomic_barrier(); | |
| } | |