char-fork/llama.cpp

git clone https://git.t4t.associates/char-fork/llama.cpp

Georgi Gerganovmetal : single-source fusion table + fusion debug rework (#28164)a2878d30d

char/aliceai-t5-moe
19.1 KiB360 linesraw
1#pragma once
2
3#include "ggml.h"
4
5#ifdef __cplusplus
6extern "C" {
7#endif
8
9struct ggml_metal_buffer_id {
10    void * metal; // id<MTLBuffer>
11    size_t offs;
12};
13
14typedef struct ggml_metal_device * ggml_metal_device_t;
15
16//
17// MTLFunctionConstantValues wrapper
18//
19
20typedef struct ggml_metal_cv * ggml_metal_cv_t;
21
22ggml_metal_cv_t ggml_metal_cv_init(void);
23void ggml_metal_cv_free(ggml_metal_cv_t cv);
24
25void ggml_metal_cv_set_int16(ggml_metal_cv_t cv, int16_t value, int32_t idx);
26void ggml_metal_cv_set_int32(ggml_metal_cv_t cv, int32_t value, int32_t idx);
27void ggml_metal_cv_set_bool (ggml_metal_cv_t cv, bool    value, int32_t idx);
28
29//
30// MTLComputePipelineState wrapper
31//
32
33typedef struct ggml_metal_pipeline * ggml_metal_pipeline_t;
34
35ggml_metal_pipeline_t ggml_metal_pipeline_init(void);
36void ggml_metal_pipeline_free(ggml_metal_pipeline_t pipeline);
37
38// a collection of pipelines
39typedef struct ggml_metal_pipelines * ggml_metal_pipelines_t;
40
41ggml_metal_pipelines_t ggml_metal_pipelines_init(void);
42void ggml_metal_pipelines_free(ggml_metal_pipelines_t ppls);
43
44void                  ggml_metal_pipelines_add(ggml_metal_pipelines_t ppls, const char * name, ggml_metal_pipeline_t pipeline);
45ggml_metal_pipeline_t ggml_metal_pipelines_get(ggml_metal_pipelines_t ppls, const char * name);
46
47struct ggml_metal_pipeline_with_params {
48    ggml_metal_pipeline_t pipeline;
49
50    int nsg;
51
52    int nr0;
53    int nr1;
54
55    size_t smem;
56
57    bool c4;
58    bool cnt;
59};
60
61int ggml_metal_pipeline_max_theads_per_threadgroup(struct ggml_metal_pipeline_with_params pipeline);
62
63//
64// MTLCommandBuffer wrapper
65//
66
67typedef void * ggml_metal_cmd_buf_t;
68
69//
70// MTLComputeCommandEncoder wrapper
71//
72
73typedef struct ggml_metal_encoder * ggml_metal_encoder_t;
74
75ggml_metal_encoder_t ggml_metal_encoder_init(ggml_metal_cmd_buf_t cmd_buf_raw, bool concurrent);
76void ggml_metal_encoder_free(ggml_metal_encoder_t encoder);
77
78void ggml_metal_encoder_debug_group_push(ggml_metal_encoder_t encoder, const char * name);
79void ggml_metal_encoder_debug_group_pop (ggml_metal_encoder_t encoder);
80
81void ggml_metal_encoder_set_pipeline(ggml_metal_encoder_t encoder, struct ggml_metal_pipeline_with_params pipeline);
82
83void ggml_metal_encoder_set_bytes (ggml_metal_encoder_t encoder, void * data, size_t size, int idx);
84void ggml_metal_encoder_set_buffer(ggml_metal_encoder_t encoder, struct ggml_metal_buffer_id buffer, int idx);
85
86void ggml_metal_encoder_set_threadgroup_memory_size(ggml_metal_encoder_t encoder, size_t size, int idx);
87
88void ggml_metal_encoder_dispatch_threadgroups(ggml_metal_encoder_t encoder, int tg0, int tg1, int tg2, int tptg0, int tptg1, int tptg2);
89
90void ggml_metal_encoder_memory_barrier(ggml_metal_encoder_t encoder);
91
92void ggml_metal_encoder_end_encoding(ggml_metal_encoder_t encoder);
93
94//
95// MTLLibrary wrapper
96//
97
98typedef struct ggml_metal_library * ggml_metal_library_t;
99
100ggml_metal_library_t ggml_metal_library_init            (ggml_metal_device_t dev);
101ggml_metal_library_t ggml_metal_library_init_from_source(ggml_metal_device_t dev, const char * source, bool verbose);
102
103void ggml_metal_library_free(ggml_metal_library_t lib);
104
105ggml_metal_device_t ggml_metal_library_get_device(ggml_metal_library_t lib);
106
107struct ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline    (ggml_metal_library_t lib, const char * name);
108struct ggml_metal_pipeline_with_params ggml_metal_library_compile_pipeline(ggml_metal_library_t lib, const char * base, const char * name, ggml_metal_cv_t cv);
109
110struct ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_base              (ggml_metal_library_t lib, enum ggml_op op);
111struct ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_cpy               (ggml_metal_library_t lib, enum ggml_type tsrc, enum ggml_type tdst);
112struct ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_pool_1d           (ggml_metal_library_t lib, const struct ggml_tensor * op, enum ggml_op_pool op_pool);
113struct ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_pool_2d           (ggml_metal_library_t lib, const struct ggml_tensor * op, enum ggml_op_pool op_pool);
114struct ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_get_rows          (ggml_metal_library_t lib, enum ggml_type tsrc);
115struct ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_set_rows          (ggml_metal_library_t lib, const struct ggml_tensor * op);
116struct ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_diag              (ggml_metal_library_t lib, const struct ggml_tensor * op);
117struct ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_repeat            (ggml_metal_library_t lib, enum ggml_type tsrc);
118struct ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_concat            (ggml_metal_library_t lib, enum ggml_type tsrc);
119struct ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_unary             (ggml_metal_library_t lib, const struct ggml_tensor * op);
120struct ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_silu_back         (ggml_metal_library_t lib, const struct ggml_tensor * op);
121struct ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_glu               (ggml_metal_library_t lib, const struct ggml_tensor * op);
122struct ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_sum               (ggml_metal_library_t lib, const struct ggml_tensor * op);
123struct ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_sum_rows          (ggml_metal_library_t lib, const struct ggml_tensor * op);
124struct ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_cumsum_blk        (ggml_metal_library_t lib, const struct ggml_tensor * op);
125struct ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_cumsum_add        (ggml_metal_library_t lib, const struct ggml_tensor * op);
126struct ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_tri               (ggml_metal_library_t lib, const struct ggml_tensor * op);
127struct ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_soft_max          (ggml_metal_library_t lib, const struct ggml_tensor * op);
128struct ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_lightning_indexer (ggml_metal_library_t lib, const struct ggml_tensor * op);
129struct ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_dsv4_hc           (ggml_metal_library_t lib, enum ggml_op op);
130struct ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_ssm_conv          (ggml_metal_library_t lib, const struct ggml_tensor * op);
131struct ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_ssm_conv_batched  (ggml_metal_library_t lib, const struct ggml_tensor * op, int ssm_conv_bs);
132struct ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_ssm_scan          (ggml_metal_library_t lib, const struct ggml_tensor * op, bool tail);
133struct ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_ssm_scan_ssd_mma  (ggml_metal_library_t lib, const struct ggml_tensor * op);
134struct ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_rwkv              (ggml_metal_library_t lib, const struct ggml_tensor * op);
135struct ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_gated_delta_net   (ggml_metal_library_t lib, const struct ggml_tensor * op);
136struct ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_solve_tri         (ggml_metal_library_t lib, const struct ggml_tensor * op);
137struct ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_mul_mv_ext        (ggml_metal_library_t lib, const struct ggml_tensor * op, int nsg, int nxpsg, int r1ptg);
138struct ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_mul_mm            (ggml_metal_library_t lib, const struct ggml_tensor * op);
139struct ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_mul_mv            (ggml_metal_library_t lib, const struct ggml_tensor * op);
140struct ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_mul_mm_id_map0    (ggml_metal_library_t lib, int ne02, int ne20);
141struct ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_mul_mm_id         (ggml_metal_library_t lib, const struct ggml_tensor * op);
142struct ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_mul_mv_id         (ggml_metal_library_t lib, const struct ggml_tensor * op);
143struct ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_argmax            (ggml_metal_library_t lib, const struct ggml_tensor * op);
144struct ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_argsort           (ggml_metal_library_t lib, const struct ggml_tensor * op);
145struct ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_argsort_merge     (ggml_metal_library_t lib, const struct ggml_tensor * op);
146struct ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_fwht              (ggml_metal_library_t lib, int n);
147struct ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_top_k             (ggml_metal_library_t lib, const struct ggml_tensor * op);
148struct ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_top_k_radix       (ggml_metal_library_t lib, const struct ggml_tensor * op);
149struct ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_top_k_merge       (ggml_metal_library_t lib, const struct ggml_tensor * op);
150struct ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_bin               (ggml_metal_library_t lib, const struct ggml_tensor * op, int32_t n_fuse );
151struct ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_bin_one           (ggml_metal_library_t lib, enum ggml_op op);
152struct ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_l2_norm           (ggml_metal_library_t lib, const struct ggml_tensor * op);
153struct ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_group_norm        (ggml_metal_library_t lib, const struct ggml_tensor * op);
154struct ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_norm              (ggml_metal_library_t lib, const struct ggml_tensor * op, int32_t n_fuse);
155struct ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_rope              (ggml_metal_library_t lib, const struct ggml_tensor * op);
156struct ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_im2col            (ggml_metal_library_t lib, const struct ggml_tensor * op);
157struct ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_conv_transpose_1d (ggml_metal_library_t lib, const struct ggml_tensor * op);
158struct ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_conv_transpose_2d (ggml_metal_library_t lib, const struct ggml_tensor * op);
159struct ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_col2im_1d         (ggml_metal_library_t lib, const struct ggml_tensor * op);
160struct ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_snake             (ggml_metal_library_t lib, enum ggml_type type);
161struct ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_conv_2d           (ggml_metal_library_t lib, const struct ggml_tensor * op);
162struct ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_conv_2d_dw        (ggml_metal_library_t lib, const struct ggml_tensor * op, bool tiled);
163struct ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_conv_3d           (ggml_metal_library_t lib, const struct ggml_tensor * op);
164struct ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_upscale           (ggml_metal_library_t lib, const struct ggml_tensor * op);
165struct ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_pad               (ggml_metal_library_t lib, const struct ggml_tensor * op);
166struct ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_pad_reflect_1d    (ggml_metal_library_t lib, const struct ggml_tensor * op);
167struct ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_roll              (ggml_metal_library_t lib, const struct ggml_tensor * op);
168struct ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_arange            (ggml_metal_library_t lib, const struct ggml_tensor * op);
169struct ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_timestep_embedding(ggml_metal_library_t lib, const struct ggml_tensor * op);
170struct ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_opt_step_adamw    (ggml_metal_library_t lib, const struct ggml_tensor * op);
171struct ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_opt_step_sgd      (ggml_metal_library_t lib, const struct ggml_tensor * op);
172struct ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_memset            (ggml_metal_library_t lib, const struct ggml_tensor * op);
173struct ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_count_equal       (ggml_metal_library_t lib, const struct ggml_tensor * op);
174
175struct ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_flash_attn_ext_pad(
176        ggml_metal_library_t lib,
177        const struct ggml_tensor * op,
178        bool    has_mask,
179        int32_t ncpsg);
180
181struct ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_flash_attn_ext_kv_f16(
182        ggml_metal_library_t lib,
183        const struct ggml_tensor * op);
184
185struct ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_flash_attn_ext_blk(
186        ggml_metal_library_t lib,
187        const struct ggml_tensor * op,
188        int32_t nqptg,
189        int32_t ncpsg);
190
191struct ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_flash_attn_ext(
192        ggml_metal_library_t lib,
193        const struct ggml_tensor * op,
194        bool    has_mask,
195        bool    has_sinks,
196        bool    has_bias,
197        bool    has_scap,
198        bool    has_kvpad,
199        int32_t nsg,
200        bool    use_kv_f16,
201        int32_t ns10,
202        int32_t ns20);
203
204struct ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_flash_attn_ext_vec_idx(
205        ggml_metal_library_t lib,
206        const struct ggml_tensor * op);
207
208struct ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_flash_attn_ext_vec(
209        ggml_metal_library_t lib,
210        const struct ggml_tensor * op,
211        bool    has_mask,
212        bool    has_sinks,
213        bool    has_bias,
214        bool    has_scap,
215        bool    has_kvpad,
216        bool    has_sparse,
217        int32_t nqpsg,
218        int32_t ne,
219        int32_t nsg,
220        int32_t nwg,
221        bool    use_kv_f16,
222        int32_t ns10,
223        int32_t ns20);
224
225struct ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_flash_attn_ext_vec_reduce(
226        ggml_metal_library_t lib,
227        const struct ggml_tensor * op,
228        int32_t dv,
229        int32_t nwg);
230
231// MTLResidencySet wrapper
232
233typedef void * ggml_metal_rset_t;
234
235// a collection of residency sets (non-owning)
236typedef struct ggml_metal_rsets * ggml_metal_rsets_t;
237
238ggml_metal_rsets_t ggml_metal_rsets_init(ggml_metal_device_t dev);
239void ggml_metal_rsets_free(ggml_metal_rsets_t rsets);
240
241//
242// device
243//
244
245enum ggml_metal_device_id {
246    GGML_METAL_DEVICE_GENERIC = 0,
247
248    GGML_METAL_DEVICE_M1,
249    GGML_METAL_DEVICE_M1_PRO,
250    GGML_METAL_DEVICE_M1_MAX,
251    GGML_METAL_DEVICE_M1_ULTRA,
252    GGML_METAL_DEVICE_M2,
253    GGML_METAL_DEVICE_M2_PRO,
254    GGML_METAL_DEVICE_M2_MAX,
255    GGML_METAL_DEVICE_M2_ULTRA,
256    GGML_METAL_DEVICE_M3,
257    GGML_METAL_DEVICE_M3_PRO,
258    GGML_METAL_DEVICE_M3_MAX,
259    GGML_METAL_DEVICE_M3_ULTRA,
260    GGML_METAL_DEVICE_M4,
261    GGML_METAL_DEVICE_M4_PRO,
262    GGML_METAL_DEVICE_M4_MAX,
263    GGML_METAL_DEVICE_M5,
264    GGML_METAL_DEVICE_M5_PRO,
265    GGML_METAL_DEVICE_M5_MAX,
266    GGML_METAL_DEVICE_M5_ULTRA,
267    GGML_METAL_DEVICE_A18_PRO,
268};
269
270const char * ggml_metal_device_id_token(enum ggml_metal_device_id id);
271
272struct ggml_metal_device_props {
273    int device;
274    int device_phys;
275    int device_virt;
276    char name[128];
277    char desc[128];
278
279    size_t max_buffer_size;
280    size_t max_working_set_size;
281    size_t max_theadgroup_memory_size;
282
283    bool has_simdgroup_reduction;
284    bool has_simdgroup_mm;
285    bool has_unified_memory;
286    bool has_bfloat;
287    bool has_tensor;
288    bool use_residency_sets;
289    bool use_shared_buffers;
290
291    bool supports_gpu_family_apple7;
292
293    enum ggml_metal_device_id device_id;
294    int gpu_family;
295
296    int op_offload_min_batch_size;
297};
298
299typedef struct ggml_metal_event * ggml_metal_event_t;
300
301void ggml_metal_event_encode_signal(ggml_metal_event_t ev, ggml_metal_cmd_buf_t cmd_buf);
302void ggml_metal_event_encode_wait  (ggml_metal_event_t ev, ggml_metal_cmd_buf_t cmd_buf);
303
304ggml_metal_device_t ggml_metal_device_init(int device, int n_devices);
305void ggml_metal_device_free(ggml_metal_device_t dev);
306
307ggml_metal_device_t ggml_metal_device_get(int device, int n_devices);
308
309void * ggml_metal_device_get_obj  (ggml_metal_device_t dev); // id<MTLDevice>
310void * ggml_metal_device_get_queue(ggml_metal_device_t dev); // id<MTLCommandQueue>
311
312ggml_metal_library_t ggml_metal_device_get_library(ggml_metal_device_t dev);
313
314void ggml_metal_device_rsets_add(ggml_metal_device_t dev, ggml_metal_rset_t rset);
315void ggml_metal_device_rsets_rm (ggml_metal_device_t dev, ggml_metal_rset_t rset);
316
317void ggml_metal_device_rsets_keep_alive(ggml_metal_device_t dev);
318
319ggml_metal_event_t ggml_metal_device_event_init(ggml_metal_device_t dev);
320void ggml_metal_device_event_free(ggml_metal_device_t dev, ggml_metal_event_t ev);
321void ggml_metal_device_event_synchronize(ggml_metal_device_t dev, ggml_metal_event_t ev);
322
323void ggml_metal_device_get_memory(ggml_metal_device_t dev, size_t * free, size_t * total);
324bool ggml_metal_device_supports_op(ggml_metal_device_t dev, const struct ggml_tensor * op);
325
326const struct ggml_metal_device_props * ggml_metal_device_get_props(ggml_metal_device_t dev);
327
328struct ggml_metal_fusion_info;
329
330// the device-owned fusion debugging context (NULL unless fusion debugging is enabled)
331struct ggml_metal_fusion_info * ggml_metal_device_get_fusion_info(ggml_metal_device_t dev);
332
333//
334// device buffers
335//
336
337typedef struct ggml_metal_buffer * ggml_metal_buffer_t;
338
339ggml_metal_buffer_t ggml_metal_buffer_init(ggml_metal_device_t dev, size_t size, bool shared);
340ggml_metal_buffer_t ggml_metal_buffer_map (ggml_metal_device_t dev, void * ptr, size_t size, size_t max_tensor_size);
341
342void   ggml_metal_buffer_free     (ggml_metal_buffer_t buf);
343void * ggml_metal_buffer_get_base (ggml_metal_buffer_t buf);
344bool   ggml_metal_buffer_is_shared(ggml_metal_buffer_t buf);
345
346void   ggml_metal_buffer_memset_tensor(ggml_metal_buffer_t buf, struct ggml_tensor * tensor, uint8_t value, size_t offset, size_t size);
347void   ggml_metal_buffer_set_tensor   (ggml_metal_buffer_t buf, struct ggml_tensor * tensor, const void * data, size_t offset, size_t size);
348void   ggml_metal_buffer_get_tensor   (ggml_metal_buffer_t buf, const struct ggml_tensor * tensor, void * data, size_t offset, size_t size);
349bool   ggml_metal_buffer_cpy_tensor   (ggml_metal_buffer_t buf, const struct ggml_tensor * src, struct ggml_tensor * dst);
350void   ggml_metal_buffer_clear        (ggml_metal_buffer_t buf, uint8_t value);
351
352// finds the Metal buffer that contains the tensor data on the GPU device
353// the assumption is that there is 1-to-1 mapping between the host and device memory buffers, so we can find the
354// Metal buffer based on the host memory pointer
355//
356struct ggml_metal_buffer_id ggml_metal_buffer_get_id(ggml_metal_buffer_t buf, const struct ggml_tensor * t);
357
358#ifdef __cplusplus
359}
360#endif