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/roll-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 / roll-ops.c` 304 lines ยท 12407 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 "dma-queue.h" 11 #include "hvx-utils.h" 12 13 #define GGML_COMMON_DECL_C 14 #include "ggml-common.h" 15 #include "htp-ctx.h" 16 #include "hex-common.h" 17 #include "hex-profile.h" 18 #include "htp-ops.h" 19 #include "htp-tensor.h" 20 #include "roll-ops.h" 21 22 struct htp_roll_context { 23 struct htp_ops_context * octx; 24 const struct htp_roll_kernel_params * kparams; 25 26 uint32_t row_start; 27 uint32_t nrows; 28 uint32_t nrows_per_thread; 29 }; 30 31 static inline uint32_t htp_roll_wrap(int32_t i, uint32_t ne) { 32 if (i < 0) { 33 return (uint32_t) (i + (int32_t) ne); 34 } 35 if ((uint32_t) i >= ne) { 36 return (uint32_t) i - ne; 37 } 38 return (uint32_t) i; 39 } 40 41 #define htp_roll_preamble \ 42 const struct htp_tensor * src0 = octx->src[0]; \ 43 const struct htp_tensor * dst = octx->dst; \ 44 const struct htp_roll_kernel_params * kparams = \ 45 (const struct htp_roll_kernel_params *) octx->kernel_params; \ 46 \ 47 const uint32_t ne0 = dst->ne[0]; \ 48 const uint32_t ne1 = dst->ne[1]; \ 49 const uint32_t ne2 = dst->ne[2]; \ 50 const uint32_t ne3 = dst->ne[3]; \ 51 \ 52 const uint32_t nb01 = src0->nb[1]; \ 53 const uint32_t nb02 = src0->nb[2]; \ 54 const uint32_t nb03 = src0->nb[3]; \ 55 \ 56 const uint32_t nb1 = dst->nb[1]; \ 57 const uint32_t nb2 = dst->nb[2]; \ 58 const uint32_t nb3 = dst->nb[3]; \ 59 \ 60 const int32_t s0 = octx->op_params[0]; \ 61 const int32_t s1 = octx->op_params[1]; \ 62 const int32_t s2 = octx->op_params[2]; \ 63 const int32_t s3 = octx->op_params[3]; \ 64 \ 65 const uint32_t i0_src0 = kparams->i0_src0; \ 66 const uint32_t n0 = kparams->n0; 67 68 #define htp_roll_dma_preamble dma_queue * q = octx->ctx->dma[0]; 69 70 static inline void roll_dma_push(dma_queue * q, 71 dma_addr_t dst, 72 dma_addr_t src, 73 uint32_t dst_stride, 74 uint32_t src_stride, 75 uint32_t bytes, 76 uint32_t nrows) { 77 if (bytes == 0 || nrows == 0) { 78 return; 79 } 80 81 if (!dma_queue_push(q, dma_make_data(dst, src), dst_stride, src_stride, bytes, nrows)) { 82 dma_queue_flush(q); 83 dma_queue_push(q, dma_make_data(dst, src), 84 dst_stride, src_stride, bytes, nrows); 85 } 86 } 87 88 static inline void roll_dma_push_rows(dma_queue * q, 89 const struct htp_tensor * dst, 90 const struct htp_tensor * src0, 91 uint32_t dst_row, 92 uint32_t src_row, 93 uint32_t nrows, 94 uint32_t row_size, 95 uint32_t i0_src0, 96 uint32_t n0) { 97 const dma_addr_t dst_base = dst->data + (size_t) dst_row * row_size; 98 const dma_addr_t src_base = src0->data + (size_t) src_row * row_size; 99 100 roll_dma_push(q, dst_base, src_base + (size_t) i0_src0 * sizeof(float), 101 row_size, row_size, n0 * sizeof(float), nrows); 102 roll_dma_push(q, dst_base + (size_t) n0 * sizeof(float), src_base, 103 row_size, row_size, i0_src0 * sizeof(float), nrows); 104 } 105 106 // Same row-wrap split as roll_dma_push_rows, but addressed with explicit byte strides so it 107 // also works for a src0 that is row-contiguous only (e.g. a permuted view) rather than fully packed. 108 static inline void roll_dma_push_range(dma_queue * q, 109 dma_addr_t dst_row, 110 dma_addr_t src_row, 111 uint32_t dst_stride, 112 uint32_t src_stride, 113 uint32_t nrows, 114 uint32_t i0_src0, 115 uint32_t n0) { 116 roll_dma_push(q, dst_row, src_row + (size_t) i0_src0 * sizeof(float), 117 dst_stride, src_stride, n0 * sizeof(float), nrows); 118 roll_dma_push(q, dst_row + (size_t) n0 * sizeof(float), src_row, 119 dst_stride, src_stride, i0_src0 * sizeof(float), nrows); 120 } 121 122 static int roll_dma_f32_contiguous(struct htp_ops_context * octx) { 123 htp_roll_preamble; 124 htp_roll_dma_preamble; 125 126 const uint32_t row_size = ne0 * sizeof(float); 127 128 if (s1 == 0 && s2 == 0 && s3 == 0) { 129 roll_dma_push_rows(q, dst, src0, 0, 0, ne1 * ne2 * ne3, row_size, i0_src0, n0); 130 dma_queue_flush(q); 131 return HTP_STATUS_OK; 132 } 133 134 if (s1 == 0) { 135 const uint32_t i2_src0 = htp_roll_wrap(-s2, ne2); 136 for (uint32_t i3 = 0; i3 < ne3; i3++) { 137 const uint32_t i03 = htp_roll_wrap((int32_t) i3 - s3, ne3); 138 const uint32_t dst_row0 = i3 * ne2 * ne1; 139 const uint32_t src_row0 = (i03 * ne2 + i2_src0) * ne1; 140 const uint32_t n2_first = ne2 - i2_src0; 141 142 roll_dma_push_rows(q, dst, src0, dst_row0, src_row0, n2_first * ne1, 143 row_size, i0_src0, n0); 144 roll_dma_push_rows(q, dst, src0, dst_row0 + n2_first * ne1, i03 * ne2 * ne1, 145 i2_src0 * ne1, row_size, i0_src0, n0); 146 } 147 148 dma_queue_flush(q); 149 return HTP_STATUS_OK; 150 } 151 152 const uint32_t i1_src0 = htp_roll_wrap(-s1, ne1); 153 const uint32_t n1_first = ne1 - i1_src0; 154 155 for (uint32_t i3 = 0; i3 < ne3; i3++) { 156 const uint32_t i03 = htp_roll_wrap((int32_t) i3 - s3, ne3); 157 for (uint32_t i2 = 0; i2 < ne2; i2++) { 158 const uint32_t i02 = htp_roll_wrap((int32_t) i2 - s2, ne2); 159 const uint32_t dst_row0 = (i3 * ne2 + i2) * ne1; 160 const uint32_t src_row0 = (i03 * ne2 + i02) * ne1; 161 162 roll_dma_push_rows(q, dst, src0, dst_row0, src_row0 + i1_src0, 163 n1_first, row_size, i0_src0, n0); 164 roll_dma_push_rows(q, dst, src0, dst_row0 + n1_first, src_row0, 165 i1_src0, row_size, i0_src0, n0); 166 } 167 } 168 169 dma_queue_flush(q); 170 return HTP_STATUS_OK; 171 } 172 173 // DMA path for a row-contiguous but otherwise arbitrarily strided src0 (e.g. a permuted view). 174 // Same row-wrap split as above, one DMA push per (i2,i3), addressed via the real nb01/nb02/nb03 175 // instead of assuming a packed layout. 176 static int roll_dma_f32_strided(struct htp_ops_context * octx) { 177 htp_roll_preamble; 178 htp_roll_dma_preamble; 179 180 const uint32_t i1_src0 = htp_roll_wrap(-s1, ne1); 181 const uint32_t n1_first = ne1 - i1_src0; 182 183 for (uint32_t i3 = 0; i3 < ne3; i3++) { 184 const uint32_t i03 = htp_roll_wrap((int32_t) i3 - s3, ne3); 185 for (uint32_t i2 = 0; i2 < ne2; i2++) { 186 const uint32_t i02 = htp_roll_wrap((int32_t) i2 - s2, ne2); 187 188 const dma_addr_t dst_row0 = dst->data + (size_t) i2 * nb2 + (size_t) i3 * nb3; 189 const dma_addr_t src_row0 = src0->data + (size_t) i02 * nb02 + (size_t) i03 * nb03; 190 191 roll_dma_push_range(q, dst_row0, src_row0 + (size_t) i1_src0 * nb01, 192 nb1, nb01, n1_first, i0_src0, n0); 193 roll_dma_push_range(q, dst_row0 + (size_t) n1_first * nb1, src_row0, 194 nb1, nb01, i1_src0, i0_src0, n0); 195 } 196 } 197 198 dma_queue_flush(q); 199 return HTP_STATUS_OK; 200 } 201 202 static void roll_thread_f32(unsigned int nth, unsigned int ith, void * data) { 203 struct htp_roll_context * rctx = (struct htp_roll_context *) data; 204 struct htp_ops_context * octx = rctx->octx; 205 206 htp_roll_preamble; 207 208 const uint32_t row_start = rctx->row_start + rctx->nrows_per_thread * ith; 209 const uint32_t row_end = MIN(row_start + rctx->nrows_per_thread, rctx->row_start + rctx->nrows); 210 if (row_start >= row_end) { 211 return; 212 } 213 214 struct htp_thread_trace * tr = &octx->ctx->trace[ith]; 215 htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_COMP, row_start); 216 217 for (uint32_t row = row_start; row < row_end; row++) { 218 const uint32_t i3 = fastdiv(row, &kparams->div_ne2_ne1); 219 const uint32_t rem = row - i3 * ne2 * ne1; 220 const uint32_t i2 = fastdiv(rem, &kparams->div_ne1); 221 const uint32_t i1 = rem - i2 * ne1; 222 223 const uint32_t i01 = htp_roll_wrap((int32_t) i1 - s1, ne1); 224 const uint32_t i02 = htp_roll_wrap((int32_t) i2 - s2, ne2); 225 const uint32_t i03 = htp_roll_wrap((int32_t) i3 - s3, ne3); 226 227 const uint8_t * src_row = (const uint8_t *) (uintptr_t) src0->data + i01*nb01 + i02*nb02 + i03*nb03; 228 uint8_t * dst_row = (uint8_t *) (uintptr_t) dst->data + i1*nb1 + i2*nb2 + i3*nb3; 229 230 hex_l2fetch(src_row + i0_src0 * sizeof(float), n0 * sizeof(float), ne0 * sizeof(float), 1); 231 hvx_copy_uu(dst_row, src_row + i0_src0 * sizeof(float), n0, sizeof(float)); 232 233 if (i0_src0 != 0) { 234 hex_l2fetch(src_row, i0_src0 * sizeof(float), ne0 * sizeof(float), 1); 235 hvx_copy_uu(dst_row + n0 * sizeof(float), src_row, i0_src0, sizeof(float)); 236 } 237 } 238 239 htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_COMP, row_start); 240 241 FARF(HIGH, "roll %d/%d: (%ux%ux%ux%u) rows %u:%u shift=(%d,%d,%d,%d)\n", 242 ith, nth, ne0, ne1, ne2, ne3, 243 row_start, row_end, s0, s1, s2, s3); 244 } 245 246 int execute_op_roll_f32(struct htp_ops_context * octx) { 247 htp_roll_preamble; 248 249 if (kparams->kernel_type == HTP_ROLL_KERNEL_DMA_CONTIGUOUS) { 250 return roll_dma_f32_contiguous(octx); 251 } 252 if (kparams->kernel_type == HTP_ROLL_KERNEL_DMA_STRIDED) { 253 return roll_dma_f32_strided(octx); 254 } 255 256 const uint32_t total_rows = kparams->total_rows; 257 const size_t dst_row_size = ne0 * sizeof(float); 258 259 uint32_t row_start = 0; 260 uint32_t nrows = total_rows; 261 262 if (octx->ctx->mdev.count > 1) { 263 uint32_t rows_per_chunk = 0; 264 htp_tensor_mdev_rows_per_chunk(dst, sizeof(float), (uint32_t) dst_row_size, &rows_per_chunk); 265 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); 266 row_start = range.start; 267 nrows = range.count; 268 } 269 270 if (nrows == 0) { 271 return HTP_STATUS_OK; 272 } 273 274 if (htp_tensor_is_extended(src0) || htp_tensor_is_extended(dst)) { 275 return HTP_STATUS_NO_SUPPORT; 276 } 277 278 if (!htp_ops_context_set_n_threads(octx, MIN(kparams->n_threads, nrows))) { 279 return HTP_STATUS_INVAL_PARAMS; 280 } 281 282 const uint32_t n_threads = octx->n_threads; 283 struct htp_roll_context rctx = { 284 .octx = octx, 285 .kparams = kparams, 286 .row_start = row_start, 287 .nrows = nrows, 288 .nrows_per_thread = fastdiv(nrows + n_threads - 1, &octx->n_threads_div), 289 }; 290 291 work_queue_run(octx->ctx->work_queue, roll_thread_f32, &rctx, n_threads); 292 293 return HTP_STATUS_OK; 294 } 295 296 int op_roll(struct htp_ops_context * octx) { 297 switch (octx->src[0]->type) { 298 case HTP_TYPE_F32: 299 return execute_op_roll_f32(octx); 300 301 default: 302 return HTP_STATUS_NO_SUPPORT; 303 } 304 }