Download code/tt_bevfusion/tt/kernels/mks_compute.cpp from changh95/bevfusion-p150: direct link, hf CLI and curl.
- Browser
- Download file 9.17 kB
-
https://huggingface.co/changh95/bevfusion-p150/resolve/main/code/tt_bevfusion/tt/kernels/mks_compute.cpp
- Command line
-
hf download hf://changh95/bevfusion-p150/code/tt_bevfusion/tt/kernels/mks_compute.cpp
-
curl -L -o mks_compute.cpp https://huggingface.co/changh95/bevfusion-p150/resolve/main/code/tt_bevfusion/tt/kernels/mks_compute.cpp
9.17 kB
| // SPDX-License-Identifier: Apache-2.0 | |
| // MK-S level megakernel (tt/sparse_mks.py, OPT_PLAN 3.3 MK-S (a)), compute. | |
| // | |
| // The K6 compute (k6_compute.cpp, SPLIT form) inside a layer loop: per layer l the bias tiles (CB_BIAS, popped at | |
| // the layer end), per chunk the weights (CB_W), per live block the tilize of the present taps, the tap matmuls in | |
| // the stock K order (fp32 weights, HiFi4, fp32 DEST), and in the last chunk the epilogue in the stock order: | |
| // + bias, + identity (RES_MASK bit l), ReLU (RELU_MASK bit l), the fp32 copy to CB_Y (WY_MASK bit l), the RNE | |
| // bf16 typecast packed untilized into CB_OUT. The layer flags are runtime branches on compile-time masks; the | |
| // arithmetic of each layer is K6's, so the outputs are bit-identical to the K6 chain. | |
| // CT args: [0] per-core RT-arg count (P3) [1] K [2] CIN_T [3] COUT_T [4] NCH [5] TG [6] NBLK [7] NC [8] RES_MASK | |
| // [9] WY_MASK [10] RELU_MASK [11] NL. Per-core RT args: [me]. | |
| using namespace ckernel; | |
| constexpr uint32_t K = get_compile_time_arg_val(1); | |
| constexpr uint32_t CIN_T = get_compile_time_arg_val(2); | |
| constexpr uint32_t COUT_T = get_compile_time_arg_val(3); | |
| constexpr uint32_t NCH = get_compile_time_arg_val(4); | |
| constexpr uint32_t TG = get_compile_time_arg_val(5); | |
| constexpr uint32_t NBLK = get_compile_time_arg_val(6); | |
| constexpr uint32_t NC = get_compile_time_arg_val(7); | |
| constexpr uint32_t RES_MASK = get_compile_time_arg_val(8); | |
| constexpr uint32_t WY_MASK = get_compile_time_arg_val(9); | |
| constexpr uint32_t RELU_MASK = get_compile_time_arg_val(10); | |
| constexpr uint32_t NL = get_compile_time_arg_val(11); | |
| constexpr uint32_t CB_GATH = 0, CB_W = 1, CB_BIAS = 2, CB_IDENT = 3, CB_META = 4, CB_TL = 5, CB_ACC = 6; | |
| constexpr uint32_t CB_GATH2 = 11, CB_OUT = 16, CB_Y = 17; | |
| constexpr uint32_t WCH = TG * CIN_T * COUT_T; | |
| constexpr uint32_t TLN = TG * CIN_T; | |
| // One layer with its residual / y flags as template parameters: the K6 kernel's if-constexpr code per layer (a | |
| // runtime-branch form measured the same; only the combinations some layer uses are instantiated). ReLU is all | |
| // layers or none (RELU_MASK, checked by the host). | |
| constexpr bool relu = RELU_MASK != 0; | |
| constexpr bool uses(bool r, bool w) { | |
| for (uint32_t l = 0; l < NL; ++l) { | |
| if ((((RES_MASK >> l) & 1u) != 0) == r && (((WY_MASK >> l) & 1u) != 0) == w) { | |
| return true; | |
| } | |
| } | |
| return false; | |
| } | |
| template <bool has_res, bool wy> | |
| inline void layer(uint32_t me) { | |
| for (uint32_t ch = 0; ch < NCH; ++ch) { | |
| const uint32_t t0 = ch * TG; | |
| const uint32_t nt = (K - t0) < TG ? (K - t0) : TG; | |
| const bool last = ch == NCH - 1; | |
| cb_wait_front(CB_W, WCH); | |
| for (uint32_t b = me; b < NBLK; b += NC) { | |
| cb_wait_front(CB_META, 1); | |
| UNPACK(asm volatile("fence")); | |
| const uint32_t mask = read_tile_value(CB_META, 0, 0); | |
| const uint32_t live = read_tile_value(CB_META, 0, 1); | |
| cb_pop_front(CB_META, 1); | |
| if (!live) { | |
| continue; | |
| } | |
| cb_reserve_back(CB_TL, TLN); | |
| if (mask) { | |
| reconfig_data_format_srca(CB_GATH); | |
| pack_reconfig_data_format(CB_TL); | |
| tilize_init(CB_GATH, CIN_T, CB_TL); | |
| uint32_t k = 0; | |
| for (uint32_t p = 0; p < nt; ++p) { | |
| if (mask & (1u << p)) { | |
| const uint32_t cb = (k & 1) ? CB_GATH2 : CB_GATH; | |
| ++k; | |
| cb_wait_front(cb, CIN_T); | |
| tilize_block(cb, CIN_T, CB_TL, 0, p * CIN_T); | |
| cb_pop_front(cb, CIN_T); | |
| } | |
| } | |
| tilize_uninit(CB_GATH, CB_TL); | |
| } | |
| cb_push_back(CB_TL, TLN); | |
| cb_wait_front(CB_TL, TLN); | |
| if (last) { | |
| cb_reserve_back(CB_OUT, COUT_T); | |
| if constexpr (has_res) { | |
| cb_wait_front(CB_IDENT, COUT_T); | |
| } | |
| } | |
| for (uint32_t n = 0; n < COUT_T; ++n) { | |
| tile_regs_acquire(); | |
| if (ch > 0) { | |
| reconfig_data_format_srca(CB_ACC); | |
| copy_init(CB_ACC); | |
| cb_wait_front(CB_ACC, 1); | |
| copy_tile(CB_ACC, 0, 0); | |
| cb_pop_front(CB_ACC, 1); | |
| } else { | |
| fill_tile_init(); | |
| fill_tile(0, 0.0f); | |
| } | |
| reconfig_data_format_srca(CB_W); | |
| reconfig_data_format_srcb(CB_TL); | |
| matmul_init(CB_TL, CB_W); | |
| for (uint32_t p = 0; p < nt; ++p) { | |
| if (mask & (1u << p)) { | |
| for (uint32_t c = 0; c < CIN_T; ++c) { | |
| matmul_tiles(CB_TL, CB_W, p * CIN_T + c, (p * CIN_T + c) * COUT_T + n, 0); | |
| } | |
| } | |
| } | |
| if (!last) { | |
| tile_regs_commit(); | |
| cb_reserve_back(CB_ACC, 1); | |
| tile_regs_wait(); | |
| pack_reconfig_data_format(CB_ACC); | |
| pack_tile(0, CB_ACC); | |
| tile_regs_release(); | |
| cb_push_back(CB_ACC, 1); | |
| continue; | |
| } | |
| reconfig_data_format_srca(CB_BIAS); | |
| copy_init(CB_BIAS); | |
| copy_tile(CB_BIAS, n, 1); | |
| add_binary_tile_init(); | |
| add_binary_tile<ckernel::DstRoundingMode::NearestEven>(0, 1, 0); | |
| if constexpr (has_res) { | |
| reconfig_data_format_srca(CB_IDENT); | |
| copy_init(CB_IDENT); | |
| copy_tile(CB_IDENT, n, 1); | |
| add_binary_tile_init(); | |
| add_binary_tile<ckernel::DstRoundingMode::NearestEven>(0, 1, 0); | |
| } | |
| if constexpr (relu) { | |
| relu_tile_init(); | |
| relu_tile(0); | |
| } | |
| if constexpr (wy) { | |
| copy_dest_values_init(); | |
| copy_dest_values<DataFormat::Float32>(0, 2); | |
| } | |
| typecast_tile_init<(uint32_t)DataFormat::Float32, (uint32_t)DataFormat::Float16_b>(); | |
| typecast_tile<(uint32_t)DataFormat::Float32, (uint32_t)DataFormat::Float16_b>(0); | |
| tile_regs_commit(); | |
| if constexpr (wy) { | |
| cb_reserve_back(CB_Y, 1); | |
| } | |
| tile_regs_wait(); | |
| if constexpr (wy) { | |
| pack_reconfig_data_format(CB_Y); | |
| pack_tile(2, CB_Y); | |
| } | |
| pack_reconfig_data_format(CB_OUT); | |
| pack_untilize_dest_init<1, COUT_T, false, TILE_C_DIM, false, false>(CB_OUT); | |
| pack_untilize_dest<1, COUT_T>(CB_OUT, 1, n); | |
| pack_untilize_uninit(CB_OUT); | |
| tile_regs_release(); | |
| if constexpr (wy) { | |
| cb_push_back(CB_Y, 1); | |
| } | |
| } | |
| if (last) { | |
| cb_push_back(CB_OUT, COUT_T); | |
| if constexpr (has_res) { | |
| cb_pop_front(CB_IDENT, COUT_T); | |
| } | |
| } | |
| cb_pop_front(CB_TL, TLN); | |
| } | |
| cb_pop_front(CB_W, WCH); | |
| } | |
| } | |
| void kernel_main() { | |
| const uint32_t me = get_arg_val<uint32_t>(0); | |
| compute_kernel_hw_startup<SrcOrder::Reverse>(CB_TL, CB_W, CB_OUT); | |
| pack_untilize_dest_init<1, COUT_T>(CB_OUT); | |
| pack_untilize_uninit(CB_OUT); | |
| for (uint32_t l = 0; l < NL; ++l) { | |
| MKS_ZONE("MKS-C-LAYER"); | |
| const bool r = (RES_MASK >> l) & 1u; | |
| const bool w = (WY_MASK >> l) & 1u; | |
| cb_wait_front(CB_BIAS, COUT_T); | |
| // only the (residual, y) combinations some layer uses are instantiated (TRISC code size) | |
| if constexpr (uses(true, true)) { | |
| if (r && w) { layer<true, true>(me); } | |
| } | |
| if constexpr (uses(true, false)) { | |
| if (r && !w) { layer<true, false>(me); } | |
| } | |
| if constexpr (uses(false, true)) { | |
| if (!r && w) { layer<false, true>(me); } | |
| } | |
| if constexpr (uses(false, false)) { | |
| if (!r && !w) { layer<false, false>(me); } | |
| } | |
| cb_pop_front(CB_BIAS, COUT_T); | |
| } | |
| } | |