ggml-metal.m 146 KB

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