ggml-metal.m 158 KB

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