ggml
[Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685)
Brought by drakestapleton
ggml / src / ggml-hexagon / htp / diag-ops.c
| 1 | #pragma clang diagnostic ignored "-Wunused-but-set-variable" |
| 2 | |
| 3 | #include <HAP_farf.h> |
| 4 | #include <HAP_perf.h> |
| 5 | |
| 6 | #define GGML_COMMON_DECL_C |
| 7 | #include "ggml-common.h" |
| 8 | #include "hex-common.h" |
| 9 | #include "hex-profile.h" |
| 10 | #include "htp-ctx.h" |
| 11 | #include "htp-ops.h" |
| 12 | #include "htp-tensor.h" |
| 13 | #include "htp-vtcm.h" |
| 14 | #include "hvx-types.h" |
| 15 | #include "hex-utils.h" |
| 16 | #include "hvx-copy.h" |
| 17 | #include "dma-queue.h" |
| 18 | #include "diag-ops.h" |
| 19 | |
| 20 | #define htp_diag_tensors_preamble \ |
| 21 | const struct htp_tensor * restrict src0 = octx->src[0]; \ |
| 22 | const struct htp_tensor * restrict dst = octx->dst; \ |
| 23 | \ |
| 24 | const uint32_t ne02 = src0->ne[2]; \ |
| 25 | \ |
| 26 | const uint32_t ne0 = dst->ne[0]; \ |
| 27 | const uint32_t ne1 = dst->ne[1]; \ |
| 28 | \ |
| 29 | const uint32_t nb02 = src0->nb[2]; \ |
| 30 | const uint32_t nb03 = src0->nb[3]; \ |
| 31 | \ |
| 32 | const uint32_t nb1 = dst->nb[1]; \ |
| 33 | const uint32_t nb2 = dst->nb[2]; \ |
| 34 | const uint32_t nb3 = dst->nb[3]; |
| 35 | |
| 36 | struct htp_diag_context { |
| 37 | struct htp_ops_context * octx; |
| 38 | const struct htp_diag_kernel_params * kparams; |
| 39 | size_t src_batch_size; |
| 40 | size_t dst_row_size; |
| 41 | size_t src_batch_size_aligned; |
| 42 | size_t dst_row_size_aligned; |
| 43 | uint32_t batches_per_thread; |
| 44 | uint32_t total_batches; |
| 45 | uint32_t batch_start; |
| 46 | }; |
| 47 | |
| 48 | #define htp_diag_preamble \ |
| 49 | struct htp_diag_context * dctx = (struct htp_diag_context *) data; \ |
| 50 | struct htp_ops_context * octx = dctx->octx; \ |
| 51 | htp_diag_tensors_preamble; |
| 52 | |
| 53 | static inline void hvx_diag_row_f32(const float * restrict src, float * restrict dst, |
| 54 | uint32_t row_idx, uint32_t n) { |
| 55 | hvx_splat_f32_a((uint8_t *) dst, 0.0f, n); |
| 56 | dst[row_idx] = src[row_idx]; |
| 57 | } |
| 58 | |
| 59 | // --------------------------------------------------------------------------- |
| 60 | // Per thread worker: DMA src fetch, compute in VTCM, DMA dst writeback |
| 61 | // --------------------------------------------------------------------------- |
| 62 | |
| 63 | static void diag_thread_f32_dma(unsigned int nth, unsigned int ith, void * data) { |
| 64 | htp_diag_preamble; |
| 65 | dma_queue * dma_q = octx->ctx->dma[ith]; |
| 66 | |
| 67 | const uint32_t ib0 = dctx->batch_start + dctx->batches_per_thread * ith; |
| 68 | const uint32_t ib1 = MIN(ib0 + dctx->batches_per_thread, dctx->batch_start + dctx->total_batches); |
| 69 | |
| 70 | if (ib0 >= ib1) { |
| 71 | return; |
| 72 | } |
| 73 | |
| 74 | const size_t src_batch_size = dctx->src_batch_size; |
| 75 | const size_t dst_row_size = dctx->dst_row_size; |
| 76 | const size_t src_batch_size_aligned = dctx->src_batch_size_aligned; |
| 77 | const size_t dst_row_size_aligned = dctx->dst_row_size_aligned; |
| 78 | |
| 79 | const dma_addr_t src_data = src0->data; |
| 80 | const dma_addr_t dst_data = dst->data; |
| 81 | |
| 82 | // 1 src buffer + 1 dst row buffer per thread in VTCM |
| 83 | uint8_t * src_spad = octx->src0_spad.data + (ith * src_batch_size_aligned); |
| 84 | uint8_t * dst_spad = octx->dst_spad.data + (ith * dst_row_size_aligned); |
| 85 | |
| 86 | struct htp_thread_trace * tr = &octx->ctx->trace[ith]; |
| 87 | |
| 88 | for (uint32_t ib = ib0; ib < ib1; ib++) { |
| 89 | const uint32_t i3 = fastdiv(ib, &dctx->kparams->div_ne02); |
| 90 | const uint32_t i2 = ib - i3 * ne02; |
| 91 | |
| 92 | const dma_addr_t src_batch = src_data + i3 * nb03 + i2 * nb02; |
| 93 | |
| 94 | // Fetch source vector into VTCM |
| 95 | dma_queue_push(dma_q, |
| 96 | dma_make_data(src_spad, src_batch), |
| 97 | src_batch_size_aligned, src_batch_size, src_batch_size, 1); |
| 98 | dma_queue_flush(dma_q); |
| 99 | |
| 100 | const float * src_spad_f32 = (const float *) src_spad; |
| 101 | float * dst_spad_f32 = (float *) dst_spad; |
| 102 | |
| 103 | for (uint32_t i1 = 0; i1 < ne1; i1++) { |
| 104 | // Compute row in VTCM |
| 105 | htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) (ib * ne1 + i1)); |
| 106 | hvx_diag_row_f32(src_spad_f32, dst_spad_f32, i1, ne0); |
| 107 | htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) (ib * ne1 + i1)); |
| 108 | |
| 109 | // Write completed row back to DDR |
| 110 | const dma_addr_t dst_row = dst_data + i3 * nb3 + i2 * nb2 + i1 * nb1; |
| 111 | dma_queue_push(dma_q, |
| 112 | dma_make_data(dst_row, dst_spad), |
| 113 | dst_row_size, dst_row_size_aligned, dst_row_size, 1); |
| 114 | dma_queue_flush(dma_q); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | FARF(HIGH, "diag-f32-dma %d/%d: %ux%ux%ux%u (%u:%u) -> %ux%ux%ux%u\n", |
| 119 | ith, nth, src0->ne[0], src0->ne[1], src0->ne[2], src0->ne[3], ib0, ib1, |
| 120 | dst->ne[0], dst->ne[1], dst->ne[2], dst->ne[3]); |
| 121 | } |
| 122 | |
| 123 | // --------------------------------------------------------------------------- |
| 124 | // Per thread worker: Direct HVX (no DMA) |
| 125 | // --------------------------------------------------------------------------- |
| 126 | |
| 127 | static void diag_thread_f32(unsigned int nth, unsigned int ith, void * data) { |
| 128 | htp_diag_preamble; |
| 129 | |
| 130 | const uint8_t * src_data = (const uint8_t *) src0->data; |
| 131 | uint8_t * dst_data = (uint8_t *) dst->data; |
| 132 | |
| 133 | const uint32_t ib0 = dctx->batch_start + dctx->batches_per_thread * ith; |
| 134 | const uint32_t ib1 = MIN(ib0 + dctx->batches_per_thread, dctx->batch_start + dctx->total_batches); |
| 135 | |
| 136 | struct htp_thread_trace * tr = &octx->ctx->trace[ith]; |
| 137 | htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) ib0); |
| 138 | |
| 139 | for (uint32_t ib = ib0; ib < ib1; ib++) { |
| 140 | const uint32_t i3 = fastdiv(ib, &dctx->kparams->div_ne02); |
| 141 | const uint32_t i2 = ib - i3 * ne02; |
| 142 | |
| 143 | const float * restrict src_batch = (const float *)(src_data + i3 * nb03 + i2 * nb02); |
| 144 | |
| 145 | for (uint32_t i1 = 0; i1 < ne1; i1++) { |
| 146 | float * restrict dst_row = (float *)(dst_data + i3 * nb3 + i2 * nb2 + i1 * nb1); |
| 147 | hvx_diag_row_f32(src_batch, dst_row, i1, ne0); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) ib0); |
| 152 | |
| 153 | FARF(HIGH, "diag-f32 %d/%d: %ux%ux%ux%u (%u:%u) -> %ux%ux%ux%u\n", |
| 154 | ith, nth, src0->ne[0], src0->ne[1], src0->ne[2], src0->ne[3], ib0, ib1, |
| 155 | dst->ne[0], dst->ne[1], dst->ne[2], dst->ne[3]); |
| 156 | } |
| 157 | |
| 158 | int op_diag_f32(struct htp_ops_context * octx) { |
| 159 | const struct htp_tensor * src0 = octx->src[0]; |
| 160 | const struct htp_tensor * dst = octx->dst; |
| 161 | const struct htp_diag_kernel_params * kparams = |
| 162 | (const struct htp_diag_kernel_params *) octx->kernel_params; |
| 163 | |
| 164 | const uint32_t total_batches = kparams->total_batches; |
| 165 | |
| 166 | uint32_t batch_start = 0; |
| 167 | uint32_t nbatches = total_batches; |
| 168 | |
| 169 | if (octx->ctx->mdev.count > 1) { |
| 170 | const bool can_split = (kparams->can_split != 0) && htp_tensor_mdev_data_aligned(dst); |
| 171 | const struct htp_tensor_mdev_range range = htp_tensor_mdev_partition(total_batches, can_split ? kparams->batches_per_chunk : 0, octx->ctx->mdev.idx, octx->ctx->mdev.count, &octx->ctx->mdev.count_div); |
| 172 | batch_start = range.start; |
| 173 | nbatches = range.count; |
| 174 | } |
| 175 | |
| 176 | if (nbatches == 0) { |
| 177 | return HTP_STATUS_OK; |
| 178 | } |
| 179 | |
| 180 | if (!htp_ops_context_set_n_threads(octx, MIN(kparams->n_threads, nbatches))) { |
| 181 | return HTP_STATUS_INVAL_PARAMS; |
| 182 | } |
| 183 | |
| 184 | const uint32_t n_threads = octx->n_threads; |
| 185 | |
| 186 | if (!kparams->use_dma && (htp_tensor_is_extended(src0) || htp_tensor_is_extended(dst))) { |
| 187 | return HTP_STATUS_NO_SUPPORT; |
| 188 | } |
| 189 | |
| 190 | const size_t src_batch_size = src0->ne[0] * sizeof(float); |
| 191 | const size_t dst_row_size = dst->ne[0] * sizeof(float); |
| 192 | |
| 193 | struct htp_diag_vtcm_layout layout = { 0 }; |
| 194 | if (kparams->use_dma) { |
| 195 | htp_diag_vtcm_layout_build(&layout, src_batch_size, dst_row_size, n_threads); |
| 196 | |
| 197 | octx->src0_spad.size_per_thread = layout.src0_bytes_per_thread; |
| 198 | octx->dst_spad.size_per_thread = layout.dst_bytes_per_thread; |
| 199 | octx->src0_spad.size = n_threads * layout.src0_bytes_per_thread; |
| 200 | octx->dst_spad.size = n_threads * layout.dst_bytes_per_thread; |
| 201 | octx->src0_spad.data = VTCM_LAYOUT_PTR(uint8_t, octx->ctx->vtcm_base, layout.off_src0); |
| 202 | octx->dst_spad.data = VTCM_LAYOUT_PTR(uint8_t, octx->ctx->vtcm_base, layout.off_dst); |
| 203 | octx->src0_spad.src = NULL; |
| 204 | octx->dst_spad.src = NULL; |
| 205 | } |
| 206 | |
| 207 | struct htp_diag_context dctx = { |
| 208 | .octx = octx, |
| 209 | .kparams = kparams, |
| 210 | .src_batch_size = src_batch_size, |
| 211 | .dst_row_size = dst_row_size, |
| 212 | .src_batch_size_aligned = layout.src_batch_size_aligned, |
| 213 | .dst_row_size_aligned = layout.dst_row_size_aligned, |
| 214 | .batches_per_thread = fastdiv(nbatches + n_threads - 1, &octx->n_threads_div), |
| 215 | .total_batches = nbatches, |
| 216 | .batch_start = batch_start, |
| 217 | }; |
| 218 | |
| 219 | if (kparams->use_dma) { |
| 220 | work_queue_run(octx->ctx->work_queue, diag_thread_f32_dma, &dctx, n_threads); |
| 221 | } else { |
| 222 | work_queue_run(octx->ctx->work_queue, diag_thread_f32, &dctx, n_threads); |
| 223 | } |
| 224 | |
| 225 | return HTP_STATUS_OK; |
| 226 | } |
| 227 | |
| 228 | int op_diag(struct htp_ops_context * octx) { |
| 229 | const struct htp_tensor * dst = octx->dst; |
| 230 | |
| 231 | int err = HTP_STATUS_OK; |
| 232 | |
| 233 | switch (dst->type) { |
| 234 | case HTP_TYPE_F32: |
| 235 | err = op_diag_f32(octx); |
| 236 | break; |
| 237 | default: |
| 238 | err = HTP_STATUS_NO_SUPPORT; |
| 239 | break; |
| 240 | } |
| 241 | |
| 242 | return err; |
| 243 | } |