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

290 lines · 11864 bytes Raw Blame History
1#pragma clang diagnostic ignored "-Wunused-but-set-variable"
2
3#include <HAP_farf.h>
4#include <string.h>
5
6#include "hex-common.h"
7#include "hex-profile.h"
8
9#define GGML_COMMON_DECL_C
10#include "ggml-common.h"
11#include "htp-ctx.h"
12#include "htp-ops.h"
13#include "htp-tensor.h"
14#include "hvx-types.h"
15#include "hvx-utils.h"
16#include "solve-tri-ops.h"
17
18struct htp_solve_tri_context {
19 struct htp_ops_context * octx;
20 const struct htp_solve_tri_kernel_params * kparams;
21 uint32_t jobs_per_thread;
22 uint32_t total_jobs;
23 uint32_t job_start;
24 uint32_t k_chunks;
25 uint32_t col_block;
26};
27
28static inline void solve_tri_row_scalar(const float * A_row,
29 const float * B_row,
30 float * X,
31 uint32_t row,
32 uint32_t k,
33 uint32_t col0,
34 uint32_t coln,
35 float inv_diag) {
36 for (uint32_t col = col0; col < col0 + coln; ++col) {
37 float sum = 0.0f;
38 for (uint32_t t = 0; t < row; ++t) {
39 sum += A_row[t] * X[t * k + col];
40 }
41 X[row * k + col] = (B_row[col] - sum) * inv_diag;
42 }
43}
44
45static inline HVX_Vector hvx_load_partial_f32(const float * src, uint32_t n) {
46 HVX_Vector v = *((const HVX_UVector *) src);
47 HVX_VectorPred mask = Q6_Q_vsetq2_R(n * sizeof(float));
48 return Q6_V_vmux_QVV(mask, v, Q6_V_vzero());
49}
50
51static inline void solve_tri_row_hvx(const float * A_row,
52 const float * B_row,
53 float * X,
54 uint32_t row,
55 uint32_t k,
56 uint32_t col0,
57 uint32_t coln,
58 float inv_diag) {
59 const bool full = (coln == VLEN_FP32);
60
61 HVX_Vector sum_v = Q6_V_vzero();
62 for (uint32_t t = 0; t < row; ++t) {
63 const float a = A_row[t];
64 const float * x_row_col = X + t * k + col0;
65
66 HVX_Vector x_v = full ? *((const HVX_UVector *) x_row_col) : hvx_load_partial_f32(x_row_col, coln);
67 HVX_Vector a_v = hvx_vec_splat_f32(a);
68 sum_v = hvx_vec_add_f32_f32(sum_v, hvx_vec_mul_f32_f32(x_v, a_v));
69 }
70
71 const float * b_row_col = B_row + col0;
72 float * x_out_col = X + row * k + col0;
73
74 HVX_Vector b_v = full ? *((const HVX_UVector *) b_row_col) : hvx_load_partial_f32(b_row_col, coln);
75 HVX_Vector inv_diag_v = hvx_vec_splat_f32(inv_diag);
76
77 HVX_Vector out_v = hvx_vec_mul_f32_f32(hvx_vec_sub_f32_f32(b_v, sum_v), inv_diag_v);
78 hvx_vec_store_u((void *) x_out_col, coln * sizeof(float), out_v);
79}
80
81// Batch-level thread: each job is one full batch.
82static void solve_tri_batch_thread_f32(unsigned int nth, unsigned int ith, void * data) {
83 struct htp_solve_tri_context * sctx = (struct htp_solve_tri_context *) data;
84 struct htp_ops_context * octx = sctx->octx;
85 const struct htp_solve_tri_kernel_params * kparams = sctx->kparams;
86
87 const struct htp_tensor * src0 = octx->src[0]; // A
88 const struct htp_tensor * src1 = octx->src[1]; // B
89 const struct htp_tensor * dst = octx->dst; // X
90
91 const uint32_t n = src0->ne[0];
92 const uint32_t k = src1->ne[0];
93
94 const uint32_t ne02 = src0->ne[2];
95
96 const uint32_t col_block = VLEN_FP32;
97 const uint32_t k_full = k & ~(col_block - 1);
98
99 const uint32_t start_batch = sctx->job_start + sctx->jobs_per_thread * ith;
100 const uint32_t end_batch = MIN(start_batch + sctx->jobs_per_thread, sctx->job_start + sctx->total_jobs);
101
102 struct htp_thread_trace * tr = &octx->ctx->trace[ith];
103 htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) start_batch);
104
105 for (uint32_t batch = start_batch; batch < end_batch; ++batch) {
106 const uint32_t i03 = fastdiv(batch, &kparams->div_ne02);
107 const uint32_t i02 = batch - i03 * ne02;
108
109 const float * A_batch =
110 (const float *) ((const uint8_t *) (uintptr_t) src0->data + i02 * src0->nb[2] + i03 * src0->nb[3]);
111 const float * B_batch =
112 (const float *) ((const uint8_t *) (uintptr_t) src1->data + i02 * src1->nb[2] + i03 * src1->nb[3]);
113 float * X_batch = (float *) ((uint8_t *) (uintptr_t) dst->data + i02 * dst->nb[2] + i03 * dst->nb[3]);
114
115 for (uint32_t row = 0; row < n; ++row) {
116 const float diag = A_batch[row * n + row];
117 const float inv_diag = 1.0f / diag;
118 const float * A_row = A_batch + row * n;
119 const float * B_row = B_batch + row * k;
120
121 uint32_t col0 = 0;
122 for (; col0 < k_full; col0 += col_block) {
123 solve_tri_row_hvx(A_row, B_row, X_batch, row, k, col0, col_block, inv_diag);
124 }
125
126 if (col0 < k) {
127 const uint32_t coln = k - col0;
128 if (coln >= 8) {
129 solve_tri_row_hvx(A_row, B_row, X_batch, row, k, col0, coln, inv_diag);
130 } else {
131 solve_tri_row_scalar(A_row, B_row, X_batch, row, k, col0, coln, inv_diag);
132 }
133 }
134 }
135 }
136
137 htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) end_batch);
138
139 FARF(HIGH, "solve-tri-batch %d/%d: A=(%ux%u) B=(%ux%u) batch %u:%u\n",
140 ith, nth, n, n, k, n, start_batch, end_batch);
141}
142
143// Chunk-level thread: each job is one (batch, col_chunk) pair.
144static void solve_tri_chunk_thread_f32(unsigned int nth, unsigned int ith, void * data) {
145 struct htp_solve_tri_context * sctx = (struct htp_solve_tri_context *) data;
146 struct htp_ops_context * octx = sctx->octx;
147 const struct htp_solve_tri_kernel_params * kparams = sctx->kparams;
148
149 const struct htp_tensor * src0 = octx->src[0]; // A
150 const struct htp_tensor * src1 = octx->src[1]; // B
151 const struct htp_tensor * dst = octx->dst; // X
152
153 const uint32_t n = src0->ne[0];
154 const uint32_t k = src1->ne[0];
155
156 const uint32_t ne02 = src0->ne[2];
157
158 const uint32_t start_job = sctx->job_start + sctx->jobs_per_thread * ith;
159 const uint32_t end_job = MIN(start_job + sctx->jobs_per_thread, sctx->job_start + sctx->total_jobs);
160
161 struct htp_thread_trace * tr = &octx->ctx->trace[ith];
162 htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) start_job);
163
164 for (uint32_t job = start_job; job < end_job; ++job) {
165 const uint32_t batch = fastdiv(job, &kparams->div_k_chunks);
166 const uint32_t chunk = job - batch * sctx->k_chunks;
167
168 const uint32_t i03 = fastdiv(batch, &kparams->div_ne02);
169 const uint32_t i02 = batch - i03 * ne02;
170
171 const float * A_batch =
172 (const float *) ((const uint8_t *) (uintptr_t) src0->data + i02 * src0->nb[2] + i03 * src0->nb[3]);
173 const float * B_batch =
174 (const float *) ((const uint8_t *) (uintptr_t) src1->data + i02 * src1->nb[2] + i03 * src1->nb[3]);
175 float * X_batch = (float *) ((uint8_t *) (uintptr_t) dst->data + i02 * dst->nb[2] + i03 * dst->nb[3]);
176
177 const uint32_t col0 = chunk * sctx->col_block;
178 const uint32_t coln = MIN(sctx->col_block, k - col0);
179
180 for (uint32_t row = 0; row < n; ++row) {
181 const float diag = A_batch[row * n + row];
182 const float inv_diag = 1.0f / diag;
183
184 const float * A_row = A_batch + row * n;
185 const float * B_row = B_batch + row * k;
186
187 if (coln >= 8) {
188 solve_tri_row_hvx(A_row, B_row, X_batch, row, k, col0, coln, inv_diag);
189 } else {
190 solve_tri_row_scalar(A_row, B_row, X_batch, row, k, col0, coln, inv_diag);
191 }
192 }
193 }
194
195 htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) end_job);
196
197 FARF(HIGH, "solve-tri-chunk %d/%d: A=(%ux%u) B=(%ux%u) jobs %u:%u\n",
198 ith, nth, n, n, k, n, start_job, end_job);
199}
200
201int op_solve_tri(struct htp_ops_context * octx) {
202 const struct htp_tensor * src0 = octx->src[0]; // A
203 const struct htp_tensor * src1 = octx->src[1]; // B
204 const struct htp_tensor * dst = octx->dst; // X
205 const struct htp_solve_tri_kernel_params * kparams =
206 (const struct htp_solve_tri_kernel_params *) octx->kernel_params;
207
208 if (htp_tensor_is_extended(src0) || htp_tensor_is_extended(src1) || htp_tensor_is_extended(dst)) {
209 return HTP_STATUS_NO_SUPPORT;
210 }
211
212 const uint32_t col_block = kparams->col_block;
213 const uint32_t k_chunks = kparams->k_chunks;
214 const uint32_t total_batches = kparams->total_batches;
215 const bool batched = (kparams->batched != 0);
216
217 FARF(HIGH, "solve-tri: (%ux%ux%ux%u) x (%ux%ux%ux%u) -> (%ux%ux%ux%u) : batched %d\n",
218 src0->ne[0], src0->ne[1], src0->ne[2], src0->ne[3],
219 src1->ne[0], src1->ne[1], src1->ne[2], src1->ne[3],
220 dst->ne[0], dst->ne[1], dst->ne[2], dst->ne[3], batched);
221
222 if (batched) {
223 uint32_t job_start = 0;
224 uint32_t njobs = total_batches;
225
226 if (octx->ctx->mdev.count > 1) {
227 const struct htp_tensor_mdev_range range = htp_tensor_mdev_partition(total_batches, htp_tensor_mdev_data_aligned(dst) ? kparams->batches_per_chunk : 0, octx->ctx->mdev.idx, octx->ctx->mdev.count, &octx->ctx->mdev.count_div);
228 job_start = range.start;
229 njobs = range.count;
230 }
231
232 if (njobs == 0) {
233 return HTP_STATUS_OK;
234 }
235
236 if (!htp_ops_context_set_n_threads(octx, MIN(kparams->n_threads, njobs))) {
237 return HTP_STATUS_INVAL_PARAMS;
238 }
239
240 const uint32_t n_threads = octx->n_threads;
241
242 struct htp_solve_tri_context sctx = {
243 .octx = octx,
244 .kparams = kparams,
245 .jobs_per_thread = fastdiv(njobs + n_threads - 1, &octx->n_threads_div),
246 .total_jobs = njobs,
247 .job_start = job_start,
248 .k_chunks = k_chunks,
249 .col_block = col_block,
250 };
251
252 work_queue_run(octx->ctx->work_queue, solve_tri_batch_thread_f32, &sctx, n_threads);
253 } else {
254 const uint32_t total_jobs = kparams->total_jobs;
255
256 uint32_t job_start = 0;
257 uint32_t njobs = total_jobs;
258
259 if (octx->ctx->mdev.count > 1) {
260 const bool can_split = htp_tensor_mdev_data_aligned(dst) && ((dst->nb[1] & (HTP_TENSOR_MDEV_LINE_SIZE - 1)) == 0);
261 const struct htp_tensor_mdev_range range = htp_tensor_mdev_partition(total_jobs, can_split ? 1 : 0, octx->ctx->mdev.idx, octx->ctx->mdev.count, &octx->ctx->mdev.count_div);
262 job_start = range.start;
263 njobs = range.count;
264 }
265
266 if (njobs == 0) {
267 return HTP_STATUS_OK;
268 }
269
270 if (!htp_ops_context_set_n_threads(octx, MIN(kparams->n_threads, njobs))) {
271 return HTP_STATUS_INVAL_PARAMS;
272 }
273
274 const uint32_t n_threads = octx->n_threads;
275
276 struct htp_solve_tri_context sctx = {
277 .octx = octx,
278 .kparams = kparams,
279 .jobs_per_thread = fastdiv(njobs + n_threads - 1, &octx->n_threads_div),
280 .total_jobs = njobs,
281 .job_start = job_start,
282 .k_chunks = k_chunks,
283 .col_block = col_block,
284 };
285
286 work_queue_run(octx->ctx->work_queue, solve_tri_chunk_thread_f32, &sctx, n_threads);
287 }
288
289 return HTP_STATUS_OK;
290}