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-metal/ggml-metal-fusion.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-metal / ggml-metal-fusion.h` 102 lines ยท 5094 bytes Raw Blame History 1 // single source of truth for the fusions supported by the Metal backend 2 // 3 // every fusable subgraph is declared exactly once as a ggml_metal_fusion entry in 4 // the table in ggml-metal-fusion.cpp. both the graph optimizer (ggml_metal_fusion_max) 5 // and the op encoders (ggml_metal_fusion_next) consult this same table with the same device 6 // properties. a check that passes in FULL mode also passes in STRUCTURAL mode, so the encoders 7 // fuse only groups that the optimizer may pack. 8 9 #pragma once 10 11 #include "ggml-impl.h" 12 13 #include 14 15 #ifdef __cplusplus 16 extern "C" { 17 #endif 18 19 struct ggml_metal_device_props; 20 21 // the maximum number of nodes that can be fused in a single kernel 22 // (also the maximum length of a packed fusion group during graph optimization) 23 #define GGML_METAL_FUSION_MAX 16 24 25 typedef enum ggml_metal_fusion_mode { 26 // structural checks for the graph optimizer, which runs before the graph tensors are allocated (weights 27 // already are); a check may skip conditions that differ between batch sizes, and so accept more than FULL 28 GGML_METAL_FUSION_STRUCTURAL = 0, 29 // full checks, including buffer placement; used by the op encoders 30 GGML_METAL_FUSION_FULL, 31 } ggml_metal_fusion_mode; 32 33 // identifier of each fusion pattern so the op encoders know which kernel to use 34 typedef enum ggml_metal_fusion_id { 35 GGML_METAL_FUSION_NONE = 0, 36 GGML_METAL_FUSION_NORM_MUL, // NORM/RMS_NORM + MUL 37 GGML_METAL_FUSION_NORM_MUL_ADD, // NORM/RMS_NORM + MUL + ADD 38 GGML_METAL_FUSION_NORM_SCALE, // NORM/RMS_NORM + SCALE 39 GGML_METAL_FUSION_ADD_CHAIN, // ADD x N (N in [2, 7]) 40 GGML_METAL_FUSION_SNAKE, // MUL + SIN + SQR + MUL + ADD 41 GGML_METAL_FUSION_GDN_CACHE, // GATED_DELTA_NET + CPY (write snapshots into the recurrent cache) 42 GGML_METAL_FUSION_TOPK_MOE, // SOFT_MAX + ARGSORT + GET_ROWS + norm/scale (MoE routing) 43 GGML_METAL_FUSION_MOE_REDUCE, // MUL + expert VIEWs + ADD chain (MoE output reduction) 44 GGML_METAL_FUSION_SSM_CONV_SILU, // SSM_CONV + UNARY (silu) 45 GGML_METAL_FUSION_MUL_MAT_ADD, // MUL_MAT + ADD (residual added in the few-row MMA store) 46 } ggml_metal_fusion_id; 47 48 struct ggml_metal_fusion; // defined in ggml-metal-fusion.cpp 49 50 typedef struct ggml_metal_fusion ggml_metal_fusion; 51 52 // access the fusion identifier without exposing the full pattern definition 53 ggml_metal_fusion_id ggml_metal_fusion_get_id(const struct ggml_metal_fusion * fusion); 54 55 // apply any alloc-dependencies required by the fused kernels during graph optimize 56 void ggml_metal_fusion_add_alloc_deps( 57 void * user_data, 58 void (*add_alloc_dep)(void *, struct ggml_tensor *, struct ggml_tensor *), 59 struct ggml_cgraph * gf); 60 61 // ---- shared fusion info --------------------------------------------------- 62 63 // shared fusion debugging context, owned by the device; newly created backend contexts for that 64 // device register with it so the fusion counters are race-free and accumulate across contexts. 65 struct ggml_metal_fusion_info; // defined in ggml-metal-fusion.cpp 66 67 struct ggml_metal_fusion_info * ggml_metal_fusion_info_init(bool enabled, int debug); 68 void ggml_metal_fusion_info_free(struct ggml_metal_fusion_info * finfo); 69 70 bool ggml_metal_fusion_info_enabled(const struct ggml_metal_fusion_info * finfo); 71 bool ggml_metal_fusion_info_stats (const struct ggml_metal_fusion_info * finfo); 72 int ggml_metal_fusion_info_debug (const struct ggml_metal_fusion_info * finfo); 73 74 int ggml_metal_fusion_info_n_fusions(const struct ggml_metal_fusion_info * finfo); 75 const char * ggml_metal_fusion_info_label (const struct ggml_metal_fusion_info * finfo, int idx); 76 uint64_t ggml_metal_fusion_info_count (const struct ggml_metal_fusion_info * finfo, int idx); 77 78 void ggml_metal_fusion_info_count_fusion(struct ggml_metal_fusion_info * finfo, const struct ggml_metal_fusion * fusion); 79 void ggml_metal_fusion_info_set_enabled (struct ggml_metal_fusion_info * finfo, bool enabled); 80 81 void ggml_metal_fusion_info_stats_init ( struct ggml_metal_fusion_info * finfo); 82 void ggml_metal_fusion_info_stats_reset( struct ggml_metal_fusion_info * finfo); 83 int ggml_metal_fusion_info_stats_get (const struct ggml_metal_fusion_info * finfo, const char ** labels, uint64_t * counts, int n); 84 void ggml_metal_fusion_info_labels_init( struct ggml_metal_fusion_info * finfo); 85 86 // compute phase: longest fusion starting at idx (a position in node_idxs) that matches in `mode` on a device with 87 // props. returns the matching pattern (nullptr if no fusion) and sets *n_out to the number of nodes consumed. 88 const ggml_metal_fusion * ggml_metal_fusion_next( 89 const struct ggml_cgraph * gf, 90 const int * node_idxs, 91 int n_idxs, 92 int idx, 93 ggml_metal_fusion_mode mode, 94 int * n_out); 95 96 // optimize phase: maximum number of nodes starting at idx (a raw sequential graph index) that 97 // could be fused on a device with props, chaining patterns back-to-back. returns at least 1. 98 int ggml_metal_fusion_max(const struct ggml_cgraph * gf, int idx); 99 100 #ifdef __cplusplus 101 } 102 #endif