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/cumsum-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 / cumsum-ops.c` 290 lines ยท 11572 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 #define GGML_COMMON_DECL_C 9 #include "ggml-common.h" 10 #include "hex-common.h" 11 #include "hex-profile.h" 12 #include "htp-ctx.h" 13 #include "htp-ops.h" 14 #include "htp-tensor.h" 15 #include "htp-vtcm.h" 16 #include "hvx-types.h" 17 #include "hvx-utils.h" 18 #include "dma-queue.h" 19 #include "cumsum-ops.h" 20 21 #define htp_cumsum_tensors_preamble \ 22 const struct htp_tensor * restrict src0 = octx->src[0]; \ 23 const struct htp_tensor * restrict dst = octx->dst; \ 24 \ 25 const uint32_t ne00 = src0->ne[0]; \ 26 const uint32_t ne01 = src0->ne[1]; \ 27 const uint32_t ne02 = src0->ne[2]; \ 28 const uint32_t ne03 = src0->ne[3]; \ 29 \ 30 const uint32_t ne0 = dst->ne[0]; \ 31 const uint32_t ne1 = dst->ne[1]; \ 32 const uint32_t ne2 = dst->ne[2]; \ 33 const uint32_t ne3 = dst->ne[3]; \ 34 \ 35 const uint32_t nb00 = src0->nb[0]; \ 36 const uint32_t nb01 = src0->nb[1]; \ 37 const uint32_t nb02 = src0->nb[2]; \ 38 const uint32_t nb03 = src0->nb[3]; \ 39 \ 40 const uint32_t nb0 = dst->nb[0]; \ 41 const uint32_t nb1 = dst->nb[1]; \ 42 const uint32_t nb2 = dst->nb[2]; \ 43 const uint32_t nb3 = dst->nb[3]; 44 45 struct htp_cumsum_context { 46 struct htp_ops_context * octx; 47 size_t src_row_size; 48 size_t dst_row_size; 49 size_t src_row_size_aligned; 50 size_t dst_row_size_aligned; 51 uint32_t rows_per_thread; 52 uint32_t total_rows; 53 uint32_t row_start; 54 }; 55 56 #define htp_cumsum_preamble \ 57 struct htp_cumsum_context * cctx = (struct htp_cumsum_context *) data; \ 58 struct htp_ops_context * octx = cctx->octx; \ 59 htp_cumsum_tensors_preamble; \ 60 dma_queue * dma_q = octx->ctx->dma[ith]; 61 62 // --------------------------------------------------------------------------- 63 // HVX prefix scan helpers 64 // --------------------------------------------------------------------------- 65 66 #if __HVX_ARCH__ > 75 67 static inline HVX_Vector hvx_cumsum_vadd(HVX_Vector a, HVX_Vector b) { 68 return Q6_Vsf_vadd_VsfVsf(a, b); 69 } 70 #else 71 static inline HVX_Vector hvx_cumsum_vadd(HVX_Vector a, HVX_Vector b) { 72 return Q6_Vsf_equals_Vqf32(Q6_Vqf32_vadd_VsfVsf(a, b)); 73 } 74 #endif // __HVX_ARCH__ > 75 75 76 static inline HVX_Vector hvx_prefix_scan_f32(HVX_Vector v, HVX_Vector carry_in) { 77 const HVX_Vector zero = Q6_V_vsplat_R(0); 78 79 v = hvx_cumsum_vadd(v, Q6_V_vlalign_VVR(v, zero, 4)); 80 v = hvx_cumsum_vadd(v, Q6_V_vlalign_VVR(v, zero, 8)); 81 v = hvx_cumsum_vadd(v, Q6_V_vlalign_VVR(v, zero, 16)); 82 v = hvx_cumsum_vadd(v, Q6_V_vlalign_VVR(v, zero, 32)); 83 v = hvx_cumsum_vadd(v, Q6_V_vlalign_VVR(v, zero, 64)); 84 v = hvx_cumsum_vadd(v, carry_in); 85 86 return v; 87 } 88 89 static inline HVX_Vector hvx_splat_last_f32(HVX_Vector v) { 90 return hvx_vec_repl4(Q6_V_vror_VR(v, 124)); 91 } 92 93 static inline void hvx_cumsum_row_f32(const float * restrict src, float * restrict dst, uint32_t n) { 94 const uint32_t nvec = n >> 5; 95 const uint32_t nloe = n & (VLEN_FP32 - 1); 96 97 HVX_Vector carry = Q6_V_vsplat_R(0); 98 99 for (uint32_t i = 0; i < nvec; i++) { 100 HVX_Vector v = *((const HVX_UVector *) (src + i * VLEN_FP32)); 101 v = hvx_prefix_scan_f32(v, carry); 102 hvx_vec_store_u(dst + i * VLEN_FP32, VLEN, v); 103 carry = hvx_splat_last_f32(v); 104 } 105 106 if (nloe) { 107 float acc = hvx_vec_get_f32(carry); 108 const float * src_tail = src + nvec * VLEN_FP32; 109 float * dst_tail = dst + nvec * VLEN_FP32; 110 for (uint32_t i = 0; i < nloe; i++) { 111 acc += src_tail[i]; 112 dst_tail[i] = acc; 113 } 114 } 115 } 116 117 // --------------------------------------------------------------------------- 118 // Per thread worker: Double-buffered DMA 119 // --------------------------------------------------------------------------- 120 121 static void cumsum_thread_f32_dma(unsigned int nth, unsigned int ith, void * data) { 122 htp_cumsum_preamble; 123 124 const uint32_t ir0 = cctx->row_start + cctx->rows_per_thread * ith; 125 const uint32_t ir1 = MIN(ir0 + cctx->rows_per_thread, cctx->row_start + cctx->total_rows); 126 127 if (ir0 >= ir1) { 128 return; 129 } 130 131 const size_t src_row_size = cctx->src_row_size; 132 const size_t dst_row_size = cctx->dst_row_size; 133 const size_t src_row_size_aligned = cctx->src_row_size_aligned; 134 const size_t dst_row_size_aligned = cctx->dst_row_size_aligned; 135 136 const dma_addr_t src_data = src0->data; 137 const dma_addr_t dst_data = dst->data; 138 139 uint8_t * src_spad = octx->src0_spad.data + (ith * src_row_size_aligned * 2); 140 uint8_t * dst_spad = octx->dst_spad.data + (ith * dst_row_size_aligned * 2); 141 142 for (uint32_t ir = ir0, spad_idx = 0; ir < ir1 && spad_idx < 2; ir++, spad_idx++) { 143 // Dummy dst writeback to establish queue ordering 144 dma_queue_push(dma_q, 145 dma_make_data(dst_data, dst_spad + (spad_idx * dst_row_size_aligned)), 146 dst_row_size, dst_row_size_aligned, dst_row_size, 0); 147 148 dma_queue_push(dma_q, 149 dma_make_data(src_spad + (spad_idx * src_row_size_aligned), 150 src_data + (ir * src_row_size)), 151 src_row_size_aligned, src_row_size, src_row_size, 1); 152 } 153 154 struct htp_thread_trace * tr = &octx->ctx->trace[ith]; 155 156 for (uint32_t ir = ir0; ir < ir1; ir++) { 157 float * dst_spad_row = (float *) dma_queue_pop(dma_q).src; 158 float * src_spad_row = (float *) dma_queue_pop(dma_q).dst; 159 160 htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) ir); 161 hvx_cumsum_row_f32(src_spad_row, dst_spad_row, ne00); 162 htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) ir); 163 164 dma_queue_push(dma_q, 165 dma_make_data(dst_data + (ir * dst_row_size), dst_spad_row), 166 dst_row_size, dst_row_size_aligned, dst_row_size, 1); 167 168 const uint32_t next_row = ir + 2; 169 if (next_row < ir1) { 170 dma_queue_push(dma_q, 171 dma_make_data(src_spad_row, src_data + (next_row * src_row_size)), 172 src_row_size_aligned, src_row_size, src_row_size, 1); 173 } 174 } 175 176 dma_queue_flush(dma_q); 177 178 FARF(HIGH, "cumsum-f32-dma %d/%d: %ux%ux%ux%u (%u:%u) -> %ux%ux%ux%u\n", 179 ith, nth, src0->ne[0], src0->ne[1], src0->ne[2], src0->ne[3], ir0, ir1, 180 dst->ne[0], dst->ne[1], dst->ne[2], dst->ne[3]); 181 } 182 183 // --------------------------------------------------------------------------- 184 // Per thread worker: Direct HVX (no DMA) 185 // --------------------------------------------------------------------------- 186 187 static void cumsum_thread_f32(unsigned int nth, unsigned int ith, void * data) { 188 htp_cumsum_preamble; 189 190 const uint8_t * src_data = (const uint8_t *) src0->data; 191 uint8_t * dst_data = (uint8_t *) dst->data; 192 193 const uint32_t ir0 = cctx->row_start + cctx->rows_per_thread * ith; 194 const uint32_t ir1 = MIN(ir0 + cctx->rows_per_thread, cctx->row_start + cctx->total_rows); 195 196 struct htp_thread_trace * tr = &octx->ctx->trace[ith]; 197 htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) ir0); 198 199 for (uint32_t ir = ir0; ir < ir1; ir++) { 200 const float * restrict src_row = (const float *) (src_data + ir * cctx->src_row_size); 201 float * restrict dst_row = (float *) (dst_data + ir * cctx->dst_row_size); 202 hvx_cumsum_row_f32(src_row, dst_row, ne00); 203 } 204 205 htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) ir0); 206 207 FARF(HIGH, "cumsum-f32 %d/%d: %ux%ux%ux%u (%u:%u) -> %ux%ux%ux%u\n", 208 ith, nth, src0->ne[0], src0->ne[1], src0->ne[2], src0->ne[3], ir0, ir1, 209 dst->ne[0], dst->ne[1], dst->ne[2], dst->ne[3]); 210 } 211 212 int op_cumsum_f32(struct htp_ops_context * octx) { 213 const struct htp_tensor * src0 = octx->src[0]; 214 const struct htp_tensor * dst = octx->dst; 215 const struct htp_cumsum_kernel_params * kparams = 216 (const struct htp_cumsum_kernel_params *) octx->kernel_params; 217 218 const uint32_t total_rows = kparams->total_rows; 219 const size_t src_row_size = src0->nb[1]; 220 const size_t dst_row_size = dst->nb[1]; 221 222 uint32_t row_start = 0; 223 uint32_t nrows = total_rows; 224 225 if (octx->ctx->mdev.count > 1) { 226 uint32_t rows_per_chunk = 0; 227 htp_tensor_mdev_rows_per_chunk(dst, sizeof(float), (uint32_t) dst_row_size, &rows_per_chunk); 228 const struct htp_tensor_mdev_range range = htp_tensor_mdev_partition(total_rows, rows_per_chunk, octx->ctx->mdev.idx, octx->ctx->mdev.count, &octx->ctx->mdev.count_div); 229 row_start = range.start; 230 nrows = range.count; 231 } 232 233 if (nrows == 0) { 234 return HTP_STATUS_OK; 235 } 236 237 if (!htp_ops_context_set_n_threads(octx, MIN(kparams->n_threads, nrows))) { 238 return HTP_STATUS_INVAL_PARAMS; 239 } 240 241 const uint32_t n_threads = octx->n_threads; 242 243 if (!kparams->use_dma && (htp_tensor_is_extended(src0) || htp_tensor_is_extended(dst))) { 244 return HTP_STATUS_NO_SUPPORT; 245 } 246 247 struct htp_cumsum_vtcm_layout layout = { 0 }; 248 if (kparams->use_dma) { 249 htp_cumsum_vtcm_layout_build(&layout, src_row_size, dst_row_size, n_threads); 250 251 octx->src0_spad.size_per_thread = layout.src0_bytes_per_thread; 252 octx->dst_spad.size_per_thread = layout.dst_bytes_per_thread; 253 octx->src0_spad.size = n_threads * layout.src0_bytes_per_thread; 254 octx->dst_spad.size = n_threads * layout.dst_bytes_per_thread; 255 octx->src0_spad.data = VTCM_LAYOUT_PTR(uint8_t, octx->ctx->vtcm_base, layout.off_src0); 256 octx->dst_spad.data = VTCM_LAYOUT_PTR(uint8_t, octx->ctx->vtcm_base, layout.off_dst); 257 octx->src0_spad.src = NULL; 258 octx->dst_spad.src = NULL; 259 } 260 261 struct htp_cumsum_context cctx = { 262 .octx = octx, 263 .src_row_size = src_row_size, 264 .dst_row_size = dst_row_size, 265 .src_row_size_aligned = layout.src_row_size_aligned, 266 .dst_row_size_aligned = layout.dst_row_size_aligned, 267 .rows_per_thread = fastdiv(nrows + n_threads - 1, &octx->n_threads_div), 268 .total_rows = nrows, 269 .row_start = row_start, 270 }; 271 272 if (kparams->use_dma) { 273 work_queue_run(octx->ctx->work_queue, cumsum_thread_f32_dma, &cctx, n_threads); 274 } else { 275 work_queue_run(octx->ctx->work_queue, cumsum_thread_f32, &cctx, n_threads); 276 } 277 278 return HTP_STATUS_OK; 279 } 280 281 int op_cumsum(struct htp_ops_context * octx) { 282 const struct htp_tensor * dst = octx->dst; 283 284 switch (dst->type) { 285 case HTP_TYPE_F32: 286 return op_cumsum_f32(octx); 287 default: 288 return HTP_STATUS_NO_SUPPORT; 289 } 290 }