Gilad S commited on
Commit
eb910b1
·
1 Parent(s): 81ec961

metal : use `vm_allocate` instead of `posix_memalign` on macOS (llama/7078)

Browse files

* fix: use `malloc` instead of `posix_memalign` in `ggml-metal.m` to make it not crash Electron proccesses

* fix: typo

* fix: use `vm_allocate` instead of `posix_memalign`

* fix: don't call `newBufferWithBytesNoCopy` with `NULL` when `ggml_metal_host_malloc` returns `NULL`

* fix: use `vm_allocate` only on macOS

Files changed (1) hide show
  1. ggml-metal.m +22 -7
ggml-metal.m CHANGED
@@ -266,11 +266,20 @@ static void ggml_metal_log(enum ggml_log_level level, const char * format, ...){
266
 
267
  static void * ggml_metal_host_malloc(size_t n) {
268
  void * data = NULL;
 
 
 
 
 
 
 
 
269
  const int result = posix_memalign((void **) &data, sysconf(_SC_PAGESIZE), n);
270
  if (result != 0) {
271
  GGML_METAL_LOG_ERROR("%s: error: posix_memalign failed\n", __func__);
272
  return NULL;
273
  }
 
274
 
275
  return data;
276
  }
@@ -2855,7 +2864,11 @@ GGML_CALL static void ggml_backend_metal_buffer_free_buffer(ggml_backend_buffer_
2855
  ggml_backend_metal_free_device();
2856
 
2857
  if (ctx->owned) {
 
 
 
2858
  free(ctx->all_data);
 
2859
  }
2860
 
2861
  free(ctx);
@@ -2959,14 +2972,16 @@ GGML_CALL static ggml_backend_buffer_t ggml_backend_metal_buffer_type_alloc_buff
2959
  ctx->owned = true;
2960
  ctx->n_buffers = 1;
2961
 
2962
- ctx->buffers[0].data = ctx->all_data;
2963
- ctx->buffers[0].size = size;
2964
- ctx->buffers[0].metal = [device newBufferWithBytesNoCopy:ctx->all_data
2965
- length:size_aligned
2966
- options:MTLResourceStorageModeShared
2967
- deallocator:nil];
 
 
2968
 
2969
- if (ctx->buffers[0].metal == nil) {
2970
  GGML_METAL_LOG_ERROR("%s: error: failed to allocate buffer, size = %8.2f MiB\n", __func__, size_aligned / 1024.0 / 1024.0);
2971
  free(ctx);
2972
  ggml_backend_metal_free_device();
 
266
 
267
  static void * ggml_metal_host_malloc(size_t n) {
268
  void * data = NULL;
269
+
270
+ #if TARGET_OS_OSX
271
+ kern_return_t err = vm_allocate((vm_map_t) mach_task_self(), (void *) &data, n, VM_FLAGS_ANYWHERE);
272
+ if (err != KERN_SUCCESS) {
273
+ GGML_METAL_LOG_ERROR("%s: error: vm_allocate failed\n", __func__);
274
+ return NULL;
275
+ }
276
+ #else
277
  const int result = posix_memalign((void **) &data, sysconf(_SC_PAGESIZE), n);
278
  if (result != 0) {
279
  GGML_METAL_LOG_ERROR("%s: error: posix_memalign failed\n", __func__);
280
  return NULL;
281
  }
282
+ #endif
283
 
284
  return data;
285
  }
 
2864
  ggml_backend_metal_free_device();
2865
 
2866
  if (ctx->owned) {
2867
+ #if TARGET_OS_OSX
2868
+ vm_deallocate((vm_map_t)mach_task_self(), (vm_address_t)ctx->all_data, ctx->all_size);
2869
+ #else
2870
  free(ctx->all_data);
2871
+ #endif
2872
  }
2873
 
2874
  free(ctx);
 
2972
  ctx->owned = true;
2973
  ctx->n_buffers = 1;
2974
 
2975
+ if (ctx->all_data != NULL) {
2976
+ ctx->buffers[0].data = ctx->all_data;
2977
+ ctx->buffers[0].size = size;
2978
+ ctx->buffers[0].metal = [device newBufferWithBytesNoCopy:ctx->all_data
2979
+ length:size_aligned
2980
+ options:MTLResourceStorageModeShared
2981
+ deallocator:nil];
2982
+ }
2983
 
2984
+ if (ctx->all_data == NULL || ctx->buffers[0].metal == nil) {
2985
  GGML_METAL_LOG_ERROR("%s: error: failed to allocate buffer, size = %8.2f MiB\n", __func__, size_aligned / 1024.0 / 1024.0);
2986
  free(ctx);
2987
  ggml_backend_metal_free_device();