Skip to content

Commit 06b119a

Browse files
committed
fix: use vm_allocate to allocate CPU backend buffer on macOS
1 parent d4c19c0 commit 06b119a

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

ggml/src/ggml-backend.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
#ifdef __APPLE__
2525
#include <sys/types.h>
2626
#include <sys/sysctl.h>
27+
#include <mach/mach.h>
28+
#include <TargetConditionals.h>
2729
#endif
2830

2931

@@ -770,12 +772,21 @@ static const char * ggml_backend_cpu_buffer_type_get_name(ggml_backend_buffer_ty
770772
}
771773

772774
static ggml_backend_buffer_t ggml_backend_cpu_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size) {
775+
#ifdef TARGET_OS_OSX
776+
void * data = NULL;
777+
kern_return_t alloc_status = vm_allocate((vm_map_t) mach_task_self(), (vm_address_t *) &data, size, VM_FLAGS_ANYWHERE);
778+
if (alloc_status != KERN_SUCCESS) {
779+
GGML_LOG_ERROR("%s: failed to allocate buffer using vm_allocate with size %zu\n", __func__, size);
780+
return NULL;
781+
}
782+
#else
773783
size += TENSOR_ALIGNMENT; // malloc may return an address that is not aligned
774784
void * data = malloc(size); // TODO: use GGML_ALIGNED_MALLOC (move to ggml-impl.h)
775785
if (data == NULL) {
776786
GGML_LOG_ERROR("%s: failed to allocate buffer of size %zu\n", __func__, size);
777787
return NULL;
778788
}
789+
#endif
779790

780791
return ggml_backend_buffer_init(buft, ggml_backend_cpu_buffer_i, data, size);
781792
}

0 commit comments

Comments
 (0)