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

958 lines · 37387 bytes Raw Blame History
1#pragma clang diagnostic ignored "-Wunused-variable"
2
3#include <float.h>
4#include <math.h>
5#include <HAP_farf.h>
6
7#include "hex-common.h"
8#include "dma-queue.h"
9#include "hex-profile.h"
10#include "htp-ctx.h"
11#include "htp-ops.h"
12#include "htp-tensor.h"
13#include "hvx-types.h"
14#include "hvx-base.h"
15#include "hvx-utils.h"
16#include "upscale-ops.h"
17
18static inline int32_t clamp_i32(int32_t v, int32_t lo, int32_t hi) {
19 return v < lo ? lo : (v > hi ? hi : v);
20}
21
22static inline float clamp_f32(float v, float lo, float hi) {
23 return v < lo ? lo : (v > hi ? hi : v);
24}
25
26static inline float bicubic_weight1(float x) {
27 const float a = -0.75f;
28 return ((a + 2.0f) * x - (a + 3.0f)) * x * x + 1.0f;
29}
30
31static inline float bicubic_weight2(float x) {
32 const float a = -0.75f;
33 return ((a * x - 5.0f * a) * x + 8.0f * a) * x - 4.0f * a;
34}
35
36static inline void bicubic_weights(float x, float w[4]) {
37 w[0] = bicubic_weight2(x + 1.0f);
38 w[1] = bicubic_weight1(x);
39 w[2] = bicubic_weight1(1.0f - x);
40 w[3] = bicubic_weight2(2.0f - x);
41}
42
43static void upscale_init_luts_nearest(
44 uint32_t * lut_x,
45 uint32_t * lut_y,
46 const struct htp_tensor * src0,
47 const struct htp_tensor * dst,
48 const struct htp_upscale_kernel_params * p
49) {
50 const uint32_t src_x = src0->ne[0];
51 const uint32_t src_y = src0->ne[1];
52 const uint32_t dst_x = dst->ne[0];
53 const uint32_t dst_y = dst->ne[1];
54 const uint32_t dst_x_aligned = (dst_x + VLEN_FP32 - 1) & ~(VLEN_FP32 - 1);
55
56 for (uint32_t ox = 0; ox < dst_x_aligned; ++ox) {
57 if (ox < dst_x) {
58 uint32_t x0 = (uint32_t) ((float) ox * p->inv_sf0);
59 lut_x[ox] = MIN(x0, src_x - 1) * sizeof(float);
60 } else {
61 lut_x[ox] = 0;
62 }
63 }
64 for (uint32_t oy = 0; oy < dst_y; ++oy) {
65 uint32_t y0 = (uint32_t) ((float) oy * p->inv_sf1);
66 lut_y[oy] = MIN(y0, src_y - 1);
67 }
68}
69
70static void upscale_init_luts_bilinear(
71 struct upscale_bilinear_x_chunk * lut_x,
72 struct upscale_bilinear_y * lut_y,
73 const struct htp_tensor * src0,
74 const struct htp_tensor * dst,
75 const struct htp_upscale_kernel_params * p
76) {
77 const uint32_t src_x = src0->ne[0];
78 const uint32_t src_y = src0->ne[1];
79 const uint32_t dst_x = dst->ne[0];
80 const uint32_t dst_y = dst->ne[1];
81 const uint32_t n_chunks = (dst_x + VLEN_FP32 - 1) / VLEN_FP32;
82
83 for (uint32_t c = 0; c < n_chunks; ++c) {
84 struct upscale_bilinear_x_chunk * chunk = &lut_x[c];
85 const uint32_t ox_start = c * VLEN_FP32;
86 for (uint32_t l = 0; l < VLEN_FP32; ++l) {
87 const uint32_t ox = ox_start + l;
88 if (ox < dst_x) {
89 float xf = ((float) ox + p->pixel_offset) * p->inv_sf0 - p->pixel_offset;
90 int32_t x0 = (int32_t) floorf(xf);
91 int32_t x1 = x0 + 1;
92 x0 = clamp_i32(x0, 0, (int32_t) src_x - 1);
93 x1 = clamp_i32(x1, 0, (int32_t) src_x - 1);
94 float dx = clamp_f32(xf - (float) x0, 0.0f, 1.0f);
95
96 chunk->off0[l] = x0 * (int32_t) sizeof(float);
97 chunk->off1[l] = x1 * (int32_t) sizeof(float);
98 chunk->w0[l] = 1.0f - dx;
99 chunk->dx[l] = dx;
100 } else {
101 chunk->off0[l] = 0;
102 chunk->off1[l] = 0;
103 chunk->w0[l] = 0.0f;
104 chunk->dx[l] = 0.0f;
105 }
106 }
107 }
108 for (uint32_t oy = 0; oy < dst_y; ++oy) {
109 float yf = ((float) oy + p->pixel_offset) * p->inv_sf1 - p->pixel_offset;
110 int32_t y0 = (int32_t) floorf(yf);
111 int32_t y1 = y0 + 1;
112 y0 = clamp_i32(y0, 0, (int32_t) src_y - 1);
113 y1 = clamp_i32(y1, 0, (int32_t) src_y - 1);
114 float dy = clamp_f32(yf - (float) y0, 0.0f, 1.0f);
115
116 lut_y[oy].y0 = (uint32_t) y0;
117 lut_y[oy].y1 = (uint32_t) y1;
118 lut_y[oy].wy1 = dy;
119 lut_y[oy].wy0 = 1.0f - dy;
120 }
121}
122
123static void upscale_init_luts_bicubic(
124 struct upscale_bicubic_x * lut_x,
125 struct upscale_bicubic_y * lut_y,
126 const struct htp_tensor * src0,
127 const struct htp_tensor * dst,
128 const struct htp_upscale_kernel_params * p
129) {
130 const uint32_t src_x = src0->ne[0];
131 const uint32_t src_y = src0->ne[1];
132 const uint32_t dst_x = dst->ne[0];
133 const uint32_t dst_y = dst->ne[1];
134
135 for (uint32_t ox = 0; ox < dst_x; ++ox) {
136 float xf = ((float) ox + p->pixel_offset) * p->inv_sf0 - p->pixel_offset;
137 int32_t x0 = (int32_t) floorf(xf);
138 float dx = xf - (float) x0;
139 bicubic_weights(dx, lut_x[ox].w);
140
141 lut_x[ox].x[0] = clamp_i32(x0 - 1, 0, (int32_t) src_x - 1);
142 lut_x[ox].x[1] = clamp_i32(x0 + 0, 0, (int32_t) src_x - 1);
143 lut_x[ox].x[2] = clamp_i32(x0 + 1, 0, (int32_t) src_x - 1);
144 lut_x[ox].x[3] = clamp_i32(x0 + 2, 0, (int32_t) src_x - 1);
145 }
146 for (uint32_t oy = 0; oy < dst_y; ++oy) {
147 float yf = ((float) oy + p->pixel_offset) * p->inv_sf1 - p->pixel_offset;
148 int32_t y0 = (int32_t) floorf(yf);
149 float dy = yf - (float) y0;
150 bicubic_weights(dy, lut_y[oy].w);
151
152 lut_y[oy].y[0] = clamp_i32(y0 - 1, 0, (int32_t) src_y - 1);
153 lut_y[oy].y[1] = clamp_i32(y0 + 0, 0, (int32_t) src_y - 1);
154 lut_y[oy].y[2] = clamp_i32(y0 + 1, 0, (int32_t) src_y - 1);
155 lut_y[oy].y[3] = clamp_i32(y0 + 2, 0, (int32_t) src_y - 1);
156 }
157}
158
159static void upscale_init_luts_antialias(
160 struct upscale_antialias_x * lut_x,
161 struct upscale_antialias_y * lut_y,
162 const struct htp_tensor * src0,
163 const struct htp_tensor * dst,
164 const struct htp_upscale_kernel_params * p
165) {
166 const uint32_t src_x = src0->ne[0];
167 const uint32_t src_y = src0->ne[1];
168 const uint32_t dst_x = dst->ne[0];
169 const uint32_t dst_y = dst->ne[1];
170
171 const float filter_radius0 = p->filter_radius0;
172 const float filter_radius1 = p->filter_radius1;
173
174 for (uint32_t ox = 0; ox < dst_x; ++ox) {
175 const float xf = ((float) ox + p->pixel_offset) * p->inv_sf0;
176 int32_t x_min = (int32_t) MAX(xf - filter_radius0 + p->pixel_offset, 0.0f);
177 int32_t x_max = (int32_t) MIN(xf + filter_radius0 + p->pixel_offset, (float) src_x);
178 lut_x[ox].x_min = clamp_i32(x_min, 0, (int32_t) src_x);
179 lut_x[ox].x_max = clamp_i32(x_max, 0, (int32_t) src_x);
180 lut_x[ox].xf = xf;
181 }
182 for (uint32_t oy = 0; oy < dst_y; ++oy) {
183 const float yf = ((float) oy + p->pixel_offset) * p->inv_sf1;
184 int32_t y_min = (int32_t) MAX(yf - filter_radius1 + p->pixel_offset, 0.0f);
185 int32_t y_max = (int32_t) MIN(yf + filter_radius1 + p->pixel_offset, (float) src_y);
186 lut_y[oy].y_min = clamp_i32(y_min, 0, (int32_t) src_y);
187 lut_y[oy].y_max = clamp_i32(y_max, 0, (int32_t) src_y);
188 lut_y[oy].yf = yf;
189 }
190}
191
192static inline void get_src_y_window(
193 const struct htp_tensor * src0,
194 const struct htp_tensor * dst,
195 const struct htp_upscale_kernel_params * p,
196 const void * lut_y,
197 uint32_t dst_y_start,
198 uint32_t dst_y_count,
199 uint32_t * out_src_y_start,
200 uint32_t * out_src_y_count
201) {
202 const uint32_t src_y = src0->ne[1];
203 const uint32_t dst_y = dst->ne[1];
204
205 if (dst_y_count == dst_y) {
206 *out_src_y_start = 0;
207 *out_src_y_count = src_y;
208 return;
209 }
210
211 const uint32_t last_oy = dst_y_start + dst_y_count - 1;
212
213 if (p->mode == HTP_UPSCALE_MODE_NEAREST) {
214 const uint32_t * ly = (const uint32_t *) lut_y;
215 const uint32_t y0 = ly[dst_y_start];
216 const uint32_t y1 = ly[last_oy];
217 *out_src_y_start = y0;
218 *out_src_y_count = y1 - y0 + 1;
219 } else if (p->mode == HTP_UPSCALE_MODE_BICUBIC) {
220 const struct upscale_bicubic_y * ly = (const struct upscale_bicubic_y *) lut_y;
221 const int32_t y0 = ly[dst_y_start].y[0];
222 const int32_t y1 = ly[last_oy].y[3];
223 *out_src_y_start = (uint32_t) y0;
224 *out_src_y_count = (uint32_t) (y1 - y0 + 1);
225 } else if (p->antialias) {
226 const struct upscale_antialias_y * ly = (const struct upscale_antialias_y *) lut_y;
227 const int32_t y0 = ly[dst_y_start].y_min;
228 const int32_t y1 = ly[last_oy].y_max;
229 *out_src_y_start = (uint32_t) y0;
230 *out_src_y_count = (uint32_t) (y1 - y0);
231 } else {
232 const struct upscale_bilinear_y * ly = (const struct upscale_bilinear_y *) lut_y;
233 const uint32_t y0 = ly[dst_y_start].y0;
234 const uint32_t y1 = ly[last_oy].y1;
235 *out_src_y_start = y0;
236 *out_src_y_count = y1 - y0 + 1;
237 }
238}
239
240static inline void resolve_dst_slice(
241 const struct htp_tensor * dst,
242 const struct htp_upscale_kernel_params * p,
243 uint32_t chunk_idx,
244 dma_addr_t * out_slice_dst_off,
245 uint32_t * out_dst_ys,
246 uint32_t * out_dst_yc
247) {
248 const uint32_t s_idx = fastdiv(chunk_idx, &p->div_chunks_per_slice);
249 const uint32_t c_idx = chunk_idx - s_idx * p->chunks_per_slice;
250 const uint32_t d_ys = c_idx * p->dst_nrows_per_chunk;
251 const uint32_t d_yc = MIN(p->dst_nrows_per_chunk, dst->ne[1] - d_ys);
252 const uint32_t i3 = fastdiv(s_idx, &p->div_dst_z);
253 const uint32_t i2 = s_idx - i3 * dst->ne[2];
254
255 *out_slice_dst_off = (dma_addr_t) i2 * dst->nb[2] + (dma_addr_t) i3 * dst->nb[3];
256 *out_dst_ys = d_ys;
257 *out_dst_yc = d_yc;
258}
259
260static inline void resolve_src_slice(
261 const struct htp_tensor * src0,
262 const struct htp_tensor * dst,
263 const struct htp_upscale_kernel_params * p,
264 const void * lut_y,
265 uint32_t chunk_idx,
266 dma_addr_t * out_slice_src_off,
267 uint32_t * out_src_ys,
268 uint32_t * out_src_yc
269) {
270 const uint32_t s_idx = fastdiv(chunk_idx, &p->div_chunks_per_slice);
271 const uint32_t c_idx = chunk_idx - s_idx * p->chunks_per_slice;
272 const uint32_t d_ys = c_idx * p->dst_nrows_per_chunk;
273 const uint32_t d_yc = MIN(p->dst_nrows_per_chunk, dst->ne[1] - d_ys);
274 uint32_t s_ys, s_yc;
275 get_src_y_window(src0, dst, p, lut_y, d_ys, d_yc, &s_ys, &s_yc);
276
277 const uint32_t i3 = fastdiv(s_idx, &p->div_dst_z);
278 const uint32_t i2 = s_idx - i3 * dst->ne[2];
279 const uint32_t i03 = (uint32_t) ((float) i3 * p->inv_sf3);
280 const uint32_t i02 = (uint32_t) ((float) i2 * p->inv_sf2);
281
282 *out_slice_src_off = (dma_addr_t) i02 * src0->nb[2] + (dma_addr_t) i03 * src0->nb[3];
283 *out_src_ys = s_ys;
284 *out_src_yc = s_yc;
285}
286
287struct upscale_context;
288
289typedef void (*upscale_chunk_fn_t)(
290 const struct upscale_context * ctx,
291 const float * srcb,
292 float * dstb,
293 float * R_buf,
294 float * gather_tmp,
295 uint32_t dst_y_start,
296 uint32_t dst_y_count,
297 uint32_t src_y_start
298);
299
300struct upscale_context {
301 struct htp_ops_context * octx;
302 const struct htp_upscale_kernel_params * kparams;
303 struct htp_upscale_vtcm_layout layout;
304 upscale_chunk_fn_t compute_chunk;
305 const void * lut_x;
306 const void * lut_y;
307 uint32_t chunk_start;
308 uint32_t chunk_count;
309 uint32_t chunks_per_thread;
310};
311
312static void compute_nearest_chunk(
313 const struct upscale_context * ctx,
314 const float * srcb,
315 float * dstb,
316 float * R_buf,
317 float * gather_tmp,
318 uint32_t dst_y_start,
319 uint32_t dst_y_count,
320 uint32_t src_y_start
321) {
322 const uint32_t src_x = ctx->octx->src[0]->ne[0];
323 const uint32_t dst_x = ctx->octx->dst->ne[0];
324 const uint32_t * lut_x = (const uint32_t *) ctx->lut_x;
325 const uint32_t * lut_y = (const uint32_t *) ctx->lut_y;
326 const uint32_t region_size = hex_round_up(src_x * sizeof(float), 128);
327
328 for (uint32_t oy = dst_y_start; oy < dst_y_start + dst_y_count; ++oy) {
329 const uint32_t y0 = lut_y[oy];
330 const float * r0 = srcb + (y0 - src_y_start) * src_x;
331 float * dst_row = dstb + (oy - dst_y_start) * dst_x;
332
333 for (uint32_t ix = 0; ix < src_x; ix += VLEN_FP32) {
334 *(HVX_Vector *) (R_buf + ix) = hvx_vmemu(r0 + ix);
335 }
336
337 for (uint32_t ox = 0; ox < dst_x; ox += VLEN_FP32) {
338 const uint32_t rem = dst_x - ox;
339 const uint32_t nbytes = (rem < VLEN_FP32) ? (rem * sizeof(float)) : VLEN;
340 const HVX_Vector voff = hvx_vmem(lut_x + ox);
341 Q6_vgather_ARMVw((HVX_Vector *) gather_tmp, (size_t) R_buf, region_size, voff);
342 hvx_gather_sync(gather_tmp);
343 hvx_vec_store_u(dst_row + ox, nbytes, hvx_vmem(gather_tmp));
344 }
345 }
346}
347
348static void compute_nearest_2x_chunk(
349 const struct upscale_context * ctx,
350 const float * srcb,
351 float * dstb,
352 float * R_buf,
353 float * gather_tmp,
354 uint32_t dst_y_start,
355 uint32_t dst_y_count,
356 uint32_t src_y_start
357) {
358 (void) R_buf;
359 (void) gather_tmp;
360 const uint32_t src_x = ctx->octx->src[0]->ne[0];
361 const uint32_t dst_x = ctx->octx->dst->ne[0];
362
363 for (uint32_t oy = dst_y_start; oy < dst_y_start + dst_y_count; ++oy) {
364 const uint32_t sy = oy >> 1;
365 const float * r0 = srcb + (sy - src_y_start) * src_x;
366 float * dst_row = dstb + (oy - dst_y_start) * dst_x;
367
368 for (uint32_t ix = 0; ix < src_x; ix += VLEN_FP32) {
369 const HVX_Vector vin = hvx_vmemu(r0 + ix);
370
371 const HVX_VectorPair p = Q6_W_vshuff_VVR(vin, vin, -4);
372 const HVX_Vector v_lo = Q6_V_lo_W(p);
373 const HVX_Vector v_hi = Q6_V_hi_W(p);
374
375 const uint32_t ox0 = 2 * ix;
376 const uint32_t rem_out0 = (dst_x > ox0) ? (dst_x - ox0) : 0;
377 const uint32_t nbytes_out0 = (rem_out0 < VLEN_FP32) ? (rem_out0 * sizeof(float)) : VLEN;
378 hvx_vec_store_u(dst_row + ox0, nbytes_out0, v_lo);
379
380 const uint32_t ox1 = ox0 + VLEN_FP32;
381 if (dst_x > ox1) {
382 const uint32_t rem_out1 = dst_x - ox1;
383 const uint32_t nbytes_out1 = (rem_out1 < VLEN_FP32) ? (rem_out1 * sizeof(float)) : VLEN;
384 hvx_vec_store_u(dst_row + ox1, nbytes_out1, v_hi);
385 }
386 }
387 }
388}
389
390static void compute_nearest_4x_chunk(
391 const struct upscale_context * ctx,
392 const float * srcb,
393 float * dstb,
394 float * R_buf,
395 float * gather_tmp,
396 uint32_t dst_y_start,
397 uint32_t dst_y_count,
398 uint32_t src_y_start
399) {
400 (void) R_buf;
401 (void) gather_tmp;
402 const uint32_t src_x = ctx->octx->src[0]->ne[0];
403 const uint32_t dst_x = ctx->octx->dst->ne[0];
404
405 for (uint32_t oy = dst_y_start; oy < dst_y_start + dst_y_count; ++oy) {
406 const uint32_t sy = oy >> 2;
407 const float * r0 = srcb + (sy - src_y_start) * src_x;
408 float * dst_row = dstb + (oy - dst_y_start) * dst_x;
409
410 for (uint32_t ix = 0; ix < src_x; ix += VLEN_FP32) {
411 const HVX_Vector vin = hvx_vmemu(r0 + ix);
412
413 const HVX_VectorPair p = Q6_W_vshuff_VVR(vin, vin, -4);
414 const HVX_Vector v_lo = Q6_V_lo_W(p);
415 const HVX_Vector v_hi = Q6_V_hi_W(p);
416
417 const HVX_VectorPair p0 = Q6_W_vshuff_VVR(v_lo, v_lo, -4);
418 const HVX_VectorPair p1 = Q6_W_vshuff_VVR(v_hi, v_hi, -4);
419
420 const HVX_Vector out0 = Q6_V_lo_W(p0);
421 const HVX_Vector out1 = Q6_V_hi_W(p0);
422 const HVX_Vector out2 = Q6_V_lo_W(p1);
423 const HVX_Vector out3 = Q6_V_hi_W(p1);
424
425 const uint32_t ox = 4 * ix;
426 if (dst_x > ox) {
427 const uint32_t rem = dst_x - ox;
428 hvx_vec_store_u(dst_row + ox, (rem < VLEN_FP32) ? (rem * sizeof(float)) : VLEN, out0);
429 }
430 if (dst_x > ox + VLEN_FP32) {
431 const uint32_t rem = dst_x - (ox + VLEN_FP32);
432 hvx_vec_store_u(dst_row + ox + VLEN_FP32, (rem < VLEN_FP32) ? (rem * sizeof(float)) : VLEN, out1);
433 }
434 if (dst_x > ox + 2 * VLEN_FP32) {
435 const uint32_t rem = dst_x - (ox + 2 * VLEN_FP32);
436 hvx_vec_store_u(dst_row + ox + 2 * VLEN_FP32, (rem < VLEN_FP32) ? (rem * sizeof(float)) : VLEN, out2);
437 }
438 if (dst_x > ox + 3 * VLEN_FP32) {
439 const uint32_t rem = dst_x - (ox + 3 * VLEN_FP32);
440 hvx_vec_store_u(dst_row + ox + 3 * VLEN_FP32, (rem < VLEN_FP32) ? (rem * sizeof(float)) : VLEN, out3);
441 }
442 }
443 }
444}
445
446static inline void compute_bilinear_chunk_32(
447 float * restrict dst_chunk,
448 const float * restrict R_buf,
449 float * restrict tmp_a,
450 float * restrict tmp_b,
451 uint32_t region_size,
452 const struct upscale_bilinear_x_chunk * restrict chunk,
453 uint32_t nbytes
454) {
455 const HVX_Vector voff0 = hvx_vmem(chunk->off0);
456 const HVX_Vector voff1 = hvx_vmem(chunk->off1);
457 const HVX_Vector vw0 = hvx_vmem(chunk->w0);
458 const HVX_Vector vdx = hvx_vmem(chunk->dx);
459
460 Q6_vgather_ARMVw((HVX_Vector *) tmp_a, (size_t) R_buf, region_size, voff0);
461 Q6_vgather_ARMVw((HVX_Vector *) tmp_b, (size_t) R_buf, region_size, voff1);
462 hvx_gather_sync(tmp_b);
463
464 const HVX_Vector va = hvx_vmem(tmp_a);
465 const HVX_Vector vb = hvx_vmem(tmp_b);
466
467 const HVX_Vector val = hvx_vec_add_f32_f32(
468 hvx_vec_mul_f32_f32(vw0, va),
469 hvx_vec_mul_f32_f32(vdx, vb)
470 );
471
472 hvx_vec_store_u(dst_chunk, nbytes, val);
473}
474
475static void compute_bilinear_chunk(
476 const struct upscale_context * ctx,
477 const float * srcb,
478 float * dstb,
479 float * R_buf,
480 float * gather_tmp,
481 uint32_t dst_y_start,
482 uint32_t dst_y_count,
483 uint32_t src_y_start
484) {
485 const uint32_t src_x = ctx->octx->src[0]->ne[0];
486 const uint32_t dst_x = ctx->octx->dst->ne[0];
487 const struct upscale_bilinear_x_chunk * lut_x = (const struct upscale_bilinear_x_chunk *) ctx->lut_x;
488 const struct upscale_bilinear_y * lut_y = (const struct upscale_bilinear_y *) ctx->lut_y;
489 const uint32_t n_chunks = (dst_x + VLEN_FP32 - 1) / VLEN_FP32;
490 const uint32_t region_size = hex_round_up(src_x * sizeof(float), 128);
491 float * tmp_a = gather_tmp;
492 float * tmp_b = gather_tmp + VLEN_FP32;
493
494 for (uint32_t oy = dst_y_start; oy < dst_y_start + dst_y_count; ++oy) {
495 const struct upscale_bilinear_y * ey = &lut_y[oy];
496 const float * r0 = srcb + (ey->y0 - src_y_start) * src_x;
497 const float * r1 = srcb + (ey->y1 - src_y_start) * src_x;
498 float * dst_row = dstb + (oy - dst_y_start) * dst_x;
499
500 if (ey->y0 == ey->y1) {
501 for (uint32_t ix = 0; ix < src_x; ix += VLEN_FP32) {
502 *(HVX_Vector *) (R_buf + ix) = hvx_vmemu(r0 + ix);
503 }
504 } else {
505 const HVX_Vector vwy0 = hvx_vec_splat_f32(ey->wy0);
506 const HVX_Vector vwy1 = hvx_vec_splat_f32(ey->wy1);
507 for (uint32_t ix = 0; ix < src_x; ix += VLEN_FP32) {
508 const HVX_Vector vr0 = hvx_vmemu(r0 + ix);
509 const HVX_Vector vr1 = hvx_vmemu(r1 + ix);
510 *(HVX_Vector *) (R_buf + ix) = hvx_vec_add_f32_f32(
511 hvx_vec_mul_f32_f32(vwy0, vr0),
512 hvx_vec_mul_f32_f32(vwy1, vr1)
513 );
514 }
515 }
516
517 for (uint32_t c = 0; c < n_chunks; ++c) {
518 const uint32_t ox = c * VLEN_FP32;
519 const uint32_t rem = dst_x - ox;
520 const uint32_t nbytes = (rem < VLEN_FP32) ? (rem * sizeof(float)) : VLEN;
521 compute_bilinear_chunk_32(dst_row + ox, R_buf, tmp_a, tmp_b, region_size, &lut_x[c], nbytes);
522 }
523 }
524}
525
526static void compute_bilinear_2x_chunk(
527 const struct upscale_context * ctx,
528 const float * srcb,
529 float * dstb,
530 float * R_buf,
531 float * gather_tmp,
532 uint32_t dst_y_start,
533 uint32_t dst_y_count,
534 uint32_t src_y_start
535) {
536 (void) R_buf;
537 (void) gather_tmp;
538 const uint32_t src_x = ctx->octx->src[0]->ne[0];
539 const uint32_t dst_x = ctx->octx->dst->ne[0];
540 const struct upscale_bilinear_y * lut_y = (const struct upscale_bilinear_y *) ctx->lut_y;
541 const HVX_Vector c_075 = hvx_vec_splat_f32(0.75f);
542 const HVX_Vector c_025 = hvx_vec_splat_f32(0.25f);
543
544 for (uint32_t oy = dst_y_start; oy < dst_y_start + dst_y_count; ++oy) {
545 const struct upscale_bilinear_y * ey = &lut_y[oy];
546 const float * r0 = srcb + (ey->y0 - src_y_start) * src_x;
547 const float * r1 = srcb + (ey->y1 - src_y_start) * src_x;
548 float * dst_row = dstb + (oy - dst_y_start) * dst_x;
549
550 const bool same_row = (ey->y0 == ey->y1);
551 const HVX_Vector vwy0 = hvx_vec_splat_f32(ey->wy0);
552 const HVX_Vector vwy1 = hvx_vec_splat_f32(ey->wy1);
553
554 HVX_Vector v_curr = hvx_vmem(r0);
555 if (!same_row) {
556 const HVX_Vector v1 = hvx_vmem(r1);
557 v_curr = hvx_vec_add_f32_f32(hvx_vec_mul_f32_f32(vwy0, v_curr), hvx_vec_mul_f32_f32(vwy1, v1));
558 }
559
560 HVX_Vector v_prev = Q6_V_vror_VR(v_curr, 4);
561
562 for (uint32_t ix = 0; ix < src_x; ix += VLEN_FP32) {
563 HVX_Vector v_next_chunk;
564 const uint32_t next_ix = ix + VLEN_FP32;
565 if (next_ix < src_x) {
566 v_next_chunk = hvx_vmem(r0 + next_ix);
567 if (!same_row) {
568 const HVX_Vector v1 = hvx_vmem(r1 + next_ix);
569 v_next_chunk = hvx_vec_add_f32_f32(hvx_vec_mul_f32_f32(vwy0, v_next_chunk), hvx_vec_mul_f32_f32(vwy1, v1));
570 }
571 } else {
572 v_next_chunk = Q6_V_vror_VR(v_curr, 124);
573 }
574
575 const HVX_Vector v_shl = Q6_V_valign_VVR(v_curr, v_prev, 124);
576 const HVX_Vector v_shr = Q6_V_valign_VVR(v_next_chunk, v_curr, 4);
577
578 const HVX_Vector ve = hvx_vec_add_f32_f32(hvx_vec_mul_f32_f32(c_075, v_curr), hvx_vec_mul_f32_f32(c_025, v_shl));
579 const HVX_Vector vo = hvx_vec_add_f32_f32(hvx_vec_mul_f32_f32(c_075, v_curr), hvx_vec_mul_f32_f32(c_025, v_shr));
580
581 const HVX_VectorPair p = Q6_W_vshuff_VVR(vo, ve, -4);
582 const HVX_Vector out0 = Q6_V_lo_W(p);
583 const HVX_Vector out1 = Q6_V_hi_W(p);
584
585 const uint32_t ox0 = 2 * ix;
586 hvx_vmem(dst_row + ox0) = out0;
587 hvx_vmem(dst_row + ox0 + VLEN_FP32) = out1;
588
589 v_prev = v_curr;
590 v_curr = v_next_chunk;
591 }
592 }
593}
594
595static void compute_bilinear_antialias_chunk(
596 const struct upscale_context * ctx,
597 const float * srcb,
598 float * dstb,
599 float * R_buf,
600 float * gather_tmp,
601 uint32_t dst_y_start,
602 uint32_t dst_y_count,
603 uint32_t src_y_start
604) {
605 (void) R_buf;
606 (void) gather_tmp;
607 const uint32_t src_x = ctx->octx->src[0]->ne[0];
608 const uint32_t dst_x = ctx->octx->dst->ne[0];
609 const struct htp_upscale_kernel_params * p = ctx->kparams;
610 const struct upscale_antialias_x * lut_x = (const struct upscale_antialias_x *) ctx->lut_x;
611 const struct upscale_antialias_y * lut_y = (const struct upscale_antialias_y *) ctx->lut_y;
612
613 const float invscale1 = p->invscale1;
614 const float invscale0 = p->invscale0;
615
616 for (uint32_t oy = dst_y_start; oy < dst_y_start + dst_y_count; ++oy) {
617 const struct upscale_antialias_y * ey = &lut_y[oy];
618 const float yf = ey->yf;
619 const int32_t y_min = ey->y_min;
620 const int32_t y_max = ey->y_max;
621
622 float * dst_row = dstb + (oy - dst_y_start) * dst_x;
623
624 for (uint32_t ox = 0; ox < dst_x; ox += VLEN_FP32) {
625 const uint32_t rem = dst_x - ox;
626 const uint32_t nbytes = (rem < VLEN_FP32) ? (rem * sizeof(float)) : VLEN;
627 float tmp[VLEN_FP32] __attribute__((aligned(128)));
628 const uint32_t cnt = rem < VLEN_FP32 ? rem : VLEN_FP32;
629
630 for (uint32_t l = 0; l < cnt; ++l) {
631 const uint32_t cur_ox = ox + l;
632 const struct upscale_antialias_x * ex = &lut_x[cur_ox];
633 const float xf = ex->xf;
634 const int32_t x_min = ex->x_min;
635 const int32_t x_max = ex->x_max;
636
637 float val = 0.0f;
638 float total_weight = 0.0f;
639
640 for (int32_t sy = y_min; sy < y_max; ++sy) {
641 const float diff_y = fabsf((float) sy - yf + p->pixel_offset) * invscale1;
642 const float weight_y = MAX(1.0f - diff_y, 0.0f);
643 if (weight_y <= 0.0f) {
644 continue;
645 }
646 const float * r = srcb + (uint32_t) (sy - (int32_t) src_y_start) * src_x;
647
648 for (int32_t sx = x_min; sx < x_max; ++sx) {
649 const float diff_x = fabsf((float) sx - xf + p->pixel_offset) * invscale0;
650 const float weight_x = MAX(1.0f - diff_x, 0.0f);
651 const float weight = weight_x * weight_y;
652 if (weight <= 0.0f) {
653 continue;
654 }
655 val += r[sx] * weight;
656 total_weight += weight;
657 }
658 }
659
660 tmp[l] = (total_weight > 0.0f) ? (val / total_weight) : 0.0f;
661 }
662
663 hvx_vec_store_u(dst_row + ox, nbytes, *(const HVX_Vector *) tmp);
664 }
665 }
666}
667
668static void compute_bicubic_chunk(
669 const struct upscale_context * ctx,
670 const float * srcb,
671 float * dstb,
672 float * R_buf,
673 float * gather_tmp,
674 uint32_t dst_y_start,
675 uint32_t dst_y_count,
676 uint32_t src_y_start
677) {
678 (void) R_buf;
679 (void) gather_tmp;
680 const uint32_t src_x = ctx->octx->src[0]->ne[0];
681 const uint32_t dst_x = ctx->octx->dst->ne[0];
682 const struct upscale_bicubic_x * lut_x = (const struct upscale_bicubic_x *) ctx->lut_x;
683 const struct upscale_bicubic_y * lut_y = (const struct upscale_bicubic_y *) ctx->lut_y;
684
685 for (uint32_t oy = dst_y_start; oy < dst_y_start + dst_y_count; ++oy) {
686 const struct upscale_bicubic_y * ey = &lut_y[oy];
687 const float * r0 = srcb + (uint32_t) (ey->y[0] - (int32_t) src_y_start) * src_x;
688 const float * r1 = srcb + (uint32_t) (ey->y[1] - (int32_t) src_y_start) * src_x;
689 const float * r2 = srcb + (uint32_t) (ey->y[2] - (int32_t) src_y_start) * src_x;
690 const float * r3 = srcb + (uint32_t) (ey->y[3] - (int32_t) src_y_start) * src_x;
691
692 float * dst_row = dstb + (oy - dst_y_start) * dst_x;
693
694 const HVX_Vector vwy0 = hvx_vec_splat_f32(ey->w[0]);
695 const HVX_Vector vwy1 = hvx_vec_splat_f32(ey->w[1]);
696 const HVX_Vector vwy2 = hvx_vec_splat_f32(ey->w[2]);
697 const HVX_Vector vwy3 = hvx_vec_splat_f32(ey->w[3]);
698
699 for (uint32_t ox = 0; ox < dst_x; ox += VLEN_FP32) {
700 const uint32_t rem = dst_x - ox;
701 const uint32_t nbytes = (rem < VLEN_FP32) ? (rem * sizeof(float)) : VLEN;
702
703 float v0[VLEN_FP32] __attribute__((aligned(128)));
704 float v1[VLEN_FP32] __attribute__((aligned(128)));
705 float v2[VLEN_FP32] __attribute__((aligned(128)));
706 float v3[VLEN_FP32] __attribute__((aligned(128)));
707
708 const uint32_t cnt = rem < VLEN_FP32 ? rem : VLEN_FP32;
709 for (uint32_t l = 0; l < cnt; ++l) {
710 const struct upscale_bicubic_x * e = &lut_x[ox + l];
711 v0[l] = r0[e->x[0]] * e->w[0] + r0[e->x[1]] * e->w[1] + r0[e->x[2]] * e->w[2] + r0[e->x[3]] * e->w[3];
712 v1[l] = r1[e->x[0]] * e->w[0] + r1[e->x[1]] * e->w[1] + r1[e->x[2]] * e->w[2] + r1[e->x[3]] * e->w[3];
713 v2[l] = r2[e->x[0]] * e->w[0] + r2[e->x[1]] * e->w[1] + r2[e->x[2]] * e->w[2] + r2[e->x[3]] * e->w[3];
714 v3[l] = r3[e->x[0]] * e->w[0] + r3[e->x[1]] * e->w[1] + r3[e->x[2]] * e->w[2] + r3[e->x[3]] * e->w[3];
715 }
716 for (uint32_t l = cnt; l < VLEN_FP32; ++l) {
717 v0[l] = v1[l] = v2[l] = v3[l] = 0.0f;
718 }
719
720 const HVX_Vector vv0 = *(const HVX_Vector *) v0;
721 const HVX_Vector vv1 = *(const HVX_Vector *) v1;
722 const HVX_Vector vv2 = *(const HVX_Vector *) v2;
723 const HVX_Vector vv3 = *(const HVX_Vector *) v3;
724
725 const HVX_Vector acc0 = hvx_vec_add_f32_f32(hvx_vec_mul_f32_f32(vwy0, vv0), hvx_vec_mul_f32_f32(vwy1, vv1));
726 const HVX_Vector acc1 = hvx_vec_add_f32_f32(hvx_vec_mul_f32_f32(vwy2, vv2), hvx_vec_mul_f32_f32(vwy3, vv3));
727 const HVX_Vector val = hvx_vec_add_f32_f32(acc0, acc1);
728
729 hvx_vec_store_u(dst_row + ox, nbytes, val);
730 }
731 }
732}
733
734static inline void dma_push_src_chunk(
735 dma_queue * dma_q,
736 float * spad,
737 const struct htp_tensor * src0,
738 dma_addr_t slice_src_off,
739 uint32_t src_ys,
740 uint32_t src_yc
741) {
742 const uint32_t src_x = src0->ne[0];
743 const dma_addr_t src_addr = src0->data + slice_src_off + (dma_addr_t) src_ys * src0->nb[1];
744
745 if (src0->nb[0] == sizeof(float)) {
746 const uint32_t row_size = src_x * sizeof(float);
747 const uint32_t dst_stride = row_size;
748 const uint32_t src_stride = src0->nb[1];
749 dma_queue_push(dma_q, dma_make_data(spad, src_addr), dst_stride, src_stride, row_size, src_yc);
750 } else {
751 const uint32_t row_size = sizeof(float);
752 const uint32_t dst_stride = sizeof(float);
753 const uint32_t src_stride = src0->nb[0];
754 const uint32_t nrows = src_x * src_yc;
755 dma_queue_push(dma_q, dma_make_data(spad, src_addr), dst_stride, src_stride, row_size, nrows);
756 }
757}
758
759static void upscale_worker(unsigned int nth, unsigned int ith, void * data) {
760 struct upscale_context * ctx = (struct upscale_context *) data;
761 const struct htp_upscale_kernel_params * p = ctx->kparams;
762 const struct htp_upscale_vtcm_layout * L = &ctx->layout;
763 const struct htp_tensor * src0 = ctx->octx->src[0];
764 const struct htp_tensor * dst = ctx->octx->dst;
765
766 const uint32_t first = ctx->chunk_start + ith * ctx->chunks_per_thread;
767 const uint32_t last = MIN(first + ctx->chunks_per_thread, ctx->chunk_start + ctx->chunk_count);
768
769 if (first >= last) {
770 return;
771 }
772
773 struct htp_thread_trace * tr = &ctx->octx->ctx->trace[ith];
774 dma_queue * dma_q = ctx->octx->ctx->dma[ith];
775
776 const uint32_t src_spad_half = (uint32_t) L->src_spad_half_size;
777 const uint32_t dst_spad_half = (uint32_t) L->dst_spad_half_size;
778
779 uint8_t * vtcm_base = (uint8_t *) ctx->octx->ctx->vtcm_base;
780 uint8_t * src_spad = VTCM_LAYOUT_PTR(uint8_t, vtcm_base, L->off_src) + ith * L->src_bytes_per_thread;
781 uint8_t * dst_spad = VTCM_LAYOUT_PTR(uint8_t, vtcm_base, L->off_dst) + ith * L->dst_bytes_per_thread;
782 float * R_buf = VTCM_LAYOUT_PTR(float, vtcm_base, L->off_r) + ith * (L->r_bytes_per_thread / sizeof(float));
783 float * gather_tmp = VTCM_LAYOUT_PTR(float, vtcm_base, L->off_gather) + ith * (L->gather_bytes_per_thread / sizeof(float));
784 const void * lut_y = ctx->lut_y;
785
786 float * srcb2[2] = { (float *) src_spad, (float *) (src_spad + src_spad_half) };
787 float * dstb2[2] = { (float *) dst_spad, (float *) (dst_spad + dst_spad_half) };
788
789 const uint32_t dst_row_size = dst->ne[0] * sizeof(float);
790 const uint32_t dst_stride = dst->nb[1];
791
792 const uint32_t total = last - first;
793
794 // Warmup: prime pipeline with initial transfers
795 for (uint32_t i = 0; i < total && i < 2; ++i) {
796 dma_queue_push(dma_q, dma_make_data(dst->data, dstb2[i]), dst_stride, dst_row_size, dst_row_size, 0);
797
798 dma_addr_t slice_src_off;
799 uint32_t src_ys, src_yc;
800 resolve_src_slice(src0, dst, p, lut_y, first + i, &slice_src_off, &src_ys, &src_yc);
801
802 dma_push_src_chunk(dma_q, srcb2[i], src0, slice_src_off, src_ys, src_yc);
803 }
804
805 // Steady state loop
806 for (uint32_t i = 0; i < total; ++i) {
807 const uint32_t buf = i & 1u;
808 float * srcb = srcb2[buf];
809 float * dstb = dstb2[buf];
810
811 dma_queue_pop(dma_q);
812 dma_queue_pop(dma_q);
813
814 dma_addr_t slice_dst_off;
815 uint32_t dst_ys, dst_yc;
816 resolve_dst_slice(dst, p, first + i, &slice_dst_off, &dst_ys, &dst_yc);
817
818 uint32_t src_ys, src_yc;
819 get_src_y_window(src0, dst, p, lut_y, dst_ys, dst_yc, &src_ys, &src_yc);
820
821 htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) (first + i));
822 ctx->compute_chunk(ctx, srcb, dstb, R_buf, gather_tmp, dst_ys, dst_yc, src_ys);
823 htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) (first + i));
824
825 const dma_addr_t dst_addr = dst->data + slice_dst_off + (dma_addr_t) dst_ys * dst_stride;
826 dma_queue_push(dma_q, dma_make_data(dst_addr, dstb), dst_stride, dst_row_size, dst_row_size, dst_yc);
827
828 const uint32_t next = i + 2;
829 if (next < total) {
830 dma_addr_t next_src_off;
831 uint32_t next_src_ys, next_src_yc;
832 resolve_src_slice(src0, dst, p, lut_y, first + next, &next_src_off, &next_src_ys, &next_src_yc);
833
834 dma_push_src_chunk(dma_q, srcb, src0, next_src_off, next_src_ys, next_src_yc);
835 }
836 }
837
838 dma_queue_flush(dma_q);
839
840 (void) nth;
841}
842
843int op_upscale(struct htp_ops_context * octx) {
844 const struct htp_tensor * src0 = octx->src[0];
845 const struct htp_tensor * dst = octx->dst;
846 const struct htp_upscale_kernel_params * p =
847 (const struct htp_upscale_kernel_params *) octx->kernel_params;
848
849 const uint32_t src_x = src0->ne[0];
850 const uint32_t src_y = src0->ne[1];
851 const uint32_t dst_x = dst->ne[0];
852 const uint32_t dst_y = dst->ne[1];
853
854 uint32_t chunk_start = 0;
855 uint32_t chunk_count = p->total_chunks;
856
857 if (octx->ctx->mdev.count > 1) {
858 const struct htp_tensor_mdev_range range = htp_tensor_mdev_partition(
859 chunk_count, htp_tensor_mdev_data_aligned(dst) ? p->chunks_per_group : 0,
860 octx->ctx->mdev.idx, octx->ctx->mdev.count, &octx->ctx->mdev.count_div);
861 chunk_start = range.start;
862 chunk_count = range.count;
863 }
864
865 if (chunk_count == 0) {
866 return HTP_STATUS_OK;
867 }
868
869 const uint32_t n_threads = MIN(p->n_threads, chunk_count);
870 if (!htp_ops_context_set_n_threads(octx, n_threads)) {
871 return HTP_STATUS_INVAL_PARAMS;
872 }
873
874 const uint32_t chunks_per_thread = fastdiv(chunk_count + n_threads - 1, &octx->n_threads_div);
875
876 size_t lut_x_size = 0;
877 size_t elem_y_sz = sizeof(uint32_t);
878
879 if (p->mode == HTP_UPSCALE_MODE_BICUBIC) {
880 lut_x_size = hex_round_up(dst_x * sizeof(struct upscale_bicubic_x), 128);
881 elem_y_sz = sizeof(struct upscale_bicubic_y);
882 } else if (p->antialias) {
883 lut_x_size = hex_round_up(dst_x * sizeof(struct upscale_antialias_x), 128);
884 elem_y_sz = sizeof(struct upscale_antialias_y);
885 } else if (p->mode == HTP_UPSCALE_MODE_BILINEAR) {
886 const uint32_t n_chunks = (dst_x + VLEN_FP32 - 1) / VLEN_FP32;
887 lut_x_size = hex_round_up(n_chunks * sizeof(struct upscale_bilinear_x_chunk), 128);
888 elem_y_sz = sizeof(struct upscale_bilinear_y);
889 } else {
890 lut_x_size = hex_round_up(dst_x * sizeof(uint32_t), 128);
891 }
892
893 const size_t lut_y_size = hex_round_up(dst_y * elem_y_sz, 128);
894 const size_t total_lut_size = lut_x_size + lut_y_size;
895
896 if (total_lut_size > octx->ctx->ddr_spad_size) {
897 return HTP_STATUS_NO_SUPPORT;
898 }
899
900 void * base_scratch = octx->ctx->ddr_spad_base;
901 void * lut_x = base_scratch;
902 void * lut_y = (uint8_t *) base_scratch + lut_x_size;
903
904 if (p->mode == HTP_UPSCALE_MODE_NEAREST) {
905 upscale_init_luts_nearest((uint32_t *) lut_x, (uint32_t *) lut_y, src0, dst, p);
906 } else if (p->mode == HTP_UPSCALE_MODE_BICUBIC) {
907 upscale_init_luts_bicubic((struct upscale_bicubic_x *) lut_x, (struct upscale_bicubic_y *) lut_y, src0, dst, p);
908 } else if (p->antialias) {
909 upscale_init_luts_antialias((struct upscale_antialias_x *) lut_x, (struct upscale_antialias_y *) lut_y, src0, dst, p);
910 } else {
911 upscale_init_luts_bilinear((struct upscale_bilinear_x_chunk *) lut_x, (struct upscale_bilinear_y *) lut_y, src0, dst, p);
912 }
913
914 upscale_chunk_fn_t compute_chunk;
915 switch (p->kernel_type) {
916 case HTP_UPSCALE_KERNEL_NEAREST:
917 compute_chunk = compute_nearest_chunk;
918 break;
919 case HTP_UPSCALE_KERNEL_NEAREST_2X:
920 compute_chunk = compute_nearest_2x_chunk;
921 break;
922 case HTP_UPSCALE_KERNEL_NEAREST_4X:
923 compute_chunk = compute_nearest_4x_chunk;
924 break;
925 case HTP_UPSCALE_KERNEL_BILINEAR:
926 compute_chunk = compute_bilinear_chunk;
927 break;
928 case HTP_UPSCALE_KERNEL_BILINEAR_2X:
929 compute_chunk = compute_bilinear_2x_chunk;
930 break;
931 case HTP_UPSCALE_KERNEL_BILINEAR_AA:
932 compute_chunk = compute_bilinear_antialias_chunk;
933 break;
934 case HTP_UPSCALE_KERNEL_BICUBIC:
935 compute_chunk = compute_bicubic_chunk;
936 break;
937 default:
938 return HTP_STATUS_INVAL_PARAMS;
939 }
940
941 struct upscale_context ctx = {
942 .octx = octx,
943 .kparams = p,
944 .compute_chunk = compute_chunk,
945 .lut_x = lut_x,
946 .lut_y = lut_y,
947 .chunk_start = chunk_start,
948 .chunk_count = chunk_count,
949 .chunks_per_thread = chunks_per_thread,
950 };
951
952 htp_upscale_vtcm_layout_build(&ctx.layout, src_x, src_y, dst_x, dst_y,
953 p->dst_nrows_per_chunk, p->inv_sf1, p->filter_radius1, n_threads);
954
955 work_queue_run(octx->ctx->work_queue, upscale_worker, &ctx, n_threads);
956
957 return HTP_STATUS_OK;
958}