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-opnode.cpp
| 1 | #include "htp-opnode.h" |
| 2 | |
| 3 | #include "htp/allreduce-ops.h" |
| 4 | #include "htp/argsort-ops.h" |
| 5 | #include "htp/binary-ops.h" |
| 6 | #include "htp/cumsum-ops.h" |
| 7 | #include "htp/diag-ops.h" |
| 8 | #include "htp/dsv4-hc-ops.h" |
| 9 | #include "htp/fill-ops.h" |
| 10 | #include "htp/flash-attn-ops.h" |
| 11 | #include "htp/gated-delta-net-ops.h" |
| 12 | #include "htp/get-rows-ops.h" |
| 13 | #include "htp/glu-ops.h" |
| 14 | #include "htp/lightning-indexer-ops.h" |
| 15 | #include "htp/matmul-ops.h" |
| 16 | #include "htp/pad-ops.h" |
| 17 | #include "htp/repeat-ops.h" |
| 18 | #include "htp/roll-ops.h" |
| 19 | #include "htp/softmax-ops.h" |
| 20 | #include "htp/solve-tri-ops.h" |
| 21 | #include "htp/ssm-conv.h" |
| 22 | #include "htp/sum-rows-ops.h" |
| 23 | #include "htp/top-k-mask-ops.h" |
| 24 | #include "htp/unary-ops.h" |
| 25 | #include "htp/upscale-ops.h" |
| 26 | |
| 27 | #include <stdio.h> |
| 28 | |
| 29 | std::string htp_opnode::name() const { |
| 30 | switch (opcode) { |
| 31 | case HTP_OP_MDEV_GROUP: return "MDEV_GROUP"; |
| 32 | case HTP_OP_CPY_FENCE: return "CPY+FENCE"; |
| 33 | case HTP_OP_FENCE: return (node && node->op_params[1] == 0) ? "FENCE_WAIT" : "FENCE_SIGNAL"; |
| 34 | case HTP_OP_ALLREDUCE: return "ALLREDUCE"; |
| 35 | case HTP_OP_ALLREDUCE_ADD: return "ALLREDUCE+ADD"; |
| 36 | case HTP_OP_HADAMARD: return "MUL_MAT_HADAMARD"; |
| 37 | case HTP_OP_MUL_MAT_NX: return "MUL_MAT_NX"; |
| 38 | case HTP_OP_MUL_MAT_ID_NX: return "MUL_MAT_ID_NX"; |
| 39 | case HTP_OP_ADD: |
| 40 | case HTP_OP_SUB: |
| 41 | case HTP_OP_MUL: |
| 42 | case HTP_OP_DIV: |
| 43 | if (!fused.empty()) { |
| 44 | const auto * kp = kparams<htp_binary_kernel_params>(); |
| 45 | if (kp->kernel_type != HTP_BINARY_KERNEL_SCALAR) { |
| 46 | switch (opcode) { |
| 47 | case HTP_OP_ADD: return "ADD_NX"; |
| 48 | case HTP_OP_SUB: return "SUB_NX"; |
| 49 | case HTP_OP_MUL: return "MUL_NX"; |
| 50 | case HTP_OP_DIV: return "DIV_NX"; |
| 51 | default: break; |
| 52 | } |
| 53 | } |
| 54 | } |
| 55 | break; |
| 56 | default: |
| 57 | break; |
| 58 | } |
| 59 | |
| 60 | if (!node) { |
| 61 | return ""; |
| 62 | } |
| 63 | if (htp_is_hadamard(node)) { |
| 64 | return "MUL_MAT_HADAMARD"; |
| 65 | } |
| 66 | |
| 67 | std::string res = ggml_op_desc(node); |
| 68 | for (const auto * t : fused) { |
| 69 | res += "+"; |
| 70 | res += ggml_op_desc(t); |
| 71 | } |
| 72 | return res; |
| 73 | } |
| 74 | |
| 75 | static const char * format_tensor_name(const struct ggml_tensor * t) { |
| 76 | return t ? t->name : "NONE"; |
| 77 | } |
| 78 | |
| 79 | static std::string format_tensor_dims(const struct ggml_tensor * t) { |
| 80 | if (!t) { |
| 81 | return "NONE"; |
| 82 | } |
| 83 | char buf[64]; |
| 84 | if (t->ne[2] == 1 && t->ne[3] == 1) { |
| 85 | snprintf(buf, sizeof(buf), "%d:%d", (int) t->ne[0], (int) t->ne[1]); |
| 86 | } else { |
| 87 | snprintf(buf, sizeof(buf), "%d:%d:%d:%d", (int) t->ne[0], (int) t->ne[1], (int) t->ne[2], (int) t->ne[3]); |
| 88 | } |
| 89 | return buf; |
| 90 | } |
| 91 | |
| 92 | static const char * format_tensor_type(const struct ggml_tensor * t) { |
| 93 | return t ? ggml_type_name(t->type) : "NONE"; |
| 94 | } |
| 95 | |
| 96 | static std::string format_tensor_strides(const struct ggml_tensor * t) { |
| 97 | if (!t) { |
| 98 | return "NONE"; |
| 99 | } |
| 100 | char buf[64]; |
| 101 | const char * c = ggml_is_contiguous(t) ? "" : "!"; |
| 102 | if (t->ne[2] == 1 && t->ne[3] == 1) { |
| 103 | snprintf(buf, sizeof(buf), "%zu:%zu%s", (size_t) t->nb[0], (size_t) t->nb[1], c); |
| 104 | } else { |
| 105 | snprintf(buf, sizeof(buf), "%zu:%zu:%zu:%zu%s", (size_t) t->nb[0], (size_t) t->nb[1], (size_t) t->nb[2], (size_t) t->nb[3], c); |
| 106 | } |
| 107 | return buf; |
| 108 | } |
| 109 | |
| 110 | static const char * format_tensor_buff(const struct ggml_tensor * t) { |
| 111 | if (t && t->buffer) { |
| 112 | return ggml_backend_buffer_name(t->buffer); |
| 113 | } |
| 114 | return "NONE"; |
| 115 | } |
| 116 | |
| 117 | template <typename Fn> |
| 118 | static std::string format_op_tensors(const htp_opnode & node, Fn fn) { |
| 119 | std::string res; |
| 120 | const auto & inputs = node.get_inputs(); |
| 121 | const auto & outputs = node.get_outputs(); |
| 122 | |
| 123 | if (!inputs.empty()) { |
| 124 | for (size_t i = 0; i < inputs.size(); i++) { |
| 125 | if (i > 0) res += " x "; |
| 126 | res += fn(inputs[i]); |
| 127 | } |
| 128 | res += " -> "; |
| 129 | } |
| 130 | |
| 131 | if (outputs.empty()) { |
| 132 | res += fn(node.dst()); |
| 133 | } else { |
| 134 | for (size_t i = 0; i < outputs.size(); i++) { |
| 135 | if (i > 0) res += " x "; |
| 136 | res += fn(outputs[i]); |
| 137 | } |
| 138 | } |
| 139 | return res; |
| 140 | } |
| 141 | |
| 142 | static std::string format_kernel_params(const htp_opnode & node) { |
| 143 | char str[128]; |
| 144 | const size_t max_size = sizeof(str); |
| 145 | |
| 146 | if (node.opcode == HTP_OP_MUL_MAT || node.opcode == HTP_OP_MUL_MAT_ID || |
| 147 | node.opcode == HTP_OP_MUL_MAT_NX || node.opcode == HTP_OP_MUL_MAT_ID_NX || |
| 148 | node.opcode == HTP_OP_MUL_MAT_ADD) { |
| 149 | const auto * kparams = node.kparams<htp_mm_kernel_params>(); |
| 150 | const char * path = "unknown"; |
| 151 | int32_t type = kparams->kernel_type; |
| 152 | if (type == HTP_MM_KERNEL_HMX_2D || type == HTP_MM_KERNEL_HMX_BATCHED) { |
| 153 | path = (kparams->tile_size > 0) ? "hmx-tiled" : "hmx-flat"; |
| 154 | } else if (type == HTP_MM_KERNEL_HVX_F16_F16_VTCM || type == HTP_MM_KERNEL_HVX_F32_F32_VTCM || |
| 155 | type == HTP_MM_KERNEL_HVX_BF16_F32_VTCM) { |
| 156 | path = "hvx-flat"; |
| 157 | } else if (type == HTP_MM_KERNEL_HVX_QUANT_ROW || type == HTP_MM_KERNEL_HVX_QUANT_BLOCK) { |
| 158 | path = "hvx-tiled"; |
| 159 | } |
| 160 | snprintf(str, max_size, "%s vtcm %d", path, (int) kparams->vtcm_size); |
| 161 | } else if (node.opcode == HTP_OP_FLASH_ATTN_EXT) { |
| 162 | const auto * kparams = node.kparams<htp_fa_kernel_params>(); |
| 163 | const char * path = "unknown"; |
| 164 | int32_t type = kparams->kernel_type; |
| 165 | if (type == HTP_FA_KERNEL_HMX) { |
| 166 | path = kparams->u.hmx.pipeline ? "hmx-pipe" : "hmx-seq"; |
| 167 | } else if (type == HTP_FA_KERNEL_HVX) { |
| 168 | path = "hvx"; |
| 169 | } |
| 170 | snprintf(str, max_size, "%s vtcm %d", path, (int) kparams->vtcm_size); |
| 171 | } else if (htp_op_is_unary(node.opcode)) { |
| 172 | const auto * kparams = node.kparams<htp_unary_kernel_params>(); |
| 173 | snprintf(str, max_size, "%s vtcm %d", kparams->col_tile ? "wide-row" : "row-block", (int) kparams->vtcm_size); |
| 174 | } else if (node.opcode == HTP_OP_MDEV_GROUP && node.node) { |
| 175 | snprintf(str, max_size, "idx %d count %d", (int) node.node->op_params[0], (int) node.dst()->ne[1]); |
| 176 | } else if ((node.opcode == HTP_OP_FENCE || node.opcode == HTP_OP_CPY_FENCE) && node.node) { |
| 177 | snprintf(str, max_size, "seq 0x%x", (uint32_t) node.node->op_params[0]); |
| 178 | } else if (node.opcode == HTP_OP_ALLREDUCE && node.node) { |
| 179 | snprintf(str, max_size, "seq 0x%x -> 0x%x", (uint32_t) node.node->op_params[0], (uint32_t) node.node->op_params[1]); |
| 180 | } else if (node.opcode == HTP_OP_SSM_CONV) { |
| 181 | const auto * kparams = node.kparams<htp_ssm_conv_kernel_params>(); |
| 182 | snprintf(str, max_size, "%s vtcm %d", kparams->n_t == 1 ? "decode" : "prefill", (int) kparams->vtcm_size); |
| 183 | } else if (node.opcode == HTP_OP_SOFTMAX) { |
| 184 | const auto * kparams = node.kparams<htp_softmax_kernel_params>(); |
| 185 | snprintf(str, max_size, "k%d nth %d vtcm %d", (int) kparams->kernel_type, (int) kparams->n_threads, (int) kparams->vtcm_size); |
| 186 | } else if (node.opcode == HTP_OP_GATED_DELTA_NET) { |
| 187 | const auto * kparams = node.kparams<htp_gdn_kernel_params>(); |
| 188 | const char * path = (kparams->kernel_type == HTP_GDN_KERNEL_HMX_CHUNKED) ? "hmx-chunked" : "hvx-recurrent"; |
| 189 | snprintf(str, max_size, "%s-%s vtcm %u", path, kparams->kda ? "kda" : "scalar", |
| 190 | (unsigned int) kparams->vtcm_size); |
| 191 | } else if (node.opcode == HTP_OP_DSV4_HC_PRE) { |
| 192 | const auto * kparams = node.kparams<htp_dsv4_hc_pre_kernel_params>(); |
| 193 | const char * ktype_str = kparams->kernel_type == HTP_DSV4_HC_PRE_KERNEL_GATED ? "gated" : "plain"; |
| 194 | snprintf(str, max_size, "%s nth %u vtcm %u", ktype_str, |
| 195 | (unsigned int) kparams->n_threads, (unsigned int) kparams->vtcm_size); |
| 196 | } else if (node.opcode == HTP_OP_DSV4_HC_POST) { |
| 197 | const auto * kparams = node.kparams<htp_dsv4_hc_post_kernel_params>(); |
| 198 | const char * ktype_str = kparams->kernel_type == HTP_DSV4_HC_POST_KERNEL_COMB ? "comb" : "identity"; |
| 199 | snprintf(str, max_size, "%s nth %u vtcm %u", ktype_str, |
| 200 | (unsigned int) kparams->n_threads, (unsigned int) kparams->vtcm_size); |
| 201 | } else if (node.opcode == HTP_OP_LIGHTNING_INDEXER) { |
| 202 | const auto * kparams = node.kparams<htp_lid_kernel_params>(); |
| 203 | snprintf(str, max_size, "%s tok %u bc %u nth %u vtcm %u", |
| 204 | kparams->kernel_type == HTP_LID_KERNEL_HMX ? "hmx" : "hvx", |
| 205 | (unsigned int) kparams->n_tokens_tile, (unsigned int) kparams->kv_block, |
| 206 | (unsigned int) kparams->n_threads, (unsigned int) kparams->vtcm_size); |
| 207 | } else if (node.opcode == HTP_OP_MUL || node.opcode == HTP_OP_ADD || node.opcode == HTP_OP_ADD_ID || |
| 208 | node.opcode == HTP_OP_SUB || node.opcode == HTP_OP_DIV) { |
| 209 | const auto * kparams = node.kparams<htp_binary_kernel_params>(); |
| 210 | snprintf(str, max_size, "vtcm %u", (unsigned int) kparams->vtcm_size); |
| 211 | } else if (node.opcode == HTP_OP_ARGSORT || node.opcode == HTP_OP_TOP_K) { |
| 212 | const auto * kparams = node.kparams<htp_sort_kernel_params>(); |
| 213 | snprintf(str, max_size, "%s nth %d nchk %d chk %d vtcm %d", |
| 214 | node.opcode == HTP_OP_TOP_K ? "top_k" : "argsort", |
| 215 | (int) kparams->n_threads, (int) kparams->n_chunks, |
| 216 | (int) kparams->chunk_elems, (int) kparams->vtcm_size); |
| 217 | } else if (node.opcode == HTP_OP_GET_ROWS) { |
| 218 | const auto * kparams = node.kparams<htp_get_rows_kernel_params>(); |
| 219 | const char * ktype_str = "unknown"; |
| 220 | switch (kparams->kernel_type) { |
| 221 | case HTP_GET_ROWS_KERNEL_SAMETYPE: ktype_str = "sametype"; break; |
| 222 | case HTP_GET_ROWS_KERNEL_TILED: ktype_str = "tiled"; break; |
| 223 | case HTP_GET_ROWS_KERNEL_FLAT: ktype_str = "flat"; break; |
| 224 | } |
| 225 | snprintf(str, max_size, "%s%s vtcm %u", ktype_str, kparams->n_threads > 1 ? "-multi" : "", (unsigned int) kparams->vtcm_size); |
| 226 | } else if (htp_op_is_glu(node.opcode)) { |
| 227 | const auto * kparams = node.kparams<htp_glu_kernel_params>(); |
| 228 | snprintf(str, max_size, "nth %d vtcm %u", (int) kparams->n_threads, (unsigned int) kparams->vtcm_size); |
| 229 | } else if (node.opcode == HTP_OP_PAD) { |
| 230 | const auto * kparams = node.kparams<htp_pad_kernel_params>(); |
| 231 | snprintf(str, max_size, "k%d nth %d vtcm %u", (int) kparams->kernel_type, (int) kparams->n_threads, (unsigned int) kparams->vtcm_size); |
| 232 | } else if (node.opcode == HTP_OP_CUMSUM) { |
| 233 | const auto * kparams = node.kparams<htp_cumsum_kernel_params>(); |
| 234 | snprintf(str, max_size, "%s nth %d vtcm %u", kparams->use_dma ? "dma" : "hvx", (int) kparams->n_threads, (unsigned int) kparams->vtcm_size); |
| 235 | } else if (node.opcode == HTP_OP_TOP_K_MASK) { |
| 236 | const auto * kparams = node.kparams<htp_top_k_mask_kernel_params>(); |
| 237 | const char * ktype_str = "?"; |
| 238 | switch (kparams->kernel_type) { |
| 239 | case HTP_TOP_K_MASK_KERNEL_ROWS: ktype_str = "rows"; break; |
| 240 | case HTP_TOP_K_MASK_KERNEL_PICKS: ktype_str = "picks"; break; |
| 241 | } |
| 242 | snprintf(str, max_size, "%s nth %d tile %u x%u vtcm %u", ktype_str, (int) kparams->n_threads, (unsigned int) kparams->tile_cols, (unsigned int) kparams->n_col_tiles, (unsigned int) kparams->vtcm_size); |
| 243 | } else if (node.opcode == HTP_OP_FILL) { |
| 244 | const auto * kparams = node.kparams<htp_fill_kernel_params>(); |
| 245 | snprintf(str, max_size, "%s nth %d", kparams->kernel_type == HTP_FILL_KERNEL_CONTIGUOUS ? "contig" : "strided", (int) kparams->n_threads); |
| 246 | } else if (node.opcode == HTP_OP_ROLL) { |
| 247 | const auto * kparams = node.kparams<htp_roll_kernel_params>(); |
| 248 | snprintf(str, max_size, "k%d nth %d", (int) kparams->kernel_type, (int) kparams->n_threads); |
| 249 | } else if (node.opcode == HTP_OP_DIAG) { |
| 250 | const auto * kparams = node.kparams<htp_diag_kernel_params>(); |
| 251 | snprintf(str, max_size, "%s nth %d vtcm %u", kparams->use_dma ? "dma" : "hvx", (int) kparams->n_threads, (unsigned int) kparams->vtcm_size); |
| 252 | } else if (node.opcode == HTP_OP_SOLVE_TRI) { |
| 253 | const auto * kparams = node.kparams<htp_solve_tri_kernel_params>(); |
| 254 | snprintf(str, max_size, "%s nth %d", kparams->batched ? "batched" : "chunked", (int) kparams->n_threads); |
| 255 | } else if (node.opcode == HTP_OP_UPSCALE) { |
| 256 | const auto * kparams = node.kparams<htp_upscale_kernel_params>(); |
| 257 | const char * ktype_str = "unknown"; |
| 258 | switch (kparams->kernel_type) { |
| 259 | case HTP_UPSCALE_KERNEL_NEAREST: ktype_str = "nearest"; break; |
| 260 | case HTP_UPSCALE_KERNEL_NEAREST_2X: ktype_str = "nearest-2x"; break; |
| 261 | case HTP_UPSCALE_KERNEL_NEAREST_4X: ktype_str = "nearest-4x"; break; |
| 262 | case HTP_UPSCALE_KERNEL_BILINEAR: ktype_str = kparams->align_corners ? "bilinear-ac" : "bilinear"; break; |
| 263 | case HTP_UPSCALE_KERNEL_BILINEAR_2X: ktype_str = "bilinear-2x"; break; |
| 264 | case HTP_UPSCALE_KERNEL_BILINEAR_AA: ktype_str = "bilinear-aa"; break; |
| 265 | case HTP_UPSCALE_KERNEL_BICUBIC: ktype_str = "bicubic"; break; |
| 266 | } |
| 267 | snprintf(str, max_size, "%s nth %d chk %d vtcm %u", ktype_str, (int) kparams->n_threads, (int) kparams->dst_nrows_per_chunk, (unsigned int) kparams->vtcm_size); |
| 268 | } else { |
| 269 | snprintf(str, max_size, "----"); |
| 270 | } |
| 271 | return str; |
| 272 | } |
| 273 | |
| 274 | static void append_section(std::string & res, const std::string & sec) { |
| 275 | if (!res.empty()) { |
| 276 | res += "|"; |
| 277 | } |
| 278 | res += sec; |
| 279 | } |
| 280 | |
| 281 | std::string htp_format_tensor(const ggml_tensor * t, uint32_t flags) { |
| 282 | std::string res; |
| 283 | if (flags & HTP_OPFMT_NAMES) append_section(res, format_tensor_name(t)); |
| 284 | if (flags & HTP_OPFMT_DIMS) append_section(res, format_tensor_dims(t)); |
| 285 | if (flags & HTP_OPFMT_TYPES) append_section(res, format_tensor_type(t)); |
| 286 | if (flags & HTP_OPFMT_STRIDES) append_section(res, format_tensor_strides(t)); |
| 287 | if (flags & HTP_OPFMT_BUFFS) append_section(res, format_tensor_buff(t)); |
| 288 | return res; |
| 289 | } |
| 290 | |
| 291 | std::string htp_format_op(const htp_opnode & node, uint32_t flags) { |
| 292 | std::string res; |
| 293 | if (flags & HTP_OPFMT_OP) append_section(res, node.name()); |
| 294 | if (flags & HTP_OPFMT_NAMES) append_section(res, format_op_tensors(node, format_tensor_name)); |
| 295 | if (flags & HTP_OPFMT_DIMS) append_section(res, format_op_tensors(node, format_tensor_dims)); |
| 296 | if (flags & HTP_OPFMT_TYPES) append_section(res, format_op_tensors(node, format_tensor_type)); |
| 297 | if (flags & HTP_OPFMT_STRIDES) append_section(res, format_op_tensors(node, format_tensor_strides)); |
| 298 | if (flags & HTP_OPFMT_BUFFS) append_section(res, format_op_tensors(node, format_tensor_buff)); |
| 299 | if (flags & HTP_OPFMT_KPARAMS) append_section(res, format_kernel_params(node)); |
| 300 | return res; |
| 301 | } |