okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
b2de2641f
main
1#ifndef GGML_METAL_IMPL 2#define GGML_METAL_IMPL 3 4// kernel parameters for mat-mat threadgroups 5// 6// TODO: become function constants 7 8#define SZ_SIMDGROUP 16 9#define N_MM_NK 2 10#define N_MM_NK_TOTAL (SZ_SIMDGROUP * N_MM_NK) 11 12#define N_MM_BLOCK_X 4 13#define N_MM_BLOCK_Y 2 14#define N_MM_SIMD_GROUP_X 2 15#define N_MM_SIMD_GROUP_Y 2 16 17#define N_MM_NPART_AMAX 256 18 19// kernel parameters for mat-vec threadgroups 20// 21// N_R0: number of src0 rows to process per simdgroup 22// N_SG: number of simdgroups per threadgroup 23// 24// TODO: for optimal performance, become function of the device and work size 25 26#define N_R0_Q1_0 8 27#define N_SG_Q1_0 2 28 29#define N_R0_Q2_0 8 30#define N_SG_Q2_0 2 31 32#define N_R0_Q4_0 4 33#define N_SG_Q4_0 2 34 35#define N_R0_Q4_1 4 36#define N_SG_Q4_1 2 37 38#define N_R0_Q5_0 4 39#define N_SG_Q5_0 2 40 41#define N_R0_Q5_1 4 42#define N_SG_Q5_1 2 43 44#define N_R0_Q8_0 2 45#define N_SG_Q8_0 4 46 47#define N_R0_MXFP4 2 48#define N_SG_MXFP4 2 49 50#define N_R0_Q2_K 4 51#define N_SG_Q2_K 2 52 53#define N_R0_Q3_K 2 54#define N_SG_Q3_K 2 55 56#define N_R0_Q4_K 2 57#define N_SG_Q4_K 2 58 59#define N_R0_Q5_K 1 60#define N_SG_Q5_K 2 61 62#define N_R0_Q6_K 2 63#define N_SG_Q6_K 2 64 65#define N_R0_IQ1_S 4 66#define N_SG_IQ1_S 2 67#define N_R0_IQ1_S_SPLIT 8 68 69#define N_R0_IQ1_M 4 70#define N_SG_IQ1_M 2 71#define N_R0_IQ1_M_SPLIT 8 72 73#define N_R0_IQ2_XXS 4 74#define N_SG_IQ2_XXS 2 75#define N_R0_IQ2_XXS_SPLIT 8 76 77#define N_R0_IQ2_XS 4 78#define N_SG_IQ2_XS 2 79#define N_R0_IQ2_XS_SPLIT 8 80 81#define N_R0_IQ2_S 4 82#define N_SG_IQ2_S 2 83#define N_R0_IQ2_S_SPLIT 8 84 85#define N_R0_IQ3_XXS 4 86#define N_SG_IQ3_XXS 2 87#define N_R0_IQ3_XXS_SPLIT 8 88 89#define N_R0_IQ3_S 4 90#define N_SG_IQ3_S 2 91#define N_R0_IQ3_S_SPLIT 8 92 93#define N_R0_IQ4_NL 2 94#define N_SG_IQ4_NL 2 95 96#define N_R0_IQ4_XS 2 97#define N_SG_IQ4_XS 2 98 99#define N_R0_TQ2_0 4 100#define N_SG_TQ2_0 2 101 102// function constants offsets 103#define FC_FLASH_ATTN_EXT_PAD 100 104#define FC_FLASH_ATTN_EXT_BLK 200 105#define FC_FLASH_ATTN_EXT 300 106#define FC_FLASH_ATTN_EXT_VEC 400 107#define FC_FLASH_ATTN_EXT_VEC_REDUCE 500 108#define FC_MUL_MV 600 109#define FC_MUL_MM 700 110#define FC_ROPE 800 111#define FC_SSM_CONV 900 112#define FC_SOLVE_TRI 1000 113#define FC_COUNT_EQUAL 1100 114#define FC_UNARY 1200 115#define FC_BIN 1300 116#define FC_SUM_ROWS 1400 117#define FC_UPSCALE 1500 118#define FC_GATED_DELTA_NET 1600 119#define FC_NORM 1700 120#define FC_TOPK_MOE 1800 121#define FC_MOE_REDUCE 1900 122 123// op-specific constants 124#define OP_FLASH_ATTN_EXT_NQPSG 8 125#define OP_FLASH_ATTN_EXT_NCPSG 64 126 127#define OP_FLASH_ATTN_EXT_VEC_NQPSG 1 128#define OP_FLASH_ATTN_EXT_VEC_NCPSG 32 129 130#define OP_LIGHTNING_INDEXER_DK 128 131#define OP_LIGHTNING_INDEXER_NH 64 132#define OP_LIGHTNING_INDEXER_NHPTG 8 133#define OP_LIGHTNING_INDEXER_NKPSG 8 134#define OP_LIGHTNING_INDEXER_NSG 8 135#define OP_LIGHTNING_INDEXER_NBPTG 8 136 137#define OP_UNARY_NUM_SCALE 10 138#define OP_UNARY_NUM_FILL 11 139#define OP_UNARY_NUM_CLAMP 12 140#define OP_UNARY_NUM_SQR 13 141#define OP_UNARY_NUM_SQRT 14 142#define OP_UNARY_NUM_SIN 15 143#define OP_UNARY_NUM_COS 16 144#define OP_UNARY_NUM_LOG 17 145#define OP_UNARY_NUM_LEAKY_RELU 18 146 147#define OP_UNARY_NUM_TANH 100 148#define OP_UNARY_NUM_RELU 101 149#define OP_UNARY_NUM_SIGMOID 102 150#define OP_UNARY_NUM_GELU 103 151#define OP_UNARY_NUM_GELU_ERF 104 152#define OP_UNARY_NUM_GELU_QUICK 105 153#define OP_UNARY_NUM_SILU 106 154#define OP_UNARY_NUM_ELU 107 155#define OP_UNARY_NUM_NEG 108 156#define OP_UNARY_NUM_ABS 109 157#define OP_UNARY_NUM_SGN 110 158#define OP_UNARY_NUM_STEP 111 159#define OP_UNARY_NUM_HARDSWISH 112 160#define OP_UNARY_NUM_HARDSIGMOID 113 161#define OP_UNARY_NUM_EXP 114 162#define OP_UNARY_NUM_SOFTPLUS 115 163#define OP_UNARY_NUM_EXPM1 116 164#define OP_UNARY_NUM_FLOOR 117 165#define OP_UNARY_NUM_CEIL 118 166#define OP_UNARY_NUM_ROUND 119 167#define OP_UNARY_NUM_TRUNC 120 168#define OP_UNARY_NUM_XIELU 121 169 170#define OP_SUM_ROWS_NUM_SUM_ROWS 10 171#define OP_SUM_ROWS_NUM_MEAN 11 172 173#define OP_SSM_SCAN_SSD_CS 64 // Metal-specific; Chunk Size; 64 is largest multiple of 8 (simdgroup tile) fitting into 32 KiB Metal threadgroup mem limit (~26.75 KiB shared mem; see smem layout comment in kernel_ssm_scan_ssd_mma_f32) 174#define OP_SSM_SCAN_SSD_HD 64 // Metal-specific; Head Dim the MMA kernel is specialized for (Mamba-2); use_mma gates on d_inner == this 175#define OP_SSM_SCAN_SSD_NSG 4 // Metal-specific; Number of SimdGroups per threadgroup; NSG*32 == threads dispatched per threadgroup 176 177// kernel argument structs 178// 179// - element counters (e.g. ne00) typically use int32_t to reduce register usage 180// however, be careful from int overflows when using those in the kernel implementation 181// 182// - strides (e.g. nb00) use uint64_t 183 184typedef struct { 185int32_t ne00 ; 186int32_t ne01 ; 187int32_t ne02 ; 188int32_t ne03 ; 189uint64_t nb00 ; 190uint64_t nb01 ; 191uint64_t nb02 ; 192uint64_t nb03 ; 193int32_t ne10 ; 194int32_t ne11 ; 195int32_t ne12 ; 196int32_t ne13 ; 197uint64_t nb10 ; 198uint64_t nb11 ; 199uint64_t nb12 ; 200uint64_t nb13 ; 201int32_t ne0 ; 202int32_t ne1 ; 203int32_t ne2 ; 204int32_t ne3 ; 205uint64_t nb0 ; 206uint64_t nb1 ; 207uint64_t nb2 ; 208uint64_t nb3 ; 209int32_t dim ; 210}ggml_metal_kargs_concat ; 211 212typedef struct { 213int32_t ne00 ; 214int32_t ne01 ; 215int32_t ne02 ; 216int32_t ne03 ; 217uint64_t nb00 ; 218uint64_t nb01 ; 219uint64_t nb02 ; 220uint64_t nb03 ; 221int32_t ne0 ; 222int32_t ne1 ; 223int32_t ne2 ; 224int32_t ne3 ; 225uint64_t nb0 ; 226uint64_t nb1 ; 227uint64_t nb2 ; 228uint64_t nb3 ; 229float slope ; 230float scale ; 231float bias ; 232float val ; 233float min ; 234float max ; 235}ggml_metal_kargs_unary ; 236 237typedef struct { 238int32_t ne00 ; 239int32_t ne01 ; 240int32_t ne02 ; 241int32_t ne03 ; 242uint64_t nb00 ; 243uint64_t nb01 ; 244uint64_t nb02 ; 245uint64_t nb03 ; 246int32_t ne10 ; 247int32_t ne11 ; 248int32_t ne12 ; 249int32_t ne13 ; 250uint64_t nb10 ; 251uint64_t nb11 ; 252uint64_t nb12 ; 253uint64_t nb13 ; 254int32_t ne0 ; 255int32_t ne1 ; 256int32_t ne2 ; 257int32_t ne3 ; 258uint64_t nb0 ; 259uint64_t nb1 ; 260uint64_t nb2 ; 261uint64_t nb3 ; 262uint64_t offs ; 263uint64_t o1 [8 ]; 264}ggml_metal_kargs_bin ; 265 266typedef struct { 267int64_t ne0 ; 268int64_t ne1 ; 269size_t nb01 ; 270size_t nb02 ; 271size_t nb11 ; 272size_t nb21 ; 273}ggml_metal_kargs_add_id ; 274 275typedef struct { 276int32_t ne00 ; 277int32_t ne01 ; 278int32_t ne02 ; 279int32_t ne03 ; 280uint64_t nb00 ; 281uint64_t nb01 ; 282uint64_t nb02 ; 283uint64_t nb03 ; 284int32_t ne0 ; 285int32_t ne1 ; 286int32_t ne2 ; 287int32_t ne3 ; 288uint64_t nb0 ; 289uint64_t nb1 ; 290uint64_t nb2 ; 291uint64_t nb3 ; 292}ggml_metal_kargs_repeat ; 293 294typedef struct { 295int64_t nk0 ; 296int64_t ne00 ; 297int64_t ne01 ; 298int64_t ne02 ; 299int64_t ne03 ; 300uint64_t nb00 ; 301uint64_t nb01 ; 302uint64_t nb02 ; 303uint64_t nb03 ; 304int64_t ne0 ; 305int64_t ne1 ; 306int64_t ne2 ; 307int64_t ne3 ; 308uint64_t nb0 ; 309uint64_t nb1 ; 310uint64_t nb2 ; 311uint64_t nb3 ; 312}ggml_metal_kargs_cpy ; 313 314typedef struct { 315int64_t ne10 ; 316int64_t ne11 ; 317int64_t ne12 ; 318uint64_t nb10 ; 319uint64_t nb11 ; 320uint64_t nb12 ; 321uint64_t nb13 ; 322uint64_t nb1 ; 323uint64_t nb2 ; 324uint64_t nb3 ; 325uint64_t offs ; 326bool inplace ; 327}ggml_metal_kargs_set ; 328 329typedef struct { 330int32_t ne00 ; 331int32_t ne01 ; 332int32_t ne02 ; 333int32_t ne03 ; 334uint64_t nb00 ; 335uint64_t nb01 ; 336uint64_t nb02 ; 337uint64_t nb03 ; 338int32_t ne0 ; 339int32_t ne1 ; 340int32_t ne2 ; 341int32_t ne3 ; 342uint64_t nb0 ; 343uint64_t nb1 ; 344uint64_t nb2 ; 345uint64_t nb3 ; 346int32_t n_past ; 347int32_t n_dims ; 348int32_t n_offs ; 349int32_t n_ctx_orig ; 350float freq_base ; 351float freq_scale ; 352float ext_factor ; 353float attn_factor ; 354float beta_fast ; 355float beta_slow ; 356int32_t sect_0 ; 357int32_t sect_1 ; 358int32_t sect_2 ; 359int32_t sect_3 ; 360bool src2 ; 361bool inplace ; 362}ggml_metal_kargs_rope ; 363 364typedef struct { 365int32_t ne0 ; 366int32_t ne1 ; 367int32_t ne2 ; 368int32_t ne3 ; 369uint64_t nb0 ; 370uint64_t nb1 ; 371uint64_t nb2 ; 372uint64_t nb3 ; 373int32_t nblocks ; 374}ggml_metal_kargs_flash_attn_ext_kv_f16 ; 375 376typedef struct { 377int32_t ne11 ; 378int32_t ne_12_2 ;// assume K and V are same shape 379int32_t ne_12_3 ; 380uint64_t nb11 ; 381uint64_t nb12 ; 382uint64_t nb13 ; 383uint64_t nb21 ; 384uint64_t nb22 ; 385uint64_t nb23 ; 386int32_t ne31 ; 387int32_t ne32 ; 388int32_t ne33 ; 389uint64_t nb31 ; 390uint64_t nb32 ; 391uint64_t nb33 ; 392}ggml_metal_kargs_flash_attn_ext_pad ; 393 394typedef struct { 395int32_t ne01 ; 396int32_t ne30 ; 397int32_t ne31 ; 398int32_t ne32 ; 399int32_t ne33 ; 400uint64_t nb31 ; 401uint64_t nb32 ; 402uint64_t nb33 ; 403}ggml_metal_kargs_flash_attn_ext_blk ; 404 405typedef struct { 406int32_t ne01 ; 407int32_t ne02 ; 408int32_t ne03 ; 409uint64_t nb01 ; 410uint64_t nb02 ; 411uint64_t nb03 ; 412int32_t ne11 ; 413int32_t ne_12_2 ;// assume K and V are same shape 414int32_t ne_12_3 ; 415int32_t ns10 ; 416uint64_t nb11 ; 417uint64_t nb12 ; 418uint64_t nb13 ; 419int32_t ns20 ; 420uint64_t nb21 ; 421uint64_t nb22 ; 422uint64_t nb23 ; 423int32_t ne31 ; 424int32_t ne32 ; 425int32_t ne33 ; 426uint64_t nb31 ; 427uint64_t nb32 ; 428uint64_t nb33 ; 429int32_t ne1 ; 430int32_t ne2 ; 431int32_t ne3 ; 432float scale ; 433float max_bias ; 434float m0 ; 435float m1 ; 436int32_t n_head_log2 ; 437float logit_softcap ; 438}ggml_metal_kargs_flash_attn_ext ; 439 440typedef struct { 441int32_t ne01 ; 442int32_t ne02 ; 443int32_t ne03 ; 444uint64_t nb01 ; 445uint64_t nb02 ; 446uint64_t nb03 ; 447int32_t ne11 ; 448int32_t ne_12_2 ;// assume K and V are same shape 449int32_t ne_12_3 ; 450int32_t ns10 ; 451uint64_t nb11 ; 452uint64_t nb12 ; 453uint64_t nb13 ; 454int32_t ns20 ; 455uint64_t nb21 ; 456uint64_t nb22 ; 457uint64_t nb23 ; 458int32_t ne31 ; 459int32_t ne32 ; 460int32_t ne33 ; 461uint64_t nb31 ; 462uint64_t nb32 ; 463uint64_t nb33 ; 464int32_t ne1 ; 465int32_t ne2 ; 466int32_t ne3 ; 467float scale ; 468float max_bias ; 469float m0 ; 470float m1 ; 471int32_t n_head_log2 ; 472float logit_softcap ; 473int32_t n_kv_max_padded ; 474}ggml_metal_kargs_flash_attn_ext_vec ; 475 476typedef struct { 477int32_t ne30 ; 478int32_t ne31 ; 479int32_t ne32 ; 480int32_t ne33 ; 481uint64_t nb31 ; 482uint64_t nb32 ; 483uint64_t nb33 ; 484int32_t n_kv_max ; 485int32_t n_kv_max_padded ; 486}ggml_metal_kargs_flash_attn_ext_vec_idx ; 487 488typedef struct { 489int32_t nrows ; 490}ggml_metal_kargs_flash_attn_ext_vec_reduce ; 491 492typedef struct { 493int32_t ne00 ; 494int32_t ne02 ; 495uint64_t nb01 ; 496uint64_t nb02 ; 497uint64_t nb03 ; 498int32_t ne12 ; 499uint64_t nb10 ; 500uint64_t nb11 ; 501uint64_t nb12 ; 502uint64_t nb13 ; 503int32_t ne0 ; 504int32_t ne1 ; 505int16_t r2 ; 506int16_t r3 ; 507}ggml_metal_kargs_mul_mm ; 508 509typedef struct { 510int32_t ne00 ; 511int32_t ne01 ; 512int32_t ne02 ; 513uint64_t nb00 ; 514uint64_t nb01 ; 515uint64_t nb02 ; 516uint64_t nb03 ; 517int32_t ne10 ; 518int32_t ne11 ; 519int32_t ne12 ; 520uint64_t nb10 ; 521uint64_t nb11 ; 522uint64_t nb12 ; 523uint64_t nb13 ; 524int32_t ne0 ; 525int32_t ne1 ; 526int32_t nr0 ; 527int16_t r2 ; 528int16_t r3 ; 529}ggml_metal_kargs_mul_mv ; 530 531typedef struct { 532int32_t ne00 ; 533int32_t ne01 ; 534int32_t ne02 ; 535uint64_t nb00 ; 536uint64_t nb01 ; 537uint64_t nb02 ; 538uint64_t nb03 ; 539int32_t ne10 ; 540int32_t ne11 ; 541int32_t ne12 ; 542uint64_t nb10 ; 543uint64_t nb11 ; 544uint64_t nb12 ; 545uint64_t nb13 ; 546int32_t ne0 ; 547int32_t ne1 ; 548int16_t r2 ; 549int16_t r3 ; 550}ggml_metal_kargs_mul_mv_ext ; 551 552typedef struct { 553int32_t ne02 ; 554int32_t ne10 ; 555int32_t ne11 ;// n_expert_used (bcast) 556uint64_t nb11 ; 557uint64_t nb12 ; 558int32_t ne21 ;// n_tokens 559int32_t ne20 ;// n_expert_used 560uint64_t nb21 ; 561}ggml_metal_kargs_mul_mm_id_map0 ; 562 563typedef struct { 564int32_t ne00 ; 565int32_t ne01 ; 566int32_t ne02 ; 567uint64_t nb01 ; 568uint64_t nb02 ; 569}ggml_metal_kargs_mul_mm_id_amax ; 570 571typedef struct { 572int32_t ne00 ; 573int32_t ne02 ; 574uint64_t nb01 ; 575uint64_t nb02 ; 576uint64_t nb03 ; 577int32_t ne11 ; 578uint64_t nb10 ; 579uint64_t nb11 ; 580uint64_t nb12 ; 581uint64_t nb13 ; 582int32_t ne20 ; 583int32_t ne21 ; 584int32_t ne0 ; 585int32_t ne1 ; 586int16_t r2 ; 587int16_t r3 ; 588}ggml_metal_kargs_mul_mm_id ; 589 590typedef struct { 591int32_t nei0 ; 592int32_t nei1 ; 593uint64_t nbi1 ; 594int32_t ne00 ; 595int32_t ne01 ; 596int32_t ne02 ; 597uint64_t nb00 ; 598uint64_t nb01 ; 599uint64_t nb02 ; 600int32_t ne10 ; 601int32_t ne11 ; 602int32_t ne12 ; 603int32_t ne13 ; 604uint64_t nb10 ; 605uint64_t nb11 ; 606uint64_t nb12 ; 607int32_t ne0 ; 608int32_t ne1 ; 609uint64_t nb1 ; 610int32_t nr0 ; 611}ggml_metal_kargs_mul_mv_id ; 612 613// NORM 614// RMS_NORM 615typedef struct { 616int32_t ne00 ; 617int32_t ne00_t ; 618uint64_t nb1 ; 619uint64_t nb2 ; 620uint64_t nb3 ; 621float eps ; 622int32_t nef1 [3 ]; 623int32_t nef2 [3 ]; 624int32_t nef3 [3 ]; 625uint64_t nbf1 [3 ]; 626uint64_t nbf2 [3 ]; 627uint64_t nbf3 [3 ]; 628float scale ; 629}ggml_metal_kargs_norm ; 630 631typedef struct { 632int32_t ne00 ; 633int32_t ne01 ; 634int32_t ne02 ; 635int32_t ne03 ; 636uint64_t nb00 ; 637uint64_t nb01 ; 638uint64_t nb02 ; 639uint64_t nb03 ; 640int32_t ne0 ; 641int32_t ne1 ; 642int32_t ne2 ; 643int32_t ne3 ; 644uint64_t nb0 ; 645uint64_t nb1 ; 646uint64_t nb2 ; 647uint64_t nb3 ; 648float eps ; 649}ggml_metal_kargs_l2_norm ; 650 651typedef struct { 652int64_t ne00 ; 653int64_t ne01 ; 654int64_t ne02 ; 655uint64_t nb00 ; 656uint64_t nb01 ; 657uint64_t nb02 ; 658int32_t ngrp ; 659float eps ; 660}ggml_metal_kargs_group_norm ; 661 662typedef struct { 663int32_t IC ; 664int32_t IL ; 665int32_t K ; 666int32_t s0 ; 667uint64_t nb0 ; 668uint64_t nb1 ; 669}ggml_metal_kargs_conv_transpose_1d ; 670 671typedef struct { 672int32_t T_in ; 673int32_t T_out ; 674int32_t OC ; 675int32_t K ; 676int32_t K_OC ; 677int32_t s0 ; 678int32_t p0 ; 679}ggml_metal_kargs_col2im_1d ; 680 681typedef struct { 682int32_t T ; 683int32_t C ; 684}ggml_metal_kargs_snake ; 685 686typedef struct { 687int32_t IC ; 688int32_t IH ; 689int32_t IW ; 690int32_t KH ; 691int32_t KW ; 692int32_t OC ; 693int32_t s0 ; 694uint64_t nb0 ; 695uint64_t nb1 ; 696uint64_t nb2 ; 697uint64_t nb3 ; 698}ggml_metal_kargs_conv_transpose_2d ; 699 700typedef struct { 701uint64_t nb00 ; 702uint64_t nb01 ; 703uint64_t nb02 ; 704uint64_t nb03 ; 705uint64_t nb10 ; 706uint64_t nb11 ; 707uint64_t nb12 ; 708uint64_t nb13 ; 709uint64_t nb0 ; 710uint64_t nb1 ; 711uint64_t nb2 ; 712uint64_t nb3 ; 713int32_t IW ; 714int32_t IH ; 715int32_t KW ; 716int32_t KH ; 717int32_t IC ; 718int32_t OC ; 719int32_t OW ; 720int32_t OH ; 721int32_t N ; 722int32_t s0 ; 723int32_t s1 ; 724int32_t p0 ; 725int32_t p1 ; 726int32_t d0 ; 727int32_t d1 ; 728}ggml_metal_kargs_conv_2d ; 729 730typedef struct { 731uint64_t nb00 ;// kernel strides 732uint64_t nb01 ; 733uint64_t nb02 ; 734uint64_t nb10 ;// input strides 735uint64_t nb11 ; 736uint64_t nb12 ; 737uint64_t nb13 ; 738uint64_t nb0 ;// output strides 739uint64_t nb1 ; 740uint64_t nb2 ; 741uint64_t nb3 ; 742int32_t IW ;// input width 743int32_t IH ;// input height 744int32_t KW ;// kernel width 745int32_t KH ;// kernel height 746int32_t C ;// channels (IC == OC for depthwise) 747int32_t OW ;// output width 748int32_t OH ;// output height 749int32_t N ;// batch size 750int32_t s0 ;// stride x 751int32_t s1 ;// stride y 752int32_t p0 ;// padding x 753int32_t p1 ;// padding y 754int32_t d0 ;// dilation x 755int32_t d1 ;// dilation y 756}ggml_metal_kargs_conv_2d_dw ; 757 758typedef struct { 759uint64_t ofs0 ; 760uint64_t ofs1 ; 761int32_t IW ; 762int32_t IH ; 763int32_t CHW ; 764int32_t s0 ; 765int32_t s1 ; 766int32_t p0 ; 767int32_t p1 ; 768int32_t d0 ; 769int32_t d1 ; 770int32_t N ; 771int32_t KH ; 772int32_t KW ; 773int32_t KHW ;// KH * KW, pre-computed on CPU to save GPU resources 774}ggml_metal_kargs_im2col ; 775 776typedef struct { 777int32_t IW ; 778int32_t IH ; 779int32_t ID ; 780int32_t OW ; 781int32_t OH ; 782int32_t OD ; 783int32_t KW ; 784int32_t KH ; 785int32_t KD ; 786int32_t s0 ; 787int32_t s1 ; 788int32_t s2 ; 789int32_t p0 ; 790int32_t p1 ; 791int32_t p2 ; 792int32_t d0 ; 793int32_t d1 ; 794int32_t d2 ; 795int32_t IC ; 796int32_t N ; 797int32_t OC ; 798uint64_t nb00 ; 799uint64_t nb01 ; 800uint64_t nb02 ; 801uint64_t nb03 ; 802uint64_t nb10 ; 803uint64_t nb11 ; 804uint64_t nb12 ; 805uint64_t nb13 ; 806uint64_t nb0 ; 807uint64_t nb1 ; 808uint64_t nb2 ; 809uint64_t nb3 ; 810}ggml_metal_kargs_conv_3d ; 811 812typedef struct { 813int32_t ne00 ; 814uint64_t nb01 ; 815int32_t ne10 ; 816uint64_t nb11 ; 817int32_t ne0 ; 818uint64_t nb1 ; 819int32_t i00 ; 820int32_t i10 ; 821float alpha ; 822float limit ; 823}ggml_metal_kargs_glu ; 824 825typedef struct { 826uint64_t np ; 827}ggml_metal_kargs_sum ; 828 829typedef struct { 830int64_t ne00 ; 831int64_t ne01 ; 832int64_t ne02 ; 833int64_t ne03 ; 834uint64_t nb00 ; 835uint64_t nb01 ; 836uint64_t nb02 ; 837uint64_t nb03 ; 838int64_t ne0 ; 839int64_t ne1 ; 840int64_t ne2 ; 841int64_t ne3 ; 842uint64_t nb0 ; 843uint64_t nb1 ; 844uint64_t nb2 ; 845uint64_t nb3 ; 846}ggml_metal_kargs_sum_rows ; 847 848typedef struct { 849int64_t ne00 ; 850int64_t ne01 ; 851int64_t ne02 ; 852int64_t ne03 ; 853uint64_t nb00 ; 854uint64_t nb01 ; 855uint64_t nb02 ; 856uint64_t nb03 ; 857int64_t net0 ; 858int64_t net1 ; 859int64_t net2 ; 860int64_t net3 ; 861uint64_t nbt0 ; 862uint64_t nbt1 ; 863uint64_t nbt2 ; 864uint64_t nbt3 ; 865bool outb ; 866}ggml_metal_kargs_cumsum_blk ; 867 868typedef struct { 869int64_t ne00 ; 870int64_t ne01 ; 871int64_t ne02 ; 872int64_t ne03 ; 873uint64_t nb00 ; 874uint64_t nb01 ; 875uint64_t nb02 ; 876uint64_t nb03 ; 877int64_t net0 ; 878int64_t net1 ; 879int64_t net2 ; 880int64_t net3 ; 881uint64_t nbt0 ; 882uint64_t nbt1 ; 883uint64_t nbt2 ; 884uint64_t nbt3 ; 885}ggml_metal_kargs_cumsum_add ; 886 887typedef struct { 888int32_t ne00 ; 889int32_t ne01 ; 890int32_t ne02 ; 891uint64_t nb01 ; 892uint64_t nb02 ; 893uint64_t nb03 ; 894int32_t ne11 ; 895int32_t ne12 ; 896int32_t ne13 ; 897uint64_t nb11 ; 898uint64_t nb12 ; 899uint64_t nb13 ; 900uint64_t nb1 ; 901uint64_t nb2 ; 902uint64_t nb3 ; 903float scale ; 904float max_bias ; 905float m0 ; 906float m1 ; 907int32_t n_head_log2 ; 908}ggml_metal_kargs_soft_max ; 909 910typedef struct { 911int64_t ne00 ; 912int64_t ne01 ; 913int64_t ne02 ; 914uint64_t nb00 ; 915uint64_t nb01 ; 916uint64_t nb02 ; 917int64_t ne11 ; 918uint64_t nb10 ; 919uint64_t nb11 ; 920int64_t ne0 ; 921int64_t ne1 ; 922int64_t ne2 ; 923uint64_t nb0 ; 924uint64_t nb1 ; 925uint64_t nb2 ; 926}ggml_metal_kargs_ssm_conv ; 927 928typedef struct { 929int64_t d_state ; 930int64_t d_inner ; 931int64_t n_head ; 932int64_t n_group ; 933int64_t n_seq_tokens ; 934int64_t n_seq_tokens_total ; 935int64_t token_offset ; 936int64_t n_seqs ; 937int64_t K ; 938uint64_t s_off ; 939uint64_t nb00 ; 940uint64_t nb01 ; 941uint64_t nb02 ; 942uint64_t nb03 ; 943uint64_t nb10 ; 944uint64_t nb11 ; 945uint64_t nb12 ; 946uint64_t ns12 ; 947uint64_t nb13 ; 948uint64_t nb20 ; 949uint64_t nb21 ; 950uint64_t ns21 ; 951uint64_t nb22 ; 952int64_t ne30 ; 953uint64_t nb31 ; 954uint64_t nb41 ; 955uint64_t nb42 ; 956uint64_t ns42 ; 957uint64_t nb43 ; 958uint64_t nb51 ; 959uint64_t nb52 ; 960uint64_t ns52 ; 961uint64_t nb53 ; 962uint64_t nb0 ; 963}ggml_metal_kargs_ssm_scan ; 964 965typedef struct { 966int32_t ne00 ; 967int32_t ne01 ; 968int32_t ne02 ; 969int32_t ne03 ; 970uint64_t nb00 ; 971uint64_t nb01 ; 972uint64_t nb02 ; 973uint64_t nb03 ; 974int32_t ne10 ; 975int32_t ne11 ; 976int32_t ne12 ; 977int32_t ne13 ; 978uint64_t nb10 ; 979uint64_t nb11 ; 980uint64_t nb12 ; 981uint64_t nb13 ; 982int32_t ne20 ; 983int32_t ne21 ; 984int32_t ne22 ; 985int32_t ne23 ; 986uint64_t nb20 ; 987uint64_t nb21 ; 988uint64_t nb22 ; 989uint64_t nb23 ; 990int32_t ns02 ; 991int32_t ns12 ; 992int32_t ns22 ; 993int32_t ne0 ; 994int32_t ne1 ; 995int32_t ne2 ; 996int32_t ne3 ; 997uint64_t nb0 ; 998uint64_t nb1 ; 999uint64_t nb2 ; 1000uint64_t nb3 ; 1001uint64_t nb_out ;// 0 => snapshots are appended after the attn scores (unfused) 1002}ggml_metal_kargs_gated_delta_net ; 1003 1004typedef struct { 1005int32_t ne00 ; 1006int32_t ne01 ; 1007int32_t ne02 ; 1008int32_t ne03 ; 1009uint64_t nb00 ; 1010uint64_t nb01 ; 1011uint64_t nb02 ; 1012uint64_t nb03 ; 1013int32_t ne10 ; 1014int32_t ne11 ; 1015int32_t ne12 ; 1016int32_t ne13 ; 1017uint64_t nb10 ; 1018uint64_t nb11 ; 1019uint64_t nb12 ; 1020uint64_t nb13 ; 1021int32_t ne0 ; 1022int32_t ne1 ; 1023int32_t ne2 ; 1024int32_t ne3 ; 1025uint64_t nb0 ; 1026uint64_t nb1 ; 1027uint64_t nb2 ; 1028uint64_t nb3 ; 1029}ggml_metal_kargs_solve_tri ; 1030 1031typedef struct { 1032int32_t ne00t ; 1033int32_t ne00 ; 1034uint64_t nb01 ; 1035uint64_t nb02 ; 1036uint64_t nb03 ; 1037int32_t ne10 ; 1038uint64_t nb10 ; 1039uint64_t nb11 ; 1040uint64_t nb12 ; 1041uint64_t nb1 ; 1042uint64_t nb2 ; 1043uint64_t nb3 ; 1044}ggml_metal_kargs_get_rows ; 1045 1046typedef struct { 1047int32_t nk0 ; 1048int32_t ne01 ; 1049uint64_t nb01 ; 1050uint64_t nb02 ; 1051uint64_t nb03 ; 1052int32_t ne11 ; 1053int32_t ne12 ; 1054uint64_t nb10 ; 1055uint64_t nb11 ; 1056uint64_t nb12 ; 1057uint64_t nb1 ; 1058uint64_t nb2 ; 1059uint64_t nb3 ; 1060}ggml_metal_kargs_set_rows ; 1061 1062typedef struct { 1063int32_t ne00 ; 1064int32_t ne01 ; 1065int32_t ne02 ; 1066int32_t ne03 ; 1067uint64_t nb00 ; 1068uint64_t nb01 ; 1069uint64_t nb02 ; 1070uint64_t nb03 ; 1071int32_t ne0 ; 1072int32_t ne1 ; 1073int32_t ne2 ; 1074int32_t ne3 ; 1075uint64_t nb0 ; 1076uint64_t nb1 ; 1077uint64_t nb2 ; 1078uint64_t nb3 ; 1079}ggml_metal_kargs_diag ; 1080 1081typedef struct { 1082int64_t ne00 ; 1083int64_t ne01 ; 1084int64_t ne02 ; 1085int64_t ne03 ; 1086uint64_t nb00 ; 1087uint64_t nb01 ; 1088uint64_t nb02 ; 1089uint64_t nb03 ; 1090int64_t ne0 ; 1091int64_t ne1 ; 1092int64_t ne2 ; 1093int64_t ne3 ; 1094uint64_t nb0 ; 1095uint64_t nb1 ; 1096uint64_t nb2 ; 1097uint64_t nb3 ; 1098float sf0 ; 1099float sf1 ; 1100float sf2 ; 1101float sf3 ; 1102float poffs ; 1103}ggml_metal_kargs_upscale ; 1104 1105typedef struct { 1106int64_t ne00 ; 1107int64_t ne01 ; 1108int64_t ne02 ; 1109int64_t ne03 ; 1110uint64_t nb00 ; 1111uint64_t nb01 ; 1112uint64_t nb02 ; 1113uint64_t nb03 ; 1114int64_t ne0 ; 1115int64_t ne1 ; 1116int64_t ne2 ; 1117int64_t ne3 ; 1118uint64_t nb0 ; 1119uint64_t nb1 ; 1120uint64_t nb2 ; 1121uint64_t nb3 ; 1122}ggml_metal_kargs_pad ; 1123 1124typedef struct { 1125int64_t ne00 ; 1126int64_t ne01 ; 1127int64_t ne02 ; 1128int64_t ne03 ; 1129uint64_t nb00 ; 1130uint64_t nb01 ; 1131uint64_t nb02 ; 1132uint64_t nb03 ; 1133int64_t ne0 ; 1134int64_t ne1 ; 1135int64_t ne2 ; 1136int64_t ne3 ; 1137uint64_t nb0 ; 1138uint64_t nb1 ; 1139uint64_t nb2 ; 1140uint64_t nb3 ; 1141int32_t p0 ; 1142int32_t p1 ; 1143}ggml_metal_kargs_pad_reflect_1d ; 1144 1145typedef struct { 1146int64_t ne00 ; 1147int64_t ne01 ; 1148int64_t ne02 ; 1149int64_t ne03 ; 1150uint64_t nb00 ; 1151uint64_t nb01 ; 1152uint64_t nb02 ; 1153uint64_t nb03 ; 1154int64_t ne0 ; 1155int64_t ne1 ; 1156int64_t ne2 ; 1157int64_t ne3 ; 1158uint64_t nb0 ; 1159uint64_t nb1 ; 1160uint64_t nb2 ; 1161uint64_t nb3 ; 1162int32_t s0 ; 1163int32_t s1 ; 1164int32_t s2 ; 1165int32_t s3 ; 1166}ggml_metal_kargs_roll ; 1167 1168typedef struct { 1169uint64_t nb1 ; 1170int dim ; 1171int max_period ; 1172}ggml_metal_kargs_timestep_embedding ; 1173 1174typedef struct { 1175int32_t ne00 ; 1176int32_t ne01 ; 1177int32_t ne02 ; 1178int32_t ne03 ; 1179uint64_t nb00 ; 1180uint64_t nb01 ; 1181uint64_t nb02 ; 1182uint64_t nb03 ; 1183int32_t ne0 ; 1184int32_t ne1 ; 1185int32_t ne2 ; 1186int32_t ne3 ; 1187uint64_t nb0 ; 1188uint64_t nb1 ; 1189uint64_t nb2 ; 1190uint64_t nb3 ; 1191}ggml_metal_kargs_tri ; 1192 1193typedef struct { 1194int32_t ne00 ; 1195int32_t ne01 ; 1196int32_t ne02 ; 1197int32_t ne03 ; 1198uint64_t nb00 ; 1199uint64_t nb01 ; 1200uint64_t nb02 ; 1201uint64_t nb03 ; 1202int32_t ne0 ; 1203int32_t ne1 ; 1204int32_t ne2 ; 1205int32_t ne3 ; 1206int32_t top_k ; 1207}ggml_metal_kargs_argsort ; 1208 1209typedef struct { 1210int64_t ne00 ; 1211int64_t ne01 ; 1212int64_t ne02 ; 1213int64_t ne03 ; 1214uint64_t nb00 ; 1215uint64_t nb01 ; 1216uint64_t nb02 ; 1217uint64_t nb03 ; 1218int32_t ne0 ; 1219int32_t ne1 ; 1220int32_t ne2 ; 1221int32_t ne3 ; 1222int32_t top_k ; 1223int32_t len ; 1224}ggml_metal_kargs_argsort_merge ; 1225 1226typedef struct { 1227int32_t ne00 ;// number of columns (elements per row) 1228int32_t ne01 ;// rows 1229int32_t ne02 ; 1230int32_t ne03 ; 1231uint64_t nb01 ;// row stride in src0 1232uint64_t nb02 ; 1233uint64_t nb03 ; 1234int32_t top_k ;// k 1235}ggml_metal_kargs_top_k ; 1236 1237typedef struct { 1238int32_t ne01 ;// n_tokens 1239uint64_t nb01 ;// logits row stride 1240uint64_t nb1_ids ;// ids row stride 1241float clamp ; 1242float scale ; 1243}ggml_metal_kargs_topk_moe ; 1244 1245typedef struct { 1246int32_t ne00 ;// n_embd 1247int32_t ne02 ;// n_tokens 1248}ggml_metal_kargs_moe_reduce ; 1249 1250typedef struct { 1251int32_t nrows ; 1252}ggml_metal_kargs_fwht ; 1253 1254typedef struct { 1255int64_t ne0 ; 1256float start ; 1257float step ; 1258}ggml_metal_kargs_arange ; 1259 1260typedef struct { 1261int64_t val ; 1262}ggml_metal_kargs_memset ; 1263 1264typedef struct { 1265int32_t n_kv ; 1266int32_t n_batch ; 1267int32_t mask_ne3 ; 1268uint64_t nb1 ; 1269uint64_t nb3 ; 1270uint64_t nbq1 ; 1271uint64_t nbq2 ; 1272uint64_t nbq3 ; 1273uint64_t nbk2 ; 1274uint64_t nbk3 ; 1275uint64_t nbw1 ; 1276uint64_t nbw3 ; 1277uint64_t nbm1 ; 1278uint64_t nbm3 ; 1279}ggml_metal_kargs_lightning_indexer ; 1280 1281typedef struct { 1282int32_t n_tokens ; 1283int32_t n_iter ; 1284uint64_t nb_m0 ; 1285uint64_t nb_m1 ; 1286uint64_t nb_s0 ; 1287uint64_t nb_b0 ; 1288uint64_t nb_d0 ; 1289uint64_t nb_d1 ; 1290uint64_t nb_d2 ; 1291float eps ; 1292}ggml_metal_kargs_dsv4_hc_comb ; 1293 1294typedef struct { 1295int32_t n_embd ; 1296int32_t n_tokens ; 1297uint64_t nb_x0 ; 1298uint64_t nb_x1 ; 1299uint64_t nb_x2 ; 1300uint64_t nb_w0 ; 1301uint64_t nb_w1 ; 1302uint64_t nb_w2 ; 1303uint64_t nb_d0 ; 1304uint64_t nb_d1 ; 1305float scale ; 1306}ggml_metal_kargs_dsv4_hc_pre ; 1307 1308typedef struct { 1309int32_t n_embd ; 1310int32_t n_tokens ; 1311uint64_t nb_x0 ; 1312uint64_t nb_x1 ; 1313uint64_t nb_r0 ; 1314uint64_t nb_r1 ; 1315uint64_t nb_r2 ; 1316uint64_t nb_p0 ; 1317uint64_t nb_p1 ; 1318uint64_t nb_c0 ; 1319uint64_t nb_c1 ; 1320uint64_t nb_c2 ; 1321uint64_t nb_d0 ; 1322uint64_t nb_d1 ; 1323uint64_t nb_d2 ; 1324}ggml_metal_kargs_dsv4_hc_post ; 1325 1326typedef struct { 1327int32_t ne00 ; 1328int32_t ne01 ; 1329int32_t ne02 ; 1330int32_t ne03 ; 1331uint64_t nb00 ; 1332uint64_t nb01 ; 1333uint64_t nb02 ; 1334uint64_t nb03 ; 1335uint64_t nb10 ; 1336uint64_t nb11 ; 1337uint64_t nb12 ; 1338uint64_t nb13 ; 1339}ggml_metal_kargs_count_equal ; 1340 1341typedef struct { 1342int32_t k0 ; 1343int32_t k1 ; 1344int32_t s0 ; 1345int32_t s1 ; 1346int32_t p0 ; 1347int32_t p1 ; 1348int64_t IH ; 1349int64_t IW ; 1350int64_t OH ; 1351int64_t OW ; 1352int64_t np ; 1353}ggml_metal_kargs_pool_2d ; 1354 1355typedef struct { 1356int32_t k0 ; 1357int32_t s0 ; 1358int32_t p0 ; 1359int64_t IW ; 1360int64_t OW ; 1361int64_t np ; 1362}ggml_metal_kargs_pool_1d ; 1363 1364typedef struct { 1365int64_t ne00 ; 1366uint64_t nb01 ; 1367}ggml_metal_kargs_argmax ; 1368 1369typedef struct { 1370int64_t np ; 1371}ggml_metal_kargs_opt_step_adamw ; 1372 1373typedef struct { 1374int64_t np ; 1375}ggml_metal_kargs_opt_step_sgd ; 1376 1377typedef struct { 1378int64_t ne ; 1379}ggml_metal_kargs_silu_back ; 1380 1381#endif // GGML_METAL_IMPL