Skip to content

Commit a824f03

Browse files
committed
metal : avoid call free for non-owned buffer
1 parent 62c3b64 commit a824f03

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

ggml/src/ggml-metal/ggml-metal-device.m

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -817,6 +817,7 @@ bool ggml_metal_device_supports_op(ggml_metal_device_t dev, const struct ggml_te
817817

818818
// if false, the Metal buffer data is allocated in private GPU memory and is not shared with the host
819819
bool is_shared;
820+
bool owned;
820821

821822
// multiple buffers are used only to avoid the maximum buffer size limitation when using mmap
822823
int n_buffers;
@@ -949,6 +950,7 @@ ggml_metal_buffer_t ggml_metal_buffer_init(ggml_metal_device_t dev, size_t size,
949950
if (shared) {
950951
res->all_data = ggml_metal_host_malloc(size_aligned);
951952
res->is_shared = true;
953+
res->owned = true;
952954
} else {
953955
// dummy, non-NULL value - we'll populate this after creating the Metal buffer below
954956
res->all_data = (void *) 0x000000400ULL;
@@ -1007,6 +1009,7 @@ ggml_metal_buffer_t ggml_metal_buffer_map(ggml_metal_device_t dev, void * ptr, s
10071009
res->all_size = size;
10081010

10091011
res->is_shared = true;
1012+
res->owned = false;
10101013

10111014
res->n_buffers = 0;
10121015

@@ -1100,7 +1103,7 @@ void ggml_metal_buffer_free(ggml_metal_buffer_t buf) {
11001103

11011104
ggml_metal_buffer_rset_free(buf);
11021105

1103-
if (buf->is_shared) {
1106+
if (buf->is_shared && buf->owned) {
11041107
#if TARGET_OS_OSX
11051108
vm_deallocate((vm_map_t)mach_task_self(), (vm_address_t)buf->all_data, buf->all_size);
11061109
#else

0 commit comments

Comments
 (0)