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