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/set-rows-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 / set-rows-ops.c` 254 lines ยท 18579 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 9 #include 10 11 #include "dma-queue.h" 12 #include "work-queue.h" 13 #include "hvx-utils.h" 14 #include "hex-utils.h" 15 #include "hvx-copy.h" 16 #include "hvx-quant.h" 17 18 #define GGML_COMMON_DECL_C 19 #include "ggml-common.h" 20 21 #include "hex-common.h" 22 #include "htp-ctx.h" 23 #include "htp-ops.h" 24 #include "htp-tensor.h" 25 #include "htp/set-rows-ops.h" 26 27 #define set_rows_preamble \ 28 const uint32_t ne00 = octx->src[0]->ne[0]; \ 29 const uint32_t ne01 = octx->src[0]->ne[1]; \ 30 const uint32_t ne02 = octx->src[0]->ne[2]; \ 31 const uint32_t ne03 = octx->src[0]->ne[3]; \ 32 \ 33 const uint32_t ne10 = octx->src[1]->ne[0]; \ 34 const uint32_t ne11 = octx->src[1]->ne[1]; \ 35 const uint32_t ne12 = octx->src[1]->ne[2]; \ 36 const uint32_t ne13 = octx->src[1]->ne[3]; \ 37 \ 38 const uint32_t nb01 = octx->src[0]->nb[1]; \ 39 const uint32_t nb02 = octx->src[0]->nb[2]; \ 40 const uint32_t nb03 = octx->src[0]->nb[3]; \ 41 \ 42 const uint32_t nb10 = octx->src[1]->nb[0]; \ 43 const uint32_t nb11 = octx->src[1]->nb[1]; \ 44 const uint32_t nb12 = octx->src[1]->nb[2]; \ 45 \ 46 const uint32_t nb1 = octx->dst->nb[1]; \ 47 const uint32_t nb2 = octx->dst->nb[2]; \ 48 const uint32_t nb3 = octx->dst->nb[3]; \ 49 \ 50 const uint32_t ne0 = octx->dst->ne[0]; \ 51 const uint32_t ne1 = octx->dst->ne[1]; \ 52 const uint32_t ne2 = octx->dst->ne[2]; \ 53 const uint32_t ne3 = octx->dst->ne[3]; \ 54 \ 55 const uint32_t nr = ne01; 56 57 struct set_rows_context { 58 struct htp_ops_context * octx; 59 const struct htp_set_rows_kernel_params * kparams; 60 struct htp_set_rows_vtcm_layout vtcm_layout; 61 uint8_t * vtcm_base; 62 uint32_t task_start; 63 uint32_t tasks; 64 uint32_t tasks_per_thread; 65 }; 66 67 #define SET_ROWS_THREAD_DMA_FN(TYPE_NAME, IDX_TYPE, COMPUTE_EXPR) \ 68 static void set_rows_thread_dma_##TYPE_NAME##_##IDX_TYPE(unsigned int nth, unsigned int ith, void *data) { \ 69 struct set_rows_context * srctx = (struct set_rows_context *)data; \ 70 struct htp_ops_context * octx = srctx->octx; \ 71 const struct htp_set_rows_kernel_params * kparams = srctx->kparams; \ 72 set_rows_preamble; \ 73 struct htp_thread_trace * tr = &octx->ctx->trace[ith]; \ 74 const uint32_t dr = srctx->tasks_per_thread; \ 75 const uint32_t ir0 = srctx->task_start + dr * ith; \ 76 if (ir0 >= srctx->task_start + srctx->tasks) { \ 77 return; \ 78 } \ 79 const uint32_t ir1 = MIN(ir0 + dr, srctx->task_start + srctx->tasks); \ 80 dma_queue * dma_q = octx->ctx->dma[ith]; \ 81 const struct htp_set_rows_vtcm_layout * vtcm_layout = &srctx->vtcm_layout; \ 82 uint8_t * vtcm_src0 = VTCM_LAYOUT_PTR(uint8_t, srctx->vtcm_base, vtcm_layout->off_src0) + \ 83 ith * vtcm_layout->src0_bytes_per_thread; \ 84 uint8_t * vtcm_dst = VTCM_LAYOUT_PTR(uint8_t, srctx->vtcm_base, vtcm_layout->off_dst) + \ 85 ith * vtcm_layout->dst_bytes_per_thread; \ 86 const uint32_t src0_row_size = ne00 * sizeof(float); \ 87 const uint32_t dst_row_size = htp_tensor_get_row_size(octx->dst->type, ne00); \ 88 const uint32_t nrows_per_thread = ir1 - ir0; \ 89 const uint32_t total_steps = ne03 * ne02 * nrows_per_thread; \ 90 uint32_t pi_step = 0; \ 91 uint32_t pi02 = 0; \ 92 uint32_t pi03 = 0; \ 93 for (uint32_t step = 0, spad_idx = 0; step < total_steps && spad_idx < 2; ++step, spad_idx++) { \ 94 uint32_t i = ir0 + pi_step; \ 95 const dma_addr_t src0_data = octx->src[0]->data + i*nb01 + pi02*nb02 + pi03*nb03; \ 96 dma_queue_push(dma_q, \ 97 dma_make_data(octx->dst->data, \ 98 vtcm_dst + spad_idx * vtcm_layout->dst_spad_half_size), \ 99 dst_row_size, vtcm_layout->dst_spad_half_size, dst_row_size, 0); \ 100 dma_queue_push(dma_q, \ 101 dma_make_data(vtcm_src0 + spad_idx * vtcm_layout->src0_spad_half_size, \ 102 src0_data), \ 103 vtcm_layout->src0_spad_half_size, src0_row_size, src0_row_size, 1); \ 104 pi_step++; \ 105 if (pi_step == nrows_per_thread) { \ 106 pi_step = 0; \ 107 pi02++; \ 108 if (pi02 == ne02) { \ 109 pi02 = 0; \ 110 pi03++; \ 111 } \ 112 } \ 113 } \ 114 uint32_t ci_step = 0; \ 115 uint32_t ci02 = 0; \ 116 uint32_t ci03 = 0; \ 117 uint32_t ci11_base = 0; \ 118 uint32_t ci12_base = 0; \ 119 for (uint32_t step = 0; step < total_steps; ++step) { \ 120 void * dst_spad = (void *) dma_queue_pop(dma_q).src; \ 121 void * src_spad = (void *) dma_queue_pop(dma_q).dst; \ 122 uint32_t i = ir0 + ci_step; \ 123 const uintptr_t src1_addr = octx->src[1]->data + i*nb10 + ci11_base*nb11 + ci12_base*nb12; \ 124 const IDX_TYPE i1 = *(const IDX_TYPE *)src1_addr; \ 125 const bool valid_i1 = ((uint64_t)i1 < (uint64_t)ne1); \ 126 const uint32_t target_i1 = (uint32_t)i1; \ 127 htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_COMP, step); \ 128 if (valid_i1) { \ 129 COMPUTE_EXPR; \ 130 } \ 131 htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_COMP, step); \ 132 if (valid_i1) { \ 133 const dma_addr_t dst_data = octx->dst->data + target_i1*nb1 + ci02*nb2 + ci03*nb3; \ 134 dma_queue_push(dma_q, \ 135 dma_make_data(dst_data, dst_spad), \ 136 dst_row_size, vtcm_layout->dst_spad_half_size, dst_row_size, 1); \ 137 } else { \ 138 dma_queue_push(dma_q, \ 139 dma_make_data(octx->dst->data, dst_spad), \ 140 dst_row_size, vtcm_layout->dst_spad_half_size, dst_row_size, 0); \ 141 } \ 142 const uint32_t next_step = step + 2; \ 143 if (next_step < total_steps) { \ 144 uint32_t ni = ir0 + pi_step; \ 145 const dma_addr_t psrc0_data = octx->src[0]->data + ni*nb01 + pi02*nb02 + pi03*nb03; \ 146 dma_queue_push(dma_q, \ 147 dma_make_data(src_spad, psrc0_data), \ 148 vtcm_layout->src0_spad_half_size, src0_row_size, src0_row_size, 1); \ 149 pi_step++; \ 150 if (pi_step == nrows_per_thread) { \ 151 pi_step = 0; \ 152 pi02++; \ 153 if (pi02 == ne02) { \ 154 pi02 = 0; \ 155 pi03++; \ 156 } \ 157 } \ 158 } \ 159 ci_step++; \ 160 if (ci_step == nrows_per_thread) { \ 161 ci_step = 0; \ 162 ci02++; \ 163 ci11_base++; \ 164 if (ci11_base == ne11) { \ 165 ci11_base = 0; \ 166 } \ 167 if (ci02 == ne02) { \ 168 ci02 = 0; \ 169 ci03++; \ 170 ci12_base++; \ 171 if (ci12_base == ne12) { \ 172 ci12_base = 0; \ 173 } \ 174 } \ 175 } \ 176 } \ 177 dma_queue_flush(dma_q); \ 178 } 179 180 SET_ROWS_THREAD_DMA_FN(f32, int32_t, { hvx_copy_f32_uu((uint8_t *)dst_spad, (const uint8_t *)src_spad, ne00); }) 181 SET_ROWS_THREAD_DMA_FN(f32, int64_t, { hvx_copy_f32_uu((uint8_t *)dst_spad, (const uint8_t *)src_spad, ne00); }) 182 183 SET_ROWS_THREAD_DMA_FN(f16, int32_t, { hvx_copy_f16_f32_uu((uint8_t *)dst_spad, (const uint8_t *)src_spad, ne00); }) 184 SET_ROWS_THREAD_DMA_FN(f16, int64_t, { hvx_copy_f16_f32_uu((uint8_t *)dst_spad, (const uint8_t *)src_spad, ne00); }) 185 186 SET_ROWS_THREAD_DMA_FN(q8_0, int32_t, { hvx_quantize_row_q8_0_f32(dst_spad, (const float *)src_spad, ne00); }) 187 SET_ROWS_THREAD_DMA_FN(q8_0, int64_t, { hvx_quantize_row_q8_0_f32(dst_spad, (const float *)src_spad, ne00); }) 188 189 int op_set_rows(struct htp_ops_context * octx) { 190 const struct htp_set_rows_kernel_params * kparams = (const struct htp_set_rows_kernel_params *)octx->kernel_params; 191 set_rows_preamble; 192 193 if (htp_tensor_is_extended(octx->src[1])) { 194 return HTP_STATUS_NO_SUPPORT; 195 } 196 197 const struct htp_tensor * dst = octx->dst; 198 const uint32_t total_tasks = kparams->total_tasks; 199 200 uint32_t task_start = 0; 201 uint32_t tasks = total_tasks; 202 203 if (octx->ctx->mdev.count > 1) { 204 const bool can_split = htp_tensor_mdev_data_aligned(dst) && (dst->nb[1] & (HTP_TENSOR_MDEV_LINE_SIZE - 1)) == 0 && !htp_tensor_is_permuted(dst); 205 const struct htp_tensor_mdev_range range = htp_tensor_mdev_partition(total_tasks, can_split ? 1 : 0, octx->ctx->mdev.idx, octx->ctx->mdev.count, &octx->ctx->mdev.count_div); 206 task_start = range.start; 207 tasks = range.count; 208 } 209 210 if (tasks == 0) { 211 return HTP_STATUS_OK; 212 } 213 214 if (!htp_ops_context_set_n_threads(octx, (uint32_t) kparams->n_threads)) { 215 return HTP_STATUS_INVAL_PARAMS; 216 } 217 218 const uint32_t n_threads = octx->n_threads; 219 220 // l2fetch the src1 (indices) tensor in the main thread 221 hex_l2fetch_block((const void *)octx->src[1]->data, octx->src[1]->ne[3] * octx->src[1]->nb[3]); 222 223 struct set_rows_context srctx; 224 srctx.octx = octx; 225 srctx.kparams = kparams; 226 srctx.task_start = task_start; 227 srctx.tasks = tasks; 228 srctx.tasks_per_thread = fastdiv(tasks + n_threads - 1, &octx->n_threads_div); 229 230 htp_set_rows_vtcm_layout_build(&srctx.vtcm_layout, octx->dst->type, ne00, n_threads); 231 srctx.vtcm_base = (uint8_t *)octx->ctx->vtcm_base; 232 233 work_queue_func_t q_func = NULL; 234 const bool is_i32 = (octx->src[1]->type == HTP_TYPE_I32); 235 236 switch (octx->dst->type) { 237 case HTP_TYPE_F32: q_func = is_i32 ? set_rows_thread_dma_f32_int32_t : set_rows_thread_dma_f32_int64_t; break; 238 case HTP_TYPE_F16: q_func = is_i32 ? set_rows_thread_dma_f16_int32_t : set_rows_thread_dma_f16_int64_t; break; 239 case HTP_TYPE_Q8_0: q_func = is_i32 ? set_rows_thread_dma_q8_0_int32_t : set_rows_thread_dma_q8_0_int64_t; break; 240 default: return HTP_STATUS_NO_SUPPORT; 241 } 242 243 FARF(HIGH, "set-rows: (%ux%ux%ux%u) x (%ux%ux%ux%u) -> (%ux%ux%ux%u) : src0-vtcm-size %zu dst-vtcm-size %zu n-threads %d\n", 244 octx->src[0]->ne[0], octx->src[0]->ne[1], octx->src[0]->ne[2], octx->src[0]->ne[3], 245 octx->src[1]->ne[0], octx->src[1]->ne[1], octx->src[1]->ne[2], octx->src[1]->ne[3], 246 octx->dst->ne[0], octx->dst->ne[1], octx->dst->ne[2], octx->dst->ne[3], 247 srctx.vtcm_layout.src0_bytes_per_thread * n_threads, 248 srctx.vtcm_layout.dst_bytes_per_thread * n_threads, 249 n_threads); 250 251 work_queue_run(octx->ctx->work_queue, q_func, &srctx, n_threads); 252 253 return HTP_STATUS_OK; 254 }