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/argsort-ops.h # 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 / argsort-ops.h` 163 lines ยท 5200 bytes Raw Blame History 1 #ifndef HTP_ARGSORT_OPS_H 2 #define HTP_ARGSORT_OPS_H 3 4 #include 5 #include 6 #include 7 #include 8 9 #include "hex-fastdiv.h" 10 #include "htp-vtcm.h" 11 12 struct htp_sort_kernel_params { 13 uint32_t n_threads; 14 uint32_t order; // GGML_SORT_ORDER_ASC (0) or GGML_SORT_ORDER_DESC (1) 15 uint32_t is_top_k; // 1 if TOP_K, 0 if ARGSORT 16 uint32_t chunk_elems; 17 uint32_t n_chunks; 18 uint32_t vtcm_size; 19 uint32_t merge_elems; 20 uint32_t n_slots; 21 struct fastdiv_values div_ne01; 22 struct fastdiv_values div_ne01_ne02; 23 }; 24 25 struct htp_sort_vtcm_layout { 26 size_t total_bytes; 27 size_t phase1_slot_size; 28 size_t off_phase1; 29 size_t off_merge_values; 30 size_t off_merge_indices; 31 uint32_t chunk_elems; 32 uint32_t n_chunks; 33 uint32_t merge_elems; 34 uint32_t n_threads; 35 uint32_t n_slots; 36 }; 37 38 static inline void htp_sort_vtcm_layout_build( 39 struct htp_sort_vtcm_layout * L, 40 uint32_t total_rows, 41 uint32_t chunk_elems, 42 uint32_t n_chunks, 43 uint32_t merge_elems, 44 uint32_t n_threads, 45 uint32_t n_slots) { 46 47 memset(L, 0, sizeof(*L)); 48 L->chunk_elems = chunk_elems; 49 L->n_chunks = n_chunks; 50 L->merge_elems = merge_elems; 51 L->n_threads = n_threads; 52 L->n_slots = n_slots; 53 54 const size_t values_size = ((chunk_elems * sizeof(float)) + 127) & ~127; 55 const size_t indices_size = ((chunk_elems * sizeof(int32_t)) + 127) & ~127; 56 L->phase1_slot_size = ((values_size + indices_size) + 255) & ~255; 57 58 size_t off = 0; 59 if (total_rows > 1) { 60 VTCM_LAYOUT_ALLOC(off, off_phase1, L->phase1_slot_size * n_slots * n_threads); 61 } else { 62 VTCM_LAYOUT_ALLOC(off, off_phase1, L->phase1_slot_size * n_chunks * n_slots); 63 const size_t merge_values_size = ((merge_elems * sizeof(float)) + 127) & ~127; 64 const size_t merge_indices_size = ((merge_elems * sizeof(int32_t)) + 127) & ~127; 65 VTCM_LAYOUT_ALLOC_OPTIONAL(off, off_merge_values, merge_values_size, n_chunks > 1); 66 VTCM_LAYOUT_ALLOC_OPTIONAL(off, off_merge_indices, merge_indices_size, n_chunks > 1); 67 } 68 L->total_bytes = off; 69 } 70 71 static inline bool htp_sort_solve_layout( 72 struct htp_sort_vtcm_layout * layout, 73 uint32_t ne00, 74 uint32_t total_rows, 75 uint32_t k, 76 uint32_t n_threads_max, 77 size_t vtcm_budget, 78 bool is_top_k) { 79 80 memset(layout, 0, sizeof(*layout)); 81 82 if (total_rows > 1) { 83 uint32_t n_threads = total_rows < n_threads_max ? total_rows : n_threads_max; 84 85 uint32_t n_vec = (ne00 + 31) / 32; 86 uint32_t n_vec_pow2 = 1; 87 while (n_vec_pow2 < n_vec) n_vec_pow2 <<= 1; 88 uint32_t ne00_padded = n_vec_pow2 * 32; 89 90 size_t values_size = ((ne00_padded * sizeof(float)) + 127) & ~127; 91 size_t indices_size = ((ne00_padded * sizeof(int32_t)) + 127) & ~127; 92 size_t spad_per_slot = ((values_size + indices_size) + 255) & ~255; 93 94 uint32_t n_slots = 2; 95 if (spad_per_slot * 2 > vtcm_budget) { 96 n_slots = 1; 97 } 98 99 size_t spad_per_thread = spad_per_slot * n_slots; 100 101 while (n_threads > 1 && (spad_per_thread * n_threads) > vtcm_budget) { 102 n_threads--; 103 } 104 105 htp_sort_vtcm_layout_build(layout, total_rows, ne00_padded, 1, 0, n_threads, n_slots); 106 return layout->total_bytes <= vtcm_budget; 107 } 108 109 uint32_t n_vec = (ne00 + 31) / 32; 110 uint32_t n_vec_pow2 = 1; 111 while (n_vec_pow2 < n_vec) n_vec_pow2 <<= 1; 112 113 uint32_t n_chunks = 1; 114 if (ne00 > 1024) { 115 while (n_chunks * 2 <= n_threads_max && n_chunks * 2 <= n_vec_pow2) { 116 n_chunks *= 2; 117 } 118 } 119 120 uint32_t chunk_n_vec = n_vec_pow2 / n_chunks; 121 uint32_t chunk_elems = chunk_n_vec * 32; 122 uint32_t merge_elems = 0; 123 124 if (n_chunks > 1) { 125 if (is_top_k) { 126 uint32_t local_k = k < chunk_elems ? k : chunk_elems; 127 uint32_t total_candidates = n_chunks * local_k; 128 uint32_t merge_n_vec = (total_candidates + 31) / 32; 129 uint32_t merge_n_vec_pow2 = 1; 130 while (merge_n_vec_pow2 < merge_n_vec) merge_n_vec_pow2 <<= 1; 131 merge_elems = merge_n_vec_pow2 * 32; 132 } else { 133 merge_elems = n_vec_pow2 * 32; 134 } 135 } 136 137 htp_sort_vtcm_layout_build(layout, 1, chunk_elems, n_chunks, merge_elems, n_chunks, 1); 138 139 if (layout->total_bytes > vtcm_budget && n_chunks > 1) { 140 n_chunks = 1; 141 chunk_elems = n_vec_pow2 * 32; 142 merge_elems = 0; 143 htp_sort_vtcm_layout_build(layout, 1, chunk_elems, n_chunks, merge_elems, 1, 1); 144 } 145 146 if (layout->total_bytes > vtcm_budget) { 147 return false; 148 } 149 150 if (n_chunks == 1 && layout->phase1_slot_size * 2 <= vtcm_budget) { 151 htp_sort_vtcm_layout_build(layout, 1, chunk_elems, 1, 0, 1, 2); 152 } 153 154 return true; 155 } 156 157 #if defined(__cplusplus) 158 static_assert(sizeof(struct htp_sort_kernel_params) <= 128, "htp_sort_kernel_params is too large for kernel_params blob"); 159 #else 160 _Static_assert(sizeof(struct htp_sort_kernel_params) <= 128, "htp_sort_kernel_params is too large for kernel_params blob"); 161 #endif 162 163 #endif // HTP_ARGSORT_OPS_H