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/ssm-conv.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 / ssm-conv.c` 646 lines ยท 29197 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 #include 8 #include 9 #include 10 #include 11 #include 12 #include 13 14 #define GGML_COMMON_DECL_C 15 #include "ggml-common.h" 16 #include "htp-ctx.h" 17 #include "dma-queue.h" 18 #include "hex-profile.h" 19 #include "htp-ops.h" 20 #include "htp-tensor.h" 21 #include "hvx-utils.h" 22 #include "ssm-conv.h" 23 24 struct htp_ssm_conv_context { 25 struct htp_ops_context * octx; 26 const struct htp_ssm_conv_kernel_params * kparams; 27 uint32_t nrows_per_thread; 28 uint32_t d_inner_tile; 29 uint32_t row_start; 30 uint32_t nrows; 31 }; 32 33 // In-register 32x32 fp32 transpose using std 5-stage HVX vshuff butterfly. 34 static inline void hvx_transpose_32x32_f32(HVX_Vector m[32]) { 35 HVX_Vector tmp[32]; 36 37 // Stage 0 (R = -4): pair (2i, 2i+1) for i = 0..15. m -> tmp. 38 for (int i = 0; i < 16; ++i) { 39 HVX_VectorPair p = Q6_W_vshuff_VVR(m[2*i + 1], m[2*i], -4); 40 tmp[2*i + 0] = Q6_V_lo_W(p); 41 tmp[2*i + 1] = Q6_V_hi_W(p); 42 } 43 44 // Stage 1 (R = -8): per block of 4, pair (b+0, b+2) and (b+1, b+3). tmp -> m. 45 for (int b = 0; b < 32; b += 4) { 46 HVX_VectorPair p0 = Q6_W_vshuff_VVR(tmp[b + 2], tmp[b + 0], -8); 47 HVX_VectorPair p1 = Q6_W_vshuff_VVR(tmp[b + 3], tmp[b + 1], -8); 48 m[b + 0] = Q6_V_lo_W(p0); m[b + 1] = Q6_V_hi_W(p0); 49 m[b + 2] = Q6_V_lo_W(p1); m[b + 3] = Q6_V_hi_W(p1); 50 } 51 52 // Stage 2 (R = -16): per block of 8, pair (b+i, b+i+4) for i = 0..3. m -> tmp. 53 for (int b = 0; b < 32; b += 8) { 54 for (int i = 0; i < 4; ++i) { 55 HVX_VectorPair p = Q6_W_vshuff_VVR(m[b + i + 4], m[b + i], -16); 56 tmp[b + 2*i + 0] = Q6_V_lo_W(p); 57 tmp[b + 2*i + 1] = Q6_V_hi_W(p); 58 } 59 } 60 61 // Stage 3 (R = -32): per block of 16, pair (b+i, b+i+8) for i = 0..7. tmp -> m. 62 for (int b = 0; b < 32; b += 16) { 63 for (int i = 0; i < 8; ++i) { 64 HVX_VectorPair p = Q6_W_vshuff_VVR(tmp[b + i + 8], tmp[b + i], -32); 65 m[b + 2*i + 0] = Q6_V_lo_W(p); 66 m[b + 2*i + 1] = Q6_V_hi_W(p); 67 } 68 } 69 70 // Stage 4 (R = -64): pair (i, i+16) for i = 0..15. m -> tmp -> m. 71 for (int i = 0; i < 16; ++i) { 72 HVX_VectorPair p = Q6_W_vshuff_VVR(m[i + 16], m[i], -64); 73 tmp[2 * i + 0] = Q6_V_lo_W(p); 74 tmp[2 * i + 1] = Q6_V_hi_W(p); 75 } 76 77 for (int i = 0; i < 32; ++i) { 78 m[i] = tmp[i]; 79 } 80 } 81 82 // HVX deinterleave for d_conv == 4: channel-major raw VTCM -> tap-major T VTCM 83 static inline void hvx_ssm_conv_unpack_to_T_4(const float * raw, float * T, uint32_t d_inner_per_thread, uint32_t d_inner_stride) { 84 for (uint32_t cb = 0; cb < d_inner_per_thread; cb += VLEN_FP32) { 85 HVX_Vector v0 = *(const HVX_Vector *)(raw + (cb + 0) * 4); 86 HVX_Vector v1 = *(const HVX_Vector *)(raw + (cb + 8) * 4); 87 HVX_Vector v2 = *(const HVX_Vector *)(raw + (cb + 16) * 4); 88 HVX_Vector v3 = *(const HVX_Vector *)(raw + (cb + 24) * 4); 89 90 HVX_VectorPair p01 = Q6_W_vdeal_VVR(v1, v0, -4); 91 HVX_VectorPair p23 = Q6_W_vdeal_VVR(v3, v2, -4); 92 93 HVX_VectorPair p_w02 = Q6_W_vdeal_VVR(Q6_V_lo_W(p23), Q6_V_lo_W(p01), -4); 94 HVX_VectorPair p_w13 = Q6_W_vdeal_VVR(Q6_V_hi_W(p23), Q6_V_hi_W(p01), -4); 95 96 *(HVX_Vector *)(T + 0 * d_inner_stride + cb) = Q6_V_lo_W(p_w02); 97 *(HVX_Vector *)(T + 1 * d_inner_stride + cb) = Q6_V_lo_W(p_w13); 98 *(HVX_Vector *)(T + 2 * d_inner_stride + cb) = Q6_V_hi_W(p_w02); 99 *(HVX_Vector *)(T + 3 * d_inner_stride + cb) = Q6_V_hi_W(p_w13); 100 } 101 } 102 103 // HVX transpose for general d_conv <= 32: channel-major raw VTCM -> tap-major T VTCM 104 static inline void hvx_ssm_conv_unpack_to_T_gen(const float * raw, float * T, uint32_t d_inner_per_thread, uint32_t d_inner_stride, uint32_t d_conv) { 105 uint32_t __attribute__((aligned(VLEN))) mask_buf[VLEN_FP32] = { 0 }; 106 for (uint32_t j = 0; j < d_conv; ++j) { 107 mask_buf[j] = 0xFFFFFFFF; 108 } 109 const HVX_Vector mask = *(const HVX_Vector *) mask_buf; 110 111 for (uint32_t cb = 0; cb < d_inner_per_thread; cb += VLEN_FP32) { 112 const uint32_t cb_n = MIN(VLEN_FP32, d_inner_per_thread - cb); 113 HVX_Vector sub[32]; 114 for (uint32_t r = 0; r < cb_n; ++r) { 115 const float * ch_ptr = raw + (cb + r) * d_conv; 116 sub[r] = Q6_V_vand_VV(*(const HVX_UVector *) ch_ptr, mask); 117 } 118 for (uint32_t r = cb_n; r < 32; ++r) { 119 sub[r] = hvx_vec_splat_f32(0.0f); 120 } 121 122 hvx_transpose_32x32_f32(sub); 123 124 for (uint32_t j = 0; j < d_conv; ++j) { 125 *(HVX_Vector *)(T + j * d_inner_stride + cb) = sub[j]; 126 } 127 } 128 } 129 130 static inline void hvx_ssm_conv_unpack_to_T(const float * raw, float * T, uint32_t d_inner_per_thread, uint32_t d_inner_stride, uint32_t d_conv) { 131 if (d_conv == 4 && (d_inner_per_thread % VLEN_FP32 == 0)) { 132 hvx_ssm_conv_unpack_to_T_4(raw, T, d_inner_per_thread, d_inner_stride); 133 } else { 134 hvx_ssm_conv_unpack_to_T_gen(raw, T, d_inner_per_thread, d_inner_stride, d_conv); 135 } 136 } 137 138 static __attribute__((noinline)) void hvx_ssm_conv_silu_vtcm(float * restrict buf, uint32_t n_vecs) { 139 HVX_Vector * restrict vbuf = (HVX_Vector *) buf; 140 for (uint32_t i = 0; i < n_vecs; ++i) { 141 vbuf[i] = hvx_vec_silu_f32(vbuf[i]); 142 } 143 } 144 145 // Decode dot product specialization for d_conv == 4: multiply in the raw channel-major layout, 146 // then deinterleave the products so each vector holds one tap of 32 channels, and sum. 147 // Keeps both operands in DMA layout - no transpose, no scratch. 148 static inline void hvx_ssm_conv_decode_4(const float * x, const float * w, float * out, uint32_t n_ch) { 149 for (uint32_t cb = 0; cb < n_ch; cb += VLEN_FP32) { 150 const float * xp = x + cb * 4; 151 const float * wp = w + cb * 4; 152 153 HVX_Vector p0 = Q6_Vqf32_vmpy_VsfVsf(*(const HVX_Vector *)(xp + 0), *(const HVX_Vector *)(wp + 0)); 154 HVX_Vector p1 = Q6_Vqf32_vmpy_VsfVsf(*(const HVX_Vector *)(xp + 32), *(const HVX_Vector *)(wp + 32)); 155 HVX_Vector p2 = Q6_Vqf32_vmpy_VsfVsf(*(const HVX_Vector *)(xp + 64), *(const HVX_Vector *)(wp + 64)); 156 HVX_Vector p3 = Q6_Vqf32_vmpy_VsfVsf(*(const HVX_Vector *)(xp + 96), *(const HVX_Vector *)(wp + 96)); 157 158 HVX_VectorPair p01 = Q6_W_vdeal_VVR(p1, p0, -4); 159 HVX_VectorPair p23 = Q6_W_vdeal_VVR(p3, p2, -4); 160 161 HVX_VectorPair q02 = Q6_W_vdeal_VVR(Q6_V_lo_W(p23), Q6_V_lo_W(p01), -4); 162 HVX_VectorPair q13 = Q6_W_vdeal_VVR(Q6_V_hi_W(p23), Q6_V_hi_W(p01), -4); 163 164 HVX_Vector a = Q6_Vqf32_vadd_Vqf32Vqf32(Q6_V_lo_W(q02), Q6_V_lo_W(q13)); 165 HVX_Vector b = Q6_Vqf32_vadd_Vqf32Vqf32(Q6_V_hi_W(q02), Q6_V_hi_W(q13)); 166 167 *(HVX_Vector *)(out + cb) = Q6_Vsf_equals_Vqf32(Q6_Vqf32_vadd_Vqf32Vqf32(a, b)); 168 } 169 } 170 171 // Transpose src0 for prefill: one 32-channel block {32, ncs} (VTCM) -> T {ncs, 32} (VTCM). 172 // One VTCM gather per output row: lane r picks channel cb+r, the region bound drops 173 // the lanes past the channel tail. 174 static inline void hvx_ssm_conv_transpose_block(const float * raw_block, 175 float * T, 176 uint32_t raw_stride_bytes, 177 uint32_t ncs, 178 uint32_t cb_n, 179 HVX_Vector vv) { 180 const size_t base = (size_t) raw_block; 181 const uint32_t mu = cb_n * raw_stride_bytes - 1; 182 183 for (uint32_t t = 0; t < ncs; ++t) { 184 Q6_vgather_ARMVw((HVX_Vector *) (T + (size_t) t * VLEN_FP32), base + t * sizeof(float), mu, vv); 185 } 186 } 187 188 // Single-token decode worker (n_t == 1) 189 static void ssm_conv_thread_f32_decode(unsigned int nth, unsigned int ith, void * data) { 190 struct htp_ssm_conv_context * scctx = (struct htp_ssm_conv_context *) data; 191 struct htp_ops_context * octx = scctx->octx; 192 const struct htp_ssm_conv_kernel_params * kparams = scctx->kparams; 193 194 const struct htp_tensor * restrict src0 = octx->src[0]; 195 const struct htp_tensor * restrict src1 = octx->src[1]; 196 const struct htp_tensor * restrict dst = octx->dst; 197 198 dma_queue * dma_q = octx->ctx->dma[ith]; 199 200 const uint32_t d_conv = kparams->d_conv; 201 const uint32_t d_inner = kparams->d_inner; 202 const uint32_t n_s = kparams->n_s; 203 const bool apply_silu = kparams->silu != 0; 204 205 const uint32_t dr = scctx->nrows_per_thread; 206 const uint32_t ir0 = scctx->row_start + dr * ith; 207 const uint32_t ir1 = MIN(ir0 + dr, scctx->row_start + scctx->nrows); 208 209 if (ir0 >= ir1) { 210 return; 211 } 212 213 const uint32_t d_inner_per_thread = ir1 - ir0; 214 const uint32_t d_inner_stride = hex_round_up(d_inner_per_thread, VLEN_FP32); 215 const uint32_t d_inner_tile = scctx->d_inner_tile; 216 217 const size_t src0_stride_inner_bytes = src0->nb[1]; 218 const size_t src0_stride_seq_bytes = src0->nb[2]; 219 const size_t dst_stride_seq_bytes = dst->nb[2]; 220 221 uint8_t * src1_spad_base = octx->src1_spad.data + ith * octx->src1_spad.size_per_thread; 222 uint8_t * src0_spad_base = octx->src0_spad.data + ith * octx->src0_spad.size_per_thread; 223 uint8_t * dst_spad_base = octx->dst_spad.data + ith * octx->dst_spad.size_per_thread; 224 225 const size_t weight_bytes = (size_t) d_inner_per_thread * d_conv * sizeof(float); 226 const size_t weight_raw_size = hex_round_up(weight_bytes, 128); 227 228 float * src1_raw = (float *) src1_spad_base; 229 float * src1_T = (float *) (src1_spad_base + weight_raw_size); 230 231 const size_t src0_tile_raw_bytes = hex_round_up(d_inner_tile * d_conv * sizeof(float), 128); 232 const size_t dst_tile_bytes = hex_round_up(d_inner_tile * sizeof(float), 128); 233 234 float * src0_tile_raw[2] = { (float *) src0_spad_base, (float *) (src0_spad_base + src0_tile_raw_bytes) }; 235 float * src0_T = (float *) (src0_spad_base + 2 * src0_tile_raw_bytes); 236 237 float * dst_tile[2] = { (float *) dst_spad_base, (float *) (dst_spad_base + dst_tile_bytes) }; 238 239 struct htp_thread_trace * tr = &octx->ctx->trace[ith]; 240 241 // raw_dot keeps both operands in the DMA layout, so src1 needs no prep pass 242 const bool raw_dot = (d_conv == 4) && (d_inner_per_thread % VLEN_FP32 == 0); 243 244 const uint32_t n_tiles = fastdiv(d_inner_per_thread + d_inner_tile - 1, &kparams->div_d_inner_tile); 245 const uint32_t n_chunks = n_s * n_tiles; 246 const size_t row_bytes = d_conv * sizeof(float); 247 248 uint32_t s_fetch = 0; 249 uint32_t tile_off_fetch = 0; 250 251 #define SSM_CONV_DECODE_PUSH_FETCH(c) \ 252 do { \ 253 const uint32_t cur_tile_n = MIN(d_inner_tile, d_inner_per_thread - tile_off_fetch); \ 254 const dma_addr_t fetch_ddr = src0->data + s_fetch * src0_stride_seq_bytes + \ 255 (ir0 + tile_off_fetch) * src0_stride_inner_bytes; \ 256 dma_queue_push(dma_q, \ 257 dma_make_data((uint8_t *) src0_tile_raw[(c) & 1], fetch_ddr), \ 258 row_bytes, src0_stride_inner_bytes, row_bytes, cur_tile_n); \ 259 tile_off_fetch += d_inner_tile; \ 260 if (tile_off_fetch >= d_inner_per_thread) { \ 261 tile_off_fetch = 0; \ 262 s_fetch++; \ 263 } \ 264 } while (0) 265 266 // Queue weights and initial input tiles together so DDR reads overlap 267 const dma_addr_t src1_ddr = src1->data + ir0 * d_conv * sizeof(float); 268 dma_queue_push(dma_q, dma_make_data((uint8_t *) src1_raw, src1_ddr), weight_bytes, weight_bytes, weight_bytes, 1); 269 270 SSM_CONV_DECODE_PUSH_FETCH(0); 271 if (n_chunks > 1) { 272 SSM_CONV_DECODE_PUSH_FETCH(1); 273 } 274 275 dma_queue_pop(dma_q); // weights 276 277 if (!raw_dot) { 278 // Unpack/transpose src1_raw into src1_T {d_conv, d_inner_stride} 279 htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_W_PREP, (uint16_t) ir0); 280 hvx_ssm_conv_unpack_to_T(src1_raw, src1_T, d_inner_per_thread, d_inner_stride, d_conv); 281 htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_W_PREP, (uint16_t) ir0); 282 } 283 284 uint32_t i3 = 0; 285 uint32_t tile_off = 0; 286 287 for (uint32_t c = 0; c < n_chunks; ++c) { 288 const uint32_t tile_n = MIN(d_inner_tile, d_inner_per_thread - tile_off); 289 290 if (c >= 2) { 291 dma_queue_pop(dma_q); // writeback of chunk c-2, frees dst_tile[c & 1] 292 } 293 dma_queue_pop(dma_q); // fetch chunk c 294 295 float * restrict out = dst_tile[c & 1]; 296 297 htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) i3); 298 if (raw_dot) { 299 const float * xp = src0_tile_raw[c & 1]; 300 const float * wp = src1_raw + tile_off * 4; 301 hvx_ssm_conv_decode_4(xp, wp, out, tile_n); 302 } else { 303 const uint32_t tile_stride = hex_round_up(tile_n, VLEN_FP32); 304 hvx_ssm_conv_unpack_to_T(src0_tile_raw[c & 1], src0_T, tile_n, tile_stride, d_conv); 305 306 for (uint32_t cb = 0; cb < tile_n; cb += VLEN_FP32) { 307 HVX_Vector acc = hvx_vec_splat_f32(0.0f); 308 for (uint32_t j = 0; j < d_conv; ++j) { 309 HVX_Vector x = *(const HVX_Vector *)(src0_T + j * tile_stride + cb); 310 HVX_Vector w = *(const HVX_Vector *)(src1_T + j * d_inner_stride + tile_off + cb); 311 acc = Q6_Vqf32_vadd_Vqf32Vqf32(acc, Q6_Vqf32_vmpy_VsfVsf(x, w)); 312 } 313 *(HVX_Vector *)(out + cb) = Q6_Vsf_equals_Vqf32(acc); 314 } 315 } 316 if (apply_silu) { 317 hvx_ssm_conv_silu_vtcm(out, hex_round_up(tile_n, VLEN_FP32) / VLEN_FP32); 318 } 319 htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) i3); 320 321 const dma_addr_t dst_ddr = dst->data + i3 * dst_stride_seq_bytes + (ir0 + tile_off) * sizeof(float); 322 const size_t tile_out_bytes = (size_t) tile_n * sizeof(float); 323 dma_queue_push(dma_q, dma_make_data(dst_ddr, (uint8_t *) out), 324 tile_out_bytes, tile_out_bytes, tile_out_bytes, 1); 325 326 if (c + 2 < n_chunks) { 327 SSM_CONV_DECODE_PUSH_FETCH(c + 2); 328 } 329 330 tile_off += d_inner_tile; 331 if (tile_off >= d_inner_per_thread) { 332 tile_off = 0; 333 i3++; 334 } 335 } 336 337 for (uint32_t k = MIN(n_chunks, 2); k > 0; --k) { 338 dma_queue_pop(dma_q); // drain the last writebacks 339 } 340 341 #undef SSM_CONV_DECODE_PUSH_FETCH 342 343 FARF(HIGH, "ssm-conv-f32-decode %d/%d: %ux%ux%ux%u (%u:%u) * %ux%ux%ux%u -> %ux%ux%ux%u\n", 344 ith, nth, src0->ne[0], src0->ne[1], src0->ne[2], src0->ne[3], ir0, ir1, 345 src1->ne[0], src1->ne[1], src1->ne[2], src1->ne[3], dst->ne[0], dst->ne[1], 346 dst->ne[2], dst->ne[3]); 347 } 348 349 // Multi-token prefill worker (n_t > 1) 350 static void ssm_conv_thread_f32_prefill(unsigned int nth, unsigned int ith, void * data) { 351 struct htp_ssm_conv_context * scctx = (struct htp_ssm_conv_context *) data; 352 struct htp_ops_context * octx = scctx->octx; 353 const struct htp_ssm_conv_kernel_params * kparams = scctx->kparams; 354 355 const struct htp_tensor * restrict src0 = octx->src[0]; 356 const struct htp_tensor * restrict src1 = octx->src[1]; 357 const struct htp_tensor * restrict dst = octx->dst; 358 359 dma_queue * dma_q = octx->ctx->dma[ith]; 360 361 const uint32_t d_conv = kparams->d_conv; 362 const uint32_t d_inner = kparams->d_inner; 363 const uint32_t n_t = kparams->n_t; 364 const uint32_t n_s = kparams->n_s; 365 const uint32_t ncs = src0->ne[0]; 366 const bool apply_silu = kparams->silu != 0; 367 368 const uint32_t dr = scctx->nrows_per_thread; 369 const uint32_t ir0 = scctx->row_start + dr * ith; 370 const uint32_t ir1 = MIN(ir0 + dr, scctx->row_start + scctx->nrows); 371 372 if (ir0 >= ir1) { 373 return; 374 } 375 376 const uint32_t d_inner_per_thread = ir1 - ir0; 377 const uint32_t d_inner_stride = hex_round_up(d_inner_per_thread, VLEN_FP32); 378 const uint32_t d_inner_tile = scctx->d_inner_tile; 379 380 const size_t src0_stride_inner_bytes = src0->nb[1]; 381 const size_t src0_stride_seq_bytes = src0->nb[2]; 382 const size_t dst_stride_token_bytes = dst->nb[1]; 383 const size_t dst_stride_seq_bytes = dst->nb[2]; 384 385 uint8_t * src1_spad_base = octx->src1_spad.data + ith * octx->src1_spad.size_per_thread; 386 uint8_t * src0_spad_base = octx->src0_spad.data + ith * octx->src0_spad.size_per_thread; 387 uint8_t * dst_spad_base = octx->dst_spad.data + ith * octx->dst_spad.size_per_thread; 388 389 const size_t weight_bytes = (size_t) d_inner_per_thread * d_conv * sizeof(float); 390 const size_t weight_raw_size = hex_round_up(weight_bytes, 128); 391 392 float * src1_raw = (float *) src1_spad_base; 393 float * src1_T = (float *) (src1_spad_base + weight_raw_size); 394 395 // src0 spad holds two raw tiles (fetch of tile n+1 overlaps compute of tile n) plus 396 // the transposed block. dst spad holds two tiles so a writeback can stay in flight. 397 const size_t raw_stride_bytes = ncs * sizeof(float) + sizeof(float); 398 const size_t src0_tile_raw_bytes = hex_round_up(d_inner_tile * raw_stride_bytes, 128); 399 const size_t dst_tile_bytes = hex_round_up(d_inner_tile * n_t * sizeof(float), 128); 400 401 float * src0_tile_raw[2] = { (float *) src0_spad_base, (float *) (src0_spad_base + src0_tile_raw_bytes) }; 402 float * src0_T = (float *) (src0_spad_base + 2 * src0_tile_raw_bytes); 403 404 float * dst_tile[2] = { (float *) dst_spad_base, (float *) (dst_spad_base + dst_tile_bytes) }; 405 406 struct htp_thread_trace * tr = &octx->ctx->trace[ith]; 407 408 const uint32_t n_tiles = fastdiv(d_inner_per_thread + d_inner_tile - 1, &kparams->div_d_inner_tile); 409 const uint32_t n_chunks = n_s * n_tiles; 410 const size_t row_bytes = ncs * sizeof(float); 411 412 uint32_t s_fetch = 0; 413 uint32_t tile_off_fetch = 0; 414 415 // Chunk c fetches into src0_tile_raw[c & 1] and writes back from dst_tile[c & 1]. 416 // Two fetches run ahead, so the queue order is F0 F1 W0 F2 W1 ... and pops follow it. 417 #define SSM_CONV_PUSH_FETCH(c) \ 418 do { \ 419 const uint32_t cur_tile_n = MIN(d_inner_tile, d_inner_per_thread - tile_off_fetch); \ 420 const dma_addr_t fetch_ddr = src0->data + s_fetch * src0_stride_seq_bytes + \ 421 (ir0 + tile_off_fetch) * src0_stride_inner_bytes; \ 422 dma_queue_push(dma_q, \ 423 dma_make_data((uint8_t *) src0_tile_raw[(c) & 1], fetch_ddr), \ 424 raw_stride_bytes, src0_stride_inner_bytes, row_bytes, cur_tile_n); \ 425 tile_off_fetch += d_inner_tile; \ 426 if (tile_off_fetch >= d_inner_per_thread) { \ 427 tile_off_fetch = 0; \ 428 s_fetch++; \ 429 } \ 430 } while (0) 431 432 // Queue weights and initial input tiles together so DDR reads overlap 433 const dma_addr_t src1_ddr = src1->data + ir0 * d_conv * sizeof(float); 434 dma_queue_push(dma_q, dma_make_data((uint8_t *) src1_raw, src1_ddr), weight_bytes, weight_bytes, weight_bytes, 1); 435 436 SSM_CONV_PUSH_FETCH(0); 437 if (n_chunks > 1) { 438 SSM_CONV_PUSH_FETCH(1); 439 } 440 441 dma_queue_pop(dma_q); // weights 442 443 // Unpack/transpose src1_raw into src1_T {d_conv, d_inner_stride} 444 htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_W_PREP, (uint16_t) ir0); 445 hvx_ssm_conv_unpack_to_T(src1_raw, src1_T, d_inner_per_thread, d_inner_stride, d_conv); 446 htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_W_PREP, (uint16_t) ir0); 447 448 const uint32_t C_TILE = VLEN_FP32; 449 450 // gather offsets: lane r reads channel r of the raw tile block 451 const HVX_Vector vv = hvx_vec_gather_offsets_w(raw_stride_bytes); 452 453 uint32_t i3 = 0; 454 uint32_t tile_off = 0; 455 456 for (uint32_t c = 0; c < n_chunks; ++c) { 457 const uint32_t tile_n = MIN(d_inner_tile, d_inner_per_thread - tile_off); 458 459 const float * restrict raw = src0_tile_raw[c & 1]; 460 float * restrict out = dst_tile[c & 1]; 461 462 if (c >= 2) { 463 dma_queue_pop(dma_q); // writeback of chunk c-2, frees dst_tile[c & 1] 464 } 465 dma_queue_pop(dma_q); // fetch chunk c 466 467 // Channel block outer, token inner: the taps and the sliding window stay in 468 // registers, so a new output row costs one src0_T load. 469 const uint32_t dst_tile_stride = hex_round_up(tile_n, C_TILE); 470 471 for (uint32_t cb = 0; cb < tile_n; cb += C_TILE) { 472 const uint32_t cb_n = MIN(C_TILE, tile_n - cb); 473 474 htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_A_PREP, (uint16_t) tile_off); 475 const float * raw_ch = (const float *) ((const uint8_t *) raw + (size_t) cb * raw_stride_bytes); 476 hvx_ssm_conv_transpose_block(raw_ch, src0_T, raw_stride_bytes, ncs, cb_n, vv); 477 htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_A_PREP, (uint16_t) tile_off); 478 479 const float * restrict wp = src1_T + tile_off + cb; 480 float * restrict op = out + cb; 481 482 htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) tile_off); 483 if (d_conv == 4) { 484 const HVX_Vector w0 = *(const HVX_Vector *) (wp); 485 const HVX_Vector w1 = *(const HVX_Vector *) (wp + d_inner_stride); 486 const HVX_Vector w2 = *(const HVX_Vector *) (wp + 2 * d_inner_stride); 487 const HVX_Vector w3 = *(const HVX_Vector *) (wp + 3 * d_inner_stride); 488 489 HVX_Vector x0 = *(const HVX_Vector *) (src0_T); 490 HVX_Vector x1 = *(const HVX_Vector *) (src0_T + C_TILE); 491 HVX_Vector x2 = *(const HVX_Vector *) (src0_T + 2 * C_TILE); 492 493 for (uint32_t t = 0; t < n_t; ++t) { 494 const HVX_Vector x3 = *(const HVX_Vector *) (src0_T + (t + 3) * C_TILE); 495 496 HVX_Vector a = Q6_Vqf32_vadd_Vqf32Vqf32(Q6_Vqf32_vmpy_VsfVsf(x0, w0), 497 Q6_Vqf32_vmpy_VsfVsf(x1, w1)); 498 HVX_Vector b = Q6_Vqf32_vadd_Vqf32Vqf32(Q6_Vqf32_vmpy_VsfVsf(x2, w2), 499 Q6_Vqf32_vmpy_VsfVsf(x3, w3)); 500 501 *(HVX_Vector *) (op + t * dst_tile_stride) = Q6_Vsf_equals_Vqf32(Q6_Vqf32_vadd_Vqf32Vqf32(a, b)); 502 503 x0 = x1; 504 x1 = x2; 505 x2 = x3; 506 } 507 } else { 508 for (uint32_t t = 0; t < n_t; ++t) { 509 HVX_Vector acc = hvx_vec_splat_f32(0.0f); 510 for (uint32_t j = 0; j < d_conv; ++j) { 511 HVX_Vector x = *(const HVX_Vector *) (src0_T + (t + j) * C_TILE); 512 HVX_Vector w = *(const HVX_Vector *) (wp + j * d_inner_stride); 513 acc = Q6_Vqf32_vadd_Vqf32Vqf32(acc, Q6_Vqf32_vmpy_VsfVsf(x, w)); 514 } 515 *(HVX_Vector *) (op + t * dst_tile_stride) = Q6_Vsf_equals_Vqf32(acc); 516 } 517 } 518 htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) tile_off); 519 } 520 521 if (apply_silu) { 522 htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) tile_off); 523 hvx_ssm_conv_silu_vtcm(out, n_t * (dst_tile_stride / C_TILE)); 524 htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) tile_off); 525 } 526 527 // Writeback dst_tile VTCM -> DDR via 2D DMA 528 dma_queue_push(dma_q, dma_make_data(dst->data + i3 * dst_stride_seq_bytes + (ir0 + tile_off) * sizeof(float), (uint8_t *) out), 529 dst_stride_token_bytes, dst_tile_stride * sizeof(float), tile_n * sizeof(float), n_t); 530 531 if (c + 2 < n_chunks) { 532 SSM_CONV_PUSH_FETCH(c + 2); 533 } 534 535 tile_off += d_inner_tile; 536 if (tile_off >= d_inner_per_thread) { 537 tile_off = 0; 538 i3++; 539 } 540 } 541 542 for (uint32_t k = MIN(n_chunks, 2); k > 0; --k) { 543 dma_queue_pop(dma_q); // drain the last writebacks 544 } 545 546 #undef SSM_CONV_PUSH_FETCH 547 548 FARF(HIGH, "ssm-conv-f32-prefill %d/%d: %ux%ux%ux%u (%u:%u) * %ux%ux%ux%u -> %ux%ux%ux%u\n", 549 ith, nth, src0->ne[0], src0->ne[1], src0->ne[2], src0->ne[3], ir0, ir1, 550 src1->ne[0], src1->ne[1], src1->ne[2], src1->ne[3], dst->ne[0], dst->ne[1], 551 dst->ne[2], dst->ne[3]); 552 } 553 554 int op_ssm_conv_f32(struct htp_ops_context * octx) { 555 const struct htp_tensor * src0 = octx->src[0]; 556 const struct htp_tensor * src1 = octx->src[1]; 557 const struct htp_tensor * dst = octx->dst; 558 559 const struct htp_ssm_conv_kernel_params * kparams = (const struct htp_ssm_conv_kernel_params *) octx->kernel_params; 560 561 if (!htp_ops_context_set_n_threads(octx, kparams->n_threads)) { 562 return HTP_STATUS_INVAL_PARAMS; 563 } 564 565 uint32_t row_start = 0; 566 uint32_t nrows = kparams->d_inner; 567 568 if (octx->ctx->mdev.count > 1) { 569 const uint32_t elems_per_chunk = VLEN_FP32; 570 const struct htp_tensor_mdev_range range = htp_tensor_mdev_partition( 571 kparams->d_inner, 572 htp_tensor_mdev_data_aligned(dst) ? elems_per_chunk : 0, 573 octx->ctx->mdev.idx, 574 octx->ctx->mdev.count, 575 &octx->ctx->mdev.count_div 576 ); 577 row_start = range.start; 578 nrows = range.count; 579 } 580 581 if (nrows == 0) { 582 return HTP_STATUS_OK; 583 } 584 585 if (kparams->vtcm_size > octx->ctx->vtcm_size) { 586 return HTP_STATUS_VTCM_TOO_SMALL; 587 } 588 589 const uint32_t n_threads = octx->n_threads; 590 const uint32_t raw_rpt = fastdiv(nrows + n_threads - 1, &octx->n_threads_div); 591 const uint32_t d_inner_per_thread = hex_round_up(raw_rpt, VLEN_FP32); 592 593 struct htp_ssm_conv_vtcm_layout L; 594 htp_ssm_conv_vtcm_layout_build(&L, d_inner_per_thread, kparams->d_conv, kparams->n_t, kparams->d_inner_tile, n_threads); 595 596 uint8_t * const base = (uint8_t *) octx->ctx->vtcm_base; 597 598 octx->src0_spad.size_per_thread = L.src0_bytes_per_thread; 599 octx->src1_spad.size_per_thread = L.src1_bytes_per_thread; 600 octx->dst_spad.size_per_thread = L.dst_bytes_per_thread; 601 602 octx->src0_spad.size = L.src0_bytes; 603 octx->src1_spad.size = L.src1_bytes; 604 octx->dst_spad.size = L.dst_bytes; 605 606 octx->src0_spad.data = VTCM_LAYOUT_PTR(uint8_t, base, L.off_src0); 607 octx->src1_spad.data = VTCM_LAYOUT_PTR(uint8_t, base, L.off_src1); 608 octx->dst_spad.data = VTCM_LAYOUT_PTR(uint8_t, base, L.off_dst); 609 octx->src0_spad.src = NULL; 610 octx->src1_spad.src = NULL; 611 octx->dst_spad.src = NULL; 612 613 struct htp_ssm_conv_context scctx = { 614 .octx = octx, 615 .kparams = kparams, 616 .nrows_per_thread = d_inner_per_thread, 617 .d_inner_tile = kparams->d_inner_tile, 618 .row_start = row_start, 619 .nrows = nrows, 620 }; 621 622 FARF(HIGH, "ssm-conv-f32: (%ux%ux%ux%u) x (%ux%ux%ux%u) -> (%ux%ux%ux%u) : mode %s\n", 623 src0->ne[0], src0->ne[1], src0->ne[2], src0->ne[3], 624 src1->ne[0], src1->ne[1], src1->ne[2], src1->ne[3], 625 dst->ne[0], dst->ne[1], dst->ne[2], dst->ne[3], 626 kparams->n_t == 1 ? "decode" : "prefill"); 627 628 if (kparams->n_t == 1) { 629 work_queue_run(octx->ctx->work_queue, ssm_conv_thread_f32_decode, &scctx, n_threads); 630 } else { 631 work_queue_run(octx->ctx->work_queue, ssm_conv_thread_f32_prefill, &scctx, n_threads); 632 } 633 634 return HTP_STATUS_OK; 635 } 636 637 int op_ssm_conv(struct htp_ops_context * octx) { 638 const struct htp_tensor * dst = octx->dst; 639 640 switch (dst->type) { 641 case HTP_TYPE_F32: 642 return op_ssm_conv_f32(octx); 643 default: 644 return HTP_STATUS_NO_SUPPORT; 645 } 646 }