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