Skip to content

Commit 0792375

Browse files
committed
metal : handle zero-sized allocs
1 parent 0abc6a2 commit 0792375

File tree

1 file changed

+29
-19
lines changed

1 file changed

+29
-19
lines changed

ggml/src/ggml-metal.m

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3224,15 +3224,19 @@ GGML_CALL static ggml_backend_buffer_t ggml_backend_metal_buffer_type_alloc_buff
32243224
ctx->n_buffers = 1;
32253225

32263226
if (ctx->all_data != NULL) {
3227-
ctx->buffers[0].data = ctx->all_data;
3228-
ctx->buffers[0].size = size;
3229-
ctx->buffers[0].metal = [device newBufferWithBytesNoCopy:ctx->all_data
3230-
length:size_aligned
3231-
options:MTLResourceStorageModeShared
3232-
deallocator:nil];
3227+
ctx->buffers[0].data = ctx->all_data;
3228+
ctx->buffers[0].size = size;
3229+
ctx->buffers[0].metal = nil;
3230+
3231+
if (size_aligned > 0) {
3232+
ctx->buffers[0].metal = [device newBufferWithBytesNoCopy:ctx->all_data
3233+
length:size_aligned
3234+
options:MTLResourceStorageModeShared
3235+
deallocator:nil];
3236+
}
32333237
}
32343238

3235-
if (ctx->all_data == NULL || ctx->buffers[0].metal == nil) {
3239+
if (size_aligned > 0 && (ctx->all_data == NULL || ctx->buffers[0].metal == nil)) {
32363240
GGML_METAL_LOG_ERROR("%s: error: failed to allocate buffer, size = %8.2f MiB\n", __func__, size_aligned / 1024.0 / 1024.0);
32373241
free(ctx);
32383242
ggml_backend_metal_free_device();
@@ -3309,14 +3313,17 @@ GGML_CALL ggml_backend_buffer_t ggml_backend_metal_buffer_from_ptr(void * data,
33093313

33103314
// the buffer fits into the max buffer size allowed by the device
33113315
if (size_aligned <= device.maxBufferLength) {
3312-
ctx->buffers[ctx->n_buffers].data = data;
3313-
ctx->buffers[ctx->n_buffers].size = size;
3316+
ctx->buffers[ctx->n_buffers].data = data;
3317+
ctx->buffers[ctx->n_buffers].size = size;
3318+
ctx->buffers[ctx->n_buffers].metal = nil;
33143319

3315-
ctx->buffers[ctx->n_buffers].metal = [device newBufferWithBytesNoCopy:data length:size_aligned options:MTLResourceStorageModeShared deallocator:nil];
3320+
if (size_aligned > 0) {
3321+
ctx->buffers[ctx->n_buffers].metal = [device newBufferWithBytesNoCopy:data length:size_aligned options:MTLResourceStorageModeShared deallocator:nil];
33163322

3317-
if (ctx->buffers[ctx->n_buffers].metal == nil) {
3318-
GGML_METAL_LOG_ERROR("%s: error: failed to allocate buffer, size = %8.2f MiB\n", __func__, size_aligned / 1024.0 / 1024.0);
3319-
return false;
3323+
if (ctx->buffers[ctx->n_buffers].metal == nil) {
3324+
GGML_METAL_LOG_ERROR("%s: error: failed to allocate buffer, size = %8.2f MiB\n", __func__, size_aligned / 1024.0 / 1024.0);
3325+
return false;
3326+
}
33203327
}
33213328

33223329
ggml_backend_metal_log_allocated_size(device, size_aligned);
@@ -3332,14 +3339,17 @@ GGML_CALL ggml_backend_buffer_t ggml_backend_metal_buffer_from_ptr(void * data,
33323339
for (size_t i = 0; i < size; i += size_step) {
33333340
const size_t size_step_aligned = (i + size_view <= size) ? size_view : (size_aligned - i);
33343341

3335-
ctx->buffers[ctx->n_buffers].data = (void *) ((uint8_t *) data + i);
3336-
ctx->buffers[ctx->n_buffers].size = size_step_aligned;
3342+
ctx->buffers[ctx->n_buffers].data = (void *) ((uint8_t *) data + i);
3343+
ctx->buffers[ctx->n_buffers].size = size_step_aligned;
3344+
ctx->buffers[ctx->n_buffers].metal = nil;
33373345

3338-
ctx->buffers[ctx->n_buffers].metal = [device newBufferWithBytesNoCopy:(void *) ((uint8_t *) data + i) length:size_step_aligned options:MTLResourceStorageModeShared deallocator:nil];
3346+
if (size_step_aligned > 0) {
3347+
ctx->buffers[ctx->n_buffers].metal = [device newBufferWithBytesNoCopy:(void *) ((uint8_t *) data + i) length:size_step_aligned options:MTLResourceStorageModeShared deallocator:nil];
33393348

3340-
if (ctx->buffers[ctx->n_buffers].metal == nil) {
3341-
GGML_METAL_LOG_ERROR("%s: error: failed to allocate buffer, size = %8.2f MiB\n", __func__, size_step_aligned / 1024.0 / 1024.0);
3342-
return false;
3349+
if (ctx->buffers[ctx->n_buffers].metal == nil) {
3350+
GGML_METAL_LOG_ERROR("%s: error: failed to allocate buffer, size = %8.2f MiB\n", __func__, size_step_aligned / 1024.0 / 1024.0);
3351+
return false;
3352+
}
33433353
}
33443354

33453355
ggml_backend_metal_log_allocated_size(device, size_step_aligned);

0 commit comments

Comments
 (0)