ggml-metal.m 49 KB

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