okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge f-own-1b (033cfdd8c) into f-own-int: nothing downloads llama.cppb2de2641f

main
83.2 KiB2565 linesraw
1#include "ggml-metal-device.h"
2
3#include "ggml-metal-impl.h"
4#include "ggml-metal-tuning.h"
5
6#include "ggml-impl.h"
7
8#include <cassert>
9#include <memory>
10#include <string>
11#include <unordered_map>
12
13struct ggml_metal_device_deleter {
14    void operator()(ggml_metal_device_t ctx) {
15        ggml_metal_device_free(ctx);
16    }
17};
18
19typedef std::unique_ptr<ggml_metal_device, ggml_metal_device_deleter> ggml_metal_device_ptr;
20
21ggml_metal_device_t ggml_metal_device_get(int device, int n_devices) {
22    static std::vector<ggml_metal_device_ptr> devs;
23
24    devs.emplace_back(ggml_metal_device_init(device, n_devices));
25
26    return devs.back().get();
27}
28
29struct ggml_metal_pipelines {
30    std::unordered_map<std::string, ggml_metal_pipeline_t> data;
31};
32
33ggml_metal_pipelines_t ggml_metal_pipelines_init(void) {
34    ggml_metal_pipelines_t res = new ggml_metal_pipelines();
35
36    return res;
37}
38
39void ggml_metal_pipelines_free(ggml_metal_pipelines_t ppls) {
40    if (!ppls) {
41        return;
42    }
43
44    for (auto it = ppls->data.begin(); it != ppls->data.end(); ++it) {
45        ggml_metal_pipeline_free(it->second);
46    }
47
48    delete ppls;
49}
50
51void ggml_metal_pipelines_add(ggml_metal_pipelines_t ppls, const char * name, ggml_metal_pipeline_t pipeline) {
52    ppls->data[name] = pipeline;
53}
54
55ggml_metal_pipeline_t ggml_metal_pipelines_get(ggml_metal_pipelines_t ppls, const char * name) {
56    if (ppls->data.find(name) == ppls->data.end()) {
57        return nullptr;
58    }
59
60    return ppls->data[name];
61}
62
63struct ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_base(ggml_metal_library_t lib, ggml_op op) {
64    char base[256];
65    char name[256];
66
67    const char * op_str = "undefined";
68    switch (op) {
69        case GGML_OP_ADD_ID: op_str = "add_id"; break;
70        default: GGML_ABORT("fatal error");
71    };
72
73    snprintf(base, 256, "kernel_%s", op_str);
74    snprintf(name, 256, "%s", base);
75
76    ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name);
77    if (!res.pipeline) {
78        res = ggml_metal_library_compile_pipeline(lib, base, name, nullptr);
79    }
80
81    return res;
82}
83
84ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_cpy(ggml_metal_library_t lib, ggml_type tsrc, ggml_type tdst) {
85    char base[256];
86    char name[256];
87
88    snprintf(base, 256, "kernel_cpy_%s_%s", ggml_type_name(tsrc), ggml_type_name(tdst));
89    snprintf(name, 256, "%s", base);
90
91    ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name);
92    if (!res.pipeline) {
93        res = ggml_metal_library_compile_pipeline(lib, base, name, nullptr);
94    }
95
96    return res;
97}
98
99ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_pool_1d(ggml_metal_library_t lib, const ggml_tensor * op, ggml_op_pool op_pool) {
100    GGML_ASSERT(ggml_is_contiguous(op->src[0]));
101    GGML_ASSERT(op->src[0]->type == GGML_TYPE_F32 && op->src[0]->type == op->type);
102
103    const char * pool_str = "undefined";
104    switch (op_pool) {
105        case GGML_OP_POOL_AVG: pool_str = "avg"; break;
106        case GGML_OP_POOL_MAX: pool_str = "max"; break;
107        default: GGML_ASSERT(false && "not implemented");
108    };
109
110    char base[256];
111    char name[256];
112
113    snprintf(base, sizeof(base), "kernel_pool_1d_%s_%s", pool_str, ggml_type_name(op->src[0]->type));
114    snprintf(name, sizeof(name), "%s", base);
115
116    ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name);
117    if (!res.pipeline) {
118        res = ggml_metal_library_compile_pipeline(lib, base, name, nullptr);
119    }
120
121    return res;
122}
123
124ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_pool_2d(ggml_metal_library_t lib, const ggml_tensor * op, ggml_op_pool op_pool) {
125    GGML_ASSERT(ggml_is_contiguous(op->src[0]));
126    GGML_ASSERT(op->src[0]->type == GGML_TYPE_F32 && op->src[0]->type == op->type);
127
128    const char * pool_str = "undefined";
129    switch (op_pool) {
130        case GGML_OP_POOL_AVG: pool_str = "avg"; break;
131        case GGML_OP_POOL_MAX: pool_str = "max"; break;
132        default: GGML_ASSERT(false && "not implemented");
133    };
134
135    char base[256];
136    char name[256];
137
138    snprintf(base, 256, "kernel_pool_2d_%s_%s", pool_str, ggml_type_name(op->src[0]->type));
139    snprintf(name, 256, "%s", base);
140
141    ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name);
142    if (!res.pipeline) {
143        res = ggml_metal_library_compile_pipeline(lib, base, name, nullptr);
144    }
145
146    return res;
147}
148
149ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_get_rows(ggml_metal_library_t lib, ggml_type tsrc) {
150    char base[256];
151    char name[256];
152
153    snprintf(base, 256, "kernel_get_rows_%s", ggml_type_name(tsrc));
154    snprintf(name, 256, "%s", base);
155
156    ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name);
157    if (!res.pipeline) {
158        res = ggml_metal_library_compile_pipeline(lib, base, name, nullptr);
159    }
160
161    return res;
162}
163
164ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_set_rows(ggml_metal_library_t lib, const ggml_tensor * op) {
165    char base[256];
166    char name[256];
167
168    const auto tsrc = op->src[0]->type;
169    const auto tidx = op->src[1]->type;
170    const auto tdst = op->type;
171
172    snprintf(base, 256, "kernel_set_rows_%s_%s_%s", ggml_type_name(tsrc), ggml_type_name(tidx), ggml_type_name(tdst));
173    snprintf(name, 256, "%s", base);
174
175    ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name);
176    if (!res.pipeline) {
177        res = ggml_metal_library_compile_pipeline(lib, base, name, nullptr);
178    }
179
180    return res;
181}
182
183ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_diag(ggml_metal_library_t lib, const ggml_tensor * op) {
184    char base[256];
185    char name[256];
186
187    const int n = op->src[0]->ne[0];
188
189    snprintf(base, 256, "kernel_diag_%s", ggml_type_name(op->src[0]->type));
190    snprintf(name, 256, "%s_n=%d", base, n);
191
192    ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name);
193    if (!res.pipeline) {
194        res = ggml_metal_library_compile_pipeline(lib, base, name, nullptr);
195    }
196
197    res.nsg  = 1;
198    res.smem = 0;
199
200    return res;
201}
202
203ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_repeat(ggml_metal_library_t lib, ggml_type tsrc) {
204    char base[256];
205    char name[256];
206
207    snprintf(base, 256, "kernel_repeat_%s", ggml_type_name(tsrc));
208    snprintf(name, 256, "%s", base);
209
210    ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name);
211    if (!res.pipeline) {
212        res = ggml_metal_library_compile_pipeline(lib, base, name, nullptr);
213    }
214
215    return res;
216}
217
218ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_concat(ggml_metal_library_t lib, ggml_type tsrc) {
219    char base[256];
220    char name[256];
221
222    snprintf(base, 256, "kernel_concat_%s", ggml_type_name(tsrc));
223    snprintf(name, 256, "%s", base);
224
225    ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name);
226    if (!res.pipeline) {
227        res = ggml_metal_library_compile_pipeline(lib, base, name, nullptr);
228    }
229
230    return res;
231}
232
233ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_unary(ggml_metal_library_t lib, const ggml_tensor * op) {
234    char base[256];
235    char name[256];
236
237    int op_num = -1;
238
239    switch (op->op) {
240        case GGML_OP_SCALE:      op_num = OP_UNARY_NUM_SCALE;      break;
241        case GGML_OP_FILL:       op_num = OP_UNARY_NUM_FILL;       break;
242        case GGML_OP_CLAMP:      op_num = OP_UNARY_NUM_CLAMP;      break;
243        case GGML_OP_SQR:        op_num = OP_UNARY_NUM_SQR;        break;
244        case GGML_OP_SQRT:       op_num = OP_UNARY_NUM_SQRT;       break;
245        case GGML_OP_SIN:        op_num = OP_UNARY_NUM_SIN;        break;
246        case GGML_OP_COS:        op_num = OP_UNARY_NUM_COS;        break;
247        case GGML_OP_LOG:        op_num = OP_UNARY_NUM_LOG;        break;
248        case GGML_OP_LEAKY_RELU: op_num = OP_UNARY_NUM_LEAKY_RELU; break;
249        case GGML_OP_UNARY:
250            switch (ggml_get_unary_op(op)) {
251                case GGML_UNARY_OP_TANH:        op_num = OP_UNARY_NUM_TANH;        break;
252                case GGML_UNARY_OP_RELU:        op_num = OP_UNARY_NUM_RELU;        break;
253                case GGML_UNARY_OP_SIGMOID:     op_num = OP_UNARY_NUM_SIGMOID;     break;
254                case GGML_UNARY_OP_GELU:        op_num = OP_UNARY_NUM_GELU;        break;
255                case GGML_UNARY_OP_GELU_ERF:    op_num = OP_UNARY_NUM_GELU_ERF;    break;
256                case GGML_UNARY_OP_GELU_QUICK:  op_num = OP_UNARY_NUM_GELU_QUICK;  break;
257                case GGML_UNARY_OP_SILU:        op_num = OP_UNARY_NUM_SILU;        break;
258                case GGML_UNARY_OP_ELU:         op_num = OP_UNARY_NUM_ELU;         break;
259                case GGML_UNARY_OP_NEG:         op_num = OP_UNARY_NUM_NEG;         break;
260                case GGML_UNARY_OP_ABS:         op_num = OP_UNARY_NUM_ABS;         break;
261                case GGML_UNARY_OP_SGN:         op_num = OP_UNARY_NUM_SGN;         break;
262                case GGML_UNARY_OP_STEP:        op_num = OP_UNARY_NUM_STEP;        break;
263                case GGML_UNARY_OP_HARDSWISH:   op_num = OP_UNARY_NUM_HARDSWISH;   break;
264                case GGML_UNARY_OP_HARDSIGMOID: op_num = OP_UNARY_NUM_HARDSIGMOID; break;
265                case GGML_UNARY_OP_EXP:         op_num = OP_UNARY_NUM_EXP;         break;
266                case GGML_UNARY_OP_SOFTPLUS:    op_num = OP_UNARY_NUM_SOFTPLUS;    break;
267                case GGML_UNARY_OP_EXPM1:       op_num = OP_UNARY_NUM_EXPM1;       break;
268                case GGML_UNARY_OP_FLOOR:       op_num = OP_UNARY_NUM_FLOOR;       break;
269                case GGML_UNARY_OP_CEIL:        op_num = OP_UNARY_NUM_CEIL;        break;
270                case GGML_UNARY_OP_ROUND:       op_num = OP_UNARY_NUM_ROUND;       break;
271                case GGML_UNARY_OP_TRUNC:       op_num = OP_UNARY_NUM_TRUNC;       break;
272                case GGML_UNARY_OP_XIELU:       op_num = OP_UNARY_NUM_XIELU;       break;
273                default: GGML_ABORT("fatal error");
274            } break;
275        default: GGML_ABORT("fatal error");
276    };
277
278    const char * t0_str = ggml_type_name(op->src[0]->type);
279    const char * t_str  = ggml_type_name(op->type);
280
281    const bool is_c4 = op->src[0]->ne[0] % 4 == 0;
282    const bool is_cnt = ggml_is_contiguous(op->src[0]) && ggml_nelements(op) < 32768;
283
284    snprintf(base, 256, "kernel_unary_%s_%s%s", t0_str, t_str, is_c4 ? "_4" : "");
285    snprintf(name, 256, "%s_op=%d_cnt=%d", base, op_num, is_cnt);
286
287    ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name);
288    if (!res.pipeline) {
289        ggml_metal_cv_t cv = ggml_metal_cv_init();
290
291        ggml_metal_cv_set_int16(cv, op_num, FC_UNARY + 0);
292        ggml_metal_cv_set_bool (cv, is_cnt, FC_UNARY + 1);
293
294        res = ggml_metal_library_compile_pipeline(lib, base, name, cv);
295
296        ggml_metal_cv_free(cv);
297    }
298
299    res.c4  = is_c4;
300    res.cnt = is_cnt;
301
302    return res;
303}
304
305ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_glu(ggml_metal_library_t lib, const ggml_tensor * op) {
306    GGML_ASSERT(ggml_is_contiguous_1(op->src[0]));
307
308    char base[256];
309    char name[256];
310
311    const char * op_str = "undefined";
312    switch (op->op) {
313        case GGML_OP_GLU:
314            switch (ggml_get_glu_op(op)) {
315                case GGML_GLU_OP_REGLU:        op_str = "reglu";        break;
316                case GGML_GLU_OP_GEGLU:        op_str = "geglu";        break;
317                case GGML_GLU_OP_SWIGLU:       op_str = "swiglu";       break;
318                case GGML_GLU_OP_SWIGLU_OAI:   op_str = "swiglu_oai";   break;
319                case GGML_GLU_OP_GEGLU_ERF:    op_str = "geglu_erf";    break;
320                case GGML_GLU_OP_GEGLU_QUICK:  op_str = "geglu_quick";  break;
321                case GGML_GLU_OP_SWIGLU_CLAMP: op_str = "swiglu_clamp"; break;
322                default: GGML_ABORT("fatal error");
323            } break;
324        default: GGML_ABORT("fatal error");
325    };
326
327    snprintf(base, 256, "kernel_%s_%s", op_str, ggml_type_name(op->src[0]->type));
328    snprintf(name, 256, "%s", base);
329
330    ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name);
331    if (!res.pipeline) {
332        res = ggml_metal_library_compile_pipeline(lib, base, name, nullptr);
333    }
334
335    return res;
336}
337
338ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_sum(ggml_metal_library_t lib, const ggml_tensor * op) {
339    assert(op->op == GGML_OP_SUM);
340
341    char base[256];
342    char name[256];
343
344    snprintf(base, 256, "kernel_op_sum_%s", ggml_type_name(op->src[0]->type));
345    snprintf(name, 256, "%s", base);
346
347    ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name);
348    if (!res.pipeline) {
349        res = ggml_metal_library_compile_pipeline(lib, base, name, nullptr);
350    }
351
352    return res;
353}
354
355ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_sum_rows(ggml_metal_library_t lib, const ggml_tensor * op) {
356    GGML_ASSERT(ggml_is_contiguous_rows(op->src[0]));
357
358    char base[256];
359    char name[256];
360
361    int op_num = -1;
362
363    switch (op->op) {
364        case GGML_OP_SUM_ROWS: op_num = OP_SUM_ROWS_NUM_SUM_ROWS; break;
365        case GGML_OP_MEAN:     op_num = OP_SUM_ROWS_NUM_MEAN;     break;
366        default: GGML_ABORT("fatal error");
367    };
368
369    const char * t0_str = ggml_type_name(op->src[0]->type);
370    const char * t_str  = ggml_type_name(op->type);
371
372    const bool is_c4 = op->src[0]->ne[0] % 4 == 0;
373
374    snprintf(base, 256, "kernel_sum_rows_%s_%s%s", t0_str, t_str, is_c4 ? "_4" : "");
375    snprintf(name, 256, "%s_op=%d", base, op_num);
376
377    ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name);
378    if (!res.pipeline) {
379        ggml_metal_cv_t cv = ggml_metal_cv_init();
380
381        ggml_metal_cv_set_int16(cv, op_num, FC_SUM_ROWS + 0);
382
383        res = ggml_metal_library_compile_pipeline(lib, base, name, cv);
384
385        ggml_metal_cv_free(cv);
386    }
387
388    res.smem = 32*sizeof(float);
389
390    if (is_c4) {
391        res.smem *= 4;
392    }
393
394    res.c4  = is_c4;
395
396    return res;
397}
398
399ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_cumsum_blk(ggml_metal_library_t lib, const ggml_tensor * op) {
400    GGML_ASSERT(op->op == GGML_OP_CUMSUM);
401
402    char base[256];
403    char name[256];
404
405    snprintf(base, 256, "kernel_cumsum_blk_%s", ggml_type_name(op->src[0]->type));
406    snprintf(name, 256, "%s", base);
407
408    ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name);
409    if (!res.pipeline) {
410        res = ggml_metal_library_compile_pipeline(lib, base, name, nullptr);
411    }
412
413    return res;
414}
415
416ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_cumsum_add(ggml_metal_library_t lib, const ggml_tensor * op) {
417    GGML_ASSERT(op->op == GGML_OP_CUMSUM);
418
419    char base[256];
420    char name[256];
421
422    snprintf(base, 256, "kernel_cumsum_add_%s", ggml_type_name(op->src[0]->type));
423    snprintf(name, 256, "%s", base);
424
425    ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name);
426    if (!res.pipeline) {
427        res = ggml_metal_library_compile_pipeline(lib, base, name, nullptr);
428    }
429
430    return res;
431}
432
433ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_tri(ggml_metal_library_t lib, const ggml_tensor * op) {
434    GGML_ASSERT(op->op == GGML_OP_TRI);
435    GGML_ASSERT(op->src[0]->nb[0] == ggml_type_size(op->src[0]->type));
436
437    char base[256];
438    char name[256];
439
440    const char * op_str = "tri";
441    const int ttype = op->op_params[0];
442
443    snprintf(base, 256, "kernel_%s_%s_%d", op_str, ggml_type_name(op->src[0]->type), ttype);
444
445    snprintf(name, 256, "%s", base);
446
447    ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name);
448    if (!res.pipeline) {
449        res = ggml_metal_library_compile_pipeline(lib, base, name, nullptr);
450    }
451
452    return res;
453}
454
455ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_soft_max(ggml_metal_library_t lib, const ggml_tensor * op) {
456    GGML_ASSERT(!op->src[1] || op->src[1]->type == GGML_TYPE_F16 || op->src[1]->type == GGML_TYPE_F32);
457
458    char base[256];
459    char name[256];
460
461    const char * suffix = "";
462
463    if (op->src[0]->ne[0] % 4 == 0) {
464        suffix = "_4";
465    }
466
467    const ggml_type tsrc1 = op->src[1] ? op->src[1]->type : GGML_TYPE_F32;
468
469    snprintf(base, 256, "kernel_soft_max_%s%s", ggml_type_name(tsrc1), suffix);
470    snprintf(name, 256, "%s", base);
471
472    ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name);
473    if (!res.pipeline) {
474        res = ggml_metal_library_compile_pipeline(lib, base, name, nullptr);
475    }
476
477    res.smem = 32*sizeof(float);
478
479    return res;
480}
481
482ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_lightning_indexer(
483        ggml_metal_library_t lib,
484        const ggml_tensor * op) {
485    GGML_ASSERT(op->op == GGML_OP_LIGHTNING_INDEXER);
486
487    char name[256];
488
489    snprintf(name, 256, "kernel_lightning_indexer_%s", ggml_type_name(op->src[1]->type));
490
491    ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name);
492    if (!res.pipeline) {
493        res = ggml_metal_library_compile_pipeline(lib, name, name, nullptr);
494    }
495
496    return res;
497}
498
499ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_dsv4_hc(ggml_metal_library_t lib, const ggml_tensor * op) {
500    const char * name = nullptr;
501
502    switch (op->op) {
503        case GGML_OP_DSV4_HC_COMB:
504            name = "kernel_dsv4_hc_comb_f32";
505            break;
506        case GGML_OP_DSV4_HC_PRE:
507            if (ggml_get_op_params_i32(op, 1) != 0) {
508                name = "kernel_dsv4_hc_pre_gated_f32";
509            } else {
510                name = "kernel_dsv4_hc_pre_f32";
511            }
512            break;
513        case GGML_OP_DSV4_HC_POST:
514            if (op->src[3]) {
515                name = "kernel_dsv4_hc_post_f32";
516            } else {
517                name = "kernel_dsv4_hc_post_nocomb_f32";
518            }
519            break;
520        default:
521            GGML_ABORT("fatal error");
522    }
523
524    ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name);
525    if (!res.pipeline) {
526        res = ggml_metal_library_compile_pipeline(lib, name, name, nullptr);
527    }
528
529    return res;
530}
531
532ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_ssm_conv(
533        ggml_metal_library_t lib, const ggml_tensor * op, int32_t nc, bool use_silu) {
534    GGML_ASSERT(op->src[0]->type == GGML_TYPE_F32);
535    GGML_ASSERT(op->src[1]->type == GGML_TYPE_F32);
536
537    GGML_ASSERT(ggml_is_contiguous(op->src[0]));
538    GGML_ASSERT(ggml_is_contiguous(op->src[1]));
539
540    char base[256];
541    char name[256];
542
543    const char * suffix = "";
544
545    if (op->src[1]->ne[0] % 4 == 0) {
546        suffix = "_4";
547    }
548
549    snprintf(base, 256, "kernel_ssm_conv_%s_%s%s", ggml_type_name(op->src[0]->type), ggml_type_name(op->src[1]->type), suffix);
550    snprintf(name, 256, "%s_nc=%d_silu=%d", base, nc, use_silu ? 1 : 0);
551
552    ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name);
553    if (!res.pipeline) {
554        ggml_metal_cv_t cv = ggml_metal_cv_init();
555        ggml_metal_cv_set_bool(cv, use_silu, FC_SSM_CONV + 1);
556        ggml_metal_cv_set_int32(cv, nc,      FC_SSM_CONV + 2);
557
558        res = ggml_metal_library_compile_pipeline(lib, base, name, cv);
559
560        ggml_metal_cv_free(cv);
561    }
562
563    return res;
564}
565
566ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_ssm_conv_batched(
567        ggml_metal_library_t lib, const ggml_tensor * op, int ssm_conv_bs, int32_t nc, bool use_silu) {
568    GGML_ASSERT(op->src[0]->type == GGML_TYPE_F32);
569    GGML_ASSERT(op->src[1]->type == GGML_TYPE_F32);
570
571    GGML_ASSERT(ggml_is_contiguous(op->src[0]));
572    GGML_ASSERT(ggml_is_contiguous(op->src[1]));
573
574    char base[256];
575    char name[256];
576
577    const char * suffix = "";
578    if (op->src[1]->ne[0] % 4 == 0) {
579        suffix = "_4";
580    }
581
582    snprintf(base, 256, "kernel_ssm_conv_%s_%s_batched%s", ggml_type_name(op->src[0]->type), ggml_type_name(op->src[1]->type), suffix);
583    snprintf(name, 256, "%s_ssm_conv_bs=%d_nc=%d_silu=%d", base, ssm_conv_bs, nc, use_silu ? 1 : 0);
584
585    ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name);
586    if (!res.pipeline) {
587        ggml_metal_cv_t cv = ggml_metal_cv_init();
588
589        ggml_metal_cv_set_int16(cv, ssm_conv_bs, FC_SSM_CONV + 0);
590        ggml_metal_cv_set_bool(cv, use_silu,     FC_SSM_CONV + 1);
591        ggml_metal_cv_set_int32(cv, nc,          FC_SSM_CONV + 2);
592
593        res = ggml_metal_library_compile_pipeline(lib, base, name, cv);
594
595        ggml_metal_cv_free(cv);
596    }
597
598    return res;
599}
600
601ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_ssm_scan(ggml_metal_library_t lib, const ggml_tensor * op, bool tail)  {
602    GGML_TENSOR_LOCALS( int32_t, ne0, op->src[0], ne);
603
604    char base[256];
605    char name[256];
606
607    const int nsg = (ne00 + 31)/32;
608
609    snprintf(base, 256, "kernel_ssm_scan_%s%s", ggml_type_name(op->src[0]->type), tail ? "_tail" : "");
610    snprintf(name, 256, "%s_nsg=%d", base, nsg);
611
612    ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name);
613    if (!res.pipeline) {
614        res = ggml_metal_library_compile_pipeline(lib, base, name, nullptr);
615    }
616
617    // Shared memory layout:
618    // - sgptg * NW floats for partial sums (nsg * 32)
619    // - sgptg floats for shared_x_dt (nsg)
620    // - sgptg floats for shared_dA (nsg)
621    // Total: nsg * (32 + 2) floats
622    res.smem = GGML_PAD((32 + 2)*sizeof(float)*nsg, 16);
623
624    return res;
625}
626
627ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_ssm_scan_ssd_mma(ggml_metal_library_t lib, const ggml_tensor * op)  {
628    char base[256];
629    char name[256];
630
631    snprintf(base, 256, "kernel_ssm_scan_ssd_mma_%s", ggml_type_name(op->src[0]->type));
632    snprintf(name, 256, "%s", base);
633
634    ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name);
635    if (!res.pipeline) {
636        res = ggml_metal_library_compile_pipeline(lib, base, name, nullptr);
637    }
638
639    // acs/exp(acs)/state-decay vectors + dtX + SAM rows + two 8x8 tiles per simdgroup
640    res.smem = (3*OP_SSM_SCAN_SSD_CS +
641                OP_SSM_SCAN_SSD_CS*OP_SSM_SCAN_SSD_HD +
642                OP_SSM_SCAN_SSD_NSG*8*OP_SSM_SCAN_SSD_CS +
643                OP_SSM_SCAN_SSD_NSG*2*8*8)*sizeof(float);
644
645    return res;
646}
647
648ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_rwkv(ggml_metal_library_t lib, const ggml_tensor * op) {
649    char base[256];
650    char name[256];
651
652    const int64_t C = op->ne[0];
653    const int64_t H = op->src[0]->ne[1];
654
655    switch (op->op) {
656        case GGML_OP_RWKV_WKV6:
657            {
658                GGML_ASSERT(op->src[5]->type == GGML_TYPE_F32);
659                GGML_ASSERT(C % H == 0);
660                GGML_ASSERT(C / H == 64);
661
662                snprintf(base, 256, "kernel_rwkv_wkv6_%s", ggml_type_name(op->src[0]->type));
663            } break;
664        case GGML_OP_RWKV_WKV7:
665            {
666                GGML_ASSERT(op->src[6]->type == GGML_TYPE_F32);
667                GGML_ASSERT(C % H == 0);
668                GGML_ASSERT(C / H == 64);
669
670                snprintf(base, 256, "kernel_rwkv_wkv7_%s", ggml_type_name(op->src[0]->type));
671            } break;
672        default:
673            GGML_ABORT("fatal error");
674    }
675
676    snprintf(name, 256, "%s", base);
677
678    ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name);
679    if (!res.pipeline) {
680        res = ggml_metal_library_compile_pipeline(lib, base, name, nullptr);
681    }
682
683    return res;
684}
685
686ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_gated_delta_net(ggml_metal_library_t lib, const ggml_tensor * op) {
687    char base[256];
688    char name[256];
689
690    // v is src[2], dimensions: S_v = ne[0], H = ne[1]
691    const int ne20 = op->src[2]->ne[0]; // S_v
692    const int ne21 = op->src[2]->ne[1]; // H
693    const int ne30 = op->src[3]->ne[0]; // G
694    // state is src[5], 4D [S_v, S_v, H_v, n_seqs] (s0 only); K is op param 0.
695    const int K = ggml_get_op_params_i32(op, 0);
696
697    const int nsg = op->src[2]->ne[0]/32;
698
699    GGML_ASSERT(op->src[5]->type == GGML_TYPE_F32);
700    GGML_ASSERT(op->ne[0] == ne20 * ne21);
701    GGML_ASSERT(ne20 % 32 == 0);
702
703    snprintf(base, 256, "kernel_gated_delta_net_%s_%d", ggml_type_name(op->src[0]->type), nsg);
704    snprintf(name, 256, "%s_ne20=%d_ne30=%d_K=%d", base, ne20, ne30, K);
705
706    ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name);
707    if (!res.pipeline) {
708        ggml_metal_cv_t cv = ggml_metal_cv_init();
709
710        ggml_metal_cv_set_int16(cv, ne20, FC_GATED_DELTA_NET + 0);
711        ggml_metal_cv_set_int16(cv, ne30, FC_GATED_DELTA_NET + 1);
712        ggml_metal_cv_set_int16(cv, K,    FC_GATED_DELTA_NET + 2);
713
714        res = ggml_metal_library_compile_pipeline(lib, base, name, cv);
715
716        ggml_metal_cv_free(cv);
717    }
718
719    res.nsg = nsg;
720
721    return res;
722}
723
724ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_solve_tri(ggml_metal_library_t lib, const ggml_tensor * op) {
725    char base[256];
726    char name[256];
727
728    const int nsg = 8;
729    const int n   = op->src[1]->ne[1];
730    const int k   = op->src[1]->ne[0];
731
732    snprintf(base, 256, "kernel_solve_tri_%s", ggml_type_name(op->src[0]->type));
733    snprintf(name, 256, "%s_nsg=%d_n=%d_k=%d", base, nsg, n, k);
734
735    ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name);
736    if (!res.pipeline) {
737        ggml_metal_cv_t cv = ggml_metal_cv_init();
738
739        ggml_metal_cv_set_int16(cv, nsg, FC_SOLVE_TRI + 0);
740        ggml_metal_cv_set_int16(cv, n,   FC_SOLVE_TRI + 1);
741        ggml_metal_cv_set_int16(cv, k,   FC_SOLVE_TRI + 2);
742
743        res = ggml_metal_library_compile_pipeline(lib, base, name, cv);
744
745        ggml_metal_cv_free(cv);
746    }
747
748    res.nsg  = nsg;
749    res.smem = GGML_PAD(GGML_PAD(n, 32)*nsg*sizeof(float), 16);
750
751    return res;
752}
753
754ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_mul_mv_ext(ggml_metal_library_t lib, const ggml_tensor * op, int nsg, int nxpsg, int r1ptg) {
755    char base[256];
756    char name[256];
757
758    const ggml_type tsrc0 = op->src[0]->type;
759    const ggml_type tsrc1 = op->src[1]->type;
760    const int       ne12  = op->src[1]->ne[2];
761    const int       r2    = ne12 / op->src[0]->ne[2];
762    const int       r3    = op->src[1]->ne[3] / op->src[0]->ne[3];
763
764    GGML_ASSERT(ne12 <= INT16_MAX && r2 <= INT16_MAX && r3 <= INT16_MAX);
765
766    snprintf(base, 256, "kernel_mul_mv_ext_%s_%s_r1_%d", ggml_type_name(tsrc0), ggml_type_name(tsrc1), r1ptg);
767    snprintf(name, 256, "%s_nsg=%d_nxpsg=%d_ne12=%d_r2=%d_r3=%d", base, nsg, nxpsg, ne12, r2, r3);
768
769    ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name);
770    if (!res.pipeline) {
771        ggml_metal_cv_t cv = ggml_metal_cv_init();
772
773        ggml_metal_cv_set_int16(cv, nsg,            FC_MUL_MV + 0);
774        ggml_metal_cv_set_int16(cv, nxpsg,          FC_MUL_MV + 1);
775        ggml_metal_cv_set_int16(cv, (int16_t) ne12, FC_MUL_MV + 2);
776        ggml_metal_cv_set_int16(cv, (int16_t) r2,   FC_MUL_MV + 3);
777        ggml_metal_cv_set_int16(cv, (int16_t) r3,   FC_MUL_MV + 4);
778
779        res = ggml_metal_library_compile_pipeline(lib, base, name, cv);
780
781        ggml_metal_cv_free(cv);
782    }
783
784    return res;
785}
786
787ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_mul_mm(ggml_metal_library_t lib, const ggml_tensor * op) {
788    char base[256];
789    char name[256];
790
791    const ggml_type tsrc0 = op->src[0]->type;
792    const ggml_type tsrc1 = op->src[1]->type;
793
794    const bool bc_inp = op->src[0]->ne[0] % 32 != 0;
795
796    constexpr int NRA = SZ_SIMDGROUP * N_MM_BLOCK_Y * N_MM_SIMD_GROUP_Y;
797    constexpr int NRB = SZ_SIMDGROUP * N_MM_BLOCK_X * N_MM_SIMD_GROUP_X;
798
799    const bool has_tensor = ggml_metal_device_get_props(ggml_metal_library_get_device(lib))->has_tensor;
800
801    const bool bc_out = has_tensor
802        ? (op->ne[0] % NRA != 0 || op->ne[1] % NRB != 0)
803        : (op->ne[0] % 64  != 0 || op->ne[1] % 32  != 0);
804
805    GGML_ASSERT(op->src[1]->ne[2] <= INT16_MAX && op->src[1]->ne[3] <= INT16_MAX);
806    const int16_t ne12 = (int16_t) op->src[1]->ne[2];
807    const int16_t ne13 = (int16_t) op->src[1]->ne[3];
808    const int16_t r2   = (int16_t) (ne12 / op->src[0]->ne[2]);
809    const int16_t r3   = (int16_t) (ne13 / op->src[0]->ne[3]);
810
811    snprintf(base, 256, "kernel_mul_mm_%s_%s", ggml_type_name(tsrc0), ggml_type_name(tsrc1));
812    snprintf(name, 256, "%s_bci=%d_bco=%d_ne12=%d_ne13=%d_r2=%d_r3=%d",
813             base, bc_inp, bc_out, ne12, ne13, r2, r3);
814
815    ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name);
816    if (!res.pipeline) {
817        ggml_metal_cv_t cv = ggml_metal_cv_init();
818
819        ggml_metal_cv_set_bool(cv, bc_inp, FC_MUL_MM + 0);
820        ggml_metal_cv_set_bool(cv, bc_out, FC_MUL_MM + 1);
821        ggml_metal_cv_set_int16(cv, ne12,  FC_MUL_MM + 2);
822        ggml_metal_cv_set_int16(cv, ne13,  FC_MUL_MM + 3);
823        ggml_metal_cv_set_int16(cv, r2,    FC_MUL_MM + 4);
824        ggml_metal_cv_set_int16(cv, r3,    FC_MUL_MM + 5);
825
826        res = ggml_metal_library_compile_pipeline(lib, base, name, cv);
827
828        ggml_metal_cv_free(cv);
829    }
830
831    if (has_tensor) {
832        res.nr0 = NRA;
833        res.nr1 = NRB;
834
835        const size_t smem_a = NRA * N_MM_NK_TOTAL * sizeof(ggml_fp16_t);
836        res.smem = smem_a;
837    } else {
838        res.nr0 = 64;
839        res.nr1 = 32;
840
841        res.smem = bc_out ? 8192 : (4096 + 2048);
842    }
843
844    res.nsg = N_MM_SIMD_GROUP_X * N_MM_SIMD_GROUP_Y;
845
846    return res;
847}
848
849ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_mul_mv(ggml_metal_library_t lib, const ggml_tensor * op) {
850    GGML_TENSOR_LOCALS( int32_t, ne0, op->src[0], ne);
851    GGML_TENSOR_LOCALS( int32_t, ne1, op->src[1], ne);
852
853    char base[256];
854    char name[256];
855
856    int nsg = 0; // number of simdgroups
857    int nr0 = 0; // number of src0 rows per simdgroup
858    int nr1 = 1; // number of src1 rows per threadgroup
859
860    size_t smem = 0; // shared memory
861
862    const ggml_type tsrc0 = op->src[0]->type;
863    const ggml_type tsrc1 = op->src[1]->type;
864
865    const char * suffix = "";
866
867    bool split = false;
868
869    // use custom matrix x vector kernel
870    switch (tsrc0) {
871        case GGML_TYPE_F32:
872        case GGML_TYPE_F16:
873        case GGML_TYPE_BF16:
874            {
875                if (ne00 < 32) {
876                    nsg = 1;
877                    nr0 = 32;
878                    nr1 = 1;
879                    suffix = "_short";
880                } else {
881                    nsg = std::min(4, (ne00 + 127) / 128);
882                    nr0 = 2;
883                    nr1 = 1;
884                    smem = 32*sizeof(float)*nr0;
885                    suffix = ne00 % 4 == 0 ? "_4" : "";
886                }
887            } break;
888        case GGML_TYPE_Q1_0:
889            {
890                nsg = N_SG_Q1_0;
891                nr0 = N_R0_Q1_0;
892            } break;
893        case GGML_TYPE_Q2_0:
894            {
895                nsg = N_SG_Q2_0;
896                nr0 = N_R0_Q2_0;
897            } break;
898        case GGML_TYPE_Q4_0:
899            {
900                nsg = N_SG_Q4_0;
901                nr0 = N_R0_Q4_0;
902            } break;
903        case GGML_TYPE_Q4_1:
904            {
905                nsg = N_SG_Q4_1;
906                nr0 = N_R0_Q4_1;
907            } break;
908        case GGML_TYPE_Q5_0:
909            {
910                nsg = N_SG_Q5_0;
911                nr0 = N_R0_Q5_0;
912            } break;
913        case GGML_TYPE_Q5_1:
914            {
915                nsg = N_SG_Q5_1;
916                nr0 = N_R0_Q5_1;
917            } break;
918        case GGML_TYPE_Q8_0:
919            {
920                nsg = N_SG_Q8_0;
921                nr0 = N_R0_Q8_0;
922                smem = 32*sizeof(float)*N_R0_Q8_0;
923            } break;
924        case GGML_TYPE_MXFP4:
925            {
926                nsg = N_SG_MXFP4;
927                nr0 = N_R0_MXFP4;
928                smem = 32*sizeof(float);
929            } break;
930        case GGML_TYPE_Q2_K:
931            {
932                nsg = N_SG_Q2_K;
933                nr0 = N_R0_Q2_K;
934            } break;
935        case GGML_TYPE_Q3_K:
936            {
937                nsg = N_SG_Q3_K;
938                nr0 = N_R0_Q3_K;
939            } break;
940        case GGML_TYPE_Q4_K:
941            {
942                nsg = N_SG_Q4_K;
943                nr0 = N_R0_Q4_K;
944            } break;
945        case GGML_TYPE_Q5_K:
946            {
947                nsg = N_SG_Q5_K;
948                nr0 = N_R0_Q5_K;
949            } break;
950        case GGML_TYPE_Q6_K:
951            {
952                nsg = N_SG_Q6_K;
953                nr0 = N_R0_Q6_K;
954            } break;
955        case GGML_TYPE_IQ2_XXS:
956            {
957                nsg = N_SG_IQ2_XXS;
958                nr0 = N_R0_IQ2_XXS;
959                smem = 256*8+128;
960
961                const int nb32 = ne00/32;
962                if (nb32 < 32 && (32 % nb32) == 0) {
963                    nr0 = N_R0_IQ2_XXS_SPLIT;
964                    split = true;
965                }
966            } break;
967        case GGML_TYPE_IQ2_XS:
968            {
969                nsg = N_SG_IQ2_XS;
970                nr0 = N_R0_IQ2_XS;
971                smem = 512*8+128;
972
973                const int nb32 = ne00/32;
974                if (nb32 < 32 && (32 % nb32) == 0) {
975                    nr0 = N_R0_IQ2_XS_SPLIT;
976                    split = true;
977                }
978            } break;
979        case GGML_TYPE_IQ3_XXS:
980            {
981                nsg = N_SG_IQ3_XXS;
982                nr0 = N_R0_IQ3_XXS;
983                smem = 256*4+128;
984
985                // split the rows across threads when there are fewer than 32 chunks per row
986                const int nb32 = ne00/32;
987                if (nb32 < 32 && (32 % nb32) == 0) {
988                    nr0 = N_R0_IQ3_XXS_SPLIT;
989                    split = true;
990                }
991            } break;
992        case GGML_TYPE_IQ3_S:
993            {
994                nsg = N_SG_IQ3_S;
995                nr0 = N_R0_IQ3_S;
996                smem = 512*4;
997
998                const int nb32 = ne00/32;
999                if (nb32 < 32 && (32 % nb32) == 0) {
1000                    nr0 = N_R0_IQ3_S_SPLIT;
1001                    split = true;
1002                }
1003            } break;
1004        case GGML_TYPE_IQ2_S:
1005            {
1006                nsg = N_SG_IQ2_S;
1007                nr0 = N_R0_IQ2_S;
1008
1009                const int nb32 = ne00/32;
1010                if (nb32 < 32 && (32 % nb32) == 0) {
1011                    nr0 = N_R0_IQ2_S_SPLIT;
1012                    split = true;
1013                }
1014            } break;
1015        case GGML_TYPE_IQ1_S:
1016            {
1017                nsg = N_SG_IQ1_S;
1018                nr0 = N_R0_IQ1_S;
1019
1020                const int nb32 = ne00/32;
1021                if (nb32 < 32 && (32 % nb32) == 0) {
1022                    nr0 = N_R0_IQ1_S_SPLIT;
1023                    split = true;
1024                }
1025            } break;
1026        case GGML_TYPE_IQ1_M:
1027            {
1028                nsg = N_SG_IQ1_M;
1029                nr0 = N_R0_IQ1_M;
1030
1031                const int nb32 = ne00/32;
1032                if (nb32 < 32 && (32 % nb32) == 0) {
1033                    nr0 = N_R0_IQ1_M_SPLIT;
1034                    split = true;
1035                }
1036            } break;
1037        case GGML_TYPE_IQ4_NL:
1038            {
1039                nsg = N_SG_IQ4_NL;
1040                nr0 = N_R0_IQ4_NL;
1041                smem = 32*sizeof(float);
1042            } break;
1043        case GGML_TYPE_IQ4_XS:
1044            {
1045                nsg = N_SG_IQ4_XS;
1046                nr0 = N_R0_IQ4_XS;
1047                smem = 32*sizeof(float);
1048            } break;
1049        case GGML_TYPE_TQ2_0:
1050            {
1051                nsg = N_SG_TQ2_0;
1052                nr0 = N_R0_TQ2_0;
1053            } break;
1054        default:
1055            {
1056                GGML_LOG_ERROR("Asserting on type %d\n", (int) tsrc0);
1057                GGML_ABORT("not implemented");
1058            }
1059    };
1060
1061    GGML_ASSERT(ne12 <= INT16_MAX && ne13 <= INT16_MAX);
1062    const int16_t r2 = (int16_t) (ne12 / ne02);
1063    const int16_t r3 = (int16_t) (ne13 / ne03);
1064
1065    snprintf(base, 256, "kernel_mul_mv_%s_%s%s", ggml_type_name(tsrc0), ggml_type_name(tsrc1), suffix);
1066    snprintf(name, 256, "%s_nsg=%d_ne12=%d_r2=%d_r3=%d_split=%d", base, nsg, ne12, r2, r3, split);
1067
1068    ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name);
1069    if (!res.pipeline) {
1070        ggml_metal_cv_t cv = ggml_metal_cv_init();
1071
1072        ggml_metal_cv_set_int16(cv, nsg,            FC_MUL_MV + 0);
1073        ggml_metal_cv_set_int16(cv, (int16_t) ne12, FC_MUL_MV + 2);
1074        ggml_metal_cv_set_int16(cv, r2,             FC_MUL_MV + 3);
1075        ggml_metal_cv_set_int16(cv, r3,             FC_MUL_MV + 4);
1076        ggml_metal_cv_set_bool (cv, split,          FC_MUL_MV + 5);
1077
1078        res = ggml_metal_library_compile_pipeline(lib, base, name, cv);
1079
1080        ggml_metal_cv_free(cv);
1081    }
1082
1083    res.nr0  = nr0;
1084    res.nr1  = nr1;
1085    res.nsg  = nsg;
1086    res.smem = smem;
1087
1088    return res;
1089}
1090
1091ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_mul_mm_id_amax_part(ggml_metal_library_t lib) {
1092    char base[256];
1093    char name[256];
1094
1095    snprintf(base, 256, "kernel_mul_mm_id_amax_part_f32");
1096    snprintf(name, 256, "%s", base);
1097
1098    ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name);
1099    if (!res.pipeline) {
1100        res = ggml_metal_library_compile_pipeline(lib, base, name, nullptr);
1101    }
1102
1103    res.smem = 32*sizeof(float);
1104
1105    return res;
1106}
1107
1108ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_mul_mm_id_amax(ggml_metal_library_t lib) {
1109    char base[256];
1110    char name[256];
1111
1112    snprintf(base, 256, "kernel_mul_mm_id_amax_f32");
1113    snprintf(name, 256, "%s", base);
1114
1115    ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name);
1116    if (!res.pipeline) {
1117        res = ggml_metal_library_compile_pipeline(lib, base, name, nullptr);
1118    }
1119
1120    res.smem = 32*sizeof(float);
1121
1122    return res;
1123}
1124
1125ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_mul_mm_id_map0(ggml_metal_library_t lib, int ne02, int ne20) {
1126    char base[256];
1127    char name[256];
1128
1129    snprintf(base, 256, "kernel_mul_mm_id_map0_ne20_%d", ne20);
1130    snprintf(name, 256, "%s_ne02=%d", base, ne02);
1131
1132    ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name);
1133    if (!res.pipeline) {
1134        res = ggml_metal_library_compile_pipeline(lib, base, name, nullptr);
1135    }
1136
1137    res.smem = (size_t) ne02*ne20*sizeof(uint16_t);
1138    res.smem = GGML_PAD(res.smem, 16);
1139
1140    return res;
1141}
1142
1143ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_mul_mm_id(ggml_metal_library_t lib, const ggml_tensor * op) {
1144    char base[256];
1145    char name[256];
1146
1147    const ggml_type tsrc0 = op->src[0]->type;
1148    const ggml_type tsrc1 = op->src[1]->type;
1149
1150    const bool bc_inp = op->src[0]->ne[0] % 32 != 0;
1151
1152    snprintf(base, 256, "kernel_mul_mm_id_%s_%s", ggml_type_name(tsrc0), ggml_type_name(tsrc1));
1153    snprintf(name, 256, "%s_bci=%d", base, bc_inp);
1154
1155    ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name);
1156    if (!res.pipeline) {
1157        ggml_metal_cv_t cv = ggml_metal_cv_init();
1158
1159        ggml_metal_cv_set_bool(cv, bc_inp, FC_MUL_MM + 0);
1160
1161        res = ggml_metal_library_compile_pipeline(lib, base, name, cv);
1162
1163        ggml_metal_cv_free(cv);
1164    }
1165
1166    res.smem = 8192;
1167
1168    return res;
1169}
1170
1171ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_mul_mv_id(ggml_metal_library_t lib, const ggml_tensor * op) {
1172    GGML_TENSOR_LOCALS( int32_t, ne0, op->src[0], ne);
1173    GGML_TENSOR_LOCALS( int32_t, ne1, op->src[1], ne);
1174
1175    char base[256];
1176    char name[256];
1177
1178    int nsg = 0; // number of simdgroups
1179    int nr0 = 0; // number of src0 rows per simdgroup
1180    int nr1 = 1; // number of src1 rows per threadgroup
1181
1182    size_t smem = 0; // shared memory
1183
1184    const ggml_type tsrc0 = op->src[0]->type;
1185    const ggml_type tsrc1 = op->src[1]->type;
1186
1187    const char * suffix = "";
1188
1189    bool split = false;
1190
1191        // use custom matrix x vector kernel
1192    switch (tsrc0) {
1193        case GGML_TYPE_F32:
1194        case GGML_TYPE_F16:
1195        case GGML_TYPE_BF16:
1196            {
1197                nsg = std::min(4, (ne00 + 127) / 128);
1198                nr0 = 2;
1199                nr1 = 1;
1200                smem = 32*sizeof(float)*nr0;
1201                suffix = ne00 % 4 == 0 ? "_4" : "";
1202            } break;
1203        case GGML_TYPE_Q1_0:
1204            {
1205                nsg = N_SG_Q1_0;
1206                nr0 = N_R0_Q1_0;
1207            } break;
1208        case GGML_TYPE_Q2_0:
1209            {
1210                nsg = N_SG_Q2_0;
1211                nr0 = N_R0_Q2_0;
1212            } break;
1213        case GGML_TYPE_Q4_0:
1214            {
1215                nsg = N_SG_Q4_0;
1216                nr0 = N_R0_Q4_0;
1217            } break;
1218        case GGML_TYPE_Q4_1:
1219            {
1220                nsg = N_SG_Q4_1;
1221                nr0 = N_R0_Q4_1;
1222            } break;
1223        case GGML_TYPE_Q5_0:
1224            {
1225                nsg = N_SG_Q5_0;
1226                nr0 = N_R0_Q5_0;
1227            } break;
1228        case GGML_TYPE_Q5_1:
1229            {
1230                nsg = N_SG_Q5_1;
1231                nr0 = N_R0_Q5_1;
1232            } break;
1233        case GGML_TYPE_Q8_0:
1234            {
1235                nsg = N_SG_Q8_0;
1236                nr0 = N_R0_Q8_0;
1237                smem = 32*sizeof(float)*N_R0_Q8_0;
1238            } break;
1239        case GGML_TYPE_MXFP4:
1240            {
1241                nsg = N_SG_MXFP4;
1242                nr0 = N_R0_MXFP4;
1243                smem = 32*sizeof(float);
1244            } break;
1245        case GGML_TYPE_Q2_K:
1246            {
1247                nsg = N_SG_Q2_K;
1248                nr0 = N_R0_Q2_K;
1249            } break;
1250        case GGML_TYPE_Q3_K:
1251            {
1252                nsg = N_SG_Q3_K;
1253                nr0 = N_R0_Q3_K;
1254            } break;
1255        case GGML_TYPE_Q4_K:
1256            {
1257                nsg = N_SG_Q4_K;
1258                nr0 = N_R0_Q4_K;
1259            } break;
1260        case GGML_TYPE_Q5_K:
1261            {
1262                nsg = N_SG_Q5_K;
1263                nr0 = N_R0_Q5_K;
1264            } break;
1265        case GGML_TYPE_Q6_K:
1266            {
1267                nsg = N_SG_Q6_K;
1268                nr0 = N_R0_Q6_K;
1269            } break;
1270        case GGML_TYPE_IQ2_XXS:
1271            {
1272                nsg = N_SG_IQ2_XXS;
1273                nr0 = N_R0_IQ2_XXS;
1274                smem = 256*8+128;
1275
1276                const int nb32 = ne00/32;
1277                if (nb32 < 32 && (32 % nb32) == 0) {
1278                    nr0 = N_R0_IQ2_XXS_SPLIT;
1279                    split = true;
1280                }
1281            } break;
1282        case GGML_TYPE_IQ2_XS:
1283            {
1284                nsg = N_SG_IQ2_XS;
1285                nr0 = N_R0_IQ2_XS;
1286                smem = 512*8+128;
1287
1288                const int nb32 = ne00/32;
1289                if (nb32 < 32 && (32 % nb32) == 0) {
1290                    nr0 = N_R0_IQ2_XS_SPLIT;
1291                    split = true;
1292                }
1293            } break;
1294        case GGML_TYPE_IQ3_XXS:
1295            {
1296                nsg = N_SG_IQ3_XXS;
1297                nr0 = N_R0_IQ3_XXS;
1298                smem = 256*4+128;
1299
1300                // split the rows across threads when there are fewer than 32 chunks per row
1301                const int nb32 = ne00/32;
1302                if (nb32 < 32 && (32 % nb32) == 0) {
1303                    nr0 = N_R0_IQ3_XXS_SPLIT;
1304                    split = true;
1305                }
1306            } break;
1307        case GGML_TYPE_IQ3_S:
1308            {
1309                nsg = N_SG_IQ3_S;
1310                nr0 = N_R0_IQ3_S;
1311                smem = 512*4;
1312
1313                const int nb32 = ne00/32;
1314                if (nb32 < 32 && (32 % nb32) == 0) {
1315                    nr0 = N_R0_IQ3_S_SPLIT;
1316                    split = true;
1317                }
1318            } break;
1319        case GGML_TYPE_IQ2_S:
1320            {
1321                nsg = N_SG_IQ2_S;
1322                nr0 = N_R0_IQ2_S;
1323
1324                const int nb32 = ne00/32;
1325                if (nb32 < 32 && (32 % nb32) == 0) {
1326                    nr0 = N_R0_IQ2_S_SPLIT;
1327                    split = true;
1328                }
1329            } break;
1330        case GGML_TYPE_IQ1_S:
1331            {
1332                nsg = N_SG_IQ1_S;
1333                nr0 = N_R0_IQ1_S;
1334
1335                const int nb32 = ne00/32;
1336                if (nb32 < 32 && (32 % nb32) == 0) {
1337                    nr0 = N_R0_IQ1_S_SPLIT;
1338                    split = true;
1339                }
1340            } break;
1341        case GGML_TYPE_IQ1_M:
1342            {
1343                nsg = N_SG_IQ1_M;
1344                nr0 = N_R0_IQ1_M;
1345
1346                const int nb32 = ne00/32;
1347                if (nb32 < 32 && (32 % nb32) == 0) {
1348                    nr0 = N_R0_IQ1_M_SPLIT;
1349                    split = true;
1350                }
1351            } break;
1352        case GGML_TYPE_IQ4_NL:
1353            {
1354                nsg = N_SG_IQ4_NL;
1355                nr0 = N_R0_IQ4_NL;
1356                smem = 32*sizeof(float);
1357            } break;
1358        case GGML_TYPE_IQ4_XS:
1359            {
1360                nsg = N_SG_IQ4_XS;
1361                nr0 = N_R0_IQ4_XS;
1362                smem = 32*sizeof(float);
1363            } break;
1364        case GGML_TYPE_TQ2_0:
1365            {
1366                nsg = N_SG_TQ2_0;
1367                nr0 = N_R0_TQ2_0;
1368            } break;
1369        default:
1370            {
1371                GGML_LOG_ERROR("Asserting on type %d\n", (int)op->src[2]->type);
1372                GGML_ABORT("not implemented");
1373            }
1374    };
1375
1376    snprintf(base, 256, "kernel_mul_mv_id_%s_%s%s", ggml_type_name(tsrc0), ggml_type_name(tsrc1), suffix);
1377    snprintf(name, 256, "%s_nsg=%d_split=%d", base, nsg, split);
1378
1379    ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name);
1380    if (!res.pipeline) {
1381        ggml_metal_cv_t cv = ggml_metal_cv_init();
1382
1383        ggml_metal_cv_set_int16(cv, nsg, FC_MUL_MV + 0);
1384        ggml_metal_cv_set_int16(cv, 1,   FC_MUL_MV + 2);
1385        ggml_metal_cv_set_int16(cv, 1,   FC_MUL_MV + 3);
1386        ggml_metal_cv_set_int16(cv, 1,   FC_MUL_MV + 4);
1387        ggml_metal_cv_set_bool (cv, split, FC_MUL_MV + 5);
1388
1389        res = ggml_metal_library_compile_pipeline(lib, base, name, cv);
1390
1391        ggml_metal_cv_free(cv);
1392    }
1393
1394    res.nr0  = nr0;
1395    res.nr1  = nr1;
1396    res.nsg  = nsg;
1397    res.smem = smem;
1398
1399    return res;
1400}
1401
1402ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_argmax(ggml_metal_library_t lib, const ggml_tensor * op) {
1403    GGML_ASSERT(op->src[0]->type == GGML_TYPE_F32);
1404    GGML_ASSERT(ggml_is_contiguous_1(op->src[0]));
1405    GGML_ASSERT(op->src[0]->nb[0] == ggml_type_size(op->src[0]->type));
1406
1407    char base[256];
1408    char name[256];
1409
1410    snprintf(base, 256, "kernel_argmax_%s", ggml_type_name(op->src[0]->type));
1411    snprintf(name, 256, "%s", base);
1412
1413    ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name);
1414    if (!res.pipeline) {
1415        res = ggml_metal_library_compile_pipeline(lib, base, name, nullptr);
1416    }
1417
1418    res.smem = 32*(sizeof(float) + sizeof(int32_t));
1419
1420    return res;
1421}
1422
1423ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_argsort(ggml_metal_library_t lib, const ggml_tensor * op) {
1424    assert(op->op == GGML_OP_ARGSORT);
1425
1426    char base[256];
1427    char name[256];
1428
1429    ggml_sort_order order = (ggml_sort_order) op->op_params[0];
1430
1431    const char * order_str = "undefined";
1432    switch (order) {
1433        case GGML_SORT_ORDER_ASC:  order_str = "asc";  break;
1434        case GGML_SORT_ORDER_DESC: order_str = "desc"; break;
1435        default: GGML_ABORT("fatal error");
1436    };
1437
1438    snprintf(base, 256, "kernel_argsort_%s_%s_%s", ggml_type_name(op->src[0]->type), ggml_type_name(op->type), order_str);
1439    snprintf(name, 256, "%s", base);
1440
1441    ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name);
1442    if (!res.pipeline) {
1443        res = ggml_metal_library_compile_pipeline(lib, base, name, nullptr);
1444    }
1445
1446    return res;
1447}
1448
1449ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_argsort_merge(ggml_metal_library_t lib, const ggml_tensor * op) {
1450    assert(op->op == GGML_OP_ARGSORT);
1451
1452    char base[256];
1453    char name[256];
1454
1455    ggml_sort_order order = (ggml_sort_order) op->op_params[0];
1456
1457    const char * order_str = "undefined";
1458    switch (order) {
1459        case GGML_SORT_ORDER_ASC:  order_str = "asc";  break;
1460        case GGML_SORT_ORDER_DESC: order_str = "desc"; break;
1461        default: GGML_ABORT("fatal error");
1462    };
1463
1464    snprintf(base, 256, "kernel_argsort_merge_%s_%s_%s", ggml_type_name(op->src[0]->type), ggml_type_name(op->type), order_str);
1465    snprintf(name, 256, "%s", base);
1466
1467    ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name);
1468    if (!res.pipeline) {
1469        res = ggml_metal_library_compile_pipeline(lib, base, name, nullptr);
1470    }
1471
1472    return res;
1473}
1474
1475ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_fwht(ggml_metal_library_t lib, int n, ggml_type tsrc) {
1476    char base[256];
1477    char name[256];
1478
1479    snprintf(base, 256, "kernel_fwht_%s_%d", ggml_type_name(tsrc), n);
1480    snprintf(name, 256, "%s", base);
1481
1482    ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name);
1483    if (!res.pipeline) {
1484        res = ggml_metal_library_compile_pipeline(lib, base, name, nullptr);
1485    }
1486
1487    return res;
1488}
1489
1490// note: reuse the argsort kernel for the bitonic top_k fallback
1491ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_top_k(ggml_metal_library_t lib, const ggml_tensor * op) {
1492    assert(op->op == GGML_OP_TOP_K);
1493
1494    char base[256];
1495    char name[256];
1496
1497    // note: the top_k kernel is always descending order
1498    ggml_sort_order order = GGML_SORT_ORDER_DESC;
1499
1500    const char * order_str = "undefined";
1501    switch (order) {
1502        case GGML_SORT_ORDER_ASC:  order_str = "asc";  break;
1503        case GGML_SORT_ORDER_DESC: order_str = "desc"; break;
1504        default: GGML_ABORT("fatal error");
1505    };
1506
1507    snprintf(base, 256, "kernel_argsort_%s_%s_%s", ggml_type_name(op->src[0]->type), ggml_type_name(op->type), order_str);
1508    snprintf(name, 256, "%s", base);
1509
1510    ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name);
1511    if (!res.pipeline) {
1512        res = ggml_metal_library_compile_pipeline(lib, base, name, nullptr);
1513    }
1514
1515    return res;
1516}
1517
1518ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_top_k_radix(ggml_metal_library_t lib, const ggml_tensor * op) {
1519    assert(op->op == GGML_OP_TOP_K);
1520
1521    char base[256];
1522    char name[256];
1523
1524    snprintf(base, 256, "kernel_top_k_%s_%s", ggml_type_name(op->src[0]->type), ggml_type_name(op->type));
1525    snprintf(name, 256, "%s", base);
1526
1527    ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name);
1528    if (!res.pipeline) {
1529        res = ggml_metal_library_compile_pipeline(lib, base, name, nullptr);
1530    }
1531
1532    return res;
1533}
1534
1535ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_top_k_merge(ggml_metal_library_t lib, const ggml_tensor * op) {
1536    assert(op->op == GGML_OP_TOP_K);
1537
1538    char base[256];
1539    char name[256];
1540
1541    ggml_sort_order order = GGML_SORT_ORDER_DESC;
1542
1543    const char * order_str = "undefined";
1544    switch (order) {
1545        case GGML_SORT_ORDER_ASC:  order_str = "asc";  break;
1546        case GGML_SORT_ORDER_DESC: order_str = "desc"; break;
1547        default: GGML_ABORT("fatal error");
1548    };
1549
1550    snprintf(base, 256, "kernel_argsort_merge_%s_%s_%s", ggml_type_name(op->src[0]->type), ggml_type_name(op->type), order_str);
1551    snprintf(name, 256, "%s", base);
1552
1553    ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name);
1554    if (!res.pipeline) {
1555        res = ggml_metal_library_compile_pipeline(lib, base, name, nullptr);
1556    }
1557
1558    return res;
1559}
1560
1561ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_topk_moe(
1562        ggml_metal_library_t lib, int32_t n_expert, int32_t top_k, bool with_norm) {
1563    char base[256];
1564    char name[256];
1565
1566    snprintf(base, 256, "kernel_topk_moe_f32");
1567    snprintf(name, 256, "%s_n_expert=%d_top_k=%d_with_norm=%d", base, n_expert, top_k, with_norm ? 1 : 0);
1568
1569    ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name);
1570    if (!res.pipeline) {
1571        ggml_metal_cv_t cv = ggml_metal_cv_init();
1572        ggml_metal_cv_set_bool (cv, with_norm, FC_TOPK_MOE + 0);
1573        ggml_metal_cv_set_int32(cv, n_expert,  FC_TOPK_MOE + 1);
1574        ggml_metal_cv_set_int32(cv, top_k,     FC_TOPK_MOE + 2);
1575
1576        res = ggml_metal_library_compile_pipeline(lib, base, name, cv);
1577
1578        ggml_metal_cv_free(cv);
1579    }
1580
1581    return res;
1582}
1583
1584ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_moe_reduce(ggml_metal_library_t lib, int32_t n_expert_used) {
1585    char base[256];
1586    char name[256];
1587
1588    snprintf(base, 256, "kernel_moe_reduce_f32");
1589    snprintf(name, 256, "%s_n_expert_used=%d", base, n_expert_used);
1590
1591    ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name);
1592    if (!res.pipeline) {
1593        ggml_metal_cv_t cv = ggml_metal_cv_init();
1594        ggml_metal_cv_set_int32(cv, n_expert_used, FC_MOE_REDUCE + 0);
1595
1596        res = ggml_metal_library_compile_pipeline(lib, base, name, cv);
1597
1598        ggml_metal_cv_free(cv);
1599    }
1600
1601    return res;
1602}
1603
1604ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_flash_attn_ext_pad(
1605        ggml_metal_library_t lib,
1606        const struct ggml_tensor * op,
1607        bool    has_mask,
1608        int32_t ncpsg) {
1609    assert(op->op == GGML_OP_FLASH_ATTN_EXT);
1610    GGML_UNUSED(op);
1611
1612    char base[256];
1613    char name[256];
1614
1615    snprintf(base, 256, "kernel_%s",
1616            "flash_attn_ext_pad");
1617
1618    snprintf(name, 256, "%s_mask=%d_ncpsg=%d",
1619            base,
1620            has_mask,
1621            ncpsg);
1622
1623    ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name);
1624    if (!res.pipeline) {
1625        ggml_metal_cv_t cv = ggml_metal_cv_init();
1626
1627        ggml_metal_cv_set_bool(cv, has_mask,  FC_FLASH_ATTN_EXT_PAD + 0);
1628        //ggml_metal_cv_set_bool(cv, has_sinks, FC_FLASH_ATTN_EXT_PAD + 1);
1629        //ggml_metal_cv_set_bool(cv, has_bias,  FC_FLASH_ATTN_EXT_PAD + 2);
1630        //ggml_metal_cv_set_bool(cv, has_scap,  FC_FLASH_ATTN_EXT_PAD + 3);
1631
1632        //ggml_metal_cv_set_int32(cv, ns10, FC_FLASH_ATTN_EXT_PAD + 20);
1633        //ggml_metal_cv_set_int32(cv, ns20, FC_FLASH_ATTN_EXT_PAD + 21);
1634        //ggml_metal_cv_set_int32(cv, nsg,  FC_FLASH_ATTN_EXT_PAD + 22);
1635        //ggml_metal_cv_set_int32(cv, nwg,  FC_FLASH_ATTN_EXT_PAD + 23);
1636        //ggml_metal_cv_set_int32(cv, nqptg, FC_FLASH_ATTN_EXT_PAD + 24);
1637        ggml_metal_cv_set_int32(cv, ncpsg, FC_FLASH_ATTN_EXT_PAD + 25);
1638
1639        res = ggml_metal_library_compile_pipeline(lib, base, name, cv);
1640
1641        ggml_metal_cv_free(cv);
1642    }
1643
1644    return res;
1645}
1646
1647ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_flash_attn_ext_kv_f16(
1648        ggml_metal_library_t lib,
1649        const ggml_tensor * op) {
1650    assert(op->op == GGML_OP_FLASH_ATTN_EXT);
1651
1652    char base[256];
1653
1654    snprintf(base, 256, "kernel_flash_attn_ext_kv_%s_f16", ggml_type_name(op->src[1]->type));
1655
1656    ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, base);
1657    if (!res.pipeline) {
1658        res = ggml_metal_library_compile_pipeline(lib, base, base, nullptr);
1659    }
1660
1661    return res;
1662}
1663
1664ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_flash_attn_ext_blk(
1665        ggml_metal_library_t lib,
1666        const struct ggml_tensor * op,
1667        int32_t nqptg,
1668        int32_t ncpsg) {
1669    assert(op->op == GGML_OP_FLASH_ATTN_EXT);
1670    GGML_UNUSED(op);
1671
1672    char base[256];
1673    char name[256];
1674
1675    snprintf(base, 256, "kernel_%s",
1676            "flash_attn_ext_blk");
1677
1678    snprintf(name, 256, "%s_nqptg=%d_ncpsg=%d",
1679            base,
1680            nqptg,
1681            ncpsg);
1682
1683    ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name);
1684    if (!res.pipeline) {
1685        ggml_metal_cv_t cv = ggml_metal_cv_init();
1686
1687        //ggml_metal_cv_set_bool(cv, has_mask,  FC_FLASH_ATTN_EXT_BLK + 0);
1688        //ggml_metal_cv_set_bool(cv, has_sinks, FC_FLASH_ATTN_EXT_BLK + 1);
1689        //ggml_metal_cv_set_bool(cv, has_bias,  FC_FLASH_ATTN_EXT_BLK + 2);
1690        //ggml_metal_cv_set_bool(cv, has_scap,  FC_FLASH_ATTN_EXT_BLK + 3);
1691
1692        //ggml_metal_cv_set_int32(cv, ns10, FC_FLASH_ATTN_EXT_BLK + 20);
1693        //ggml_metal_cv_set_int32(cv, ns20, FC_FLASH_ATTN_EXT_BLK + 21);
1694        //ggml_metal_cv_set_int32(cv, nsg,  FC_FLASH_ATTN_EXT_BLK + 22);
1695        //ggml_metal_cv_set_int32(cv, nwg,  FC_FLASH_ATTN_EXT_BLK + 23);
1696        ggml_metal_cv_set_int32(cv, nqptg, FC_FLASH_ATTN_EXT_BLK + 24);
1697        ggml_metal_cv_set_int32(cv, ncpsg, FC_FLASH_ATTN_EXT_BLK + 25);
1698
1699        res = ggml_metal_library_compile_pipeline(lib, base, name, cv);
1700
1701        ggml_metal_cv_free(cv);
1702    }
1703
1704    return res;
1705}
1706
1707ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_flash_attn_ext(
1708        ggml_metal_library_t lib,
1709        const ggml_tensor * op,
1710        bool    has_mask,
1711        bool    has_sinks,
1712        bool    has_bias,
1713        bool    has_scap,
1714        bool    has_kvpad,
1715        int32_t nsg,
1716        bool    use_kv_f16,
1717        int32_t ns10,
1718        int32_t ns20) {
1719    assert(op->op == GGML_OP_FLASH_ATTN_EXT);
1720
1721    char base[256];
1722    char name[256];
1723
1724    const int32_t dk = (int32_t) op->src[1]->ne[0];
1725    const int32_t dv = (int32_t) op->src[2]->ne[0];
1726
1727    const char * type = use_kv_f16 ? "f16" : ggml_type_name(op->src[1]->type);
1728
1729    // do bounds checks for the mask?
1730    const bool bc_mask = op->src[3] && (op->src[3]->ne[1] % 8 != 0);
1731
1732    snprintf(base, 256, "kernel_%s_%s_dk%d_dv%d",
1733            "flash_attn_ext",
1734            type,
1735            dk,
1736            dv);
1737
1738    snprintf(name, 256, "%s_mask=%d_sinks=%d_bias=%d_scap=%d_kvpad=%d_bcm=%d_ns10=%d_ns20=%d_nsg=%d",
1739            base,
1740            has_mask,
1741            has_sinks,
1742            has_bias,
1743            has_scap,
1744            has_kvpad,
1745            bc_mask,
1746            ns10,
1747            ns20,
1748            nsg);
1749
1750    ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name);
1751    if (!res.pipeline) {
1752        ggml_metal_cv_t cv = ggml_metal_cv_init();
1753
1754        ggml_metal_cv_set_bool(cv, has_mask,  FC_FLASH_ATTN_EXT + 0);
1755        ggml_metal_cv_set_bool(cv, has_sinks, FC_FLASH_ATTN_EXT + 1);
1756        ggml_metal_cv_set_bool(cv, has_bias,  FC_FLASH_ATTN_EXT + 2);
1757        ggml_metal_cv_set_bool(cv, has_scap,  FC_FLASH_ATTN_EXT + 3);
1758        ggml_metal_cv_set_bool(cv, has_kvpad, FC_FLASH_ATTN_EXT + 4);
1759
1760        ggml_metal_cv_set_bool(cv, bc_mask, FC_FLASH_ATTN_EXT + 10);
1761
1762        ggml_metal_cv_set_int32(cv, ns10, FC_FLASH_ATTN_EXT + 20);
1763        ggml_metal_cv_set_int32(cv, ns20, FC_FLASH_ATTN_EXT + 21);
1764        ggml_metal_cv_set_int32(cv, nsg,  FC_FLASH_ATTN_EXT + 22);
1765
1766        res = ggml_metal_library_compile_pipeline(lib, base, name, cv);
1767
1768        ggml_metal_cv_free(cv);
1769    }
1770
1771    return res;
1772}
1773
1774ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_flash_attn_ext_vec_idx(
1775        ggml_metal_library_t lib,
1776        const ggml_tensor * op) {
1777    assert(op->op == GGML_OP_FLASH_ATTN_EXT);
1778    assert(op->src[3]);
1779
1780    char name[256];
1781
1782    snprintf(name, 256, "kernel_flash_attn_ext_vec_idx");
1783
1784    ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name);
1785    if (!res.pipeline) {
1786        res = ggml_metal_library_compile_pipeline(lib, name, name, nullptr);
1787    }
1788
1789    GGML_UNUSED(op);
1790
1791    return res;
1792}
1793
1794ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_flash_attn_ext_vec(
1795        ggml_metal_library_t lib,
1796        const ggml_tensor * op,
1797        bool    has_mask,
1798        bool    has_sinks,
1799        bool    has_bias,
1800        bool    has_scap,
1801        bool    has_kvpad,
1802        bool    has_sparse,
1803        int32_t nqpsg,
1804        int32_t ne,
1805        int32_t nsg,
1806        int32_t nwg,
1807        bool    use_kv_f16,
1808        int32_t ns10,
1809        int32_t ns20) {
1810    assert(op->op == GGML_OP_FLASH_ATTN_EXT);
1811
1812    char base[256];
1813    char name[256];
1814
1815    const int32_t dk = (int32_t) op->src[1]->ne[0];
1816    const int32_t dv = (int32_t) op->src[2]->ne[0];
1817
1818    const char * type = use_kv_f16 ? "f16" : ggml_type_name(op->src[1]->type);
1819
1820    char qne_suffix[16] = {0};
1821    if (!(nqpsg == 1 && ne == ggml_metal_tuning::fa_vec_baseline_ne(dk, dv))) {
1822        snprintf(qne_suffix, sizeof(qne_suffix), "_q%d_ne%d", nqpsg, ne);
1823    }
1824
1825    snprintf(base, 256, "kernel_%s_%s_dk%d_dv%d%s",
1826            "flash_attn_ext_vec",
1827            type,
1828            dk,
1829            dv,
1830            qne_suffix);
1831
1832    snprintf(name, 256, "%s_mask=%d_sink=%d_bias=%d_scap=%d_kvpad=%d_sparse=%d_ns10=%d_ns20=%d_nsg=%d_nwg=%d",
1833            base,
1834            has_mask,
1835            has_sinks,
1836            has_bias,
1837            has_scap,
1838            has_kvpad,
1839            has_sparse,
1840            ns10,
1841            ns20,
1842            nsg, nwg);
1843
1844    ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name);
1845    if (!res.pipeline) {
1846        ggml_metal_cv_t cv = ggml_metal_cv_init();
1847
1848        ggml_metal_cv_set_bool(cv, has_mask,  FC_FLASH_ATTN_EXT_VEC + 0);
1849        ggml_metal_cv_set_bool(cv, has_sinks, FC_FLASH_ATTN_EXT_VEC + 1);
1850        ggml_metal_cv_set_bool(cv, has_bias,  FC_FLASH_ATTN_EXT_VEC + 2);
1851        ggml_metal_cv_set_bool(cv, has_scap,  FC_FLASH_ATTN_EXT_VEC + 3);
1852        ggml_metal_cv_set_bool(cv, has_kvpad,  FC_FLASH_ATTN_EXT_VEC + 4);
1853        ggml_metal_cv_set_bool(cv, has_sparse, FC_FLASH_ATTN_EXT_VEC + 5);
1854
1855        ggml_metal_cv_set_int32(cv, ns10, FC_FLASH_ATTN_EXT_VEC + 20);
1856        ggml_metal_cv_set_int32(cv, ns20, FC_FLASH_ATTN_EXT_VEC + 21);
1857        ggml_metal_cv_set_int32(cv, nsg,  FC_FLASH_ATTN_EXT_VEC + 22);
1858        ggml_metal_cv_set_int32(cv, nwg,  FC_FLASH_ATTN_EXT_VEC + 23);
1859
1860        res = ggml_metal_library_compile_pipeline(lib, base, name, cv);
1861
1862        ggml_metal_cv_free(cv);
1863    }
1864
1865    return res;
1866}
1867
1868ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_flash_attn_ext_vec_reduce(
1869        ggml_metal_library_t lib,
1870        const ggml_tensor * op,
1871        int32_t dv,
1872        int32_t nwg) {
1873    assert(op->op == GGML_OP_FLASH_ATTN_EXT);
1874
1875    char base[256];
1876    char name[256];
1877
1878    snprintf(base, 256, "kernel_flash_attn_ext_vec_reduce");
1879    snprintf(name, 256, "%s_dv=%d_nwg=%d", base, dv, nwg);
1880
1881    ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name);
1882    if (!res.pipeline) {
1883        ggml_metal_cv_t cv = ggml_metal_cv_init();
1884
1885        ggml_metal_cv_set_int32(cv, dv,  FC_FLASH_ATTN_EXT_VEC_REDUCE + 0);
1886        ggml_metal_cv_set_int32(cv, nwg, FC_FLASH_ATTN_EXT_VEC_REDUCE + 1);
1887
1888        res = ggml_metal_library_compile_pipeline(lib, base, name, cv);
1889
1890        ggml_metal_cv_free(cv);
1891    }
1892
1893    return res;
1894
1895    GGML_UNUSED(op);
1896}
1897
1898ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_bin(ggml_metal_library_t lib, const ggml_tensor * op, int32_t n_fuse) {
1899    char base[256];
1900    char name[256];
1901
1902    int op_num = -1;
1903
1904    switch (op->op) {
1905        case GGML_OP_ADD: op_num = 0; break;
1906        case GGML_OP_SUB: op_num = 1; break;
1907        case GGML_OP_MUL: op_num = 2; break;
1908        case GGML_OP_DIV: op_num = 3; break;
1909        default: GGML_ABORT("fatal error");
1910    };
1911
1912    const char * t0_str = ggml_type_name(op->src[0]->type);
1913    const char * t1_str = ggml_type_name(op->src[1]->type);
1914    const char * t_str  = ggml_type_name(op->type);
1915
1916    const bool is_c4 = (op->src[0]->ne[0] % 4 == 0) && (op->src[1]->ne[0] % 4 == 0);
1917
1918    const bool is_cb = op->src[0]->ne[0] != op->src[1]->ne[0];
1919    const bool is_rb = ggml_is_contiguous(op->src[0]) && ggml_is_contiguous(op->src[1]) && (ggml_nrows(op->src[1]) == 1) && ggml_nelements(op) < 65536;
1920
1921    snprintf(base, 256, "kernel_bin_fuse_%s_%s_%s%s", t0_str, t1_str, t_str, is_c4 ? "_4" : "");
1922    snprintf(name, 256, "%s_op=%d_nf=%d_rb=%d_cb=%d", base, op_num, n_fuse, is_rb, is_cb);
1923
1924    ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name);
1925    if (!res.pipeline) {
1926        ggml_metal_cv_t cv = ggml_metal_cv_init();
1927
1928        ggml_metal_cv_set_int16(cv, op_num, FC_BIN + 0);
1929        ggml_metal_cv_set_int16(cv, n_fuse, FC_BIN + 1);
1930        ggml_metal_cv_set_bool (cv, is_rb,  FC_BIN + 2);
1931        ggml_metal_cv_set_bool (cv, is_cb,  FC_BIN + 3);
1932
1933        res = ggml_metal_library_compile_pipeline(lib, base, name, cv);
1934
1935        ggml_metal_cv_free(cv);
1936    }
1937
1938    res.c4  = is_c4;
1939    res.cnt = is_rb;
1940
1941    return res;
1942}
1943
1944ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_bin_one(ggml_metal_library_t lib, ggml_op op) {
1945    char base[256];
1946    char name[256];
1947
1948    int op_num = -1;
1949
1950    switch (op) {
1951        case GGML_OP_ADD: op_num = 0; break;
1952        case GGML_OP_SUB: op_num = 1; break;
1953        case GGML_OP_MUL: op_num = 2; break;
1954        case GGML_OP_DIV: op_num = 3; break;
1955        default: GGML_ABORT("fatal error");
1956    };
1957
1958    snprintf(base, 256, "kernel_bin_fuse_%s_%s_%s", "f32", "f32", "f32");
1959    snprintf(name, 256, "%s_op=%d_nf=%d", base, op_num, 1);
1960
1961    ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name);
1962    if (!res.pipeline) {
1963        ggml_metal_cv_t cv = ggml_metal_cv_init();
1964
1965        ggml_metal_cv_set_int16(cv, op_num, FC_BIN + 0);
1966        ggml_metal_cv_set_int16(cv, 1,      FC_BIN + 1);
1967        ggml_metal_cv_set_bool (cv, false,  FC_BIN + 2);
1968
1969        res = ggml_metal_library_compile_pipeline(lib, base, name, cv);
1970
1971        ggml_metal_cv_free(cv);
1972    }
1973
1974    return res;
1975}
1976
1977ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_l2_norm(ggml_metal_library_t lib, const ggml_tensor * op) {
1978    assert(op->op == GGML_OP_L2_NORM);
1979
1980    char base[256];
1981    char name[256];
1982
1983    const bool is_c4 = op->src[0]->ne[0] % 4 == 0;
1984
1985    const char * t0_str = ggml_type_name(op->src[0]->type);
1986    const char * t_str  = ggml_type_name(op->type);
1987
1988    snprintf(base, 256, "kernel_l2_norm_%s_%s%s", t0_str, t_str, is_c4 ? "_4" : "");
1989    snprintf(name, 256, "%s", base);
1990
1991    ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name);
1992    if (!res.pipeline) {
1993        res = ggml_metal_library_compile_pipeline(lib, base, name, nullptr);
1994    }
1995
1996    res.c4   = is_c4;
1997    res.smem = 32*sizeof(float);
1998
1999    return res;
2000}
2001
2002ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_group_norm(ggml_metal_library_t lib, const ggml_tensor * op) {
2003    assert(op->op == GGML_OP_GROUP_NORM);
2004
2005    GGML_ASSERT(ggml_is_contiguous(op->src[0]));
2006
2007    char base[256];
2008    char name[256];
2009
2010    snprintf(base, 256, "kernel_group_norm_f32");
2011    snprintf(name, 256, "%s", base);
2012
2013    ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name);
2014    if (!res.pipeline) {
2015        res = ggml_metal_library_compile_pipeline(lib, base, name, nullptr);
2016    }
2017
2018    res.smem = 32*sizeof(float);
2019
2020    return res;
2021}
2022
2023ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_norm(ggml_metal_library_t lib, const ggml_tensor * op, int n_fuse) {
2024    assert(op->op == GGML_OP_NORM || op->op == GGML_OP_RMS_NORM);
2025
2026    GGML_ASSERT(ggml_is_contiguous_rows(op->src[0]));
2027
2028    char base[256];
2029    char name[256];
2030
2031    const char * suffix = "";
2032    if (op->ne[0] % 4 == 0) {
2033        suffix = "_4";
2034    }
2035
2036    switch (op->op) {
2037        case GGML_OP_NORM:
2038            switch (n_fuse) {
2039                case 1: snprintf(base, 256, "kernel_norm_f32%s", suffix);         break;
2040                case 2: snprintf(base, 256, "kernel_norm_mul_f32%s", suffix);     break;
2041                case 3: snprintf(base, 256, "kernel_norm_mul_add_f32%s", suffix); break;
2042                default: GGML_ABORT("fatal error");
2043            } break;
2044        case GGML_OP_RMS_NORM:
2045            switch (n_fuse) {
2046                case 1: snprintf(base, 256, "kernel_rms_norm_f32%s", suffix);         break;
2047                case 2: snprintf(base, 256, "kernel_rms_norm_mul_f32%s", suffix);     break;
2048                case 3: snprintf(base, 256, "kernel_rms_norm_mul_add_f32%s", suffix); break;
2049                default: GGML_ABORT("fatal error");
2050            } break;
2051        default: GGML_ABORT("fatal error");
2052    }
2053
2054    snprintf(name, 256, "%s", base);
2055
2056    ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name);
2057    if (!res.pipeline) {
2058        ggml_metal_cv_t cv = ggml_metal_cv_init();
2059        ggml_metal_cv_set_bool(cv, false, FC_NORM + 0);
2060
2061        res = ggml_metal_library_compile_pipeline(lib, base, name, cv);
2062
2063        ggml_metal_cv_free(cv);
2064    }
2065
2066    res.smem = 32*sizeof(float);
2067
2068    return res;
2069}
2070
2071ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_norm_scale(ggml_metal_library_t lib, const ggml_tensor * op) {
2072    assert(op->op == GGML_OP_NORM || op->op == GGML_OP_RMS_NORM);
2073
2074    GGML_ASSERT(ggml_is_contiguous_rows(op->src[0]));
2075
2076    char base[256];
2077    char name[256];
2078
2079    const char * suffix = "";
2080    if (op->ne[0] % 4 == 0) {
2081        suffix = "_4";
2082    }
2083
2084    switch (op->op) {
2085        case GGML_OP_NORM:     snprintf(base, 256, "kernel_norm_mul_f32%s", suffix);     break;
2086        case GGML_OP_RMS_NORM: snprintf(base, 256, "kernel_rms_norm_mul_f32%s", suffix); break;
2087        default: GGML_ABORT("fatal error");
2088    }
2089
2090    snprintf(name, 256, "%s_use_scale", base);
2091
2092    ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name);
2093    if (!res.pipeline) {
2094        ggml_metal_cv_t cv = ggml_metal_cv_init();
2095        ggml_metal_cv_set_bool(cv, true, FC_NORM + 0);
2096
2097        res = ggml_metal_library_compile_pipeline(lib, base, name, cv);
2098
2099        ggml_metal_cv_free(cv);
2100    }
2101
2102    res.smem = 32*sizeof(float);
2103
2104    return res;
2105}
2106
2107ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_rope(ggml_metal_library_t lib, const ggml_tensor * op) {
2108    assert(op->op == GGML_OP_ROPE || op->op == GGML_OP_ROPE_BACK);
2109
2110    const bool is_back = op->op == GGML_OP_ROPE_BACK;
2111
2112    char base[256];
2113    char name[256];
2114
2115    const int mode = ((const int32_t *) op->op_params)[2];
2116
2117    const bool is_neox   = mode & GGML_ROPE_TYPE_NEOX;
2118    const bool is_mrope  = mode & GGML_ROPE_TYPE_MROPE;
2119    const bool is_imrope = mode == GGML_ROPE_TYPE_IMROPE;
2120    const bool is_vision = mode == GGML_ROPE_TYPE_VISION;
2121
2122    if (is_neox) {
2123        snprintf(base, 256, "kernel_rope_neox_%s", ggml_type_name(op->src[0]->type));
2124    } else if ((is_mrope || is_imrope) && !is_vision) {
2125        GGML_ASSERT(op->src[1]->ne[0]*4 >= op->src[0]->ne[2]); // need at least 4 pos per token
2126        snprintf(base, 256, "kernel_rope_multi_%s", ggml_type_name(op->src[0]->type));
2127    } else if (is_vision) {
2128        GGML_ASSERT(op->src[1]->ne[0]*4 >= op->src[0]->ne[2]); // need at least 4 pos per token
2129        snprintf(base, 256, "kernel_rope_vision_%s", ggml_type_name(op->src[0]->type));
2130    } else {
2131        snprintf(base, 256, "kernel_rope_norm_%s", ggml_type_name(op->src[0]->type));
2132    }
2133
2134    snprintf(name, 256, "%s_imrope=%d_is_back=%d", base, is_imrope ? 1 : 0, is_back ? 1 : 0);
2135
2136    ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name);
2137    if (!res.pipeline) {
2138        ggml_metal_cv_t cv = ggml_metal_cv_init();
2139
2140        ggml_metal_cv_set_bool(cv, is_imrope, FC_ROPE + 0);
2141        ggml_metal_cv_set_bool(cv, is_back,   FC_ROPE + 1);
2142
2143        res = ggml_metal_library_compile_pipeline(lib, base, name, cv);
2144
2145        ggml_metal_cv_free(cv);
2146    }
2147
2148    return res;
2149}
2150
2151ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_im2col(ggml_metal_library_t lib, const ggml_tensor * op) {
2152    assert(op->op == GGML_OP_IM2COL);
2153
2154    GGML_TENSOR_LOCALS(int64_t, ne0, op->src[0], ne);
2155
2156    GGML_ASSERT(ggml_is_contiguous(op->src[1]));
2157    GGML_ASSERT(op->src[1]->type == GGML_TYPE_F32);
2158    GGML_ASSERT(op->type         == GGML_TYPE_F16 || op->type == GGML_TYPE_F32);
2159
2160    const bool is_2D = ((const int32_t *)(op->op_params))[6] == 1;
2161    const int64_t KH = is_2D ? ne01 : 1;
2162    const int64_t KW = ne00;
2163
2164    char base[256];
2165    char name[256];
2166
2167    if (KH*KW <= 1024) {
2168        snprintf(base, 256, "kernel_im2col_%s", ggml_type_name(op->type));
2169    } else {
2170        snprintf(base, 256, "kernel_im2col_ext_%s", ggml_type_name(op->type));
2171    }
2172    snprintf(name, 256, "%s", base);
2173
2174    ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name);
2175    if (!res.pipeline) {
2176        res = ggml_metal_library_compile_pipeline(lib, base, name, nullptr);
2177    }
2178
2179    return res;
2180}
2181
2182ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_conv_transpose_1d(ggml_metal_library_t lib, const ggml_tensor * op) {
2183    assert(op->op == GGML_OP_CONV_TRANSPOSE_1D);
2184
2185    GGML_ASSERT(ggml_is_contiguous(op->src[0]));
2186    GGML_ASSERT(ggml_is_contiguous(op->src[1]));
2187    GGML_ASSERT(op->src[0]->type == GGML_TYPE_F16 || op->src[0]->type == GGML_TYPE_F32);
2188    GGML_ASSERT(op->src[1]->type == GGML_TYPE_F32);
2189    GGML_ASSERT(op->type         == GGML_TYPE_F32);
2190
2191    char base[256];
2192    char name[256];
2193
2194    snprintf(base, 256, "kernel_conv_transpose_1d_%s_%s", ggml_type_name(op->src[0]->type), ggml_type_name(op->src[1]->type));
2195    snprintf(name, 256, "%s", base);
2196
2197    ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name);
2198    if (!res.pipeline) {
2199        res = ggml_metal_library_compile_pipeline(lib, base, name, nullptr);
2200    }
2201
2202    return res;
2203}
2204
2205ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_col2im_1d(ggml_metal_library_t lib, const ggml_tensor * op) {
2206    assert(op->op == GGML_OP_COL2IM_1D);
2207
2208    GGML_ASSERT(ggml_is_contiguous(op->src[0]));
2209    GGML_ASSERT(op->src[0]->type == GGML_TYPE_F32 || op->src[0]->type == GGML_TYPE_F16 || op->src[0]->type == GGML_TYPE_BF16);
2210
2211    char base[256];
2212    char name[256];
2213
2214    snprintf(base, 256, "kernel_col2im_1d_%s", ggml_type_name(op->src[0]->type));
2215    snprintf(name, 256, "%s", base);
2216
2217    ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name);
2218    if (!res.pipeline) {
2219        res = ggml_metal_library_compile_pipeline(lib, base, name, nullptr);
2220    }
2221
2222    return res;
2223}
2224
2225ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_snake(ggml_metal_library_t lib, enum ggml_type type) {
2226    GGML_ASSERT(type == GGML_TYPE_F32 || type == GGML_TYPE_F16 || type == GGML_TYPE_BF16);
2227
2228    char base[256];
2229    char name[256];
2230
2231    snprintf(base, 256, "kernel_snake_%s", ggml_type_name(type));
2232    snprintf(name, 256, "%s", base);
2233
2234    ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name);
2235    if (!res.pipeline) {
2236        res = ggml_metal_library_compile_pipeline(lib, base, name, nullptr);
2237    }
2238
2239    return res;
2240}
2241
2242ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_conv_transpose_2d(ggml_metal_library_t lib, const ggml_tensor * op) {
2243    assert(op->op == GGML_OP_CONV_TRANSPOSE_2D);
2244
2245    GGML_ASSERT(ggml_is_contiguous(op->src[0]));
2246    GGML_ASSERT(ggml_is_contiguous(op->src[1]));
2247    GGML_ASSERT(op->src[0]->type == GGML_TYPE_F16 || op->src[0]->type == GGML_TYPE_F32);
2248    GGML_ASSERT(op->src[1]->type == GGML_TYPE_F32);
2249    GGML_ASSERT(op->type         == GGML_TYPE_F32);
2250
2251    char base[256];
2252    char name[256];
2253
2254    snprintf(base, 256, "kernel_conv_transpose_2d_%s_%s", ggml_type_name(op->src[0]->type), ggml_type_name(op->src[1]->type));
2255    snprintf(name, 256, "%s", base);
2256
2257    ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name);
2258    if (!res.pipeline) {
2259        res = ggml_metal_library_compile_pipeline(lib, base, name, nullptr);
2260    }
2261
2262    return res;
2263}
2264
2265ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_conv_2d(ggml_metal_library_t lib, const ggml_tensor * op) {
2266    assert(op->op == GGML_OP_CONV_2D);
2267
2268    GGML_ASSERT(ggml_is_contiguous(op->src[0]));
2269    GGML_ASSERT(op->src[0]->type == GGML_TYPE_F16 || op->src[0]->type == GGML_TYPE_F32);
2270    GGML_ASSERT(op->src[1]->type == GGML_TYPE_F32);
2271    GGML_ASSERT(op->type         == GGML_TYPE_F32);
2272
2273    char base[256];
2274    char name[256];
2275
2276    snprintf(base, 256, "kernel_conv_2d_%s_%s", ggml_type_name(op->src[0]->type), ggml_type_name(op->src[1]->type));
2277    snprintf(name, 256, "%s", base);
2278
2279    ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name);
2280    if (!res.pipeline) {
2281        res = ggml_metal_library_compile_pipeline(lib, base, name, nullptr);
2282    }
2283
2284    return res;
2285}
2286
2287ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_conv_2d_dw(ggml_metal_library_t lib, const ggml_tensor * op, bool tiled) {
2288    assert(op->op == GGML_OP_CONV_2D_DW);
2289
2290    GGML_ASSERT(op->src[0]->type == GGML_TYPE_F16 || op->src[0]->type == GGML_TYPE_F32);
2291    GGML_ASSERT(op->src[1]->type == GGML_TYPE_F32);
2292    GGML_ASSERT(op->type         == GGML_TYPE_F32);
2293
2294    char base[256];
2295    char name[256];
2296
2297    snprintf(base, 256, "kernel_conv_2d_dw%s_%s_%s",
2298             tiled ? "_tiled" : "",
2299             ggml_type_name(op->src[0]->type), ggml_type_name(op->src[1]->type));
2300    snprintf(name, 256, "%s", base);
2301
2302    ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name);
2303    if (!res.pipeline) {
2304        res = ggml_metal_library_compile_pipeline(lib, base, name, nullptr);
2305    }
2306
2307    return res;
2308}
2309
2310ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_conv_3d(ggml_metal_library_t lib, const ggml_tensor * op) {
2311    assert(op->op == GGML_OP_CONV_3D);
2312
2313    GGML_ASSERT(ggml_is_contiguous(op->src[0]));
2314    GGML_ASSERT(op->src[0]->type == GGML_TYPE_F16 || op->src[0]->type == GGML_TYPE_F32);
2315    GGML_ASSERT(op->src[1]->type == GGML_TYPE_F32);
2316    GGML_ASSERT(op->type         == GGML_TYPE_F32);
2317
2318    char base[256];
2319    char name[256];
2320
2321    snprintf(base, 256, "kernel_conv_3d_%s_%s", ggml_type_name(op->src[0]->type), ggml_type_name(op->src[1]->type));
2322    snprintf(name, 256, "%s", base);
2323
2324    ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name);
2325    if (!res.pipeline) {
2326        res = ggml_metal_library_compile_pipeline(lib, base, name, nullptr);
2327    }
2328
2329    return res;
2330}
2331
2332ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_upscale(ggml_metal_library_t lib, const ggml_tensor * op) {
2333    assert(op->op == GGML_OP_UPSCALE);
2334
2335    char base[256];
2336    char name[256];
2337
2338    const int32_t mode_flags = ggml_get_op_params_i32(op, 0);
2339    const ggml_scale_mode mode = (ggml_scale_mode) (mode_flags & 0xFF);
2340
2341    const bool antialias = (mode_flags & GGML_SCALE_FLAG_ANTIALIAS);
2342
2343    if (mode == GGML_SCALE_MODE_BILINEAR) {
2344        snprintf(base, 256, "kernel_upscale_bilinear_%s", ggml_type_name(op->src[0]->type));
2345    } else if (mode == GGML_SCALE_MODE_BICUBIC) {
2346        snprintf(base, 256, "kernel_upscale_bicubic_%s", ggml_type_name(op->src[0]->type));
2347    } else {
2348        snprintf(base, 256, "kernel_upscale_nearest_%s", ggml_type_name(op->src[0]->type));
2349    }
2350    snprintf(name, 256, "%s_aa=%d", base, antialias);
2351
2352    ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name);
2353    if (!res.pipeline) {
2354        ggml_metal_cv_t cv = ggml_metal_cv_init();
2355
2356        ggml_metal_cv_set_bool(cv, antialias, FC_UPSCALE + 0);
2357
2358        res = ggml_metal_library_compile_pipeline(lib, base, name, cv);
2359
2360        ggml_metal_cv_free(cv);
2361    }
2362
2363    return res;
2364}
2365
2366ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_roll(ggml_metal_library_t lib, const ggml_tensor * op) {
2367    assert(op->op == GGML_OP_ROLL);
2368
2369    char base[256];
2370    char name[256];
2371
2372    snprintf(base, 256, "kernel_roll_%s", ggml_type_name(op->src[0]->type));
2373    snprintf(name, 256, "%s", base);
2374
2375    ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name);
2376    if (!res.pipeline) {
2377        res = ggml_metal_library_compile_pipeline(lib, base, name, nullptr);
2378    }
2379
2380    return res;
2381}
2382
2383ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_pad(ggml_metal_library_t lib, const ggml_tensor * op) {
2384    assert(op->op == GGML_OP_PAD);
2385
2386    char base[256];
2387    char name[256];
2388
2389    // note: this is slower
2390    //const bool is_c4 = op->src[0]->ne[0] % 4 == 0 && op->ne[0] % 4 == 0;
2391    const bool is_c4 = false;
2392
2393    snprintf(base, 256, "kernel_pad_%s%s", ggml_type_name(op->src[0]->type), is_c4 ? "_4" : "");
2394    snprintf(name, 256, "%s", base);
2395
2396    ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name);
2397    if (res.pipeline) {
2398        return res;
2399    }
2400
2401    res = ggml_metal_library_compile_pipeline(lib, base, name, nullptr);
2402
2403    res.c4 = is_c4;
2404
2405    return res;
2406}
2407
2408ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_pad_reflect_1d(ggml_metal_library_t lib, const ggml_tensor * op) {
2409    assert(op->op == GGML_OP_PAD_REFLECT_1D);
2410
2411    char base[256];
2412    char name[256];
2413
2414    snprintf(base, 256, "kernel_pad_reflect_1d_%s", ggml_type_name(op->src[0]->type));
2415    snprintf(name, 256, "%s", base);
2416
2417    ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name);
2418    if (!res.pipeline) {
2419        res = ggml_metal_library_compile_pipeline(lib, base, name, nullptr);
2420    }
2421
2422    return res;
2423}
2424
2425ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_arange(ggml_metal_library_t lib, const ggml_tensor * op) {
2426    assert(op->op == GGML_OP_ARANGE);
2427
2428    char base[256];
2429    char name[256];
2430
2431    snprintf(base, 256, "kernel_arange_%s", ggml_type_name(op->type));
2432    snprintf(name, 256, "%s", base);
2433
2434    ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name);
2435    if (!res.pipeline) {
2436        res = ggml_metal_library_compile_pipeline(lib, base, name, nullptr);
2437    }
2438
2439    return res;
2440}
2441
2442ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_timestep_embedding(ggml_metal_library_t lib, const ggml_tensor * op) {
2443    assert(op->op == GGML_OP_TIMESTEP_EMBEDDING);
2444
2445    char base[256];
2446    char name[256];
2447
2448    snprintf(base, 256, "kernel_timestep_embedding_%s", ggml_type_name(op->src[0]->type));
2449    snprintf(name, 256, "%s", base);
2450
2451    ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name);
2452    if (!res.pipeline) {
2453        res = ggml_metal_library_compile_pipeline(lib, base, name, nullptr);
2454    }
2455
2456    return res;
2457}
2458
2459ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_opt_step_adamw(ggml_metal_library_t lib, const ggml_tensor * op) {
2460    assert(op->op == GGML_OP_OPT_STEP_ADAMW);
2461
2462    char base[256];
2463    char name[256];
2464
2465    snprintf(base, 256, "kernel_opt_step_adamw_%s", ggml_type_name(op->src[0]->type));
2466    snprintf(name, 256, "%s", base);
2467
2468    ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name);
2469    if (!res.pipeline) {
2470        res = ggml_metal_library_compile_pipeline(lib, base, name, nullptr);
2471    }
2472
2473    return res;
2474}
2475
2476ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_opt_step_sgd(ggml_metal_library_t lib, const ggml_tensor * op) {
2477    assert(op->op == GGML_OP_OPT_STEP_SGD);
2478
2479    char base[256];
2480    char name[256];
2481
2482    snprintf(base, 256, "kernel_opt_step_sgd_%s", ggml_type_name(op->src[0]->type));
2483    snprintf(name, 256, "%s", base);
2484
2485    ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name);
2486    if (!res.pipeline) {
2487        res = ggml_metal_library_compile_pipeline(lib, base, name, nullptr);
2488    }
2489
2490    return res;
2491}
2492
2493ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_silu_back(ggml_metal_library_t lib, const ggml_tensor * op) {
2494    assert(op->op == GGML_OP_SILU_BACK);
2495
2496    char base[256];
2497    char name[256];
2498
2499    snprintf(base, 256, "kernel_silu_back_%s", ggml_type_name(op->src[0]->type));
2500    snprintf(name, 256, "%s", base);
2501
2502    ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name);
2503    if (!res.pipeline) {
2504        res = ggml_metal_library_compile_pipeline(lib, base, name, nullptr);
2505    }
2506
2507    return res;
2508}
2509
2510ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_memset(ggml_metal_library_t lib, const ggml_tensor *  op) {
2511    GGML_ASSERT(op->type == GGML_TYPE_I64);
2512
2513    char base[256];
2514    char name[256];
2515
2516    snprintf(base, 256, "kernel_memset_%s", ggml_type_name(op->type));
2517    snprintf(name, 256, "%s", base);
2518
2519    ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name);
2520    if (!res.pipeline) {
2521        res = ggml_metal_library_compile_pipeline(lib, base, name, nullptr);
2522    }
2523
2524    return res;
2525}
2526
2527ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_count_equal(ggml_metal_library_t lib, const ggml_tensor *  op) {
2528    assert(op->op == GGML_OP_COUNT_EQUAL);
2529
2530    GGML_TENSOR_LOCALS(int64_t, ne0, op->src[0], ne);
2531
2532    GGML_ASSERT(op->src[0]->type == op->src[1]->type);
2533    GGML_ASSERT(op->src[0]->type == GGML_TYPE_I32);
2534    GGML_ASSERT(op->type == GGML_TYPE_I64);
2535
2536    // note: the kernel only supports i32 output due to metal atomic add only supporting atomic_int
2537    GGML_ASSERT(ggml_nelements(op->src[0]) < (1LL << 31));
2538
2539    char base[256];
2540    char name[256];
2541
2542    int nsg = 1;
2543    while (32*nsg < ne00 && nsg < 32) {
2544        nsg *= 2;
2545    }
2546
2547    snprintf(base, 256, "kernel_count_equal_%s", ggml_type_name(op->src[0]->type));
2548    snprintf(name, 256, "%s_nsg=%d", base, nsg);
2549
2550    ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name);
2551    if (!res.pipeline) {
2552        ggml_metal_cv_t cv = ggml_metal_cv_init();
2553
2554        ggml_metal_cv_set_int16(cv, nsg, FC_COUNT_EQUAL + 0);
2555
2556        res = ggml_metal_library_compile_pipeline(lib, base, name, cv);
2557
2558        ggml_metal_cv_free(cv);
2559    }
2560
2561    res.smem = 32 * sizeof(int32_t);
2562    res.nsg  = nsg;
2563
2564    return res;
2565}