Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/ggml/blob/master/src/ggml-hexagon/htp/lightning-indexer-ops.c # ggml [Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685) Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `ggml / src / ggml-hexagon / htp / lightning-indexer-ops.c` 1722 lines ยท 74511 bytes Raw Blame History 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 6 #include 7 8 #include "dma-queue.h" 9 #include "hex-profile.h" 10 #include "hvx-utils.h" 11 #include "hvx-copy.h" 12 #include "htp-vtcm.h" 13 #include "work-queue.h" 14 15 #define GGML_COMMON_DECL_C 16 #include "ggml-common.h" 17 #include "htp-ctx.h" 18 #include "htp-ops.h" 19 #include "htp-tensor.h" 20 #include "hvx-quant.h" 21 #include "hvx-fa-kernels.h" 22 #include "hmx-utils.h" 23 #include "hmx-fa-kernels.h" 24 #include "hmx-queue.h" 25 26 #include "lightning-indexer-ops.h" 27 28 struct htp_lid_context { 29 const struct htp_ops_context * octx; 30 const struct htp_lid_kernel_params * kp; 31 32 uint32_t n_blocks; 33 uint32_t blocks_per_thread; 34 35 // current stream and token tile 36 uint32_t is; 37 uint32_t im3; 38 uint32_t t0; 39 uint32_t nt; 40 41 struct htp_lid_vtcm_layout L; 42 43 uint8_t * spad_q; 44 uint8_t * spad_w; 45 uint8_t * spad_qs; 46 uint8_t * spad_k; 47 uint8_t * spad_kt; 48 uint8_t * spad_m; 49 uint8_t * spad_o; 50 }; 51 52 static inline void lid_prep_step_info(uint32_t k, uint32_t K, uint32_t t_first, uint32_t i0, uint32_t i1, uint32_t n_head, 53 uint32_t * out_t, uint32_t * out_ha, uint32_t * out_hb) { 54 const uint32_t t = t_first + k; 55 const uint32_t ha = (k == 0) ? (i0 - t * n_head) : 0; 56 const uint32_t hb = (k == K - 1) ? (i1 - t * n_head) : n_head; 57 *out_t = t; 58 *out_ha = ha; 59 *out_hb = hb; 60 } 61 62 static inline void lid_prep_push_step(dma_queue * dma_q, const struct htp_lid_context * lctx, 63 const struct htp_tensor * q, const struct htp_tensor * w, 64 uint8_t * qs, uint8_t * ws, uint32_t qs_row_size, uint32_t w_row_size, 65 uint32_t D, uint32_t n_head, uint32_t t, uint32_t ha, uint32_t hb) { 66 const uint32_t it = lctx->t0 + t; 67 dma_queue_push(dma_q, dma_make_data(qs, q->data + it * q->nb[2] + lctx->is * q->nb[3] + ha * q->nb[1]), 68 qs_row_size, q->nb[1], D * sizeof(float), hb - ha); 69 dma_queue_push(dma_q, dma_make_data(ws, w->data + it * w->nb[1] + lctx->is * w->nb[3]), 70 w_row_size, w_row_size, n_head * sizeof(float), 1); 71 } 72 73 // Splat every Q element (f16) and every head weight (f32) into a full vector. 74 // Work is split over (token, head) pairs. 75 static void lid_prep_thread(unsigned int nth, unsigned int ith, void * data) { 76 struct htp_lid_context * lctx = (struct htp_lid_context *) data; 77 const struct htp_ops_context * octx = lctx->octx; 78 const struct htp_tensor * q = octx->src[0]; 79 const struct htp_tensor * w = octx->src[2]; 80 81 const struct htp_lid_vtcm_layout * L = &lctx->L; 82 83 const uint32_t D = q->ne[0]; 84 const uint32_t n_head = q->ne[1]; 85 86 // nth is min(n_threads, n_items): the same chunk as a split over n_threads 87 const uint32_t n_items = lctx->nt * n_head; 88 const uint32_t chunk = fastdiv(n_items + octx->n_threads - 1, &octx->n_threads_div); 89 const uint32_t i0 = ith * chunk; 90 const uint32_t i1 = MIN(i0 + chunk, n_items); 91 92 if (i0 >= i1) { 93 return; 94 } 95 96 struct htp_thread_trace * tr = &octx->ctx->trace[ith]; 97 dma_queue * dma_q = octx->ctx->dma[ith]; 98 99 const uint32_t w_row_size = hex_round_up(n_head * sizeof(float), 128); 100 const size_t qs_size = n_head * L->size_qs_row; 101 const size_t slot_size = qs_size + w_row_size; 102 103 uint8_t * qs[2]; 104 uint8_t * ws[2]; 105 qs[0] = lctx->spad_qs + ith * L->size_qs; 106 ws[0] = qs[0] + qs_size; 107 qs[1] = qs[0] + slot_size; 108 ws[1] = qs[1] + qs_size; 109 uint8_t * qh = qs[0] + 2 * slot_size; 110 111 const uint32_t t_first = fastdiv(i0, &lctx->kp->div_n_head); 112 const uint32_t t_last = fastdiv(i1 - 1, &lctx->kp->div_n_head); 113 const uint32_t n_steps = t_last - t_first + 1; 114 115 // Prologue: prime both slots 116 uint32_t t, ha, hb; 117 lid_prep_step_info(0, n_steps, t_first, i0, i1, n_head, &t, &ha, &hb); 118 lid_prep_push_step(dma_q, lctx, q, w, qs[0], ws[0], L->size_qs_row, w_row_size, D, n_head, t, ha, hb); 119 120 if (n_steps > 1) { 121 lid_prep_step_info(1, n_steps, t_first, i0, i1, n_head, &t, &ha, &hb); 122 lid_prep_push_step(dma_q, lctx, q, w, qs[1], ws[1], L->size_qs_row, w_row_size, D, n_head, t, ha, hb); 123 } 124 125 // Loop: pop(slot) -> compute(slot) -> push(slot, k + 2) 126 for (uint32_t k = 0; k < n_steps; ++k) { 127 const uint32_t slot = k & 1; 128 129 dma_queue_pop(dma_q); 130 dma_queue_pop(dma_q); 131 132 lid_prep_step_info(k, n_steps, t_first, i0, i1, n_head, &t, &ha, &hb); 133 const uint32_t it = lctx->t0 + t; 134 135 htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_A_PREP, (uint16_t) it); 136 137 HVX_Vector * restrict q_sp = (HVX_Vector *) lctx->spad_q + (t * n_head + ha) * D; 138 for (uint32_t h = ha; h < hb; ++h) { 139 hvx_copy_f16_f32_aa(qh, qs[slot] + (h - ha) * L->size_qs_row, D); 140 141 const HVX_Vector * restrict qv = (const HVX_Vector *) qh; 142 for (uint32_t d0 = 0; d0 < D; d0 += VLEN_FP16) { 143 const HVX_Vector v = qv[d0 / VLEN_FP16]; 144 const uint32_t nd = MIN(VLEN_FP16, D - d0); 145 for (uint32_t j = 0; j < nd; ++j) { 146 *q_sp++ = hvx_vec_repl_f16(Q6_V_vror_VR(v, j * SIZEOF_FP16)); 147 } 148 } 149 } 150 151 const HVX_Vector * restrict wv = (const HVX_Vector *) ws[slot]; 152 HVX_Vector * restrict w_dst = (HVX_Vector *) (lctx->spad_w + t * n_head * VLEN); 153 for (uint32_t h = ha; h < hb; ++h) { 154 w_dst[h] = hvx_vec_repl4(Q6_V_vror_VR(wv[h / VLEN_FP32], (h % VLEN_FP32) * SIZEOF_FP32)); 155 } 156 157 htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_A_PREP, (uint16_t) it); 158 159 if (k + 2 < n_steps) { 160 uint32_t next_t, next_ha, next_hb; 161 lid_prep_step_info(k + 2, n_steps, t_first, i0, i1, n_head, &next_t, &next_ha, &next_hb); 162 lid_prep_push_step(dma_q, lctx, q, w, qs[slot], ws[slot], L->size_qs_row, w_row_size, D, n_head, next_t, next_ha, next_hb); 163 } 164 } 165 } 166 167 #if __HVX_ARCH__ < 79 168 #define LID_MUL_F16(a, b) Q6_Vhf_equals_Wqf32(Q6_Wqf32_vmpy_VhfVhf(a, b)) 169 #else 170 #define LID_MUL_F16(a, b) Q6_Vhf_vmpy_VhfVhf(a, b) 171 #endif 172 173 // Q8_0 row to f16 in place, 4 blocks (128 values) per step. 174 // Go backwards: the f16 output of a group is wider than its input and covers the input of the next group. 175 static inline void lid_dequant_row_q8_0(uint8_t * restrict row, uint32_t D) { 176 const int32_t nb = D / QK8_0; 177 178 const HVX_VectorPred q32 = Q6_Q_vsetq_R(32); 179 const HVX_VectorPred q64 = Q6_Q_vsetq_R(64); 180 const HVX_VectorPred q96 = Q6_Q_vsetq_R(96); 181 182 for (int32_t g = (nb - 1) / 4; g >= 0; --g) { 183 const uint8_t * src = row + g * 4 * sizeof(block_q8_0); 184 const HVX_Vector v0 = *(const HVX_UVector *) src; 185 const HVX_Vector v1 = *(const HVX_UVector *) (src + VLEN); 186 187 HVX_Vector qs = Q6_V_vmux_QVV(q96, Q6_V_valign_VVR(v1, v0, 6), Q6_V_valign_VVR(v1, v0, 8)); 188 qs = Q6_V_vmux_QVV(q64, Q6_V_valign_VVR(v1, v0, 4), qs); 189 qs = Q6_V_vmux_QVV(q32, Q6_V_valign_VVR(v1, v0, 2), qs); 190 191 const HVX_Vector s01 = Q6_V_vmux_QVV(q64, hvx_vec_repl_f16(v0), hvx_vec_repl_f16(Q6_V_valign_VVR(v1, v0, 34))); 192 const HVX_Vector s23 = Q6_V_vmux_QVV(q64, hvx_vec_repl_f16(Q6_V_valign_VVR(v1, v0, 68)), hvx_vec_repl_f16(Q6_V_valign_VVR(v1, v0, 102))); 193 194 const HVX_VectorPair qh = Q6_Wh_vunpack_Vb(qs); 195 196 HVX_Vector * dst = (HVX_Vector *) (row + g * 2 * VLEN); 197 dst[0] = Q6_Vhf_equals_Wqf32(Q6_Wqf32_vmpy_VhfVhf(Q6_Vhf_equals_Vh(Q6_V_lo_W(qh)), s01)); 198 if (g * 4 + 2 < nb) { 199 dst[1] = Q6_Vhf_equals_Wqf32(Q6_Wqf32_vmpy_VhfVhf(Q6_Vhf_equals_Vh(Q6_V_hi_W(qh)), s23)); 200 } 201 } 202 } 203 204 // Q4_0 blocks i, i + 1 (v loaded at block i) to signed quants: block i in bytes 0..31, block i + 1 in bytes 32..63. 205 // qs[j] holds value j (low nibble) and j + 16 (high nibble). lo16 selects bytes 0..15 and 32..47. 206 static inline HVX_Vector lid_q4_0_pair_quants(HVX_Vector v, HVX_VectorPred q32, HVX_VectorPred lo16) { 207 const HVX_Vector qs = Q6_V_vmux_QVV(q32, Q6_V_vror_VR(v, sizeof(ggml_half)), 208 Q6_V_vror_VR(v, VLEN - 32 + sizeof(block_q4_0) + sizeof(ggml_half))); 209 const HVX_Vector lo = Q6_Vb_vsub_VbVb(Q6_V_vand_VV(qs, Q6_Vb_vsplat_R(0x0F)), Q6_Vb_vsplat_R(8)); 210 const HVX_Vector hi = Q6_Vb_vsub_VbVb(Q6_Vub_vlsr_VubR(qs, 4), Q6_Vb_vsplat_R(8)); 211 return Q6_V_vmux_QVV(lo16, lo, Q6_V_vlalign_VVR(hi, Q6_V_vzero(), 16)); 212 } 213 214 // Q4_0 row to f16 in place, 2 blocks (64 values) per step. Backwards, as for Q8_0. 215 static inline void lid_dequant_row_q4_0(uint8_t * restrict row, uint32_t D) { 216 const int32_t nb = D / QK4_0; 217 218 const HVX_VectorPred q32 = Q6_Q_vsetq_R(32); 219 const HVX_VectorPred q64 = Q6_Q_vsetq_R(64); 220 const HVX_VectorPred lo16 = Q6_Q_or_QQ(Q6_Q_vsetq_R(16), Q6_Q_and_QQn(Q6_Q_vsetq_R(48), q32)); 221 222 for (int32_t p = (nb - 1) / 2; p >= 0; --p) { 223 const HVX_Vector v = *(const HVX_UVector *) (row + p * 2 * sizeof(block_q4_0)); 224 const HVX_Vector q = lid_q4_0_pair_quants(v, q32, lo16); 225 const HVX_Vector d1 = 2 * p + 1 < nb ? hvx_vec_repl_f16(Q6_V_vror_VR(v, sizeof(block_q4_0))) : Q6_V_vzero(); 226 const HVX_Vector d = Q6_V_vmux_QVV(q64, hvx_vec_repl_f16(v), d1); 227 228 ((HVX_Vector *) row)[p] = LID_MUL_F16(Q6_Vhf_equals_Vh(Q6_V_lo_W(Q6_Wh_vunpack_Vb(q))), d); 229 } 230 } 231 232 // Quantized kv block to f16 rows in place. Not inlined: the dequant constants would stay live across the compute loop. 233 static __attribute__((noinline)) void lid_dequant_block(uint8_t * restrict k_buf, size_t row_stride, uint32_t nkv, uint32_t D, bool q4) { 234 if (q4) { 235 for (uint32_t r = 0; r < nkv; ++r) { 236 lid_dequant_row_q4_0(k_buf + r * row_stride, D); 237 } 238 } else { 239 for (uint32_t r = 0; r < nkv; ++r) { 240 lid_dequant_row_q8_0(k_buf + r * row_stride, D); 241 } 242 } 243 } 244 245 static inline void lid_convert_row_f32_to_f16(uint8_t * row, uint32_t D) { 246 const HVX_Vector zero = Q6_V_vzero(); 247 const HVX_Vector * src = (const HVX_Vector *) row; 248 HVX_Vector * dst = (HVX_Vector *) row; 249 250 for (uint32_t d = 0; d < D; d += 64) { 251 dst[d / 64] = hvx_vec_f32_to_f16(src[d / 32], d + 32 < D ? src[d / 32 + 1] : zero); 252 } 253 } 254 255 // Q8_0 gather transpose limits: tmp (two 64x64 scratch tiles) holds D / 2 pairs, gather offsets are 16 bit 256 static inline bool lid_q8_0_gather_ok(size_t row_size, uint32_t D) { 257 return D <= 4 * HTP_LID_KV_BLOCK && (HTP_LID_KV_BLOCK - 1) * row_size + (D / QK8_0) * sizeof(block_q8_0) <= 32767; 258 } 259 260 // Transpose a raw Q8_0 kv block: one halfword gather reads q[d], q[d + 1] of all 64 rows, then split, widen to f16 and scale. 261 // Lanes of missing rows hold stale data; they are not stored. 262 static inline void lid_transpose_q8_0_gather(HVX_Vector * restrict kt, 263 HVX_Vector * restrict tmp, 264 HVX_Vector * restrict sc, 265 const uint8_t * restrict k_buf, 266 size_t row_size, 267 HVX_Vector offsets, 268 uint32_t D) { 269 const uint32_t mu = HTP_LID_KV_BLOCK * row_size; 270 for (uint32_t b = 0; b < D / QK8_0; ++b) { 271 Q6_vgather_ARMVh(sc + b, (size_t) (k_buf + b * sizeof(block_q8_0)), mu, offsets); 272 } 273 for (uint32_t d = 0; d < D; d += 2) { 274 const uint32_t off = (d / QK8_0) * sizeof(block_q8_0) + sizeof(ggml_half) + d % QK8_0; 275 Q6_vgather_ARMVh(tmp + d / 2, (size_t) (k_buf + off), mu, offsets); 276 } 277 hvx_gather_sync(sc); 278 hvx_gather_sync(tmp); 279 280 for (uint32_t d = 0; d < D; d += 2) { 281 const HVX_Vector s = sc[d / QK8_0]; 282 const HVX_VectorPair qh = Q6_Wh_vunpack_Vb(tmp[d / 2]); 283 const HVX_VectorPair qd = Q6_W_vdeal_VVR(Q6_V_hi_W(qh), Q6_V_lo_W(qh), -2); 284 kt[d + 0] = Q6_Vhf_equals_Wqf32(Q6_Wqf32_vmpy_VhfVhf(Q6_Vhf_equals_Vh(Q6_V_lo_W(qd)), s)); 285 kt[d + 1] = Q6_Vhf_equals_Wqf32(Q6_Wqf32_vmpy_VhfVhf(Q6_Vhf_equals_Vh(Q6_V_hi_W(qd)), s)); 286 } 287 } 288 289 static inline void lid_shuff_pass(HVX_Vector * restrict dst, const HVX_Vector * restrict src) { 290 for (uint32_t j = 0; j < 32; ++j) { 291 HVX_VectorPair p = Q6_W_vshuff_VVR(src[j + 32], src[j], -SIZEOF_FP16); 292 dst[2 * j + 0] = Q6_V_lo_W(p); 293 dst[2 * j + 1] = Q6_V_hi_W(p); 294 } 295 } 296 297 // 64x64 f16 transpose: each shuffle pass rotates the 12-bit (row, lane) index by one bit, six passes swap row and lane 298 static inline void lid_transpose_64x64_f16(HVX_Vector * restrict dst, 299 const uint8_t * restrict src, 300 size_t src_stride, 301 uint32_t nkv, 302 HVX_Vector * restrict t0, 303 HVX_Vector * restrict t1) { 304 if (nkv >= HTP_LID_KV_BLOCK) { 305 for (uint32_t j = 0; j < 32; ++j) { 306 const HVX_Vector r0 = *(const HVX_Vector *) (src + j * src_stride); 307 const HVX_Vector r1 = *(const HVX_Vector *) (src + (j + 32) * src_stride); 308 HVX_VectorPair p = Q6_W_vshuff_VVR(r1, r0, -SIZEOF_FP16); 309 t0[2 * j + 0] = Q6_V_lo_W(p); 310 t0[2 * j + 1] = Q6_V_hi_W(p); 311 } 312 } else { 313 const HVX_Vector zero = Q6_V_vzero(); 314 for (uint32_t j = 0; j < 32; ++j) { 315 const HVX_Vector r0 = (j < nkv) ? *(const HVX_Vector *) (src + j * src_stride) : zero; 316 const HVX_Vector r1 = (j + 32 < nkv) ? *(const HVX_Vector *) (src + (j + 32) * src_stride) : zero; 317 HVX_VectorPair p = Q6_W_vshuff_VVR(r1, r0, -SIZEOF_FP16); 318 t0[2 * j + 0] = Q6_V_lo_W(p); 319 t0[2 * j + 1] = Q6_V_hi_W(p); 320 } 321 } 322 lid_shuff_pass(t1, t0); 323 lid_shuff_pass(t0, t1); 324 lid_shuff_pass(t1, t0); 325 lid_shuff_pass(t0, t1); 326 lid_shuff_pass(dst, t0); 327 } 328 329 #if __HVX_ARCH__ < 79 330 // qf32 accumulators, converted to sf once per head 331 static inline HVX_VectorPair lid_mpyacc(HVX_VectorPair acc, HVX_Vector x, HVX_Vector y) { 332 HVX_VectorPair m = Q6_Wqf32_vmpy_VhfVhf(x, y); 333 return Q6_W_vcombine_VV(Q6_Vqf32_vadd_Vqf32Vqf32(Q6_V_hi_W(acc), Q6_V_hi_W(m)), 334 Q6_Vqf32_vadd_Vqf32Vqf32(Q6_V_lo_W(acc), Q6_V_lo_W(m))); 335 } 336 static inline HVX_Vector lid_acc_to_sf(HVX_Vector v) { 337 return Q6_Vsf_equals_Vqf32(v); 338 } 339 #else 340 static inline HVX_VectorPair lid_mpyacc(HVX_VectorPair acc, HVX_Vector x, HVX_Vector y) { 341 return Q6_Wsf_vmpyacc_WsfVhfVhf(acc, x, y); 342 } 343 static inline HVX_Vector lid_acc_to_sf(HVX_Vector v) { 344 return v; 345 } 346 #endif 347 348 // out += relu(s) * w, for the even and odd kv lanes 349 static inline HVX_VectorPair lid_head_acc(HVX_VectorPair out, HVX_VectorPair s, HVX_Vector w) { 350 const HVX_Vector zero = Q6_V_vzero(); 351 HVX_Vector e = HVX_OP_ADD_F32(Q6_V_lo_W(out), HVX_OP_MUL_F32(Q6_Vsf_vmax_VsfVsf(lid_acc_to_sf(Q6_V_lo_W(s)), zero), w)); 352 HVX_Vector o = HVX_OP_ADD_F32(Q6_V_hi_W(out), HVX_OP_MUL_F32(Q6_Vsf_vmax_VsfVsf(lid_acc_to_sf(Q6_V_hi_W(s)), zero), w)); 353 return Q6_W_vcombine_VV(o, e); 354 } 355 356 // Store one output row: (even, odd) lanes back to natural order, add the mask 357 static inline void lid_store_row(float * restrict o, HVX_VectorPair out, const __fp16 * restrict m) { 358 const HVX_Vector zero = Q6_V_vzero(); 359 360 const HVX_VectorPair out_nat = Q6_W_vshuff_VVR(Q6_V_hi_W(out), Q6_V_lo_W(out), -SIZEOF_FP32); 361 362 HVX_Vector acc0 = Q6_V_lo_W(out_nat); 363 HVX_Vector acc1 = Q6_V_hi_W(out_nat); 364 365 // qf32 math does not keep inf: add a finite mask, then put -inf back 366 const HVX_Vector m_f16 = *(const HVX_Vector *) m; 367 const HVX_VectorPred m_inf = Q6_Q_vcmp_eq_VhVh(m_f16, Q6_Vh_vsplat_R(0xFC00)); 368 369 const HVX_VectorPair m_f32 = hvx_vec_f16_to_f32(Q6_V_vmux_QVV(m_inf, zero, m_f16)); 370 const HVX_VectorPair f_f32 = hvx_vec_f16_to_f32(Q6_V_vmux_QVV(m_inf, Q6_Vh_vsplat_R(0x3C00), zero)); 371 372 const HVX_Vector ninf = Q6_V_vsplat_R(0xFF800000); 373 374 acc0 = HVX_OP_ADD_F32(acc0, Q6_V_lo_W(m_f32)); 375 acc1 = HVX_OP_ADD_F32(acc1, Q6_V_hi_W(m_f32)); 376 acc0 = Q6_V_vmux_QVV(Q6_Q_vcmp_gt_VwVw(Q6_V_lo_W(f_f32), zero), ninf, acc0); 377 acc1 = Q6_V_vmux_QVV(Q6_Q_vcmp_gt_VwVw(Q6_V_hi_W(f_f32), zero), ninf, acc1); 378 379 HVX_Vector * restrict vo = (HVX_Vector *) o; 380 vo[0] = acc0; 381 vo[1] = acc1; 382 } 383 384 // One output row (token) of a kv block: sum_h relu(q_h . k) * w_h + mask 385 // kt holds the K block transposed (one f16 vector of 64 kv per d), q_t holds the Q splats of the token. 386 static inline void lid_compute_row(float * restrict o, 387 const HVX_Vector * restrict q_t, 388 const HVX_Vector * restrict w_t, 389 const HVX_Vector * restrict kt, 390 const __fp16 * restrict m, 391 uint32_t D, 392 uint32_t n_head) { 393 // f16 x f16 products land in (even kv, odd kv) lane order 394 HVX_VectorPair out = Q6_W_vzero(); 395 396 uint32_t h = 0; 397 for (; h + 4 <= n_head; h += 4) { 398 const HVX_Vector * restrict q0 = q_t + h * D; 399 const HVX_Vector * restrict q1 = q0 + D; 400 const HVX_Vector * restrict q2 = q1 + D; 401 const HVX_Vector * restrict q3 = q2 + D; 402 403 HVX_VectorPair s0 = Q6_W_vzero(); 404 HVX_VectorPair s1 = Q6_W_vzero(); 405 HVX_VectorPair s2 = Q6_W_vzero(); 406 HVX_VectorPair s3 = Q6_W_vzero(); 407 408 for (uint32_t d = 0; d < D; ++d) { 409 const HVX_Vector k = kt[d]; 410 s0 = lid_mpyacc(s0, k, q0[d]); 411 s1 = lid_mpyacc(s1, k, q1[d]); 412 s2 = lid_mpyacc(s2, k, q2[d]); 413 s3 = lid_mpyacc(s3, k, q3[d]); 414 } 415 416 out = lid_head_acc(out, s0, w_t[h + 0]); 417 out = lid_head_acc(out, s1, w_t[h + 1]); 418 out = lid_head_acc(out, s2, w_t[h + 2]); 419 out = lid_head_acc(out, s3, w_t[h + 3]); 420 } 421 for (; h < n_head; ++h) { 422 const HVX_Vector * restrict q0 = q_t + h * D; 423 424 HVX_VectorPair s0 = Q6_W_vzero(); 425 for (uint32_t d = 0; d < D; ++d) { 426 s0 = lid_mpyacc(s0, kt[d], q0[d]); 427 } 428 out = lid_head_acc(out, s0, w_t[h]); 429 } 430 431 lid_store_row(o, out, m); 432 } 433 434 // Same as lid_compute_row for two kv blocks at once: each Q splat load feeds both blocks 435 static inline void lid_compute_row2(float * restrict o0, 436 float * restrict o1, 437 const HVX_Vector * restrict q_t, 438 const HVX_Vector * restrict w_t, 439 const HVX_Vector * restrict kt0, 440 const HVX_Vector * restrict kt1, 441 const __fp16 * restrict m0, 442 const __fp16 * restrict m1, 443 uint32_t D, 444 uint32_t n_head) { 445 HVX_VectorPair out0 = Q6_W_vzero(); 446 HVX_VectorPair out1 = Q6_W_vzero(); 447 448 uint32_t h = 0; 449 for (; h + 4 <= n_head; h += 4) { 450 const HVX_Vector * restrict q0 = q_t + h * D; 451 const HVX_Vector * restrict q1 = q0 + D; 452 const HVX_Vector * restrict q2 = q1 + D; 453 const HVX_Vector * restrict q3 = q2 + D; 454 455 HVX_VectorPair a0 = Q6_W_vzero(); 456 HVX_VectorPair a1 = Q6_W_vzero(); 457 HVX_VectorPair a2 = Q6_W_vzero(); 458 HVX_VectorPair a3 = Q6_W_vzero(); 459 HVX_VectorPair b0 = Q6_W_vzero(); 460 HVX_VectorPair b1 = Q6_W_vzero(); 461 HVX_VectorPair b2 = Q6_W_vzero(); 462 HVX_VectorPair b3 = Q6_W_vzero(); 463 464 for (uint32_t d = 0; d < D; ++d) { 465 const HVX_Vector k0 = kt0[d]; 466 const HVX_Vector k1 = kt1[d]; 467 const HVX_Vector v0 = q0[d]; 468 const HVX_Vector v1 = q1[d]; 469 const HVX_Vector v2 = q2[d]; 470 const HVX_Vector v3 = q3[d]; 471 a0 = lid_mpyacc(a0, k0, v0); 472 b0 = lid_mpyacc(b0, k1, v0); 473 a1 = lid_mpyacc(a1, k0, v1); 474 b1 = lid_mpyacc(b1, k1, v1); 475 a2 = lid_mpyacc(a2, k0, v2); 476 b2 = lid_mpyacc(b2, k1, v2); 477 a3 = lid_mpyacc(a3, k0, v3); 478 b3 = lid_mpyacc(b3, k1, v3); 479 } 480 481 out0 = lid_head_acc(out0, a0, w_t[h + 0]); 482 out0 = lid_head_acc(out0, a1, w_t[h + 1]); 483 out0 = lid_head_acc(out0, a2, w_t[h + 2]); 484 out0 = lid_head_acc(out0, a3, w_t[h + 3]); 485 out1 = lid_head_acc(out1, b0, w_t[h + 0]); 486 out1 = lid_head_acc(out1, b1, w_t[h + 1]); 487 out1 = lid_head_acc(out1, b2, w_t[h + 2]); 488 out1 = lid_head_acc(out1, b3, w_t[h + 3]); 489 } 490 for (; h < n_head; ++h) { 491 const HVX_Vector * restrict q0 = q_t + h * D; 492 493 HVX_VectorPair a0 = Q6_W_vzero(); 494 HVX_VectorPair b0 = Q6_W_vzero(); 495 for (uint32_t d = 0; d < D; ++d) { 496 const HVX_Vector v0 = q0[d]; 497 a0 = lid_mpyacc(a0, kt0[d], v0); 498 b0 = lid_mpyacc(b0, kt1[d], v0); 499 } 500 out0 = lid_head_acc(out0, a0, w_t[h]); 501 out1 = lid_head_acc(out1, b0, w_t[h]); 502 } 503 504 lid_store_row(o0, out0, m0); 505 lid_store_row(o1, out1, m1); 506 } 507 508 // Each thread owns a range of kv blocks and streams K / mask in, output out (HTP_LID_N_BUF buffers) 509 static void lid_compute_thread(unsigned int nth, unsigned int ith, void * data) { 510 struct htp_lid_context * lctx = (struct htp_lid_context *) data; 511 const struct htp_ops_context * octx = lctx->octx; 512 const struct htp_tensor * q = octx->src[0]; 513 const struct htp_tensor * k = octx->src[1]; 514 const struct htp_tensor * m = octx->src[3]; 515 const struct htp_tensor * dst = octx->dst; 516 517 const uint32_t b0 = lctx->blocks_per_thread * ith; 518 const uint32_t b1 = MIN(b0 + lctx->blocks_per_thread, lctx->n_blocks); 519 520 if (b0 >= b1) { 521 return; 522 } 523 524 const struct htp_lid_vtcm_layout * L = &lctx->L; 525 526 const uint32_t D = q->ne[0]; 527 const uint32_t n_head = q->ne[1]; 528 const uint32_t n_kv = k->ne[2]; 529 const uint32_t nt = lctx->nt; 530 531 const bool is_f32 = (k->type == HTP_TYPE_F32); 532 const bool is_q8_0 = (k->type == HTP_TYPE_Q8_0); 533 const bool is_q4_0 = (k->type == HTP_TYPE_Q4_0); 534 const uint32_t k_row_size = lctx->kp->k_row_size; 535 536 const uint32_t nbk2 = k->nb[2]; 537 const uint32_t nbm1 = m->nb[1]; 538 const uint32_t nb1 = dst->nb[1]; 539 540 const dma_addr_t k_base = k->data + lctx->is * k->nb[3]; 541 const dma_addr_t m_base = m->data + lctx->t0 * nbm1 + lctx->im3 * m->nb[3]; 542 const dma_addr_t d_base = dst->data + lctx->t0 * nb1 + lctx->is * dst->nb[3]; 543 544 uint8_t * spad_k = lctx->spad_k + ith * HTP_LID_N_BUF * L->size_k_buf; 545 uint8_t * spad_m = lctx->spad_m + ith * HTP_LID_N_BUF * L->size_m_buf; 546 uint8_t * spad_o = lctx->spad_o + ith * HTP_LID_N_BUF * L->size_o_buf; 547 548 HVX_Vector * kt[2]; 549 kt[0] = (HVX_Vector *) (lctx->spad_kt + ith * L->size_kt); 550 kt[1] = kt[0] + hex_round_up(D, HTP_LID_KV_BLOCK); 551 HVX_Vector * kt_t0 = kt[1] + hex_round_up(D, HTP_LID_KV_BLOCK); 552 HVX_Vector * kt_t1 = kt_t0 + HTP_LID_KV_BLOCK; 553 554 // Q8_0: transpose by gather from the raw rows (sc: one vector of block scales per 32 d) 555 const bool q8_g = is_q8_0 && lid_q8_0_gather_ok(L->size_k_raw_row, D); 556 HVX_Vector * sc = kt_t1 + HTP_LID_KV_BLOCK; 557 const HVX_Vector g_offs = hvx_vec_gather_offsets_h(L->size_k_raw_row); 558 559 struct htp_thread_trace * tr = &octx->ctx->trace[ith]; 560 dma_queue * dma_q = octx->ctx->dma[ith]; 561 562 for (uint32_t b = b0, i = 0; b < b1 && i < HTP_LID_N_BUF; ++b, ++i) { 563 const uint32_t kv0 = b * HTP_LID_KV_BLOCK; 564 const uint32_t nkv = MIN(HTP_LID_KV_BLOCK, n_kv - kv0); 565 566 dma_queue_push(dma_q, dma_make_data(d_base, spad_o + i * L->size_o_buf), 567 nb1, L->size_o_row, nkv * sizeof(float), 0); 568 dma_queue_push(dma_q, dma_make_data(spad_k + i * L->size_k_buf, k_base + kv0 * nbk2), 569 L->size_k_raw_row, nbk2, k_row_size, nkv); 570 dma_queue_push(dma_q, dma_make_data(spad_m + i * L->size_m_buf, m_base + kv0 * sizeof(__fp16)), 571 L->size_m_row, nbm1, nkv * sizeof(__fp16), nt); 572 } 573 574 // Two kv blocks per step, the next two are in flight 575 for (uint32_t b = b0; b < b1; b += 2) { 576 const uint32_t nb = MIN(2, b1 - b); 577 578 uint8_t * o_buf[2]; 579 uint8_t * k_buf[2]; 580 uint8_t * m_buf[2]; 581 582 for (uint32_t j = 0; j < nb; ++j) { 583 const uint32_t nkv = MIN(HTP_LID_KV_BLOCK, n_kv - (b + j) * HTP_LID_KV_BLOCK); 584 585 o_buf[j] = (uint8_t *) (uintptr_t) dma_queue_pop(dma_q).src; 586 k_buf[j] = (uint8_t *) (uintptr_t) dma_queue_pop(dma_q).dst; 587 m_buf[j] = (uint8_t *) (uintptr_t) dma_queue_pop(dma_q).dst; 588 589 if ((is_q8_0 && !q8_g) || is_q4_0) { 590 htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_W_DEQUANT, (uint16_t) (b + j)); 591 lid_dequant_block(k_buf[j], L->size_k_raw_row, nkv, D, is_q4_0); 592 htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_W_DEQUANT, (uint16_t) (b + j)); 593 } 594 595 htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_W_PREP, (uint16_t) (b + j)); 596 if (is_f32) { 597 for (uint32_t r = 0; r < nkv; ++r) { 598 lid_convert_row_f32_to_f16(k_buf[j] + r * L->size_k_raw_row, D); 599 } 600 } 601 if (q8_g) { 602 lid_transpose_q8_0_gather(kt[j], kt_t0, sc, k_buf[j], L->size_k_raw_row, g_offs, D); 603 } else { 604 for (uint32_t d0 = 0; d0 < D; d0 += HTP_LID_KV_BLOCK) { 605 lid_transpose_64x64_f16(kt[j] + d0, k_buf[j] + d0 * sizeof(__fp16), L->size_k_raw_row, nkv, kt_t0, kt_t1); 606 } 607 } 608 htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_W_PREP, (uint16_t) (b + j)); 609 } 610 611 htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) b); 612 for (uint32_t t = 0; t < nt; ++t) { 613 const HVX_Vector * q_t = (const HVX_Vector *) lctx->spad_q + t * n_head * D; 614 const HVX_Vector * w_t = (const HVX_Vector *) (lctx->spad_w + t * n_head * VLEN); 615 if (nb == 2) { 616 lid_compute_row2((float *) (o_buf[0] + t * L->size_o_row), (float *) (o_buf[1] + t * L->size_o_row), 617 q_t, w_t, kt[0], kt[1], 618 (const __fp16 *) (m_buf[0] + t * L->size_m_row), (const __fp16 *) (m_buf[1] + t * L->size_m_row), 619 D, n_head); 620 } else { 621 lid_compute_row((float *) (o_buf[0] + t * L->size_o_row), q_t, w_t, kt[0], 622 (const __fp16 *) (m_buf[0] + t * L->size_m_row), D, n_head); 623 } 624 } 625 htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) b); 626 627 for (uint32_t j = 0; j < nb; ++j) { 628 const uint32_t kv0 = (b + j) * HTP_LID_KV_BLOCK; 629 const uint32_t nkv = MIN(HTP_LID_KV_BLOCK, n_kv - kv0); 630 631 dma_queue_push(dma_q, dma_make_data(d_base + kv0 * sizeof(float), o_buf[j]), 632 nb1, L->size_o_row, nkv * sizeof(float), nt); 633 634 if (b + j + HTP_LID_N_BUF < b1) { 635 const uint32_t kv2 = kv0 + HTP_LID_N_BUF * HTP_LID_KV_BLOCK; 636 const uint32_t nkv2 = MIN(HTP_LID_KV_BLOCK, n_kv - kv2); 637 638 dma_queue_push(dma_q, dma_make_data(k_buf[j], k_base + kv2 * nbk2), 639 L->size_k_raw_row, nbk2, k_row_size, nkv2); 640 dma_queue_push(dma_q, dma_make_data(m_buf[j], m_base + kv2 * sizeof(__fp16)), 641 L->size_m_row, nbm1, nkv2 * sizeof(__fp16), nt); 642 } 643 } 644 } 645 646 for (uint32_t i = 0; i < MIN(b1 - b0, HTP_LID_N_BUF); ++i) { 647 dma_queue_pop(dma_q); 648 } 649 650 (void) nth; 651 } 652 653 // ----- HMX path: K is the activation, Q the weights, head sum by a second HMX pass ----- 654 // 655 // Channel c = t * n_head + h of a token tile. Per kv row tile (32 kv) and token tile u (32 tokens): 656 // mm1: S[kv, c] = relu(k . q_c) * |w_c| (convert scale |w_c|, pos clamp) 657 // mm2: O[kv, t] = sum_c S[kv, c] * G[c, t], G[c, t] = sign(w_c) for c of token t 658 // O tiles hold f16 (kv pair, token) words; gather one token column, add the mask, store f32. 659 // 660 // A run preps the Q sets of up to n_qsets (stream, token tile) pairs, then pipelines all their kv rows. 661 // Row x of a run is kv x % kv_seg of set x / kv_seg (kv_seg = n_kv padded to 64), so a block of Bc rows can 662 // span sets: it is split in segments, one per set. HMX computes block g while HVX preps K of g + 1 and extracts O of g - 1. 663 664 struct lid_hmx_set { 665 uint32_t is; 666 uint32_t im3; 667 uint32_t t0; 668 uint32_t nt; 669 uint32_t n_ch; 670 uint32_t n_u; 671 }; 672 673 struct htp_lid_hmx_context { 674 const struct htp_ops_context * octx; 675 const struct htp_lid_kernel_params * kp; 676 struct htp_lid_hmx_vtcm_layout L; 677 678 uint32_t Bc; 679 uint32_t kv_seg; // rows per set: n_kv padded to 64 680 uint32_t n_blocks; // per run 681 uint32_t n_tile; // tokens per token tile 682 uint32_t n_tt; // token tiles per stream 683 684 uint32_t n_qsets; // Q sets per buffer 685 uint32_t qbuf; // Q buffer of the run (two buffers: the next run's Q is prepped during this run) 686 687 uint32_t tile0; // first (stream, token tile) of the run 688 uint32_t n_run; // sets in the run 689 uint32_t n_qthr; // threads with Q work in the run 690 uint32_t nt_run; // tokens of each set in the run 691 uint32_t n_u_run; // token tiles of 32 of each set in the run 692 int32_t g; // current stage 693 694 struct lid_hmx_set sets[HTP_LID_HMX_MAX_QSETS]; 695 696 // next run, Q prepped in the stages of this one 697 bool nx_on; 698 uint32_t nx_n_run; 699 struct fastdiv_values nx_stg_div; // stages of this run: n_blocks + 1 700 struct lid_hmx_set nx_sets[HTP_LID_HMX_MAX_QSETS]; 701 702 __fp16 * qw; 703 __fp16 * gw; 704 uint8_t * sc; 705 uint8_t * sc_id; 706 __fp16 * s; 707 float * w; 708 uint8_t * qs; 709 uint8_t * k_raw[2]; 710 __fp16 * k_t[2]; 711 __fp16 * o[2]; 712 uint8_t * m[4]; 713 uint8_t * out[2]; 714 }; 715 716 #define LID_HMX_MAX_SEGS HTP_LID_HMX_MAX_QSETS 717 718 // Rows of one set in a block: valid rows [x0, x0 + nv) of the block are kv [kv0, kv0 + nv) of the set 719 struct lid_hmx_seg { 720 uint32_t set; 721 uint32_t x0; 722 uint32_t kv0; 723 uint32_t nv; 724 }; 725 726 static inline struct lid_hmx_set lid_hmx_set_init(const struct htp_lid_hmx_context * hctx, uint32_t set) { 727 const struct htp_ops_context * octx = hctx->octx; 728 const uint32_t ti = hctx->tile0 + set; 729 730 struct lid_hmx_set S; 731 S.is = fastdiv(ti, &hctx->kp->div_n_tt); 732 S.im3 = fastmodulo(S.is, octx->src[3]->ne[3], &hctx->kp->div_m_ne3); 733 S.t0 = (ti - S.is * hctx->n_tt) * hctx->n_tile; 734 S.nt = MIN(hctx->n_tile, octx->src[0]->ne[2] - S.t0); 735 S.n_ch = (S.nt * octx->src[0]->ne[1] + 31) / 32; 736 S.n_u = (S.nt + 31) / 32; 737 return S; 738 } 739 740 // Segments of block g with valid rows. Returns their count, n_rows gets the block rows (multiple of 64). 741 static inline uint32_t lid_hmx_segs(const struct htp_lid_hmx_context * hctx, uint32_t g, struct lid_hmx_seg * seg, uint32_t * n_rows) { 742 const uint32_t n_kv = hctx->octx->src[1]->ne[2]; 743 const uint32_t ks = hctx->kv_seg; 744 const uint32_t xa = g * hctx->Bc; 745 const uint32_t xb = MIN(xa + hctx->Bc, hctx->n_run * ks); 746 747 uint32_t n = 0; 748 for (uint32_t x = xa; x < xb;) { 749 const uint32_t set = fastdiv(x, &hctx->kp->div_kv_seg); 750 const uint32_t kv = x - set * ks; 751 const uint32_t end = MIN(xb, (set + 1) * ks); 752 if (kv < n_kv) { 753 seg[n].set = set; 754 seg[n].x0 = x - xa; 755 seg[n].kv0 = kv; 756 seg[n].nv = MIN(n_kv, kv + (end - x)) - kv; 757 n++; 758 } 759 x = end; 760 } 761 *n_rows = xb - xa; 762 return n; 763 } 764 765 typedef struct { 766 const __fp16 * qw; 767 const __fp16 * gw; 768 const uint8_t * sc; 769 uint32_t rt0; 770 uint32_t n_rt; 771 } lid_hmx_job_seg_t; 772 773 typedef struct { 774 lid_hmx_job_seg_t seg[LID_HMX_MAX_SEGS]; 775 uint32_t n_seg; 776 777 const __fp16 * k_t; 778 const uint8_t * sc_id; 779 __fp16 * s; 780 __fp16 * o; 781 uint32_t n_ch; 782 uint32_t n_u; 783 uint32_t n_dot; 784 uint32_t n_head; 785 uint32_t n_sg; 786 } lid_hmx_job_t; 787 788 // acc += sum of n tile products, a and b advance one tile per step 789 static inline void lid_hmx_dot(const __fp16 * a, const __fp16 * b, uint32_t n) { 790 for (; n > 32; n -= 32) { 791 asm volatile(HMX_LOAD_MPY_DEEP_F16("%1", "%2", "%0") :: "r"(65535), "r"(a), "r"(b)); 792 a += 32 * HMX_FP16_TILE_N_ELMS; 793 b += 32 * HMX_FP16_TILE_N_ELMS; 794 } 795 asm volatile(HMX_LOAD_MPY_DEEP_F16("%1", "%2", "%0") :: "r"(n * HMX_FP16_TILE_SIZE - 1), "r"(a), "r"(b)); 796 } 797 798 // Per segment: channel tile outer, row tile inner, one bias load per channel tile. S holds [n_rt][n_sg] tiles. 799 static void lid_hmx_worker(void * data) { 800 const lid_hmx_job_t * job = (const lid_hmx_job_t *) data; 801 802 const size_t dot_stride = (size_t) job->n_dot * HMX_FP16_TILE_N_ELMS; 803 const size_t s_stride = (size_t) job->n_sg * HMX_FP16_TILE_N_ELMS; 804 const uint32_t nh = job->n_head; 805 806 for (uint32_t i = 0; i < job->n_seg; ++i) { 807 const lid_hmx_job_seg_t * sg = &job->seg[i]; 808 const uint32_t n_rt = sg->n_rt; 809 const __fp16 * kt = job->k_t + sg->rt0 * dot_stride; 810 __fp16 * ot = job->o + sg->rt0 * job->n_u * HMX_FP16_TILE_N_ELMS; 811 812 for (uint32_t u = 0; u < job->n_u; ++u) { 813 const uint32_t j0 = u * nh; 814 const uint32_t j1 = MIN(j0 + nh, job->n_ch); 815 816 for (uint32_t c = j0; c < j1; ++c) { 817 const __fp16 * qw = sg->qw + c * dot_stride; 818 __fp16 * st = job->s + (c - j0) * HMX_FP16_TILE_N_ELMS; 819 820 asm volatile(HMX_SET_BIAS("%0") :: "r"(sg->sc + c * 256)); 821 for (uint32_t rt = 0; rt < n_rt; ++rt) { 822 asm volatile(HMX_CLRACC_F16()); 823 lid_hmx_dot(kt + rt * dot_stride, qw, job->n_dot); 824 asm volatile("mxmem(%0, %1):after:pos.hf = acc\n" :: "r"(st + rt * s_stride), "r"(0) : "memory"); 825 } 826 } 827 828 asm volatile(HMX_SET_BIAS("%0") :: "r"(job->sc_id)); 829 for (uint32_t rt = 0; rt < n_rt; ++rt) { 830 asm volatile(HMX_CLRACC_F16()); 831 lid_hmx_dot(job->s + rt * s_stride, sg->gw + j0 * HMX_FP16_TILE_N_ELMS, j1 - j0); 832 asm volatile(HMX_STORE_AFTER_F16("%0", "%1") :: "r"(ot + (rt * job->n_u + u) * HMX_FP16_TILE_N_ELMS), "r"(0) : "memory"); 833 } 834 } 835 } 836 } 837 838 // Thread slices: K rows by row pairs of the padded block, mask / output rows by token. 839 // Block 0 is prepped together with Q: its K rows go to the threads without Q work. 840 // K rows are split only in stages on all n_threads threads. 841 static inline void lid_hmx_rows(const struct htp_lid_hmx_context * hctx, uint32_t ith, uint32_t nth, uint32_t g, uint32_t n_rows, 842 uint32_t * r0, uint32_t * r1) { 843 uint32_t per; 844 if (g == 0 && hctx->n_qthr < nth) { 845 if (ith < hctx->n_qthr) { 846 *r0 = *r1 = 0; 847 return; 848 } 849 ith -= hctx->n_qthr; 850 per = (n_rows + nth - hctx->n_qthr - 1) / (nth - hctx->n_qthr); 851 } else { 852 per = fastdiv(n_rows + hctx->octx->n_threads - 1, &hctx->octx->n_threads_div); 853 } 854 per = hex_round_up(per, 2); 855 *r0 = MIN(ith * per, n_rows); 856 *r1 = MIN(*r0 + per, n_rows); 857 } 858 859 // Split over n_threads; a stage on one thread has one token, which this split also gives to thread 0 860 static inline void lid_hmx_toks(const struct htp_lid_hmx_context * hctx, uint32_t ith, uint32_t nt, uint32_t * t0, uint32_t * t1) { 861 const uint32_t per = fastdiv(nt + hctx->octx->n_threads - 1, &hctx->octx->n_threads_div); 862 *t0 = MIN(ith * per, nt); 863 *t1 = MIN((ith + 1) * per, nt); 864 } 865 866 // Push (or only count, push == false) the K row and mask DMAs of block g for this thread, one per segment 867 static uint32_t lid_hmx_in(const struct htp_lid_hmx_context * hctx, uint32_t ith, uint32_t nth, uint32_t g, bool push) { 868 const struct htp_ops_context * octx = hctx->octx; 869 const struct htp_tensor * k = octx->src[1]; 870 const struct htp_tensor * m = octx->src[3]; 871 const struct htp_lid_hmx_vtcm_layout * L = &hctx->L; 872 873 struct lid_hmx_seg seg[LID_HMX_MAX_SEGS]; 874 uint32_t n_rows; 875 const uint32_t n_seg = lid_hmx_segs(hctx, g, seg, &n_rows); 876 877 uint32_t r0, r1, ta, tb; 878 lid_hmx_rows(hctx, ith, nth, g, n_rows, &r0, &r1); 879 lid_hmx_toks(hctx, ith, hctx->nt_run, &ta, &tb); 880 881 dma_queue * dma_q = octx->ctx->dma[ith]; 882 const uint32_t k_row_size = hctx->kp->k_row_size; 883 884 uint32_t n = 0; 885 for (uint32_t i = 0; i < n_seg; ++i) { 886 const struct lid_hmx_set S = hctx->sets[seg[i].set]; 887 888 const uint32_t ra = MAX(r0, seg[i].x0); 889 const uint32_t rb = MIN(r1, seg[i].x0 + seg[i].nv); 890 if (ra < rb) { 891 if (push) { 892 dma_queue_push(dma_q, dma_make_data(hctx->k_raw[g % 2] + ra * L->size_k_raw_row, 893 k->data + S.is * k->nb[3] + (seg[i].kv0 + ra - seg[i].x0) * k->nb[2]), 894 L->size_k_raw_row, k->nb[2], k_row_size, rb - ra); 895 } 896 n++; 897 } 898 if (ta < tb) { 899 if (push) { 900 dma_queue_push(dma_q, dma_make_data(hctx->m[g % 4] + ta * L->size_m_row + seg[i].x0 * sizeof(__fp16), 901 m->data + S.im3 * m->nb[3] + (S.t0 + ta) * m->nb[1] + seg[i].kv0 * sizeof(__fp16)), 902 L->size_m_row, m->nb[1], seg[i].nv * sizeof(__fp16), tb - ta); 903 } 904 n++; 905 } 906 } 907 return n; 908 } 909 910 static inline uint32_t lid_hmx_push_in(const struct htp_lid_hmx_context * hctx, uint32_t ith, uint32_t nth, uint32_t g) { 911 return lid_hmx_in(hctx, ith, nth, g, true); 912 } 913 914 // Row pair (a, b) quants of two blocks (64 values each, bytes 0..63) to the two tile vectors, scaled by s0, s1 915 static inline void lid_q8_0_pair_store(HVX_Vector * restrict dst, HVX_Vector a, HVX_Vector b, HVX_Vector s0, HVX_Vector s1, bool two) { 916 const HVX_VectorPair qh = Q6_Wh_vunpack_Vb(Q6_V_lo_W(Q6_W_vshuff_VVR(b, a, -1))); 917 dst[0] = LID_MUL_F16(Q6_Vhf_equals_Vh(Q6_V_lo_W(qh)), s0); 918 if (two) { 919 dst[16] = LID_MUL_F16(Q6_Vhf_equals_Vh(Q6_V_hi_W(qh)), s1); 920 } 921 } 922 923 // Raw Q8_0 rows [r0, r1) to activation tiles, dequant fused with the row pair interleave. Rows >= nv are zero. 924 // Per row pair and two blocks: interleave the quants of both rows as bytes, widen, then scale by the (d_r, d_r+1) word. 925 static inline void lid_hmx_prep_k_q8_0(__fp16 * restrict k_t, const uint8_t * restrict k_raw, size_t row_stride, 926 uint32_t r0, uint32_t r1, uint32_t nv, uint32_t n_dot) { 927 const HVX_Vector zero = Q6_V_vzero(); 928 const HVX_VectorPred q32 = Q6_Q_vsetq_R(32); 929 930 for (uint32_t r = r0; r < r1; r += 2) { 931 const uint8_t * p0 = k_raw + r * row_stride; 932 const uint8_t * p1 = p0 + row_stride; 933 const bool v0 = r < nv; 934 const bool v1 = r + 1 < nv; 935 936 HVX_Vector * dst = (HVX_Vector *) (k_t + (r / 32) * n_dot * HMX_FP16_TILE_N_ELMS) + (r % 32) / 2; 937 938 if (n_dot == 4) { 939 // D = 128: the row is 136 bytes, two aligned loads; scales at halfwords 0, 17, 34, 51 940 const HVX_Vector a0 = v0 ? ((const HVX_Vector *) p0)[0] : zero; 941 const HVX_Vector a1 = v0 ? ((const HVX_Vector *) p0)[1] : zero; 942 const HVX_Vector b0 = v1 ? ((const HVX_Vector *) p1)[0] : zero; 943 const HVX_Vector b1 = v1 ? ((const HVX_Vector *) p1)[1] : zero; 944 945 const HVX_VectorPair ds = Q6_W_vshuff_VVR(b0, a0, -SIZEOF_FP16); 946 947 lid_q8_0_pair_store(dst, 948 Q6_V_vmux_QVV(q32, Q6_V_valign_VVR(a0, a0, 2), Q6_V_valign_VVR(a0, a0, 4)), 949 Q6_V_vmux_QVV(q32, Q6_V_valign_VVR(b0, b0, 2), Q6_V_valign_VVR(b0, b0, 4)), 950 hvx_vec_repl4(Q6_V_lo_W(ds)), hvx_vec_repl4(Q6_V_vror_VR(Q6_V_lo_W(ds), 17 * 4)), true); 951 lid_q8_0_pair_store(dst + 32, 952 Q6_V_vmux_QVV(q32, Q6_V_valign_VVR(a1, a0, 70), Q6_V_valign_VVR(a1, a0, 72)), 953 Q6_V_vmux_QVV(q32, Q6_V_valign_VVR(b1, b0, 70), Q6_V_valign_VVR(b1, b0, 72)), 954 hvx_vec_repl4(Q6_V_vror_VR(Q6_V_hi_W(ds), 2 * 4)), hvx_vec_repl4(Q6_V_vror_VR(Q6_V_hi_W(ds), 19 * 4)), true); 955 continue; 956 } 957 958 for (uint32_t i = 0; i < n_dot; i += 2) { 959 const uint8_t * pa = p0 + i * sizeof(block_q8_0); 960 const uint8_t * pb = p1 + i * sizeof(block_q8_0); 961 962 // quants of blocks i, i + 1 in bytes 0..63, scales d_i at byte 0 and d_i+1 at byte 34 963 const HVX_Vector a = v0 ? Q6_V_vmux_QVV(q32, *(const HVX_UVector *) (pa + 2), *(const HVX_UVector *) (pa + 4)) : zero; 964 const HVX_Vector b = v1 ? Q6_V_vmux_QVV(q32, *(const HVX_UVector *) (pb + 2), *(const HVX_UVector *) (pb + 4)) : zero; 965 const HVX_Vector da = v0 ? *(const HVX_UVector *) pa : zero; 966 const HVX_Vector db = v1 ? *(const HVX_UVector *) pb : zero; 967 const HVX_Vector ds = Q6_V_lo_W(Q6_W_vshuff_VVR(db, da, -SIZEOF_FP16)); 968 969 lid_q8_0_pair_store(dst + i * 16, a, b, hvx_vec_repl4(ds), hvx_vec_repl4(Q6_V_vror_VR(ds, 2 * sizeof(block_q8_0))), i + 1 < n_dot); 970 } 971 } 972 } 973 974 // Raw Q4_0 rows [r0, r1) to activation tiles, same as Q8_0: per row pair and two blocks, one load per row. 975 // Rows >= nv are zero. 976 static inline void lid_hmx_prep_k_q4_0(__fp16 * restrict k_t, const uint8_t * restrict k_raw, size_t row_stride, 977 uint32_t r0, uint32_t r1, uint32_t nv, uint32_t n_dot) { 978 const HVX_Vector zero = Q6_V_vzero(); 979 const HVX_VectorPred q32 = Q6_Q_vsetq_R(32); 980 const HVX_VectorPred lo16 = Q6_Q_or_QQ(Q6_Q_vsetq_R(16), Q6_Q_and_QQn(Q6_Q_vsetq_R(48), q32)); 981 982 for (uint32_t r = r0; r < r1; r += 2) { 983 const uint8_t * p0 = k_raw + r * row_stride; 984 const uint8_t * p1 = p0 + row_stride; 985 const bool v0 = r < nv; 986 const bool v1 = r + 1 < nv; 987 988 HVX_Vector * dst = (HVX_Vector *) (k_t + (r / 32) * n_dot * HMX_FP16_TILE_N_ELMS) + (r % 32) / 2; 989 990 for (uint32_t i = 0; i < n_dot; i += 2) { 991 // blocks i, i + 1: scales d_i at byte 0 and d_i+1 at byte 18 992 const HVX_Vector da = v0 ? *(const HVX_UVector *) (p0 + i * sizeof(block_q4_0)) : zero; 993 const HVX_Vector db = v1 ? *(const HVX_UVector *) (p1 + i * sizeof(block_q4_0)) : zero; 994 const HVX_Vector ds = Q6_V_lo_W(Q6_W_vshuff_VVR(db, da, -SIZEOF_FP16)); 995 996 lid_q8_0_pair_store(dst + i * 16, lid_q4_0_pair_quants(da, q32, lo16), lid_q4_0_pair_quants(db, q32, lo16), 997 hvx_vec_repl4(ds), hvx_vec_repl4(Q6_V_vror_VR(ds, 2 * sizeof(block_q4_0))), i + 1 < n_dot); 998 } 999 } 1000 } 1001 1002 // F16 rows [r0, r1) to activation tiles: row pairs interleaved. Rows >= nv are zero. 1003 static inline void lid_hmx_prep_k_f16(__fp16 * restrict k_t, const uint8_t * restrict k_raw, size_t row_stride, 1004 uint32_t r0, uint32_t r1, uint32_t nv, uint32_t n_dot) { 1005 const HVX_Vector zero = Q6_V_vzero(); 1006 1007 for (uint32_t r = r0; r < r1; r += 2) { 1008 const HVX_Vector * p0 = (const HVX_Vector *) (k_raw + r * row_stride); 1009 const HVX_Vector * p1 = (const HVX_Vector *) (k_raw + (r + 1) * row_stride); 1010 const bool v0 = r < nv; 1011 const bool v1 = r + 1 < nv; 1012 1013 HVX_Vector * dst = (HVX_Vector *) (k_t + (r / 32) * n_dot * HMX_FP16_TILE_N_ELMS) + (r % 32) / 2; 1014 1015 for (uint32_t i = 0; i < n_dot; i += 2) { 1016 const HVX_VectorPair p = Q6_W_vshuff_VVR(v1 ? p1[i / 2] : zero, v0 ? p0[i / 2] : zero, -SIZEOF_FP16); 1017 dst[i * 16] = Q6_V_lo_W(p); 1018 if (i + 1 < n_dot) { 1019 dst[(i + 1) * 16] = Q6_V_hi_W(p); 1020 } 1021 } 1022 } 1023 } 1024 1025 // K rows of block g (this thread's rows) to activation tiles, one piece per set; set padding rows are zero. 1026 // Not inlined: keeps the stage's registers out of the prep loops. 1027 static __attribute__((noinline)) void lid_hmx_prep_k(const struct htp_lid_hmx_context * hctx, uint32_t ith, uint32_t nth, uint32_t g) { 1028 const struct htp_lid_hmx_vtcm_layout * L = &hctx->L; 1029 const struct htp_tensor * k = hctx->octx->src[1]; 1030 1031 const uint32_t n_kv = k->ne[2]; 1032 const uint32_t ks = hctx->kv_seg; 1033 const uint32_t xa = g * hctx->Bc; 1034 const uint32_t n_rows = MIN(hctx->Bc, hctx->n_run * ks - xa); 1035 1036 uint32_t r0, r1; 1037 lid_hmx_rows(hctx, ith, nth, g, n_rows, &r0, &r1); 1038 1039 uint8_t * k_raw = hctx->k_raw[g % 2]; 1040 __fp16 * k_t = hctx->k_t[g % 2]; 1041 const bool f32 = k->type == HTP_TYPE_F32; 1042 const bool q8 = k->type == HTP_TYPE_Q8_0; 1043 const bool q4 = k->type == HTP_TYPE_Q4_0; 1044 1045 for (uint32_t r = r0; r < r1;) { 1046 const uint32_t x = xa + r; 1047 const uint32_t set = fastdiv(x, &hctx->kp->div_kv_seg); 1048 const uint32_t kv = x - set * ks; 1049 const uint32_t end = MIN(r1, (set + 1) * ks - xa); 1050 const uint32_t nv = r + (kv < n_kv ? n_kv - kv : 0); 1051 1052 if (q8) { 1053 lid_hmx_prep_k_q8_0(k_t, k_raw, L->size_k_raw_row, r, end, nv, L->n_dot); 1054 } else if (q4) { 1055 lid_hmx_prep_k_q4_0(k_t, k_raw, L->size_k_raw_row, r, end, nv, L->n_dot); 1056 } else { 1057 if (f32) { 1058 for (uint32_t i = r; i < end; ++i) { 1059 lid_convert_row_f32_to_f16(k_raw + i * L->size_k_raw_row, k->ne[0]); 1060 } 1061 } 1062 lid_hmx_prep_k_f16(k_t, k_raw, L->size_k_raw_row, r, end, nv, L->n_dot); 1063 } 1064 r = end; 1065 } 1066 } 1067 1068 // O column of each token (64 kv per gather) + mask -> f32 rows, then one DMA out per segment 1069 static __attribute__((noinline)) void lid_hmx_extract(const struct htp_lid_hmx_context * hctx, uint32_t ith, uint32_t nth, uint32_t g) { 1070 const struct htp_ops_context * octx = hctx->octx; 1071 const struct htp_tensor * dst = octx->dst; 1072 const struct htp_lid_hmx_vtcm_layout * L = &hctx->L; 1073 1074 uint32_t ta, tb; 1075 lid_hmx_toks(hctx, ith, hctx->nt_run, &ta, &tb); 1076 if (ta >= tb) { 1077 return; 1078 } 1079 1080 struct lid_hmx_seg seg[LID_HMX_MAX_SEGS]; 1081 uint32_t n_rows; 1082 const uint32_t n_seg = lid_hmx_segs(hctx, g, seg, &n_rows); 1083 1084 struct htp_thread_trace * tr = &octx->ctx->trace[ith]; 1085 htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_O_PROC, (uint16_t) g); 1086 1087 const uint32_t n_u = hctx->n_u_run; 1088 const uint32_t mu = (n_u + 1) * HMX_FP16_TILE_SIZE - 1; 1089 1090 int32_t offs[32] __attribute__((aligned(128))); 1091 for (int l = 0; l < 32; l++) { 1092 offs[l] = (l / 16) * n_u * HMX_FP16_TILE_SIZE + (l % 16) * 128; 1093 } 1094 const HVX_Vector v_offs = *(const HVX_Vector *) offs; 1095 1096 HVX_Vector * gbuf = (HVX_Vector *) (hctx->qs + ith * L->size_qs + 2 * 32 * L->size_qs_row + 32 * L->size_qh_row); 1097 1098 const HVX_Vector zero = Q6_V_vzero(); 1099 const HVX_Vector ninf = Q6_V_vsplat_R(0xFF800000); 1100 const HVX_Vector one = Q6_Vh_vsplat_R(0x3C00); 1101 const HVX_Vector hinf = Q6_Vh_vsplat_R(0xFC00); 1102 1103 const __fp16 * o_base = hctx->o[g % 2]; 1104 const uint8_t * m_buf = hctx->m[g % 4]; 1105 uint8_t * o_buf = hctx->out[g % 2]; 1106 1107 for (uint32_t i = 0; i < n_seg; ++i) { 1108 const uint32_t q0 = seg[i].x0 / 64; 1109 const uint32_t q1 = (seg[i].x0 + seg[i].nv + 63) / 64; 1110 1111 for (uint32_t t = ta; t < tb; ++t) { 1112 const uint32_t u = t / 32; 1113 const HVX_Vector off = Q6_Vw_vadd_VwVw(v_offs, Q6_V_vsplat_R((t % 32) * 4)); 1114 1115 for (uint32_t q = q0; q < q1; ++q) { 1116 const __fp16 * base = o_base + (2 * q * n_u + u) * HMX_FP16_TILE_N_ELMS; 1117 Q6_vgather_ARMVw(gbuf + q - q0, (size_t) base, mu, off); 1118 } 1119 hvx_gather_sync(gbuf + q1 - q0 - 1); 1120 1121 const HVX_Vector * mv = (const HVX_Vector *) (m_buf + t * L->size_m_row); 1122 HVX_Vector * ov = (HVX_Vector *) (o_buf + t * L->size_o_row); 1123 1124 for (uint32_t q = q0; q < q1; ++q) { 1125 const HVX_VectorPair s = hvx_vec_f16_to_f32(gbuf[q - q0]); 1126 1127 // qf32 math does not keep inf: add a finite mask, then put -inf back 1128 const HVX_Vector m_f16 = mv[q]; 1129 const HVX_VectorPred m_inf = Q6_Q_vcmp_eq_VhVh(m_f16, hinf); 1130 const HVX_VectorPair m_f32 = hvx_vec_f16_to_f32(Q6_V_vmux_QVV(m_inf, zero, m_f16)); 1131 const HVX_VectorPair f_f32 = hvx_vec_f16_to_f32(Q6_V_vmux_QVV(m_inf, one, zero)); 1132 1133 const HVX_Vector acc0 = HVX_OP_ADD_F32(Q6_V_lo_W(s), Q6_V_lo_W(m_f32)); 1134 const HVX_Vector acc1 = HVX_OP_ADD_F32(Q6_V_hi_W(s), Q6_V_hi_W(m_f32)); 1135 ov[2 * q + 0] = Q6_V_vmux_QVV(Q6_Q_vcmp_gt_VwVw(Q6_V_lo_W(f_f32), zero), ninf, acc0); 1136 ov[2 * q + 1] = Q6_V_vmux_QVV(Q6_Q_vcmp_gt_VwVw(Q6_V_hi_W(f_f32), zero), ninf, acc1); 1137 } 1138 } 1139 1140 const struct lid_hmx_set S = hctx->sets[seg[i].set]; 1141 dma_queue_push(octx->ctx->dma[ith], 1142 dma_make_data(dst->data + S.is * dst->nb[3] + (S.t0 + ta) * dst->nb[1] + seg[i].kv0 * sizeof(float), 1143 o_buf + ta * L->size_o_row + seg[i].x0 * sizeof(float)), 1144 dst->nb[1], L->size_o_row, seg[i].nv * sizeof(float), tb - ta); 1145 } 1146 1147 htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_O_PROC, (uint16_t) g); 1148 } 1149 1150 // Number of output DMAs of block g for this thread 1151 static inline uint32_t lid_hmx_n_out(const struct htp_lid_hmx_context * hctx, uint32_t ith, uint32_t nth, uint32_t g) { 1152 uint32_t ta, tb; 1153 lid_hmx_toks(hctx, ith, hctx->nt_run, &ta, &tb); 1154 if (ta >= tb) { 1155 return 0; 1156 } 1157 struct lid_hmx_seg seg[LID_HMX_MAX_SEGS]; 1158 uint32_t n_rows; 1159 return lid_hmx_segs(hctx, g, seg, &n_rows); 1160 } 1161 1162 static __attribute__((noinline)) void lid_hmx_q_chunk(const struct htp_lid_hmx_context * hctx, uint32_t ith, uint32_t nth); 1163 1164 // Pipeline stage g (HMX runs block g meanwhile): prep block g + 1, extract block g - 1 1165 static void lid_hmx_stage(const struct htp_lid_hmx_context * hctx, uint32_t ith, uint32_t nth) { 1166 const int32_t g = hctx->g; 1167 const int32_t n = (int32_t) hctx->n_blocks; 1168 1169 struct htp_thread_trace * tr = &hctx->octx->ctx->trace[ith]; 1170 dma_queue * dma_q = hctx->octx->ctx->dma[ith]; 1171 1172 // FIFO order per thread: OUT(g - 3), IN(g + 1), OUT(g - 2). When this stage preps next run Q, also pop 1173 // OUT(g - 2) so the queue is empty for the Q DMAs; else leave it in flight for one more stage. 1174 const int32_t lag = hctx->nx_on ? 2 : 3; 1175 1176 uint32_t n_pop = 0; 1177 if (lag == 3 && g - 3 >= 0) { 1178 n_pop += lid_hmx_n_out(hctx, ith, nth, g - 3); 1179 } 1180 if (g >= 0 && g + 1 < n) { 1181 n_pop += lid_hmx_in(hctx, ith, nth, g + 1, false); 1182 } 1183 if (lag == 2 && g - 2 >= 0) { 1184 n_pop += lid_hmx_n_out(hctx, ith, nth, g - 2); 1185 } 1186 for (uint32_t i = 0; i < n_pop; ++i) { 1187 dma_queue_pop(dma_q); 1188 } 1189 1190 if (hctx->nx_on && g >= 0) { 1191 htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_A_PREP, (uint16_t) g); 1192 lid_hmx_q_chunk(hctx, ith, nth); 1193 htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_A_PREP, (uint16_t) g); 1194 } 1195 if (g + 2 < n) { 1196 lid_hmx_push_in(hctx, ith, nth, g + 2); 1197 } 1198 if (g + 1 < n) { 1199 htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_W_PREP, (uint16_t) (g + 1)); 1200 lid_hmx_prep_k(hctx, ith, nth, g + 1); 1201 htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_W_PREP, (uint16_t) (g + 1)); 1202 } 1203 if (g - 1 >= 0) { 1204 lid_hmx_extract(hctx, ith, nth, g - 1); 1205 } 1206 if (g == n) { 1207 uint32_t n_tail = lid_hmx_n_out(hctx, ith, nth, n - 1); 1208 if (lag == 3 && n >= 2) { 1209 n_tail += lid_hmx_n_out(hctx, ith, nth, n - 2); 1210 } 1211 for (uint32_t i = 0; i < n_tail; ++i) { 1212 dma_queue_pop(dma_q); 1213 } 1214 } 1215 } 1216 1217 static void lid_hmx_stage_thread(unsigned int nth, unsigned int ith, void * data) { 1218 lid_hmx_stage((const struct htp_lid_hmx_context *) data, ith, nth); 1219 } 1220 1221 // Push the Q rows of channel tile c of set S, one DMA per token. Returns the number of DMA descriptors. 1222 static uint32_t lid_hmx_push_q(const struct htp_lid_hmx_context * hctx, dma_queue * dma_q, uint8_t * qs, 1223 const struct lid_hmx_set * S, uint32_t c) { 1224 const struct htp_tensor * q = hctx->octx->src[0]; 1225 const uint32_t nh = q->ne[1]; 1226 const uint32_t ch = c * 32; 1227 const uint32_t nr = MIN(32, S->nt * nh - ch); 1228 1229 // channel ch + r is (token t, head h); a DMA ends at the token end, so the next one starts at h 0 1230 uint32_t t = fastdiv(ch, &hctx->kp->div_n_head); 1231 uint32_t h = ch - t * nh; 1232 uint32_t n = 0; 1233 for (uint32_t r = 0; r < nr; ++t, h = 0) { 1234 const uint32_t cnt = MIN(nh - h, nr - r); 1235 dma_queue_push(dma_q, dma_make_data(qs + r * hctx->L.size_qs_row, 1236 q->data + S->is * q->nb[3] + (S->t0 + t) * q->nb[2] + h * q->nb[1]), 1237 hctx->L.size_qs_row, q->nb[1], q->ne[0] * sizeof(float), cnt); 1238 r += cnt; 1239 n++; 1240 } 1241 return n; 1242 } 1243 1244 // Scales (|w| per channel, f16 in the low half of each word) and G tile (sign(w) at row c, column token(c) % 32) 1245 // of one channel tile. w points at the tile's 32 channels, the first one is (token t, head h). 1246 static inline void lid_hmx_build_sc_g(uint8_t * restrict sc, __fp16 * restrict gt, const float * restrict w, 1247 uint32_t t, uint32_t h, uint32_t nr, uint32_t nh) { 1248 const HVX_Vector zero = Q6_V_vzero(); 1249 const HVX_Vector wv = Q6_V_vmux_QVV(Q6_Q_vsetq2_R(nr * sizeof(float)), *(const HVX_Vector *) w, zero); 1250 1251 HVX_Vector * scv = (HVX_Vector *) sc; 1252 scv[0] = hvx_vec_f32_to_f16_shuff(Q6_V_vand_VV(wv, Q6_V_vsplat_R(0x7FFFFFFF)), zero); 1253 scv[1] = zero; 1254 1255 // sign(w) as f16 bits in the low half of each word, then packed: row r in halfword r 1256 const HVX_VectorPred nz = Q6_Q_vcmp_gt_VuwVuw(Q6_V_vand_VV(wv, Q6_V_vsplat_R(0x7FFFFFFF)), zero); 1257 const HVX_Vector sgn = Q6_V_vor_VV(Q6_V_vsplat_R(0x3C00), Q6_Vw_vasr_VwR(Q6_V_vand_VV(wv, Q6_V_vsplat_R(0x80000000)), 16)); 1258 const HVX_Vector sv = Q6_Vh_vpacke_VwVw(zero, Q6_V_vmux_QVV(nz, sgn, zero)); 1259 1260 // token column of each row; -1 for missing rows (only rows 0..31 are used) 1261 int16_t tcol[64] __attribute__((aligned(128))) = { 0 }; 1262 for (uint32_t r = 0; r < 32; ++r) { 1263 tcol[r] = r < nr ? (int16_t) (t % 32) : -1; 1264 if (++h == nh) { 1265 h = 0; 1266 t++; 1267 } 1268 } 1269 const HVX_Vector tv = *(const HVX_Vector *) tcol; 1270 1271 // halfword 2n and 2n + 1 of a G vector are column n 1272 static const int16_t col[64] __attribute__((aligned(128))) = { 1273 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 1274 11, 11, 12, 12, 13, 13, 14, 14, 15, 15, 16, 16, 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 1275 22, 22, 23, 23, 24, 24, 25, 25, 26, 26, 27, 27, 28, 28, 29, 29, 30, 30, 31, 31, 1276 }; 1277 const HVX_Vector cv = *(const HVX_Vector *) col; 1278 1279 // vector p holds rows 2p, 2p + 1 1280 HVX_Vector * gv = (HVX_Vector *) gt; 1281 for (uint32_t p = 0; p < 16; ++p) { 1282 const HVX_Vector tp = hvx_vec_repl4(Q6_V_vror_VR(tv, p * 4)); 1283 const HVX_Vector sp = hvx_vec_repl4(Q6_V_vror_VR(sv, p * 4)); 1284 gv[p] = Q6_V_vmux_QVV(Q6_Q_vcmp_eq_VhVh(cv, tp), sp, zero); 1285 } 1286 } 1287 1288 // Q buffer slot of set 1289 static inline uint32_t lid_hmx_slot(const struct htp_lid_hmx_context * hctx, uint32_t qbuf, uint32_t set) { 1290 return qbuf * hctx->n_qsets + set; 1291 } 1292 1293 // Scales and G of channel tile c of a Q slot (needs the slot's w) 1294 static __attribute__((noinline)) void lid_hmx_q_scg(const struct htp_lid_hmx_context * hctx, uint32_t slot, const struct lid_hmx_set * S, uint32_t c) { 1295 const struct htp_lid_hmx_vtcm_layout * L = &hctx->L; 1296 const uint32_t nh = hctx->octx->src[0]->ne[1]; 1297 const uint32_t ch = c * 32; 1298 const uint32_t t = fastdiv(ch, &hctx->kp->div_n_head); 1299 1300 lid_hmx_build_sc_g(hctx->sc + (slot * L->n_ch + c) * 256, hctx->gw + (slot * L->n_ch + c) * HMX_FP16_TILE_N_ELMS, 1301 hctx->w + slot * (L->size_w / sizeof(float)) + ch, t, ch - t * nh, MIN(32, S->nt * nh - ch), nh); 1302 } 1303 1304 // Staged f32 Q rows of channel tile c to f16, then to the weight tiles of a Q slot 1305 static __attribute__((noinline)) void lid_hmx_q_tiles(const struct htp_lid_hmx_context * hctx, uint32_t slot, const struct lid_hmx_set * S, uint32_t c, 1306 const uint8_t * qs, uint8_t * qh) { 1307 const struct htp_lid_hmx_vtcm_layout * L = &hctx->L; 1308 const uint32_t D = hctx->octx->src[0]->ne[0]; 1309 const uint32_t nh = hctx->octx->src[0]->ne[1]; 1310 const uint32_t nr = MIN(32, S->nt * nh - c * 32); 1311 1312 for (uint32_t r = 0; r < nr; ++r) { 1313 hvx_copy_f16_f32_aa(qh + r * L->size_qh_row, qs + r * L->size_qs_row, D); 1314 } 1315 1316 // missing channels must be zero (finite): zero the tiles, then scatter only the valid rows 1317 __fp16 * qw = hctx->qw + (slot * L->n_ch + c) * L->n_dot * HMX_FP16_TILE_N_ELMS; 1318 if (nr < 32) { 1319 for (uint32_t i = 0; i < L->n_dot * 16; ++i) { 1320 ((HVX_Vector *) qw)[i] = Q6_V_vzero(); 1321 } 1322 } 1323 hmx_interleave_rows_to_tiles(qw, (const __fp16 *) qh, nr, D, L->size_qh_row / sizeof(__fp16), 0, hex_round_up(nr, 2)); 1324 hvx_gather_sync(qw); 1325 } 1326 1327 static inline uint32_t lid_hmx_find_valid_gc(const struct htp_lid_hmx_context * hctx, uint32_t gc, uint32_t gc1) { 1328 const struct fastdiv_values * div_n_ch = &hctx->kp->div_n_ch; 1329 const uint32_t n_ch = hctx->L.n_ch; 1330 while (gc < gc1) { 1331 const uint32_t set = fastdiv(gc, div_n_ch); 1332 const uint32_t c = gc - set * n_ch; 1333 if (c < hctx->nx_sets[set].n_ch) { 1334 return gc; 1335 } 1336 gc++; 1337 } 1338 return gc1; 1339 } 1340 1341 static inline uint32_t lid_hmx_q_chunk_push(const struct htp_lid_hmx_context * hctx, dma_queue * dma_q, 1342 uint8_t * qs, uint32_t qb, uint32_t gc) { 1343 const struct htp_ops_context * octx = hctx->octx; 1344 const struct htp_tensor * w = octx->src[2]; 1345 const struct htp_lid_hmx_vtcm_layout * L = &hctx->L; 1346 const uint32_t nh = octx->src[0]->ne[1]; 1347 1348 const uint32_t set = fastdiv(gc, &hctx->kp->div_n_ch); 1349 const uint32_t c = gc - set * L->n_ch; 1350 const struct lid_hmx_set * S = &hctx->nx_sets[set]; 1351 1352 const uint32_t slot = lid_hmx_slot(hctx, qb, set); 1353 const uint32_t ch = c * 32; 1354 const uint32_t ta = fastdiv(ch, &hctx->kp->div_n_head); 1355 const uint32_t tb = fastdiv(MIN(ch + 32, S->nt * nh) - 1, &hctx->kp->div_n_head) + 1; 1356 1357 float * wd = hctx->w + slot * (L->size_w / sizeof(float)); 1358 dma_queue_push(dma_q, dma_make_data(wd + ta * nh, w->data + S->is * w->nb[3] + (S->t0 + ta) * w->nb[1]), 1359 nh * sizeof(float), w->nb[1], nh * sizeof(float), tb - ta); 1360 return 1 + lid_hmx_push_q(hctx, dma_q, qs, S, c); 1361 } 1362 1363 // Stage g share of the next run's Q prep (the DMA queue of the thread is empty here) 1364 static __attribute__((noinline)) void lid_hmx_q_chunk(const struct htp_lid_hmx_context * hctx, uint32_t ith, uint32_t nth) { 1365 const struct htp_ops_context * octx = hctx->octx; 1366 const struct htp_lid_hmx_vtcm_layout * L = &hctx->L; 1367 1368 const uint32_t n_tot = hctx->nx_n_run * L->n_ch; 1369 const uint32_t a = fastdiv(hctx->g * n_tot, &hctx->nx_stg_div); 1370 const uint32_t b = fastdiv((hctx->g + 1) * n_tot, &hctx->nx_stg_div); 1371 const uint32_t per = fastdiv(b - a + octx->n_threads - 1, &octx->n_threads_div); 1372 const uint32_t gc0 = MIN(a + ith * per, b); 1373 const uint32_t gc1 = MIN(gc0 + per, b); 1374 const uint32_t qb = hctx->qbuf ^ 1; 1375 1376 dma_queue * dma_q = octx->ctx->dma[ith]; 1377 uint8_t * qs[2]; 1378 qs[0] = hctx->qs + ith * L->size_qs; 1379 qs[1] = qs[0] + 32 * L->size_qs_row; 1380 uint8_t * qh = qs[0] + 2 * 32 * L->size_qs_row; 1381 1382 uint32_t gc_cur = lid_hmx_find_valid_gc(hctx, gc0, gc1); 1383 if (gc_cur >= gc1) { 1384 return; 1385 } 1386 1387 const uint32_t gc_next = lid_hmx_find_valid_gc(hctx, gc_cur + 1, gc1); 1388 uint32_t gc_prefetch = gc1; 1389 1390 uint32_t n_dma[2] = {0, 0}; 1391 n_dma[0] = lid_hmx_q_chunk_push(hctx, dma_q, qs[0], qb, gc_cur); 1392 if (gc_next < gc1) { 1393 n_dma[1] = lid_hmx_q_chunk_push(hctx, dma_q, qs[1], qb, gc_next); 1394 gc_prefetch = lid_hmx_find_valid_gc(hctx, gc_next + 1, gc1); 1395 } 1396 1397 uint32_t slot = 0; 1398 while (gc_cur < gc1) { 1399 for (uint32_t i = 0; i < n_dma[slot]; ++i) { 1400 dma_queue_pop(dma_q); 1401 } 1402 1403 const uint32_t set = fastdiv(gc_cur, &hctx->kp->div_n_ch); 1404 const uint32_t c = gc_cur - set * L->n_ch; 1405 const struct lid_hmx_set * S = &hctx->nx_sets[set]; 1406 const uint32_t qslot = lid_hmx_slot(hctx, qb, set); 1407 1408 lid_hmx_q_scg(hctx, qslot, S, c); 1409 lid_hmx_q_tiles(hctx, qslot, S, c, qs[slot], qh); 1410 1411 if (gc_prefetch < gc1) { 1412 n_dma[slot] = lid_hmx_q_chunk_push(hctx, dma_q, qs[slot], qb, gc_prefetch); 1413 gc_prefetch = lid_hmx_find_valid_gc(hctx, gc_prefetch + 1, gc1); 1414 } else { 1415 n_dma[slot] = 0; 1416 } 1417 1418 gc_cur = lid_hmx_find_valid_gc(hctx, gc_cur + 1, gc1); 1419 slot ^= 1; 1420 } 1421 } 1422 1423 // Q rows to weight tiles, per channel scales |w| and G tiles for all sets of the run, then stage -1 (prep block 0). 1424 // Q work goes to the first threads (thread 0 starts first), the K rows of block 0 to the others. 1425 static void lid_hmx_qprep_thread(unsigned int nth, unsigned int ith, void * data) { 1426 struct htp_lid_hmx_context * hctx = (struct htp_lid_hmx_context *) data; 1427 const struct htp_ops_context * octx = hctx->octx; 1428 const struct htp_tensor * q = octx->src[0]; 1429 const struct htp_tensor * w = octx->src[2]; 1430 const struct htp_lid_hmx_vtcm_layout * L = &hctx->L; 1431 1432 const uint32_t nh = q->ne[1]; 1433 1434 struct htp_thread_trace * tr = &octx->ctx->trace[ith]; 1435 dma_queue * dma_q = octx->ctx->dma[ith]; 1436 1437 const uint32_t n_tot = hctx->n_run * L->n_ch; 1438 const uint32_t per = fastdiv(n_tot + octx->n_threads - 1, &octx->n_threads_div); 1439 const uint32_t gc0 = MIN(ith * per, n_tot); 1440 const uint32_t gc1 = MIN(gc0 + per, n_tot); 1441 1442 uint8_t * qs = hctx->qs + ith * L->size_qs; 1443 uint8_t * qh = qs + 2 * 32 * L->size_qs_row; 1444 1445 // all input DMAs in flight at once: w of the sets, Q of the first channel tile, then K and mask of block 0 1446 uint32_t n_dma = 0; 1447 uint32_t first = gc1; 1448 const struct fastdiv_values * div_n_ch = &hctx->kp->div_n_ch; 1449 for (uint32_t set = fastdiv(gc0, div_n_ch); gc0 < gc1 && set <= fastdiv(gc1 - 1, div_n_ch); ++set) { 1450 const struct lid_hmx_set S = hctx->sets[set]; 1451 1452 const uint32_t c0 = MAX(gc0, set * L->n_ch) - set * L->n_ch; 1453 const uint32_t c1 = MIN(MIN(gc1, (set + 1) * L->n_ch) - set * L->n_ch, S.n_ch); 1454 if (c0 >= c1) { 1455 continue; 1456 } 1457 first = MIN(first, set * L->n_ch + c0); 1458 1459 const uint32_t ta = fastdiv(c0 * 32, &hctx->kp->div_n_head); 1460 const uint32_t tb = fastdiv(MIN(c1 * 32, S.nt * nh) - 1, &hctx->kp->div_n_head) + 1; 1461 float * wd = hctx->w + lid_hmx_slot(hctx, hctx->qbuf, set) * (L->size_w / sizeof(float)); 1462 dma_queue_push(dma_q, dma_make_data(wd + ta * nh, w->data + S.is * w->nb[3] + (S.t0 + ta) * w->nb[1]), 1463 nh * sizeof(float), w->nb[1], nh * sizeof(float), tb - ta); 1464 n_dma++; 1465 } 1466 1467 if (first < gc1) { 1468 const uint32_t set = fastdiv(first, div_n_ch); 1469 const struct lid_hmx_set S = hctx->sets[set]; 1470 n_dma += lid_hmx_push_q(hctx, dma_q, qs, &S, first - set * L->n_ch); 1471 } 1472 1473 uint32_t n_in = lid_hmx_push_in(hctx, ith, nth, 0); 1474 1475 for (uint32_t i = 0; i < n_dma; ++i) { 1476 dma_queue_pop(dma_q); 1477 } 1478 1479 htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_A_PREP, (uint16_t) gc0); 1480 1481 for (uint32_t gc = first; gc < gc1; ++gc) { 1482 const uint32_t set = fastdiv(gc, div_n_ch); 1483 const uint32_t c = gc - set * L->n_ch; 1484 const struct lid_hmx_set S = hctx->sets[set]; 1485 if (c >= S.n_ch) { 1486 continue; 1487 } 1488 1489 uint32_t n_q = 0; 1490 if (gc != first) { 1491 n_q = lid_hmx_push_q(hctx, dma_q, qs, &S, c) + n_in; 1492 n_in = 0; 1493 } 1494 1495 // scales and G while the Q rows land 1496 const uint32_t slot = lid_hmx_slot(hctx, hctx->qbuf, set); 1497 lid_hmx_q_scg(hctx, slot, &S, c); 1498 1499 for (uint32_t i = 0; i < n_q; ++i) { 1500 dma_queue_pop(dma_q); 1501 } 1502 1503 lid_hmx_q_tiles(hctx, slot, &S, c, qs, qh); 1504 } 1505 1506 htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_A_PREP, (uint16_t) gc0); 1507 1508 for (uint32_t i = 0; i < n_in; ++i) { 1509 dma_queue_pop(dma_q); 1510 } 1511 1512 lid_hmx_stage(hctx, ith, nth); 1513 } 1514 1515 // Run start when its Q was prepped in the previous run: block 0 K and mask, then stage -1 1516 static void lid_hmx_first_thread(unsigned int nth, unsigned int ith, void * data) { 1517 const struct htp_lid_hmx_context * hctx = (const struct htp_lid_hmx_context *) data; 1518 1519 const uint32_t n_in = lid_hmx_push_in(hctx, ith, nth, 0); 1520 for (uint32_t i = 0; i < n_in; ++i) { 1521 dma_queue_pop(hctx->octx->ctx->dma[ith]); 1522 } 1523 lid_hmx_stage(hctx, ith, nth); 1524 } 1525 1526 static int lid_hmx_run(struct htp_ops_context * octx, const struct htp_lid_kernel_params * kparams) { 1527 const struct htp_tensor * q = octx->src[0]; 1528 1529 const uint32_t D = q->ne[0]; 1530 const uint32_t n_head = q->ne[1]; 1531 const uint32_t n_stream = q->ne[3]; 1532 const uint32_t n_threads = kparams->n_threads; 1533 const uint32_t n_qsets = kparams->n_qsets; 1534 1535 struct htp_lid_hmx_context hctx; 1536 memset(&hctx, 0, sizeof(hctx)); 1537 hctx.octx = octx; 1538 hctx.kp = kparams; 1539 hctx.Bc = kparams->kv_block; 1540 hctx.kv_seg = kparams->kv_seg; 1541 hctx.n_tile = kparams->n_tokens_tile; 1542 hctx.n_tt = kparams->n_tt; 1543 hctx.n_qsets = n_qsets; 1544 htp_lid_hmx_vtcm_layout_build(&hctx.L, D, n_head, hctx.n_tile, hctx.Bc, n_qsets * kparams->n_qbuf, n_threads, 1545 kparams->k_row_size); 1546 1547 uint8_t * const base = octx->ctx->vtcm_base; 1548 const struct htp_lid_hmx_vtcm_layout * L = &hctx.L; 1549 1550 hctx.qw = VTCM_LAYOUT_PTR(__fp16, base, L->off_qw); 1551 hctx.gw = VTCM_LAYOUT_PTR(__fp16, base, L->off_g); 1552 hctx.sc = VTCM_LAYOUT_PTR(uint8_t, base, L->off_sc); 1553 hctx.sc_id = VTCM_LAYOUT_PTR(uint8_t, base, L->off_sc_id); 1554 hctx.s = VTCM_LAYOUT_PTR(__fp16, base, L->off_s); 1555 hctx.w = VTCM_LAYOUT_PTR(float, base, L->off_w); 1556 hctx.qs = VTCM_LAYOUT_PTR(uint8_t, base, L->off_qs); 1557 for (int i = 0; i < 2; i++) { 1558 hctx.k_raw[i] = VTCM_LAYOUT_PTR(uint8_t, base, L->off_k_raw[i]); 1559 hctx.k_t[i] = VTCM_LAYOUT_PTR(__fp16, base, L->off_k_t[i]); 1560 hctx.o[i] = VTCM_LAYOUT_PTR(__fp16, base, L->off_o[i]); 1561 hctx.out[i] = VTCM_LAYOUT_PTR(uint8_t, base, L->off_out[i]); 1562 } 1563 for (int i = 0; i < 4; i++) { 1564 hctx.m[i] = VTCM_LAYOUT_PTR(uint8_t, base, L->off_m[i]); 1565 } 1566 1567 hmx_init_column_scales(hctx.sc_id, Q6_V_vsplat_R(0x3c00)); // identity 1.0 f16 1568 1569 hmx_queue_t hmx_q = octx->ctx->hmx_queue; 1570 lid_hmx_job_t job[2]; 1571 1572 const uint32_t n_tiles = n_stream * hctx.n_tt; 1573 1574 for (uint32_t tile0 = 0, r = 0; tile0 < n_tiles; tile0 += n_qsets, ++r) { 1575 hctx.tile0 = tile0; 1576 hctx.n_run = MIN(n_qsets, n_tiles - tile0); 1577 hctx.qbuf = r & (kparams->n_qbuf - 1); // n_qbuf is 1 or 2 1578 1579 for (uint32_t i = 0; i < hctx.n_run; ++i) { 1580 hctx.sets[i] = lid_hmx_set_init(&hctx, i); 1581 } 1582 1583 // all sets of a run have the same token count: one set per run, or all tokens in one tile 1584 const struct lid_hmx_set S0 = hctx.sets[0]; 1585 hctx.nt_run = S0.nt; 1586 hctx.n_u_run = S0.n_u; 1587 hctx.n_blocks = hctx.n_run == n_qsets ? kparams->n_kv_blocks : (hctx.n_run * hctx.kv_seg + hctx.Bc - 1) / hctx.Bc; 1588 1589 const int32_t n = (int32_t) hctx.n_blocks; 1590 const uint32_t n_tot = hctx.n_run * L->n_ch; 1591 const uint32_t per = fastdiv(n_tot + n_threads - 1, &octx->n_threads_div); 1592 1593 hctx.n_qthr = (n_tot + per - 1) / per; 1594 hctx.g = -1; 1595 1596 const bool q_ready = hctx.nx_on; 1597 if (q_ready) { 1598 hctx.n_qthr = 0; 1599 hctx.nx_on = false; 1600 work_queue_run(octx->ctx->work_queue, lid_hmx_first_thread, &hctx, n_threads); 1601 } else { 1602 work_queue_run(octx->ctx->work_queue, lid_hmx_qprep_thread, &hctx, n_threads); 1603 } 1604 1605 // with two Q buffers the stages of this run prep the Q of the next one 1606 if (kparams->n_qbuf == 2 && tile0 + n_qsets < n_tiles) { 1607 struct htp_lid_hmx_context nx = hctx; 1608 nx.tile0 = tile0 + n_qsets; 1609 hctx.nx_n_run = MIN(n_qsets, n_tiles - nx.tile0); 1610 for (uint32_t i = 0; i < hctx.nx_n_run; ++i) { 1611 hctx.nx_sets[i] = lid_hmx_set_init(&nx, i); 1612 } 1613 hctx.nx_stg_div = init_fastdiv_values(hctx.n_blocks + 1); 1614 hctx.nx_on = true; 1615 } 1616 1617 for (int32_t g = 0; g < n; ++g) { 1618 struct lid_hmx_seg seg[LID_HMX_MAX_SEGS]; 1619 uint32_t n_rows; 1620 const uint32_t n_seg = lid_hmx_segs(&hctx, g, seg, &n_rows); 1621 1622 lid_hmx_job_t * j = &job[g % 2]; 1623 #pragma clang loop vectorize(disable) 1624 for (uint32_t i = 0; i < n_seg; ++i) { 1625 const uint32_t slot = lid_hmx_slot(&hctx, hctx.qbuf, seg[i].set); 1626 j->seg[i].qw = hctx.qw + slot * L->n_ch * L->n_dot * HMX_FP16_TILE_N_ELMS; 1627 j->seg[i].gw = hctx.gw + slot * L->n_ch * HMX_FP16_TILE_N_ELMS; 1628 j->seg[i].sc = hctx.sc + slot * L->n_ch * 256; 1629 j->seg[i].rt0 = seg[i].x0 / 32; 1630 j->seg[i].n_rt = (seg[i].nv + 31) / 32; 1631 } 1632 j->n_seg = n_seg; 1633 j->k_t = hctx.k_t[g % 2]; 1634 j->sc_id = hctx.sc_id; 1635 j->s = hctx.s; 1636 j->o = hctx.o[g % 2]; 1637 j->n_ch = S0.n_ch; 1638 j->n_u = S0.n_u; 1639 j->n_dot = L->n_dot; 1640 j->n_head = n_head; 1641 j->n_sg = L->n_sg; 1642 hmx_queue_push(hmx_q, hmx_queue_make_desc(lid_hmx_worker, j)); 1643 1644 // stage g has work only if there is a next block, a previous one or next run Q work 1645 hctx.g = g; 1646 if (g + 1 < n || g >= 1 || hctx.nx_on) { 1647 work_queue_run(octx->ctx->work_queue, lid_hmx_stage_thread, &hctx, n_threads); 1648 } 1649 1650 hmx_queue_pop(hmx_q); 1651 } 1652 1653 // last stage only extracts; with one token only thread 0 has work (same token split for nth 1) 1654 hctx.g = n; 1655 const uint32_t n_last = hctx.nt_run == 1 && !hctx.nx_on ? 1 : n_threads; 1656 work_queue_run(octx->ctx->work_queue, lid_hmx_stage_thread, &hctx, n_last); 1657 } 1658 1659 return HTP_STATUS_OK; 1660 } 1661 1662 int op_lightning_indexer(struct htp_ops_context * octx) { 1663 const struct htp_tensor * q = octx->src[0]; 1664 const struct htp_tensor * m = octx->src[3]; 1665 1666 // no multi-device split yet, device 0 does all the work 1667 if (octx->ctx->mdev.count > 1 && octx->ctx->mdev.idx != 0) { 1668 return HTP_STATUS_OK; 1669 } 1670 1671 const struct htp_lid_kernel_params * kparams = (const struct htp_lid_kernel_params *) octx->kernel_params; 1672 1673 if (kparams->vtcm_size > octx->ctx->vtcm_size) { 1674 return HTP_STATUS_VTCM_TOO_SMALL; 1675 } 1676 1677 if (!htp_ops_context_set_n_threads(octx, kparams->n_threads)) { 1678 return HTP_STATUS_INVAL_PARAMS; 1679 } 1680 1681 if (kparams->kernel_type == HTP_LID_KERNEL_HMX) { 1682 return lid_hmx_run(octx, kparams); 1683 } 1684 1685 struct htp_lid_context lctx; 1686 lctx.octx = octx; 1687 lctx.kp = kparams; 1688 lctx.n_blocks = kparams->n_kv_blocks; 1689 lctx.blocks_per_thread = kparams->blocks_per_thread; 1690 1691 htp_lid_vtcm_layout_build(&lctx.L, q->ne[0], q->ne[1], kparams->n_tokens_tile, kparams->n_threads, kparams->k_row_size); 1692 1693 uint8_t * const base = octx->ctx->vtcm_base; 1694 1695 lctx.spad_q = VTCM_LAYOUT_PTR(uint8_t, base, lctx.L.off_q); 1696 lctx.spad_w = VTCM_LAYOUT_PTR(uint8_t, base, lctx.L.off_w); 1697 lctx.spad_qs = VTCM_LAYOUT_PTR(uint8_t, base, lctx.L.off_qs); 1698 lctx.spad_k = VTCM_LAYOUT_PTR(uint8_t, base, lctx.L.off_k); 1699 lctx.spad_kt = VTCM_LAYOUT_PTR(uint8_t, base, lctx.L.off_kt); 1700 lctx.spad_m = VTCM_LAYOUT_PTR(uint8_t, base, lctx.L.off_m); 1701 lctx.spad_o = VTCM_LAYOUT_PTR(uint8_t, base, lctx.L.off_o); 1702 1703 const uint32_t n_tokens = q->ne[2]; 1704 const uint32_t n_stream = q->ne[3]; 1705 const uint32_t n_tile = kparams->n_tokens_tile; 1706 const uint32_t n_threads = kparams->n_threads; 1707 1708 for (uint32_t is = 0; is < n_stream; ++is) { 1709 lctx.is = is; 1710 lctx.im3 = fastmodulo(is, m->ne[3], &kparams->div_m_ne3); 1711 1712 for (uint32_t t0 = 0; t0 < n_tokens; t0 += n_tile) { 1713 lctx.t0 = t0; 1714 lctx.nt = MIN(n_tile, n_tokens - t0); 1715 1716 work_queue_run(octx->ctx->work_queue, lid_prep_thread, &lctx, MIN(n_threads, lctx.nt * q->ne[1])); 1717 work_queue_run(octx->ctx->work_queue, lid_compute_thread, &lctx, n_threads); 1718 } 1719 } 1720 1721 return HTP_STATUS_OK; 1722 }