Download code/tt_bevformer/tt/kernels/mmres_k_compute.cpp from changh95/bevformer-p150: direct link, hf CLI and curl.
- Browser
- Download file 11.6 kB
-
https://huggingface.co/changh95/bevformer-p150/resolve/main/code/tt_bevformer/tt/kernels/mmres_k_compute.cpp
- Command line
-
hf download hf://changh95/bevformer-p150/code/tt_bevformer/tt/kernels/mmres_k_compute.cpp
-
curl -L -o mmres_k_compute.cpp https://huggingface.co/changh95/bevformer-p150/resolve/main/code/tt_bevformer/tt/kernels/mmres_k_compute.cpp
11.6 kB
| // SPDX-License-Identifier: Apache-2.0 | |
| // mmres_k (tt/mmres_k.py), compute. Per output tile row (Nt tiles, in 4-tile subblocks: fp32 DEST, half sync): | |
| // [MODE 1] r = bf16(x @ W2 + b2) (the stride-1 downsample, the same steps as the main product) | |
| // y = bf16(x @ W + b): for each K block of KB tiles, DEST += a[k] * W[k] (matmul_block, HiFi4), packed to the fp32 | |
| // partials (packer L1 accumulation from the second block on), then partials + bias row (row broadcast, FPU) | |
| // packed to bf16: the step sequence of ttnn's bmm_large_block_zm_fused_bias_activation (FUSE_BIAS, | |
| // PACKER_L1_ACC, FP32_DEST_ACC_EN, in0_block_w = KB) for one output tile, so y is bit-identical to ttnn.linear | |
| // with that config; | |
| // out = relu(y + r) packed to bf16 (FPU add, packer ReLU: binary_ng's add with a fused RELU). | |
| constexpr uint32_t KT = get_compile_time_arg_val(0); | |
| constexpr uint32_t NT = get_compile_time_arg_val(1); | |
| constexpr uint32_t MT = get_compile_time_arg_val(2); | |
| constexpr uint32_t NC = get_compile_time_arg_val(3); | |
| constexpr uint32_t GX = get_compile_time_arg_val(4); | |
| constexpr uint32_t MODE = get_compile_time_arg_val(5); | |
| constexpr uint32_t K2T = get_compile_time_arg_val(6); | |
| constexpr uint32_t RB = get_compile_time_arg_val(7); | |
| constexpr uint32_t KB = get_compile_time_arg_val(8); // in0_block_w of the replicated ttnn config | |
| constexpr uint32_t NEXT = get_compile_time_arg_val(9); // 1: also the next block's conv1 relu(out @ Wn + bn) | |
| constexpr uint32_t NNT = get_compile_time_arg_val(10); // its N tiles | |
| constexpr uint32_t NG = get_compile_time_arg_val(11); // N groups: core group g computes the N tiles [g NT, g NT + NT) | |
| constexpr uint32_t KBN = get_compile_time_arg_val(12); // K block of the next conv1 (BEVFORMER_MMRES_KB, D20) | |
| constexpr uint32_t cb_a = 0, cb_w = 1, cb_b = 2, cb_r = 3, cb_w2 = 5, cb_b2 = 6, cb_wn = 7, cb_bn = 8, cb_out = 16, | |
| cb_out2 = 17; | |
| constexpr uint32_t cb_part = 24, cb_mm = 25, cb_res = 26, cb_y = 27, cb_part2 = 28; | |
| constexpr uint32_t SB = 4; | |
| // BEVFORMER_BACKBONE_S2=prod: only the tile rows with a pixel on an even image row (y % 2 == 0 of the N x SUB_H x | |
| // SUB_W map) are computed / read / written (the others feed no even pixel: the output is the even pixels' map) | |
| static inline bool sub2_need(uint32_t m) { | |
| const uint32_t ya = (m * 32) / SUB_W, yb = (m * 32 + 31) / SUB_W; | |
| for (uint32_t t = ya; t <= yb; ++t) { | |
| if (((t % SUB_H) & 1) == 0) { | |
| return true; | |
| } | |
| } | |
| return false; | |
| } | |
| // tiles per DEST section (fp32) | |
| static_assert(NT % SB == 0, "mmres_k: N a multiple of 128"); | |
| static_assert(NEXT == 0 || (NNT % 2 == 0 && NT % KBN == 0), "mmres_k: next conv1 N a multiple of 64"); | |
| static_assert(KT % KB == 0 && (MODE == 0 || K2T % KB == 0), "mmres_k: K a multiple of the K block"); | |
| // y[row] = bf16(in[row] @ W + b) [+ packer ReLU] -> dst (tiles dst_off .. dst_off + N - 1 of a reserved region); | |
| // N in subblocks of SB tiles (or 2 when N is not a multiple of SB) | |
| // (each partials CB holds exactly N tiles, so its ring pointer returns to the start after every row: the out-of-order | |
| // packs index from it) | |
| template <uint32_t K, uint32_t N = NT, bool RELU = false, uint32_t CBP = cb_part, uint32_t KBL = KB> | |
| inline void mm_bias_row(uint32_t cb_in, uint32_t in_off, uint32_t cbw, uint32_t cbb, uint32_t dst, uint32_t dst_off) { | |
| constexpr uint32_t S_ = N % SB == 0 ? SB : 2; | |
| // ---- matmul, K blocks with packer L1 accumulation into the fp32 partials | |
| reconfig_data_format_srca(cbw); | |
| reconfig_data_format_srcb(cb_in); | |
| matmul_block_init(cb_in, cbw, 0, S_, 1, K); | |
| pack_reconfig_data_format(CBP); | |
| cb_reserve_back(CBP, N); | |
| for (uint32_t n0 = 0; n0 < N; n0 += S_) { | |
| for (uint32_t kb = 0; kb < K; kb += KBL) { | |
| tile_regs_acquire(); | |
| for (uint32_t k = kb; k < kb + KBL; ++k) { | |
| matmul_block(cb_in, cbw, in_off + k, k * N + n0, 0, 0, S_, 1, K); | |
| } | |
| tile_regs_commit(); | |
| tile_regs_wait(); | |
| pack_reconfig_l1_acc(kb == 0 ? 0 : 1); | |
| for (uint32_t i = 0; i < S_; ++i) { | |
| pack_tile<true>(i, CBP, n0 + i); | |
| } | |
| tile_regs_release(); | |
| } | |
| } | |
| pack_reconfig_l1_acc(0); | |
| cb_push_back(CBP, N); | |
| // ---- bias (row broadcast) -> bf16 | |
| cb_wait_front(CBP, N); | |
| reconfig_data_format_srca(CBP); | |
| reconfig_data_format_srcb(cbb); | |
| add_bcast_rows_init(CBP, cbb); | |
| pack_reconfig_data_format(dst); | |
| if constexpr (RELU) { | |
| pack_relu_config(ReluConfig::zero()); | |
| } | |
| for (uint32_t n0 = 0; n0 < N; n0 += S_) { | |
| tile_regs_acquire(); | |
| for (uint32_t i = 0; i < S_; ++i) { | |
| add_tiles_bcast_rows(CBP, cbb, n0 + i, n0 + i, i); | |
| } | |
| tile_regs_commit(); | |
| tile_regs_wait(); | |
| for (uint32_t i = 0; i < S_; ++i) { | |
| pack_tile<true>(i, dst, dst_off + n0 + i); | |
| } | |
| tile_regs_release(); | |
| } | |
| if constexpr (RELU) { | |
| pack_relu_config(ReluConfig::none()); | |
| } | |
| cb_pop_front(CBP, N); | |
| } | |
| void kernel_main() { | |
| const uint32_t ci = get_absolute_logical_y() * GX + get_absolute_logical_x(); | |
| if (ci >= NC) { | |
| return; | |
| } | |
| constexpr uint32_t CPG = NC / NG; // cores per N group (rows split over them) | |
| const uint32_t li = ci % CPG; | |
| const uint32_t m0 = li * MT / CPG; | |
| const uint32_t m1 = (li + 1) * MT / CPG; | |
| if (m0 == m1) { | |
| return; | |
| } | |
| compute_kernel_hw_startup<SrcOrder::Reverse>(cb_a, cb_w, cb_part); | |
| cb_wait_front(cb_w, KT * NT); | |
| cb_wait_front(cb_b, NT); | |
| if constexpr (MODE == 1) { | |
| cb_wait_front(cb_w2, K2T * NT); | |
| cb_wait_front(cb_b2, NT); | |
| } | |
| if constexpr (NEXT) { | |
| cb_wait_front(cb_wn, NT * NNT); | |
| cb_wait_front(cb_bn, NNT); | |
| } | |
| constexpr uint32_t RT = MODE == 1 ? K2T : NT; | |
| for (uint32_t m = m0; m < m1; m += RB) { | |
| if (!sub2_need(m)) { | |
| continue; | |
| } | |
| const uint32_t nr = (m + RB <= m1) ? RB : m1 - m; | |
| cb_wait_front(cb_a, RB * KT); | |
| cb_wait_front(cb_r, RB * RT); | |
| cb_reserve_back(cb_out, RB * NT); | |
| if constexpr (NEXT) { | |
| cb_reserve_back(cb_out2, RB * NNT); | |
| } | |
| for (uint32_t i = 0; i < nr; ++i) { | |
| uint32_t res_cb = cb_r, res_off = i * NT; | |
| if constexpr (MODE == 1) { | |
| cb_reserve_back(cb_res, NT); | |
| mm_bias_row<K2T>(cb_r, i * K2T, cb_w2, cb_b2, cb_res, 0); | |
| cb_push_back(cb_res, NT); | |
| cb_wait_front(cb_res, NT); | |
| res_cb = cb_res; | |
| res_off = 0; | |
| } | |
| cb_reserve_back(cb_mm, NT); | |
| mm_bias_row<KT>(cb_a, i * KT, cb_w, cb_b, cb_mm, 0); | |
| cb_push_back(cb_mm, NT); | |
| cb_wait_front(cb_mm, NT); | |
| // ---- out = relu(y + r) | |
| reconfig_data_format_srca(cb_mm); | |
| reconfig_data_format_srcb(res_cb); | |
| add_init(cb_mm, res_cb); | |
| pack_reconfig_data_format(cb_out); | |
| pack_relu_config(ReluConfig::zero()); | |
| if constexpr (NEXT) { | |
| cb_reserve_back(cb_y, NT); | |
| } | |
| for (uint32_t n0 = 0; n0 < NT; n0 += SB) { | |
| tile_regs_acquire(); | |
| for (uint32_t j = 0; j < SB; ++j) { | |
| add_tiles(cb_mm, res_cb, n0 + j, res_off + n0 + j, j); | |
| } | |
| tile_regs_commit(); | |
| tile_regs_wait(); | |
| for (uint32_t j = 0; j < SB; ++j) { | |
| pack_tile<true>(j, cb_out, i * NT + n0 + j); | |
| } | |
| if constexpr (NEXT) { | |
| pack_reconfig_data_format(cb_y); // BEVFORMER_ACT8: out bfp8, the next conv1's input bf16 | |
| for (uint32_t j = 0; j < SB; ++j) { | |
| pack_tile<true>(j, cb_y, n0 + j); | |
| } | |
| pack_reconfig_data_format(cb_out); | |
| } | |
| tile_regs_release(); | |
| } | |
| pack_relu_config(ReluConfig::none()); | |
| if constexpr (NEXT) { | |
| // the next block's conv1 on the row just written: relu(bf16(out @ Wn + bn)) | |
| cb_push_back(cb_y, NT); | |
| cb_wait_front(cb_y, NT); | |
| mm_bias_row<NT, NNT, true, cb_part2, KBN>(cb_y, 0, cb_wn, cb_bn, cb_out2, i * NNT); | |
| cb_pop_front(cb_y, NT); | |
| } | |
| cb_pop_front(cb_mm, NT); | |
| if constexpr (MODE == 1) { | |
| cb_pop_front(cb_res, NT); | |
| } | |
| } | |
| cb_push_back(cb_out, RB * NT); | |
| // BEVFORMER_BACKBONE_S2=prod (D20 round 3): the output row untilized here (pack_untilize, 4-tile blocks of | |
| // the NT-tile row) -> cb 19; the writer stores the even pixels' sticks of the next stage's strided input | |
| { | |
| static_assert(RB == 1 && NT % 4 == 0 && NEXT == 0, "mmres_k: the sub2 output needs RB 1, NT % 4 == 0"); | |
| constexpr uint32_t cb_orm = 19; | |
| cb_reserve_back(cb_orm, NT); | |
| reconfig_data_format_srca(cb_out); | |
| pack_reconfig_data_format(cb_orm); | |
| pack_untilize_init<4, NT>(cb_out, cb_orm); | |
| for (uint32_t c = 0; c < NT / 4; ++c) { | |
| cb_wait_front(cb_out, 4); | |
| pack_untilize_block<4, NT>(cb_out, 1, cb_orm, c); | |
| cb_pop_front(cb_out, 4); | |
| } | |
| pack_untilize_uninit(cb_orm); | |
| cb_push_back(cb_orm, NT); | |
| } | |
| if constexpr (NEXT) { | |
| cb_push_back(cb_out2, RB * NNT); | |
| // BEVFORMER_RES3_RM (D20 round 2): the next conv1's row block untilized here (pack_untilize, 4-tile | |
| // blocks of the NNT-tile row) -> cb 18; the writer stores its 32 sticks in the ROW_MAJOR L1 map that the | |
| // DCN's offset conv and dcn_k read (no separate untilize program, no DRAM round trip of the map) | |
| static_assert(RB == 1 && NNT % 4 == 0, "mmres_k: the row-major next conv1 needs RB 1, NNT % 4 == 0"); | |
| constexpr uint32_t cb_o2rm = 18; | |
| cb_reserve_back(cb_o2rm, NNT); | |
| reconfig_data_format_srca(cb_out2); | |
| pack_reconfig_data_format(cb_o2rm); | |
| pack_untilize_init<4, NNT>(cb_out2, cb_o2rm); | |
| for (uint32_t c = 0; c < NNT / 4; ++c) { | |
| cb_wait_front(cb_out2, 4); | |
| pack_untilize_block<4, NNT>(cb_out2, 1, cb_o2rm, c); | |
| cb_pop_front(cb_out2, 4); | |
| } | |
| pack_untilize_uninit(cb_o2rm); | |
| cb_push_back(cb_o2rm, NNT); | |
| } | |
| cb_pop_front(cb_a, RB * KT); | |
| cb_pop_front(cb_r, RB * RT); | |
| } | |
| cb_pop_front(cb_w, KT * NT); | |
| cb_pop_front(cb_b, NT); | |
| if constexpr (NEXT) { | |
| cb_pop_front(cb_wn, NT * NNT); | |
| cb_pop_front(cb_bn, NNT); | |
| } | |
| if constexpr (MODE == 1) { | |
| cb_pop_front(cb_w2, K2T * NT); | |
| cb_pop_front(cb_b2, NT); | |
| } | |
| } | |