ggml-metal-darwin_arm64.m 184 KB

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