ggml-metal.m 149 KB

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