Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/ggml/blob/master/src/ggml-hexagon/htp/pool-ops.c # ggml [Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685) Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `ggml / src / ggml-hexagon / htp / pool-ops.c` 390 lines ยท 17173 bytes Raw Blame History 1 #pragma clang diagnostic ignored "-Wunused-variable" 2 3 #include 4 #include 5 6 #include "hex-common.h" 7 #include "dma-queue.h" 8 #include "hex-profile.h" 9 #include "htp-ctx.h" 10 #include "htp-ops.h" 11 #include "htp-tensor.h" 12 #include "hvx-inverse.h" 13 #include "hvx-types.h" 14 #include "hvx-utils.h" 15 #include "pool-ops.h" 16 17 #define HTP_POOL_MAX 0 18 #define HTP_POOL_AVG 1 19 20 // Fast path: exact non-overlapping tiling (stride == kernel, no padding), kernel_x in {1,2}. 21 // Every window is guaranteed fully in-bounds, so this never needs boundary clamping. 22 static void pool_plane_hvx( 23 const float * src, float * dst, const struct htp_pool_2d_kernel_params * p) { 24 const bool is_max = (p->pool_op == HTP_POOL_MAX); 25 const HVX_Vector scale = hvx_vec_splat_f32(p->inv_kernel_area); 26 const HVX_Vector seed = is_max ? hvx_vec_splat_f32(-FLT_MAX) : Q6_V_vsplat_R(0); 27 28 for (uint32_t oy = 0; oy < p->dst_y; ++oy) { 29 const uint32_t sy = oy * p->kernel_y; 30 for (uint32_t ox = 0; ox < p->dst_x; ox += VLEN_FP32) { 31 const uint32_t rem = p->dst_x - ox; 32 const uint32_t nbytes = (rem < VLEN_FP32) ? (rem * sizeof(float)) : VLEN; 33 HVX_Vector acc = seed; 34 for (uint32_t ky = 0; ky < p->kernel_y; ++ky) { 35 const float * row = src + (sy + ky) * p->src_x; 36 if (p->kernel_x == 2) { 37 const HVX_Vector v0 = *(const HVX_UVector *) (row + ox * 2); 38 const HVX_Vector v1 = *(const HVX_UVector *) (row + ox * 2 + VLEN_FP32); 39 const HVX_VectorPair deinterleaved = Q6_W_vdeal_VVR(v1, v0, -4); 40 const HVX_Vector lo = Q6_V_lo_W(deinterleaved); 41 const HVX_Vector hi = Q6_V_hi_W(deinterleaved); 42 acc = is_max ? Q6_Vsf_vmax_VsfVsf(acc, Q6_Vsf_vmax_VsfVsf(lo, hi)) 43 : hvx_vec_add_f32_f32(acc, hvx_vec_add_f32_f32(lo, hi)); 44 } else if (p->kernel_x == 1) { 45 const HVX_Vector v = *(const HVX_UVector *) (row + ox); 46 acc = is_max ? Q6_Vsf_vmax_VsfVsf(acc, v) : hvx_vec_add_f32_f32(acc, v); 47 } 48 } 49 hvx_vec_store_u(dst + oy * p->dst_x + ox, nbytes, is_max ? acc : hvx_vec_mul_f32_f32(acc, scale)); 50 } 51 } 52 } 53 54 // Narrow exact-tiling path. The input is staged in VTCM with one vector of 55 // guard space, so full-width loads are safe even when src_x is below 32. 56 static void pool_plane_hvx_narrow( 57 const float * src, float * dst, const struct htp_pool_2d_kernel_params * p) { 58 const bool is_max = (p->pool_op == HTP_POOL_MAX); 59 const HVX_Vector scale = hvx_vec_splat_f32(p->inv_kernel_area); 60 const HVX_Vector seed = is_max ? hvx_vec_splat_f32(-FLT_MAX) : Q6_V_vsplat_R(0); 61 62 for (uint32_t oy = 0; oy < p->dst_y; ++oy) { 63 const uint32_t sy = oy * p->kernel_y; 64 HVX_Vector acc = seed; 65 for (uint32_t ky = 0; ky < p->kernel_y; ++ky) { 66 const float * row = src + (sy + ky) * p->src_x; 67 const HVX_Vector v0 = *(const HVX_UVector *) row; 68 HVX_Vector v = v0; 69 if (p->kernel_x == 2) { 70 // The second vector is zero because a narrow row has fewer 71 // than 32 input elements. The low lanes still contain the 72 // complete even/odd pairs needed by the output. 73 const HVX_Vector zero = Q6_V_vsplat_R(0); 74 const HVX_VectorPair deinterleaved = Q6_W_vdeal_VVR(zero, v0, -4); 75 const HVX_Vector even = Q6_V_lo_W(deinterleaved); 76 const HVX_Vector odd = Q6_V_hi_W(deinterleaved); 77 v = is_max ? Q6_Vsf_vmax_VsfVsf(even, odd) 78 : hvx_vec_add_f32_f32(even, odd); 79 } 80 acc = is_max ? Q6_Vsf_vmax_VsfVsf(acc, v) 81 : hvx_vec_add_f32_f32(acc, v); 82 } 83 hvx_vec_store_u(dst + oy * p->dst_x, p->dst_x * sizeof(float), 84 is_max ? acc : hvx_vec_mul_f32_f32(acc, scale)); 85 } 86 } 87 88 static void pool_plane_global( 89 const float * src, float * dst, const struct htp_pool_2d_kernel_params * p) { 90 const uint32_t n = p->src_x * p->src_y; 91 float val; 92 if (p->pool_op == HTP_POOL_MAX) { 93 val = hvx_reduce_max_f32((const uint8_t *) src, n); 94 } else { 95 val = hvx_reduce_sum_f32((const uint8_t *) src, n) * p->inv_kernel_area; 96 } 97 hvx_vec_store_u(dst, sizeof(float), hvx_vec_splat_f32(val)); 98 } 99 100 static void pool_plane_block( 101 const float * src, float * dst, const struct htp_pool_2d_kernel_params * p) { 102 for (uint32_t oy = 0; oy < p->dst_y; ++oy) { 103 const float * row = src + oy * p->src_x; 104 float * dst_row = dst + oy * p->dst_x; 105 for (uint32_t ox = 0; ox < p->dst_x; ++ox) { 106 const uint8_t * block = (const uint8_t *) (row + ox * p->kernel_x); 107 float val; 108 if (p->pool_op == HTP_POOL_MAX) { 109 val = hvx_reduce_max_f32(block, p->kernel_x); 110 } else { 111 val = hvx_reduce_sum_f32(block, p->kernel_x) * p->inv_kernel_area; 112 } 113 hvx_vec_store_u(dst_row + ox, sizeof(float), hvx_vec_splat_f32(val)); 114 } 115 } 116 } 117 118 // General path: arbitrary kernel/stride/padding 119 120 // Vertical padding is uniform across a whole output row (iy0 depends only on oy, not ox), 121 // so it collapses to one valid-ky range per row instead of a per-element check. 122 static inline void pool_row_bounds_y( 123 const struct htp_pool_2d_kernel_params * p, uint32_t oy, int32_t * iy0, uint32_t * ky_lo, uint32_t * ky_hi) { 124 *iy0 = (int32_t) (oy * p->stride_y) - p->pad_y; 125 const int32_t lo = -(*iy0); 126 const int32_t hi = (int32_t) p->src_y - *iy0; 127 *ky_lo = (uint32_t) MAX(0, lo); 128 *ky_hi = (uint32_t) MAX(0, MIN((int32_t) p->kernel_y, hi)); 129 } 130 131 static inline void pool_pixel_boundary_vec( 132 const float * src, float * dst_row, const struct htp_pool_2d_kernel_params * p, 133 uint32_t ox, int32_t iy0, uint32_t ky_lo, uint32_t ky_hi, bool is_max) { 134 const int32_t ix0 = (int32_t) (ox * p->stride_x) - p->pad_x; 135 const int32_t kx_lo = MAX(0, -ix0); 136 const int32_t kx_hi = MIN((int32_t) p->kernel_x, (int32_t) p->src_x - ix0); 137 138 if (kx_lo >= kx_hi || ky_lo >= ky_hi) { 139 HVX_Vector empty_val = is_max ? hvx_vec_splat_f32(-FLT_MAX) : Q6_V_vsplat_R(0); 140 hvx_vec_store_u(dst_row + ox, sizeof(float), empty_val); 141 return; 142 } 143 144 const uint32_t valid_kx = (uint32_t) (kx_hi - kx_lo); 145 const HVX_Vector mask_identity = is_max ? hvx_vec_splat_f32(-FLT_MAX) : Q6_V_vsplat_R(0); 146 147 HVX_Vector acc = mask_identity; 148 for (uint32_t ky = ky_lo; ky < ky_hi; ++ky) { 149 const float * row = src + (uint32_t) (iy0 + (int32_t) ky) * p->src_x; 150 for (uint32_t k = 0; k < valid_kx; k += VLEN_FP32) { 151 const uint32_t k_rem = valid_kx - k; 152 const uint32_t n = (k_rem < VLEN_FP32) ? k_rem : VLEN_FP32; 153 const HVX_VectorPred q = Q6_Q_vsetq_R(n * sizeof(float)); 154 const HVX_Vector raw = *(const HVX_UVector *) (row + ix0 + kx_lo + k); 155 const HVX_Vector v = Q6_V_vmux_QVV(q, raw, mask_identity); 156 acc = is_max ? Q6_Vsf_vmax_VsfVsf(acc, v) : hvx_vec_add_f32_f32(acc, v); 157 } 158 } 159 160 HVX_Vector reduced = is_max ? hvx_vec_reduce_max_f32(acc) : hvx_vec_reduce_sum_f32(acc); 161 if (!is_max) { 162 HVX_Vector scale_vec; 163 if (p->avg_divide_count) { 164 const uint32_t count = (ky_hi - ky_lo) * valid_kx; 165 scale_vec = hvx_vec_inverse_f32(hvx_vec_splat_f32((float) count)); 166 } else { 167 scale_vec = hvx_vec_splat_f32(p->inv_kernel_area); 168 } 169 reduced = hvx_vec_mul_f32_f32(reduced, scale_vec); 170 } 171 hvx_vec_store_u(dst_row + ox, sizeof(float), reduced); 172 } 173 174 static inline void pool_row_general_boundary_vec( 175 const float * src, float * dst_row, const struct htp_pool_2d_kernel_params * p, 176 uint32_t ox_start, uint32_t ox_end, int32_t iy0, uint32_t ky_lo, uint32_t ky_hi, bool is_max) { 177 for (uint32_t ox = ox_start; ox < ox_end; ++ox) { 178 pool_pixel_boundary_vec(src, dst_row, p, ox, iy0, ky_lo, ky_hi, is_max); 179 } 180 } 181 182 // Vectorized interior loop. 183 static inline void pool_row_general_vec( 184 const float * src, float * dst_row, const struct htp_pool_2d_kernel_params * p, 185 uint32_t ox_start, uint32_t ox_end, int32_t iy0, uint32_t ky_lo, uint32_t ky_hi, bool is_max) { 186 if (ox_start >= ox_end) { 187 return; 188 } 189 190 if (p->stride_x != 1 && p->stride_x != 2) { 191 pool_row_general_boundary_vec(src, dst_row, p, ox_start, ox_end, iy0, ky_lo, ky_hi, is_max); 192 return; 193 } 194 195 const HVX_Vector scale = hvx_vec_splat_f32(p->inv_kernel_area); 196 const HVX_Vector seed = is_max ? hvx_vec_splat_f32(-FLT_MAX) : Q6_V_vsplat_R(0); 197 198 for (uint32_t ox = ox_start; ox < ox_end; ox += VLEN_FP32) { 199 const uint32_t rem = ox_end - ox; 200 const uint32_t nbytes = (rem < VLEN_FP32) ? (rem * sizeof(float)) : VLEN; 201 const int32_t ix0 = (int32_t) (ox * p->stride_x) - p->pad_x; 202 HVX_Vector acc = seed; 203 204 for (uint32_t ky = ky_lo; ky < ky_hi; ++ky) { 205 const float * row = src + (uint32_t) (iy0 + (int32_t) ky) * p->src_x; 206 if (p->stride_x == 1) { 207 for (uint32_t kx = 0; kx < p->kernel_x; ++kx) { 208 const HVX_Vector v = *(const HVX_UVector *) (row + ix0 + (int32_t) kx); 209 acc = is_max ? Q6_Vsf_vmax_VsfVsf(acc, v) : hvx_vec_add_f32_f32(acc, v); 210 } 211 } else { 212 for (uint32_t kx = 0; kx < p->kernel_x; kx += 2) { 213 const HVX_Vector v0 = *(const HVX_UVector *) (row + ix0 + (int32_t) kx); 214 const HVX_Vector v1 = *(const HVX_UVector *) (row + ix0 + (int32_t) kx + VLEN_FP32); 215 const HVX_VectorPair deinterleaved = Q6_W_vdeal_VVR(v1, v0, -4); 216 const HVX_Vector lo = Q6_V_lo_W(deinterleaved); 217 acc = is_max ? Q6_Vsf_vmax_VsfVsf(acc, lo) : hvx_vec_add_f32_f32(acc, lo); 218 if (kx + 1 < p->kernel_x) { 219 const HVX_Vector hi = Q6_V_hi_W(deinterleaved); 220 acc = is_max ? Q6_Vsf_vmax_VsfVsf(acc, hi) : hvx_vec_add_f32_f32(acc, hi); 221 } 222 } 223 } 224 } 225 hvx_vec_store_u(dst_row + ox, nbytes, is_max ? acc : hvx_vec_mul_f32_f32(acc, scale)); 226 } 227 } 228 229 static void pool_plane_general( 230 const float * src, float * dst, const struct htp_pool_2d_kernel_params * p) { 231 const bool is_max = (p->pool_op == HTP_POOL_MAX); 232 233 for (uint32_t oy = 0; oy < p->dst_y; ++oy) { 234 int32_t iy0; 235 uint32_t ky_lo, ky_hi; 236 pool_row_bounds_y(p, oy, &iy0, &ky_lo, &ky_hi); 237 float * dst_row = dst + oy * p->dst_x; 238 239 pool_row_general_boundary_vec(src, dst_row, p, 0, p->ox_lo, iy0, ky_lo, ky_hi, is_max); 240 pool_row_general_vec(src, dst_row, p, p->ox_lo, p->ox_hi, iy0, ky_lo, ky_hi, is_max); 241 pool_row_general_boundary_vec(src, dst_row, p, p->ox_hi, p->dst_x, iy0, ky_lo, ky_hi, is_max); 242 } 243 } 244 245 typedef void (*pool_plane_fn_t)(const float * src, float * dst, const struct htp_pool_2d_kernel_params * p); 246 247 struct pool_2d_context { 248 struct htp_ops_context * octx; 249 const struct htp_pool_2d_kernel_params * kparams; 250 struct htp_pool_vtcm_layout layout; 251 pool_plane_fn_t pool_plane; 252 uint32_t n_threads; 253 uint32_t plane_start; 254 uint32_t plane_count; 255 uint32_t planes_per_thread; 256 }; 257 258 static void pool_2d_thread(unsigned int nth, unsigned int ith, void * data) { 259 struct pool_2d_context * ctx = (struct pool_2d_context *) data; 260 const struct htp_pool_2d_kernel_params * p = ctx->kparams; 261 const struct htp_pool_vtcm_layout * L = &ctx->layout; 262 const struct htp_tensor * src0 = ctx->octx->src[0]; 263 const struct htp_tensor * dst = ctx->octx->dst; 264 pool_plane_fn_t pool_plane = ctx->pool_plane; 265 const uint32_t planes_per_thread = ctx->planes_per_thread; 266 const uint32_t first = ctx->plane_start + ith * planes_per_thread; 267 const uint32_t last = MIN(first + planes_per_thread, ctx->plane_start + ctx->plane_count); 268 269 if (first >= last) { 270 return; 271 } 272 273 struct htp_thread_trace * tr = &ctx->octx->ctx->trace[ith]; 274 dma_queue * dma_queue = ctx->octx->ctx->dma[ith]; 275 276 const uint32_t src_plane_bytes = L->src_plane_bytes; 277 const uint32_t dst_plane_bytes = L->dst_plane_bytes; 278 const uint32_t src_spad_half = (uint32_t) L->src_spad_half_size; 279 const uint32_t dst_spad_half = (uint32_t) L->dst_spad_half_size; 280 281 uint8_t * vtcm_base = (uint8_t *) ctx->octx->ctx->vtcm_base; 282 uint8_t * src_spad = VTCM_LAYOUT_PTR(uint8_t, vtcm_base, L->off_src) + ith * L->src_bytes_per_thread; 283 uint8_t * dst_spad = VTCM_LAYOUT_PTR(uint8_t, vtcm_base, L->off_dst) + ith * L->dst_bytes_per_thread; 284 285 float * srcb2[2] = { (float *) src_spad, (float *) (src_spad + src_spad_half) }; 286 float * dstb2[2] = { (float *) dst_spad, (float *) (dst_spad + dst_spad_half) }; 287 288 const uint32_t total = last - first; 289 290 // Warm up the pipeline: push up to 2 initial (dummy dst, src) transfer pairs. 291 for (uint32_t i = 0; i < total && i < 2; ++i) { 292 dma_queue_push(dma_queue, 293 dma_make_data(dst->data, dstb2[i]), 294 dst_plane_bytes, dst_spad_half, 295 dst_plane_bytes, 0); 296 297 const dma_addr_t src_addr = src0->data + (first + i) * src_plane_bytes; 298 dma_queue_push(dma_queue, 299 dma_make_data(srcb2[i], src_addr), 300 src_spad_half, src_plane_bytes, 301 src_plane_bytes, 1); 302 } 303 304 for (uint32_t i = 0; i < total; ++i) { 305 const uint32_t plane = first + i; 306 const uint32_t buf = i & 1u; 307 float * srcb = srcb2[buf]; 308 float * dstb = dstb2[buf]; 309 310 dma_queue_pop(dma_queue); // dst writeback from plane i - 2 (or dummy on iter 0, 1) 311 dma_queue_pop(dma_queue); // input for plane i 312 313 htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) plane); 314 pool_plane(srcb, dstb, p); 315 htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) plane); 316 317 const dma_addr_t dst_addr = dst->data + plane * dst_plane_bytes; 318 dma_queue_push(dma_queue, 319 dma_make_data(dst_addr, dstb), 320 dst_plane_bytes, dst_spad_half, 321 dst_plane_bytes, 1); 322 323 if (i + 2 < total) { 324 const dma_addr_t next_src_addr = src0->data + (plane + 2) * src_plane_bytes; 325 dma_queue_push(dma_queue, 326 dma_make_data(srcb, next_src_addr), 327 src_spad_half, src_plane_bytes, 328 src_plane_bytes, 1); 329 } 330 } 331 332 dma_queue_flush(dma_queue); 333 334 FARF(HIGH, "pool2d-f32-dma %d/%d: %ux%ux%ux%u -> %ux%ux%ux%u (%u:%u)\n", 335 ith, nth, src0->ne[0], src0->ne[1], src0->ne[2], src0->ne[3], 336 dst->ne[0], dst->ne[1], dst->ne[2], dst->ne[3], first, last); 337 (void) nth; 338 } 339 340 int op_pool_2d(struct htp_ops_context * octx) { 341 const struct htp_tensor * dst = octx->dst; 342 const struct htp_pool_2d_kernel_params * p = 343 (const struct htp_pool_2d_kernel_params *) octx->kernel_params; 344 345 uint32_t plane_start = 0; 346 uint32_t plane_count = p->planes; 347 if (octx->ctx->mdev.count > 1) { 348 const struct htp_tensor_mdev_range range = htp_tensor_mdev_partition( 349 plane_count, htp_tensor_mdev_data_aligned(dst) ? p->planes_per_chunk : 0, 350 octx->ctx->mdev.idx, octx->ctx->mdev.count, &octx->ctx->mdev.count_div); 351 plane_start = range.start; 352 plane_count = range.count; 353 } 354 if (plane_count == 0) { 355 return HTP_STATUS_OK; 356 } 357 358 const uint32_t n_threads = MIN(p->n_threads, plane_count); 359 if (!htp_ops_context_set_n_threads(octx, n_threads)) { 360 return HTP_STATUS_INVAL_PARAMS; 361 } 362 363 const uint32_t planes_per_thread = fastdiv(plane_count + n_threads - 1, &octx->n_threads_div); 364 365 pool_plane_fn_t pool_plane; 366 switch (p->kernel_type) { 367 case HTP_POOL_KERNEL_GLOBAL: pool_plane = pool_plane_global; break; 368 case HTP_POOL_KERNEL_BLOCK: pool_plane = pool_plane_block; break; 369 case HTP_POOL_KERNEL_HVX_NARROW: pool_plane = pool_plane_hvx_narrow; break; 370 case HTP_POOL_KERNEL_HVX: pool_plane = pool_plane_hvx; break; 371 default: pool_plane = pool_plane_general; break; 372 } 373 374 struct pool_2d_context ctx = { 375 .octx = octx, 376 .kparams = p, 377 .pool_plane = pool_plane, 378 .n_threads = n_threads, 379 .plane_start = plane_start, 380 .plane_count = plane_count, 381 .planes_per_thread = planes_per_thread, 382 }; 383 htp_pool_vtcm_layout_build(&ctx.layout, p->src_x, p->src_y, p->dst_x, p->dst_y, n_threads); 384 work_queue_run(octx->ctx->work_queue, pool_2d_thread, &ctx, n_threads); 385 return HTP_STATUS_OK; 386 } 387 388 int op_pool_1d(struct htp_ops_context * octx) { 389 return op_pool_2d(octx); 390 }