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/unary-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 / unary-ops.h` 215 lines ยท 7450 bytes Raw Blame History 1 #ifndef HTP_UNARY_OPS_H 2 #define HTP_UNARY_OPS_H 3 4 #include "hex-common.h" 5 #include "hex-fastdiv.h" 6 #include "htp-ops.h" 7 #include "htp-vtcm.h" 8 9 // Op-specific struct for precomputed unary params 10 struct htp_unary_kernel_params { 11 uint32_t n_threads; 12 uint32_t col_tile; 13 uint32_t block; 14 uint32_t broadcast_weight; 15 16 uint32_t src0_contig; 17 uint32_t src1_contig; 18 19 uint32_t vtcm_size; 20 uint32_t tiles_per_row; 21 22 // Fastdiv helpers 23 struct fastdiv_values div_ne01; 24 struct fastdiv_values div_ne02; 25 struct fastdiv_values div_ne012; 26 struct fastdiv_values div_tpr; 27 }; 28 29 #if defined(__cplusplus) 30 static_assert(sizeof(struct htp_unary_kernel_params) <= 128, "htp_unary_kernel_params is too large for kernel_params blob"); 31 #else 32 _Static_assert(sizeof(struct htp_unary_kernel_params) <= 128, "htp_unary_kernel_params is too large for kernel_params blob"); 33 #endif 34 35 static inline bool htp_op_is_unary(uint32_t opcode) { 36 switch (opcode) { 37 case HTP_OP_CLAMP: 38 case HTP_OP_LEAKY_RELU: 39 case HTP_OP_NORM: 40 case HTP_OP_RMS_NORM: 41 case HTP_OP_RMS_NORM_MUL: 42 case HTP_OP_SCALE: 43 case HTP_OP_SQR: 44 case HTP_OP_SQRT: 45 case HTP_OP_UNARY_NEG: 46 case HTP_OP_UNARY_EXP: 47 case HTP_OP_UNARY_SIGMOID: 48 case HTP_OP_UNARY_SILU: 49 case HTP_OP_UNARY_GELU: 50 case HTP_OP_UNARY_GELU_QUICK: 51 case HTP_OP_UNARY_GELU_ERF: 52 case HTP_OP_UNARY_SOFTPLUS: 53 case HTP_OP_UNARY_TANH: 54 case HTP_OP_UNARY_ABS: 55 case HTP_OP_UNARY_LOG: 56 case HTP_OP_UNARY_RELU: 57 case HTP_OP_UNARY_STEP: 58 case HTP_OP_UNARY_CEIL: 59 case HTP_OP_UNARY_SGN: 60 case HTP_OP_L2_NORM: 61 case HTP_OP_TRI: 62 case HTP_OP_HADAMARD: 63 return true; 64 default: 65 return false; 66 } 67 } 68 69 static inline bool htp_unary_is_elementwise(uint32_t opcode) { 70 if (!htp_op_is_unary(opcode)) { 71 return false; 72 } 73 switch (opcode) { 74 case HTP_OP_NORM: 75 case HTP_OP_RMS_NORM: 76 case HTP_OP_RMS_NORM_MUL: 77 case HTP_OP_L2_NORM: 78 case HTP_OP_HADAMARD: 79 case HTP_OP_TRI: 80 return false; 81 default: 82 return true; 83 } 84 } 85 86 struct htp_unary_vtcm_layout { 87 size_t total_bytes; 88 size_t off_src0; 89 size_t off_src1; 90 size_t off_dst; 91 92 size_t src0_row_size_aligned; 93 size_t src1_row_size_aligned; 94 size_t dst_row_size_aligned; 95 96 size_t src0_bytes; 97 size_t src1_bytes; 98 size_t dst_bytes; 99 }; 100 101 static inline void htp_unary_vtcm_layout_build( 102 struct htp_unary_vtcm_layout * L, 103 uint32_t op, 104 size_t src0_data_row_size, 105 size_t dst_data_row_size, 106 size_t src1_data_row_size, 107 size_t elem_size, 108 uint32_t col_tile, 109 uint32_t block, 110 bool broadcast_weight, 111 uint32_t n_threads 112 ) { 113 L->src0_row_size_aligned = hex_round_up(src0_data_row_size, 128); 114 L->dst_row_size_aligned = hex_round_up(dst_data_row_size, 128); 115 L->src1_row_size_aligned = (op == HTP_OP_RMS_NORM_MUL) ? hex_round_up(src1_data_row_size, 128) : 0; 116 117 if (col_tile > 0) { 118 const size_t col_tile_bytes = col_tile * elem_size; 119 L->src0_bytes = col_tile_bytes * 2; 120 L->dst_bytes = col_tile_bytes * 2; 121 L->src1_bytes = 0; 122 } else { 123 L->src0_bytes = L->src0_row_size_aligned * block * 2; 124 L->dst_bytes = L->dst_row_size_aligned * block * 2; 125 if (op == HTP_OP_RMS_NORM_MUL) { 126 L->src1_bytes = broadcast_weight ? L->src1_row_size_aligned : (L->src1_row_size_aligned * block * 2); 127 } else { 128 L->src1_bytes = 0; 129 } 130 } 131 132 size_t off = 0; 133 VTCM_LAYOUT_ALLOC(off, off_src0, L->src0_bytes * n_threads); 134 VTCM_LAYOUT_ALLOC_OPTIONAL(off, off_src1, L->src1_bytes * n_threads, L->src1_bytes > 0); 135 VTCM_LAYOUT_ALLOC(off, off_dst, L->dst_bytes * n_threads); 136 L->total_bytes = off; 137 } 138 139 static inline void htp_unary_solve_layout( 140 struct htp_unary_vtcm_layout * L, 141 uint32_t op, 142 uint32_t ne00, 143 uint32_t ne10, 144 uint32_t ne11, 145 uint32_t max_block, 146 bool broadcast_weight, 147 uint32_t n_threads, 148 size_t vtcm_size, 149 size_t elem_size, 150 uint32_t * out_col_tile, 151 uint32_t * out_vtcm_row_per_thread 152 ) { 153 const size_t dst_elem_size = (op == HTP_OP_HADAMARD) ? sizeof(float) : elem_size; 154 const size_t src0_data_row_size = ne00 * elem_size; 155 const size_t dst_data_row_size = ne10 * dst_elem_size; 156 const size_t src1_data_row_size = (op == HTP_OP_RMS_NORM_MUL) ? (ne11 * sizeof(float)) : 0; 157 158 const size_t src0_row_size_aligned = hex_round_up(src0_data_row_size, 128); 159 const size_t dst_row_size_aligned = hex_round_up(dst_data_row_size, 128); 160 const size_t src1_row_size_aligned = (op == HTP_OP_RMS_NORM_MUL) ? hex_round_up(src1_data_row_size, 128) : 0; 161 162 size_t vtcm_size_per_row = 0; 163 size_t vtcm_row_per_thread = 0; 164 165 if (op == HTP_OP_RMS_NORM_MUL) { 166 if (broadcast_weight) { 167 size_t available_vtcm = vtcm_size; 168 size_t src1_vtcm_total = n_threads * src1_row_size_aligned; 169 if (available_vtcm > src1_vtcm_total) { 170 available_vtcm -= src1_vtcm_total; 171 } else { 172 available_vtcm = 0; 173 } 174 vtcm_size_per_row = 2 * (src0_row_size_aligned + dst_row_size_aligned); 175 vtcm_row_per_thread = available_vtcm / (n_threads * vtcm_size_per_row); 176 } else { 177 vtcm_size_per_row = 2 * (src0_row_size_aligned + dst_row_size_aligned + src1_row_size_aligned); 178 vtcm_row_per_thread = vtcm_size / (n_threads * vtcm_size_per_row); 179 } 180 } else { 181 vtcm_size_per_row = 2 * (src0_row_size_aligned + dst_row_size_aligned); 182 vtcm_row_per_thread = vtcm_size / (n_threads * vtcm_size_per_row); 183 } 184 185 if (max_block > 0 && vtcm_row_per_thread > max_block) { 186 vtcm_row_per_thread = max_block; 187 } 188 189 const bool is_reduction = (op == HTP_OP_NORM || op == HTP_OP_RMS_NORM || 190 op == HTP_OP_RMS_NORM_MUL || op == HTP_OP_L2_NORM || 191 op == HTP_OP_HADAMARD); 192 // The tiled fallback path below only has F32 task functions (unary_task_f32_tiled_*); 193 // F16 has no tiled kernels, so it must stay on the row-block path like reduction ops. 194 // NOTE: if F16 ends up with vtcm_row_per_thread == 0 here (row too large for the VTCM 195 // budget), execute_op_unary() will see BLOCK == 0 and skip computation for that op 196 // (logged via FARF(ERROR, ...)) since there is no F16 tiled fallback. This is a known 197 // limitation; supporting it would require adding F16 tiled kernels. 198 const bool is_f16 = (elem_size == sizeof(_Float16)); 199 uint32_t col_tile = 0; 200 201 if (vtcm_row_per_thread == 0 && !is_reduction && !is_f16) { 202 const size_t per_thread_budget = vtcm_size / n_threads; 203 const size_t col_tile_bytes = hex_align_down(per_thread_budget / 4, 128); 204 col_tile = (uint32_t) (col_tile_bytes / elem_size); 205 } 206 207 const uint32_t block = col_tile ? 0 : (uint32_t) vtcm_row_per_thread; 208 htp_unary_vtcm_layout_build(L, op, src0_data_row_size, dst_data_row_size, src1_data_row_size, 209 elem_size, col_tile, block, broadcast_weight, n_threads); 210 211 *out_col_tile = col_tile; 212 *out_vtcm_row_per_thread = (uint32_t) vtcm_row_per_thread; 213 } 214 215 #endif /* HTP_UNARY_OPS_H */