Download code/tt_rdt2/tt/kernels/pxf_qkv_compute.cpp from changh95/rdt2-base-p150: direct link, hf CLI and curl.
- Browser
- Download file 4.98 kB
-
https://huggingface.co/changh95/rdt2-base-p150/resolve/main/code/tt_rdt2/tt/kernels/pxf_qkv_compute.cpp
- Command line
-
hf download hf://changh95/rdt2-base-p150/code/tt_rdt2/tt/kernels/pxf_qkv_compute.cpp
-
curl -L -o pxf_qkv_compute.cpp https://huggingface.co/changh95/rdt2-base-p150/resolve/main/code/tt_rdt2/tt/kernels/pxf_qkv_compute.cpp
4.98 kB
| // SPDX-License-Identifier: Apache-2.0 | |
| // | |
| // RDT2-FM fused PREFIX op "qkvpost", compute: RoPE (HF rotate-half layout) of the q and k heads in fp32 on the SFPU, | |
| // out_t = x_t cos_t - x_{t+2} sin_t, out_{t+2} = x_{t+2} cos_t + x_t sin_t (t = 0, 1: d-tiles of a head) | |
| // q = bf16(out); for k (bias-free projection, lm_kscore "table"): K = bf16(out + T), ks = bf16(out + D) with the | |
| // fp32 host tables T = RoPE_j(b_k), D = T - mean_j T (one rounding each; the projection's bf16 k enters exactly). | |
| // v units never reach this kernel. | |
| using namespace pxf; | |
| namespace { | |
| constexpr uint32_t NQ = get_compile_time_arg_val(QP_CT_NQ); | |
| constexpr uint32_t NKV = get_compile_time_arg_val(QP_CT_NKV); | |
| constexpr uint32_t NH = NQ + 2 * NKV; | |
| constexpr uint32_t P = get_compile_time_arg_val(QP_CT_P); | |
| NOINL void ld(uint32_t cb, uint32_t t, uint32_t d) { | |
| reconfig_data_format_srca(cb); | |
| copy_init(cb); | |
| copy_tile(cb, t, d); | |
| } | |
| NOINL void fadd(uint32_t a, uint32_t b, uint32_t o) { add_binary_tile_init(); add_binary_tile(a, b, o); } | |
| NOINL void fmul(uint32_t a, uint32_t b, uint32_t o) { mul_binary_tile_init(); mul_binary_tile(a, b, o); } | |
| NOINL void pk(uint32_t d, uint32_t cb, uint32_t idx) { | |
| pack_reconfig_data_format(cb); | |
| pack_tile<true>(d, cb, idx); | |
| } | |
| FORCE_INLINE void acq() { tile_regs_acquire(); } | |
| FORCE_INLINE void done() { | |
| tile_regs_commit(); | |
| tile_regs_wait(); | |
| } | |
| FORCE_INLINE void rel() { tile_regs_release(); } | |
| // DST 2 = out_t, DST 4 = out_{t+2} for t in {0, 1} (x_t in DST 0, x_{t+2} in DST 1; cos_t = cos_{t+2} and | |
| // sin_t = sin_{t+2}: out_t = x_t cos_t - x_{t+2} sin_t, out_{t+2} = x_{t+2} cos_t + x_t sin_t) | |
| NOINL void rope_pair(uint32_t t) { | |
| PXF_ZONE("QP_ROPE"); | |
| reconfig_data_format_srca(QP_CB_X); | |
| copy_init(QP_CB_X); | |
| copy_tile(QP_CB_X, t, 0); | |
| copy_tile(QP_CB_X, t + 2, 1); | |
| reconfig_data_format_srca(QP_CB_CS); | |
| copy_init(QP_CB_CS); | |
| copy_tile(QP_CB_CS, t, 2); // cos_t | |
| copy_tile(QP_CB_CS, t, 4); // cos_t (= cos_{t+2}) | |
| copy_tile(QP_CB_CS, 2 + t, 3); // sin_t | |
| copy_tile(QP_CB_CS, 2 + t, 5); // sin_t (= sin_{t+2}) | |
| mul_binary_tile_init(); | |
| mul_binary_tile(0, 2, 2); // x_t cos | |
| mul_binary_tile(1, 3, 3); // x_{t+2} sin | |
| mul_binary_tile(1, 4, 4); // x_{t+2} cos | |
| mul_binary_tile(0, 5, 5); // x_t sin | |
| sub_binary_tile_init(); | |
| sub_binary_tile(2, 3, 2); | |
| add_binary_tile_init(); | |
| add_binary_tile(4, 5, 4); | |
| } | |
| } // namespace | |
| void kernel_main() { | |
| uint32_t r, h0; | |
| qp_share(get_absolute_logical_x(), get_absolute_logical_y(), get_compile_time_arg_val(QP_CT_GX), P, r, h0); | |
| compute_kernel_hw_startup(QP_CB_X, QP_CB_CS, QP_CB_Q); | |
| const bool any = h0 < NQ + NKV; | |
| if (any) { | |
| cb_wait_front(QP_CB_CS, HD_T); | |
| } | |
| for (uint32_t h = h0; h < NQ + NKV; h += P) { | |
| cb_wait_front(QP_CB_X, HD_T); | |
| if (h < NQ) { | |
| cb_reserve_back(QP_CB_Q, HD_T); | |
| for (uint32_t t = 0; t < 2; ++t) { | |
| acq(); | |
| rope_pair(t); | |
| done(); | |
| pk(2, QP_CB_Q, t); | |
| pk(4, QP_CB_Q, t + 2); | |
| rel(); | |
| } | |
| cb_push_back(QP_CB_Q, HD_T); | |
| } else { | |
| cb_wait_front(QP_CB_T, HD_T); | |
| cb_wait_front(QP_CB_D, HD_T); | |
| cb_reserve_back(QP_CB_K, HD_T); | |
| cb_reserve_back(QP_CB_KS, HD_T); | |
| for (uint32_t t = 0; t < 2; ++t) { | |
| PXF_ZONE("QP_KPAIR"); | |
| acq(); | |
| rope_pair(t); | |
| reconfig_data_format_srca(QP_CB_T); | |
| copy_init(QP_CB_T); | |
| copy_tile(QP_CB_T, t, 3); | |
| copy_tile(QP_CB_T, t + 2, 5); | |
| copy_tile(QP_CB_D, t, 6); | |
| copy_tile(QP_CB_D, t + 2, 7); | |
| add_binary_tile_init(); | |
| add_binary_tile(2, 6, 6); // ks_t | |
| add_binary_tile(4, 7, 7); // ks_{t+2} | |
| add_binary_tile(2, 3, 3); // K_t | |
| add_binary_tile(4, 5, 5); // K_{t+2} | |
| done(); | |
| pk(3, QP_CB_K, t); | |
| pk(5, QP_CB_K, t + 2); | |
| pk(6, QP_CB_KS, t); | |
| pk(7, QP_CB_KS, t + 2); | |
| rel(); | |
| } | |
| cb_push_back(QP_CB_K, HD_T); | |
| cb_push_back(QP_CB_KS, HD_T); | |
| cb_pop_front(QP_CB_T, HD_T); | |
| cb_pop_front(QP_CB_D, HD_T); | |
| } | |
| cb_pop_front(QP_CB_X, HD_T); | |
| } | |
| if (any) { | |
| cb_pop_front(QP_CB_CS, HD_T); | |
| } | |
| } | |