ggml-metal.m 205 KB

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