ggml-metal.m 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131
  1. #import "ggml-metal.h"
  2. #import "ggml.h"
  3. #import <Foundation/Foundation.h>
  4. #import <Metal/Metal.h>
  5. #import <MetalPerformanceShaders/MetalPerformanceShaders.h>
  6. #ifdef GGML_METAL_NDEBUG
  7. #define metal_printf(...)
  8. #else
  9. #define metal_printf(...) fprintf(stderr, __VA_ARGS__)
  10. #endif
  11. #define UNUSED(x) (void)(x)
  12. struct ggml_metal_buffer {
  13. const char * name;
  14. void * data;
  15. size_t size;
  16. id<MTLBuffer> metal;
  17. };
  18. struct ggml_metal_context {
  19. int n_cb;
  20. float * logits;
  21. id<MTLDevice> device;
  22. id<MTLCommandQueue> queue;
  23. id<MTLLibrary> library;
  24. int n_buffers;
  25. struct ggml_metal_buffer buffers[GGML_METAL_MAX_BUFFERS];
  26. int concur_list[GGML_MAX_NODES];
  27. int concur_list_len;
  28. // custom kernels
  29. #define GGML_METAL_DECL_KERNEL(name) \
  30. id<MTLFunction> function_##name; \
  31. id<MTLComputePipelineState> pipeline_##name
  32. GGML_METAL_DECL_KERNEL(add);
  33. GGML_METAL_DECL_KERNEL(add_row); // TODO: avoid this extra kernel, instead extend the "add" kernel to support broadcast
  34. GGML_METAL_DECL_KERNEL(mul);
  35. GGML_METAL_DECL_KERNEL(mul_row); // TODO: avoid this extra kernel, instead extend the "mul" kernel to support broadcast
  36. GGML_METAL_DECL_KERNEL(scale);
  37. GGML_METAL_DECL_KERNEL(silu);
  38. GGML_METAL_DECL_KERNEL(relu);
  39. GGML_METAL_DECL_KERNEL(gelu);
  40. GGML_METAL_DECL_KERNEL(soft_max);
  41. GGML_METAL_DECL_KERNEL(diag_mask_inf);
  42. GGML_METAL_DECL_KERNEL(get_rows_f16);
  43. GGML_METAL_DECL_KERNEL(get_rows_q4_0);
  44. GGML_METAL_DECL_KERNEL(get_rows_q4_1);
  45. GGML_METAL_DECL_KERNEL(get_rows_q2_K);
  46. GGML_METAL_DECL_KERNEL(get_rows_q3_K);
  47. GGML_METAL_DECL_KERNEL(get_rows_q4_K);
  48. GGML_METAL_DECL_KERNEL(get_rows_q5_K);
  49. GGML_METAL_DECL_KERNEL(get_rows_q6_K);
  50. GGML_METAL_DECL_KERNEL(rms_norm);
  51. GGML_METAL_DECL_KERNEL(norm);
  52. GGML_METAL_DECL_KERNEL(mul_mat_f16_f32);
  53. GGML_METAL_DECL_KERNEL(mul_mat_q4_0_f32);
  54. GGML_METAL_DECL_KERNEL(mul_mat_q4_1_f32);
  55. GGML_METAL_DECL_KERNEL(mul_mat_q2_K_f32);
  56. GGML_METAL_DECL_KERNEL(mul_mat_q3_K_f32);
  57. GGML_METAL_DECL_KERNEL(mul_mat_q4_K_f32);
  58. GGML_METAL_DECL_KERNEL(mul_mat_q5_K_f32);
  59. GGML_METAL_DECL_KERNEL(mul_mat_q6_K_f32);
  60. GGML_METAL_DECL_KERNEL(rope);
  61. GGML_METAL_DECL_KERNEL(alibi_f32);
  62. GGML_METAL_DECL_KERNEL(cpy_f32_f16);
  63. GGML_METAL_DECL_KERNEL(cpy_f32_f32);
  64. GGML_METAL_DECL_KERNEL(cpy_f16_f16);
  65. #undef GGML_METAL_DECL_KERNEL
  66. };
  67. // MSL code
  68. // TODO: move the contents here when ready
  69. // for now it is easier to work in a separate file
  70. static NSString * const msl_library_source = @"see metal.metal";
  71. // Here to assist with NSBundle Path Hack
  72. @interface GGMLMetalClass : NSObject
  73. @end
  74. @implementation GGMLMetalClass
  75. @end
  76. struct ggml_metal_context * ggml_metal_init(int n_cb) {
  77. fprintf(stderr, "%s: allocating\n", __func__);
  78. struct ggml_metal_context * ctx = malloc(sizeof(struct ggml_metal_context));
  79. ctx->n_cb = n_cb;
  80. ctx->device = MTLCreateSystemDefaultDevice();
  81. ctx->queue = [ctx->device newCommandQueue];
  82. ctx->n_buffers = 0;
  83. ctx->concur_list_len = 0;
  84. // determine if we can use MPS
  85. if (MPSSupportsMTLDevice(ctx->device)) {
  86. fprintf(stderr, "%s: using MPS\n", __func__);
  87. } else {
  88. fprintf(stderr, "%s: not using MPS\n", __func__);
  89. GGML_ASSERT(false && "MPS not supported");
  90. }
  91. #if 0
  92. // compile from source string and show compile log
  93. {
  94. NSError * error = nil;
  95. ctx->library = [ctx->device newLibraryWithSource:msl_library_source options:nil error:&error];
  96. if (error) {
  97. fprintf(stderr, "%s: error: %s\n", __func__, [[error description] UTF8String]);
  98. exit(1);
  99. }
  100. }
  101. #else
  102. UNUSED(msl_library_source);
  103. // read the source from "ggml-metal.metal" into a string and use newLibraryWithSource
  104. {
  105. NSError * error = nil;
  106. //NSString * path = [[NSBundle mainBundle] pathForResource:@"../../examples/metal/metal" ofType:@"metal"];
  107. NSBundle * bundle = [NSBundle bundleForClass:[GGMLMetalClass class]];
  108. NSString * path = [bundle pathForResource:@"ggml-metal" ofType:@"metal"];
  109. fprintf(stderr, "%s: loading '%s'\n", __func__, [path UTF8String]);
  110. NSString * src = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error];
  111. if (error) {
  112. fprintf(stderr, "%s: error: %s\n", __func__, [[error description] UTF8String]);
  113. exit(1);
  114. }
  115. #ifdef GGML_QKK_64
  116. MTLCompileOptions* options = [MTLCompileOptions new];
  117. options.preprocessorMacros = @{ @"QK_K" : @(64) };
  118. ctx->library = [ctx->device newLibraryWithSource:src options:options error:&error];
  119. #else
  120. ctx->library = [ctx->device newLibraryWithSource:src options:nil error:&error];
  121. #endif
  122. if (error) {
  123. fprintf(stderr, "%s: error: %s\n", __func__, [[error description] UTF8String]);
  124. exit(1);
  125. }
  126. }
  127. #endif
  128. // load kernels
  129. {
  130. #define GGML_METAL_ADD_KERNEL(name) \
  131. ctx->function_##name = [ctx->library newFunctionWithName:@"kernel_"#name]; \
  132. ctx->pipeline_##name = [ctx->device newComputePipelineStateWithFunction:ctx->function_##name error:nil]; \
  133. fprintf(stderr, "%s: loaded %-32s %16p\n", __func__, "kernel_"#name, (void *) ctx->pipeline_##name);
  134. GGML_METAL_ADD_KERNEL(add);
  135. GGML_METAL_ADD_KERNEL(add_row);
  136. GGML_METAL_ADD_KERNEL(mul);
  137. GGML_METAL_ADD_KERNEL(mul_row);
  138. GGML_METAL_ADD_KERNEL(scale);
  139. GGML_METAL_ADD_KERNEL(silu);
  140. GGML_METAL_ADD_KERNEL(relu);
  141. GGML_METAL_ADD_KERNEL(gelu);
  142. GGML_METAL_ADD_KERNEL(soft_max);
  143. GGML_METAL_ADD_KERNEL(diag_mask_inf);
  144. GGML_METAL_ADD_KERNEL(get_rows_f16);
  145. GGML_METAL_ADD_KERNEL(get_rows_q4_0);
  146. GGML_METAL_ADD_KERNEL(get_rows_q4_1);
  147. GGML_METAL_ADD_KERNEL(get_rows_q2_K);
  148. GGML_METAL_ADD_KERNEL(get_rows_q3_K);
  149. GGML_METAL_ADD_KERNEL(get_rows_q4_K);
  150. GGML_METAL_ADD_KERNEL(get_rows_q5_K);
  151. GGML_METAL_ADD_KERNEL(get_rows_q6_K);
  152. GGML_METAL_ADD_KERNEL(rms_norm);
  153. GGML_METAL_ADD_KERNEL(norm);
  154. GGML_METAL_ADD_KERNEL(mul_mat_f16_f32);
  155. GGML_METAL_ADD_KERNEL(mul_mat_q4_0_f32);
  156. GGML_METAL_ADD_KERNEL(mul_mat_q4_1_f32);
  157. GGML_METAL_ADD_KERNEL(mul_mat_q2_K_f32);
  158. GGML_METAL_ADD_KERNEL(mul_mat_q3_K_f32);
  159. GGML_METAL_ADD_KERNEL(mul_mat_q4_K_f32);
  160. GGML_METAL_ADD_KERNEL(mul_mat_q5_K_f32);
  161. GGML_METAL_ADD_KERNEL(mul_mat_q6_K_f32);
  162. GGML_METAL_ADD_KERNEL(rope);
  163. GGML_METAL_ADD_KERNEL(alibi_f32);
  164. GGML_METAL_ADD_KERNEL(cpy_f32_f16);
  165. GGML_METAL_ADD_KERNEL(cpy_f32_f32);
  166. GGML_METAL_ADD_KERNEL(cpy_f16_f16);
  167. #undef GGML_METAL_ADD_KERNEL
  168. }
  169. fprintf(stderr, "%s: recommendedMaxWorkingSetSize = %8.2f MB\n", __func__, ctx->device.recommendedMaxWorkingSetSize / 1024.0 / 1024.0);
  170. fprintf(stderr, "%s: hasUnifiedMemory = %s\n", __func__, ctx->device.hasUnifiedMemory ? "true" : "false");
  171. if (ctx->device.maxTransferRate != 0) {
  172. fprintf(stderr, "%s: maxTransferRate = %8.2f MB/s\n", __func__, ctx->device.maxTransferRate / 1024.0 / 1024.0);
  173. } else {
  174. fprintf(stderr, "%s: maxTransferRate = built-in GPU\n", __func__);
  175. }
  176. return ctx;
  177. }
  178. void ggml_metal_free(struct ggml_metal_context * ctx) {
  179. fprintf(stderr, "%s: deallocating\n", __func__);
  180. for (int i = 0; i < ctx->n_buffers; ++i) {
  181. [ctx->buffers[i].metal release];
  182. }
  183. free(ctx);
  184. }
  185. void ggml_metal_set_n_cb(struct ggml_metal_context * ctx, int n_cb) {
  186. ctx->n_cb = n_cb;
  187. }
  188. bool ggml_metal_if_optimized(struct ggml_metal_context * ctx) {
  189. if (ctx->concur_list_len) {
  190. return true;
  191. }
  192. return false;
  193. }
  194. // finds the Metal buffer that contains the tensor data on the GPU device
  195. // the assumption is that there is 1-to-1 mapping between the host and device memory buffers, so we can find the
  196. // Metal buffer based on the host memory pointer
  197. //
  198. static id<MTLBuffer> ggml_metal_get_buffer(struct ggml_metal_context * ctx, struct ggml_tensor * t, size_t * offs) {
  199. //fprintf(stderr, "%s: data tensor '%16s', offs_data = %8ld, offs_eval = %8ld, offs_cach = %8ld\n", __func__, t->name, offs_data, offs_eval, offs_cach);
  200. const int64_t tsize = ggml_nbytes(t);
  201. // find the view that contains the tensor fully
  202. for (int i = 0; i < ctx->n_buffers; ++i) {
  203. const int64_t ioffs = (int64_t) t->data - (int64_t) ctx->buffers[i].data;
  204. if (ioffs >= 0 && ioffs + tsize <= (int64_t) ctx->buffers[i].size) {
  205. *offs = (size_t) ioffs;
  206. //fprintf(stderr, "%s: '%s' tensor '%16s', offs = %8ld\n", __func__, ctx->buffers[i].name, t->name, *offs);
  207. return ctx->buffers[i].metal;
  208. }
  209. }
  210. fprintf(stderr, "%s: error: buffer is nil\n", __func__);
  211. return nil;
  212. }
  213. bool ggml_metal_add_buffer(
  214. struct ggml_metal_context * ctx,
  215. const char * name,
  216. void * data,
  217. size_t size,
  218. size_t max_size) {
  219. if (ctx->n_buffers >= GGML_METAL_MAX_BUFFERS) {
  220. fprintf(stderr, "%s: too many buffers\n", __func__);
  221. return false;
  222. }
  223. if (data) {
  224. // verify that the buffer does not overlap with any of the existing buffers
  225. for (int i = 0; i < ctx->n_buffers; ++i) {
  226. const int64_t ioffs = (int64_t) data - (int64_t) ctx->buffers[i].data;
  227. if (ioffs >= 0 && ioffs < (int64_t) ctx->buffers[i].size) {
  228. fprintf(stderr, "%s: error: buffer '%s' overlaps with '%s'\n", __func__, name, ctx->buffers[i].name);
  229. return false;
  230. }
  231. }
  232. const size_t size_page = getpagesize();
  233. size_t size_aligned = size;
  234. if ((size_aligned % size_page) != 0) {
  235. size_aligned += (size_page - (size_aligned % size_page));
  236. }
  237. // the buffer fits into the max buffer size allowed by the device
  238. if (size_aligned <= ctx->device.maxBufferLength) {
  239. ctx->buffers[ctx->n_buffers].name = name;
  240. ctx->buffers[ctx->n_buffers].data = data;
  241. ctx->buffers[ctx->n_buffers].size = size;
  242. ctx->buffers[ctx->n_buffers].metal = [ctx->device newBufferWithBytesNoCopy:data length:size_aligned options:MTLResourceStorageModeShared deallocator:nil];
  243. if (ctx->buffers[ctx->n_buffers].metal == nil) {
  244. fprintf(stderr, "%s: failed to allocate '%-16s' buffer, size = %8.2f MB\n", __func__, name, size_aligned / 1024.0 / 1024.0);
  245. return false;
  246. }
  247. fprintf(stderr, "%s: allocated '%-16s' buffer, size = %8.2f MB", __func__, name, size_aligned / 1024.0 / 1024.0);
  248. ++ctx->n_buffers;
  249. } else {
  250. // this overlap between the views will guarantee that the tensor with the maximum size will fully fit into
  251. // one of the views
  252. const size_t size_ovlp = ((max_size + size_page - 1) / size_page + 1) * size_page; // round-up 2 pages just in case
  253. const size_t size_step = ctx->device.maxBufferLength - size_ovlp;
  254. const size_t size_view = ctx->device.maxBufferLength;
  255. for (size_t i = 0; i < size; i += size_step) {
  256. const size_t size_step_aligned = (i + size_view <= size) ? size_view : (size_aligned - i);
  257. ctx->buffers[ctx->n_buffers].name = name;
  258. ctx->buffers[ctx->n_buffers].data = (void *) ((uint8_t *) data + i);
  259. ctx->buffers[ctx->n_buffers].size = size_step_aligned;
  260. ctx->buffers[ctx->n_buffers].metal = [ctx->device newBufferWithBytesNoCopy:(void *) ((uint8_t *) data + i) length:size_step_aligned options:MTLResourceStorageModeShared deallocator:nil];
  261. if (ctx->buffers[ctx->n_buffers].metal == nil) {
  262. fprintf(stderr, "%s: failed to allocate '%-16s' buffer, size = %8.2f MB\n", __func__, name, size_step_aligned / 1024.0 / 1024.0);
  263. return false;
  264. }
  265. fprintf(stderr, "%s: allocated '%-16s' buffer, size = %8.2f MB, offs = %12ld", __func__, name, size_step_aligned / 1024.0 / 1024.0, i);
  266. if (i + size_step < size) {
  267. fprintf(stderr, "\n");
  268. }
  269. ++ctx->n_buffers;
  270. }
  271. }
  272. fprintf(stderr, ", (%8.2f / %8.2f)",
  273. ctx->device.currentAllocatedSize / 1024.0 / 1024.0,
  274. ctx->device.recommendedMaxWorkingSetSize / 1024.0 / 1024.0);
  275. if (ctx->device.currentAllocatedSize > ctx->device.recommendedMaxWorkingSetSize) {
  276. fprintf(stderr, ", warning: current allocated size is greater than the recommended max working set size\n");
  277. } else {
  278. fprintf(stderr, "\n");
  279. }
  280. }
  281. return true;
  282. }
  283. void ggml_metal_set_tensor(
  284. struct ggml_metal_context * ctx,
  285. struct ggml_tensor * t) {
  286. metal_printf("%s: set input for tensor '%s'\n", __func__, t->name);
  287. size_t offs;
  288. id<MTLBuffer> id_dst = ggml_metal_get_buffer(ctx, t, &offs);
  289. memcpy((void *) ((uint8_t *) id_dst.contents + offs), t->data, ggml_nbytes(t));
  290. }
  291. void ggml_metal_get_tensor(
  292. struct ggml_metal_context * ctx,
  293. struct ggml_tensor * t) {
  294. metal_printf("%s: extract results for tensor '%s'\n", __func__, t->name);
  295. size_t offs;
  296. id<MTLBuffer> id_src = ggml_metal_get_buffer(ctx, t, &offs);
  297. memcpy(t->data, (void *) ((uint8_t *) id_src.contents + offs), ggml_nbytes(t));
  298. }
  299. void ggml_metal_graph_find_concurrency(
  300. struct ggml_metal_context * ctx,
  301. struct ggml_cgraph * gf) {
  302. int search_depth = gf->n_nodes; //we only find concurrency in this range to avoid wasting too much time
  303. int nodes_unused[GGML_MAX_NODES];
  304. for (int i = 0; i < GGML_MAX_NODES; i++) {ctx->concur_list[i] = 0;}
  305. for (int i = 0; i < gf->n_nodes; i++) {nodes_unused[i] = 1;}
  306. ctx->concur_list_len = 0;
  307. int n_left = gf->n_nodes;
  308. int n_start = 0; // all nodes before n_start at nodes_unused array have been sorted and store back to ctx->concur_list
  309. int level_pos = 0; // at ctx->concur_list, the last layer (level) ends at level_pos
  310. while (n_left > 0) {
  311. // number of nodes at a layer (that can be issued concurrently)
  312. int concurrency = 0;
  313. for (int i = n_start; i < ((n_start + search_depth > gf->n_nodes) ? gf->n_nodes : n_start + search_depth); i++) {
  314. if (nodes_unused[i]) {
  315. // if the requirements for gf->nodes[i] are satisfied
  316. int exe_flag=1;
  317. // scan all srcs
  318. for (int src_ind = 0; src_ind < GGML_MAX_SRC; src_ind++) {
  319. struct ggml_tensor * src_cur = gf->nodes[i]->src[src_ind];
  320. if (src_cur) {
  321. // if is leaf nodes it's satisfied.
  322. if (src_cur->op == GGML_OP_NONE && src_cur->grad == NULL) {continue;}
  323. // otherwise this src should be the output from previous nodes.
  324. int is_found = 0;
  325. // scan 2*search_depth back because we inserted barrier.
  326. for (int j = ((level_pos - 2*search_depth) < 0 ? 0 : (level_pos - 2*search_depth)); j < level_pos; j++) {
  327. if (gf->nodes[ctx->concur_list[j]] == src_cur) {is_found = 1; break;}
  328. }
  329. if (is_found == 0) {exe_flag = 0; break;}
  330. }
  331. }
  332. if (exe_flag) {
  333. // check if nodes[i]'s data will be overwritten by a node before nodes[i].
  334. // if node[5] and node[3] write to the same memory region, then we can't issue node[5] before node[3]
  335. int64_t data_start = (int64_t) gf->nodes[i]->data;
  336. int64_t length = (int64_t) ggml_nbytes(gf->nodes[i]);
  337. for (int j = n_start; j < i; j++) {
  338. if (nodes_unused[j] && gf->nodes[j]->op != GGML_OP_RESHAPE \
  339. && gf->nodes[j]->op != GGML_OP_VIEW \
  340. && gf->nodes[j]->op != GGML_OP_TRANSPOSE \
  341. && gf->nodes[j]->op != GGML_OP_PERMUTE) {
  342. if (((int64_t)gf->nodes[j]->data) >= data_start + length || \
  343. ((int64_t)gf->nodes[j]->data) + (int64_t) ggml_nbytes(gf->nodes[j]) <= data_start) {
  344. continue;
  345. } else {
  346. exe_flag = 0;
  347. }
  348. }
  349. }
  350. }
  351. if (exe_flag) {
  352. ctx->concur_list[level_pos + concurrency] = i;
  353. nodes_unused[i] = 0;
  354. concurrency++;
  355. ctx->concur_list_len++;
  356. }
  357. }
  358. }
  359. n_left -= concurrency;
  360. // adding a barrier different layer
  361. ctx->concur_list[level_pos + concurrency] = -1;
  362. ctx->concur_list_len++;
  363. // jump all sorted nodes at nodes_bak
  364. while (!nodes_unused[n_start]) {n_start++;}
  365. level_pos += concurrency + 1;
  366. }
  367. if (ctx->concur_list_len > GGML_MAX_NODES) {
  368. fprintf(stderr, "%s: too many elements for metal ctx->concur_list!\n", __func__);
  369. }
  370. }
  371. void ggml_metal_graph_compute(
  372. struct ggml_metal_context * ctx,
  373. struct ggml_cgraph * gf) {
  374. metal_printf("%s: evaluating graph\n", __func__);
  375. // if there is ctx->concur_list, dispatch concurrently
  376. // else fallback to serial dispatch
  377. MTLComputePassDescriptor * edesc = MTLComputePassDescriptor.computePassDescriptor;
  378. const bool has_concur = ctx->concur_list_len && ctx->concur_list_len <= GGML_MAX_NODES;
  379. const int n_nodes = has_concur ? ctx->concur_list_len : gf->n_nodes;
  380. edesc.dispatchType = has_concur ? MTLDispatchTypeConcurrent : MTLDispatchTypeSerial;
  381. // create multiple command buffers and enqueue them
  382. // then, we encode the graph into the command buffers in parallel
  383. const int n_cb = ctx->n_cb;
  384. NSMutableArray * command_buffers = [NSMutableArray arrayWithCapacity:n_cb];
  385. for (int i = 0; i < n_cb; ++i) {
  386. command_buffers[i] = [ctx->queue commandBuffer];
  387. // enqueue the command buffers in order to specify their execution order
  388. [command_buffers[i] enqueue];
  389. }
  390. // TODO: is this the best way to start threads?
  391. dispatch_queue_t queue = dispatch_queue_create("llama.cpp", DISPATCH_QUEUE_CONCURRENT);
  392. for (int cb_idx = 0; cb_idx < n_cb; ++cb_idx) {
  393. const int n_nodes_per_cb = (n_nodes + n_cb - 1) / n_cb;
  394. dispatch_async(queue, ^{
  395. size_t offs_src0 = 0;
  396. size_t offs_src1 = 0;
  397. size_t offs_dst = 0;
  398. id<MTLCommandBuffer> command_buffer = command_buffers[cb_idx];
  399. id<MTLComputeCommandEncoder> encoder = nil;
  400. const int node_start = (cb_idx + 0) * n_nodes_per_cb;
  401. const int node_end = (cb_idx == n_cb - 1) ? n_nodes : (cb_idx + 1) * n_nodes_per_cb;
  402. for (int ind = node_start; ind < node_end; ++ind) {
  403. const int i = has_concur ? ctx->concur_list[ind] : ind;
  404. if (i == -1) {
  405. if (encoder == nil) {
  406. encoder = [command_buffer computeCommandEncoderWithDescriptor: edesc];
  407. continue;
  408. }
  409. [encoder memoryBarrierWithScope:MTLBarrierScopeBuffers];
  410. continue;
  411. }
  412. metal_printf("%s: encoding node %3d, op = %8s\n", __func__, i, ggml_op_name(gf->nodes[i]->op));
  413. struct ggml_tensor * src0 = gf->nodes[i]->src[0];
  414. struct ggml_tensor * src1 = gf->nodes[i]->src[1];
  415. struct ggml_tensor * dst = gf->nodes[i];
  416. const int64_t ne00 = src0 ? src0->ne[0] : 0;
  417. const int64_t ne01 = src0 ? src0->ne[1] : 0;
  418. const int64_t ne02 = src0 ? src0->ne[2] : 0;
  419. const int64_t ne03 = src0 ? src0->ne[3] : 0;
  420. const uint64_t nb00 = src0 ? src0->nb[0] : 0;
  421. const uint64_t nb01 = src0 ? src0->nb[1] : 0;
  422. const uint64_t nb02 = src0 ? src0->nb[2] : 0;
  423. const uint64_t nb03 = src0 ? src0->nb[3] : 0;
  424. const int64_t ne10 = src1 ? src1->ne[0] : 0;
  425. const int64_t ne11 = src1 ? src1->ne[1] : 0;
  426. const int64_t ne12 = src1 ? src1->ne[2] : 0;
  427. const int64_t ne13 = src1 ? src1->ne[3] : 0; UNUSED(ne13);
  428. const uint64_t nb10 = src1 ? src1->nb[0] : 0;
  429. const uint64_t nb11 = src1 ? src1->nb[1] : 0;
  430. const uint64_t nb12 = src1 ? src1->nb[2] : 0;
  431. const uint64_t nb13 = src1 ? src1->nb[3] : 0; UNUSED(nb13);
  432. const int64_t ne0 = dst ? dst->ne[0] : 0;
  433. const int64_t ne1 = dst ? dst->ne[1] : 0;
  434. const int64_t ne2 = dst ? dst->ne[2] : 0;
  435. const int64_t ne3 = dst ? dst->ne[3] : 0;
  436. const uint64_t nb0 = dst ? dst->nb[0] : 0;
  437. const uint64_t nb1 = dst ? dst->nb[1] : 0;
  438. const uint64_t nb2 = dst ? dst->nb[2] : 0;
  439. const uint64_t nb3 = dst ? dst->nb[3] : 0;
  440. const enum ggml_type src0t = src0 ? src0->type : GGML_TYPE_COUNT;
  441. const enum ggml_type src1t = src1 ? src1->type : GGML_TYPE_COUNT;
  442. const enum ggml_type dstt = dst ? dst->type : GGML_TYPE_COUNT;
  443. id<MTLBuffer> id_src0 = src0 ? ggml_metal_get_buffer(ctx, src0, &offs_src0) : nil;
  444. id<MTLBuffer> id_src1 = src1 ? ggml_metal_get_buffer(ctx, src1, &offs_src1) : nil;
  445. id<MTLBuffer> id_dst = dst ? ggml_metal_get_buffer(ctx, dst, &offs_dst) : nil;
  446. //metal_printf("%s: op - %s\n", __func__, ggml_op_name(dst->op));
  447. //if (src0) {
  448. // metal_printf("%s: src0 - %4s [%5lld, %5lld, %5lld], %d, %s\n", __func__, ggml_type_name(src0t), ne00, ne01, ne02,
  449. // ggml_is_contiguous(src0), src0->name);
  450. //}
  451. //if (src1) {
  452. // metal_printf("%s: src1 - %4s [%5lld, %5lld, %5lld], %d, %s\n", __func__, ggml_type_name(src1t), ne10, ne11, ne12,
  453. // ggml_is_contiguous(src1), src1->name);
  454. //}
  455. //if (dst) {
  456. // metal_printf("%s: dst - %4s [%5lld, %5lld, %5lld], 1, %s\n", __func__, ggml_type_name(dstt), ne0, ne1, ne2,
  457. // dst->name);
  458. //}
  459. switch (dst->op) {
  460. case GGML_OP_NONE:
  461. case GGML_OP_RESHAPE:
  462. case GGML_OP_VIEW:
  463. case GGML_OP_TRANSPOSE:
  464. case GGML_OP_PERMUTE:
  465. {
  466. // noop
  467. } break;
  468. case GGML_OP_ADD:
  469. {
  470. if (encoder == nil) {
  471. encoder = [command_buffer computeCommandEncoderWithDescriptor: edesc];
  472. }
  473. if (ggml_nelements(src1) == ne10) {
  474. // src1 is a row
  475. [encoder setComputePipelineState:ctx->pipeline_add_row];
  476. } else {
  477. [encoder setComputePipelineState:ctx->pipeline_add];
  478. }
  479. [encoder setBuffer:id_src0 offset:offs_src0 atIndex:0];
  480. [encoder setBuffer:id_src1 offset:offs_src1 atIndex:1];
  481. [encoder setBuffer:id_dst offset:offs_dst atIndex:2];
  482. [encoder setBytes:&ne00 length:sizeof(ne00) atIndex:3];
  483. const int64_t n = ggml_nelements(dst);
  484. [encoder dispatchThreadgroups:MTLSizeMake(n, 1, 1) threadsPerThreadgroup:MTLSizeMake(1, 1, 1)];
  485. } break;
  486. case GGML_OP_MUL:
  487. {
  488. if (encoder == nil) {
  489. encoder = [command_buffer computeCommandEncoderWithDescriptor: edesc];
  490. }
  491. if (ggml_nelements(src1) == ne10) {
  492. // src1 is a row
  493. [encoder setComputePipelineState:ctx->pipeline_mul_row];
  494. } else {
  495. [encoder setComputePipelineState:ctx->pipeline_mul];
  496. }
  497. [encoder setBuffer:id_src0 offset:offs_src0 atIndex:0];
  498. [encoder setBuffer:id_src1 offset:offs_src1 atIndex:1];
  499. [encoder setBuffer:id_dst offset:offs_dst atIndex:2];
  500. [encoder setBytes:&ne00 length:sizeof(ne00) atIndex:3];
  501. const int64_t n = ggml_nelements(dst);
  502. [encoder dispatchThreadgroups:MTLSizeMake(n, 1, 1) threadsPerThreadgroup:MTLSizeMake(1, 1, 1)];
  503. } break;
  504. case GGML_OP_SCALE:
  505. {
  506. if (encoder == nil) {
  507. encoder = [command_buffer computeCommandEncoderWithDescriptor: edesc];
  508. }
  509. const float scale = *(const float *) src1->data;
  510. [encoder setComputePipelineState:ctx->pipeline_scale];
  511. [encoder setBuffer:id_src0 offset:offs_src0 atIndex:0];
  512. [encoder setBuffer:id_dst offset:offs_dst atIndex:1];
  513. [encoder setBytes:&scale length:sizeof(scale) atIndex:2];
  514. const int64_t n = ggml_nelements(dst);
  515. [encoder dispatchThreadgroups:MTLSizeMake(n, 1, 1) threadsPerThreadgroup:MTLSizeMake(1, 1, 1)];
  516. } break;
  517. case GGML_OP_UNARY:
  518. switch (ggml_get_unary_op(gf->nodes[i])) {
  519. case GGML_UNARY_OP_SILU:
  520. {
  521. if (encoder == nil) {
  522. encoder = [command_buffer computeCommandEncoderWithDescriptor: edesc];
  523. }
  524. [encoder setComputePipelineState:ctx->pipeline_silu];
  525. [encoder setBuffer:id_src0 offset:offs_src0 atIndex:0];
  526. [encoder setBuffer:id_dst offset:offs_dst atIndex:1];
  527. const int64_t n = ggml_nelements(dst);
  528. [encoder dispatchThreadgroups:MTLSizeMake(n, 1, 1) threadsPerThreadgroup:MTLSizeMake(1, 1, 1)];
  529. } break;
  530. case GGML_UNARY_OP_RELU:
  531. {
  532. if (encoder == nil) {
  533. encoder = [command_buffer computeCommandEncoderWithDescriptor: edesc];
  534. }
  535. [encoder setComputePipelineState:ctx->pipeline_relu];
  536. [encoder setBuffer:id_src0 offset:offs_src0 atIndex:0];
  537. [encoder setBuffer:id_dst offset:offs_dst atIndex:1];
  538. const int64_t n = ggml_nelements(dst);
  539. [encoder dispatchThreadgroups:MTLSizeMake(n, 1, 1) threadsPerThreadgroup:MTLSizeMake(1, 1, 1)];
  540. } break;
  541. case GGML_UNARY_OP_GELU:
  542. {
  543. if (encoder == nil) {
  544. encoder = [command_buffer computeCommandEncoderWithDescriptor: edesc];
  545. }
  546. [encoder setComputePipelineState:ctx->pipeline_gelu];
  547. [encoder setBuffer:id_src0 offset:offs_src0 atIndex:0];
  548. [encoder setBuffer:id_dst offset:offs_dst atIndex:1];
  549. const int64_t n = ggml_nelements(dst);
  550. [encoder dispatchThreadgroups:MTLSizeMake(n, 1, 1) threadsPerThreadgroup:MTLSizeMake(1, 1, 1)];
  551. } break;
  552. default:
  553. {
  554. fprintf(stderr, "%s: node %3d, op = %8s not implemented\n", __func__, i, ggml_op_name(dst->op));
  555. GGML_ASSERT(false);
  556. }
  557. } break;
  558. case GGML_OP_SOFT_MAX:
  559. {
  560. if (encoder == nil) {
  561. encoder = [command_buffer computeCommandEncoderWithDescriptor: edesc];
  562. }
  563. const int nth = 32;
  564. [encoder setComputePipelineState:ctx->pipeline_soft_max];
  565. [encoder setBuffer:id_src0 offset:offs_src0 atIndex:0];
  566. [encoder setBuffer:id_dst offset:offs_dst atIndex:1];
  567. [encoder setBytes:&ne00 length:sizeof(ne00) atIndex:2];
  568. [encoder setBytes:&ne01 length:sizeof(ne01) atIndex:3];
  569. [encoder setBytes:&ne02 length:sizeof(ne02) atIndex:4];
  570. [encoder setThreadgroupMemoryLength:nth*sizeof(float) atIndex:0];
  571. [encoder dispatchThreadgroups:MTLSizeMake(ne01, ne02, ne03) threadsPerThreadgroup:MTLSizeMake(nth, 1, 1)];
  572. } break;
  573. case GGML_OP_DIAG_MASK_INF:
  574. {
  575. if (encoder == nil) {
  576. encoder = [command_buffer computeCommandEncoderWithDescriptor: edesc];
  577. }
  578. const int n_past = ((int32_t *)(dst->op_params))[0];
  579. [encoder setComputePipelineState:ctx->pipeline_diag_mask_inf];
  580. [encoder setBuffer:id_src0 offset:offs_src0 atIndex:0];
  581. [encoder setBuffer:id_dst offset:offs_dst atIndex:1];
  582. [encoder setBytes:&ne00 length:sizeof(ne00) atIndex:2];
  583. [encoder setBytes:&ne01 length:sizeof(ne01) atIndex:3];
  584. [encoder setBytes:&n_past length:sizeof(int) atIndex:4];
  585. [encoder dispatchThreadgroups:MTLSizeMake(ne00, ne01, ne02) threadsPerThreadgroup:MTLSizeMake(1, 1, 1)];
  586. } break;
  587. case GGML_OP_MUL_MAT:
  588. {
  589. // TODO: needs to be updated after PR: https://github.com/ggerganov/ggml/pull/224
  590. GGML_ASSERT(ne00 == ne10);
  591. // GGML_ASSERT(ne02 == ne12); // Should be checked on individual data types until broadcast is implemented everywhere
  592. GGML_ASSERT(ne03 == ne13);
  593. if (ggml_is_contiguous(src0) &&
  594. ggml_is_contiguous(src1) &&
  595. (src0t == GGML_TYPE_F32 || src0t == GGML_TYPE_F16) && ne11 > 1) {
  596. if (encoder != nil) {
  597. [encoder endEncoding];
  598. encoder = nil;
  599. }
  600. MPSDataType src0dt = src0t == GGML_TYPE_F32 ? MPSDataTypeFloat32 : MPSDataTypeFloat16;
  601. MPSDataType src1dt = src1t == GGML_TYPE_F32 ? MPSDataTypeFloat32 : MPSDataTypeFloat16;
  602. // for F32 x F32 we use MPS
  603. MPSMatrixDescriptor * desc0 = [MPSMatrixDescriptor
  604. matrixDescriptorWithRows:ne01 columns:ne00 rowBytes:src0->nb[1] dataType:src0dt];
  605. MPSMatrixDescriptor * desc1 = [MPSMatrixDescriptor
  606. matrixDescriptorWithRows:ne11 columns:ne10 rowBytes:src1->nb[1] dataType:src1dt];
  607. MPSMatrixDescriptor * desc = [MPSMatrixDescriptor
  608. matrixDescriptorWithRows:ne1 columns:ne0 rowBytes:dst->nb[1] dataType:MPSDataTypeFloat32];
  609. MPSMatrixMultiplication * mul = [[MPSMatrixMultiplication alloc]
  610. initWithDevice:ctx->device transposeLeft:false transposeRight:true
  611. resultRows:ne11 resultColumns:ne01 interiorColumns:ne00 alpha:1.0 beta:0.0];
  612. // we need to do ne12 multiplications
  613. // TODO: is there a way to do this in parallel - currently very slow ..
  614. // TODO: might be possible to offload part of the computation to ANE using Accelerate's CBLAS
  615. for (int64_t i02 = 0; i02 < ne12; ++i02) {
  616. size_t offs_src0_cur = offs_src0 + i02/(ne12/ne02)*nb02; // gqa not used for now
  617. size_t offs_src1_cur = offs_src1 + i02*nb12;
  618. size_t offs_dst_cur = offs_dst + i02*nb2;
  619. MPSMatrix * mat_src0 = [[MPSMatrix alloc] initWithBuffer:id_src0 offset:offs_src0_cur descriptor:desc0];
  620. MPSMatrix * mat_src1 = [[MPSMatrix alloc] initWithBuffer:id_src1 offset:offs_src1_cur descriptor:desc1];
  621. MPSMatrix * mat_dst = [[MPSMatrix alloc] initWithBuffer:id_dst offset:offs_dst_cur descriptor:desc ];
  622. [mul encodeToCommandBuffer:command_buffer leftMatrix:mat_src1 rightMatrix:mat_src0 resultMatrix:mat_dst];
  623. }
  624. } else {
  625. if (encoder == nil) {
  626. encoder = [command_buffer computeCommandEncoderWithDescriptor: edesc];
  627. }
  628. int nth0 = 32;
  629. int nth1 = 1;
  630. // use custom matrix x vector kernel
  631. switch (src0t) {
  632. case GGML_TYPE_F16:
  633. {
  634. nth0 = 64;
  635. nth1 = 1;
  636. [encoder setComputePipelineState:ctx->pipeline_mul_mat_f16_f32];
  637. } break;
  638. case GGML_TYPE_Q4_0:
  639. {
  640. GGML_ASSERT(ne02 == 1);
  641. GGML_ASSERT(ne12 == 1);
  642. nth0 = 8;
  643. nth1 = 8;
  644. [encoder setComputePipelineState:ctx->pipeline_mul_mat_q4_0_f32];
  645. } break;
  646. case GGML_TYPE_Q4_1:
  647. {
  648. GGML_ASSERT(ne02 == 1);
  649. GGML_ASSERT(ne12 == 1);
  650. nth0 = 8;
  651. nth1 = 8;
  652. [encoder setComputePipelineState:ctx->pipeline_mul_mat_q4_1_f32];
  653. } break;
  654. case GGML_TYPE_Q2_K:
  655. {
  656. GGML_ASSERT(ne02 == 1);
  657. GGML_ASSERT(ne12 == 1);
  658. nth0 = 2;
  659. nth1 = 32;
  660. [encoder setComputePipelineState:ctx->pipeline_mul_mat_q2_K_f32];
  661. } break;
  662. case GGML_TYPE_Q3_K:
  663. {
  664. GGML_ASSERT(ne02 == 1);
  665. GGML_ASSERT(ne12 == 1);
  666. nth0 = 2;
  667. nth1 = 32;
  668. [encoder setComputePipelineState:ctx->pipeline_mul_mat_q3_K_f32];
  669. } break;
  670. case GGML_TYPE_Q4_K:
  671. {
  672. GGML_ASSERT(ne02 == 1);
  673. GGML_ASSERT(ne12 == 1);
  674. nth0 = 2;
  675. nth1 = 32;
  676. [encoder setComputePipelineState:ctx->pipeline_mul_mat_q4_K_f32];
  677. } break;
  678. case GGML_TYPE_Q5_K:
  679. {
  680. GGML_ASSERT(ne02 == 1);
  681. GGML_ASSERT(ne12 == 1);
  682. nth0 = 2;
  683. nth1 = 32;
  684. [encoder setComputePipelineState:ctx->pipeline_mul_mat_q5_K_f32];
  685. } break;
  686. case GGML_TYPE_Q6_K:
  687. {
  688. GGML_ASSERT(ne02 == 1);
  689. GGML_ASSERT(ne12 == 1);
  690. nth0 = 2;
  691. nth1 = 32;
  692. [encoder setComputePipelineState:ctx->pipeline_mul_mat_q6_K_f32];
  693. } break;
  694. default:
  695. {
  696. fprintf(stderr, "Asserting on type %d\n",(int)src0t);
  697. GGML_ASSERT(false && "not implemented");
  698. }
  699. };
  700. [encoder setBuffer:id_src0 offset:offs_src0 atIndex:0];
  701. [encoder setBuffer:id_src1 offset:offs_src1 atIndex:1];
  702. [encoder setBuffer:id_dst offset:offs_dst atIndex:2];
  703. [encoder setBytes:&ne00 length:sizeof(ne00) atIndex:3];
  704. [encoder setBytes:&ne01 length:sizeof(ne01) atIndex:4];
  705. [encoder setBytes:&ne02 length:sizeof(ne02) atIndex:5];
  706. [encoder setBytes:&nb00 length:sizeof(nb00) atIndex:6];
  707. [encoder setBytes:&nb01 length:sizeof(nb01) atIndex:7];
  708. [encoder setBytes:&nb02 length:sizeof(nb02) atIndex:8];
  709. [encoder setBytes:&ne10 length:sizeof(ne10) atIndex:9];
  710. [encoder setBytes:&ne11 length:sizeof(ne11) atIndex:10];
  711. [encoder setBytes:&ne12 length:sizeof(ne12) atIndex:11];
  712. [encoder setBytes:&nb10 length:sizeof(nb10) atIndex:12];
  713. [encoder setBytes:&nb11 length:sizeof(nb11) atIndex:13];
  714. [encoder setBytes:&nb12 length:sizeof(nb12) atIndex:14];
  715. [encoder setBytes:&ne0 length:sizeof(ne0) atIndex:15];
  716. [encoder setBytes:&ne1 length:sizeof(ne1) atIndex:16];
  717. if (src0t == GGML_TYPE_Q4_0 || src0t == GGML_TYPE_Q4_1 ||
  718. src0t == GGML_TYPE_Q2_K || src0t == GGML_TYPE_Q4_K) {
  719. [encoder dispatchThreadgroups:MTLSizeMake((ne01 + 7) / 8, ne11, 1) threadsPerThreadgroup:MTLSizeMake(nth0, nth1, 1)];
  720. }
  721. else if (src0t == GGML_TYPE_Q3_K) {
  722. #ifdef GGML_QKK_64
  723. [encoder dispatchThreadgroups:MTLSizeMake((ne01+1)/2, ne11, 1) threadsPerThreadgroup:MTLSizeMake(nth0, nth1, 1)];
  724. #else
  725. [encoder dispatchThreadgroups:MTLSizeMake((ne01+3)/4, ne11, 1) threadsPerThreadgroup:MTLSizeMake(nth0, nth1, 1)];
  726. #endif
  727. }
  728. else if (src0t == GGML_TYPE_Q5_K) {
  729. [encoder dispatchThreadgroups:MTLSizeMake((ne01 + 3) / 4, ne11, 1) threadsPerThreadgroup:MTLSizeMake(nth0, nth1, 1)];
  730. }
  731. else if (src0t == GGML_TYPE_Q6_K) {
  732. [encoder dispatchThreadgroups:MTLSizeMake((ne01+1)/2, ne11, 1) threadsPerThreadgroup:MTLSizeMake(nth0, nth1, 1)];
  733. } else {
  734. [encoder setThreadgroupMemoryLength:nth0*sizeof(float) atIndex:0];
  735. [encoder dispatchThreadgroups:MTLSizeMake(ne01, ne11, ne12) threadsPerThreadgroup:MTLSizeMake(nth0, nth1, 1)];
  736. }
  737. }
  738. } break;
  739. case GGML_OP_GET_ROWS:
  740. {
  741. if (encoder == nil) {
  742. encoder = [command_buffer computeCommandEncoderWithDescriptor: edesc];
  743. }
  744. switch (src0->type) {
  745. case GGML_TYPE_F16: [encoder setComputePipelineState:ctx->pipeline_get_rows_f16]; break;
  746. case GGML_TYPE_Q4_0: [encoder setComputePipelineState:ctx->pipeline_get_rows_q4_0]; break;
  747. case GGML_TYPE_Q4_1: [encoder setComputePipelineState:ctx->pipeline_get_rows_q4_1]; break;
  748. case GGML_TYPE_Q2_K: [encoder setComputePipelineState:ctx->pipeline_get_rows_q2_K]; break;
  749. case GGML_TYPE_Q3_K: [encoder setComputePipelineState:ctx->pipeline_get_rows_q3_K]; break;
  750. case GGML_TYPE_Q4_K: [encoder setComputePipelineState:ctx->pipeline_get_rows_q4_K]; break;
  751. case GGML_TYPE_Q5_K: [encoder setComputePipelineState:ctx->pipeline_get_rows_q5_K]; break;
  752. case GGML_TYPE_Q6_K: [encoder setComputePipelineState:ctx->pipeline_get_rows_q6_K]; break;
  753. default: GGML_ASSERT(false && "not implemented");
  754. }
  755. [encoder setBuffer:id_src0 offset:offs_src0 atIndex:0];
  756. [encoder setBuffer:id_src1 offset:offs_src1 atIndex:1];
  757. [encoder setBuffer:id_dst offset:offs_dst atIndex:2];
  758. [encoder setBytes:&(src0->ne[0]) length:sizeof( int64_t) atIndex:3];
  759. [encoder setBytes:&(src0->nb[1]) length:sizeof(uint64_t) atIndex:4];
  760. [encoder setBytes:&(dst->nb[1]) length:sizeof(uint64_t) atIndex:5];
  761. const int64_t n = ggml_nelements(src1);
  762. [encoder dispatchThreadgroups:MTLSizeMake(n, 1, 1) threadsPerThreadgroup:MTLSizeMake(1, 1, 1)];
  763. } break;
  764. case GGML_OP_RMS_NORM:
  765. {
  766. if (encoder == nil) {
  767. encoder = [command_buffer computeCommandEncoderWithDescriptor: edesc];
  768. }
  769. float eps;
  770. memcpy(&eps, dst->op_params, sizeof(float));
  771. const int nth = 512;
  772. [encoder setComputePipelineState:ctx->pipeline_rms_norm];
  773. [encoder setBuffer:id_src0 offset:offs_src0 atIndex:0];
  774. [encoder setBuffer:id_dst offset:offs_dst atIndex:1];
  775. [encoder setBytes:&ne00 length:sizeof( int64_t) atIndex:2];
  776. [encoder setBytes:&nb01 length:sizeof(uint64_t) atIndex:3];
  777. [encoder setBytes:&eps length:sizeof( float) atIndex:4];
  778. [encoder setThreadgroupMemoryLength:nth/32*sizeof(float) atIndex:0];
  779. const int64_t nrows = ggml_nrows(src0);
  780. [encoder dispatchThreadgroups:MTLSizeMake(nrows, 1, 1) threadsPerThreadgroup:MTLSizeMake(nth, 1, 1)];
  781. } break;
  782. case GGML_OP_NORM:
  783. {
  784. if (encoder == nil) {
  785. encoder = [command_buffer computeCommandEncoderWithDescriptor: edesc];
  786. }
  787. const float eps = 1e-5f;
  788. const int nth = 256;
  789. [encoder setComputePipelineState:ctx->pipeline_norm];
  790. [encoder setBuffer:id_src0 offset:offs_src0 atIndex:0];
  791. [encoder setBuffer:id_dst offset:offs_dst atIndex:1];
  792. [encoder setBytes:&ne00 length:sizeof( int64_t) atIndex:2];
  793. [encoder setBytes:&nb01 length:sizeof(uint64_t) atIndex:3];
  794. [encoder setBytes:&eps length:sizeof( float) atIndex:4];
  795. [encoder setThreadgroupMemoryLength:nth*sizeof(float) atIndex:0];
  796. const int64_t nrows = ggml_nrows(src0);
  797. [encoder dispatchThreadgroups:MTLSizeMake(nrows, 1, 1) threadsPerThreadgroup:MTLSizeMake(nth, 1, 1)];
  798. } break;
  799. case GGML_OP_ALIBI:
  800. {
  801. if (encoder == nil) {
  802. encoder = [command_buffer computeCommandEncoderWithDescriptor: edesc];
  803. }
  804. GGML_ASSERT((src0t == GGML_TYPE_F32));
  805. const int n_past = ((int32_t *) dst->op_params)[0]; UNUSED(n_past);
  806. const int n_head = ((int32_t *) dst->op_params)[1];
  807. float max_bias;
  808. memcpy(&max_bias, (int32_t *) dst->op_params + 2, sizeof(float));
  809. if (__builtin_popcount(n_head) != 1) {
  810. GGML_ASSERT(false && "only power-of-two n_head implemented");
  811. }
  812. const int n_heads_log2_floor = 1 << (int) floor(log2(n_head));
  813. const float m0 = powf(2.0f, -(max_bias) / n_heads_log2_floor);
  814. [encoder setComputePipelineState:ctx->pipeline_alibi_f32];
  815. [encoder setBuffer:id_src0 offset:offs_src0 atIndex:0];
  816. [encoder setBuffer:id_dst offset:offs_dst atIndex:1];
  817. [encoder setBytes:&ne00 length:sizeof( int64_t) atIndex:2];
  818. [encoder setBytes:&ne01 length:sizeof( int64_t) atIndex:3];
  819. [encoder setBytes:&ne02 length:sizeof( int64_t) atIndex:4];
  820. [encoder setBytes:&ne03 length:sizeof( int64_t) atIndex:5];
  821. [encoder setBytes:&nb00 length:sizeof(uint64_t) atIndex:6];
  822. [encoder setBytes:&nb01 length:sizeof(uint64_t) atIndex:7];
  823. [encoder setBytes:&nb02 length:sizeof(uint64_t) atIndex:8];
  824. [encoder setBytes:&nb03 length:sizeof(uint64_t) atIndex:9];
  825. [encoder setBytes:&ne0 length:sizeof( int64_t) atIndex:10];
  826. [encoder setBytes:&ne1 length:sizeof( int64_t) atIndex:11];
  827. [encoder setBytes:&ne2 length:sizeof( int64_t) atIndex:12];
  828. [encoder setBytes:&ne3 length:sizeof( int64_t) atIndex:13];
  829. [encoder setBytes:&nb0 length:sizeof(uint64_t) atIndex:14];
  830. [encoder setBytes:&nb1 length:sizeof(uint64_t) atIndex:15];
  831. [encoder setBytes:&nb2 length:sizeof(uint64_t) atIndex:16];
  832. [encoder setBytes:&nb3 length:sizeof(uint64_t) atIndex:17];
  833. [encoder setBytes:&m0 length:sizeof( float) atIndex:18];
  834. const int nth = 32;
  835. [encoder dispatchThreadgroups:MTLSizeMake(ne01, ne02, ne03) threadsPerThreadgroup:MTLSizeMake(nth, 1, 1)];
  836. } break;
  837. case GGML_OP_ROPE:
  838. {
  839. if (encoder == nil) {
  840. encoder = [command_buffer computeCommandEncoderWithDescriptor: edesc];
  841. }
  842. const int n_past = ((int32_t *) dst->op_params)[0];
  843. const int n_dims = ((int32_t *) dst->op_params)[1];
  844. const int mode = ((int32_t *) dst->op_params)[2];
  845. float freq_base;
  846. float freq_scale;
  847. memcpy(&freq_base, (int32_t *) dst->op_params + 4, sizeof(float));
  848. memcpy(&freq_scale, (int32_t *) dst->op_params + 5, sizeof(float));
  849. [encoder setComputePipelineState:ctx->pipeline_rope];
  850. [encoder setBuffer:id_src0 offset:offs_src0 atIndex:0];
  851. [encoder setBuffer:id_dst offset:offs_dst atIndex:1];
  852. [encoder setBytes:&ne00 length:sizeof( int64_t) atIndex:2];
  853. [encoder setBytes:&ne01 length:sizeof( int64_t) atIndex:3];
  854. [encoder setBytes:&ne02 length:sizeof( int64_t) atIndex:4];
  855. [encoder setBytes:&ne03 length:sizeof( int64_t) atIndex:5];
  856. [encoder setBytes:&nb00 length:sizeof(uint64_t) atIndex:6];
  857. [encoder setBytes:&nb01 length:sizeof(uint64_t) atIndex:7];
  858. [encoder setBytes:&nb02 length:sizeof(uint64_t) atIndex:8];
  859. [encoder setBytes:&nb03 length:sizeof(uint64_t) atIndex:9];
  860. [encoder setBytes:&ne0 length:sizeof( int64_t) atIndex:10];
  861. [encoder setBytes:&ne1 length:sizeof( int64_t) atIndex:11];
  862. [encoder setBytes:&ne2 length:sizeof( int64_t) atIndex:12];
  863. [encoder setBytes:&ne3 length:sizeof( int64_t) atIndex:13];
  864. [encoder setBytes:&nb0 length:sizeof(uint64_t) atIndex:14];
  865. [encoder setBytes:&nb1 length:sizeof(uint64_t) atIndex:15];
  866. [encoder setBytes:&nb2 length:sizeof(uint64_t) atIndex:16];
  867. [encoder setBytes:&nb3 length:sizeof(uint64_t) atIndex:17];
  868. [encoder setBytes:&n_past length:sizeof( int) atIndex:18];
  869. [encoder setBytes:&n_dims length:sizeof( int) atIndex:19];
  870. [encoder setBytes:&mode length:sizeof( int) atIndex:20];
  871. [encoder setBytes:&freq_base length:sizeof(float) atIndex:21];
  872. [encoder setBytes:&freq_scale length:sizeof(float) atIndex:22];
  873. [encoder dispatchThreadgroups:MTLSizeMake(ne01, ne02, ne03) threadsPerThreadgroup:MTLSizeMake(1, 1, 1)];
  874. } break;
  875. case GGML_OP_DUP:
  876. case GGML_OP_CPY:
  877. case GGML_OP_CONT:
  878. {
  879. if (encoder == nil) {
  880. encoder = [command_buffer computeCommandEncoderWithDescriptor: edesc];
  881. }
  882. const int nth = 32;
  883. switch (src0t) {
  884. case GGML_TYPE_F32:
  885. {
  886. switch (dstt) {
  887. case GGML_TYPE_F16: [encoder setComputePipelineState:ctx->pipeline_cpy_f32_f16]; break;
  888. case GGML_TYPE_F32: [encoder setComputePipelineState:ctx->pipeline_cpy_f32_f32]; break;
  889. default: GGML_ASSERT(false && "not implemented");
  890. };
  891. } break;
  892. case GGML_TYPE_F16:
  893. {
  894. switch (dstt) {
  895. case GGML_TYPE_F16: [encoder setComputePipelineState:ctx->pipeline_cpy_f16_f16]; break;
  896. case GGML_TYPE_F32: GGML_ASSERT(false && "cpy_f16_f32 not implemented"); break;
  897. default: GGML_ASSERT(false && "not implemented");
  898. };
  899. } break;
  900. default: GGML_ASSERT(false && "not implemented");
  901. }
  902. [encoder setBuffer:id_src0 offset:offs_src0 atIndex:0];
  903. [encoder setBuffer:id_dst offset:offs_dst atIndex:1];
  904. [encoder setBytes:&ne00 length:sizeof( int64_t) atIndex:2];
  905. [encoder setBytes:&ne01 length:sizeof( int64_t) atIndex:3];
  906. [encoder setBytes:&ne02 length:sizeof( int64_t) atIndex:4];
  907. [encoder setBytes:&ne03 length:sizeof( int64_t) atIndex:5];
  908. [encoder setBytes:&nb00 length:sizeof(uint64_t) atIndex:6];
  909. [encoder setBytes:&nb01 length:sizeof(uint64_t) atIndex:7];
  910. [encoder setBytes:&nb02 length:sizeof(uint64_t) atIndex:8];
  911. [encoder setBytes:&nb03 length:sizeof(uint64_t) atIndex:9];
  912. [encoder setBytes:&ne0 length:sizeof( int64_t) atIndex:10];
  913. [encoder setBytes:&ne1 length:sizeof( int64_t) atIndex:11];
  914. [encoder setBytes:&ne2 length:sizeof( int64_t) atIndex:12];
  915. [encoder setBytes:&ne3 length:sizeof( int64_t) atIndex:13];
  916. [encoder setBytes:&nb0 length:sizeof(uint64_t) atIndex:14];
  917. [encoder setBytes:&nb1 length:sizeof(uint64_t) atIndex:15];
  918. [encoder setBytes:&nb2 length:sizeof(uint64_t) atIndex:16];
  919. [encoder setBytes:&nb3 length:sizeof(uint64_t) atIndex:17];
  920. [encoder dispatchThreadgroups:MTLSizeMake(ne01, ne02, ne03) threadsPerThreadgroup:MTLSizeMake(nth, 1, 1)];
  921. } break;
  922. default:
  923. {
  924. fprintf(stderr, "%s: node %3d, op = %8s not implemented\n", __func__, i, ggml_op_name(dst->op));
  925. GGML_ASSERT(false);
  926. }
  927. }
  928. }
  929. if (encoder != nil) {
  930. [encoder endEncoding];
  931. encoder = nil;
  932. }
  933. [command_buffer commit];
  934. });
  935. }
  936. // wait for all threads to finish
  937. dispatch_barrier_sync(queue, ^{});
  938. [command_buffers[n_cb - 1] waitUntilCompleted];
  939. // check status of command buffers
  940. // needed to detect if the device ran out-of-memory for example (#1881)
  941. for (int i = 0; i < n_cb; i++) {
  942. MTLCommandBufferStatus status = (MTLCommandBufferStatus) [command_buffers[i] status];
  943. if (status != MTLCommandBufferStatusCompleted) {
  944. fprintf(stderr, "%s: command buffer %d failed with status %lu\n", __func__, i, status);
  945. GGML_ASSERT(false);
  946. }
  947. }
  948. }