ggml-metal.m 147 KB

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