ggml
[Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685)
Brought by drakestapleton
ggml / src / ggml-hexagon / htp / argsort-ops.c
| 1 | #include <string.h> |
| 2 | #include <stdlib.h> |
| 3 | #include <math.h> |
| 4 | #include <HAP_farf.h> |
| 5 | #include <HAP_perf.h> |
| 6 | |
| 7 | #define GGML_COMMON_DECL_C |
| 8 | #include "ggml-common.h" |
| 9 | #include "ggml.h" |
| 10 | |
| 11 | #include "hvx-utils.h" |
| 12 | #include "hvx-copy.h" |
| 13 | #include "hvx-reduce.h" |
| 14 | #include "dma-queue.h" |
| 15 | |
| 16 | #include "hex-common.h" |
| 17 | #include "htp-ctx.h" |
| 18 | #include "htp-ops.h" |
| 19 | #include "htp-tensor.h" |
| 20 | #include "argsort-ops.h" |
| 21 | |
| 22 | #ifndef MIN |
| 23 | #define MIN(a, b) ((a) < (b) ? (a) : (b)) |
| 24 | #endif |
| 25 | |
| 26 | |
| 27 | static void quicksort_values_indices_asc(float * values, int32_t * indices, int left, int right) { |
| 28 | while (left < right) { |
| 29 | float pivot = values[left + (right - left) / 2]; |
| 30 | int i = left; |
| 31 | int j = right; |
| 32 | |
| 33 | while (i <= j) { |
| 34 | while (values[i] < pivot) i++; |
| 35 | while (values[j] > pivot) j--; |
| 36 | |
| 37 | if (i <= j) { |
| 38 | float tmp_val = values[i]; |
| 39 | values[i] = values[j]; |
| 40 | values[j] = tmp_val; |
| 41 | |
| 42 | int32_t tmp_idx = indices[i]; |
| 43 | indices[i] = indices[j]; |
| 44 | indices[j] = tmp_idx; |
| 45 | i++; |
| 46 | j--; |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | // Tail-recursion elimination to bound stack depth |
| 51 | if (j - left < right - i) { |
| 52 | if (left < j) quicksort_values_indices_asc(values, indices, left, j); |
| 53 | left = i; |
| 54 | } else { |
| 55 | if (i < right) quicksort_values_indices_asc(values, indices, i, right); |
| 56 | right = j; |
| 57 | } |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | static void quicksort_values_indices_desc(float * values, int32_t * indices, int left, int right) { |
| 62 | while (left < right) { |
| 63 | float pivot = values[left + (right - left) / 2]; |
| 64 | int i = left; |
| 65 | int j = right; |
| 66 | |
| 67 | while (i <= j) { |
| 68 | while (values[i] > pivot) i++; |
| 69 | while (values[j] < pivot) j--; |
| 70 | |
| 71 | if (i <= j) { |
| 72 | float tmp_val = values[i]; |
| 73 | values[i] = values[j]; |
| 74 | values[j] = tmp_val; |
| 75 | |
| 76 | int32_t tmp_idx = indices[i]; |
| 77 | indices[i] = indices[j]; |
| 78 | indices[j] = tmp_idx; |
| 79 | i++; |
| 80 | j--; |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | // Tail-recursion elimination to bound stack depth |
| 85 | if (j - left < right - i) { |
| 86 | if (left < j) quicksort_values_indices_desc(values, indices, left, j); |
| 87 | left = i; |
| 88 | } else { |
| 89 | if (i < right) quicksort_values_indices_desc(values, indices, i, right); |
| 90 | right = j; |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | static inline uint32_t top_k_max_value_index(const float * values, uint32_t n, float * value) { |
| 96 | int32_t max_idx = 0; |
| 97 | hvx_argmax_f32(values, n, 0, value, &max_idx); |
| 98 | return (uint32_t) max_idx; |
| 99 | } |
| 100 | |
| 101 | static const int32_t argsort_ramp_lut[32] __attribute__((aligned(VLEN))) = { |
| 102 | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, |
| 103 | 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31 |
| 104 | }; |
| 105 | |
| 106 | static inline void init_indices_ramp(int32_t * indices_buf, uint32_t count, uint32_t base) { |
| 107 | const HVX_Vector ind_init_vec = Q6_Vw_vadd_VwVw(*(const HVX_Vector *) argsort_ramp_lut, Q6_V_vsplat_R(base)); |
| 108 | const HVX_Vector ind_diff_vec = Q6_V_vsplat_R(32); |
| 109 | HVX_Vector * indices_buf_vec = (HVX_Vector *) indices_buf; |
| 110 | uint32_t num_vecs = count / 32; |
| 111 | HVX_Vector curr_ind_vec = ind_init_vec; |
| 112 | for (uint32_t j = 0; j < num_vecs; j++) { |
| 113 | indices_buf_vec[j] = curr_ind_vec; |
| 114 | curr_ind_vec = Q6_Vw_vadd_VwVw(curr_ind_vec, ind_diff_vec); |
| 115 | } |
| 116 | uint32_t rem = count % 32; |
| 117 | if (rem > 0) { |
| 118 | hvx_vec_store_a((void *) &indices_buf_vec[num_vecs], rem * sizeof(int32_t), curr_ind_vec); |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | __attribute__((always_inline)) |
| 123 | static inline void vec_cas(HVX_Vector * X_val, HVX_Vector * X_idx, HVX_Vector * Y_val, HVX_Vector * Y_idx, bool asc) { |
| 124 | HVX_VectorPred pred = asc ? Q6_Q_vcmp_gt_VsfVsf(*X_val, *Y_val) |
| 125 | : Q6_Q_vcmp_gt_VsfVsf(*Y_val, *X_val); |
| 126 | HVX_Vector next_X_val = Q6_V_vmux_QVV(pred, *Y_val, *X_val); |
| 127 | HVX_Vector next_Y_val = Q6_V_vmux_QVV(pred, *X_val, *Y_val); |
| 128 | HVX_Vector next_X_idx = Q6_V_vmux_QVV(pred, *Y_idx, *X_idx); |
| 129 | HVX_Vector Y_tmp_idx = Q6_V_vmux_QVV(pred, *X_idx, *Y_idx); |
| 130 | *X_val = next_X_val; |
| 131 | *Y_val = next_Y_val; |
| 132 | *X_idx = next_X_idx; |
| 133 | *Y_idx = Y_tmp_idx; |
| 134 | } |
| 135 | |
| 136 | __attribute__((always_inline)) |
| 137 | static inline void bitonic_cas_32(HVX_Vector * V, HVX_Vector * I, int d, HVX_VectorPred dir_mask, HVX_Vector idx_vec, HVX_Vector zero_vec) { |
| 138 | HVX_VectorPred mask_left; |
| 139 | HVX_Vector V_rot_left, V_rot_right; |
| 140 | HVX_Vector I_rot_left, I_rot_right; |
| 141 | |
| 142 | if (d == 1) { |
| 143 | mask_left = Q6_Q_vcmp_eq_VwVw(Q6_V_vand_VV(idx_vec, Q6_V_vsplat_R(1)), zero_vec); |
| 144 | V_rot_left = Q6_V_vror_VR(*V, 4); |
| 145 | V_rot_right = Q6_V_vror_VR(*V, 124); |
| 146 | I_rot_left = Q6_V_vror_VR(*I, 4); |
| 147 | I_rot_right = Q6_V_vror_VR(*I, 124); |
| 148 | } else if (d == 2) { |
| 149 | mask_left = Q6_Q_vcmp_eq_VwVw(Q6_V_vand_VV(idx_vec, Q6_V_vsplat_R(2)), zero_vec); |
| 150 | V_rot_left = Q6_V_vror_VR(*V, 8); |
| 151 | V_rot_right = Q6_V_vror_VR(*V, 120); |
| 152 | I_rot_left = Q6_V_vror_VR(*I, 8); |
| 153 | I_rot_right = Q6_V_vror_VR(*I, 120); |
| 154 | } else if (d == 4) { |
| 155 | mask_left = Q6_Q_vcmp_eq_VwVw(Q6_V_vand_VV(idx_vec, Q6_V_vsplat_R(4)), zero_vec); |
| 156 | V_rot_left = Q6_V_vror_VR(*V, 16); |
| 157 | V_rot_right = Q6_V_vror_VR(*V, 112); |
| 158 | I_rot_left = Q6_V_vror_VR(*I, 16); |
| 159 | I_rot_right = Q6_V_vror_VR(*I, 112); |
| 160 | } else if (d == 8) { |
| 161 | mask_left = Q6_Q_vcmp_eq_VwVw(Q6_V_vand_VV(idx_vec, Q6_V_vsplat_R(8)), zero_vec); |
| 162 | V_rot_left = Q6_V_vror_VR(*V, 32); |
| 163 | V_rot_right = Q6_V_vror_VR(*V, 96); |
| 164 | I_rot_left = Q6_V_vror_VR(*I, 32); |
| 165 | I_rot_right = Q6_V_vror_VR(*I, 96); |
| 166 | } else { |
| 167 | mask_left = Q6_Q_vcmp_eq_VwVw(Q6_V_vand_VV(idx_vec, Q6_V_vsplat_R(16)), zero_vec); |
| 168 | V_rot_left = Q6_V_vror_VR(*V, 64); |
| 169 | V_rot_right = Q6_V_vror_VR(*V, 64); |
| 170 | I_rot_left = Q6_V_vror_VR(*I, 64); |
| 171 | I_rot_right = Q6_V_vror_VR(*I, 64); |
| 172 | } |
| 173 | |
| 174 | HVX_Vector V_paired = Q6_V_vmux_QVV(mask_left, V_rot_left, V_rot_right); |
| 175 | HVX_Vector I_paired = Q6_V_vmux_QVV(mask_left, I_rot_left, I_rot_right); |
| 176 | |
| 177 | HVX_VectorPred V_gt_Vpaired = Q6_Q_vcmp_gt_VsfVsf(*V, V_paired); |
| 178 | HVX_VectorPred Vpaired_gt_V = Q6_Q_vcmp_gt_VsfVsf(V_paired, *V); |
| 179 | HVX_VectorPred mask_right = Q6_Q_not_Q(mask_left); |
| 180 | HVX_VectorPred Q_asc = Q6_Q_or_QQ( |
| 181 | Q6_Q_and_QQ(mask_left, V_gt_Vpaired), |
| 182 | Q6_Q_and_QQ(Vpaired_gt_V, mask_right) |
| 183 | ); |
| 184 | HVX_VectorPred Q_swap = Q6_Q_or_QQ( |
| 185 | Q6_Q_and_QQ(dir_mask, Q_asc), |
| 186 | Q6_Q_and_QQ(Q6_Q_not_Q(dir_mask), Q6_Q_not_Q(Q_asc)) |
| 187 | ); |
| 188 | |
| 189 | *V = Q6_V_vmux_QVV(Q_swap, V_paired, *V); |
| 190 | *I = Q6_V_vmux_QVV(Q_swap, I_paired, *I); |
| 191 | } |
| 192 | |
| 193 | __attribute__((always_inline)) |
| 194 | static inline void bitonic_sort_generic_hvx(uint8_t * values, uint8_t * indices, int K, bool asc_order) { |
| 195 | HVX_Vector V[32]; |
| 196 | HVX_Vector I[32]; |
| 197 | |
| 198 | HVX_Vector zero_vec = Q6_V_vzero(); |
| 199 | HVX_Vector idx_vec = *(const HVX_Vector *) argsort_ramp_lut; |
| 200 | |
| 201 | for (int v = 0; v < K; v++) { |
| 202 | V[v] = *(HVX_Vector *)(values + v * 128); |
| 203 | I[v] = Q6_Vw_vadd_VwVw(idx_vec, Q6_V_vsplat_R(v * 32)); |
| 204 | } |
| 205 | |
| 206 | HVX_VectorPred pred_all_1s = Q6_Q_vcmp_eq_VwVw(zero_vec, zero_vec); |
| 207 | HVX_VectorPred pred_all_0s = Q6_Q_not_Q(pred_all_1s); |
| 208 | |
| 209 | int M = 5; |
| 210 | while ((1 << (M - 5)) < K) M++; |
| 211 | |
| 212 | for (int s = 1; s <= M; s++) { |
| 213 | for (int stage_d = s - 1; stage_d >= 0; stage_d--) { |
| 214 | int d = 1 << stage_d; |
| 215 | if (d >= 32) { |
| 216 | int v_dist = d / 32; |
| 217 | for (int v1 = 0; v1 < K; v1++) { |
| 218 | if ((v1 & v_dist) == 0) { |
| 219 | int v2 = v1 + v_dist; |
| 220 | bool asc = (s < M) ? ((((v1 * 32) >> s) % 2) == 0) : asc_order; |
| 221 | vec_cas(&V[v1], &I[v1], &V[v2], &I[v2], asc); |
| 222 | } |
| 223 | } |
| 224 | } else { |
| 225 | if (s < 5) { |
| 226 | HVX_VectorPred dir_mask = Q6_Q_vcmp_eq_VwVw(Q6_V_vand_VV(idx_vec, Q6_V_vsplat_R(1 << s)), zero_vec); |
| 227 | for (int v = 0; v < K; v++) { |
| 228 | bitonic_cas_32(&V[v], &I[v], d, dir_mask, idx_vec, zero_vec); |
| 229 | } |
| 230 | } else { |
| 231 | for (int v = 0; v < K; v++) { |
| 232 | bool asc = (s < M) ? ((((v * 32) >> s) % 2) == 0) : asc_order; |
| 233 | HVX_VectorPred dir_mask = asc ? pred_all_1s : pred_all_0s; |
| 234 | bitonic_cas_32(&V[v], &I[v], d, dir_mask, idx_vec, zero_vec); |
| 235 | } |
| 236 | } |
| 237 | } |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | for (int v = 0; v < K; v++) { |
| 242 | *(HVX_Vector *)(values + v * 128) = V[v]; |
| 243 | *(HVX_Vector *)(indices + v * 128) = I[v]; |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | static void bitonic_sort_vtcm_desc(uint8_t * values, uint8_t * indices, uint32_t n_vec, bool init_indices) { |
| 248 | HVX_Vector zero_vec = Q6_V_vzero(); |
| 249 | HVX_Vector idx_vec = *(const HVX_Vector *) argsort_ramp_lut; |
| 250 | |
| 251 | HVX_VectorPred pred_all_1s = Q6_Q_vcmp_eq_VwVw(zero_vec, zero_vec); |
| 252 | HVX_VectorPred pred_all_0s = Q6_Q_not_Q(pred_all_1s); |
| 253 | |
| 254 | if (init_indices) { |
| 255 | for (uint32_t v = 0; v < n_vec; v++) { |
| 256 | HVX_Vector idx = Q6_Vw_vadd_VwVw(idx_vec, Q6_V_vsplat_R(v * 32)); |
| 257 | *(HVX_Vector *)(indices + v * 128) = idx; |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | int M = 5; |
| 262 | while ((1u << (M - 5)) < n_vec) M++; |
| 263 | |
| 264 | for (int s = 1; s <= M; s++) { |
| 265 | for (int stage_d = s - 1; stage_d >= 0; stage_d--) { |
| 266 | int d = 1 << stage_d; |
| 267 | if (d >= 32) { |
| 268 | uint32_t v_dist = d / 32; |
| 269 | for (uint32_t v1 = 0; v1 < n_vec; v1++) { |
| 270 | if ((v1 & v_dist) == 0) { |
| 271 | uint32_t v2 = v1 + v_dist; |
| 272 | bool asc = (s < M) ? ((((v1 * 32) >> s) % 2) == 0) : false; |
| 273 | |
| 274 | HVX_Vector Vv1 = *(HVX_Vector *)(values + v1 * 128); |
| 275 | HVX_Vector Iv1 = *(HVX_Vector *)(indices + v1 * 128); |
| 276 | HVX_Vector Vv2 = *(HVX_Vector *)(values + v2 * 128); |
| 277 | HVX_Vector Iv2 = *(HVX_Vector *)(indices + v2 * 128); |
| 278 | |
| 279 | vec_cas(&Vv1, &Iv1, &Vv2, &Iv2, asc); |
| 280 | |
| 281 | *(HVX_Vector *)(values + v1 * 128) = Vv1; |
| 282 | *(HVX_Vector *)(indices + v1 * 128) = Iv1; |
| 283 | *(HVX_Vector *)(values + v2 * 128) = Vv2; |
| 284 | *(HVX_Vector *)(indices + v2 * 128) = Iv2; |
| 285 | } |
| 286 | } |
| 287 | } else { |
| 288 | if (s < 5) { |
| 289 | HVX_VectorPred dir_mask = Q6_Q_vcmp_eq_VwVw(Q6_V_vand_VV(idx_vec, Q6_V_vsplat_R(1 << s)), zero_vec); |
| 290 | for (uint32_t v = 0; v < n_vec; v++) { |
| 291 | HVX_Vector Vv = *(HVX_Vector *)(values + v * 128); |
| 292 | HVX_Vector Iv = *(HVX_Vector *)(indices + v * 128); |
| 293 | |
| 294 | bitonic_cas_32(&Vv, &Iv, d, dir_mask, idx_vec, zero_vec); |
| 295 | |
| 296 | *(HVX_Vector *)(values + v * 128) = Vv; |
| 297 | *(HVX_Vector *)(indices + v * 128) = Iv; |
| 298 | } |
| 299 | } else { |
| 300 | for (uint32_t v = 0; v < n_vec; v++) { |
| 301 | bool asc = (s < M) ? ((((v * 32) >> s) % 2) == 0) : false; |
| 302 | HVX_VectorPred dir_mask = asc ? pred_all_1s : pred_all_0s; |
| 303 | |
| 304 | HVX_Vector Vv = *(HVX_Vector *)(values + v * 128); |
| 305 | HVX_Vector Iv = *(HVX_Vector *)(indices + v * 128); |
| 306 | |
| 307 | bitonic_cas_32(&Vv, &Iv, d, dir_mask, idx_vec, zero_vec); |
| 308 | |
| 309 | *(HVX_Vector *)(values + v * 128) = Vv; |
| 310 | *(HVX_Vector *)(indices + v * 128) = Iv; |
| 311 | } |
| 312 | } |
| 313 | } |
| 314 | } |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | // top-k of one row held in VTCM, k <= 64. Keeps a sorted list of the k best, rejects 256-float blocks with one max-reduce. |
| 319 | // Writes k values and k indices (ties: first seen wins). Slots with no finite value get -INF and index 0. |
| 320 | static void top_k_scan(const float * vals, uint32_t n, uint32_t k, float * out_v, int32_t * out_i) { |
| 321 | float best_v[64]; |
| 322 | int32_t best_i[64]; |
| 323 | uint32_t cnt = 0; |
| 324 | float thr = -INFINITY; |
| 325 | |
| 326 | const uint32_t blk = 256; |
| 327 | for (uint32_t off = 0; off < n; off += blk) { |
| 328 | const uint32_t bn = MIN(blk, n - off); |
| 329 | if (hvx_reduce_max_f32((const uint8_t *) (vals + off), bn) <= thr) { |
| 330 | continue; |
| 331 | } |
| 332 | for (uint32_t vi = 0; vi < bn; vi += 32) { |
| 333 | const uint32_t vn = MIN(32, bn - vi); |
| 334 | HVX_VectorAlias v_local; |
| 335 | v_local.v = hvx_vmem(vals + off + vi); |
| 336 | for (uint32_t i = 0; i < vn; i++) { |
| 337 | const float v = v_local.fp32[i]; |
| 338 | if (!(v > thr)) continue; |
| 339 | uint32_t pos = cnt < k ? cnt++ : k - 1; |
| 340 | while (pos > 0 && best_v[pos - 1] < v) { |
| 341 | best_v[pos] = best_v[pos - 1]; |
| 342 | best_i[pos] = best_i[pos - 1]; |
| 343 | pos--; |
| 344 | } |
| 345 | best_v[pos] = v; |
| 346 | best_i[pos] = (int32_t) (off + vi + i); |
| 347 | if (cnt == k) thr = best_v[k - 1]; |
| 348 | } |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | // fewer than k values above -INF: take -INF or NaN elements, then pad with index 0 |
| 353 | for (uint32_t off = 0; off < n && cnt < k; off += 32) { |
| 354 | const uint32_t vn = MIN(32, n - off); |
| 355 | HVX_VectorAlias v_local; |
| 356 | v_local.v = hvx_vmem(vals + off); |
| 357 | for (uint32_t j = 0; j < vn && cnt < k; j++) { |
| 358 | if (!(v_local.fp32[j] > -INFINITY)) { |
| 359 | best_v[cnt] = -INFINITY; |
| 360 | best_i[cnt] = (int32_t) (off + j); |
| 361 | cnt++; |
| 362 | } |
| 363 | } |
| 364 | } |
| 365 | for (; cnt < k; cnt++) { |
| 366 | best_v[cnt] = -INFINITY; |
| 367 | best_i[cnt] = 0; |
| 368 | } |
| 369 | |
| 370 | for (uint32_t j = 0; j < k; j++) { |
| 371 | out_v[j] = best_v[j]; |
| 372 | out_i[j] = best_i[j]; |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | __attribute__((always_inline)) |
| 377 | static inline void sort32_f32_hvx(uint8_t * values, uint8_t * indices, enum ggml_sort_order order) { |
| 378 | bitonic_sort_generic_hvx(values, indices, 1, order == GGML_SORT_ORDER_ASC); |
| 379 | } |
| 380 | |
| 381 | __attribute__((always_inline)) |
| 382 | static inline void sort64_f32_hvx(uint8_t * values, uint8_t * indices, enum ggml_sort_order order) { |
| 383 | bitonic_sort_generic_hvx(values, indices, 2, order == GGML_SORT_ORDER_ASC); |
| 384 | } |
| 385 | |
| 386 | __attribute__((always_inline)) |
| 387 | static inline void sort128_f32_hvx(uint8_t * values, uint8_t * indices, enum ggml_sort_order order) { |
| 388 | bitonic_sort_generic_hvx(values, indices, 4, order == GGML_SORT_ORDER_ASC); |
| 389 | } |
| 390 | |
| 391 | __attribute__((always_inline)) |
| 392 | static inline void sort256_f32_hvx(uint8_t * values, uint8_t * indices, enum ggml_sort_order order) { |
| 393 | bitonic_sort_generic_hvx(values, indices, 8, order == GGML_SORT_ORDER_ASC); |
| 394 | } |
| 395 | |
| 396 | __attribute__((always_inline)) |
| 397 | static inline void sort512_f32_hvx(uint8_t * values, uint8_t * indices, enum ggml_sort_order order) { |
| 398 | bitonic_sort_generic_hvx(values, indices, 16, order == GGML_SORT_ORDER_ASC); |
| 399 | } |
| 400 | |
| 401 | __attribute__((always_inline)) |
| 402 | static inline void sort1024_f32_hvx(uint8_t * values, uint8_t * indices, enum ggml_sort_order order) { |
| 403 | bitonic_sort_generic_hvx(values, indices, 32, order == GGML_SORT_ORDER_ASC); |
| 404 | } |
| 405 | |
| 406 | static void merge_runs( |
| 407 | const float * restrict in_val0, const int32_t * restrict in_idx0, uint32_t n0, |
| 408 | const float * restrict in_val1, const int32_t * restrict in_idx1, uint32_t n1, |
| 409 | float * restrict out_val, int32_t * restrict out_idx, |
| 410 | bool asc |
| 411 | ) { |
| 412 | uint32_t i0 = 0; |
| 413 | uint32_t i1 = 0; |
| 414 | uint32_t out = 0; |
| 415 | |
| 416 | if (asc) { |
| 417 | while (i0 < n0 && i1 < n1) { |
| 418 | if (in_val0[i0] <= in_val1[i1]) { |
| 419 | if (out_val) out_val[out] = in_val0[i0]; |
| 420 | out_idx[out] = in_idx0[i0]; |
| 421 | i0++; |
| 422 | } else { |
| 423 | if (out_val) out_val[out] = in_val1[i1]; |
| 424 | out_idx[out] = in_idx1[i1]; |
| 425 | i1++; |
| 426 | } |
| 427 | out++; |
| 428 | } |
| 429 | } else { |
| 430 | while (i0 < n0 && i1 < n1) { |
| 431 | if (in_val0[i0] >= in_val1[i1]) { |
| 432 | if (out_val) out_val[out] = in_val0[i0]; |
| 433 | out_idx[out] = in_idx0[i0]; |
| 434 | i0++; |
| 435 | } else { |
| 436 | if (out_val) out_val[out] = in_val1[i1]; |
| 437 | out_idx[out] = in_idx1[i1]; |
| 438 | i1++; |
| 439 | } |
| 440 | out++; |
| 441 | } |
| 442 | } |
| 443 | |
| 444 | while (i0 < n0) { |
| 445 | if (out_val) out_val[out] = in_val0[i0]; |
| 446 | out_idx[out] = in_idx0[i0]; |
| 447 | i0++; |
| 448 | out++; |
| 449 | } |
| 450 | |
| 451 | while (i1 < n1) { |
| 452 | if (out_val) out_val[out] = in_val1[i1]; |
| 453 | out_idx[out] = in_idx1[i1]; |
| 454 | i1++; |
| 455 | out++; |
| 456 | } |
| 457 | } |
| 458 | |
| 459 | struct htp_sort_chunk_ctx { |
| 460 | struct htp_ops_context * octx; |
| 461 | const struct htp_sort_kernel_params * kparams; |
| 462 | uint8_t * phase1_base; |
| 463 | size_t phase1_slot_size; |
| 464 | float * merge_values; |
| 465 | int32_t * merge_indices; |
| 466 | uint32_t real_count[HTP_MAX_NTHREADS]; |
| 467 | }; |
| 468 | |
| 469 | static void htp_sort_chunk_job(unsigned int n, unsigned int i, void * data) { |
| 470 | struct htp_sort_chunk_ctx * cctx = (struct htp_sort_chunk_ctx *) data; |
| 471 | struct htp_ops_context * octx = cctx->octx; |
| 472 | const struct htp_sort_kernel_params * kparams = cctx->kparams; |
| 473 | const struct htp_tensor * src0 = octx->src[0]; |
| 474 | const struct htp_tensor * dst = octx->dst; |
| 475 | |
| 476 | uint32_t chunk_elems = (uint32_t) kparams->chunk_elems; |
| 477 | uint32_t ne00 = src0->ne[0]; |
| 478 | uint32_t chunk_base = i * chunk_elems; |
| 479 | |
| 480 | uint32_t real_count = (chunk_base < ne00) ? MIN(chunk_elems, ne00 - chunk_base) : 0; |
| 481 | cctx->real_count[i] = real_count; |
| 482 | |
| 483 | uint8_t * spad = cctx->phase1_base + cctx->phase1_slot_size * i; |
| 484 | float * values_buf = (float *) spad; |
| 485 | int32_t * indices_buf = (int32_t *) (spad + chunk_elems * sizeof(float)); |
| 486 | |
| 487 | float * merge_values = cctx->merge_values; |
| 488 | int32_t * merge_indices = cctx->merge_indices; |
| 489 | |
| 490 | struct htp_thread_trace * tr = &octx->ctx->trace[i]; |
| 491 | const dma_addr_t src_addr = src0->data + (size_t) chunk_base * sizeof(float); |
| 492 | |
| 493 | if (kparams->is_top_k) { |
| 494 | uint32_t k = dst->ne[0]; |
| 495 | uint32_t local_k = MIN(k, chunk_elems); |
| 496 | |
| 497 | if (real_count == 0) { |
| 498 | for (uint32_t j = 0; j < local_k; j++) { |
| 499 | merge_values[i * local_k + j] = -INFINITY; |
| 500 | merge_indices[i * local_k + j] = 0; |
| 501 | } |
| 502 | return; |
| 503 | } |
| 504 | |
| 505 | if (local_k == 1 && ne00 >= 128*1024) { |
| 506 | dma_sync_read(octx->ctx->dma[i], values_buf, src_addr, real_count * sizeof(float)); |
| 507 | |
| 508 | htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) i); |
| 509 | float max_value; |
| 510 | uint32_t max_index = top_k_max_value_index(values_buf, real_count, &max_value); |
| 511 | merge_values[i] = max_value; |
| 512 | merge_indices[i] = (int32_t) (max_index + chunk_base); |
| 513 | htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) i); |
| 514 | return; |
| 515 | } |
| 516 | |
| 517 | if (local_k > 1 && local_k <= 64 && chunk_elems > 1024) { |
| 518 | dma_sync_read(octx->ctx->dma[i], values_buf, src_addr, real_count * sizeof(float)); |
| 519 | |
| 520 | htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) i); |
| 521 | top_k_scan(values_buf, real_count, local_k, &merge_values[i * local_k], &merge_indices[i * local_k]); |
| 522 | for (uint32_t j = 0; j < local_k; j++) { |
| 523 | merge_indices[i * local_k + j] += (int32_t) chunk_base; |
| 524 | } |
| 525 | htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) i); |
| 526 | return; |
| 527 | } |
| 528 | |
| 529 | dma_sync_read(octx->ctx->dma[i], values_buf, src_addr, real_count * sizeof(float)); |
| 530 | |
| 531 | htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) i); |
| 532 | if (chunk_elems > real_count) { |
| 533 | hvx_splat_f32_u((uint8_t *)(values_buf + real_count), -INFINITY, chunk_elems - real_count); |
| 534 | } |
| 535 | |
| 536 | bitonic_sort_vtcm_desc((uint8_t *) values_buf, (uint8_t *) indices_buf, chunk_elems / 32, true); |
| 537 | |
| 538 | for (uint32_t j = 0; j < local_k; j++) { |
| 539 | merge_values[i * local_k + j] = values_buf[j]; |
| 540 | merge_indices[i * local_k + j] = indices_buf[j] + (int32_t) chunk_base; |
| 541 | } |
| 542 | htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) i); |
| 543 | } else { |
| 544 | if (real_count > 0) { |
| 545 | dma_sync_read(octx->ctx->dma[i], values_buf, src_addr, real_count * sizeof(float)); |
| 546 | |
| 547 | htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) i); |
| 548 | init_indices_ramp(indices_buf, real_count, chunk_base); |
| 549 | |
| 550 | if (kparams->order == GGML_SORT_ORDER_ASC) { |
| 551 | quicksort_values_indices_asc(values_buf, indices_buf, 0, real_count - 1); |
| 552 | } else { |
| 553 | quicksort_values_indices_desc(values_buf, indices_buf, 0, real_count - 1); |
| 554 | } |
| 555 | htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) i); |
| 556 | } |
| 557 | } |
| 558 | } |
| 559 | |
| 560 | struct htp_argsort_merge_l1_ctx { |
| 561 | struct htp_sort_chunk_ctx * cctx; |
| 562 | bool asc; |
| 563 | }; |
| 564 | |
| 565 | static void htp_argsort_merge_l1_job(unsigned int n, unsigned int i, void * data) { |
| 566 | struct htp_argsort_merge_l1_ctx * mctx = (struct htp_argsort_merge_l1_ctx *) data; |
| 567 | struct htp_sort_chunk_ctx * cctx = mctx->cctx; |
| 568 | const struct htp_sort_kernel_params * kparams = cctx->kparams; |
| 569 | |
| 570 | uint32_t c0 = i * 2; |
| 571 | uint32_t c1 = c0 + 1; |
| 572 | |
| 573 | uint32_t count0 = cctx->real_count[c0]; |
| 574 | uint32_t count1 = cctx->real_count[c1]; |
| 575 | |
| 576 | size_t slot_size = cctx->phase1_slot_size; |
| 577 | uint32_t chunk_elems = (uint32_t) kparams->chunk_elems; |
| 578 | |
| 579 | const float * val0 = (const float *) (cctx->phase1_base + slot_size * c0); |
| 580 | const int32_t * idx0 = (const int32_t *) (cctx->phase1_base + slot_size * c0 + chunk_elems * sizeof(float)); |
| 581 | const float * val1 = (const float *) (cctx->phase1_base + slot_size * c1); |
| 582 | const int32_t * idx1 = (const int32_t *) (cctx->phase1_base + slot_size * c1 + chunk_elems * sizeof(float)); |
| 583 | |
| 584 | uint32_t out_offset = (2 * i) * chunk_elems; |
| 585 | float * out_val = cctx->merge_values + out_offset; |
| 586 | int32_t * out_idx = cctx->merge_indices + out_offset; |
| 587 | |
| 588 | struct htp_thread_trace * tr = &cctx->octx->ctx->trace[i]; |
| 589 | htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) i); |
| 590 | merge_runs(val0, idx0, count0, val1, idx1, count1, out_val, out_idx, mctx->asc); |
| 591 | htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) i); |
| 592 | } |
| 593 | |
| 594 | struct htp_argsort_merge_l2_ctx { |
| 595 | struct htp_sort_chunk_ctx * cctx; |
| 596 | bool asc; |
| 597 | }; |
| 598 | |
| 599 | static void htp_argsort_merge_l2_job(unsigned int n, unsigned int i, void * data) { |
| 600 | struct htp_argsort_merge_l2_ctx * mctx = (struct htp_argsort_merge_l2_ctx *) data; |
| 601 | struct htp_sort_chunk_ctx * cctx = mctx->cctx; |
| 602 | const struct htp_sort_kernel_params * kparams = cctx->kparams; |
| 603 | |
| 604 | size_t slot_size = cctx->phase1_slot_size; |
| 605 | uint32_t chunk_elems = (uint32_t) kparams->chunk_elems; |
| 606 | |
| 607 | uint32_t off0 = (4 * i) * chunk_elems; |
| 608 | uint32_t off1 = (4 * i + 2) * chunk_elems; |
| 609 | |
| 610 | const float * val0 = cctx->merge_values + off0; |
| 611 | const int32_t * idx0 = cctx->merge_indices + off0; |
| 612 | const float * val1 = cctx->merge_values + off1; |
| 613 | const int32_t * idx1 = cctx->merge_indices + off1; |
| 614 | |
| 615 | uint32_t count0 = cctx->real_count[4*i + 0] + cctx->real_count[4*i + 1]; |
| 616 | uint32_t count1 = cctx->real_count[4*i + 2] + cctx->real_count[4*i + 3]; |
| 617 | |
| 618 | uint8_t * slot_base = cctx->phase1_base + (4 * i) * slot_size; |
| 619 | float * out_val = (float *) slot_base; |
| 620 | int32_t * out_idx = (int32_t *) (slot_base + 4 * chunk_elems * sizeof(float)); |
| 621 | |
| 622 | struct htp_thread_trace * tr = &cctx->octx->ctx->trace[i]; |
| 623 | htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) i); |
| 624 | merge_runs(val0, idx0, count0, val1, idx1, count1, out_val, out_idx, mctx->asc); |
| 625 | htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) i); |
| 626 | } |
| 627 | |
| 628 | struct htp_sort_multi_row_ctx { |
| 629 | struct htp_ops_context * octx; |
| 630 | const struct htp_sort_kernel_params * kparams; |
| 631 | uint8_t * phase1_base; |
| 632 | size_t phase1_slot_size; |
| 633 | uint32_t row_start; |
| 634 | uint32_t row_end; |
| 635 | uint32_t nrows_per_thread; |
| 636 | }; |
| 637 | |
| 638 | struct htp_tensor_row_coord { |
| 639 | uint32_t i01; |
| 640 | uint32_t i02; |
| 641 | uint32_t i03; |
| 642 | }; |
| 643 | |
| 644 | static inline struct htp_tensor_row_coord init_row_coord( |
| 645 | uint32_t r, |
| 646 | uint32_t ne01, |
| 647 | uint32_t ne02, |
| 648 | const struct fastdiv_values * div_ne01, |
| 649 | const struct fastdiv_values * div_ne01_ne02 |
| 650 | ) { |
| 651 | uint32_t ne01_ne02 = ne01 * ne02; |
| 652 | uint32_t i03 = (ne01_ne02 > 0) ? fastdiv(r, div_ne01_ne02) : 0; |
| 653 | uint32_t rem = (ne01_ne02 > 0) ? (r - i03 * ne01_ne02) : 0; |
| 654 | uint32_t i02 = (ne01 > 0) ? fastdiv(rem, div_ne01) : 0; |
| 655 | uint32_t i01 = (ne01 > 0) ? (rem - i02 * ne01) : 0; |
| 656 | return (struct htp_tensor_row_coord){ i01, i02, i03 }; |
| 657 | } |
| 658 | |
| 659 | static inline void step_row_coord(struct htp_tensor_row_coord * c, uint32_t ne01, uint32_t ne02) { |
| 660 | c->i01++; |
| 661 | if (c->i01 == ne01) { |
| 662 | c->i01 = 0; |
| 663 | c->i02++; |
| 664 | if (c->i02 == ne02) { |
| 665 | c->i02 = 0; |
| 666 | c->i03++; |
| 667 | } |
| 668 | } |
| 669 | } |
| 670 | |
| 671 | static inline size_t coord_offset(struct htp_tensor_row_coord c, size_t nb1, size_t nb2, size_t nb3) { |
| 672 | return (size_t) c.i01 * nb1 + (size_t) c.i02 * nb2 + (size_t) c.i03 * nb3; |
| 673 | } |
| 674 | |
| 675 | static inline void compute_row_sort( |
| 676 | float * cur_values, |
| 677 | int32_t * cur_indices, |
| 678 | uint32_t ne00, |
| 679 | uint32_t chunk_elems, |
| 680 | bool is_top_k, |
| 681 | enum ggml_sort_order order, |
| 682 | uint32_t k |
| 683 | ) { |
| 684 | if (is_top_k) { |
| 685 | if (k > 1 && k <= 64 && ne00 > 1024) { |
| 686 | float out_v[64]; |
| 687 | top_k_scan(cur_values, ne00, k, out_v, cur_indices); |
| 688 | } else if (ne00 == 32 || ne00 == 64 || ne00 == 128 || ne00 == 256 || ne00 == 512 || ne00 == 1024) { |
| 689 | switch (ne00) { |
| 690 | case 32: sort32_f32_hvx((uint8_t *) cur_values, (uint8_t *) cur_indices, GGML_SORT_ORDER_DESC); break; |
| 691 | case 64: sort64_f32_hvx((uint8_t *) cur_values, (uint8_t *) cur_indices, GGML_SORT_ORDER_DESC); break; |
| 692 | case 128: sort128_f32_hvx((uint8_t *) cur_values, (uint8_t *) cur_indices, GGML_SORT_ORDER_DESC); break; |
| 693 | case 256: sort256_f32_hvx((uint8_t *) cur_values, (uint8_t *) cur_indices, GGML_SORT_ORDER_DESC); break; |
| 694 | case 512: sort512_f32_hvx((uint8_t *) cur_values, (uint8_t *) cur_indices, GGML_SORT_ORDER_DESC); break; |
| 695 | case 1024: sort1024_f32_hvx((uint8_t *) cur_values, (uint8_t *) cur_indices, GGML_SORT_ORDER_DESC); break; |
| 696 | } |
| 697 | } else { |
| 698 | if (chunk_elems > ne00) { |
| 699 | hvx_splat_f32_u((uint8_t *)(cur_values + ne00), -INFINITY, chunk_elems - ne00); |
| 700 | } |
| 701 | bitonic_sort_vtcm_desc((uint8_t *) cur_values, (uint8_t *) cur_indices, chunk_elems / 32, true); |
| 702 | } |
| 703 | } else { |
| 704 | if (ne00 == 32 || ne00 == 64 || ne00 == 128 || ne00 == 256 || ne00 == 512 || ne00 == 1024) { |
| 705 | switch (ne00) { |
| 706 | case 32: sort32_f32_hvx((uint8_t *) cur_values, (uint8_t *) cur_indices, order); break; |
| 707 | case 64: sort64_f32_hvx((uint8_t *) cur_values, (uint8_t *) cur_indices, order); break; |
| 708 | case 128: sort128_f32_hvx((uint8_t *) cur_values, (uint8_t *) cur_indices, order); break; |
| 709 | case 256: sort256_f32_hvx((uint8_t *) cur_values, (uint8_t *) cur_indices, order); break; |
| 710 | case 512: sort512_f32_hvx((uint8_t *) cur_values, (uint8_t *) cur_indices, order); break; |
| 711 | case 1024: sort1024_f32_hvx((uint8_t *) cur_values, (uint8_t *) cur_indices, order); break; |
| 712 | } |
| 713 | } else { |
| 714 | init_indices_ramp(cur_indices, ne00, 0); |
| 715 | if (order == GGML_SORT_ORDER_ASC) { |
| 716 | quicksort_values_indices_asc(cur_values, cur_indices, 0, ne00 - 1); |
| 717 | } else { |
| 718 | quicksort_values_indices_desc(cur_values, cur_indices, 0, ne00 - 1); |
| 719 | } |
| 720 | } |
| 721 | } |
| 722 | } |
| 723 | |
| 724 | static void htp_sort_multi_row_job(unsigned int n, unsigned int ith, void * data) { |
| 725 | struct htp_sort_multi_row_ctx * mctx = (struct htp_sort_multi_row_ctx *) data; |
| 726 | struct htp_ops_context * octx = mctx->octx; |
| 727 | const struct htp_sort_kernel_params * kparams = mctx->kparams; |
| 728 | const struct htp_tensor * src0 = octx->src[0]; |
| 729 | const struct htp_tensor * dst = octx->dst; |
| 730 | |
| 731 | uint32_t ne00 = src0->ne[0]; |
| 732 | uint32_t ne01 = src0->ne[1]; |
| 733 | uint32_t ne02 = src0->ne[2]; |
| 734 | |
| 735 | uint32_t nb01 = src0->nb[1]; |
| 736 | uint32_t nb02 = src0->nb[2]; |
| 737 | uint32_t nb03 = src0->nb[3]; |
| 738 | |
| 739 | uint32_t nb1 = dst->nb[1]; |
| 740 | uint32_t nb2 = dst->nb[2]; |
| 741 | uint32_t nb3 = dst->nb[3]; |
| 742 | |
| 743 | uint32_t row_start = mctx->row_start; |
| 744 | uint32_t row_end = mctx->row_end; |
| 745 | uint32_t r_start = row_start + ith * mctx->nrows_per_thread; |
| 746 | uint32_t r_end = MIN(r_start + mctx->nrows_per_thread, row_end); |
| 747 | |
| 748 | if (r_start >= r_end) return; |
| 749 | |
| 750 | size_t slot_size = mctx->phase1_slot_size; |
| 751 | size_t spad_offset = ith * slot_size * (size_t) kparams->n_slots; |
| 752 | uint8_t * thread_spad = mctx->phase1_base + spad_offset; |
| 753 | |
| 754 | uint32_t chunk_elems = (uint32_t) kparams->chunk_elems; |
| 755 | float * values_buf = (float *) thread_spad; |
| 756 | int32_t * indices_buf = (int32_t *) (thread_spad + chunk_elems * sizeof(float)); |
| 757 | |
| 758 | dma_queue * dma_q = octx->ctx->dma[ith]; |
| 759 | struct htp_thread_trace * tr = &octx->ctx->trace[ith]; |
| 760 | |
| 761 | bool is_top_k = kparams->is_top_k != 0; |
| 762 | uint32_t k = dst->ne[0]; |
| 763 | enum ggml_sort_order order = (enum ggml_sort_order) kparams->order; |
| 764 | |
| 765 | const uint32_t src_bytes = ne00 * sizeof(float); |
| 766 | const uint32_t dst_bytes = (is_top_k ? k : ne00) * sizeof(int32_t); |
| 767 | |
| 768 | if (kparams->n_slots == 2) { |
| 769 | struct htp_tensor_row_coord prime_coord = init_row_coord(r_start, ne01, ne02, &kparams->div_ne01, &kparams->div_ne01_ne02); |
| 770 | for (uint32_t r = r_start, spad_idx = 0; r < r_end && spad_idx < 2; r++, spad_idx++) { |
| 771 | uint8_t * cur_spad = thread_spad + spad_idx * slot_size; |
| 772 | float * cur_values = (float *) cur_spad; |
| 773 | int32_t * cur_indices = (int32_t *) (cur_spad + chunk_elems * sizeof(float)); |
| 774 | |
| 775 | dma_addr_t dst_addr = dst->data + coord_offset(prime_coord, nb1, nb2, nb3); |
| 776 | dma_addr_t src_addr = src0->data + coord_offset(prime_coord, nb01, nb02, nb03); |
| 777 | |
| 778 | // Dummy dst writeback to establish queue ordering |
| 779 | dma_queue_push(dma_q, dma_make_data(dst_addr, cur_indices), |
| 780 | dst_bytes, dst_bytes, dst_bytes, 0); |
| 781 | |
| 782 | // Prefetch input row |
| 783 | dma_queue_push(dma_q, dma_make_data(cur_values, src_addr), |
| 784 | src_bytes, src_bytes, src_bytes, 1); |
| 785 | |
| 786 | step_row_coord(&prime_coord, ne01, ne02); |
| 787 | } |
| 788 | |
| 789 | struct htp_tensor_row_coord dst_coord = init_row_coord(r_start, ne01, ne02, &kparams->div_ne01, &kparams->div_ne01_ne02); |
| 790 | struct htp_tensor_row_coord next_src_coord = init_row_coord(r_start + 2, ne01, ne02, &kparams->div_ne01, &kparams->div_ne01_ne02); |
| 791 | |
| 792 | for (uint32_t r = r_start; r < r_end; r++) { |
| 793 | uint32_t cur_slot = (r - r_start) & 1; |
| 794 | uint8_t * cur_spad = thread_spad + cur_slot * slot_size; |
| 795 | float * cur_values = (float *) cur_spad; |
| 796 | int32_t * cur_indices = (int32_t *) (cur_spad + chunk_elems * sizeof(float)); |
| 797 | |
| 798 | // Wait for previous writeback of this slot to complete |
| 799 | dma_queue_pop(dma_q); |
| 800 | |
| 801 | // Wait for input row DMA read into this slot to complete |
| 802 | dma_queue_pop(dma_q); |
| 803 | |
| 804 | htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) r); |
| 805 | compute_row_sort(cur_values, cur_indices, ne00, chunk_elems, is_top_k, order, k); |
| 806 | htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) r); |
| 807 | |
| 808 | // Push writeback of current slot |
| 809 | dma_addr_t dst_addr = dst->data + coord_offset(dst_coord, nb1, nb2, nb3); |
| 810 | dma_queue_push(dma_q, dma_make_data(dst_addr, cur_indices), |
| 811 | dst_bytes, dst_bytes, dst_bytes, 1); |
| 812 | step_row_coord(&dst_coord, ne01, ne02); |
| 813 | |
| 814 | // Prefetch next row into this slot |
| 815 | const uint32_t next_row = r + 2; |
| 816 | if (next_row < r_end) { |
| 817 | dma_addr_t next_src_addr = src0->data + coord_offset(next_src_coord, nb01, nb02, nb03); |
| 818 | dma_queue_push(dma_q, dma_make_data(cur_values, next_src_addr), |
| 819 | src_bytes, src_bytes, src_bytes, 1); |
| 820 | step_row_coord(&next_src_coord, ne01, ne02); |
| 821 | } |
| 822 | } |
| 823 | |
| 824 | dma_queue_flush(dma_q); |
| 825 | } else { |
| 826 | struct htp_tensor_row_coord coord = init_row_coord(r_start, ne01, ne02, &kparams->div_ne01, &kparams->div_ne01_ne02); |
| 827 | for (uint32_t r = r_start; r < r_end; r++) { |
| 828 | dma_addr_t src_addr = src0->data + coord_offset(coord, nb01, nb02, nb03); |
| 829 | dma_addr_t dst_addr = dst->data + coord_offset(coord, nb1, nb2, nb3); |
| 830 | |
| 831 | dma_sync_read(dma_q, values_buf, src_addr, src_bytes); |
| 832 | |
| 833 | htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) r); |
| 834 | compute_row_sort(values_buf, indices_buf, ne00, chunk_elems, is_top_k, order, k); |
| 835 | htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) r); |
| 836 | |
| 837 | dma_sync_write(dma_q, dst_addr, indices_buf, dst_bytes); |
| 838 | step_row_coord(&coord, ne01, ne02); |
| 839 | } |
| 840 | } |
| 841 | } |
| 842 | |
| 843 | static int op_sort_common(struct htp_ops_context * octx, bool is_top_k) { |
| 844 | if (octx->src[0]->type != HTP_TYPE_F32) { |
| 845 | return HTP_STATUS_NO_SUPPORT; |
| 846 | } |
| 847 | |
| 848 | const struct htp_tensor * src0 = octx->src[0]; |
| 849 | const struct htp_tensor * dst = octx->dst; |
| 850 | |
| 851 | if (htp_tensor_is_extended(src0) || htp_tensor_is_extended(dst)) { |
| 852 | return HTP_STATUS_NO_SUPPORT; |
| 853 | } |
| 854 | |
| 855 | const uint32_t total_rows = src0->ne[1] * src0->ne[2] * src0->ne[3]; |
| 856 | const size_t dst_row_size = dst->ne[0] * sizeof(int32_t); |
| 857 | |
| 858 | uint32_t row_start = 0; |
| 859 | uint32_t row_end = total_rows; |
| 860 | if (octx->ctx->mdev.count > 1) { |
| 861 | uint32_t rows_per_chunk = 0; |
| 862 | htp_tensor_mdev_rows_per_chunk(dst, sizeof(int32_t), (uint32_t) dst_row_size, &rows_per_chunk); |
| 863 | const struct htp_tensor_mdev_range range = htp_tensor_mdev_partition( |
| 864 | total_rows, rows_per_chunk, octx->ctx->mdev.idx, octx->ctx->mdev.count, &octx->ctx->mdev.count_div); |
| 865 | row_start = range.start; |
| 866 | row_end = range.start + range.count; |
| 867 | } |
| 868 | |
| 869 | const uint32_t nrows = row_end - row_start; |
| 870 | if (nrows == 0) { |
| 871 | return HTP_STATUS_OK; |
| 872 | } |
| 873 | |
| 874 | uint32_t ne00 = src0->ne[0]; |
| 875 | uint32_t k = dst->ne[0]; |
| 876 | |
| 877 | const struct htp_sort_kernel_params * kparams = (const struct htp_sort_kernel_params *) octx->kernel_params; |
| 878 | |
| 879 | if (!htp_ops_context_set_n_threads(octx, kparams->n_threads)) { |
| 880 | return HTP_STATUS_INVAL_PARAMS; |
| 881 | } |
| 882 | |
| 883 | struct htp_sort_vtcm_layout layout; |
| 884 | htp_sort_vtcm_layout_build(&layout, |
| 885 | total_rows, |
| 886 | (uint32_t) kparams->chunk_elems, |
| 887 | (uint32_t) kparams->n_chunks, |
| 888 | (uint32_t) kparams->merge_elems, |
| 889 | (uint32_t) kparams->n_threads, |
| 890 | (uint32_t) kparams->n_slots); |
| 891 | |
| 892 | if (octx->ctx->vtcm_size < layout.total_bytes) { |
| 893 | FARF(ERROR, "sort: VTCM size too small. Needed %zu, have %zu", layout.total_bytes, octx->ctx->vtcm_size); |
| 894 | return HTP_STATUS_VTCM_TOO_SMALL; |
| 895 | } |
| 896 | |
| 897 | uint8_t * vtcm_base = (uint8_t *) octx->ctx->vtcm_base; |
| 898 | uint8_t * phase1_base = VTCM_LAYOUT_PTR(uint8_t, vtcm_base, layout.off_phase1); |
| 899 | float * merge_values = VTCM_LAYOUT_PTR(float, vtcm_base, layout.off_merge_values); |
| 900 | int32_t * merge_indices = VTCM_LAYOUT_PTR(int32_t, vtcm_base, layout.off_merge_indices); |
| 901 | |
| 902 | if (total_rows == 1 && kparams->n_chunks > 1) { |
| 903 | struct htp_sort_chunk_ctx cctx; |
| 904 | cctx.octx = octx; |
| 905 | cctx.kparams = kparams; |
| 906 | cctx.phase1_base = phase1_base; |
| 907 | cctx.phase1_slot_size = layout.phase1_slot_size; |
| 908 | cctx.merge_values = merge_values; |
| 909 | cctx.merge_indices = merge_indices; |
| 910 | memset(cctx.real_count, 0, sizeof(cctx.real_count)); |
| 911 | |
| 912 | work_queue_run(octx->ctx->work_queue, htp_sort_chunk_job, &cctx, (uint32_t) kparams->n_chunks); |
| 913 | |
| 914 | if (is_top_k) { |
| 915 | uint32_t chunk_elems = (uint32_t) kparams->chunk_elems; |
| 916 | uint32_t local_k = MIN(k, chunk_elems); |
| 917 | |
| 918 | if (k == 1 && ne00 >= 128*1024) { |
| 919 | float * cand_vals = merge_values; |
| 920 | int32_t * cand_idxs = merge_indices; |
| 921 | float best_val = cand_vals[0]; |
| 922 | int32_t best_idx = cand_idxs[0]; |
| 923 | for (int32_t c = 1; c < kparams->n_chunks; c++) { |
| 924 | if (cand_vals[c] > best_val) { |
| 925 | best_val = cand_vals[c]; |
| 926 | best_idx = cand_idxs[c]; |
| 927 | } |
| 928 | } |
| 929 | int32_t * dst_ptr = (int32_t *) dst->data; |
| 930 | dst_ptr[0] = best_idx; |
| 931 | return HTP_STATUS_OK; |
| 932 | } |
| 933 | |
| 934 | uint32_t total_candidates = (uint32_t) kparams->n_chunks * local_k; |
| 935 | float * cand_vals = merge_values; |
| 936 | int32_t * cand_idxs = merge_indices; |
| 937 | uint32_t merge_elems = (uint32_t) kparams->merge_elems; |
| 938 | |
| 939 | if (merge_elems > total_candidates) { |
| 940 | hvx_splat_f32_u((uint8_t *) (cand_vals + total_candidates), -INFINITY, merge_elems - total_candidates); |
| 941 | for (uint32_t j = total_candidates; j < merge_elems; j++) { |
| 942 | cand_idxs[j] = 0; |
| 943 | } |
| 944 | } |
| 945 | |
| 946 | struct htp_thread_trace * tr = &octx->ctx->trace[0]; |
| 947 | htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_COMP, 0); |
| 948 | bitonic_sort_vtcm_desc((uint8_t *) cand_vals, (uint8_t *) cand_idxs, merge_elems / 32, false); |
| 949 | htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_COMP, 0); |
| 950 | dma_sync_write(octx->ctx->dma[0], dst->data, cand_idxs, k * sizeof(int32_t)); |
| 951 | } else { |
| 952 | bool asc = kparams->order == GGML_SORT_ORDER_ASC; |
| 953 | if (kparams->n_chunks == 8) { |
| 954 | struct htp_argsort_merge_l1_ctx l1ctx = { &cctx, asc }; |
| 955 | work_queue_run(octx->ctx->work_queue, htp_argsort_merge_l1_job, &l1ctx, 4); |
| 956 | |
| 957 | struct htp_argsort_merge_l2_ctx l2ctx = { &cctx, asc }; |
| 958 | work_queue_run(octx->ctx->work_queue, htp_argsort_merge_l2_job, &l2ctx, 2); |
| 959 | |
| 960 | size_t slot_size = layout.phase1_slot_size; |
| 961 | uint32_t chunk_elems = (uint32_t) kparams->chunk_elems; |
| 962 | |
| 963 | const float * val0 = (const float *) phase1_base; |
| 964 | const int32_t * idx0 = (const int32_t *) (phase1_base + 4 * chunk_elems * sizeof(float)); |
| 965 | const float * val1 = (const float *) (phase1_base + 4 * slot_size); |
| 966 | const int32_t * idx1 = (const int32_t *) (phase1_base + 4 * slot_size + 4 * chunk_elems * sizeof(float)); |
| 967 | |
| 968 | uint32_t n01 = cctx.real_count[0] + cctx.real_count[1] + cctx.real_count[2] + cctx.real_count[3]; |
| 969 | uint32_t n23 = cctx.real_count[4] + cctx.real_count[5] + cctx.real_count[6] + cctx.real_count[7]; |
| 970 | |
| 971 | int32_t * final_idx = merge_indices; |
| 972 | struct htp_thread_trace * tr = &octx->ctx->trace[0]; |
| 973 | htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_COMP, 0); |
| 974 | merge_runs(val0, idx0, n01, val1, idx1, n23, NULL, final_idx, asc); |
| 975 | htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_COMP, 0); |
| 976 | dma_sync_write(octx->ctx->dma[0], dst->data, final_idx, ne00 * sizeof(int32_t)); |
| 977 | } else if (kparams->n_chunks == 4) { |
| 978 | struct htp_argsort_merge_l1_ctx l1ctx = { &cctx, asc }; |
| 979 | work_queue_run(octx->ctx->work_queue, htp_argsort_merge_l1_job, &l1ctx, 2); |
| 980 | |
| 981 | uint32_t chunk_elems = (uint32_t) kparams->chunk_elems; |
| 982 | uint32_t n01 = cctx.real_count[0] + cctx.real_count[1]; |
| 983 | uint32_t n23 = cctx.real_count[2] + cctx.real_count[3]; |
| 984 | |
| 985 | const float * val01 = merge_values; |
| 986 | const int32_t * idx01 = merge_indices; |
| 987 | const float * val23 = merge_values + 2 * chunk_elems; |
| 988 | const int32_t * idx23 = merge_indices + 2 * chunk_elems; |
| 989 | |
| 990 | int32_t * final_idx = (int32_t *) phase1_base; |
| 991 | struct htp_thread_trace * tr = &octx->ctx->trace[0]; |
| 992 | htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_COMP, 0); |
| 993 | merge_runs(val01, idx01, n01, val23, idx23, n23, NULL, final_idx, asc); |
| 994 | htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_COMP, 0); |
| 995 | dma_sync_write(octx->ctx->dma[0], dst->data, final_idx, ne00 * sizeof(int32_t)); |
| 996 | } else if (kparams->n_chunks == 2) { |
| 997 | size_t slot_size = layout.phase1_slot_size; |
| 998 | uint32_t chunk_elems = (uint32_t) kparams->chunk_elems; |
| 999 | |
| 1000 | const float * val0 = (const float *) (phase1_base); |
| 1001 | const int32_t * idx0 = (const int32_t *) (phase1_base + chunk_elems * sizeof(float)); |
| 1002 | const float * val1 = (const float *) (phase1_base + slot_size); |
| 1003 | const int32_t * idx1 = (const int32_t *) (phase1_base + slot_size + chunk_elems * sizeof(float)); |
| 1004 | |
| 1005 | int32_t * final_idx = merge_indices; |
| 1006 | struct htp_thread_trace * tr = &octx->ctx->trace[0]; |
| 1007 | htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_COMP, 0); |
| 1008 | merge_runs(val0, idx0, cctx.real_count[0], val1, idx1, cctx.real_count[1], NULL, final_idx, asc); |
| 1009 | htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_COMP, 0); |
| 1010 | dma_sync_write(octx->ctx->dma[0], dst->data, final_idx, ne00 * sizeof(int32_t)); |
| 1011 | } else { |
| 1012 | uint32_t chunk_elems = (uint32_t) kparams->chunk_elems; |
| 1013 | int32_t * final_idx = (int32_t *) (phase1_base + chunk_elems * sizeof(float)); |
| 1014 | dma_sync_write(octx->ctx->dma[0], dst->data, final_idx, ne00 * sizeof(int32_t)); |
| 1015 | } |
| 1016 | } |
| 1017 | return HTP_STATUS_OK; |
| 1018 | } |
| 1019 | |
| 1020 | struct htp_sort_multi_row_ctx mctx; |
| 1021 | mctx.octx = octx; |
| 1022 | mctx.kparams = kparams; |
| 1023 | mctx.phase1_base = phase1_base; |
| 1024 | mctx.phase1_slot_size = layout.phase1_slot_size; |
| 1025 | mctx.row_start = row_start; |
| 1026 | mctx.row_end = row_end; |
| 1027 | |
| 1028 | uint32_t dst_row_bytes = (uint32_t) dst_row_size; |
| 1029 | uint32_t rows_per_chunk = 0; |
| 1030 | htp_tensor_mdev_rows_per_chunk(dst, sizeof(int32_t), dst_row_bytes, &rows_per_chunk); |
| 1031 | if (rows_per_chunk == 0) { |
| 1032 | rows_per_chunk = 1; |
| 1033 | } |
| 1034 | uint32_t dr = fastdiv(nrows + octx->n_threads - 1, &octx->n_threads_div); |
| 1035 | if (rows_per_chunk > 1) { |
| 1036 | dr = hex_round_up(dr, rows_per_chunk); |
| 1037 | } |
| 1038 | mctx.nrows_per_thread = dr; |
| 1039 | |
| 1040 | work_queue_run(octx->ctx->work_queue, htp_sort_multi_row_job, &mctx, octx->n_threads); |
| 1041 | |
| 1042 | return HTP_STATUS_OK; |
| 1043 | } |
| 1044 | |
| 1045 | int op_argsort(struct htp_ops_context * octx) { |
| 1046 | return op_sort_common(octx, false); |
| 1047 | } |
| 1048 | |
| 1049 | int op_top_k(struct htp_ops_context * octx) { |
| 1050 | return op_sort_common(octx, true); |
| 1051 | } |