ggerganov HF Staff commited on
Commit
868283e
·
1 Parent(s): e893c97

metal : handle zero-sized allocs (llama/9466)

Browse files
Files changed (1) hide show
  1. ggml/src/ggml-metal.m +29 -19
ggml/src/ggml-metal.m CHANGED
@@ -3226,15 +3226,19 @@ GGML_CALL static ggml_backend_buffer_t ggml_backend_metal_buffer_type_alloc_buff
3226
  ctx->n_buffers = 1;
3227
 
3228
  if (ctx->all_data != NULL) {
3229
- ctx->buffers[0].data = ctx->all_data;
3230
- ctx->buffers[0].size = size;
3231
- ctx->buffers[0].metal = [device newBufferWithBytesNoCopy:ctx->all_data
3232
- length:size_aligned
3233
- options:MTLResourceStorageModeShared
3234
- deallocator:nil];
 
 
 
 
3235
  }
3236
 
3237
- if (ctx->all_data == NULL || ctx->buffers[0].metal == nil) {
3238
  GGML_METAL_LOG_ERROR("%s: error: failed to allocate buffer, size = %8.2f MiB\n", __func__, size_aligned / 1024.0 / 1024.0);
3239
  free(ctx);
3240
  ggml_backend_metal_free_device();
@@ -3311,14 +3315,17 @@ GGML_CALL ggml_backend_buffer_t ggml_backend_metal_buffer_from_ptr(void * data,
3311
 
3312
  // the buffer fits into the max buffer size allowed by the device
3313
  if (size_aligned <= device.maxBufferLength) {
3314
- ctx->buffers[ctx->n_buffers].data = data;
3315
- ctx->buffers[ctx->n_buffers].size = size;
 
3316
 
3317
- ctx->buffers[ctx->n_buffers].metal = [device newBufferWithBytesNoCopy:data length:size_aligned options:MTLResourceStorageModeShared deallocator:nil];
 
3318
 
3319
- if (ctx->buffers[ctx->n_buffers].metal == nil) {
3320
- GGML_METAL_LOG_ERROR("%s: error: failed to allocate buffer, size = %8.2f MiB\n", __func__, size_aligned / 1024.0 / 1024.0);
3321
- return false;
 
3322
  }
3323
 
3324
  ggml_backend_metal_log_allocated_size(device, size_aligned);
@@ -3334,14 +3341,17 @@ GGML_CALL ggml_backend_buffer_t ggml_backend_metal_buffer_from_ptr(void * data,
3334
  for (size_t i = 0; i < size; i += size_step) {
3335
  const size_t size_step_aligned = (i + size_view <= size) ? size_view : (size_aligned - i);
3336
 
3337
- ctx->buffers[ctx->n_buffers].data = (void *) ((uint8_t *) data + i);
3338
- ctx->buffers[ctx->n_buffers].size = size_step_aligned;
 
3339
 
3340
- ctx->buffers[ctx->n_buffers].metal = [device newBufferWithBytesNoCopy:(void *) ((uint8_t *) data + i) length:size_step_aligned options:MTLResourceStorageModeShared deallocator:nil];
 
3341
 
3342
- if (ctx->buffers[ctx->n_buffers].metal == nil) {
3343
- GGML_METAL_LOG_ERROR("%s: error: failed to allocate buffer, size = %8.2f MiB\n", __func__, size_step_aligned / 1024.0 / 1024.0);
3344
- return false;
 
3345
  }
3346
 
3347
  ggml_backend_metal_log_allocated_size(device, size_step_aligned);
 
3226
  ctx->n_buffers = 1;
3227
 
3228
  if (ctx->all_data != NULL) {
3229
+ ctx->buffers[0].data = ctx->all_data;
3230
+ ctx->buffers[0].size = size;
3231
+ ctx->buffers[0].metal = nil;
3232
+
3233
+ if (size_aligned > 0) {
3234
+ ctx->buffers[0].metal = [device newBufferWithBytesNoCopy:ctx->all_data
3235
+ length:size_aligned
3236
+ options:MTLResourceStorageModeShared
3237
+ deallocator:nil];
3238
+ }
3239
  }
3240
 
3241
+ if (size_aligned > 0 && (ctx->all_data == NULL || ctx->buffers[0].metal == nil)) {
3242
  GGML_METAL_LOG_ERROR("%s: error: failed to allocate buffer, size = %8.2f MiB\n", __func__, size_aligned / 1024.0 / 1024.0);
3243
  free(ctx);
3244
  ggml_backend_metal_free_device();
 
3315
 
3316
  // the buffer fits into the max buffer size allowed by the device
3317
  if (size_aligned <= device.maxBufferLength) {
3318
+ ctx->buffers[ctx->n_buffers].data = data;
3319
+ ctx->buffers[ctx->n_buffers].size = size;
3320
+ ctx->buffers[ctx->n_buffers].metal = nil;
3321
 
3322
+ if (size_aligned > 0) {
3323
+ ctx->buffers[ctx->n_buffers].metal = [device newBufferWithBytesNoCopy:data length:size_aligned options:MTLResourceStorageModeShared deallocator:nil];
3324
 
3325
+ if (ctx->buffers[ctx->n_buffers].metal == nil) {
3326
+ GGML_METAL_LOG_ERROR("%s: error: failed to allocate buffer, size = %8.2f MiB\n", __func__, size_aligned / 1024.0 / 1024.0);
3327
+ return false;
3328
+ }
3329
  }
3330
 
3331
  ggml_backend_metal_log_allocated_size(device, size_aligned);
 
3341
  for (size_t i = 0; i < size; i += size_step) {
3342
  const size_t size_step_aligned = (i + size_view <= size) ? size_view : (size_aligned - i);
3343
 
3344
+ ctx->buffers[ctx->n_buffers].data = (void *) ((uint8_t *) data + i);
3345
+ ctx->buffers[ctx->n_buffers].size = size_step_aligned;
3346
+ ctx->buffers[ctx->n_buffers].metal = nil;
3347
 
3348
+ if (size_step_aligned > 0) {
3349
+ ctx->buffers[ctx->n_buffers].metal = [device newBufferWithBytesNoCopy:(void *) ((uint8_t *) data + i) length:size_step_aligned options:MTLResourceStorageModeShared deallocator:nil];
3350
 
3351
+ if (ctx->buffers[ctx->n_buffers].metal == nil) {
3352
+ GGML_METAL_LOG_ERROR("%s: error: failed to allocate buffer, size = %8.2f MiB\n", __func__, size_step_aligned / 1024.0 / 1024.0);
3353
+ return false;
3354
+ }
3355
  }
3356
 
3357
  ggml_backend_metal_log_allocated_size(device, size_step_aligned);