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 / fill-ops.c

162 lines · 5696 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 <string.h>
7
8#include "hex-common.h"
9#include "hex-profile.h"
10
11#include "hvx-copy.h"
12#include "hvx-utils.h"
13
14#define GGML_COMMON_DECL_C
15#include "ggml-common.h"
16#include "htp-ctx.h"
17#include "htp-ops.h"
18#include "htp-tensor.h"
19#include "fill-ops.h"
20
21// ggml op_params layout for FILL:
22// op_params[0] (as float) - the scalar fill value
23
24#define fill_preamble \
25 const struct htp_tensor * dst = octx->dst; \
26 \
27 const uint32_t ne0 = dst->ne[0]; \
28 const uint32_t ne1 = dst->ne[1]; \
29 const uint32_t ne2 = dst->ne[2]; \
30 const uint32_t ne3 = dst->ne[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
36struct htp_fill_context {
37 struct htp_ops_context * octx;
38 const struct htp_fill_kernel_params * kparams;
39 uint32_t nrows_per_thread;
40 uint32_t total_rows; // ne1 * ne2 * ne3
41 uint32_t row_start;
42 HVX_Vector splat_vec;
43 uint32_t elem_size;
44};
45
46static void fill_thread_contiguous(unsigned int nth, unsigned int ith, void * data) {
47 const struct htp_fill_context * fctx = (const struct htp_fill_context *) data;
48 struct htp_ops_context * octx = fctx->octx;
49 fill_preamble;
50
51 // Parallelise over the flat row index spanning ne1*ne2*ne3
52 const uint32_t ir0 = fctx->row_start + fctx->nrows_per_thread * ith;
53 const uint32_t ir1 = MIN(ir0 + fctx->nrows_per_thread, fctx->row_start + fctx->total_rows);
54
55 if (ir0 >= ir1) {
56 return;
57 }
58
59 struct htp_thread_trace * tr = &octx->ctx->trace[ith];
60 htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) ir0);
61
62 const uint32_t elem_start = ir0 * ne0;
63 const uint32_t elem_end = ir1 * ne0;
64 uint8_t * dst_ptr = (uint8_t *) dst->data + elem_start * fctx->elem_size;
65 hvx_splat_u(dst_ptr, fctx->splat_vec, elem_end - elem_start, fctx->elem_size);
66
67 htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) ir1);
68 FARF(HIGH, "fill-contig %u/%u: rows %u:%u\n", ith, nth, ir0, ir1);
69}
70
71static void fill_thread_strided(unsigned int nth, unsigned int ith, void * data) {
72 const struct htp_fill_context * fctx = (const struct htp_fill_context *) data;
73 struct htp_ops_context * octx = fctx->octx;
74 const struct htp_fill_kernel_params * kparams = fctx->kparams;
75 fill_preamble;
76
77 // Parallelise over the flat row index spanning ne1*ne2*ne3
78 const uint32_t ir0 = fctx->row_start + fctx->nrows_per_thread * ith;
79 const uint32_t ir1 = MIN(ir0 + fctx->nrows_per_thread, fctx->row_start + fctx->total_rows);
80
81 if (ir0 >= ir1) {
82 return;
83 }
84
85 struct htp_thread_trace * tr = &octx->ctx->trace[ith];
86 htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) ir0);
87
88 for (uint32_t ir = ir0; ir < ir1; ++ir) {
89 const uint32_t i3 = fastdiv(ir, &kparams->div_ne1_ne2);
90 const uint32_t rem = ir - i3 * (ne1 * ne2);
91 const uint32_t i2 = fastdiv(rem, &kparams->div_ne1);
92 const uint32_t i1 = rem - i2 * ne1;
93 uint8_t * dst_ptr = (uint8_t *) dst->data + i1*nb1 + i2*nb2 + i3*nb3;
94 hvx_splat_u(dst_ptr, fctx->splat_vec, ne0, fctx->elem_size);
95 }
96
97 htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) ir1);
98 FARF(HIGH, "fill-strided %u/%u: rows %u:%u\n", ith, nth, ir0, ir1);
99}
100
101int op_fill(struct htp_ops_context * octx) {
102 const struct htp_tensor * dst = octx->dst;
103 const struct htp_fill_kernel_params * kparams =
104 (const struct htp_fill_kernel_params *) octx->kernel_params;
105
106 if (htp_tensor_is_extended(dst)) {
107 return HTP_STATUS_NO_SUPPORT;
108 }
109
110 uint32_t row_start = 0;
111 uint32_t nrows = kparams->total_rows;
112
113 if (octx->ctx->mdev.count > 1) {
114 const struct htp_tensor_mdev_range range = htp_tensor_mdev_partition(kparams->total_rows, htp_tensor_mdev_data_aligned(dst) ? kparams->rows_per_chunk : 0, octx->ctx->mdev.idx, octx->ctx->mdev.count, &octx->ctx->mdev.count_div);
115 row_start = range.start;
116 nrows = range.count;
117 }
118
119 if (nrows == 0) {
120 return HTP_STATUS_OK;
121 }
122
123 if (!htp_ops_context_set_n_threads(octx, MIN(kparams->n_threads, nrows))) {
124 return HTP_STATUS_INVAL_PARAMS;
125 }
126
127 const uint32_t n_threads = octx->n_threads;
128
129 FARF(HIGH, "fill: (%ux%ux%ux%u) type=%u ktype=%u\n",
130 dst->ne[0], dst->ne[1], dst->ne[2], dst->ne[3], dst->type, kparams->kernel_type);
131
132 float val_f32 = 0.f;
133 memcpy(&val_f32, &octx->op_params[0], sizeof(float));
134
135 struct htp_fill_context fctx = {
136 .octx = octx,
137 .kparams = kparams,
138 .nrows_per_thread = fastdiv(nrows + n_threads - 1, &octx->n_threads_div),
139 .total_rows = nrows,
140 .row_start = row_start,
141 .elem_size = kparams->elem_size,
142 };
143
144 switch (dst->type) {
145 case HTP_TYPE_F32:
146 fctx.splat_vec = hvx_vec_splat_f32(val_f32);
147 break;
148 case HTP_TYPE_F16:
149 fctx.splat_vec = hvx_vec_splat_f16((_Float16) val_f32);
150 break;
151 default:
152 return HTP_STATUS_NO_SUPPORT;
153 }
154
155 work_queue_func_t worker = (kparams->kernel_type == HTP_FILL_KERNEL_CONTIGUOUS)
156 ? fill_thread_contiguous
157 : fill_thread_strided;
158
159 work_queue_run(octx->ctx->work_queue, worker, &fctx, n_threads);
160
161 return HTP_STATUS_OK;
162}