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