ggml-metal.m 183 KB

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