interplane.Get Omega
Read in your language

ggml

[Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685)

Brought by drakestapleton

branch master Branches Tags History

ggml / src / ggml-hexagon / htp / sum-rows-ops.c

418 lines · 14518 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 <HAP_farf.h>
6#include <HAP_perf.h>
7
8#include <string.h>
9#include <math.h>
10
11#include "dma-queue.h"
12#include "hvx-utils.h"
13
14#define GGML_COMMON_DECL_C
15#include "ggml-common.h"
16#include "hex-common.h"
17#include "hex-profile.h"
18#include "htp-ctx.h"
19#include "htp-ops.h"
20#include "htp-tensor.h"
21#include "sum-rows-ops.h"
22
23#define sum_rows_preamble \
24 const struct htp_tensor *src0 = octx->src[0]; \
25 const struct htp_tensor *dst = octx->dst; \
26 \
27 const uint32_t ne00 = src0->ne[0]; \
28 const uint32_t ne01 = src0->ne[1]; \
29 const uint32_t ne02 = src0->ne[2]; \
30 const uint32_t ne03 = src0->ne[3]; \
31 \
32 const uint32_t nb00 = src0->nb[0]; \
33 const uint32_t nb01 = src0->nb[1]; \
34 const uint32_t nb02 = src0->nb[2]; \
35 const uint32_t nb03 = src0->nb[3]; \
36 \
37 const uint32_t ne0 = dst->ne[0]; \
38 const uint32_t ne1 = dst->ne[1]; \
39 const uint32_t ne2 = dst->ne[2]; \
40 const uint32_t ne3 = dst->ne[3]; \
41 \
42 const uint32_t nb0 = dst->nb[0]; \
43 const uint32_t nb1 = dst->nb[1]; \
44 const uint32_t nb2 = dst->nb[2]; \
45 const uint32_t nb3 = dst->nb[3]; \
46
47struct sum_rows_context {
48 struct htp_ops_context * octx;
49 const uint8_t * src_data;
50 uint8_t * dst_data;
51 uint32_t ne00;
52 size_t src_stride;
53 size_t dst_stride;
54 uint32_t rows_per_thread;
55 uint32_t total_rows;
56};
57
58static void sum_rows_thread_f32(unsigned int nth, unsigned int ith, void *data) {
59 const struct sum_rows_context * smctx = (const struct sum_rows_context *) data;
60
61 const uint32_t rows_per_thread = smctx->rows_per_thread;
62 const uint32_t total_rows = smctx->total_rows;
63
64 const uint32_t start_row = rows_per_thread * ith;
65 const uint32_t end_row = MIN(start_row + rows_per_thread, total_rows);
66
67 if (start_row >= end_row) {
68 return;
69 }
70
71 const size_t src_stride = smctx->src_stride;
72 const size_t dst_stride = smctx->dst_stride;
73 const uint32_t ne00 = smctx->ne00;
74
75 const float * restrict src_th = (const float *) (smctx->src_data + (start_row * src_stride));
76 float * restrict dst_th = (float *) (smctx->dst_data + (start_row * dst_stride));
77
78 // Calculate actual number of rows for this thread
79 const uint32_t n_rows = end_row - start_row;
80
81 struct htp_thread_trace * tr = &smctx->octx->ctx->trace[ith];
82 htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) start_row);
83
84 for (uint32_t ir = 0; ir < n_rows; ir++) {
85 const float * restrict src_local = (const float *) ((const uint8_t *) src_th + ir * src_stride);
86 float * restrict dst_local = (float *) ((uint8_t *) dst_th + ir * dst_stride);
87
88 if (ir + 1 < n_rows) {
89 hex_l2fetch((const uint8_t *) src_local + src_stride, src_stride, src_stride, 1);
90 }
91
92 *dst_local = hvx_reduce_sum_f32((const uint8_t *) src_local, ne00);
93 }
94
95 htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) start_row);
96}
97
98int op_sum_rows(struct htp_ops_context * octx) {
99 sum_rows_preamble;
100
101 if (octx->src[0]->type != HTP_TYPE_F32) {
102 return HTP_STATUS_NO_SUPPORT;
103 }
104
105 if (htp_tensor_is_extended(src0) || htp_tensor_is_extended(dst)) {
106 return HTP_STATUS_NO_SUPPORT;
107 }
108
109 const struct htp_sum_rows_kernel_params * kparams =
110 (const struct htp_sum_rows_kernel_params *) octx->kernel_params;
111
112 if (!htp_ops_context_set_n_threads(octx, kparams->n_threads)) {
113 return HTP_STATUS_INVAL_PARAMS;
114 }
115
116 const uint32_t src0_nrows = ne01 * ne02 * ne03;
117 const size_t dst_data_row_size = dst->ne[0] * sizeof(float);
118
119 uint32_t row_start = 0;
120 uint32_t nrows = src0_nrows;
121
122 if (octx->ctx->mdev.count > 1) {
123 uint32_t rows_per_chunk = 0;
124 htp_tensor_mdev_rows_per_chunk(dst, sizeof(float), (uint32_t) dst_data_row_size, &rows_per_chunk);
125 const struct htp_tensor_mdev_range range = htp_tensor_mdev_partition(src0_nrows, rows_per_chunk, octx->ctx->mdev.idx, octx->ctx->mdev.count, &octx->ctx->mdev.count_div);
126 row_start = range.start;
127 nrows = range.count;
128 }
129
130 if (nrows == 0) {
131 return HTP_STATUS_OK;
132 }
133
134 const uint32_t n_threads = octx->n_threads;
135 const uint32_t rows_per_thread = fastdiv(nrows + n_threads - 1, &octx->n_threads_div);
136
137 struct sum_rows_context smctx = {
138 .octx = octx,
139 .src_data = (const uint8_t *) src0->data + row_start * nb01,
140 .dst_data = (uint8_t *) dst->data + row_start * nb1,
141 .ne00 = ne00,
142 .src_stride = nb01,
143 .dst_stride = nb1,
144 .rows_per_thread = rows_per_thread,
145 .total_rows = nrows,
146 };
147
148 work_queue_run(octx->ctx->work_queue, sum_rows_thread_f32, &smctx, n_threads);
149
150 return HTP_STATUS_OK;
151}
152
153struct sum_context {
154 struct htp_ops_context * octx;
155 const float * src_data;
156 float partial_sums[HTP_MAX_NTHREADS];
157 uint32_t total_elems;
158 uint32_t elems_per_thread;
159};
160
161static void sum_thread_f32(unsigned int nth, unsigned int ith, void * data) {
162 struct sum_context * sctx = (struct sum_context *) data;
163 const uint32_t start = sctx->elems_per_thread * ith;
164 const uint32_t end = MIN(start + sctx->elems_per_thread, sctx->total_elems);
165
166 if (start >= end) {
167 sctx->partial_sums[ith] = 0.0f;
168 return;
169 }
170
171 const uint32_t n = end - start;
172 const float * src = sctx->src_data + start;
173
174 struct htp_thread_trace * tr = &sctx->octx->ctx->trace[ith];
175 htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) start);
176
177 hex_l2fetch_block((const void *) src, n * sizeof(float));
178
179 sctx->partial_sums[ith] = hvx_reduce_sum_f32((const uint8_t *) src, n);
180
181 htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) start);
182}
183
184int op_sum(struct htp_ops_context * octx) {
185 const struct htp_tensor * src0 = octx->src[0];
186 const struct htp_tensor * dst = octx->dst;
187
188 if (src0->type != HTP_TYPE_F32) {
189 return HTP_STATUS_NO_SUPPORT;
190 }
191
192 if (htp_tensor_is_extended(src0) || htp_tensor_is_extended(dst)) {
193 return HTP_STATUS_NO_SUPPORT;
194 }
195
196 const struct htp_sum_rows_kernel_params * kparams =
197 (const struct htp_sum_rows_kernel_params *) octx->kernel_params;
198
199 if (!htp_ops_context_set_n_threads(octx, kparams->n_threads)) {
200 return HTP_STATUS_INVAL_PARAMS;
201 }
202
203 if (octx->ctx->mdev.count > 1 && octx->ctx->mdev.idx > 0) {
204 return HTP_STATUS_OK;
205 }
206
207 const uint32_t total_elems = (uint32_t) (src0->ne[0] * src0->ne[1] * src0->ne[2] * src0->ne[3]);
208 if (total_elems == 0) {
209 ((float *) dst->data)[0] = 0.0f;
210 return HTP_STATUS_OK;
211 }
212
213 const uint32_t n_threads = octx->n_threads;
214 const uint32_t elems_per_thread = kparams->elems_per_thread;
215
216 struct sum_context sctx = {
217 .octx = octx,
218 .src_data = (const float *) src0->data,
219 .total_elems = total_elems,
220 .elems_per_thread = elems_per_thread,
221 };
222
223 work_queue_run(octx->ctx->work_queue, sum_thread_f32, &sctx, n_threads);
224
225 float sum = 0.0f;
226 for (uint32_t i = 0; i < n_threads; i++) {
227 sum += sctx.partial_sums[i];
228 }
229 ((float *) dst->data)[0] = sum;
230
231 return HTP_STATUS_OK;
232}
233
234static inline void argmax_slice_f32(
235 const float * restrict src,
236 uint32_t n,
237 uint32_t offset,
238 float * out_val,
239 int32_t * out_idx
240) {
241 hvx_argmax_f32(src, n, offset, out_val, out_idx);
242}
243
244struct argmax_context {
245 struct htp_ops_context * octx;
246 const float * src_data;
247 int32_t * dst_data;
248 uint32_t ne00;
249 uint32_t src_stride;
250 uint32_t dst_stride;
251 uint32_t row_start;
252 uint32_t nrows;
253 uint32_t rows_per_thread;
254 uint32_t elems_per_thread;
255
256 float partial_max[HTP_MAX_NTHREADS];
257 int32_t partial_idx[HTP_MAX_NTHREADS];
258};
259
260static void argmax_thread_single_row(unsigned int nth, unsigned int ith, void * data) {
261 struct argmax_context * actx = (struct argmax_context *) data;
262 const uint32_t start = actx->elems_per_thread * ith;
263 const uint32_t end = MIN(start + actx->elems_per_thread, actx->ne00);
264
265 if (start >= end) {
266 actx->partial_max[ith] = -INFINITY;
267 actx->partial_idx[ith] = 0;
268 return;
269 }
270
271 const uint32_t n = end - start;
272 const float * src = actx->src_data + start;
273
274 struct htp_thread_trace * tr = &actx->octx->ctx->trace[ith];
275 htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) start);
276
277 hex_l2fetch_block((const void *) src, n * sizeof(float));
278
279 float max_val;
280 int32_t max_idx;
281 argmax_slice_f32(src, n, start, &max_val, &max_idx);
282
283 actx->partial_max[ith] = max_val;
284 actx->partial_idx[ith] = max_idx;
285
286 htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) start);
287}
288
289static void argmax_thread_multi_row(unsigned int nth, unsigned int ith, void * data) {
290 struct argmax_context * actx = (struct argmax_context *) data;
291 const uint32_t r0 = actx->row_start + actx->rows_per_thread * ith;
292 const uint32_t r1 = MIN(r0 + actx->rows_per_thread, actx->row_start + actx->nrows);
293
294 if (r0 >= r1) {
295 return;
296 }
297
298 struct htp_thread_trace * tr = &actx->octx->ctx->trace[ith];
299 htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) r0);
300
301 for (uint32_t r = r0; r < r1; r++) {
302 const float * src_row = (const float *) ((const uint8_t *) actx->src_data + r * actx->src_stride);
303 int32_t * dst_val = (int32_t *) ((uint8_t *) actx->dst_data + r * actx->dst_stride);
304
305 hex_l2fetch_block((const void *) src_row, actx->ne00 * sizeof(float));
306
307 float max_val;
308 int32_t max_idx;
309 argmax_slice_f32(src_row, actx->ne00, 0, &max_val, &max_idx);
310
311 *dst_val = max_idx;
312 }
313
314 htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) r0);
315}
316
317int op_argmax(struct htp_ops_context * octx) {
318 const struct htp_tensor * src0 = octx->src[0];
319 const struct htp_tensor * dst = octx->dst;
320
321 if (src0->type != HTP_TYPE_F32 || dst->type != HTP_TYPE_I32) {
322 return HTP_STATUS_NO_SUPPORT;
323 }
324
325 if (htp_tensor_is_extended(src0) || htp_tensor_is_extended(dst)) {
326 return HTP_STATUS_NO_SUPPORT;
327 }
328
329 const struct htp_sum_rows_kernel_params * kparams =
330 (const struct htp_sum_rows_kernel_params *) octx->kernel_params;
331
332 if (!htp_ops_context_set_n_threads(octx, kparams->n_threads)) {
333 return HTP_STATUS_INVAL_PARAMS;
334 }
335
336 const uint32_t ne00 = src0->ne[0];
337 const uint32_t src0_nrows = src0->ne[1] * src0->ne[2] * src0->ne[3];
338
339 if (ne00 == 0 || src0_nrows == 0) {
340 return HTP_STATUS_OK;
341 }
342
343 if (src0_nrows == 1) {
344 if (octx->ctx->mdev.count > 1 && octx->ctx->mdev.idx > 0) {
345 return HTP_STATUS_OK;
346 }
347
348 if (ne00 == 1) {
349 ((int32_t *) dst->data)[0] = 0;
350 return HTP_STATUS_OK;
351 }
352
353 const uint32_t n_threads = octx->n_threads;
354 const uint32_t elems_per_thread = kparams->elems_per_thread;
355
356 struct argmax_context actx = {
357 .octx = octx,
358 .src_data = (const float *) src0->data,
359 .dst_data = (int32_t *) dst->data,
360 .ne00 = ne00,
361 .src_stride = src0->nb[1] > 0 ? (uint32_t) src0->nb[1] : (uint32_t) (ne00 * sizeof(float)),
362 .dst_stride = dst->nb[0] > 0 ? (uint32_t) dst->nb[0] : (uint32_t) sizeof(int32_t),
363 .row_start = 0,
364 .nrows = 1,
365 .rows_per_thread = 1,
366 .elems_per_thread = elems_per_thread,
367 };
368
369 work_queue_run(octx->ctx->work_queue, argmax_thread_single_row, &actx, n_threads);
370
371 float best_val = actx.partial_max[0];
372 int32_t best_idx = actx.partial_idx[0];
373 for (uint32_t i = 1; i < n_threads; i++) {
374 if (actx.partial_max[i] > best_val) {
375 best_val = actx.partial_max[i];
376 best_idx = actx.partial_idx[i];
377 }
378 }
379 ((int32_t *) dst->data)[0] = best_idx;
380 return HTP_STATUS_OK;
381 }
382
383 uint32_t row_start = 0;
384 uint32_t nrows = src0_nrows;
385
386 if (octx->ctx->mdev.count > 1) {
387 const bool can_split = htp_tensor_mdev_data_aligned(dst) &&
388 htp_tensor_is_contiguous(dst, sizeof(int32_t));
389 const uint32_t elems_per_chunk = can_split ? 32 : 0;
390 const struct htp_tensor_mdev_range range = htp_tensor_mdev_partition(
391 src0_nrows, elems_per_chunk, octx->ctx->mdev.idx, octx->ctx->mdev.count, &octx->ctx->mdev.count_div);
392 row_start = range.start;
393 nrows = range.count;
394 }
395
396 if (nrows == 0) {
397 return HTP_STATUS_OK;
398 }
399
400 const uint32_t n_threads = octx->n_threads;
401 const uint32_t rows_per_thread = fastdiv(nrows + n_threads - 1, &octx->n_threads_div);
402
403 struct argmax_context actx = {
404 .octx = octx,
405 .src_data = (const float *) src0->data,
406 .dst_data = (int32_t *) dst->data,
407 .ne00 = ne00,
408 .src_stride = src0->nb[1] > 0 ? (uint32_t) src0->nb[1] : (uint32_t) (ne00 * sizeof(float)),
409 .dst_stride = dst->nb[0] > 0 ? (uint32_t) dst->nb[0] : (uint32_t) sizeof(int32_t),
410 .row_start = row_start,
411 .nrows = nrows,
412 .rows_per_thread = rows_per_thread,
413 .elems_per_thread = 0,
414 };
415
416 work_queue_run(octx->ctx->work_queue, argmax_thread_multi_row, &actx, n_threads);
417 return HTP_STATUS_OK;
418}