ggml-metal.m 146 KB

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