@@ -50,6 +50,7 @@ htp_op_code htp_op_remap(const struct ggml_tensor * t) {
5050 case GGML_OP_ROPE : return HTP_OP_ROPE ;
5151 case GGML_OP_REPEAT : return HTP_OP_REPEAT ;
5252 case GGML_OP_CUMSUM : return HTP_OP_CUMSUM ;
53+ case GGML_OP_TOP_K_MASK : return HTP_OP_TOP_K_MASK ;
5354 case GGML_OP_FILL : return HTP_OP_FILL ;
5455 case GGML_OP_DIAG : return HTP_OP_DIAG ;
5556 case GGML_OP_SOLVE_TRI : return HTP_OP_SOLVE_TRI ;
@@ -3402,6 +3403,194 @@ static bool htp_kparams_solve_cumsum(
34023403 return true ;
34033404}
34043405
3406+ // Shrink until the layout fits: halve tile_cols down to 1024 columns, then drop threads. False below the VTCM floor.
3407+ static bool htp_top_k_mask_search (struct htp_top_k_mask_vtcm_layout * L, const struct htp_top_k_mask_shape * S, uint32_t kernel_type,
3408+ size_t vtcm_size, uint32_t * n_threads, uint32_t * tile_cols) {
3409+ const uint32_t min_cols = 1024 ;
3410+ while (true ) {
3411+ htp_top_k_mask_vtcm_layout_build (L, S, kernel_type, *n_threads, *tile_cols);
3412+ if (L->total_bytes <= vtcm_size) {
3413+ return true ;
3414+ }
3415+ if (*tile_cols > min_cols) {
3416+ *tile_cols = (std::max)(min_cols, (uint32_t ) hex_round_up (*tile_cols / 2 , 64 ));
3417+ } else if (*n_threads > 1 ) {
3418+ (*n_threads)--;
3419+ } else {
3420+ return false ;
3421+ }
3422+ }
3423+ }
3424+
3425+ // Row r of t (same ne[1..3] as dst) is at r * nb[1]: the NPU needs one stride per input.
3426+ // A mask broadcasts by i mod ne: its rows are dense above nb[1], the divisors and dst ne[1..2] fit 16 bits.
3427+ static bool htp_top_k_mask_mask_ok (const struct ggml_tensor * m, const struct ggml_tensor * dst) {
3428+ return m->ne [1 ] <= UINT16_MAX && m->ne [2 ] <= UINT16_MAX && m->ne [3 ] <= UINT16_MAX && dst->ne [1 ] <= UINT16_MAX && dst->ne [2 ] <= UINT16_MAX &&
3429+ (m->ne [2 ] == 1 || m->nb [2 ] == m->nb [1 ] * m->ne [1 ]) &&
3430+ (m->ne [3 ] == 1 || m->nb [3 ] == m->nb [1 ] * m->ne [1 ] * m->ne [2 ]);
3431+ }
3432+
3433+ static bool htp_top_k_mask_row_flat (const struct ggml_tensor * t, const struct ggml_tensor * dst) {
3434+ return t->ne [1 ] == dst->ne [1 ] && t->ne [2 ] == dst->ne [2 ] && t->ne [3 ] == dst->ne [3 ] &&
3435+ (t->ne [2 ] == 1 || t->nb [2 ] == t->nb [1 ] * t->ne [1 ]) &&
3436+ (t->ne [3 ] == 1 || t->nb [3 ] == t->nb [1 ] * t->ne [1 ] * t->ne [2 ]);
3437+ }
3438+
3439+ static bool htp_kparams_solve_top_k_mask (
3440+ const struct ggml_hexagon_session * sess,
3441+ const struct ggml_tensor * op,
3442+ struct htp_top_k_mask_kernel_params * kparams
3443+ ) {
3444+ memset (kparams, 0 , sizeof (*kparams));
3445+
3446+ const struct ggml_tensor * mask = op->src [0 ];
3447+ const struct ggml_tensor * top_k = op->src [1 ];
3448+ const struct ggml_tensor * score = op->src [2 ];
3449+ const struct ggml_tensor * blocks = op->src [3 ];
3450+ const struct ggml_tensor * extra = op->src [4 ];
3451+ const struct ggml_tensor * dst = op;
3452+
3453+ if ((mask->type != GGML_TYPE_F16 && mask->type != GGML_TYPE_F32 ) || dst->type != mask->type ) {
3454+ return false ;
3455+ }
3456+ const size_t elem_size = ggml_type_size (dst->type );
3457+ if (mask->nb [0 ] != elem_size || dst->nb [0 ] != elem_size || !ggml_is_contiguous (dst)) {
3458+ return false ;
3459+ }
3460+ if (top_k->type != GGML_TYPE_I32 || top_k->nb [0 ] != 4 ) {
3461+ return false ;
3462+ }
3463+ if (score && (score->type != GGML_TYPE_F32 || score->nb [0 ] != 4 )) {
3464+ return false ;
3465+ }
3466+ if (extra && (extra->type != GGML_TYPE_I32 || extra->nb [0 ] != 4 )) {
3467+ return false ;
3468+ }
3469+ // flat row addressing: inputs follow the dst rows, a mask with more than one row too
3470+ if (!htp_top_k_mask_row_flat (top_k, dst) || (score && !htp_top_k_mask_row_flat (score, dst)) ||
3471+ (extra && !htp_top_k_mask_row_flat (extra, dst)) || (ggml_nrows (mask) > 1 && !htp_top_k_mask_mask_ok (mask, dst))) {
3472+ return false ;
3473+ }
3474+ // the kernel scales block ids by blocks->nb[1] with a 16-bit vector multiply
3475+ if (blocks && (blocks->type != GGML_TYPE_I32 || blocks->nb [0 ] != 4 || blocks->nb [1 ] != (size_t ) blocks->ne [0 ] * 4 || blocks->nb [1 ] > INT16_MAX )) {
3476+ return false ;
3477+ }
3478+
3479+ struct htp_top_k_mask_shape S = {};
3480+ S.n_kv = (uint32_t ) dst->ne [0 ];
3481+ S.elem_size = (uint32_t ) elem_size;
3482+ S.n_top = (uint32_t ) top_k->ne [0 ];
3483+ S.n_blk = blocks ? (uint32_t ) blocks->ne [0 ] : 0 ;
3484+ S.n_pick = (uint32_t ) (blocks ? blocks->ne [1 ] : score ? score->ne [0 ] : dst->ne [0 ]);
3485+ S.n_extra = extra ? (uint32_t ) extra->ne [0 ] : 0 ;
3486+ const uint32_t total_rows = (uint32_t ) ggml_nrows (dst);
3487+ S.has_score = score ? 1 : 0 ;
3488+ S.has_blocks = blocks ? 1 : 0 ;
3489+ S.mask_resident = ggml_nrows (mask) == 1 ? 1 : 0 ;
3490+
3491+ // empty inputs would give out-of-range gathers and zero-length DMA rows: leave them to the CPU
3492+ if (S.n_kv == 0 || total_rows == 0 || S.n_pick == 0 || S.n_top == 0 || (blocks && S.n_blk == 0 ) || (extra && S.n_extra == 0 )) {
3493+ return false ;
3494+ }
3495+
3496+ // PICKS for one row with a resident mask when its single row tile fits, ROWS otherwise
3497+ const uint32_t sess_threads = (std::max)(1u , htp_session_n_threads (sess));
3498+ const size_t vtcm_size = htp_session_vtcm_size (sess);
3499+
3500+ uint32_t kernel_type = HTP_TOP_K_MASK_KERNEL_ROWS ;
3501+ uint32_t n_threads = sess_threads;
3502+ uint32_t tile_cols = 0 ;
3503+
3504+ struct htp_top_k_mask_vtcm_layout L;
3505+ bool found = false ;
3506+ if (total_rows == 1 && S.mask_resident && sess_threads > 1 ) {
3507+ kernel_type = HTP_TOP_K_MASK_KERNEL_PICKS ;
3508+ tile_cols = hex_round_up (S.n_kv , 64 );
3509+ htp_top_k_mask_vtcm_layout_build (&L, &S, kernel_type, n_threads, tile_cols);
3510+ found = L.total_bytes <= vtcm_size;
3511+ }
3512+ if (!found) {
3513+ kernel_type = HTP_TOP_K_MASK_KERNEL_ROWS ;
3514+ n_threads = (std::max)(1u , (std::min)(sess_threads, total_rows));
3515+ tile_cols = hex_round_up (S.n_kv , 64 );
3516+ found = htp_top_k_mask_search (&L, &S, kernel_type, vtcm_size, &n_threads, &tile_cols);
3517+ }
3518+ if (!found) {
3519+ return false ;
3520+ }
3521+
3522+ // compact kernel_params fields: sizes and offsets in HTP_TOP_K_MASK_UNIT bytes as u16, all multiples of the unit
3523+ const uint64_t off_extra = htp_top_k_mask_rowin_off_extra (S.n_top );
3524+ const uint64_t off_score = htp_top_k_mask_rowin_off_score (S.n_top , S.n_extra );
3525+ const uint64_t u16_max = (uint64_t ) UINT16_MAX * HTP_TOP_K_MASK_UNIT ;
3526+ if (L.rowin_bytes > u16_max || L.tile_bytes > u16_max || off_extra > u16_max || off_score > u16_max || L.off_scratch > u16_max) {
3527+ return false ;
3528+ }
3529+ const auto units = [](uint64_t bytes) { return (uint16_t ) (bytes / HTP_TOP_K_MASK_UNIT ); };
3530+
3531+ kparams->n_threads = (uint8_t ) n_threads;
3532+ kparams->kernel_type = (uint8_t ) kernel_type;
3533+ kparams->flags = (dst->type == GGML_TYPE_F16 ? HTP_TOP_K_MASK_F16 : 0 ) |
3534+ (score ? HTP_TOP_K_MASK_HAS_SCORE : 0 ) |
3535+ (blocks ? HTP_TOP_K_MASK_HAS_BLOCKS : 0 ) |
3536+ (extra ? HTP_TOP_K_MASK_HAS_EXTRA : 0 ) |
3537+ (S.mask_resident ? HTP_TOP_K_MASK_MASK_RESIDENT : 0 );
3538+ kparams->elem_size = (uint8_t ) elem_size;
3539+ kparams->vtcm_size = (uint32_t ) L.total_bytes ;
3540+ kparams->n_kv = S.n_kv ;
3541+ kparams->n_top = S.n_top ;
3542+ kparams->n_pick = S.n_pick ;
3543+ kparams->n_extra = S.n_extra ;
3544+ kparams->ninf = dst->type == GGML_TYPE_F16 ? 0xfc00fc00u : 0xff800000u ;
3545+ kparams->blocks_bytes = (uint32_t ) L.blocks_bytes ;
3546+ kparams->score_bytes = score ? S.n_pick * 4 : 0 ;
3547+ kparams->tile_cols = tile_cols;
3548+ kparams->n_col_tiles = (S.n_kv + tile_cols - 1 ) / tile_cols;
3549+ kparams->n_blk = (uint16_t ) S.n_blk ;
3550+ kparams->blocks_nb1 = (uint16_t ) (blocks ? blocks->nb [1 ] : 0 );
3551+ kparams->off_blocks_u = units (L.off_blocks );
3552+ kparams->off_mask_u = units (L.off_mask );
3553+ kparams->off_rowin_u = units (L.off_rowin );
3554+ kparams->off_tiles_u = units (L.off_tiles );
3555+ kparams->off_mtiles_u = units (L.off_mtiles );
3556+ kparams->off_scratch_u = units (L.off_scratch );
3557+ kparams->rowin_units = units (L.rowin_bytes );
3558+ kparams->tile_units = units (L.tile_bytes );
3559+ kparams->off_extra_units = units (off_extra);
3560+ kparams->off_score_units = units (off_score);
3561+
3562+ if (kernel_type == HTP_TOP_K_MASK_KERNEL_PICKS ) {
3563+ // per thread slices of the static copies and of the flag clear, a multiple of the unit
3564+ const auto slice = [&](uint64_t n) { return units (hex_round_up ((n + n_threads - 1 ) / n_threads, HTP_TOP_K_MASK_UNIT )); };
3565+ kparams->u .picks .picks_per_thread = (uint32_t ) hex_round_up ((S.n_top + n_threads - 1 ) / n_threads, 32 );
3566+ kparams->u .picks .blocks_slice_u = slice (L.blocks_bytes );
3567+ kparams->u .picks .top_slice_u = slice ((uint64_t ) S.n_top * 4 );
3568+ kparams->u .picks .extra_slice_u = slice ((uint64_t ) S.n_extra * 4 );
3569+ kparams->u .picks .score_slice_u = slice (kparams->score_bytes );
3570+ kparams->u .picks .flags_slice_u = slice (L.tile_bytes );
3571+ } else {
3572+ kparams->u .rows .total_rows = total_rows;
3573+ kparams->u .rows .rs_top = (uint32_t ) top_k->nb [1 ];
3574+ kparams->u .rows .rs_extra = extra ? (uint32_t ) extra->nb [1 ] : 0 ;
3575+ kparams->u .rows .rs_score = score ? (uint32_t ) score->nb [1 ] : 0 ;
3576+ kparams->u .rows .rs_dst = (uint32_t ) dst->nb [1 ];
3577+ kparams->u .rows .mask_nb1 = (uint32_t ) mask->nb [1 ];
3578+ if (S.mask_resident ) {
3579+ kparams->u .rows .m .mask_bytes = (uint32_t ) L.mask_bytes ;
3580+ } else {
3581+ const int64_t d[5 ] = { dst->ne [1 ], dst->ne [2 ], mask->ne [1 ], mask->ne [2 ], mask->ne [3 ] };
3582+ for (int k = 0 ; k < 5 ; k++) {
3583+ const struct fastdiv_values fd = init_fastdiv_values ((uint32_t ) d[k]);
3584+ kparams->u .rows .m .dec .mp [k] = fd.mp ;
3585+ kparams->u .rows .m .dec .l [k] = (uint8_t ) fd.l ;
3586+ kparams->u .rows .m .dec .ne [k] = (uint16_t ) d[k];
3587+ }
3588+ }
3589+ }
3590+
3591+ return true ;
3592+ }
3593+
34053594static bool htp_kparams_solve_fill (
34063595 const struct ggml_hexagon_session * sess,
34073596 const struct ggml_tensor * op,
@@ -4807,6 +4996,9 @@ bool htp_kparams_solve(
48074996 case HTP_OP_CUMSUM :
48084997 return htp_kparams_invoke<htp_kparams_solve_cumsum>(sess, op, kparams);
48094998
4999+ case HTP_OP_TOP_K_MASK :
5000+ return htp_kparams_invoke<htp_kparams_solve_top_k_mask>(sess, op, kparams);
5001+
48105002 case HTP_OP_FILL :
48115003 return htp_kparams_invoke<htp_kparams_solve_fill>(sess, op, kparams);
48125004
0 commit comments