ggml-metal.m 143 KB

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