Skip to content

Commit 5f14ee0

Browse files
jmousseauggerganov
andcommitted
metal : add debug capture backend function (ggml/694)
Co-authored-by: Georgi Gerganov <[email protected]>
1 parent 8e14e3d commit 5f14ee0

File tree

2 files changed

+37
-6
lines changed

2 files changed

+37
-6
lines changed

ggml-metal.h

+3
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ GGML_API GGML_CALL ggml_backend_buffer_type_t ggml_backend_metal_buffer_type(voi
5757
// ref: https://developer.apple.com/metal/Metal-Feature-Set-Tables.pdf
5858
GGML_API bool ggml_backend_metal_supports_family(ggml_backend_t backend, int family);
5959

60+
// capture all command buffers committed the next time `ggml_backend_graph_compute` is called
61+
GGML_API void ggml_backend_metal_capture_next_compute(ggml_backend_t backend);
62+
6063
#ifdef __cplusplus
6164
}
6265
#endif

ggml-metal.m

+34-6
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,8 @@
168168

169169
bool support_simdgroup_reduction;
170170
bool support_simdgroup_mm;
171+
172+
bool should_capture_next_compute;
171173
};
172174

173175
// MSL code
@@ -687,6 +689,20 @@ static bool ggml_metal_graph_compute(
687689
const int n_cb = ctx->n_cb;
688690
const int n_nodes_per_cb = (n_nodes + n_cb - 1) / n_cb;
689691

692+
const bool should_capture = ctx->should_capture_next_compute;
693+
if (should_capture) {
694+
ctx->should_capture_next_compute = false;
695+
696+
MTLCaptureDescriptor * descriptor = [MTLCaptureDescriptor new];
697+
descriptor.captureObject = ctx->queue;
698+
699+
NSError * error = nil;
700+
if (![[MTLCaptureManager sharedCaptureManager] startCaptureWithDescriptor:descriptor error:&error]) {
701+
GGML_METAL_LOG_ERROR("%s: error: unable to start capture '%s'\n", __func__, [[error localizedDescription] UTF8String]);
702+
GGML_ASSERT(!"capture failed");
703+
}
704+
}
705+
690706
id<MTLCommandBuffer> command_buffer_builder[n_cb];
691707
for (int cb_idx = 0; cb_idx < n_cb; ++cb_idx) {
692708
id<MTLCommandBuffer> command_buffer = [ctx->queue commandBufferWithUnretainedReferences];
@@ -695,6 +711,7 @@ static bool ggml_metal_graph_compute(
695711
// enqueue the command buffers in order to specify their execution order
696712
[command_buffer enqueue];
697713
}
714+
698715
const id<MTLCommandBuffer> *command_buffers = command_buffer_builder;
699716

700717
dispatch_apply(n_cb, ctx->d_queue, ^(size_t iter) {
@@ -741,9 +758,9 @@ static bool ggml_metal_graph_compute(
741758
GGML_ASSERT(!"unsupported op");
742759
}
743760

744-
#ifndef GGML_METAL_NDEBUG
745-
[encoder pushDebugGroup:[NSString stringWithCString:ggml_op_desc(dst) encoding:NSUTF8StringEncoding]];
746-
#endif
761+
if (should_capture) {
762+
[encoder pushDebugGroup:[NSString stringWithCString:ggml_op_desc(dst) encoding:NSUTF8StringEncoding]];
763+
}
747764

748765
const int64_t ne00 = src0 ? src0->ne[0] : 0;
749766
const int64_t ne01 = src0 ? src0->ne[1] : 0;
@@ -2218,9 +2235,9 @@ static bool ggml_metal_graph_compute(
22182235
}
22192236
}
22202237

2221-
#ifndef GGML_METAL_NDEBUG
2222-
[encoder popDebugGroup];
2223-
#endif
2238+
if (should_capture) {
2239+
[encoder popDebugGroup];
2240+
}
22242241
}
22252242

22262243
[encoder endEncoding];
@@ -2242,6 +2259,10 @@ static bool ggml_metal_graph_compute(
22422259
}
22432260
}
22442261

2262+
if (should_capture) {
2263+
[[MTLCaptureManager sharedCaptureManager] stopCapture];
2264+
}
2265+
22452266
return true;
22462267
}
22472268

@@ -2613,6 +2634,13 @@ bool ggml_backend_metal_supports_family(ggml_backend_t backend, int family) {
26132634
return [ctx->device supportsFamily:(MTLGPUFamilyApple1 + family - 1)];
26142635
}
26152636

2637+
void ggml_backend_metal_capture_next_compute(ggml_backend_t backend) {
2638+
GGML_ASSERT(ggml_backend_is_metal(backend));
2639+
2640+
struct ggml_metal_context * ctx = (struct ggml_metal_context *)backend->context;
2641+
ctx->should_capture_next_compute = true;
2642+
}
2643+
26162644
GGML_CALL ggml_backend_t ggml_backend_reg_metal_init(const char * params, void * user_data); // silence warning
26172645

26182646
GGML_CALL ggml_backend_t ggml_backend_reg_metal_init(const char * params, void * user_data) {

0 commit comments

Comments
 (0)