ggml-metal.m 149 KB

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