ggml-metal.m 163 KB

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