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/repeat-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 / repeat-ops.c` 163 lines ยท 5761 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 10 #include "hvx-utils.h" 11 12 #define GGML_COMMON_DECL_C 13 #include "ggml-common.h" 14 #include "htp-ctx.h" 15 #include "hex-common.h" 16 #include "hex-profile.h" 17 #include "htp-ops.h" 18 #include "htp-tensor.h" 19 #include "repeat-ops.h" 20 21 struct htp_repeat_context { 22 struct htp_ops_context * octx; 23 const struct htp_repeat_kernel_params * kparams; 24 25 uint32_t nrows_per_thread; 26 uint32_t total_dst_rows; // ne1 * ne2 * ne3 27 uint32_t row_start; 28 29 size_t type_size; 30 }; 31 32 static void repeat_job_per_thread(unsigned int nth, unsigned int ith, void * data) { 33 const struct htp_repeat_context * rctx = (const struct htp_repeat_context *) data; 34 struct htp_ops_context * octx = rctx->octx; 35 const struct htp_repeat_kernel_params * kparams = rctx->kparams; 36 const struct htp_tensor * src = octx->src[0]; 37 const struct htp_tensor * dst = octx->dst; 38 39 const uint32_t ne00 = src->ne[0]; 40 const uint32_t ne01 = src->ne[1]; 41 const uint32_t ne02 = src->ne[2]; 42 const uint32_t ne03 = src->ne[3]; 43 44 const uint32_t nb00 = src->nb[0]; 45 const uint32_t nb01 = src->nb[1]; 46 const uint32_t nb02 = src->nb[2]; 47 const uint32_t nb03 = src->nb[3]; 48 49 const uint32_t ne0 = dst->ne[0]; 50 const uint32_t ne1 = dst->ne[1]; 51 const uint32_t ne2 = dst->ne[2]; 52 const uint32_t ne3 = dst->ne[3]; 53 54 const uint32_t nb0 = dst->nb[0]; 55 const uint32_t nb1 = dst->nb[1]; 56 const uint32_t nb2 = dst->nb[2]; 57 const uint32_t nb3 = dst->nb[3]; 58 59 const uint32_t nr0 = kparams->nr0; 60 61 const size_t row_bytes = ne00 * rctx->type_size; 62 63 const uint32_t row_start = rctx->row_start + rctx->nrows_per_thread * ith; 64 const uint32_t row_end = MIN(row_start + rctx->nrows_per_thread, rctx->row_start + rctx->total_dst_rows); 65 if (row_start >= row_end) { 66 return; 67 } 68 69 struct htp_thread_trace * tr = &octx->ctx->trace[ith]; 70 htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_COMP, row_start); 71 72 for (uint32_t dst_row = row_start; dst_row < row_end; dst_row++) { 73 // Decompose flat dst row index into (i1, i2, i3) 74 const uint32_t dst_row_div_ne1 = fastdiv(dst_row, &kparams->div_ne1); 75 const uint32_t i1 = dst_row - dst_row_div_ne1 * ne1; 76 const uint32_t i3 = fastdiv(dst_row, &kparams->div_ne12); 77 const uint32_t i2 = dst_row_div_ne1 - i3 * ne2; 78 79 // Map to source indices (tiling) 80 const uint32_t k1 = fastmodulo(i1, ne01, &kparams->div_ne01); 81 const uint32_t k2 = fastmodulo(i2, ne02, &kparams->div_ne02); 82 const uint32_t k3 = fastmodulo(i3, ne03, &kparams->div_ne03); 83 84 const uint8_t * src_row = (const uint8_t *) src->data + k1 * nb01 + k2 * nb02 + k3 * nb03; 85 uint8_t * dst_base = (uint8_t *) dst->data + i1 * nb1 + i2 * nb2 + i3 * nb3; 86 87 // Tile along dimension 0 88 for (uint32_t i0 = 0; i0 < nr0; i0++) { 89 uint8_t * dst_ptr = dst_base + i0 * ne00 * nb0; 90 memcpy(dst_ptr, src_row, row_bytes); 91 } 92 } 93 94 htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_COMP, row_start); 95 96 FARF(HIGH, "repeat %d/%d: (%ux%ux%ux%u) -> (%ux%ux%ux%u) rows %u:%u\n", 97 ith, nth, src->ne[0], src->ne[1], src->ne[2], src->ne[3], 98 dst->ne[0], dst->ne[1], dst->ne[2], dst->ne[3], 99 row_start, row_end); 100 } 101 102 int op_repeat(struct htp_ops_context * octx) { 103 const struct htp_tensor * src0 = octx->src[0]; 104 const struct htp_tensor * dst = octx->dst; 105 106 size_t type_size; 107 switch (src0->type) { 108 case HTP_TYPE_F32: type_size = 4; break; 109 case HTP_TYPE_F16: type_size = 2; break; 110 default: 111 FARF(ERROR, "repeat: unsupported type %u\n", src0->type); 112 return HTP_STATUS_NO_SUPPORT; 113 } 114 115 if (htp_tensor_is_extended(src0) || htp_tensor_is_extended(dst)) { 116 return HTP_STATUS_NO_SUPPORT; 117 } 118 119 const struct htp_repeat_kernel_params * kparams = 120 (const struct htp_repeat_kernel_params *) octx->kernel_params; 121 122 if (!htp_ops_context_set_n_threads(octx, kparams->n_threads)) { 123 return HTP_STATUS_INVAL_PARAMS; 124 } 125 126 const uint32_t total_dst_rows = dst->ne[1] * dst->ne[2] * dst->ne[3]; 127 const size_t dst_row_size = dst->ne[0] * type_size; 128 129 uint32_t row_start = 0; 130 uint32_t nrows = total_dst_rows; 131 132 if (octx->ctx->mdev.count > 1) { 133 uint32_t rows_per_chunk = 0; 134 htp_tensor_mdev_rows_per_chunk(dst, type_size, (uint32_t) dst_row_size, &rows_per_chunk); 135 const struct htp_tensor_mdev_range range = htp_tensor_mdev_partition(total_dst_rows, rows_per_chunk, octx->ctx->mdev.idx, octx->ctx->mdev.count, &octx->ctx->mdev.count_div); 136 row_start = range.start; 137 nrows = range.count; 138 } 139 140 if (nrows == 0) { 141 return HTP_STATUS_OK; 142 } 143 144 const uint32_t n_threads = octx->n_threads; 145 146 struct htp_repeat_context rctx = { 147 .octx = octx, 148 .kparams = kparams, 149 .nrows_per_thread = fastdiv(nrows + n_threads - 1, &octx->n_threads_div), 150 .total_dst_rows = nrows, 151 .row_start = row_start, 152 .type_size = type_size, 153 }; 154 155 FARF(HIGH, "repeat: (%ux%ux%ux%u) -> (%ux%ux%ux%u) nr=(%u,%u,%u,%u)\n", 156 src0->ne[0], src0->ne[1], src0->ne[2], src0->ne[3], 157 dst->ne[0], dst->ne[1], dst->ne[2], dst->ne[3], 158 kparams->nr0, kparams->nr1, kparams->nr2, kparams->nr3); 159 160 work_queue_run(octx->ctx->work_queue, repeat_job_per_thread, &rctx, n_threads); 161 162 return HTP_STATUS_OK; 163 }