ggml-metal.m 143 KB

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