|
| 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 "htp-ctx.h" |
| 9 | +#include "htp-ops.h" |
| 10 | +#include "hvx-types.h" |
| 11 | +#include "hex-utils.h" |
| 12 | +#include "hvx-copy.h" |
| 13 | +#include "hex-dma.h" |
| 14 | + |
| 15 | +#define htp_diag_tensors_preamble \ |
| 16 | + const struct htp_tensor * restrict src0 = octx->src[0]; \ |
| 17 | + const struct htp_tensor * restrict dst = octx->dst; \ |
| 18 | + \ |
| 19 | + const uint32_t ne02 = src0->ne[2]; \ |
| 20 | + \ |
| 21 | + const uint32_t ne0 = dst->ne[0]; \ |
| 22 | + const uint32_t ne1 = dst->ne[1]; \ |
| 23 | + \ |
| 24 | + const uint32_t nb02 = src0->nb[2]; \ |
| 25 | + const uint32_t nb03 = src0->nb[3]; \ |
| 26 | + \ |
| 27 | + const uint32_t nb1 = dst->nb[1]; \ |
| 28 | + const uint32_t nb2 = dst->nb[2]; \ |
| 29 | + const uint32_t nb3 = dst->nb[3]; |
| 30 | + |
| 31 | +struct htp_diag_context { |
| 32 | + struct htp_ops_context * octx; |
| 33 | + size_t src_batch_size; |
| 34 | + size_t dst_row_size; |
| 35 | + size_t src_batch_size_aligned; |
| 36 | + size_t dst_row_size_aligned; |
| 37 | + uint32_t batches_per_thread; |
| 38 | + uint32_t total_batches; |
| 39 | +}; |
| 40 | + |
| 41 | +#define htp_diag_preamble \ |
| 42 | + struct htp_diag_context * dctx = (struct htp_diag_context *) data; \ |
| 43 | + struct htp_ops_context * octx = dctx->octx; \ |
| 44 | + htp_diag_tensors_preamble; |
| 45 | + |
| 46 | +static inline void hvx_diag_row_f32(const float * restrict src, float * restrict dst, |
| 47 | + uint32_t row_idx, uint32_t n) { |
| 48 | + hvx_splat_f32_a((uint8_t *) dst, 0.0f, n); |
| 49 | + dst[row_idx] = src[row_idx]; |
| 50 | +} |
| 51 | + |
| 52 | +// --------------------------------------------------------------------------- |
| 53 | +// Per thread worker: DMA src fetch, compute in VTCM, DMA dst writeback |
| 54 | +// --------------------------------------------------------------------------- |
| 55 | + |
| 56 | +static void diag_thread_f32_dma(unsigned int nth, unsigned int ith, void * data) { |
| 57 | + htp_diag_preamble; |
| 58 | + dma_queue * dma_queue = octx->ctx->dma[ith]; |
| 59 | + |
| 60 | + uint64_t t1, t2; |
| 61 | + t1 = HAP_perf_get_qtimer_count(); |
| 62 | + |
| 63 | + const uint32_t ib0 = dctx->batches_per_thread * ith; |
| 64 | + const uint32_t ib1 = MIN(ib0 + dctx->batches_per_thread, dctx->total_batches); |
| 65 | + |
| 66 | + if (ib0 >= ib1) { |
| 67 | + return; |
| 68 | + } |
| 69 | + |
| 70 | + const size_t src_batch_size = dctx->src_batch_size; |
| 71 | + const size_t dst_row_size = dctx->dst_row_size; |
| 72 | + const size_t src_batch_size_aligned = dctx->src_batch_size_aligned; |
| 73 | + const size_t dst_row_size_aligned = dctx->dst_row_size_aligned; |
| 74 | + |
| 75 | + const uint8_t * src_data = (const uint8_t *) src0->data; |
| 76 | + uint8_t * dst_data = (uint8_t *) dst->data; |
| 77 | + |
| 78 | + // 1 src buffer + 1 dst row buffer per thread in VTCM |
| 79 | + uint8_t * src_spad = octx->src0_spad.data + (ith * src_batch_size_aligned); |
| 80 | + uint8_t * dst_spad = octx->dst_spad.data + (ith * dst_row_size_aligned); |
| 81 | + |
| 82 | + for (uint32_t ib = ib0; ib < ib1; ib++) { |
| 83 | + const uint32_t i3 = ib / ne02; |
| 84 | + const uint32_t i2 = ib % ne02; |
| 85 | + |
| 86 | + const uint8_t * src_batch = src_data + i3 * nb03 + i2 * nb02; |
| 87 | + |
| 88 | + // Fetch source vector into VTCM |
| 89 | + dma_queue_push_ddr_to_vtcm(dma_queue, |
| 90 | + dma_make_ptr(src_spad, src_batch), |
| 91 | + src_batch_size_aligned, src_batch_size, 1); |
| 92 | + dma_queue_flush(dma_queue); |
| 93 | + |
| 94 | + const float * src_spad_f32 = (const float *) src_spad; |
| 95 | + float * dst_spad_f32 = (float *) dst_spad; |
| 96 | + |
| 97 | + for (uint32_t i1 = 0; i1 < ne1; i1++) { |
| 98 | + // Compute row in VTCM |
| 99 | + hvx_diag_row_f32(src_spad_f32, dst_spad_f32, i1, ne0); |
| 100 | + |
| 101 | + // Write completed row back to DDR |
| 102 | + uint8_t * dst_row = dst_data + i3 * nb3 + i2 * nb2 + i1 * nb1; |
| 103 | + dma_queue_push_vtcm_to_ddr(dma_queue, |
| 104 | + dma_make_ptr(dst_row, dst_spad), |
| 105 | + dst_row_size, dst_row_size_aligned, 1); |
| 106 | + dma_queue_flush(dma_queue); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + t2 = HAP_perf_get_qtimer_count(); |
| 111 | + |
| 112 | + FARF(HIGH, "diag-f32-dma %d/%d: %ux%ux%ux%u (%u:%u) -> %ux%ux%ux%u usec %u\n", |
| 113 | + ith, nth, src0->ne[0], src0->ne[1], src0->ne[2], src0->ne[3], ib0, ib1, |
| 114 | + dst->ne[0], dst->ne[1], dst->ne[2], dst->ne[3], |
| 115 | + (unsigned) HAP_perf_qtimer_count_to_us(t2 - t1)); |
| 116 | +} |
| 117 | + |
| 118 | +// --------------------------------------------------------------------------- |
| 119 | +// Per thread worker: Direct HVX (no DMA) |
| 120 | +// --------------------------------------------------------------------------- |
| 121 | + |
| 122 | +static void diag_thread_f32(unsigned int nth, unsigned int ith, void * data) { |
| 123 | + htp_diag_preamble; |
| 124 | + |
| 125 | + uint64_t t1, t2; |
| 126 | + t1 = HAP_perf_get_qtimer_count(); |
| 127 | + |
| 128 | + const uint8_t * src_data = (const uint8_t *) src0->data; |
| 129 | + uint8_t * dst_data = (uint8_t *) dst->data; |
| 130 | + |
| 131 | + const uint32_t ib0 = dctx->batches_per_thread * ith; |
| 132 | + const uint32_t ib1 = MIN(ib0 + dctx->batches_per_thread, dctx->total_batches); |
| 133 | + |
| 134 | + for (uint32_t ib = ib0; ib < ib1; ib++) { |
| 135 | + const uint32_t i3 = ib / ne02; |
| 136 | + const uint32_t i2 = ib % ne02; |
| 137 | + |
| 138 | + const float * restrict src_batch = (const float *)(src_data + i3 * nb03 + i2 * nb02); |
| 139 | + |
| 140 | + for (uint32_t i1 = 0; i1 < ne1; i1++) { |
| 141 | + float * restrict dst_row = (float *)(dst_data + i3 * nb3 + i2 * nb2 + i1 * nb1); |
| 142 | + hvx_diag_row_f32(src_batch, dst_row, i1, ne0); |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + t2 = HAP_perf_get_qtimer_count(); |
| 147 | + |
| 148 | + FARF(HIGH, "diag-f32 %d/%d: %ux%ux%ux%u (%u:%u) -> %ux%ux%ux%u usec %u\n", |
| 149 | + ith, nth, src0->ne[0], src0->ne[1], src0->ne[2], src0->ne[3], ib0, ib1, |
| 150 | + dst->ne[0], dst->ne[1], dst->ne[2], dst->ne[3], |
| 151 | + (unsigned) HAP_perf_qtimer_count_to_us(t2 - t1)); |
| 152 | +} |
| 153 | + |
| 154 | +int op_diag_f32(struct htp_ops_context * octx) { |
| 155 | + const struct htp_tensor * src0 = octx->src[0]; |
| 156 | + const struct htp_tensor * dst = octx->dst; |
| 157 | + |
| 158 | + if (octx->flags & HTP_OPFLAGS_SKIP_COMPUTE) { |
| 159 | + return HTP_STATUS_OK; |
| 160 | + } |
| 161 | + |
| 162 | + const uint32_t total_batches = src0->ne[2] * src0->ne[3]; |
| 163 | + const uint32_t n_threads = MIN(octx->n_threads, total_batches); |
| 164 | + |
| 165 | + const size_t src_batch_size = src0->ne[0] * sizeof(float); |
| 166 | + const size_t dst_row_size = dst->ne[0] * sizeof(float); |
| 167 | + const size_t src_batch_size_aligned = hex_round_up(src_batch_size, VLEN); |
| 168 | + const size_t dst_row_size_aligned = hex_round_up(dst_row_size, VLEN); |
| 169 | + |
| 170 | + // 1 src buffer + 1 dst row buffer per thread |
| 171 | + const size_t spad_per_thread = src_batch_size_aligned + dst_row_size_aligned; |
| 172 | + |
| 173 | + octx->src0_spad.size_per_thread = src_batch_size_aligned; |
| 174 | + octx->dst_spad.size_per_thread = dst_row_size_aligned; |
| 175 | + |
| 176 | + octx->src0_spad.size = n_threads * octx->src0_spad.size_per_thread; |
| 177 | + octx->dst_spad.size = n_threads * octx->dst_spad.size_per_thread; |
| 178 | + |
| 179 | + octx->src0_spad.data = octx->ctx->vtcm_base; octx->src0_spad.src = NULL; |
| 180 | + octx->dst_spad.data = octx->src0_spad.data + octx->src0_spad.size; octx->dst_spad.src = NULL; |
| 181 | + |
| 182 | + struct htp_diag_context dctx = { |
| 183 | + .octx = octx, |
| 184 | + .src_batch_size = src_batch_size, |
| 185 | + .dst_row_size = dst_row_size, |
| 186 | + .src_batch_size_aligned = src_batch_size_aligned, |
| 187 | + .dst_row_size_aligned = dst_row_size_aligned, |
| 188 | + .batches_per_thread = (total_batches + n_threads - 1) / n_threads, |
| 189 | + .total_batches = total_batches, |
| 190 | + }; |
| 191 | + |
| 192 | + if (octx->ctx->vtcm_size < spad_per_thread * n_threads) { |
| 193 | + worker_pool_run_func(octx->ctx->worker_pool, diag_thread_f32, &dctx, n_threads); |
| 194 | + } else { |
| 195 | + worker_pool_run_func(octx->ctx->worker_pool, diag_thread_f32_dma, &dctx, n_threads); |
| 196 | + } |
| 197 | + |
| 198 | + return HTP_STATUS_OK; |
| 199 | +} |
| 200 | + |
| 201 | +int op_diag(struct htp_ops_context * octx) { |
| 202 | + const struct htp_tensor * dst = octx->dst; |
| 203 | + |
| 204 | + int err = HTP_STATUS_OK; |
| 205 | + |
| 206 | + switch (dst->type) { |
| 207 | + case HTP_TYPE_F32: |
| 208 | + err = op_diag_f32(octx); |
| 209 | + break; |
| 210 | + default: |
| 211 | + err = HTP_STATUS_NO_SUPPORT; |
| 212 | + break; |
| 213 | + } |
| 214 | + |
| 215 | + return err; |
| 216 | +} |
0 commit comments