Skip to content

Commit 53dea11

Browse files
committed
metal : fix encoders memory leak
1 parent 95c4af3 commit 53dea11

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

ggml-metal.m

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -515,13 +515,16 @@ void ggml_metal_graph_compute(
515515

516516
const int n_cb = ctx->n_cb;
517517

518-
NSMutableArray * command_buffers = [NSMutableArray arrayWithCapacity:n_cb];
518+
NSMutableArray * command_buffers = [NSMutableArray arrayWithCapacity:n_cb];
519+
NSMutableArray * command_encoders = [NSMutableArray arrayWithCapacity:n_cb];
519520

520521
for (int i = 0; i < n_cb; ++i) {
521522
command_buffers[i] = [ctx->queue commandBuffer];
522523

523524
// enqueue the command buffers in order to specify their execution order
524525
[command_buffers[i] enqueue];
526+
527+
command_encoders[i] = [command_buffers[i] computeCommandEncoderWithDescriptor: edesc];
525528
}
526529

527530
// TODO: is this the best way to start threads?
@@ -535,9 +538,8 @@ void ggml_metal_graph_compute(
535538
size_t offs_src1 = 0;
536539
size_t offs_dst = 0;
537540

538-
id<MTLCommandBuffer> command_buffer = command_buffers[cb_idx];
539-
540-
id<MTLComputeCommandEncoder> encoder = [command_buffer computeCommandEncoderWithDescriptor: edesc];
541+
id<MTLCommandBuffer> command_buffer = command_buffers[cb_idx];
542+
id<MTLComputeCommandEncoder> encoder = command_encoders[cb_idx];
541543

542544
const int node_start = (cb_idx + 0) * n_nodes_per_cb;
543545
const int node_end = MIN((cb_idx == n_cb - 1) ? n_nodes : (cb_idx + 1) * n_nodes_per_cb, n_nodes);
@@ -1116,8 +1118,10 @@ void ggml_metal_graph_compute(
11161118
GGML_ASSERT(false);
11171119
}
11181120

1121+
[command_encoders[i] release];
11191122
[command_buffers[i] release];
11201123
}
11211124

1125+
[command_encoders release];
11221126
[command_buffers release];
11231127
}

0 commit comments

Comments
 (0)