ggml-metal.m 143 KB

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