2020// max number of MTLCommandBuffer used to submit a graph for processing
2121#define GGML_METAL_MAX_COMMAND_BUFFERS 8
2222
23+ // the fewest extra command buffers: short ones keep the GPU fed while the rest of the graph is encoded
24+ #define GGML_METAL_N_CB_STREAM 8
25+
2326struct ggml_metal_command_buffer {
2427 id <MTLCommandBuffer > obj;
2528};
5255 id <MTLCaptureScope > capture_scope;
5356
5457 // command buffer state
55- int n_cb; // number of extra threads used to submit the command buffers
58+ int n_cb; // number of extra command buffers
59+ int n_par; // number of extra threads used to submit them
5660
5761 // first node of each extra command buffer, then the end of the graph
5862 int cb_start[GGML_METAL_MAX_COMMAND_BUFFERS + 1 ];
@@ -155,7 +159,8 @@ ggml_metal_t ggml_metal_init(ggml_metal_device_t dev) {
155159 res->finfo = ggml_metal_device_get_fusion_info (dev);
156160 if (ggml_metal_fusion_info_stats (res->finfo )) {
157161 ggml_metal_fusion_info_labels_init (res->finfo );
158- res->n_cb = 0 ;
162+ res->n_cb = 0 ;
163+ res->n_par = 0 ;
159164 }
160165
161166 GGML_LOG_INFO (" %s : use fusion = %s \n " , __func__, ggml_metal_fusion_info_enabled (res->finfo ) ? " true" : " false" );
@@ -482,10 +487,13 @@ enum ggml_status ggml_metal_graph_compute(ggml_metal_t ctx, struct ggml_cgraph *
482487 }
483488
484489 // number of nodes encoded by the main thread (empirically determined)
485- const int n_main = MAX (64 , 0.1 *gf->n_nodes );
490+ // number of command buffers in addition to the main one, and of threads that encode them
491+ const int n_cb = ctx->n_cb ;
492+ const int n_par = ctx->n_par ;
486493
487- // number of threads in addition to the main thread
488- const int n_cb = ctx->n_cb ;
494+ // when a thread encodes several command buffers in order, each one is committed as soon as it is encoded and the
495+ // GPU starts early on a short first one; otherwise the main thread covers the time the threads need
496+ const int n_main = n_par < n_cb ? MAX (64 , gf->n_nodes /25 ) : MAX (64 , 0.1 *gf->n_nodes );
489497
490498 // keep the memory wired
491499 ggml_metal_device_rsets_keep_alive (ctx->dev );
@@ -503,18 +511,14 @@ enum ggml_status ggml_metal_graph_compute(ggml_metal_t ctx, struct ggml_cgraph *
503511 // the main thread encodes [0, cb_start[0]) and the extra command buffer i encodes [cb_start[i], cb_start[i + 1])
504512 // a fusion cannot span two command buffers, so each boundary moves to the end of the fusable group it falls
505513 // into and the encoders fuse the same nodes as with a single command buffer
514+ // only the boundary of the main command buffer is needed before its encoding: the others are found while the
515+ // GPU runs it
516+ int i_split = 0 ;
506517 {
507- const int n_rest = gf->n_nodes - MIN (n_main, gf->n_nodes );
508- const int n_per_cb = n_cb > 0 ? (n_rest + n_cb - 1 ) / n_cb : 0 ;
509-
510- int i = 0 ;
511- for (int cb_idx = 0 ; cb_idx < n_cb; ++cb_idx) {
512- const int target = MIN (n_main + cb_idx*n_per_cb, gf->n_nodes );
513- while (i < target) {
514- i += ggml_metal_fusion_max (gf, i);
515- }
516- ctx->cb_start [cb_idx] = MIN (i, gf->n_nodes );
518+ while (i_split < MIN (n_main, gf->n_nodes )) {
519+ i_split += ggml_metal_fusion_max (gf, i_split);
517520 }
521+ ctx->cb_start [0 ] = n_cb > 0 ? MIN (i_split, gf->n_nodes ) : gf->n_nodes ;
518522 ctx->cb_start [n_cb] = gf->n_nodes ;
519523 }
520524
@@ -582,6 +586,20 @@ enum ggml_status ggml_metal_graph_compute(ggml_metal_t ctx, struct ggml_cgraph *
582586 // remember the command buffer for the next iteration
583587 ctx->cmd_buf_last = ctx->cmd_bufs [n_cb].obj ;
584588
589+ if (n_cb > 0 ) {
590+ const int n_rest = gf->n_nodes - ctx->cb_start [0 ];
591+ const int n_per_cb = (n_rest + n_cb - 1 ) / n_cb;
592+
593+ int i = ctx->cb_start [0 ];
594+ for (int cb_idx = 1 ; cb_idx < n_cb; ++cb_idx) {
595+ const int target = MIN (ctx->cb_start [0 ] + cb_idx*n_per_cb, gf->n_nodes );
596+ while (i < target) {
597+ i += ggml_metal_fusion_max (gf, i);
598+ }
599+ ctx->cb_start [cb_idx] = MIN (i, gf->n_nodes );
600+ }
601+ }
602+
585603 // prepare the rest of the command buffers asynchronously (optional)
586604 // cmd_buf[0.. n_cb)
587605 for (int cb_idx = 0 ; cb_idx < n_cb; ++cb_idx) {
@@ -604,7 +622,18 @@ enum ggml_status ggml_metal_graph_compute(ggml_metal_t ctx, struct ggml_cgraph *
604622 }
605623 }
606624
607- dispatch_apply (n_cb, ctx->d_queue , ctx->encode_async );
625+ if (n_par == n_cb) {
626+ dispatch_apply (n_cb, ctx->d_queue , ctx->encode_async );
627+ } else if (n_par > 0 ) {
628+ // each thread encodes a run of consecutive command buffers, in order
629+ void (^enc)(size_t ) = ctx->encode_async ;
630+ dispatch_apply (n_par, ctx->d_queue , ^(size_t t) {
631+ const int per = (n_cb + n_par - 1 )/n_par;
632+ for (int cb_idx = t*per; cb_idx < MIN ((int ) (t + 1 )*per, n_cb); ++cb_idx) {
633+ enc (cb_idx);
634+ }
635+ });
636+ }
608637
609638 // for debugging: block until graph is computed
610639 // [ctx->cmd_buf_last waitUntilCompleted];
@@ -676,6 +705,7 @@ enum ggml_status ggml_metal_graph_compute(ggml_metal_t ctx, struct ggml_cgraph *
676705 return res;
677706 }
678707 }
708+
679709 }
680710
681711 return GGML_STATUS_SUCCESS ;
@@ -734,10 +764,15 @@ void ggml_metal_set_n_cb(ggml_metal_t ctx, int n_cb) {
734764 n_cb = 0 ;
735765 }
736766
737- if (ctx->n_cb != n_cb) {
738- ctx->n_cb = MIN (n_cb, GGML_METAL_MAX_COMMAND_BUFFERS );
767+ // n_cb threads encode at least GGML_METAL_N_CB_STREAM command buffers, the ones of a thread in order
768+ const int n_par = MIN (MAX (n_cb, 0 ), GGML_METAL_MAX_COMMAND_BUFFERS );
769+ const int n_buf = n_par > 0 ? MIN (MAX (n_par, GGML_METAL_N_CB_STREAM ), GGML_METAL_MAX_COMMAND_BUFFERS ) : 0 ;
770+
771+ if (ctx->n_par != n_par || ctx->n_cb != n_buf) {
772+ ctx->n_par = n_par;
773+ ctx->n_cb = n_buf;
739774
740- if (ctx-> n_cb > 2 ) {
775+ if (n_par > 2 ) {
741776 GGML_LOG_WARN (" %s : n_cb = %d , using n_cb > 2 is not recommended and can degrade the performance in some cases\n " , __func__, n_cb);
742777 }
743778 }
0 commit comments