ggml
[Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685)
Brought by drakestapleton
ggml / src / ggml-hexagon / htp / top-k-mask-ops.c
| 1 | #pragma clang diagnostic ignored "-Wunused-variable" |
| 2 | #pragma clang diagnostic ignored "-Wunused-function" |
| 3 | #pragma clang diagnostic ignored "-Wunused-but-set-variable" |
| 4 | |
| 5 | #include <HAP_farf.h> |
| 6 | #include <HAP_perf.h> |
| 7 | #include <stdatomic.h> |
| 8 | |
| 9 | #define GGML_COMMON_DECL_C |
| 10 | #include "ggml-common.h" |
| 11 | #include "hex-common.h" |
| 12 | #include "hex-profile.h" |
| 13 | #include "htp-ctx.h" |
| 14 | #include "htp-ops.h" |
| 15 | #include "htp-tensor.h" |
| 16 | #include "htp-vtcm.h" |
| 17 | #include "hvx-types.h" |
| 18 | #include "hvx-utils.h" |
| 19 | #include "dma-queue.h" |
| 20 | #include "top-k-mask-ops.h" |
| 21 | |
| 22 | #define TOP_K_MASK_NINF_F32 0xff800000u // f32 score compare |
| 23 | |
| 24 | struct htp_top_k_mask_context { |
| 25 | struct htp_ops_context * octx; |
| 26 | const struct htp_top_k_mask_kernel_params * kparams; |
| 27 | |
| 28 | uint32_t rows_per_thread; |
| 29 | uint32_t row0, row1; // rows this device handles |
| 30 | uint32_t off_extra, off_score; // parts inside one row input slot |
| 31 | uint32_t col0, ncols_out; // PICKS: columns this device writes back |
| 32 | atomic_uint barrier; // PICKS: arrivals, barrier k waits for k * n_threads |
| 33 | |
| 34 | uint8_t * blocks; |
| 35 | uint8_t * mask; |
| 36 | uint8_t * rowin; |
| 37 | uint8_t * tiles; |
| 38 | uint8_t * mtiles; |
| 39 | uint8_t * scratch; |
| 40 | }; |
| 41 | |
| 42 | // DMA queue pop with a trace event around the wait |
| 43 | static inline void top_k_mask_pop(dma_queue * dma_q, struct htp_thread_trace * tr, uint16_t info) { |
| 44 | htp_trace_event_start(tr, HTP_TRACE_EVT_DMA, info); |
| 45 | dma_queue_pop(dma_q); |
| 46 | htp_trace_event_stop(tr, HTP_TRACE_EVT_DMA, info); |
| 47 | } |
| 48 | |
| 49 | // Lanes 0..31 as words, in registers: inclusive scan of ones, minus one |
| 50 | static inline HVX_Vector top_k_mask_lane_iota(void) { |
| 51 | const HVX_Vector zero = Q6_V_vzero(); |
| 52 | const HVX_Vector one = Q6_V_vsplat_R(1); |
| 53 | HVX_Vector v = one; |
| 54 | v = Q6_Vw_vadd_VwVw(v, Q6_V_vlalign_VVR(v, zero, 4)); |
| 55 | v = Q6_Vw_vadd_VwVw(v, Q6_V_vlalign_VVR(v, zero, 8)); |
| 56 | v = Q6_Vw_vadd_VwVw(v, Q6_V_vlalign_VVR(v, zero, 16)); |
| 57 | v = Q6_Vw_vadd_VwVw(v, Q6_V_vlalign_VVR(v, zero, 32)); |
| 58 | v = Q6_Vw_vadd_VwVw(v, Q6_V_vlalign_VVR(v, zero, 64)); |
| 59 | return Q6_Vw_vsub_VwVw(v, one); |
| 60 | } |
| 61 | |
| 62 | static __attribute__((noinline)) void top_k_mask_fill(uint8_t * restrict dst, uint32_t nbytes, uint32_t value) { |
| 63 | const HVX_Vector v = Q6_V_vsplat_R(value); |
| 64 | HVX_Vector * vdst = (HVX_Vector *) dst; |
| 65 | const uint32_t nvec = nbytes / VLEN; |
| 66 | |
| 67 | #pragma unroll(4) |
| 68 | for (uint32_t i = 0; i < nvec; i++) { |
| 69 | vdst[i] = v; |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | #define TOP_K_MASK_DEAD 0x7fffffffu // cell value of a dead lane: c - c0 >=u ncols for every tile, signed max |
| 74 | |
| 75 | // Byte offsets of the cells in [c0, c0 + ncols) relative to c0, 0 for the others |
| 76 | static inline HVX_Vector top_k_mask_keep_off(HVX_Vector c, HVX_Vector v_c0, HVX_Vector v_ncols, const bool is_f16) { |
| 77 | const HVX_Vector cr = Q6_Vw_vsub_VwVw(c, v_c0); |
| 78 | return Q6_Vw_vasl_VwR(Q6_V_vmux_QVV(Q6_Q_vcmp_gt_VuwVuw(v_ncols, cr), cr, Q6_V_vzero()), is_f16 ? 1 : 2); |
| 79 | } |
| 80 | |
| 81 | // Mask value of each cell lane in [c0, c0 + ncols) into tmp (predicated gather), returns the scatter offsets |
| 82 | static inline HVX_Vector top_k_mask_keep_gather(HVX_Vector c, const uint8_t * restrict mrow, HVX_Vector v_c0, HVX_Vector v_ncols, |
| 83 | uint32_t mu, HVX_Vector * restrict tmp, const bool is_f16) { |
| 84 | const HVX_VectorPred q = Q6_Q_vcmp_gt_VuwVuw(v_ncols, Q6_Vw_vsub_VwVw(c, v_c0)); |
| 85 | const HVX_Vector off = top_k_mask_keep_off(c, v_c0, v_ncols, is_f16); |
| 86 | |
| 87 | // Halfword data with word offsets: both halves of the pair hold the same offsets, the word predicate covers both halfwords of a lane |
| 88 | if (is_f16) { |
| 89 | Q6_vgather_AQRMWw(tmp, q, (size_t) mrow, mu, Q6_W_vcombine_VV(off, off)); |
| 90 | } else { |
| 91 | Q6_vgather_AQRMVw(tmp, q, (size_t) mrow, mu, off); |
| 92 | } |
| 93 | return off; |
| 94 | } |
| 95 | |
| 96 | static inline void top_k_mask_keep_scatter(HVX_Vector c, HVX_Vector off, uint8_t * restrict tile, HVX_Vector v_c0, HVX_Vector v_ncols, |
| 97 | uint32_t mu, HVX_Vector val, const bool is_f16) { |
| 98 | const HVX_VectorPred q = Q6_Q_vcmp_gt_VuwVuw(v_ncols, Q6_Vw_vsub_VwVw(c, v_c0)); |
| 99 | if (is_f16) { |
| 100 | Q6_vscatter_QRMWwV(q, (size_t) tile, mu, Q6_W_vcombine_VV(off, off), val); |
| 101 | } else { |
| 102 | Q6_vscatter_QRMVwV(q, (size_t) tile, mu, off, val); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | // Keep 4 cell vectors (dead lanes = TOP_K_MASK_DEAD): tile[c - c0] = mrow[c - c0] for c in [c0, c0 + ncols). One gather sync. |
| 107 | // flag: no mask, tile[c - c0] = all ones (keep flag) |
| 108 | static inline void top_k_mask_keep4(HVX_Vector c0v, HVX_Vector c1v, HVX_Vector c2v, HVX_Vector c3v, uint8_t * restrict tile, |
| 109 | const uint8_t * restrict mrow, HVX_Vector v_c0, HVX_Vector v_ncols, uint32_t mu, |
| 110 | HVX_Vector * restrict tmp, const bool is_f16, const bool flag) { |
| 111 | if (flag) { |
| 112 | const HVX_Vector ones = Q6_V_vsplat_R(0xffffffffu); |
| 113 | top_k_mask_keep_scatter(c0v, top_k_mask_keep_off(c0v, v_c0, v_ncols, is_f16), tile, v_c0, v_ncols, mu, ones, is_f16); |
| 114 | top_k_mask_keep_scatter(c1v, top_k_mask_keep_off(c1v, v_c0, v_ncols, is_f16), tile, v_c0, v_ncols, mu, ones, is_f16); |
| 115 | top_k_mask_keep_scatter(c2v, top_k_mask_keep_off(c2v, v_c0, v_ncols, is_f16), tile, v_c0, v_ncols, mu, ones, is_f16); |
| 116 | top_k_mask_keep_scatter(c3v, top_k_mask_keep_off(c3v, v_c0, v_ncols, is_f16), tile, v_c0, v_ncols, mu, ones, is_f16); |
| 117 | return; |
| 118 | } |
| 119 | const HVX_Vector o0 = top_k_mask_keep_gather(c0v, mrow, v_c0, v_ncols, mu, tmp + 0, is_f16); |
| 120 | const HVX_Vector o1 = top_k_mask_keep_gather(c1v, mrow, v_c0, v_ncols, mu, tmp + 1, is_f16); |
| 121 | const HVX_Vector o2 = top_k_mask_keep_gather(c2v, mrow, v_c0, v_ncols, mu, tmp + 2, is_f16); |
| 122 | const HVX_Vector o3 = top_k_mask_keep_gather(c3v, mrow, v_c0, v_ncols, mu, tmp + 3, is_f16); |
| 123 | hvx_gather_sync(tmp + 3); |
| 124 | top_k_mask_keep_scatter(c0v, o0, tile, v_c0, v_ncols, mu, tmp[0], is_f16); |
| 125 | top_k_mask_keep_scatter(c1v, o1, tile, v_c0, v_ncols, mu, tmp[1], is_f16); |
| 126 | top_k_mask_keep_scatter(c2v, o2, tile, v_c0, v_ncols, mu, tmp[2], is_f16); |
| 127 | top_k_mask_keep_scatter(c3v, o3, tile, v_c0, v_ncols, mu, tmp[3], is_f16); |
| 128 | } |
| 129 | |
| 130 | // Pick vector j: picks below j1 and n_pick are valid, returns them with dead lanes marked. v_rem = j1 - lane index. |
| 131 | static inline HVX_Vector top_k_mask_load_picks(const int32_t * restrict tk, uint32_t j, HVX_Vector v_rem, HVX_Vector v_npick) { |
| 132 | const HVX_Vector p_raw = hvx_vmem(tk + j); |
| 133 | // lane + j <u j1 is the tail, p <u n_pick drops p < 0 and p >= n_pick |
| 134 | const HVX_VectorPred valid = Q6_Q_vcmp_gtand_QVuwVuw(Q6_Q_vcmp_gt_VwVw(v_rem, Q6_V_vsplat_R(j)), v_npick, p_raw); |
| 135 | return Q6_V_vmux_QVV(valid, p_raw, Q6_V_vsplat_R(TOP_K_MASK_DEAD)); |
| 136 | } |
| 137 | |
| 138 | // Scatter the kept cells of picks [j0, j1) (and the extras) into the tile of columns [c0, c0 + ncols). slot holds the row inputs, mrow the mask columns. |
| 139 | // Gathers are issued in batches with one sync per batch: tmp[0..3] scores/blocks, tmp[4] score with blocks, tmp[5..8] mask values. |
| 140 | static inline __attribute__((always_inline)) void top_k_mask_scatter_tile( |
| 141 | const struct htp_top_k_mask_context * tctx, const uint8_t * restrict slot, const uint8_t * restrict mrow, uint8_t * restrict tile, |
| 142 | HVX_Vector * restrict tmp, uint32_t c0, uint32_t ncols, uint32_t j0, uint32_t j1, bool do_extra, |
| 143 | const bool is_f16, const bool flag) { |
| 144 | const struct htp_top_k_mask_kernel_params * kp = tctx->kparams; |
| 145 | |
| 146 | const int32_t * restrict tk = (const int32_t *) slot; |
| 147 | const int32_t * restrict ex = (const int32_t *) (slot + tctx->off_extra); |
| 148 | const uint8_t * restrict sc = slot + tctx->off_score; |
| 149 | const uint8_t * restrict blk = tctx->blocks; |
| 150 | |
| 151 | const bool has_score = kp->flags & HTP_TOP_K_MASK_HAS_SCORE; |
| 152 | const bool has_blocks = kp->flags & HTP_TOP_K_MASK_HAS_BLOCKS; |
| 153 | const uint32_t n_blk = kp->n_blk; |
| 154 | const uint32_t n_extra = do_extra ? kp->n_extra : 0; |
| 155 | const uint32_t mu = ncols * kp->elem_size - 1; |
| 156 | const uint32_t sc_mu = kp->score_bytes - 1; |
| 157 | const uint32_t blk_mu = kp->blocks_bytes - 1; |
| 158 | const uint32_t nb1_h = ((uint32_t) kp->blocks_nb1 << 16) | kp->blocks_nb1; |
| 159 | |
| 160 | const HVX_Vector v_lane = top_k_mask_lane_iota(); // in the HVX_COMP scope of the caller |
| 161 | const HVX_Vector v_c0 = Q6_V_vsplat_R(c0); |
| 162 | const HVX_Vector v_ncols = Q6_V_vsplat_R(ncols); |
| 163 | const HVX_Vector v_npick = Q6_V_vsplat_R(kp->n_pick); |
| 164 | const HVX_Vector v_pmax = Q6_V_vsplat_R(kp->n_pick - 1); // dead picks gather the last entry, every gather stays in its region |
| 165 | const HVX_Vector v_rem = Q6_Vw_vsub_VwVw(Q6_V_vsplat_R(j1), v_lane); |
| 166 | const HVX_Vector v_ninf = Q6_V_vsplat_R(TOP_K_MASK_NINF_F32); |
| 167 | const HVX_Vector v_dead = Q6_V_vsplat_R(TOP_K_MASK_DEAD); |
| 168 | |
| 169 | HVX_Vector * restrict mtmp = tmp + 5; |
| 170 | |
| 171 | if (has_blocks) { |
| 172 | for (uint32_t j = j0; j < j1; j += VLEN_FP32) { |
| 173 | HVX_Vector cell = top_k_mask_load_picks(tk, j, v_rem, v_npick); |
| 174 | const HVX_Vector p = Q6_Vw_vmin_VwVw(cell, v_pmax); |
| 175 | if (has_score) { |
| 176 | Q6_vgather_ARMVw(tmp + 4, (size_t) sc, sc_mu, Q6_Vw_vasl_VwR(p, 2)); |
| 177 | } |
| 178 | const HVX_Vector boff = Q6_Vw_vmpyi_VwRh(p, nb1_h); |
| 179 | for (uint32_t k = 0; k < n_blk; k += 4) { |
| 180 | // blocks k..k+3 of every pick; the missing ones (k + i >= n_blk) are dead |
| 181 | const uint32_t nk = MIN(n_blk - k, 4); |
| 182 | const HVX_Vector bo = Q6_Vw_vadd_VwVw(boff, Q6_V_vsplat_R(k * 4)); |
| 183 | for (uint32_t i = 0; i < nk; i++) { |
| 184 | Q6_vgather_ARMVw(tmp + i, (size_t) blk, blk_mu, Q6_Vw_vadd_VwVw(bo, Q6_V_vsplat_R(i * 4))); |
| 185 | } |
| 186 | hvx_gather_sync(tmp + nk - 1); |
| 187 | if (has_score && k == 0) { |
| 188 | // IEEE compare: NaN and -inf scores are dead |
| 189 | cell = Q6_V_vmux_QVV(Q6_Q_vcmp_gt_VsfVsf(tmp[4], v_ninf), cell, v_dead); |
| 190 | } |
| 191 | const HVX_VectorPred dead = Q6_Q_vcmp_eq_VwVw(cell, v_dead); |
| 192 | top_k_mask_keep4(Q6_V_vmux_QVV(dead, v_dead, tmp[0]), |
| 193 | nk > 1 ? Q6_V_vmux_QVV(dead, v_dead, tmp[1]) : v_dead, |
| 194 | nk > 2 ? Q6_V_vmux_QVV(dead, v_dead, tmp[2]) : v_dead, |
| 195 | nk > 3 ? Q6_V_vmux_QVV(dead, v_dead, tmp[3]) : v_dead, |
| 196 | tile, mrow, v_c0, v_ncols, mu, mtmp, is_f16, flag); |
| 197 | } |
| 198 | } |
| 199 | } else { |
| 200 | // 4 pick vectors per batch, the vectors at or past j1 are dead and not loaded |
| 201 | for (uint32_t j = j0; j < j1; j += 4 * VLEN_FP32) { |
| 202 | HVX_Vector c0v = top_k_mask_load_picks(tk, j, v_rem, v_npick); |
| 203 | HVX_Vector c1v = j + 1 * VLEN_FP32 < j1 ? top_k_mask_load_picks(tk, j + 1 * VLEN_FP32, v_rem, v_npick) : v_dead; |
| 204 | HVX_Vector c2v = j + 2 * VLEN_FP32 < j1 ? top_k_mask_load_picks(tk, j + 2 * VLEN_FP32, v_rem, v_npick) : v_dead; |
| 205 | HVX_Vector c3v = j + 3 * VLEN_FP32 < j1 ? top_k_mask_load_picks(tk, j + 3 * VLEN_FP32, v_rem, v_npick) : v_dead; |
| 206 | if (has_score) { |
| 207 | Q6_vgather_ARMVw(tmp + 0, (size_t) sc, sc_mu, Q6_Vw_vasl_VwR(Q6_Vw_vmin_VwVw(c0v, v_pmax), 2)); |
| 208 | Q6_vgather_ARMVw(tmp + 1, (size_t) sc, sc_mu, Q6_Vw_vasl_VwR(Q6_Vw_vmin_VwVw(c1v, v_pmax), 2)); |
| 209 | Q6_vgather_ARMVw(tmp + 2, (size_t) sc, sc_mu, Q6_Vw_vasl_VwR(Q6_Vw_vmin_VwVw(c2v, v_pmax), 2)); |
| 210 | Q6_vgather_ARMVw(tmp + 3, (size_t) sc, sc_mu, Q6_Vw_vasl_VwR(Q6_Vw_vmin_VwVw(c3v, v_pmax), 2)); |
| 211 | hvx_gather_sync(tmp + 3); |
| 212 | c0v = Q6_V_vmux_QVV(Q6_Q_vcmp_gt_VsfVsf(tmp[0], v_ninf), c0v, v_dead); |
| 213 | c1v = Q6_V_vmux_QVV(Q6_Q_vcmp_gt_VsfVsf(tmp[1], v_ninf), c1v, v_dead); |
| 214 | c2v = Q6_V_vmux_QVV(Q6_Q_vcmp_gt_VsfVsf(tmp[2], v_ninf), c2v, v_dead); |
| 215 | c3v = Q6_V_vmux_QVV(Q6_Q_vcmp_gt_VsfVsf(tmp[3], v_ninf), c3v, v_dead); |
| 216 | } |
| 217 | top_k_mask_keep4(c0v, c1v, c2v, c3v, tile, mrow, v_c0, v_ncols, mu, mtmp, is_f16, flag); |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | const HVX_Vector v_rem_ex = Q6_Vw_vsub_VwVw(Q6_V_vsplat_R(n_extra), v_lane); |
| 222 | for (uint32_t j = 0; j < n_extra; j += VLEN_FP32) { |
| 223 | const HVX_VectorPred tail = Q6_Q_vcmp_gt_VwVw(v_rem_ex, Q6_V_vsplat_R(j)); |
| 224 | top_k_mask_keep4(Q6_V_vmux_QVV(tail, hvx_vmem(ex + j), v_dead), v_dead, v_dead, v_dead, tile, mrow, v_c0, v_ncols, mu, mtmp, is_f16, flag); |
| 225 | } |
| 226 | |
| 227 | // release the scatters before DMA reads the tile |
| 228 | hvx_gather_sync(tile); |
| 229 | } |
| 230 | |
| 231 | static __attribute__((noinline)) void top_k_mask_scatter_tile_f32(const struct htp_top_k_mask_context * tctx, const uint8_t * restrict slot, |
| 232 | const uint8_t * restrict mrow, uint8_t * restrict tile, HVX_Vector * restrict tmp, |
| 233 | uint32_t c0, uint32_t ncols, uint32_t j0, uint32_t j1, bool do_extra) { |
| 234 | top_k_mask_scatter_tile(tctx, slot, mrow, tile, tmp, c0, ncols, j0, j1, do_extra, false, false); |
| 235 | } |
| 236 | |
| 237 | static __attribute__((noinline)) void top_k_mask_scatter_tile_f16(const struct htp_top_k_mask_context * tctx, const uint8_t * restrict slot, |
| 238 | const uint8_t * restrict mrow, uint8_t * restrict tile, HVX_Vector * restrict tmp, |
| 239 | uint32_t c0, uint32_t ncols, uint32_t j0, uint32_t j1, bool do_extra) { |
| 240 | top_k_mask_scatter_tile(tctx, slot, mrow, tile, tmp, c0, ncols, j0, j1, do_extra, true, false); |
| 241 | } |
| 242 | |
| 243 | // PICKS: keep flags (all ones) of the picks [j0, j1) into the flag tile of the whole row, the element size picks the flag width |
| 244 | static __attribute__((noinline)) void top_k_mask_flag_tile_f32(const struct htp_top_k_mask_context * tctx, const uint8_t * restrict slot, |
| 245 | uint8_t * restrict flags, HVX_Vector * restrict tmp, uint32_t j0, uint32_t j1, bool do_extra) { |
| 246 | top_k_mask_scatter_tile(tctx, slot, NULL, flags, tmp, 0, tctx->kparams->n_kv, j0, j1, do_extra, false, true); |
| 247 | } |
| 248 | |
| 249 | static __attribute__((noinline)) void top_k_mask_flag_tile_f16(const struct htp_top_k_mask_context * tctx, const uint8_t * restrict slot, |
| 250 | uint8_t * restrict flags, HVX_Vector * restrict tmp, uint32_t j0, uint32_t j1, bool do_extra) { |
| 251 | top_k_mask_scatter_tile(tctx, slot, NULL, flags, tmp, 0, tctx->kparams->n_kv, j0, j1, do_extra, true, true); |
| 252 | } |
| 253 | |
| 254 | // out = flag ? out : -inf over nbytes (a multiple of 128) of the row tile |
| 255 | static __attribute__((noinline)) void top_k_mask_select(uint8_t * restrict out, const uint8_t * restrict flags, uint32_t nbytes, uint32_t ninf) { |
| 256 | const HVX_Vector v_ninf = Q6_V_vsplat_R(ninf); |
| 257 | HVX_Vector * vout = (HVX_Vector *) out; |
| 258 | const HVX_Vector * vflag = (const HVX_Vector *) flags; |
| 259 | const uint32_t nvec = nbytes / VLEN; |
| 260 | |
| 261 | #pragma unroll(4) |
| 262 | for (uint32_t i = 0; i < nvec; i++) { |
| 263 | vout[i] = Q6_V_vxor_VV(v_ninf, Q6_V_vand_VV(Q6_V_vxor_VV(vout[i], v_ninf), vflag[i])); |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | // Push the row inputs of flat row r into slot: top_k, extra, score. Returns the push count. |
| 268 | static inline uint32_t top_k_mask_push_rowin(const struct htp_top_k_mask_context * tctx, dma_queue * dma_q, uint32_t r, uint8_t * slot) { |
| 269 | const struct htp_ops_context * octx = tctx->octx; |
| 270 | const struct htp_top_k_mask_kernel_params * kp = tctx->kparams; |
| 271 | const struct htp_tensor * top_k = octx->src[1]; |
| 272 | const struct htp_tensor * score = octx->src[2]; |
| 273 | const struct htp_tensor * extra = octx->src[4]; |
| 274 | |
| 275 | const uint32_t tk_bytes = kp->n_top * sizeof(int32_t); |
| 276 | dma_queue_push(dma_q, dma_make_data(slot, top_k->data + r * kp->u.rows.rs_top), tk_bytes, tk_bytes, tk_bytes, 1); |
| 277 | uint32_t n = 1; |
| 278 | |
| 279 | if (kp->flags & HTP_TOP_K_MASK_HAS_EXTRA) { |
| 280 | const uint32_t ex_bytes = kp->n_extra * sizeof(int32_t); |
| 281 | dma_queue_push(dma_q, dma_make_data(slot + tctx->off_extra, extra->data + r * kp->u.rows.rs_extra), ex_bytes, ex_bytes, ex_bytes, 1); |
| 282 | n++; |
| 283 | } |
| 284 | |
| 285 | if (kp->flags & HTP_TOP_K_MASK_HAS_SCORE) { |
| 286 | dma_queue_push(dma_q, dma_make_data(slot + tctx->off_score, score->data + r * kp->u.rows.rs_score), kp->score_bytes, kp->score_bytes, kp->score_bytes, 1); |
| 287 | n++; |
| 288 | } |
| 289 | |
| 290 | return n; |
| 291 | } |
| 292 | |
| 293 | static inline struct fastdiv_values top_k_mask_fd(const struct htp_top_k_mask_kernel_params * kp, uint32_t k) { |
| 294 | const struct fastdiv_values fd = { kp->u.rows.m.dec.mp[k], kp->u.rows.m.dec.l[k] }; |
| 295 | return fd; |
| 296 | } |
| 297 | |
| 298 | // Mask row of flat row r: (i1, i2, i3) of dst, each taken modulo the mask dims, rows dense above the mask ne1 |
| 299 | static inline uint32_t top_k_mask_mask_row(const struct htp_top_k_mask_kernel_params * kp, uint32_t r) { |
| 300 | const uint16_t * ne = kp->u.rows.m.dec.ne; |
| 301 | const struct fastdiv_values fd1 = top_k_mask_fd(kp, 0); |
| 302 | const struct fastdiv_values fd2 = top_k_mask_fd(kp, 1); |
| 303 | const struct fastdiv_values fm1 = top_k_mask_fd(kp, 2); |
| 304 | const struct fastdiv_values fm2 = top_k_mask_fd(kp, 3); |
| 305 | const struct fastdiv_values fm3 = top_k_mask_fd(kp, 4); |
| 306 | |
| 307 | const uint32_t q1 = fastdiv(r, &fd1); |
| 308 | const uint32_t i1 = r - q1 * ne[0]; |
| 309 | const uint32_t i3 = fastdiv(q1, &fd2); |
| 310 | const uint32_t i2 = q1 - i3 * ne[1]; |
| 311 | |
| 312 | const uint32_t m1 = fastmodulo(i1, ne[2], &fm1); |
| 313 | const uint32_t m2 = fastmodulo(i2, ne[3], &fm2); |
| 314 | const uint32_t m3 = fastmodulo(i3, ne[4], &fm3); |
| 315 | return (m3 * ne[3] + m2) * ne[2] + m1; |
| 316 | } |
| 317 | |
| 318 | // Push the inputs of item (row r, col tile ct): row inputs on the first tile of a row, then the mask tile. Returns the push count. |
| 319 | static inline uint32_t top_k_mask_push_item(const struct htp_top_k_mask_context * tctx, dma_queue * dma_q, uint32_t r, uint32_t ct, |
| 320 | uint8_t * slot, uint8_t * mslot) { |
| 321 | const struct htp_ops_context * octx = tctx->octx; |
| 322 | const struct htp_top_k_mask_kernel_params * kp = tctx->kparams; |
| 323 | const struct htp_tensor * mask = octx->src[0]; |
| 324 | |
| 325 | const uint32_t c0 = ct * kp->tile_cols; |
| 326 | |
| 327 | uint32_t n = 0; |
| 328 | if (ct == 0) { |
| 329 | n += top_k_mask_push_rowin(tctx, dma_q, r, slot); |
| 330 | } |
| 331 | |
| 332 | if (mslot) { |
| 333 | const uint32_t m_off = top_k_mask_mask_row(kp, r) * kp->u.rows.mask_nb1 + c0 * kp->elem_size; |
| 334 | const uint32_t nb = MIN(kp->tile_cols, kp->n_kv - c0) * kp->elem_size; |
| 335 | dma_queue_push(dma_q, dma_make_data(mslot, mask->data + m_off), nb, nb, nb, 1); |
| 336 | n++; |
| 337 | } |
| 338 | |
| 339 | return n; |
| 340 | } |
| 341 | |
| 342 | static inline void top_k_mask_next(uint32_t * r, uint32_t * ct, uint32_t n_col_tiles) { |
| 343 | if (++(*ct) == n_col_tiles) { |
| 344 | *ct = 0; |
| 345 | ++(*r); |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | // ROWS: the thread count follows the device rows (mdev), so the slice of an n byte copy is sized here |
| 350 | static inline uint32_t top_k_mask_slice(const struct htp_ops_context * octx, unsigned int nth, uint32_t n) { |
| 351 | return hex_round_up(fastdiv(n + nth - 1, &octx->n_threads_div), HTP_TOP_K_MASK_UNIT); |
| 352 | } |
| 353 | |
| 354 | // Push this thread's slice (128B aligned) of a contiguous n byte copy. Returns the push count. |
| 355 | static inline uint32_t top_k_mask_push_slice(dma_queue * dma_q, unsigned int ith, dma_addr_t dst, dma_addr_t src, uint32_t n, uint32_t slice) { |
| 356 | const uint32_t b0 = ith * slice; |
| 357 | if (b0 >= n) { |
| 358 | return 0; |
| 359 | } |
| 360 | const uint32_t nb = MIN(slice, n - b0); |
| 361 | dma_queue_push(dma_q, dma_make_data(dst + b0, src + b0), nb, nb, nb, 1); |
| 362 | return 1; |
| 363 | } |
| 364 | |
| 365 | // Pass 1 (ROWS): load the blocks table and the resident mask row, split across threads. Fill the tiles of this thread with -inf meanwhile. |
| 366 | static void top_k_mask_preload(unsigned int nth, unsigned int ith, void * data) { |
| 367 | struct htp_top_k_mask_context * tctx = (struct htp_top_k_mask_context *) data; |
| 368 | const struct htp_top_k_mask_kernel_params * kp = tctx->kparams; |
| 369 | struct htp_ops_context * octx = tctx->octx; |
| 370 | dma_queue * dma_q = octx->ctx->dma[ith]; |
| 371 | struct htp_thread_trace * tr = &octx->ctx->trace[ith]; |
| 372 | |
| 373 | uint32_t n_pushed = 0; |
| 374 | |
| 375 | if (kp->blocks_bytes) { |
| 376 | n_pushed += top_k_mask_push_slice(dma_q, ith, (dma_addr_t) tctx->blocks, octx->src[3]->data, kp->blocks_bytes, top_k_mask_slice(octx, nth, kp->blocks_bytes)); |
| 377 | } |
| 378 | if (kp->flags & HTP_TOP_K_MASK_MASK_RESIDENT) { |
| 379 | const uint32_t mask_bytes = kp->u.rows.m.mask_bytes; |
| 380 | n_pushed += top_k_mask_push_slice(dma_q, ith, (dma_addr_t) tctx->mask, octx->src[0]->data, mask_bytes, top_k_mask_slice(octx, nth, mask_bytes)); |
| 381 | } |
| 382 | |
| 383 | const uint32_t tile_bytes = kp->tile_units * HTP_TOP_K_MASK_UNIT; |
| 384 | |
| 385 | htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) ith); |
| 386 | top_k_mask_fill(tctx->tiles + ith * 2 * tile_bytes, 2 * tile_bytes, kp->ninf); |
| 387 | htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) ith); |
| 388 | |
| 389 | for (uint32_t i = 0; i < n_pushed; i++) { |
| 390 | top_k_mask_pop(dma_q, tr, (uint16_t) i); |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | // Pass 2 (ROWS): depth-2 DMA pipeline over the items of rows [row_first, row_last), item = (row, col tile). |
| 395 | // The row inputs alternate between 2 slots by row. |
| 396 | static void top_k_mask_run_items(const struct htp_top_k_mask_context * tctx, unsigned int ith, uint32_t row_first, uint32_t row_last) { |
| 397 | struct htp_ops_context * octx = tctx->octx; |
| 398 | const struct htp_top_k_mask_kernel_params * kp = tctx->kparams; |
| 399 | const struct htp_tensor * dst = octx->dst; |
| 400 | dma_queue * dma_q = octx->ctx->dma[ith]; |
| 401 | struct htp_thread_trace * tr = &octx->ctx->trace[ith]; |
| 402 | |
| 403 | const bool is_f16 = kp->flags & HTP_TOP_K_MASK_F16; |
| 404 | const uint32_t elem_size = kp->elem_size; |
| 405 | |
| 406 | const uint32_t rowin_bytes = kp->rowin_units * HTP_TOP_K_MASK_UNIT; |
| 407 | const uint32_t tile_bytes = kp->tile_units * HTP_TOP_K_MASK_UNIT; |
| 408 | const uint32_t tile_cols = kp->tile_cols; |
| 409 | const uint32_t n_col_tiles = kp->n_col_tiles; |
| 410 | const uint32_t n_items = (row_last - row_first) * n_col_tiles; |
| 411 | |
| 412 | // 2 row input slots per thread, alternating by row |
| 413 | uint8_t * rowin = tctx->rowin + ith * 2 * rowin_bytes; |
| 414 | uint8_t * tiles = tctx->tiles + ith * 2 * tile_bytes; |
| 415 | uint8_t * mtiles = (kp->flags & HTP_TOP_K_MASK_MASK_RESIDENT) ? NULL : tctx->mtiles + ith * 2 * tile_bytes; |
| 416 | |
| 417 | HVX_Vector * tmp = (HVX_Vector *) (tctx->scratch + ith * HTP_TOP_K_MASK_SCRATCH_BYTES); |
| 418 | |
| 419 | const dma_addr_t dst_data = dst->data; |
| 420 | |
| 421 | uint32_t n_in[2] = { 0, 0 }; |
| 422 | |
| 423 | // prefetch cursor |
| 424 | uint32_t pr = row_first, pct = 0; |
| 425 | |
| 426 | for (uint32_t it = 0; it < MIN(n_items, 2); it++) { |
| 427 | // Dummy dst writeback to establish queue ordering |
| 428 | dma_queue_push(dma_q, dma_make_data(dst_data, tiles + it * tile_bytes), 0, 0, 0, 0); |
| 429 | n_in[it] = top_k_mask_push_item(tctx, dma_q, pr, pct, rowin + ((pr - row_first) & 1) * rowin_bytes, mtiles ? mtiles + it * tile_bytes : NULL); |
| 430 | top_k_mask_next(&pr, &pct, n_col_tiles); |
| 431 | } |
| 432 | |
| 433 | uint32_t r = row_first, ct = 0; |
| 434 | |
| 435 | for (uint32_t it = 0; it < n_items; it++) { |
| 436 | const uint32_t s = it & 1; |
| 437 | uint8_t * tile = tiles + s * tile_bytes; |
| 438 | |
| 439 | // writeback of item it - 2 (or the dummy), then the tile is free again |
| 440 | top_k_mask_pop(dma_q, tr, (uint16_t) it); |
| 441 | if (it >= 2) { |
| 442 | htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) it); |
| 443 | top_k_mask_fill(tile, tile_bytes, kp->ninf); |
| 444 | htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) it); |
| 445 | } |
| 446 | |
| 447 | for (uint32_t i = 0; i < n_in[s]; i++) { |
| 448 | top_k_mask_pop(dma_q, tr, (uint16_t) it); |
| 449 | } |
| 450 | |
| 451 | const uint32_t c0 = ct * tile_cols; |
| 452 | const uint32_t ncols = MIN(tile_cols, kp->n_kv - c0); |
| 453 | |
| 454 | const uint8_t * slot = rowin + ((r - row_first) & 1) * rowin_bytes; |
| 455 | const uint8_t * mrow = mtiles ? mtiles + s * tile_bytes : tctx->mask + c0 * elem_size; |
| 456 | |
| 457 | htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) it); |
| 458 | if (is_f16) { |
| 459 | top_k_mask_scatter_tile_f16(tctx, slot, mrow, tile, tmp, c0, ncols, 0, kp->n_top, true); |
| 460 | } else { |
| 461 | top_k_mask_scatter_tile_f32(tctx, slot, mrow, tile, tmp, c0, ncols, 0, kp->n_top, true); |
| 462 | } |
| 463 | htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) it); |
| 464 | |
| 465 | const uint32_t d_off = r * kp->u.rows.rs_dst + c0 * elem_size; |
| 466 | const uint32_t nb = ncols * elem_size; |
| 467 | dma_queue_push(dma_q, dma_make_data(dst_data + d_off, tile), nb, nb, nb, 1); |
| 468 | |
| 469 | if (it + 2 < n_items) { |
| 470 | n_in[s] = top_k_mask_push_item(tctx, dma_q, pr, pct, rowin + ((pr - row_first) & 1) * rowin_bytes, mtiles ? mtiles + s * tile_bytes : NULL); |
| 471 | top_k_mask_next(&pr, &pct, n_col_tiles); |
| 472 | } |
| 473 | top_k_mask_next(&r, &ct, n_col_tiles); |
| 474 | } |
| 475 | |
| 476 | const uint32_t n_wb = MIN(n_items, 2); |
| 477 | for (uint32_t i = 0; i < n_wb; i++) { |
| 478 | top_k_mask_pop(dma_q, tr, (uint16_t) i); |
| 479 | } |
| 480 | } |
| 481 | |
| 482 | // Pass 2 (ROWS): whole rows split across threads, the col tiles of a row in order |
| 483 | static void top_k_mask_thread_rows(unsigned int nth, unsigned int ith, void * data) { |
| 484 | const struct htp_top_k_mask_context * tctx = (const struct htp_top_k_mask_context *) data; |
| 485 | |
| 486 | const uint32_t ir0 = tctx->row0 + ith * tctx->rows_per_thread; |
| 487 | const uint32_t ir1 = MIN(ir0 + tctx->rows_per_thread, tctx->row1); |
| 488 | if (ir0 >= ir1) { |
| 489 | return; |
| 490 | } |
| 491 | |
| 492 | top_k_mask_run_items(tctx, ith, ir0, ir1); |
| 493 | } |
| 494 | |
| 495 | static inline void top_k_mask_barrier(atomic_uint * b, uint32_t target, struct htp_thread_trace * tr, uint16_t info) { |
| 496 | htp_trace_event_start(tr, HTP_TRACE_EVT_FENCE, info); |
| 497 | atomic_fetch_add_explicit(b, 1, memory_order_release); |
| 498 | while (atomic_load_explicit(b, memory_order_acquire) < target) { |
| 499 | hex_pause(); |
| 500 | } |
| 501 | htp_trace_event_stop(tr, HTP_TRACE_EVT_FENCE, info); |
| 502 | } |
| 503 | |
| 504 | // PICKS, one pass over n_virt = kp->n_threads virtual workers (host slices); real worker ith runs the virtual workers ith, ith + nth, ... |
| 505 | // A: shared preload and a slice of the flag clear per virtual worker, the mask slice of this worker into the row tile; |
| 506 | // barrier; B: scatter keep flags of the picks of each virtual worker; barrier; C: select mask or -inf in this worker's slice and write it back |
| 507 | static void top_k_mask_thread_picks(unsigned int nth, unsigned int ith, void * data) { |
| 508 | struct htp_top_k_mask_context * tctx = (struct htp_top_k_mask_context *) data; |
| 509 | const struct htp_top_k_mask_kernel_params * kp = tctx->kparams; |
| 510 | struct htp_ops_context * octx = tctx->octx; |
| 511 | dma_queue * dma_q = octx->ctx->dma[ith]; |
| 512 | struct htp_thread_trace * tr = &octx->ctx->trace[ith]; |
| 513 | |
| 514 | const struct htp_tensor * top_k = octx->src[1]; |
| 515 | const struct htp_tensor * score = octx->src[2]; |
| 516 | const struct htp_tensor * extra = octx->src[4]; |
| 517 | |
| 518 | uint8_t * slot = tctx->rowin; |
| 519 | const uint32_t tile_bytes = kp->tile_units * HTP_TOP_K_MASK_UNIT; |
| 520 | uint8_t * out = tctx->tiles; |
| 521 | uint8_t * flags = tctx->tiles + tile_bytes; |
| 522 | |
| 523 | const uint32_t n_virt = kp->n_threads; |
| 524 | |
| 525 | // this worker's slice of the device columns: the mask goes straight into the row tile, it is popped after the scan. |
| 526 | // The device share of the columns depends on mdev, so this slice is sized here. |
| 527 | const uint32_t out_b0 = tctx->col0 * kp->elem_size; |
| 528 | const uint32_t out_nb = tctx->ncols_out * kp->elem_size; |
| 529 | const uint32_t wslice = hex_round_up(fastdiv(out_nb + nth - 1, &octx->n_threads_div), 128); |
| 530 | const uint32_t wb0 = out_b0 + ith * wslice; |
| 531 | const uint32_t wnb = ith * wslice < out_nb ? MIN(wslice, out_nb - ith * wslice) : 0; |
| 532 | |
| 533 | const uint32_t fslice = kp->u.picks.flags_slice_u * HTP_TOP_K_MASK_UNIT; |
| 534 | |
| 535 | for (uint32_t v = ith; v < n_virt; v += nth) { |
| 536 | uint32_t n_pushed = 0; |
| 537 | if (kp->blocks_bytes) { |
| 538 | n_pushed += top_k_mask_push_slice(dma_q, v, (dma_addr_t) tctx->blocks, octx->src[3]->data, kp->blocks_bytes, kp->u.picks.blocks_slice_u * HTP_TOP_K_MASK_UNIT); |
| 539 | } |
| 540 | n_pushed += top_k_mask_push_slice(dma_q, v, (dma_addr_t) slot, top_k->data, kp->n_top * sizeof(int32_t), kp->u.picks.top_slice_u * HTP_TOP_K_MASK_UNIT); |
| 541 | if (kp->flags & HTP_TOP_K_MASK_HAS_EXTRA) { |
| 542 | n_pushed += top_k_mask_push_slice(dma_q, v, (dma_addr_t) (slot + tctx->off_extra), extra->data, kp->n_extra * sizeof(int32_t), kp->u.picks.extra_slice_u * HTP_TOP_K_MASK_UNIT); |
| 543 | } |
| 544 | if (kp->flags & HTP_TOP_K_MASK_HAS_SCORE) { |
| 545 | n_pushed += top_k_mask_push_slice(dma_q, v, (dma_addr_t) (slot + tctx->off_score), score->data, kp->score_bytes, kp->u.picks.score_slice_u * HTP_TOP_K_MASK_UNIT); |
| 546 | } |
| 547 | |
| 548 | // the mask slice follows the last preload in the queue, so the pops below leave it in flight |
| 549 | if (wnb && v + nth >= n_virt) { |
| 550 | dma_queue_push(dma_q, dma_make_data(out + wb0, octx->src[0]->data + wb0), wnb, wnb, wnb, 1); |
| 551 | } |
| 552 | |
| 553 | const uint32_t fb0 = v * fslice; |
| 554 | if (fb0 < tile_bytes) { |
| 555 | htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) v); |
| 556 | top_k_mask_fill(flags + fb0, MIN(fslice, tile_bytes - fb0), 0); // clear the flags |
| 557 | htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) v); |
| 558 | } |
| 559 | |
| 560 | for (uint32_t i = 0; i < n_pushed; i++) { |
| 561 | top_k_mask_pop(dma_q, tr, (uint16_t) i); |
| 562 | } |
| 563 | } |
| 564 | |
| 565 | top_k_mask_barrier(&tctx->barrier, nth, tr, 0); |
| 566 | |
| 567 | HVX_Vector * tmp = (HVX_Vector *) (tctx->scratch + ith * HTP_TOP_K_MASK_SCRATCH_BYTES); |
| 568 | |
| 569 | for (uint32_t v = ith; v < n_virt; v += nth) { |
| 570 | const uint32_t j0 = MIN(v * kp->u.picks.picks_per_thread, kp->n_top); |
| 571 | const uint32_t j1 = MIN(j0 + kp->u.picks.picks_per_thread, kp->n_top); |
| 572 | const bool do_extra = v == n_virt - 1; |
| 573 | |
| 574 | htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) v); |
| 575 | if (kp->flags & HTP_TOP_K_MASK_F16) { |
| 576 | top_k_mask_flag_tile_f16(tctx, slot, flags, tmp, j0, j1, do_extra); |
| 577 | } else { |
| 578 | top_k_mask_flag_tile_f32(tctx, slot, flags, tmp, j0, j1, do_extra); |
| 579 | } |
| 580 | htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) v); |
| 581 | } |
| 582 | |
| 583 | top_k_mask_barrier(&tctx->barrier, 2 * nth, tr, 1); |
| 584 | |
| 585 | if (wnb) { |
| 586 | top_k_mask_pop(dma_q, tr, (uint16_t) ith); |
| 587 | // wb0 is 128B aligned (col0 is a multiple of 64 columns), the select may run past the row inside the tile |
| 588 | htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) ith); |
| 589 | top_k_mask_select(out + wb0, flags + wb0, hex_round_up(wnb, 128), kp->ninf); |
| 590 | htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) ith); |
| 591 | dma_queue_push(dma_q, dma_make_data(octx->dst->data + wb0, out + wb0), wnb, wnb, wnb, 1); |
| 592 | top_k_mask_pop(dma_q, tr, (uint16_t) ith); |
| 593 | } |
| 594 | } |
| 595 | |
| 596 | int op_top_k_mask(struct htp_ops_context * octx) { |
| 597 | const struct htp_top_k_mask_kernel_params * kp = (const struct htp_top_k_mask_kernel_params *) octx->kernel_params; |
| 598 | |
| 599 | if (kp->vtcm_size > octx->ctx->vtcm_size) { |
| 600 | return HTP_STATUS_VTCM_TOO_SMALL; |
| 601 | } |
| 602 | |
| 603 | const bool picks = kp->kernel_type == HTP_TOP_K_MASK_KERNEL_PICKS; |
| 604 | |
| 605 | // mdev: ROWS gives this device a contiguous share of the rows, DMA writes are exempt from the 128B rule. |
| 606 | // PICKS: every device scans the row, the write back is split by 64 column chunks. |
| 607 | uint32_t dev_row0 = 0; |
| 608 | uint32_t dev_rows = picks ? 1 : kp->u.rows.total_rows; |
| 609 | uint32_t dev_c0 = 0; |
| 610 | uint32_t dev_cols = kp->n_kv; |
| 611 | if (octx->ctx->mdev.count > 1) { |
| 612 | const struct htp_tensor_mdev_range range = picks ? |
| 613 | htp_tensor_mdev_partition(kp->n_kv, 64, octx->ctx->mdev.idx, octx->ctx->mdev.count, &octx->ctx->mdev.count_div) : |
| 614 | htp_tensor_mdev_partition(kp->u.rows.total_rows, 1, octx->ctx->mdev.idx, octx->ctx->mdev.count, &octx->ctx->mdev.count_div); |
| 615 | if (range.count == 0) { |
| 616 | return HTP_STATUS_OK; |
| 617 | } |
| 618 | if (picks) { |
| 619 | dev_c0 = range.start; |
| 620 | dev_cols = range.count; |
| 621 | } else { |
| 622 | dev_row0 = range.start; |
| 623 | dev_rows = range.count; |
| 624 | } |
| 625 | } |
| 626 | |
| 627 | // fewer workers than kparams->n_threads always fit the VTCM layout: ROWS runs one row range per worker, PICKS loops over the host slices |
| 628 | const uint32_t max_threads = MIN((uint32_t) kp->n_threads, octx->ctx->n_threads); |
| 629 | if (!htp_ops_context_set_n_threads(octx, picks ? max_threads : MIN(max_threads, dev_rows))) { |
| 630 | return HTP_STATUS_INVAL_PARAMS; |
| 631 | } |
| 632 | const uint32_t n_threads = octx->n_threads; |
| 633 | |
| 634 | uint8_t * vtcm = octx->ctx->vtcm_base; |
| 635 | |
| 636 | struct htp_top_k_mask_context tctx = { |
| 637 | .octx = octx, |
| 638 | .kparams = kp, |
| 639 | .off_extra = kp->off_extra_units * HTP_TOP_K_MASK_UNIT, |
| 640 | .off_score = kp->off_score_units * HTP_TOP_K_MASK_UNIT, |
| 641 | .row0 = dev_row0, |
| 642 | .row1 = dev_row0 + dev_rows, |
| 643 | .col0 = dev_c0, |
| 644 | .ncols_out = dev_cols, |
| 645 | .blocks = VTCM_LAYOUT_PTR(uint8_t, vtcm, kp->off_blocks_u * HTP_TOP_K_MASK_UNIT), |
| 646 | .mask = VTCM_LAYOUT_PTR(uint8_t, vtcm, kp->off_mask_u * HTP_TOP_K_MASK_UNIT), |
| 647 | .rowin = VTCM_LAYOUT_PTR(uint8_t, vtcm, kp->off_rowin_u * HTP_TOP_K_MASK_UNIT), |
| 648 | .tiles = VTCM_LAYOUT_PTR(uint8_t, vtcm, kp->off_tiles_u * HTP_TOP_K_MASK_UNIT), |
| 649 | .mtiles = VTCM_LAYOUT_PTR(uint8_t, vtcm, kp->off_mtiles_u * HTP_TOP_K_MASK_UNIT), |
| 650 | .scratch = VTCM_LAYOUT_PTR(uint8_t, vtcm, kp->off_scratch_u * HTP_TOP_K_MASK_UNIT), |
| 651 | }; |
| 652 | |
| 653 | if (picks) { |
| 654 | atomic_init(&tctx.barrier, 0); |
| 655 | work_queue_run(octx->ctx->work_queue, top_k_mask_thread_picks, &tctx, n_threads); |
| 656 | return HTP_STATUS_OK; |
| 657 | } |
| 658 | |
| 659 | tctx.rows_per_thread = fastdiv(dev_rows + n_threads - 1, &octx->n_threads_div); |
| 660 | |
| 661 | work_queue_run(octx->ctx->work_queue, top_k_mask_preload, &tctx, n_threads); |
| 662 | work_queue_run(octx->ctx->work_queue, top_k_mask_thread_rows, &tctx, n_threads); |
| 663 | |
| 664 | return HTP_STATUS_OK; |
| 665 | } |