ggml-metal.m 146 KB

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