okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
b2de2641f
main
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, so the two 6// phases can never disagree about what can be fused. 7 8#pragma once 9 10#include "ggml-impl.h" 11 12#include <stdbool.h> 13 14#ifdef __cplusplus 15extern "C" { 16#endif 17 18// the maximum number of nodes that can be fused in a single kernel 19// (also the maximum length of a packed fusion group during graph optimization) 20#define GGML_METAL_FUSION_MAX 16 21 22typedef enum ggml_metal_fusion_mode { 23// structural checks only; used by the graph optimizer, at which point the graph 24// tensors are not allocated yet, so buffer placement cannot be verified 25GGML_METAL_FUSION_STRUCTURAL = 0 , 26// full checks, including buffer placement; used by the op encoders 27GGML_METAL_FUSION_FULL , 28}ggml_metal_fusion_mode ; 29 30// identifier of each fusion pattern so the op encoders know which kernel to use 31typedef enum ggml_metal_fusion_id { 32GGML_METAL_FUSION_NONE = 0 , 33GGML_METAL_FUSION_NORM_MUL ,// NORM/RMS_NORM + MUL 34GGML_METAL_FUSION_NORM_MUL_ADD ,// NORM/RMS_NORM + MUL + ADD 35GGML_METAL_FUSION_NORM_SCALE ,// NORM/RMS_NORM + SCALE 36GGML_METAL_FUSION_ADD_CHAIN ,// ADD x N (N in [2, 7]) 37GGML_METAL_FUSION_SNAKE ,// MUL + SIN + SQR + MUL + ADD 38GGML_METAL_FUSION_GDN_CACHE ,// GATED_DELTA_NET + CPY (write snapshots into the recurrent cache) 39GGML_METAL_FUSION_TOPK_MOE ,// SOFT_MAX + ARGSORT + GET_ROWS + norm/scale (MoE routing) 40GGML_METAL_FUSION_MOE_REDUCE ,// MUL + expert VIEWs + ADD chain (MoE output reduction) 41GGML_METAL_FUSION_SSM_CONV_SILU ,// SSM_CONV + UNARY (silu) 42}ggml_metal_fusion_id ; 43 44struct ggml_metal_fusion ;// defined in ggml-metal-fusion.cpp 45 46typedef struct ggml_metal_fusion ggml_metal_fusion ; 47 48// access the fusion identifier without exposing the full pattern definition 49ggml_metal_fusion_id ggml_metal_fusion_get_id (const struct ggml_metal_fusion * fusion ); 50 51// apply any alloc-dependencies required by the fused kernels during graph optimize 52void ggml_metal_fusion_add_alloc_deps ( 53void * user_data , 54void (* add_alloc_dep )(void * ,struct ggml_tensor * ,struct ggml_tensor * ), 55struct ggml_cgraph * gf ); 56 57// ---- shared fusion info --------------------------------------------------- 58 59// shared fusion debugging context, owned by the device; newly created backend contexts for that 60// device register with it so the fusion counters are race-free and accumulate across contexts. 61struct ggml_metal_fusion_info ;// defined in ggml-metal-fusion.cpp 62 63struct ggml_metal_fusion_info * ggml_metal_fusion_info_init (bool enabled ,int debug ); 64void ggml_metal_fusion_info_free (struct ggml_metal_fusion_info * finfo ); 65 66bool ggml_metal_fusion_info_enabled (const struct ggml_metal_fusion_info * finfo ); 67bool ggml_metal_fusion_info_stats (const struct ggml_metal_fusion_info * finfo ); 68int ggml_metal_fusion_info_debug (const struct ggml_metal_fusion_info * finfo ); 69 70int ggml_metal_fusion_info_n_fusions (const struct ggml_metal_fusion_info * finfo ); 71const char * ggml_metal_fusion_info_label (const struct ggml_metal_fusion_info * finfo ,int idx ); 72uint64_t ggml_metal_fusion_info_count (const struct ggml_metal_fusion_info * finfo ,int idx ); 73 74void ggml_metal_fusion_info_count_fusion (struct ggml_metal_fusion_info * finfo ,const struct ggml_metal_fusion * fusion ); 75void ggml_metal_fusion_info_set_enabled (struct ggml_metal_fusion_info * finfo ,bool enabled ); 76 77void ggml_metal_fusion_info_stats_init (struct ggml_metal_fusion_info * finfo ); 78void ggml_metal_fusion_info_stats_reset (struct ggml_metal_fusion_info * finfo ); 79int ggml_metal_fusion_info_stats_get (const struct ggml_metal_fusion_info * finfo ,const char ** labels ,uint64_t * counts ,int n ); 80void ggml_metal_fusion_info_labels_init (struct ggml_metal_fusion_info * finfo ); 81 82// compute phase: longest fusion starting at idx (a position in node_idxs) that matches in `mode`. 83// returns the matching pattern (nullptr if no fusion) and sets *n_out to the number of nodes consumed. 84const ggml_metal_fusion * ggml_metal_fusion_next ( 85const struct ggml_cgraph * gf , 86const int * node_idxs , 87int n_idxs , 88int idx , 89ggml_metal_fusion_mode mode , 90int * n_out ); 91 92// optimize phase: maximum number of nodes starting at idx (a raw sequential graph index) that 93// could be fused, chaining patterns back-to-back. returns at least 1. 94int ggml_metal_fusion_max (const struct ggml_cgraph * gf ,int idx ); 95 96#ifdef __cplusplus 97} 98#endif