Download code/tt_autoe2e/tt/kernels/msda_cm_compute.cpp from changh95/auto_e2e-p150: direct link, hf CLI and curl.
- Browser
- Download file 15.4 kB
-
https://huggingface.co/changh95/auto_e2e-p150/resolve/main/code/tt_autoe2e/tt/kernels/msda_cm_compute.cpp
- Command line
-
hf download hf://changh95/auto_e2e-p150/code/tt_autoe2e/tt/kernels/msda_cm_compute.cpp
-
curl -L -o msda_cm_compute.cpp https://huggingface.co/changh95/auto_e2e-p150/resolve/main/code/tt_autoe2e/tt/kernels/msda_cm_compute.cpp
15.4 kB
| // SPDX-License-Identifier: Apache-2.0 | |
| // msda_cm (tt/msda_cm.py), compute: per work item (32 cells x one head; see msda_cm_reader.cpp) the chunk count from | |
| // cb_ctl (read_tile_value: the same value on the three TRISCs), then per chunk (slot, level): | |
| // 1. for each half h (the points of RISC h): DEST0..2 <- cb_geo_h (fx, fy, attn; fp32); SFPU: W_c = a * wx * wy for | |
| // the 4 corners (fx / fy decoded from 2^23 + q) -> cb_w pages 4h .. 4h + 3 (fp32; own point q in column 4 q); | |
| // 2. the [32 x 128] quad staging blocks tilized into the 4 corner tiles per point; | |
| // 3. for each (point q, corner c): DEST = V * (column 4 q of W_{h,c} broadcast over the 32 channels: an FPU | |
| // multiply with the weight tile read 16 q bytes on), packer L1 accumulation into cb_acc (fp32) over ALL chunks | |
| // of the item (slots and levels); | |
| // then cb_acc -> bf16 -> cb_out. | |
| namespace ckernel::sfpu { | |
| // DEST tiles 0, 1, 2 = fx, fy (as 2^23 + q), attn -> tiles 0..3 = the NW, NE, SW, SE weights (one face per call) | |
| inline void msda_cm_w_face() { | |
| for (int d = 0; d < 8; d++) { | |
| sfpi::vFloat fx = (sfpi::dst_reg[0] - 8388608.0f) * 0.0000152587890625f; | |
| sfpi::vFloat fy = (sfpi::dst_reg[32] - 8388608.0f) * 0.0000152587890625f; | |
| sfpi::vFloat a = sfpi::dst_reg[64]; | |
| sfpi::vFloat gx = 1.0f - fx; | |
| sfpi::vFloat ay0 = a * (1.0f - fy); | |
| sfpi::vFloat ay1 = a * fy; | |
| sfpi::dst_reg[0] = gx * ay0; | |
| sfpi::dst_reg[32] = fx * ay0; | |
| sfpi::dst_reg[64] = gx * ay1; | |
| sfpi::dst_reg[96] = fx * ay1; | |
| sfpi::dst_reg++; | |
| } | |
| } | |
| } // namespace ckernel::sfpu | |
| // llk_unpack_AB<COL> with srcB read ofs16 x 16 bytes past the tile: the 4 fp32 weight columns 0, 4, 8, 12 of a | |
| // corner tile each become column 0 of the unpacked faces (the column the FPU broadcasts) | |
| inline void unpack_ab_col_ofs(uint32_t cba, uint32_t cbb, uint32_t ta, uint32_t tb, uint32_t ofs16) { | |
| const uint32_t ida = get_operand_id(cba); | |
| const uint32_t idb = get_operand_id(cbb); | |
| const uint32_t aa = get_local_cb_interface(ida).fifo_rd_ptr - 1 + get_local_cb_interface(ida).fifo_page_size * ta; | |
| const uint32_t ab = | |
| get_local_cb_interface(idb).fifo_rd_ptr - 1 + get_local_cb_interface(idb).fifo_page_size * tb + ofs16; | |
| llk_unpack_AB_impl<BroadcastType::COL>(aa, ab, 0, unpack_src_format[idb]); | |
| } | |
| constexpr uint32_t NWORK = get_compile_time_arg_val(0); | |
| constexpr uint32_t NC = get_compile_time_arg_val(1); | |
| constexpr uint32_t GX = get_compile_time_arg_val(2); | |
| constexpr uint32_t P = 8; | |
| constexpr uint32_t PH = P / 2; | |
| constexpr uint32_t NS = PH * 4; | |
| constexpr uint32_t BW = 4; // tiles per quad staging block | |
| constexpr uint32_t cb_in0 = 0, cb_in1 = 1, cb_geo0 = 2, cb_geo1 = 3, cb_w = 5, cb_acc = 6, cb_ctl = 9, | |
| cb_out = 16, cb_tl0 = 19, cb_tl1 = 20; | |
| namespace ckl = compute_kernel_lib; | |
| // the per-item conversions (msda_cm_reader.cpp): the raw sampling projection tiles -> int32 (Q16.16; the same SFPU | |
| // typecast as ttnn.typecast), the logit tile -> softmax over the head's 32 columns (the LLK sequence of | |
| // ttnn.softmax(numeric_stable) on one fp32 tile, as msda_k_compute.cpp: row max -> exp(x - max) -> row sum -> 1 / sum | |
| // -> x * (1 / sum)), packed once per reader RISC | |
| constexpr uint32_t cb_scmax = 10, cb_scsum = 11, cb_lg = 12, cb_max = 13, cb_exps = 14, cb_rsum = 15, cb_raw0 = 17, | |
| cb_raw1 = 18, cb_off0 = 21, cb_off1 = 22, cb_sm0 = 23, cb_sm1 = 24; | |
| static inline void convert_item() { | |
| for (uint32_t h = 0; h < 2; ++h) { | |
| const uint32_t cr = h ? cb_raw1 : cb_raw0; | |
| const uint32_t co = h ? cb_off1 : cb_off0; | |
| cb_wait_front(cr, 1); | |
| cb_reserve_back(co, 1); | |
| tile_regs_acquire(); | |
| reconfig_data_format_srca(cr); | |
| copy_init(cr); | |
| copy_tile(cr, 0, 0); | |
| typecast_tile_init<static_cast<uint32_t>(DataFormat::Float32), static_cast<uint32_t>(DataFormat::Int32)>(); | |
| typecast_tile<static_cast<uint32_t>(DataFormat::Float32), static_cast<uint32_t>(DataFormat::Int32)>(0); | |
| tile_regs_commit(); | |
| tile_regs_wait(); | |
| pack_reconfig_data_format(co); | |
| pack_tile(0, co); | |
| tile_regs_release(); | |
| cb_push_back(co, 1); | |
| cb_pop_front(cr, 1); | |
| } | |
| reconfig_data_format(cb_lg, cb_lg); | |
| pack_reconfig_data_format(cb_exps); | |
| copy_init(cb_lg); | |
| compute_kernel_lib::reduce<PoolType::MAX, ReduceDim::REDUCE_ROW, cb_lg, cb_scmax, cb_max, | |
| compute_kernel_lib::ReduceInputPolicy::WaitUpfrontNoPop, | |
| compute_kernel_lib::ReduceDataFormatReconfigMode::INPUT>( | |
| compute_kernel_lib::ReduceInputBlockShape::row(1)); | |
| ckl::eltwise_chain( | |
| ckl::IterationShape::tiles(1).block_size(1), | |
| ckl::BinaryFpu<ckl::BinaryFpuOp::Sub, | |
| ckl::input(cb_lg, ckl::WaitPolicy::Upfront, ckl::PopPolicy::AtEnd, ckl::InputTileMapping::Block, | |
| ckl::DataFormatReconfig::Disabled), | |
| ckl::input(cb_max, ckl::BroadcastDim::Col, ckl::WaitPolicy::Upfront, ckl::PopPolicy::AtEnd)>{}, | |
| ckl::Exp<static_cast<ckl::Approx>(0), ckl::Dst::D0>{}, | |
| ckl::PackTile<ckl::output(cb_exps, ckl::ReservePolicy::PerBlockSize, ckl::PushPolicy::PerBlockSize, | |
| ckl::DataFormatReconfig::Disabled)>{}); | |
| cb_wait_front(cb_exps, 1); | |
| compute_kernel_lib::reduce<PoolType::SUM, ReduceDim::REDUCE_ROW, cb_exps, cb_scsum, cb_rsum, | |
| compute_kernel_lib::ReduceInputPolicy::WaitUpfrontNoPop>( | |
| compute_kernel_lib::ReduceInputBlockShape::row(1), compute_kernel_lib::ReduceInputMemoryLayout::contiguous(), | |
| compute_kernel_lib::NoAccumulation{}, [](uint32_t) { | |
| recip_tile_init<ReciprocalDestAcc::FP32, ReciprocalApproxMode::Precise>(); | |
| recip_tile<ReciprocalDestAcc::FP32, ReciprocalApproxMode::Precise>(0); | |
| }); | |
| cb_wait_front(cb_rsum, 1); | |
| cb_reserve_back(cb_sm0, 1); | |
| cb_reserve_back(cb_sm1, 1); | |
| tile_regs_acquire(); | |
| reconfig_data_format(cb_exps, cb_rsum); | |
| mul_bcast_cols_init(cb_exps, cb_rsum); | |
| mul_tiles_bcast_cols(cb_exps, cb_rsum, 0, 0, 0); | |
| tile_regs_commit(); | |
| tile_regs_wait(); | |
| pack_reconfig_data_format(cb_sm0); | |
| pack_tile(0, cb_sm0); | |
| pack_tile(0, cb_sm1); | |
| tile_regs_release(); | |
| cb_push_back(cb_sm0, 1); | |
| cb_push_back(cb_sm1, 1); | |
| cb_pop_front(cb_exps, 1); | |
| cb_pop_front(cb_rsum, 1); | |
| } | |
| void kernel_main() { | |
| const uint32_t ci = get_absolute_logical_y() * GX + get_absolute_logical_x(); | |
| if (ci >= NC || ci >= NWORK) { | |
| return; | |
| } | |
| compute_kernel_hw_startup(cb_lg, cb_scmax, cb_exps); | |
| cb_wait_front(cb_scmax, 1); | |
| cb_wait_front(cb_scsum, 1); | |
| convert_item(); // the first item; then one item ahead | |
| uint32_t t_geo = 0, t_w = 0, t_tl = 0, t_st = 0, t_fin = 0, t_acq = 0, t_pw = 0, t0, t1; | |
| for (uint32_t w = ci; w < NWORK; w += NC) { | |
| if (w + NC < NWORK) { | |
| convert_item(); | |
| } | |
| cb_wait_front(cb_ctl, 1); | |
| const uint32_t nch = read_tile_value(cb_ctl, 0, 0); | |
| cb_pop_front(cb_ctl, 1); | |
| cb_reserve_back(cb_acc, 1); | |
| for (uint32_t ch = 0; ch < nch; ++ch) { | |
| t1 = TNOW(); | |
| // ---- 1. corner weights per half (exact fp32 copies into DEST: an FPU add would round the 2^23 + q | |
| // fractions to tf32), the SFPU on the left faces only (the point columns 0..7) | |
| // probe: consume the chunk's inputs without any work | |
| for (uint32_t h = 0; h < 2; ++h) { | |
| cb_wait_front(h ? cb_geo1 : cb_geo0, 3); | |
| cb_pop_front(h ? cb_geo1 : cb_geo0, 3); | |
| cb_wait_front(h ? cb_in1 : cb_in0, NS); | |
| cb_pop_front(h ? cb_in1 : cb_in0, NS); | |
| } | |
| continue; | |
| cb_reserve_back(cb_w, 8); | |
| for (uint32_t h = 0; h < 2; ++h) { | |
| cb_wait_front(h ? cb_geo1 : cb_geo0, 3); | |
| cb_pop_front(h ? cb_geo1 : cb_geo0, 3); | |
| } | |
| for (uint32_t h = 0; h < 2; ++h) { | |
| const uint32_t cg = h ? cb_geo1 : cb_geo0; | |
| t0 = TNOW(); | |
| cb_wait_front(cg, 3); | |
| t_geo += TNOW() - t0; | |
| tile_regs_acquire(); | |
| reconfig_data_format_srca(cg); | |
| copy_init(cg); | |
| copy_tile(cg, 0, 0); | |
| copy_tile(cg, 1, 1); | |
| copy_tile(cg, 2, 2); | |
| MATH((SFPU_UNARY_INIT(unused))); | |
| MATH((SFPU_UNARY_CALL_NO_TEMPLATE_ARGS(DST_SYNC_MODE, DST_ACCUM_MODE, msda_cm_w_face, 0, | |
| VectorMode::C))); | |
| tile_regs_commit(); | |
| tile_regs_wait(); | |
| pack_reconfig_data_format(cb_w); | |
| for (uint32_t c = 0; c < 4; ++c) { | |
| pack_tile<true>(c, cb_w, h * 4 + c); | |
| } | |
| tile_regs_release(); | |
| cb_pop_front(cg, 3); | |
| } | |
| cb_push_back(cb_w, 8); | |
| cb_wait_front(cb_w, 8); | |
| t0 = TNOW(); | |
| t_w += t0 - t1; | |
| // ---- 2. quad staging blocks -> corner tiles | |
| for (uint32_t h = 0; h < 2; ++h) { | |
| const uint32_t cin = h ? cb_in1 : cb_in0; | |
| const uint32_t ct = h ? cb_tl1 : cb_tl0; | |
| cb_wait_front(cin, NS); | |
| cb_reserve_back(ct, NS); | |
| cb_push_back(ct, NS); | |
| cb_pop_front(cin, NS); | |
| continue; | |
| reconfig_data_format_srca(cin); | |
| pack_reconfig_data_format(ct); | |
| fast_tilize_init(cin, BW, ct); | |
| for (uint32_t bk = 0; bk < NS / BW; ++bk) { | |
| fast_tilize_block(cin, BW, ct, BW * bk, BW * bk); | |
| } | |
| fast_tilize_uninit(cin, ct, BW); | |
| cb_push_back(ct, NS); | |
| cb_pop_front(cin, NS); | |
| } | |
| t1 = TNOW(); | |
| t_tl += t1 - t0; | |
| // ---- 3. the 4 PH (point, corner) steps of each half, accumulated over the item's chunks | |
| reconfig_data_format(cb_tl0, cb_w); | |
| pack_reconfig_data_format(cb_acc); | |
| for (uint32_t h = 0; h < 2; ++h) { | |
| const uint32_t ci_ = h ? cb_tl1 : cb_tl0; | |
| cb_wait_front(ci_, NS); | |
| for (uint32_t k = 0; k < 4; k += 4) { | |
| for (uint32_t k = 0; k < NS; k += 4) { | |
| uint32_t ta = TNOW(); | |
| tile_regs_acquire(); | |
| t_acq += TNOW() - ta; | |
| // DEST[c] = V_{q, c} * (W_{h, c} column 4 q broadcast over the 32 channels): one FPU product per | |
| // (point, corner); the weight tf32 in srcB, as the earlier broadcast matmul gave it (bit for bit) | |
| if (k == 0) { | |
| mul_bcast_cols_init(ci_, cb_w); | |
| } | |
| for (uint32_t c = 0; c < 4; ++c) { | |
| MATH((llk_math_eltwise_binary<EltwiseBinaryType::ELWMUL, BroadcastType::COL, | |
| DST_ACCUM_MODE, MATH_FIDELITY, | |
| EltwiseBinaryReuseDestType::NONE>(ci_, cb_w, c, true))); | |
| UNPACK((unpack_ab_col_ofs(ci_, cb_w, k + c, h * 4 + c, k >> 2))); | |
| } | |
| tile_regs_commit(); | |
| ta = TNOW(); | |
| tile_regs_wait(); | |
| t_pw += TNOW() - ta; | |
| for (uint32_t c = 0; c < 4; ++c) { | |
| pack_reconfig_l1_acc((ch == 0 && h == 0 && k == 0 && c == 0) ? 0 : 1); | |
| pack_tile<true>(c, cb_acc, 0); | |
| } | |
| // L1 accumulation switched only where its state changes (each switch stalls the packer until | |
| // it is idle): off for the item's very first pack, on for the chunk's steps, off again after | |
| // them (the weight / tilize packs of the next chunk overwrite) | |
| uint32_t c0 = 0; | |
| if (h == 0 && k == 0) { | |
| if (ch == 0) { | |
| pack_tile<true>(0, cb_acc, 0); | |
| c0 = 1; | |
| } | |
| pack_reconfig_l1_acc(1); | |
| } | |
| for (uint32_t c = c0; c < 1; ++c) { | |
| for (uint32_t c = c0; c < 4; ++c) { | |
| pack_tile<true>(c, cb_acc, 0); | |
| } | |
| tile_regs_release(); | |
| } | |
| cb_pop_front(ci_, NS); | |
| } | |
| pack_reconfig_l1_acc(0); | |
| cb_pop_front(cb_w, 8); | |
| t0 = TNOW(); | |
| t_st += t0 - t1; | |
| } | |
| cb_reserve_back(cb_out, 1); | |
| cb_push_back(cb_out, 1); | |
| continue; | |
| cb_push_back(cb_acc, 1); | |
| // ---- fp32 sum -> bf16 output tile | |
| cb_wait_front(cb_acc, 1); | |
| cb_reserve_back(cb_out, 1); | |
| tile_regs_acquire(); | |
| reconfig_data_format_srca(cb_acc); | |
| copy_init(cb_acc); | |
| copy_tile(cb_acc, 0, 0); | |
| tile_regs_commit(); | |
| tile_regs_wait(); | |
| pack_reconfig_data_format(cb_out); | |
| pack_tile(0, cb_out); | |
| tile_regs_release(); | |
| cb_push_back(cb_out, 1); | |
| cb_pop_front(cb_acc, 1); | |
| } | |
| DeviceTimestampedData("geo", t_geo); | |
| DeviceTimestampedData("w", t_w); | |
| DeviceTimestampedData("tl", t_tl); | |
| DeviceTimestampedData("st", t_st); | |
| DeviceTimestampedData("acq", t_acq); | |
| DeviceTimestampedData("pw", t_pw); | |
| } | |