ggml-metal.m 156 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775
  1. #import "ggml-metal.h"
  2. #import "ggml-backend-impl.h"
  3. #import "ggml.h"
  4. #import <Foundation/Foundation.h>
  5. #import <Metal/Metal.h>
  6. #undef MIN
  7. #undef MAX
  8. #define MIN(a, b) ((a) < (b) ? (a) : (b))
  9. #define MAX(a, b) ((a) > (b) ? (a) : (b))
  10. #ifdef GGML_METAL_NDEBUG
  11. #define GGML_METAL_LOG_INFO(...)
  12. #define GGML_METAL_LOG_WARN(...)
  13. #define GGML_METAL_LOG_ERROR(...)
  14. #else
  15. #define GGML_METAL_LOG_INFO(...) ggml_metal_log(GGML_LOG_LEVEL_INFO, __VA_ARGS__)
  16. #define GGML_METAL_LOG_WARN(...) ggml_metal_log(GGML_LOG_LEVEL_WARN, __VA_ARGS__)
  17. #define GGML_METAL_LOG_ERROR(...) ggml_metal_log(GGML_LOG_LEVEL_ERROR, __VA_ARGS__)
  18. #endif
  19. #define UNUSED(x) (void)(x)
  20. struct ggml_metal_kernel {
  21. id<MTLComputePipelineState> pipeline;
  22. };
  23. enum ggml_metal_kernel_type {
  24. GGML_METAL_KERNEL_TYPE_ADD,
  25. GGML_METAL_KERNEL_TYPE_ADD_ROW,
  26. GGML_METAL_KERNEL_TYPE_MUL,
  27. GGML_METAL_KERNEL_TYPE_MUL_ROW,
  28. GGML_METAL_KERNEL_TYPE_DIV,
  29. GGML_METAL_KERNEL_TYPE_DIV_ROW,
  30. GGML_METAL_KERNEL_TYPE_SCALE,
  31. GGML_METAL_KERNEL_TYPE_SCALE_4,
  32. GGML_METAL_KERNEL_TYPE_TANH,
  33. GGML_METAL_KERNEL_TYPE_RELU,
  34. GGML_METAL_KERNEL_TYPE_GELU,
  35. GGML_METAL_KERNEL_TYPE_GELU_QUICK,
  36. GGML_METAL_KERNEL_TYPE_SILU,
  37. GGML_METAL_KERNEL_TYPE_SOFT_MAX,
  38. GGML_METAL_KERNEL_TYPE_SOFT_MAX_4,
  39. GGML_METAL_KERNEL_TYPE_DIAG_MASK_INF,
  40. GGML_METAL_KERNEL_TYPE_DIAG_MASK_INF_8,
  41. GGML_METAL_KERNEL_TYPE_GET_ROWS_F32,
  42. GGML_METAL_KERNEL_TYPE_GET_ROWS_F16,
  43. GGML_METAL_KERNEL_TYPE_GET_ROWS_Q4_0,
  44. GGML_METAL_KERNEL_TYPE_GET_ROWS_Q4_1,
  45. GGML_METAL_KERNEL_TYPE_GET_ROWS_Q5_0,
  46. GGML_METAL_KERNEL_TYPE_GET_ROWS_Q5_1,
  47. GGML_METAL_KERNEL_TYPE_GET_ROWS_Q8_0,
  48. GGML_METAL_KERNEL_TYPE_GET_ROWS_Q2_K,
  49. GGML_METAL_KERNEL_TYPE_GET_ROWS_Q3_K,
  50. GGML_METAL_KERNEL_TYPE_GET_ROWS_Q4_K,
  51. GGML_METAL_KERNEL_TYPE_GET_ROWS_Q5_K,
  52. GGML_METAL_KERNEL_TYPE_GET_ROWS_Q6_K,
  53. GGML_METAL_KERNEL_TYPE_GET_ROWS_IQ2_XXS,
  54. GGML_METAL_KERNEL_TYPE_GET_ROWS_IQ2_XS,
  55. GGML_METAL_KERNEL_TYPE_GET_ROWS_IQ3_XXS,
  56. GGML_METAL_KERNEL_TYPE_GET_ROWS_IQ3_S,
  57. GGML_METAL_KERNEL_TYPE_GET_ROWS_IQ1_S,
  58. GGML_METAL_KERNEL_TYPE_GET_ROWS_IQ4_NL,
  59. GGML_METAL_KERNEL_TYPE_GET_ROWS_I32,
  60. GGML_METAL_KERNEL_TYPE_RMS_NORM,
  61. GGML_METAL_KERNEL_TYPE_GROUP_NORM,
  62. GGML_METAL_KERNEL_TYPE_NORM,
  63. GGML_METAL_KERNEL_TYPE_MUL_MV_F32_F32,
  64. GGML_METAL_KERNEL_TYPE_MUL_MV_F16_F16,
  65. GGML_METAL_KERNEL_TYPE_MUL_MV_F16_F32,
  66. GGML_METAL_KERNEL_TYPE_MUL_MV_F16_F32_1ROW,
  67. GGML_METAL_KERNEL_TYPE_MUL_MV_F16_F32_L4,
  68. GGML_METAL_KERNEL_TYPE_MUL_MV_Q4_0_F32,
  69. GGML_METAL_KERNEL_TYPE_MUL_MV_Q4_1_F32,
  70. GGML_METAL_KERNEL_TYPE_MUL_MV_Q5_0_F32,
  71. GGML_METAL_KERNEL_TYPE_MUL_MV_Q5_1_F32,
  72. GGML_METAL_KERNEL_TYPE_MUL_MV_Q8_0_F32,
  73. GGML_METAL_KERNEL_TYPE_MUL_MV_Q2_K_F32,
  74. GGML_METAL_KERNEL_TYPE_MUL_MV_Q3_K_F32,
  75. GGML_METAL_KERNEL_TYPE_MUL_MV_Q4_K_F32,
  76. GGML_METAL_KERNEL_TYPE_MUL_MV_Q5_K_F32,
  77. GGML_METAL_KERNEL_TYPE_MUL_MV_Q6_K_F32,
  78. GGML_METAL_KERNEL_TYPE_MUL_MV_IQ2_XXS_F32,
  79. GGML_METAL_KERNEL_TYPE_MUL_MV_IQ2_XS_F32,
  80. GGML_METAL_KERNEL_TYPE_MUL_MV_IQ3_XXS_F32,
  81. GGML_METAL_KERNEL_TYPE_MUL_MV_IQ3_S_F32,
  82. GGML_METAL_KERNEL_TYPE_MUL_MV_IQ1_S_F32,
  83. GGML_METAL_KERNEL_TYPE_MUL_MV_IQ4_NL_F32,
  84. GGML_METAL_KERNEL_TYPE_MUL_MV_ID_F32_F32,
  85. //GGML_METAL_KERNEL_TYPE_MUL_MV_ID_F16_F16,
  86. GGML_METAL_KERNEL_TYPE_MUL_MV_ID_F16_F32,
  87. //GGML_METAL_KERNEL_TYPE_MUL_MV_ID_F16_F32_1ROW,
  88. //GGML_METAL_KERNEL_TYPE_MUL_MV_ID_F16_F32_L4,
  89. GGML_METAL_KERNEL_TYPE_MUL_MV_ID_Q4_0_F32,
  90. GGML_METAL_KERNEL_TYPE_MUL_MV_ID_Q4_1_F32,
  91. GGML_METAL_KERNEL_TYPE_MUL_MV_ID_Q5_0_F32,
  92. GGML_METAL_KERNEL_TYPE_MUL_MV_ID_Q5_1_F32,
  93. GGML_METAL_KERNEL_TYPE_MUL_MV_ID_Q8_0_F32,
  94. GGML_METAL_KERNEL_TYPE_MUL_MV_ID_Q2_K_F32,
  95. GGML_METAL_KERNEL_TYPE_MUL_MV_ID_Q3_K_F32,
  96. GGML_METAL_KERNEL_TYPE_MUL_MV_ID_Q4_K_F32,
  97. GGML_METAL_KERNEL_TYPE_MUL_MV_ID_Q5_K_F32,
  98. GGML_METAL_KERNEL_TYPE_MUL_MV_ID_Q6_K_F32,
  99. GGML_METAL_KERNEL_TYPE_MUL_MV_ID_IQ2_XXS_F32,
  100. GGML_METAL_KERNEL_TYPE_MUL_MV_ID_IQ2_XS_F32,
  101. GGML_METAL_KERNEL_TYPE_MUL_MV_ID_IQ3_XXS_F32,
  102. GGML_METAL_KERNEL_TYPE_MUL_MV_ID_IQ3_S_F32,
  103. GGML_METAL_KERNEL_TYPE_MUL_MV_ID_IQ1_S_F32,
  104. GGML_METAL_KERNEL_TYPE_MUL_MV_ID_IQ4_NL_F32,
  105. GGML_METAL_KERNEL_TYPE_MUL_MM_F32_F32,
  106. GGML_METAL_KERNEL_TYPE_MUL_MM_F16_F32,
  107. GGML_METAL_KERNEL_TYPE_MUL_MM_Q4_0_F32,
  108. GGML_METAL_KERNEL_TYPE_MUL_MM_Q4_1_F32,
  109. GGML_METAL_KERNEL_TYPE_MUL_MM_Q5_0_F32,
  110. GGML_METAL_KERNEL_TYPE_MUL_MM_Q5_1_F32,
  111. GGML_METAL_KERNEL_TYPE_MUL_MM_Q8_0_F32,
  112. GGML_METAL_KERNEL_TYPE_MUL_MM_Q2_K_F32,
  113. GGML_METAL_KERNEL_TYPE_MUL_MM_Q3_K_F32,
  114. GGML_METAL_KERNEL_TYPE_MUL_MM_Q4_K_F32,
  115. GGML_METAL_KERNEL_TYPE_MUL_MM_Q5_K_F32,
  116. GGML_METAL_KERNEL_TYPE_MUL_MM_Q6_K_F32,
  117. GGML_METAL_KERNEL_TYPE_MUL_MM_IQ2_XXS_F32,
  118. GGML_METAL_KERNEL_TYPE_MUL_MM_IQ2_XS_F32,
  119. GGML_METAL_KERNEL_TYPE_MUL_MM_IQ3_XXS_F32,
  120. GGML_METAL_KERNEL_TYPE_MUL_MM_IQ3_S_F32,
  121. GGML_METAL_KERNEL_TYPE_MUL_MM_IQ1_S_F32,
  122. GGML_METAL_KERNEL_TYPE_MUL_MM_IQ4_NL_F32,
  123. GGML_METAL_KERNEL_TYPE_MUL_MM_ID_F32_F32,
  124. GGML_METAL_KERNEL_TYPE_MUL_MM_ID_F16_F32,
  125. GGML_METAL_KERNEL_TYPE_MUL_MM_ID_Q4_0_F32,
  126. GGML_METAL_KERNEL_TYPE_MUL_MM_ID_Q4_1_F32,
  127. GGML_METAL_KERNEL_TYPE_MUL_MM_ID_Q5_0_F32,
  128. GGML_METAL_KERNEL_TYPE_MUL_MM_ID_Q5_1_F32,
  129. GGML_METAL_KERNEL_TYPE_MUL_MM_ID_Q8_0_F32,
  130. GGML_METAL_KERNEL_TYPE_MUL_MM_ID_Q2_K_F32,
  131. GGML_METAL_KERNEL_TYPE_MUL_MM_ID_Q3_K_F32,
  132. GGML_METAL_KERNEL_TYPE_MUL_MM_ID_Q4_K_F32,
  133. GGML_METAL_KERNEL_TYPE_MUL_MM_ID_Q5_K_F32,
  134. GGML_METAL_KERNEL_TYPE_MUL_MM_ID_Q6_K_F32,
  135. GGML_METAL_KERNEL_TYPE_MUL_MM_ID_IQ2_XXS_F32,
  136. GGML_METAL_KERNEL_TYPE_MUL_MM_ID_IQ2_XS_F32,
  137. GGML_METAL_KERNEL_TYPE_MUL_MM_ID_IQ3_XXS_F32,
  138. GGML_METAL_KERNEL_TYPE_MUL_MM_ID_IQ3_S_F32,
  139. GGML_METAL_KERNEL_TYPE_MUL_MM_ID_IQ1_S_F32,
  140. GGML_METAL_KERNEL_TYPE_MUL_MM_ID_IQ4_NL_F32,
  141. GGML_METAL_KERNEL_TYPE_ROPE_F32,
  142. GGML_METAL_KERNEL_TYPE_ROPE_F16,
  143. GGML_METAL_KERNEL_TYPE_ALIBI_F32,
  144. GGML_METAL_KERNEL_TYPE_IM2COL_F16,
  145. GGML_METAL_KERNEL_TYPE_IM2COL_F32,
  146. GGML_METAL_KERNEL_TYPE_UPSCALE_F32,
  147. GGML_METAL_KERNEL_TYPE_PAD_F32,
  148. GGML_METAL_KERNEL_TYPE_ARGSORT_F32_I32_ASC,
  149. GGML_METAL_KERNEL_TYPE_ARGSORT_F32_I32_DESC,
  150. GGML_METAL_KERNEL_TYPE_LEAKY_RELU_F32,
  151. GGML_METAL_KERNEL_TYPE_CPY_F32_F16,
  152. GGML_METAL_KERNEL_TYPE_CPY_F32_F32,
  153. GGML_METAL_KERNEL_TYPE_CPY_F32_Q8_0,
  154. GGML_METAL_KERNEL_TYPE_CPY_F32_Q4_0,
  155. GGML_METAL_KERNEL_TYPE_CPY_F32_Q4_1,
  156. //GGML_METAL_KERNEL_TYPE_CPY_F32_Q5_0,
  157. //GGML_METAL_KERNEL_TYPE_CPY_F32_Q5_1,
  158. GGML_METAL_KERNEL_TYPE_CPY_F16_F16,
  159. GGML_METAL_KERNEL_TYPE_CPY_F16_F32,
  160. GGML_METAL_KERNEL_TYPE_CONCAT,
  161. GGML_METAL_KERNEL_TYPE_SQR,
  162. GGML_METAL_KERNEL_TYPE_SUM_ROWS,
  163. GGML_METAL_KERNEL_TYPE_COUNT
  164. };
  165. struct ggml_metal_context {
  166. int n_cb;
  167. id<MTLDevice> device;
  168. id<MTLCommandQueue> queue;
  169. dispatch_queue_t d_queue;
  170. struct ggml_metal_kernel kernels[GGML_METAL_KERNEL_TYPE_COUNT];
  171. bool support_simdgroup_reduction;
  172. bool support_simdgroup_mm;
  173. bool should_capture_next_compute;
  174. };
  175. // MSL code
  176. // TODO: move the contents here when ready
  177. // for now it is easier to work in a separate file
  178. // static NSString * const msl_library_source = @"see metal.metal";
  179. // Here to assist with NSBundle Path Hack
  180. @interface GGMLMetalClass : NSObject
  181. @end
  182. @implementation GGMLMetalClass
  183. @end
  184. static void ggml_metal_default_log_callback(enum ggml_log_level level, const char * msg, void * user_data) {
  185. fprintf(stderr, "%s", msg);
  186. UNUSED(level);
  187. UNUSED(user_data);
  188. }
  189. ggml_log_callback ggml_metal_log_callback = ggml_metal_default_log_callback;
  190. void * ggml_metal_log_user_data = NULL;
  191. GGML_ATTRIBUTE_FORMAT(2, 3)
  192. static void ggml_metal_log(enum ggml_log_level level, const char * format, ...){
  193. if (ggml_metal_log_callback != NULL) {
  194. va_list args;
  195. va_start(args, format);
  196. char buffer[128];
  197. int len = vsnprintf(buffer, 128, format, args);
  198. if (len < 128) {
  199. ggml_metal_log_callback(level, buffer, ggml_metal_log_user_data);
  200. } else {
  201. char* buffer2 = malloc(len+1);
  202. va_end(args);
  203. va_start(args, format);
  204. vsnprintf(buffer2, len+1, format, args);
  205. buffer2[len] = 0;
  206. ggml_metal_log_callback(level, buffer2, ggml_metal_log_user_data);
  207. free(buffer2);
  208. }
  209. va_end(args);
  210. }
  211. }
  212. static void * ggml_metal_host_malloc(size_t n) {
  213. void * data = NULL;
  214. const int result = posix_memalign((void **) &data, sysconf(_SC_PAGESIZE), n);
  215. if (result != 0) {
  216. GGML_METAL_LOG_ERROR("%s: error: posix_memalign failed\n", __func__);
  217. return NULL;
  218. }
  219. return data;
  220. }
  221. static struct ggml_metal_context * ggml_metal_init(int n_cb) {
  222. GGML_METAL_LOG_INFO("%s: allocating\n", __func__);
  223. #if TARGET_OS_OSX && !GGML_METAL_NDEBUG
  224. // Show all the Metal device instances in the system
  225. NSArray * devices = MTLCopyAllDevices();
  226. for (id<MTLDevice> device in devices) {
  227. GGML_METAL_LOG_INFO("%s: found device: %s\n", __func__, [[device name] UTF8String]);
  228. }
  229. [devices release]; // since it was created by a *Copy* C method
  230. #endif
  231. // Pick and show default Metal device
  232. id<MTLDevice> device = MTLCreateSystemDefaultDevice();
  233. GGML_METAL_LOG_INFO("%s: picking default device: %s\n", __func__, [[device name] UTF8String]);
  234. // Configure context
  235. struct ggml_metal_context * ctx = malloc(sizeof(struct ggml_metal_context));
  236. ctx->device = device;
  237. ctx->n_cb = MIN(n_cb, GGML_METAL_MAX_BUFFERS);
  238. ctx->queue = [ctx->device newCommandQueue];
  239. ctx->d_queue = dispatch_queue_create("ggml-metal", DISPATCH_QUEUE_CONCURRENT);
  240. id<MTLLibrary> metal_library;
  241. // load library
  242. {
  243. NSBundle * bundle = nil;
  244. #ifdef SWIFT_PACKAGE
  245. bundle = SWIFTPM_MODULE_BUNDLE;
  246. #else
  247. bundle = [NSBundle bundleForClass:[GGMLMetalClass class]];
  248. #endif
  249. NSError * error = nil;
  250. NSString * libPath = [bundle pathForResource:@"default" ofType:@"metallib"];
  251. if (libPath != nil) {
  252. // pre-compiled library found
  253. NSURL * libURL = [NSURL fileURLWithPath:libPath];
  254. GGML_METAL_LOG_INFO("%s: loading '%s'\n", __func__, [libPath UTF8String]);
  255. metal_library = [ctx->device newLibraryWithURL:libURL error:&error];
  256. if (error) {
  257. GGML_METAL_LOG_ERROR("%s: error: %s\n", __func__, [[error description] UTF8String]);
  258. return NULL;
  259. }
  260. } else {
  261. #if GGML_METAL_EMBED_LIBRARY
  262. GGML_METAL_LOG_INFO("%s: using embedded metal library\n", __func__);
  263. extern const char ggml_metallib_start[];
  264. extern const char ggml_metallib_end[];
  265. NSString * src = [[NSString alloc] initWithBytes:ggml_metallib_start length:(ggml_metallib_end-ggml_metallib_start) encoding:NSUTF8StringEncoding];
  266. #else
  267. GGML_METAL_LOG_INFO("%s: default.metallib not found, loading from source\n", __func__);
  268. NSString * sourcePath;
  269. NSString * ggmlMetalPathResources = [[NSProcessInfo processInfo].environment objectForKey:@"GGML_METAL_PATH_RESOURCES"];
  270. GGML_METAL_LOG_INFO("%s: GGML_METAL_PATH_RESOURCES = %s\n", __func__, ggmlMetalPathResources ? [ggmlMetalPathResources UTF8String] : "nil");
  271. if (ggmlMetalPathResources) {
  272. sourcePath = [ggmlMetalPathResources stringByAppendingPathComponent:@"ggml-metal.metal"];
  273. } else {
  274. sourcePath = [bundle pathForResource:@"ggml-metal" ofType:@"metal"];
  275. }
  276. if (sourcePath == nil) {
  277. GGML_METAL_LOG_WARN("%s: error: could not use bundle path to find ggml-metal.metal, falling back to trying cwd\n", __func__);
  278. sourcePath = @"ggml-metal.metal";
  279. }
  280. GGML_METAL_LOG_INFO("%s: loading '%s'\n", __func__, [sourcePath UTF8String]);
  281. NSString * src = [NSString stringWithContentsOfFile:sourcePath encoding:NSUTF8StringEncoding error:&error];
  282. if (error) {
  283. GGML_METAL_LOG_ERROR("%s: error: %s\n", __func__, [[error description] UTF8String]);
  284. return NULL;
  285. }
  286. #endif
  287. @autoreleasepool {
  288. // dictionary of preprocessor macros
  289. NSMutableDictionary * prep = [NSMutableDictionary dictionary];
  290. #ifdef GGML_QKK_64
  291. prep[@"QK_K"] = @(64);
  292. #endif
  293. MTLCompileOptions* options = [MTLCompileOptions new];
  294. options.preprocessorMacros = prep;
  295. //[options setFastMathEnabled:false];
  296. metal_library = [ctx->device newLibraryWithSource:src options:options error:&error];
  297. if (error) {
  298. GGML_METAL_LOG_ERROR("%s: error: %s\n", __func__, [[error description] UTF8String]);
  299. return NULL;
  300. }
  301. }
  302. }
  303. }
  304. // print MTL GPU family:
  305. GGML_METAL_LOG_INFO("%s: GPU name: %s\n", __func__, [[ctx->device name] UTF8String]);
  306. const NSInteger MTLGPUFamilyMetal3 = 5001;
  307. // determine max supported GPU family
  308. // https://developer.apple.com/metal/Metal-Shading-Language-Specification.pdf
  309. // https://developer.apple.com/metal/Metal-Feature-Set-Tables.pdf
  310. {
  311. for (int i = MTLGPUFamilyApple1 + 20; i >= MTLGPUFamilyApple1; --i) {
  312. if ([ctx->device supportsFamily:i]) {
  313. GGML_METAL_LOG_INFO("%s: GPU family: MTLGPUFamilyApple%d (%d)\n", __func__, i - (int) MTLGPUFamilyApple1 + 1, i);
  314. break;
  315. }
  316. }
  317. for (int i = MTLGPUFamilyCommon1 + 5; i >= MTLGPUFamilyCommon1; --i) {
  318. if ([ctx->device supportsFamily:i]) {
  319. GGML_METAL_LOG_INFO("%s: GPU family: MTLGPUFamilyCommon%d (%d)\n", __func__, i - (int) MTLGPUFamilyCommon1 + 1, i);
  320. break;
  321. }
  322. }
  323. for (int i = MTLGPUFamilyMetal3 + 5; i >= MTLGPUFamilyMetal3; --i) {
  324. if ([ctx->device supportsFamily:i]) {
  325. GGML_METAL_LOG_INFO("%s: GPU family: MTLGPUFamilyMetal%d (%d)\n", __func__, i - (int) MTLGPUFamilyMetal3 + 3, i);
  326. break;
  327. }
  328. }
  329. }
  330. ctx->support_simdgroup_reduction = [ctx->device supportsFamily:MTLGPUFamilyApple7];
  331. ctx->support_simdgroup_reduction |= [ctx->device supportsFamily:MTLGPUFamilyMetal3];
  332. ctx->support_simdgroup_mm = [ctx->device supportsFamily:MTLGPUFamilyApple7];
  333. GGML_METAL_LOG_INFO("%s: simdgroup reduction support = %s\n", __func__, ctx->support_simdgroup_reduction ? "true" : "false");
  334. GGML_METAL_LOG_INFO("%s: simdgroup matrix mul. support = %s\n", __func__, ctx->support_simdgroup_mm ? "true" : "false");
  335. GGML_METAL_LOG_INFO("%s: hasUnifiedMemory = %s\n", __func__, ctx->device.hasUnifiedMemory ? "true" : "false");
  336. ctx->should_capture_next_compute = false;
  337. #if TARGET_OS_OSX || (TARGET_OS_IOS && __clang_major__ >= 15)
  338. if (@available(macOS 10.12, iOS 16.0, *)) {
  339. GGML_METAL_LOG_INFO("%s: recommendedMaxWorkingSetSize = %8.2f MB\n", __func__, ctx->device.recommendedMaxWorkingSetSize / 1e6);
  340. }
  341. #elif TARGET_OS_OSX
  342. if (ctx->device.maxTransferRate != 0) {
  343. GGML_METAL_LOG_INFO("%s: maxTransferRate = %8.2f MB/s\n", __func__, ctx->device.maxTransferRate / 1e6);
  344. } else {
  345. GGML_METAL_LOG_INFO("%s: maxTransferRate = built-in GPU\n", __func__);
  346. }
  347. #endif
  348. // load kernels
  349. {
  350. NSError * error = nil;
  351. for (int i = 0; i < GGML_METAL_KERNEL_TYPE_COUNT; ++i) {
  352. ctx->kernels[i].pipeline = nil;
  353. }
  354. /*
  355. GGML_METAL_LOG_INFO("%s: loaded %-32s %16p | th_max = %4d | th_width = %4d\n", __func__, "kernel_"#name, (void *) kernel->pipeline, \
  356. (int) kernel->pipeline.maxTotalThreadsPerThreadgroup, \
  357. (int) kernel->pipeline.threadExecutionWidth); \
  358. */
  359. #define GGML_METAL_ADD_KERNEL(e, name, supported) \
  360. if (supported) { \
  361. struct ggml_metal_kernel * kernel = &ctx->kernels[e]; \
  362. id<MTLFunction> metal_function = [metal_library newFunctionWithName:@"kernel_"#name]; \
  363. kernel->pipeline = [ctx->device newComputePipelineStateWithFunction:metal_function error:&error]; \
  364. [metal_function release]; \
  365. if (error) { \
  366. GGML_METAL_LOG_ERROR("%s: error: load pipeline error: %s\n", __func__, [[error description] UTF8String]); \
  367. [metal_library release]; \
  368. return NULL; \
  369. } \
  370. } else { \
  371. GGML_METAL_LOG_WARN("%s: skipping %-32s (not supported)\n", __func__, "kernel_"#name); \
  372. }
  373. // simd_sum and simd_max requires MTLGPUFamilyApple7
  374. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_ADD, add, true);
  375. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_ADD_ROW, add_row, true);
  376. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL, mul, true);
  377. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_ROW, mul_row, true);
  378. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_DIV, div, true);
  379. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_DIV_ROW, div_row, true);
  380. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_SCALE, scale, true);
  381. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_SCALE_4, scale_4, true);
  382. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_TANH, tanh, true);
  383. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_RELU, relu, true);
  384. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_GELU, gelu, true);
  385. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_GELU_QUICK, gelu_quick, true);
  386. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_SILU, silu, true);
  387. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_SOFT_MAX, soft_max, ctx->support_simdgroup_reduction);
  388. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_SOFT_MAX_4, soft_max_4, ctx->support_simdgroup_reduction);
  389. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_DIAG_MASK_INF, diag_mask_inf, true);
  390. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_DIAG_MASK_INF_8, diag_mask_inf_8, true);
  391. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_GET_ROWS_F32, get_rows_f32, true);
  392. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_GET_ROWS_F16, get_rows_f16, true);
  393. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_GET_ROWS_Q4_0, get_rows_q4_0, true);
  394. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_GET_ROWS_Q4_1, get_rows_q4_1, true);
  395. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_GET_ROWS_Q5_0, get_rows_q5_0, true);
  396. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_GET_ROWS_Q5_1, get_rows_q5_1, true);
  397. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_GET_ROWS_Q8_0, get_rows_q8_0, true);
  398. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_GET_ROWS_Q2_K, get_rows_q2_K, true);
  399. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_GET_ROWS_Q3_K, get_rows_q3_K, true);
  400. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_GET_ROWS_Q4_K, get_rows_q4_K, true);
  401. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_GET_ROWS_Q5_K, get_rows_q5_K, true);
  402. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_GET_ROWS_Q6_K, get_rows_q6_K, true);
  403. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_GET_ROWS_IQ2_XXS, get_rows_iq2_xxs, true);
  404. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_GET_ROWS_IQ2_XS, get_rows_iq2_xs, true);
  405. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_GET_ROWS_IQ3_XXS, get_rows_iq3_xxs, true);
  406. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_GET_ROWS_IQ3_S, get_rows_iq3_s, true);
  407. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_GET_ROWS_IQ1_S, get_rows_iq1_s, true);
  408. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_GET_ROWS_IQ4_NL, get_rows_iq4_nl, true);
  409. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_GET_ROWS_I32, get_rows_i32, true);
  410. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_RMS_NORM, rms_norm, ctx->support_simdgroup_reduction);
  411. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_GROUP_NORM, group_norm, ctx->support_simdgroup_reduction);
  412. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_NORM, norm, true);
  413. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_F32_F32, mul_mv_f32_f32, ctx->support_simdgroup_reduction);
  414. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_F16_F16, mul_mv_f16_f16, ctx->support_simdgroup_reduction);
  415. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_F16_F32, mul_mv_f16_f32, ctx->support_simdgroup_reduction);
  416. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_F16_F32_1ROW, mul_mv_f16_f32_1row, ctx->support_simdgroup_reduction);
  417. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_F16_F32_L4, mul_mv_f16_f32_l4, ctx->support_simdgroup_reduction);
  418. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_Q4_0_F32, mul_mv_q4_0_f32, ctx->support_simdgroup_reduction);
  419. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_Q4_1_F32, mul_mv_q4_1_f32, ctx->support_simdgroup_reduction);
  420. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_Q5_0_F32, mul_mv_q5_0_f32, ctx->support_simdgroup_reduction);
  421. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_Q5_1_F32, mul_mv_q5_1_f32, ctx->support_simdgroup_reduction);
  422. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_Q8_0_F32, mul_mv_q8_0_f32, ctx->support_simdgroup_reduction);
  423. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_Q2_K_F32, mul_mv_q2_K_f32, ctx->support_simdgroup_reduction);
  424. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_Q3_K_F32, mul_mv_q3_K_f32, ctx->support_simdgroup_reduction);
  425. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_Q4_K_F32, mul_mv_q4_K_f32, ctx->support_simdgroup_reduction);
  426. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_Q5_K_F32, mul_mv_q5_K_f32, ctx->support_simdgroup_reduction);
  427. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_Q6_K_F32, mul_mv_q6_K_f32, ctx->support_simdgroup_reduction);
  428. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_IQ2_XXS_F32, mul_mv_iq2_xxs_f32, ctx->support_simdgroup_reduction);
  429. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_IQ2_XS_F32, mul_mv_iq2_xs_f32, ctx->support_simdgroup_reduction);
  430. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_IQ3_XXS_F32, mul_mv_iq3_xxs_f32, ctx->support_simdgroup_reduction);
  431. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_IQ3_S_F32, mul_mv_iq3_s_f32, ctx->support_simdgroup_reduction);
  432. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_IQ1_S_F32, mul_mv_iq1_s_f32, ctx->support_simdgroup_reduction);
  433. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_IQ4_NL_F32, mul_mv_iq4_nl_f32, ctx->support_simdgroup_reduction);
  434. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_ID_F32_F32, mul_mv_id_f32_f32, ctx->support_simdgroup_reduction);
  435. //GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_ID_F16_F16, mul_mv_id_f16_f16, ctx->support_simdgroup_reduction);
  436. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_ID_F16_F32, mul_mv_id_f16_f32, ctx->support_simdgroup_reduction);
  437. //GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_ID_F16_F32_1ROW, mul_mv_id_f16_f32_1row, ctx->support_simdgroup_reduction);
  438. //GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_ID_F16_F32_L4, mul_mv_id_f16_f32_l4, ctx->support_simdgroup_reduction);
  439. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_ID_Q4_0_F32, mul_mv_id_q4_0_f32, ctx->support_simdgroup_reduction);
  440. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_ID_Q4_1_F32, mul_mv_id_q4_1_f32, ctx->support_simdgroup_reduction);
  441. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_ID_Q5_0_F32, mul_mv_id_q5_0_f32, ctx->support_simdgroup_reduction);
  442. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_ID_Q5_1_F32, mul_mv_id_q5_1_f32, ctx->support_simdgroup_reduction);
  443. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_ID_Q8_0_F32, mul_mv_id_q8_0_f32, ctx->support_simdgroup_reduction);
  444. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_ID_Q2_K_F32, mul_mv_id_q2_K_f32, ctx->support_simdgroup_reduction);
  445. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_ID_Q3_K_F32, mul_mv_id_q3_K_f32, ctx->support_simdgroup_reduction);
  446. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_ID_Q4_K_F32, mul_mv_id_q4_K_f32, ctx->support_simdgroup_reduction);
  447. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_ID_Q5_K_F32, mul_mv_id_q5_K_f32, ctx->support_simdgroup_reduction);
  448. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_ID_Q6_K_F32, mul_mv_id_q6_K_f32, ctx->support_simdgroup_reduction);
  449. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_ID_IQ2_XXS_F32, mul_mv_id_iq2_xxs_f32, ctx->support_simdgroup_reduction);
  450. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_ID_IQ2_XS_F32, mul_mv_id_iq2_xs_f32, ctx->support_simdgroup_reduction);
  451. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_ID_IQ3_XXS_F32, mul_mv_id_iq3_xxs_f32, ctx->support_simdgroup_reduction);
  452. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_ID_IQ3_S_F32, mul_mv_id_iq3_s_f32, ctx->support_simdgroup_reduction);
  453. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_ID_IQ1_S_F32, mul_mv_id_iq1_s_f32, ctx->support_simdgroup_reduction);
  454. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_ID_IQ4_NL_F32, mul_mv_id_iq4_nl_f32, ctx->support_simdgroup_reduction);
  455. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MM_F32_F32, mul_mm_f32_f32, ctx->support_simdgroup_mm);
  456. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MM_F16_F32, mul_mm_f16_f32, ctx->support_simdgroup_mm);
  457. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MM_Q4_0_F32, mul_mm_q4_0_f32, ctx->support_simdgroup_mm);
  458. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MM_Q4_1_F32, mul_mm_q4_1_f32, ctx->support_simdgroup_mm);
  459. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MM_Q5_0_F32, mul_mm_q5_0_f32, ctx->support_simdgroup_mm);
  460. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MM_Q5_1_F32, mul_mm_q5_1_f32, ctx->support_simdgroup_mm);
  461. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MM_Q8_0_F32, mul_mm_q8_0_f32, ctx->support_simdgroup_mm);
  462. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MM_Q2_K_F32, mul_mm_q2_K_f32, ctx->support_simdgroup_mm);
  463. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MM_Q3_K_F32, mul_mm_q3_K_f32, ctx->support_simdgroup_mm);
  464. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MM_Q4_K_F32, mul_mm_q4_K_f32, ctx->support_simdgroup_mm);
  465. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MM_Q5_K_F32, mul_mm_q5_K_f32, ctx->support_simdgroup_mm);
  466. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MM_Q6_K_F32, mul_mm_q6_K_f32, ctx->support_simdgroup_mm);
  467. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MM_IQ2_XXS_F32, mul_mm_iq2_xxs_f32, ctx->support_simdgroup_mm);
  468. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MM_IQ2_XS_F32, mul_mm_iq2_xs_f32, ctx->support_simdgroup_mm);
  469. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MM_IQ3_XXS_F32, mul_mm_iq3_xxs_f32, ctx->support_simdgroup_mm);
  470. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MM_IQ3_S_F32, mul_mm_iq3_s_f32, ctx->support_simdgroup_mm);
  471. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MM_IQ1_S_F32, mul_mm_iq1_s_f32, ctx->support_simdgroup_mm);
  472. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MM_IQ4_NL_F32, mul_mm_iq4_nl_f32, ctx->support_simdgroup_mm);
  473. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MM_ID_F32_F32, mul_mm_id_f32_f32, ctx->support_simdgroup_mm);
  474. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MM_ID_F16_F32, mul_mm_id_f16_f32, ctx->support_simdgroup_mm);
  475. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MM_ID_Q4_0_F32, mul_mm_id_q4_0_f32, ctx->support_simdgroup_mm);
  476. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MM_ID_Q4_1_F32, mul_mm_id_q4_1_f32, ctx->support_simdgroup_mm);
  477. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MM_ID_Q5_0_F32, mul_mm_id_q5_0_f32, ctx->support_simdgroup_mm);
  478. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MM_ID_Q5_1_F32, mul_mm_id_q5_1_f32, ctx->support_simdgroup_mm);
  479. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MM_ID_Q8_0_F32, mul_mm_id_q8_0_f32, ctx->support_simdgroup_mm);
  480. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MM_ID_Q2_K_F32, mul_mm_id_q2_K_f32, ctx->support_simdgroup_mm);
  481. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MM_ID_Q3_K_F32, mul_mm_id_q3_K_f32, ctx->support_simdgroup_mm);
  482. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MM_ID_Q4_K_F32, mul_mm_id_q4_K_f32, ctx->support_simdgroup_mm);
  483. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MM_ID_Q5_K_F32, mul_mm_id_q5_K_f32, ctx->support_simdgroup_mm);
  484. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MM_ID_Q6_K_F32, mul_mm_id_q6_K_f32, ctx->support_simdgroup_mm);
  485. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MM_ID_IQ2_XXS_F32, mul_mm_id_iq2_xxs_f32, ctx->support_simdgroup_mm);
  486. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MM_ID_IQ2_XS_F32, mul_mm_id_iq2_xs_f32, ctx->support_simdgroup_mm);
  487. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MM_ID_IQ3_XXS_F32, mul_mm_id_iq3_xxs_f32, ctx->support_simdgroup_mm);
  488. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MM_ID_IQ3_S_F32, mul_mm_id_iq3_s_f32, ctx->support_simdgroup_mm);
  489. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MM_ID_IQ1_S_F32, mul_mm_id_iq1_s_f32, ctx->support_simdgroup_mm);
  490. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MM_ID_IQ4_NL_F32, mul_mm_id_iq4_nl_f32, ctx->support_simdgroup_mm);
  491. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_ROPE_F32, rope_f32, true);
  492. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_ROPE_F16, rope_f16, true);
  493. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_ALIBI_F32, alibi_f32, true);
  494. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_IM2COL_F16, im2col_f16, true);
  495. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_IM2COL_F32, im2col_f32, true);
  496. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_UPSCALE_F32, upscale_f32, true);
  497. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_PAD_F32, pad_f32, true);
  498. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_ARGSORT_F32_I32_ASC, argsort_f32_i32_asc, true);
  499. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_ARGSORT_F32_I32_DESC, argsort_f32_i32_desc, true);
  500. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_LEAKY_RELU_F32, leaky_relu_f32, true);
  501. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_CPY_F32_F16, cpy_f32_f16, true);
  502. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_CPY_F32_F32, cpy_f32_f32, true);
  503. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_CPY_F32_Q8_0, cpy_f32_q8_0, true);
  504. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_CPY_F32_Q4_0, cpy_f32_q4_0, true);
  505. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_CPY_F32_Q4_1, cpy_f32_q4_1, true);
  506. //GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_CPY_F32_Q5_0, cpy_f32_q5_0, true);
  507. //GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_CPY_F32_Q5_1, cpy_f32_q5_1, true);
  508. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_CPY_F16_F16, cpy_f16_f16, true);
  509. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_CPY_F16_F32, cpy_f16_f32, true);
  510. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_CONCAT, concat, true);
  511. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_SQR, sqr, true);
  512. GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_SUM_ROWS, sum_rows, true);
  513. }
  514. [metal_library release];
  515. return ctx;
  516. }
  517. static void ggml_metal_free(struct ggml_metal_context * ctx) {
  518. GGML_METAL_LOG_INFO("%s: deallocating\n", __func__);
  519. for (int i = 0; i < GGML_METAL_KERNEL_TYPE_COUNT; ++i) {
  520. [ctx->kernels[i].pipeline release];
  521. }
  522. [ctx->queue release];
  523. [ctx->device release];
  524. dispatch_release(ctx->d_queue);
  525. free(ctx);
  526. }
  527. // temporarily defined here for compatibility between ggml-backend and the old API
  528. struct ggml_backend_metal_buffer {
  529. void * data;
  530. size_t size;
  531. id<MTLBuffer> metal;
  532. };
  533. struct ggml_backend_metal_buffer_context {
  534. void * all_data;
  535. size_t all_size;
  536. bool owned;
  537. // multiple buffers are used only to avoid the maximum buffer size limitation when using mmap
  538. int n_buffers;
  539. struct ggml_backend_metal_buffer buffers[GGML_METAL_MAX_BUFFERS];
  540. };
  541. // finds the Metal buffer that contains the tensor data on the GPU device
  542. // the assumption is that there is 1-to-1 mapping between the host and device memory buffers, so we can find the
  543. // Metal buffer based on the host memory pointer
  544. //
  545. static id<MTLBuffer> ggml_metal_get_buffer(struct ggml_tensor * t, size_t * offs) {
  546. //GGML_METAL_LOG_INFO("%s: data tensor '%16s', offs_data = %8ld, offs_eval = %8ld, offs_cach = %8ld\n", __func__, t->name, offs_data, offs_eval, offs_cach);
  547. const int64_t tsize = ggml_nbytes(t);
  548. ggml_backend_buffer_t buffer = t->view_src ? t->view_src->buffer : t->buffer;
  549. struct ggml_backend_metal_buffer_context * buf_ctx = (struct ggml_backend_metal_buffer_context *) buffer->context;
  550. // find the view that contains the tensor fully
  551. for (int i = 0; i < buf_ctx->n_buffers; ++i) {
  552. const int64_t ioffs = (int64_t) t->data - (int64_t) buf_ctx->buffers[i].data;
  553. //GGML_METAL_LOG_INFO("ioffs = %10ld, tsize = %10ld, sum = %10ld, buf_ctx->buffers[%d].size = %10ld\n", ioffs, tsize, ioffs + tsize, i, buf_ctx->buffers[i].size);
  554. if (ioffs >= 0 && ioffs + tsize <= (int64_t) buf_ctx->buffers[i].size) {
  555. *offs = (size_t) ioffs;
  556. //GGML_METAL_LOG_INFO("%s: tensor '%16s', offs = %8ld\n", __func__, t->name, *offs);
  557. return buf_ctx->buffers[i].metal;
  558. }
  559. }
  560. GGML_METAL_LOG_ERROR("%s: error: tensor '%s' buffer is nil\n", __func__, t->name);
  561. return nil;
  562. }
  563. static bool ggml_metal_supports_op(const struct ggml_metal_context * ctx, const struct ggml_tensor * op) {
  564. switch (op->op) {
  565. case GGML_OP_UNARY:
  566. switch (ggml_get_unary_op(op)) {
  567. case GGML_UNARY_OP_TANH:
  568. case GGML_UNARY_OP_RELU:
  569. case GGML_UNARY_OP_GELU:
  570. case GGML_UNARY_OP_GELU_QUICK:
  571. case GGML_UNARY_OP_SILU:
  572. return true;
  573. default:
  574. return false;
  575. }
  576. case GGML_OP_NONE:
  577. case GGML_OP_RESHAPE:
  578. case GGML_OP_VIEW:
  579. case GGML_OP_TRANSPOSE:
  580. case GGML_OP_PERMUTE:
  581. case GGML_OP_CONCAT:
  582. case GGML_OP_ADD:
  583. case GGML_OP_ACC:
  584. case GGML_OP_MUL:
  585. case GGML_OP_DIV:
  586. case GGML_OP_SCALE:
  587. case GGML_OP_SQR:
  588. case GGML_OP_SUM_ROWS:
  589. return true;
  590. case GGML_OP_SOFT_MAX:
  591. case GGML_OP_RMS_NORM:
  592. case GGML_OP_GROUP_NORM:
  593. return ctx->support_simdgroup_reduction;
  594. case GGML_OP_NORM:
  595. case GGML_OP_ALIBI:
  596. case GGML_OP_ROPE:
  597. case GGML_OP_IM2COL:
  598. return true;
  599. case GGML_OP_POOL_1D:
  600. case GGML_OP_POOL_2D:
  601. return false;
  602. case GGML_OP_UPSCALE:
  603. case GGML_OP_PAD:
  604. case GGML_OP_ARGSORT:
  605. case GGML_OP_LEAKY_RELU:
  606. return true;
  607. case GGML_OP_MUL_MAT:
  608. case GGML_OP_MUL_MAT_ID:
  609. return ctx->support_simdgroup_reduction &&
  610. (op->src[0]->type != GGML_TYPE_F32 || op->src[1]->type == GGML_TYPE_F32);
  611. case GGML_OP_CPY:
  612. case GGML_OP_DUP:
  613. case GGML_OP_CONT:
  614. {
  615. switch (op->src[0]->type) {
  616. case GGML_TYPE_F32:
  617. switch (op->type) {
  618. case GGML_TYPE_F16:
  619. case GGML_TYPE_F32:
  620. case GGML_TYPE_Q8_0:
  621. case GGML_TYPE_Q4_0:
  622. case GGML_TYPE_Q4_1:
  623. return true;
  624. default:
  625. return false;
  626. }
  627. case GGML_TYPE_F16:
  628. switch (op->type) {
  629. case GGML_TYPE_F16:
  630. case GGML_TYPE_F32:
  631. return true;
  632. default:
  633. return false;
  634. }
  635. default:
  636. return false;
  637. };
  638. }
  639. case GGML_OP_DIAG_MASK_INF:
  640. case GGML_OP_GET_ROWS:
  641. {
  642. return op->ne[3] == 1;
  643. }
  644. default:
  645. return false;
  646. }
  647. }
  648. static bool ggml_metal_graph_compute(
  649. struct ggml_metal_context * ctx,
  650. struct ggml_cgraph * gf) {
  651. @autoreleasepool {
  652. MTLComputePassDescriptor * edesc = MTLComputePassDescriptor.computePassDescriptor;
  653. edesc.dispatchType = MTLDispatchTypeSerial;
  654. // create multiple command buffers and enqueue them
  655. // then, we encode the graph into the command buffers in parallel
  656. const int n_nodes = gf->n_nodes;
  657. const int n_cb = ctx->n_cb;
  658. const int n_nodes_per_cb = (n_nodes + n_cb - 1) / n_cb;
  659. const bool should_capture = ctx->should_capture_next_compute;
  660. if (should_capture) {
  661. ctx->should_capture_next_compute = false;
  662. MTLCaptureDescriptor * descriptor = [MTLCaptureDescriptor new];
  663. descriptor.captureObject = ctx->queue;
  664. NSError * error = nil;
  665. if (![[MTLCaptureManager sharedCaptureManager] startCaptureWithDescriptor:descriptor error:&error]) {
  666. GGML_METAL_LOG_ERROR("%s: error: unable to start capture '%s'\n", __func__, [[error localizedDescription] UTF8String]);
  667. GGML_ASSERT(!"capture failed");
  668. }
  669. }
  670. id<MTLCommandBuffer> command_buffer_builder[n_cb];
  671. for (int cb_idx = 0; cb_idx < n_cb; ++cb_idx) {
  672. id<MTLCommandBuffer> command_buffer = [ctx->queue commandBufferWithUnretainedReferences];
  673. command_buffer_builder[cb_idx] = command_buffer;
  674. // enqueue the command buffers in order to specify their execution order
  675. [command_buffer enqueue];
  676. }
  677. const id<MTLCommandBuffer> *command_buffers = command_buffer_builder;
  678. dispatch_apply(n_cb, ctx->d_queue, ^(size_t iter) {
  679. const int cb_idx = iter;
  680. size_t offs_src0 = 0;
  681. size_t offs_src1 = 0;
  682. size_t offs_src2 = 0;
  683. size_t offs_dst = 0;
  684. id<MTLCommandBuffer> command_buffer = command_buffers[cb_idx];
  685. id<MTLComputeCommandEncoder> encoder = [command_buffer computeCommandEncoderWithDescriptor: edesc];
  686. const int node_start = (cb_idx + 0) * n_nodes_per_cb;
  687. const int node_end = MIN((cb_idx == n_cb - 1) ? n_nodes : (cb_idx + 1) * n_nodes_per_cb, n_nodes);
  688. for (int i = node_start; i < node_end; ++i) {
  689. if (i == -1) {
  690. [encoder memoryBarrierWithScope:MTLBarrierScopeBuffers];
  691. continue;
  692. }
  693. //GGML_METAL_LOG_INFO("%s: encoding node %3d, op = %8s\n", __func__, i, ggml_op_name(gf->nodes[i]->op));
  694. struct ggml_tensor * src0 = gf->nodes[i]->src[0];
  695. struct ggml_tensor * src1 = gf->nodes[i]->src[1];
  696. struct ggml_tensor * src2 = gf->nodes[i]->src[2];
  697. struct ggml_tensor * dst = gf->nodes[i];
  698. switch (dst->op) {
  699. case GGML_OP_NONE:
  700. case GGML_OP_RESHAPE:
  701. case GGML_OP_VIEW:
  702. case GGML_OP_TRANSPOSE:
  703. case GGML_OP_PERMUTE:
  704. {
  705. // noop -> next node
  706. } continue;
  707. default:
  708. {
  709. } break;
  710. }
  711. if (!ggml_metal_supports_op(ctx, dst)) {
  712. GGML_METAL_LOG_ERROR("%s: error: unsupported op '%s'\n", __func__, ggml_op_desc(dst));
  713. GGML_ASSERT(!"unsupported op");
  714. }
  715. if (should_capture) {
  716. [encoder pushDebugGroup:[NSString stringWithCString:ggml_op_desc(dst) encoding:NSUTF8StringEncoding]];
  717. }
  718. const int64_t ne00 = src0 ? src0->ne[0] : 0;
  719. const int64_t ne01 = src0 ? src0->ne[1] : 0;
  720. const int64_t ne02 = src0 ? src0->ne[2] : 0;
  721. const int64_t ne03 = src0 ? src0->ne[3] : 0;
  722. const uint64_t nb00 = src0 ? src0->nb[0] : 0;
  723. const uint64_t nb01 = src0 ? src0->nb[1] : 0;
  724. const uint64_t nb02 = src0 ? src0->nb[2] : 0;
  725. const uint64_t nb03 = src0 ? src0->nb[3] : 0;
  726. const int64_t ne10 = src1 ? src1->ne[0] : 0;
  727. const int64_t ne11 = src1 ? src1->ne[1] : 0;
  728. const int64_t ne12 = src1 ? src1->ne[2] : 0;
  729. const int64_t ne13 = src1 ? src1->ne[3] : 0; UNUSED(ne13);
  730. const uint64_t nb10 = src1 ? src1->nb[0] : 0;
  731. const uint64_t nb11 = src1 ? src1->nb[1] : 0;
  732. const uint64_t nb12 = src1 ? src1->nb[2] : 0;
  733. const uint64_t nb13 = src1 ? src1->nb[3] : 0; UNUSED(nb13);
  734. const int64_t ne0 = dst ? dst->ne[0] : 0;
  735. const int64_t ne1 = dst ? dst->ne[1] : 0;
  736. const int64_t ne2 = dst ? dst->ne[2] : 0;
  737. const int64_t ne3 = dst ? dst->ne[3] : 0;
  738. const uint64_t nb0 = dst ? dst->nb[0] : 0;
  739. const uint64_t nb1 = dst ? dst->nb[1] : 0;
  740. const uint64_t nb2 = dst ? dst->nb[2] : 0;
  741. const uint64_t nb3 = dst ? dst->nb[3] : 0;
  742. const enum ggml_type src0t = src0 ? src0->type : GGML_TYPE_COUNT;
  743. const enum ggml_type src1t = src1 ? src1->type : GGML_TYPE_COUNT;
  744. const enum ggml_type dstt = dst ? dst->type : GGML_TYPE_COUNT;
  745. id<MTLBuffer> id_src0 = src0 ? ggml_metal_get_buffer(src0, &offs_src0) : nil;
  746. id<MTLBuffer> id_src1 = src1 ? ggml_metal_get_buffer(src1, &offs_src1) : nil;
  747. id<MTLBuffer> id_src2 = src2 ? ggml_metal_get_buffer(src2, &offs_src2) : nil;
  748. id<MTLBuffer> id_dst = dst ? ggml_metal_get_buffer(dst, &offs_dst) : nil;
  749. //GGML_METAL_LOG_INFO("%s: op - %s\n", __func__, ggml_op_name(dst->op));
  750. //if (src0) {
  751. // GGML_METAL_LOG_INFO("%s: src0 - %4s [%5lld, %5lld, %5lld], %d, %s\n", __func__, ggml_type_name(src0t), ne00, ne01, ne02,
  752. // ggml_is_contiguous(src0), src0->name);
  753. //}
  754. //if (src1) {
  755. // GGML_METAL_LOG_INFO("%s: src1 - %4s [%5lld, %5lld, %5lld], %d, %s\n", __func__, ggml_type_name(src1t), ne10, ne11, ne12,
  756. // ggml_is_contiguous(src1), src1->name);
  757. //}
  758. //if (dst) {
  759. // GGML_METAL_LOG_INFO("%s: dst - %4s [%5lld, %5lld, %5lld], 1, %s\n", __func__, ggml_type_name(dstt), ne0, ne1, ne2,
  760. // dst->name);
  761. //}
  762. switch (dst->op) {
  763. case GGML_OP_CONCAT:
  764. {
  765. const int64_t nb = ne00;
  766. id<MTLComputePipelineState> pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_CONCAT].pipeline;
  767. [encoder setComputePipelineState:pipeline];
  768. [encoder setBuffer:id_src0 offset:offs_src0 atIndex:0];
  769. [encoder setBuffer:id_src1 offset:offs_src1 atIndex:1];
  770. [encoder setBuffer:id_dst offset:offs_dst atIndex:2];
  771. [encoder setBytes:&ne00 length:sizeof(ne00) atIndex:3];
  772. [encoder setBytes:&ne01 length:sizeof(ne01) atIndex:4];
  773. [encoder setBytes:&ne02 length:sizeof(ne02) atIndex:5];
  774. [encoder setBytes:&ne03 length:sizeof(ne03) atIndex:6];
  775. [encoder setBytes:&nb00 length:sizeof(nb00) atIndex:7];
  776. [encoder setBytes:&nb01 length:sizeof(nb01) atIndex:8];
  777. [encoder setBytes:&nb02 length:sizeof(nb02) atIndex:9];
  778. [encoder setBytes:&nb03 length:sizeof(nb03) atIndex:10];
  779. [encoder setBytes:&ne10 length:sizeof(ne10) atIndex:11];
  780. [encoder setBytes:&ne11 length:sizeof(ne11) atIndex:12];
  781. [encoder setBytes:&ne12 length:sizeof(ne12) atIndex:13];
  782. [encoder setBytes:&ne13 length:sizeof(ne13) atIndex:14];
  783. [encoder setBytes:&nb10 length:sizeof(nb10) atIndex:15];
  784. [encoder setBytes:&nb11 length:sizeof(nb11) atIndex:16];
  785. [encoder setBytes:&nb12 length:sizeof(nb12) atIndex:17];
  786. [encoder setBytes:&nb13 length:sizeof(nb13) atIndex:18];
  787. [encoder setBytes:&ne0 length:sizeof(ne0) atIndex:19];
  788. [encoder setBytes:&ne1 length:sizeof(ne1) atIndex:20];
  789. [encoder setBytes:&ne2 length:sizeof(ne2) atIndex:21];
  790. [encoder setBytes:&ne3 length:sizeof(ne3) atIndex:22];
  791. [encoder setBytes:&nb0 length:sizeof(nb0) atIndex:23];
  792. [encoder setBytes:&nb1 length:sizeof(nb1) atIndex:24];
  793. [encoder setBytes:&nb2 length:sizeof(nb2) atIndex:25];
  794. [encoder setBytes:&nb3 length:sizeof(nb3) atIndex:26];
  795. [encoder setBytes:&nb length:sizeof(nb) atIndex:27];
  796. const int nth = MIN(1024, ne0);
  797. [encoder dispatchThreadgroups:MTLSizeMake(ne1, ne2, ne3) threadsPerThreadgroup:MTLSizeMake(nth, 1, 1)];
  798. } break;
  799. case GGML_OP_ADD:
  800. case GGML_OP_MUL:
  801. case GGML_OP_DIV:
  802. {
  803. const size_t offs = 0;
  804. bool bcast_row = false;
  805. int64_t nb = ne00;
  806. id<MTLComputePipelineState> pipeline = nil;
  807. if (ggml_nelements(src1) == ne10 && ggml_is_contiguous(src1) && ne00 % 4 == 0 && ne10 % 4 == 0) {
  808. GGML_ASSERT(ggml_is_contiguous(src0));
  809. // src1 is a row
  810. GGML_ASSERT(ne11 == 1);
  811. nb = ne00 / 4;
  812. switch (dst->op) {
  813. case GGML_OP_ADD: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_ADD_ROW].pipeline; break;
  814. case GGML_OP_MUL: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_ROW].pipeline; break;
  815. case GGML_OP_DIV: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_DIV_ROW].pipeline; break;
  816. default: GGML_ASSERT(false);
  817. }
  818. bcast_row = true;
  819. } else {
  820. switch (dst->op) {
  821. case GGML_OP_ADD: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_ADD].pipeline; break;
  822. case GGML_OP_MUL: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL].pipeline; break;
  823. case GGML_OP_DIV: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_DIV].pipeline; break;
  824. default: GGML_ASSERT(false);
  825. }
  826. }
  827. [encoder setComputePipelineState:pipeline];
  828. [encoder setBuffer:id_src0 offset:offs_src0 atIndex:0];
  829. [encoder setBuffer:id_src1 offset:offs_src1 atIndex:1];
  830. [encoder setBuffer:id_dst offset:offs_dst atIndex:2];
  831. [encoder setBytes:&ne00 length:sizeof(ne00) atIndex:3];
  832. [encoder setBytes:&ne01 length:sizeof(ne01) atIndex:4];
  833. [encoder setBytes:&ne02 length:sizeof(ne02) atIndex:5];
  834. [encoder setBytes:&ne03 length:sizeof(ne03) atIndex:6];
  835. [encoder setBytes:&nb00 length:sizeof(nb00) atIndex:7];
  836. [encoder setBytes:&nb01 length:sizeof(nb01) atIndex:8];
  837. [encoder setBytes:&nb02 length:sizeof(nb02) atIndex:9];
  838. [encoder setBytes:&nb03 length:sizeof(nb03) atIndex:10];
  839. [encoder setBytes:&ne10 length:sizeof(ne10) atIndex:11];
  840. [encoder setBytes:&ne11 length:sizeof(ne11) atIndex:12];
  841. [encoder setBytes:&ne12 length:sizeof(ne12) atIndex:13];
  842. [encoder setBytes:&ne13 length:sizeof(ne13) atIndex:14];
  843. [encoder setBytes:&nb10 length:sizeof(nb10) atIndex:15];
  844. [encoder setBytes:&nb11 length:sizeof(nb11) atIndex:16];
  845. [encoder setBytes:&nb12 length:sizeof(nb12) atIndex:17];
  846. [encoder setBytes:&nb13 length:sizeof(nb13) atIndex:18];
  847. [encoder setBytes:&ne0 length:sizeof(ne0) atIndex:19];
  848. [encoder setBytes:&ne1 length:sizeof(ne1) atIndex:20];
  849. [encoder setBytes:&ne2 length:sizeof(ne2) atIndex:21];
  850. [encoder setBytes:&ne3 length:sizeof(ne3) atIndex:22];
  851. [encoder setBytes:&nb0 length:sizeof(nb0) atIndex:23];
  852. [encoder setBytes:&nb1 length:sizeof(nb1) atIndex:24];
  853. [encoder setBytes:&nb2 length:sizeof(nb2) atIndex:25];
  854. [encoder setBytes:&nb3 length:sizeof(nb3) atIndex:26];
  855. [encoder setBytes:&offs length:sizeof(offs) atIndex:27];
  856. [encoder setBytes:&nb length:sizeof(nb) atIndex:28];
  857. if (bcast_row) {
  858. const int64_t n = ggml_nelements(dst)/4;
  859. [encoder dispatchThreadgroups:MTLSizeMake(n, 1, 1) threadsPerThreadgroup:MTLSizeMake(1, 1, 1)];
  860. } else {
  861. const int nth = MIN((int) pipeline.maxTotalThreadsPerThreadgroup, ne0);
  862. [encoder dispatchThreadgroups:MTLSizeMake(ne01, ne02, ne03) threadsPerThreadgroup:MTLSizeMake(nth, 1, 1)];
  863. }
  864. } break;
  865. case GGML_OP_ACC:
  866. {
  867. GGML_ASSERT(src0t == GGML_TYPE_F32);
  868. GGML_ASSERT(src1t == GGML_TYPE_F32);
  869. GGML_ASSERT(dstt == GGML_TYPE_F32);
  870. GGML_ASSERT(ggml_is_contiguous(src0));
  871. GGML_ASSERT(ggml_is_contiguous(src1));
  872. const size_t pnb1 = ((int32_t *) dst->op_params)[0];
  873. const size_t pnb2 = ((int32_t *) dst->op_params)[1];
  874. const size_t pnb3 = ((int32_t *) dst->op_params)[2];
  875. const size_t offs = ((int32_t *) dst->op_params)[3];
  876. const bool inplace = (bool) ((int32_t *) dst->op_params)[4];
  877. if (!inplace) {
  878. // run a separete kernel to cpy src->dst
  879. // not sure how to avoid this
  880. // TODO: make a simpler cpy_bytes kernel
  881. const id<MTLComputePipelineState> pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_CPY_F32_F32].pipeline;
  882. [encoder setComputePipelineState:pipeline];
  883. [encoder setBuffer:id_src0 offset:offs_src0 atIndex:0];
  884. [encoder setBuffer:id_dst offset:offs_dst atIndex:1];
  885. [encoder setBytes:&ne00 length:sizeof( int64_t) atIndex:2];
  886. [encoder setBytes:&ne01 length:sizeof( int64_t) atIndex:3];
  887. [encoder setBytes:&ne02 length:sizeof( int64_t) atIndex:4];
  888. [encoder setBytes:&ne03 length:sizeof( int64_t) atIndex:5];
  889. [encoder setBytes:&nb00 length:sizeof(uint64_t) atIndex:6];
  890. [encoder setBytes:&nb01 length:sizeof(uint64_t) atIndex:7];
  891. [encoder setBytes:&nb02 length:sizeof(uint64_t) atIndex:8];
  892. [encoder setBytes:&nb03 length:sizeof(uint64_t) atIndex:9];
  893. [encoder setBytes:&ne0 length:sizeof( int64_t) atIndex:10];
  894. [encoder setBytes:&ne1 length:sizeof( int64_t) atIndex:11];
  895. [encoder setBytes:&ne2 length:sizeof( int64_t) atIndex:12];
  896. [encoder setBytes:&ne3 length:sizeof( int64_t) atIndex:13];
  897. [encoder setBytes:&nb0 length:sizeof(uint64_t) atIndex:14];
  898. [encoder setBytes:&nb1 length:sizeof(uint64_t) atIndex:15];
  899. [encoder setBytes:&nb2 length:sizeof(uint64_t) atIndex:16];
  900. [encoder setBytes:&nb3 length:sizeof(uint64_t) atIndex:17];
  901. const int nth = MIN((int) pipeline.maxTotalThreadsPerThreadgroup, ne00);
  902. [encoder dispatchThreadgroups:MTLSizeMake(ne01, ne02, ne03) threadsPerThreadgroup:MTLSizeMake(nth, 1, 1)];
  903. }
  904. const id<MTLComputePipelineState> pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_ADD].pipeline;
  905. [encoder setComputePipelineState:pipeline];
  906. [encoder setBuffer:id_src0 offset:offs_src0 atIndex:0];
  907. [encoder setBuffer:id_src1 offset:offs_src1 atIndex:1];
  908. [encoder setBuffer:id_dst offset:offs_dst atIndex:2];
  909. [encoder setBytes:&ne00 length:sizeof(ne00) atIndex:3];
  910. [encoder setBytes:&ne01 length:sizeof(ne01) atIndex:4];
  911. [encoder setBytes:&ne02 length:sizeof(ne02) atIndex:5];
  912. [encoder setBytes:&ne03 length:sizeof(ne03) atIndex:6];
  913. [encoder setBytes:&nb00 length:sizeof(nb00) atIndex:7];
  914. [encoder setBytes:&pnb1 length:sizeof(pnb1) atIndex:8];
  915. [encoder setBytes:&pnb2 length:sizeof(pnb2) atIndex:9];
  916. [encoder setBytes:&pnb3 length:sizeof(pnb3) atIndex:10];
  917. [encoder setBytes:&ne10 length:sizeof(ne10) atIndex:11];
  918. [encoder setBytes:&ne11 length:sizeof(ne11) atIndex:12];
  919. [encoder setBytes:&ne12 length:sizeof(ne12) atIndex:13];
  920. [encoder setBytes:&ne13 length:sizeof(ne13) atIndex:14];
  921. [encoder setBytes:&nb10 length:sizeof(nb10) atIndex:15];
  922. [encoder setBytes:&nb11 length:sizeof(nb11) atIndex:16];
  923. [encoder setBytes:&nb12 length:sizeof(nb12) atIndex:17];
  924. [encoder setBytes:&nb13 length:sizeof(nb13) atIndex:18];
  925. [encoder setBytes:&ne0 length:sizeof(ne0) atIndex:19];
  926. [encoder setBytes:&ne1 length:sizeof(ne1) atIndex:20];
  927. [encoder setBytes:&ne2 length:sizeof(ne2) atIndex:21];
  928. [encoder setBytes:&ne3 length:sizeof(ne3) atIndex:22];
  929. [encoder setBytes:&nb0 length:sizeof(nb0) atIndex:23];
  930. [encoder setBytes:&pnb1 length:sizeof(pnb1) atIndex:24];
  931. [encoder setBytes:&pnb2 length:sizeof(pnb2) atIndex:25];
  932. [encoder setBytes:&pnb3 length:sizeof(pnb3) atIndex:26];
  933. [encoder setBytes:&offs length:sizeof(offs) atIndex:27];
  934. const int nth = MIN((int) pipeline.maxTotalThreadsPerThreadgroup, ne00);
  935. [encoder dispatchThreadgroups:MTLSizeMake(ne11, ne12, ne13) threadsPerThreadgroup:MTLSizeMake(nth, 1, 1)];
  936. } break;
  937. case GGML_OP_SCALE:
  938. {
  939. GGML_ASSERT(ggml_is_contiguous(src0));
  940. const float scale = *(const float *) dst->op_params;
  941. int64_t n = ggml_nelements(dst);
  942. id<MTLComputePipelineState> pipeline = nil;
  943. if (n % 4 == 0) {
  944. n /= 4;
  945. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_SCALE_4].pipeline;
  946. } else {
  947. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_SCALE].pipeline;
  948. }
  949. [encoder setComputePipelineState:pipeline];
  950. [encoder setBuffer:id_src0 offset:offs_src0 atIndex:0];
  951. [encoder setBuffer:id_dst offset:offs_dst atIndex:1];
  952. [encoder setBytes:&scale length:sizeof(scale) atIndex:2];
  953. [encoder dispatchThreadgroups:MTLSizeMake(n, 1, 1) threadsPerThreadgroup:MTLSizeMake(1, 1, 1)];
  954. } break;
  955. case GGML_OP_UNARY:
  956. switch (ggml_get_unary_op(gf->nodes[i])) {
  957. case GGML_UNARY_OP_TANH:
  958. {
  959. id<MTLComputePipelineState> pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_TANH].pipeline;
  960. [encoder setComputePipelineState:pipeline];
  961. [encoder setBuffer:id_src0 offset:offs_src0 atIndex:0];
  962. [encoder setBuffer:id_dst offset:offs_dst atIndex:1];
  963. const int64_t n = ggml_nelements(dst);
  964. [encoder dispatchThreadgroups:MTLSizeMake(n, 1, 1) threadsPerThreadgroup:MTLSizeMake(1, 1, 1)];
  965. } break;
  966. case GGML_UNARY_OP_RELU:
  967. {
  968. id<MTLComputePipelineState> pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_RELU].pipeline;
  969. [encoder setComputePipelineState:pipeline];
  970. [encoder setBuffer:id_src0 offset:offs_src0 atIndex:0];
  971. [encoder setBuffer:id_dst offset:offs_dst atIndex:1];
  972. const int64_t n = ggml_nelements(dst);
  973. [encoder dispatchThreadgroups:MTLSizeMake(n, 1, 1) threadsPerThreadgroup:MTLSizeMake(1, 1, 1)];
  974. } break;
  975. case GGML_UNARY_OP_GELU:
  976. {
  977. id<MTLComputePipelineState> pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_GELU].pipeline;
  978. [encoder setComputePipelineState:pipeline];
  979. [encoder setBuffer:id_src0 offset:offs_src0 atIndex:0];
  980. [encoder setBuffer:id_dst offset:offs_dst atIndex:1];
  981. const int64_t n = ggml_nelements(dst);
  982. GGML_ASSERT(n % 4 == 0);
  983. [encoder dispatchThreadgroups:MTLSizeMake(n/4, 1, 1) threadsPerThreadgroup:MTLSizeMake(1, 1, 1)];
  984. } break;
  985. case GGML_UNARY_OP_GELU_QUICK:
  986. {
  987. id<MTLComputePipelineState> pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_GELU_QUICK].pipeline;
  988. [encoder setComputePipelineState:pipeline];
  989. [encoder setBuffer:id_src0 offset:offs_src0 atIndex:0];
  990. [encoder setBuffer:id_dst offset:offs_dst atIndex:1];
  991. const int64_t n = ggml_nelements(dst);
  992. GGML_ASSERT(n % 4 == 0);
  993. [encoder dispatchThreadgroups:MTLSizeMake(n/4, 1, 1) threadsPerThreadgroup:MTLSizeMake(1, 1, 1)];
  994. } break;
  995. case GGML_UNARY_OP_SILU:
  996. {
  997. id<MTLComputePipelineState> pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_SILU].pipeline;
  998. [encoder setComputePipelineState:pipeline];
  999. [encoder setBuffer:id_src0 offset:offs_src0 atIndex:0];
  1000. [encoder setBuffer:id_dst offset:offs_dst atIndex:1];
  1001. const int64_t n = ggml_nelements(dst);
  1002. GGML_ASSERT(n % 4 == 0);
  1003. [encoder dispatchThreadgroups:MTLSizeMake(n/4, 1, 1) threadsPerThreadgroup:MTLSizeMake(1, 1, 1)];
  1004. } break;
  1005. default:
  1006. {
  1007. GGML_METAL_LOG_WARN("%s: node %3d, op = %8s not implemented\n", __func__, i, ggml_op_name(dst->op));
  1008. GGML_ASSERT(false);
  1009. }
  1010. } break;
  1011. case GGML_OP_SQR:
  1012. {
  1013. GGML_ASSERT(ggml_is_contiguous(src0));
  1014. id<MTLComputePipelineState> pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_SQR].pipeline;
  1015. [encoder setComputePipelineState:pipeline];
  1016. [encoder setBuffer:id_src0 offset:offs_src0 atIndex:0];
  1017. [encoder setBuffer:id_dst offset:offs_dst atIndex:1];
  1018. const int64_t n = ggml_nelements(dst);
  1019. [encoder dispatchThreadgroups:MTLSizeMake(n, 1, 1) threadsPerThreadgroup:MTLSizeMake(1, 1, 1)];
  1020. } break;
  1021. case GGML_OP_SUM_ROWS:
  1022. {
  1023. GGML_ASSERT(src0->nb[0] == ggml_type_size(src0->type));
  1024. id<MTLComputePipelineState> pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_SUM_ROWS].pipeline;
  1025. [encoder setComputePipelineState:pipeline];
  1026. [encoder setBuffer:id_src0 offset:offs_src0 atIndex:0];
  1027. [encoder setBuffer:id_dst offset:offs_dst atIndex:1];
  1028. [encoder setBytes:&ne00 length:sizeof(ne00) atIndex:2];
  1029. [encoder setBytes:&ne01 length:sizeof(ne01) atIndex:3];
  1030. [encoder setBytes:&ne02 length:sizeof(ne02) atIndex:4];
  1031. [encoder setBytes:&ne03 length:sizeof(ne03) atIndex:5];
  1032. [encoder setBytes:&nb00 length:sizeof(nb00) atIndex:6];
  1033. [encoder setBytes:&nb01 length:sizeof(nb01) atIndex:7];
  1034. [encoder setBytes:&nb02 length:sizeof(nb02) atIndex:8];
  1035. [encoder setBytes:&nb03 length:sizeof(nb03) atIndex:9];
  1036. [encoder setBytes:&ne10 length:sizeof(ne10) atIndex:10];
  1037. [encoder setBytes:&ne11 length:sizeof(ne11) atIndex:11];
  1038. [encoder setBytes:&ne12 length:sizeof(ne12) atIndex:12];
  1039. [encoder setBytes:&ne13 length:sizeof(ne13) atIndex:13];
  1040. [encoder setBytes:&nb10 length:sizeof(nb10) atIndex:14];
  1041. [encoder setBytes:&nb11 length:sizeof(nb11) atIndex:15];
  1042. [encoder setBytes:&nb12 length:sizeof(nb12) atIndex:16];
  1043. [encoder setBytes:&nb13 length:sizeof(nb13) atIndex:17];
  1044. [encoder setBytes:&ne0 length:sizeof(ne0) atIndex:18];
  1045. [encoder setBytes:&ne1 length:sizeof(ne1) atIndex:19];
  1046. [encoder setBytes:&ne2 length:sizeof(ne2) atIndex:20];
  1047. [encoder setBytes:&ne3 length:sizeof(ne3) atIndex:21];
  1048. [encoder setBytes:&nb0 length:sizeof(nb0) atIndex:22];
  1049. [encoder setBytes:&nb1 length:sizeof(nb1) atIndex:23];
  1050. [encoder setBytes:&nb2 length:sizeof(nb2) atIndex:24];
  1051. [encoder setBytes:&nb3 length:sizeof(nb3) atIndex:25];
  1052. [encoder dispatchThreadgroups:MTLSizeMake(ne01, ne02, ne03) threadsPerThreadgroup:MTLSizeMake(1, 1, 1)];
  1053. } break;
  1054. case GGML_OP_SOFT_MAX:
  1055. {
  1056. int nth = 32; // SIMD width
  1057. id<MTLComputePipelineState> pipeline = nil;
  1058. if (ne00%4 == 0) {
  1059. while (nth < ne00/4 && nth < 256) {
  1060. nth *= 2;
  1061. }
  1062. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_SOFT_MAX_4].pipeline;
  1063. } else {
  1064. while (nth < ne00 && nth < 1024) {
  1065. nth *= 2;
  1066. }
  1067. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_SOFT_MAX].pipeline;
  1068. }
  1069. const float scale = ((float *) dst->op_params)[0];
  1070. const float max_bias = ((float *) dst->op_params)[1];
  1071. const int64_t nrows_x = ggml_nrows(src0);
  1072. const int64_t nrows_y = src0->ne[1];
  1073. const uint32_t n_head_kv = nrows_x/nrows_y;
  1074. const uint32_t n_head_log2 = 1u << (uint32_t) floorf(log2f((float) n_head_kv));
  1075. const float m0 = powf(2.0f, -(max_bias ) / n_head_log2);
  1076. const float m1 = powf(2.0f, -(max_bias / 2.0f) / n_head_log2);
  1077. [encoder setComputePipelineState:pipeline];
  1078. [encoder setBuffer:id_src0 offset:offs_src0 atIndex:0];
  1079. if (id_src1) {
  1080. [encoder setBuffer:id_src1 offset:offs_src1 atIndex:1];
  1081. } else {
  1082. [encoder setBuffer:id_src0 offset:offs_src0 atIndex:1];
  1083. }
  1084. if (id_src2) {
  1085. [encoder setBuffer:id_src2 offset:offs_src2 atIndex:2];
  1086. } else {
  1087. [encoder setBuffer:id_src0 offset:offs_src0 atIndex:2];
  1088. }
  1089. [encoder setBuffer:id_dst offset:offs_dst atIndex:3];
  1090. [encoder setBytes:&ne00 length:sizeof(ne00) atIndex:4];
  1091. [encoder setBytes:&ne01 length:sizeof(ne01) atIndex:5];
  1092. [encoder setBytes:&ne02 length:sizeof(ne02) atIndex:6];
  1093. [encoder setBytes:&scale length:sizeof(scale) atIndex:7];
  1094. [encoder setBytes:&max_bias length:sizeof(max_bias) atIndex:8];
  1095. [encoder setBytes:&m0 length:sizeof(m0) atIndex:9];
  1096. [encoder setBytes:&m1 length:sizeof(m1) atIndex:10];
  1097. [encoder setBytes:&n_head_log2 length:sizeof(n_head_log2) atIndex:11];
  1098. [encoder setThreadgroupMemoryLength:32*sizeof(float) atIndex:0];
  1099. [encoder dispatchThreadgroups:MTLSizeMake(ne01*ne02*ne03, 1, 1) threadsPerThreadgroup:MTLSizeMake(nth, 1, 1)];
  1100. } break;
  1101. case GGML_OP_DIAG_MASK_INF:
  1102. {
  1103. const int n_past = ((int32_t *)(dst->op_params))[0];
  1104. id<MTLComputePipelineState> pipeline = nil;
  1105. if (ne00%8 == 0) {
  1106. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_DIAG_MASK_INF_8].pipeline;
  1107. } else {
  1108. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_DIAG_MASK_INF].pipeline;
  1109. }
  1110. [encoder setComputePipelineState:pipeline];
  1111. [encoder setBuffer:id_src0 offset:offs_src0 atIndex:0];
  1112. [encoder setBuffer:id_dst offset:offs_dst atIndex:1];
  1113. [encoder setBytes:&ne00 length:sizeof(ne00) atIndex:2];
  1114. [encoder setBytes:&ne01 length:sizeof(ne01) atIndex:3];
  1115. [encoder setBytes:&n_past length:sizeof(int) atIndex:4];
  1116. if (ne00%8 == 0) {
  1117. [encoder dispatchThreadgroups:MTLSizeMake(ne00*ne01*ne02/8, 1, 1) threadsPerThreadgroup:MTLSizeMake(1, 1, 1)];
  1118. }
  1119. else {
  1120. [encoder dispatchThreadgroups:MTLSizeMake(ne00, ne01, ne02) threadsPerThreadgroup:MTLSizeMake(1, 1, 1)];
  1121. }
  1122. } break;
  1123. case GGML_OP_MUL_MAT:
  1124. {
  1125. GGML_ASSERT(ne00 == ne10);
  1126. // TODO: assert that dim2 and dim3 are contiguous
  1127. GGML_ASSERT(ne12 % ne02 == 0);
  1128. GGML_ASSERT(ne13 % ne03 == 0);
  1129. const uint r2 = ne12/ne02;
  1130. const uint r3 = ne13/ne03;
  1131. // find the break-even point where the matrix-matrix kernel becomes more efficient compared
  1132. // to the matrix-vector kernel
  1133. int ne11_mm_min = 1;
  1134. #if 0
  1135. // the numbers below are measured on M2 Ultra for 7B and 13B models
  1136. // these numbers do not translate to other devices or model sizes
  1137. // TODO: need to find a better approach
  1138. if ([ctx->device.name isEqualToString:@"Apple M2 Ultra"]) {
  1139. switch (src0t) {
  1140. case GGML_TYPE_F16: ne11_mm_min = 2; break;
  1141. case GGML_TYPE_Q8_0: ne11_mm_min = 7; break;
  1142. case GGML_TYPE_Q2_K: ne11_mm_min = 15; break;
  1143. case GGML_TYPE_Q3_K: ne11_mm_min = 7; break;
  1144. case GGML_TYPE_Q4_0:
  1145. case GGML_TYPE_Q4_1: ne11_mm_min = 15; break;
  1146. case GGML_TYPE_Q4_K: ne11_mm_min = 11; break;
  1147. case GGML_TYPE_Q5_0: // not tested yet
  1148. case GGML_TYPE_Q5_1: ne11_mm_min = 13; break; // not tested yet
  1149. case GGML_TYPE_Q5_K: ne11_mm_min = 7; break;
  1150. case GGML_TYPE_Q6_K: ne11_mm_min = 7; break;
  1151. default: ne11_mm_min = 1; break;
  1152. }
  1153. }
  1154. #endif
  1155. // for now the matrix-matrix multiplication kernel only works on A14+/M1+ SoCs
  1156. // AMD GPU and older A-chips will reuse matrix-vector multiplication kernel
  1157. if ([ctx->device supportsFamily:MTLGPUFamilyApple7] &&
  1158. !ggml_is_transposed(src0) &&
  1159. !ggml_is_transposed(src1) &&
  1160. src1t == GGML_TYPE_F32 &&
  1161. ne00 % 32 == 0 && ne00 >= 64 &&
  1162. (ne11 > ne11_mm_min || (ggml_is_quantized(src0t) && ne12 > 1))) {
  1163. //printf("matrix: ne00 = %6d, ne01 = %6d, ne02 = %6d, ne11 = %6d, ne12 = %6d\n", ne00, ne01, ne02, ne11, ne12);
  1164. id<MTLComputePipelineState> pipeline = nil;
  1165. switch (src0->type) {
  1166. case GGML_TYPE_F32: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MM_F32_F32 ].pipeline; break;
  1167. case GGML_TYPE_F16: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MM_F16_F32 ].pipeline; break;
  1168. case GGML_TYPE_Q4_0: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MM_Q4_0_F32 ].pipeline; break;
  1169. case GGML_TYPE_Q4_1: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MM_Q4_1_F32 ].pipeline; break;
  1170. case GGML_TYPE_Q5_0: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MM_Q5_0_F32 ].pipeline; break;
  1171. case GGML_TYPE_Q5_1: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MM_Q5_1_F32 ].pipeline; break;
  1172. case GGML_TYPE_Q8_0: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MM_Q8_0_F32 ].pipeline; break;
  1173. case GGML_TYPE_Q2_K: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MM_Q2_K_F32 ].pipeline; break;
  1174. case GGML_TYPE_Q3_K: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MM_Q3_K_F32 ].pipeline; break;
  1175. case GGML_TYPE_Q4_K: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MM_Q4_K_F32 ].pipeline; break;
  1176. case GGML_TYPE_Q5_K: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MM_Q5_K_F32 ].pipeline; break;
  1177. case GGML_TYPE_Q6_K: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MM_Q6_K_F32 ].pipeline; break;
  1178. case GGML_TYPE_IQ2_XXS: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MM_IQ2_XXS_F32].pipeline; break;
  1179. case GGML_TYPE_IQ2_XS: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MM_IQ2_XS_F32 ].pipeline; break;
  1180. case GGML_TYPE_IQ3_XXS: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MM_IQ3_XXS_F32].pipeline; break;
  1181. case GGML_TYPE_IQ3_S: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MM_IQ3_S_F32 ].pipeline; break;
  1182. case GGML_TYPE_IQ1_S: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MM_IQ1_S_F32 ].pipeline; break;
  1183. case GGML_TYPE_IQ4_NL: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MM_IQ4_NL_F32 ].pipeline; break;
  1184. default: GGML_ASSERT(false && "MUL MAT-MAT not implemented");
  1185. }
  1186. [encoder setComputePipelineState:pipeline];
  1187. [encoder setBuffer:id_src0 offset:offs_src0 atIndex:0];
  1188. [encoder setBuffer:id_src1 offset:offs_src1 atIndex:1];
  1189. [encoder setBuffer:id_dst offset:offs_dst atIndex:2];
  1190. [encoder setBytes:&ne00 length:sizeof(ne00) atIndex:3];
  1191. [encoder setBytes:&ne02 length:sizeof(ne02) atIndex:4];
  1192. [encoder setBytes:&nb01 length:sizeof(nb01) atIndex:5];
  1193. [encoder setBytes:&nb02 length:sizeof(nb02) atIndex:6];
  1194. [encoder setBytes:&ne12 length:sizeof(ne12) atIndex:7];
  1195. [encoder setBytes:&nb10 length:sizeof(nb10) atIndex:8];
  1196. [encoder setBytes:&nb11 length:sizeof(nb11) atIndex:9];
  1197. [encoder setBytes:&nb12 length:sizeof(nb12) atIndex:10];
  1198. [encoder setBytes:&ne0 length:sizeof(ne0) atIndex:11];
  1199. [encoder setBytes:&ne1 length:sizeof(ne1) atIndex:12];
  1200. [encoder setBytes:&r2 length:sizeof(r2) atIndex:13];
  1201. [encoder setBytes:&r3 length:sizeof(r3) atIndex:14];
  1202. [encoder setThreadgroupMemoryLength:8192 atIndex:0];
  1203. [encoder dispatchThreadgroups:MTLSizeMake( (ne11 + 31)/32, (ne01 + 63)/64, ne12*ne13) threadsPerThreadgroup:MTLSizeMake(128, 1, 1)];
  1204. } else {
  1205. int nth0 = 32;
  1206. int nth1 = 1;
  1207. int nrows = 1;
  1208. //printf("vector: ne00 = %6d, ne01 = %6d, ne02 = %6d, ne11 = %6d, ne12 = %6d\n", ne00, ne01, ne02, ne11, ne12);
  1209. id<MTLComputePipelineState> pipeline = nil;
  1210. // use custom matrix x vector kernel
  1211. switch (src0t) {
  1212. case GGML_TYPE_F32:
  1213. {
  1214. GGML_ASSERT(src1t == GGML_TYPE_F32);
  1215. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_F32_F32].pipeline;
  1216. nrows = 4;
  1217. } break;
  1218. case GGML_TYPE_F16:
  1219. {
  1220. nth0 = 32;
  1221. nth1 = 1;
  1222. if (src1t == GGML_TYPE_F32) {
  1223. if (ne11 * ne12 < 4) {
  1224. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_F16_F32_1ROW].pipeline;
  1225. } else if (ne00 >= 128 && ne01 >= 8 && ne00%4 == 0) {
  1226. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_F16_F32_L4].pipeline;
  1227. nrows = ne11;
  1228. } else {
  1229. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_F16_F32].pipeline;
  1230. nrows = 4;
  1231. }
  1232. } else {
  1233. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_F16_F16].pipeline;
  1234. nrows = 4;
  1235. }
  1236. } break;
  1237. case GGML_TYPE_Q4_0:
  1238. {
  1239. nth0 = 8;
  1240. nth1 = 8;
  1241. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_Q4_0_F32].pipeline;
  1242. } break;
  1243. case GGML_TYPE_Q4_1:
  1244. {
  1245. nth0 = 8;
  1246. nth1 = 8;
  1247. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_Q4_1_F32].pipeline;
  1248. } break;
  1249. case GGML_TYPE_Q5_0:
  1250. {
  1251. nth0 = 8;
  1252. nth1 = 8;
  1253. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_Q5_0_F32].pipeline;
  1254. } break;
  1255. case GGML_TYPE_Q5_1:
  1256. {
  1257. nth0 = 8;
  1258. nth1 = 8;
  1259. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_Q5_1_F32].pipeline;
  1260. } break;
  1261. case GGML_TYPE_Q8_0:
  1262. {
  1263. nth0 = 8;
  1264. nth1 = 8;
  1265. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_Q8_0_F32].pipeline;
  1266. } break;
  1267. case GGML_TYPE_Q2_K:
  1268. {
  1269. nth0 = 2;
  1270. nth1 = 32;
  1271. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_Q2_K_F32].pipeline;
  1272. } break;
  1273. case GGML_TYPE_Q3_K:
  1274. {
  1275. nth0 = 2;
  1276. nth1 = 32;
  1277. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_Q3_K_F32].pipeline;
  1278. } break;
  1279. case GGML_TYPE_Q4_K:
  1280. {
  1281. nth0 = 4; //1;
  1282. nth1 = 8; //32;
  1283. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_Q4_K_F32].pipeline;
  1284. } break;
  1285. case GGML_TYPE_Q5_K:
  1286. {
  1287. nth0 = 2;
  1288. nth1 = 32;
  1289. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_Q5_K_F32].pipeline;
  1290. } break;
  1291. case GGML_TYPE_Q6_K:
  1292. {
  1293. nth0 = 2;
  1294. nth1 = 32;
  1295. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_Q6_K_F32].pipeline;
  1296. } break;
  1297. case GGML_TYPE_IQ2_XXS:
  1298. {
  1299. nth0 = 4;
  1300. nth1 = 16;
  1301. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_IQ2_XXS_F32].pipeline;
  1302. } break;
  1303. case GGML_TYPE_IQ2_XS:
  1304. {
  1305. nth0 = 4;
  1306. nth1 = 16;
  1307. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_IQ2_XS_F32].pipeline;
  1308. } break;
  1309. case GGML_TYPE_IQ3_XXS:
  1310. {
  1311. nth0 = 4;
  1312. nth1 = 16;
  1313. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_IQ3_XXS_F32].pipeline;
  1314. } break;
  1315. case GGML_TYPE_IQ3_S:
  1316. {
  1317. nth0 = 4;
  1318. nth1 = 16;
  1319. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_IQ3_S_F32].pipeline;
  1320. } break;
  1321. case GGML_TYPE_IQ1_S:
  1322. {
  1323. nth0 = 4;
  1324. nth1 = 16;
  1325. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_IQ1_S_F32].pipeline;
  1326. } break;
  1327. case GGML_TYPE_IQ4_NL:
  1328. {
  1329. nth0 = 4;
  1330. nth1 = 16;
  1331. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_IQ4_NL_F32].pipeline;
  1332. } break;
  1333. default:
  1334. {
  1335. GGML_METAL_LOG_ERROR("Asserting on type %d\n", (int)src0t);
  1336. GGML_ASSERT(false && "not implemented");
  1337. }
  1338. };
  1339. if (ggml_is_quantized(src0t)) {
  1340. GGML_ASSERT(ne00 >= nth0*nth1);
  1341. }
  1342. [encoder setComputePipelineState:pipeline];
  1343. [encoder setBuffer:id_src0 offset:offs_src0 atIndex:0];
  1344. [encoder setBuffer:id_src1 offset:offs_src1 atIndex:1];
  1345. [encoder setBuffer:id_dst offset:offs_dst atIndex:2];
  1346. [encoder setBytes:&ne00 length:sizeof(ne00) atIndex:3];
  1347. [encoder setBytes:&ne01 length:sizeof(ne01) atIndex:4];
  1348. [encoder setBytes:&ne02 length:sizeof(ne02) atIndex:5];
  1349. [encoder setBytes:&nb00 length:sizeof(nb00) atIndex:6];
  1350. [encoder setBytes:&nb01 length:sizeof(nb01) atIndex:7];
  1351. [encoder setBytes:&nb02 length:sizeof(nb02) atIndex:8];
  1352. [encoder setBytes:&ne10 length:sizeof(ne10) atIndex:9];
  1353. [encoder setBytes:&ne11 length:sizeof(ne11) atIndex:10];
  1354. [encoder setBytes:&ne12 length:sizeof(ne12) atIndex:11];
  1355. [encoder setBytes:&nb10 length:sizeof(nb10) atIndex:12];
  1356. [encoder setBytes:&nb11 length:sizeof(nb11) atIndex:13];
  1357. [encoder setBytes:&nb12 length:sizeof(nb12) atIndex:14];
  1358. [encoder setBytes:&ne0 length:sizeof(ne0) atIndex:15];
  1359. [encoder setBytes:&ne1 length:sizeof(ne1) atIndex:16];
  1360. [encoder setBytes:&r2 length:sizeof(r2) atIndex:17];
  1361. [encoder setBytes:&r3 length:sizeof(r3) atIndex:18];
  1362. if (src0t == GGML_TYPE_Q4_0 || src0t == GGML_TYPE_Q4_1 ||
  1363. src0t == GGML_TYPE_Q5_0 || src0t == GGML_TYPE_Q5_1 || src0t == GGML_TYPE_Q8_0 ||
  1364. src0t == GGML_TYPE_Q2_K || src0t == GGML_TYPE_IQ1_S) { // || src0t == GGML_TYPE_Q4_K) {
  1365. [encoder dispatchThreadgroups:MTLSizeMake((ne01 + 7)/8, ne11, ne12*ne13) threadsPerThreadgroup:MTLSizeMake(nth0, nth1, 1)];
  1366. }
  1367. else if (src0t == GGML_TYPE_IQ2_XXS || src0t == GGML_TYPE_IQ2_XS) {
  1368. const int mem_size = src0t == GGML_TYPE_IQ2_XXS ? 256*8+128 : 512*8+128;
  1369. [encoder setThreadgroupMemoryLength:mem_size atIndex:0];
  1370. [encoder dispatchThreadgroups:MTLSizeMake((ne01 + 7)/8, ne11, ne12*ne13) threadsPerThreadgroup:MTLSizeMake(nth0, nth1, 1)];
  1371. }
  1372. else if (src0t == GGML_TYPE_IQ3_XXS || src0t == GGML_TYPE_IQ3_S) {
  1373. const int mem_size = src0t == GGML_TYPE_IQ3_XXS ? 256*4+128 : 512*4;
  1374. [encoder setThreadgroupMemoryLength:mem_size atIndex:0];
  1375. [encoder dispatchThreadgroups:MTLSizeMake((ne01 + 7)/8, ne11, ne12*ne13) threadsPerThreadgroup:MTLSizeMake(nth0, nth1, 1)];
  1376. }
  1377. else if (src0t == GGML_TYPE_IQ4_NL) {
  1378. const int mem_size = 32*sizeof(float);
  1379. [encoder setThreadgroupMemoryLength:mem_size atIndex:0];
  1380. [encoder dispatchThreadgroups:MTLSizeMake((ne01 + 3)/4, ne11, ne12*ne13) threadsPerThreadgroup:MTLSizeMake(nth0, nth1, 1)];
  1381. }
  1382. else if (src0t == GGML_TYPE_Q4_K) {
  1383. [encoder dispatchThreadgroups:MTLSizeMake((ne01 + 3)/4, ne11, ne12*ne13) threadsPerThreadgroup:MTLSizeMake(nth0, nth1, 1)];
  1384. }
  1385. else if (src0t == GGML_TYPE_Q3_K) {
  1386. #ifdef GGML_QKK_64
  1387. [encoder dispatchThreadgroups:MTLSizeMake((ne01 + 1)/2, ne11, ne12*ne13) threadsPerThreadgroup:MTLSizeMake(nth0, nth1, 1)];
  1388. #else
  1389. [encoder dispatchThreadgroups:MTLSizeMake((ne01 + 3)/4, ne11, ne12*ne13) threadsPerThreadgroup:MTLSizeMake(nth0, nth1, 1)];
  1390. #endif
  1391. }
  1392. else if (src0t == GGML_TYPE_Q5_K) {
  1393. [encoder dispatchThreadgroups:MTLSizeMake((ne01 + 3)/4, ne11, ne12*ne13) threadsPerThreadgroup:MTLSizeMake(nth0, nth1, 1)];
  1394. }
  1395. else if (src0t == GGML_TYPE_Q6_K) {
  1396. [encoder dispatchThreadgroups:MTLSizeMake((ne01 + 1)/2, ne11, ne12*ne13) threadsPerThreadgroup:MTLSizeMake(nth0, nth1, 1)];
  1397. } else {
  1398. const int64_t ny = (ne11 + nrows - 1)/nrows;
  1399. [encoder dispatchThreadgroups:MTLSizeMake(ne01, ny, ne12*ne13) threadsPerThreadgroup:MTLSizeMake(nth0, nth1, 1)];
  1400. }
  1401. }
  1402. } break;
  1403. case GGML_OP_MUL_MAT_ID:
  1404. {
  1405. //GGML_ASSERT(ne00 == ne10);
  1406. //GGML_ASSERT(ne03 == ne13);
  1407. GGML_ASSERT(src0t == GGML_TYPE_I32);
  1408. const int n_as = ((int32_t *) dst->op_params)[1];
  1409. // TODO: make this more general
  1410. GGML_ASSERT(n_as <= 8);
  1411. // max size of the src1ids array in the kernel stack
  1412. GGML_ASSERT(ne11 <= 512);
  1413. const int64_t ne20 = src2 ? src2->ne[0] : 0;
  1414. const int64_t ne21 = src2 ? src2->ne[1] : 0;
  1415. const int64_t ne22 = src2 ? src2->ne[2] : 0;
  1416. const int64_t ne23 = src2 ? src2->ne[3] : 0; GGML_UNUSED(ne23);
  1417. const uint64_t nb20 = src2 ? src2->nb[0] : 0; GGML_UNUSED(nb20);
  1418. const uint64_t nb21 = src2 ? src2->nb[1] : 0;
  1419. const uint64_t nb22 = src2 ? src2->nb[2] : 0;
  1420. const uint64_t nb23 = src2 ? src2->nb[3] : 0; GGML_UNUSED(nb23);
  1421. const enum ggml_type src2t = src2 ? src2->type : GGML_TYPE_COUNT; GGML_UNUSED(src2t);
  1422. GGML_ASSERT(!ggml_is_transposed(src2));
  1423. GGML_ASSERT(!ggml_is_transposed(src1));
  1424. GGML_ASSERT(src1t == GGML_TYPE_F32);
  1425. const uint r2 = ne12/ne22;
  1426. const uint r3 = ne13/ne23;
  1427. // find the break-even point where the matrix-matrix kernel becomes more efficient compared
  1428. // to the matrix-vector kernel
  1429. int ne11_mm_min = n_as;
  1430. const int idx = ((int32_t *) dst->op_params)[0];
  1431. // batch size
  1432. GGML_ASSERT(ne01 == ne11);
  1433. // for now the matrix-matrix multiplication kernel only works on A14+/M1+ SoCs
  1434. // AMD GPU and older A-chips will reuse matrix-vector multiplication kernel
  1435. // !!!
  1436. // TODO: for now, always use mat-vec kernels until we figure out how to improve the
  1437. // indirect matrix multiplication
  1438. // !!!
  1439. if ([ctx->device supportsFamily:MTLGPUFamilyApple7] &&
  1440. ne20 % 32 == 0 && ne20 >= 64 &&
  1441. ne11 > ne11_mm_min) {
  1442. id<MTLComputePipelineState> pipeline = nil;
  1443. switch (src2->type) {
  1444. case GGML_TYPE_F32: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MM_ID_F32_F32 ].pipeline; break;
  1445. case GGML_TYPE_F16: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MM_ID_F16_F32 ].pipeline; break;
  1446. case GGML_TYPE_Q4_0: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MM_ID_Q4_0_F32 ].pipeline; break;
  1447. case GGML_TYPE_Q4_1: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MM_ID_Q4_1_F32 ].pipeline; break;
  1448. case GGML_TYPE_Q5_0: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MM_ID_Q5_0_F32 ].pipeline; break;
  1449. case GGML_TYPE_Q5_1: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MM_ID_Q5_1_F32 ].pipeline; break;
  1450. case GGML_TYPE_Q8_0: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MM_ID_Q8_0_F32 ].pipeline; break;
  1451. case GGML_TYPE_Q2_K: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MM_ID_Q2_K_F32 ].pipeline; break;
  1452. case GGML_TYPE_Q3_K: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MM_ID_Q3_K_F32 ].pipeline; break;
  1453. case GGML_TYPE_Q4_K: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MM_ID_Q4_K_F32 ].pipeline; break;
  1454. case GGML_TYPE_Q5_K: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MM_ID_Q5_K_F32 ].pipeline; break;
  1455. case GGML_TYPE_Q6_K: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MM_ID_Q6_K_F32 ].pipeline; break;
  1456. case GGML_TYPE_IQ2_XXS: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MM_ID_IQ2_XXS_F32].pipeline; break;
  1457. case GGML_TYPE_IQ2_XS: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MM_ID_IQ2_XS_F32 ].pipeline; break;
  1458. case GGML_TYPE_IQ3_XXS: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MM_ID_IQ3_XXS_F32].pipeline; break;
  1459. case GGML_TYPE_IQ3_S: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MM_ID_IQ3_S_F32 ].pipeline; break;
  1460. case GGML_TYPE_IQ1_S: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MM_ID_IQ1_S_F32 ].pipeline; break;
  1461. case GGML_TYPE_IQ4_NL: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MM_ID_IQ4_NL_F32 ].pipeline; break;
  1462. default: GGML_ASSERT(false && "MUL_MAT_ID not implemented");
  1463. }
  1464. [encoder setComputePipelineState:pipeline];
  1465. [encoder setBuffer:id_src0 offset:offs_src0 atIndex:0];
  1466. [encoder setBuffer:id_src1 offset:offs_src1 atIndex:1];
  1467. [encoder setBuffer:id_dst offset:offs_dst atIndex:2];
  1468. [encoder setBytes:&nb01 length:sizeof(nb01) atIndex:3];
  1469. [encoder setBytes:&ne20 length:sizeof(ne20) atIndex:4];
  1470. [encoder setBytes:&ne22 length:sizeof(ne22) atIndex:5];
  1471. [encoder setBytes:&nb21 length:sizeof(nb21) atIndex:6];
  1472. [encoder setBytes:&nb22 length:sizeof(nb22) atIndex:7];
  1473. [encoder setBytes:&ne12 length:sizeof(ne12) atIndex:8];
  1474. [encoder setBytes:&ne13 length:sizeof(ne13) atIndex:9];
  1475. [encoder setBytes:&nb10 length:sizeof(nb10) atIndex:10];
  1476. [encoder setBytes:&nb11 length:sizeof(nb11) atIndex:11];
  1477. [encoder setBytes:&nb12 length:sizeof(nb12) atIndex:12];
  1478. [encoder setBytes:&ne0 length:sizeof(ne0) atIndex:13];
  1479. [encoder setBytes:&ne1 length:sizeof(ne1) atIndex:14];
  1480. [encoder setBytes:&nb1 length:sizeof(nb1) atIndex:15];
  1481. [encoder setBytes:&r2 length:sizeof(r2) atIndex:16];
  1482. [encoder setBytes:&r3 length:sizeof(r3) atIndex:17];
  1483. [encoder setBytes:&idx length:sizeof(idx) atIndex:18];
  1484. // TODO: how to make this an array? read Metal docs
  1485. for (int j = 0; j < 8; ++j) {
  1486. // NOTE: this is done like this to avoid uninitialized kernel arguments when n_as < 8
  1487. struct ggml_tensor * src_cur = dst->src[2 + (j % n_as)];
  1488. size_t offs_src_cur = 0;
  1489. id<MTLBuffer> id_src_cur = ggml_metal_get_buffer(src_cur, &offs_src_cur);
  1490. [encoder setBuffer:id_src_cur offset:offs_src_cur atIndex:19 + j];
  1491. }
  1492. [encoder setThreadgroupMemoryLength:8192 atIndex:0];
  1493. [encoder dispatchThreadgroups:MTLSizeMake((ne11 + 31)/32, (ne21 + 63)/64, n_as*ne12*ne13) threadsPerThreadgroup:MTLSizeMake(128, 1, 1)];
  1494. } else {
  1495. int nth0 = 32;
  1496. int nth1 = 1;
  1497. int nrows = 1;
  1498. //printf("vector: ne00 = %6d, ne01 = %6d, ne02 = %6d, ne11 = %6d, ne12 = %6d\n", ne00, ne01, ne02, ne11, ne12);
  1499. id<MTLComputePipelineState> pipeline = nil;
  1500. // use custom matrix x vector kernel
  1501. switch (src2t) {
  1502. case GGML_TYPE_F32:
  1503. {
  1504. GGML_ASSERT(src1t == GGML_TYPE_F32);
  1505. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_ID_F32_F32].pipeline;
  1506. } break;
  1507. case GGML_TYPE_F16:
  1508. {
  1509. GGML_ASSERT(src1t == GGML_TYPE_F32);
  1510. nth0 = 32;
  1511. nth1 = 1;
  1512. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_ID_F16_F32].pipeline;
  1513. } break;
  1514. case GGML_TYPE_Q4_0:
  1515. {
  1516. nth0 = 8;
  1517. nth1 = 8;
  1518. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_ID_Q4_0_F32].pipeline;
  1519. } break;
  1520. case GGML_TYPE_Q4_1:
  1521. {
  1522. nth0 = 8;
  1523. nth1 = 8;
  1524. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_ID_Q4_1_F32].pipeline;
  1525. } break;
  1526. case GGML_TYPE_Q5_0:
  1527. {
  1528. nth0 = 8;
  1529. nth1 = 8;
  1530. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_ID_Q5_0_F32].pipeline;
  1531. } break;
  1532. case GGML_TYPE_Q5_1:
  1533. {
  1534. nth0 = 8;
  1535. nth1 = 8;
  1536. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_ID_Q5_1_F32].pipeline;
  1537. } break;
  1538. case GGML_TYPE_Q8_0:
  1539. {
  1540. nth0 = 8;
  1541. nth1 = 8;
  1542. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_ID_Q8_0_F32].pipeline;
  1543. } break;
  1544. case GGML_TYPE_Q2_K:
  1545. {
  1546. nth0 = 2;
  1547. nth1 = 32;
  1548. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_ID_Q2_K_F32].pipeline;
  1549. } break;
  1550. case GGML_TYPE_Q3_K:
  1551. {
  1552. nth0 = 2;
  1553. nth1 = 32;
  1554. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_ID_Q3_K_F32].pipeline;
  1555. } break;
  1556. case GGML_TYPE_Q4_K:
  1557. {
  1558. nth0 = 4; //1;
  1559. nth1 = 8; //32;
  1560. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_ID_Q4_K_F32].pipeline;
  1561. } break;
  1562. case GGML_TYPE_Q5_K:
  1563. {
  1564. nth0 = 2;
  1565. nth1 = 32;
  1566. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_ID_Q5_K_F32].pipeline;
  1567. } break;
  1568. case GGML_TYPE_Q6_K:
  1569. {
  1570. nth0 = 2;
  1571. nth1 = 32;
  1572. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_ID_Q6_K_F32].pipeline;
  1573. } break;
  1574. case GGML_TYPE_IQ2_XXS:
  1575. {
  1576. nth0 = 4;
  1577. nth1 = 16;
  1578. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_ID_IQ2_XXS_F32].pipeline;
  1579. } break;
  1580. case GGML_TYPE_IQ2_XS:
  1581. {
  1582. nth0 = 4;
  1583. nth1 = 16;
  1584. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_ID_IQ2_XS_F32].pipeline;
  1585. } break;
  1586. case GGML_TYPE_IQ3_XXS:
  1587. {
  1588. nth0 = 4;
  1589. nth1 = 16;
  1590. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_ID_IQ3_XXS_F32].pipeline;
  1591. } break;
  1592. case GGML_TYPE_IQ3_S:
  1593. {
  1594. nth0 = 4;
  1595. nth1 = 16;
  1596. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_ID_IQ3_S_F32].pipeline;
  1597. } break;
  1598. case GGML_TYPE_IQ1_S:
  1599. {
  1600. nth0 = 4;
  1601. nth1 = 16;
  1602. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_ID_IQ1_S_F32].pipeline;
  1603. } break;
  1604. case GGML_TYPE_IQ4_NL:
  1605. {
  1606. nth0 = 4;
  1607. nth1 = 16;
  1608. pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_MUL_MV_ID_IQ4_NL_F32].pipeline;
  1609. } break;
  1610. default:
  1611. {
  1612. GGML_METAL_LOG_ERROR("Asserting on type %d\n", (int)src2t);
  1613. GGML_ASSERT(false && "not implemented");
  1614. }
  1615. };
  1616. if (ggml_is_quantized(src2t)) {
  1617. GGML_ASSERT(ne20 >= nth0*nth1);
  1618. }
  1619. const int64_t _ne1 = 1; // kernels needs a reference in constant memory
  1620. [encoder setComputePipelineState:pipeline];
  1621. [encoder setBuffer:id_src0 offset:offs_src0 atIndex:0];
  1622. [encoder setBuffer:id_src1 offset:offs_src1 atIndex:1];
  1623. [encoder setBuffer:id_dst offset:offs_dst atIndex:2];
  1624. [encoder setBytes:&nb01 length:sizeof(nb01) atIndex:3];
  1625. [encoder setBytes:&ne20 length:sizeof(ne20) atIndex:4];
  1626. [encoder setBytes:&ne21 length:sizeof(ne21) atIndex:5];
  1627. [encoder setBytes:&ne22 length:sizeof(ne22) atIndex:6];
  1628. [encoder setBytes:&nb20 length:sizeof(nb20) atIndex:7];
  1629. [encoder setBytes:&nb21 length:sizeof(nb21) atIndex:8];
  1630. [encoder setBytes:&nb22 length:sizeof(nb22) atIndex:9];
  1631. [encoder setBytes:&ne10 length:sizeof(ne10) atIndex:10];
  1632. [encoder setBytes:&_ne1 length:sizeof(_ne1) atIndex:11];
  1633. [encoder setBytes:&ne12 length:sizeof(ne12) atIndex:12];
  1634. [encoder setBytes:&ne13 length:sizeof(ne13) atIndex:13];
  1635. [encoder setBytes:&nb10 length:sizeof(nb10) atIndex:14];
  1636. [encoder setBytes:&nb11 length:sizeof(nb11) atIndex:15];
  1637. [encoder setBytes:&nb12 length:sizeof(nb12) atIndex:16];
  1638. [encoder setBytes:&ne0 length:sizeof(ne0) atIndex:17];
  1639. [encoder setBytes:&_ne1 length:sizeof(_ne1) atIndex:18];
  1640. [encoder setBytes:&nb1 length:sizeof(nb1) atIndex:19];
  1641. [encoder setBytes:&r2 length:sizeof(r2) atIndex:20];
  1642. [encoder setBytes:&r3 length:sizeof(r3) atIndex:21];
  1643. [encoder setBytes:&idx length:sizeof(idx) atIndex:22];
  1644. // TODO: how to make this an array? read Metal docs
  1645. for (int j = 0; j < 8; ++j) {
  1646. // NOTE: this is done like this to avoid uninitialized kernel arguments when n_as < 8
  1647. struct ggml_tensor * src_cur = dst->src[2 + (j % n_as)];
  1648. size_t offs_src_cur = 0;
  1649. id<MTLBuffer> id_src_cur = ggml_metal_get_buffer(src_cur, &offs_src_cur);
  1650. [encoder setBuffer:id_src_cur offset:offs_src_cur atIndex:23 + j];
  1651. }
  1652. if (src2t == GGML_TYPE_Q4_0 || src2t == GGML_TYPE_Q4_1 ||
  1653. src2t == GGML_TYPE_Q5_0 || src2t == GGML_TYPE_Q5_1 || src2t == GGML_TYPE_Q8_0 ||
  1654. src2t == GGML_TYPE_Q2_K || src2t == GGML_TYPE_IQ1_S) { // || src2t == GGML_TYPE_Q4_K) {
  1655. [encoder dispatchThreadgroups:MTLSizeMake((ne21 + 7)/8, _ne1, ne01*ne12*ne13) threadsPerThreadgroup:MTLSizeMake(nth0, nth1, 1)];
  1656. }
  1657. else if (src2t == GGML_TYPE_IQ2_XXS || src2t == GGML_TYPE_IQ2_XS) {
  1658. const int mem_size = src2t == GGML_TYPE_IQ2_XXS ? 256*8+128 : 512*8+128;
  1659. [encoder setThreadgroupMemoryLength:mem_size atIndex:0];
  1660. [encoder dispatchThreadgroups:MTLSizeMake((ne21 + 7)/8, _ne1, ne01*ne12*ne13) threadsPerThreadgroup:MTLSizeMake(nth0, nth1, 1)];
  1661. }
  1662. else if (src2t == GGML_TYPE_IQ3_XXS || src2t == GGML_TYPE_IQ3_S) {
  1663. const int mem_size = src2t == GGML_TYPE_IQ3_XXS ? 256*4+128 : 512*4;
  1664. [encoder setThreadgroupMemoryLength:mem_size atIndex:0];
  1665. [encoder dispatchThreadgroups:MTLSizeMake((ne21 + 7)/8, _ne1, ne01*ne12*ne13) threadsPerThreadgroup:MTLSizeMake(nth0, nth1, 1)];
  1666. }
  1667. else if (src2t == GGML_TYPE_IQ4_NL) {
  1668. const int mem_size = 32*sizeof(float);
  1669. [encoder setThreadgroupMemoryLength:mem_size atIndex:0];
  1670. [encoder dispatchThreadgroups:MTLSizeMake((ne21 + 3)/4, _ne1, ne01*ne12*ne13) threadsPerThreadgroup:MTLSizeMake(nth0, nth1, 1)];
  1671. }
  1672. else if (src2t == GGML_TYPE_Q4_K) {
  1673. [encoder dispatchThreadgroups:MTLSizeMake((ne21 + 3)/4, _ne1, ne01*ne12*ne13) threadsPerThreadgroup:MTLSizeMake(nth0, nth1, 1)];
  1674. }
  1675. else if (src2t == GGML_TYPE_Q3_K) {
  1676. #ifdef GGML_QKK_64
  1677. [encoder dispatchThreadgroups:MTLSizeMake((ne21 + 1)/2, _ne1, ne01*ne12*ne13) threadsPerThreadgroup:MTLSizeMake(nth0, nth1, 1)];
  1678. #else
  1679. [encoder dispatchThreadgroups:MTLSizeMake((ne21 + 3)/4, _ne1, ne01*ne12*ne13) threadsPerThreadgroup:MTLSizeMake(nth0, nth1, 1)];
  1680. #endif
  1681. }
  1682. else if (src2t == GGML_TYPE_Q5_K) {
  1683. [encoder dispatchThreadgroups:MTLSizeMake((ne21 + 3)/4, _ne1, ne01*ne12*ne13) threadsPerThreadgroup:MTLSizeMake(nth0, nth1, 1)];
  1684. }
  1685. else if (src2t == GGML_TYPE_Q6_K) {
  1686. [encoder dispatchThreadgroups:MTLSizeMake((ne21 + 1)/2, _ne1, ne01*ne12*ne13) threadsPerThreadgroup:MTLSizeMake(nth0, nth1, 1)];
  1687. } else {
  1688. const int64_t ny = (_ne1 + nrows - 1)/nrows;
  1689. [encoder dispatchThreadgroups:MTLSizeMake(ne21, ny, ne01*ne12*ne13) threadsPerThreadgroup:MTLSizeMake(nth0, nth1, 1)];
  1690. }
  1691. }
  1692. } break;
  1693. case GGML_OP_GET_ROWS:
  1694. {
  1695. id<MTLComputePipelineState> pipeline = nil;
  1696. switch (src0->type) {
  1697. case GGML_TYPE_F32: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_GET_ROWS_F32 ].pipeline; break;
  1698. case GGML_TYPE_F16: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_GET_ROWS_F16 ].pipeline; break;
  1699. case GGML_TYPE_Q4_0: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_GET_ROWS_Q4_0 ].pipeline; break;
  1700. case GGML_TYPE_Q4_1: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_GET_ROWS_Q4_1 ].pipeline; break;
  1701. case GGML_TYPE_Q5_0: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_GET_ROWS_Q5_0 ].pipeline; break;
  1702. case GGML_TYPE_Q5_1: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_GET_ROWS_Q5_1 ].pipeline; break;
  1703. case GGML_TYPE_Q8_0: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_GET_ROWS_Q8_0 ].pipeline; break;
  1704. case GGML_TYPE_Q2_K: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_GET_ROWS_Q2_K ].pipeline; break;
  1705. case GGML_TYPE_Q3_K: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_GET_ROWS_Q3_K ].pipeline; break;
  1706. case GGML_TYPE_Q4_K: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_GET_ROWS_Q4_K ].pipeline; break;
  1707. case GGML_TYPE_Q5_K: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_GET_ROWS_Q5_K ].pipeline; break;
  1708. case GGML_TYPE_Q6_K: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_GET_ROWS_Q6_K ].pipeline; break;
  1709. case GGML_TYPE_IQ2_XXS: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_GET_ROWS_IQ2_XXS].pipeline; break;
  1710. case GGML_TYPE_IQ2_XS: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_GET_ROWS_IQ2_XS ].pipeline; break;
  1711. case GGML_TYPE_IQ3_XXS: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_GET_ROWS_IQ3_XXS].pipeline; break;
  1712. case GGML_TYPE_IQ3_S: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_GET_ROWS_IQ3_S ].pipeline; break;
  1713. case GGML_TYPE_IQ1_S: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_GET_ROWS_IQ1_S ].pipeline; break;
  1714. case GGML_TYPE_IQ4_NL: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_GET_ROWS_IQ4_NL ].pipeline; break;
  1715. case GGML_TYPE_I32: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_GET_ROWS_I32 ].pipeline; break;
  1716. default: GGML_ASSERT(false && "not implemented");
  1717. }
  1718. [encoder setComputePipelineState:pipeline];
  1719. [encoder setBuffer:id_src0 offset:offs_src0 atIndex:0];
  1720. [encoder setBuffer:id_src1 offset:offs_src1 atIndex:1];
  1721. [encoder setBuffer:id_dst offset:offs_dst atIndex:2];
  1722. [encoder setBytes:&ne00 length:sizeof( int64_t) atIndex:3];
  1723. [encoder setBytes:&nb01 length:sizeof(uint64_t) atIndex:4];
  1724. [encoder setBytes:&nb02 length:sizeof(uint64_t) atIndex:5];
  1725. [encoder setBytes:&ne10 length:sizeof( int64_t) atIndex:6];
  1726. [encoder setBytes:&nb10 length:sizeof( int64_t) atIndex:7];
  1727. [encoder setBytes:&nb11 length:sizeof( int64_t) atIndex:8];
  1728. [encoder setBytes:&nb1 length:sizeof(uint64_t) atIndex:9];
  1729. [encoder setBytes:&nb2 length:sizeof(uint64_t) atIndex:10];
  1730. [encoder dispatchThreadgroups:MTLSizeMake(ne10, ne11, 1) threadsPerThreadgroup:MTLSizeMake(32, 1, 1)];
  1731. } break;
  1732. case GGML_OP_RMS_NORM:
  1733. {
  1734. GGML_ASSERT(ne00 % 4 == 0);
  1735. float eps;
  1736. memcpy(&eps, dst->op_params, sizeof(float));
  1737. int nth = 32; // SIMD width
  1738. while (nth < ne00/4 && nth < 1024) {
  1739. nth *= 2;
  1740. }
  1741. id<MTLComputePipelineState> pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_RMS_NORM].pipeline;
  1742. [encoder setComputePipelineState:pipeline];
  1743. [encoder setBuffer:id_src0 offset:offs_src0 atIndex:0];
  1744. [encoder setBuffer:id_dst offset:offs_dst atIndex:1];
  1745. [encoder setBytes:&ne00 length:sizeof( int64_t) atIndex:2];
  1746. [encoder setBytes:&nb01 length:sizeof(uint64_t) atIndex:3];
  1747. [encoder setBytes:&eps length:sizeof( float) atIndex:4];
  1748. [encoder setThreadgroupMemoryLength:32*sizeof(float) atIndex:0];
  1749. const int64_t nrows = ggml_nrows(src0);
  1750. [encoder dispatchThreadgroups:MTLSizeMake(nrows, 1, 1) threadsPerThreadgroup:MTLSizeMake(nth, 1, 1)];
  1751. } break;
  1752. case GGML_OP_GROUP_NORM:
  1753. {
  1754. GGML_ASSERT(ne00 % 4 == 0);
  1755. //float eps;
  1756. //memcpy(&eps, dst->op_params, sizeof(float));
  1757. const float eps = 1e-6f; // TODO: temporarily hardcoded
  1758. const int32_t n_groups = ((int32_t *) dst->op_params)[0];
  1759. int nth = 32; // SIMD width
  1760. //while (nth < ne00/4 && nth < 1024) {
  1761. // nth *= 2;
  1762. //}
  1763. id<MTLComputePipelineState> pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_GROUP_NORM].pipeline;
  1764. [encoder setComputePipelineState:pipeline];
  1765. [encoder setBuffer:id_src0 offset:offs_src0 atIndex:0];
  1766. [encoder setBuffer:id_dst offset:offs_dst atIndex:1];
  1767. [encoder setBytes:&ne00 length:sizeof( int64_t) atIndex:2];
  1768. [encoder setBytes:&ne01 length:sizeof( int64_t) atIndex:3];
  1769. [encoder setBytes:&ne02 length:sizeof( int64_t) atIndex:4];
  1770. [encoder setBytes:&nb00 length:sizeof(uint64_t) atIndex:5];
  1771. [encoder setBytes:&nb01 length:sizeof(uint64_t) atIndex:6];
  1772. [encoder setBytes:&nb02 length:sizeof(uint64_t) atIndex:7];
  1773. [encoder setBytes:&n_groups length:sizeof( int32_t) atIndex:8];
  1774. [encoder setBytes:&eps length:sizeof( float) atIndex:9];
  1775. [encoder setThreadgroupMemoryLength:32*sizeof(float) atIndex:0];
  1776. [encoder dispatchThreadgroups:MTLSizeMake(n_groups, 1, 1) threadsPerThreadgroup:MTLSizeMake(nth, 1, 1)];
  1777. } break;
  1778. case GGML_OP_NORM:
  1779. {
  1780. float eps;
  1781. memcpy(&eps, dst->op_params, sizeof(float));
  1782. const int nth = MIN(256, ne00);
  1783. id<MTLComputePipelineState> pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_NORM].pipeline;
  1784. [encoder setComputePipelineState:pipeline];
  1785. [encoder setBuffer:id_src0 offset:offs_src0 atIndex:0];
  1786. [encoder setBuffer:id_dst offset:offs_dst atIndex:1];
  1787. [encoder setBytes:&ne00 length:sizeof( int64_t) atIndex:2];
  1788. [encoder setBytes:&nb01 length:sizeof(uint64_t) atIndex:3];
  1789. [encoder setBytes:&eps length:sizeof( float) atIndex:4];
  1790. [encoder setThreadgroupMemoryLength:GGML_PAD(nth*sizeof(float), 16) atIndex:0];
  1791. const int64_t nrows = ggml_nrows(src0);
  1792. [encoder dispatchThreadgroups:MTLSizeMake(nrows, 1, 1) threadsPerThreadgroup:MTLSizeMake(nth, 1, 1)];
  1793. } break;
  1794. case GGML_OP_ALIBI:
  1795. {
  1796. GGML_ASSERT((src0t == GGML_TYPE_F32));
  1797. const int nth = MIN(1024, ne00);
  1798. //const int n_past = ((int32_t *) dst->op_params)[0];
  1799. const int n_head = ((int32_t *) dst->op_params)[1];
  1800. float max_bias;
  1801. memcpy(&max_bias, (int32_t *) dst->op_params + 2, sizeof(float));
  1802. const int n_heads_log2_floor = 1 << (int) floor(log2(n_head));
  1803. const float m0 = powf(2.0f, -(max_bias) / n_heads_log2_floor);
  1804. const float m1 = powf(2.0f, -(max_bias / 2.0f) / n_heads_log2_floor);
  1805. id<MTLComputePipelineState> pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_ALIBI_F32].pipeline;
  1806. [encoder setComputePipelineState:pipeline];
  1807. [encoder setBuffer:id_src0 offset:offs_src0 atIndex:0];
  1808. [encoder setBuffer:id_dst offset:offs_dst atIndex:1];
  1809. [encoder setBytes:&ne00 length:sizeof( int64_t) atIndex:2];
  1810. [encoder setBytes:&ne01 length:sizeof( int64_t) atIndex:3];
  1811. [encoder setBytes:&ne02 length:sizeof( int64_t) atIndex:4];
  1812. [encoder setBytes:&ne03 length:sizeof( int64_t) atIndex:5];
  1813. [encoder setBytes:&nb00 length:sizeof(uint64_t) atIndex:6];
  1814. [encoder setBytes:&nb01 length:sizeof(uint64_t) atIndex:7];
  1815. [encoder setBytes:&nb02 length:sizeof(uint64_t) atIndex:8];
  1816. [encoder setBytes:&nb03 length:sizeof(uint64_t) atIndex:9];
  1817. [encoder setBytes:&ne0 length:sizeof( int64_t) atIndex:10];
  1818. [encoder setBytes:&ne1 length:sizeof( int64_t) atIndex:11];
  1819. [encoder setBytes:&ne2 length:sizeof( int64_t) atIndex:12];
  1820. [encoder setBytes:&ne3 length:sizeof( int64_t) atIndex:13];
  1821. [encoder setBytes:&nb0 length:sizeof(uint64_t) atIndex:14];
  1822. [encoder setBytes:&nb1 length:sizeof(uint64_t) atIndex:15];
  1823. [encoder setBytes:&nb2 length:sizeof(uint64_t) atIndex:16];
  1824. [encoder setBytes:&nb3 length:sizeof(uint64_t) atIndex:17];
  1825. [encoder setBytes:&m0 length:sizeof( float) atIndex:18];
  1826. [encoder setBytes:&m1 length:sizeof( float) atIndex:19];
  1827. [encoder setBytes:&n_heads_log2_floor length:sizeof(int) atIndex:20];
  1828. [encoder dispatchThreadgroups:MTLSizeMake(ne01, ne02, ne03) threadsPerThreadgroup:MTLSizeMake(nth, 1, 1)];
  1829. } break;
  1830. case GGML_OP_ROPE:
  1831. {
  1832. GGML_ASSERT(ne10 == ne02);
  1833. const int nth = MIN(1024, ne00);
  1834. const int n_past = ((int32_t *) dst->op_params)[0];
  1835. const int n_dims = ((int32_t *) dst->op_params)[1];
  1836. const int mode = ((int32_t *) dst->op_params)[2];
  1837. // skip 3, n_ctx, used in GLM RoPE, unimplemented in metal
  1838. const int n_orig_ctx = ((int32_t *) dst->op_params)[4];
  1839. float freq_base, freq_scale, ext_factor, attn_factor, beta_fast, beta_slow;
  1840. memcpy(&freq_base, (int32_t *) dst->op_params + 5, sizeof(float));
  1841. memcpy(&freq_scale, (int32_t *) dst->op_params + 6, sizeof(float));
  1842. memcpy(&ext_factor, (int32_t *) dst->op_params + 7, sizeof(float));
  1843. memcpy(&attn_factor, (int32_t *) dst->op_params + 8, sizeof(float));
  1844. memcpy(&beta_fast, (int32_t *) dst->op_params + 9, sizeof(float));
  1845. memcpy(&beta_slow, (int32_t *) dst->op_params + 10, sizeof(float));
  1846. id<MTLComputePipelineState> pipeline = nil;
  1847. switch (src0->type) {
  1848. case GGML_TYPE_F32: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_ROPE_F32].pipeline; break;
  1849. case GGML_TYPE_F16: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_ROPE_F16].pipeline; break;
  1850. default: GGML_ASSERT(false);
  1851. };
  1852. [encoder setComputePipelineState:pipeline];
  1853. [encoder setBuffer:id_src0 offset:offs_src0 atIndex:0];
  1854. [encoder setBuffer:id_src1 offset:offs_src1 atIndex:1];
  1855. [encoder setBuffer:id_dst offset:offs_dst atIndex:2];
  1856. [encoder setBytes:&ne00 length:sizeof( int64_t) atIndex:3];
  1857. [encoder setBytes:&ne01 length:sizeof( int64_t) atIndex:4];
  1858. [encoder setBytes:&ne02 length:sizeof( int64_t) atIndex:5];
  1859. [encoder setBytes:&ne03 length:sizeof( int64_t) atIndex:6];
  1860. [encoder setBytes:&nb00 length:sizeof(uint64_t) atIndex:7];
  1861. [encoder setBytes:&nb01 length:sizeof(uint64_t) atIndex:8];
  1862. [encoder setBytes:&nb02 length:sizeof(uint64_t) atIndex:9];
  1863. [encoder setBytes:&nb03 length:sizeof(uint64_t) atIndex:10];
  1864. [encoder setBytes:&ne0 length:sizeof( int64_t) atIndex:11];
  1865. [encoder setBytes:&ne1 length:sizeof( int64_t) atIndex:12];
  1866. [encoder setBytes:&ne2 length:sizeof( int64_t) atIndex:13];
  1867. [encoder setBytes:&ne3 length:sizeof( int64_t) atIndex:14];
  1868. [encoder setBytes:&nb0 length:sizeof(uint64_t) atIndex:15];
  1869. [encoder setBytes:&nb1 length:sizeof(uint64_t) atIndex:16];
  1870. [encoder setBytes:&nb2 length:sizeof(uint64_t) atIndex:17];
  1871. [encoder setBytes:&nb3 length:sizeof(uint64_t) atIndex:18];
  1872. [encoder setBytes:&n_past length:sizeof( int) atIndex:19];
  1873. [encoder setBytes:&n_dims length:sizeof( int) atIndex:20];
  1874. [encoder setBytes:&mode length:sizeof( int) atIndex:21];
  1875. [encoder setBytes:&n_orig_ctx length:sizeof( int) atIndex:22];
  1876. [encoder setBytes:&freq_base length:sizeof( float) atIndex:23];
  1877. [encoder setBytes:&freq_scale length:sizeof( float) atIndex:24];
  1878. [encoder setBytes:&ext_factor length:sizeof( float) atIndex:25];
  1879. [encoder setBytes:&attn_factor length:sizeof( float) atIndex:26];
  1880. [encoder setBytes:&beta_fast length:sizeof( float) atIndex:27];
  1881. [encoder setBytes:&beta_slow length:sizeof( float) atIndex:28];
  1882. [encoder dispatchThreadgroups:MTLSizeMake(ne01, ne02, ne03) threadsPerThreadgroup:MTLSizeMake(nth, 1, 1)];
  1883. } break;
  1884. case GGML_OP_IM2COL:
  1885. {
  1886. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  1887. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  1888. GGML_ASSERT( dst->type == GGML_TYPE_F16 || dst->type == GGML_TYPE_F32);
  1889. const int32_t s0 = ((const int32_t *)(dst->op_params))[0];
  1890. const int32_t s1 = ((const int32_t *)(dst->op_params))[1];
  1891. const int32_t p0 = ((const int32_t *)(dst->op_params))[2];
  1892. const int32_t p1 = ((const int32_t *)(dst->op_params))[3];
  1893. const int32_t d0 = ((const int32_t *)(dst->op_params))[4];
  1894. const int32_t d1 = ((const int32_t *)(dst->op_params))[5];
  1895. const bool is_2D = ((const int32_t *)(dst->op_params))[6] == 1;
  1896. const int32_t N = src1->ne[is_2D ? 3 : 2];
  1897. const int32_t IC = src1->ne[is_2D ? 2 : 1];
  1898. const int32_t IH = is_2D ? src1->ne[1] : 1;
  1899. const int32_t IW = src1->ne[0];
  1900. const int32_t KH = is_2D ? src0->ne[1] : 1;
  1901. const int32_t KW = src0->ne[0];
  1902. const int32_t OH = is_2D ? dst->ne[2] : 1;
  1903. const int32_t OW = dst->ne[1];
  1904. const int32_t CHW = IC * KH * KW;
  1905. const int32_t ofs0 = src1->nb[is_2D ? 3 : 2] / 4;
  1906. const int32_t ofs1 = src1->nb[is_2D ? 2 : 1] / 4;
  1907. id<MTLComputePipelineState> pipeline = nil;
  1908. switch (dst->type) {
  1909. case GGML_TYPE_F32: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_IM2COL_F32].pipeline; break;
  1910. case GGML_TYPE_F16: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_IM2COL_F16].pipeline; break;
  1911. default: GGML_ASSERT(false);
  1912. };
  1913. [encoder setComputePipelineState:pipeline];
  1914. [encoder setBuffer:id_src1 offset:offs_src1 atIndex:0];
  1915. [encoder setBuffer:id_dst offset:offs_dst atIndex:1];
  1916. [encoder setBytes:&ofs0 length:sizeof( int32_t) atIndex:2];
  1917. [encoder setBytes:&ofs1 length:sizeof( int32_t) atIndex:3];
  1918. [encoder setBytes:&IW length:sizeof( int32_t) atIndex:4];
  1919. [encoder setBytes:&IH length:sizeof( int32_t) atIndex:5];
  1920. [encoder setBytes:&CHW length:sizeof( int32_t) atIndex:6];
  1921. [encoder setBytes:&s0 length:sizeof( int32_t) atIndex:7];
  1922. [encoder setBytes:&s1 length:sizeof( int32_t) atIndex:8];
  1923. [encoder setBytes:&p0 length:sizeof( int32_t) atIndex:9];
  1924. [encoder setBytes:&p1 length:sizeof( int32_t) atIndex:10];
  1925. [encoder setBytes:&d0 length:sizeof( int32_t) atIndex:11];
  1926. [encoder setBytes:&d1 length:sizeof( int32_t) atIndex:12];
  1927. [encoder dispatchThreadgroups:MTLSizeMake(IC, OH, OW) threadsPerThreadgroup:MTLSizeMake(N, KH, KW)];
  1928. } break;
  1929. case GGML_OP_UPSCALE:
  1930. {
  1931. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  1932. const int sf = dst->op_params[0];
  1933. const id<MTLComputePipelineState> pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_UPSCALE_F32].pipeline;
  1934. [encoder setComputePipelineState:pipeline];
  1935. [encoder setBuffer:id_src0 offset:offs_src0 atIndex:0];
  1936. [encoder setBuffer:id_dst offset:offs_dst atIndex:1];
  1937. [encoder setBytes:&ne00 length:sizeof(ne00) atIndex:2];
  1938. [encoder setBytes:&ne01 length:sizeof(ne01) atIndex:3];
  1939. [encoder setBytes:&ne02 length:sizeof(ne02) atIndex:4];
  1940. [encoder setBytes:&ne03 length:sizeof(ne03) atIndex:5];
  1941. [encoder setBytes:&nb00 length:sizeof(nb00) atIndex:6];
  1942. [encoder setBytes:&nb01 length:sizeof(nb01) atIndex:7];
  1943. [encoder setBytes:&nb02 length:sizeof(nb02) atIndex:8];
  1944. [encoder setBytes:&nb03 length:sizeof(nb03) atIndex:9];
  1945. [encoder setBytes:&ne0 length:sizeof(ne0) atIndex:10];
  1946. [encoder setBytes:&ne1 length:sizeof(ne1) atIndex:11];
  1947. [encoder setBytes:&ne2 length:sizeof(ne2) atIndex:12];
  1948. [encoder setBytes:&ne3 length:sizeof(ne3) atIndex:13];
  1949. [encoder setBytes:&nb0 length:sizeof(nb0) atIndex:14];
  1950. [encoder setBytes:&nb1 length:sizeof(nb1) atIndex:15];
  1951. [encoder setBytes:&nb2 length:sizeof(nb2) atIndex:16];
  1952. [encoder setBytes:&nb3 length:sizeof(nb3) atIndex:17];
  1953. [encoder setBytes:&sf length:sizeof(sf) atIndex:18];
  1954. const int nth = MIN((int) pipeline.maxTotalThreadsPerThreadgroup, ne0);
  1955. [encoder dispatchThreadgroups:MTLSizeMake(ne1, ne2, ne3) threadsPerThreadgroup:MTLSizeMake(nth, 1, 1)];
  1956. } break;
  1957. case GGML_OP_PAD:
  1958. {
  1959. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  1960. id<MTLComputePipelineState> pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_PAD_F32].pipeline;
  1961. [encoder setComputePipelineState:pipeline];
  1962. [encoder setBuffer:id_src0 offset:offs_src0 atIndex:0];
  1963. [encoder setBuffer:id_dst offset:offs_dst atIndex:1];
  1964. [encoder setBytes:&ne00 length:sizeof(ne00) atIndex:2];
  1965. [encoder setBytes:&ne01 length:sizeof(ne01) atIndex:3];
  1966. [encoder setBytes:&ne02 length:sizeof(ne02) atIndex:4];
  1967. [encoder setBytes:&ne03 length:sizeof(ne03) atIndex:5];
  1968. [encoder setBytes:&nb00 length:sizeof(nb00) atIndex:6];
  1969. [encoder setBytes:&nb01 length:sizeof(nb01) atIndex:7];
  1970. [encoder setBytes:&nb02 length:sizeof(nb02) atIndex:8];
  1971. [encoder setBytes:&nb03 length:sizeof(nb03) atIndex:9];
  1972. [encoder setBytes:&ne0 length:sizeof(ne0) atIndex:10];
  1973. [encoder setBytes:&ne1 length:sizeof(ne1) atIndex:11];
  1974. [encoder setBytes:&ne2 length:sizeof(ne2) atIndex:12];
  1975. [encoder setBytes:&ne3 length:sizeof(ne3) atIndex:13];
  1976. [encoder setBytes:&nb0 length:sizeof(nb0) atIndex:14];
  1977. [encoder setBytes:&nb1 length:sizeof(nb1) atIndex:15];
  1978. [encoder setBytes:&nb2 length:sizeof(nb2) atIndex:16];
  1979. [encoder setBytes:&nb3 length:sizeof(nb3) atIndex:17];
  1980. const int nth = MIN(1024, ne0);
  1981. [encoder dispatchThreadgroups:MTLSizeMake(ne1, ne2, ne3) threadsPerThreadgroup:MTLSizeMake(nth, 1, 1)];
  1982. } break;
  1983. case GGML_OP_ARGSORT:
  1984. {
  1985. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  1986. GGML_ASSERT( dst->type == GGML_TYPE_I32);
  1987. const int nrows = ggml_nrows(src0);
  1988. enum ggml_sort_order order = (enum ggml_sort_order) dst->op_params[0];
  1989. id<MTLComputePipelineState> pipeline = nil;
  1990. switch (order) {
  1991. case GGML_SORT_ORDER_ASC: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_ARGSORT_F32_I32_ASC].pipeline; break;
  1992. case GGML_SORT_ORDER_DESC: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_ARGSORT_F32_I32_DESC].pipeline; break;
  1993. default: GGML_ASSERT(false);
  1994. };
  1995. [encoder setComputePipelineState:pipeline];
  1996. [encoder setBuffer:id_src0 offset:offs_src0 atIndex:0];
  1997. [encoder setBuffer:id_dst offset:offs_dst atIndex:1];
  1998. [encoder setBytes:&ne00 length:sizeof( int64_t) atIndex:2];
  1999. [encoder dispatchThreadgroups:MTLSizeMake(1, nrows, 1) threadsPerThreadgroup:MTLSizeMake(ne00, 1, 1)];
  2000. } break;
  2001. case GGML_OP_LEAKY_RELU:
  2002. {
  2003. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  2004. float slope;
  2005. memcpy(&slope, dst->op_params, sizeof(float));
  2006. id<MTLComputePipelineState> pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_LEAKY_RELU_F32].pipeline;
  2007. [encoder setComputePipelineState:pipeline];
  2008. [encoder setBuffer:id_src0 offset:offs_src0 atIndex:0];
  2009. [encoder setBuffer:id_dst offset:offs_dst atIndex:1];
  2010. [encoder setBytes:&slope length:sizeof(slope) atIndex:2];
  2011. const int64_t n = ggml_nelements(dst);
  2012. [encoder dispatchThreadgroups:MTLSizeMake(n, 1, 1) threadsPerThreadgroup:MTLSizeMake(1, 1, 1)];
  2013. } break;
  2014. case GGML_OP_DUP:
  2015. case GGML_OP_CPY:
  2016. case GGML_OP_CONT:
  2017. {
  2018. GGML_ASSERT(ne00 % ggml_blck_size(src0->type) == 0);
  2019. int nth = MIN(1024, ne00/ggml_blck_size(src0->type));
  2020. id<MTLComputePipelineState> pipeline = nil;
  2021. switch (src0t) {
  2022. case GGML_TYPE_F32:
  2023. {
  2024. GGML_ASSERT(ne0 % ggml_blck_size(dst->type) == 0);
  2025. switch (dstt) {
  2026. case GGML_TYPE_F16: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_CPY_F32_F16].pipeline; break;
  2027. case GGML_TYPE_F32: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_CPY_F32_F32].pipeline; break;
  2028. case GGML_TYPE_Q8_0: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_CPY_F32_Q8_0].pipeline; break;
  2029. case GGML_TYPE_Q4_0: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_CPY_F32_Q4_0].pipeline; break;
  2030. case GGML_TYPE_Q4_1: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_CPY_F32_Q4_1].pipeline; break;
  2031. //case GGML_TYPE_Q5_0: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_CPY_F32_Q5_0].pipeline; break;
  2032. //case GGML_TYPE_Q5_1: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_CPY_F32_Q5_1].pipeline; break;
  2033. default: GGML_ASSERT(false && "not implemented");
  2034. };
  2035. } break;
  2036. case GGML_TYPE_F16:
  2037. {
  2038. switch (dstt) {
  2039. case GGML_TYPE_F16: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_CPY_F16_F16].pipeline; break;
  2040. case GGML_TYPE_F32: pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_CPY_F16_F32].pipeline; break;
  2041. default: GGML_ASSERT(false && "not implemented");
  2042. };
  2043. } break;
  2044. default: GGML_ASSERT(false && "not implemented");
  2045. }
  2046. [encoder setComputePipelineState:pipeline];
  2047. [encoder setBuffer:id_src0 offset:offs_src0 atIndex:0];
  2048. [encoder setBuffer:id_dst offset:offs_dst atIndex:1];
  2049. [encoder setBytes:&ne00 length:sizeof( int64_t) atIndex:2];
  2050. [encoder setBytes:&ne01 length:sizeof( int64_t) atIndex:3];
  2051. [encoder setBytes:&ne02 length:sizeof( int64_t) atIndex:4];
  2052. [encoder setBytes:&ne03 length:sizeof( int64_t) atIndex:5];
  2053. [encoder setBytes:&nb00 length:sizeof(uint64_t) atIndex:6];
  2054. [encoder setBytes:&nb01 length:sizeof(uint64_t) atIndex:7];
  2055. [encoder setBytes:&nb02 length:sizeof(uint64_t) atIndex:8];
  2056. [encoder setBytes:&nb03 length:sizeof(uint64_t) atIndex:9];
  2057. [encoder setBytes:&ne0 length:sizeof( int64_t) atIndex:10];
  2058. [encoder setBytes:&ne1 length:sizeof( int64_t) atIndex:11];
  2059. [encoder setBytes:&ne2 length:sizeof( int64_t) atIndex:12];
  2060. [encoder setBytes:&ne3 length:sizeof( int64_t) atIndex:13];
  2061. [encoder setBytes:&nb0 length:sizeof(uint64_t) atIndex:14];
  2062. [encoder setBytes:&nb1 length:sizeof(uint64_t) atIndex:15];
  2063. [encoder setBytes:&nb2 length:sizeof(uint64_t) atIndex:16];
  2064. [encoder setBytes:&nb3 length:sizeof(uint64_t) atIndex:17];
  2065. [encoder dispatchThreadgroups:MTLSizeMake(ne01, ne02, ne03) threadsPerThreadgroup:MTLSizeMake(nth, 1, 1)];
  2066. } break;
  2067. default:
  2068. {
  2069. GGML_METAL_LOG_ERROR("%s: error: node %3d, op = %8s not implemented\n", __func__, i, ggml_op_name(dst->op));
  2070. GGML_ASSERT(false);
  2071. }
  2072. }
  2073. if (should_capture) {
  2074. [encoder popDebugGroup];
  2075. }
  2076. }
  2077. [encoder endEncoding];
  2078. [command_buffer commit];
  2079. });
  2080. // Wait for completion and check status of each command buffer
  2081. // needed to detect if the device ran out-of-memory for example (#1881)
  2082. for (int i = 0; i < n_cb; ++i) {
  2083. id<MTLCommandBuffer> command_buffer = command_buffers[i];
  2084. [command_buffer waitUntilCompleted];
  2085. MTLCommandBufferStatus status = [command_buffer status];
  2086. if (status != MTLCommandBufferStatusCompleted) {
  2087. GGML_METAL_LOG_INFO("%s: command buffer %d failed with status %lu\n", __func__, i, status);
  2088. return false;
  2089. }
  2090. }
  2091. if (should_capture) {
  2092. [[MTLCaptureManager sharedCaptureManager] stopCapture];
  2093. }
  2094. }
  2095. return true;
  2096. }
  2097. ////////////////////////////////////////////////////////////////////////////////
  2098. // backend interface
  2099. // default buffer
  2100. static id<MTLDevice> g_backend_device = nil;
  2101. static int g_backend_device_ref_count = 0;
  2102. static id<MTLDevice> ggml_backend_metal_get_device(void) {
  2103. if (g_backend_device == nil) {
  2104. g_backend_device = MTLCreateSystemDefaultDevice();
  2105. }
  2106. g_backend_device_ref_count++;
  2107. return g_backend_device;
  2108. }
  2109. static void ggml_backend_metal_free_device(void) {
  2110. assert(g_backend_device_ref_count > 0);
  2111. g_backend_device_ref_count--;
  2112. if (g_backend_device_ref_count == 0) {
  2113. [g_backend_device release];
  2114. g_backend_device = nil;
  2115. }
  2116. }
  2117. GGML_CALL static const char * ggml_backend_metal_buffer_get_name(ggml_backend_buffer_t buffer) {
  2118. return "Metal";
  2119. UNUSED(buffer);
  2120. }
  2121. GGML_CALL static void ggml_backend_metal_buffer_free_buffer(ggml_backend_buffer_t buffer) {
  2122. struct ggml_backend_metal_buffer_context * ctx = (struct ggml_backend_metal_buffer_context *)buffer->context;
  2123. for (int i = 0; i < ctx->n_buffers; i++) {
  2124. [ctx->buffers[i].metal release];
  2125. }
  2126. ggml_backend_metal_free_device();
  2127. if (ctx->owned) {
  2128. free(ctx->all_data);
  2129. }
  2130. free(ctx);
  2131. }
  2132. GGML_CALL static void * ggml_backend_metal_buffer_get_base(ggml_backend_buffer_t buffer) {
  2133. struct ggml_backend_metal_buffer_context * ctx = (struct ggml_backend_metal_buffer_context *)buffer->context;
  2134. return ctx->all_data;
  2135. }
  2136. GGML_CALL static void ggml_backend_metal_buffer_set_tensor(ggml_backend_buffer_t buffer, struct ggml_tensor * tensor, const void * data, size_t offset, size_t size) {
  2137. memcpy((char *)tensor->data + offset, data, size);
  2138. UNUSED(buffer);
  2139. }
  2140. GGML_CALL static void ggml_backend_metal_buffer_get_tensor(ggml_backend_buffer_t buffer, const struct ggml_tensor * tensor, void * data, size_t offset, size_t size) {
  2141. memcpy(data, (const char *)tensor->data + offset, size);
  2142. UNUSED(buffer);
  2143. }
  2144. GGML_CALL static bool ggml_backend_metal_buffer_cpy_tensor(ggml_backend_buffer_t buffer, const struct ggml_tensor * src, struct ggml_tensor * dst) {
  2145. if (ggml_backend_buffer_is_host(src->buffer)) {
  2146. memcpy(dst->data, src->data, ggml_nbytes(src));
  2147. return true;
  2148. }
  2149. return false;
  2150. UNUSED(buffer);
  2151. }
  2152. GGML_CALL static void ggml_backend_metal_buffer_clear(ggml_backend_buffer_t buffer, uint8_t value) {
  2153. struct ggml_backend_metal_buffer_context * ctx = (struct ggml_backend_metal_buffer_context *)buffer->context;
  2154. memset(ctx->all_data, value, ctx->all_size);
  2155. }
  2156. static struct ggml_backend_buffer_i ggml_backend_metal_buffer_i = {
  2157. /* .get_name = */ ggml_backend_metal_buffer_get_name,
  2158. /* .free_buffer = */ ggml_backend_metal_buffer_free_buffer,
  2159. /* .get_base = */ ggml_backend_metal_buffer_get_base,
  2160. /* .init_tensor = */ NULL,
  2161. /* .set_tensor = */ ggml_backend_metal_buffer_set_tensor,
  2162. /* .get_tensor = */ ggml_backend_metal_buffer_get_tensor,
  2163. /* .cpy_tensor = */ ggml_backend_metal_buffer_cpy_tensor,
  2164. /* .clear = */ ggml_backend_metal_buffer_clear,
  2165. /* .reset = */ NULL,
  2166. };
  2167. // default buffer type
  2168. GGML_CALL static const char * ggml_backend_metal_buffer_type_get_name(ggml_backend_buffer_type_t buft) {
  2169. return "Metal";
  2170. UNUSED(buft);
  2171. }
  2172. static void ggml_backend_metal_log_allocated_size(id<MTLDevice> device) {
  2173. #if TARGET_OS_OSX || (TARGET_OS_IOS && __clang_major__ >= 15)
  2174. if (@available(macOS 10.12, iOS 16.0, *)) {
  2175. GGML_METAL_LOG_INFO(", (%8.2f / %8.2f)",
  2176. device.currentAllocatedSize / 1024.0 / 1024.0,
  2177. device.recommendedMaxWorkingSetSize / 1024.0 / 1024.0);
  2178. if (device.currentAllocatedSize > device.recommendedMaxWorkingSetSize) {
  2179. GGML_METAL_LOG_WARN("%s: warning: current allocated size is greater than the recommended max working set size\n", __func__);
  2180. } else {
  2181. GGML_METAL_LOG_INFO("\n");
  2182. }
  2183. } else {
  2184. GGML_METAL_LOG_INFO(", (%8.2f)\n", device.currentAllocatedSize / 1024.0 / 1024.0);
  2185. }
  2186. #endif
  2187. UNUSED(device);
  2188. }
  2189. GGML_CALL static ggml_backend_buffer_t ggml_backend_metal_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size) {
  2190. struct ggml_backend_metal_buffer_context * ctx = malloc(sizeof(struct ggml_backend_metal_buffer_context));
  2191. const size_t size_page = sysconf(_SC_PAGESIZE);
  2192. size_t size_aligned = size;
  2193. if ((size_aligned % size_page) != 0) {
  2194. size_aligned += (size_page - (size_aligned % size_page));
  2195. }
  2196. id<MTLDevice> device = ggml_backend_metal_get_device();
  2197. ctx->all_data = ggml_metal_host_malloc(size_aligned);
  2198. ctx->all_size = size_aligned;
  2199. ctx->owned = true;
  2200. ctx->n_buffers = 1;
  2201. ctx->buffers[0].data = ctx->all_data;
  2202. ctx->buffers[0].size = size;
  2203. ctx->buffers[0].metal = [device newBufferWithBytesNoCopy:ctx->all_data
  2204. length:size_aligned
  2205. options:MTLResourceStorageModeShared
  2206. deallocator:nil];
  2207. if (ctx->buffers[0].metal == nil) {
  2208. GGML_METAL_LOG_ERROR("%s: error: failed to allocate buffer, size = %8.2f MiB\n", __func__, size_aligned / 1024.0 / 1024.0);
  2209. free(ctx);
  2210. ggml_backend_metal_free_device();
  2211. return NULL;
  2212. }
  2213. GGML_METAL_LOG_INFO("%s: allocated buffer, size = %8.2f MiB", __func__, size_aligned / 1024.0 / 1024.0);
  2214. ggml_backend_metal_log_allocated_size(device);
  2215. return ggml_backend_buffer_init(buft, ggml_backend_metal_buffer_i, ctx, size);
  2216. }
  2217. GGML_CALL static size_t ggml_backend_metal_buffer_type_get_alignment(ggml_backend_buffer_type_t buft) {
  2218. return 32;
  2219. UNUSED(buft);
  2220. }
  2221. GGML_CALL static size_t ggml_backend_metal_buffer_type_get_max_size(ggml_backend_buffer_type_t buft) {
  2222. id<MTLDevice> device = ggml_backend_metal_get_device();
  2223. size_t max_size = device.maxBufferLength;
  2224. ggml_backend_metal_free_device();
  2225. return max_size;
  2226. UNUSED(buft);
  2227. }
  2228. GGML_CALL static bool ggml_backend_metal_buffer_type_supports_backend(ggml_backend_buffer_type_t buft, ggml_backend_t backend) {
  2229. return ggml_backend_is_metal(backend) || ggml_backend_is_cpu(backend);
  2230. UNUSED(buft);
  2231. }
  2232. GGML_CALL static bool ggml_backend_metal_buffer_type_is_host(ggml_backend_buffer_type_t buft) {
  2233. return true;
  2234. UNUSED(buft);
  2235. }
  2236. GGML_CALL ggml_backend_buffer_type_t ggml_backend_metal_buffer_type(void) {
  2237. static struct ggml_backend_buffer_type ggml_backend_buffer_type_metal = {
  2238. /* .iface = */ {
  2239. /* .get_name = */ ggml_backend_metal_buffer_type_get_name,
  2240. /* .alloc_buffer = */ ggml_backend_metal_buffer_type_alloc_buffer,
  2241. /* .get_alignment = */ ggml_backend_metal_buffer_type_get_alignment,
  2242. /* .get_max_size = */ ggml_backend_metal_buffer_type_get_max_size,
  2243. /* .get_alloc_size = */ NULL, // defaults to ggml_nbytes
  2244. /* .supports_backend = */ ggml_backend_metal_buffer_type_supports_backend,
  2245. /* .is_host = */ ggml_backend_metal_buffer_type_is_host,
  2246. },
  2247. /* .context = */ NULL,
  2248. };
  2249. return &ggml_backend_buffer_type_metal;
  2250. }
  2251. // buffer from ptr
  2252. GGML_CALL ggml_backend_buffer_t ggml_backend_metal_buffer_from_ptr(void * data, size_t size, size_t max_size) {
  2253. struct ggml_backend_metal_buffer_context * ctx = malloc(sizeof(struct ggml_backend_metal_buffer_context));
  2254. ctx->all_data = data;
  2255. ctx->all_size = size;
  2256. ctx->owned = false;
  2257. ctx->n_buffers = 0;
  2258. const size_t size_page = sysconf(_SC_PAGESIZE);
  2259. // page-align the data ptr
  2260. {
  2261. const uintptr_t offs = (uintptr_t) data % size_page;
  2262. data = (void *) ((char *) data - offs);
  2263. size += offs;
  2264. }
  2265. size_t size_aligned = size;
  2266. if ((size_aligned % size_page) != 0) {
  2267. size_aligned += (size_page - (size_aligned % size_page));
  2268. }
  2269. id<MTLDevice> device = ggml_backend_metal_get_device();
  2270. // the buffer fits into the max buffer size allowed by the device
  2271. if (size_aligned <= device.maxBufferLength) {
  2272. ctx->buffers[ctx->n_buffers].data = data;
  2273. ctx->buffers[ctx->n_buffers].size = size;
  2274. ctx->buffers[ctx->n_buffers].metal = [device newBufferWithBytesNoCopy:data length:size_aligned options:MTLResourceStorageModeShared deallocator:nil];
  2275. if (ctx->buffers[ctx->n_buffers].metal == nil) {
  2276. GGML_METAL_LOG_ERROR("%s: error: failed to allocate buffer, size = %8.2f MiB\n", __func__, size_aligned / 1024.0 / 1024.0);
  2277. return false;
  2278. }
  2279. GGML_METAL_LOG_INFO("%s: allocated buffer, size = %8.2f MiB", __func__, size_aligned / 1024.0 / 1024.0);
  2280. ++ctx->n_buffers;
  2281. } else {
  2282. // this overlap between the views will guarantee that the tensor with the maximum size will fully fit into
  2283. // one of the views
  2284. const size_t size_ovlp = ((max_size + size_page - 1) / size_page + 1) * size_page; // round-up 2 pages just in case
  2285. const size_t size_step = device.maxBufferLength - size_ovlp;
  2286. const size_t size_view = device.maxBufferLength;
  2287. for (size_t i = 0; i < size; i += size_step) {
  2288. const size_t size_step_aligned = (i + size_view <= size) ? size_view : (size_aligned - i);
  2289. ctx->buffers[ctx->n_buffers].data = (void *) ((uint8_t *) data + i);
  2290. ctx->buffers[ctx->n_buffers].size = size_step_aligned;
  2291. ctx->buffers[ctx->n_buffers].metal = [device newBufferWithBytesNoCopy:(void *) ((uint8_t *) data + i) length:size_step_aligned options:MTLResourceStorageModeShared deallocator:nil];
  2292. if (ctx->buffers[ctx->n_buffers].metal == nil) {
  2293. GGML_METAL_LOG_ERROR("%s: error: failed to allocate buffer, size = %8.2f MiB\n", __func__, size_step_aligned / 1024.0 / 1024.0);
  2294. return false;
  2295. }
  2296. GGML_METAL_LOG_INFO("%s: allocated buffer, size = %8.2f MiB, offs = %12ld", __func__, size_step_aligned / 1024.0 / 1024.0, i);
  2297. if (i + size_step < size) {
  2298. GGML_METAL_LOG_INFO("\n");
  2299. }
  2300. ++ctx->n_buffers;
  2301. }
  2302. }
  2303. ggml_backend_metal_log_allocated_size(device);
  2304. return ggml_backend_buffer_init(ggml_backend_metal_buffer_type(), ggml_backend_metal_buffer_i, ctx, size);
  2305. }
  2306. // backend
  2307. GGML_CALL static const char * ggml_backend_metal_name(ggml_backend_t backend) {
  2308. return "Metal";
  2309. UNUSED(backend);
  2310. }
  2311. GGML_CALL static void ggml_backend_metal_free(ggml_backend_t backend) {
  2312. struct ggml_metal_context * ctx = (struct ggml_metal_context *)backend->context;
  2313. ggml_metal_free(ctx);
  2314. free(backend);
  2315. }
  2316. GGML_CALL static ggml_backend_buffer_type_t ggml_backend_metal_get_default_buffer_type(ggml_backend_t backend) {
  2317. return ggml_backend_metal_buffer_type();
  2318. UNUSED(backend);
  2319. }
  2320. GGML_CALL static bool ggml_backend_metal_graph_compute(ggml_backend_t backend, struct ggml_cgraph * cgraph) {
  2321. struct ggml_metal_context * metal_ctx = (struct ggml_metal_context *)backend->context;
  2322. return ggml_metal_graph_compute(metal_ctx, cgraph);
  2323. }
  2324. GGML_CALL static bool ggml_backend_metal_supports_op(ggml_backend_t backend, const struct ggml_tensor * op) {
  2325. struct ggml_metal_context * metal_ctx = (struct ggml_metal_context *)backend->context;
  2326. return ggml_metal_supports_op(metal_ctx, op);
  2327. }
  2328. static struct ggml_backend_i ggml_backend_metal_i = {
  2329. /* .get_name = */ ggml_backend_metal_name,
  2330. /* .free = */ ggml_backend_metal_free,
  2331. /* .get_default_buffer_type = */ ggml_backend_metal_get_default_buffer_type,
  2332. /* .set_tensor_async = */ NULL,
  2333. /* .get_tensor_async = */ NULL,
  2334. /* .cpy_tensor_async = */ NULL,
  2335. /* .synchronize = */ NULL,
  2336. /* .graph_plan_create = */ NULL,
  2337. /* .graph_plan_free = */ NULL,
  2338. /* .graph_plan_compute = */ NULL,
  2339. /* .graph_compute = */ ggml_backend_metal_graph_compute,
  2340. /* .supports_op = */ ggml_backend_metal_supports_op,
  2341. };
  2342. void ggml_backend_metal_log_set_callback(ggml_log_callback log_callback, void * user_data) {
  2343. ggml_metal_log_callback = log_callback;
  2344. ggml_metal_log_user_data = user_data;
  2345. }
  2346. ggml_backend_t ggml_backend_metal_init(void) {
  2347. struct ggml_metal_context * ctx = ggml_metal_init(GGML_DEFAULT_N_THREADS);
  2348. if (ctx == NULL) {
  2349. return NULL;
  2350. }
  2351. ggml_backend_t metal_backend = malloc(sizeof(struct ggml_backend));
  2352. *metal_backend = (struct ggml_backend) {
  2353. /* .interface = */ ggml_backend_metal_i,
  2354. /* .context = */ ctx,
  2355. };
  2356. return metal_backend;
  2357. }
  2358. bool ggml_backend_is_metal(ggml_backend_t backend) {
  2359. return backend && backend->iface.get_name == ggml_backend_metal_name;
  2360. }
  2361. void ggml_backend_metal_set_n_cb(ggml_backend_t backend, int n_cb) {
  2362. GGML_ASSERT(ggml_backend_is_metal(backend));
  2363. struct ggml_metal_context * ctx = (struct ggml_metal_context *)backend->context;
  2364. ctx->n_cb = MIN(n_cb, GGML_METAL_MAX_BUFFERS);
  2365. }
  2366. bool ggml_backend_metal_supports_family(ggml_backend_t backend, int family) {
  2367. GGML_ASSERT(ggml_backend_is_metal(backend));
  2368. struct ggml_metal_context * ctx = (struct ggml_metal_context *)backend->context;
  2369. return [ctx->device supportsFamily:(MTLGPUFamilyApple1 + family - 1)];
  2370. }
  2371. void ggml_backend_metal_capture_next_compute(ggml_backend_t backend) {
  2372. GGML_ASSERT(ggml_backend_is_metal(backend));
  2373. struct ggml_metal_context * ctx = (struct ggml_metal_context *)backend->context;
  2374. ctx->should_capture_next_compute = true;
  2375. }
  2376. GGML_CALL ggml_backend_t ggml_backend_reg_metal_init(const char * params, void * user_data); // silence warning
  2377. GGML_CALL ggml_backend_t ggml_backend_reg_metal_init(const char * params, void * user_data) {
  2378. return ggml_backend_metal_init();
  2379. GGML_UNUSED(params);
  2380. GGML_UNUSED(user_data);
  2381. }