Download code/tt_rdt2/tt/kernels/pxf_mlp_compute.cpp from changh95/rdt2-base-p150: direct link, hf CLI and curl.
- Browser
- Download file 7.71 kB
-
https://huggingface.co/changh95/rdt2-base-p150/resolve/main/code/tt_rdt2/tt/kernels/pxf_mlp_compute.cpp
- Command line
-
hf download hf://changh95/rdt2-base-p150/code/tt_rdt2/tt/kernels/pxf_mlp_compute.cpp
-
curl -L -o pxf_mlp_compute.cpp https://huggingface.co/changh95/rdt2-base-p150/resolve/main/code/tt_rdt2/tt/kernels/pxf_mlp_compute.cpp
7.71 kB
| // SPDX-License-Identifier: Apache-2.0 | |
| // | |
| // RDT2-FM fused PREFIX op "swiglu_mm" (round 6 / prefix), compute: m = bf16(silu(h W_g) * (h W_u)) for the core's nc | |
| // output columns over all MT row tiles. Sub-blocks of 2 rows x (gate c, up c) (fp32 DST, half sync: 4 tiles), K | |
| // accumulated per block in DST; the block partials accumulate in L1 in fp32 (packer L1 accumulation into MM_CB_ACC, | |
| // tile (r, w) at r * WMAX + w); on the last K-block the partial is reloaded into DST (exact, UnpackToDestFp32), the | |
| // block's products added, and silu(g) * u formed in fp32 on the SFPU (x * sigmoid(x) * y, the silu_tile sigmoid), so m | |
| // is rounded once. | |
| namespace ckernel::sfpu { | |
| // o = g * sigmoid(g) * u (fp32; the sigmoid of calculate_silu / calculate_clamped_silu_glu) | |
| template <bool is_fp32_dest_acc_en, int ITERATIONS = 8> | |
| inline void pxf_swiglu(const uint g, const uint u, const uint o) { | |
| constexpr uint ts = 32; // rows per tile in SFPU addressing | |
| for (int d = 0; d < ITERATIONS; d++) { | |
| sfpi::vFloat x = sfpi::dst_reg[g * ts]; | |
| sfpi::vFloat y = sfpi::dst_reg[u * ts]; | |
| sfpi::dst_reg[o * ts] = x * _sfpu_sigmoid_<is_fp32_dest_acc_en>(x) * y; | |
| sfpi::dst_reg++; | |
| } | |
| } | |
| inline void pxf_swiglu_init() { sigmoid_init<false>(); } | |
| } // namespace ckernel::sfpu | |
| // the SwiGLU runs on the PACK thread by default (round 6 sweep: 880 -> 856 us at 416); PXF_MM_MATH_EPI (development | |
| // A / B) runs it on the math thread | |
| namespace ckernel::sfpu { | |
| // pack-thread variant: dst_reg[0] = g (in / out), dst_reg[uoff * 32] = u (the unary SFPU params walk the faces) | |
| template <bool is_fp32_dest_acc_en, int ITERATIONS = 8> | |
| inline void pxf_swiglu_u(const uint uoff) { | |
| for (int d = 0; d < ITERATIONS; d++) { | |
| sfpi::vFloat x = sfpi::dst_reg[0]; | |
| sfpi::vFloat y = sfpi::dst_reg[uoff * 32]; | |
| sfpi::dst_reg[0] = x * _sfpu_sigmoid_<is_fp32_dest_acc_en>(x) * y; | |
| sfpi::dst_reg++; | |
| } | |
| } | |
| } // namespace ckernel::sfpu | |
| 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 WMAX = get_compile_time_arg_val(MM_CT_WMAX); | |
| constexpr uint32_t NKB = KT / KBW; | |
| constexpr uint32_t BLK = MT * KBW; | |
| FORCE_INLINE void swiglu_init() { MATH((SFPU_BINARY_INIT_FN_NO_ARGS(unused, sfpu::pxf_swiglu_init))); } | |
| FORCE_INLINE void swiglu(uint32_t g, uint32_t u) { | |
| MATH((SFPU_BINARY_CALL(DST_SYNC_MODE, DST_ACCUM_MODE, pxf_swiglu, (DST_ACCUM_MODE, 8), g, u, g, VectorMode::RC))); | |
| } | |
| // the SwiGLU of rt row tiles on the PACK thread (as ttnn's fused matmul activations, bmm_fused_activation.hpp): it | |
| // replaces tile_regs_wait(), so the math thread starts the next sub-block's matmuls meanwhile | |
| FORCE_INLINE void swiglu_from_pack(uint32_t rt) { | |
| PACK(TTI_SEMWAIT(p_stall::STALL_TDMA | p_stall::STALL_CFG, semaphore::t6_sem(semaphore::MATH_PACK), | |
| p_stall::STALL_ON_ZERO)); | |
| PACK(TT_SETC16(DEST_TARGET_REG_CFG_MATH_Offset_ADDR32, ckernel::packer::get_packer_dest_offset())); | |
| for (uint32_t i = 0; i < rt; ++i) { | |
| PACK((SFPU_UNARY_CALL(DST_SYNC_MODE, DST_ACCUM_MODE, pxf_swiglu_u, (DST_ACCUM_MODE, 8), 2 * i, VectorMode::RC, | |
| 1u))); | |
| } | |
| PACK(TTI_STALLWAIT(p_stall::STALL_PACK, p_stall::WAIT_SFPU)); | |
| } | |
| } // namespace | |
| void kernel_main() { | |
| const MmShare sh = mm_share(get_absolute_logical_x(), get_absolute_logical_y(), get_compile_time_arg_val(MM_CT_GX), | |
| get_compile_time_arg_val(MM_CT_NC), NF); | |
| const uint32_t nc = sh.nc; | |
| compute_kernel_hw_startup<SrcOrder::Reverse>(MM_CB_IN0, MM_CB_IN1, MM_CB_ACC); | |
| cb_reserve_back(MM_CB_ACC, MT * WMAX); // the partials live at fixed tile indices; never pushed | |
| PACK((SFPU_UNARY_INIT_FN(silu, sfpu::silu_init, (APPROX)))); // the sigmoid constants of the pack-thread SwiGLU | |
| uint32_t cur_rt = 2; | |
| matmul_block_init(MM_CB_IN0, MM_CB_IN1, 0, 2, cur_rt, KBW); | |
| for (uint32_t kb = 0; kb < NKB; ++kb) { | |
| const bool last = kb == NKB - 1; | |
| cb_wait_front(MM_CB_IN0, BLK); | |
| cb_wait_front(MM_CB_IN1, KBW * WMAX); | |
| if (!last) { | |
| pack_reconfig_data_format(MM_CB_ACC); | |
| pack_reconfig_l1_acc(kb > 0 ? 1 : 0); | |
| } else { | |
| pack_reconfig_data_format(MM_CB_OUT); | |
| pack_reconfig_l1_acc(0); | |
| } | |
| PXF_ZONE("MM_BLK"); | |
| 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 < nc; ++j) { | |
| tile_regs_acquire(); | |
| if (last && NKB > 1) { // the fp32 partial of blocks 0 .. NKB - 2 -> DST | |
| reconfig_data_format_srca(MM_CB_ACC); | |
| copy_init(MM_CB_ACC); | |
| for (uint32_t i = 0; i < rt; ++i) { | |
| copy_tile(MM_CB_ACC, (r0 + i) * WMAX + 2 * j, 2 * i); | |
| copy_tile(MM_CB_ACC, (r0 + i) * WMAX + 2 * j + 1, 2 * i + 1); | |
| } | |
| reconfig_data_format_srca(MM_CB_IN1); | |
| matmul_block_init(MM_CB_IN0, MM_CB_IN1, 0, 2, rt, KBW); | |
| cur_rt = rt; | |
| } else if (rt != cur_rt || last) { | |
| matmul_block_init(MM_CB_IN0, MM_CB_IN1, 0, 2, rt, KBW); | |
| cur_rt = rt; | |
| } | |
| uint32_t i0 = r0 * KBW, i1 = 2 * j; | |
| for (uint32_t k = 0; k < KBW; ++k) { | |
| matmul_block(MM_CB_IN0, MM_CB_IN1, i0, i1, 0, 0, 2, rt, KBW); | |
| ++i0; | |
| i1 += WMAX; | |
| } | |
| if (!last) { | |
| tile_regs_commit(); | |
| tile_regs_wait(); | |
| for (uint32_t i = 0; i < rt; ++i) { | |
| pack_tile<true>(2 * i, MM_CB_ACC, (r0 + i) * WMAX + 2 * j); | |
| pack_tile<true>(2 * i + 1, MM_CB_ACC, (r0 + i) * WMAX + 2 * j + 1); | |
| } | |
| tile_regs_release(); | |
| } else { | |
| swiglu_init(); | |
| for (uint32_t i = 0; i < rt; ++i) { | |
| swiglu(2 * i, 2 * i + 1); | |
| } | |
| tile_regs_commit(); | |
| cb_reserve_back(MM_CB_OUT, rt); | |
| swiglu_from_pack(rt); | |
| tile_regs_wait(); | |
| for (uint32_t i = 0; i < rt; ++i) { | |
| pack_tile<true>(2 * i, MM_CB_OUT, i); | |
| } | |
| tile_regs_release(); | |
| cb_push_back(MM_CB_OUT, rt); | |
| } | |
| } | |
| } | |
| cb_pop_front(MM_CB_IN0, BLK); | |
| cb_pop_front(MM_CB_IN1, KBW * WMAX); | |
| } | |
| } | |