metal : avoid call free for non-owned buffer (#16067)

This commit is contained in:
Jhen-Jie Hong
2025-09-18 15:06:48 +08:00
committed by GitHub
parent f2f28380ea
commit e00f3fd8ff

View File

@@ -824,6 +824,7 @@ struct ggml_metal_buffer {
// if false, the Metal buffer data is allocated in private GPU memory and is not shared with the host // if false, the Metal buffer data is allocated in private GPU memory and is not shared with the host
bool is_shared; bool is_shared;
bool owned;
// multiple buffers are used only to avoid the maximum buffer size limitation when using mmap // multiple buffers are used only to avoid the maximum buffer size limitation when using mmap
int n_buffers; int n_buffers;
@@ -956,6 +957,7 @@ ggml_metal_buffer_t ggml_metal_buffer_init(ggml_metal_device_t dev, size_t size,
if (shared) { if (shared) {
res->all_data = ggml_metal_host_malloc(size_aligned); res->all_data = ggml_metal_host_malloc(size_aligned);
res->is_shared = true; res->is_shared = true;
res->owned = true;
} else { } else {
// dummy, non-NULL value - we'll populate this after creating the Metal buffer below // dummy, non-NULL value - we'll populate this after creating the Metal buffer below
res->all_data = (void *) 0x000000400ULL; res->all_data = (void *) 0x000000400ULL;
@@ -1014,6 +1016,7 @@ ggml_metal_buffer_t ggml_metal_buffer_map(ggml_metal_device_t dev, void * ptr, s
res->all_size = size; res->all_size = size;
res->is_shared = true; res->is_shared = true;
res->owned = false;
res->n_buffers = 0; res->n_buffers = 0;
@@ -1107,7 +1110,7 @@ void ggml_metal_buffer_free(ggml_metal_buffer_t buf) {
ggml_metal_buffer_rset_free(buf); ggml_metal_buffer_rset_free(buf);
if (buf->is_shared) { if (buf->is_shared && buf->owned) {
#if TARGET_OS_OSX #if TARGET_OS_OSX
vm_deallocate((vm_map_t)mach_task_self(), (vm_address_t)buf->all_data, buf->all_size); vm_deallocate((vm_map_t)mach_task_self(), (vm_address_t)buf->all_data, buf->all_size);
#else #else