ggml-metal.m 144 KB

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